| 1 |
#!/usr/bin/env bash |
| 2 |
set -Eeuo pipefail |
| 3 |
|
| 4 |
possibles=( nobody daemon irc www-data ) |
| 5 |
if [ -n "${AUTOPKGTEST_NORMAL_USER:-}" ]; then |
| 6 |
possibles=( "$AUTOPKGTEST_NORMAL_USER" "${possibles[@]}" ) |
| 7 |
fi |
| 8 |
if self="$(id -un 2>/dev/null)"; then |
| 9 |
# try "self" last because that's the least interesting test (root becoming root) |
| 10 |
possibles+=( "$self" ) |
| 11 |
fi |
| 12 |
|
| 13 |
gosuAs= |
| 14 |
expect= |
| 15 |
for user in "${possibles[@]}"; do |
| 16 |
if grep -qP "^\Q${user}\E:" /etc/passwd && expect="$(id "$user" 2> /dev/null)"; then |
| 17 |
gosuAs="$user" |
| 18 |
break |
| 19 |
fi |
| 20 |
done |
| 21 |
|
| 22 |
if [ -z "$gosuAs" ]; then |
| 23 |
cat >&2 <<-EOF |
| 24 |
|
| 25 |
error: AUTOPKGTEST_NORMAL_USER not set (or not usable) and could not find a suitable alternative user |
| 26 |
tried: ${possibles[*]} |
| 27 |
|
| 28 |
EOF |
| 29 |
id "${possibles[@]}" >&2 || : |
| 30 |
exit 1 |
| 31 |
fi |
| 32 |
|
| 33 |
actual="$(gosu "$gosuAs" id)" |
| 34 |
|
| 35 |
if [ "$actual" != "$expect" ]; then |
| 36 |
cat >&2 <<-EOF |
| 37 |
|
| 38 |
error: unexpected output ("gosu $gosuAs id"): |
| 39 |
actual: $actual |
| 40 |
expect: $expect |
| 41 |
|
| 42 |
EOF |
| 43 |
exit 1 |
| 44 |
fi |