#!/bin/sh
#
# Copyright 1996 Dynamic Software AB
# This is unpublished proprietary source code.
#
# $Id: checkssm.sh,v 1.1.2.3 1996/10/04 12:22:39 russin Exp $

PCNFSD_PROGRAM=/opt/SUNWpcnfs/sbin/rpc.pcnfsd
XDM_PROGRAM=/usr/openwin/bin/xdm
DTLOGIN_PROGRAM=/usr/dt/bin/dtlogin
XINIT_PROGRAM=/usr/openwin/bin/xinit

MAYBE_PROGRAMS="$PCNFSD_PROGRAM $XDM_PROGRAM $XINIT_PROGRAM $DTLOGIN_PROGRAM"
PROGRAMS="/usr/bin/login /sbin/su /usr/bin/su /usr/bin/passwd"

INETD_REPLACE='ftp shell exec'
INETD_CONF=/etc/inetd.conf

ftp=in.ftpd
shell=in.rshd
exec=in.rexecd

# check every 10 minutes

SLEEP_TIME=600

PATH=/sbin:/usr/sbin:/etc:/usr/bin

for f in /etc/opt/SUNWssm/ENV /etc/boksenv; do
    if [ -f $f ]; then
	eval `grep '^BOKS_lib=' $f`
	PATH="$BOKS_lib:$PATH"
	break
    fi
done

test -z "$BOKS_lib" && { sleep 60; exit 1; }

# Get BoKS locations
for d in DIR etc sbin lib var; do
    eval BOKS_$d="`boksdir $d`"
    export BOKS_$d
done
test -z "$BOKS_lib" && { echo "Must be run in a BoKS environment" >&2; exit 1; }
PATH="$BOKS_lib:$BOKS_sbin:$BOKS_bin:$PATH"; export PATH


INTERACTIVE=
if [ $# != 0 ]; then
	INTERACTIVE=yes
fi

# This is the warn function

warn () {
	p=$1
	level=$2
	cmess=
	lmess=
	case $level in
		GONE)
			cmess="SSM PANIC! Cannot find $p"
			label="ssmcheck_gone"
			lmess="PANIC! Cannot find program %s"
			;;
		REPLACE)
			cmess="SSM ERROR! $p is not an SSM program"
			label="ssmcheck_replace"
			lmess="ERROR! program %s is not the SSM version"
			;;
		INETD_GONE)
			# maybe shut up about this
			;;
		INETD_REPLACE)
			cmess="SSM ERROR! $p in $INETD_CONF does not refer to the SSM program"
			label="ssmcheck_inetd_replace"
			lmess="ERROR! %s in $INETD_CONF does not refer to the SSM version"
			;;
		*)
			cmess="SSM UNKNONW! label $level for program $p"
			label="ssmcheck_unknown"
			lmess="check_ssm: Unknown label for program %s"
			;;
	esac
	if [ -n "$cmess" ]; then
		if [ -z "$INTERACTIVE" ]; then
			echo $cmess > /dev/console
		else
			echo $cmess
		fi
	fi
	if [ -n "$lmess" ]; then
		if [ -z "$INTERACTIVE" ]; then
			clntlog -s -L "$label" -a "$lmess" "$p"
		fi
	fi
}

ssm_active () {
	SSM_ACTIVE=
	eval `grep '^SSM_ACTIVE=' $BOKS_etc/ENV 2>/dev/null`
	if [ -n "$SSM_ACTIVE" ]; then
		return 0
	fi
	return 1
}

if [ -z "$INTERACTIVE" ]; then
	# Write lockpid file so we are killed on Boot -k
	echo $$ > $BOKS_var/LCK/checkssm
fi

# loop forever

while :; do
	if ssm_active; then
		# these must be replaced
		for p in $PROGRAMS; do
			if [ ! -f $p ]; then
				warn $p GONE
			elif ls -l $p | grep ..ssm > /dev/null 2>&1; then
				:
			else
				warn $p REPLACE
			fi
		done

		# these may be replaced, don't complain if they
		# dont exist
		for p in $MAYBE_PROGRAMS; do
			if [ ! -f $p ]; then
				:
			elif ls -l $p | grep SUNWssm > /dev/null 2>&1; then
				:
			else
				warn $p REPLACE
			fi
		done

		# now check inetd.conf
		for p in $INETD_REPLACE; do
			eval prg=\$$p
			line="`grep \"^$p[ 	]\" $INETD_CONF 2> /dev/null`"
			if [ -z "$line" ]; then
				warn $prg INETD_GONE
			elif echo $line | grep 'SUNWssm/lib' > /dev/null 2>&1; then
				:
			else
				warn $prg INETD_REPLACE
			fi
		done
	fi
	if [ -z "$INTERACTIVE" ]; then
		sleep $SLEEP_TIME
	else
		exit
	fi
done
