tweak hashing and backup logic
authorDennis Groenen <tj.groenen@gmail.com>
Tue, 16 Oct 2012 14:11:20 +0000 (16:11 +0200)
committerDennis Groenen <tj.groenen@gmail.com>
Tue, 16 Oct 2012 18:46:02 +0000 (20:46 +0200)
debian/scripts/install-binary.sh
debian/scripts/uninstall-binary.sh

index 2ac1af8..dd3a11e 100755 (executable)
@@ -17,6 +17,9 @@ INSTALLDIR="/opt/busybox-power"
 EXECPWR="$INSTALLDIR/busybox.power"
 VERBOSE="0"
 
+INSTBINARY_SHA1=`$EXECPWR sha1sum $EXECPWR | $EXECPWR awk '{ print $1 }'`
+ORIGBINARY_SHA1=`$EXECPWR sha1sum /bin/busybox | $EXECPWR awk '{ print $1 }'`
+
 # Load shared functions
 source $INSTALLDIR/functions
 
@@ -37,42 +40,24 @@ CHECK_SYMLINKSFILE() {
     fi
 }
 
-# Create SHA1 hashes of relevant binaries
-HASH_BINARIES() {
-    $EXECPWR sha1sum $INSTALLDIR/busybox.power | $EXECPWR awk '{ print $1 }' \
-      > $INSTALLDIR/busybox.power.sha1
-    $EXECPWR sha1sum /bin/busybox | $EXECPWR awk '{ print $1 }' \
-      > $INSTALLDIR/busybox.original.sha1
-}
-
 # Backup the original BusyBox binary
 BACKUP() {
-    case $ENVIRONMENT in
-      SDK)
-        # Scratchbox does not ship with BusyBox by default
-        if test -e /bin/busybox; then
-          $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original; fi
-        ;;
-      FREMANTLE)
-        # Check whether busybox-power isn't somehow installed already
-        INSTBINARY_SHA1=`$EXECPWR cat $INSTALLDIR/busybox.power.sha1`
-        ORIGBINARY_SHA1=`$EXECPWR cat $INSTALLDIR/busybox.original.sha1`
-        if test "$INSTBINARY_SHA1" == "$ORIGBINARY_SHA1"; then
-          echo "warning: installed busybox binary matches the binary"
-          echo "  that is to be installed"
-          if ! test -e $INSTALLDIR/busybox.original; then 
-            $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original; fi
-        else
-          $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original
-        fi
-        ;;
-    esac
+    if test ! -e /bin/busybox; then
+      # Scratchbox does not ship with BusyBox by default
+      return
+    fi
+
+    if ! test "$INSTBINARY_SHA1" == "$ORIGBINARY_SHA1"; then
+      $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original
+      $EXECPWR sha1sum $INSTALLDIR/busybox.original | $EXECPWR awk '{ print $1 }' \
+        > $INSTALLDIR/busybox.original.sha1
+    fi
 }
 
 # Overwrite the installed binary with the enhanced binary
 INSTALL() {
     /usr/sbin/dpkg-divert --local --divert /bin/busybox.distrib /bin/busybox
-    $EXECPWR cp -f $INSTALLDIR/busybox.power /bin/busybox
+    $EXECPWR cp -f $EXECPWR /bin/busybox
 }
 
 # Create missing symlinks to the enhanced binary
@@ -148,9 +133,7 @@ CHECK_STANDALONE
 CHECK_APPLETSFILE
 CHECK_SYMLINKSFILE
 if test "$ENVIRONMENT" != "SDK"; then
-  CHECK_ROOT
-  HASH_BINARIES
-fi
+  CHECK_ROOT; fi
 BACKUP
 INSTALL
 SYMLINK
index dbe6785..43407f3 100755 (executable)
@@ -17,6 +17,10 @@ VERBOSE="0"
 MODIFIEDBIN="0"
 DIVERTED="0"
 
+INSTBINARY_SHA1=`sha1sum $EXECPWR | awk '{ print $1 }'`
+if test -e $INSTALLDIR/busybox.original; then
+  ORIGBINARY_SHA1=`sha1sum $INSTALLDIR/busybox.original | awk '{ print $1 }'`; fi
+
 # Load shared functions
 source $INSTALLDIR/functions
 
@@ -35,6 +39,10 @@ CHECK_DIVERT() {
 
 # Check the (integrity) of our BusyBox backup
 CHECK_BACKUP() {
+    # SDK doesn't ship with BusyBox by default, there might be no backup at all
+    if test ! -e $INSTALLDIR/busybox.original -a "$ENVIRONMENT" == "SDK" ; then
+      return; fi
+
     # Firstly, check whether the backup still exists
     if test ! -e $INSTALLDIR/busybox.original; then
       echo -e "Error: original binary is missing! Continuing will only remove the symlinks made during installation, /bin/busybox stays untouched.\n" >> /tmp/busybox-power-error
@@ -43,9 +51,7 @@ CHECK_BACKUP() {
 
     # Secondly, check the integrity of the backup
     if test -e $INSTALLDIR/busybox.original.sha1; then
-      INSTBINARY_SHA1=`cat $INSTALLDIR/busybox.original.sha1`
-      ORIGBINARY_SHA1=`sha1sum $INSTALLDIR/busybox.original | awk '{ print $1 }'`
-      if test ! "$INSTBINARY_SHA1" == "$ORIGBINARY_SHA1"; then
+      if test ! "`cat $INSTALLDIR/busybox.original.sha1`" == "$ORIGBINARY_SHA1"; then
         echo -e "Warning: the backed-up original binary has been modified since installing busybox-power (invalid SHA1 checksum). Do not continue unless you're sure $INSTALLDIR/busybox.original isn't corrupted.\n" >> /tmp/busybox-power-error
       fi
     else
@@ -55,13 +61,9 @@ CHECK_BACKUP() {
 
 # Check whether /bin/busybox has been modified after bb-power's installation
 CHECK_INSTALLEDBIN() {
-    if test -e $INSTALLDIR/busybox.power.sha1; then
-      INSTBINARY_SHA1=`sha1sum /bin/busybox | awk '{ print $1 }'`
-      ORIGBINARY_SHA1=`cat $INSTALLDIR/busybox.power.sha1`
-      if test ! "$INSTBINARY_SHA1" == "$ORIGBINARY_SHA1"; then
-        echo -e "Warning: /bin/busybox has been modified since installing busybox-power (invalid SHA1 checksum). Your current /bin/busybox won't be touched, our backup of the original /bin/busybox will be copied to /opt/busybox.original. \n" >> /tmp/busybox-power-error
-        MODIFIEDBIN="1"
-      fi
+    if test ! "$INSTBINARY_SHA1" == "`sha1sum /bin/busybox | awk '{ print $1 }'`"; then
+      echo -e "Warning: /bin/busybox has been modified since installing busybox-power (invalid SHA1 checksum). Your current /bin/busybox won't be touched, our backup of the original /bin/busybox will be copied to /opt/busybox.original. \n" >> /tmp/busybox-power-error
+      MODIFIEDBIN="1"
     fi
 }
 
@@ -154,7 +156,6 @@ UNSYMLINK() {
 # Action to be performed after restoring original busybox
 CLEANUP() {
     OLDFILES="busybox-power.symlinks
-      busybox.power.sha1
       busybox.original.sha1"
 
     for file in $OLDFILES; do