adduser.sh revision 109751
1107543Sscottl#!/bin/sh
2107543Sscottl#
3107543Sscottl# Copyright (c) 2002 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# 3. The name of the author may not be used to endorse or promote products
14107543Sscottl#    derived from this software without specific prior written permission.
15107543Sscottl#
16107543Sscottl# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17107543Sscottl# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18107543Sscottl# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19107543Sscottl# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20107543Sscottl# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21107543Sscottl# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22107543Sscottl# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23107543Sscottl# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24107543Sscottl# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25107543Sscottl# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26107543Sscottl#
27107543Sscottl#       Email: Mike Makonnen <mtm@identd.net>
28107543Sscottl#
29107543Sscottl# $FreeBSD: head/usr.sbin/adduser/adduser.sh 109751 2003-01-23 20:07:40Z fjoe $
30107543Sscottl#
31107543Sscottl
32107543Sscottl# err msg
33107543Sscottl#       Display $msg on stderr, unless we're being quiet.
34107543Sscottl# 
35107543Sscottlerr() {
36107543Sscottl	if [ -z "$quietflag" ]; then
37107543Sscottl        	echo 1>&2 ${THISCMD}: ERROR: $*
38107543Sscottl	fi
39107543Sscottl}
40107543Sscottl
41107543Sscottl# info msg
42107543Sscottl#       Display $msg on stdout, unless we're being quiet.
43107543Sscottl# 
44107543Sscottlinfo() {
45107543Sscottl	if [ -z "$quietflag" ]; then
46107543Sscottl        	echo ${THISCMD}: INFO: $*
47107543Sscottl	fi
48107543Sscottl}
49107543Sscottl
50107543Sscottl# get_nextuid
51107543Sscottl#	Output the value of $_uid if it is available for use. If it
52107543Sscottl#	is not, output the value of the next higher uid that is available.
53107543Sscottl#	If a uid is not specified, output the first available uid, as indicated
54107543Sscottl#	by pw(8).
55107543Sscottl# 
56107543Sscottlget_nextuid () {
57107543Sscottl	_uid=$1
58107543Sscottl	_nextuid=
59107543Sscottl
60107543Sscottl	if [ -z "$_uid" ]; then
61107543Sscottl		_nextuid="`${PWCMD} usernext | cut -f1 -d:`"
62107543Sscottl	else
63107543Sscottl		while : ; do
64107543Sscottl			${PWCMD} usershow $_uid > /dev/null 2>&1
65107543Sscottl			if [ ! "$?" -eq 0 ]; then
66107543Sscottl				_nextuid=$_uid
67107543Sscottl				break
68107543Sscottl			fi
69107543Sscottl			_uid=$(($_uid + 1))
70107543Sscottl		done
71107543Sscottl	fi
72107543Sscottl	echo $_nextuid
73107543Sscottl}
74107543Sscottl
75107543Sscottl# show_usage
76107543Sscottl#	Display usage information for this utility.
77107543Sscottl#
78107543Sscottlshow_usage() {
79107543Sscottl	echo "usage: ${THISCMD} [options]"
80107543Sscottl	echo "  options may include:"
81107543Sscottl	echo "  -C		save to the configuration file only"
82107543Sscottl	echo "  -E		disable this account after creation"
83107543Sscottl	echo "  -G		additional groups to add accounts to"
84107543Sscottl	echo "  -L		login class of the user"
85107543Sscottl	echo "  -N		do not read configuration file"
86107543Sscottl	echo "  -d		home directory"
87107543Sscottl	echo "  -f		file from which input will be received"
88107543Sscottl	echo "  -h		display this usage message"
89107543Sscottl	echo "  -k		path to skeleton home directory"
90107543Sscottl	echo "  -m		user welcome message file"
91107543Sscottl	echo "  -q		absolute minimal user feedback"
92107543Sscottl	echo "  -s		shell"
93107543Sscottl	echo "  -u		uid to start at"
94107543Sscottl	echo "  -w		password type: no, none, yes or random"
95107543Sscottl}
96107543Sscottl
97107543Sscottl# valid_shells
98107543Sscottl#	Outputs a list of valid shells from /etc/shells. Only the
99107543Sscottl#	basename of the shell is output.
100107543Sscottl#
101107543Sscottlvalid_shells() {
102107543Sscottl	_prefix=
103107543Sscottl	cat ${ETCSHELLS} |
104107543Sscottl	while read _path _junk ; do
105107543Sscottl		case $_path in
106107543Sscottl		\#*|'')
107107543Sscottl			;;
108107543Sscottl		*)
109107543Sscottl			echo -n "${_prefix}`basename $_path`"
110107543Sscottl			_prefix=' '
111107543Sscottl			;;
112107543Sscottl		esac
113107543Sscottl	done
114107543Sscottl}
115107543Sscottl
116107543Sscottl# fullpath_from_shell shell
117107543Sscottl#	Given $shell, the basename component of a valid shell, get the
118107543Sscottl#	full path to the shell from the /etc/shells file.
119107543Sscottl#
120107543Sscottlfullpath_from_shell() {
121107543Sscottl	_shell=$1
122107543Sscottl	[ -z "$_shell" ] && return 1
123107543Sscottl
124107543Sscottl	cat ${ETCSHELLS} |
125107543Sscottl	while read _path _junk ; do
126107543Sscottl		case "$_path" in
127107543Sscottl		\#*|'')
128107543Sscottl			;;
129107543Sscottl		*)
130107543Sscottl			if [ "`basename $_path`" = "$_shell" ]; then
131107543Sscottl				echo $_path
132107543Sscottl				return 0
133107543Sscottl			fi
134107543Sscottl			;;
135107543Sscottl		esac
136107543Sscottl	done
137107543Sscottl	return 1
138107543Sscottl}
139107543Sscottl
140107543Sscottl# save_config
141107543Sscottl#	Save some variables to a configuration file.
142107543Sscottl#	Note: not all script variables are saved, only those that
143107543Sscottl#	      it makes sense to save.
144107543Sscottl#
145107543Sscottlsave_config() {
146107543Sscottl	echo "# Configuration file for adduser(8)."     >  ${ADDUSERCONF}
147107543Sscottl	echo "# NOTE: only *some* variables are saved." >> ${ADDUSERCONF}
148109751Sfjoe	echo "# Last Modified on `${DATECMD}`."		>> ${ADDUSERCONF}
149107543Sscottl	echo ''				>> ${ADDUSERCONF}
150107543Sscottl	echo "defaultclass=$uclass"	>> ${ADDUSERCONF}
151107543Sscottl	echo "defaultgroups=$ugroups"	>> ${ADDUSERCONF}
152107543Sscottl	echo "passwdtype=$passwdtype" 	>> ${ADDUSERCONF}
153107543Sscottl	echo "homeprefix=$homeprefix" 	>> ${ADDUSERCONF}
154107543Sscottl	echo "defaultshell=$ushell"	>> ${ADDUSERCONF}
155107543Sscottl	echo "udotdir=$udotdir"		>> ${ADDUSERCONF}
156107543Sscottl	echo "msgfile=$msgfile"		>> ${ADDUSERCONF}
157107543Sscottl	echo "disableflag=$disableflag" >> ${ADDUSERCONF}
158109751Sfjoe	echo "adduserlog=$adduserlog"	>> ${ADDUSERCONF}
159107543Sscottl}
160107543Sscottl
161107543Sscottl# add_user
162107543Sscottl#	Add a user to the user database. If the user chose to send a welcome
163107543Sscottl#	message or lock the account, do so.
164107543Sscottl#
165107543Sscottladd_user() {
166107543Sscottl
167107543Sscottl	# Is this a configuration run? If so, don't modify user database.
168107543Sscottl	#
169107543Sscottl	if [ -n "$configflag" ]; then
170107543Sscottl		save_config
171107543Sscottl		return
172107543Sscottl	fi
173107543Sscottl
174107543Sscottl	_uid=
175107543Sscottl	_name=
176107543Sscottl	_comment=
177107543Sscottl	_gecos=
178107543Sscottl	_home=
179107543Sscottl	_group=
180107543Sscottl	_grouplist=
181107543Sscottl	_shell=
182107543Sscottl	_class=
183107543Sscottl	_dotdir=
184107543Sscottl	_expire=
185107543Sscottl	_pwexpire=
186107543Sscottl	_passwd=
187107543Sscottl	_upasswd=
188107543Sscottl	_passwdmethod=
189107543Sscottl
190109720Smtm	_name="-n '$username'"
191109720Smtm	[ -n "$uuid" ] && _uid="-u '$uuid'"
192109720Smtm	[ -n "$ulogingroup" ] && _group="-g '$ulogingroup'"
193109720Smtm	[ -n "$ugroups" ] && _grouplist="-G '$ugroups'"
194109720Smtm	[ -n "$ushell" ] && _shell="-s '$ushell'"
195109720Smtm	[ -n "$uhome" ] && _home="-m -d '$uhome'"
196109720Smtm	[ -n "$uclass" ] && _class="-L '$uclass'"
197107543Sscottl	[ -n "$ugecos" ] && _comment="-c '$ugecos'"
198109720Smtm	[ -n "$udotdir" ] && _dotdir="-k '$udotdir'"
199107543Sscottl	[ -n "$uexpire" ] && _expire="-e '$uexpire'"
200107543Sscottl	[ -n "$upwexpire" ] && _pwexpire="-p '$upwexpire'"
201107543Sscottl	case $passwdtype in
202107543Sscottl	no)
203107543Sscottl		_passwdmethod="-w no"
204107543Sscottl		_passwd="-h -"
205107543Sscottl		;;
206107543Sscottl	yes)
207107543Sscottl		_passwdmethod="-w yes"
208107543Sscottl		_passwd="-h 0"
209109635Smtm		_upasswd="echo '$upass' |"
210107543Sscottl		;;
211107543Sscottl	none)
212107543Sscottl		_passwdmethod="-w none"
213107543Sscottl		;;
214107543Sscottl	random)
215107543Sscottl		_passwdmethod="-w random"
216107543Sscottl		;;
217107543Sscottl	esac
218107543Sscottl
219107543Sscottl	_pwcmd="$_upasswd ${PWCMD} useradd $_uid $_name $_group $_grouplist $_comment"
220107543Sscottl	_pwcmd="$_pwcmd $_shell $_class $_home $_dotdir $_passwdmethod $_passwd"
221107543Sscottl	_pwcmd="$_pwcmd $_expire $_pwexpire"
222107543Sscottl
223107543Sscottl	if ! _output=`eval $_pwcmd` ; then
224107543Sscottl		err "There was an error adding user ($username)."
225107543Sscottl		return 1
226107543Sscottl	else
227107543Sscottl		info "Successfully added ($username) to the user database."
228107543Sscottl		if [ "random" = "$passwdtype" ]; then
229107543Sscottl			randompass="$_output"
230107543Sscottl			info "Password for ($username) is: $randompass"
231107543Sscottl		fi
232107543Sscottl	fi
233107543Sscottl
234107543Sscottl	if [ -n "$disableflag" ]; then
235107543Sscottl		if ${PWCMD} lock $username ; then
236107543Sscottl			info "Account ($username) is locked."
237107543Sscottl		else
238107543Sscottl			info "Account ($username) could NOT be locked."
239107543Sscottl		fi
240107543Sscottl	fi
241107543Sscottl
242109751Sfjoe	_log=${adduserlog:-no}
243109751Sfjoe	[ x"$_log" = x"no" ] || (echo "$(${DATECMD} +'%Y/%m/%d %T') $(${PWCMD} 2>/dev/null usershow -n $username)" >> $_log)
244109751Sfjoe
245107543Sscottl	_line=
246107543Sscottl	_owner=
247107543Sscottl	_perms=
248107543Sscottl	if [ -n "$msgflag" ]; then
249107543Sscottl		[ -r "$msgfile" ] && {
250107543Sscottl			# We're evaluating the contents of an external file.
251107543Sscottl			# Let's not open ourselves up for attack. _perms will
252107543Sscottl			# be empty if it's writeable only by the owner. _owner
253107543Sscottl			# will *NOT* be empty if the file is owned by root.
254107543Sscottl			#
255107543Sscottl			_dir="`dirname $msgfile`"
256107543Sscottl			_file="`basename $msgfile`"
257107543Sscottl			_perms=`/usr/bin/find $_dir -name $_file -perm +07022 -prune`
258107543Sscottl			_owner=`/usr/bin/find $_dir -name $_file -user 0 -prune`
259107543Sscottl			if [ -z "$_owner" -o -n "$_perms" ]; then
260107543Sscottl				err "The message file ($msgfile) may be writeable only by root."
261107543Sscottl				return 1
262107543Sscottl			fi
263107543Sscottl			cat "$msgfile" |
264107543Sscottl			while read _line ; do
265107543Sscottl				eval echo "$_line"
266107543Sscottl			done | ${MAILCMD} -s"Welcome" ${username}
267107543Sscottl			info "Sent welcome message to ($username)."
268107543Sscottl		}
269107543Sscottl	fi
270107543Sscottl}
271107543Sscottl
272107543Sscottl# get_user
273107543Sscottl#	Reads username of the account from standard input or from a global
274107543Sscottl#	variable containing an account line from a file. The username is
275107543Sscottl#	required. If this is an interactive session it will prompt in
276107543Sscottl#	a loop until a username is entered. If it is batch processing from
277107543Sscottl#	a file it will output an error message and return to the caller.
278107543Sscottl#
279107543Sscottlget_user() {
280107543Sscottl	_input=
281107543Sscottl
282107543Sscottl	# No need to take down user names if this is a configuration saving run.
283107543Sscottl	[ -n "$configflag" ] && return
284107543Sscottl
285107543Sscottl	while : ; do
286107543Sscottl		if [ -z "$fflag" ]; then
287107543Sscottl			echo -n "Username: "
288107543Sscottl			read _input
289107543Sscottl		else
290107543Sscottl			_input="`echo "$fileline" | cut -f1 -d:`"
291107543Sscottl		fi
292107543Sscottl
293107543Sscottl		# There *must* be a username. If this is an interactive
294107543Sscottl		# session give the user an opportunity to retry.
295107543Sscottl		#
296107543Sscottl		if [ -z "$_input" ]; then
297107543Sscottl			err "You must enter a username!"
298107543Sscottl			[ -z "$fflag" ] && continue
299107543Sscottl		fi
300107543Sscottl		break
301107543Sscottl	done
302107543Sscottl	username="$_input"
303107543Sscottl}
304107543Sscottl
305107543Sscottl# get_gecos
306107543Sscottl#	Reads extra information about the user. Can be used both in interactive
307107543Sscottl#	and batch (from file) mode.
308107543Sscottl#
309107543Sscottlget_gecos() {
310107543Sscottl	_input=
311107543Sscottl
312107543Sscottl	# No need to take down additional user information for a configuration run.
313107543Sscottl	[ -n "$configflag" ] && return
314107543Sscottl
315107543Sscottl	if [ -z "$fflag" ]; then
316107543Sscottl		echo -n "Full name: "
317107543Sscottl		read _input
318107543Sscottl	else
319107543Sscottl		_input="`echo "$fileline" | cut -f7 -d:`"
320107543Sscottl	fi
321107543Sscottl	ugecos="$_input"
322107543Sscottl}
323107543Sscottl
324107543Sscottl# get_shell
325107543Sscottl#	Get the account's shell. Works in interactive and batch mode. It
326107543Sscottl#	accepts only the base name of the shell, NOT the full path.
327107543Sscottl#	If an invalid shell is entered it will simply use the default shell.
328107543Sscottl#
329107543Sscottlget_shell() {
330107543Sscottl	_input=
331107543Sscottl	_fullpath=
332107543Sscottl	ushell="$defaultshell"
333107543Sscottl
334107543Sscottl	# Make sure the current value of the shell is a valid one
335109751Sfjoe	_shellchk="${GREPCMD} '^$ushell$' ${ETCSHELLS} > /dev/null 2>&1"
336107543Sscottl	eval $_shellchk || {
337107543Sscottl		err "Invalid shell ($ushell). Using default shell ${defaultshell}."
338107543Sscottl		ushell="$defaultshell"
339107543Sscottl	}
340107543Sscottl
341107543Sscottl	if [ -z "$fflag" ]; then
342107543Sscottl		echo -n "Shell ($shells) [`basename $ushell`]: "
343107543Sscottl		read _input
344107543Sscottl	else
345107543Sscottl		_input="`echo "$fileline" | cut -f9 -d:`"
346107543Sscottl	fi
347107543Sscottl	if [ -n "$_input" ]; then
348107543Sscottl		_fullpath=`fullpath_from_shell $_input`
349107543Sscottl		if [ -n "$_fullpath" ]; then
350107543Sscottl			ushell="$_fullpath"
351107543Sscottl		else
352107543Sscottl			err "Invalid shell selection. Using default shell ${defaultshell}."
353107543Sscottl			ushell="$defaultshell"
354107543Sscottl		fi
355107543Sscottl	fi
356107543Sscottl}
357107543Sscottl
358107543Sscottl# get_homedir
359107543Sscottl#	Reads the account's home directory. Used both with interactive input
360107543Sscottl#	and batch input.
361107543Sscottl#
362107543Sscottlget_homedir() {
363107543Sscottl	_input=
364107543Sscottl	if [ -z "$fflag" ]; then
365107543Sscottl		echo -n "Home directory [${homeprefix}/${username}]: "
366107543Sscottl		read _input
367107543Sscottl	else
368107543Sscottl		_input="`echo "$fileline" | cut -f8 -d:`"
369107543Sscottl	fi
370107543Sscottl
371107543Sscottl	if [ -n "$_input" ]; then
372107543Sscottl		uhome="$_input"
373107543Sscottl		# if this is a configuration run, then user input is the home
374107543Sscottl		# directory prefix. Otherwise it is understood to
375107543Sscottl		# be $prefix/$user
376107543Sscottl		#
377107543Sscottl		[ -z "$configflag" ] && homeprefix="`dirname $uhome`" || homeprefix="$uhome"
378107543Sscottl	else
379107543Sscottl		uhome="${homeprefix}/${username}"
380107543Sscottl	fi
381107543Sscottl}
382107543Sscottl
383107543Sscottl# get_uid
384107543Sscottl#	Reads a numeric userid in an interactive or batch session. Automatically
385107543Sscottl#	allocates one if it is not specified.
386107543Sscottl#
387107543Sscottlget_uid() {
388107543Sscottl	uuid=${uidstart}
389107543Sscottl	_input=
390107543Sscottl	_prompt=
391107543Sscottl
392107543Sscottl	# No need to take down uids for a configuration saving run.
393107543Sscottl	[ -n "$configflag" ] && return
394107543Sscottl
395107543Sscottl	if [ -n "$uuid" ]; then
396107543Sscottl		_prompt="Uid [$uuid]: "
397107543Sscottl	else
398107543Sscottl		_prompt="Uid (Leave empty for default): "
399107543Sscottl	fi
400107543Sscottl	if [ -z "$fflag" ]; then
401109573Sfjoe		echo -n "$_prompt"
402107543Sscottl		read _input
403107543Sscottl	else
404107543Sscottl		_input="`echo "$fileline" | cut -f2 -d:`"
405107543Sscottl	fi
406107543Sscottl
407107543Sscottl	[ -n "$_input" ] && uuid=$_input
408107543Sscottl	uuid=`get_nextuid $uuid`
409107543Sscottl	uidstart=$uuid
410107543Sscottl}
411107543Sscottl
412107543Sscottl# get_class
413107543Sscottl#	Reads login class of account. Can be used in interactive or batch mode.
414107543Sscottl#
415107543Sscottlget_class() {
416107543Sscottl	uclass="$defaultclass"
417107543Sscottl	_input=
418107543Sscottl	_class=${uclass:-"default"}
419107543Sscottl
420107543Sscottl	if [ -z "$fflag" ]; then
421107543Sscottl		echo -n "Login class [$_class]: "
422107543Sscottl		read _input
423107543Sscottl	else
424107543Sscottl		_input="`echo "$fileline" | cut -f4 -d:`"
425107543Sscottl	fi
426107543Sscottl
427107543Sscottl	[ -n "$_input" ] && uclass="$_input"
428107543Sscottl}
429107543Sscottl
430107543Sscottl# get_logingroup
431107543Sscottl#	Reads user's login group. Can be used in both interactive and batch
432107543Sscottl#	modes. The specified value can be a group name or its numeric id.
433107543Sscottl#	This routine leaves the field blank if nothing is provided. The pw(8)
434107543Sscottl#	command will then provide a login group with the same name as the username.
435107543Sscottl#
436107543Sscottlget_logingroup() {
437107543Sscottl	ulogingroup=
438107543Sscottl	_input=
439107543Sscottl
440107543Sscottl	# No need to take down a login group for a configuration saving run.
441107543Sscottl	[ -n "$configflag" ] && return
442107543Sscottl
443107543Sscottl	if [ -z "$fflag" ]; then
444107543Sscottl		echo -n "Login group [$username]: "
445107543Sscottl		read _input
446107543Sscottl	else
447107543Sscottl		_input="`echo "$fileline" | cut -f3 -d:`"
448107543Sscottl	fi
449107543Sscottl
450107543Sscottl	# Pw(8) will use the username as login group if it's left empty
451107543Sscottl	[ -n "$_input" ] && ulogingroup="$_input" || ulogingroup=
452107543Sscottl}
453107543Sscottl
454107543Sscottl# get_groups
455107543Sscottl#	Read additional groups for the user. It can be used in both interactive
456107543Sscottl#	and batch modes.
457107543Sscottl#
458107543Sscottlget_groups() {
459107543Sscottl	ugroups="$defaultgroups"
460107543Sscottl	_input=
461107543Sscottl	_group=${ulogingroup:-"${username}"}
462107543Sscottl
463107543Sscottl	if [ -z "$configflag" ]; then
464107543Sscottl		[ -z "$fflag" ] && echo -n "Login group is $_group. Invite $username"
465107543Sscottl		[ -z "$fflag" ] && echo -n " into other groups? [$ugroups]: "
466107543Sscottl	else
467107543Sscottl		[ -z "$fflag" ] && echo -n "Enter additional groups [$ugroups]: "
468107543Sscottl	fi
469107543Sscottl	read _input
470107543Sscottl
471107543Sscottl	[ -n "$_input" ] && ugroups="$_input"
472107543Sscottl}
473107543Sscottl
474107543Sscottl# get_expire_dates
475107543Sscottl#	Read expiry information for the account and also for the password. This
476107543Sscottl#	routine is used only from batch processing mode.
477107543Sscottl#
478107543Sscottlget_expire_dates() {
479107543Sscottl	upwexpire="`echo "$fileline" | cut -f5 -d:`"
480107543Sscottl	uexpire="`echo "$fileline" | cut -f6 -d:`"
481107543Sscottl}
482107543Sscottl
483107543Sscottl# get_password
484107543Sscottl#	Read the password in batch processing mode. The password field matters
485107543Sscottl#	only when the password type is "yes" or "random". If the field is empty and the
486107543Sscottl#	password type is "yes", then it assumes the account has an empty passsword
487107543Sscottl#	and changes the password type accordingly. If the password type is "random"
488107543Sscottl#	and the password field is NOT empty, then it assumes the account will NOT
489107543Sscottl#	have a random password and set passwdtype to "yes."
490107543Sscottl#
491107543Sscottlget_password() {
492107543Sscottl	# We may temporarily change a password type. Make sure it's changed
493107543Sscottl	# back to whatever it was before we process the next account.
494107543Sscottl	#
495107543Sscottl	[ -n "$savedpwtype" ] && {
496107543Sscottl		passwdtype=$savedpwtype
497107543Sscottl		savedpwtype=
498107543Sscottl	}
499107543Sscottl
500107543Sscottl	# There may be a ':' in the password
501107543Sscottl	upass=${fileline#*:*:*:*:*:*:*:*:*:}
502107543Sscottl
503107543Sscottl	if [ -z "$upass" ]; then
504107543Sscottl		case $passwdtype in
505107543Sscottl		yes)
506107543Sscottl			# if it's empty, assume an empty password
507107543Sscottl			passwdtype=none
508107543Sscottl			savedpwtype=yes
509107543Sscottl			;;
510107543Sscottl		esac
511107543Sscottl	else
512107543Sscottl		case $passwdtype in
513107543Sscottl		random)
514107543Sscottl			passwdtype=yes
515107543Sscottl			savedpwtype=random
516107543Sscottl			;;
517107543Sscottl		esac
518107543Sscottl	fi
519107543Sscottl}
520107543Sscottl
521107543Sscottl# input_from_file
522107543Sscottl#	Reads a line of account information from standard input and
523107543Sscottl#	adds it to the user database.
524107543Sscottl#
525107543Sscottlinput_from_file() {
526107543Sscottl	_field=
527107543Sscottl
528107543Sscottl	while read fileline ; do
529107543Sscottl		case "$fileline" in
530107543Sscottl		\#*|'')
531107543Sscottl			return 0
532107543Sscottl			;;
533107543Sscottl		esac
534107543Sscottl
535107543Sscottl		get_user || continue
536107543Sscottl		get_gecos
537107543Sscottl		get_uid
538107543Sscottl		get_logingroup
539107543Sscottl		get_class
540107543Sscottl		get_shell
541107543Sscottl		get_homedir
542107543Sscottl		get_password
543107543Sscottl		get_expire_dates
544107543Sscottl
545107543Sscottl		add_user
546107543Sscottl	done
547107543Sscottl}
548107543Sscottl
549107543Sscottl# input_interactive
550107543Sscottl#	Prompts for user information interactively, and commits to
551107543Sscottl#	the user database.
552107543Sscottl#
553107543Sscottlinput_interactive() {
554107543Sscottl
555107543Sscottl	_disable=
556107543Sscottl	_pass=
557107543Sscottl	_passconfirm=
558107543Sscottl	_random="no"
559107543Sscottl	_emptypass="no"
560107543Sscottl	_usepass="yes"
561107543Sscottl	case $passwdtype in
562107543Sscottl	none)
563107543Sscottl		_emptypass="yes"
564107543Sscottl		_usepass="yes"
565107543Sscottl		;;
566107543Sscottl	no)
567107543Sscottl		_usepass="no"
568107543Sscottl		;;
569107543Sscottl	random)
570107543Sscottl		_random="yes"
571107543Sscottl		;;
572107543Sscottl	esac
573107543Sscottl
574107543Sscottl	get_user
575107543Sscottl	get_gecos
576107543Sscottl	get_uid
577107543Sscottl	get_logingroup
578107543Sscottl	get_groups
579107543Sscottl	get_class
580107543Sscottl	get_shell
581107543Sscottl	get_homedir
582107543Sscottl
583107543Sscottl	while : ; do
584107543Sscottl		echo -n "Use password-based authentication? [$_usepass]: "
585107543Sscottl		read _input
586107543Sscottl		[ -z "$_input" ] && _input=$_usepass
587107543Sscottl		case $_input in
588107543Sscottl		[Nn][Oo]|[Nn])
589107543Sscottl			passwdtype="no"
590107543Sscottl			;;
591107543Sscottl		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
592107543Sscottl			while : ; do
593107543Sscottl				echo -n "Use an empty password? (yes/no) [$_emptypass]: "
594107543Sscottl				read _input
595107543Sscottl				[ -n "$_input" ] && _emptypass=$_input
596107543Sscottl				case $_emptypass in
597107543Sscottl				[Nn][Oo]|[Nn])
598107543Sscottl					echo -n "Use a random password? (yes/no) [$_random]: "
599107543Sscottl					read _input
600107543Sscottl					[ -n "$_input" ] && _random="$_input"
601107543Sscottl					case $_random in
602107543Sscottl					[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
603107543Sscottl						passwdtype="random"
604107543Sscottl						break
605107543Sscottl						;;
606107543Sscottl					esac
607107543Sscottl					passwdtype="yes"
608109751Sfjoe					[ -n "$configrun" ] && break
609107543Sscottl					trap 'stty echo; exit' 0 1 2 3 15
610107543Sscottl					stty -echo
611107543Sscottl					echo -n "Enter password: "
612107543Sscottl					read upass
613107543Sscottl					echo''
614107543Sscottl					echo -n "Enter password again: "
615107543Sscottl					read _passconfirm
616107543Sscottl					echo ''
617107543Sscottl					stty echo
618107543Sscottl					# if user entered a blank password
619107543Sscottl					# explicitly ask again.
620107543Sscottl					[ -z "$upass" -a -z "$_passconfirm" ] \
621107543Sscottl					    && continue
622107543Sscottl					;;
623107543Sscottl				[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
624107543Sscottl					passwdtype="none"
625107543Sscottl					break;
626107543Sscottl					;;
627107543Sscottl				*)
628107543Sscottl					# invalid answer; repeat the loop
629107543Sscottl					continue
630107543Sscottl					;;
631107543Sscottl				esac
632107543Sscottl				if [ "$upass" != "$_passconfirm" ]; then
633107543Sscottl					echo "Passwords did not match!"
634107543Sscottl					continue
635107543Sscottl				fi
636107543Sscottl				break
637107543Sscottl			done
638107543Sscottl			;;
639107543Sscottl		*)
640107543Sscottl			# invalid answer; repeat loop
641107543Sscottl			continue
642107543Sscottl			;;
643107543Sscottl		esac
644107543Sscottl		break;
645107543Sscottl	done
646107543Sscottl	_disable=${disableflag:-"no"}
647107543Sscottl	while : ; do
648107543Sscottl		echo -n "Lock out the account after creation? [$_disable]: "
649107543Sscottl		read _input
650107543Sscottl		[ -z "$_input" ] && _input=$_disable
651107543Sscottl		case $_input in
652107543Sscottl		[Nn][Oo]|[Nn])
653107543Sscottl			disableflag=
654107543Sscottl			;;
655107543Sscottl		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
656107543Sscottl			disableflag=yes
657107543Sscottl			;;
658107543Sscottl		*)
659107543Sscottl			# invalid answer; repeat loop
660107543Sscottl			continue
661107543Sscottl			;;
662107543Sscottl		esac
663107543Sscottl		break
664107543Sscottl	done
665107543Sscottl	
666107543Sscottl	# Display the information we have so far and prompt to
667107543Sscottl	# commit it.
668107543Sscottl	#
669107543Sscottl	_disable=${disableflag:-"no"}
670107543Sscottl	[ -z "$configflag" ] && printf "%-10s : %s\n" Username $username
671107543Sscottl	case $passwdtype in
672107543Sscottl	yes)
673107543Sscottl		_pass='*****'
674107543Sscottl		;;
675107543Sscottl	no)
676107543Sscottl		_pass='<disabled>'
677107543Sscottl		;;
678107543Sscottl	none)
679107543Sscottl		_pass='<blank>'
680107543Sscottl		;;
681107543Sscottl	random)
682107543Sscottl		_pass='<random>'
683107543Sscottl		;;
684107543Sscottl	esac
685109751Sfjoe	[ -z "$configflag" ] && printf "%-10s : %s\n" "Password" "$_pass"
686109751Sfjoe	[ -n "$configflag" ] && printf "%-10s : %s\n" "Pass Type" "$passwdtype"
687107543Sscottl	[ -z "$configflag" ] && printf "%-10s : %s\n" "Full Name" "$ugecos"
688107543Sscottl	[ -z "$configflag" ] && printf "%-10s : %s\n" "Uid" "$uuid"
689107543Sscottl	printf "%-10s : %s\n" "Class" "$uclass"
690107543Sscottl	[ -z "$configflag" ] && printf "%-10s : %s %s\n" "Groups" "${ulogingroup:-$username}" "$ugroups"
691107543Sscottl	printf "%-10s : %s\n" "Home" "$uhome"
692107543Sscottl	printf "%-10s : %s\n" "Shell" "$ushell"
693107543Sscottl	printf "%-10s : %s\n" "Locked" "$_disable"
694107543Sscottl	while : ; do
695107543Sscottl		echo -n "OK? (yes/no): "
696107543Sscottl		read _input
697107543Sscottl		case $_input in
698107543Sscottl		[Nn][Oo]|[Nn])
699107543Sscottl			return 1
700107543Sscottl			;;
701107543Sscottl		[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
702107543Sscottl			add_user
703107543Sscottl			;;
704107543Sscottl		*)
705107543Sscottl			continue
706107543Sscottl			;;
707107543Sscottl		esac
708107543Sscottl		break
709107543Sscottl	done
710107543Sscottl	return 0
711107543Sscottl}
712107543Sscottl
713107543Sscottl#### END SUBROUTINE DEFENITION ####
714107543Sscottl
715107543SscottlTHISCMD=`/usr/bin/basename $0`
716107543SscottlDEFAULTSHELL=/bin/sh
717107543SscottlADDUSERCONF="${ADDUSERCONF:-/etc/adduser.conf}"
718109751SfjoeADDUSERLOG="${ADDUSERLOG:-/var/log/adduser}"
719107543SscottlPWCMD="${PWCMD:-/usr/sbin/pw}"
720107543SscottlMAILCMD="${MAILCMD:-mail}"
721107543SscottlETCSHELLS="${ETCSHELLS:-/etc/shells}"
722109751SfjoeGREPCMD="/usr/bin/grep"
723109751SfjoeDATECMD="/bin/date"
724107543Sscottl
725107543Sscottl# Set default values
726107543Sscottl#
727107543Sscottlusername=
728107543Sscottluuid=
729107543Sscottluidstart=
730107543Sscottlugecos=
731107543Sscottlulogingroup=
732107543Sscottluclass=
733107543Sscottluhome=
734107543Sscottlupass=
735107543Sscottlushell=
736107543Sscottludotdir=/usr/share/skel
737107543Sscottlugroups=
738107543Sscottluexpire=
739107543Sscottlupwexpire=
740107543Sscottlshells="`valid_shells`"
741107543Sscottlpasswdtype="yes"
742107543Sscottlmsgfile=/etc/adduser.msg
743107543Sscottlmsgflag=
744107543Sscottlquietflag=
745107543Sscottlconfigflag=
746107543Sscottlfflag=
747107543Sscottlinfile=
748107543Sscottldisableflag=
749109751Sfjoeadduserlog="${ADDUSERLOG}"
750107543Sscottlreadconfig="yes"
751107543Sscottlhomeprefix="/home"
752107543Sscottlrandompass=
753107543Sscottlfileline=
754107543Sscottlsavedpwtype=
755107543Sscottldefaultclass=
756107543Sscottldefaultgoups=
757107543Sscottldefaultshell="${DEFAULTSHELL}"
758107543Sscottl
759107543Sscottl# Make sure the user running this program is root. This isn't a security
760107543Sscottl# measure as much as it is a usefull method of reminding the user to
761107543Sscottl# 'su -' before he/she wastes time entering data that won't be saved.
762107543Sscottl#
763107543Sscottlprocowner=${procowner:-`/usr/bin/id -u`}
764107543Sscottlif [ "$procowner" != "0" ]; then
765107543Sscottl	err 'you must be the super-user (uid 0) to use this utility.'
766107543Sscottl	exit 1
767107543Sscottlfi
768107543Sscottl
769107543Sscottl# Overide from our conf file
770107543Sscottl# Quickly go through the commandline line to see if we should read
771107543Sscottl# from our configuration file. The actual parsing of the commandline
772107543Sscottl# arguments happens after we read in our configuration file (commandline
773107543Sscottl# should override configuration file).
774107543Sscottl#
775107543Sscottlfor _i in $* ; do
776107543Sscottl	if [ "$_i" = "-N" ]; then
777107543Sscottl		readconfig=
778107543Sscottl		break;
779107543Sscottl	fi
780107543Sscottldone
781107543Sscottlif [ -n "$readconfig" ]; then
782107543Sscottl	# On a long-lived system, the first time this script is run it
783107543Sscottl	# will barf upon reading the configuration file for its perl predecessor.
784107543Sscottl	if ( . ${ADDUSERCONF} > /dev/null 2>&1 ); then
785107543Sscottl		[ -r ${ADDUSERCONF} ] && . ${ADDUSERCONF} > /dev/null 2>&1
786107543Sscottl	fi
787107543Sscottlfi 
788107543Sscottl
789107543Sscottl# Proccess command-line options
790107543Sscottl#
791107543Sscottlfor _switch ; do
792107543Sscottl	case $_switch in
793107543Sscottl	-L)
794107543Sscottl		defaultclass="$2"
795107543Sscottl		shift; shift
796107543Sscottl		;;
797107543Sscottl	-C)
798107543Sscottl		configflag=yes
799107543Sscottl		shift
800107543Sscottl		;;
801107543Sscottl	-E)
802107543Sscottl		disableflag=yes
803107543Sscottl		shift
804107543Sscottl		;;
805107543Sscottl	-k)
806107543Sscottl		udotdir="$2"
807107543Sscottl		shift; shift
808107543Sscottl		;;
809107543Sscottl	-f)
810107543Sscottl		[ "$2" != "-" ] && infile="$2"
811107543Sscottl		fflag=yes
812107543Sscottl		shift; shift
813107543Sscottl		;;
814107543Sscottl	-G)
815107543Sscottl		defaultgroups="$2"
816107543Sscottl		shift; shift
817107543Sscottl		;;
818107543Sscottl	-h)
819107543Sscottl		show_usage
820107543Sscottl		exit 0
821107543Sscottl		;;
822107543Sscottl	-d)
823107543Sscottl		homeprefix="$2"
824107543Sscottl		shift; shift
825107543Sscottl		;;
826107543Sscottl	-m)
827107543Sscottl		case "$2" in
828107543Sscottl		[Nn][Oo])
829107543Sscottl			msgflag=
830107543Sscottl			;;
831107543Sscottl		*)
832107543Sscottl			msgflag=yes
833107543Sscottl			msgfile="$2"
834107543Sscottl			;;
835107543Sscottl		esac
836107543Sscottl		shift; shift
837107543Sscottl		;;
838107543Sscottl	-N)
839107543Sscottl		readconfig=
840107543Sscottl		shift
841107543Sscottl		;;
842107543Sscottl	-w)
843107543Sscottl		case "$2" in
844107543Sscottl		no|none|random|yes)
845107543Sscottl			passwdtype=$2
846107543Sscottl			;;
847107543Sscottl		*)
848107543Sscottl			show_usage
849107543Sscottl			exit 1
850107543Sscottl			;;
851107543Sscottl		esac
852107543Sscottl		shift; shift
853107543Sscottl		;;
854107543Sscottl	-q)
855107543Sscottl		quietflag=yes
856107543Sscottl		shift
857107543Sscottl		;;
858107543Sscottl	-s)
859107543Sscottl		defaultshell="`fullpath_from_shell $2`"
860107543Sscottl		shift; shift
861107543Sscottl		;;
862107543Sscottl	-u)
863107543Sscottl		uidstart=$2
864107543Sscottl		shift; shift
865107543Sscottl		;;
866107543Sscottl	esac
867107543Sscottldone
868107543Sscottl
869107543Sscottl# If the -f switch was used, get input from a file. Otherwise,
870107543Sscottl# this is an interactive session.
871107543Sscottl#
872107543Sscottlif [ -n "$fflag" ]; then
873107543Sscottl	if [ -z "$infile" ]; then
874107543Sscottl		input_from_file
875107543Sscottl	elif [ -n "$infile" ]; then
876107543Sscottl		if [ -r "$infile" ]; then
877107543Sscottl			input_from_file < $infile
878107543Sscottl		else
879107543Sscottl			err "File ($infile) is unreadable or does not exist."
880107543Sscottl		fi
881107543Sscottl	fi
882107543Sscottlelse
883107543Sscottl	input_interactive
884107543Sscottlfi
885