]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - adduser/adduser.sh
This manual page will not first appear in 4.10 as RELENG_4 has a different
[pw-darwin.git] / adduser / adduser.sh
index 96049bd844718010fffb9cedcf6dc4d197ff03a2..c04463d389d0fc4ab748fcf091f3f30f74fe472b 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# Copyright (c) 2002 Michael Telahun Makonnen. All rights reserved.
+# Copyright (c) 2002-2004 Michael Telahun Makonnen. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
@@ -10,8 +10,6 @@
 # 2. Redistributions in binary form must reproduce the above copyright
 #    notice, this list of conditions and the following disclaimer in the
 #    documentation and/or other materials provided with the distribution.
-# 3. The name of the author may not be used to endorse or promote products
-#    derived from this software without specific prior written permission.
 #
 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +22,7 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #
-#       Email: Mike Makonnen <mtm@identd.net>
+#       Email: Mike Makonnen <mtm@FreeBSD.Org>
 #
 # $FreeBSD$
 #
@@ -79,12 +77,15 @@ show_usage() {
        echo "usage: ${THISCMD} [options]"
        echo "  options may include:"
        echo "  -C              save to the configuration file only"
+       echo "  -D              do not attempt to create the home directory"
        echo "  -E              disable this account after creation"
        echo "  -G              additional groups to add accounts to"
        echo "  -L              login class of the user"
        echo "  -N              do not read configuration file"
+       echo "  -S              a nonexistent shell is not an error"
        echo "  -d              home directory"
        echo "  -f              file from which input will be received"
+       echo "  -g              default login group"
        echo "  -h              display this usage message"
        echo "  -k              path to skeleton home directory"
        echo "  -m              user welcome message file"
@@ -111,6 +112,9 @@ valid_shells() {
                        ;;
                esac
        done
+
+       # /usr/sbin/nologin is a special case
+       [ -x "${NOLOGIN_PATH}" ] && echo -n " ${NOLOGIN}"
 }
 
 # fullpath_from_shell shell
@@ -134,9 +138,41 @@ fullpath_from_shell() {
                        ;;
                esac
        done
+
+       # /usr/sbin/nologin is a special case
+       if [ "$_shell" = "${NOLOGIN}" ]; then
+               echo ${NOLOGIN_PATH}
+               return 0;
+       fi
+
        return 1
 }
 
+# shell_exists shell
+#      If the given shell is listed in ${ETCSHELLS} or it is
+#      the nologin shell this function will return 0.
+#      Otherwise, it will return 1. If shell is valid but
+#      the path is invalid or it is not executable it
+#      will emit an informational message saying so.
+#
+shell_exists()
+{
+       _sh="$1"
+       _shellchk="${GREPCMD} '^$_sh$' ${ETCSHELLS} > /dev/null 2>&1"
+
+       if ! eval $_shellchk; then
+               # The nologin shell is not listed in /etc/shells.
+               if [ "$_sh" != "${NOLOGIN_PATH}" ]; then
+                       err "Invalid shell ($_sh) for user $username."
+                       return 1
+               fi
+       fi
+       ! [ -x "$_sh" ] &&
+           info "The shell ($_sh) does not exist or is not executable."
+
+       return 0
+}
+
 # save_config
 #      Save some variables to a configuration file.
 #      Note: not all script variables are saved, only those that
@@ -147,6 +183,7 @@ save_config() {
        echo "# NOTE: only *some* variables are saved." >> ${ADDUSERCONF}
        echo "# Last Modified on `${DATECMD}`."         >> ${ADDUSERCONF}
        echo ''                         >> ${ADDUSERCONF}
+       echo "defaultLgroup=$ulogingroup" >> ${ADDUSERCONF}
        echo "defaultclass=$uclass"     >> ${ADDUSERCONF}
        echo "defaultgroups=$ugroups"   >> ${ADDUSERCONF}
        echo "passwdtype=$passwdtype"   >> ${ADDUSERCONF}
@@ -187,25 +224,41 @@ add_user() {
        _passwdmethod=
 
        _name="-n '$username'"
-       [ -n "$uuid" ] && _uid="-u '$uuid'"
-       [ -n "$ulogingroup" ] && _group="-g '$ulogingroup'"
-       [ -n "$ugroups" ] && _grouplist="-G '$ugroups'"
-       [ -n "$ushell" ] && _shell="-s '$ushell'"
-       [ -n "$uhome" ] && _home="-m -d '$uhome'"
-       [ -n "$uclass" ] && _class="-L '$uclass'"
-       [ -n "$ugecos" ] && _comment="-c '$ugecos'"
-       [ -n "$udotdir" ] && _dotdir="-k '$udotdir'"
-       [ -n "$uexpire" ] && _expire="-e '$uexpire'"
-       [ -n "$upwexpire" ] && _pwexpire="-p '$upwexpire'"
+       [ -n "$uuid" ] && _uid='-u "$uuid"'
+       [ -n "$ulogingroup" ] && _group='-g "$ulogingroup"'
+       [ -n "$ugroups" ] && _grouplist='-G "$ugroups"'
+       [ -n "$ushell" ] && _shell='-s "$ushell"'
+       [ -n "$uclass" ] && _class='-L "$uclass"'
+       [ -n "$ugecos" ] && _comment='-c "$ugecos"'
+       [ -n "$udotdir" ] && _dotdir='-k "$udotdir"'
+       [ -n "$uexpire" ] && _expire='-e "$uexpire"'
+       [ -n "$upwexpire" ] && _pwexpire='-p "$upwexpire"'
+       if [ -z "$Dflag" -a -n "$uhome" ]; then
+               # The /nonexistent home directory is special. It
+               # means the user has no home directory.
+               if [ "$uhome" = "$NOHOME" ]; then
+                       _home='-d "$uhome"'
+               else
+                       _home='-m -d "$uhome"'
+               fi
+       elif [ -n "$Dflag" -a -n "$uhome" ]; then
+               _home='-d "$uhome"'
+       fi
        case $passwdtype in
        no)
                _passwdmethod="-w no"
                _passwd="-h -"
                ;;
        yes)
+               # Note on processing the password: The outer double quotes
+               # make literal everything except ` and \ and $.
+               # The outer single quotes make literal ` and $.
+               # We can ensure the \ isn't treated specially by specifying
+               # the -r switch to the read command used to obtain the input.
+               #
                _passwdmethod="-w yes"
                _passwd="-h 0"
-               _upasswd="echo '$upass' |"
+               _upasswd='echo "$upass" |'
                ;;
        none)
                _passwdmethod="-w none"
