make scripts fail on unsupported environments
[busybox-power] / debian / scripts / functions
index 31e1454..eb79512 100644 (file)
+#!/bin/sh
+# This file contains functions that are used in multiple other scripts, i.e.
+# shared functions. The purpose of centralising these, is to deduplicate code
+# and increase maintainability
 #
-# This file contains (ideally) all functions supported by busybox sorted by
-# their respective location in a standard Debian installation.
-#
-# Please send in enhancements/corrections to my e-mail adress.
-#
-# By Dennis Groenen <dennis_groenen@hotmail.com>
-# Version 0.1 05-06-2011 (MM-DD-YYYY)
+# By Dennis Groenen <tj.groenen@gmail.com>
+# GPLv3 licensed
 #
 
-# Define possible destinations for the binaries
-DESTINATIONS="DEST_BIN DEST_SBIN DEST_USRBIN DEST_USRSBIN"
+# Verbose-aware echo
+ECHO_VERBOSE() {
+  if test $VERBOSE == 1; then 
+    echo -e "$1"; fi
+}
+
+# Detect the current environment
+CHECK_ENV() {
+    if test -d /scratchbox; then
+      ENVIRONMENT="SDK"
+    else
+      if test -e /proc/component_version; then
+        PROD=$($EXECPWR cat /proc/component_version | $EXECPWR grep product | $EXECPWR cut -d" " -f 6)
+      else
+        PROD=$(/usr/bin/sysinfoclient --get /component/product | $EXECPWR awk '{ print $3 }')
+      fi
+
+      case $PROD in
+        RX-51)
+          ENVIRONMENT="FREMANTLE"
+          ;;
+        RM-680|RM-696)
+          ENVIRONMENT="HARMATTAN"
+          ;;
+        *)
+          echo "busybox-power: unsupported environment: $PROD"
+          exit 1
+          ;;
+      esac
+    fi
+}
+
+# Check whether the user is root
+CHECK_ROOT() {
+    if test "`$EXECPWR id -u`" -ne 0; then
+      echo "error: you're not running me as root"
+      exit 1
+    fi
+}
+
+# Get the version string of the package providing /bin/busybox
+GETBBVERSION() {
+    # XXX We assume the package "busybox" provides /bin/busybox
+    /usr/bin/dpkg -s busybox | $EXECPWR awk '/^Version:/ {print $2}'
+}
+
+# Get the enforcement status of aegis' source origin check. Returns "1" when
+# the check is active, otherwise "0"
+GETORIGINCHECK_STATUS() {
+    ENFORCE="/sys/kernel/security/validator/enforce"
+    ENFORCE_HEX=`$EXECPWR cat $ENFORCE`
+    SID_CHECK_BIT="2"
+
+    if test "$ENFORCE_HEX" == ""; then exit 1; fi
+    RETVAL="1"
+    if test `echo $(($ENFORCE_HEX & $SID_CHECK_BIT))` -eq 0; then
+      RETVAL="0"
+    fi
+    echo $RETVAL
+}
+
+# Set the enforcement status of aegis' source origin check. The check will be
+# enabled when passed "1"; passing "0" will disable it.
+# Works in both normal and open mode via aegisctl, and in patched open mode via
+# via writing to sysfs entries directly
+SETORIGINCHECK_STATUS() {
+    ENABLE=$1
+
+    ENFORCE="/sys/kernel/security/validator/enforce"
+    ENFORCE_HEX=`$EXECPWR cat $ENFORCE`
+    SID_CHECK_BIT="2"
+
+    if test $ENABLE -gt 0; then
+      if test `GETORIGINCHECK_STATUS` -eq 1; then return; fi # Already on
+      ENFORCE_NEW_DEC=`echo $(($ENFORCE_HEX | $SID_CHECK_BIT))`
+      ENFORCE_NEW_HEX=`printf "0x%02x" $ENFORCE_NEW_DEC`
+      echo $ENFORCE_NEW_HEX > $ENFORCE 2> /dev/null
+      if test $? -gt 0; then
+        # Do not exit 1 on failure to re-enable the origincheck; not fatal for
+        # (un)installation of busybox-power
+        /usr/sbin/aegisctl +s > /dev/null
+      fi
+    else
+      if test `GETORIGINCHECK_STATUS` -eq 0; then return; fi # Already off
+      ENFORCE_NEW_DEC=`echo $(($ENFORCE_HEX ^ $SID_CHECK_BIT))`
+      ENFORCE_NEW_HEX=`printf "0x%02x" $ENFORCE_NEW_DEC`
+      echo $ENFORCE_NEW_HEX > $ENFORCE 2> /dev/null
+      if test $? -gt 0; then
+        /usr/sbin/aegisctl @s > /dev/null || exit 1
+      fi
+    fi
+
+    ECHO_VERBOSE "new origincheck: $ENABLE ($ENFORCE_NEW_HEX)"
+}
 
