Skip to content

Commit e6ecc5e

Browse files
committed
fix: support custom web environment paths
1 parent 779e759 commit e6ecc5e

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

amneziawg-web/scripts/amneziawg-web-privileged

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,17 +477,22 @@ resolve_awg_install_script() {
477477
require_executable "${STAT_BIN}"
478478
validate_root_owned_file "${WEB_SYSTEMD_UNIT}" false
479479
while IFS= read -r unit_line || [[ -n "${unit_line}" ]]; do
480-
if [[ "${unit_line}" =~ ^EnvironmentFile=-?(/etc/[A-Za-z0-9_./-]+)$ ]]; then
481-
environment_file="${BASH_REMATCH[1]}"
480+
if [[ "${unit_line}" == EnvironmentFile=* ]]; then
481+
if [[ "${unit_line}" =~ ^EnvironmentFile=-?(/[A-Za-z0-9_./-]+)$ ]]; then
482+
environment_file="${BASH_REMATCH[1]}"
483+
else
484+
fail "service unit contains an unsupported environment-file path"
485+
fi
482486
fi
483487
done < "${WEB_SYSTEMD_UNIT}"
484488
if [[ -n "${environment_file}" ]]; then
485489
if [[ "${environment_file}" == *..* || "${environment_file}" == *//* ]]; then
486490
fail "service unit contains an unsafe environment-file path"
487491
fi
488492
web_config_root="${environment_file%/*}"
493+
[[ -n "${web_config_root}" ]] || web_config_root="/"
489494
fi
490-
marker_path="${web_config_root}/installed-awg-script.path"
495+
marker_path="${web_config_root%/}/installed-awg-script.path"
491496

492497
validate_root_owned_directory "${web_config_root}"
493498
validate_root_owned_file "${marker_path}" false

tests/test-install-mock.sh

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,10 @@ if [[ -x "${PRIVILEGED_HELPER}" ]]; then
22742274
HELPER_INSTALL_SCRIPT="$(head -n 1 "${WEB_AWG_SCRIPT_MARKER}")"
22752275
HELPER_SCRIPT_BACKUP="/tmp/amneziawg-helper-script-backup"
22762276
HELPER_PROTOCOL_CALLS="/tmp/amneziawg-helper-protocol-calls"
2277+
HELPER_UNIT_BACKUP="/tmp/amneziawg-helper-unit-backup"
2278+
HELPER_CUSTOM_ENV_ROOT="/opt/amneziawg-web-helper-test"
22772279
cp -p "${HELPER_INSTALL_SCRIPT}" "${HELPER_SCRIPT_BACKUP}"
2280+
cp -p /etc/systemd/system/amneziawg-web.service "${HELPER_UNIT_BACKUP}"
22782281
printf '#!/bin/bash\nprintf "%%s\\n" "$1" >> %s\n' \
22792282
"${HELPER_PROTOCOL_CALLS}" > "${HELPER_INSTALL_SCRIPT}"
22802283
chmod 0755 "${HELPER_INSTALL_SCRIPT}"
@@ -2287,8 +2290,41 @@ if [[ -x "${PRIVILEGED_HELPER}" ]]; then
22872290
[[ "$(sed -n '2p' "${HELPER_PROTOCOL_CALLS}")" == "--disable-awg3" ]]; then
22882291
HELPER_PROTOCOL_DISPATCH_OK=1
22892292
fi
2293+
2294+
# The installer supports root-controlled custom --env-file locations. The
2295+
# privileged helper must find the ownership marker beside that effective
2296+
# EnvironmentFile without weakening its directory/file checks.
2297+
rm -rf "${HELPER_CUSTOM_ENV_ROOT}"
2298+
install -d -m 0700 -o root -g root "${HELPER_CUSTOM_ENV_ROOT}"
2299+
install -m 0600 -o root -g root "${WEB_TEST_ENV_FILE}" \
2300+
"${HELPER_CUSTOM_ENV_ROOT}/env.conf"
2301+
install -m 0600 -o root -g root "${WEB_AWG_SCRIPT_MARKER}" \
2302+
"${HELPER_CUSTOM_ENV_ROOT}/installed-awg-script.path"
2303+
sed -i "s#^EnvironmentFile=.*#EnvironmentFile=${HELPER_CUSTOM_ENV_ROOT}/env.conf#" \
2304+
/etc/systemd/system/amneziawg-web.service
2305+
HELPER_CUSTOM_ENV_OK=0
2306+
if grep -Fqx "EnvironmentFile=${HELPER_CUSTOM_ENV_ROOT}/env.conf" \
2307+
/etc/systemd/system/amneziawg-web.service && \
2308+
"${PRIVILEGED_HELPER}" enable-awg3 && \
2309+
[[ "$(sed -n '3p' "${HELPER_PROTOCOL_CALLS}")" == "--enable-awg3" ]]; then
2310+
HELPER_CUSTOM_ENV_OK=1
2311+
fi
2312+
chmod 0777 "${HELPER_CUSTOM_ENV_ROOT}"
2313+
if "${PRIVILEGED_HELPER}" disable-awg3 >/dev/null 2>&1; then
2314+
HELPER_CUSTOM_ENV_OK=0
2315+
fi
2316+
chmod 0700 "${HELPER_CUSTOM_ENV_ROOT}"
2317+
cp -p "${HELPER_UNIT_BACKUP}" /etc/systemd/system/amneziawg-web.service
2318+
rm -rf "${HELPER_CUSTOM_ENV_ROOT}"
2319+
if [[ "${HELPER_CUSTOM_ENV_OK}" -eq 1 ]]; then
2320+
echo "OK: Privileged helper supports secure custom EnvironmentFile directories"
2321+
else
2322+
echo "FAIL: Privileged helper did not safely support a custom EnvironmentFile directory"
2323+
FAILED=$((FAILED + 1))
2324+
fi
2325+
22902326
cp -p "${HELPER_SCRIPT_BACKUP}" "${HELPER_INSTALL_SCRIPT}"
2291-
rm -f "${HELPER_SCRIPT_BACKUP}" "${HELPER_PROTOCOL_CALLS}"
2327+
rm -f "${HELPER_SCRIPT_BACKUP}" "${HELPER_PROTOCOL_CALLS}" "${HELPER_UNIT_BACKUP}"
22922328
if [[ "${HELPER_PROTOCOL_DISPATCH_OK}" -eq 1 ]]; then
22932329
echo "OK: Privileged helper dispatches only fixed protocol migration flags"
22942330
else

0 commit comments

Comments
 (0)