#!/bin/sh
# Version: 16.09.96
# Geaendert durch: JPB
# add a user to /etc/netpasswd/passwd 
#               /etc/netgroup
#               mail:/usr1/usr/lib/aliases
#               make homedirectory on fob
#               make newaliases on mailslv1 and mail
#
# All validations first
#
#	check if the user is logged in as superuser
#
ARCHDIR=/home/unixsoft/accmgr/Auto-Account/Bearbeitet
cd /home/unixsoft/accmgr/addusr
rm -f /tmp/$$
touch /tmp/$$
USER=`ls -l /tmp/$$ | awk '{ print $3 }' `
rm /tmp/$$
if [ "$USER" != "root" ]
then
	echo "Permission denied."
	echo "You must be logged in as superuser."
	exit 1
fi
echo "adduser-fob create a new user with homedirectory on fob"
echo "adduser-fob try to change: /etc/netpasswd/passwd"
echo "                           /etc/netgroup"
echo "                           fob:/export/volume?/...."
echo "                           mail:/usr1/usr/lib/aliases"
echo "                           mail:/etc/mail/aliases and restart sendmail"
echo "                           mailslv1:/usr/var/adm/aliases and restart sendmail"
gsw=0
while [ $gsw -eq 0 ]
do

sw=0
while [ $sw -eq 0 ]
do
  if [ "x$username" != "x" ] 
    then
      sw=1
  fi
  echo -n "username (max. 8 Zeichen) [$username]: "
  read answer
  if [ "x$answer" != "x" ]
    then
      username=$answer
      sw=1
  fi
  if [ "x$username" = "x" ] 
    then
     sw=0
     continue
  fi
  #	check if the length of input is longer than 8
  length=`echo $username | wc -c`
  if [ "$length" -gt "9" ]
  then 
	echo "User name can not be longer than eight characters."
        sw=0
        continue
  fi

  #check for illegal character (s)

  if echo "$username" | grep "[^0-9a-z]" > /dev/null
  then 
	echo "Only numbers & lower case letters are permitted in user name." 
        sw=0
        continue
  fi

  #	check if login name already exists

  grep "^$username:" /etc/netpasswd/passwd >/dev/null
  rc=$?
  if [ $rc -eq 0 ]
  then
	echo "$user name already exists on your system. Choose another user name." 
        echo -n "Passwd-entry: "
        grep "^$username:" /etc/netpasswd/passwd
        sw=0
        continue
  fi
  X=`grep "^$username " /vol/mail-adm/lib/aliases`
  if [ "x$X" != "x" ]
    then
      echo $username already exist in alias-file
      sw=0
      continue
  fi
  X=`grep "^$username:" /vol/mail-adm/lib/aliases`
  if [ "x$X" != "x" ]
    then
      echo $username already exist in alias-file
      sw=0
      continue
  fi
done

sw=0
while [ $sw -eq 0 ] 
do
   if [ "x$grpname" != "x" ]
     then
       sw=1
   fi
   echo "Groups: mi88 mi89 mi90 mi902 mi91 mi92 mi93"
   echo "        mi94 mi95 mi96 mi97 mi98 mi99 mi00 mi01"
   echo "        lehrer97"
   echo "        schueler nebenf nebenf96 nebenf97 nebenf98 nebenf99 nebenf00"
   echo "        nebenf01"
   echo "        guests guest96 guest97 guest98 guest99 guest00 guest01"
   echo -n "groupname [$grpname]: "
   read answer
   if [ "x$answer" != "x" ]
     then 
       grpname=$answer
       sw=1
   fi
   if [ $sw -eq 0 ]
     then
       continue
   fi
   #  check grpname

   grep "^$grpname:" /etc/group >/dev/null
   rc=$?
   if [ $rc -ne 0 ]
     then
       echo "Group $grpname does not exist!"
       sw=0
       continue
   fi
done

sw=0
while [ $sw -eq 0 ] 
do
   if [ "x$ngrpname" != "x" ]
     then
       sw=1
     else 
       ngrpname=$grpname
       sw=1
   fi
   echo "Net-Groups: mi88 mi89 mi90 mi902  mi91 mi911 mi92 mi93 mi93-ws"
   echo "       mi94 mi941 mi95 mi96 mi961 mi97 mi98 mi99 mi00" mi01
   echo "       lehrer97"
   echo "       schueler schuel96 schuel97 schuel98 schuel99 schuel00 schuel01"
   echo "       nebenf nebenf95 nebenf96 nebenf97 nebenf98 nebenf99 nebenf00"
   echo "       nebenf01"
   echo "       inf-ass guests guest96 guest97 guest98 guest99 guest00 guest01"
   echo -n "net-groupname [$ngrpname]: "
   read answer
   if [ "x$answer" != "x" ]
     then 
       ngrpname=$answer
       sw=1
   fi
   if [ $sw -eq 0 ]
     then
       continue
   fi
   #  check ngrpname
   grep "^$ngrpname " /etc/netgroup >/dev/null
   rc=$?
   if [ $rc -ne 0 ]
     then
       echo "Net-Group $ngrpname does not exist!"
       sw=0
       continue
   fi
done
sw=0
while [ $sw -eq 0 ]
do
   if [ "x$immnr" != "x" ]
     then
       sw=1
   fi
   echo -n "Immatrikulationsnummer [$immnr]: "
   read answer
   if [ "x$answer" != "x" ]
     then
       immnr=$answer
       sw=1
   fi
   if [ $sw -eq 0 ]
     then
       continue
   fi
done

sw=0
while [ $sw -eq 0 ]
do
   if [ "x$immjahr" != "x" ]
     then
       sw=1
   fi
   echo -n "Immatrikulationsjahr [$immjahr]: "
   read answer
   if [ "x$answer" != "x" ]
     then
       immjahr=$answer
       sw=1
   fi
   if [ $sw -eq 0 ]
     then
       continue
   fi
done

#
#
#	Set default group id 
#
groupid=`grep "$grpname:" /etc/group | awk -F: '{printf $3}'`
#
lastuid=`grep ":$groupid:" /etc/netpasswd/passwd | awk -F: ' { xx = $3 } END { print xx } '`
if [ "x$lastuid" = "x" ]
  then
    lastuid=$groupid
fi
nuid=`bc <<EOF
$lastuid+1
EOF`
if [ "x$userid" = "x" ]
  then 
    userid=$nuid
fi
sw=0
while [ $sw -eq 0 ]
do
  if [ "x$userid" != "x" ]
    then
      sw=1
  fi
  echo -n "uid [$userid]: "
  read answer
  if [ "x$answer" != "x" ]
    then
      userid=$answer
      sw=1
  fi
  if [ $sw -eq 0 ] 
    then
      continue
  fi
  if echo "$userid" | grep "[^0-9]" > /dev/null
  then 
	echo "Only numbers  are permitted in uid." 
        sw=0
        continue
  fi
  if [ "$userid" -gt 50000 -o "$userid" -lt 100 ]
	then 
	echo "User ID number out of legal range (100-50000)."
        sw=0
        continue
  fi

  #	Check if input userid exists already

  if grep "^[^:]*:[^:]*:0*$userid:" /etc/netpasswd/passwd > /dev/null
  then
	echo "User Id already used on system, please choose another."
        sw=0
        continue
  fi
done
# read comment
echo -n "Comment [$comment]: "
read answer
if [ "x$answer" != "x" ]
  then
    comment=$answer
fi

if [ "x$shell" = "x" ] 
  then
    shell="/bin/csh"
fi
echo "shells:"
echo "       csh   sh ksh tcsh bash"
echo -n "shell [$shell]: "
read answer
if [ "x$answer" != "x" ]
   then 
      case $answer in
        "sh")   shell=/bin/sh ;;
        "csh")  shell=/bin/csh ;;
        "bash") shell=/usr/local/bin/bash ;;
        "ksh")  shell=/usr/local/bin/ksh ;;
        "tcsh") shell=/usr/local/bin/tcsh ;;
     esac
fi
#  check shell

# compute maschine and vol from grpname
#  fob-vol1: mi88 mi93 mi94 mi99
#  fob-vol2: mi89 mi90 mi901 mi902 mi903 mi95
#            mi97 mi01 nebenf01 guest01
#  fob-vol3: mi91 mi92 mi98
#  fob-vol4: fstudent guests lehrer92 lehrer93 lehrer95 nebenf schueler
#            mi96 lehrer96 nebenf96 guest96 guest98 guest99
#            lehrer97 guest97 nebenf97 nebenf98 nebenf99
#  fob-vol5: mi00 nebenf00 guest00
maschine=fob
case $grpname in
   "mi88") vol=vol1 ;;
   "mi93") vol=vol1 ;;
   "mi94") vol=vol1 ;;
   "mi99") vol=vol1 ;;
   "mi89") vol=vol2 ;;
   "mi90") vol=vol2 ;;
   "mi901") vol=vol2 ;;
   "mi902") vol=vol2 ;;
   "mi903") vol=vol2 ;;
   "mi95") vol=vol2 ;;
   "mi01") vol=vol2 ;;
   "nebenf01") vol=vol2 ;;
   "guest01") vol=vol2 ;;
   "mi91") vol=vol3 ;;
   "mi92") vol=vol3 ;;
   "fstudent") vol=vol4 ;;
   "guests") vol=vol4 ;;
   "lehrer92") vol=vol4 ;;
   "lehrer93") vol=vol4 ;;
   "lehrer95") vol=vol4 ;;
   "nebenf") vol=vol4 ;;
   "schueler") vol=vol4 ;;
   "mi96") vol=vol4 ;;
   "lehrer96") vol=vol4 ;;
   "nebenf96") vol=vol4 ;;
   "guest96") vol=vol4 ;;
   "mi97") vol=vol2 ;;
   "lehrer97") vol=vol4 ;;
   "nebenf97") vol=vol4 ;;
   "guest97") vol=vol4 ;;
   "mi98") vol=vol3 ;;
   "lehrer98") vol=vol4 ;;
   "nebenf98") vol=vol4 ;;
   "guest98") vol=vol4 ;;
   "nebenf99") vol=vol4 ;;
   "guest99") vol=vol4 ;;
   "mi00") vol=vol5 ;;
   "guest00") vol=vol5 ;;
   "nebenf00") vol=vol5 ;;
