*BSD News Article 6193


Return to BSD News archive

Newsgroups: comp.unix.bsd
Path: sserve!manuel.anu.edu.au!munnari.oz.au!hp9000.csc.cuhk.hk!uakari.primate.wisc.edu!sdd.hp.com!wupost!darwin.sura.net!uvaarpa!rucs!rucs2!bmyers
From: bmyers@rucs2.sunlab.cs.runet.edu (B.Myers)
Subject: add_user script for 386bsd
Message-ID: <1992Oct9.035112.4826@rucs2.sunlab.cs.runet.edu>
Organization: Radford University
Date: Fri, 9 Oct 92 03:51:12 GMT
Lines: 145


I have modified an "add user" script, included below,
that will automatically add users to a 386bsd system.

Hope this helps.

#------- Start of goodies --------
#-------     cut here     --------
#!/bin/sh
#
# add user script for use with 386bsd
# arguments: uname uid gid fullname homedir shell
#
# bmyers@rucs2.sunlab.cs.runet.edu
# to execute type:
# sh add_user [uname] [uid] [gid] ["full name"] [homedir] [shell]

myname=`basename $0`
Passwd=/etc/passwd
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin
export PATH
# check for root
if [ "`whoami`x" != "root"x ]; then
   echo "You must be root to do $myname!"
   exit 1
fi

# check for number of args
# Change to 5 if the directories are going to be in the same place
# ie. no homedir option given to the program

if [ $# -ne 6 ]; then 
	echo "${myname}: invalid number of arguments"
	echo "   usage: ${myname} uname uid gid \"fullname\" shell"
	exit 1
fi

# put args into named variables
uname=$1
uid=$2
gid=$3
fullname=$4
homedir=$5  # If all directories are going to be in the same place
shell=$6    # Then change homedir=$5 to homedir=/DIR/../$1 ie: /DIR/../UNAME
            # homedir will be assumed on each call

# checks for validity of arguments
# check uid
if test $uid -lt 10 ; then
	echo  "uid: uid must be greater than 10 and less than 60000"
	exit 1
elif test $uid -gt 60000 ; then 
	echo  "uid: uid must be greater than 10 and less than 60000"
	exit 1
fi

# check gid
if test $gid -lt 10 ; then
	echo  "gid: gid must be greater than 10 and less than 60000"
	exit 1
elif test $gid -gt 60000 ; then 
	echo  "gid: gid must be greater than 10 and less than 60000"
	exit 1
fi


# check shell
if test ! -x $shell ; then
	echo "$shell: the program does not exist or is not executable"
	exit 1
fi

# check homedir
# check if homedir already exists
if [ -f ${homedir} ]; then
	echo "${myname}: WARNING: a file named \"${homedir}\" already exists"
	echo "and is NOT a directory, NOT setting up user account"
	exit 1
fi
if [ -d ${homedir} ]; then
	echo "${myname}: WARNING: home directory \"${homedir}\" already exists"
	echo "    no files copied, NOT setting up user account"
	exit 1
fi
# check if all but last path of homedir exits
#dir=`shdirname $homedir`
#if test ! -d $dir ; then
#	echo "$dir: does not exist or is not a directory"
#	exit 1
#fi
# check if $homedir is local
dfout=`df $dir | ( read aline; read aline; echo $aline )`
case $dfout in
  /dev*) ;; # $dir is on local machine
      *) echo "$dir: is not on local machine"
         exit 1;;
esac

# create a null /etc/passwd entry
# first check if one already exists
if grep -s "^${uname}:" ${Passwd} ; then
	echo "${myname}: ERROR: ${uname} aleady in ${Passwd}";
	exit 1;
fi
# check if uid already exists
if grep -s ".*:.*:${uid}:" ${Passwd} ; then
	echo "uid: ERROR: ${uid} already in ${Passwd}";
	exit 1;
fi
pwent="${uname}::${uid}:${gid}::0:0:${fullname}:${homedir}:${shell}"
# XXX sould we use tmp file and rename it?

rm -f /tmp/passwdentree # remove any previous file passwdentree
( echo "${pwent}"; ) > /tmp/passwdentree
( echo ':$';         # Go to end of file
  echo ":r /tmp/passwdentree";  # Read passwdentree as last line
  echo ":wq"; ) | vipw ${Passwd} 
rm -f /tmp/passwdentree # remove old file

# Verify that the entree was saved.
if grep -s "^${uname}:" ${Passwd} ; then
	:
else
	echo "${myname}: ERROR: password entry didn't go to ${Passwd}";
	exit 1;
fi
# make the home directory
/bin/mkdir ${homedir}
/usr/sbin/chown ${uname} ${homedir}
/usr/bin/chgrp ${gid} ${homedir}

# add default user startup files
cp /usr/lib/Cshrc ${homedir}/.cshrc
cp /usr/lib/Login ${homedir}/.login
cp /usr/lib/.xsession ${homedir}/.xsession
/usr/sbin/chown -R ${uname} ${homedir}
/usr/bin/chgrp -R ${gid} ${homedir}

# is ok, exit 0
exit 0


--
Brandon Myers
bmyers@rucs2.sunlab.cs.runet.edu