#! /bin/sh

# Author: Derek Flynn
# Date:   1/26/1999
# File:   add_user_to_passwd
#
# This shell script adds the user "$1" with the password "$2" to
# the password file.  To ensure that another admin isn't editing
# the file, it uses RCS (the revision control system).  The utilities
# rcsdiff(1), ci(1), co(1) are all part of this system.  See 
# rcsintro(1) for an introduction to RCS.
#
# The passwd file needs to be under RCS control before running the
# script.  To do this, run 'ci -u passwd'.  When prompted, add any
# text you want (preferably something like "Example passwd file".
#
# This script doesn't do any sanity checking --- it just adds an
# account the the passwd file.  It isn't meant to be run by a user,
# it should instead be run by another script (something that sanity
# checks the information before calling this script).
#
# The return values of this script are as follows:
#
# 1  - No login specified
# 2  - No passwd string specified
# 3  - Couldn't find a UID
# 4  - Couldn't find a GID
# 5  - Problems checking out a copy of the passwd file
# 6  - Problems checking in the changes to the password file
# 7  - Problems with the rcsdiff on the passwd file
# 8  - Problems checking in the modified passwd file
#
# No guarantees that this works.  I believe that it does, and I
# have done some testing.  However, I just whipped it up for this
# class --- it has not been put to use.  Please report any problems
# you have, so I can fix it up.   -Derek


#
# These are set to files in the local directory for now.  On a
# real system, they would point to /etc/passwd and /etc/group.
#

# This is the location of the password file to experiment with.
PASSWDFILE=./passwd

# This is the location of the group file to experiment with.
GROUPFILE=./group


#
# Here are the fields to the password file:
#

# Login name for the account
LOGIN=$1
if test ! "$LOGIN"
then
  echo "No login specified.  Fatal error.  Exiting."
  exit 1
fi

# Password string for the account
PASSWD=$2
if test ! "$PASSWD"
then
  echo "No passwd string specified.  Fatal error.  Exiting."
  exit 2
fi

# Get the next available uid (this method just takes the current
# highest uid and adds one).  See notes on course page (lecture 4)
# for a proper discussion of this.
CURRUID=`awk -F: '{ print $3 }' $PASSWDFILE | sort -nr | head -1`
if test ! "$CURRUID"
then
  echo "Cannot find a UID.  Fatal error.  Exiting."
  exit 3
fi
UID=`expr $CURRUID + 1`
if test ! "$UID"
then
  echo "Cannot find a UID.  Fatal error.  Exiting."
  exit 3
fi

# Get the appropriate GID (corresponding to $GROUP)
# This is the name of the group to put users in by default.
GROUP="users"
GID=`grep "^$GROUP" $GROUPFILE | awk -F: '{ print $3 }' | head -1`
if test ! "$GID"
then
  echo "Cannot find a GID.  Fatal error.  Exiting."
  exit 4
fi

# No GECOS field --- let them change it with chfn(1)
GECOS=""

# Home directory
HOME=/homes/$LOGIN

# Give them a default shell.  Let them change it with chsh(1).
SHELL=/bin/sh


#
# Get a copy of the passwd file that we can edit without fear (and with
# logging).
#

# People may have used passwd(1) to change their passwords.  So we look
# at the difference (with rcsdiff(1)) and register the changes if there
# are any.
rcsdiff $PASSWDFILE >/dev/null 2>&1
case $?
in
  0) # There are no undocumented changes to the password file.  Lock
     # editing of it (rcs locking).
     co -q -l $PASSWDFILE
     if test "$?" != 0
     then
       echo "Errors checking out the passwd file.  Fatal error.  Exiting."
       exit 5
     fi
     ;;
  1) # There are changes.  Register them.
     ci  -m"Probably a password change." -q -l $PASSWDFILE
     if test "$?" != 0
     then
       echo "Errors checking in the passwd file.  Fatal error.  Exiting."
       exit 6
     fi
     ;;
  *) # Lets bail --- problems with rcsdiff
     echo "Rcsdiff trouble.  Fatal error.  Exiting."
     exit 7
     ;;
esac


#
# Add the account and register the changes
#

# Add our user.
echo "$LOGIN:$PASSWD:$UID:$GID:$GECOS:$HOME:$SHELL" >>$PASSWDFILE

# Register the changes and let the next person edit the file
ci -m"Added user $LOGIN" -q -u $PASSWDFILE >/dev/null 2>&1
if test "$?" != 0
then
  echo "Errors checking in the modified passwd file.  Fatal error.  Exiting."
  exit 8
fi

