home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 15 / hacker15 / 15_H4CK3R#15.ISO / linux_security / engarde / ENGARDE_COMMUNITY_FEINT.ISO / usr / bin / gzexe < prev    next >
Encoding:
Text File  |  2002-05-30  |  3.6 KB  |  148 lines

  1. #!/bin/sh
  2. # gzexe: compressor for Unix executables.
  3. # Use this only for binaries that you do not use frequently.
  4. #
  5. # The compressed version is a shell script which decompresses itself after
  6. # skipping $skip lines of shell commands.  We try invoking the compressed
  7. # executable with the original name (for programs looking at their name).
  8. # We also try to retain the original file permissions on the compressed file.
  9. # For safety reasons, gzexe will not create setuid or setgid shell scripts.
  10.  
  11. # WARNING: the first line of this file must be either : or #!/bin/sh
  12. # The : is required for some old versions of csh.
  13. # On Ultrix, /bin/sh is too buggy, change the first line to: #!/bin/sh5
  14.  
  15. x=`basename "$0"`
  16. if test $# = 0; then
  17.   echo compress executables. original file foo is renamed to foo~
  18.   echo usage: ${x} [-d] files...
  19.   echo   "   -d  decompress the executables"
  20.   exit 1
  21. fi
  22.  
  23. decomp=0
  24. res=0
  25. test "$x" = "ungzexe" && decomp=1
  26. if test "x$1" = "x-d"; then
  27.   decomp=1
  28.   shift
  29. fi
  30.  
  31. cpmod=
  32. if type ${CPMOD:-cpmod} 2>/dev/null; then
  33.   cpmod=${CPMOD:-cpmod}
  34. fi
  35.  
  36. tail=""
  37. IFS="${IFS=     }"; saveifs="$IFS"; IFS="${IFS}:"
  38. for dir in $PATH; do
  39.   test -z "$dir" && dir=.
  40.   if test -f $dir/tail; then
  41.     tail="$dir/tail"
  42.     break
  43.   fi
  44. done
  45. IFS="$saveifs"
  46. if test -z "$tail"; then
  47.   echo cannot find tail
  48.   exit 1
  49. fi
  50.  
  51. for i do
  52.   if test ! -f "$i" ; then
  53.     echo ${x}: $i not a file
  54.     res=1
  55.     continue
  56.   fi
  57.   if test $decomp -eq 0; then
  58.     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
  59.       echo "${x}: $i is already gzexe'd"
  60.       continue
  61.     fi
  62.   fi
  63.   if ls -l "$i" | grep '^...[sS]' > /dev/null; then
  64.     echo "${x}: $i has setuid permission, unchanged"
  65.     continue
  66.   fi
  67.   if ls -l "$i" | grep '^......[sS]' > /dev/null; then
  68.     echo "${x}: $i has setgid permission, unchanged"
  69.     continue
  70.   fi
  71.   case "`basename $i`" in
  72.   bash | chmod | gzip | ln | mktemp | rm | sed | sh | tail)
  73.     echo "${x}: $i would depend on itself"; continue ;;
  74.   esac
  75.  
  76.   tmp=`/bin/mktemp ${TMPDIR:-/tmp}/gzexe.XXXXXXXXXX` || exit 1
  77.   trap "rm -f $tmp; exit 1" HUP INT QUIT PIPE TERM
  78.   trap "rm -f $tmp; exit 0" EXIT
  79.  
  80.   if test -z "$cpmod"; then
  81.     cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
  82.     if test -w $tmp 2>/dev/null; then
  83.       writable=1
  84.     else
  85.       writable=0
  86.       chmod u+w $tmp 2>/dev/null
  87.     fi
  88.   fi
  89.   if test $decomp -eq 0; then
  90.     sed 1q "$0" > $tmp
  91.     sed "s|^if tail|if $tail|" >> $tmp <<'EOF'
  92. skip=14
  93. tmpdir=`/bin/mktemp -d ${TMPDIR:-/tmp}/gzexe.XXXXXXXXXX` || exit 1
  94. prog="${tmpdir}/`echo \"$0\" | sed 's|^.*/||'`"
  95. if tail +$skip "$0" | "/usr/bin"/gzip -cd > "$prog"; then
  96.   /bin/chmod 700 "$prog"
  97.   trap '/bin/rm -rf $tmpdir; exit $res' EXIT
  98.   "$prog" ${1+"$@"}; res=$?
  99. else
  100.   echo "Cannot decompress $0"
  101.   /bin/rm -rf $tmpdir
  102.   exit 1
  103. fi; exit $res
  104. EOF
  105.     gzip -cv9 "$i" >> $tmp || {
  106.       /bin/rm -f $tmp
  107.       echo ${x}: compression not possible for $i, file unchanged.
  108.       res=1
  109.       continue
  110.     }
  111.  
  112.   else
  113.     # decompression
  114.     skip=22
  115.     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
  116.       eval `sed -e 1d -e 2q "$i"`
  117.     fi
  118.     if tail +$skip "$i" | gzip -cd > $tmp; then
  119.       :
  120.     else
  121.       echo ${x}: $i probably not in gzexe format, file unchanged.
  122.       rm -f $tmp
  123.       res=1
  124.       continue
  125.     fi
  126.   fi
  127.   rm -f "$i~"
  128.   mv "$i" "$i~" || {
  129.     echo ${x}: cannot backup $i as $i~
  130.     rm -f $tmp
  131.     res=1
  132.     continue
  133.   }
  134.   mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
  135.     echo ${x}: cannot create $i
  136.     rm -f $tmp
  137.     res=1
  138.     continue
  139.   }
  140.   rm -f $tmp
  141.   if test -n "$cpmod"; then
  142.     $cpmod "$i~" "$i" 2>/dev/null
  143.   elif test $writable -eq 0; then
  144.     chmod u-w $i 2>/dev/null
  145.   fi
  146. done
  147. exit $res
  148.