#!/bin/sh
##set -x
#
# Copyright (c) 1998,1999, by Sun Microsystems, Inc.
# All rights reserved.
#
#ident @(#)securid_stubclient_setup	3.2 04/19/99 Sun Microsystems
#
# This script instantiates a stub ACE/Client(R), enabling it to
# correctly use the authentication services of an ACE/Server(R)
# which has the stub client system configured as a Unix client.

# ACE/Client(R), ACE/Server(R), and SecurID(R) are registered
# trademarks of Security Dynamics, Inc.

read SUNSCREEN_BASE <${SUNSCREEN_ETC:-/etc/opt/SUNWicg/SunScreen}/location
. $SUNSCREEN_BASE/bin/.ssenv.profile

PROGNAME=securid_stubclient_setup
FORCE=0
DEF_BASEDIR=/opt/ace
BASEDIR=$DEF_BASEDIR
SPEW=: echo
WAD=${WAD:-$LIB_DIR/securid_stubclient.tar}
T1=/tmp/$$.tmp1
T2=/tmp/$$.tmp2

cleanup() {
	rm -f $T1 $T2
	if [ $1 -ne 0 ] ; then
		echo "$PROGNAME: Processing aborted."
	fi
	exit $1
}

cleanupF() {
	if [ $FORCE -eq 0 ] ; then
		cleanup $1
	fi
}

usage() {
	echo "Usage: $PROGNAME: [-d <base_of_ace_dir>] <server_conf_file>"
	echo "       '-f' forces overwriting of existing files"
	echo "       '<base_of_ace_dir>' defaults to '$DEF_BASEDIR'"
	cleanup 1
}


while [ $# -gt 1 ] ; do
	case $1 in
	-f)
		FORCE=1
		;;
	-d)
		if [ -z "$2" ] ; then
			usage
		fi
		BASEDIR=$2
		shift
		;;
	-d*)
		BASEDIR=`expr $1 : '..\(.*\)'`
		;;
	*)
		break
		;;
	esac
	shift
done

$SPEW FORCE==$FORCE
$SPEW BASEDIR==$BASEDIR

# check for argument
if [ $# -ne 1 ] ; then
	usage
fi
if [ ! -s $1 ] ; then
	usage
fi
$SPEW ARG1==$1

# check for root
ID=`id`
ID=`expr "$ID" : 'uid=\([0-9][0-9]*\)'`
if [ "$ID" -ne 0 ] ; then
	echo "$PROGNAME: must be superuser to run this command"
	cleanup 2
fi

umask 0

if [ -d $BASEDIR ] ; then
	echo "$PROGNAME: $BASEDIR already exists"
	cleanupF 3
fi
mkdir $BASEDIR
E1=$?
chmod 755 $BASEDIR
if [ $? -ne 0 -o $E1 -ne 0 ] ; then
	echo "$PROGNAME: $BASEDIR creation failed"
	cleanupF 3
fi

ER=" (pwd)"
D=`pwd`
E1=$?
if [ $E1 -eq 0 ] ; then
	ER=" (cd)"
	cd $BASEDIR
	E1=$?
fi
if [ $E1 -eq 0 ] ; then
	ER=
	tar xvpf $WAD
	E1=$?
	ER=" (cd back)"
	cd $D
	if [ $? -ne 0 ] ; then
		E1=1
	fi
fi
if [ $E1 -ne 0 ] ; then
	echo "$PROGNAME: $BASEDIR extraction$ER failed"
	cleanupF 3
fi

SDACE=$BASEDIR/data/sdace.txt
if [ $FORCE -ne 0 -a -f $SDACE ] ; then
	rm -f $SDACE
fi
if [ -f $SDACE ] ; then
	echo "$PROGNAME: $SDACE already exists"
	cleanup 4
fi
echo "VAR_ACE=$BASEDIR/data" >$SDACE
if [ $? -ne 0 ] ; then
	echo "$PROGNAME: $SDACE creation failed"
	cleanupF 4
else
	echo "USR_ACE=$BASEDIR/prog" >>$SDACE
	chmod 644 $SDACE
fi

SDLINK=/etc/sdace.txt
if [ $FORCE -ne 0 -a -h $SDLINK ] ; then
	rm -f $SDLINK
fi
if [ -h $SDLINK ] ; then
	echo "$PROGNAME: $SDLINK already exists"
	cleanup 4
fi
ln -s $SDACE $SDLINK
if [ $? -ne 0 ] ; then
	echo "$PROGNAME: $SDLINK symbolic link creation failed"
	cleanupF 4
fi

SDCONF=$BASEDIR/data/sdconf.rec
if [ $FORCE -ne 0 -a -r $SDCONF ] ; then
	rm -f $SDCONF
fi
if [ -r $SDCONF ] ; then
	echo "$PROGNAME: $SDCONF already exists"
	cleanup 4
fi
cp $1 $SDCONF
E1=$?
if [ $E1 -eq 0 ] ; then
	chmod 644 $SDCONF
	E1=$?
fi
if [ $E1 -ne 0 ] ; then
	echo "$PROGNAME: $SDCONF copying failed"
	cleanupF 4
fi

# check for 'securid' services
ETCSRV=/etc/inet/services
S0="## SecurID / ACE/Client stub services - added by $PROGNAME"
S1="securid		5500/udp"
S2="securidprop	5510/tcp"
ST1="    The following line(s) must be added to the services mapping in effect"
ST2="    for your system, before this client stub installation will function:"
ypmatch securid services >$T1 2>$T2
if [ $? -eq 0 -a -s $T1 ] ; then
	# that one exists in yp ... check for the other
	ypmatch securidprop services >$T1 2>$T2
	if [ $? -eq 0 -a -s $T1 ] ; then
		cleanup 0
	fi
	echo "$PROGNAME: NIS 'service' map incomplete."
	echo "$ST1"
	echo "$ST2"
	echo "$S2"
	cleanup 5
fi
SS=`grep '^services:.*[ 	]files' /etc/nsswitch.conf`
if [ -n "$SS" ] ; then
	# nsswitch sez to use files, at least sometimes
	NC=`grep '^tcp[ 	]' /etc/netconfig`
	if [ -n "$NC" -a `echo $NC | awk '{ print $7; }'` = "-" ] ; then
		# no library substitution; look for services now
		ST=`grep '^securid[ 	]' $ETCSRV`
		if [ -z "$ST" ] ; then
			echo "##" >>$ETCSRV
			echo "$S0" >>$ETCSRV
			echo "$S1" >>$ETCSRV
			S0=
		fi
		ST=`grep '^securidprop[ 	]' $ETCSRV`
		if [ -z "$ST" ] ; then
			if [ -n "$S0" ] ; then
				echo "##" >>$ETCSRV
				echo "$S0" >>$ETCSRV
			fi
			echo "$S2" >>$ETCSRV
		fi
	else
		SS=
	fi
fi
if [ -z "$SS" ] ; then
	# something wrong - tell user what to do
	echo "$PROGNAME: $ETCSRV could not be updated or isn't active."
	echo "$ST1"
	echo "$ST2"
	echo "$S1"
	echo "$S2"
	cleanup 5
fi

cleanup 0

# eof
