]> 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 53d9b86c9022f0714ccb7986d20bf13423a740d2..2b6398af973b3cf26b4273a4edc0123069727e84 100644 (file)
@@ -145,7 +145,7 @@ fullpath_from_shell() {
 save_config() {
        echo "# Configuration file for adduser(8)."     >  ${ADDUSERCONF}
        echo "# NOTE: only *some* variables are saved." >> ${ADDUSERCONF}
-       echo "# Last Modified on `date`."                     >> ${ADDUSERCONF}
+       echo "# Last Modified on `${DATECMD}`."         >> ${ADDUSERCONF}
        echo ''                         >> ${ADDUSERCONF}
        echo "defaultclass=$uclass"     >> ${ADDUSERCONF}
        echo "defaultgroups=$ugroups"   >> ${ADDUSERCONF}
@@ -186,26 +186,32 @@ add_user() {
        _upasswd=
        _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'"
+       _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"'
        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,7 +334,7 @@ get_shell() {
        ushell="$defaultshell"
 
        # Make sure the current value of the shell is a valid one
-       _shellchk="grep '^$ushell$' ${ETCSHELLS} > /dev/null 2>&1"
+       _shellchk="${GREPCMD} '^$ushell$' ${ETCSHELLS} > /dev/null 2>&1"
        eval $_shellchk || {
                err "Invalid shell ($ushell). Using default shell ${defaultshell}."
                ushell="$defaultshell"
@@ -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
@@ -601,10 +631,11 @@ input_interactive() {
                                                ;;
                                        esac
                                        passwdtype="yes"
+                                       [ -n "$configrun" ] && 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
@@ -677,7 +708,8 @@ input_interactive() {
                _pass='<random>'
                ;;
        esac
-       printf "%-10s : %s\n" "Password" "$_pass"
+       [ -z "$configflag" ] && printf "%-10s : %s\n" "Password" "$_pass"
+       [ -n "$configflag" ] && printf "%-10s : %s\n" "Pass Type" "$passwdtype"
        [ -z "$configflag" ] && printf "%-10s : %s\n" "Full Name" "$ugecos"
        [ -z "$configflag" ] && printf "%-10s : %s\n" "Uid" "$uuid"
        printf "%-10s : %s\n" "Class" "$uclass"
@@ -712,6 +744,8 @@ ADDUSERCONF="${ADDUSERCONF:-/etc/adduser.conf}"
 PWCMD="${PWCMD:-/usr/sbin/pw}"
 MAILCMD="${MAILCMD:-mail}"
 ETCSHELLS="${ETCSHELLS:-/etc/shells}"
+GREPCMD="/usr/bin/grep"
+DATECMD="/bin/date"
 
 # Set default values
 #
@@ -871,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