#
#  chnguser    (4/2/88)
#-----------------------------------------------------------------------------
#  Chnguser will change a user entry in the password file.
#
#  NOTE:  This script will only work for single line entries in the password
#    file.  If you use the multi-line feature, which is unique to UX-DOS,
#    this script will not find the second line and will therefore run
#    incorrectly.  It is suggested, therefore, that you do not use the
#    multi-line feature of the password file if you plan to maintain that
#    file with adduser, chnguser, and deluser.  Adduser will set up single
#    line entries that will work with chnguser without any problems.
#
#   Start by making sure everything is set up right
#-----------------------------------------------------------------------------
: ${PASSWD:=\\etc\\passwd}                       # make sure $PASSWD is set
if [ ! -f $PASSWD ]                              # look for password file
   then                                          # handle errors if not found
   echo "$PASSWD not found."
   exit 1
fi

clear                                            # clear the screen
echo "CHANGE A USER DESCRIPTION WITHIN THE PASSWORD FILE:
"
case "$1" in                                     # look for name as argument
   ?*) USER=$1                                   # if present
       Echo "Changing $USER.";;                  # set USER from it
    *) echo "Enter the name of the user to change (no entry to quit)"
       echo  -n "       : "                      # otherwise ask for user name
       read USER;;                               # and read USER from terminal
esac

case $USER in                                    # check for null entry
   "") echo "No user name entered, processing terminated."
       exit 1;;                                  # otherwise quit
esac

trap 'rm -fs ${TEMPDIR}temp[12]' 0               # set trap to remove files
grep "^${USER}:" $PASSWD > ${TEMPDIR}temp1       # look for existing user
case $? in                                       # and extract its description
   1) echo "$USER is not currently in $PASSWD, processing terminated."
      exit 1;;                                   # handle error if not found
   2) echo "Cannot search $PASSWD for $USER, processing terminated."
      exit 1;;                                   # handle error from grep
esac
#-----------------------------------------------------------------------------
#  Found an existing user in the password file.  Now use sed to change all
#  empty fields (::) into dummy fields (:~:) and then translate all quoting
#  characters (usually in encrypted passwords) to harmless characters (.) so
#  we can read them with the shell read command.  To do this, save the old
#  $IFS value, change $IFS to ":", read the reformatted password line
#  breaking words on the colon characters (:), and then restore the old
#  $IFS value.
#-----------------------------------------------------------------------------
sed -e 's/::/:~:/g' < ${TEMPDIR}temp1 > ${TEMPDIR}temp2
tr "\042\047\140" "..." < ${TEMPDIR}temp2 > ${TEMPDIR}temp1
OLDIFS="$IFS"                                    # save old $IFS
IFS=":"                                          # break "words" on :
read OLDUSER PWORD OLDCOMMENT OLDHOME OLDSUFFIX OLDCMD < "${TEMPDIR}temp1"
IFS="$OLDIFS"                                    # restore old $IFS
#-----------------------------------------------------------------------------
#  Now look at each word read.  If it is the dummy value (~), change it back
#  to an empty field.
#-----------------------------------------------------------------------------
for X in "OLDUSER" "PWORD" "OLDCOMMENT" "OLDHOME" "OLDSUFFIX" "OLDCMD"
   do                                            # if the value of a word is
   eval VALUE="\$$X"                             # the dummy value (~),
   case $VALUE in                                # change that variable to a
     ~) $X=;;                                    # null value
   esac                                          # otherwise leave it alone
done
#-----------------------------------------------------------------------------
#  Now we have our existing login entry.  If it has a password set, we cannot
#  change the enty and must end.
#-----------------------------------------------------------------------------
case $PWORD in                                   # look for non-empty passwd
   ?*) echo "$USER is currently password protected.  Change the password to a
null entry, and then re-run this script (chnguser).  If you do not know the
password, you cannot change the user description.

