adduser.sh revision 135616
1107543Sscottl#!/bin/sh
2107543Sscottl#
3127076Smtm# Copyright (c) 2002-2004 Michael Telahun Makonnen. All rights reserved.
4107543Sscottl#
5107543Sscottl# Redistribution and use in source and binary forms, with or without
6107543Sscottl# modification, are permitted provided that the following conditions
7107543Sscottl# are met:
8107543Sscottl# 1. Redistributions of source code must retain the above copyright
9107543Sscottl#    notice, this list of conditions and the following disclaimer.
10107543Sscottl# 2. Redistributions in binary form must reproduce the above copyright
11107543Sscottl#    notice, this list of conditions and the following disclaimer in the
12107543Sscottl#    documentation and/or other materials provided with the distribution.
13107543Sscottl#
14107543Sscottl# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15107543Sscottl# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16107543Sscottl# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17107543Sscottl# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18107543Sscottl# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19107543Sscottl# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20107543Sscottl# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21107543Sscottl# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22107543Sscottl# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23107543Sscottl# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24107543Sscottl#
25116624Smtm#       Email: Mike Makonnen <mtm@FreeBSD.Org>
26107543Sscottl#
27107543Sscottl# $FreeBSD: head/usr.sbin/adduser/adduser.sh 135616 2004-09-23 13:09:42Z roam $
28107543Sscottl#
29107543Sscottl
30107543Sscottl# err msg
31107543Sscottl#       Display $msg on stderr, unless we're being quiet.
32107543Sscottl# 
33107543Sscottlerr() {
34107543Sscottl	if [ -z "$quietflag" ]; then
35107543Sscottl        	echo 1>&2 ${THISCMD}: ERROR: $*
36107543Sscottl	fi
37107543Sscottl}
38107543Sscottl
39107543Sscottl# info msg
40107543Sscottl#       Display $msg on stdout, unless we're being quiet.
41107543Sscottl# 
42107543Sscottlinfo() {
43107543Sscottl	if [ -z "$quietflag" ]; then
44107543Sscottl        	echo ${THISCMD}: INFO: $*
45107543Sscottl	fi
46107543Sscottl}
47107543Sscottl
48107543Sscottl# get_nextuid
49107543Sscottl#	Output the value of $_uid if it is available for use. If it
50107543Sscottl#	is not, output the value of the next higher uid that is available.
51107543Sscottl#	If a uid is not specified, output the first available uid, as indicated
52107543Sscottl#	by pw(8).
53107543Sscottl# 
54107543Sscottlget_nextuid () {
55107543Sscottl	_uid=$1
56107543Sscottl	_nextuid=
57107543Sscottl
58107543Sscottl	if [ -z "$_uid" ]; then
59107543Sscottl		_nextuid="`${PWCMD} usernext | cut -f1 -d:`"
60107543Sscottl	else
61107543Sscottl		while : ; do
62107543Sscottl			${PWCMD} usershow $_uid > /dev/null 2>&1
63107543Sscottl			if [ ! "$?" -eq 0 ]; then
64107543Sscottl				_nextuid=$_uid
65107543Sscottl				break
66107543Sscottl			fi
67107543Sscottl			_uid=$(($_uid + 1))
68107543Sscottl		done
69107543Sscottl	fi
70107543Sscottl	echo $_nextuid
71107543Sscottl}
72107543Sscottl
73107543Sscottl# show_usage
74107543Sscottl#	Display usage information for this utility.
75107543Sscottl#
76107543Sscottlshow_usage() {
77107543Sscottl	echo "usage: ${THISCMD} [options]"
78107543Sscottl	echo "  options may include:"
79107543Sscottl	echo "  -C		save to the configuration file only"
80127076Smtm	echo "  -D		do not attempt to create the home directory"
81107543Sscottl	echo "  -E		disable this account after creation"
82107543Sscottl	echo "  -G		additional groups to add accounts to"
83107543Sscottl	echo "  -L		login class of the user"
84107543Sscottl	echo "  -N		do not read configuration file"
85127076Smtm	echo "  -S		a nonexistent shell is not an error"
86107543Sscottl	echo "  -d		home directory"
87107543Sscottl	echo "  -f		file from which input will be received"
88112519Smtm	echo "  -g		default login group"
89107543Sscottl	echo "  -h		display this usage message"
90107543Sscottl	echo "  -k		path to skeleton home directory"
91107543Sscottl	echo "  -m		user welcome message file"
92107543Sscottl	echo "  -q		absolute minimal user feedback"
93107543Sscottl	echo "  -s		shell"
94107543Sscottl	echo "  -u		uid to start at"
95107543Sscottl	echo "  -w		password type: no, none, yes or random"
96107543Sscottl}
97107543Sscottl
98107543Sscottl# valid_shells
99107543Sscottl#	Outputs a list of valid shells from /etc/shells. Only the
100107543Sscottl#	basename of the shell is output.
101107543Sscottl#
102107543Sscottlvalid_shells() {
103107543Sscottl	_prefix=
104107543Sscottl	cat ${ETCSHELLS} |
105107543Sscottl	while read _path _junk ; do
106107543Sscottl		case $_path in
107107543Sscottl		\#*|'')
108107543Sscottl			;;
109107543Sscottl		*)
110107543Sscottl			echo -n "${_prefix}`basename $_path`"
111107543Sscottl			_prefix=' '
112107543Sscottl			;;
113107543Sscottl		esac
114107543Sscottl	done
115116627Smtm
116127635Scperciva	# /usr/sbin/nologin is a special case
117116627Smtm	[ -x "${NOLOGIN_PATH}" ] && echo -n " ${NOLOGIN}"
118107543Sscottl}
119107543Sscottl
120107543Sscottl# fullpath_from_shell shell
121130160Smtm#	Given $shell, which is either the full path to a shell or
122130160Smtm#	the basename component of a valid shell, get the
123107543Sscottl#	full path to the shell from the /etc/shells file.
124107543Sscottl#
125107543Sscottlfullpath_from_shell() {
126107543Sscottl	_shell=$1
127107543Sscottl	[ -z "$_shell" ] && return 1
128107543Sscottl
129135616Sroam	# /usr/sbin/nologin is a special case; it needs to be handled
130135616Sroam	# before the cat | while loop, since a 'return' from within
131135616Sroam	# a subshell will not terminate the function's execution, and
132135616Sroam	# the path to the nologin shell might be printed out twice.
133135616Sroam	#
134135616Sroam	if [ "$_shell" = "${NOLOGIN}" -o \
135135616Sroam	    "$_shell" = "${NOLOGIN_PATH}" ]; then
136135616Sroam		echo ${NOLOGIN_PATH}
137135616Sroam		return 0;
138135616Sroam	fi
139135616Sroam
140107543Sscottl	cat ${ETCSHELLS} |
141107543Sscottl	while read _path _junk ; do
142107543Sscottl		case "$_path" in
143107543Sscottl		\#*|'')
144107543Sscottl			;;
145107543Sscottl		*)
146130160Smtm			if [ "$_path" = "$_shell" -o \
147130160Smtm			    "`basename $_path`" = "$_shell" ]; then
148107543Sscottl				echo $_path
149107543Sscottl				return 0
150107543Sscottl			fi
151107543Sscottl			;;
152107543Sscottl		esac
153107543Sscottl	done
154116627Smtm
155107543Sscottl	return 1
156107543Sscottl}
157107543Sscottl
158116627Smtm# shell_exists shell
159116627Smtm#	If the given shell is listed in ${ETCSHELLS} or it is
160116627Smtm#	the nologin shell this function will return 0.
161116627Smtm#	Otherwise, it will return 1. If shell is valid but
162116627Smtm#	the path is invalid or it is not executable it
163116627Smtm#	will emit an informational message saying so.
164116627Smtm#
165116627Smtmshell_exists()
166116627Smtm{
167116627Smtm	_sh="$1"
168116627Smtm	_shellchk="${GREPCMD} '^$_sh$' ${ETCSHELLS} > /dev/null 2>&1"
169116627Smtm
170116627Smtm	if ! eval $_shellchk; then
171116627Smtm		# The nologin shell is not listed in /etc/shells.
172116627Smtm		if [ "$_sh" != "${NOLOGIN_PATH}" ]; then
173116627Smtm			err "Invalid shell ($_sh) for user $username."
174116627Smtm			return 1
175116627Smtm		fi
176116627Smtm	fi
177116627Smtm	! [ -x "$_sh" ] &&
178116628Smtm	    info "The shell ($_sh) does not exist or is not executable."
179116627Smtm
180116627Smtm	return 0
181116627Smtm}
182116627Smtm
183107543Sscottl# save_config
184107543Sscottl#	Save some variables to a configuration file.
185107543Sscottl#	Note: not all script variables are saved, only those that
186107543Sscottl#	      it makes sense to save.
187107543Sscottl#
188107543Sscottlsave_config() {
189107543Sscottl	echo "# Configuration file for adduser(8)."     >  ${ADDUSERCONF}
190107543Sscottl	echo "# NOTE: only *some* variables are saved." >> ${ADDUSERCONF}
191109751Sfjoe	echo "# Last Modified on `${DATECMD}`."		>> ${ADDUSERCONF}
192107543Sscottl	echo ''				>> ${ADDUSERCONF}
193112433Smtm	echo "defaultLgroup=$ulogingroup" >> ${ADDUSERCONF}
194107543Sscottl	echo "defaultclass=$uclass"	>> ${ADDUSERCONF}
195107543Sscottl	echo "defaultgroups=$ugroups"	>> ${ADDUSERCONF}
196107543Sscottl	echo "passwdtype=$passwdtype" 	>> ${ADDUSERCONF}
197107543Sscottl	echo "homeprefix=$homeprefix" 	>> ${ADDUSERCONF}
198107543Sscottl	echo "defaultshell=$ushell"	>> ${ADDUSERCONF}
199107543Sscottl	echo "udotdir=$udotdir"		>> ${ADDUSERCONF}
200107543Sscottl	echo "msgfile=$msgfile"		>> ${ADDUSERCONF}
201107543Sscottl	echo "disableflag=$disableflag" >> ${ADDUSERCONF}
202107543Sscottl}
203107543Sscottl
204107543Sscottl# add_user
205107543Sscottl#	Add a user to the user database. If the user chose to send a welcome
206107543Sscottl#	message or lock the account, do so.
207107543Sscottl#
208107543Sscottladd_user() {
209107543Sscottl
210107543Sscottl	# Is this a configuration run? If so, don't modify user database.
211107543Sscottl	#
212107543Sscottl	if [ -n "$configflag" ]; then
213107543Sscottl		save_config
214107543Sscottl		return
215107543Sscottl	fi
216107543Sscottl
217107543Sscottl	_uid=
218107543Sscottl	_name=
219107543Sscottl	_comment=
220107543Sscottl	_gecos=
221107543Sscottl	_home=
222107543Sscottl	_group=
223107543Sscottl	_grouplist=
224107543Sscottl	_shell=
225107543Sscottl	_class=
226107543Sscottl	_dotdir=
227107543Sscottl	_expire=
228107543Sscottl	_pwexpire=
229107543Sscottl	_passwd=
230107543Sscottl	_upasswd=
231107543Sscottl	_passwdmethod=
232107543Sscottl
233109720Smtm	_name="-n '$username'"
234110595Smtm	[ -n "$uuid" ] && _uid='-u "$uuid"'
235110595Smtm	[ -n "$ulogingroup" ] && _group='-g "$ulogingroup"'
236110595Smtm	[ -n "$ugroups" ] && _grouplist='-G "$ugroups"'
237110595Smtm	[ -n "$ushell" ] && _shell='-s "$ushell"'
238110595Smtm	[ -n "$uclass" ] && _class='-L "$uclass"'
239110595Smtm	[ -n "$ugecos" ] && _comment='-c "$ugecos"'
240110595Smtm	[ -n "$udotdir" ] && _dotdir='-k "$udotdir"'
241110595Smtm	[ -n "$uexpire" ] && _expire='-e "$uexpire"'
242110595Smtm	[ -n "$upwexpire" ] && _pwexpire='-p "$upwexpire"'
243127076Smtm	if [ -z "$Dflag" -a -n "$uhome" ]; then
244127076Smtm		# The /nonexistent home directory is special. It
245127076Smtm		# means the user has no home directory.
246127076Smtm		if [ "$uhome" = "$NOHOME" ]; then
247127076Smtm			_home='-d "$uhome"'
248127076Smtm		else
249127076Smtm			_home='-m -d "$uhome"'
250127076Smtm		fi
251127076Smtm	elif [ -n "$Dflag" -a -n "$uhome" ]; then
252127076Smtm		_home='-d "$uhome"'
253127076Smtm	fi
254107543Sscottl	case $passwdtype in
255107543Sscottl	no)
256107543Sscottl		_passwdmethod="-w no"
257107543Sscottl		_passwd="-h -"
258107543Sscottl		;;
259107543Sscottl	yes)
260110595Smtm		# Note on processing the password: The outer double quotes
261110595Smtm		# make literal everything except ` and \ and $.
262110595Smtm		# The outer single quotes make literal ` and $.
263110595Smtm		# We can ensure the \ isn't treated specially by specifying
264110595Smtm		# the -r switch to the read command used to obtain the input.
265110595Smtm		#
266107543Sscottl		_passwdmethod="-w yes"
267107543Sscottl		_passwd="-h 0"
268110595Smtm		_upasswd='echo "$upass" |'
269107543Sscottl		;;
270107543Sscottl	none)
271107543Sscottl		_passwdmethod="-w none"
272107543Sscottl		;;
273107543Sscottl	random)
274107543Sscottl		_passwdmethod="-w random"
275107543Sscottl		;;
276107543Sscottl	esac
277107543Sscottl
278107543Sscottl	_pwcmd="$_upasswd ${PWCMD} useradd $_uid $_name $_group $_grouplist $_comment"
279107543Sscottl	_pwcmd="$_pwcmd $_shell $_class $_home $_dotdir $_passwdmethod $_passwd"
280107543Sscottl	_pwcmd="$_pwcmd $_expire $_pwexpire"
281107543Sscottl
282107543Sscottl	if ! _output=`eval $_pwcmd` ; then
283107543Sscottl		err "There was an error adding user ($username)."
284107543Sscottl		return 1
285107543Sscottl	else
286107543Sscottl		info "Successfully added ($username) to the user database."
287107543Sscottl		if [ "random" = "$passwdtype" ]; then
288107543Sscottl			randompass="$_output"
289107543Sscottl			info "Password for ($username) is: $randompass"
290107543Sscottl		fi
291107543Sscottl	fi
292107543Sscottl
293107543Sscottl	if [ -n "$disableflag" ]; then
294107543Sscottl		if ${PWCMD} lock $username ; then
295107543Sscottl			info "Account ($username) is locked."
296107543Sscottl		else
297107543Sscottl			info "Account ($username) could NOT be locked."
298107543Sscottl		fi
299107543Sscottl	fi
300107543Sscottl
301107543Sscottl	_line=
302107543Sscottl	_owner=
303107543Sscottl	_perms=
304107543Sscottl	if [ -n "$msgflag" ]; then
305107543Sscottl		[ -r "$msgfile" ] && {
306107543Sscottl			# We're evaluating the contents of an external file.
307107543Sscottl			# Let's not open ourselves up for attack. _perms will
308107543Sscottl			# be empty if it's writeable only by the owner. _owner
309107543Sscottl			# will *NOT* be empty if the file is owned by root.
310107543Sscottl			#
311107543Sscottl			_dir="`dirname $msgfile`"
312107543Sscottl			_file="`basename $msgfile`"
313107543Sscottl			_perms=`/usr/bin/find $_dir -name $_file -perm +07022 -prune`
314107543Sscottl			_owner=`/usr/bin/find $_dir -name $_file -user 0 -prune`
315107543Sscottl			if [ -z "$_owner" -o -n "$_perms" ]; then
316107543Sscottl				err "The message file ($msgfile) may be writeable only by root."
317107543Sscottl				return 1
318107543Sscottl			fi
319107543Sscottl			cat "$msgfile" |
320107543Sscottl			while read _line ; do
321107543Sscottl				eval echo "$_line"
322107543Sscottl			done | ${MAILCMD} -s"Welcome" ${username}
323107543Sscottl			info "Sent welcome message to ($username)."
324107543Sscottl		}
325107543Sscottl	fi
326107543Sscottl}
327107543Sscottl
328107543Sscottl# get_user
329107543Sscottl#	Reads username of the account from standard input or from a global
330107543Sscottl#	variable containing an account line from a file. The username is
331107543Sscottl#	required. If this is an interactive session it will prompt in
332107543Sscottl#	a loop until a username is entered. If it is batch processing from
333107543Sscottl#	a file it will output an error message and return to the caller.
334107543Sscottl#
335107543Sscottlget_user() {
336107543Sscottl	_input=
337107543Sscottl
338107543Sscottl	# No need to take down user names if this is a configuration saving run.
339107543Sscottl	[ -n "$configflag" ] && return
340107543Sscottl
341107543Sscottl	while : ; do
342107543Sscottl		if [ -z "$fflag" ]; then
343107543Sscottl			echo -n "Username: "
344107543Sscottl			read _input
345107543Sscottl		else
346107543Sscottl			_input="`echo "$fileline" | cut -f1 -d:`"
347107543Sscottl		fi
348107543Sscottl
349107543Sscottl		# There *must* be a username. If this is an interactive
350107543Sscottl		# session give the user an opportunity to retry.
351107543Sscottl		#
352107543Sscottl		if [ -z "$_input" ]; then
353107543Sscottl			err "You must enter a username!"
354107543Sscottl			[ -z "$fflag" ] && continue
355107543Sscottl		fi
356107543Sscottl		break
357107543Sscottl	done
358107543Sscottl	username="$_input"
359107543Sscottl}
360107543Sscottl
361107543Sscottl# get_gecos
362107543Sscottl#	Reads extra information about the user. Can be used both in interactive
363107543Sscottl#	and batch (from file) mode.
364107543Sscottl#
365107543Sscottlget_gecos() {
366107543Sscottl	_input=
367107543Sscottl
368107543Sscottl	# No need to take down additional user information for a configuration run.
369107543Sscottl	[ -n "$configflag" ] && return
370107543Sscottl
371107543Sscottl	if [ -z "$fflag" ]; then
372107543Sscottl		echo -n "Full name: "
373107543Sscottl		read _input
374107543Sscottl	else
375107543Sscottl		_input="`echo "$fileline" | cut -f7 -d:`"
376107543Sscottl	fi
377107543Sscottl	ugecos="$_input"
378107543Sscottl}
379107543Sscottl
380107543Sscottl# get_shell
381107543Sscottl#	Get the account's shell. Works in interactive and batch mode. It
382130160Smtm#	accepts either the base name of the shell or the full path.
383107543Sscottl#	If an invalid shell is entered it will simply use the default shell.
384107543Sscottl#
385107543Sscottlget_shell() {
386107543Sscottl	_input=
387107543Sscottl	_fullpath=
388107543Sscottl	ushell="$defaultshell"
389107543Sscottl
390107543Sscottl	# Make sure the current value of the shell is a valid one
391127076Smtm	if [ -z "$Sflag" ]; then
392127076Smtm		if ! shell_exists $ushell ; then
393127076Smtm			info "Using default shell ${defaultshell}."
394127076Smtm			ushell="$defaultshell"
395127076Smtm		fi
396116627Smtm	fi
397107543Sscottl
398107543Sscottl	if [ -z "$fflag" ]; then
399107543Sscottl		echo -n "Shell ($shells) [`basename $ushell`]: "
400107543Sscottl		read _input
401107543Sscottl	else
402107543Sscottl		_input="`echo "$fileline" | cut -f9 -d:`"
403107543Sscottl	fi
404107543Sscottl	if [ -n "$_input" ]; then
405127076Smtm		if [ -n "$Sflag" ]; then
406127076Smtm			ushell="$_input"
407107543Sscottl		else
408127076Smtm			_fullpath=`fullpath_from_shell $_input`
409127076Smtm			if [ -n "$_fullpath" ]; then
410127076Smtm				ushell="$_fullpath"
411127076Smtm			else
412127076Smtm				err "Invalid shell ($_input) for user $username."
413127076Smtm				info "Using default shell ${defaultshell}."
414127076Smtm				ushell="$defaultshell"
415127076Smtm			fi
416107543Sscottl		fi
417107543Sscottl	fi
418107543Sscottl}
419107543Sscottl
420107543Sscottl# get_homedir
421107543Sscottl#	Reads the account's home directory. Used both with interactive input
422107543Sscottl#	and batch input.
423107543Sscottl#
424107543Sscottlget_homedir() {
425107543Sscottl	_input=
426107543Sscottl	if [ -z "$fflag" ]; then
427107543Sscottl		echo -n "Home directory [${homeprefix}/${username}]: "
428107543Sscottl		read _input
429107543Sscottl	else
430107543Sscottl		_input="`echo "$fileline" | cut -f8 -d:`"
431107543Sscottl	fi
432107543Sscottl
433107543Sscottl	if [ -n "$_input" ]; then
434107543Sscottl		uhome="$_input"
435107543Sscottl		# if this is a configuration run, then user input is the home
436107543Sscottl		# directory prefix. Otherwise it is understood to
437107543Sscottl		# be $prefix/$user
438107543Sscottl		#
439107543Sscottl		[ -z "$configflag" ] && homeprefix="`dirname $uhome`" || homeprefix="$uhome"
440107543Sscottl	else
441107543Sscottl		uhome="${homeprefix}/${username}"
442107543Sscottl	fi
443107543Sscottl}
444107543Sscottl
445107543Sscottl# get_uid
446107543Sscottl#	Reads a numeric userid in an interactive or batch session. Automatically
447107543Sscottl#	allocates one if it is not specified.
448107543Sscottl#
449107543Sscottlget_uid() {
450107543Sscottl	uuid=${uidstart}
451107543Sscottl	_input=
452107543Sscottl	_prompt=
453107543Sscottl
454107543Sscottl	# No need to take down uids for a configuration saving run.
455107543Sscottl	[ -n "$configflag" ] && return
456107543Sscottl
457107543Sscottl	if [ -n "$uuid" ]; then
458107543Sscottl		_prompt="Uid [$uuid]: "
459107543Sscottl	else
460107543Sscottl		_prompt="Uid (Leave empty for default): "
461107543Sscottl	fi
462107543Sscottl	if [ -z "$fflag" ]; then
463109573Sfjoe		echo -n "$_prompt"
464107543Sscottl		read _input
465107543Sscottl	else
466107543Sscottl		_input="`echo "$fileline" | cut -f2 -d:`"
467107543Sscottl	fi
468107543Sscottl
469107543Sscottl	[ -n "$_input" ] && uuid=$_input
470107543Sscottl	uuid=`get_nextuid $uuid`
471107543Sscottl	uidstart=$uuid
472107543Sscottl}
473107543Sscottl
474107543Sscottl# get_class
475107543Sscottl#	Reads login class of account. Can be used in interactive or batch mode.
476107543Sscottl#
477107543Sscottlget_class() {
478107543Sscottl	uclass="$defaultclass"
479107543Sscottl	_input=
480107543Sscottl	_class=${uclass:-"default"}
481107543Sscottl
482107543Sscottl	if [ -z "$fflag" ]; then
483107543Sscottl		echo -n "Login class [$_class]: "
484107543Sscottl		read _input
485107543Sscottl	else
486107543Sscottl		_input="`echo "$fileline" | cut -f4 -d:`"
487107543Sscottl	fi
488107543Sscottl
489107543Sscottl	[ -n "$_input" ] && uclass="$_input"
490107543Sscottl}
491107543Sscottl
492107543Sscottl# get_logingroup
493107543Sscottl#	Reads user's login group. Can be used in both interactive and batch
494107543Sscottl#	modes. The specified value can be a group name or its numeric id.
495112433Smtm#	This routine leaves the field blank if nothing is provided and
496112433Smtm#	a default login group has not been set. The pw(8) command
497112433Smtm#	will then provide a login group with the same name as the username.
498107543Sscottl#
499107543Sscottlget_logingroup() {
500112433Smtm	ulogingroup="$defaultLgroup"
501107543Sscottl	_input=
502107543Sscottl
503107543Sscottl	if [ -z "$fflag" ]; then
504112433Smtm		echo -n "Login group [${ulogingroup:-$username}]: "
505107543Sscottl		read _input
506107543Sscottl	else
507107543Sscottl		_input="`echo "$fileline" | cut -f3 -d:`"
508107543Sscottl	fi
509107543Sscottl
510107543Sscottl	# Pw(8) will use the username as login group if it's left empty
511112433Smtm	[ -n "$_input" ] && ulogingroup="$_input"
512107543Sscottl}
513107543Sscottl
514107543Sscottl# get_groups
515107543Sscottl#	Read additional groups for the user. It can be used in both interactive
516107543Sscottl#	and batch modes.
517107543Sscottl#
518107543Sscottlget_groups() {
519107543Sscottl	ugroups="$defaultgroups"
520107543Sscottl	_input=
521107543Sscottl	_group=${ulogingroup:-"${username}"}
522107543Sscottl
523107543Sscottl	if [ -z "$configflag" ]; then
524107543Sscottl		[ -z "$fflag" ] && echo -n "Login group is $_group. Invite $username"
525107543Sscottl		[ -z "$fflag" ] && echo -n " into other groups? [$ugroups]: "
526107543Sscottl	else
527107543Sscottl		[ -z "$fflag" ] && echo -n "Enter additional groups [$ugroups]: "
528107543Sscottl	fi
529107543Sscottl	read _input
530107543Sscottl
531107543Sscottl	[ -n "$_input" ] && ugroups="$_input"
532107543Sscottl}
533107543Sscottl
534107543Sscottl# get_expire_dates
535107543Sscottl#	Read expiry information for the account and also for the password. This
536107543Sscottl#	routine is used only from batch processing mode.
537107543Sscottl#
538107543Sscottlget_expire_dates() {
539107543Sscottl	upwexpire="`echo "$fileline" | cut -f5 -d:`"
540107543Sscottl	uexpire="`echo "$fileline" | cut -f6 -d:`"
541107543Sscottl}
542107543Sscottl
543107543Sscottl# get_password
544107543Sscottl#	Read the password in batch processing mode. The password field matters
545107543Sscottl#	only when the password type is "yes" or "random". If the field is empty and the
546107543Sscottl#	password type is "yes", then it assumes the account has an empty passsword
547107543Sscottl#	and changes the password type accordingly. If the password type is "random"
548107543Sscottl#	and the password field is NOT empty, then it assumes the account will NOT
549107543Sscottl#	have a random password and set passwdtype to "yes."
550107543Sscottl#
551107543Sscottlget_password() {
552107543Sscottl	# We may temporarily change a password type. Make sure it's changed
553107543Sscottl	# back to whatever it was before we process the next account.
554107543Sscottl	#
555107543Sscottl	[ -n "$savedpwtype" ] && {
556107543Sscottl		passwdtype=$savedpwtype
557107543Sscottl		savedpwtype=
558107543Sscottl	}
559107543Sscottl
560107543Sscottl	# There may be a ':' in the password
561107543Sscottl	upass=${fileline#*:*:*:*:*:*:*:*:*:}
562107543Sscottl
563107543Sscottl	if [ -z "$upass" ]; then
564107543Sscottl		case $passwdtype in
565107543Sscottl		yes)
566107543Sscottl			# if it's empty, assume an empty password
567107543Sscottl			passwdtype=none
568107543Sscottl			savedpwtype=yes
569107543Sscottl			;;
570107543Sscottl		esac
571107543Sscottl	else
572107543Sscottl		case $passwdtype in
573107543Sscottl		random)
574107543Sscottl			passwdtype=yes
575107543Sscottl			savedpwtype=random
576107543Sscottl			;;
577107543Sscottl		esac
578107543Sscottl	fi
579107543Sscottl}
580107543Sscottl
581107543Sscottl# input_from_file
582107543Sscottl#	Reads a line of account information from standard input and
583107543Sscottl#	adds it to the user database.
584107543Sscottl#
585107543Sscottlinput_from_file() {
586107543Sscottl	_field=
587107543Sscottl
588110595Smtm	while read -r fileline ; do
589107543Sscottl		case "$fileline" in
590107543Sscottl		\#*|'')
591107543Sscottl			return 0
592107543Sscottl			;;
593107543Sscottl		esac
594107543Sscottl
595107543Sscottl		get_user || continue
596107543Sscottl		get_gecos
597107543Sscottl		get_uid
598107543Sscottl		get_logingroup
599107543Sscottl		get_class
600107543Sscottl		get_shell
601107543Sscottl		get_homedir
602107543Sscottl		get_password
603107543Sscottl		get_expire_dates
604107543Sscottl
605107543Sscottl		add_user
606107543Sscottl	done
607107543Sscottl}
608107543Sscottl
609107543Sscottl# input_interactive
610107543Sscottl#	Prompts for user information interactively, and commits to
611107543Sscottl#	the user database.
612107543Sscottl#
613107543Sscottlinput_interactive() {
614107543Sscottl
615107543Sscottl	_disable=
616107543Sscottl	_pass=
617107543Sscottl	_passconfirm=
618107543Sscottl	_random="no"
619107543Sscottl	_emptypass="no"
620107543Sscottl	_usepass="yes"
621112401Smtm	_logingroup_ok="no"
622112401Smtm	_groups_ok="no"
623107543Sscottl	case $passwdtype in
624107543Sscottl	none)
625107543Sscottl		_emptypass="yes"
626107543Sscottl		_usepass="yes"
627107543Sscottl		;;
628107543Sscottl	no)
629107543Sscottl		_usepass="no"
630107543Sscottl		;;
631107543Sscottl	random)
632107543Sscottl		_random="yes"
633107543Sscottl		;;
634107543Sscottl	esac
635107543Sscottl
636107543Sscottl	get_user
637107543Sscottl	get_gecos
638107543Sscottl	get_uid
639110537Sadrian
640110537Sadrian	# The case where group = user is handled elsewhere, so
641110537Sadrian	# validate any other groups the user is invited to.
642110537Sadrian	until [ "$_logingroup_ok" = yes ]; do
643110537Sadrian		get_logingroup
644110537Sadrian		_logingroup_ok=yes
645110537Sadrian		if [ -n "$ulogingroup" -a "$username" != "$ulogingroup" ]; then
646110537Sadrian			if ! ${PWCMD} show group $ulogingroup > /dev/null 2>&1; then
647110537Sadrian				echo "Group $ulogingroup does not exist!"
648110537Sadrian				_logingroup_ok=no
649110537Sadrian			fi
650110537Sadrian		fi
651110537Sadrian	done
652110537Sadrian	until [ "$_groups_ok" = yes ]; do
653110537Sadrian		get_groups
654110537Sadrian		_groups_ok=yes
655110537Sadrian		for i in $ugroups; do
656110537Sadrian			if [ "$username" != "$i" ]; then
657110537Sadrian				if ! ${PWCMD} show group $i > /dev/null 2>&1; then
658110537Sadrian					echo "Group $i does not exist!"
659110537Sadrian					_groups_ok=no
660110537Sadrian				fi
661110537Sadrian			fi
662110537Sadrian		done
663110537Sadrian	done
664110537Sadrian
665107543Sscottl	get_class
666107543Sscottl	get_shell
667107543Sscottl	get_homedir
668107543Sscottl
669107543Sscottl	while : ; do
670107543Sscottl		echo -n "Use password-based authentication? [$_usepass]: "
671107543Sscottl		read _input
672107543Sscottl		[ -z "$_input" ] && _input=$_usepass
673107543Sscottl		case $_input in
674107543Sscottl		[Nn][Oo]|[Nn])
675107543Sscottl			passwdtype="no"
676107543Sscottl			;;
677107543Sscottl		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
678107543Sscottl			while : ; do
679107543Sscottl				echo -n "Use an empty password? (yes/no) [$_emptypass]: "
680107543Sscottl				read _input
681107543Sscottl				[ -n "$_input" ] && _emptypass=$_input
682107543Sscottl				case $_emptypass in
683107543Sscottl				[Nn][Oo]|[Nn])
684107543Sscottl					echo -n "Use a random password? (yes/no) [$_random]: "
685107543Sscottl					read _input
686107543Sscottl					[ -n "$_input" ] && _random="$_input"
687107543Sscottl					case $_random in
688107543Sscottl					[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
689107543Sscottl						passwdtype="random"
690107543Sscottl						break
691107543Sscottl						;;
692107543Sscottl					esac
693107543Sscottl					passwdtype="yes"
694112401Smtm					[ -n "$configflag" ] && break
695107543Sscottl					trap 'stty echo; exit' 0 1 2 3 15
696107543Sscottl					stty -echo
697107543Sscottl					echo -n "Enter password: "
698110595Smtm					read -r upass
699107543Sscottl					echo''
700107543Sscottl					echo -n "Enter password again: "
701116623Smtm					read -r _passconfirm
702107543Sscottl					echo ''
703107543Sscottl					stty echo
704107543Sscottl					# if user entered a blank password
705107543Sscottl					# explicitly ask again.
706107543Sscottl					[ -z "$upass" -a -z "$_passconfirm" ] \
707107543Sscottl					    && continue
708107543Sscottl					;;
709107543Sscottl				[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
710107543Sscottl					passwdtype="none"
711107543Sscottl					break;
712107543Sscottl					;;
713107543Sscottl				*)
714107543Sscottl					# invalid answer; repeat the loop
715107543Sscottl					continue
716107543Sscottl					;;
717107543Sscottl				esac
718107543Sscottl				if [ "$upass" != "$_passconfirm" ]; then
719107543Sscottl					echo "Passwords did not match!"
720107543Sscottl					continue
721107543Sscottl				fi
722107543Sscottl				break
723107543Sscottl			done
724107543Sscottl			;;
725107543Sscottl		*)
726107543Sscottl			# invalid answer; repeat loop
727107543Sscottl			continue
728107543Sscottl			;;
729107543Sscottl		esac
730107543Sscottl		break;
731107543Sscottl	done
732107543Sscottl	_disable=${disableflag:-"no"}
733107543Sscottl	while : ; do
734107543Sscottl		echo -n "Lock out the account after creation? [$_disable]: "
735107543Sscottl		read _input
736107543Sscottl		[ -z "$_input" ] && _input=$_disable
737107543Sscottl		case $_input in
738107543Sscottl		[Nn][Oo]|[Nn])
739107543Sscottl			disableflag=
740107543Sscottl			;;
741107543Sscottl		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
742107543Sscottl			disableflag=yes
743107543Sscottl			;;
744107543Sscottl		*)
745107543Sscottl			# invalid answer; repeat loop
746107543Sscottl			continue
747107543Sscottl			;;
748107543Sscottl		esac
749107543Sscottl		break
750107543Sscottl	done
751107543Sscottl	
752107543Sscottl	# Display the information we have so far and prompt to
753107543Sscottl	# commit it.
754107543Sscottl	#
755107543Sscottl	_disable=${disableflag:-"no"}
756107543Sscottl	[ -z "$configflag" ] && printf "%-10s : %s\n" Username $username
757107543Sscottl	case $passwdtype in
758107543Sscottl	yes)
759107543Sscottl		_pass='*****'
760107543Sscottl		;;
761107543Sscottl	no)
762107543Sscottl		_pass='<disabled>'
763107543Sscottl		;;
764107543Sscottl	none)
765107543Sscottl		_pass='<blank>'
766107543Sscottl		;;
767107543Sscottl	random)
768107543Sscottl		_pass='<random>'
769107543Sscottl		;;
770107543Sscottl	esac
771109751Sfjoe	[ -z "$configflag" ] && printf "%-10s : %s\n" "Password" "$_pass"
772109751Sfjoe	[ -n "$configflag" ] && printf "%-10s : %s\n" "Pass Type" "$passwdtype"
773107543Sscottl	[ -z "$configflag" ] && printf "%-10s : %s\n" "Full Name" "$ugecos"
774107543Sscottl	[ -z "$configflag" ] && printf "%-10s : %s\n" "Uid" "$uuid"
775107543Sscottl	printf "%-10s : %s\n" "Class" "$uclass"
776112433Smtm	printf "%-10s : %s %s\n" "Groups" "${ulogingroup:-$username}" "$ugroups"
777107543Sscottl	printf "%-10s : %s\n" "Home" "$uhome"
778107543Sscottl	printf "%-10s : %s\n" "Shell" "$ushell"
779107543Sscottl	printf "%-10s : %s\n" "Locked" "$_disable"
780107543Sscottl	while : ; do
781107543Sscottl		echo -n "OK? (yes/no): "
782107543Sscottl		read _input
783107543Sscottl		case $_input in
784107543Sscottl		[Nn][Oo]|[Nn])
785107543Sscottl			return 1
786107543Sscottl			;;
787107543Sscottl		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
788107543Sscottl			add_user
789107543Sscottl			;;
790107543Sscottl		*)
791107543Sscottl			continue
792107543Sscottl			;;
793107543Sscottl		esac
794107543Sscottl		break
795107543Sscottl	done
796107543Sscottl	return 0
797107543Sscottl}
798107543Sscottl
799107543Sscottl#### END SUBROUTINE DEFENITION ####
800107543Sscottl
801107543SscottlTHISCMD=`/usr/bin/basename $0`
802107543SscottlDEFAULTSHELL=/bin/sh
803107543SscottlADDUSERCONF="${ADDUSERCONF:-/etc/adduser.conf}"
804107543SscottlPWCMD="${PWCMD:-/usr/sbin/pw}"
805107543SscottlMAILCMD="${MAILCMD:-mail}"
806107543SscottlETCSHELLS="${ETCSHELLS:-/etc/shells}"
807127076SmtmNOHOME="/nonexistent"
808116627SmtmNOLOGIN="nologin"
809127635ScpercivaNOLOGIN_PATH="/usr/sbin/nologin"
810109751SfjoeGREPCMD="/usr/bin/grep"
811109751SfjoeDATECMD="/bin/date"
812107543Sscottl
813107543Sscottl# Set default values
814107543Sscottl#
815107543Sscottlusername=
816107543Sscottluuid=
817107543Sscottluidstart=
818107543Sscottlugecos=
819107543Sscottlulogingroup=
820107543Sscottluclass=
821107543Sscottluhome=
822107543Sscottlupass=
823107543Sscottlushell=
824107543Sscottludotdir=/usr/share/skel
825107543Sscottlugroups=
826107543Sscottluexpire=
827107543Sscottlupwexpire=
828107543Sscottlshells="`valid_shells`"
829107543Sscottlpasswdtype="yes"
830107543Sscottlmsgfile=/etc/adduser.msg
831107543Sscottlmsgflag=
832107543Sscottlquietflag=
833107543Sscottlconfigflag=
834107543Sscottlfflag=
835107543Sscottlinfile=
836107543Sscottldisableflag=
837127076SmtmDflag=
838127076SmtmSflag=
839107543Sscottlreadconfig="yes"
840107543Sscottlhomeprefix="/home"
841107543Sscottlrandompass=
842107543Sscottlfileline=
843107543Sscottlsavedpwtype=
844107543Sscottldefaultclass=
845112433SmtmdefaultLgroup=
846116784Smtmdefaultgroups=
847107543Sscottldefaultshell="${DEFAULTSHELL}"
848107543Sscottl
849107543Sscottl# Make sure the user running this program is root. This isn't a security
850107543Sscottl# measure as much as it is a usefull method of reminding the user to
851107543Sscottl# 'su -' before he/she wastes time entering data that won't be saved.
852107543Sscottl#
853107543Sscottlprocowner=${procowner:-`/usr/bin/id -u`}
854107543Sscottlif [ "$procowner" != "0" ]; then
855107543Sscottl	err 'you must be the super-user (uid 0) to use this utility.'
856107543Sscottl	exit 1
857107543Sscottlfi
858107543Sscottl
859107543Sscottl# Overide from our conf file
860107543Sscottl# Quickly go through the commandline line to see if we should read
861107543Sscottl# from our configuration file. The actual parsing of the commandline
862107543Sscottl# arguments happens after we read in our configuration file (commandline
863107543Sscottl# should override configuration file).
864107543Sscottl#
865107543Sscottlfor _i in $* ; do
866107543Sscottl	if [ "$_i" = "-N" ]; then
867107543Sscottl		readconfig=
868107543Sscottl		break;
869107543Sscottl	fi
870107543Sscottldone
871107543Sscottlif [ -n "$readconfig" ]; then
872107543Sscottl	# On a long-lived system, the first time this script is run it
873107543Sscottl	# will barf upon reading the configuration file for its perl predecessor.
874107543Sscottl	if ( . ${ADDUSERCONF} > /dev/null 2>&1 ); then
875107543Sscottl		[ -r ${ADDUSERCONF} ] && . ${ADDUSERCONF} > /dev/null 2>&1
876107543Sscottl	fi
877107543Sscottlfi 
878107543Sscottl
879107543Sscottl# Proccess command-line options
880107543Sscottl#
881107543Sscottlfor _switch ; do
882107543Sscottl	case $_switch in
883107543Sscottl	-L)
884107543Sscottl		defaultclass="$2"
885107543Sscottl		shift; shift
886107543Sscottl		;;
887107543Sscottl	-C)
888107543Sscottl		configflag=yes
889107543Sscottl		shift
890107543Sscottl		;;
891127076Smtm	-D)
892127076Smtm		Dflag=yes
893127076Smtm		shift
894127076Smtm		;;
895107543Sscottl	-E)
896107543Sscottl		disableflag=yes
897107543Sscottl		shift
898107543Sscottl		;;
899107543Sscottl	-k)
900107543Sscottl		udotdir="$2"
901107543Sscottl		shift; shift
902107543Sscottl		;;
903107543Sscottl	-f)
904107543Sscottl		[ "$2" != "-" ] && infile="$2"
905107543Sscottl		fflag=yes
906107543Sscottl		shift; shift
907107543Sscottl		;;
908112433Smtm	-g)
909112433Smtm		defaultLgroup="$2"
910112433Smtm		shift; shift
911112433Smtm		;;
912107543Sscottl	-G)
913107543Sscottl		defaultgroups="$2"
914107543Sscottl		shift; shift
915107543Sscottl		;;
916107543Sscottl	-h)
917107543Sscottl		show_usage
918107543Sscottl		exit 0
919107543Sscottl		;;
920107543Sscottl	-d)
921107543Sscottl		homeprefix="$2"
922107543Sscottl		shift; shift
923107543Sscottl		;;
924107543Sscottl	-m)
925107543Sscottl		case "$2" in
926107543Sscottl		[Nn][Oo])
927107543Sscottl			msgflag=
928107543Sscottl			;;
929107543Sscottl		*)
930107543Sscottl			msgflag=yes
931107543Sscottl			msgfile="$2"
932107543Sscottl			;;
933107543Sscottl		esac
934107543Sscottl		shift; shift
935107543Sscottl		;;
936107543Sscottl	-N)
937107543Sscottl		readconfig=
938107543Sscottl		shift
939107543Sscottl		;;
940107543Sscottl	-w)
941107543Sscottl		case "$2" in
942107543Sscottl		no|none|random|yes)
943107543Sscottl			passwdtype=$2
944107543Sscottl			;;
945107543Sscottl		*)
946107543Sscottl			show_usage
947107543Sscottl			exit 1
948107543Sscottl			;;
949107543Sscottl		esac
950107543Sscottl		shift; shift
951107543Sscottl		;;
952107543Sscottl	-q)
953107543Sscottl		quietflag=yes
954107543Sscottl		shift
955107543Sscottl		;;
956107543Sscottl	-s)
957107543Sscottl		defaultshell="`fullpath_from_shell $2`"
958107543Sscottl		shift; shift
959107543Sscottl		;;
960127076Smtm	-S)
961127076Smtm		Sflag=yes
962127076Smtm		shift
963127076Smtm		;;
964107543Sscottl	-u)
965107543Sscottl		uidstart=$2
966107543Sscottl		shift; shift
967107543Sscottl		;;
968107543Sscottl	esac
969107543Sscottldone
970107543Sscottl
971107543Sscottl# If the -f switch was used, get input from a file. Otherwise,
972107543Sscottl# this is an interactive session.
973107543Sscottl#
974107543Sscottlif [ -n "$fflag" ]; then
975107543Sscottl	if [ -z "$infile" ]; then
976107543Sscottl		input_from_file
977107543Sscottl	elif [ -n "$infile" ]; then
978107543Sscottl		if [ -r "$infile" ]; then
979107543Sscottl			input_from_file < $infile
980107543Sscottl		else
981107543Sscottl			err "File ($infile) is unreadable or does not exist."
982107543Sscottl		fi
983107543Sscottl	fi
984107543Sscottlelse
985107543Sscottl	input_interactive
986109768Smtm	while : ; do
987112401Smtm		if [ -z "$configflag" ]; then
988112401Smtm			echo -n "Add another user? (yes/no): "
989112401Smtm		else
990112401Smtm			echo -n "Re-edit the default configuration? (yes/no): "
991112401Smtm		fi
992109768Smtm		read _input
993109768Smtm		case $_input in
994109768Smtm		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
995109768Smtm			uidstart=`get_nextuid $uidstart`
996109768Smtm			input_interactive
997109768Smtm			continue
998109768Smtm			;;
999109768Smtm		[Nn][Oo]|[Nn])
1000109768Smtm			echo "Goodbye!"
1001109768Smtm			;;
1002109768Smtm		*)
1003109768Smtm			continue
1004109768Smtm			;;
1005109768Smtm		esac
1006109768Smtm		break
1007109768Smtm	done
1008107543Sscottlfi
1009