#ident "@(#)/usr/local/sysadmin/USERMGR 2.3 03/18/91" Author: Mr Reinhard Pfaffinger
# Office: 1924 CSS/SDA, Randolph AFB, Texas 78150
# Phone: AV 487-8649  Comm: (512) 652-8649
#
# This script allows the system administrator to manage user configurations.
#
# Ver 2.0 - Added UID/GID modification module: MOD-UID_GID.  RP 8-5-91
#
# Ver 2.1 - Added disable shadow/enable shadow to add/delete user functions.
#             RP 9-23-91
#
# Ver 2.11 - Allowed user to return to menu via NULL entry from chgusrdesc 
#              function.  RP 10-11-91
#
# Ver 2.2 - Allowed for users' directories to be relocated across file systems.
#             RP 9-23-92
#
# Ver 2.21 - Added 'echo' statement in the 'mvpardir' function to display the
#              new user's current directory.  RP 4-15-93
#
# Ver 2.3 - Added option 12 to allow the system administrator to modify the
#           UserID and Parent directory defaults globally.
#

chkfortty() {
             if [ "$TERM" = "tty" ]
               then
                   echo  
             else
                 tput clear 
             fi
            }

rtn() {
       echo "\nPress <RETURN> ... \c"
       read RTN
      }

modpswd() {
           echo "\nModifying /etc/passwd ... \c"
           passmgmt -m -h $NEWDIR $NEWLOGIN 
           echo "done."
          }

modenv() {
          if [ -f /usr/mail/$OLDLOGIN ]
            then
              echo "\nRenaming system mailbox for <$NEWLOGIN> ... \c"
              mv /usr/mail/$OLDLOGIN /usr/mail/$NEWLOGIN
              echo "done."
          fi
          if [ -f $NEWDIR/.elm/elmrc ]
            then
                echo "\nModifying '$NEWDIR/.elm/elmrc' file ... \c"
                sed -e "s/$OLDLOGIN/$NEWLOGIN/g" $NEWDIR/.elm/elmrc > /tmp/$$.$$
                cp /tmp/$$.$$ $NEWDIR/.elm/elmrc
                echo "done."
                /bin/rm -f /tmp/$$.$$
          fi
         }

