*BSD News Article 25799


Return to BSD News archive

Newsgroups: comp.os.386bsd.misc
Path: sserve!newshost.anu.edu.au!munnari.oz.au!bunyip.cc.uq.oz.au!harbinger.cc.monash.edu.au!msuinfo!uwm.edu!vixen.cso.uiuc.edu!howland.reston.ans.net!pipex!uknet!cf-cm!paul
From: paul@myrddin.isl.cf.ac.uk (Paul)
Subject: Re: Creating Users..
Message-ID: <1994Jan10.180023.5972@cm.cf.ac.uk>
Sender: news@cm.cf.ac.uk (Network News System)
Organization: Intelligent Systems Lab, ELSYM, University of Wales, Cardiff
References: <1994Jan09.143751.23898@crash>
Date: Mon, 10 Jan 1994 18:00:20 +0000
Lines: 156

In article <1994Jan09.143751.23898@crash> kestryn@crash.cts.com (Zach Williams) writes:
>     I cannot seem to create a new user account in FreeBSD 1.0r.  Is there
>an "adduser" script somewhere for FreeBSD?   Thanks in advance.
>		--Zach
>

Here's one that someone wrote for 386BSD, it's what I've been using. I
suppose we should disttribute one with the next release? Note that it
was not written by me and I've no idea who bmyers is. Also, I've
occasionally had problems with it not writing an entry to the passwd
file. No idea why but just retrying usually fixes it.


#!/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\" homedir 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?

( echo "${pwent}"; ) > /tmp/passwdentree
echo 
( echo ':$';         # Go to end of file
  echo ":r /tmp/passwdentree";  # Read passwdentree as last line
  echo ":x"; ) | vipw ${Passwd} 

# 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;
	rm -f /tmp/passwdentree # remove old file
fi
rm -f /tmp/passwdentree # remove old file

# Set a temporary password so that passwd will work
passwd ${uname}
# make the home directory
/bin/mkdir ${homedir}
/usr/sbin/chown ${uname} ${homedir}
/usr/bin/chgrp ${gid} ${homedir}

# add default user startup files
cp /usr/share/skel/dot.cshrc ${homedir}/.cshrc
cp /usr/share/skel/dot.login ${homedir}/.login
cp /usr/share/skel/dot.profile ${homedir}/.profile
cp /usr/share/skel/dot.mailrc ${homedir}/.mailrc
cp /usr/share/skel/dot.rhosts ${homedir}/.rhosts
cp /usr/share/skel/dot.xsession ${homedir}/.xsession
/usr/sbin/chown -R ${uname} ${homedir}
/usr/bin/chgrp -R ${gid} ${homedir}

# is ok, exit 0
exit 0
-- 
  Paul Richards, 
  Intelligent Systems Laboratory, ELSYM ,University of Wales, College Cardiff
  Internet: paul@isl.cf.ac.uk,  JANET(UK): RICHARDSDP@UK.AC.CARDIFF