]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - adduser/adduser.sh
Revisit the shell special characters issue and settle it once-and-forall.
[pw-darwin.git] / adduser / adduser.sh
index 6c142513b1d2c396ccbee837e4b7f6c98d051120..2b6398af973b3cf26b4273a4edc0123069727e84 100644 (file)
@@ -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=
@@ -525,7 +527,7 @@ get_password() {
 input_from_file() {
        _field=
 
-       while read fileline ; do
+       while read -r fileline ; do
                case "$fileline" in
                \#*|'')
                        return 0
@@ -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
@@ -609,7 +635,7 @@ input_interactive() {
                                        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
@@ -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,7 +771,6 @@ configflag=
 fflag=
 infile=
 disableflag=
-adduserlog="${ADDUSERLOG}"
 readconfig="yes"
 homeprefix="/home"
 randompass=
@@ -881,4 +905,22 @@ if [ -n "$fflag" ]; then
        fi
 else
        input_interactive
+       while : ; do
+               echo -n "Add another user? (yes/no): "
+               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