installer: find a CA-signed nginx certificate, don't get stuck on http

- an upgrade only stays on http if --http was given explicitly (marker
  file data/http-only); before, a 4.0 install that had fallen back to
  http never switched to https
- look for certificates in all nginx config files and prefer a CA-signed
  one (chain file or issuer != subject) over the self-signed default
- hint how to install libustream-openssl when uhttpd lacks TLS

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pascal Suter
2026-11-16 15:37:07 +01:00
co-authored by Claude Opus 5.5
parent 1459b3228d
commit 8928ae7077
3 changed files with 84 additions and 29 deletions
+2 -1
View File
@@ -33,7 +33,8 @@ Running a newer installer over an existing install upgrades it and keeps
accounts, messages, photos, the port and the http/https choice. Options: accounts, messages, photos, the port and the http/https choice. Options:
-p <port> port (default 8088) -p <port> port (default 8088)
--http plain http instead of https --http plain http instead of https (kept on upgrades;
--https switches back)
--cert <file> --key <file> https with your own certificate (PEM, kept on upgrades) --cert <file> --key <file> https with your own certificate (PEM, kept on upgrades)
wipe delete all messages and photos (accounts stay) wipe delete all messages and photos (accounts stay)
uninstall remove everything uninstall remove everything
+41 -14
View File
@@ -39,15 +39,39 @@ configured_port() {
tr ' ' '\n' | sed -n 's/.*:\([0-9][0-9]*\)$/\1/p' | head -n 1 tr ' ' '\n' | sed -n 's/.*:\([0-9][0-9]*\)$/\1/p' | head -n 1
} }
# the certificate of the router's own web admin -> CERT, KEY # "cert key" pairs from the nginx configuration (GL.iNet firmware), in order
# (GL.iNet firmware: nginx; plain OpenWrt: uhttpd) nginx_certs() {
admin_cert() { for f in /etc/nginx/nginx.conf /etc/nginx/conf.d/* /etc/nginx/*/*.conf; do
for f in /etc/nginx/conf.d/*.conf /etc/nginx/nginx.conf; do
[ -f "$f" ] || continue [ -f "$f" ] || continue
c=$(sed -n 's/^[[:space:]]*ssl_certificate[[:space:]]\{1,\}\([^;[:space:]]*\);.*/\1/p' "$f" | head -n 1) awk '{ sub(/#.*/, "") }
k=$(sed -n 's/^[[:space:]]*ssl_certificate_key[[:space:]]\{1,\}\([^;[:space:]]*\);.*/\1/p' "$f" | head -n 1) $1 == "ssl_certificate" { c = $2; sub(/;.*/, "", c) }
if [ -s "$c" ] && [ -s "$k" ]; then CERT=$c; KEY=$k; return 0; fi $1 == "ssl_certificate_key" { k = $2; sub(/;.*/, "", k) }
done c != "" && k != "" { print c, k; c = k = "" }' "$f"
done | awk '!seen[$0]++'
}
# is this certificate issued by a CA (not self-signed)? A chain file with more
# than one certificate is; otherwise ask openssl if it is there.
ca_signed() {
[ "$(grep -c 'BEGIN CERTIFICATE' "$1" 2>/dev/null)" -gt 1 ] && return 0
command -v openssl >/dev/null || return 1
s=$(openssl x509 -in "$1" -noout -subject 2>/dev/null | sed 's/^subject=[[:space:]]*//')
i=$(openssl x509 -in "$1" -noout -issuer 2>/dev/null | sed 's/^issuer=[[:space:]]*//')
[ -n "$i" ] && [ "$s" != "$i" ]
}
# the certificate of the router's own web admin -> CERT, KEY
# (GL.iNet firmware: nginx, preferring a CA-signed one; plain OpenWrt: uhttpd)
admin_cert() {
CERT=; KEY=
while read -r c k; do
[ -s "$c" ] && [ -s "$k" ] || continue
[ -n "$CERT" ] || { CERT=$c; KEY=$k; }
if ca_signed "$c"; then CERT=$c; KEY=$k; break; fi
done <<EOF
$(nginx_certs)
EOF
[ -n "$CERT" ] && return 0
c=$(uci -q get uhttpd.main.cert); k=$(uci -q get uhttpd.main.key) c=$(uci -q get uhttpd.main.cert); k=$(uci -q get uhttpd.main.key)
if [ -s "$c" ] && [ -s "$k" ]; then CERT=$c; KEY=$k; return 0; fi if [ -s "$c" ] && [ -s "$k" ]; then CERT=$c; KEY=$k; return 0; fi
if [ -s /etc/uhttpd.crt ] && [ -s /etc/uhttpd.key ]; then CERT=/etc/uhttpd.crt; KEY=/etc/uhttpd.key; return 0; fi if [ -s /etc/uhttpd.crt ] && [ -s /etc/uhttpd.key ]; then CERT=/etc/uhttpd.crt; KEY=/etc/uhttpd.key; return 0; fi
@@ -4209,13 +4233,12 @@ do_install() {
die "port $PORT is already in use - choose another one with -p <port>" die "port $PORT is already in use - choose another one with -p <port>"
fi fi
# https unless asked otherwise; an upgrade of a 4.x+ http install stays on http # https unless asked otherwise; once --http was given, upgrades stay on http
OLDCERT=$(uci -q get uhttpd.chat.cert) OLDCERT=$(uci -q get uhttpd.chat.cert)
TLS_ARG=$TLS
if [ -z "$TLS" ]; then if [ -z "$TLS" ]; then
TLS=1 TLS=1
case $OLDVER in [4-9].*|[1-9][0-9]*) [ -f "$DATA/http-only" ] && TLS=0 && info "staying on plain http as chosen before (use --https to switch)"
uci -q get uhttpd.chat.listen_https >/dev/null || TLS=0 ;;
esac
fi fi
CERT=; KEY= CERT=; KEY=
if [ "$TLS" = 1 ]; then if [ "$TLS" = 1 ]; then
@@ -4229,6 +4252,7 @@ do_install() {
fi fi
if [ "$TLS" = 1 ] && ! have_tls; then if [ "$TLS" = 1 ] && ! have_tls; then
warn "uhttpd has no TLS support (libustream-ssl missing) - using plain http" warn "uhttpd has no TLS support (libustream-ssl missing) - using plain http"
warn " to get https: opkg update && opkg install libustream-openssl, then run this installer again"
TLS=0 TLS=0
fi fi
fi fi
@@ -4274,6 +4298,7 @@ do_install() {
rm -rf "$KEEPIMG" rm -rf "$KEEPIMG"
[ -f "$CLOCK" ] || date +%s > "$CLOCK" [ -f "$CLOCK" ] || date +%s > "$CLOCK"
if [ "$TLS_ARG" = 0 ]; then touch "$DATA/http-only"; elif [ "$TLS" = 1 ]; then rm -f "$DATA/http-only"; fi
if [ "$TLS" = 1 ] && [ -n "$CERT_ARG" ]; then if [ "$TLS" = 1 ] && [ -n "$CERT_ARG" ]; then
mkdir -p "$DATA/tls" mkdir -p "$DATA/tls"
cp "$CERT_ARG" "$DATA/tls/cert.pem" && cp "$KEY_ARG" "$DATA/tls/key.pem" || die "cannot copy the certificate" cp "$CERT_ARG" "$DATA/tls/cert.pem" && cp "$KEY_ARG" "$DATA/tls/key.pem" || die "cannot copy the certificate"
@@ -4328,8 +4353,10 @@ do_install() {
info "Cabin Chat $VERSION is running ($a accounts, $n messages from the last 24 h kept)" info "Cabin Chat $VERSION is running ($a accounts, $n messages from the last 24 h kept)"
echo " $SCHEME://$LANIP:$PORT" echo " $SCHEME://$LANIP:$PORT"
echo " $SCHEME://$DNSNAME:$PORT" echo " $SCHEME://$DNSNAME:$PORT"
if [ "$TLS" = 1 ] && [ -z "$CERT_ARG" ] && [ "$CERT" != "$DATA/tls/cert.pem" ]; then if [ "$TLS" = 1 ] && ! ca_signed "$CERT"; then
echo " (the router's certificate is self-signed: browsers warn once, accept it to continue)" echo " (the certificate looks self-signed: browsers warn once, accept it to continue)"
elif [ "$TLS" = 1 ]; then
echo " or https://<the name in the certificate>:$PORT once that name points to $LANIP"
fi fi
} }
+41 -14
View File
@@ -39,15 +39,39 @@ configured_port() {
tr ' ' '\n' | sed -n 's/.*:\([0-9][0-9]*\)$/\1/p' | head -n 1 tr ' ' '\n' | sed -n 's/.*:\([0-9][0-9]*\)$/\1/p' | head -n 1
} }
# the certificate of the router's own web admin -> CERT, KEY # "cert key" pairs from the nginx configuration (GL.iNet firmware), in order
# (GL.iNet firmware: nginx; plain OpenWrt: uhttpd) nginx_certs() {
admin_cert() { for f in /etc/nginx/nginx.conf /etc/nginx/conf.d/* /etc/nginx/*/*.conf; do
for f in /etc/nginx/conf.d/*.conf /etc/nginx/nginx.conf; do
[ -f "$f" ] || continue [ -f "$f" ] || continue
c=$(sed -n 's/^[[:space:]]*ssl_certificate[[:space:]]\{1,\}\([^;[:space:]]*\);.*/\1/p' "$f" | head -n 1) awk '{ sub(/#.*/, "") }
k=$(sed -n 's/^[[:space:]]*ssl_certificate_key[[:space:]]\{1,\}\([^;[:space:]]*\);.*/\1/p' "$f" | head -n 1) $1 == "ssl_certificate" { c = $2; sub(/;.*/, "", c) }
if [ -s "$c" ] && [ -s "$k" ]; then CERT=$c; KEY=$k; return 0; fi $1 == "ssl_certificate_key" { k = $2; sub(/;.*/, "", k) }
done c != "" && k != "" { print c, k; c = k = "" }' "$f"
done | awk '!seen[$0]++'
}
# is this certificate issued by a CA (not self-signed)? A chain file with more
# than one certificate is; otherwise ask openssl if it is there.
ca_signed() {
[ "$(grep -c 'BEGIN CERTIFICATE' "$1" 2>/dev/null)" -gt 1 ] && return 0
command -v openssl >/dev/null || return 1
s=$(openssl x509 -in "$1" -noout -subject 2>/dev/null | sed 's/^subject=[[:space:]]*//')
i=$(openssl x509 -in "$1" -noout -issuer 2>/dev/null | sed 's/^issuer=[[:space:]]*//')
[ -n "$i" ] && [ "$s" != "$i" ]
}
# the certificate of the router's own web admin -> CERT, KEY
# (GL.iNet firmware: nginx, preferring a CA-signed one; plain OpenWrt: uhttpd)
admin_cert() {
CERT=; KEY=
while read -r c k; do
[ -s "$c" ] && [ -s "$k" ] || continue
[ -n "$CERT" ] || { CERT=$c; KEY=$k; }
if ca_signed "$c"; then CERT=$c; KEY=$k; break; fi
done <<EOF
$(nginx_certs)
EOF
[ -n "$CERT" ] && return 0
c=$(uci -q get uhttpd.main.cert); k=$(uci -q get uhttpd.main.key) c=$(uci -q get uhttpd.main.cert); k=$(uci -q get uhttpd.main.key)
if [ -s "$c" ] && [ -s "$k" ]; then CERT=$c; KEY=$k; return 0; fi if [ -s "$c" ] && [ -s "$k" ]; then CERT=$c; KEY=$k; return 0; fi
if [ -s /etc/uhttpd.crt ] && [ -s /etc/uhttpd.key ]; then CERT=/etc/uhttpd.crt; KEY=/etc/uhttpd.key; return 0; fi if [ -s /etc/uhttpd.crt ] && [ -s /etc/uhttpd.key ]; then CERT=/etc/uhttpd.crt; KEY=/etc/uhttpd.key; return 0; fi
@@ -108,13 +132,12 @@ do_install() {
die "port $PORT is already in use - choose another one with -p <port>" die "port $PORT is already in use - choose another one with -p <port>"
fi fi
# https unless asked otherwise; an upgrade of a 4.x+ http install stays on http # https unless asked otherwise; once --http was given, upgrades stay on http
OLDCERT=$(uci -q get uhttpd.chat.cert) OLDCERT=$(uci -q get uhttpd.chat.cert)
TLS_ARG=$TLS
if [ -z "$TLS" ]; then if [ -z "$TLS" ]; then
TLS=1 TLS=1
case $OLDVER in [4-9].*|[1-9][0-9]*) [ -f "$DATA/http-only" ] && TLS=0 && info "staying on plain http as chosen before (use --https to switch)"
uci -q get uhttpd.chat.listen_https >/dev/null || TLS=0 ;;
esac
fi fi
CERT=; KEY= CERT=; KEY=
if [ "$TLS" = 1 ]; then if [ "$TLS" = 1 ]; then
@@ -128,6 +151,7 @@ do_install() {
fi fi
if [ "$TLS" = 1 ] && ! have_tls; then if [ "$TLS" = 1 ] && ! have_tls; then
warn "uhttpd has no TLS support (libustream-ssl missing) - using plain http" warn "uhttpd has no TLS support (libustream-ssl missing) - using plain http"
warn " to get https: opkg update && opkg install libustream-openssl, then run this installer again"
TLS=0 TLS=0
fi fi
fi fi
@@ -173,6 +197,7 @@ do_install() {
rm -rf "$KEEPIMG" rm -rf "$KEEPIMG"
[ -f "$CLOCK" ] || date +%s > "$CLOCK" [ -f "$CLOCK" ] || date +%s > "$CLOCK"
if [ "$TLS_ARG" = 0 ]; then touch "$DATA/http-only"; elif [ "$TLS" = 1 ]; then rm -f "$DATA/http-only"; fi
if [ "$TLS" = 1 ] && [ -n "$CERT_ARG" ]; then if [ "$TLS" = 1 ] && [ -n "$CERT_ARG" ]; then
mkdir -p "$DATA/tls" mkdir -p "$DATA/tls"
cp "$CERT_ARG" "$DATA/tls/cert.pem" && cp "$KEY_ARG" "$DATA/tls/key.pem" || die "cannot copy the certificate" cp "$CERT_ARG" "$DATA/tls/cert.pem" && cp "$KEY_ARG" "$DATA/tls/key.pem" || die "cannot copy the certificate"
@@ -227,8 +252,10 @@ do_install() {
info "Cabin Chat $VERSION is running ($a accounts, $n messages from the last 24 h kept)" info "Cabin Chat $VERSION is running ($a accounts, $n messages from the last 24 h kept)"
echo " $SCHEME://$LANIP:$PORT" echo " $SCHEME://$LANIP:$PORT"
echo " $SCHEME://$DNSNAME:$PORT" echo " $SCHEME://$DNSNAME:$PORT"
if [ "$TLS" = 1 ] && [ -z "$CERT_ARG" ] && [ "$CERT" != "$DATA/tls/cert.pem" ]; then if [ "$TLS" = 1 ] && ! ca_signed "$CERT"; then
echo " (the router's certificate is self-signed: browsers warn once, accept it to continue)" echo " (the certificate looks self-signed: browsers warn once, accept it to continue)"
elif [ "$TLS" = 1 ]; then
echo " or https://<the name in the certificate>:$PORT once that name points to $LANIP"
fi fi
} }