#!/bin/bash
#
#    Copyright (c) 1996-2007 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#      Initialization script to execute the KATs on system reboots
#      KAT failure will cause a system reboot

#
# Ignore Ctrl-C(Interrupt), Ctrl-Z, Ctrl-/ signal.
#
trap '' SIGINT SIGQUIT SIGTSTP

#
# Set a signal handler for the script.
#
trap sig_handler SIGBUS

#
# Signal handler for this script.
#
sig_handler()
{
	echo "**********************************************************"
	echo "$0: RECEIVED UNEXPECTED SIGNAL. PROCESS INFORMATION:"
	echo "**********************************************************"
	ps auxw
	exit 1
}

DEBUG=0

#
# Set the Selftests states
#
STESTS_DEFAULT=0
STESTS_SET_NK=1  
STESTS_KF=2
STESTS_SET=3 
STESTS_RESET_NR=4 
rc=0

#
# Environment variables for the fipscfg command to work
#
export ROLE_ID=root
export CURRENT_VF=255
export PATH="$PATH:/fabos/abin"

FIPSCFG_PATH="/fabos/abin"
FIPSCFG="fipscfg"
FIPSCFGFILE="/etc/fabos/fips_selftests.conf"

#
# API to set the selftests states
#
setFipsSelfTests()
{
	if [ "$DEBUG" -eq 1 ] 
	then
		set -x
	fi

	if [ -w $FIPSCFGFILE ]
	then
		echo $* >$FIPSCFGFILE
		rc=$?
	else
		rc=-1	
	fi

	if [ $rc -ne 0 ]
	then
		echo -e "\n$0: Error in Setting FIPS Selftests mode"
	fi

	return $rc

}

# 
# Plugin to check the KAT failure path
#
checkKatFail()
{
	if [ "$DEBUG" -eq 1 ] 
	then
	  set -x
	fi

  KATFAILFILE="/katfail"
  if [ -f "$KATFAILFILE" ] 
  then
	  return 1
  fi
  return 0

}

# 
# Reboot the system
#
sysReboot()
{
	if [ "$DEBUG" -eq 1 ] 
	then
		set -x
	fi

	echo -e "\n$0: Rebooting the system due to KAT failure"
	echo "Please contact the system administrator to correct this state"

	sleep 5

	if [ "$DEBUG" -eq 1 ]
	then
		echo -e "\nShould the system be rebooted ? [yes/no]:"
		read YN
		if [ "$YN" != "yes" ]
		then
			echo "Aborting reboot on User Request"
			exit 0
		fi
	fi

	/usr/bin/reboot
  
}

# Main starts here
if [ "$DEBUG" -eq 1 ] 
then
	set -x
fi

if [ ! -x ${FIPSCFG_PATH}/${FIPSCFG} ]
then
	echo "${FIPSCFG} does not exist on the system"
	# Not rebooting the system here - Self tests mode may not be set
	exit 0
fi

if [ -r $FIPSCFGFILE ]
then 
	read steststatus <$FIPSCFGFILE
	rc=$steststatus
else
	#echo "Error getting FIPS selftest status - $FIPSCFGFILE does not exist or not readable."
	exit 0
fi

# TR000345864: Resetting back if previous KAT was failed
if [ -f "/katfail" ]
then
	/bin/rm -f /katfail 2>/dev/null
fi

if [ $rc -lt 0 ]
then
	echo -e "\n$0: Error in retrieving fips self tests mode"
else
	case $rc in 
		($STESTS_DEFAULT) :
			echo "$0: Selftests mode is not set" 
			rc=0
			;;
		($STESTS_KF|$STESTS_SET|$STESTS_SET_NK) :
			${FIPSCFG} --KAT
			rc=$?
			checkKatFail
			if [ $? -ne 0 -o $rc -ne 0 ]
			then
				setFipsSelfTests $STESTS_KF 
				if [ $? -eq 0 ] 
				then
					sysReboot
				else
					echo -e "Aborting reboot .. \n"
				fi
			else
				setFipsSelfTests $STESTS_SET 
			fi
			rc=$?
			;;
		(*): 
			echo "$0: Unknown state for selftests: $rc"
			;;
	esac 
fi

exit $rc

