Fix a bunch of little things from the previous changeset.
authorSteve Youngs <steve@sxemacs.org>
Mon, 10 Mar 2014 03:27:58 +0000 (13:27 +1000)
committerSteve Youngs <steve@sxemacs.org>
Mon, 10 Mar 2014 03:27:58 +0000 (13:27 +1000)
* usr/lib/pkgusr/chgrp: Fix shell quoting.
Better test for GIDs vs names

* usr/lib/pkgusr/chmod: Fix shell quoting.
Fix inf-loop in symbol->octal conversion.

* usr/lib/pkgusr/chown: Fix shell quoting.
Fix logic in user test.
Better tests for UID/GID vs names.

* usr/lib/pkgusr/install: Re-factor getopts cmdline parsing.
(_dirs): Use $pristinecmd instead of $@,
exit if not creating directory.
(_leading_dirs): Removed, not needed.
(_perms): Better test for symbol vs octal.
Call /bin/chmod directly in the hack-o-matic.
(_group): Better test for GID vs name
(_owner): Better test for UID vs name

* usr/lib/pkgusr/mkdir:  Remove spurious `fi'.
Logged 'install cmd suggestion' had incorrect -g option.

* usr/bin/forall_direntries_from (prune_prefixes): Add
/usr/lib/pkgusr.  Non-pkgusrs are not allowed in this directory
anymore.

* usr/bin/grep_all_regular_files_for (prune_prefixes): Ditto.

* etc/pkgusr/bash_profile (SUPPRESSLOCALEDIR): Typo.

Signed-off-by: Steve Youngs <steve@sxemacs.org>
etc/pkgusr/bash_profile
usr/bin/forall_direntries_from
usr/bin/grep_all_regular_files_for
usr/bin/list_suspicious_files
usr/lib/pkgusr/chgrp
usr/lib/pkgusr/chmod
usr/lib/pkgusr/chown
usr/lib/pkgusr/install
usr/lib/pkgusr/mkdir

index d1705ef..5f1a4f8 100644 (file)
@@ -47,7 +47,7 @@ SUPPRESSLOCALEDIR=1
 
 ### export everything
 export LC_ALL PATH LESS LESSCHARSET LESSOPEN TZ PKG_CONFIG_PATH QTDIR
-export SUPPRESSLOCALDIR
+export SUPPRESSLOCALEDIR
 
 # Make prompt reflect that we are a package user.
 export PROMPT_COMMAND='PS1="[pkgusr (\u)] \w> "'
index 0d998f6..2b336e7 100755 (executable)
@@ -49,6 +49,7 @@ fs_to_scan=(/)
 #      /etc/sudoers.d \
 #      /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
 #      /var/log \
+#      /usr/lib/pkgusr \
 #      /usr/share/polkit-1/rules.d \
 #      /var/tmp \
 #       /var/{cache,chroot,run,snmp,spool} \
@@ -57,6 +58,7 @@ fs_to_scan=(/)
 prune_prefixes=(\
        /home \
        /root \
+       /usr/lib/pkgusr \
        /usr/src) #NO TRAILING SLASHES!!!!
 
 ## NOTE: 
index 009cab9..c5e9c71 100755 (executable)
@@ -58,12 +58,18 @@ NOLEAF=
 #      /etc/sudoers.d \
 #      /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
 #      /var/log \
+#      /usr/lib/pkgusr \
 #      /usr/share/polkit-1/rules.d \
 #      /var/tmp \
 #       /var/{cache,chroot,run,snmp,spool} \
 #       /var/lib/{sshd,nfs,spamassassin,pulse}) #NO TRAILING SLASHES!!!!
 
-prune_prefixes=(/home /usr/src /dev /tools) #NO TRAILING SLASHES!!!
+prune_prefixes=(\
+       /home \
+       /usr/lib/pkgusr \
+       /usr/src \
+       /dev \
+       /tools) #NO TRAILING SLASHES!!!
 
 if [ $# -lt 1 -o "$1" = "--help" ]; then
     echo 1>&2 
index d7491ee..0ef1839 100755 (executable)
@@ -51,6 +51,7 @@ prune_prefixes=(/root /{,*/{,*/}}lost+found) #NO TRAILING SLASHES!!!
 #      /etc/sudoers.d \
 #      /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
 #      /var/log \
+#      /usr/lib/pkgusr \
 #      /usr/share/polkit-1/rules.d \
 #      /var/tmp \
 #       /var/{cache,chroot,run,snmp,spool} \
index 3c8158c..530ae9c 100755 (executable)
@@ -23,7 +23,7 @@ fi
 
 if [ $UID == 0 ]; then
     echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
-    echo 1>&2 '***' call "$DAISY_CHAIN $@" directly
+    echo 1>&2 '***' call '"'$DAISY_CHAIN $@'"' directly
     exit 1
 fi
 
@@ -42,7 +42,8 @@ done
 # find out.
 GRP_CHAIN=""
 # name or GID?
-if [ $1 -ge 0 2>/dev/null ]; then
+printf '%d' $1 &>/dev/null
+if [ $? -eq 0 ]; then
     GRP_LIST=$(id -G)
 else
     GRP_LIST=$(id -Gn)
@@ -55,9 +56,9 @@ for g in ${GRP_LIST}; do
 done
 
 if [ -z "$GRP_CHAIN" ]; then
-    echo 1>&2 '***' chgrp "$cmdline"
+    echo 1>&2 '***' chgrp ${cmdline}
 else
-    exec $DAISY_CHAIN "$cmdline" || exit $?
+    exec $DAISY_CHAIN ${cmdline} || exit $?
 fi
 
 exit 0
index 555ca3f..e1aa49e 100755 (executable)
@@ -24,16 +24,13 @@ fi
 
 if [ $UID == 0 ]; then
     echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
-    echo 1>&2 '***' call "$DAISY_CHAIN $@" directly.
+    echo 1>&2 '***' call '"'$DAISY_CHAIN $@'"' directly.
     exit 1
 fi
 
 # Save the original cmdline as we're gonna mess with it
 cmdline="$@"
 
-reportmsg="*** chmod $cmdline"
-report=0
-
 # Remove any options so $1 becomes the perm arg, however save the
 # options for later
 opts=""
@@ -46,11 +43,12 @@ done
 # $1 should now be the perm arg
 
 # Octal or symbolic?
-if [ $1 -ge 0 2>/dev/null ]; then
+printf '%d' $1 &>/dev/null
+if [ $? -eq 0 ]; then
     perm=$1
 else
     touch /tmp/hack-o-matic-4500
-    chmod $1 /tmp/hack-o-matic-4500
+    $DAISY_CHAIN $1 /tmp/hack-o-matic-4500
     perm=$(stat --printf "%a" /tmp/hack-o-matic-4500)
     rm -f /tmp/hack-o-matic-4500
 fi
@@ -59,13 +57,13 @@ fi
 if [ $perm -gt 999 ]; then
     # Chop off the 1st digit (the set{uid,gid,sticky} bit)
     perm=${perm/?/}
-    echo 1>&2 "$reportmsg"
+    echo 1>&2 '***' chmod ${cmdline}
 fi
 
 # kill off $1 and replace it with our maybe sanitised $perm
 shift 1; set -- $perm "$@"
 
-exec $DAISY_CHAIN "$opts $@" || exit $?
+exec $DAISY_CHAIN ${opts} $@ || exit $?
 exit 0
 
 # Local variables:
index f48828a..933c415 100755 (executable)
@@ -51,15 +51,24 @@ grp=${usrgrp/*[.:]/}
 report=0
 
 # Catch the case where USER is somebody else.
-if [[ -n "$usr" && ("$usr" != "$(id -un)" ||
-               ($usr -ge 0 && $usr -ne $(id -u))) ]]; then
-    report=1
+if [ -n "$usr" ]; then
+    printf '%d' $usr &>/dev/null
+    if [ $? -eq 0 ]; then
+       if [ $usr -ne $(id -u) ]; then
+           report=1
+       fi
+    else
+       if [ "$usr" != "$(id -un)" ]; then
+           report=1
+       fi
+    fi
 fi
 
 # Catch the case where GROUP isn't in our groups.
 if [ -n "$grp" -a $report -eq 0 ]; then
     GRP_CHAIN=""
-    if [ $grp -ge 0 2>/dev/null ]; then
+    printf '%d' $grp &>/dev/null
+    if [ $? -eq 0 ]; then
        GRP_LIST=$(id -G)
     else
        GRP_LIST=$(id -Gn)
@@ -77,9 +86,9 @@ if [ -n "$grp" -a $report -eq 0 ]; then
 fi
 
 if [ $report -eq 1 ]; then
-    echo 1>&2 '***' chown "$cmdline"  
+    echo 1>&2 '***' chown ${cmdline}
 else
-    exec $DAISY_CHAIN "$cmdline" || exit $?
+    exec $DAISY_CHAIN ${cmdline} || exit $?
 fi
 
 exit 0
index 1e721af..bca9010 100755 (executable)
@@ -100,9 +100,9 @@ if [ ! -n "$DAISY_CHAIN" ]; then
 fi
 
 ## root has no business installing things here!!
-if [ $UID == 0 ]; then
+if [ $UID -eq 0 ]; then
     echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
-    echo 1>&2 '***' call '"'$DAISY_CHAIN ${pristinecmd[*]'"' directly
+    echo 1>&2 '***' call '"'$DAISY_CHAIN ${pristinecmd[*]}'"' directly
     exit 1
 fi
 
@@ -136,42 +136,17 @@ fi
 #  Only allow the creation of directories that don't already exist.
 _dirs()
 {
-    local myarray=($@)
-    local dir=${myarray[-1]}
+    local dir=${pristinecmd[-1]}
 
     if [ -d ${dir} ]; then
        report
+       exit 0
     else
        cmdopts="$cmdopts -d"
     fi
     return
 }
 
-## Leading directories (install option -D)
-#  The same as for -d, only create if it doesn't exist.  But we need
-#  to do a little more this time because if we decide to NOT create
-#  the directory we will need to alter the command line further...
-#
-#    install -Dm755 myprog /usr/bin/myprog
-#  To
-#    install -m755 myprog /usr/bin
-_leading_dirs()
-{
-    local myarray=($@)
-    local dir=$(dirname ${myarray[-1]})
-    local last=$((${#myarray[*]}-1))
-
-    if [ -d ${dir} ]; then
-       report
-       myarray[${last}]=${dir}
-       set --
-       set -- "$@" "${myarray[*]}"
-    else
-       cmdopts="$cmdopts -D"
-    fi
-    return
-}
-
 ## Group
 #  If $group is one of the groups we belong to, use it, otherwise set
 #  -g to our currently active group and report it.
@@ -181,7 +156,8 @@ _group()
     local GRP_LIST
 
     # GID or name?
-    if [ $group -ge 0 2>/dev/null ]; then
+    printf '%d' $group &>/dev/null
+    if [ $? -eq 0 ]; then
        GRP_LIST=$(id -G)
     else
        GRP_LIST=$(id -Gn)
@@ -204,13 +180,14 @@ _group()
 }
 
 ## Owner
-#  Set -u to our username and report if it wasn't already our name.
+#  Set -o to our username and report if it wasn't already our name.
 _owner()
 {
     local MYNAME
 
     # UID or name?
-    if [ $owner -ge 0 2>/dev/null ]; then
+    printf '%d' $owner &>/dev/null
+    if [ $? -eq 0 ]; then
        MYNAME=$(id -u)
     else
        MYNAME=$(id -un)
@@ -219,7 +196,7 @@ _owner()
     if [ $owner != "$MYNAME" ]; then
        report
     fi
-    cmdopts="$cmdopts -u$(id -un)"
+    cmdopts="$cmdopts -o$(id -un)"
     return
 }
 
@@ -232,14 +209,15 @@ _perms()
 {
     local p
     ### HACK-O-MATIC:
-    # Convert symbolic permissions to numerical.  This works via
-    # side-effect because if $perm is text it causes the if to error
-    # and you come out on the else branch.
-    if [ $perm -ge 0 2>/dev/null ]; then
+    # Convert symbolic permissions to numerical.
+    printf '%d' $perm &>/dev/null
+    if [ $? -eq 0 ]; then
        p=$perm
     else
        touch /tmp/hack-o-matic-9000
-       chmod $perm /tmp/hack-o-matic-9000
+       # A tiny risk hard-coding /bin/chmod here, but I don't won't
+       # the chmod wrapper in play for this.
+       /bin/chmod $perm /tmp/hack-o-matic-9000
        p=$(stat --printf "%a" /tmp/hack-o-matic-9000)
        rm -f /tmp/hack-o-matic-9000
     fi
@@ -254,7 +232,7 @@ _perms()
 }
 
 ## Parse the command line.
-#   All we really care about here is -d, -D, -o, -g, and -m.  -c is
+#   All we really care about here is -d, -o, -g, and -m.  -c is
 #   silently dropped.  Unrecognised options cause the script to exit
 #   with a non-zero return code.
 #
@@ -273,48 +251,40 @@ args=bcCdDpsTvg:m:o:S:t:Z:-:
 while getopts $args opts; do
     case $opts in
        (-)
-       # long opts
+       ## Long options
        case $OPTARG in
-           (backup*) cmdopts="$cmdopts --${OPTARG}" ;;
-           (compare) cmdopts="$cmdopts -C" ;;
+           # Passed through unaltered.
+           (backup*|compare|strip|suffix*|verbose|context*|version|help)
+               cmdopts="$cmdopts --${OPTARG}" ;;
+           # The options we care about.
            (directory) _dirs ;;
            (group*) group=${OPTARG/group[=[:space:]]/}; _group ;;
            (mode*) perm=${OPTARG/mode[=[:space:]]/}; _perms ;;
            (owner*) owner=${OPTARG/owner[=[:space:]]/}; _owner ;;
-           (strip) cmdopts="$cmdopts -s" ;;
-           (suffix*) cmdopts="$cmdopts -S${OPTARG/suffix[=[:space:]]/}" ;;
-           (verbose) cmdopts="$cmdopts -v" ;;
-           (context*) cmdopts="$cmdopts -Z${OPTARG/context[=[:space:]]/}" ;;
-           (help) cmdopts="$cmdopts --help" ;;
-           (version) cmdopts="$cmdopts --version" ;;
+           # Anything else errors out.
            (*) echo 1>&2 '***' Illegal option -- ${OPTARG}
                exit 1
                ;;
        esac
        ;;
-       # short opts
-       (b) cmdopts="$cmdopts -b" ;;
-       (C) cmdopts="$cmdopts -C" ;;
-       (c) ;;                  # no-op
+       ## Short options
+       # Passed through unaltered.
+       (b|C|D|p|s|T|v|S|t|Z) cmdopts="$cmdopts -${opts}${OPTARG}" ;;
+       # Dropped.
+       (c) ;;
+       # The options we care about.
        (d) _dirs ;;
-       (D) _leading_dirs ;;
        (g) group=${OPTARG}; _group ;;
        (m) perm=${OPTARG}; _perms ;;
        (o) owner=${OPTARG}; _owner ;;
-       (p) cmdopts="$cmdopts -p" ;;
-       (s) cmdopts="$cmdopts -s" ;;
-       (S) cmdopts="$cmdopts -S${OPTARG}" ;;
-       (t) cmdopts="$cmdopts -t${OPTARG}" ;;
-       (T) cmdopts="$cmdopts -T" ;;
-       (v) cmdopts="$cmdopts -v" ;;
-       (Z) cmdopts="$cmdopts -Z${OPTARG}" ;;
+       # Illegal options.
        (*) exit 1 ;;
     esac
 done
 shift $(( $OPTIND - 1 ))
 
 # We've done all we can, now lets run install
-$DAISY_CHAIN "$cmdopts $@" || exit $?
+$DAISY_CHAIN ${cmdopts} $@ || exit $?
 exit 0
 
 ### End install
index 976ade9..7ca6d26 100755 (executable)
@@ -55,7 +55,7 @@ for dir in $dirs ; do
        cumuldir=$cumuldir$d/
        if [ $SUPPRESSLOCALEDIR -eq 0 ]; then
            echo 1>&2 '***' Possible root intervention required 
-           echo 1>&2 '***' install -vdm1775 -oroot -groot $watchdir/$cumuldir
+           echo 1>&2 '***' install -vdm1775 -oroot -ginstall $watchdir/$cumuldir
        else
            echo 1>&2 '***' Locale directory creation suppressed
            echo 1>&2 '***' $watchdir/$cumuldir
@@ -63,7 +63,7 @@ for dir in $dirs ; do
        fi
     done  
 done
-fi
+
 exit 0
 
 # Local variables: