#! /bin/sh
#
# this gathers all password files into one location
# and records lines that have changed since this was done
#
# USAGE
# 	update [ -f file | host1 ... ]
# stores the current password file for host in a file named host,
# and the new accounts and accounts with new passwords in a file
# named diffs.  Types of hosts recognized are:
#		host		password file is on host in /etc/passwd
#		host:file	password file is on host in file
#		host%map	get the YP password info for domain
#					host and map map (default host
#					is local domain, map is passwd)
# You can put these in a file (separated by newlines) and have
# the file used by giving the -f option.  Only up to the first
# blank is used, so the rest of the line can be a comment.
#
#################################################################
# 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=.:/bin:/usr/bin:/usr/ucb		# search path
PWDDIR=%%LIBDIR%%/pwdnew		# password files go here
TMP=upd$$				# temporary files for new pwd file
OLDSUF=.old				# append to old file
DIFFSUF=.diff				# append to difference file
# VARIABLES -- the program will change these
# i=					# index variable in for loop
# flag=					# indicates arg to option expected
hostlist=				# list of hosts to get files from
host=					# current host/domain name
pwdfile=				# current password file/YP map name
error=					# error message for fetch program
#
# on an interrupt, delete the intermediate file
#
trap "rm -f $TMP; exit 2" 1 2 3 15
#
# check the argument list
# if the first one is -f, the second one is a file
# otherwise they are all host:file, host, or domain%map
#
for i in $*
do
	# this is an argument to an option; stuff it and loop
	if test -n "$flag"
	then
		case $flag in
		f)	hostlist="$hostlist `awk '{ print $1 }' < $i`";;
		esac
		flag=
		continue
	fi
	# this is an option or a host name
	case $i in
	-f)	x=`expr "$i" : '-f\(.*\)'`	# host list file
		if test -n "$x"
		then
			hostlist="$hostlist `awk '{ print $1 }' < $i`"
		else
			flag=f
		fi
		;;
	*)	hostlist="$hostlist $i"		# a host name
		;;
	esac
done
#
# oops ... missing argument
#
if test -n "$flag"
then
	echo "$0: expected argument to -$flag" 1>&2
	exit 1
fi
#
# first, see if the directory to hold the password files exists
# if not, report error and croak
#
if test ! -d $PWDDIR
then
	echo $PWDDIR does not exist 1>&2
	exit 1
fi
#
# it does; change to it
#
cd $PWDDIR
#
# now for each host:
#
for i in $hostlist
do
	#
	# if a:b syntax, host is a, file is b,
	# and use rcp to suck it over
	#
	if expr "$i" : '^[^:]*:.*$' > /dev/null
	then
		host=`expr "$i" : '^\([^:]*\):.*$'`
		pwdfile=`expr "$i" : '^[^:]*:\(.*\)$'`
		error="$0: rcp $host:$pwdfile ... failed"
		rcp $host:$pwdfile $TMP 2> /dev/null
	#
	# if a%b syntax, YP domain is a, YP map name is b,
	# and use ypcat to show it and stuff it into a file
	#
	elif expr "$i" : '^[^%]*%.*$' > /dev/null
	then
		host=`expr "$i" : '^\([^%]*\)%.*$'`
		pwdfile=`expr "$i" : '^[^%]*%\(.*\)$'`
		if test -z "$pwdfile"
		then
			pwdfile=passwd
		fi
		if test -n "$host"
		then
			error="$0: ypcat -d $host $pwdfile ... failed"
			ypcat -d $host $pwdfile > $TMP 2> /dev/null
		else
			error="$0: ypcat $pwdfile ... failed"
			ypcat $pwdfile > $TMP 2> /dev/null
		fi
	#
	# if a syntax, host is a, file is /etc/passwd,
	# and use rcp to suck it over
	#
	else
		host=$i
		pwdfile=/etc/passwd
		error="$0: rcp $host:$pwdfile ... failed"
		rcp $host:$pwdfile $TMP 2> /dev/null
	fi
	#
	# if this fails, quit
	#
	if test $? -ne 0
	then
		echo $error 1>&2
		rm -f $TMP
		exit 1
	fi
	#
	# if there is a version of that host's password file there already,
	# only some (not all) of the passwords may have changed
	# back up the earlier version for comparison
	#
	if test -r $host
	then
		mv $host $host$OLDSUF
	fi
	mv $TMP $host
	#
	# now diff them on the basis of the password field and user name
	# note that if $host.old does not exist, diffpw simply prints
	# $host
	#
	diffpw $host $host$OLDSUF > $host$DIFFSUF
done
#
# clean up and exit
#
rm -f $TMP
exit 0

