3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright (c) 2002-2004 Michael Telahun Makonnen. All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 # Email: Mike Makonnen <mtm@FreeBSD.Org>
33 # Display $msg on stderr, unless we're being quiet.
36 if [ -z "$quietflag" ]; then
37 echo 1>&2 ${THISCMD}: ERROR
: $
*
42 # Display $msg on stdout, unless we're being quiet.
45 if [ -z "$quietflag" ]; then
46 echo ${THISCMD}: INFO
: $
*
51 # Output the value of $_uid if it is available for use. If it
52 # is not, output the value of the next higher uid that is available.
53 # If a uid is not specified, output the first available uid, as indicated
60 if [ -z "$_uid" ]; then
61 _nextuid
="`${PWCMD} usernext | cut -f1 -d:`"
64 ${PWCMD} usershow
$_uid > /dev
/null
2>&1
65 if [ ! "$?" -eq 0 ]; then
76 # Display usage information for this utility.
79 echo "usage: ${THISCMD} [options]"
80 echo " options may include:"
81 echo " -C save to the configuration file only"
82 echo " -D do not attempt to create the home directory"
83 echo " -E disable this account after creation"
84 echo " -G additional groups to add accounts to"
85 echo " -L login class of the user"
86 echo " -M file permission for home directory"
87 echo " -N do not read configuration file"
88 echo " -S a nonexistent shell is not an error"
89 echo " -d home directory"
90 echo " -f file from which input will be received"
91 echo " -g default login group"
92 echo " -h display this usage message"
93 echo " -k path to skeleton home directory"
94 echo " -m user welcome message file"
95 echo " -q absolute minimal user feedback"
97 echo " -u uid to start at"
98 echo " -w password type: no, none, yes or random"
102 # Outputs a list of valid shells from /etc/shells. Only the
103 # basename of the shell is output.
108 while read _path _junk
; do
113 echo -n "${_prefix}`basename $_path`"
119 # /usr/sbin/nologin is a special case
120 [ -x "${NOLOGIN_PATH}" ] && echo -n " ${NOLOGIN}"
123 # fullpath_from_shell shell
124 # Given $shell, which is either the full path to a shell or
125 # the basename component of a valid shell, get the
126 # full path to the shell from the /etc/shells file.
128 fullpath_from_shell
() {
130 [ -z "$_shell" ] && return 1
132 # /usr/sbin/nologin is a special case; it needs to be handled
133 # before the cat | while loop, since a 'return' from within
134 # a subshell will not terminate the function's execution, and
135 # the path to the nologin shell might be printed out twice.
137 if [ "$_shell" = "${NOLOGIN}" -o \
138 "$_shell" = "${NOLOGIN_PATH}" ]; then
144 while read _path _junk
; do
149 if [ "$_path" = "$_shell" -o \
150 "`basename $_path`" = "$_shell" ]; then
162 # If the given shell is listed in ${ETCSHELLS} or it is
163 # the nologin shell this function will return 0.
164 # Otherwise, it will return 1. If shell is valid but
165 # the path is invalid or it is not executable it
166 # will emit an informational message saying so.
170 _shellchk
="${GREPCMD} '^$_sh$' ${ETCSHELLS} > /dev/null 2>&1"
172 if ! eval $_shellchk; then
173 # The nologin shell is not listed in /etc/shells.
174 if [ "$_sh" != "${NOLOGIN_PATH}" ]; then
175 err
"Invalid shell ($_sh) for user $username."
180 info
"The shell ($_sh) does not exist or is not executable."
186 # Save some variables to a configuration file.
187 # Note: not all script variables are saved, only those that
188 # it makes sense to save.
191 echo "# Configuration file for adduser(8)." > ${ADDUSERCONF}
192 echo "# NOTE: only *some* variables are saved." >> ${ADDUSERCONF}
193 echo "# Last Modified on `${DATECMD}`." >> ${ADDUSERCONF}
194 echo '' >> ${ADDUSERCONF}
195 echo "defaultHomePerm=$uhomeperm" >> ${ADDUSERCONF}
196 echo "defaultLgroup=$ulogingroup" >> ${ADDUSERCONF}
197 echo "defaultclass=$uclass" >> ${ADDUSERCONF}
198 echo "defaultgroups=$ugroups" >> ${ADDUSERCONF}
199 echo "passwdtype=$passwdtype" >> ${ADDUSERCONF}
200 echo "homeprefix=$homeprefix" >> ${ADDUSERCONF}
201 echo "defaultshell=$ushell" >> ${ADDUSERCONF}
202 echo "udotdir=$udotdir" >> ${ADDUSERCONF}
203 echo "msgfile=$msgfile" >> ${ADDUSERCONF}
204 echo "disableflag=$disableflag" >> ${ADDUSERCONF}
205 echo "uidstart=$uidstart" >> ${ADDUSERCONF}
209 # Add a user to the user database. If the user chose to send a welcome
210 # message or lock the account, do so.
214 # Is this a configuration run? If so, don't modify user database.
216 if [ -n "$configflag" ]; then
237 _name
="-n '$username'"
238 [ -n "$uuid" ] && _uid
='-u "$uuid"'
239 [ -n "$ulogingroup" ] && _group
='-g "$ulogingroup"'
240 [ -n "$ugroups" ] && _grouplist
='-G "$ugroups"'
241 [ -n "$ushell" ] && _shell
='-s "$ushell"'
242 [ -n "$uclass" ] && _class
='-L "$uclass"'
243 [ -n "$ugecos" ] && _comment
='-c "$ugecos"'
244 [ -n "$udotdir" ] && _dotdir
='-k "$udotdir"'
245 [ -n "$uexpire" ] && _expire
='-e "$uexpire"'
246 [ -n "$upwexpire" ] && _pwexpire
='-p "$upwexpire"'
247 if [ -z "$Dflag" -a -n "$uhome" ]; then
248 # The /nonexistent home directory is special. It
249 # means the user has no home directory.
250 if [ "$uhome" = "$NOHOME" ]; then
253 # Use home directory permissions if specified
254 if [ -n "$uhomeperm" ]; then
255 _home
='-m -d "$uhome" -M "$uhomeperm"'
257 _home
='-m -d "$uhome"'
260 elif [ -n "$Dflag" -a -n "$uhome" ]; then
265 _passwdmethod
="-w no"
269 # Note on processing the password: The outer double quotes
270 # make literal everything except ` and \ and $.
271 # The outer single quotes make literal ` and $.
272 # We can ensure the \ isn't treated specially by specifying
273 # the -r switch to the read command used to obtain the input.
275 _passwdmethod
="-w yes"
277 _upasswd
='echo "$upass" |'
280 _passwdmethod
="-w none"
283 _passwdmethod
="-w random"
287 _pwcmd
="$_upasswd ${PWCMD} useradd $_uid $_name $_group $_grouplist $_comment"
288 _pwcmd
="$_pwcmd $_shell $_class $_home $_dotdir $_passwdmethod $_passwd"
289 _pwcmd
="$_pwcmd $_expire $_pwexpire"
291 if ! _output
=`eval $_pwcmd` ; then
292 err
"There was an error adding user ($username)."
295 info
"Successfully added ($username) to the user database."
296 if [ "random" = "$passwdtype" ]; then
297 randompass
="$_output"
298 info
"Password for ($username) is: $randompass"
302 if [ -n "$disableflag" ]; then
303 if ${PWCMD} lock
$username ; then
304 info
"Account ($username) is locked."
306 info
"Account ($username) could NOT be locked."
313 if [ -n "$msgflag" ]; then
314 [ -r "$msgfile" ] && {
315 # We're evaluating the contents of an external file.
316 # Let's not open ourselves up for attack. _perms will
317 # be empty if it's writeable only by the owner. _owner
318 # will *NOT* be empty if the file is owned by root.
320 _dir
="`dirname $msgfile`"
321 _file
="`basename $msgfile`"
322 _perms
=`@MEMO_PREFIX@@MEMO_SUB_PREFIX@/bin/find $_dir -name $_file -perm +07022 -prune`
323 _owner
=`@MEMO_PREFIX@@MEMO_SUB_PREFIX@/bin/find $_dir -name $_file -user 0 -prune`
324 if [ -z "$_owner" -o -n "$_perms" ]; then
325 err
"The message file ($msgfile) may be writeable only by root."
329 while read _line
; do
331 done | ${MAILCMD} -s"Welcome" ${username}
332 info
"Sent welcome message to ($username)."
338 # Reads username of the account from standard input or from a global
339 # variable containing an account line from a file. The username is
340 # required. If this is an interactive session it will prompt in
341 # a loop until a username is entered. If it is batch processing from
342 # a file it will output an error message and return to the caller.
347 # No need to take down user names if this is a configuration saving run.
348 [ -n "$configflag" ] && return
351 if [ -z "$fflag" ]; then
355 _input
="`echo "$fileline" | cut -f1 -d:`"
358 # There *must* be a username, and it must not exist. If
359 # this is an interactive session give the user an
360 # opportunity to retry.
362 if [ -z "$_input" ]; then
363 err
"You must enter a username!"
364 [ -z "$fflag" ] && continue
366 ${PWCMD} usershow
$_input > /dev
/null
2>&1
367 if [ "$?" -eq 0 ]; then
369 [ -z "$fflag" ] && continue
377 # Reads extra information about the user. Can be used both in interactive
378 # and batch (from file) mode.
383 # No need to take down additional user information for a configuration run.
384 [ -n "$configflag" ] && return
386 if [ -z "$fflag" ]; then
387 echo -n "Full name: "
390 _input
="`echo "$fileline" | cut -f7 -d:`"
396 # Get the account's shell. Works in interactive and batch mode. It
397 # accepts either the base name of the shell or the full path.
398 # If an invalid shell is entered it will simply use the default shell.
403 ushell
="$defaultshell"
405 # Make sure the current value of the shell is a valid one
406 if [ -z "$Sflag" ]; then
407 if ! shell_exists
$ushell ; then
408 info
"Using default shell ${defaultshell}."
409 ushell
="$defaultshell"
413 if [ -z "$fflag" ]; then
414 echo -n "Shell ($shells) [`basename $ushell`]: "
417 _input
="`echo "$fileline" | cut -f9 -d:`"
419 if [ -n "$_input" ]; then
420 if [ -n "$Sflag" ]; then
423 _fullpath
=`fullpath_from_shell $_input`
424 if [ -n "$_fullpath" ]; then
427 err
"Invalid shell ($_input) for user $username."
428 info
"Using default shell ${defaultshell}."
429 ushell
="$defaultshell"
436 # Reads the account's home directory. Used both with interactive input
441 if [ -z "$fflag" ]; then
442 echo -n "Home directory [${homeprefix}/${username}]: "
445 _input
="`echo "$fileline" | cut -f8 -d:`"
448 if [ -n "$_input" ]; then
450 # if this is a configuration run, then user input is the home
451 # directory prefix. Otherwise it is understood to
454 [ -z "$configflag" ] && homeprefix
="`dirname $uhome`" || homeprefix
="$uhome"
456 uhome
="${homeprefix}/${username}"
461 # Reads the account's home directory permissions.
464 uhomeperm
=$defaultHomePerm
468 if [ -n "$uhomeperm" ]; then
469 _prompt
="Home directory permissions [${uhomeperm}]: "
471 _prompt
="Home directory permissions (Leave empty for default): "
473 if [ -z "$fflag" ]; then
478 if [ -n "$_input" ]; then
484 # Reads a numeric userid in an interactive or batch session. Automatically
485 # allocates one if it is not specified.
492 if [ -n "$uuid" ]; then
493 uuid
=`get_nextuid $uuid`
494 _prompt
="Uid [$uuid]: "
496 _prompt
="Uid (Leave empty for default): "
498 if [ -z "$fflag" ]; then
502 _input
="`echo "$fileline" | cut -f2 -d:`"
505 [ -n "$_input" ] && uuid
=$_input
506 uuid
=`get_nextuid $uuid`
511 # Reads login class of account. Can be used in interactive or batch mode.
514 uclass
="$defaultclass"
516 _class
=${uclass:-"default"}
518 if [ -z "$fflag" ]; then
519 echo -n "Login class [$_class]: "
522 _input
="`echo "$fileline" | cut -f4 -d:`"
525 [ -n "$_input" ] && uclass
="$_input"
529 # Reads user's login group. Can be used in both interactive and batch
530 # modes. The specified value can be a group name or its numeric id.
531 # This routine leaves the field blank if nothing is provided and
532 # a default login group has not been set. The pw(8) command
533 # will then provide a login group with the same name as the username.
536 ulogingroup
="$defaultLgroup"
539 if [ -z "$fflag" ]; then
540 echo -n "Login group [${ulogingroup:-$username}]: "
543 _input
="`echo "$fileline" | cut -f3 -d:`"
546 # Pw(8) will use the username as login group if it's left empty
547 [ -n "$_input" ] && ulogingroup
="$_input"
551 # Read additional groups for the user. It can be used in both interactive
555 ugroups
="$defaultgroups"
557 _group
=${ulogingroup:-"${username}"}
559 if [ -z "$configflag" ]; then
560 [ -z "$fflag" ] && echo -n "Login group is $_group. Invite $username"
561 [ -z "$fflag" ] && echo -n " into other groups? [$ugroups]: "
563 [ -z "$fflag" ] && echo -n "Enter additional groups [$ugroups]: "
567 [ -n "$_input" ] && ugroups
="$_input"
571 # Read expiry information for the account and also for the password. This
572 # routine is used only from batch processing mode.
575 upwexpire
="`echo "$fileline" | cut -f5 -d:`"
576 uexpire
="`echo "$fileline" | cut -f6 -d:`"
580 # Read the password in batch processing mode. The password field matters
581 # only when the password type is "yes" or "random". If the field is empty and the
582 # password type is "yes", then it assumes the account has an empty passsword
583 # and changes the password type accordingly. If the password type is "random"
584 # and the password field is NOT empty, then it assumes the account will NOT
585 # have a random password and set passwdtype to "yes."
588 # We may temporarily change a password type. Make sure it's changed
589 # back to whatever it was before we process the next account.
591 [ -n "$savedpwtype" ] && {
592 passwdtype
=$savedpwtype
596 # There may be a ':' in the password
597 upass
=${fileline#*:*:*:*:*:*:*:*:*:}
599 if [ -z "$upass" ]; then
602 # if it's empty, assume an empty password
618 # Reads a line of account information from standard input and
619 # adds it to the user database.
624 while read -r fileline
; do
639 ugroups
="$defaultgroups"
648 # Prompts for user information interactively, and commits to
651 input_interactive
() {
677 # The case where group = user is handled elsewhere, so
678 # validate any other groups the user is invited to.
679 until [ "$_logingroup_ok" = yes ]; do
682 if [ -n "$ulogingroup" -a "$username" != "$ulogingroup" ]; then
683 if ! ${PWCMD} show group
$ulogingroup > /dev
/null
2>&1; then
684 echo "Group $ulogingroup does not exist!"
689 until [ "$_groups_ok" = yes ]; do
692 for i
in $ugroups; do
693 if [ "$username" != "$i" ]; then
694 if ! ${PWCMD} show group
$i > /dev
/null
2>&1; then
695 echo "Group $i does not exist!"
708 echo -n "Use password-based authentication? [$_usepass]: "
710 [ -z "$_input" ] && _input
=$_usepass
715 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
717 echo -n "Use an empty password? (yes/no) [$_emptypass]: "
719 [ -n "$_input" ] && _emptypass
=$_input
722 echo -n "Use a random password? (yes/no) [$_random]: "
724 [ -n "$_input" ] && _random
="$_input"
726 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
732 [ -n "$configflag" ] && break
733 trap 'stty echo; exit' 0 1 2 3 15
735 echo -n "Enter password: "
738 echo -n "Enter password again: "
739 IFS
= read -r _passconfirm
742 # if user entered a blank password
743 # explicitly ask again.
744 [ -z "$upass" -a -z "$_passconfirm" ] \
747 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
752 # invalid answer; repeat the loop
756 if [ "$upass" != "$_passconfirm" ]; then
757 echo "Passwords did not match!"
764 # invalid answer; repeat loop
770 _disable
=${disableflag:-"no"}
772 echo -n "Lock out the account after creation? [$_disable]: "
774 [ -z "$_input" ] && _input
=$_disable
779 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
783 # invalid answer; repeat loop
790 # Display the information we have so far and prompt to
793 _disable
=${disableflag:-"no"}
794 [ -z "$configflag" ] && printf "%-10s : %s\n" Username
$username
809 [ -z "$configflag" ] && printf "%-10s : %s\n" "Password" "$_pass"
810 [ -n "$configflag" ] && printf "%-10s : %s\n" "Pass Type" "$passwdtype"
811 [ -z "$configflag" ] && printf "%-10s : %s\n" "Full Name" "$ugecos"
812 [ -z "$configflag" ] && printf "%-10s : %s\n" "Uid" "$uuid"
813 printf "%-10s : %s\n" "Class" "$uclass"
814 printf "%-10s : %s %s\n" "Groups" "${ulogingroup:-$username}" "$ugroups"
815 printf "%-10s : %s\n" "Home" "$uhome"
816 printf "%-10s : %s\n" "Home Mode" "$uhomeperm"
817 printf "%-10s : %s\n" "Shell" "$ushell"
818 printf "%-10s : %s\n" "Locked" "$_disable"
820 echo -n "OK? (yes/no): "
826 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
838 #### END SUBROUTINE DEFINITION ####
840 THISCMD
=`@MEMO_PREFIX@@MEMO_SUB_PREFIX@/bin/basename $0`
841 DEFAULTSHELL
=@PREFIX@
/bin
/sh
842 ADDUSERCONF
="${ADDUSERCONF:-@MEMO_PREFIX@/etc/adduser.conf}"
843 PWCMD
="${PWCMD:-@MEMO_PREFIX@@MEMO_SUB_PREFIX@/sbin/pw}"
844 MAILCMD
="${MAILCMD:-mail}"
845 ETCSHELLS
="${ETCSHELLS:-@MEMO_PREFIX@/etc/shells}"
846 NOHOME
="@MEMO_PREFIX@/var/lib/empty"
848 NOLOGIN_PATH
="@MEMO_PREFIX@@MEMO_SUB_PREFIX@/sbin/nologin"
849 GREPCMD
="@MEMO_PREFIX@@MEMO_SUB_PREFIX@/bin/grep"
850 DATECMD
="@MEMO_PREFIX@/bin/date"
864 udotdir
=@MEMO_PREFIX@@MEMO_SUB_PREFIX@
/share
/skel
868 shells
="`valid_shells`"
870 msgfile
=@MEMO_PREFIX@
/etc
/adduser.msg
880 homeprefix
="@MEMO_PREFIX@/var"
887 defaultshell
="${DEFAULTSHELL}"
890 # Make sure the user running this program is root. This isn't a security
891 # measure as much as it is a useful method of reminding the user to
892 # 'su -' before he/she wastes time entering data that won't be saved.
894 procowner
=${procowner:-`@MEMO_PREFIX@@MEMO_SUB_PREFIX@/bin/id -u`}
895 if [ "$procowner" != "0" ]; then
896 err
'you must be the super-user (uid 0) to use this utility.'
900 # Override from our conf file
901 # Quickly go through the commandline line to see if we should read
902 # from our configuration file. The actual parsing of the commandline
903 # arguments happens after we read in our configuration file (commandline
904 # should override configuration file).
907 if [ "$_i" = "-N" ]; then
912 if [ -n "$readconfig" ]; then
913 # On a long-lived system, the first time this script is run it
914 # will barf upon reading the configuration file for its perl predecessor.
915 if ( .
${ADDUSERCONF} > /dev
/null
2>&1 ); then
916 [ -r ${ADDUSERCONF} ] && .
${ADDUSERCONF} > /dev
/null
2>&1
920 # Process command-line options
945 [ "$2" != "-" ] && infile
="$2"
1002 defaultshell
="`fullpath_from_shell $2`"
1016 # If the -f switch was used, get input from a file. Otherwise,
1017 # this is an interactive session.
1019 if [ -n "$fflag" ]; then
1020 if [ -z "$infile" ]; then
1022 elif [ -n "$infile" ]; then
1023 if [ -r "$infile" ]; then
1024 input_from_file
< $infile
1026 err
"File ($infile) is unreadable or does not exist."
1032 if [ -z "$configflag" ]; then
1033 echo -n "Add another user? (yes/no): "
1035 echo -n "Re-edit the default configuration? (yes/no): "
1039 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
1040 uidstart
=`get_nextuid $uidstart`