@@ -328,11 +381,12 @@ get_shell() {
        ushell="$defaultshell"
 
        # Make sure the current value of the shell is a valid one
-       _shellchk="${GREPCMD} '^$ushell$' ${ETCSHELLS} > /dev/null 2>&1"
-       eval $_shellchk || {
-               err "Invalid shell ($ushell). Using default shell ${defaultshell}."
-               ushell="$defaultshell"
-       }
+       if [ -z "$Sflag" ]; then
+               if ! shell_exists $ushell ; then
+                       info "Using default shell ${defaultshell}."
+                       ushell="$defaultshell"
+               fi
+       fi
 
        if [ -z "$fflag" ]; then
                echo -n "Shell ($shells) [`basename $ushell`]: "
@@ -341,12 +395,17 @@ get_shell() {
                _input="`echo "$fileline" | cut -f9 -d:`"
        fi
        if [ -n "$_input" ]; then
-               _fullpath=`fullpath_from_shell $_input`
-               if [ -n "$_fullpath" ]; then
-                       ushell="$_fullpath"
+               if [ -n "$Sflag" ]; then
+                       ushell="$_input"
                else
-                       err "Invalid shell selection. Using default shell ${defaultshell}."
-                       ushell="$defaultshell"
+                       _fullpath=`fullpath_from_shell $_input`
+                       if [ -n "$_fullpath" ]; then
+                               ushell="$_fullpath"
+                       else
+                               err "Invalid shell ($_input) for user $username."
+                               info "Using default shell ${defaultshell}."
+                               ushell="$defaultshell"
+                       fi
                fi
        fi
 }
@@ -426,25 +485,23 @@ get_class() {
 # get_logingroup
 #      Reads user's login group. Can be used in both interactive and batch
 #      modes. The specified value can be a group name or its numeric id.
-#      This routine leaves the field blank if nothing is provided. The pw(8)
-#      command will then provide a login group with the same name as the username.
+#      This routine leaves the field blank if nothing is provided and
+#      a default login group has not been set. The pw(8) command
+#      will then provide a login group with the same name as the username.
 #
 get_logingroup() {
-       ulogingroup=
+       ulogingroup="$defaultLgroup"
        _input=
 
-       # No need to take down a login group for a configuration saving run.
-       [ -n "$configflag" ] && return
-
        if [ -z "$fflag" ]; then
-               echo -n "Login group [$username]: "
+               echo -n "Login group [${ulogingroup:-$username}]: "
                read _input
        else
                _input="`echo "$fileline" | cut -f3 -d:`"
        fi
 
        # Pw(8) will use the username as login group if it's left empty
-       [ -n "$_input" ] && ulogingroup="$_input" || ulogingroup=
+       [ -n "$_input" ] && ulogingroup="$_input"
 }
 
 # get_groups
@@ -521,7 +578,7 @@ get_password() {
 input_from_file() {
        _field=
 
-       while read fileline ; do
+       while read -r fileline ; do
                case "$fileline" in
                \#*|'')
                        return 0
@@ -554,6 +611,8 @@ input_interactive() {
        _random="no"
        _emptypass="no"
        _usepass="yes"
+       _logingroup_ok="no"
+       _groups_ok="no"
        case $passwdtype in
        none)
                _emptypass="yes"
@@ -570,8 +629,32 @@ input_interactive() {
        get_user
        get_gecos
        get_uid
-       get_logingroup
-       get_groups
+
+       # The case where group = user is handled elsewhere, so
+       # validate any other groups the user is invited to.
+       until [ "$_logingroup_ok" = yes ]; do
+               get_logingroup
+               _logingroup_ok=yes
+               if [ -n "$ulogingroup" -a "$username" != "$ulogingroup" ]; then
+                       if ! ${PWCMD} show group $ulogingroup > /dev/null 2>&1; then
+                               echo "Group $ulogingroup does not exist!"
+                               _logingroup_ok=no
+                       fi
+               fi
+       done
+       until [ "$_groups_ok" = yes ]; do
+               get_groups
+               _groups_ok=yes
+               for i in $ugroups; do
+                       if [ "$username" != "$i" ]; then
+                               if ! ${PWCMD} show group $i > /dev/null 2>&1; then
+                                       echo "Group $i does not exist!"
+                                       _groups_ok=no
+                               fi
+                       fi
+               done
+       done
+
        get_class
        get_shell
        get_homedir
@@ -601,14 +684,14 @@ input_interactive() {
                                                ;;
                                        esac
                                        passwdtype="yes"
-                                       [ -n "$configrun" ] && break
+                                       [ -n "$configflag" ] && break
                                        trap 'stty echo; exit' 0 1 2 3 15
                                        stty -echo
                                        echo -n "Enter password: "
-                                       read upass
+                                       read -r upass
                                        echo''
                                        echo -n "Enter password again: "
-                                       read _passconfirm
+                                       read -r _passconfirm
                                        echo ''
                                        stty echo
                                        # if user entered a blank password
@@ -683,7 +766,7 @@ input_interactive() {
        [ -z "$configflag" ] && printf "%-10s : %s\n" "Full Name" "$ugecos"
        [ -z "$configflag" ] && printf "%-10s : %s\n" "Uid" "$uuid"
        printf "%-10s : %s\n" "Class" "$uclass"
-       [ -z "$configflag" ] && printf "%-10s : %s %s\n" "Groups" "${ulogingroup:-$username}" "$ugroups"
+       printf "%-10s : %s %s\n" "Groups" "${ulogingroup:-$username}" "$ugroups"
        printf "%-10s : %s\n" "Home" "$uhome"
        printf "%-10s : %s\n" "Shell" "$ushell"
        printf "%-10s : %s\n" "Locked" "$_disable"
@@ -714,6 +797,9 @@ ADDUSERCONF="${ADDUSERCONF:-/etc/adduser.conf}"
 PWCMD="${PWCMD:-/usr/sbin/pw}"
 MAILCMD="${MAILCMD:-mail}"
 ETCSHELLS="${ETCSHELLS:-/etc/shells}"
+NOHOME="/nonexistent"
+NOLOGIN="nologin"
+NOLOGIN_PATH="/usr/sbin/nologin"
 GREPCMD="/usr/bin/grep"
 DATECMD="/bin/date"
 
@@ -741,13 +827,16 @@ configflag=
 fflag=
 infile=
 disableflag=
+Dflag=
+Sflag=
 readconfig="yes"
 homeprefix="/home"
 randompass=
 fileline=
 savedpwtype=
 defaultclass=
-defaultgoups=
+defaultLgroup=
+defaultgroups=
 defaultshell="${DEFAULTSHELL}"
 
 # Make sure the user running this program is root. This isn't a security
@@ -792,6 +881,10 @@ for _switch ; do
                configflag=yes
                shift
                ;;
+       -D)
+               Dflag=yes
+               shift
+               ;;
        -E)
                disableflag=yes
                shift
@@ -805,6 +898,10 @@ for _switch ; do
                fflag=yes
                shift; shift
                ;;
+       -g)
+               defaultLgroup="$2"
+               shift; shift
+               ;;
        -G)
                defaultgroups="$2"
                shift; shift
@@ -853,6 +950,10 @@ for _switch ; do
                defaultshell="`fullpath_from_shell $2`"
                shift; shift
                ;;
+       -S)
+               Sflag=yes
+               shift
+               ;;
        -u)
                uidstart=$2
                shift; shift
@@ -876,7 +977,11 @@ if [ -n "$fflag" ]; then
 else
        input_interactive
        while : ; do
-               echo -n "Add another user? (yes/no): "
+               if [ -z "$configflag" ]; then
+                       echo -n "Add another user? (yes/no): "
+               else
+                       echo -n "Re-edit the default configuration? (yes/no): "
+               fi
                read _input
                case $_input in
                [Yy][Ee][Ss]|[Yy][Ee]|[Yy])