1
0

bundle-libraries.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env bash
  2. #
  3. # Script to install host system binaries along with required libraries.
  4. #
  5. # Copyright (C) 2012-2017 Jo-Philipp Wich <jo@mein.io>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. DIR="$1"; shift
  21. _cp() {
  22. cp ${VERBOSE:+-v} -L "$1" "$2" || {
  23. echo "cp($1 $2) failed" >&2
  24. exit 1
  25. }
  26. }
  27. _mv() {
  28. mv ${VERBOSE:+-v} "$1" "$2" || {
  29. echo "mv($1 $2) failed" >&2
  30. exit 1
  31. }
  32. }
  33. _md() {
  34. mkdir ${VERBOSE:+-v} -p "$1" || {
  35. echo "mkdir($1) failed" >&2
  36. exit 2
  37. }
  38. }
  39. _ln() {
  40. ln ${VERBOSE:+-v} -sf "$1" "$2" || {
  41. echo "ln($1 $2) failed" >&2
  42. exit 3
  43. }
  44. }
  45. _relpath() {
  46. local base="$(readlink -f "$1")"
  47. local dest="$(readlink -f "$2")"
  48. local up
  49. [ -d "$base" ] || base="${base%/*}"
  50. [ -d "$dest" ] || dest="${dest%/*}"
  51. while true; do
  52. case "$base"
  53. in "$dest"/*)
  54. echo "$up/${base#$dest/}"
  55. break
  56. ;;
  57. *)
  58. dest="${dest%/*}"
  59. up="${up:+$up/}.."
  60. ;;
  61. esac
  62. done
  63. }
  64. _runas_so() {
  65. cat <<-EOT | ${CC:-gcc} -x c -fPIC -shared -o "$1" -
  66. #include <unistd.h>
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69. int mangle_arg0(int argc, char **argv, char **env) {
  70. char *arg0 = getenv("RUNAS_ARG0");
  71. if (arg0) {
  72. argv[0] = arg0;
  73. unsetenv("RUNAS_ARG0");
  74. }
  75. return 0;
  76. }
  77. #ifdef __APPLE__
  78. __attribute__((section("__DATA,__mod_init_func")))
  79. #else
  80. __attribute__((section(".init_array")))
  81. #endif
  82. static void *mangle_arg0_constructor = &mangle_arg0;
  83. EOT
  84. [ -x "$1" ] || {
  85. echo "compiling preload library failed" >&2
  86. exit 5
  87. }
  88. }
  89. _patch_ldso() {
  90. _cp "$1" "$1.patched"
  91. sed -i -e 's,/\(usr\|lib\|etc\)/,/###/,g' "$1.patched"
  92. if "$1.patched" 2>&1 | grep -q -- --library-path; then
  93. _mv "$1.patched" "$1"
  94. else
  95. echo "binary patched ${1##*/} not executable, using original" >&2
  96. rm -f "$1.patched"
  97. fi
  98. }
  99. _patch_glibc() {
  100. _cp "$1" "$1.patched"
  101. sed -i -e 's,/usr/\(\(lib\|share\)/locale\),/###/\1,g' "$1.patched"
  102. if "$1.patched" 2>&1 | grep -q -- GNU; then
  103. _mv "$1.patched" "$1"
  104. else
  105. echo "binary patched ${1##*/} not executable, using original" >&2
  106. rm -f "$1.patched"
  107. fi
  108. }
  109. should_be_patched() {
  110. local bin="$1"
  111. [ -x "$bin" ] || return 1
  112. case "$bin" in
  113. *.so|*.so.[0-9]*)
  114. return 1
  115. ;;
  116. *)
  117. file "$bin" | grep -sqE "ELF.*(executable|interpreter)" && return 0
  118. ;;
  119. esac
  120. return 1
  121. }
  122. for LDD in ${PATH//://ldd }/ldd; do
  123. "$LDD" --version >/dev/null 2>/dev/null && break
  124. LDD=""
  125. done
  126. [ -n "$LDD" -a -x "$LDD" ] || LDD=
  127. for BIN in "$@"; do
  128. [ -n "$BIN" -a -n "$DIR" ] || {
  129. echo "Usage: $0 <destdir> <executable> ..." >&2
  130. exit 1
  131. }
  132. [ ! -d "$DIR/lib" ] && {
  133. _md "$DIR/lib"
  134. _md "$DIR/usr"
  135. _ln "../lib" "$DIR/usr/lib"
  136. }
  137. [ ! -x "$DIR/lib/runas.so" ] && {
  138. _runas_so "$DIR/lib/runas.so"
  139. }
  140. LDSO=""
  141. [ -n "$LDD" ] && should_be_patched "$BIN" && {
  142. for token in $("$LDD" "$BIN" 2>/dev/null); do
  143. case "$token" in */*.so*)
  144. dest="$DIR/lib/${token##*/}"
  145. ddir="${dest%/*}"
  146. case "$token" in
  147. */ld-*.so*) LDSO="${token##*/}" ;;
  148. esac
  149. [ -f "$token" -a ! -f "$dest" ] && {
  150. _md "$ddir"
  151. _cp "$token" "$dest"
  152. case "$token" in
  153. */ld-*.so*) _patch_ldso "$dest" ;;
  154. */libc.so.6) _patch_glibc "$dest" ;;
  155. esac
  156. }
  157. ;; esac
  158. done
  159. }
  160. # is a dynamically linked executable
  161. if [ -n "$LDSO" ]; then
  162. echo "Bundling ${BIN##*/}"
  163. RUNDIR="$(readlink -f "$BIN")"; RUNDIR="${RUNDIR%/*}"
  164. RUN="${LDSO#ld-}"; RUN="run-${RUN%%.so*}.sh"
  165. REL="$(_relpath "$DIR/lib" "$BIN")"
  166. _mv "$BIN" "$RUNDIR/.${BIN##*/}.bin"
  167. cat <<-EOF > "$BIN"
  168. #!/usr/bin/env bash
  169. dir="\$(dirname "\$0")"
  170. export RUNAS_ARG0="\$0"
  171. export LD_PRELOAD="\${LD_PRELOAD:+\$LD_PRELOAD:}\$dir/${REL:+$REL/}runas.so"
  172. exec "\$dir/${REL:+$REL/}$LDSO" --library-path "\$dir/${REL:+$REL/}" "\$dir/.${BIN##*/}.bin" "\$@"
  173. EOF
  174. chmod ${VERBOSE:+-v} 0755 "$BIN"
  175. fi
  176. done