:
#      @(#)snmp	5.6 SCO Unix 3.2 TCP/IP/NFS  source
#      SCCS IDENTIFICATION
#
#	System V STREAMS SNMP - Release 4.0
#
#	Copyright 1987, 1988, 1989 Lachman Associates, Incorporated (LAI)
#
#	All Rights Reserved.
#
#	The copyright above and this notice must be preserved in all
#	copies of this source code.  The copyright above does not
#	evidence any actual or intended publication of this source
#	code.
#
#	This is unpublished proprietary trade secret source code of
#	Lachman Associates.  This source code may not be copied,
#	disclosed, distributed, demonstrated or licensed except as
#	expressly authorized by Lachman Associates.
#
#	System V STREAMS TCP was jointly developed by Lachman
#	Associates and Convergent Technologies.
#
#
: ${OK=0} ${FAIL=1}
PATH=/bin:/usr/bin:/etc:/etc/conf/bin

TCPPERM=/etc/perms/tcprt			# permlist of this product
TRUE=1
CONF_FILE=/etc/snmpd.conf
COMM_FILE=/etc/snmpd.comm
TRAP_FILE=/etc/snmpd.trap
PEER_FILE=/etc/snmp.peers

DEF_CONT="Local System Administrator"
DEF_LOC="Computer Center"
START=/etc/rc2.d/S89snmp
STOP=/etc/rc0.d/K89snmp
tmp=/tmp/snmp$$

##############################################################################
#
# Remove temp files and exit with the status passed as argument
#
cleanup() {
	trap '' 1 2 3 15
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}

##############################################################################
#
# Prompt with mesg, return non-zero on q. Also allows setting -x and +x,
# shell escapes and the passing of default values
#
# Usage: prompt "message" [default]
# Argument is a quoted message
# Dependencies: expr
# Notes: returns 1 if the user answers q or Q, returns 0 otherwise
#	 if $quit is not set, it is set to "quit"
#	 $cmd is set to whatever is input by the user, and then can
#		subsequently be used in the shell script.
#
prompt() {
	_mesg=$1
	while	echo "${_mesg} [$2]: \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		Q|q)	return 1					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# If there is an second argument use it
			# as the default else loop until 'cmd' is set
			[ "$2" ] && { 
				cmd=$2
				return 0
			}
			: continue
			;;
		*)	unset _mesg
			return 0					;;
		esac
	done
}

##############################################################################
#
# Prompt for yes or no answer with message passed as argument - 
# returns non-zero for no
#
getyn() {
	case $2 in
	    [yY])
		default="y"
		mesg="$1 (y/n) [$default] "
		;;

	    [nN])
		default="n"
		mesg="$1 (y/n) [$default] "
		;;

	    *)
		default=""
		mesg="$1 (y/n) "
		;;
	esac

	while	echo "\n$mesg\c">&2
	do	read yn rest

		if [ "$yn" = "" -a "$default" ]
		then
		    yn=$default
		fi

		case $yn in
		+x|-x)	set $yn					;;
		[yY])	return 0 				;;
		[nN])	return 1				;;
		*)	echo "Please answer y or n" >&2		;;
		esac
	done
}

##############################################################################
#
# Remove SNMP
#
remove() {
	# Shut snmp down if necessary
	if [ "$_no_prompt" ] ; then
		[ -x /etc/snmp ] && /etc/snmp stop >/dev/null
	else
		[ -x /etc/snmp ] && /etc/snmp stop
	fi

	# Save the snmp startup script and restore the
	# configuration files to original state
	[ ! "$_no_prompt" ] && echo "Removing SNMP Agent ..."

	# Save current SNMP startup script
	[ ! -d /usr/lib/tcprt/snmp ] && mkdir -p /usr/lib/tcprt/snmp
	[ -f /etc/snmp ] && cp /etc/snmp /usr/lib/tcprt/snmp

	# Break links to start/stop scripts
	rm -f $START $STOP /etc/snmp

	# Clean out configuration files
	for i in $CONF_FILE $COMM_FILE $TRAP_FILE $PEER_FILE
	do
		[ -f $i ] && {
			base=`basename $i`
			cp $i /usr/lib/tcprt/snmp/$basename
			grep "#" $i > $tmp
			mv $tmp $i
		}
	done
	echo "public 0.0.0.0 READ" >> /etc/snmpd.comm
}

##############################################################################
# 
# Test to see if the TCPRT Package is installed
#
chktcp() {
	PWD=`/bin/pwd` ; cd /
	fixperm -i -d TCPRT ${TCPPERM} ; res=$?
	if [ $res -lt 6 ]
	then
		idcheck -p tcp ; res=$?
		if [ $res -lt 16 ] 
		then
			echo "SNMP Agent cannot be installed without the TCPRT package."
			cleanup $FAIL
		fi
	else
		echo "SNMP Agent cannot be installed without the TCPRT package."
		cleanup $FAIL
	fi
	cd $PWD
}

##############################################################################
#
# Get IP address
#
IP=
getipaddr() {
	while :
	do
	    prompt "\nHost IP address :"
	    case "$cmd" in

		[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)
			IP=$cmd
			break
			;;

		*)
			echo "Address must have n.n.n.n form, i.e., 192.9.200.9"
			;;

	    esac
	done

}

