#!/bin/sh
#
#    Broadcom Proprietary and Confidential.
#    Copyright © 1996-2020 Broadcom.
#    All Rights Reserved.
#    The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
#
#    static CCM postplay script to resolve FOS configuration compability issues from firmware upgrade
#    /etc/fabos/config/scripts/postplay
#

PROGRAM='postplay'
cfgutil="/etc/fabos/config/scripts/cfgutil"

TTY=$(tty)
if [ "$TTY" = "not a tty" ]
then
    TTY="/dev/null"
fi

PATH=/fabos/link_bin:/bin:/usr/bin:/sbin:/usr/sbin:/fabos/link_abin:/fabos/link_sbin:/fabos/link_rbin:/fabos/factory:/fabos/xtool

#export CHASSIS_ROLEID=0
#export ROLE_ID=root
#export HOME=/root
#export LOGNAME=root
#export PWD=/root
#export LOGIN_ID=root

#
#include cfg utility
#
if [ -e "$cfgutil" ]
then
    . $cfgutil
    rc=$?
    if [ $rc -ne 0 ]
    then
      echo "Error -- $cfgutil err=$rc"
      exit 3
    fi
else
    echo "Error -- could not locate configuration common library $cfgutil"
    exit 3
fi

# not defined yet
if [ "$TO_VERSION" == "" ]; then
  CCM_VERSION="/etc/fabos/config/scripts/ccm_version.conf"

  if [ -f "$CCM_VERSION" ]; then
    TO_MAJOR=`cat $CCM_VERSION  | grep "TO" | /bin/awk '{print $2}'`
    TO_MINOR=`cat $CCM_VERSION | grep "TO" | /bin/awk '{print $3}'`
    TO_PATCH=`cat $CCM_VERSION | grep "TO" | /bin/awk '{print $4}'`
    TO_RELEASE=`cat  $CCM_VERSION | grep "TO" | /bin/awk '{print $5}'`

    FROM_MAJOR=`cat $CCM_VERSION | grep "FROM" | /bin/awk '{print $2}'`
    FROM_MINOR=`cat $CCM_VERSION | grep "FROM" | /bin/awk '{print $3}'`
    FROM_PATCH=`cat $CCM_VERSION | grep "FROM" | /bin/awk '{print $4}'`
    FROM_RELEASE=`cat $CCM_VERSION | grep "FROM" | /bin/awk '{print $5}'`
    /bin/chmod 644 $CCM_VERSION
  fi

  TO_VERSION="$TO_MAJOR$TO_MINOR$TO_PATCH$TO_RELEASE"
  FROM_VERSION="$FROM_MAJOR$FROM_MINOR$FROM_PATCH$FROM_RELEASE"
  /bin/chmod 644 $CCM_VERSION
fi

ntp_authentication_handling() {
	
	#Upgrade handling
	#Add new config keys: ts.clockServerAuthSpec and ts.clockServerLegacyMode if NTP auth supported
	authspec_mode=`/fabos/cliexec/config get ts.clockServerAuthSpec 2`
	legacy_mode=`/fabos/cliexec/config get ts.clockServerLegacyMode 2`
	if [ -z "$authspec_mode" ]; then
		#if empty, add config with default values
		cfgkey="ts.clockServerAuthSpec"
    	cfgkey_type=2
    	cfgkey_value=0
		$SWITCHCMD -1 /fabos/cliexec/config set $cfgkey $cfgkey_type $cfgkey_value
	fi
	if [ -z "$legacy_mode" ]; then
		#if empty, add config with default values
		cfgkey="ts.clockServerLegacyMode"
    	cfgkey_type=2
    	cfgkey_value=1
		$SWITCHCMD -1 /fabos/cliexec/config set $cfgkey $cfgkey_type $cfgkey_value
	fi
}

