#!/bin/sh
#
#    Copyright (c) 1996-2002 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Initialization script to start/stop network services based on
#    chassis configuration.

# Commands
GREP=/bin/grep
SED=/bin/sed
CAT=/bin/cat
CUT=/usr/bin/cut
CP=/bin/cp
MV=/bin/mv
PS=/bin/ps
NETSTAT=/bin/netstat
KILL=/bin/kill
KILLALL=/usr/bin/killall
INETD_PROC=/usr/sbin/inetd

RESTART_INETD="0"
DEBUG=0
if [ "$DEBUG" == 1 ]; then
    ECHO='echo -e'
else
    ECHO=:
fi
# No arguments, then show Telnet status
#if [ $# == 0 ]; then
	CHASSIS_FILE="/etc/fabos/fabos.chassis.conf"
#else
#	CHASSIS_FILE=$1
#fi

# Only accept 1 argument
#if [ $# > 1 ]; then
#  echo Too many arguments
#  echo Usage: $0 [config file]
#  exit 1
#fi


# Does file exist?
if [ ! -r $CHASSIS_FILE ]; then
	echo File does not exist: $CHASSIS_FILE
	exit 0;
fi

if [ "$DEBUG" == 1 ]; then
	size=`/usr/bin/wc -c < /var/log/inetdDebug.log`
    if [ "$size" -ge 50000 ]; then
		$MV /var/log/inetdDebug.log /var/log/inetdDebug.log.1
	fi
	$ECHO "start date `date`" >> /var/log/inetdDebug.log
	$CAT /etc/inetd.conf >> /var/log/inetdDebug.log
