#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# tcpip720 src/tcpip/usr/sbin/snmpv3_ssw/snmpv3_ssw 1.4.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2002,2013 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)19        1.4.1.1  src/tcpip/usr/sbin/snmpv3_ssw/snmpv3_ssw, snmp, tcpip720 2/20/13 16:09:05

let TOTALFLAG=0
let E_FLAG=0
let N_FLAG=0
let V1_FLAG=0

L_FLAG=     # link flag
R_FLAG=	    # snmpd agent running flag	
DAEMON=
SNMPFILENAME=
CLSNMPFILENAME=

################################# usage #########################################
#
# NAME: usage
#
# DESCRIPTION: Display the usage and exit
#
usage(){
   msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 1 2>/dev/null`
   if [ -n "$msg" ]
   then
	/usr/bin/dspcat snmpv3_ssw.cat 1 1 >&2
   else
        cat <<- HERE >&2
	Usage:  /usr/sbin/snmpv3_ssw  [-e|-n|-1]
	-e 	Switch to encrypted version of snmpdv3 agent.
	-n 	Switch to non-encrypted version of snmpdv3 agent.
	-1      Switch to snmpdv1 agent.
	HERE
   fi
   exit 1
}

############################## problem ########################################
#
# NAME: problem
#
# DESCRIPTION: Issue "problem" message and exit
#
# INPUT:
#        $RTNCODE
#        $1             Name of command that encountered an error
#
# OUTPUT:
#        Error messages (Standard Error)
#
# RETURN VALUE DESCRIPTION:
#                           $RTNCODE
#
# NOTE: This function will not return (i.e., it will exit the entire
#       script with exit status of $RTNCODE).
#
problem(){
msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 3 2>/dev/null`
if [[ -n "$msg" ]]
then
	/usr/bin/dspcat snmpv3_ssw.cat 1 3 >&2
       	echo $1 >&2
      	/usr/bin/dspcat snmpv3_ssw.cat 1 4 >&2
     	echo $RTNCODE >&2
else
       echo "snmpv3_ssw: 1473-901 Problem with command: $1. Return code = $RTNCODE" >&2
fi
exit $RTNCODE                        # don't return
}

############################## checkexistence ###################################
#
# NAME: checkexistence
#
# DESCRIPTION: Test if the executable already exists. 
#
# INPUT:
#        $1             Name of executable file to be tested
#
checkexistence(){
if [[ ! -a $1 ]]; then
	msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 6 2>/dev/null`
	if [ -n "$msg" ]
	then
		echo $1 >&2
		/usr/bin/dspcat snmpv3_ssw.cat 1 6 >&2
	else
		echo "$1 doesn't exist." >&2
	fi
	exit 2
fi
}

############################## checklink ########################################
#
# NAME: checklink
#
# DESCRIPTION: Test if the symbolic link already exists. 
#
# INPUT:
#        $1             Name of executable file to be tested
#
checklink(){
L_FLAG=`ls -al /usr/sbin/snmpd | awk '{print substr($1,1,1)}'`
if [ "$L_FLAG" = "l" ]; then
	NAME=`ls -al /usr/sbin/snmpd | awk '{print $11}'`
	if [ "$NAME" = "$1" ]; then
		msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 5 2>/dev/null`
		if [ -n "$msg" ]
		then
			/usr/bin/dspcat snmpv3_ssw.cat 1 5 >&2
			echo $1 >&2
		else
			echo "snmpv3_ssw: 1473-902 /usr/sbin/snmpd is already linked to $1" >&2
		fi
		exit 3
	fi
fi
}

