#!csh  "@[$]adduser     2.8   LAMBDA/MICRO INC."
#
# /usr/bin/adduser
#
#	- shell script to add a new user and create misc stuff for her/him
#	First version For LEX * MAXIMA SYSTEM
#
# SCO XENIX V 286 Port - Revision 11/87
#
set MAILDIR = /usr/spool/mail
set AUTOTAR = /etc/autotar
set BACKUP = /etc/backup

# Default directory for user home directories, and default group.
        set firm = /usr
	set DEFAULTGROUP = users

# where to save backup files
	set SAVE = /usr/lxmx/Save

# where to get standard .cshrc file and .profile for new user
	set CRC = /usr/lxmx/etc/tcshrc
	set PRF = /usr/lxmx/etc/tprofile
	set LGN = /usr/lxmx/etc/tlogin
	set LGO = /usr/lxmx/etc/tlogout

# set up temp file names
        set tmp1 = /tmp/newu$$
        set tmp2 = /tmp/nu1$$
        set tmp3 = /tmp/nu2$$

# what to do if signals occur
        onintr byebye

# set password and group file name
        set pwfile = /etc/passwd
        set grpfile = /etc/group

# set backup file names
        set pwbackup = $SAVE/passwd
        set grpbackup = $SAVE/group

# get new user name
        while ( 1 )
                /bin/echo
                /bin/echo
                /bin/echo '                    ALWAYS USE LOWER CASE FOR LOGINS'
# NB single quotes may not be escaped within single quotes and single quotes within
#    double quotes need not be escaped at all!
                /bin/echo "                              'e' to exit         "
                /bin/echo
                /bin/echo
                /bin/echo "NEW USER'S NAME> \c"
                set uname = `gets`
                if ( $uname == "" ) continue
                if ( $uname == 'e' ) exit 0
                set len = `expr $uname : '.*'`
                if ( $len > 8 ) then
                        /bin/echo "           '$uname': USER NAMES LIMITED TO 8 CHARACTERS MAXIMUM; TRY AGAIN"
                        continue
                endif
                egrep "^${uname}:" $pwfile >&! /dev/null
                if ( $status == 0 ) then
                        /bin/echo "           ${uname}: ALREADY IN USE. PLEASE USE ANOTHER NAME."
                else
                        /bin/echo "                       ${uname} ACCEPTED"
                        break
                endif
        end

# get the next uid to assign
        /bin/echo
        /bin/echo
        /bin/echo "                   SCANNING FOR USER IDENTIFICATION NUMBER."
# this ugly mess finds the highest number uid now in use and sets hiuid to it
        set hiuid = `sort -nt: +2 -3 $pwfile | tail -1l | sed 's/[^:]*:[^:]*:\([^:]*\):.*/\1/'`
        set hiuid = `expr $hiuid + 1`

# get a home directory, and try to make it.
# Home directory path must be explict, we check for it by an initial slash
        while ( 1 )
                /bin/echo CHOOSE A HOME DIRECTORY FOR $uname OR IF YOU HIT RETURN, I WILL CHOOSE ONE\.
                /bin/echo "ENTER> \c"
                set homedir = `gets`
                if ( $homedir == "" ) then
                        if ( -d $firm ) then
                                set homedir = $firm/$uname
                        else
                                set homedir = /usr/$uname
                        endif
                        /bin/echo "                  YOUR HOME DIRECTORY IS $homedir"
                endif
                if ( ! `expr $homedir : '^/'` ) then
                        /bin/echo "A FULL EXPLICIT PATHNAME MUST BE USED FOR THE HOME DIRECTORY"
                        /bin/echo e.g. /usr/$uname
                        continue
                endif
                if ( -e $homedir ) then
                        while ( 1 )
                                /bin/echo  "           THIS IS AN EXISTING DIRECTORY."
                                /bin/echo  "           NEW USER TO TAKE UP RESIDENCE? (y/n)"
                                /bin/echo "ENTER> \c"
                                set yesno = `gets`
                                if ( $yesno == 'n' ) then
                                        /bin/echo byebye
                                        goto byebye
                                else if ( $yesno == 'y' )
                                        goto hell
                                else
                                        continue
                                endif
                        end
                endif
                /bin/echo  "                       CREATE $homedir? (y/n)"
                /bin/echo "                       ENTER> \c"
                set yesno = `gets`
                if ( $yesno != 'y' ) then
                        /bin/echo byebye
                        goto bybye
                else
                        mkdir $homedir
                        if ( $status == 0 ) then
