]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - adduser/adduser.sh
Don't forget the -r on the second password prompt.
[pw-darwin.git] / adduser / adduser.sh
index 6c142513b1d2c396ccbee837e4b7f6c98d051120..096e537bb1710fa3a50f35f095dffca7a491f690 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# Copyright (c) 2002 Michael Telahun Makonnen. All rights reserved.
+# Copyright (c) 2002, 2003 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
@@ -85,6 +83,7 @@ show_usage() {
        echo "  -N              do not read configuration file"
        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"
@@ -147,6 +146,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}
@@ -155,7 +155,6 @@ save_config() {
        echo "udotdir=$udotdir"         >> ${ADDUSERCONF}
        echo "msgfile=$msgfile"         >> ${ADDUSERCONF}
        echo "disableflag=$disableflag" >> ${ADDUSERCONF}
-       echo "adduserlog=$adduserlog"   >> ${ADDUSERCONF}
 }
 
 # add_user
@@ -188,25 +187,31 @@ 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 "$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"'
        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"
@@ -239,9 +244,6 @@ add_user() {
                fi
        fi
 
-       _log=${adduserlog:-no}
-       [ x"$_log" = x"no" ] || (echo "$(${DATECMD} +'%Y/%m/%d %T') $(${PWCMD} 2>/dev/null usershow -n $username)" >> $_log)
-
        _line=
        _owner=
        _perms=
@@ -430,25 +432,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
@@ -525,7 +525,7 @@ get_password() {
 input_from_file() {
        _field=
 
-       while read fileline ; do
+       while read -r fileline ; do
                case "$fileline" in
                \#*|'')
                        return 0
@@ -558,6 +558,8 @@ input_interactive() {
        _random="no"
        _emptypass="no"
        _usepass="yes"
+       _logingroup_ok="no"
+       _groups_ok="no"
        case $passwdtype in
        none)
                _emptypass="yes"
@@ -574,8 +576,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
@@ -605,14 +631,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
@@ -687,7 +713,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"
@@ -715,7 +741,6 @@ input_interactive() {
 THISCMD=`/usr/bin/basename $0`
 DEFAULTSHELL=/bin/sh
 ADDUSERCONF="${ADDUSERCONF:-/etc/adduser.conf}"
-ADDUSERLOG="${ADDUSERLOG:-/var/log/adduser}"
 PWCMD="${PWCMD:-/usr/sbin/pw}"
 MAILCMD="${MAILCMD:-mail}"
 ETCSHELLS="${ETCSHELLS:-/etc/shells}"
@@ -746,13 +771,13 @@ configflag=
 fflag=
 infile=
 disableflag=
-adduserlog="${ADDUSERLOG}"
 readconfig="yes"
 homeprefix="/home"
 randompass=
 fileline=
 savedpwtype=
 defaultclass=
+defaultLgroup=
 defaultgoups=
 defaultshell="${DEFAULTSHELL}"
 
@@ -811,6 +836,10 @@ for _switch ; do
                fflag=yes
                shift; shift
                ;;
+       -g)
+               defaultLgroup="$2"
+               shift; shift
+               ;;
        -G)
                defaultgroups="$2"
                shift; shift
@@ -881,4 +910,26 @@ if [ -n "$fflag" ]; then
        fi
 else
        input_interactive
+       while : ; do
+               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])
+                       uidstart=`get_nextuid $uidstart`
+                       input_interactive
+                       continue
+                       ;;
+               [Nn][Oo]|[Nn])
+                       echo "Goodbye!"
+                       ;;
+               *)
+                       continue
+                       ;;
+               esac
+               break
+       done
 fi