#! /bin/sh
#
# this analyzes output files from the guesser and executes
# a series of commands to forward the results to the administrator
# in an appropriate form (via mail, printer, etc.); it also
# allows users to be notified
#
# USAGE
#	advise [ option ] file
# executes a command (or series of commands) to forward the results
# of the guessing to the appropriate people using the appropriate media.
# Options are:
#	-c cmd		handle results given the named command;
#				%o is replaced by output of run,
#	-C cmdfile	like -c, but a file
#	-o output	save output file as output
# 	-u mesg		all users; mesg is a file containing the
#			message to be sent.  %w is replaced by
#			second field of file (user@host, usually)
# 
#################################################################
# Copyright notice.						#
# This software is copyrighted (c) 1991 by Matt Bishop and the	#
# Trustees of Dartmouth College.  All rights reserved.		#
# 								#
# Author:	Matt Bishop					#
# Address:	Department of Mathematics and Computer Science	#
#	 	Dartmouth College				#
# 		Hanover, NH  03755-1831				#
# 		USA						#
# telephone:	+1 603 646 3267					#
# fax:		+1 603 646 1312					#
# internet:	Matt.Bishop@dartmouth.edu			#
# usenet:	...!decvax!dartvax!Matt.Bishop			#
#################################################################
#
# PARAMETERS -- changing these affects how the program works
VERSION="Version GAMMA 6/31/91 Matt.Bishop@dartmouth.edu"	# version
PATH=%%BINDIR%%:/bin:/usr/bin:/usr/ucb	# search path
OUTARG=				# output file name
USRMSG=				# message to be sent to users
TMPCMD=adv$$.1			# holds list of command-line commands
OUTPUT=adv$$.2			# intermediate output file
TMP=adv$$.3			# temporary file
# VARIABLES -- the program will change these
# i=				# temporary in various loops
# x=				# holds part of options after flag
cmdfiles=			# list of files with commands to be executed
file=				# list of files of output from guesser
flag=				# for options with args
#
# process signals
#
trap "rm -f $TMP $TMPCMD $OUTPUT; exit 2" 1 2 3 15
#
# process arguments
#
for i in "$@"
do
	# this is an argument to an option; stuff it and loop
	if test -n "$flag"
	then
		case $flag in
		c)	echo $i >> $TMPCMD	;;	# -c ...
		C)	cmdfiles="$cmdfiles $i"	;;	# -C ...
		o)	OUTARG="$i"		;;	# -o ...
		u)	USRMSG="$i"		;;	# -u ...
		esac
		flag=
	else
		case $i in
		-c*)	x=`expr "$i" : '-c\(.*\)'`	# special command
			if test -n "$x"
			then
				echo $i >> $TMPCMD
			else
				flag=c
			fi
			;;
		-C*)	x=`expr "$i" : '-C\(.*\)'`	# commands file
			if test -n "$x"
			then
				cmdfiles="$cmdfiles $x"
			else
				flag=C
			fi
			;;
		-o*)	x=`expr "$i" : '-o\(.*\)'`	# output file name
			if test -n "$x"
			then
				OUTARG="$x"
			else
				flag=o
			fi
			;;
		-u*)	x=`expr "$i" : '-u\(.*\)'`	# user message file
			if test -n "$x"
			then
				USRMSG="$x"
			else
				flag=u
			fi
			;;
		*)	file="$file $i"
			;;
		esac
	fi
done
#
# merge command-line commands and filed commands
#
if test -s "$TMPCMD"
then
	cmdfiles="$TMPCMD $cmdfiles"
fi
#
# if output file is to be saved, rename it
#
if test -n "$OUTARG"
then
	OUTPUT="$OUTARG"
fi
cp /dev/null $OUTPUT
#
# general information for the output
#
echo '******* OUTPUT OF PASSWORD GUESSING RUN' >> $OUTPUT
echo 'System on which the test was run:' `hostname` >> $OUTPUT
echo 'Date:' `date` >> $OUTPUT
echo ' ' >> $OUTPUT
#
# warn of password-free accounts
#
if grep 'DANGER' $file > $TMP
then
	echo 'The following accounts have no passwords and hence can be accessed ' >> $OUTPUT
	echo 'by ANYONE!  They usually pose a SERIOUS security threat unless their' >> $OUTPUT
	echo 'environment is tightly restricted.' >> $OUTPUT
	echo '********** CHECK THEM CAREFULLY **********' >> $OUTPUT
	echo ' ' >> $OUTPUT
	awk ' { printf "\t%s\n", $2 } ' $TMP | sort >> $OUTPUT
	echo ' ' >> $OUTPUT
fi
#
# warn of guessed accounts
#
if grep 'GUESSED' $file > $TMP
then
	echo 'The following accounts have passwords that this system was able to' >> $OUTPUT
	echo 'guess, which means that others can guess them too.  They should' >> $OUTPUT
	echo 'be changed AT ONCE' >> $OUTPUT
	echo ' ' >> $OUTPUT
	awk ' { printf "\t%s (password is \"%s\", guessed in %s)\n", $2, $3, $4 } ' $TMP >> $OUTPUT
	echo ' ' >> $OUTPUT
	#
	# now notify users if so desired
	#
	if test -n "$USRMSG"
	then
		awk ' { print $2 } ' < $TMP | \
			( while read WHO ; \
			  do \
				sed -e "s/%w/$WHO/g" $USRMSG | sh
			  done
			)
	fi
fi
#
# inform of accounts with passwords turned off
#
if grep 'BLOCKED' $file > $TMP
then
	echo 'The following accounts have invalid passwords and hence can be' >> $OUTPUT
	echo 'reached only if permission is given using (usually) a .rhosts' >> $OUTPUT
	echo 'or hosts.equiv file, or the equivalent' >> $OUTPUT
	echo ' ' >> $OUTPUT
	awk ' { printf "\t%s\n", $2 } ' $TMP | sort >> $OUTPUT
	echo ' ' >> $OUTPUT
fi
#
# run the requisite commands, replacing %o with the output file name
#
for i in $cmdfiles
do
	 sed "s/%o/$OUTPUT/g" $i | sh
done
#
# delete temporary files
#
rm -f $TMP $TMPCMD
if test -z "$OUTARG"
then
	rm -f $OUTPUT
fi
exit 0