##############################################################################
#
# Get community name
#
COMM=
getcommname() {
	cmd=
	while [ -z "$cmd" ]
	do
		prompt "Enter community/session name, 'q' to end" "q"
		[ $? -eq 1 -o "$cmd" = "q" ] && return $FAIL
		COMM=$cmd
	done
	return $OK
}

##############################################################################
#
# Get permissions name for local host
#
PERM=
getperms() {
	cmd=
	while [ -z "$cmd" ]
	do
		prompt "Enter permission for $IP" "NONE"
		case $cmd in
			READ|WRITE|NONE) PERM=$cmd
					 ;;
				      *) cmd=
					 ;;
		esac
	done
}

##############################################################################
#
# Get sysDescr name for local host
#
SYSCONTACT=
getcontact() {
	cmd=
	while [ -z "$cmd" ]
	do
		prompt "Enter sysContact name" "$DEF_CONT"
		SYSCONTACT=$cmd
	done
}

##############################################################################
#
# Get sysLocation name for local host
#
SYSLOC=
getlocname() {
	cmd=
	while [ -z "$cmd" ]
	do
		prompt "Enter sysLocation" "$DEF_LOC"
		SYSLOC=$cmd
	done
}

##############################################################################
#
# Install SNMP
#
install() {
    if [ ! "$_no_prompt" ] ; then
	tput clear
	cat - <<EOF

			SNMP Agent Setup


You'll be asked to supply values to a number of configuration parameters.
If there is a default, you may press 'Return' to use it.

Note: This shell script is designed to handle the most common subset of
SNMP configuration needs.  It's possible that your needs may not be
addressed here If that is the case, the best approach is to run through 
this anyway and then edit the resulting file.
EOF

	getyn "\n\t Do you wish to continue?"
	[ $? -eq 1 ] && exit $FAIL

	# Set sysContact and sysLocation
	tput clear
	cat - <<EOF
			
			MIB System Setup

One section of the MIB is the system group.  This group contains the
elements specific to the system being configured.  SysContact is a text 
string describing the the person responsible for the system on which the
agent/server is being run.  SysLocation is the location of the system.
These values are kept in the file /etc/snmpd.conf.

EOF

	getcontact
	getlocname
	echo contact=$SYSCONTACT >> $CONF_FILE 
	echo location=$SYSLOC   >> $CONF_FILE

	# Set communities
	tput clear
	cat - <<EOF

			SNMP Community Setup

Access to the agent/server is controlled by SNMP communities.  The 
access is granted to hosts are READ, WRITE (implies READ), or NONE.
You will now be prompted for a community name, Internet address in
dot notation, and the permission for the community.  These values are 
kept in the file /etc/snmpd.comm.

EOF

	while [ ! -z TRUE ] 
	do
		getcommname
		[ $? -ne 0 ] && break
		getipaddr
		getperms
		echo $COMM $IP $PERM >> $COMM_FILE
	done

	# Set trap hosts
	tput clear
	cat - <<EOF

			SNMP Trap Systems Setup

Trap systems are those systems which are notified of trap events which occur.
In general, only management stations should be used.  You will now
be asked for a list of communities and IP addresses.  These values are 
kept in the file /etc/snmpd.trap.

EOF

	while :
	do
		getcommname
		[ $? -ne 0 ] && break
		getipaddr
		echo $COMM $IP 162 >> $TRAP_FILE
	done
    fi	# End of if ! _no_prompt

    [ -f /usr/lib/tcprt/snmp/snmp ] && mv /usr/lib/tcprt/snmp/snmp /etc/snmp 

    rm -f $START $STOP
    ln /etc/snmp $START
    ln /etc/snmp $STOP

    [ ! "$_no_prompt" ] && echo "SNMP Agent Configuration Complete."
}

##############################################################################
# 
# usage() - Displays usage message (when script in called directly)
#
usage() {
	echo "Usage: snmp [ -i | -r ] [ -n ]\n"
}

##############################################################################
# 
# main()
#
#	Options:
#		-i  ...  Install snmp
#		-r  ...  Remove snmp
#		-n  ...  No prompt mode
#
_no_prompt=
_install=
_remove=

while getopts nir opt
do
	case $opt in

		n) _no_prompt=TRUE
		   ;;

		i) _install=TRUE
		   ;;

		r) _remove=TRUE
		   ;;

		\?) usage
		   exit $FAIL
		   ;;

	esac
done

[ "$_install" -a "$_remove" ] && {
	usage
	exit $FAIL
}

[ "$_install" ] && {
	install
	exit $OK
}
[ "$_remove" ] && {
	remove
	exit $OK
}

# Must assume to be in interactive mode
tput clear
echo "\t\tSNMP Agent Configuration"

# check to see if TCP/IP is installed
chktcp

while prompt "\nDo you wish to install or delete the SNMP Agent (i/r/q)" "q"
do

	[ $? -eq 1 ] && exit $FAIL

	case $cmd in

		I|i) install
		     exit $OK
		     ;;

		R|r) remove
		     exit $OK
		     ;;

		Q|q) exit $FAIL
		     ;;

		*)   echo "Please answer 'i', 'r' or 'q'."
		     ;;

	esac
done