esac
if [ "x$vol" = "x" ] 
  then 
    echo wrong groupname: $grpname
    exit 1
fi

logdir="/vol/${maschine}-${vol}/${grpname}/${username}"
#if test -f "$logdir" -o -d "$logdir" -o -h "$logdir"
#then
#	echo "$logdir directory already exists. Choose another HOME directory."
#	exit 1
#fi
echo "$logdir" | grep "[^0-9_/.a-zA-Z-]" >/dev/null
rc=$?
if [ "$rc" = "0" ]
then
	echo "Directory name contains illegal characters." 
	exit 1
fi

#
#       check maschine-name
#
grep "^$maschine" /etc/hosts > /dev/null
rc=$?
if [ $rc -eq 0 ]
then
   echo "maschine name unknown"
   exit 1
fi

#
#	check volume
#
if [ "$vol" != "vol1" -a "$vol" != "vol2" -a "$vol" != "vol3" -a "$vol" != "vol4" -a "$vol" != "vol5" ]
then
	echo "The volume can be: vol1, vol2, vol3, vol4 or vol5."
	exit 1
fi
if [ "$vol" = vol1 ]
then 
    volume="volume1"
fi
if [ "$vol" = "vol2" ]
then 
    volume="volume2"
fi
if [ "$vol" = "vol3" ]
then 
    volume="volume3"
fi
if [ "$vol" = "vol4" ]
then 
    volume="volume4"
fi
if [ "$vol" = "vol5" ] 
then
    volume="volume5"
fi
#
#	Echo entry into /etc/netpasswd/passwd
#
passent="${username}::${userid}:${groupid}:${comment}:${logdir}:${shell}"
echo Net-Group: $ngrpname
echo $passent
echo -n "korrekt?? [j/n]: "
read answer
if [ "x$answer" = "xj" ]
  then
    gsw=1
  else
    gsw=0
fi
done
echo Start user $username in /etc/netpasswd/passwd
rm -f /tmp/$$passwdentry
echo $passent > /tmp/$$pwdentry
rm -f /etc/netpasswd/opasswd
cp /etc/netpasswd/passwd /etc/netpasswd/opasswd
cat /etc/netpasswd/opasswd /tmp/$$pwdentry > /etc/netpasswd/passwd
rm -f /tmp/$$pwdentry
echo done
#
gsw=0
while [ $gsw -eq 0 ]
do
echo "Enter a password for $username:"
/bin/passwd -F /etc/netpasswd/passwd $username
echo -n "Passwort ok ? [j/n]: "
read answer
if [ "x$answer" = "xj" ]
  then
    gsw=1
  else
    gsw=0
fi
done
# insert in /etc/netgroup
echo Start user $username in $ngrpname in /etc/netgroup
rm -f /etc/netgroup.new
cp /etc/netgroup /etc/netgroup.old
/usr/bin/gawk '{ print ; if ( $0 ~ (GROUP " ")) { printf ("        (,%s,) \\\n",USER); } }'  USER=$username GROUP=\^$ngrpname /etc/netgroup >/etc/netgroup.new
mv /etc/netgroup.new /etc/netgroup
echo done
#
#	Make home directory 
#
umask 002

ssh fob /export/volume1/rbg/addusr/mkhome $username $grpname $volume $userid
ssh -l root samy /www/server/etc/scripts/install/gen-new-user $username $logdir $userid $groupid
# insert user in mail:/usr1/usr/lib/aliases
#  $username : $username@mailslv1 
ssh -l root mail /usr1/usr/bin/addmail $username mailslv1 $grpname
echo -n "Entry in mail:/usr1/usr/lib/aliases is:   "
grep "^$username " /vol/mail-adm/lib/aliases
sw=0
while [ $sw -eq 0 ]
do
  echo -n "Weiterer alias-Name [Nein=<NL>]:  "
  read alias
  if [ "x$alias" != "x" ]
  then
    X=`grep "^$alias " /vol/mail-adm/lib/aliases`
    if [ "x$X" != "x" ]
    then
      echo $alias already exist in alias-file
      continue
    fi
    ssh -l root mail /usr1/usr/bin/addalias $username mailslv1 $grpname $alias
  else
    echo keine weiteren alias-Eintragungen
    sw=1
  fi
done
FILE=$ARCHDIR/$username-$immjahr-$immnr
echo "Name=\"$comment\""   > $FILE
echo "ImmNr=\"$immnr\""   >> $FILE
echo "ImmJahr=\"$immjahr\""   >> $FILE
echo "UID=\"$username\""   >> $FILE
echo "GRUPPE=\"$grpname\""   >> $FILE
echo "DATE=\"`date`\""   >> $FILE
##ssh -l root mail /usr1/usr/bin/newali
echo $username >>adduser.prot
echo done
echo " "
echo "!! Achtung !!"
echo " "
echo ./newali, wenn alle Nutzer eingerichtet!
echo "make: um 16.oo Uhr oder in /var/yp von Hand starten."
echo " "
