adduser.sh revision 168656
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 168656 2007-04-12 08:17:56Z mtm $
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}
202168656Smtm	echo "uidstart=$uidstart"       >> ${ADDUSERCONF}
203107543Sscottl}
204107543Sscottl
205107543Sscottl# add_user
206107543Sscottl#	Add a user to the user database. If the user chose to send a welcome
207107543Sscottl#	message or lock the account, do so.
208107543Sscottl#
209107543Sscottladd_user() {
210107543Sscottl
211107543Sscottl	# Is this a configuration run? If so, don't modify user database.
212107543Sscottl	#
213107543Sscottl	if [ -n "$configflag" ]; then
214107543Sscottl		save_config
215107543Sscottl		return
216107543Sscottl	fi
217107543Sscottl
218107543Sscottl	_uid=
219107543Sscottl	_name=
220107543Sscottl	_comment=
221107543Sscottl	_gecos=
222107543Sscottl	_home=
223107543Sscottl	_group=
224107543Sscottl	_grouplist=
225107543Sscottl	_shell=
226107543Sscottl	_class=
227107543Sscottl	_dotdir=
228107543Sscottl	_expire=
229107543Sscottl	_pwexpire=
230107543Sscottl	_passwd=
231107543Sscottl	_upasswd=
232107543Sscottl	_passwdmethod=
233107543Sscottl
234109720Smtm	_name="-n '$username'"
235110595Smtm	[ -n "$uuid" ] && _uid='-u "$uuid"'
236110595Smtm	[ -n "$ulogingroup" ] && _group='-g "$ulogingroup"'
237110595Smtm	[ -n "$ugroups" ] && _grouplist='-G "$ugroups"'
238110595Smtm	[ -n "$ushell" ] && _shell='-s "$ushell"'
239110595Smtm	[ -n "$uclass" ] && _class='-L "$uclass"'
240110595Smtm	[ -n "$ugecos" ] && _comment='-c "$ugecos"'
241110595Smtm	[ -n "$udotdir" ] && _dotdir='-k "$udotdir"'
242110595Smtm	[ -n "$uexpire" ] && _expire='-e "$uexpire"'
243110595Smtm	[ -n "$upwexpire" ] && _pwexpire='-p "$upwexpire"'
244127076Smtm	if [ -z "$Dflag" -a -n "$uhome" ]; then
245127076Smtm		# The /nonexistent home directory is special. It
246127076Smtm		# means the user has no home directory.
247127076Smtm		if [ "$uhome" = "$NOHOME" ]; then
248127076Smtm			_home='-d "$uhome"'
249127076Smtm		else
250127076Smtm			_home='-m -d "$uhome"'
251127076Smtm		fi
252127076Smtm	elif [ -n "$Dflag" -a -n "$uhome" ]; then
253127076Smtm		_home='-d "$uhome"'
254127076Smtm	fi
255107543Sscottl	case $passwdtype in
256107543Sscottl	no)
257107543Sscottl		_passwdmethod="-w no"
258107543Sscottl		_passwd="-h -"
259107543Sscottl		;;
260107543Sscottl	yes)
261110595Smtm		# Note on processing the password: The outer double quotes
262110595Smtm		# make literal everything except ` and \ and $.
263110595Smtm		# The outer single quotes make literal ` and $.
264110595Smtm		# We can ensure the \ isn't treated specially by specifying
265110595Smtm		# the -r switch to the read command used to obtain the input.
266110595Smtm		#
267107543Sscottl		_passwdmethod="-w yes"
268107543Sscottl		_passwd="-h 0"
269110595Smtm		_upasswd='echo "$upass" |'
270107543Sscottl		;;
271107543Sscottl	none)
272107543Sscottl		_passwdmethod="-w none"
273107543Sscottl		;;
274107543Sscottl	random)
275107543Sscottl		_passwdmethod="-w random"
276107543Sscottl		;;
277107543Sscottl	esac
278107543Sscottl
279107543Sscottl	_pwcmd="$_upasswd ${PWCMD} useradd $_uid $_name $_group $_grouplist $_comment"
280107543Sscottl	_pwcmd="$_pwcmd $_shell $_class $_home $_dotdir $_passwdmethod $_passwd"
281107543Sscottl	_pwcmd="$_pwcmd $_expire $_pwexpire"
282107543Sscottl
283107543Sscottl	if ! _output=`eval $_pwcmd` ; then
284107543Sscottl		err "There was an error adding user ($username)."
285107543Sscottl		return 1
286107543Sscottl	else
287107543Sscottl		info "Successfully added ($username) to the user database."
288107543Sscottl		if [ "random" = "$passwdtype" ]; then
289107543Sscottl			randompass="$_output"
290107543Sscottl			info "Password for ($username) is: $randompass"
291107543Sscottl		fi
292107543Sscottl	fi
293107543Sscottl
294107543Sscottl	if [ -n "$disableflag" ]; then
295107543Sscottl		if ${PWCMD} lock $username ; then
296107543Sscottl			info "Account ($username) is locked."
297107543Sscottl		else
298107543Sscottl			info "Account ($username) could NOT be locked."
299107543Sscottl		fi
300107543Sscottl	fi
301107543Sscottl
302107543Sscottl	_line=
303107543Sscottl	_owner=
304107543Sscottl	_perms=
305107543Sscottl	if [ -n "$msgflag" ]; then
306107543Sscottl		[ -r "$msgfile" ] && {
307107543Sscottl			# We're evaluating the contents of an external file.
308107543Sscottl			# Let's not open ourselves up for attack. _perms will
309107543Sscottl			# be empty if it's writeable only by the owner. _owner
310107543Sscottl			# will *NOT* be empty if the file is owned by root.
311107543Sscottl			#
312107543Sscottl			_dir="`dirname $msgfile`"
313107543Sscottl			_file="`basename $msgfile`"
314107543Sscottl			_perms=`/usr/bin/find $_dir -name $_file -perm +07022 -prune`
315107543Sscottl			_owner=`/usr/bin/find $_dir -name $_file -user 0 -prune`
316107543Sscottl			if [ -z "$_owner" -o -n "$_perms" ]; then
317107543Sscottl				err "The message file ($msgfile) may be writeable only by root."
318107543Sscottl				return 1
319107543Sscottl			fi
320107543Sscottl			cat "$msgfile" |
321107543Sscottl			while read _line ; do
322107543Sscottl				eval echo "$_line"
323107543Sscottl			done | ${MAILCMD} -s"Welcome" ${username}
324107543Sscottl			info "Sent welcome message to ($username)."
325107543Sscottl		}
326107543Sscottl	fi
327107543Sscottl}
328107543Sscottl
329107543Sscottl# get_user
330107543Sscottl#	Reads username of the account from standard input or from a global
331107543Sscottl#	variable containing an account line from a file. The username is
332107543Sscottl#	required. If this is an interactive session it will prompt in
333107543Sscottl#	a loop until a username is entered. If it is batch processing from
334107543Sscottl#	a file it will output an error message and return to the caller.
335107543Sscottl#
336107543Sscottlget_user() {
337107543Sscottl	_input=
338107543Sscottl
339107543Sscottl	# No need to take down user names if this is a configuration saving run.
340107543Sscottl	[ -n "$configflag" ] && return
341107543Sscottl
342107543Sscottl	while : ; do
343107543Sscottl		if [ -z "$fflag" ]; then
344107543Sscottl			echo -n "Username: "
345107543Sscottl			read _input
346107543Sscottl		else
347107543Sscottl			_input="`echo "$fileline" | cut -f1 -d:`"
348107543Sscottl		fi
349107543Sscottl
350167917Sle		# There *must* be a username, and it must not exist. If
351167917Sle		# this is an interactive session give the user an
352167917Sle		# opportunity to retry.
353107543Sscottl		#
354107543Sscottl		if [ -z "$_input" ]; then
355107543Sscottl			err "You must enter a username!"
356107543Sscottl			[ -z "$fflag" ] && continue
357107543Sscottl		fi
358167917Sle		${PWCMD} usershow $_input > /dev/null 2>&1
359167917Sle		if [ "$?" -eq 0 ]; then
360167917Sle			err "User exists!"
361167917Sle			[ -z "$fflag" ] && continue
362167917Sle		fi
363107543Sscottl		break
364107543Sscottl	done
365107543Sscottl	username="$_input"
366107543Sscottl}
367107543Sscottl
368107543Sscottl# get_gecos
369107543Sscottl#	Reads extra information about the user. Can be used both in interactive
370107543Sscottl#	and batch (from file) mode.
371107543Sscottl#
372107543Sscottlget_gecos() {
373107543Sscottl	_input=
374107543Sscottl
375107543Sscottl	# No need to take down additional user information for a configuration run.
376107543Sscottl	[ -n "$configflag" ] && return
377107543Sscottl
378107543Sscottl	if [ -z "$fflag" ]; then
379107543Sscottl		echo -n "Full name: "
380107543Sscottl		read _input
381107543Sscottl	else
382107543Sscottl		_input="`echo "$fileline" | cut -f7 -d:`"
383107543Sscottl	fi
384107543Sscottl	ugecos="$_input"
385107543Sscottl}
386107543Sscottl
387107543Sscottl# get_shell
388107543Sscottl#	Get the account's shell. Works in interactive and batch mode. It
389130160Smtm#	accepts either the base name of the shell or the full path.
390107543Sscottl#	If an invalid shell is entered it will simply use the default shell.
391107543Sscottl#
392107543Sscottlget_shell() {
393107543Sscottl	_input=
394107543Sscottl	_fullpath=
395107543Sscottl	ushell="$defaultshell"
396107543Sscottl
397107543Sscottl	# Make sure the current value of the shell is a valid one
398127076Smtm	if [ -z "$Sflag" ]; then
399127076Smtm		if ! shell_exists $ushell ; then
400127076Smtm			info "Using default shell ${defaultshell}."
401127076Smtm			ushell="$defaultshell"
402127076Smtm		fi
403116627Smtm	fi
404107543Sscottl
405107543Sscottl	if [ -z "$fflag" ]; then
406107543Sscottl		echo -n "Shell ($shells) [`basename $ushell`]: "
407107543Sscottl		read _input
408107543Sscottl	else
409107543Sscottl		_input="`echo "$fileline" | cut -f9 -d:`"
410107543Sscottl	fi
411107543Sscottl	if [ -n "$_input" ]; then
412127076Smtm		if [ -n "$Sflag" ]; then
413127076Smtm			ushell="$_input"
414107543Sscottl		else
415127076Smtm			_fullpath=`fullpath_from_shell $_input`
416127076Smtm			if [ -n "$_fullpath" ]; then
417127076Smtm				ushell="$_fullpath"
418127076Smtm			else
419127076Smtm				err "Invalid shell ($_input) for user $username."
420127076Smtm				info "Using default shell ${defaultshell}."
421127076Smtm				ushell="$defaultshell"
422127076Smtm			fi
423107543Sscottl		fi
424107543Sscottl	fi
425107543Sscottl}
426107543Sscottl
427107543Sscottl# get_homedir
428107543Sscottl#	Reads the account's home directory. Used both with interactive input
429107543Sscottl#	and batch input.
430107543Sscottl#
431107543Sscottlget_homedir() {
432107543Sscottl	_input=
433107543Sscottl	if [ -z "$fflag" ]; then
434107543Sscottl		echo -n "Home directory [${homeprefix}/${username}]: "
435107543Sscottl		read _input
436107543Sscottl	else
437107543Sscottl		_input="`echo "$fileline" | cut -f8 -d:`"
438107543Sscottl	fi
439107543Sscottl
440107543Sscottl	if [ -n "$_input" ]; then
441107543Sscottl		uhome="$_input"
442107543Sscottl		# if this is a configuration run, then user input is the home
443107543Sscottl		# directory prefix. Otherwise it is understood to
444107543Sscottl		# be $prefix/$user
445107543Sscottl		#
446107543Sscottl		[ -z "$configflag" ] && homeprefix="`dirname $uhome`" || homeprefix="$uhome"
447107543Sscottl	else
448107543Sscottl		uhome="${homeprefix}/${username}"
449107543Sscottl	fi
450107543Sscottl}
451107543Sscottl
452107543Sscottl# get_uid
453107543Sscottl#	Reads a numeric userid in an interactive or batch session. Automatically
454107543Sscottl#	allocates one if it is not specified.
455107543Sscottl#
456107543Sscottlget_uid() {
457168656Smtm	uuid=${uidstart}
458107543Sscottl	_input=
459107543Sscottl	_prompt=
460107543Sscottl
461107543Sscottl	if [ -n "$uuid" ]; then
462107543Sscottl		_prompt="Uid [$uuid]: "
463107543Sscottl	else
464107543Sscottl		_prompt="Uid (Leave empty for default): "
465107543Sscottl	fi
466107543Sscottl	if [ -z "$fflag" ]; then
467109573Sfjoe		echo -n "$_prompt"
468107543Sscottl		read _input
469107543Sscottl	else
470107543Sscottl		_input="`echo "$fileline" | cut -f2 -d:`"
471107543Sscottl	fi
472107543Sscottl
473107543Sscottl	[ -n "$_input" ] && uuid=$_input
474107543Sscottl	uuid=`get_nextuid $uuid`
475107543Sscottl	uidstart=$uuid
476107543Sscottl}
477107543Sscottl
478107543Sscottl# get_class
479107543Sscottl#	Reads login class of account. Can be used in interactive or batch mode.
480107543Sscottl#
481107543Sscottlget_class() {
482107543Sscottl	uclass="$defaultclass"
483107543Sscottl	_input=
484107543Sscottl	_class=${uclass:-"default"}
485107543Sscottl
486107543Sscottl	if [ -z "$fflag" ]; then
487107543Sscottl		echo -n "Login class [$_class]: "
488107543Sscottl		read _input
489107543Sscottl	else
490107543Sscottl		_input="`echo "$fileline" | cut -f4 -d:`"
491107543Sscottl	fi
492107543Sscottl
493107543Sscottl	[ -n "$_input" ] && uclass="$_input"
494107543Sscottl}
495107543Sscottl
496107543Sscottl# get_logingroup
497107543Sscottl#	Reads user's login group. Can be used in both interactive and batch
498107543Sscottl#	modes. The specified value can be a group name or its numeric id.
499112433Smtm#	This routine leaves the field blank if nothing is provided and
500112433Smtm#	a default login group has not been set. The pw(8) command
501112433Smtm#	will then provide a login group with the same name as the username.
502107543Sscottl#
503107543Sscottlget_logingroup() {
504112433Smtm	ulogingroup="$defaultLgroup"
505107543Sscottl	_input=
506107543Sscottl
507107543Sscottl	if [ -z "$fflag" ]; then
508112433Smtm		echo -n "Login group [${ulogingroup:-$username}]: "
509107543Sscottl		read _input
510107543Sscottl	else
511107543Sscottl		_input="`echo "$fileline" | cut -f3 -d:`"
512107543Sscottl	fi
513107543Sscottl
514107543Sscottl	# Pw(8) will use the username as login group if it's left empty
515112433Smtm	[ -n "$_input" ] && ulogingroup="$_input"
516107543Sscottl}
517107543Sscottl
518107543Sscottl# get_groups
519107543Sscottl#	Read additional groups for the user. It can be used in both interactive
520107543Sscottl#	and batch modes.
521107543Sscottl#
522107543Sscottlget_groups() {
523107543Sscottl	ugroups="$defaultgroups"
524107543Sscottl	_input=
525107543Sscottl	_group=${ulogingroup:-"${username}"}
526107543Sscottl
527107543Sscottl	if [ -z "$configflag" ]; then
528107543Sscottl		[ -z "$fflag" ] && echo -n "Login group is $_group. Invite $username"
529107543Sscottl		[ -z "$fflag" ] && echo -n " into other groups? [$ugroups]: "
530107543Sscottl	else
531107543Sscottl		[ -z "$fflag" ] && echo -n "Enter additional groups [$ugroups]: "
532107543Sscottl	fi
533107543Sscottl	read _input
534107543Sscottl
535107543Sscottl	[ -n "$_input" ] && ugroups="$_input"
536107543Sscottl}
537107543Sscottl
538107543Sscottl# get_expire_dates
539107543Sscottl#	Read expiry information for the account and also for the password. This
540107543Sscottl#	routine is used only from batch processing mode.
541107543Sscottl#
542107543Sscottlget_expire_dates() {
543107543Sscottl	upwexpire="`echo "$fileline" | cut -f5 -d:`"
544107543Sscottl	uexpire="`echo "$fileline" | cut -f6 -d:`"
545107543Sscottl}
546107543Sscottl
547107543Sscottl# get_password
548107543Sscottl#	Read the password in batch processing mode. The password field matters
549107543Sscottl#	only when the password type is "yes" or "random". If the field is empty and the
550107543Sscottl#	password type is "yes", then it assumes the account has an empty passsword
551107543Sscottl#	and changes the password type accordingly. If the password type is "random"
552107543Sscottl#	and the password field is NOT empty, then it assumes the account will NOT
553107543Sscottl#	have a random password and set passwdtype to "yes."
554107543Sscottl#
555107543Sscottlget_password() {
556107543Sscottl	# We may temporarily change a password type. Make sure it's changed
557107543Sscottl	# back to whatever it was before we process the next account.
558107543Sscottl	#
559107543Sscottl	[ -n "$savedpwtype" ] && {
560107543Sscottl		passwdtype=$savedpwtype
561107543Sscottl		savedpwtype=
562107543Sscottl	}
563107543Sscottl
564107543Sscottl	# There may be a ':' in the password
565107543Sscottl	upass=${fileline#*:*:*:*:*:*:*:*:*:}
566107543Sscottl
567107543Sscottl	if [ -z "$upass" ]; then
568107543Sscottl		case $passwdtype in
569107543Sscottl		yes)
570107543Sscottl			# if it's empty, assume an empty password
571107543Sscottl			passwdtype=none
572107543Sscottl			savedpwtype=yes
573107543Sscottl			;;
574107543Sscottl		esac
575107543Sscottl	else
576107543Sscottl		case $passwdtype in
577107543Sscottl		random)
578107543Sscottl			passwdtype=yes
579107543Sscottl			savedpwtype=random
580107543Sscottl			;;
581107543Sscottl		esac
582107543Sscottl	fi
583107543Sscottl}
584107543Sscottl
585107543Sscottl# input_from_file
586107543Sscottl#	Reads a line of account information from standard input and
587107543Sscottl#	adds it to the user database.
588107543Sscottl#
589107543Sscottlinput_from_file() {
590107543Sscottl	_field=
591107543Sscottl
592110595Smtm	while read -r fileline ; do
593107543Sscottl		case "$fileline" in
594107543Sscottl		\#*|'')
595107543Sscottl			;;
596168651Smtm		*)
597168651Smtm			get_user || continue
598168651Smtm			get_gecos
599168651Smtm			get_uid
600168651Smtm			get_logingroup
601168651Smtm			get_class
602168651Smtm			get_shell
603168651Smtm			get_homedir
604168651Smtm			get_password
605168651Smtm			get_expire_dates
606168651Smtm
607168651Smtm			add_user
608168651Smtm			;;
609107543Sscottl		esac
610107543Sscottl	done
611107543Sscottl}
612107543Sscottl
613107543Sscottl# input_interactive
614107543Sscottl#	Prompts for user information interactively, and commits to
615107543Sscottl#	the user database.
616107543Sscottl#
617107543Sscottlinput_interactive() {
618107543Sscottl
619107543Sscottl	_disable=
620107543Sscottl	_pass=
621107543Sscottl	_passconfirm=
622107543Sscottl	_random="no"
623107543Sscottl	_emptypass="no"
624107543Sscottl	_usepass="yes"
625112401Smtm	_logingroup_ok="no"
626112401Smtm	_groups_ok="no"
627107543Sscottl	case $passwdtype in
628107543Sscottl	none)
629107543Sscottl		_emptypass="yes"
630107543Sscottl		_usepass="yes"
631107543Sscottl		;;
632107543Sscottl	no)
633107543Sscottl		_usepass="no"
634107543Sscottl		;;
635107543Sscottl	random)
636107543Sscottl		_random="yes"
637107543Sscottl		;;
638107543Sscottl	esac
639107543Sscottl
640107543Sscottl	get_user
641107543Sscottl	get_gecos
642107543Sscottl	get_uid
643110537Sadrian
644110537Sadrian	# The case where group = user is handled elsewhere, so
645110537Sadrian	# validate any other groups the user is invited to.
646110537Sadrian	until [ "$_logingroup_ok" = yes ]; do
647110537Sadrian		get_logingroup
648110537Sadrian		_logingroup_ok=yes
649110537Sadrian		if [ -n "$ulogingroup" -a "$username" != "$ulogingroup" ]; then
650110537Sadrian			if ! ${PWCMD} show group $ulogingroup > /dev/null 2>&1; then
651110537Sadrian				echo "Group $ulogingroup does not exist!"
652110537Sadrian				_logingroup_ok=no
653110537Sadrian			fi
654110537Sadrian		fi
655110537Sadrian	done
656110537Sadrian	until [ "$_groups_ok" = yes ]; do
657110537Sadrian		get_groups
658110537Sadrian		_groups_ok=yes
659110537Sadrian		for i in $ugroups; do
660110537Sadrian			if [ "$username" != "$i" ]; then
661110537Sadrian				if ! ${PWCMD} show group $i > /dev/null 2>&1; then
662110537Sadrian					echo "Group $i does not exist!"
663110537Sadrian					_groups_ok=no
664110537Sadrian				fi
665110537Sadrian			fi
666110537Sadrian		done
667110537Sadrian	done
668110537Sadrian
669107543Sscottl	get_class
670107543Sscottl	get_shell
671107543Sscottl	get_homedir
672107543Sscottl
673107543Sscottl	while : ; do
674107543Sscottl		echo -n "Use password-based authentication? [$_usepass]: "
675107543Sscottl		read _input
676107543Sscottl		[ -z "$_input" ] && _input=$_usepass
677107543Sscottl		case $_input in
678107543Sscottl		[Nn][Oo]|[Nn])
679107543Sscottl			passwdtype="no"
680107543Sscottl			;;
681107543Sscottl		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
682107543Sscottl			while : ; do
683107543Sscottl				echo -n "Use an empty password? (yes/no) [$_emptypass]: "
684107543Sscottl				read _input
685107543Sscottl				[ -n "$_input" ] && _emptypass=$_input
686107543Sscottl				case $_emptypass in
687107543Sscottl				[Nn][Oo]|[Nn])
688107543Sscottl					echo -n "Use a random password? (yes/no) [$_random]: "
689107543Sscottl					read _input
690107543Sscottl					[ -n "$_input" ] && _random="$_input"
691107543Sscottl					case $_random in
692107543Sscottl					[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
693107543Sscottl						passwdtype="random"
694107543Sscottl						break
695107543Sscottl						;;
696107543Sscottl					esac
697107543Sscottl					passwdtype="yes"
698112401Smtm					[ -n "$configflag" ] && break
699107543Sscottl					trap 'stty echo; exit' 0 1 2 3 15
700107543Sscottl					stty -echo
701107543Sscottl					echo -n "Enter password: "
702110595Smtm					read -r upass
703107543Sscottl					echo''
704107543Sscottl					echo -n "Enter password again: "
705116623Smtm					read -r _passconfirm
706107543Sscottl					echo ''
707107543Sscottl					stty echo
708107543Sscottl					# if user entered a blank password
709107543Sscottl					# explicitly ask again.
710107543Sscottl					[ -z "$upass" -a -z "$_passconfirm" ] \
711107543Sscottl					    && continue
712107543Sscottl					;;
713107543Sscottl				[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
714107543Sscottl					passwdtype="none"
715107543Sscottl					break;
716107543Sscottl					;;
717107543Sscottl				*)
718107543Sscottl					# invalid answer; repeat the loop
719107543Sscottl					continue
720107543Sscottl					;;
721107543Sscottl				esac
722107543Sscottl				if [ "$upass" != "$_passconfirm" ]; then
723107543Sscottl					echo "Passwords did not match!"
724107543Sscottl					continue
725107543Sscottl				fi
726107543Sscottl				break
727107543Sscottl			done
728107543Sscottl			;;
729107543Sscottl		*)
730107543Sscottl			# invalid answer; repeat loop
731107543Sscottl			continue
732107543Sscottl			;;
733107543Sscottl		esac
734107543Sscottl		break;
735107543Sscottl	done
736107543Sscottl	_disable=${disableflag:-"no"}
737107543Sscottl	while : ; do
738107543Sscottl		echo -n "Lock out the account after creation? [$_disable]: "
739107543Sscottl		read _input
740107543Sscottl		[ -z "$_input" ] && _input=$_disable
741107543Sscottl		case $_input in
742107543Sscottl		[Nn][Oo]|[Nn])
743107543Sscottl			disableflag=
744107543Sscottl			;;
745107543Sscottl		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
746107543Sscottl			disableflag=yes
747107543Sscottl			;;
748107543Sscottl		*)
749107543Sscottl			# invalid answer; repeat loop
750107543Sscottl			continue
751107543Sscottl			;;
752107543Sscottl		esac
753107543Sscottl		break
754107543Sscottl	done
755107543Sscottl	
756107543Sscottl	# Display the information we have so far and prompt to
757107543Sscottl	# commit it.
758107543Sscottl	#
759107543Sscottl	_disable=${disableflag:-"no"}
760107543Sscottl	[ -z "$configflag" ] && printf "%-10s : %s\n" Username $username
761107543Sscottl	case $passwdtype in
762107543Sscottl	yes)
763107543Sscottl		_pass='*****'
764107543Sscottl		;;
765107543Sscottl	no)
766107543Sscottl		_pass='<disabled>'
767107543Sscottl		;;
768107543Sscottl	none)
769107543Sscottl		_pass='<blank>'
770107543Sscottl		;;
771107543Sscottl	random)
772107543Sscottl		_pass='<random>'
773107543Sscottl		;;
774107543Sscottl	esac
775109751Sfjoe	[ -z "$configflag" ] && printf "%-10s : %s\n" "Password" "$_pass"
776109751Sfjoe	[ -n "$configflag" ] && printf "%-10s : %s\n" "Pass Type" "$passwdtype"
777107543Sscottl	[ -z "$configflag" ] && printf "%-10s : %s\n" "Full Name" "$ugecos"
778107543Sscottl	[ -z "$configflag" ] && printf "%-10s : %s\n" "Uid" "$uuid"
779107543Sscottl	printf "%-10s : %s\n" "Class" "$uclass"
780112433Smtm	printf "%-10s : %s %s\n" "Groups" "${ulogingroup:-$username}" "$ugroups"
781107543Sscottl	printf "%-10s : %s\n" "Home" "$uhome"
782107543Sscottl	printf "%-10s : %s\n" "Shell" "$ushell"
783107543Sscottl	printf "%-10s : %s\n" "Locked" "$_disable"
784107543Sscottl	while : ; do
785107543Sscottl		echo -n "OK? (yes/no): "
786107543Sscottl		read _input
787107543Sscottl		case $_input in
788107543Sscottl		[Nn][Oo]|[Nn])
789107543Sscottl			return 1
790107543Sscottl			;;
791107543Sscottl		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
792107543Sscottl			add_user
793107543Sscottl			;;
794107543Sscottl		*)
795107543Sscottl			continue
796107543Sscottl			;;
797107543Sscottl		esac
798107543Sscottl		break
799107543Sscottl	done
800107543Sscottl	return 0
801107543Sscottl}
802107543Sscottl
803145618Srobert#### END SUBROUTINE DEFINITION ####
804107543Sscottl
805107543SscottlTHISCMD=`/usr/bin/basename $0`
806107543SscottlDEFAULTSHELL=/bin/sh
807107543SscottlADDUSERCONF="${ADDUSERCONF:-/etc/adduser.conf}"
808107543SscottlPWCMD="${PWCMD:-/usr/sbin/pw}"
809107543SscottlMAILCMD="${MAILCMD:-mail}"
810107543SscottlETCSHELLS="${ETCSHELLS:-/etc/shells}"
811127076SmtmNOHOME="/nonexistent"
812116627SmtmNOLOGIN="nologin"
813127635ScpercivaNOLOGIN_PATH="/usr/sbin/nologin"
814109751SfjoeGREPCMD="/usr/bin/grep"
815109751SfjoeDATECMD="/bin/date"
816107543Sscottl
817107543Sscottl# Set default values
818107543Sscottl#
819107543Sscottlusername=
820107543Sscottluuid=
821107543Sscottluidstart=
822107543Sscottlugecos=
823107543Sscottlulogingroup=
824107543Sscottluclass=
825107543Sscottluhome=
826107543Sscottlupass=
827107543Sscottlushell=
828107543Sscottludotdir=/usr/share/skel
829107543Sscottlugroups=
830107543Sscottluexpire=
831107543Sscottlupwexpire=
832107543Sscottlshells="`valid_shells`"
833107543Sscottlpasswdtype="yes"
834107543Sscottlmsgfile=/etc/adduser.msg
835107543Sscottlmsgflag=
836107543Sscottlquietflag=
837107543Sscottlconfigflag=
838107543Sscottlfflag=
839107543Sscottlinfile=
840107543Sscottldisableflag=
841127076SmtmDflag=
842127076SmtmSflag=
843107543Sscottlreadconfig="yes"
844107543Sscottlhomeprefix="/home"
845107543Sscottlrandompass=
846107543Sscottlfileline=
847107543Sscottlsavedpwtype=
848107543Sscottldefaultclass=
849112433SmtmdefaultLgroup=
850116784Smtmdefaultgroups=
851107543Sscottldefaultshell="${DEFAULTSHELL}"
852107543Sscottl
853107543Sscottl# Make sure the user running this program is root. This isn't a security
854107543Sscottl# measure as much as it is a usefull method of reminding the user to
855107543Sscottl# 'su -' before he/she wastes time entering data that won't be saved.
856107543Sscottl#
857107543Sscottlprocowner=${procowner:-`/usr/bin/id -u`}
858107543Sscottlif [ "$procowner" != "0" ]; then
859107543Sscottl	err 'you must be the super-user (uid 0) to use this utility.'
860107543Sscottl	exit 1
861107543Sscottlfi
862107543Sscottl
863107543Sscottl# Overide from our conf file
864107543Sscottl# Quickly go through the commandline line to see if we should read
865107543Sscottl# from our configuration file. The actual parsing of the commandline
866107543Sscottl# arguments happens after we read in our configuration file (commandline
867107543Sscottl# should override configuration file).
868107543Sscottl#
869107543Sscottlfor _i in $* ; do
870107543Sscottl	if [ "$_i" = "-N" ]; then
871107543Sscottl		readconfig=
872107543Sscottl		break;
873107543Sscottl	fi
874107543Sscottldone
875107543Sscottlif [ -n "$readconfig" ]; then
876107543Sscottl	# On a long-lived system, the first time this script is run it
877107543Sscottl	# will barf upon reading the configuration file for its perl predecessor.
878107543Sscottl	if ( . ${ADDUSERCONF} > /dev/null 2>&1 ); then
879107543Sscottl		[ -r ${ADDUSERCONF} ] && . ${ADDUSERCONF} > /dev/null 2>&1
880107543Sscottl	fi
881107543Sscottlfi 
882107543Sscottl
883107543Sscottl# Proccess command-line options
884107543Sscottl#
885107543Sscottlfor _switch ; do
886107543Sscottl	case $_switch in
887107543Sscottl	-L)
888107543Sscottl		defaultclass="$2"
889107543Sscottl		shift; shift
890107543Sscottl		;;
891107543Sscottl	-C)
892107543Sscottl		configflag=yes
893107543Sscottl		shift
894107543Sscottl		;;
895127076Smtm	-D)
896127076Smtm		Dflag=yes
897127076Smtm		shift
898127076Smtm		;;
899107543Sscottl	-E)
900107543Sscottl		disableflag=yes
901107543Sscottl		shift
902107543Sscottl		;;
903107543Sscottl	-k)
904107543Sscottl		udotdir="$2"
905107543Sscottl		shift; shift
906107543Sscottl		;;
907107543Sscottl	-f)
908107543Sscottl		[ "$2" != "-" ] && infile="$2"
909107543Sscottl		fflag=yes
910107543Sscottl		shift; shift
911107543Sscottl		;;
912112433Smtm	-g)
913112433Smtm		defaultLgroup="$2"
914112433Smtm		shift; shift
915112433Smtm		;;
916107543Sscottl	-G)
917107543Sscottl		defaultgroups="$2"
918107543Sscottl		shift; shift
919107543Sscottl		;;
920107543Sscottl	-h)
921107543Sscottl		show_usage
922107543Sscottl		exit 0
923107543Sscottl		;;
924107543Sscottl	-d)
925107543Sscottl		homeprefix="$2"
926107543Sscottl		shift; shift
927107543Sscottl		;;
928107543Sscottl	-m)
929107543Sscottl		case "$2" in
930107543Sscottl		[Nn][Oo])
931107543Sscottl			msgflag=
932107543Sscottl			;;
933107543Sscottl		*)
934107543Sscottl			msgflag=yes
935107543Sscottl			msgfile="$2"
936107543Sscottl			;;
937107543Sscottl		esac
938107543Sscottl		shift; shift
939107543Sscottl		;;
940107543Sscottl	-N)
941107543Sscottl		readconfig=
942107543Sscottl		shift
943107543Sscottl		;;
944107543Sscottl	-w)
945107543Sscottl		case "$2" in
946107543Sscottl		no|none|random|yes)
947107543Sscottl			passwdtype=$2
948107543Sscottl			;;
949107543Sscottl		*)
950107543Sscottl			show_usage
951107543Sscottl			exit 1
952107543Sscottl			;;
953107543Sscottl		esac
954107543Sscottl		shift; shift
955107543Sscottl		;;
956107543Sscottl	-q)
957107543Sscottl		quietflag=yes
958107543Sscottl		shift
959107543Sscottl		;;
960107543Sscottl	-s)
961107543Sscottl		defaultshell="`fullpath_from_shell $2`"
962107543Sscottl		shift; shift
963107543Sscottl		;;
964127076Smtm	-S)
965127076Smtm		Sflag=yes
966127076Smtm		shift
967127076Smtm		;;
968107543Sscottl	-u)
969107543Sscottl		uidstart=$2
970107543Sscottl		shift; shift
971107543Sscottl		;;
972107543Sscottl	esac
973107543Sscottldone
974107543Sscottl
975107543Sscottl# If the -f switch was used, get input from a file. Otherwise,
976107543Sscottl# this is an interactive session.
977107543Sscottl#
978107543Sscottlif [ -n "$fflag" ]; then
979107543Sscottl	if [ -z "$infile" ]; then
980107543Sscottl		input_from_file
981107543Sscottl	elif [ -n "$infile" ]; then
982107543Sscottl		if [ -r "$infile" ]; then
983107543Sscottl			input_from_file < $infile
984107543Sscottl		else
985107543Sscottl			err "File ($infile) is unreadable or does not exist."
986107543Sscottl		fi
987107543Sscottl	fi
988107543Sscottlelse
989107543Sscottl	input_interactive
990109768Smtm	while : ; do
991112401Smtm		if [ -z "$configflag" ]; then
992112401Smtm			echo -n "Add another user? (yes/no): "
993112401Smtm		else
994112401Smtm			echo -n "Re-edit the default configuration? (yes/no): "
995112401Smtm		fi
996109768Smtm		read _input
997109768Smtm		case $_input in
998109768Smtm		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
999109768Smtm			uidstart=`get_nextuid $uidstart`
1000109768Smtm			input_interactive
1001109768Smtm			continue
1002109768Smtm			;;
1003109768Smtm		[Nn][Oo]|[Nn])
1004109768Smtm			echo "Goodbye!"
1005109768Smtm			;;
1006109768Smtm		*)
1007109768Smtm			continue
1008109768Smtm			;;
1009109768Smtm		esac
1010109768Smtm		break
1011109768Smtm	done
1012107543Sscottlfi
1013