Fix a bug that was exposed by the SXEmacs configure script.
authorSteve Youngs <steve@sxemacs.org>
Tue, 11 Mar 2014 04:16:31 +0000 (14:16 +1000)
committerSteve Youngs <steve@sxemacs.org>
Tue, 11 Mar 2014 04:16:31 +0000 (14:16 +1000)
Under certain conditions a directory could be created with no execute bit
set (600 instead of 700).  When the chmod wrapper (and install) encounter
symbolic permissions they are converted to octal by stat'ing a temp file
that was chmod'd with the symbolic perm.  Because of the obnoxious and
convoluted way that autoconf does things, that chmod had to be called
under an exec.

This changeset cares for that plus a couple of other tiny things like
always testing for root with `id -u' instead of $UID.

* usr/lib/pkgusr/chmod: Use id to check for root because $UID
cannot be guaranteed to exist.

Only remove real options to guard against accidently removing a
symbolic perm that begins with a dash.  I don't even know if that
is possible, I've never used symbolic perms, never will, they're a
stupid idea.

Create the hack-o-matic file with mktemp and call $DAISY_CHAIN
there via exec.

* usr/lib/pkgusr/chgrp: Use id for root check.

* usr/lib/pkgusr/chown: Ditto.

* usr/lib/pkgusr/mkdir: Ditto.

* usr/lib/pkgusr/install: Ditto.
(_perms): Use mktemp, the same as in the chmod wrapper.

Signed-off-by: Steve Youngs <steve@sxemacs.org>
usr/lib/pkgusr/chgrp
usr/lib/pkgusr/chmod
usr/lib/pkgusr/chown
usr/lib/pkgusr/install
usr/lib/pkgusr/mkdir

index 530ae9c..e61e00f 100755 (executable)
@@ -21,7 +21,7 @@ if [ ! -n "$DAISY_CHAIN" ]; then
     exit 1
 fi
 
-if [ $UID == 0 ]; then
+if [ $(id -u) == 0 ]; then
     echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
     echo 1>&2 '***' call '"'$DAISY_CHAIN $@'"' directly
     exit 1
@@ -42,7 +42,7 @@ done
 # find out.
 GRP_CHAIN=""
 # name or GID?
-printf '%d' $1 &>/dev/null
+printf '%d' "$1" &>/dev/null
 if [ $? -eq 0 ]; then
     GRP_LIST=$(id -G)
 else
index e1aa49e..03d13ce 100755 (executable)
@@ -22,7 +22,7 @@ if [ ! -n "$DAISY_CHAIN" ]; then
     exit 1
 fi
 
-if [ $UID == 0 ]; then
+if [ $(id -u) -eq 0 ]; then
     echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
     echo 1>&2 '***' call '"'$DAISY_CHAIN $@'"' directly.
     exit 1
@@ -36,25 +36,25 @@ cmdline="$@"
 opts=""
 while [ -n "$1" ]; do
     case $1 in
-       (-*) opts="$opts $1" ; shift ;;
+       (-[cfvR]|--[chnpqrsv]*) opts="$opts $1" ; shift ;;
        (*) break ;;
     esac
 done
+
 # $1 should now be the perm arg
+perm=$1
 
-# Octal or symbolic?
-printf '%d' $1 &>/dev/null
-if [ $? -eq 0 ]; then
-    perm=$1
-else
-    touch /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
+# Octal or symbolic?  If the latter, convert to the former.
+printf '%d' "$perm" &>/dev/null
+if [ $? -ne 0 ]; then
+    testfile=$(mktemp hack-o-matic-chmod.XXXXX --tmpdir)
+    exec $DAISY_CHAIN ${perm} ${testfile}
+    perm=$(stat --printf "%a" ${testfile})
+    rm -f ${testfile}
 fi
 
 # if it is 4 digits, they're trying to do funky shit
-if [ $perm -gt 999 ]; then
+if [ ${perm} -gt 999 ]; then
     # Chop off the 1st digit (the set{uid,gid,sticky} bit)
     perm=${perm/?/}
     echo 1>&2 '***' chmod ${cmdline}
@@ -63,7 +63,7 @@ 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 933c415..b2e1389 100755 (executable)
@@ -22,7 +22,7 @@ if [ ! -n "$DAISY_CHAIN" ]; then
     exit 1
 fi
 
-if [ $UID == 0 ]; then
+if [ $(id -u) == 0 ]; then
     echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
     echo 1>&2 '***' call '"'$DAISY_CHAIN $@'"' directly.
     exit 1
@@ -52,7 +52,7 @@ report=0
 
 # Catch the case where USER is somebody else.
 if [ -n "$usr" ]; then
-    printf '%d' $usr &>/dev/null
+    printf '%d' "$usr" &>/dev/null
     if [ $? -eq 0 ]; then
        if [ $usr -ne $(id -u) ]; then
            report=1
@@ -67,7 +67,7 @@ fi
 # Catch the case where GROUP isn't in our groups.
 if [ -n "$grp" -a $report -eq 0 ]; then
     GRP_CHAIN=""
-    printf '%d' $grp &>/dev/null
+    printf '%d' "$grp" &>/dev/null
     if [ $? -eq 0 ]; then
        GRP_LIST=$(id -G)
     else
index bca9010..fec2973 100755 (executable)
@@ -100,7 +100,7 @@ if [ ! -n "$DAISY_CHAIN" ]; then
 fi
 
 ## root has no business installing things here!!
-if [ $UID -eq 0 ]; then
+if [ $(id -u) -eq 0 ]; then
     echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
     echo 1>&2 '***' call '"'$DAISY_CHAIN ${pristinecmd[*]}'"' directly
     exit 1
@@ -156,7 +156,7 @@ _group()
     local GRP_LIST
 
     # GID or name?
-    printf '%d' $group &>/dev/null
+    printf '%d' "$group" &>/dev/null
     if [ $? -eq 0 ]; then
        GRP_LIST=$(id -G)
     else
@@ -186,7 +186,7 @@ _owner()
     local MYNAME
 
     # UID or name?
-    printf '%d' $owner &>/dev/null
+    printf '%d' "$owner" &>/dev/null
     if [ $? -eq 0 ]; then
        MYNAME=$(id -u)
     else
@@ -207,26 +207,24 @@ _owner()
 #  are symbolic, convert them to numerical first.
 _perms()
 {
-    local p
+    local testfile
     ### HACK-O-MATIC:
     # Convert symbolic permissions to numerical.
-    printf '%d' $perm &>/dev/null
-    if [ $? -eq 0 ]; then
-       p=$perm
-    else
-       touch /tmp/hack-o-matic-9000
+    printf '%d' "$perm" &>/dev/null
+    if [ $? -ne 0 ]; then
+       testfile=$(mktemp hack-o-matic-install.XXXXX --tmpdir)
        # 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
+       exec /bin/chmod ${perm} ${testfile}
+       perm=$(stat --printf "%a" ${testfile})
+       rm -f ${testfile}
     fi
     # Catch the funky shit
-    if [ $p -gt 999 ]; then
+    if [ $perm -gt 999 ]; then
        report
-       cmdopts="$cmdopts -m${p/?/}"
+       cmdopts="$cmdopts -m${perm/?/}"
     else
-       cmdopts="$cmdopts -m$p"
+       cmdopts="$cmdopts -m$perm"
     fi
     return
 }
@@ -284,7 +282,7 @@ done
 shift $(( $OPTIND - 1 ))
 
 # We've done all we can, now lets run install
-$DAISY_CHAIN ${cmdopts} $@ || exit $?
+exec $DAISY_CHAIN ${cmdopts} $@ || exit $?
 exit 0
 
 ### End install
index 7ca6d26..75ceaa8 100755 (executable)
@@ -23,7 +23,7 @@ if [ ! -n "$DAISY_CHAIN" ]; then
     exit 1
 fi
 
-if [ $UID == 0 ]; then
+if [ $(id -u) == 0 ]; then
     echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
     echo 1>&2 '***' Call '"'$DAISY_CHAIN $@'"' directly.
     exit 1