#!/bin/sh # # enableSwat - enable/disable SWAT # usage : enableSwat/disableSwat # -e : enable # -d : disable # -s : SWAT path # -v : verbose output # -t : test mode (backup /etc/inetd.conf and /etc/services) # # return value # 0 : success # 1 : invalid argument(s) # 2 : invalid /etc/services file # 3 : invalid or unrecognized /etc/inetd.conf file # change below depending on your configuration SWAT=/usr/sbin/swat # intialize mode variables enable=0 ; disable=0; unknown=0 verbose=0 ; debugmode=0 LANG=C; export LANG tmpfile=/tmp/enableSwat.$$; export tmpfile # a function to send HUP to inetd # hup_inetd() { # Set the architecture of this machine os_name=`uname -s`":"`uname -r` case $os_name in SunOS:5.* | HP-UX:*1[0-9].*) ps='ps -ef' kill -HUP `$ps | grep inetd | grep -v grep | awk '{print $2;}'` ;; *) ps='ps -ax' kill -HUP `$ps | grep inetd | grep -v grep | awk '{print $1;}'` ;; esac if [ $verbose -eq 1 ] ; then echo OS name is $os_name. echo HUP inetd via $ps fi } # check running mode at first if [ `basename $0` = "enableSwat" ] ; then enable=1 elif [ `basename $0` = "disableSwat" ] ; then disable=1 fi # parsing arguments if [ $# ] ; then while getopts 'edvts:' opt ; do case $opt in 'e') enable=1 ; disable=0 ;; 'd') disable=1 ; enable=0 ;; 's') SWAT=$OPTARG ;; 'v') verbose=1 ;; 't') debugmode=1 ;; '*') unknown=1 ;; esac done fi # check if invalid mode if [ $enable -eq $disable ] ; then unknown=1 fi # show usage if unknown=1 if [ $unknown -ne 0 ] ; then cat << EOF 1>&2 $0 - enable/disable SWAT [-d|-e] [-s SWAT-path] [-vt] usage : enableSwat/disableSwat -e : enable -d : disable -s : SWAT path (default $SWAT) -v : verbose output -t : test mode (backup /etc/inetd.conf and /etc/services) EOF exit 1 fi # # check the SWAT entry in /etc/services # ret=`egrep '^[ ]*swat[ ]+[0-9]' /etc/services | wc -l` # no entry in /etc/services now if [ $ret -eq 0 ] ; then if [ $verbose -ne 0 ] ; then echo "no SWAT entry in /etc/services" fi if [ $enable -ne 0 ] ; then if [ $verbose -ne 0 ] ; then echo "adding the SWAT entry to /etc/services" fi cat << 'EOF' >> /etc/services swat 901/tcp # Samba Web Admin Tool EOF fi # one valid entry in /etc/services now elif [ $ret -eq 1 ] ; then if [ $verbose -ne 0 ] ; then echo "one SWAT entry in /etc/services" fi if [ $disable -ne 0 ] ; then if [ $verbose -ne 0 ] ; then echo "deleting the SWAT entry from /etc/services" fi # backup /etc/services if [ $debugmode ] ; then cp /etc/services /tmp fi cp /etc/services $tmpfile cat $tmpfile | \ egrep -v '^[ ]*swat[ ]+[0-9]' > \ /etc/services fi # something wrong in /etc/services else echo something wrong with your /etc/services 1>&2 egrep '^[ ]*swat[ ][^0-9]' /etc/services 1>&2 exit 2 fi # # check the SWAT entry to /etc/inetd.conf # ret=`egrep '^[ ]*swat[ ].*[ ]swat[ ]*' /etc/inetd.conf | wc -l` # no entry in /etc/inetd.conf now if [ $ret -eq 0 ] ; then if [ $verbose -ne 0 ] ; then echo "no SWAT entry in /etc/inetd.conf" fi if [ $enable -ne 0 ] ; then if [ $verbose -ne 0 ] ; then echo "adding the SWAT entry to /etc/inetd.conf" fi cat << EOF >> /etc/inetd.conf swat stream tcp nowait.400 root $SWAT swat EOF hup_inetd fi # one valid entry in /etc/inetd.conf now elif [ $ret -eq 1 ] ; then if [ $verbose -ne 0 ] ; then echo "one SWAT entry in /etc/inetd.conf" fi if [ $disable -ne 0 ] ; then # backup /etc/inetd.conf if [ $debugmode ] ; then cp /etc/inetd.conf /tmp fi cp /etc/inetd.conf $tmpfile cat $tmpfile | \ egrep -v '^[ ]*swat[ ].*[ ]swat[ ]*' > \ /etc/inetd.conf hup_inetd fi # something wrong in /etc/inetd.conf else echo 'something wrong with your /etc/inetd.conf' 1>&2 echo 'OR you may add any option flags (like -a) to swat!' 1>&2 egrep '^[ ]*swat[ ][^0-9]' /etc/services 1>&2 exit 3 fi # remove tmp file if [ $debugmode -ne 1 ] ; then rm -f $tmpfile fi exit 0