fi
# Which services should be enabled based on chassis configuration?
SHALL_TELNET_ENABLE=1
SHALL_RSTAT_ENABLE=`$GREP "^rpc.rstatd:" $CHASSIS_FILE | $CUT -d':' -f2`
SHALL_RUSERS_ENABLE=`$GREP "^rpc.rusersd:" $CHASSIS_FILE | $CUT -d':' -f2`
LOOPBACK_ADDR=127.0.0.1
# Edit /etc/inetd.conf, looking for lines which refer to the first parameter
# and update them according to the second parameter
update_inetd_conf(){
	umask 022
	WS=$'[ \t]'   		# WhiteSpace
	ARG=${1//\//\\/}	# Replace / with \/ in $1 and assign to ARG
	# if the service (telnet or rstat or rusers) is ON (uncommented)
	# AND we want it off ($2 == 0) then comment it out.
	if $GREP "^$1${WS}" /etc/inetd.conf >/dev/null ; then
		if [ "$2" == 0 ] ; then
			$SED -e "s/^$ARG${WS}/#&/" /etc/inetd.conf > /etc/inetd.conf.tmp &&
			$MV -f /etc/inetd.conf.tmp /etc/inetd.conf
			$SED -e "s/^$ARG${WS}/#&/" /mnt/etc/inetd.conf > /mnt/etc/inetd.conf.tmp &&
			$MV -f /mnt/etc/inetd.conf.tmp /mnt/etc/inetd.conf
			RESTART_INETD=1
		fi
	else
		if [ "$2" == 1 ] ; then
			$SED -e "/^#$ARG${WS}/s/#//" /etc/inetd.conf > /etc/inetd.conf.tmp &&
			$MV -f /etc/inetd.conf.tmp /etc/inetd.conf
			$SED -e "/^#$ARG${WS}/s/#//" /mnt/etc/inetd.conf > /mnt/etc/inetd.conf.tmp &&
			$MV -f /mnt/etc/inetd.conf.tmp /mnt/etc/inetd.conf
			RESTART_INETD=1
		fi
	fi
}

update_inetd_conf_loopback_internal(){
	umask 022
	WS=$'[ \t]'         # WhiteSpace
	ARG=${1//\//\\/}    # Replace / with \/ in $1 and assign to ARG
	ETH1_ADDR=`/sbin/ifconfig | /fabos/link_bin/grep "eth1" -A 1 | \
        /fabos/link_bin/grep "inet addr" | /bin/sed -e 's/inet addr://g' | \
        /usr/bin/cut -d B -f 1 | /bin/sed -e 's/ //g'`
	ETH2_ADDR=`/sbin/ifconfig | /fabos/link_bin/grep "eth2" -A 1 | \
        /fabos/link_bin/grep "inet addr" | /bin/sed -e 's/inet addr://g' | \
        /usr/bin/cut -d B -f 1 | /bin/sed -e 's/ //g'`
	$ECHO "ETH1_ADDR $ETH1_ADDR" >> /var/log/inetdDebug.log
	$ECHO "ETH2_ADDR $ETH2_ADDR" >> /var/log/inetdDebug.log
	shouldMove=0
	# replace the "service name" with "service node:service name".
	# if $GREP "^$1${WS}" /etc/inetd.conf >/dev/null ; then
		if [ "$ETH1_ADDR" != "" ] && [ "$ETH2_ADDR" != "" ]; then
			$GREP "$LOOPBACK_ADDR,$ETH1_ADDR,$ETH2_ADDR:$ARG" /etc/inetd.conf >/dev/null
			if [ $? -ne 0 ]; then
				$SED -e "s/[^ ]*$ARG/$LOOPBACK_ADDR,$ETH1_ADDR,$ETH2_ADDR:$ARG/" /etc/inetd.conf > /etc/inetd.conf.tmp &&
				shouldMove=1;
			fi
		elif [ "$ETH1_ADDR" != "" ]; then
			$GREP "$LOOPBACK_ADDR,$ETH1_ADDR:$ARG" /etc/inetd.conf >/dev/null
			if [ $? -ne 0 ]; then
				$SED -e "s/[^ ]*$ARG/$LOOPBACK_ADDR,$ETH1_ADDR:$ARG/" /etc/inetd.conf > /etc/inetd.conf.tmp &&
				shouldMove=1
			fi
		elif [ "$ETH2_ADDR" != "" ]; then
			$GREP "$LOOPBACK_ADDR,$ETH2_ADDR:$ARG" /etc/inetd.conf >/dev/null
			if [ $? -ne 0 ]; then
				$SED -e "s/[^ ]*$ARG/$LOOPBACK_ADDR,$ETH2_ADDR:$ARG/" /etc/inetd.conf > /etc/inetd.conf.tmp &&
				shouldMove=1
			fi
		elif [ "$LOOPBACK_ADDR" != "" ]; then
                        $GREP "$LOOPBACK_ADDR:$ARG" /etc/inetd.conf >/dev/null
                        if [ $? -ne 0 ]; then
                                $SED -e "s/[^ ]*$ARG/$LOOPBACK_ADDR:$ARG/" /etc/inetd.conf > /etc/inetd.conf.tmp &&
                                shouldMove=1
                        fi
		else
			shouldMove=0
		fi		
		if [ "$shouldMove" == 1 ]; then

			if [ "$DEBUG" == 1 ]; then
				$ECHO "copying inetd.conf.tmp" >> /var/log/inetdDebug.log
				$CAT /etc/inetd.conf.tmp >> /var/log/inetdDebug.log
			fi
			$MV -f /etc/inetd.conf.tmp /etc/inetd.conf
			RESTART_INETD=1
		fi
	#fi
}

inetd_conf_file_size_check() {
	if [ "$DEBUG" == 1 ]; then
		file_size=`/usr/bin/wc -c < /etc/inetd.conf`
		if [ "$file_size" == 0 ]; then
			$ECHO "****************PLEASE REPORT THE ISSUE********CRITICAL/ERROR" >> /var/log/inetdDebug.log
			$ECHO "netstat o/p: `$NETSTAT -tulpn | $GREP LISTEN | $GREP inetd`" >> /var/log/inetdDebug.log
			return 1
		fi
	fi
	return 0
}
# Update Telnet in inetd.conf as neccessary
update_inetd_conf telnet $SHALL_TELNET_ENABLE

inetd_conf_file_size_check
if [ $? == 1 ];then
	$ECHO "Error after SHALL_TELNET_ENABLE $SHALL_TELNET_ENABLE" >> /var/log/inetdDebug.log
fi
# Update rstatd in inetd.conf as neccessary
update_inetd_conf rstatd/1-3 $SHALL_RSTAT_ENABLE

inetd_conf_file_size_check
if [ $? == 1 ];then
    $ECHO "Error after SHALL_RSTAT_ENABLE $SHALL_RSTAT_ENABLE" >> /var/log/inetdDebug.log
fi
# Update rusersd in inetd.conf as neccessary
update_inetd_conf rusersd/1 $SHALL_RUSERS_ENABLE

inetd_conf_file_size_check
if [ $? == 1 ];then
    $ECHO "Error after SHALL_RUSERS_ENABLE $SHALL_RSTAT_ENABLE" >> /var/log/inetdDebug.log
fi
# Update rsh to listen on loopback and internal address
update_inetd_conf_loopback_internal shell

inetd_conf_file_size_check
if [ $? == 1 ];then
    $ECHO "Error after loopback_internal shell" >> /var/log/inetdDebug.log
fi
# Update rlogin to listen on loopback address
update_inetd_conf login 0

inetd_conf_file_size_check
if [ $? == 1 ];then
    $ECHO "Error after login 0" >> /var/log/inetdDebug.log
fi
# Update exec(rpc) to listen on loopback address
update_inetd_conf exec 0

inetd_conf_file_size_check
if [ $? == 1 ];then
    $ECHO "Error after exec 0" >> /var/log/inetdDebug.log
fi

update_inetd_conf time 0

inetd_conf_file_size_check
if [ $? == 1 ];then
    $ECHO "Error after filesync call" >> /var/log/inetdDebug.log
fi
# inetd.conf has been changed. Restart the daemon.
if [ "$RESTART_INETD" == "1" ]; then
	$KILLALL -q -HUP $INETD_PROC >/dev/null 2>&1

	if [ "$SHALL_TELNET_ENABLE" == "0" ]; then
		# Kill all existing telnet sessions
		for telpid in `$NETSTAT -tp | $GREP "telnet" | $GREP "ESTAB" | $SED -e 's/	+/ /g' |
                       $SED -e 's/  */ /g' | $CUT -d' ' -f7 | $CUT -d'/' -f1`
		do
			logpid=`$PS -ef | $GREP $telpid | $GREP login | $SED -e 's/	+/ /g' |
			        $SED -e 's/  */ /g' | $CUT -d' ' -f2`
			$KILL $logpid
		done
	fi
fi
