stub rewritten for self-extraction distros
authorAlex Kirhenshtein <alk@netxms.org>
Mon, 4 Jun 2007 20:40:14 +0000 (20:40 +0000)
committerAlex Kirhenshtein <alk@netxms.org>
Mon, 4 Jun 2007 20:40:14 +0000 (20:40 +0000)
tools/sharIt
tools/stub.sh

index d2c1e4c..21db2d3 100755 (executable)
@@ -1,8 +1,10 @@
 #!/usr/local/bin/bash
+#
+# vim:ts=3 sw=3
 
-#trap '
-#      rm -f tmp
-#' INT EXIT
+trap '
+       rm -f tmp
+' INT EXIT
 
 if [ "x"$3 = "x" ]; then
        echo "Usage: $0 input.tar.gz scriptname output.sh"
@@ -14,6 +16,12 @@ if [ ! -r $1 ]; then
        exit
 fi
 
+stub=`dirname `echo $0``/stub.sh
+if [ ! -r $stub ]; then
+       echo "$stub not found!"
+       exit
+fi
+
 md5=`which md5 2>/dev/null`
 if [ $? != 0 ]; then
        md5=`which md5sum 2>/dev/null`
@@ -28,14 +36,8 @@ if [ $? != 0 ]; then
        fi
 fi
 
-case `uname -s` in
-       *BSD)
-               skip=`echo \`wc -l stub.sh|cut -b1-8\``
-               ;;
-       *)
-               skip=`cat stub.sh | wc -l | sed 's,^ *,,'`
-               ;;
-esac
+skip=`echo \`wc -l stub.sh|cut -b1-8\``
+
 if [ "x$BASH" == "x" ]; then
        skip1=`let $skip+1`
 else
@@ -43,7 +45,7 @@ else
        let skip1++
 fi
 
-sed "s,__SKIP__,$skip,g" < stub.sh |
+sed "s,__SKIP__,$skip,g" < $stub |
        sed "s,__SKIP1__,$skip1,g" |
        sed "s,__COMMAND__,$2,g" > tmp
 hash1=`tail -n +5 tmp | $md5 | cut -b1-32 | tr A-Z a-z`
index 719c499..fba8826 100755 (executable)
@@ -7,8 +7,6 @@ skip1=__SKIP1__
 command=__COMMAND__
 log=/tmp/nxagentupdate.log
 
-wd=`pwd -P`
-
 trap '
        echo "Upgrade script finished" >> $log
        cd $wd
@@ -16,21 +14,76 @@ trap '
        exit
 ' INT EXIT
 
-rm -f $log
-echo "WD = $wd" >> $log
+echo "$0 started: $*" > $log
 
-mktemp() {
+wd=`pwd -P`
+echo "Working directory: $wd" >> $log
+
+_mktemp() {
        d=/tmp/nxupdate.$$.UniQ
        mkdir $d && echo $d || false
 }
 
+for dir in /bin /sbin /usr/bin /usr/local/bin /opt/openssl*/bin; do
+       if [ -d $dir ]; then
+               for app in openssl md5sum md5; do
+                       if [ -x "$dir/$app" ]; then
+                               md5="$dir/$app"
+                               if [ "openssl" = $app ]; then
+                                       md5="$md5 md5"
+                               fi
+                               break
+                       fi
+               done
+               [ "x" != "x$md5" ] && break
+       fi
+done
+
+tmp=`which mktemp 2>/dev/null`
+if [ $? = 0 ]; then
+       echo $tmp | grep "no mptemp in " >/dev/null 2>&1
+       [ $? = 0 ] && alias mktemp=_mktemp
+fi
+
+case `uname -s` in
+       SunOS)
+               tail="tail"
+               ;;
+       *)
+               tail="tail -n"
+               ;;
+esac
+
+if [ "x$md5" != "x" ]; then
+       if [ "X"`head -n $skip $0 |
+               $tail +5 |
+               $md5 |
+               cut -b1-32 |
+               tr A-Z a-z` != "X"$hash1 ];
+       then
+               echo "Script MD5 mismach; upgrade aborted" >> $log
+               exit
+       fi
+
+       if [ "X"`$tail +$skip1 $0 |
+               $md5 |
+               cut -b1-32 |
+               tr A-Z a-z` != "X"$hash2 ];
+       then
+               echo "Payload MD5 mismach; upgrade aborted" >> $log
+               exit
+       fi
+else
+       echo "No md5/md5sum/openssl found, can't check integrity" >> $log
+fi
+
 temp=`mktemp -d /tmp/nxinst.XXXXXX 2>/dev/null`
 if [ $? != 0 ]; then
        echo "Can't create temp dir" >> $log
-       exit 0
+       exit
 fi
 cd $temp
-tail +$skip1 $wd/$0 | gzip -dc 2>/dev/null | tar xf -
+$tail +$skip1 $wd/$0 | gzip -dc 2>/dev/null | tar xf -
 if [ $? != 0 ]; then
        echo "Can't unpack" >> $log
        exit;