1
0

bundle-libraries.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. for LDD in ${PATH//://ldd }/ldd; do
  110. "$LDD" --version >/dev/null 2>/dev/null && break
  111. LDD=""
  112. done
  113. [ -n "$LDD" -a -x "$LDD" ] || LDD=
  114. for BIN in "$@"; do
  115. [ -n "$BIN" -a -n "$DIR" ] || {
  116. echo "Usage: $0 <destdir> <executable> ..." >&2
  117. exit 1
  118. }
  119. [ ! -d "$DIR/lib" ] && {
  120. _md "$DIR/lib"
  121. _md "$DIR/usr"
  122. _ln "../lib" "$DIR/usr/lib"
  123. }
  124. [ ! -x "$DIR/lib/runas.so" ] && {
  125. _runas_so "$DIR/lib/runas.so"
  126. }
  127. LDSO=""
  128. [ -n "$LDD" ] && [ -x "$BIN" ] && file "$BIN" | grep -sqE "ELF.*(executable|interpreter)" && {
  129. for token in $("$LDD" "$BIN" 2>/dev/null); do
  130. case "$token" in */*.so*)
  131. dest="$DIR/lib/${token##*/}"
  132. ddir="${dest%/*}"
  133. case "$token" in
  134. */ld-*.so*) LDSO="${token##*/}" ;;
  135. esac
  136. [ -f "$token" -a ! -f "$dest" ] && {
  137. _md "$ddir"
  138. _cp "$token" "$dest"
  139. case "$token" in
  140. */ld-*.so*) _patch_ldso "$dest" ;;
  141. */libc.so.6) _patch_glibc "$dest" ;;
  142. esac
  143. }
  144. ;; esac
  145. done
  146. }
  147. # is a dynamically linked executable
  148. if [ -n "$LDSO" ]; then
  149. echo "Bundling ${BIN##*/}"
  150. RUNDIR="$(readlink -f "$BIN")"; RUNDIR="${RUNDIR%/*}"
  151. RUN="${LDSO#ld-}"; RUN="run-${RUN%%.so*}.sh"
  152. REL="$(_relpath "$DIR/lib" "$BIN")"
  153. _mv "$BIN" "$RUNDIR/.${BIN##*/}.bin"
  154. cat <<-EOF > "$BIN"
  155. #!/usr/bin/env bash
  156. dir="\$(dirname "\$0")"
  157. export RUNAS_ARG0="\$0"
  158. export LD_PRELOAD="\$dir/${REL:+$REL/}runas.so"
  159. exec "\$dir/${REL:+$REL/}$LDSO" --library-path "\$dir/${REL:+$REL/}" "\$dir/.${BIN##*/}.bin" "\$@"
  160. EOF
  161. chmod ${VERBOSE:+-v} 0755 "$BIN"
  162. fi
  163. done