diff --git a/README.md b/README.md index bc240a2..33fd854 100644 --- a/README.md +++ b/README.md @@ -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: -p port (default 8088) - --http plain http instead of https + --http plain http instead of https (kept on upgrades; + --https switches back) --cert --key https with your own certificate (PEM, kept on upgrades) wipe delete all messages and photos (accounts stay) uninstall remove everything diff --git a/dist/install-cabinchat.sh b/dist/install-cabinchat.sh index c1ed525..c48d1bf 100755 --- a/dist/install-cabinchat.sh +++ b/dist/install-cabinchat.sh @@ -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 <" 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://:$PORT once that name points to $LANIP" fi } diff --git a/installer.sh.in b/installer.sh.in index ef63047..6c2d431 100644 --- a/installer.sh.in +++ b/installer.sh.in @@ -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 <" 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 @@ -128,6 +151,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 @@ -173,6 +197,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" @@ -227,8 +252,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://:$PORT once that name points to $LANIP" fi }