@@ -1688,25 +1688,81 @@ function resolveWebPanelEnvFile() {
16881688 printf '%s\n' "${env_file} "
16891689}
16901690
1691- # Resolve the web panel's active config directory without sourcing its env file.
1692- function resolveWebPanelConfigDir() {
1693- local env_file configured_dir
1691+ # Read one web-panel setting without evaluating shell syntax. EnvironmentFile
1692+ # values override inline Environment= values, matching systemd semantics.
1693+ function readWebPanelSetting() {
1694+ local setting_name="$1 "
1695+ local env_file inline_assignment value="" use_env_file=1
16941696 env_file="$(resolveWebPanelEnvFile)" || return 1
1697+ if [[ -f "${WEB_PANEL_SYSTEMD_UNIT} " ]] && \
1698+ ! grep -q '^[[:space:]]*EnvironmentFile=' "${WEB_PANEL_SYSTEMD_UNIT} " 2 >/ dev/ null; then
1699+ use_env_file=0
1700+ fi
16951701
1696- if [[ -f "${env_file} " ]]; then
1697- configured_dir="$(sed -n 's/^AWG_CONFIG_DIR=//p' "${env_file} " 2 >/ dev/ null | tail - n 1 )"
1698- configured_dir="${configured_dir# \" } "
1699- configured_dir="${configured_dir% \" } "
1700- configured_dir="${configured_dir# \' } "
1701- configured_dir="${configured_dir% \' } "
1702- if [[ -n "${configured_dir} " ]]; then
1703- if [[ "${configured_dir} " != /* || "${configured_dir} " =~ [[: space: ][: cntrl: ]] ]]; then
1704- echo "ERROR: refusing unsafe AWG_CONFIG_DIR '${configured_dir} '" >& 2
1702+ if [[ "${use_env_file} " - eq 1 && - f "${env_file} " ]] && \
1703+ grep -q "^[[:space:]]*${setting_name} = " "${env_file} " 2 >/ dev/ null; then
1704+ value="$(sed -n "s/^[[:space:]]*${setting_name} =// p" "${env_file} " 2 >/ dev/ null | tail - n 1 )"
1705+ else
1706+ while IFS= read -r inline_assignment; do
1707+ if [[ "${inline_assignment} " != * "${setting_name} = "* ]]; then
1708+ continue
1709+ fi
1710+ inline_assignment="${inline_assignment# \" } "
1711+ inline_assignment="${inline_assignment% \" } "
1712+ inline_assignment="${inline_assignment# \' } "
1713+ inline_assignment="${inline_assignment% \' } "
1714+ if [[ "${inline_assignment} " != "${setting_name} = "* ]]; then
1715+ echo "ERROR: unsupported inline ${setting_name} assignment in '${WEB_PANEL_SYSTEMD_UNIT} '" >& 2
17051716 return 1
17061717 fi
1707- printf '%s\n' "${configured_dir%/ } "
1708- return 0
1718+ value="${inline_assignment#* =} "
1719+ done < <(sed -n 's/^[[:space:]]*Environment=//p' "${WEB_PANEL_SYSTEMD_UNIT} " 2 >/ dev/ null)
1720+ fi
1721+
1722+ value="${value# \" } "
1723+ value="${value% \" } "
1724+ value="${value# \' } "
1725+ value="${value% \' } "
1726+ printf '%s\n' "${value} "
1727+ }
1728+
1729+ # Resolve the service working directory used for relative database paths.
1730+ function resolveWebPanelWorkingDirectory() {
1731+ local working_dir=""
1732+ if [[ -f "${WEB_PANEL_SYSTEMD_UNIT} " ]]; then
1733+ working_dir="$(sed -n 's/^[[:space:]]*WorkingDirectory=//p' "${WEB_PANEL_SYSTEMD_UNIT} " 2 >/ dev/ null | tail - n 1 )"
1734+ working_dir="${working_dir# -} "
1735+ working_dir="${working_dir# \" } "
1736+ working_dir="${working_dir% \" } "
1737+ working_dir="${working_dir# \' } "
1738+ working_dir="${working_dir% \' } "
1739+ working_dir="${working_dir:-/ } "
1740+ else
1741+ working_dir="${WEB_PANEL_DATA_DIR} "
1742+ fi
1743+
1744+ if [[ "${working_dir} " != /* || "${working_dir} " =~ [[: space: ][: cntrl: ]] ]]; then
1745+ echo "ERROR: refusing unsafe web panel WorkingDirectory '${working_dir} '" >& 2
1746+ return 1
1747+ fi
1748+ if [[ "${working_dir} " != "/ " ]]; then
1749+ working_dir="${working_dir%/ } "
1750+ fi
1751+ printf '%s\n' "${working_dir} "
1752+ }
1753+
1754+ # Resolve the web panel's active config directory without sourcing its env file.
1755+ function resolveWebPanelConfigDir() {
1756+ local configured_dir
1757+ configured_dir="$(readWebPanelSetting AWG_CONFIG_DIR)" || return 1
1758+
1759+ if [[ -n "${configured_dir} " ]]; then
1760+ if [[ "${configured_dir} " != /* || "${configured_dir} " =~ [[: space: ][: cntrl: ]] ]]; then
1761+ echo "ERROR: refusing unsafe AWG_CONFIG_DIR '${configured_dir} '" >& 2
1762+ return 1
17091763 fi
1764+ printf '%s\n' "${configured_dir%/ } "
1765+ return 0
17101766 fi
17111767
17121768 printf '%s\n' "${WEB_PANEL_CONFIG_DIR%/ } "
@@ -1717,28 +1773,42 @@ function resolveWebPanelConfigDir() {
17171773# client has been created yet. Standalone installer use (no panel env file)
17181774# retains the config-directory fallback.
17191775function resolveClientLifecycleLockDir() {
1720- local env_file database_path
1776+ local env_file database_path working_dir database_parent
17211777 env_file="$(resolveWebPanelEnvFile)" || return 1
17221778
1723- if [[ -f "${env_file} " ]]; then
1724- database_path="$(sed -n 's/^AWG_WEB_DB=//p' "${env_file} " 2 >/ dev/ null | tail - n 1 )"
1725- database_path="${database_path# \" } "
1726- database_path="${database_path% \" } "
1727- database_path="${database_path# \' } "
1728- database_path="${database_path% \' } "
1729- if [[ -n "${database_path} " ]]; then
1730- if [[ "${database_path} " != /* || "${database_path} " =~ [[: space: ][: cntrl: ]] ]]; then
1731- echo "ERROR: refusing unsafe AWG_WEB_DB '${database_path} '" >& 2
1732- return 1
1733- fi
1734- dirname -- "${database_path} "
1735- return 0
1736- fi
1737- printf '%s\n' "${WEB_PANEL_DATA_DIR%/ } "
1738- return 0
1779+ if [[ ! -f "${env_file} " && ! - e "${WEB_PANEL_SYSTEMD_UNIT} " ]]; then
1780+ resolveWebPanelConfigDir
1781+ return
17391782 fi
17401783
1741- resolveWebPanelConfigDir
1784+ database_path="$(readWebPanelSetting AWG_WEB_DB)" || return 1
1785+ database_path="${database_path:- awg-web.db} "
1786+ if [[ "${database_path} " =~ [[: space: ][: cntrl: ]] ]]; then
1787+ echo "ERROR: refusing unsafe AWG_WEB_DB '${database_path} '" >& 2
1788+ return 1
1789+ fi
1790+ case "${database_path} " in
1791+ sqlite://*) database_path="${database_path# sqlite:// } " ;;
1792+ sqlite:*) database_path="${database_path# sqlite: } " ;;
1793+ esac
1794+ database_path="${database_path%% \? * } "
1795+ working_dir="$(resolveWebPanelWorkingDirectory)" || return 1
1796+
1797+ if [[ -z "${database_path} " || "${database_path} " == ": memory: " ]]; then
1798+ printf '%s\n' "${working_dir} "
1799+ return 0
1800+ fi
1801+ database_parent="$(dirname -- "${database_path} ")"
1802+ if [[ "${database_path} " == /* ]]; then
1803+ if [[ "${database_parent} " != "/ " ]]; then
1804+ database_parent="${database_parent%/ } "
1805+ fi
1806+ printf '%s\n' "${database_parent} "
1807+ elif [[ "${database_parent} " == "." ]]; then
1808+ printf '%s\n' "${working_dir} "
1809+ else
1810+ printf '%s/%s\n' "${working_dir%/ } " "${database_parent} "
1811+ fi
17421812}
17431813
17441814# Copy a client config file to the web panel config directory so the panel
0 commit comments