From b27ca6b989de81a42414333e1cf322242f35d6ab Mon Sep 17 00:00:00 2001 From: d4nn13 Date: Sun, 19 Apr 2026 19:56:34 +0200 Subject: [PATCH] rewrite CGI scripts in POSIX sh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Busybox httpd runs CGI under ash regardless of #!/bin/bash, so bash-only constructs — arrays (restart=()), [[ ]], =~, <<< — all broke at runtime with "syntax error: unexpected (" / "unexpected redirection" messages visible in the httpd log. Switched all four CGIs (status, config-get, config-save, logs) to #!/bin/sh with POSIX-only constructs: space-separated strings instead of arrays, [ ] instead of [[ ]], parameter expansion + awk instead of =~ regex, printf '%s\n' … | awk instead of herestrings. Co-Authored-By: Claude Opus 4.7 --- lxc/var/www/wirecon/cgi-bin/config-get.sh | 7 ++-- lxc/var/www/wirecon/cgi-bin/config-save.sh | 45 ++++++++++++---------- lxc/var/www/wirecon/cgi-bin/logs.sh | 32 ++++++++++----- lxc/var/www/wirecon/cgi-bin/status.sh | 17 ++++---- 4 files changed, 61 insertions(+), 40 deletions(-) diff --git a/lxc/var/www/wirecon/cgi-bin/config-get.sh b/lxc/var/www/wirecon/cgi-bin/config-get.sh index 6239085..af115e0 100755 --- a/lxc/var/www/wirecon/cgi-bin/config-get.sh +++ b/lxc/var/www/wirecon/cgi-bin/config-get.sh @@ -1,7 +1,8 @@ -#!/bin/bash +#!/bin/sh # CGI: return a config file's contents as text/plain. # file=wg0 → /etc/wireguard/wg0.conf # file=wirecon → /etc/wirecon/wirecon.conf +# POSIX-sh only (busybox httpd can run CGI via ash regardless of shebang). set -u file="" @@ -10,13 +11,13 @@ case "${QUERY_STRING:-}" in *file=wirecon*) file=/etc/wirecon/wirecon.conf ;; esac -if [[ -z "$file" ]]; then +if [ -z "$file" ]; then printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n' echo "bad file parameter" exit 0 fi -if [[ ! -r "$file" ]]; then +if [ ! -r "$file" ]; then printf 'Status: 404 Not Found\r\nContent-Type: text/plain\r\n\r\n' echo "not readable: $file" exit 0 diff --git a/lxc/var/www/wirecon/cgi-bin/config-save.sh b/lxc/var/www/wirecon/cgi-bin/config-save.sh index 893d5c4..2c1c539 100755 --- a/lxc/var/www/wirecon/cgi-bin/config-save.sh +++ b/lxc/var/www/wirecon/cgi-bin/config-save.sh @@ -1,22 +1,23 @@ -#!/bin/bash +#!/bin/sh # CGI: accept POST body as the new contents of a config file, write it -# atomically, then restart the services that depend on it. +# atomically, then restart the services that depend on it (in background). # Query: file=wg0|wirecon # Body: raw text +# POSIX-sh; no arrays, no [[ ]], no <<<. set -u -file=""; label=""; restart=() +file=""; label=""; restart=""; mode="" case "${QUERY_STRING:-}" in *file=wg0*) file=/etc/wireguard/wg0.conf - label="wg0.conf" - restart=(wg-quick.wg0 wirecon-watchdog wirecon-monitor) + label=wg0.conf + restart="wg-quick.wg0 wirecon-watchdog wirecon-monitor" mode=0600 ;; *file=wirecon*) file=/etc/wirecon/wirecon.conf - label="wirecon.conf" - restart=(wirecon-watchdog wirecon-monitor) + label=wirecon.conf + restart="wirecon-watchdog wirecon-monitor" mode=0644 ;; *) @@ -25,7 +26,7 @@ case "${QUERY_STRING:-}" in ;; esac -if [[ "${REQUEST_METHOD:-}" != "POST" ]]; then +if [ "${REQUEST_METHOD:-}" != "POST" ]; then printf 'Status: 405 Method Not Allowed\r\nContent-Type: text/plain\r\n\r\n' echo "POST required"; exit 0 fi @@ -35,14 +36,14 @@ len="${CONTENT_LENGTH:-0}" tmp=$(mktemp "${file}.XXXXXX") trap 'rm -f "$tmp"' EXIT -if [[ "$len" -gt 0 ]]; then +if [ "$len" -gt 0 ] 2>/dev/null; then head -c "$len" > "$tmp" else cat > "$tmp" fi # Refuse empty writes — likely a bug, not intent. -if [[ ! -s "$tmp" ]]; then +if [ ! -s "$tmp" ]; then printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n' echo "refusing empty body"; exit 0 fi @@ -51,29 +52,31 @@ chmod "$mode" "$tmp" mv -f "$tmp" "$file" trap - EXIT -# Return the save confirmation and kick off restarts in the background so -# the HTTP response doesn't depend on wg-quick / watchdog finishing. Their -# output is appended to /var/log/wirecon-restart.log (viewable in Logs tab -# or via tail -F). +# Return confirmation immediately and kick off restarts in background. If we +# restarted inline, wg-quick's down/up can outlast busybox httpd's CGI read +# window and the browser reports "Failed to fetch" even though the save +# succeeded. Output goes to /var/log/wirecon-restart.log (also viewable via +# logs.sh?service=restart). +bytes=$(wc -c < "$file" | tr -d ' ') printf 'Content-Type: text/plain\r\n\r\n' -echo "saved $file ($(wc -c < "$file") bytes)" +echo "saved $file ($bytes bytes)" echo "" echo "restarting in background:" -for svc in "${restart[@]}"; do echo " - $svc"; done +for svc in $restart; do echo " - $svc"; done echo "" echo "watch: tail -F /var/log/wirecon-restart.log" echo "or refresh the Status tab in a few seconds." -{ +( ts() { date '+%Y-%m-%dT%H:%M:%S%z'; } - echo "[$(ts)] config-save: $label ($(wc -c < "$file") bytes) → restarting ${restart[*]}" - for svc in "${restart[@]}"; do + echo "[$(ts)] config-save: $label ($bytes bytes) -> restarting $restart" + for svc in $restart; do echo "[$(ts)] --- restart $svc ---" if ! timeout 20 rc-service "$svc" restart 2>&1; then echo "[$(ts)] (restart of $svc returned non-zero or timed out)" fi done echo "[$(ts)] config-save: done" -} >/var/log/wirecon-restart.log 2>&1 & -disown || true +) >/var/log/wirecon-restart.log 2>&1 & + exit 0 diff --git a/lxc/var/www/wirecon/cgi-bin/logs.sh b/lxc/var/www/wirecon/cgi-bin/logs.sh index 566b01f..1ff0b37 100755 --- a/lxc/var/www/wirecon/cgi-bin/logs.sh +++ b/lxc/var/www/wirecon/cgi-bin/logs.sh @@ -1,6 +1,7 @@ -#!/bin/bash +#!/bin/sh # CGI: return the tail of a log file as text/plain. -# Query: service=watchdog|monitor lines=N (capped to 1000) +# Query: service=watchdog|monitor|restart lines=N (capped to 1000) +# POSIX-sh; no [[ =~ ]]. set -u service=""; lines=100 @@ -10,20 +11,33 @@ case "${QUERY_STRING:-}" in *service=restart*) service=restart ;; esac -# pull lines= from query string -if [[ "${QUERY_STRING:-}" =~ lines=([0-9]+) ]]; then - lines="${BASH_REMATCH[1]}" - (( lines > 1000 )) && lines=1000 -fi +# Pull lines=N out of the query string without bash regex. +# Strategy: if the string contains "lines=", take the substring after it and +# grab the leading digits. If the result is non-empty and a number, use it. +qs="${QUERY_STRING:-}" +case "$qs" in + *lines=*) + raw="${qs#*lines=}" # everything after the first lines= + raw="${raw%%&*}" # up to next & (if any) + # keep only leading digits + n=$(printf '%s' "$raw" | awk '{ + s=""; for(i=1;i<=length($0);i++){c=substr($0,i,1); if(c<"0"||c>"9") break; s=s c} print s + }') + if [ -n "$n" ]; then + lines=$n + if [ "$lines" -gt 1000 ] 2>/dev/null; then lines=1000; fi + fi + ;; +esac -if [[ -z "$service" ]]; then +if [ -z "$service" ]; then printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n' echo "service must be watchdog, monitor, or restart"; exit 0 fi file="/var/log/wirecon-${service}.log" printf 'Content-Type: text/plain\r\n\r\n' -if [[ -r "$file" ]]; then +if [ -r "$file" ]; then tail -n "$lines" "$file" else echo "(no log yet at $file)" diff --git a/lxc/var/www/wirecon/cgi-bin/status.sh b/lxc/var/www/wirecon/cgi-bin/status.sh index 0931e02..ffa0f43 100755 --- a/lxc/var/www/wirecon/cgi-bin/status.sh +++ b/lxc/var/www/wirecon/cgi-bin/status.sh @@ -1,5 +1,6 @@ -#!/bin/bash +#!/bin/sh # CGI: emit service + tunnel state as key=value lines. +# POSIX-sh; busybox httpd may run CGI via ash regardless of shebang. set -u printf 'Content-Type: text/plain\r\n\r\n' @@ -22,14 +23,16 @@ if ip link show "$IFACE" >/dev/null 2>&1; then # peer dump: pub psk endpoint allowed_ips last_handshake rx tx keepalive dump=$(wg show "$IFACE" dump 2>/dev/null | awk 'NR==2') - if [[ -n "$dump" ]]; then - endpoint=$(awk '{print $3}' <<<"$dump") - hs_ts=$(awk '{print $5}' <<<"$dump") - rx=$(awk '{print $6}' <<<"$dump") - tx=$(awk '{print $7}' <<<"$dump") + if [ -n "$dump" ]; then + endpoint=$(printf '%s\n' "$dump" | awk '{print $3}') + hs_ts=$(printf '%s\n' "$dump" | awk '{print $5}') + rx=$(printf '%s\n' "$dump" | awk '{print $6}') + tx=$(printf '%s\n' "$dump" | awk '{print $7}') now=$(date +%s) age=-1 - [[ "$hs_ts" != "0" && -n "$hs_ts" ]] && age=$((now - hs_ts)) + if [ -n "$hs_ts" ] && [ "$hs_ts" != "0" ]; then + age=$((now - hs_ts)) + fi printf 'wg0.endpoint=%s\n' "${endpoint:-none}" printf 'wg0.handshake_age_s=%s\n' "$age" printf 'wg0.rx_bytes=%s\n' "${rx:-0}"