#!/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 restore script to restore FOS configuration for firmware downgrade
#    and firmwarerestore CLI.
#    /etc/fabos/config/scripts/restore
#

PROGRAM='restore'
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

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

ntp_authentication_handling() {
	
	POSTINST="/sbin/postinst"		  # This is the postinst of new firmware

	#Downgrade handling
	#Reset ts.clockServerIndex and ts.clockServerIndexList to default values	
	cfgkey="ts.clockServerIndex"
 	cfgkey_type=5
 	cfgkey_value="-1"
 	$SWITCHCMD -1 /fabos/cliexec/config set $cfgkey $cfgkey_type $cfgkey_value
    	
	cfgkey="ts.clockServerIndexList"
	cfgkey_type=5
	cfgkey_value="-1"
	$SWITCHCMD -1 /fabos/cliexec/config set $cfgkey $cfgkey_type $cfgkey_value
}

root_account_enable_handling() {
	
	POSTINST="/sbin/postinst"		  # This is the postinst of new firmware
	new_fw_is_SV=$(/bin/grep -c "ROOT_ACCOUNT_REMOVAL_SUPPORT" $POSTINST) # grep in postinst of New fw

	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"
	NTPKEYS_FILE="/etc/ntp.keys"
	MNT_NTPKEYS_FILE="/mnt/etc/ntp.keys"
	
	#Downgrade handling
	#Add config keys: passwdcfg.oldpasswd and root.access
	if [ $new_fw_is_SV -eq 0 ]; then

		pwcfg_oldpass=`/fabos/cliexec/config get passwdcfg.oldpasswd 2`
		if [ -z "$pwcfg_oldpass" ]; then
			#if empty, add config back
			cfgkey="passwdcfg.oldpasswd"
    			cfgkey_type=2
    			cfgkey_value=0
			$CHASSISCMD /fabos/cliexec/config set $cfgkey $cfgkey_type $cfgkey_value
		fi
		root_access=`/fabos/cliexec/config get root.access 5`
		if [ -z "$root_access" ]; then
			#if empty, add config back
			cfgkey="root.access"
    			cfgkey_type=5
    			cfgkey_value="consoleonly"
			$CHASSISCMD /fabos/cliexec/config set $cfgkey $cfgkey_type $cfgkey_value
		fi
		$CHASSISCMD /fabos/cliexec/config update
		
		#Convert /etc/passwd root entry to /bin/sh shell
		/bin/sed 's+root:/root:/bin/false+root:/root:/bin/sh+g' $PASSWD_FILE > $TMP_PASSWD
		/bin/mv $TMP_PASSWD $PASSWD_FILE
		/bin/chmod 600 $PASSWD_FILE
		/bin/sed 's+root:/root:/bin/false+root:/root:/bin/sh+g' $MNT_PASSWD_FILE > $TMP_PASSWD
		/bin/mv $TMP_PASSWD $MNT_PASSWD_FILE
		/bin/chmod 600 $MNT_PASSWD_FILE	
		
		# Change root account password to old default in shadow for pre-9.1
		/bin/awk -F: '{ if ($1 == "root") $2="$6$xk/VD29GLswzCqlC$1o6RtIdi5qwTcXCLPRuh2x3JlOEa8sjSXmtAIjdIy1tTvxCdO3Yq7Kym9lf.V3remXI7nfXN.DpwaoYxlJy2R1";}1' OFS=: $SHADOW_FILE > $TMP_SHADOW
		/bin/mv $TMP_SHADOW $SHADOW_FILE
		/bin/chmod 600 $SHADOW_FILE
		/bin/awk -F: '{ if ($1 == "root") $2="$6$xk/VD29GLswzCqlC$1o6RtIdi5qwTcXCLPRuh2x3JlOEa8sjSXmtAIjdIy1tTvxCdO3Yq7Kym9lf.V3remXI7nfXN.DpwaoYxlJy2R1";}1' OFS=:  $MNT_SHADOW_FILE > $TMP_SHADOW
		/bin/mv $TMP_SHADOW $MNT_SHADOW_FILE
		/bin/chmod 600 $MNT_SHADOW_FILE	
		
		# Remove /etc/ntp.keys
		/bin/rm -f $NTPKEYS_FILE
		/bin/rm -f $MNT_NTPKEYS_FILE
	fi
}


maintenance_working_directory_deletion() {
	POSTINST="/sbin/postinst"		  # This is the postinst of new firmware
	MAINT_WORKING_DIR="/fabos/users/maintenance/maintwd"
	new_fw_is_SV=$(/bin/grep -c "MAINTENANCE_WORKING_DIR_REMOVAL_SUPPORT" $POSTINST) # grep in postinst of New fw

	#Downgrade handling
	#Remove maintenance working directory
	if [ $new_fw_is_SV -eq 0 ]; then
		if [ -d "$MAINT_WORKING_DIR" ]; then
			/bin/rm -rf $MAINT_WORKING_DIR
		fi
	fi
}


#MAIN
# Get the command-line options

#$CFGLOG_INIT
$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_restore ()
{
  setcontext 128
  # if unit test is enabled
  cfgut_enable=`/fabos/cliexec/config get cfgut.enable 2`

  #if [ -f "$cfgutf" ]; then
  #if [ ! -z ${cfgut_enable+x} ] && [ ! -z "$cfgut_enable" ]; then

  if [ "$cfgut_enable" == "1" ]; then

    # remove 9.1 default syslog ip
    $CFGCMD syslogadmin --remove -ip 1.2.3.4

    # change 9.1 syslog faciity to 9.0.1 default value
    $CFGCMD syslogadmin --set -facility 4
    # file operation
  else
     $CFGLOG "${FUNCNAME[0]} none CFGUT"  
  fi
  
  # remove 9.1 ntp auth config keys
  ntp_authentication_handling

  # Enable root account in pre-9.1
  root_account_enable_handling

  #Remove maintenance working directory in pre-9.1.1
  maintenance_working_directory_deletion
}

$CFGLOG "FWDL $FROM_VERSION to $TO_VERSION"
# downgrade from 9.1.x to pre-9.1.0 but after 9.0.1a
if [[ "$FROM_VERSION" == "910" ]] || [[ "$FROM_VERSION" > "910" ]] && [[ "$TO_VERSION" < "910" ]]; then
  if [[ "$TO_VERSION" == "901a" ]] || [[ "$TO_VERSION" > "901a" ]]; then
   # build 901a sring to ivoke do_910 if from < 910
    do_910_restore
  fi
fi

$CFGLOGD "$PROGRAM Exit"

exit 0