################################## stopdaemon ###################################
#
# NAME: stopdaemon 
#
# DESCRIPTION: stop daemon using src and kill
#
# INPUT:
#       $1: name of daemon
#
stopdaemon()
{
	DAEMON=$1
	# get daemon name and process id
        DAEMONNAME=`ps -e | grep "$DAEMON"| awk '"$DAEMON" {print $4}'`
        DAEMONPID=`ps -e | grep "$DAEMON"| awk '"$DAEMON" {print $1}'`

	if [[ $DAEMONPID -ne "" ]]; then
        	# try using src first
        	if [  -n "$DAEMONNAME" ]; then
   			msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 8 2>/dev/null`
   			if [ -n "$msg" ]
   			then
				/usr/bin/dspcat snmpv3_ssw.cat 1 8
				echo "$1"
			else
				echo "Stop daemon: $1"
			fi
                	stopsrc -s $1 >/dev/null 2>&1
                	RTNCODE=$? 
		fi

		# If the daemon isn't started by srcmstr,
        	# then try to kill the pid of the daemon
		if [ $RTNCODE -ne 0 ]; then
			if [ -n "$DAEMONPID" ]; then
               			kill $DAEMONPID 2>/dev/null
               			sleep 1
               			kill -9 $DAEMONPID 2>/dev/null
			fi
        	fi
	fi
}

############################## checkagentrunning #############################
#
# NAME: checkagentrunning
#
# DESCRIPTION: Check if snmpd agent is running. 
#	       If so, 
#		   set the R_FLAG.
#	           stop the running snmpd agent.
#	       If switch to snmpdv1 agent
#		   stop the running snmpmibd agent
#		   comment out the snmpmibd line in /etc/rc.tcpip file
#		   uncomment out the dpid2 line in /etc/rc.tcpip file
#	       else if switch to snmpdv3 agent
#		   stop the running dpid2 agent
#		   comment out the dpid2 line in /etc/rc.tcpip file
#		   uncomment out the snmpmibd line in /etc/rc.tcpip file
#
# INPUT:
#        $1	tells who is calling this function(v1, v3ne, or v3e)
#
checkagentrunning(){
	if [ "$1" = "v1" ]; then
		# need to stop the snmpmibd if switch to snmpdv1 agent.
		stopdaemon snmpmibd

		# comment out the snmpmid line in /etc/rc.tcpip file
  		msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 11 2>/dev/null`
   		if [ -n "$msg" ]
   		then
			/usr/bin/dspcat snmpv3_ssw.cat 1 11
			echo "snmpmibd"
		else
			echo "In /etc/rc.tcpip file, comment out the line that contains: snmpmibd"
		fi
		chrctcp -d snmpmibd >/dev/null 2>&1

		# uncomment out the dpid2 line in /etc/rc.tcpip file
		msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 12 2>/dev/null`
		if [ -n "$msg" ]
		then
			/usr/bin/dspcat snmpv3_ssw.cat 1 12
			echo "dpid2"
		else
			echo "In /etc/rc.tcpip file, remove the comment from the line that contains: dpid2"
		fi
		chrctcp -a dpid2 >/dev/null 2>&1

	elif [[ "$1" = "v3e" || "$1" = "v3ne" ]]; then
		# need to stop the dpid2 if switch to snmpdv3 agent.
		stopdaemon dpid2

		# comment out dpid2 line in /etc/rc.tcpip file
		msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 11 2>/dev/null`
		if [ -n "$msg" ]
		then
			/usr/bin/dspcat snmpv3_ssw.cat 1 11
			echo "dpid2"
		else
			echo "In /etc/rc.tcpip file, comment out the line that contains: dpid2"
		fi
		chrctcp -d dpid2 >/dev/null 2>&1

		# uncomment out the snmpmibd line in /etc/rc.tcpip
  		msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 12 2>/dev/null`
   		if [ -n "$msg" ]
   		then
			/usr/bin/dspcat snmpv3_ssw.cat 1 12
			echo "snmpmibd"
		else
			echo "In /etc/rc.tcpip file, remove the comment from the line that contains: snmpmibd"
		fi
		chrctcp -a snmpmibd >/dev/null 2>&1
	fi

	AGENT=`ps -e | grep snmpd| awk '"$DAEMON" {print substr($4,1,5)}'`  >/dev/null 2>&1
	if [ "$AGENT" = "snmpd" ]; then
		R_FLAG=1
		stopdaemon snmpd
	fi
}

################################## relink ###################################
# NAME:  relink 
#
# DESCRIPTION: Relink the snmpd, clsnmp to the intended executable
#
# INPUT:
#	$1	tells who is calling this function(v1, v3ne, or v3e)
#
relink(){
if [[ -a "/usr/sbin/snmpd" ]]; then
	rm /usr/sbin/snmpd > /dev/null 2>&1
	RTNCODE=$?
	if [ $RTNCODE -ne 0 ]; then
		problem rm
	fi
fi

if [[ -a "/usr/sbin/clsnmp" ]]; then
	rm /usr/sbin/clsnmp > /dev/null 2>&1
	RTNCODE=$?
	if [ $RTNCODE -ne 0 ]; then
		problem rm
	fi
fi

if [ "$1" = "v3e" ]; then
	SNMPFILENAME=snmpdv3e
	CLSNMPFILENAME=clsnmpe
elif [ "$1" = "v3ne" ]; then
	SNMPFILENAME=snmpdv3ne
	CLSNMPFILENAME=clsnmpne
elif [ "$1" = "v1" ]; then
	SNMPFILENAME=snmpdv1
	CLSNMPFILENAME=clsnmpne # default, link to non-encrypted version
fi

msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 9 2>/dev/null`
if [ -n "$msg" ]
then
	/usr/bin/dspcat snmpv3_ssw.cat 1 9
	echo " /usr/sbin/snmpd"	
	/usr/bin/dspcat snmpv3_ssw.cat 1 10
	echo "/usr/sbin/$SNMPFILENAME"
else
	echo "Make the symbolic link from /usr/sbin/snmpd to /usr/sbin/$SNMPFILENAME"
fi
ln -s /usr/sbin/$SNMPFILENAME /usr/sbin/snmpd > /dev/null 2>&1
RTNCODE=$?
if [ $RTNCODE -ne 0 ]; then
	problem ln
fi

msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 9 2>/dev/null`
if [ -n "$msg" ]
then
	/usr/bin/dspcat snmpv3_ssw.cat 1 9
	echo " /usr/sbin/clsnmp"
	/usr/bin/dspcat snmpv3_ssw.cat 1 10
	echo "/usr/sbin/$CLSNMPFILENAME"
else
	echo "Make the symbolic link from /usr/sbin/clsnmp to /usr/sbin/$CLSNMPFILENAME"
fi
ln -s /usr/sbin/$CLSNMPFILENAME /usr/sbin/clsnmp > /dev/null 2>&1
RTNCODE=$?
if [ $RTNCODE -ne 0 ]; then
	problem ln
fi

if [ "$1" = "v1" ]; then
       	DPID2PID=`ps -e | grep "dpid2"| awk '"dpid2" {print $1}'`
	# if dpid2 is not started, then start it.
	if [[ $DPID2PID -eq "" ]]; then
		msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 7 2>/dev/null`
		if [ -n "$msg" ]
		then
			/usr/bin/dspcat snmpv3_ssw.cat 1 7
			echo "dpid2"
		else
			echo "Start daemon: dpid2"
		fi
		startsrc -s dpid2 >/dev/null 2>&1
		RTNCODE=$?
		if [ $RTNCODE -ne 0 ]; then
			problem "startsrc -s dpid2"
		fi
	fi

elif [[ "$1" = "v3e" || "$1" = "v3ne" ]]; then
       	SNMPMIBDPID=`ps -e | grep "snmpmibd"| awk '"snmpmibd" {print $1}'`
	# if snmpmibd is not started, then start it.
	if [[ $SNMPMIBDPID -eq "" ]]; then
		msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 7 2>/dev/null`
		if [ -n "$msg" ]
		then
			/usr/bin/dspcat snmpv3_ssw.cat 1 7
			echo "snmpmibd"
		else
			echo "Start daemon: snmpmibd"
		fi
		startsrc -s snmpmibd > /dev/null 2>&1
		RTNCODE=$?
		if [ $RTNCODE -ne 0 ]; then
			problem "startsrc -s snmpmibd"
		fi
	fi
fi

if [ "$R_FLAG" = "1" ]; then
	msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 7 2>/dev/null`
	if [ -n "$msg" ]
	then
		/usr/bin/dspcat snmpv3_ssw.cat 1 7
		echo "snmpd"
	else
		echo "Start daemon: snmpd"
	fi
	startsrc -s snmpd > /dev/null 2>&1
	RTNCODE=$?
	if [ $RTNCODE -ne 0 ]; then
		problem "startsrc -s snmpd"
	fi
fi
}