-# Define all applications per destination
-DEST_BIN="bunzip2 bzcat bzip2 cpio cttyhack dnsdomainname dumpkmap ed fbset fdflush hush ip ipaddr iplink iproute iprule mountpoint mt nc pipe_progress popmaildir pscan usleep ash cat chgrp chmod chown cp date dd df dmesg echo false hostname ln login ls mkdir mknod mktemp more mount mv pidof pwd readlink rm rmdir sed sleep stty su sync tar touch true umount uname which grep egrep fgrep run-parts zcat gunzip gzip uncompress sh fsync ping ping6 kill ps fuser netstat"
-DEST_SBIN="blkid blockdev bootchartd crond fdisk findfs ifenslave ip iptunnel klogd loadkmap logread makedevs mdev mkdosfs mke2fs mkfs.ext2 mkfs.vfat nbd-client raidautorun runlevel setconsole start-stop-daemon sulogin sysctl syslogd vconfig getty hwclock killall5 losetup mkswap swapoff swapon pivot_root halt reboot poweroff ifconfig nameif route slattach ifdown ifup zcip"
-DEST_USRBIN="adjtimex arping base64 beep brctl cal catv chattr chpst chrt chvt crontab cryptpw dc deallocvt devmem diff dos2unix dumpleases eject envdir envuidgid ether-wake fdformat fgconsole flock freeramdisk ftpget ftpput hexdump ionice iostat ipcalc kbd_mode last length less linux32 linux64 loadfont lpd lpq lpr lsattr lsusb lzcat lzma lzop lzopcat makemime man microcom mkpasswd mpstat nmeter openvt passwd patch pmap readahead reformime resize rev rpm rpm2cpio rtcwake runsv runsvdir rx script scriptreplay setarch setfont setkeycodes setlogcons setuidgid showkey smemcap softlimit strings sv svlogd tcpsvd telnet tftp timeout traceroute traceroute6 ttysize udhcpc udpsvd unix2dos unlzma unlzop unxz unzip uudecode uuencode vi vlock volname wall wget xz xzcat zcipice [ [[ awk basename cksum clear cmp comm cut dirname du env expand expr find fold getopt head hostid id install ipcrm ipcs logger logname md5sum mesg mkfifo nice nohup od printenv printf renice reset seq setsid sha1sum sort split stat sum tac tail tee test tr tty unexpand uniq wc which who whoami xargs yes ar time nslookup free pgrep pkill top uptime watch realpath killall chvt deallocvt openvt setkeycodes setlogcons kbd_mode"
-DEST_USRSBIN="acpid add-shell addgroup adduser arp chat chpasswd delgroup deluser dhcprelay dnsd fakeidentd ftpd hdparm httpd ifplugd inetd lspci ntpd rdate rdev readprofile remove-shell sendmail sha256sum sha512sum telnetd tftpd tunctl udhcpd watchdog chroot"