adduser.sh revision 145618
1#!/bin/sh
2#
3# Copyright (c) 2002-2004 Michael Telahun Makonnen. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24#
25#       Email: Mike Makonnen <mtm@FreeBSD.Org>
26#
27# $FreeBSD: head/usr.sbin/adduser/adduser.sh 145618 2005-04-28 12:44:02Z robert $
28#
29
30# err msg
31#       Display $msg on stderr, unless we're being quiet.
32# 
33err() {
34	if [ -z "$quietflag" ]; then
35        	echo 1>&2 ${THISCMD}: ERROR: $*
36	fi
37}
38
39# info msg
40#       Display $msg on stdout, unless we're being quiet.
41# 
42info() {
43	if [ -z "$quietflag" ]; then
44        	echo ${THISCMD}: INFO: $*
45	fi
46}
47
48# get_nextuid
49#	Output the value of $_uid if it is available for use. If it
50#	is not, output the value of the next higher uid that is available.
51#	If a uid is not specified, output the first available uid, as indicated
52#	by pw(8).
53# 
54get_nextuid () {
55	_uid=$1
56	_nextuid=
57
58	if [ -z "$_uid" ]; then
59		_nextuid="`${PWCMD} usernext | cut -f1 -d:`"
60	else
61		while : ; do
62			${PWCMD} usershow $_uid > /dev/null 2>&1
63			if [ ! "$?" -eq 0 ]; then
64				_nextuid=$_uid
65				break
66			fi
67			_uid=$(($_uid + 1))
68		done
69	fi
70	echo $_nextuid
71}
72
73# show_usage
74#	Display usage information for this utility.
75#
76show_usage() {
77	echo "usage: ${THISCMD} [options]"
78	echo "  options may include:"
79	echo "  -C		save to the configuration file only"
80	echo "  -D		do not attempt to create the home directory"
81	echo "  -E		disable this account after creation"
82	echo "  -G		additional groups to add accounts to"
83	echo "  -L		login class of the user"
84	echo "  -N		do not read configuration file"
85	echo "  -S		a nonexistent shell is not an error"
86	echo "  -d		home directory"
87	echo "  -f		file from which input will be received"
88	echo "  -g		default login group"
89	echo "  -h		display this usage message"
90	echo "  -k		path to skeleton home directory"
91	echo "  -m		user welcome message file"
92	echo "  -q		absolute minimal user feedback"
93	echo "  -s		shell"
94	echo "  -u		uid to start at"
95	echo "  -w		password type: no, none, yes or random"
96}
97
98# valid_shells
99#	Outputs a list of valid shells from /etc/shells. Only the
100#	basename of the shell is output.
101#
102valid_shells() {
103	_prefix=
104	cat ${ETCSHELLS} |
105	while read _path _junk ; do
106		case $_path in
107		\#*|'')
108			;;
109		*)
110			echo -n "${_prefix}`basename $_path`"
111			_prefix=' '
112			;;
113		esac
114	done
115
116	# /usr/sbin/nologin is a special case
117	[ -x "${NOLOGIN_PATH}" ] && echo -n " ${NOLOGIN}"
118}
119
120# fullpath_from_shell shell
121#	Given $shell, which is either the full path to a shell or
122#	the basename component of a valid shell, get the
123#	full path to the shell from the /etc/shells file.
124#
125fullpath_from_shell() {
126	_shell=$1
127	[ -z "$_shell" ] && return 1
128
129	# /usr/sbin/nologin is a special case; it needs to be handled
130	# before the cat | while loop, since a 'return' from within
131	# a subshell will not terminate the function's execution, and
132	# the path to the nologin shell might be printed out twice.
133	#
134	if [ "$_shell" = "${NOLOGIN}" -o \
135	    "$_shell" = "${NOLOGIN_PATH}" ]; then
136		echo ${NOLOGIN_PATH}
137		return 0;
138	fi
139
140	cat ${ETCSHELLS} |
141	while read _path _junk ; do
142		case "$_path" in
143		\#*|'')
144			;;
145		*)
146			if [ "$_path" = "$_shell" -o \
147			    "`basename $_path`" = "$_shell" ]; then
148				echo $_path
149				return 0
150			fi
151			;;
152		esac
153	done
154
155	return 1
156}
157
158# shell_exists shell
159#	If the given shell is listed in ${ETCSHELLS} or it is
160#	the nologin shell this function will return 0.
161#	Otherwise, it will return 1. If shell is valid but
162#	the path is invalid or it is not executable it
163#	will emit an informational message saying so.
164#
165shell_exists()
166{
167	_sh="$1"
168	_shellchk="${GREPCMD} '^$_sh$' ${ETCSHELLS} > /dev/null 2>&1"
169
170	if ! eval $_shellchk; then
171		# The nologin shell is not listed in /etc/shells.
172		if [ "$_sh" != "${NOLOGIN_PATH}" ]; then
173			err "Invalid shell ($_sh) for user $username."
174			return 1
175		fi
176	fi
177	! [ -x "$_sh" ] &&
178	    info "The shell ($_sh) does not exist or is not executable."
179
180	return 0
181}
182
183# save_config
184#	Save some variables to a configuration file.
185#	Note: not all script variables are saved, only those that
186#	      it makes sense to save.
187#
188save_config() {
189	echo "# Configuration file for adduser(8)."     >  ${ADDUSERCONF}
190	echo "# NOTE: only *some* variables are saved." >> ${ADDUSERCONF}
191	echo "# Last Modified on `${DATECMD}`."		>> ${ADDUSERCONF}
192	echo ''				>> ${ADDUSERCONF}
193	echo "defaultLgroup=$ulogingroup" >> ${ADDUSERCONF}
194	echo "defaultclass=$uclass"	>> ${ADDUSERCONF}
195	echo "defaultgroups=$ugroups"	>> ${ADDUSERCONF}
196	echo "passwdtype=$passwdtype" 	>> ${ADDUSERCONF}
197	echo "homeprefix=$homeprefix" 	>> ${ADDUSERCONF}
198	echo "defaultshell=$ushell"	>> ${ADDUSERCONF}
199	echo "udotdir=$udotdir"		>> ${ADDUSERCONF}
200	echo "msgfile=$msgfile"		>> ${ADDUSERCONF}
201	echo "disableflag=$disableflag" >> ${ADDUSERCONF}
202}
203
204# add_user
205#	Add a user to the user database. If the user chose to send a welcome
206#	message or lock the account, do so.
207#
208add_user() {
209
210	# Is this a configuration run? If so, don't modify user database.
211	#
212	if [ -n "$configflag" ]; then
213		save_config
214		return
215	fi
216
217	_uid=
218	_name=
219	_comment=
220	_gecos=
221	_home=
222	_group=
223	_grouplist=
224	_shell=
225	_class=
226	_dotdir=
227	_expire=
228	_pwexpire=
229	_passwd=
230	_upasswd=
231	_passwdmethod=
232
233	_name="-n '$username'"
234	[ -n "$uuid" ] && _uid='-u "$uuid"'
235	[ -n "$ulogingroup" ] && _group='-g "$ulogingroup"'
236	[ -n "$ugroups" ] && _grouplist='-G "$ugroups"'
237	[ -n "$ushell" ] && _shell='-s "$ushell"'
238	[ -n "$uclass" ] && _class='-L "$uclass"'
239	[ -n "$ugecos" ] && _comment='-c "$ugecos"'
240	[ -n "$udotdir" ] && _dotdir='-k "$udotdir"'
241	[ -n "$uexpire" ] && _expire='-e "$uexpire"'
242	[ -n "$upwexpire" ] && _pwexpire='-p "$upwexpire"'
243	if [ -z "$Dflag" -a -n "$uhome" ]; then
244		# The /nonexistent home directory is special. It
245		# means the user has no home directory.
246		if [ "$uhome" = "$NOHOME" ]; then
247			_home='-d "$uhome"'
248		else
249			_home='-m -d "$uhome"'
250		fi
251	elif [ -n "$Dflag" -a -n "$uhome" ]; then
252		_home='-d "$uhome"'
253	fi
254	case $passwdtype in
255	no)
256		_passwdmethod="-w no"
257		_passwd="-h -"
258		;;
259	yes)
260		# Note on processing the password: The outer double quotes
261		# make literal everything except ` and \ and $.
262		# The outer single quotes make literal ` and $.
263		# We can ensure the \ isn't treated specially by specifying
264		# the -r switch to the read command used to obtain the input.
265		#
266		_passwdmethod="-w yes"
267		_passwd="-h 0"
268		_upasswd='echo "$upass" |'
269		;;
270	none)
271		_passwdmethod="-w none"
272		;;
273	random)
274		_passwdmethod="-w random"
275		;;
276	esac
277
278	_pwcmd="$_upasswd ${PWCMD} useradd $_uid $_name $_group $_grouplist $_comment"
279	_pwcmd="$_pwcmd $_shell $_class $_home $_dotdir $_passwdmethod $_passwd"
280	_pwcmd="$_pwcmd $_expire $_pwexpire"
281
282	if ! _output=`eval $_pwcmd` ; then
283		err "There was an error adding user ($username)."
284		return 1
285	else
286		info "Successfully added ($username) to the user database."
287		if [ "random" = "$passwdtype" ]; then
288			randompass="$_output"
289			info "Password for ($username) is: $randompass"
290		fi
291	fi
292
293	if [ -n "$disableflag" ]; then
294		if ${PWCMD} lock $username ; then
295			info "Account ($username) is locked."
296		else
297			info "Account ($username) could NOT be locked."
298		fi
299	fi
300
301	_line=
302	_owner=
303	_perms=
304	if [ -n "$msgflag" ]; then
305		[ -r "$msgfile" ] && {
306			# We're evaluating the contents of an external file.
307			# Let's not open ourselves up for attack. _perms will
308			# be empty if it's writeable only by the owner. _owner
309			# will *NOT* be empty if the file is owned by root.
310			#
311			_dir="`dirname $msgfile`"
312			_file="`basename $msgfile`"
313			_perms=`/usr/bin/find $_dir -name $_file -perm +07022 -prune`
314			_owner=`/usr/bin/find $_dir -name $_file -user 0 -prune`
315			if [ -z "$_owner" -o -n "$_perms" ]; then
316				err "The message file ($msgfile) may be writeable only by root."
317				return 1
318			fi
319			cat "$msgfile" |
320			while read _line ; do
321				eval echo "$_line"
322			done | ${MAILCMD} -s"Welcome" ${username}
323			info "Sent welcome message to ($username)."
324		}
325	fi
326}
327
328# get_user
329#	Reads username of the account from standard input or from a global
330#	variable containing an account line from a file. The username is
331#	required. If this is an interactive session it will prompt in
332#	a loop until a username is entered. If it is batch processing from
333#	a file it will output an error message and return to the caller.
334#
335get_user() {
336	_input=
337
338	# No need to take down user names if this is a configuration saving run.
339	[ -n "$configflag" ] && return
340
341	while : ; do
342		if [ -z "$fflag" ]; then
343			echo -n "Username: "
344			read _input
345		else
346			_input="`echo "$fileline" | cut -f1 -d:`"
347		fi
348
349		# There *must* be a username. If this is an interactive
350		# session give the user an opportunity to retry.
351		#
352		if [ -z "$_input" ]; then
353			err "You must enter a username!"
354			[ -z "$fflag" ] && continue
355		fi
356		break
357	done
358	username="$_input"
359}
360
361# get_gecos
362#	Reads extra information about the user. Can be used both in interactive
363#	and batch (from file) mode.
364#
365get_gecos() {
366	_input=
367
368	# No need to take down additional user information for a configuration run.
369	[ -n "$configflag" ] && return
370
371	if [ -z "$fflag" ]; then
372		echo -n "Full name: "
373		read _input
374	else
375		_input="`echo "$fileline" | cut -f7 -d:`"
376	fi
377	ugecos="$_input"
378}
379
380# get_shell
381#	Get the account's shell. Works in interactive and batch mode. It
382#	accepts either the base name of the shell or the full path.
383#	If an invalid shell is entered it will simply use the default shell.
384#
385get_shell() {
386	_input=
387	_fullpath=
388	ushell="$defaultshell"
389
390	# Make sure the current value of the shell is a valid one
391	if [ -z "$Sflag" ]; then
392		if ! shell_exists $ushell ; then
393			info "Using default shell ${defaultshell}."
394			ushell="$defaultshell"
395		fi
396	fi
397
398	if [ -z "$fflag" ]; then
399		echo -n "Shell ($shells) [`basename $ushell`]: "
400		read _input
401	else
402		_input="`echo "$fileline" | cut -f9 -d:`"
403	fi
404	if [ -n "$_input" ]; then
405		if [ -n "$Sflag" ]; then
406			ushell="$_input"
407		else
408			_fullpath=`fullpath_from_shell $_input`
409			if [ -n "$_fullpath" ]; then
410				ushell="$_fullpath"
411			else
412				err "Invalid shell ($_input) for user $username."
413				info "Using default shell ${defaultshell}."
414				ushell="$defaultshell"
415			fi
416		fi
417	fi
418}
419
420# get_homedir
421#	Reads the account's home directory. Used both with interactive input
422#	and batch input.
423#
424get_homedir() {
425	_input=
426	if [ -z "$fflag" ]; then
427		echo -n "Home directory [${homeprefix}/${username}]: "
428		read _input
429	else
430		_input="`echo "$fileline" | cut -f8 -d:`"
431	fi
432
433	if [ -n "$_input" ]; then
434		uhome="$_input"
435		# if this is a configuration run, then user input is the home
436		# directory prefix. Otherwise it is understood to
437		# be $prefix/$user
438		#
439		[ -z "$configflag" ] && homeprefix="`dirname $uhome`" || homeprefix="$uhome"
440	else
441		uhome="${homeprefix}/${username}"
442	fi
443}
444
445# get_uid
446#	Reads a numeric userid in an interactive or batch session. Automatically
447#	allocates one if it is not specified.
448#
449get_uid() {
450	uuid=${uidstart}
451	_input=
452	_prompt=
453
454	# No need to take down uids for a configuration saving run.
455	[ -n "$configflag" ] && return
456
457	if [ -n "$uuid" ]; then
458		_prompt="Uid [$uuid]: "
459	else
460		_prompt="Uid (Leave empty for default): "
461	fi
462	if [ -z "$fflag" ]; then
463		echo -n "$_prompt"
464		read _input
465	else
466		_input="`echo "$fileline" | cut -f2 -d:`"
467	fi
468
469	[ -n "$_input" ] && uuid=$_input
470	uuid=`get_nextuid $uuid`
471	uidstart=$uuid
472}
473
474# get_class
475#	Reads login class of account. Can be used in interactive or batch mode.
476#
477get_class() {
478	uclass="$defaultclass"
479	_input=
480	_class=${uclass:-"default"}
481
482	if [ -z "$fflag" ]; then
483		echo -n "Login class [$_class]: "
484		read _input
485	else
486		_input="`echo "$fileline" | cut -f4 -d:`"
487	fi
488
489	[ -n "$_input" ] && uclass="$_input"
490}
491
492# get_logingroup
493#	Reads user's login group. Can be used in both interactive and batch
494#	modes. The specified value can be a group name or its numeric id.
495#	This routine leaves the field blank if nothing is provided and
496#	a default login group has not been set. The pw(8) command
497#	will then provide a login group with the same name as the username.
498#
499get_logingroup() {
500	ulogingroup="$defaultLgroup"
501	_input=
502
503	if [ -z "$fflag" ]; then
504		echo -n "Login group [${ulogingroup:-$username}]: "
505		read _input
506	else
507		_input="`echo "$fileline" | cut -f3 -d:`"
508	fi
509
510	# Pw(8) will use the username as login group if it's left empty
511	[ -n "$_input" ] && ulogingroup="$_input"
512}
513
514# get_groups
515#	Read additional groups for the user. It can be used in both interactive
516#	and batch modes.
517#
518get_groups() {
519	ugroups="$defaultgroups"
520	_input=
521	_group=${ulogingroup:-"${username}"}
522
523	if [ -z "$configflag" ]; then
524		[ -z "$fflag" ] && echo -n "Login group is $_group. Invite $username"
525		[ -z "$fflag" ] && echo -n " into other groups? [$ugroups]: "
526	else
527		[ -z "$fflag" ] && echo -n "Enter additional groups [$ugroups]: "
528	fi
529	read _input
530
531	[ -n "$_input" ] && ugroups="$_input"
532}
533
534# get_expire_dates
535#	Read expiry information for the account and also for the password. This
536#	routine is used only from batch processing mode.
537#
538get_expire_dates() {
539	upwexpire="`echo "$fileline" | cut -f5 -d:`"
540	uexpire="`echo "$fileline" | cut -f6 -d:`"
541}
542
543# get_password
544#	Read the password in batch processing mode. The password field matters
545#	only when the password type is "yes" or "random". If the field is empty and the
546#	password type is "yes", then it assumes the account has an empty passsword
547#	and changes the password type accordingly. If the password type is "random"
548#	and the password field is NOT empty, then it assumes the account will NOT
549#	have a random password and set passwdtype to "yes."
550#
551get_password() {
552	# We may temporarily change a password type. Make sure it's changed
553	# back to whatever it was before we process the next account.
554	#
555	[ -n "$savedpwtype" ] && {
556		passwdtype=$savedpwtype
557		savedpwtype=
558	}
559
560	# There may be a ':' in the password
561	upass=${fileline#*:*:*:*:*:*:*:*:*:}
562
563	if [ -z "$upass" ]; then
564		case $passwdtype in
565		yes)
566			# if it's empty, assume an empty password
567			passwdtype=none
568			savedpwtype=yes
569			;;
570		esac
571	else
572		case $passwdtype in
573		random)
574			passwdtype=yes
575			savedpwtype=random
576			;;
577		esac
578	fi
579}
580
581# input_from_file
582#	Reads a line of account information from standard input and
583#	adds it to the user database.
584#
585input_from_file() {
586	_field=
587
588	while read -r fileline ; do
589		case "$fileline" in
590		\#*|'')
591			;;
592		esac
593
594		get_user || continue
595		get_gecos
596		get_uid
597		get_logingroup
598		get_class
599		get_shell
600		get_homedir
601		get_password
602		get_expire_dates
603
604		add_user
605	done
606}
607
608# input_interactive
609#	Prompts for user information interactively, and commits to
610#	the user database.
611#
612input_interactive() {
613
614	_disable=
615	_pass=
616	_passconfirm=
617	_random="no"
618	_emptypass="no"
619	_usepass="yes"
620	_logingroup_ok="no"
621	_groups_ok="no"
622	case $passwdtype in
623	none)
624		_emptypass="yes"
625		_usepass="yes"
626		;;
627	no)
628		_usepass="no"
629		;;
630	random)
631		_random="yes"
632		;;
633	esac
634
635	get_user
636	get_gecos
637	get_uid
638
639	# The case where group = user is handled elsewhere, so
640	# validate any other groups the user is invited to.
641	until [ "$_logingroup_ok" = yes ]; do
642		get_logingroup
643		_logingroup_ok=yes
644		if [ -n "$ulogingroup" -a "$username" != "$ulogingroup" ]; then
645			if ! ${PWCMD} show group $ulogingroup > /dev/null 2>&1; then
646				echo "Group $ulogingroup does not exist!"
647				_logingroup_ok=no
648			fi
649		fi
650	done
651	until [ "$_groups_ok" = yes ]; do
652		get_groups
653		_groups_ok=yes
654		for i in $ugroups; do
655			if [ "$username" != "$i" ]; then
656				if ! ${PWCMD} show group $i > /dev/null 2>&1; then
657					echo "Group $i does not exist!"
658					_groups_ok=no
659				fi
660			fi
661		done
662	done
663
664	get_class
665	get_shell
666	get_homedir
667
668	while : ; do
669		echo -n "Use password-based authentication? [$_usepass]: "
670		read _input
671		[ -z "$_input" ] && _input=$_usepass
672		case $_input in
673		[Nn][Oo]|[Nn])
674			passwdtype="no"
675			;;
676		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
677			while : ; do
678				echo -n "Use an empty password? (yes/no) [$_emptypass]: "
679				read _input
680				[ -n "$_input" ] && _emptypass=$_input
681				case $_emptypass in
682				[Nn][Oo]|[Nn])
683					echo -n "Use a random password? (yes/no) [$_random]: "
684					read _input
685					[ -n "$_input" ] && _random="$_input"
686					case $_random in
687					[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
688						passwdtype="random"
689						break
690						;;
691					esac
692					passwdtype="yes"
693					[ -n "$configflag" ] && break
694					trap 'stty echo; exit' 0 1 2 3 15
695					stty -echo
696					echo -n "Enter password: "
697					read -r upass
698					echo''
699					echo -n "Enter password again: "
700					read -r _passconfirm
701					echo ''
702					stty echo
703					# if user entered a blank password
704					# explicitly ask again.
705					[ -z "$upass" -a -z "$_passconfirm" ] \
706					    && continue
707					;;
708				[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
709					passwdtype="none"
710					break;
711					;;
712				*)
713					# invalid answer; repeat the loop
714					continue
715					;;
716				esac
717				if [ "$upass" != "$_passconfirm" ]; then
718					echo "Passwords did not match!"
719					continue
720				fi
721				break
722			done
723			;;
724		*)
725			# invalid answer; repeat loop
726			continue
727			;;
728		esac
729		break;
730	done
731	_disable=${disableflag:-"no"}
732	while : ; do
733		echo -n "Lock out the account after creation? [$_disable]: "
734		read _input
735		[ -z "$_input" ] && _input=$_disable
736		case $_input in
737		[Nn][Oo]|[Nn])
738			disableflag=
739			;;
740		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
741			disableflag=yes
742			;;
743		*)
744			# invalid answer; repeat loop
745			continue
746			;;
747		esac
748		break
749	done
750	
751	# Display the information we have so far and prompt to
752	# commit it.
753	#
754	_disable=${disableflag:-"no"}
755	[ -z "$configflag" ] && printf "%-10s : %s\n" Username $username
756	case $passwdtype in
757	yes)
758		_pass='*****'
759		;;
760	no)
761		_pass='<disabled>'
762		;;
763	none)
764		_pass='<blank>'
765		;;
766	random)
767		_pass='<random>'
768		;;
769	esac
770	[ -z "$configflag" ] && printf "%-10s : %s\n" "Password" "$_pass"
771	[ -n "$configflag" ] && printf "%-10s : %s\n" "Pass Type" "$passwdtype"
772	[ -z "$configflag" ] && printf "%-10s : %s\n" "Full Name" "$ugecos"
773	[ -z "$configflag" ] && printf "%-10s : %s\n" "Uid" "$uuid"
774	printf "%-10s : %s\n" "Class" "$uclass"
775	printf "%-10s : %s %s\n" "Groups" "${ulogingroup:-$username}" "$ugroups"
776	printf "%-10s : %s\n" "Home" "$uhome"
777	printf "%-10s : %s\n" "Shell" "$ushell"
778	printf "%-10s : %s\n" "Locked" "$_disable"
779	while : ; do
780		echo -n "OK? (yes/no): "
781		read _input
782		case $_input in
783		[Nn][Oo]|[Nn])
784			return 1
785			;;
786		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
787			add_user
788			;;
789		*)
790			continue
791			;;
792		esac
793		break
794	done
795	return 0
796}
797
798#### END SUBROUTINE DEFINITION ####
799
800THISCMD=`/usr/bin/basename $0`
801DEFAULTSHELL=/bin/sh
802ADDUSERCONF="${ADDUSERCONF:-/etc/adduser.conf}"
803PWCMD="${PWCMD:-/usr/sbin/pw}"
804MAILCMD="${MAILCMD:-mail}"
805ETCSHELLS="${ETCSHELLS:-/etc/shells}"
806NOHOME="/nonexistent"
807NOLOGIN="nologin"
808NOLOGIN_PATH="/usr/sbin/nologin"
809GREPCMD="/usr/bin/grep"
810DATECMD="/bin/date"
811
812# Set default values
813#
814username=
815uuid=
816uidstart=
817ugecos=
818ulogingroup=
819uclass=
820uhome=
821upass=
822ushell=
823udotdir=/usr/share/skel
824ugroups=
825uexpire=
826upwexpire=
827shells="`valid_shells`"
828passwdtype="yes"
829msgfile=/etc/adduser.msg
830msgflag=
831quietflag=
832configflag=
833fflag=
834infile=
835disableflag=
836Dflag=
837Sflag=
838readconfig="yes"
839homeprefix="/home"
840randompass=
841fileline=
842savedpwtype=
843defaultclass=
844defaultLgroup=
845defaultgroups=
846defaultshell="${DEFAULTSHELL}"
847
848# Make sure the user running this program is root. This isn't a security
849# measure as much as it is a usefull method of reminding the user to
850# 'su -' before he/she wastes time entering data that won't be saved.
851#
852procowner=${procowner:-`/usr/bin/id -u`}
853if [ "$procowner" != "0" ]; then
854	err 'you must be the super-user (uid 0) to use this utility.'
855	exit 1
856fi
857
858# Overide from our conf file
859# Quickly go through the commandline line to see if we should read
860# from our configuration file. The actual parsing of the commandline
861# arguments happens after we read in our configuration file (commandline
862# should override configuration file).
863#
864for _i in $* ; do
865	if [ "$_i" = "-N" ]; then
866		readconfig=
867		break;
868	fi
869done
870if [ -n "$readconfig" ]; then
871	# On a long-lived system, the first time this script is run it
872	# will barf upon reading the configuration file for its perl predecessor.
873	if ( . ${ADDUSERCONF} > /dev/null 2>&1 ); then
874		[ -r ${ADDUSERCONF} ] && . ${ADDUSERCONF} > /dev/null 2>&1
875	fi
876fi 
877
878# Proccess command-line options
879#
880for _switch ; do
881	case $_switch in
882	-L)
883		defaultclass="$2"
884		shift; shift
885		;;
886	-C)
887		configflag=yes
888		shift
889		;;
890	-D)
891		Dflag=yes
892		shift
893		;;
894	-E)
895		disableflag=yes
896		shift
897		;;
898	-k)
899		udotdir="$2"
900		shift; shift
901		;;
902	-f)
903		[ "$2" != "-" ] && infile="$2"
904		fflag=yes
905		shift; shift
906		;;
907	-g)
908		defaultLgroup="$2"
909		shift; shift
910		;;
911	-G)
912		defaultgroups="$2"
913		shift; shift
914		;;
915	-h)
916		show_usage
917		exit 0
918		;;
919	-d)
920		homeprefix="$2"
921		shift; shift
922		;;
923	-m)
924		case "$2" in
925		[Nn][Oo])
926			msgflag=
927			;;
928		*)
929			msgflag=yes
930			msgfile="$2"
931			;;
932		esac
933		shift; shift
934		;;
935	-N)
936		readconfig=
937		shift
938		;;
939	-w)
940		case "$2" in
941		no|none|random|yes)
942			passwdtype=$2
943			;;
944		*)
945			show_usage
946			exit 1
947			;;
948		esac
949		shift; shift
950		;;
951	-q)
952		quietflag=yes
953		shift
954		;;
955	-s)
956		defaultshell="`fullpath_from_shell $2`"
957		shift; shift
958		;;
959	-S)
960		Sflag=yes
961		shift
962		;;
963	-u)
964		uidstart=$2
965		shift; shift
966		;;
967	esac
968done
969
970# If the -f switch was used, get input from a file. Otherwise,
971# this is an interactive session.
972#
973if [ -n "$fflag" ]; then
974	if [ -z "$infile" ]; then
975		input_from_file
976	elif [ -n "$infile" ]; then
977		if [ -r "$infile" ]; then
978			input_from_file < $infile
979		else
980			err "File ($infile) is unreadable or does not exist."
981		fi
982	fi
983else
984	input_interactive
985	while : ; do
986		if [ -z "$configflag" ]; then
987			echo -n "Add another user? (yes/no): "
988		else
989			echo -n "Re-edit the default configuration? (yes/no): "
990		fi
991		read _input
992		case $_input in
993		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
994			uidstart=`get_nextuid $uidstart`
995			input_interactive
996			continue
997			;;
998		[Nn][Oo]|[Nn])
999			echo "Goodbye!"
1000			;;
1001		*)
1002			continue
1003			;;
1004		esac
1005		break
1006	done
1007fi
1008