#                               create appopriate .cshrc and .profile files

hell:
				/bin/echo "Creating .cshrc .profile .login for $uname."
                                sed -e "/loginame/s//$uname/" < $CRC > ${homedir}/.cshrc
                                sed -e "/loginame/s//$uname/" < $PRF > ${homedir}/.profile
                                sed -e "/loginame/s//$uname/" < $LGN > ${homedir}/.login
                                break
                        else
                                /bin/echo "       *** ${homedir}: could not create. Please try another home directory."
                        endif
                endif
                break
        end

# get the login shell to use
        /bin/echo 'Login shell (/usr/bin/lxsh is the default if you hit return.);'
        /bin/echo '            ( /bin/csh and /bin/sh are alternatives. )'
        /bin/echo '            ( /bin/csh is the preferred alternative. )'
        /bin/echo 
        /bin/echo "ENTER> \c"
        set lshell = `gets`
        if ( $lshell == "" )  then
                set lshell = /usr/bin/lxsh
        endif

        if ( ! -e $lshell ) then
                /bin/echo \'$lshell\' does not exist\. Substituting /bin/csh\.
                set lshell = /bin/csh
        endif

# now the default group at login
        while ( 1 )
		/bin/echo  "Choose group for $uname."
		/bin/echo  "  "
		cat /etc/group
		/bin/echo  "  "
                /bin/echo  "Group for $uname. $DEFAULTGROUP is the default."
                /bin/echo "ENTER> \c"
                set defgrp = `gets`
                if ( $defgrp == "" ) set defgrp = $DEFAULTGROUP
                egrep  "^${defgrp}:" $grpfile >! $tmp3
                if ( $status == 0 ) then
# get gid
                        set defnum = `sed -e "s/^[^:]*:[^:]*:\([0123456789]*\):.*/\1/" < $tmp3`
                        break
                else
                        /bin/echo \'$defgrp\' not found in group file. Please try again.
                endif
        end