################################## ssw_v3e ###################################
# NAME: ssw_v3e 
#
# DESCRIPTION:	Switch the symbolic link to snmp v3 encrypted version.
#
# INPUT:
#	NONE
#
ssw_v3e(){
checkexistence /usr/sbin/snmpdv3e
checklink /usr/sbin/snmpdv3e
checkagentrunning v3e
relink v3e
}

################################## ssw_v3ne ###################################
# NAME: ssw_v3ne
#
# DESCRIPTION:  Switch the symbolic link to snmp v3 non-encrypted version.
#
# INPUT:
#       NONE
#
ssw_v3ne(){
checkexistence /usr/sbin/snmpdv3ne
checklink /usr/sbin/snmpdv3ne
checkagentrunning v3ne
relink v3ne
}

################################## ssw_v1 ###################################
#
# NAEME: ssw_v1 
#
# DESCRIPTION: 	Switch the link to snmp v1 version.
#
# INPUT:
#	NONE
#
ssw_v1(){
checkexistence /usr/sbin/snmpdv1
checklink /usr/sbin/snmpdv1
checkagentrunning v1
relink v1
}

############################## main ############################################
PATH=/bin:/usr/bin:/usr/sbin:/etc:/usr/ucb export PATH

# Check the permisson, either user as "root", or group as "system":
UID=`id | awk '{print $1}' | cut -c5`
GID=`id | awk '{print $2}' | cut -c5`
if [ "$UID" != "0" a "$GID" != "0" ]; then
	msg=`/usr/bin/dspcat snmpv3_ssw.cat 1 2 2>/dev/null`
        if [ -n "$msg" ]; then
       		/usr/bin/dspcat snmpv3_ssw.cat 1 2 >&2
        else
                echo "snmpv3_ssw: 1473-900 Permission denied." >&2
        fi
        exit 2
fi


# Parse command flags and arguments
set -- `getopt en1 $*` 
if [ "$1" == "" ]
then
   usage
fi

while  [ $1 != -- ]
do
	case $1 in
	-e)     # set up the -a flag
		let E_FLAG+=1
		let TOTALFLAG+=1;;
	-n)     # set up the -b flag
		let N_FLAG+=1
		let TOTALFLAG+=1;;
	-1)     # set up the -c flag
		let V1_FLAG+=1
		let TOTALFLAG+=1;
	esac
	shift   # next flag
done

# Only one option can be specified.
if ((   (((E_FLAG > 0 ) || (N_FLAG > 0) || (V1_FLAG > 0)) && (TOTALFLAG > 1))
     || ((E_FLAG == 0) && (N_FLAG == 0) && (V1_FLAG ==0))   ));
then
	usage
fi

# Do the job.
if [[ $# -gt 1 ]]; then
	usage
elif [ "$E_FLAG" = "1" ]; then
	ssw_v3e
elif [ "$N_FLAG" = "1" ]; then
	ssw_v3ne
elif [ "$V1_FLAG" = "1" ]; then
	ssw_v1
fi
exit 0