Processing terminated."                          # print message
       exit 1;;                                  # and end
esac
#-----------------------------------------------------------------------------
#  This is the main loop of the script.  Continue through the loop until
#  you have varified that the data is correct.  Remember that you can always
#  abort the script with <DEL> if you find yourself caught and just want to
#  get out.  Structure the loop so that "null" entries default to the current
#  value of each item.
#-----------------------------------------------------------------------------
while true                                       # loop until input is okay
   do                                            # show original descr
   echo "Original user description:

        User login name:      $USER
        Descriptive comment:  $OLDCOMMENT
        Home directory:       $OLDHOME
        Path string suffix:   $OLDSUFFIX
        First command:        $OLDCMD

NOTE: null entries will retain the old values.
"
   echo "Enter a comment to identify $USER"
   echo -n "    : "                              # ask for comment
   read COMMENT
   case $COMMENT in                              # look at the entry
     "") COMMENT=$OLDCOMMENT;;                   # else use the old value
   esac
   echo "Enter the home directory for $USER"
   echo -n "    : "                              # ask for home dir
   read HOMEDIR
   case $HOMEDIR in                              # look at the entry
     "") HOMEDIR=$OLDHOME;;                      # else use the old value
   esac
   while [ ! -d "$HOMEDIR" ]                     # while it's a bad directory
     do
     echo "Not a valid directory, please try again"
     echo -n "  : "                              # ask for it again
     read HOMEDIR
     case $HOMEDIR in                            # look at the entry
       "") HOMEDIR=$OLDHOME;;                    # else use the old value
     esac
   done
   echo "Enter any path suffixes to add to the normal path"
   echo -n "    : "                              # ask for path suffix
   read SUFFIX
   case $SUFFIX in                               # look at the entry
     "") SUFFIX=$OLDSUFFIX;;                     # else use the old value
   esac
   while [ \( ! -d "$SUFFIX" \) -a \( -n "$SUFFIX" \) ]
     do
     SUFFIX=                                     # if not dir or empty
     echo "Not a valid directory or entry entry, please try again"
     echo -n "  : "                              # ask for it again
     read SUFFIX
   done
   echo "Enter any initial command to execute upon login (eg. sh)"
   echo -n "    : "                              # ask for first command
   read FIRSTCMD
   case $FIRSTCMD in                             # look at the entry
     "") FIRSTCMD=$OLDCMD;;                      # else use the old value
   esac
                                                 # show all of the entries
   clear
   echo " The new entries will be:

        User login name:      $USER
        Descriptive comment:  $COMMENT
        Home directory:       $HOMEDIR
        Path string suffix:   $SUFFIX
        First command:        $FIRSTCMD
"
#-----------------------------------------------------------------------------
#  Everything checks out, now use the yesno script to see if it meets the
#  user's needs.  If done, give the user the option of using the passwd
#  program to set a password on the changed user entry.  Note that we will not
#  remove the old password file, but will leave it on the disk as passwd.old.
#-----------------------------------------------------------------------------
   if sh yesno "Is this correct"                 # ask if they are okay
     then                                        # ok, create new passwd line
     NEWLINE="${USER}::${COMMENT}:${HOMEDIR}:${SUFFIX}:${FIRSTCMD}"
     mv -f $PASSWD ${PASSWD}.old                 # save current password file
     grep -v "^${USER}:" ${PASSWD}.old > $PASSWD # make new one (w/o user)
     echo "$NEWLINE" >> $PASSWD                  # add it to password file
     echo "$USER has been changed."              # report add to the screen
                                                 # ask about a password
     if sh yesno "Do you want to protect $USER with a password"
       then                                      # if desired
       passwd $USER                              # set the password
     fi
     exit 0                                      # then end
   else                                          # if input is not okay
     clear                                       # clear screen again
     echo "Please try again.  NOTE: you may press the DEL key to quit.
"                                                # print error message
   fi
done                                             # and try again
