[comp.unix.questions] WANTED: Script for add users

ramsey@lll.llnl.gov (Ramsey, Susanne) (06/01/90)

Does anyone out there have a robust easy to use script that would automate 
the adding of users to a system .

If so, would you be willing to share a copy with me.??

Thanks 
Susanne B. Ramsey

================================================================================
DISCLAIMER: These are my words, my thoughts and my opinions.  Who else would 
	    want them .. ;-) 
================================================================================
Susanne Barbera-Ramsey			Lawerence Livermore National Laboratory
Internet: ramsey@lll.llnl.gov		7000 East Ave. L-630
(415) 423-9530				Livermore Ca 94550
================================================================================

rbottin@atl.calstate.edu (Richard John Botting) (06/03/90)

We have spent 3 or four years working on scripts to this.
They are adapted to creating a class of students at one go.
You may have to change the names and dirctories of the prototype
dot files - which are where SCO Xenix/386 (1989) puts'em.
You may also have to change the ..BE field to something else (RTFM).
Disclaimer....This has survived much but is still evolving...use with care...
and don't blame us if it goes wrong...
-------------------------cut here-------------------------------
: "Interactive script to add a user"
: usage: newuser
:    or: newuser userid
:    or: newuser userid groupid
:

# change by M. Brenner to enable locking on password file
# Thu Dec  7 12:09:28 PST 1989
if [ -f /etc/pw..LOCK ]
  then echo "Looks like someone else is already editting it"; exit 1
  else cat /dev/null >/etc/pw..LOCK
fi
trap "rm /etc/pw..LOCK && exit || exit" 1 2 3
# end of change

if [ ! -w /etc/passwd ]
then
     echo "** you must be super-user to add new users **"
     echo "** abort **"
# following line added by M. Brenner, 03 Jan 90
     rm /etc/pw..LOCK && exit 1 || exit 1
     exit 1
fi

     echo "**$userid already exists **"
     echo "** abort **"
# following line added by M. Brenner, 03 Jan 90
     rm /etc/pw..LOCK && exit 2 || exit 2
     exit 2
fi
echo looking for new user id number & uid=`/usr/bin/cut -f3 -d: /etc/passwd|sort
 -n|tail -1`
uid=`expr $uid + 1`
while true
do
        if [ $# = 0 ]
        then
                echo "Select group for $userid from student, faculty, intern, ..
."
                echo; echo -n 'Input a group name: '
      echo $gname is not in /etc/group
        fi
done

echo "enter user info (name,etc)"
echo "user info: "
read uinfo
if ls -d /usr/$gname
then echo exists
fi
echo "enter HOME directory (default /usr/$gname/$userid): "
read home
if [ -z "$home" ]
echo $userid will have $home as his/her home directory.
echo "enter login shell (default /bin/sh): "
read shell
if [ -z "$shell" ]
then
     shell=/bin/sh
fi
if file "$shell" | grep "executable" >/dev/null
then
     echo "Shell is ok."
else
     echo "shell doesn't exist or isn't executable **"
     echo "** using /bin/sh **"
     shell=/bin/sh
 ? (y/n): "
read yn
if [ "$yn" != "y" ]
then
     echo "$userid not created **"
     echo "** abort **"
# following line added by M. Brenner, 03 Jan 90
     rm /etc/pw..LOCK && exit 4 || exit 4
     exit 4
else
     echo entering user $userid in /etc/passwd
     chmod 700 /etc/passwd
     echo "$userid:,..BE:$uid:$gid:$uinfo:$home:$shell" >> /etc/passwd
     chmod 444 /etc/passwd
     echo "$userid is entered into /etc/passwd"
     mkdir $home;chown $userid $home;chgrp $gname $home
     ls -ld $home; echo "created"
     pwadmin -f $userid
     echo setting up dot files

# loops created and csh stuff added by dick/RJB Jan15th 1990
# mailrc and exrc added dick/RJB Feb 3rd 1990
     for dotfile in profile logoutsh mailrc exrc
     do

     for dotfile in login cshrc logout
     do
        cp /usr/lib/mkuser/csh/$dotfile $home/.$dotfile || {
                echo "$0: Cannot create $home/.$dotfile" >&2
# following line added by M. Brenner, 03 Jan 90
                rm /etc/pw..LOCK && exit 1 || exit 1
                exit 1
        }
     done

        chmod 755 $home/.[a-z]*
        chgrp $gname $home/.[a-z]*
        chown $userid $home/.[a-z]*
        echo Listing of $home;ls -al $home
        rm /etc/pw..LOCK && exit || exit 
fi
----------------------cut here---------------------

I hope it helps and saves some of the hastles we've had...
Richard J Botting
Rbottin@atl.calstate.edu
PS. I hope it makes it thru the mail ok...
dick

fenske@iftccu.ca.boeing.com (Brian Fenske) (06/05/90)

Here is what I use.  Disregard references to Elm, our mailer, if you don't
use it.

--------------------------------------------------
#/bin/csh -f
#
# Current working directory MUST be /users to invoke!!!
# Invoke this with:
#        newacc username group
#
if ( $#argv < 2 ) then
	echo "Usage: newacc [login name] [group]"
	exit 1
endif
grep -w ^$1 /etc/passwd > /dev/null
if ( $status == 0 ) then
	echo " "
	echo "An account with this name is already on the system\!"
	echo " "
	exit 0
endif
vipw
vi /etc/group
vi /usr/lib/aliases
newaliases
mkdir $1
chmod 755 $1
chown $1 $1
chgrp $2 $1
cd $1
cp ../.cshrc .
cp ../.login .
cp ../.elmrc .
touch .msgsrc
mkdir mail
touch mail/logfile
chown $1 .cshrc
chown $1 .login
chown $1 .elmrc
chown $1 .msgsrc
chown $1 mail/logfile
chown $1 mail
chmod 644 .cshrc
chmod 644 .login
chmod 644 .elmrc
chmod 644 .msgsrc
chmod 600 mail/logfile
chmod 755 mail
vi .elmrc
cd ..
passwd $1
--------------------------------------------------

I also have a script to remove a user:

--------------------------------------------------
#/bin/csh -f
# Invoke this with:
#        rmacc username
if ( $#argv < 1 ) then
	echo "Usage: rmacc [login name]"
	exit 1
endif
echo ""
echo -n "Do you really want to remove $1?  Answer 'yes' or 'no' > "
set RSP = $< 
if ($RSP == "yes") then
	echo ""
	echo "removing files..."
	echo ""
	rm /usr/spool/mail/$1
	rm -rf ~$1
	vipw
	vi /etc/group
	vi /usr/lib/aliases
	newaliases
	echo ""
	echo ""
	echo ""
	echo "  $1 removed from system."
	echo ""
else
	exit 0
endif
--------------------------------------------------

--
Brian Fenske                     Manufacturing Research & Development
Boeing Commercial Airplanes      747-400 Integrated Functional Test System
P.O. Box 3707,  M/S 0P-37        (206) 342-5784
Seattle, WA  98124               ...uunet!bcstec!iftccu!fenske