#!/bin/sh
set -u  # force better programming and ability to use check for not set

if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# this is cheap argument processing. Must be in this order when checking.
if [ "${1:-}" = "-a" ] ; then ALARM=1; shift; else ALARM=0; fi
if [ "${1:-}" = "-q" ] ; then QUIET=1; shift; else QUIET=0; fi

set +u
. /usr/local/etc/setups.sh
setup enstore
set -u

UNAME="/bin/uname"
ECHO="/bin/echo"
EGREP="/bin/egrep"
AWK="/bin/awk"
WC="/usr/bin/wc"
DATE="/bin/date"
if [ -x /usr/bin/cut ]; then
  CUT="/usr/bin/cut"
elif [ -x /bin/cut ]; then
  CUT="/bin/cut"
else
  echo "Can not find cut"
  exit 1
fi
SED="/bin/sed"
KTUTIL="/usr/krb5/sbin/ktutil"

node=`$UNAME -n| sed -e 's/\([^\.]\)\..*/\1/'`
gang=`/home/enstore/enstore/sbin/gang`

LOG=/tmp/netscan.log

# function to log messages
logit() { $ECHO `$DATE` "$1" >>$LOG 2>&1
	  $ECHO "$1"
	}

# first check the /etc/krb5.keytab
keys="`$ECHO 'rkt /etc/krb5.keytab
l' |$KTUTIL|$EGREP -v '^ktutil|^---'|$AWK '{print $NF}'`"

# there needs to be 2 or 4 keys in this file only
nkeys=`$ECHO $keys |$WC -w|$SED -e 's/ //g'`
if [ `echo $keys | $EGREP -c 'PILOT'` -ne 0 ]; then mkeys=4; else mkeys=2; fi
if [ $mkeys -ne $nkeys ]; then
   msg="Wrong number of keys ($nkeys) in /etc/krb5.keytab"
   logit "$msg"
   if [ $ALARM -eq 1 ]; then
      enstore alarm --raise --severity "A" --root-error "$msg"
   fi
elif [ $QUIET -ne 1 ]; then
   logit "Number of keys in /etc/krb5.keytab, ok"
fi

# now check for type = host or ftp, node name is correct, and realm is FNAL
for key in $keys; do
  kind=`$ECHO $key | $CUT -f1 -d/`
  whose=`$ECHO $key| $CUT -f2 -d/`
  whose_n=`$ECHO $whose|$CUT -f1 -d@`
  whose_d=`$ECHO $whose|$CUT -f2 -d@`
  BAD=0
  if [ `echo $kind | $EGREP -c 'ftp|host'` -ne 1 ]; then BAD=1; fi
  if [ "$whose_n" != "$node.fnal.gov" ]; then BAD=2; fi
  if [ "$whose_d" != FNAL.GOV -a "$whose_d" != PILOT.FNAL.GOV ]; then BAD=3; fi
  #echo $kind $whose_n $whose_d $BAD
  if [ $BAD -ne 0 ]; then
    msg="BAD key in /etc/krb5.keytab, $key bad type $BAD"
    logit "$msg"
    if [ $ALARM -eq 1 ]; then
       enstore alarm --raise --severity "A" --root-error "$msg"
    fi
  elif [ $QUIET -ne 1 ]; then
    logit "Key in /etc/krb5.keytab, $key ok"
  fi
done


# now check the /local/ups/kt/enstorekt
keys="`$ECHO 'rkt /local/ups/kt/enstorekt
l' |$KTUTIL|$EGREP -v '^ktutil|^---'|$AWK '{print $NF}'`"

# there needs to be 1 or 2 keys in this file only
nkeys=`$ECHO $keys |$WC -w|$SED -e 's/ //g'`
if [ `echo $keys | $EGREP -c 'PILOT'` -ne 0 ]; then mkeys=2; else mkeys=1; fi
if [ $mkeys -ne $nkeys ]; then
   msg="Wrong number of keys ($nkeys) in /local/ups/kt/enstorekt"
   logit "$msg"
   if [ $ALARM -eq 1 ]; then
      enstore alarm --raise --severity "A" --root-error "$msg"
   fi
elif [ $QUIET -ne 1 ]; then
   logit "Number of keys in /local/ups/kt/enstorekt, ok"
fi

# now check for type=enstore, type2=cd, node name is correct, and realm is FNAL
for key in $keys; do
  kind1=`$ECHO $key | $CUT -f1 -d/`
  kind2=`$ECHO $key | $CUT -f2 -d/`
  whose=`$ECHO $key| $CUT -f3 -d/`
  whose_n=`$ECHO $whose|$CUT -f1 -d@`
  whose_d=`$ECHO $whose|$CUT -f2 -d@`
  BAD=0
  if [ "$kind1" != "enstore" ]; then BAD=4; fi
  if [ "$kind2" != "cd" ]; then BAD=5; fi
  if [ "$whose_n" != "$node.fnal.gov" ]; then BAD=6; fi
  if [ "$whose_d" != FNAL.GOV -a "$whose_d" != PILOT.FNAL.GOV ]; then BAD=7; fi
  #echo $kind1 $kind2 $whose_n $whose_d $BAD
  if [ $BAD -ne 0 ]; then
    msg="BAD key in /local/ups/kt/enstorekt, $key bad type $BAD"
    logit "$msg"
    if [ $ALARM -eq 1 ]; then
       enstore alarm --raise --severity "A" --root-error "$msg"
    fi
  elif [ $QUIET -ne 1 ]; then
    logit "Key in /local/ups/kt/enstorekt, $key ok"
  fi
done