# now get group names
        /bin/echo
        /bin/echo Enter groups OTHER THAN the default group that \`$uname\' should
        /bin/echo belong to\.  Enter one group name per line, with a separate RETURN to end\.
        /bin/echo If the user should belong only to her/his default group, just
        /bin/echo HIT RETURN to the question\.
	/bin/echo "\n\nGROUP FILE:"
	cat $grpfile
	/bin/echo "\n\n"

        cat /dev/null > $tmp1

# $tmp1 will be a sed script to add the user to the groups
#
# take care of groups with existing members

        /bin/echo "/${defgrp}:.*[^:]"'$'"/s/.*/&,$uname/" >> $tmp1

# take care of groups with no existing members

        /bin/echo "/${defgrp}:.*:"'$'"/s/.*/&$uname/" >> $tmp1 

# get additional groups
        while ( 1 )
                /bin/echo "Group: \c"
                set group = `gets`
                if ( $group == "" ) break
                egrep "^${group}:" $grpfile
                if ( $status == 0 ) then
# take care of groups with existing members
                        /bin/echo "/${group}:.*[^:]"'$'"/s/.*/&,$uname/" >> $tmp1
# take care of groups with no existing members
                        /bin/echo "/${group}:.*:"'$'"/s/.*/&$uname/" >> $tmp1 
                else
                        /bin/echo $group was not found in $grpfile\. Please try again.
                        /bin/echo
                        /bin/echo
                endif
        end

# Verify that the info is correct for this user
        /bin/echo
        /bin/echo "                  Login name:             $uname"
        /bin/echo "  User Identification Number:             $hiuid"
        /bin/echo " Group Identification Number:             $defnum"
        /bin/echo "              Home Directory:             $homedir"
        /bin/echo "                 Login shell:             $lshell"
        /bin/echo
        while ( 1 )
                /bin/echo  "                      Okay to add (y or n)? "
                /bin/echo "                      ENTER> \c"
                set reply = `gets`
                if ( $reply == "" ) continue
                if ( $reply == "y"  ) then
                        break
                else
                        /bin/echo '                        *** No action taken'
                        /bin/echo
                        /bin/echo
                        goto byebye
                endif
        end

# now that we have everything we do not want to be interrupted.
        onintr -

        /bin/echo "                      ${uname}: Added"
        /bin/echo
        /bin/echo

	cp $LGO ${homedir}/.logout

# create entry for this user, if passwd call fails
# give the user no password
        /bin/echo ${uname}::${hiuid}:${defnum}::${homedir}:${shell} >> $pwfile

        /bin/echo "               *** ON TYPING PASSWORD, IT WILL NOT BE ECHOED BACK ***"
        /bin/echo
        /bin/echo
        /bin/echo
        passwd $uname

# add this user name to the appropriate groups
        sed -f $tmp1 $grpfile > $tmp2

# Copy over new group file to the normal group file, adjust mode also
        mv $tmp2 $grpfile
        chmod 644 $grpfile

# Edit autotar script to include backup of new user's home directory
	echo "Adding user's home directory to automatic daily backup sequence."

	/bin/creat $tmp1
	/bin/echo '/^\.\/usr\/lib\/cron\/log/' >> $tmp1
	/bin/echo 'a' >> $tmp1
	/bin/echo ".$homedir \c" >> $tmp1
	/bin/echo \\ >> $tmp1
	/bin/echo '.' >> $tmp1
	/bin/echo 'w' >> $tmp1
	/bin/echo 'q' >> $tmp1

	ed $AUTOTAR < $tmp1 > /dev/null

# Edit manual backup script to include backup of new user's home directory
	echo "Adding user's home directory to maual backup sequence."
	ed $BACKUP < $tmp1 > /dev/null

# remove all temp files
        rm -f $tmp1 $tmp2 $tmp3

# LEX * MAXIMA: update the backup files

        /bin/echo "                    UPDATING PASSWD, AND GROUP BACKUP FILES"
        /bin/echo
        /bin/echo
        cp $grpfile $grpbackup
        cp $pwfile $pwbackup

# Create mail file for user and fix modes for mail file and home directory
        cat /dev/null > $MAILDIR/$uname
        /etc/chmog 0600  $uname $defgrp $homedir $MAILDIR/$uname
        /etc/chmog 700 $uname $defgrp ${homedir}/.cshrc
        /etc/chmog 700 $uname $defgrp ${homedir}/.login
        /etc/chmog 700 $uname $defgrp ${homedir}/.logout
        /etc/chmog 700 $uname $defgrp ${homedir}/.profile
        /etc/chmog 775 $uname $defgrp $homedir
        /bin/echo
        /bin/echo
        /bin/echo "                             USER INSTALLATION COMPLETE"
        /bin/echo
        sleep 3 
        exit 0

byebye:
        rm -f $tmp1 $tmp2 $tmp3
        if ( $?homedir ) then
                if ( -e $homedir ) then 
                        /bin/echo  "                     REMOVE $homedir? (y/n)"
                        /bin/echo "                     ENTER> \c"
                        set yesno = `gets`
                        if ( $yesno != 'y' ) then
                                exit 0
                        else
                                rmdir $homedir
                                if ( $status == 0 ) then
                                        exit 0
                                else
                                        /bin/echo "            *** ${homedir}: could not remove."
                                        exit 0

                                endif
                        endif
                endif
        endif
        exit 0


Return to Home Page
Return to Metayoga Page
Return to shell Page


The URL for this document is:
http://graham.main.nc.us/~bhammel/graham/SHELL/adduser.html
Created: 1997
Last Updated: May 28, 2000 Email me, Bill Hammel at
bhammel@graham.main.nc.us
READ WARNING BEFORE SENDING E-MAIL