#!/bin/sh
# Installation script for the client piece of
# LAT's Garbiel network probe (e.g. SATAN) detector.

# Copyright 1995 by Los Altos Technologies Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms are
# permitted provided that this entire copyright notice is
# duplicated in all such copies.  No charge, other than "at-cost"
# distribution fee may be charged for copies, derivations, or
# distributions of this material without the expressed written consent
# of the copyright holders.
# 
# THIS SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT ANY EXPRESSED
# OR IMPLIED WARRANTIES, INCLUDING, WITHOUT FURTHER LIMITATION, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
# PARTICULAR PURPOSE.
# 
# IN NO EVENT SHALL LOS ALTOS TECHNOLOGIES, INC., OR ANY OF ITS
# EMPLOYEES, REPRESENTATIVE, AGENTS, OFFICERS, OR DIRECTORS, OR ANY
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT
# LIMITED TO, LOSS OF USE, DATA, PROFITS, OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Gabriel will be installed in the first directory below
# if it exists, otherwise in the second one.
dstdir1="/usr/local/etc"
dstdir2="/usr"
gabdir="gabriel-1.0"
dstdir=$dstdir1
srcdir=`pwd`
gab="Gabriel"

echo $srcdir

PATH=/bin:/usr/bin:/usr/5bin:/usr/sbin:/usr/ucb:/usr/contrib/bin:/usr/local/bin
export PATH
SHELL=/bin/sh
export SHELL

echo "This script installs the client piece of"
echo "the $gab SATAN detector from Los Altos Technologies, Inc."
echo "It can be run interactively where it repeatedly asks for the name"
echo "of a host to install a $gab client package, or run in"
echo "batch mode by redirecting stdin to be from a file that contains"
echo "a list of hostnames, one per line.  For example"
echo "    install_gabriel_client server_hostname < host_list"
echo ""

if test $# != 1
then
    echo "Usage: install_gabriel_clients server_hostname"
    echo "    The name of the $gab server machine must be supplied."
    echo " "
    exit
fi
server=$1

#
# Setup constants
#
rcp="rcp -p"
rsh="rsh -n -l root"
rm="/bin/rm"
ls="/bin/ls"

gabpgm="gabriel_client"
errfile="./client_error_log"
osfile="./client_uname_info"
syslog="/etc/syslog.conf"
syspid="/etc/syslog.pid"
systag="local3.info"
sysdst="@$server"
sysslice="./syslog_slice"
rmttmp="/tmp/gabriel_tmp"
saved="saved_by_gabriel"

#
# Initial setup
#
$rm -rf $errfile $sysslice
echo $systag\	\	$sysdst > $sysslice

while /bin/true
do
    echo " "
    echo "Enter the name of a client host or control-D if done."
    read host
    if test "X$host" = "X"
    then
	    echo Done.
	    exit
    fi
    echo "	Checking client $host for suitability."
    $rsh $host uname -s -r > $osfile 2>&1
    if test $? != 0
    then
	echo "---- Cannot install on host: $host ----"
	echo "Cannot connect: "
	cat $errfile
	echo "This script needs root rsh access to the client"
	echo "The name of the server machine must appear in"
	echo "the .rhosts file for roots home directory."
	continue
    fi
    if [ grep BSD/OS ] $osfile >/dev/null
    then
	echo 
    else
	echo "---- Cannot install on host: $host ----"
	echo "Unsupported operating system: "
	cat $errfile
	continue
    fi

    echo "	Checking if $gab installed already in $syslog."
    $rsh $host grep $systag $syslog > $errfile
    if grep $systag $errfile > /dev/null
    then
	echo "	    The $syslog file already contains the $gab update."
    else
	echo "	    Saving $syslog file as $syslog.$saved file."
	$rsh $host cp -p $syslog $syslog.$saved
	if test $? != 0
	then
	    echo "---- Cannot install on host: $host ----"
	    echo "Cannot save $syslog file."
	    cat $errfile
	    continue
	fi
	echo "	    Adding $gab entry to $syslog file."
	$rsh $host $rm -f $rmttmp
	$rcp $sysslice $host\:$rmttmp
	if test $? != 0
	then
	    echo "---- Cannot install on host: $host ----"
	    echo "Cannot send syslog slice"
	    cat $errfile
	    continue
	fi
	$rsh $host cat $rmttmp \>\> $syslog
	if test $? != 0
	then
	    echo "---- Cannot install on host: $host ----"
	    echo "Cannot update $syslog"
	    cat $errfile
	    continue
	fi
	$rsh $host $rm -f $rmttmp
	echo "	    Re-initializing syslog daemon."
	$rsh $host kill -1 \` cat $syspid \`
	if test $? != 0
	then
	    echo "---- Cannot install on host: $host ----"
	    echo "Cannot re-initializing syslog daemon"
	    cat $errfile
	    continue
	fi
    fi

    echo ""
    echo "	Choosing directory to install $gab."
    $rsh $host $ls -ld $dstdir1 > $errfile 2>&1
    if grep \^$dstdir $errfile > /dev/null
    then
	echo "	    The $dstdir1 does not exist, using $dstdir2/$gabdir"
	dstdir=$dstdir2
    else
	echo "	    Using $dstdir/$gabdir"
    fi

    echo ""
    echo "	Checking if $gab programs are already installed on $host."
    $rsh $host $ls $dstdir/$gabdir > $errfile 2>&1
    if grep $gabpgm $errfile > /dev/null
    then
	echo "	    The $gab programs are already installed."
    else
	echo "	    Sending $gab programs to $dstdir."
	$rcp -r $srcdir $host\:$dstdir
	if test $? != 0
	then
	    echo "---- Cannot install on host: $host ----"
	    echo "Cannot send syslog slice"
	    cat $errfile
	    continue
	fi
    fi

    echo ""
    echo "	Starting $gabpgm on $host."
    echo "	You may wish to add it to the rc start-up scripts."
    $rsh $host /bin/sh -c "$dstdir/$gabdir/$gabpgm"
    if test $? != 0
    then
	echo "---- Cannot install on host: $host ----"
	echo "Cannot start $gabpgm: "
	cat $errfile
	continue
    fi
done
exit
