#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin:$PATH
export LOGIN_ID="root"
export ROLE_ID="root"
export TERMINFO="/usr/share/terminfo"
export CURRENT_AD="0"
export SWLOGNAME="root"
export AUTH_TYPE="0"

isactive=$[0x`/bin/awk '/State:/ { print $4  }' /proc/hasm/ham | /bin/sed -e 's/,//g'`%2]

# check in FIPS mode or not
if [ $isactive -eq 1 ]; then
	FIPSCFG=/tmp/fipscfg
	/fabos/link_abin/fipscfg --showall > $FIPSCFG
	/bin/grep -iq "FIPS Selftests: Enabled" $FIPSCFG && /bin/grep -iq "Root account: Disabled" $FIPSCFG && echo "oscmd NOT available in FIPS mode" && exit 3
fi

CFGCMD='/fabos/cliexec/config'
setconfig()
{
	#status=('disable' 'enable')
	status=('0' '1')
	cfgupdate=0
	val=$[$2]
	case $1 in
	ipaddr)
		ipcmd=$2
		ipaddr=$3
		[ "$ipcmd" = "-4" -o "$ipcmd" = "-6" ] && ipopt=$2 && ipcmd=$3 && ipaddr=$4
		if [ "$ipcmd" = "add" -o "$ipcmd" = "del" -o "$ipcmd" = "show" ]; then
			if [ "$ipcmd" = "show" ]; then
				/sbin/ip addr show dev eth0
			elif [ -n "$ipaddr" ]; then
				/sbin/ip addr $ipcmd $ipaddr dev eth0
				echo ip $ipopt addr $ipcmd $ipaddr dev eth0
			else
				echo \"ip addr $ipcmd\" missing ip address
			fi
		else
			echo \"$@\" is an invalid command.
		fi
		;;
	ipneigh)
		[ "$2" = "-4" -o "$2" = "-6" ] && ipopt=$2
		[ "$2" = flush -o "$3" = flush ] && flush="flush dev eth0"
		/sbin/ip $ipopt neigh $flush
		;;
	dadaccept)
		if [ -n "$2" ]; then
			if [ $isactive -eq 0 ]; then
				echo The command is not available in standby MM
				return
			else
				data=(1 dadaccept $val)
				echo $val > /proc/sys/net/ipv6/conf/eth0/accept_dad
				echo -n "set "
			fi
		fi
		echo /proc/sys/net/ipv6/conf/eth0/accept_dad = $[`cat /proc/sys/net/ipv6/conf/eth0/accept_dad`]
		;;
	dadretransmit)
		if [ -n "$2" ]; then
			if [ $isactive -eq 0 ]; then
				echo The command is not available in standby MM
				return
			else
				echo $@ | /bin/grep -q not-persist
				if [ $? -ne 0 ]; then
					data=(1 dadretransmit $val)
				fi
				echo $val > /proc/sys/net/ipv6/conf/eth0/dad_transmits
				echo -n "set "
			fi
		fi
		echo /proc/sys/net/ipv6/conf/eth0/dad_transmits = $[`cat /proc/sys/net/ipv6/conf/eth0/dad_transmits`]
		;;
	dadns)
		if [ -n "$2" ]; then
			if [ $isactive -eq 0 ]; then
				echo The command is not available in standby MM
				return
			else
				echo $@ | /bin/grep -q not-persist
				if [ $? -ne 0 ]; then
					data=(1 dadns $val)
				fi
				echo $[${2##-}] > /proc/sys/net/ipv6/neigh/eth0/app_solicit
				echo -n "set "
			fi
		fi
		echo /proc/sys/net/ipv6/neigh/eth0/app_solicit = $[`cat /proc/sys/net/ipv6/neigh/eth0/app_solicit`]
		;;
	ipv6)
		if [ -n "$2" ]; then
			if [ $isactive -eq 0 ]; then
				echo The command is not available in standby MM
				return
			else
				val=$[($val+1)%2]
				data=(1 ipv6 $val)
				echo $val > /proc/sys/net/ipv6/conf/eth0/disable_ipv6
				echo -n "set "
			fi
		fi
		echo /proc/sys/net/ipv6/conf/eth0/disable_ipv6 = $[`cat /proc/sys/net/ipv6/conf/eth0/disable_ipv6`]
		;;
	firewall)
		if [ -n "$2" ]; then
			if [ $isactive -eq 0 ]; then
				echo The command is not available in standby MM
				return
			else
				data=(1 firewall $val)
				echo rebooted switch to take effect
				echo -n "set "
			fi
		fi
		echo firewall setting = $val
		;;
	ipv6mtu)
		if [ -n "$2" ]; then
			if [ $isactive -eq 0 ]; then
				echo The command is not available in standby MM
				return
			else
				echo $@ | /bin/grep -q not-persist
				if [ $? -ne 0 ]; then
					data=(1 ipv6mtu $val)
				fi
				echo $[${2##-}] > /proc/sys/net/ipv6/conf/eth0/mtu
				echo -n "set "
			fi
		fi
		echo /proc/sys/net/ipv6/conf/eth0/mtu = $[`cat /proc/sys/net/ipv6/conf/eth0/mtu`]
		;;
	removejitcconfig)
		if [ $isactive -eq 0 ]; then
			echo The command is not available in standby MM
			return
		else
			chassistype=`getchassisconfig | awk '/Chassis based system:/ { if ($NF="Yes") {print chassis} else {print switch} }'`
			echo "Warning: This operation will cause the $chassistype to reboot and" 
			echo "requires all existing telnet, secure telnet and SSH sessions to be restarted."
			while [ ${#y} -eq 0 ]; do
				echo -n "Are you sure you want to reboot the $chassistype [y/n]? "
				read y
			done
			if [ "$y" = y -o "$y" = Y ]; then
				data=(1 removejitcconfig $val)
			fi
		fi
		;;
	*)
		return 1
		;;
	esac
	if [ ${#data[@]} -gt 0 ]; then
		jitcparam=/etc/fabos/jitcparam
		echo ${data[@]} > $jitcparam && sync
		/fabos/bin/jitccfg.sh $jitcparam no-op
		/fabos/cliexec/config save $jitcparam
		if [ ${data[1]} = "removejitcconfig" ]; then
			rm /etc/fabos/jitc.conf /mnt/etc/fabos/jitc.conf $jitcparam /mnt$jitcparam
			sync; sleep 1; /fabos/cliexec/reboot -f -s -r ChassisReboot
		fi 
	else
		($CFGCMD update || echo "$CFGCMD update fail")
	fi
	return 0
}

###############################################################################
#Add the Linux command to be allowed and the list of options (as needed by getopt) 
#allowed for the command to the end of CMD_LIST and OPTION_LIST respectively. 
#Once they are added these commands and options will be  allowed from NOSCLI prompt.

CMD_LIST=( 'ls' 'arp' 'cat' 'ifconfig' 'tcpdump' 'rmdir' 'mv' 'rm' 'mkdir' 'cp' 'ipaddr' 'ipneigh' 'dadaccept' 'dadretransmit' 'dadns' 'ipv6' 'firewall' 'ipv6mtu' 'removejitcconfig') 
# Add new command to the end of the list 

OPTION_LIST=( 'al' 'a' ':' ':netmask:up' ':AbdDefIKlLnNOpqRStuUvxXB:c:C:G:F:i:m:M:r:s:T:w:W:E:y:z:Z:' ':' 'i' 'rf' ':' 'rfilLPpRsuvx' 'inegvd' '46' '46' '01' 'n' '01' '01' '01' 'n' 'n')
#Add the allowed options for the new command  at the end of the list 
######################################################################################
	 
if [ -z $1 ]
then
	echo "No Command Provided"
	exit 1
elif [ -n $1 ]
then
cmd=$1
#echo $cmd
fi


#Parsing command options
shift 
options=""
cmd_idx=0
for c in "${CMD_LIST[@]}"
do
	#echo "array cmd $c"
	if [ $cmd == $c ]
	then
		options=${OPTION_LIST[cmd_idx]}
		#echo "array cmd $c array option $options"
	else
		cmd_idx=$[$cmd_idx + 1]
	fi
done
#echo "final $options"

if [ -z $options  ]
then
 	echo "Command $cmd is not Permitted"
	exit 1
else
	filtered_options=`getopt $options "$@"`
	[ $? -ne 0 ] && echo "Valid options are [-$options]" && exit 3
fi

# current path is home directory
homedir=`pwd`

# get login user name
username=`basename $homedir`

# to remove last option --
for word in $filtered_options
do
	if [ $word != "--" ]
	then
		# if not root and file/directory exist
		if [ $username != "root" -a -e $word ]
		then
			# resolve absolute path
			adir=$word
			[ -d $adir ] || adir=`dirname $adir`
			adir=`cd $adir; pwd`

			# check whether path prefix with home directory
			if [ ${adir##$homedir/} = $adir ]
			then
				echo Directory $adir is not accessible !
				exit 2
			fi
		fi
	    cmd_options="$cmd_options $word"
	fi
done
#Done parsing and filtering command options

setconfig $cmd $cmd_options
[ $? -eq 0 ] && exit 0

#Now executing command 
#echo $cmd_options
sh -c "$cmd $cmd_options"
