rewrite CGI scripts in POSIX sh
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
e9256e6389
commit
b27ca6b989
4 files changed
+61
-40
No files matched your search
@@ -1,7 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
# CGI: return a config file's contents as text/plain.
|
# CGI: return a config file's contents as text/plain.
|
||||||
# file=wg0 → /etc/wireguard/wg0.conf
|
# file=wg0 → /etc/wireguard/wg0.conf
|
||||||
# file=wirecon → /etc/wirecon/wirecon.conf
|
# file=wirecon → /etc/wirecon/wirecon.conf
|
||||||
|
# POSIX-sh only (busybox httpd can run CGI via ash regardless of shebang).
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
file=""
|
file=""
|
||||||
@@ -10,13 +11,13 @@ case "${QUERY_STRING:-}" in
|
|||||||
*file=wirecon*) file=/etc/wirecon/wirecon.conf ;;
|
*file=wirecon*) file=/etc/wirecon/wirecon.conf ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
if [[ -z "$file" ]]; then
|
if [ -z "$file" ]; then
|
||||||
printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n'
|
printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n'
|
||||||
echo "bad file parameter"
|
echo "bad file parameter"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! -r "$file" ]]; then
|
if [ ! -r "$file" ]; then
|
||||||
printf 'Status: 404 Not Found\r\nContent-Type: text/plain\r\n\r\n'
|
printf 'Status: 404 Not Found\r\nContent-Type: text/plain\r\n\r\n'
|
||||||
echo "not readable: $file"
|
echo "not readable: $file"
|
||||||
exit 0
|
exit 0
|
||||||
|
|||||||
@@ -1,22 +1,23 @@
|
|||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
# CGI: accept POST body as the new contents of a config file, write it
|
# 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
|
# Query: file=wg0|wirecon
|
||||||
# Body: raw text
|
# Body: raw text
|
||||||
|
# POSIX-sh; no arrays, no [[ ]], no <<<.
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
file=""; label=""; restart=()
|
file=""; label=""; restart=""; mode=""
|
||||||
case "${QUERY_STRING:-}" in
|
case "${QUERY_STRING:-}" in
|
||||||
*file=wg0*)
|
*file=wg0*)
|
||||||
file=/etc/wireguard/wg0.conf
|
file=/etc/wireguard/wg0.conf
|
||||||
label="wg0.conf"
|
label=wg0.conf
|
||||||
restart=(wg-quick.wg0 wirecon-watchdog wirecon-monitor)
|
restart="wg-quick.wg0 wirecon-watchdog wirecon-monitor"
|
||||||
mode=0600
|
mode=0600
|
||||||
;;
|
;;
|
||||||
*file=wirecon*)
|
*file=wirecon*)
|
||||||
file=/etc/wirecon/wirecon.conf
|
file=/etc/wirecon/wirecon.conf
|
||||||
label="wirecon.conf"
|
label=wirecon.conf
|
||||||
restart=(wirecon-watchdog wirecon-monitor)
|
restart="wirecon-watchdog wirecon-monitor"
|
||||||
mode=0644
|
mode=0644
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
@@ -25,7 +26,7 @@ case "${QUERY_STRING:-}" in
|
|||||||
;;
|
;;
|
||||||
esac
|
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'
|
printf 'Status: 405 Method Not Allowed\r\nContent-Type: text/plain\r\n\r\n'
|
||||||
echo "POST required"; exit 0
|
echo "POST required"; exit 0
|
||||||
fi
|
fi
|
||||||
@@ -35,14 +36,14 @@ len="${CONTENT_LENGTH:-0}"
|
|||||||
tmp=$(mktemp "${file}.XXXXXX")
|
tmp=$(mktemp "${file}.XXXXXX")
|
||||||
trap 'rm -f "$tmp"' EXIT
|
trap 'rm -f "$tmp"' EXIT
|
||||||
|
|
||||||
if [[ "$len" -gt 0 ]]; then
|
if [ "$len" -gt 0 ] 2>/dev/null; then
|
||||||
head -c "$len" > "$tmp"
|
head -c "$len" > "$tmp"
|
||||||
else
|
else
|
||||||
cat > "$tmp"
|
cat > "$tmp"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Refuse empty writes — likely a bug, not intent.
|
# 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'
|
printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n'
|
||||||
echo "refusing empty body"; exit 0
|
echo "refusing empty body"; exit 0
|
||||||
fi
|
fi
|
||||||
@@ -51,29 +52,31 @@ chmod "$mode" "$tmp"
|
|||||||
mv -f "$tmp" "$file"
|
mv -f "$tmp" "$file"
|
||||||
trap - EXIT
|
trap - EXIT
|
||||||
|
|
||||||
# Return the save confirmation and kick off restarts in the background so
|
# Return confirmation immediately and kick off restarts in background. If we
|
||||||
# the HTTP response doesn't depend on wg-quick / watchdog finishing. Their
|
# restarted inline, wg-quick's down/up can outlast busybox httpd's CGI read
|
||||||
# output is appended to /var/log/wirecon-restart.log (viewable in Logs tab
|
# window and the browser reports "Failed to fetch" even though the save
|
||||||
# or via tail -F).
|
# 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'
|
printf 'Content-Type: text/plain\r\n\r\n'
|
||||||
echo "saved $file ($(wc -c < "$file") bytes)"
|
echo "saved $file ($bytes bytes)"
|
||||||
echo ""
|
echo ""
|
||||||
echo "restarting in background:"
|
echo "restarting in background:"
|
||||||
for svc in "${restart[@]}"; do echo " - $svc"; done
|
for svc in $restart; do echo " - $svc"; done
|
||||||
echo ""
|
echo ""
|
||||||
echo "watch: tail -F /var/log/wirecon-restart.log"
|
echo "watch: tail -F /var/log/wirecon-restart.log"
|
||||||
echo "or refresh the Status tab in a few seconds."
|
echo "or refresh the Status tab in a few seconds."
|
||||||
|
|
||||||
{
|
(
|
||||||
ts() { date '+%Y-%m-%dT%H:%M:%S%z'; }
|
ts() { date '+%Y-%m-%dT%H:%M:%S%z'; }
|
||||||
echo "[$(ts)] config-save: $label ($(wc -c < "$file") bytes) → restarting ${restart[*]}"
|
echo "[$(ts)] config-save: $label ($bytes bytes) -> restarting $restart"
|
||||||
for svc in "${restart[@]}"; do
|
for svc in $restart; do
|
||||||
echo "[$(ts)] --- restart $svc ---"
|
echo "[$(ts)] --- restart $svc ---"
|
||||||
if ! timeout 20 rc-service "$svc" restart 2>&1; then
|
if ! timeout 20 rc-service "$svc" restart 2>&1; then
|
||||||
echo "[$(ts)] (restart of $svc returned non-zero or timed out)"
|
echo "[$(ts)] (restart of $svc returned non-zero or timed out)"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
echo "[$(ts)] config-save: done"
|
echo "[$(ts)] config-save: done"
|
||||||
} </dev/null >>/var/log/wirecon-restart.log 2>&1 &
|
) </dev/null >>/var/log/wirecon-restart.log 2>&1 &
|
||||||
disown || true
|
|
||||||
exit 0
|
exit 0
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
# CGI: return the tail of a log file as text/plain.
|
# 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
|
set -u
|
||||||
|
|
||||||
service=""; lines=100
|
service=""; lines=100
|
||||||
@@ -10,20 +11,33 @@ case "${QUERY_STRING:-}" in
|
|||||||
*service=restart*) service=restart ;;
|
*service=restart*) service=restart ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# pull lines= from query string
|
# Pull lines=N out of the query string without bash regex.
|
||||||
if [[ "${QUERY_STRING:-}" =~ lines=([0-9]+) ]]; then
|
# Strategy: if the string contains "lines=", take the substring after it and
|
||||||
lines="${BASH_REMATCH[1]}"
|
# grab the leading digits. If the result is non-empty and a number, use it.
|
||||||
(( lines > 1000 )) && lines=1000
|
qs="${QUERY_STRING:-}"
|
||||||
fi
|
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'
|
printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n'
|
||||||
echo "service must be watchdog, monitor, or restart"; exit 0
|
echo "service must be watchdog, monitor, or restart"; exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
file="/var/log/wirecon-${service}.log"
|
file="/var/log/wirecon-${service}.log"
|
||||||
printf 'Content-Type: text/plain\r\n\r\n'
|
printf 'Content-Type: text/plain\r\n\r\n'
|
||||||
if [[ -r "$file" ]]; then
|
if [ -r "$file" ]; then
|
||||||
tail -n "$lines" "$file"
|
tail -n "$lines" "$file"
|
||||||
else
|
else
|
||||||
echo "(no log yet at $file)"
|
echo "(no log yet at $file)"
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
# CGI: emit service + tunnel state as key=value lines.
|
# CGI: emit service + tunnel state as key=value lines.
|
||||||
|
# POSIX-sh; busybox httpd may run CGI via ash regardless of shebang.
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
printf 'Content-Type: text/plain\r\n\r\n'
|
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
|
# peer dump: pub psk endpoint allowed_ips last_handshake rx tx keepalive
|
||||||
dump=$(wg show "$IFACE" dump 2>/dev/null | awk 'NR==2')
|
dump=$(wg show "$IFACE" dump 2>/dev/null | awk 'NR==2')
|
||||||
if [[ -n "$dump" ]]; then
|
if [ -n "$dump" ]; then
|
||||||
endpoint=$(awk '{print $3}' <<<"$dump")
|
endpoint=$(printf '%s\n' "$dump" | awk '{print $3}')
|
||||||
hs_ts=$(awk '{print $5}' <<<"$dump")
|
hs_ts=$(printf '%s\n' "$dump" | awk '{print $5}')
|
||||||
rx=$(awk '{print $6}' <<<"$dump")
|
rx=$(printf '%s\n' "$dump" | awk '{print $6}')
|
||||||
tx=$(awk '{print $7}' <<<"$dump")
|
tx=$(printf '%s\n' "$dump" | awk '{print $7}')
|
||||||
now=$(date +%s)
|
now=$(date +%s)
|
||||||
age=-1
|
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.endpoint=%s\n' "${endpoint:-none}"
|
||||||
printf 'wg0.handshake_age_s=%s\n' "$age"
|
printf 'wg0.handshake_age_s=%s\n' "$age"
|
||||||
printf 'wg0.rx_bytes=%s\n' "${rx:-0}"
|
printf 'wg0.rx_bytes=%s\n' "${rx:-0}"
|
||||||
|
|||||||
Reference in new issue
Block a user