root_account_disable_handling() {
	
	PASSWD_FILE="/etc/passwd"
	MNT_PASSWD_FILE="/mnt/etc/passwd"
	TMP_PASSWD="/tmp/passwd"
	SHADOW_FILE="/etc/shadow"
	MNT_SHADOW_FILE="/mnt/etc/shadow"
	TMP_SHADOW="/tmp/shadow"
	
	# Upgrade to 9.1 handling
	# Convert /etc/passwd root entry to /bin/false shell
	/bin/sed 's+root:/root:/bin/sh+root:/root:/bin/false+g' $PASSWD_FILE > $TMP_PASSWD
	/bin/mv $TMP_PASSWD $PASSWD_FILE
	/bin/chmod 600 $PASSWD_FILE
	/bin/sed 's+root:/root:/bin/sh+root:/root:/bin/false+g' $MNT_PASSWD_FILE > $TMP_PASSWD
	/bin/mv $TMP_PASSWD $MNT_PASSWD_FILE
	/bin/chmod 600 $MNT_PASSWD_FILE
	
	# Disable root account in 9.1
	/bin/awk -F: '{ if ($1 == "root") $8="0";}1' OFS=: $SHADOW_FILE > $TMP_SHADOW
	/bin/mv $TMP_SHADOW $SHADOW_FILE
	/bin/chmod 600 $SHADOW_FILE
	/bin/awk -F: '{ if ($1 == "root") $8="0";}1' OFS=: $MNT_SHADOW_FILE > $TMP_SHADOW
	/bin/mv $TMP_SHADOW $MNT_SHADOW_FILE
	/bin/chmod 600 $MNT_SHADOW_FILE
	
	# Change root account password to new default in shadow for 9.1
	/bin/awk -F: '{ if ($1 == "root") $2="1804289383INVALIDACCOUNT1714636915";}1' OFS=: $SHADOW_FILE > $TMP_SHADOW
	/bin/mv $TMP_SHADOW $SHADOW_FILE
	/bin/chmod 600 $SHADOW_FILE
	/bin/awk -F: '{ if ($1 == "root") $2="1804289383INVALIDACCOUNT1714636915";}1' OFS=:  $MNT_SHADOW_FILE > $TMP_SHADOW
	/bin/mv $TMP_SHADOW $MNT_SHADOW_FILE
	/bin/chmod 600 $MNT_SHADOW_FILE	 
}



#MAIN
# Get the command-line options
$CFGLOGD "$PROGRAM $@"
$CFGLOG "CURRENT_VF=$CURRENT_VF FABOS_SWITCHNO=$FABOS_SWITCHNO"

#setcontext chassis
#value=`/fabos/cliexec/config get cfgut.debug 2`
#if [ ! -z ${value+x} ]; then
#    CFGCMD="cfgcmd"
#fi  

do_910 ()
{
  setcontext chassis  
  # if unit test is enabled
  cfgut_enable=`/fabos/cliexec/config get cfgut.enable 2`
  if [ $cfgut_enable ]; then

    # add 9.1 new chassis config key
    # config set 9.1 chasis key
    cfgkey="cfgut.CH.9.1key.1"
    cfgkey_type=2
    cfgkey_value=1
    $CHASSISCMD /fabos/cliexec/config set $cfgkey $cfgkey_type $cfgkey_value

    # add 9.1 new LS config key
    # config set 9.1 LS key
    cfgkey="cfgut.SW.9.1key.4"
    cfgkey_type=2
    cfgkey_value=4
    $SWITCHCMD -1 /fabos/cliexec/config set $cfgkey $cfgkey_type $cfgkey_value
    
    setcontext chassis  
    # change 9.0.1 config key value
    cfgkey="cfgut.CH.9.0key.4"
    cfgkey_type=2
    evalue=8
    value=`/fabos/cliexec/config get $cfgkey $cfgkey_type`
    if [ ! -z ${value+x} ]; then
	if [[ "$value" < "$evalue" ]]; then
	    $CFGLOG "set $cfgkey to $evalue"
	    /fabos/cliexec/config set $cfgkey $cfgkey_type $evalue
	fi
    else
	$ECHO "postplay fail to get $cfgkey to $evalue"
    fi

    setcontext 128
    # add syslog ip as 9.1 default
    $CFGCMD syslogadmin --set -ip 1.2.3.4

    # change 9.1 syslog faciity default value
    $CFGCMD syslogadmin --set -facility 5

    # file operation

  fi

  # add 9.1 ntp auth config keys
  ntp_authentication_handling

  # Disable root account in 9.1
  root_account_disable_handling
}

$CFGLOG "FWDL $FROM_VERSION to $TO_VERSION"

# upgrade from pre-9.1 to 9.1.x
if [[ "$FROM_VERSION" < "910" ]]; then
    if [[ "$TO_VERSION" > "910" ]] || [[ "$TO_VERSION" == "910" ]]; then
      # build 901a sring to ivoke do_910 if from < 910
      do_910
    fi
fi

$CFGLOGD "$PROGRAM Exit"
exit 0
