b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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 | |
| 21 | DIR="$1"; shift |
| 22 | |
| 23 | _cp() { |
| 24 | cp ${VERBOSE:+-v} -L "$1" "$2" || { |
| 25 | echo "cp($1 $2) failed" >&2 |
| 26 | exit 1 |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | _mv() { |
| 31 | mv ${VERBOSE:+-v} "$1" "$2" || { |
| 32 | echo "mv($1 $2) failed" >&2 |
| 33 | exit 1 |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | _md() { |
| 38 | mkdir ${VERBOSE:+-v} -p "$1" || { |
| 39 | echo "mkdir($1) failed" >&2 |
| 40 | exit 2 |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | _ln() { |
| 45 | ln ${VERBOSE:+-v} -sf "$1" "$2" || { |
| 46 | echo "ln($1 $2) failed" >&2 |
| 47 | exit 3 |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | _relpath() { |
| 52 | local base="$(readlink -f "$1")" |
| 53 | local dest="$(readlink -f "$2")" |
| 54 | local up |
| 55 | |
| 56 | [ -d "$base" ] || base="${base%/*}" |
| 57 | [ -d "$dest" ] || dest="${dest%/*}" |
| 58 | |
| 59 | while true; do |
| 60 | case "$base" |
| 61 | in "$dest"/*) |
| 62 | echo "$up/${base#$dest/}" |
| 63 | break |
| 64 | ;; |
| 65 | *) |
| 66 | dest="${dest%/*}" |
| 67 | up="${up:+$up/}.." |
| 68 | ;; |
| 69 | esac |
| 70 | done |
| 71 | } |
| 72 | |
| 73 | _runas_so() { |
| 74 | cat <<-EOT | ${CC:-gcc} -x c -fPIC -shared -o "$1" - |
| 75 | #include <unistd.h> |
| 76 | #include <stdio.h> |
| 77 | #include <stdlib.h> |
| 78 | |
| 79 | int mangle_arg0(int argc, char **argv, char **env) { |
| 80 | char *arg0 = getenv("RUNAS_ARG0"); |
| 81 | |
| 82 | if (arg0) { |
| 83 | argv[0] = arg0; |
| 84 | unsetenv("RUNAS_ARG0"); |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | #ifdef __APPLE__ |
| 91 | __attribute__((section("__DATA,__mod_init_func"))) |
| 92 | #else |
| 93 | __attribute__((section(".init_array"))) |
| 94 | #endif |
| 95 | static void *mangle_arg0_constructor = &mangle_arg0; |
| 96 | EOT |
| 97 | |
| 98 | [ -x "$1" ] || { |
| 99 | echo "compiling preload library failed" >&2 |
| 100 | exit 5 |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | _patch_ldso() { |
| 105 | _cp "$1" "$1.patched" |
| 106 | sed -i -e 's,/\(usr\|lib\|etc\)/,/###/,g' "$1.patched" |
| 107 | |
| 108 | if "$1.patched" 2>&1 | grep -q -- --library-path; then |
| 109 | _mv "$1.patched" "$1" |
| 110 | else |
| 111 | echo "binary patched ${1##*/} not executable, using original" >&2 |
| 112 | rm -f "$1.patched" |
| 113 | fi |
| 114 | } |
| 115 | |
| 116 | _patch_glibc() { |
| 117 | _cp "$1" "$1.patched" |
| 118 | sed -i -e 's,/usr/\(\(lib\|share\)/locale\),/###/\1,g' "$1.patched" |
| 119 | |
| 120 | if "$1.patched" 2>&1 | grep -q -- GNU; then |
| 121 | _mv "$1.patched" "$1" |
| 122 | else |
| 123 | echo "binary patched ${1##*/} not executable, using original" >&2 |
| 124 | rm -f "$1.patched" |
| 125 | fi |
| 126 | } |
| 127 | |
| 128 | should_be_patched() { |
| 129 | local bin="$1" |
| 130 | |
| 131 | [ -x "$bin" ] || return 1 |
| 132 | |
| 133 | case "$bin" in |
| 134 | *.so|*.so.[0-9]*) |
| 135 | return 1 |
| 136 | ;; |
| 137 | *) |
| 138 | file "$bin" | grep -sqE "ELF.*(executable|interpreter)" && return 0 |
| 139 | ;; |
| 140 | esac |
| 141 | |
| 142 | return 1 |
| 143 | } |
| 144 | |
| 145 | for LDD in ${PATH//://ldd }/ldd; do |
| 146 | "$LDD" --version >/dev/null 2>/dev/null && break |
| 147 | LDD="" |
| 148 | done |
| 149 | |
| 150 | [ -n "$LDD" -a -x "$LDD" ] || LDD= |
| 151 | |
| 152 | for BIN in "$@"; do |
| 153 | [ -n "$BIN" -a -n "$DIR" ] || { |
| 154 | echo "Usage: $0 <destdir> <executable> ..." >&2 |
| 155 | exit 1 |
| 156 | } |
| 157 | |
| 158 | [ ! -d "$DIR/lib" ] && { |
| 159 | _md "$DIR/lib" |
| 160 | _md "$DIR/usr" |
| 161 | _ln "../lib" "$DIR/usr/lib" |
| 162 | } |
| 163 | |
| 164 | [ ! -x "$DIR/lib/runas.so" ] && { |
| 165 | _runas_so "$DIR/lib/runas.so" |
| 166 | } |
| 167 | |
| 168 | LDSO="" |
| 169 | |
| 170 | [ -n "$LDD" ] && should_be_patched "$BIN" && { |
| 171 | for token in $("$LDD" "$BIN" 2>/dev/null); do |
| 172 | case "$token" in */*.so*) |
| 173 | dest="$DIR/lib/${token##*/}" |
| 174 | ddir="${dest%/*}" |
| 175 | |
| 176 | case "$token" in |
| 177 | */ld-*.so*) LDSO="${token##*/}" ;; |
| 178 | esac |
| 179 | |
| 180 | [ -f "$token" -a ! -f "$dest" ] && { |
| 181 | _md "$ddir" |
| 182 | _cp "$token" "$dest" |
| 183 | case "$token" in |
| 184 | */ld-*.so*) _patch_ldso "$dest" ;; |
| 185 | */libc.so.6) _patch_glibc "$dest" ;; |
| 186 | esac |
| 187 | } |
| 188 | ;; esac |
| 189 | done |
| 190 | } |
| 191 | |
| 192 | # is a dynamically linked executable |
| 193 | if [ -n "$LDSO" ]; then |
| 194 | echo "Bundling ${BIN##*/}" |
| 195 | |
| 196 | RUNDIR="$(readlink -f "$BIN")"; RUNDIR="${RUNDIR%/*}" |
| 197 | RUN="${LDSO#ld-}"; RUN="run-${RUN%%.so*}.sh" |
| 198 | REL="$(_relpath "$DIR/lib" "$BIN")" |
| 199 | |
| 200 | _mv "$BIN" "$RUNDIR/.${BIN##*/}.bin" |
| 201 | |
| 202 | cat <<-EOF > "$BIN" |
| 203 | #!/usr/bin/env bash |
| 204 | dir="\$(dirname "\$0")" |
| 205 | export RUNAS_ARG0="\$0" |
| 206 | export LD_PRELOAD="\${LD_PRELOAD:+\$LD_PRELOAD:}\$dir/${REL:+$REL/}runas.so" |
| 207 | exec "\$dir/${REL:+$REL/}$LDSO" --library-path "\$dir/${REL:+$REL/}" "\$dir/.${BIN##*/}.bin" "\$@" |
| 208 | EOF |
| 209 | |
| 210 | chmod ${VERBOSE:+-v} 0755 "$BIN" |
| 211 | fi |
| 212 | done |