mvpardir() {
            OLDLOGIN="`cat /tmp/oldlogin`"
            NEWLOGIN="`cat /tmp/newlogin`"
            GID=`egrep "^$NEWLOGIN:" /etc/passwd | cut -d: -f4`
            GROUPID=`egrep ":$GID:" /etc/group | cut -d: -f1`
            unset GID
            OLDDIR=`egrep "^$NEWLOGIN:" /etc/passwd | cut -d: -f6`
            chkfortty
            echo "User $NEWLOGIN's current directory is: $OLDDIR."
            echo "\nEnter the name of the new parent (HOME) directory. Please provide the full"
            echo "path specification <RETURN> =>> \c"
            read NEWDIR
            if [ "$NEWDIR" = "" ]
              then
                  :
            else
                if [ "`echo $NEWDIR | cut -c1-4 | fgrep usr`" = "" ]
                  then
                      echo "\n\tNew parent directory may not be placed within root(/)!\c"
                      echo; rtn
                      mvpardir 
                else
                    if [ -d "$NEWDIR" ]
                      then
                          echo "\n\t'$NEWDIR' already exists!\c"
                          echo; rtn
                          mvpardir 
                    else
                        if [ "`echo $OLDDIR | cut -d/ -f2`" != "`echo $NEWDIR | cut -d/ -f2`" ]
                          then
                               echo "\nMoving '$OLDDIR' to '$NEWDIR' ... "
                               mkdir $NEWDIR
                               chown $NEWLOGIN $NEWDIR
                               chgrp $GROUPID $NEWDIR
                               modpswd
                               PWD=`pwd`
                               cd $OLDDIR
                               echo "\nMoving files. Please wait ... \c"
                               tar cvf /tmp/$OLDLOGIN.tar . >/dev/null 2>/dev/null
                               cd $NEWDIR
                               tar xvf /tmp/$OLDLOGIN.tar >/dev/null 2>/dev/null
                               /bin/rm -f /tmp/$OLDLOGIN.tar
                               /bin/rm -r $OLDDIR
                               sync; sync
                               echo "done.\n\nChanging permissions. Please wait ... \c"
                               find $NEWDIR -name "*" -print -exec chown $NEWLOGIN {} \; >/dev/null 2>/dev/null
                               find $NEWDIR -name "*" -print -exec chgrp $GROUPID {} \; >/dev/null 2>/dev/null
                               echo "done."
                               modenv
                               cd $PWD
                               rtn
                        else
                            modpswd
                            echo "\nRenaming old parent directory to '$NEWDIR' ... \c"
                            mv $OLDDIR $NEWDIR
                            echo "done."
                            modenv
                            rtn
                        fi
                    fi
                fi
            fi 
            bin/rm -f /tmp/*login 2>/dev/null
           }
chguserid() {
             chkfortty
             /usr/local/sysadmin/chgloginid
             echo "\nDo you wish to change the user's parent (HOME) directory? y/n <RETURN> =>> \c"
             read USRRESP
             case "$USRRESP" in
                y|Y)
                    mvpardir
                    ;;
                *)
                  /bin/rm -f /tmp/*login
                  rtn
                  ;;
             esac
            }
chgusrdesc() {
              echo "Which user would you like to change the description for? <RETURN> =>> \c"
              read USR
              if [ "$USR" = "" ]
                then
                    return
              fi
              USROK=`egrep "^$USR:" /etc/passwd`
              if [ "$USROK" = "" ]
                then
                    echo "\n\t\tInvalid User ID!\c"
                    sleep 2; echo "\n\n"
                    chgusrdesc
              else
                  echo "\nEnter the new description (less than 128 characters) <RETURN> =>> \c"
                  read NEWDESC
                  echo "\nChanging description for user '$USR' ... \c"
                  passmgmt -m -c "$NEWDESC" $USR
                  echo "done."
                  rtn
              fi
             } 
while :
 do
   chkfortty
   echo "                   <<< AT&T 3B2/600G User Management Menu >>>\n"
   echo "                       1. Query Number of Login Users on '`uname -n`'"
   echo "                       2. Add a User"
   echo "                       3. Add a Group"
   echo "                       4. Remove a User"
   echo "                       5. Remove a Group"
   echo "                       6. Modify User Configuration"
   echo "                       7. Modify Group Name"
   echo "                       8. List Login Users on '`uname -n`'"
   echo "                       9. List Groups on '`uname -n`'"
   echo "                      10. Examine /etc/passwd File"
   echo "                      11. Enable/Disable SHADOW Password Facility"
   echo "                      12. Default Group ID/Parent Directory Modification\n"
   echo "				q or Q to quit\n"
   echo "			   Enter response <RETURN> =>> \c"
   read RESP
   case "$RESP" in
      1)
        chkfortty
        /usr/local/numberofusers
        rtn
        continue
        ;;
      2)
        chkfortty
        /usr/bin/pwunconv > /dev/null 2>/dev/null
        /usr/local/sysadmin/adduser
        /usr/bin/pwconv > /dev/null 2>/dev/null
        continue
        ;;
      3)
        chkfortty
        /usr/local/sysadmin/addgroup
        continue
        ;;
      4)
        chkfortty
        /usr/bin/pwunconv > /dev/null 2>/dev/null
        /usr/local/sysadmin/deluser
        /usr/bin/pwconv > /dev/null 2>/dev/null
        continue
        ;;
      5)
        chkfortty
        /usr/local/sysadmin/delgroup
        continue
        ;;
      6)
        while :
         do
        chkfortty
        echo "\t\tSelect one of the following options:\n"
        echo "\t\t\t1. Change Login ID/Move Parent Directory"
        echo "\t\t\t2. Change User Password"
        echo "\t\t\t3. Change User Login Shell"
        echo "\t\t\t4. Change User Description"
        echo "\t\t\t5. Change User ID/Group ID\n"
        echo "\t\t\tq or Q to quit\n"
        echo "\t\tEnter response <RETURN> =>> \c"
        read MODUSRCFG
        case "$MODUSRCFG" in
           1)
             chguserid 
             ;;
           2)
             chkfortty
             /usr/local/sysadmin/chgpasswd
             ;;
           3)
             chkfortty
             /usr/local/sysadmin/chgshell
             ;;
           4)
             chkfortty
             chgusrdesc
             ;;
           5)
             /usr/local/sysadmin/MOD-UID_GID
             ;;
         q|Q)
             break;;
        esac
         done
        continue
        ;;
      7)
        /usr/local/sysadmin/chgname
        continue
        ;;
      8)
        /usr/local/listusers
        rtn
        continue
        ;;
      9)
        /usr/local/listgroups
        rtn
        continue
        ;;
     10)
        /usr/local/passchk
        rtn
        continue
        ;;
     11)
        chkfortty
        echo "Do you wish to enable or disable SHADOW passwording? e/d <RETURN> =>> \c"
        read SHADOW
        case "$SHADOW" in
           e|E)
               if [ -f /etc/shadow ]
                 then
                     echo "\n\tSHADOW passwording already enabled!\c"
                     sleep 3
               else
                   /usr/bin/pwconv
                   echo "\n\tSHADOW passwording enabled!\c"
                   sleep 3
               fi
               ;;
           d|D)
               if [ ! -f /etc/shadow ]
                 then
                     echo "\n\tSHADOW passwording already disabled!\c"
                     sleep 3
               else
                   /usr/bin/pwunconv
                   echo "\n\tSHADOW passwording disabled!\c"
                   sleep 3
               fi
               ;; 
        esac
        ;;
     12)
        chkfortty
        echo "		*** Default Group ID/Parent Login Directory Modification ***\n"
        echo "The following defaults currently exist:\n"
        tail -2 /usr/local/sysadmin/defadduser
        while :
         do
           prevgid=`egrep "^defgroup" /usr/local/sysadmin/defadduser | cut -d= -f2`
           prevpdir=`egrep "^defparent" /usr/local/sysadmin/defadduser | cut -d= -f2`
           echo "\nEnter the new default Group ID <RETURN>.  Null entry bypasses."
           echo "q or Q to Quit =>> \c"
           read newgid
           case "$newgid" in
            "")
               echo "\n\tNull response.  Default Group ID not changed."
               ;;
            q|Q)
                break
                ;;
            *)
              if [ "$newgid" = "$prevgid" ]
                then
                    echo "\n\tPrevious Group ID, $prevgid, and intended Group ID, $newgid, are identical.\n\n\t\tNo changes made."
              else
                  echo "\nModifying 'defgroup' in /usr/local/sysadmin/defadduser ... \c" 
                  ed /usr/local/sysadmin/defadduser<<! >/dev/null 2>/dev/null
/^defgroup=/
s/$prevgid/$newgid/
w
Q
!
                  echo "done."
              fi
              ;; 
           esac
           echo "\nEnter the new default parent directory <RETURN>.  Null entry bypasses."
           echo "q or Q to Quit =>> \c"
           read newpdir
           case "$newpdir" in
            "")
               echo "\n\tNull response.  Default parent directory not changed."
               ;;
            q|Q)
                break
                ;;
            *)
              if [ "$newpdir" = "$prevpdir" ]
                then
                    echo "\n\tPrevious parent directory, $prevpdir, and intended parent\n\tdirectory, $newpdir, are identical.\n\n\t\tNo changes made."
              else
                  tmp="defparent=$newpdir"
                  echo "\nModifying 'defparent' in /usr/local/sysadmin/defadduser ... \c"
                  ed /usr/local/sysadmin/defadduser<<! >/dev/null 2>/dev/null
/^defparent=/d
w
Q
!
                  echo $tmp >> /usr/local/sysadmin/defadduser
                  echo "done."
              fi
              ;; 
           esac
         if [ -z "$newgid" ] && [ -z "$newpdir" ]
           then
               break
         else
             chkfortty
             echo "\nThe new defaults are:\n"
             tail -2 /usr/local/sysadmin/defadduser
         fi
         done
        rtn
        ;;
    q|Q)
        exit 0
        ;;
      *)
        echo "\n\t\t\t\tInvalid response!\c"
        sleep 2
        continue
        ;;
  esac
 done
