Cover V05, I03
Article
Listing 1
Listing 2
Listing 3
Listing 4
Listing 5
Listing 6
Listing 7

mar96.tar


Listing 6: adduser.sh

#!/bin/sh
#######################################################
#
# adduser.sh -- add a user for local admin
#
#######################################################
# VARIABLES
PROFILE=/usr/bin/amp/profile
PWFILE=/etc/passwd
TPWD=/tmp/tpwd.$$
USERID=""
NAME=""
HOMEDIR=""
SH=""
GID=""

# Get the next uid from the passwd file
nextuid() {
LASTUID=`sort -t ":" -n +2 $PWFILE | tail -1 | cut -d":" -f3`
NEXTUID=`expr $LASTUID \+ 1`
}

# Mainline

# Get User login ID
while [ -z "$USERID" ]
do
clear
echo "\nPlease enter the new user information below."
echo "USERID: \c"
read USERID
if grep "^${USERID}:" $PWFILE >/dev/null
then
echo "\n\"$USERID\" is already in $PWFILE!"
sleep 3
USERID=""
fi
done

# Get name and info
while [ -z "$NAME" ]
do
echo "Enter name, address info: \c"
read NAME
done

# Get home directory
while [ -z "$HOMEDIR" ]
do
echo "Home directory: \c"
read HOMEDIR
done

# Get the startup shell; defaults to /bin/sh
echo "Enter startup shell [/bin/sh] : \c"
read SHL
[ -z "$SHL" ] && SH="/bin/sh"


# Get next uid for default then get uid
nextuid
echo "Enter UID [default = $NEXTUID]: \c"
read UID
[ -z "$UID" ] && UID=$NEXTUID

# Get group ID
while [ -z "$GID" ]
do
echo "Enter group id NUMBER: \c"
read GID
done


cat << EOF

*** "USERID" is about to be added. ***
Login name	$USERID
UID		$UID
Group ID	$GID
Description 	$NAME
Home Directory	$HOMEDIR
Shell Command	$SH

EOF

echo "Is it OK to continue? [(y/n) y] \c"
read ANSWER
case $ANSWER in
n*|N*)
echo "\nExiting...\c"
sleep 1
exit 0
;;
esac

STRING=$USERID:*:$UID:$GID:$NAME:$HOMEDIR:$SH
if cp $PWFILE $TPWD
then
echo "$STRING" >> $TPWD
echo "\nNow set ${USERID}'s default password..."
NOPASSWD=true
while [ "$NOPASSWD" = "true" ]
do
if /bin/passwd -f $TPWD $USERID
then
echo "OK"
NOPASSWD=false
fi
done

mv $TPWD $PWFILE && chmod 444 $PWFILE
if [ -d $HOMEDIR ]
then
echo "$HOMEDIR already exists."
else
mkdir -p $HOMEDIR
chown $UID $HOMEDIR
chgrp $GID $HOMEDIR
fi
if [ -r $PROFILE ]
then
cp $PROFILE $HOMEDIR/.profile
chown $UID $HOMEDIR/.profile
chgrp $GID $HOMEDIR/.profile
fi
fi

sleep 3

# End of File