#!/bin/sh -
#
# @(#)mk-afs-sendmail	1.15 (hursley) 11/1/96
# /afs/hursley.ibm.com/common/src/afs/@cell/rs_aix32/usr/local/sbin/mk-afs-sendmail/SCCS/s.mk-afs-sendmail
#
# NAME		mk-afs-sendmail
# AUTHOR	Paul Blackburn <mpb@acm.org>
# PURPOSE	Wrapper to get AFS authentication and then start sendmail.
#
# USAGE		After mk-afs-odmfix has changed the sendmail invocation
#		for srcmstr,  normal srcmstr commands to be used to stop and
#		start the AFS authenticated sendmail daemon via mk-afs-sendmail
#
#		    stopsrc -s sendmail
#		    startsrc -s sendmail -a "-bd -q30m"
#
# CAUTION	Do not attempt to use the mk-afs-sendmail wrapper program
#		unless you have:
#		a) a working reauth installed
#		b) configured mk-afs-sendmail to authenticate as an AFS ID
#		   (eg postman) authorised to deliver mail into $HOMEs
#		c) Set an ACL on $HOMEs to allow mail delivery
#		   (eg: fs sa $HOME postman liwk)
#
#		See: ftp://ftp.transarc.com/pub/afs-contrib/tools/reauth

CMD=`basename ${0}`

PATH=/usr/afsws/bin:${PATH}; export PATH
REAUTH=/usr/etc/mk-afs-reauth
PAGSH=/usr/afsws/bin/pagsh
SENDMAIL=/usr/lib/sendmail
PASSWORD=/etc/security/afs.postman.pw
SLEEPFOR=14400
WRAPPER=/tmp/${CMD}.wrapper.$$

export REAUTH PAGSH SENDMAIL CMD

# functions

tstamp() {
	echo "`date '+''%H''%M'':%S'` ${CMD}: ${1}"
}

warning() {
	echo "${CMD} warning: ${1}" >&2
}

error() {
        echo "${CMD} error: ${1}" >&2
}

doit() {
	tstamp "${1}"
	eval ${1}

	retcode=$?
	if [ ${retcode} != 0 ]; then
		error "\$?=${retcode}"
	fi
}

tstamp "version 1.15 commenced on `date '+%a %d %h %y'`"

# sanity checking

if [ ! -x ${PAGSH} ]; then
	warning "cannot execute ${PAGSH}" 
	warning "running un-authenticated ${SENDMAIL}"
	doit "exec ${SENDMAIL} $@"
fi
if [ ! -x ${REAUTH} ]; then
	warning "cannot execute ${REAUTH}" 
	warning "running un-authenticated ${SENDMAIL}"
	doit "exec ${SENDMAIL} $@"
fi
if [ ! -s ${PASSWORD} ]; then
	warning "cannot read password file ${PASSWORD}" 
	warning "running un-authenticated ${SENDMAIL}"
	doit "exec ${SENDMAIL} $@"
fi

# Create wrapper script

cat << eeooff > ${WRAPPER}
#!/bin/sh -
# 
# NAME		\${WRAPPER}
# CREATED	by \${CMD} on \`date\`
# PURPOSE	Script to start AFS authenticated sendmail daemon
# USAGE		Normally invoked via: startsrc -s sendmail -a "-bd -q30m"

# functions

error() {
        echo "\${CMD} error: \${1}" >&2
}
tstamp() {
        echo "\`date '+%H''%M'':%S'\` \${CMD}: \${1}"
}
doit() {
        tstamp "\${1}"
        eval \${1}

        retcode=\$?
        if [ \${retcode} != 0 ]; then
                error "\\\$?=\${retcode}"
        fi
}

CMD=\`basename ${WRAPPER}\`
tstamp "version 1.15 commenced on \`date '+%a %d %h %y'\`"
doit "groups"
doit "tokens # ante reauth"
doit "${REAUTH} ${SLEEPFOR} postman < ${PASSWORD}"
doit "tokens # post reauth"
doit "exec ${SENDMAIL} $@"
eeooff

doit "chmod 744 ${WRAPPER}"
tstamp "completed"
doit "exec ${PAGSH} ${WRAPPER}"
