]> 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 96049bd844718010fffb9cedcf6dc4d197ff03a2..2b6398af973b3cf26b4273a4edc0123069727e84 100644 (file)
@@ -187,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"
@@ -521,7 +527,7 @@ get_password() {
 input_from_file() {
        _field=
 
-       while read fileline ; do
+       while read -r fileline ; do
                case "$fileline" in
                \#*|'')
                        return 0
@@ -570,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,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