[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

new adduser - please test



Appended are the files /usr/sbin/adduser and /etc/default/adduser from
Debian 0.92.  I have just updated adduser in several ways, including
support for the project group idea.  I have not had much time to test
it (I'm working the 11-8 weekend shift now... :/).  I need to know how
well it works, so please test it thoroughly and let me know how it can
be improved.  Feel free to improve it yourself and mail me your
changes.

--- cut here: /usr/sbin/adduser ---
#! /bin/sh
#
# adduser 1.1: a utility to add users to the system
#
# Copyright (C) 1993, 1994 Ian A. Murdock <imurdock@gnu.ai.mit.edu>
#
#    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.
#

# Everything happens too fast, so don't let the user interrupt.  We
# certainly don't want a half-done job.
trap "" 1 2 3 15

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

PASSWD="/etc/passwd"
GROUP="/etc/group"
PBAK="/etc/passwd-"
GBAK="/etc/group-"

PLOCK="/etc/ptmp"		# Standard method of locking the password file.
				# Is there a ``standard'' method of locking the
				# group file?

DEFAULTS="/etc/default/adduser"

# This is a bit tricky.  If method is "default" and there are no users in the
# default group, then add the user to the group in $GROUP without a leading
# comma.  If there are, then add the user to the group in $GROUP preceded by
# a comma.  If method is "project", then simply create the new group and add
# the user to it.
add_to_group()
{
	if [ $METHOD = "default" ]; then
	 # First of all, determine the name of the group.
	 GROUP_NUM=$1
	 GROUP_NAME=`grep "::$GROUP_NUM:" $GROUP | cut -f 1 -d ":"`
	 echo -n "($GROUP_NAME)... "
	 grep -x "^$GROUP_NAME::$GROUP_NUM:$" $GROUP >/dev/null 2>&1
	 if [ $? = 0 ]; then
	 	sed "/^$GROUP_NAME/s/\$/$LOGIN/" $GROUP > /tmp/group
	 else
	 	sed "/^$GROUP_NAME/s/\$/,$LOGIN/" $GROUP > /tmp/group
	 fi
	 if [ -f /tmp/group ]; then
	 	cp $GROUP $GBAK
	 	mv /tmp/group $GROUP
	 fi
	else
	 # Note that arguments are completely ignored in this case.
	 echo -n "($LOGIN)... "
	 cp $GROUP $GBAK
	 echo "$LOGIN::$NUID:$LOGIN" >> $GROUP
	fi
}

# What do we do if $DEFAULTS doesn't exist? 
no_defaults ()
{
	DSHELL="/bin/bash"
	DHOME="/home"
	SKEL="/etc/skel"
	SPOOL="/var/spool/mail"
	METHOD="default"
	FIRST_UID=1000
	USERS_GID=1000
}

# Make sure that we can add $LOGIN to the system.
if [ `whoami` != "root" ]; then
	echo "$0: only root may add users to the system." ; exit 1
fi
if [ $NUMARG = 0 ]; then
	echo "$0: you need to specify the login to add;"
	echo "for example, \`$0 imurdock'."
	echo "The default variables are defined in the file $DEFAULTS."
	exit 1
fi
id $LOGIN >/dev/null 2>&1 && EXIST=1
if [ $EXIST = 1 ]; then
	echo "$0: the login $LOGIN already exists." ; exit 1
fi
if [ -f $PLOCK ]; then
	echo "$0: $PASSWD is locked.  Try again later." ; exit 1
fi

# Okay, we can.
cp $PASSWD $PLOCK

# And now the program begins.
if [ -f $DEFAULTS ]; then
	echo -n "Reading in $DEFAULTS..." ; source $DEFAULTS ; echo "done."
else
	echo "No defaults file found.  Using built-in defaults." ; no_defaults
fi

echo "" ; echo -n "Looking for first available UID..."
NUID=`cat /etc/passwd | sed /^nobody:/d | cut -f 3 -d ":" | sort -n | \
	tail -1` ; NUID=`expr $NUID + 1`
if [ $NUID -lt $FIRST_UID ]; then
	NUID=$FIRST_UID
fi
if [ $METHOD = "default" ]; then
	NGID=$USERS_GID
else
	if [ $METHOD = "project" ]; then
		NGID=$NUID
	else
		echo -e "\n$0: unknown method \`$METHOD'."
		rm -f $PLOCK ; exit 1
	fi
fi
echo "done.  Using UID $NUID and GID $NGID."

if [ $NUID = 0 -o $NUID -gt 65535 ]; then	# _Definitely_ don't want that!
	echo ""
	echo "$0: Ack!  Something went wrong!  Aborting!"
	echo ""
	rm -f $PLOCK
	exit 1					# _Big_ time...
fi

echo "" ; echo -n "Adding login $LOGIN..."
echo "$LOGIN:*:$NUID:$NGID::$DHOME/$LOGIN:$DSHELL" >> $PLOCK
cp $PASSWD $PBAK
mv $PLOCK $PASSWD
echo "done."

echo "" ; echo -n "Adding $LOGIN to group $NGID "
add_to_group $NGID
echo "done."

echo "" ; echo -n "Creating home directory: $DHOME/$LOGIN..."
if [ -d $DHOME/$LOGIN ]; then
	echo -e "\n*** $DHOME/$LOGIN already exists!  Not copying files from $SKEL. ***"
else
	mkdir $DHOME/$LOGIN
	if [ $METHOD = "project" ]; then
		chmod 775 $DHOME/$LOGIN ; chown $NUID.$NGID $DHOME/$LOGIN
	fi
	cp -i $SKEL/.[a-z]* $SKEL/* $DHOME/$LOGIN >/dev/null 2>&1
	if [ $METHOD = "project" ]; then
		test -f $DHOME/$LOGIN/.bashrc && \
		 ( sed "s/umask 022/umask 002/" $DHOME/$LOGIN/.bashrc > \
		   /tmp/.bashrc ; mv /tmp/.bashrc $DHOME/$LOGIN/.bashrc )
		test -f $DHOME/$LOGIN/.profile && \
		 ( sed "s/umask 022/umask 002/" $DHOME/$LOGIN/.profile > \
		   /tmp/.profile ; mv /tmp/.profile $DHOME/$LOGIN/.profile )
		test -f $DHOME/$LOGIN/.login && \
		 ( sed "s/umask 022/umask 002/" $DHOME/$LOGIN/.login > \
		   /tmp/.login ; mv /tmp/.login $DHOME/$LOGIN/.login )
	fi
	# Probably will never happen, but just in case... we don't want all
	# files on the system to be `chown'ed to $NUID.$NGID!
	if [ "$DHOME/$LOGIN" != "/" ]; then
		chown -R $NUID.$NGID $DHOME/$LOGIN
	fi
	echo "done."
fi

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

passwd $LOGIN
chfn $LOGIN

# EOF
--- end: /usr/sbin/adduser ---

--- cut here: /etc/default/adduser ---
# The DSHELL variable specifies the default login shell on your system.
DSHELL="/bin/bash"

# The DHOME variable specifies the directory containing users' home
# directories.
DHOME="/home"

# The SPOOL variable specifies the directory containing users' mailboxes.
SPOOL="/var/spool/mail"

# The SKEL variable specifies the directory containing `skeletal' user
# files; in other words, files such as a sample .profile that will be
# copied to the new user's home directory when it is created.
SKEL="/etc/skel"

# The METHOD variable can be either "default" or "project".
METHOD="default"

# FIRST_UID should be the first UID for users on your system.  UIDs below
# FIRST_UID are reserved for administrative and system accounts.
FIRST_UID=1000

# USERS_GID should be the GID of the group `users' (or the equivilant
# group) on your system.
USERS_GID=100
--- end: /etc/default/adduser --

Ian Murdock <imurdock@gnu.ai.mit.edu>


Reply to: