]>
git.cameronkatri.com Git - pw-darwin.git/blob - adduser/adduser.sh
3 # Copyright (c) 2002-2004 Michael Telahun Makonnen. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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.
25 # Email: Mike Makonnen <mtm@FreeBSD.Org>
31 # Display $msg on stderr, unless we're being quiet.
34 if [ -z "$quietflag" ]; then
35 echo 1>&2 ${THISCMD}: ERROR
: $
*
40 # Display $msg on stdout, unless we're being quiet.
43 if [ -z "$quietflag" ]; then
44 echo ${THISCMD}: INFO
: $
*
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
58 if [ -z "$_uid" ]; then
59 _nextuid
="`${PWCMD} usernext | cut -f1 -d:`"
62 ${PWCMD} usershow
$_uid > /dev
/null
2>&1
63 if [ ! "$?" -eq 0 ]; then
74 # Display usage information for this utility.
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 " -M file permission for home directory"
85 echo " -N do not read configuration file"
86 echo " -S a nonexistent shell is not an error"
87 echo " -d home directory"
88 echo " -f file from which input will be received"
89 echo " -g default login group"
90 echo " -h display this usage message"
91 echo " -k path to skeleton home directory"
92 echo " -m user welcome message file"
93 echo " -q absolute minimal user feedback"
95 echo " -u uid to start at"
96 echo " -w password type: no, none, yes or random"
100 # Outputs a list of valid shells from /etc/shells. Only the
101 # basename of the shell is output.
106 while read _path _junk
; do
111 echo -n "${_prefix}`basename $_path`"
117 # /usr/sbin/nologin is a special case
118 [ -x "${NOLOGIN_PATH}" ] && echo -n " ${NOLOGIN}"
121 # fullpath_from_shell shell
122 # Given $shell, which is either the full path to a shell or
123 # the basename component of a valid shell, get the
124 # full path to the shell from the /etc/shells file.
126 fullpath_from_shell
() {
128 [ -z "$_shell" ] && return 1
130 # /usr/sbin/nologin is a special case; it needs to be handled
131 # before the cat | while loop, since a 'return' from within
132 # a subshell will not terminate the function's execution, and
133 # the path to the nologin shell might be printed out twice.
135 if [ "$_shell" = "${NOLOGIN}" -o \
136 "$_shell" = "${NOLOGIN_PATH}" ]; then
142 while read _path _junk
; do
147 if [ "$_path" = "$_shell" -o \
148 "`basename $_path`" = "$_shell" ]; then
160 # If the given shell is listed in ${ETCSHELLS} or it is
161 # the nologin shell this function will return 0.
162 # Otherwise, it will return 1. If shell is valid but
163 # the path is invalid or it is not executable it
164 # will emit an informational message saying so.
169 _shellchk
="${GREPCMD} '^$_sh$' ${ETCSHELLS} > /dev/null 2>&1"
171 if ! eval $_shellchk; then
172 # The nologin shell is not listed in /etc/shells.
173 if [ "$_sh" != "${NOLOGIN_PATH}" ]; then
174 err
"Invalid shell ($_sh) for user $username."
179 info
"The shell ($_sh) does not exist or is not executable."
185 # Save some variables to a configuration file.
186 # Note: not all script variables are saved, only those that
187 # it makes sense to save.
190 echo "# Configuration file for adduser(8)." > ${ADDUSERCONF}
191 echo "# NOTE: only *some* variables are saved." >> ${ADDUSERCONF}
192 echo "# Last Modified on `${DATECMD}`." >> ${ADDUSERCONF}
193 echo '' >> ${ADDUSERCONF}
194 echo "defaultHomePerm=$uhomeperm" >> ${ADDUSERCONF}
195 echo "defaultLgroup=$ulogingroup" >> ${ADDUSERCONF}
196 echo "defaultclass=$uclass" >> ${ADDUSERCONF}
197 echo "defaultgroups=$ugroups" >> ${ADDUSERCONF}
198 echo "passwdtype=$passwdtype" >> ${ADDUSERCONF}
199 echo "homeprefix=$homeprefix" >> ${ADDUSERCONF}
200 echo "defaultshell=$ushell" >> ${ADDUSERCONF}
201 echo "udotdir=$udotdir" >> ${ADDUSERCONF}
202 echo "msgfile=$msgfile" >> ${ADDUSERCONF}
203 echo "disableflag=$disableflag" >> ${ADDUSERCONF}
204 echo "uidstart=$uidstart" >> ${ADDUSERCONF}
208 # Add a user to the user database. If the user chose to send a welcome
209 # message or lock the account, do so.
213 # Is this a configuration run? If so, don't modify user database.
215 if [ -n "$configflag" ]; then
236 _name
="-n '$username'"
237 [ -n "$uuid" ] && _uid
='-u "$uuid"'
238 [ -n "$ulogingroup" ] && _group
='-g "$ulogingroup"'
239 [ -n "$ugroups" ] && _grouplist
='-G "$ugroups"'
240 [ -n "$ushell" ] && _shell
='-s "$ushell"'
241 [ -n "$uclass" ] && _class
='-L "$uclass"'
242 [ -n "$ugecos" ] && _comment
='-c "$ugecos"'
243 [ -n "$udotdir" ] && _dotdir
='-k "$udotdir"'
244 [ -n "$uexpire" ] && _expire
='-e "$uexpire"'
245 [ -n "$upwexpire" ] && _pwexpire
='-p "$upwexpire"'
246 if [ -z "$Dflag" -a -n "$uhome" ]; then
247 # The /nonexistent home directory is special. It
248 # means the user has no home directory.
249 if [ "$uhome" = "$NOHOME" ]; then
252 # Use home directory permissions if specified
253 if [ -n "$uhomeperm" ]; then
254 _home
='-m -d "$uhome" -M "$uhomeperm"'
256 _home
='-m -d "$uhome"'
259 elif [ -n "$Dflag" -a -n "$uhome" ]; then
264 _passwdmethod
="-w no"
268 # Note on processing the password: The outer double quotes
269 # make literal everything except ` and \ and $.
270 # The outer single quotes make literal ` and $.
271 # We can ensure the \ isn't treated specially by specifying
272 # the -r switch to the read command used to obtain the input.
274 _passwdmethod
="-w yes"
276 _upasswd
='echo "$upass" |'
279 _passwdmethod
="-w none"
282 _passwdmethod
="-w random"
286 _pwcmd
="$_upasswd ${PWCMD} useradd $_uid $_name $_group $_grouplist $_comment"
287 _pwcmd
="$_pwcmd $_shell $_class $_home $_dotdir $_passwdmethod $_passwd"
288 _pwcmd
="$_pwcmd $_expire $_pwexpire"
290 if ! _output
=`eval $_pwcmd` ; then
291 err
"There was an error adding user ($username)."
294 info
"Successfully added ($username) to the user database."
295 if [ "random" = "$passwdtype" ]; then
296 randompass
="$_output"
297 info
"Password for ($username) is: $randompass"
301 if [ -n "$disableflag" ]; then
302 if ${PWCMD} lock
$username ; then
303 info
"Account ($username) is locked."
305 info
"Account ($username) could NOT be locked."
312 if [ -n "$msgflag" ]; then
313 [ -r "$msgfile" ] && {
314 # We're evaluating the contents of an external file.
315 # Let's not open ourselves up for attack. _perms will
316 # be empty if it's writeable only by the owner. _owner
317 # will *NOT* be empty if the file is owned by root.
319 _dir
="`dirname $msgfile`"
320 _file
="`basename $msgfile`"
321 _perms
=`/usr/bin/find $_dir -name $_file -perm +07022 -prune`
322 _owner
=`/usr/bin/find $_dir -name $_file -user 0 -prune`
323 if [ -z "$_owner" -o -n "$_perms" ]; then
324 err
"The message file ($msgfile) may be writeable only by root."
328 while read _line
; do
330 done | ${MAILCMD} -s"Welcome" ${username}
331 info
"Sent welcome message to ($username)."
337 # Reads username of the account from standard input or from a global
338 # variable containing an account line from a file. The username is
339 # required. If this is an interactive session it will prompt in
340 # a loop until a username is entered. If it is batch processing from
341 # a file it will output an error message and return to the caller.
346 # No need to take down user names if this is a configuration saving run.
347 [ -n "$configflag" ] && return
350 if [ -z "$fflag" ]; then
354 _input
="`echo "$fileline" | cut -f1 -d:`"
357 # There *must* be a username, and it must not exist. If
358 # this is an interactive session give the user an
359 # opportunity to retry.
361 if [ -z "$_input" ]; then
362 err
"You must enter a username!"
363 [ -z "$fflag" ] && continue
365 ${PWCMD} usershow
$_input > /dev
/null
2>&1
366 if [ "$?" -eq 0 ]; then
368 [ -z "$fflag" ] && continue
376 # Reads extra information about the user. Can be used both in interactive
377 # and batch (from file) mode.
382 # No need to take down additional user information for a configuration run.
383 [ -n "$configflag" ] && return
385 if [ -z "$fflag" ]; then
386 echo -n "Full name: "
389 _input
="`echo "$fileline" | cut -f7 -d:`"
395 # Get the account's shell. Works in interactive and batch mode. It
396 # accepts either the base name of the shell or the full path.
397 # If an invalid shell is entered it will simply use the default shell.
402 ushell
="$defaultshell"
404 # Make sure the current value of the shell is a valid one
405 if [ -z "$Sflag" ]; then
406 if ! shell_exists
$ushell ; then
407 info
"Using default shell ${defaultshell}."
408 ushell
="$defaultshell"
412 if [ -z "$fflag" ]; then
413 echo -n "Shell ($shells) [`basename $ushell`]: "
416 _input
="`echo "$fileline" | cut -f9 -d:`"
418 if [ -n "$_input" ]; then
419 if [ -n "$Sflag" ]; then
422 _fullpath
=`fullpath_from_shell $_input`
423 if [ -n "$_fullpath" ]; then
426 err
"Invalid shell ($_input) for user $username."
427 info
"Using default shell ${defaultshell}."
428 ushell
="$defaultshell"
435 # Reads the account's home directory. Used both with interactive input
440 if [ -z "$fflag" ]; then
441 echo -n "Home directory [${homeprefix}/${username}]: "
444 _input
="`echo "$fileline" | cut -f8 -d:`"
447 if [ -n "$_input" ]; then
449 # if this is a configuration run, then user input is the home
450 # directory prefix. Otherwise it is understood to
453 [ -z "$configflag" ] && homeprefix
="`dirname $uhome`" || homeprefix
="$uhome"
455 uhome
="${homeprefix}/${username}"
460 # Reads the account's home directory permissions.
463 uhomeperm
=$defaultHomePerm
467 if [ -n "$uhomeperm" ]; then
468 _prompt
="Home directory permissions [${uhomeperm}]: "
470 _prompt
="Home directory permissions (Leave empty for default): "
472 if [ -z "$fflag" ]; then
477 if [ -n "$_input" ]; then
483 # Reads a numeric userid in an interactive or batch session. Automatically
484 # allocates one if it is not specified.
491 if [ -n "$uuid" ]; then
492 _prompt
="Uid [$uuid]: "
494 _prompt
="Uid (Leave empty for default): "
496 if [ -z "$fflag" ]; then
500 _input
="`echo "$fileline" | cut -f2 -d:`"
503 [ -n "$_input" ] && uuid
=$_input
504 uuid
=`get_nextuid $uuid`
509 # Reads login class of account. Can be used in interactive or batch mode.
512 uclass
="$defaultclass"
514 _class
=${uclass:-"default"}
516 if [ -z "$fflag" ]; then
517 echo -n "Login class [$_class]: "
520 _input
="`echo "$fileline" | cut -f4 -d:`"
523 [ -n "$_input" ] && uclass
="$_input"
527 # Reads user's login group. Can be used in both interactive and batch
528 # modes. The specified value can be a group name or its numeric id.
529 # This routine leaves the field blank if nothing is provided and
530 # a default login group has not been set. The pw(8) command
531 # will then provide a login group with the same name as the username.
534 ulogingroup
="$defaultLgroup"
537 if [ -z "$fflag" ]; then
538 echo -n "Login group [${ulogingroup:-$username}]: "
541 _input
="`echo "$fileline" | cut -f3 -d:`"
544 # Pw(8) will use the username as login group if it's left empty
545 [ -n "$_input" ] && ulogingroup
="$_input"
549 # Read additional groups for the user. It can be used in both interactive
553 ugroups
="$defaultgroups"
555 _group
=${ulogingroup:-"${username}"}
557 if [ -z "$configflag" ]; then
558 [ -z "$fflag" ] && echo -n "Login group is $_group. Invite $username"
559 [ -z "$fflag" ] && echo -n " into other groups? [$ugroups]: "
561 [ -z "$fflag" ] && echo -n "Enter additional groups [$ugroups]: "
565 [ -n "$_input" ] && ugroups
="$_input"
569 # Read expiry information for the account and also for the password. This
570 # routine is used only from batch processing mode.
573 upwexpire
="`echo "$fileline" | cut -f5 -d:`"
574 uexpire
="`echo "$fileline" | cut -f6 -d:`"
578 # Read the password in batch processing mode. The password field matters
579 # only when the password type is "yes" or "random". If the field is empty and the
580 # password type is "yes", then it assumes the account has an empty passsword
581 # and changes the password type accordingly. If the password type is "random"
582 # and the password field is NOT empty, then it assumes the account will NOT
583 # have a random password and set passwdtype to "yes."
586 # We may temporarily change a password type. Make sure it's changed
587 # back to whatever it was before we process the next account.
589 [ -n "$savedpwtype" ] && {
590 passwdtype
=$savedpwtype
594 # There may be a ':' in the password
595 upass
=${fileline#*:*:*:*:*:*:*:*:*:}
597 if [ -z "$upass" ]; then
600 # if it's empty, assume an empty password
616 # Reads a line of account information from standard input and
617 # adds it to the user database.
622 while read -r fileline
; do
637 ugroups
="$defaultgroups"
646 # Prompts for user information interactively, and commits to
649 input_interactive
() {
676 # The case where group = user is handled elsewhere, so
677 # validate any other groups the user is invited to.
678 until [ "$_logingroup_ok" = yes ]; do
681 if [ -n "$ulogingroup" -a "$username" != "$ulogingroup" ]; then
682 if ! ${PWCMD} show group
$ulogingroup > /dev
/null
2>&1; then
683 echo "Group $ulogingroup does not exist!"
688 until [ "$_groups_ok" = yes ]; do
691 for i
in $ugroups; do
692 if [ "$username" != "$i" ]; then
693 if ! ${PWCMD} show group
$i > /dev
/null
2>&1; then
694 echo "Group $i does not exist!"
707 echo -n "Use password-based authentication? [$_usepass]: "
709 [ -z "$_input" ] && _input
=$_usepass
714 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
716 echo -n "Use an empty password? (yes/no) [$_emptypass]: "
718 [ -n "$_input" ] && _emptypass
=$_input
721 echo -n "Use a random password? (yes/no) [$_random]: "
723 [ -n "$_input" ] && _random
="$_input"
725 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
731 [ -n "$configflag" ] && break
732 trap 'stty echo; exit' 0 1 2 3 15
734 echo -n "Enter password: "
737 echo -n "Enter password again: "
741 # if user entered a blank password
742 # explicitly ask again.
743 [ -z "$upass" -a -z "$_passconfirm" ] \
746 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
751 # invalid answer; repeat the loop
755 if [ "$upass" != "$_passconfirm" ]; then
756 echo "Passwords did not match!"
763 # invalid answer; repeat loop
769 _disable
=${disableflag:-"no"}
771 echo -n "Lock out the account after creation? [$_disable]: "
773 [ -z "$_input" ] && _input
=$_disable
778 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
782 # invalid answer; repeat loop
789 # Display the information we have so far and prompt to
792 _disable
=${disableflag:-"no"}
793 [ -z "$configflag" ] && printf "%-10s : %s\n" Username
$username
808 [ -z "$configflag" ] && printf "%-10s : %s\n" "Password" "$_pass"
809 [ -n "$configflag" ] && printf "%-10s : %s\n" "Pass Type" "$passwdtype"
810 [ -z "$configflag" ] && printf "%-10s : %s\n" "Full Name" "$ugecos"
811 [ -z "$configflag" ] && printf "%-10s : %s\n" "Uid" "$uuid"
812 printf "%-10s : %s\n" "Class" "$uclass"
813 printf "%-10s : %s %s\n" "Groups" "${ulogingroup:-$username}" "$ugroups"
814 printf "%-10s : %s\n" "Home" "$uhome"
815 printf "%-10s : %s\n" "Home Mode" "$uhomeperm"
816 printf "%-10s : %s\n" "Shell" "$ushell"
817 printf "%-10s : %s\n" "Locked" "$_disable"
819 echo -n "OK? (yes/no): "
825 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
837 #### END SUBROUTINE DEFINITION ####
839 THISCMD
=`/usr/bin/basename $0`
841 ADDUSERCONF
="${ADDUSERCONF:-/etc/adduser.conf}"
842 PWCMD
="${PWCMD:-/usr/sbin/pw}"
843 MAILCMD
="${MAILCMD:-mail}"
844 ETCSHELLS
="${ETCSHELLS:-/etc/shells}"
845 NOHOME
="/nonexistent"
847 NOLOGIN_PATH
="/usr/sbin/nologin"
848 GREPCMD
="/usr/bin/grep"
863 udotdir
=/usr
/share
/skel
867 shells
="`valid_shells`"
869 msgfile
=/etc
/adduser.msg
886 defaultshell
="${DEFAULTSHELL}"
889 # Make sure the user running this program is root. This isn't a security
890 # measure as much as it is a usefull method of reminding the user to
891 # 'su -' before he/she wastes time entering data that won't be saved.
893 procowner
=${procowner:-`/usr/bin/id -u`}
894 if [ "$procowner" != "0" ]; then
895 err
'you must be the super-user (uid 0) to use this utility.'
899 # Overide from our conf file
900 # Quickly go through the commandline line to see if we should read
901 # from our configuration file. The actual parsing of the commandline
902 # arguments happens after we read in our configuration file (commandline
903 # should override configuration file).
906 if [ "$_i" = "-N" ]; then
911 if [ -n "$readconfig" ]; then
912 # On a long-lived system, the first time this script is run it
913 # will barf upon reading the configuration file for its perl predecessor.
914 if ( .
${ADDUSERCONF} > /dev
/null
2>&1 ); then
915 [ -r ${ADDUSERCONF} ] && .
${ADDUSERCONF} > /dev
/null
2>&1
919 # Proccess command-line options
944 [ "$2" != "-" ] && infile
="$2"
1001 defaultshell
="`fullpath_from_shell $2`"
1015 # If the -f switch was used, get input from a file. Otherwise,
1016 # this is an interactive session.
1018 if [ -n "$fflag" ]; then
1019 if [ -z "$infile" ]; then
1021 elif [ -n "$infile" ]; then
1022 if [ -r "$infile" ]; then
1023 input_from_file
< $infile
1025 err
"File ($infile) is unreadable or does not exist."
1031 if [ -z "$configflag" ]; then
1032 echo -n "Add another user? (yes/no): "
1034 echo -n "Re-edit the default configuration? (yes/no): "
1038 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
1039 uidstart
=`get_nextuid $uidstart`