#ident  "@(#)/usr/local/sysadmin/MOD-UID_GID 1.2 08/05/91" Author: Mr Reinhard Pfaffinger
# Office: 3324 CSS/SDA, Randolph AFB, Texas 78150
# Phone: AV 487-8649  Comm: (512) 652-8649
#
# This script will modify the UID (field 3 in /etc/passwd) and/or GID
# (field 4 in /etc/passwd) fields for a specified user.  All files under
# the user's home directory are also "chown"ed to prevent the old
# UID to appear as the owner of the user's files.
# 
# Ver 1.1 - Under chgUID module, added 'chown' command so that the
#           /usr/mail/$LOGIN file would have proper permissions.  RP 12-31-91
#
# Ver 1.2 - Allowed UID/GID processing of 100 or greater.  RP 9-23-92
#

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

chgUID() {
          HDIR=`echo $TMP | cut -d: -f6`
          echo "\nModifying /etc/passwd file ... \c"
          passmgmt -m -u $U $LOGIN
          echo "done."
          echo "\nModifying UID for all files under $HDIR ... \c"
          find $HDIR -name "*" -print -exec chown $LOGIN {} \; > /dev/null 2>/dev/null
          if [ -f /usr/mail/$LOGIN ]
            then
                chown $LOGIN /usr/mail/$LOGIN
          else
              :
          fi
          echo "done."
          sleep 3
         }

chgGID() {
          HDIR=`echo $TMP | cut -d: -f6`
          echo "\nModifying /etc/passwd file ... \c"
          passmgmt -m -g $G $LOGIN
          echo "done."
          echo "\nModifying GID for all files under $HDIR ... \c"
          find $HDIR -name "*" -print -exec chgrp $1 {} \; > /dev/null 2>/dev/null
          echo "done."
          sleep 3
         }

chkforGID() {
             chkfortty
             echo "The following groups are available:\n"
             for gid in `cut -d: -f1,3 /etc/group`
               do
                 if [ `echo $gid | cut -d: -f2` -ge 100 ]
                   then
                       echo $gid | sed -e "s/^/Group Name - /g" -e "s/:/	Group ID: /g"
                 fi
               done | pg -n18 -p"Press <RETURN> to continue; 'q' or 'Q' to quit ... "
             promptg
             GROUPNAME=`egrep ":$G:" /etc/group | cut -d: -f1`
             if [ "$GROUPNAME" = "" ]
               then
                   echo "\n\t\tGroup '$G' does not exist!\c"
                   cat /dev/null > /tmp/$$.$$
                   sleep 3; echo
                   echo "\nDo you wish to create a new Group ID '$G'? y/n <RETURN> =>> \c"
                   read RESP
                   case $RESP in
                     y|Y)
                         /usr/local/sysadmin/addgroup
                         return
                         ;;
                     *)
                       return
                       ;;
                   esac
             else
                 echo "\nYou have selected GID '$G', Group Name '$GROUPNAME'."
                 chgGID $GROUPNAME
             fi
            }

chkforuid() {
             for uid in `cut -d: -f1,3,5 /etc/passwd | sed -e "s/ /%/g"`
               do
                 if [ `echo $uid | cut -d: -f2` = $1 ]
                   then
                       echo "\n\tUID '$1' is already allocated to `echo $uid | cut -d: -f1,3 | sed -e 's/:/, /g' -e 's/%/ /g'`."
                       echo "\n\t\t\tChoose another.\c"
                       cat /dev/null > /tmp/$$
                       sleep 3; echo
                 fi
               done
            }

promptu() {
           echo "\nEnter the new UID <RETURN> =>> \c"
           read U
           if [ $U -lt 100 ]
             then
                 echo "\n\t\tYou must enter a number greater than 99!\c"
                 sleep 3
                 promptu
           fi
          }

promptg() {
           echo "\nEnter the new GID <RETURN> =>> \c"
           read G
           if [ $G -lt 100 ]
             then
                 echo "\n\t\tYou must enter a number greater than 99!\c"
                 sleep 3; echo
                 promptg
           fi
          }

verifyUID() {
             while [ -f $1 ]
              do
                /bin/rm -f $1
                promptu
                chkforuid $U
              done
            }

verifyGID() {
             while [ -f $1 ] 
              do
                /bin/rm -f $1 
                chkforGID
              done
            }

queryug() {
           case "$1" in
              u)
                promptu
                chkforuid $U
                verifyUID /tmp/$$
                ;;
              g)
                chkforGID
                verifyGID /tmp/$$.$$
                ;;
             ug)
                promptu
                chkforuid $U
                verifyUID /tmp/$$
                chgUID
                chkforGID
                verifyGID /tmp/$$.$$
                ;;
           esac 
          }

chgquery() {
            if [ "$1" = "" ]
              then 
                  echo "\n\t\t'$LOGIN' does not exist!\c"
                  sleep 3
                  return
            else
                UID=`echo $TMP | cut -d: -f3`
                GID=`echo $TMP | cut -d: -f4`
                if [ $UID -lt 100 ] && [ $GID -lt 100 ]
                  then
                      echo "\n\t\tBoth UID and GID for '$LOGIN' are less than 100.\c"
                      sleep 3
                      return
                else
                    if [ $UID -lt 100 ]
                      then
                          echo "\n\t\tUID for '$LOGIN' is less than 100.\c"
                          sleep 3
                          return
                    else
                        if [ $GID -lt 100 ]
                          then
                              echo "\n\t\tGID for '$LOGIN' is less than 100.\c"
                              sleep 3
                              return
                        else
                            :
                        fi
                    fi
                fi
                echo "\nUser <`echo $TMP | cut -d: -f1,5 | sed -e 's/:/, /g'`> has the following UID/GID:\n"
                echo $TMP | cut -d: -f3,4 | sed -e "s/^/UID = /g" \
-e "s/:/	GID = /g"
                echo "\nEnter 'u' to change UID, 'g' to change GID, or 'ug' to change both <RETURN>"
                echo "=>> \c"
                read Q
                case $Q in
                  "")
                     return
                     ;;
                  u|U)
                      queryug u
                      chgUID
                      ;;
                  g|G)
                      queryug g
                      ;;
          ug|UG|uG|Ug)
                      queryug ug
                      ;;
                esac
            fi
           }

#
# Main module.
#

while :
 do
   chkfortty
   echo "Enter the user name for which the UID/GID is to be changed; 'q' to quit <RETURN>"
   echo "=>> \c"
   read LOGIN
   case $LOGIN in
     "")
        continue
        ;;
     q|Q)
         break
         ;;
     *)
       TMP=`egrep "^$LOGIN:" /etc/passwd`
       ;;
   esac
   chgquery $TMP
 done
