head 1.24; access; symbols SHTOOL_2_0_8:1.24 SHTOOL_2_0_7:1.24 SHTOOL_2_0_6:1.24 SHTOOL_2_0_5:1.24 SHTOOL_2_0_4:1.24 SHTOOL_2_0_3:1.24 SHTOOL_2_0_2:1.24 SHTOOL_2_0_1:1.23 SHTOOL_2_0_0:1.23 SHTOOL_2_0b3:1.23 SHTOOL_2_0b2:1.23 SHTOOL_2_0b1:1.22 SHTOOL_1_6:1.19.0.2 SHTOOL_1_6_2:1.19 SHTOOL_1_6_1:1.19 SHTOOL_1_6_0:1.19 SHTOOL_1_5:1.17.0.2 SHTOOL_1_5_4:1.17 SHTOOL_1_5_3:1.17 SHTOOL_1_5_2:1.17 SHTOOL_1_5_1:1.16 SHTOOL_1_5_0:1.16 SHTOOL_1_4_9:1.16 SHTOOL_1_4_8:1.16 SHTOOL_1_4_7:1.13 SHTOOL_1_4_6:1.12 SHTOOL_1_4_5:1.11 SHTOOL_1_4_4:1.11 SHTOOL_1_4_3:1.11 SHTOOL_1_4_2:1.11 SHTOOL_1_4_1:1.11 SHTOOL_1_4_0:1.9 SHTOOL_1_3_3:1.7 SHTOOL_1_3_2:1.6 SHTOOL_1_3_1:1.6 SHTOOL_1_3_0:1.6; locks; strict; comment @# @; 1.24 date 2005.06.15.08.11.16; author rse; state Exp; branches; next 1.23; 1.23 date 2004.04.07.07.55.02; author rse; state Exp; branches; next 1.22; 1.22 date 2003.12.13.21.23.55; author rse; state Exp; branches; next 1.21; 1.21 date 2003.08.13.14.20.34; author rse; state Exp; branches; next 1.20; 1.20 date 2003.02.11.12.56.39; author rse; state Exp; branches; next 1.19; 1.19 date 2001.09.23.10.58.10; author rse; state Exp; branches; next 1.18; 1.18 date 2001.06.14.19.02.54; author rse; state Exp; branches; next 1.17; 1.17 date 2000.11.29.20.03.03; author rse; state Exp; branches; next 1.16; 1.16 date 2000.03.12.13.31.35; author rse; state Exp; branches; next 1.15; 1.15 date 2000.03.12.13.28.24; author rse; state Exp; branches; next 1.14; 1.14 date 2000.03.12.12.46.42; author rse; state Exp; branches; next 1.13; 1.13 date 99.12.24.15.11.23; author rse; state Exp; branches; next 1.12; 1.12 date 99.09.05.13.16.45; author rse; state Exp; branches; next 1.11; 1.11 date 99.07.07.14.34.55; author rse; state Exp; branches; next 1.10; 1.10 date 99.07.07.12.03.19; author rse; state Exp; branches; next 1.9; 1.9 date 99.07.01.15.04.36; author rse; state Exp; branches; next 1.8; 1.8 date 99.07.01.15.02.47; author rse; state Exp; branches; next 1.7; 1.7 date 99.06.29.17.37.34; author rse; state Exp; branches; next 1.6; 1.6 date 99.06.18.09.57.42; author rse; state Exp; branches; next 1.5; 1.5 date 99.06.17.07.35.08; author rse; state Exp; branches; next 1.4; 1.4 date 99.06.15.08.58.49; author rse; state Exp; branches; next 1.3; 1.3 date 99.06.15.08.43.26; author rse; state Exp; branches; next 1.2; 1.2 date 99.06.12.14.46.37; author rse; state Exp; branches; next 1.1; 1.1 date 99.06.12.14.08.49; author rse; state Exp; branches; next ; desc @@ 1.24 log @Fix insecure temporary file handling (CAN-2005-1751, CAN-2005-1759). Submitted by: Eric Romang @ text @## ## This file is part of shtool and free software; you can redistribute ## it and/or modify it under the terms of the GNU General Public ## License as published by the Free Software Foundation; either version ## 2 of the License, or (at your option) any later version. ## ## This file is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, ## USA, or contact Ralf S. Engelschall . ## ## ## COMMON UTILITY CODE ## # commonly used ASCII values ASC_TAB=" " ASC_NL=" " # determine name of tool if [ ".$tool" != . ]; then # used inside shtool script toolcmd="$0 $tool" toolcmdhelp="shtool $tool" msgprefix="shtool:$tool" else # used as standalone script toolcmd="$0" toolcmdhelp="sh $0" msgprefix="$str_tool" fi # parse argument specification string eval `echo $arg_spec |\ sed -e 's/^\([0-9]*\)\([+=]\)/arg_NUMS=\1; arg_MODE=\2/'` # parse option specification string eval `echo h.$opt_spec |\ sed -e 's/\([a-zA-Z0-9]\)\([.:+]\)/opt_MODE_\1=\2;/g'` # parse option alias string eval `echo h:help,$opt_alias |\ sed -e 's/-/_/g' -e 's/\([a-zA-Z0-9]\):\([^,]*\),*/opt_ALIAS_\2=\1;/g'` # interate over argument line opt_PREV='' while [ $# -gt 0 ]; do # special option stops processing if [ ".$1" = ".--" ]; then shift break fi # determine option and argument opt_ARG_OK=no if [ ".$opt_PREV" != . ]; then # merge previous seen option with argument opt_OPT="$opt_PREV" opt_ARG="$1" opt_ARG_OK=yes opt_PREV='' else # split argument into option and argument case "$1" in --[a-zA-Z0-9]*=*) eval `echo "x$1" |\ sed -e 's/^x--\([a-zA-Z0-9-]*\)=\(.*\)$/opt_OPT="\1";opt_ARG="\2"/'` opt_STR=`echo $opt_OPT | sed -e 's/-/_/g'` eval "opt_OPT=\${opt_ALIAS_${opt_STR}-${opt_OPT}}" ;; --[a-zA-Z0-9]*) opt_OPT=`echo "x$1" | cut -c4-` opt_STR=`echo $opt_OPT | sed -e 's/-/_/g'` eval "opt_OPT=\${opt_ALIAS_${opt_STR}-${opt_OPT}}" opt_ARG='' ;; -[a-zA-Z0-9]*) eval `echo "x$1" |\ sed -e 's/^x-\([a-zA-Z0-9]\)/opt_OPT="\1";/' \ -e 's/";\(.*\)$/"; opt_ARG="\1"/'` ;; -[a-zA-Z0-9]) opt_OPT=`echo "x$1" | cut -c3-` opt_ARG='' ;; *) break ;; esac fi # eat up option shift # determine whether option needs an argument eval "opt_MODE=\$opt_MODE_${opt_OPT}" if [ ".$opt_ARG" = . ] && [ ".$opt_ARG_OK" != .yes ]; then if [ ".$opt_MODE" = ".:" ] || [ ".$opt_MODE" = ".+" ]; then opt_PREV="$opt_OPT" continue fi fi # process option case $opt_MODE in '.' ) # boolean option eval "opt_${opt_OPT}=yes" ;; ':' ) # option with argument (multiple occurances override) eval "opt_${opt_OPT}=\"\$opt_ARG\"" ;; '+' ) # option with argument (multiple occurances append) eval "opt_${opt_OPT}=\"\$opt_${opt_OPT}\${ASC_NL}\$opt_ARG\"" ;; * ) echo "$msgprefix:Error: unknown option: \`$opt_OPT'" 1>&2 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2 exit 1 ;; esac done if [ ".$opt_PREV" != . ]; then echo "$msgprefix:Error: missing argument to option \`$opt_PREV'" 1>&2 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2 exit 1 fi # process help option if [ ".$opt_h" = .yes ]; then echo "Usage: $toolcmdhelp $str_usage" exit 0 fi # complain about incorrect number of arguments case $arg_MODE in '=' ) if [ $# -ne $arg_NUMS ]; then echo "$msgprefix:Error: invalid number of arguments (exactly $arg_NUMS expected)" 1>&2 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 exit 1 fi ;; '+' ) if [ $# -lt $arg_NUMS ]; then echo "$msgprefix:Error: invalid number of arguments (at least $arg_NUMS expected)" 1>&2 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 exit 1 fi ;; esac # establish a temporary file on request if [ ".$gen_tmpfile" = .yes ]; then # create (explicitly) secure temporary directory if [ ".$TMPDIR" != . ]; then tmpdir="$TMPDIR" elif [ ".$TEMPDIR" != . ]; then tmpdir="$TEMPDIR" else tmpdir="/tmp" fi tmpdir="$tmpdir/.shtool.$$" ( umask 077 rm -rf "$tmpdir" >/dev/null 2>&1 || true mkdir "$tmpdir" >/dev/null 2>&1 if [ $? -ne 0 ]; then echo "$msgprefix:Error: failed to create temporary directory \`$tmpdir'" 1>&2 exit 1 fi ) # create (implicitly) secure temporary file tmpfile="$tmpdir/shtool.tmp" touch "$tmpfile" fi # utility function: map string to lower case util_lower () { echo "$1" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' } # utility function: map string to upper case util_upper () { echo "$1" | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' } # cleanup procedure shtool_exit () { rc="$1" if [ ".$gen_tmpfile" = .yes ]; then rm -rf "$tmpdir" >/dev/null 2>&1 || true fi exit $rc } @ 1.23 log @Replace "tr 'x-' 'x_'" with "sed -e 's/-/_/g'" constructs because some tr(1) (at least Solaris' /usr/ucb/tr) go into endless loops on (incorrectly assumed) range constructs with no ends. Submitted by: Armin Wolfermann @ text @d164 1 d172 13 a184 4 tmpfile="$tmpdir/.shtool.$$" rm -f $tmpfile >/dev/null 2>&1 touch $tmpfile chmod 600 $tmpfile d201 1 a201 1 rm -f $tmpfile >/dev/null 2>&1 || true @ 1.22 log @Replaced old and inflexible "shtool guessos" command with a completely new command "shtool platform" command. It distinguishes a Unix platform according to its hardware architecture and operating system. For both there is a class, product and technology identification. For each of those six identifications, there is a verbose, regular and concise version. @ text @d50 1 a50 1 tr 'x-' 'x_' | sed -e 's/\([a-zA-Z0-9]\):\([^,]*\),*/opt_ALIAS_\2=\1;/g'` d75 1 a75 1 opt_STR=`echo $opt_OPT | tr 'x-' 'x_'` d80 1 a80 1 opt_STR=`echo $opt_OPT | tr 'x-' 'x_'` @ 1.21 log @Replace "[ x -o/-a x ]" constructs with "[ x ] ||/&& [ x ]" and "egrep"/"fgrep" constructs with compatible "grep" or "case" constructs. This way GNU shtool is now more POSIX 1003.1-2001 compliant. Submitted by: Paul Eggert @ text @d177 10 @ 1.20 log @Fixed error exit in all commands by introducing an "shtool_exit" function (which correctly cleans up temporary files) and replacing all "exit" calls with "shtool_exit". THIS THE FIRST TIME INTRODUCED THE USAGE OF A SHELL FUNCTION TO THE SHTOOL CODE. This is known to break on _very_ anchient and broken Bourne-Shell implementations and is considered to be no longer a real problem in the year 2003. Especially there is no alternative way anyway. @ text @d104 2 a105 2 if [ ".$opt_ARG" = . -a ".$opt_ARG_OK" != .yes ]; then if [ ".$opt_MODE" = ".:" -o ".$opt_MODE" = ".+" ]; then @ 1.19 log @*** empty log message *** @ text @d177 9 @ 1.18 log @*** empty log message *** @ text @d48 4 d72 12 d126 1 a126 1 echo "$msgprefix:Error: unknown option: \`-$opt_OPT'" 1>&2 d133 1 a133 1 echo "$msgprefix:Error: missing argument to option \`-$opt_PREV'" 1>&2 @ 1.17 log @*** empty log message *** @ text @d22 5 d107 1 a107 1 eval "opt_${opt_OPT}=\"\$opt_${opt_OPT} \$opt_ARG\"" @ 1.16 log @*** empty log message *** @ text @d153 1 @ 1.15 log @*** empty log message *** @ text @d36 2 a37 1 eval `echo $arg_spec | sed -e 's/^\([0-9]*\)\([+=]\)/arg_NUMS=\1; arg_MODE=\2/'` d40 2 a41 1 eval `echo h.$opt_spec | sed -e 's/\([a-zA-Z0-9]\)\([.:+]\)/opt_MODE_\1=\2;/g'` @ 1.14 log @*** empty log message *** @ text @d24 1 a24 1 # inside shtool d29 1 a29 1 # standalone d31 1 a31 1 toolcmdhelp="sh.$tool" d36 1 a36 1 eval `echo $arg_spec | sed -e 's/^\([0-9]*\)\([+=]\)/arg_NUMS="\1"; arg_MODE="\2"/'` d39 1 a39 8 for opt in `echo "h$opt_spec" | sed -e 's/\([a-zA-Z0-9][:+]*\)/\1 /g'`; do opt_MODE="." eval `echo $opt |\ sed -e 's/^\([a-zA-Z0-9]\)/opt_THIS="\1";/' \ -e 's/";\([:+]\)/"; opt_MODE="\1";/'` eval "opt_MODE_${opt_THIS}=${opt_MODE}" test "$opt_MODE" = . && eval "opt_${opt_THIS}=no" done @ 1.13 log @*** empty log message *** @ text @d36 1 a36 2 eval `echo $arg_spec | sed -e 's/^\([0-9]*\)\(.*\)/arg_NUMS="\1"; arg_MODE="\2"/'` test ".$arg_MODE" = . && arg_MODE="=" d64 1 a64 1 opt_PREV="" d74 1 a74 2 opt_OPT=`awk 'BEGIN { printf("%s", substr(OPT, 2)); }' \ "OPT=$1" &2 d126 1 a126 1 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 d132 1 a132 1 echo "Usage: $toolcmd $str_usage" @ 1.9 log @*** empty log message *** @ text @d116 2 a117 2 echo "$msgprefix:ERROR: unknown option: \`-$opt_OPT'" 1>&2 echo "$msgprefix:HINT: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 d123 2 a124 2 echo "$msgprefix:ERROR: missing argument to option \`-$opt_PREV'" 1>&2 echo "$msgprefix:HINT: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 d138 2 a139 2 echo "$msgprefix:ERROR: invalid number of arguments (exactly $arg_NUMS expected)" 1>&2 echo "$msgprefix:HINT: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 d145 2 a146 2 echo "$msgprefix:ERROR: invalid number of arguments (at least $arg_NUMS expected)" 1>&2 echo "$msgprefix:HINT: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 @ 1.8 log @*** empty log message *** @ text @d83 2 a84 2 *) break d103 1 a103 1 "." ) d107 1 a107 1 ":" ) d109 1 a109 1 eval "opt_${opt_OPT}=\"\$opt_ARG\"" d111 1 a111 1 "+" ) @ 1.7 log @*** empty log message *** @ text @d112 1 a112 1 # option with argument (multiple occurances append) @ 1.6 log @*** empty log message *** @ text @d73 1 a73 1 -[a-zA-Z0-9]*) d78 1 a78 1 -[a-zA-Z0-9]) d113 1 a113 1 eval "opt_${opt_OPT}=\"\$opt_${opt_OPT} \$opt_ARG\"" @ 1.5 log @*** empty log message *** @ text @d57 1 a57 1 if [ "$1" = "--" ]; then @ 1.4 log @*** empty log message *** @ text @d117 1 a117 1 echo "$msgprefix:HINT: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 d124 1 a124 1 echo "$msgprefix:HINT: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 d139 1 a139 1 echo "$msgprefix:HINT: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 d146 1 a146 1 echo "$msgprefix:HINT: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 @ 1.3 log @*** empty log message *** @ text @d35 1 @ 1.2 log @*** empty log message *** @ text @d62 1 d67 1 d93 1 a93 1 if [ ".$opt_ARG" = . ]; then @ 1.1 log @*** empty log message *** @ text @d1 16 @