#!/usr/bin/env bash set -euo pipefail script_dir=$(dirname "${BASH_SOURCE[0]}") guard=$(mktemp) output=$(mktemp) trap 'rm -f "$guard" "$output"' EXIT awk ' /^if \[\[ \$EUID -ne 0 \]\]; then$/ { capture = 1 } capture { print } capture && /^fi$/ { found = 1; exit } END { if (!found) exit 1 } ' "$script_dir/audit-host.sh" | sed 's/\$EUID/\$TEST_EUID/g' >"$guard" || { echo 'Could not extract the host audit root guard.' >&2 exit 1 } if ! grep -Fq 'if [[ $TEST_EUID -ne 0 ]]; then' "$guard"; then echo 'Extracted guard does not use the simulated effective UID.' >&2 exit 1 fi if TEST_EUID=0 bash "$guard" >"$output" 2>&1; then [[ ! -s $output ]] || { cat "$output" >&2; exit 1; } else echo 'Host audit root guard rejected simulated UID 0.' >&2 exit 1 fi if TEST_EUID=65534 bash "$guard" >"$output" 2>&1; then echo 'Host audit root guard accepted simulated non-root UID.' >&2 exit 1 fi grep -qx 'Run this read-only audit as root.' "$output" echo 'PASS host audit root guard accepts UID 0 and rejects non-root UIDs'