Advertisement
s243a

/usr/bin/mk_busybox_utils

Jul 19th, 2019
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.68 KB | None | 0 0
  1. #!/bin/bash
  2. BB_UTIL_DIR=/bin/bb_utils
  3. BB_PATH=/bin/busybox
  4. BB_DIR=/bin
  5. BB_NAME=busybox
  6. [ ! -e "$BB_UTIL_DIR" ] && mkdir -p "$BB_UTIL_DIR"
  7. [ ! -e "$BB_UTIL_DIR/busybox.nosuid" ] && ln $BB_DIR/busybox "$BB_UTIL_DIR/busybox.nosuid"
  8. if [ ! -e "$BB_UTIL_DIR/busybox.suid" ]  &&
  9.        [ -e "$BB_DIR/busybox.suid" ]; then
  10.   ln "$BB_DIR/busybox.suid" "$BB_UTIL_DIR/busybox.suid"
  11. fi
  12. cd "$BB_UTIL_DIR"
  13. for aBB_NAME in busybox.nosuid busybox.suid; do
  14.   cd "$BB_UTIL_DIR"
  15.   [ ! -e "./$aBB_NAME" ] && continue
  16.   #TODO considering adding an EOF at the end of --list to make sure that the read teminates
  17.   "./$aBB_NAME" --list |
  18.   while read UTIL_NAME; do
  19.        cd "$BB_UTIL_DIR"
  20.        if [ ! -e "./$UTIL_NAME" ]; then
  21.          ln -s "$aBB_NAME" "$UTIL_NAME"
  22.        fi
  23.        #echo "reading $UTIL_NAME"
  24.        #set -x
  25.        UTIL_REAL_PATH="$(realpath $(which "$UTIL_NAME"))"
  26.        UTIL_REAL_BB_PATH="$(realpath "$BB_DIR/$aBB_NAME" )"
  27.        UTIL_REAL_bname="$(basename "$UTIL_REAL_PATH")"
  28.        UTIL_REAL_DIR="$(dirname "$UTIL_REAL_PATH")"
  29.        #UTIL_REAL_BB_DIR="$(dirname UTIL_REAL_BB_PATH)"
  30.        #The second condition requires bash: TODO make compataqble with ash
  31.        #Todo, consider considering the case where versions of busybox have overlapping applets
  32.        DIR_OK=0
  33.        [ "$UTIL_REAL_DIR" = "$BB_DIR" ] && DIR_OK=1
  34.        [ "$UTIL_REAL_DIR" = "$BB_UTIL_DIR" ] && DIR_OK=1
  35.        if [ $DIR_OK -eq 1 ] && [[ "$UTIL_REAL_bname" == *busybox* ]]; then        
  36.        #if [ "$(realpath $(which "$UTIL_NAME"))" = "$(realpath "$BB_DIR/$aBB_NAME" )" ] ; then
  37.          echo 'chaing util path of '"$(which "$UTIL_NAME")"
  38.          UTIL_PATH="$(which "$UTIL_NAME")"
  39.          if [ ! -z "$(which "$UTIL_PATH")" ]; then        
  40.            UTIL_DIR="$(dirname $UTIL_PATH)"
  41.            cd "$UTIL_DIR"
  42.            UTIL_SYM_LINK="$(realpath --relative-to="$UTIL_DIR" "$BB_UTIL_DIR/$UTIL_NAME")"
  43.            if [ -e "$UTIL_SYM_LINK" ]; then
  44.              rm  "$(which "$UTIL_NAME")"
  45.              ln -s "$UTIL_SYM_LINK" "$UTIL_PATH"
  46.            elif [ -e "$BB_UTIL_DIR/UTIL_NAME" ]; then [ #In case we don't have the full version of realpath or we make a mistake
  47.              rm  "$(which "$UTIL_NAME")"
  48.              ln -s "$BB_UTIL_DIR/UTIL_NAME" "$UTIL_PATH"
  49.            fi
  50.          fi
  51.        fi
  52.        #set +x
  53.   done
  54. done
  55.  
  56. if  [ -e "$BB_DIR/busybox" ] &&  
  57.     [ $(file --mime-type "$BB_DIR/busybox" | grep -c text) -eq 0 ]; then
  58.  
  59. mv "$BB_DIR/busybox" "$BB_DIR/busybox.back.$$"
  60.  
  61.  
  62. echo "#!/bin/sh
  63. script=\${0##*/}
  64. script2=\$1
  65. if [ \"\$script\" = \"busybox\" ]; then
  66.  script=\$script2 ; shift
  67. fi
  68. exec \"$BB_UTIL_DIR/\$script\" \"\$@\"" > "/$BB_DIR/busybox"
  69. chmod +x "/$BB_DIR/busybox"
  70. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement