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:
co-authored by
Claude Opus 5.5
parent
1459b3228d
commit
8928ae7077
Vendored
+41
-14
@@ -39,15 +39,39 @@ configured_port() {
|
||||
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
|
||||
# (GL.iNet firmware: nginx; plain OpenWrt: uhttpd)
|
||||
admin_cert() {
|
||||
for f in /etc/nginx/conf.d/*.conf /etc/nginx/nginx.conf; do
|
||||
# "cert key" pairs from the nginx configuration (GL.iNet firmware), in order
|
||||
nginx_certs() {
|
||||
for f in /etc/nginx/nginx.conf /etc/nginx/conf.d/* /etc/nginx/*/*.conf; do
|
||||
[ -f "$f" ] || continue
|
||||
c=$(sed -n 's/^[[:space:]]*ssl_certificate[[:space:]]\{1,\}\([^;[:space:]]*\);.*/\1/p' "$f" | head -n 1)
|
||||
k=$(sed -n 's/^[[:space:]]*ssl_certificate_key[[:space:]]\{1,\}\([^;[:space:]]*\);.*/\1/p' "$f" | head -n 1)
|
||||
if [ -s "$c" ] && [ -s "$k" ]; then CERT=$c; KEY=$k; return 0; fi
|
||||
done
|
||||
awk '{ sub(/#.*/, "") }
|
||||
$1 == "ssl_certificate" { c = $2; sub(/;.*/, "", c) }
|
||||
$1 == "ssl_certificate_key" { k = $2; sub(/;.*/, "", k) }
|
||||
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)
|
||||
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
|
||||
@@ -4209,13 +4233,12 @@ do_install() {
|
||||
die "port $PORT is already in use - choose another one with -p <port>"
|
||||
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)
|
||||
TLS_ARG=$TLS
|
||||
if [ -z "$TLS" ]; then
|
||||
TLS=1
|
||||
case $OLDVER in [4-9].*|[1-9][0-9]*)
|
||||
uci -q get uhttpd.chat.listen_https >/dev/null || TLS=0 ;;
|
||||
esac
|
||||
[ -f "$DATA/http-only" ] && TLS=0 && info "staying on plain http as chosen before (use --https to switch)"
|
||||
fi
|
||||
CERT=; KEY=
|
||||
if [ "$TLS" = 1 ]; then
|
||||
@@ -4229,6 +4252,7 @@ do_install() {
|
||||
fi
|
||||
if [ "$TLS" = 1 ] && ! have_tls; then
|
||||
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
|
||||
fi
|
||||
fi
|
||||
@@ -4274,6 +4298,7 @@ do_install() {
|
||||
rm -rf "$KEEPIMG"
|
||||
[ -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
|
||||
mkdir -p "$DATA/tls"
|
||||
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)"
|
||||
echo " $SCHEME://$LANIP:$PORT"
|
||||
echo " $SCHEME://$DNSNAME:$PORT"
|
||||
if [ "$TLS" = 1 ] && [ -z "$CERT_ARG" ] && [ "$CERT" != "$DATA/tls/cert.pem" ]; then
|
||||
echo " (the router's certificate is self-signed: browsers warn once, accept it to continue)"
|
||||
if [ "$TLS" = 1 ] && ! ca_signed "$CERT"; then
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user