Subject: Re: Adduser, etc
To: Peter Abrahamsen <port-mac68k@NetBSD.ORG>
From: #hea <hea@ix.netcom.com>
List: port-mac68k
Date: 12/22/1997 08:29:24
On 12/21/97, Peter Abrahamsen, asked:
>I was wondering if anyone still has one of those shell scripts for adding 
users

Here is one which was derived from MkLinux's adduser script ( Thanks to 
Ian A. Murdock).  Note that it expects a sample user directory in 
/etc/skel ( instead of /usr/share/skel which contains the NetBSD sample 
directory).  You should only treat this script as a sample, and customize 
it for yourself.

...Harlan


----------------------------- Begin adduser for NetBSD script 
-----------------------------

#! /bin/sh
#
# adduser 1.0: a utility to add users to the system
#
# Copyright (C) 1994 Ian A. Murdock <imurdock@shell.portal.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Written for the Debian Linux distribution (01/21/94).  Feel free to use
# it in YOUR distribution, too. :)  Please note that this is just a simple
# script that _somewhat_ automates the really boring and repetitive task
# of creating new user accounts.  It makes no attempt to be sophisticated.
# Let me know if you improve it in any way.
#
# I need to write a man page for this.
#
# Modified by Marc Ewing <marc@redhat.com> for RHS Linux

# Everything happens too fast, so don't let the user interrupt.
trap "" 1 2 3 15

# Set a few important variables before getting started.
NUMARG=$#
LOGIN="$1"
EXIST=0

NOHOME="$2"

PASSWD=/etc/master.passwd
PBAK=/etc/master.passwd-        # Some programs use master.passwd-, 
others use
                                # /etc/master.passwd.OLD.  Take your pick.

GROUP="/etc/group"
GBAK="/etc/group-"

PLOCK="/etc/ptmp"		# Standard method of locking the password file.

DSHELL="/bin/sh"
DHOME="/home"
SKEL="/etc/skel"
SPOOL="/var/spool/mail"
FIRST_UID=500
FIRST_GID=500

# A few sanity checks...
if [ `id -u` != 0 ]; then
	echo "Only root may add users to the system." ; exit 1
fi

if [ $NUMARG = 0 ]; then
	echo "You need to specify the login to add; for example, \`adduser" \
		"imurdock'." ; exit 1
fi

id $LOGIN >/dev/null 2>/dev/null && EXIST=1

if [ $EXIST = 1 ]; then
	echo "The login $LOGIN already exists."
	exit 1
fi

if [ -f $PLOCK ]; then
	echo "$PASSWD is locked.  Try again later." ; exit 1
fi

# And now the program begins: 
echo "" ; echo -n "Looking for first available UID..."
NUID=`cut -f 3 -d ":" $PASSWD | sort -n | awk -v uid=$FIRST_UID '
		{ if ($1 == uid) uid = uid + 1; }
END		{ print uid; }
'`

if [ $NUID -ge 65536 ]; then
	echo "Sorry, ran out of uids."
	exit 1
fi
echo " $NUID"


echo -n "Looking for first available GID..."
NGID=`cut -f 3 -d ":" $GROUP | sort -n | awk -v gid=$FIRST_GID '
		{ if ($1 == gid) gid = gid + 1; }
END		{ print gid; }
'`

if [ $NGID -lt $FIRST_GID ]; then
	NGID=$FIRST_GID
fi
echo " $NGID"

echo "" ; echo -n "Adding login: $LOGIN..."
touch $PLOCK ;

cp $PASSWD $PBAK
echo "$LOGIN::$NUID:$NGID::0:0:NetBSD mac68k User:$DHOME/$LOGIN:$DSHELL" 
>> $PASSWD

# Add user to users group
cp $GROUP $GBAK
sed "s/^\(users.*[^:]\)\$/\1,$LOGIN/" < $GBAK |
sed "s/^\(users.*:\)\$/\1$LOGIN/" > $GROUP

#sed "s/^\(users.*[^:]\)$/\1,$LOGIN/" < $GBAK |
#sed "s/^\(users.*:\)$/\1,$LOGIN/" > $GROUP

echo "$LOGIN::$NGID:$LOGIN" >> $GROUP

pwd_mkdb $PASSWD

rm -f $PLOCK
echo "done."

if [ "x$NOHOME" = "x" ]; then
	echo -n "Creating home directory: $DHOME/$LOGIN..."
	mkdir $DHOME/$LOGIN
	chmod 2775 $DHOME/$LOGIN
	cp $SKEL/.??* $SKEL/* $DHOME/$LOGIN >/dev/null 2>/dev/null
	chown -R $NUID:$NGID $DHOME/$LOGIN
	echo "done."
fi

echo -n "Creating mailbox: $SPOOL/$LOGIN..."
touch $SPOOL/$LOGIN ; chmod 660 $SPOOL/$LOGIN ; chown $NUID:mail 
$SPOOL/$LOGIN
echo "done."

echo ""
echo "Don't forget to set the password."
if [ "x$NOHOME" != "x" ]; then
	echo ""
	echo "The home directory for $LOGIN was set to $DHOME/$LOGIN but the 
directory"
	echo "was not created.  Be sure that you set it up properly."
fi

#passwd $LOGIN
#chfn $LOGIN

# EOF


----------------------------- End adduser for NetBSD script 
-----------------------------