diff --git a/README.md b/README.md index 33fd854..988c579 100644 --- a/README.md +++ b/README.md @@ -27,24 +27,41 @@ Or copy it over from a checkout: scp -O dist/install-cabinchat.sh root@192.168.8.1:/tmp/ ssh root@192.168.8.1 sh /tmp/install-cabinchat.sh -Open `https://192.168.8.1:8088` (or `https://chat.lan:8088`) on the router's WiFi. +Open `https://192.168.8.1:8088` (or `https://chat.lan:8088`) on the router's +WiFi, or `https://` if you set up a name (see below). The installer prints +the addresses at the end. 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 and the settings below. Options: -p port (default 8088) - --http plain http instead of https (kept on upgrades; - --https switches back) - --cert --key https with your own certificate (PEM, kept on upgrades) + --name also serve https:/// (GL.iNet/nginx only) + --no-name forget the name + --http plain http instead of https (--https switches back) + --cert --key https with your own certificate (PEM) + --no-nginx GL.iNet: use uhttpd alone (--nginx switches back) wipe delete all messages and photos (accounts stay) uninstall remove everything ## HTTPS, app install and notifications -By default the chat uses https with the certificate of the router's web admin -(GL.iNet: the one nginx uses). That certificate is self-signed, so every -browser shows a warning once; accept it to continue. If the router has no -certificate or uhttpd lacks TLS support, the installer falls back to http. +By default the chat uses https with the certificate of the router's web admin. + +On GL.iNet firmware the web admin runs on nginx. The installer adds +`/etc/nginx/conf.d/zz-cabinchat.conf`: nginx does https with the admin's +certificate (`/etc/nginx/nginx.cer`) and serves the static files on port 8088, +and passes `/cgi-bin/chat` to a small uhttpd instance that only listens on +`127.0.0.1:8088`. (nginx's own CGI helper, fcgiwrap, runs only 4 scripts at a +time and is shared with the web admin; the chat keeps one waiting request per +open browser, so it gets its own.) The config is checked with `nginx -t` +first; if nginx refuses it, the installer falls back to uhttpd alone. + +On other OpenWrt routers uhttpd does https itself (needs `libustream-ssl`). +If the router has no certificate or no TLS support, the installer falls back +to http. + +The router's factory certificate is self-signed, so every browser shows a +warning once; accept it to continue. Browsers only offer some features on a *trusted* https connection: @@ -55,11 +72,24 @@ Browsers only offer some features on a *trusted* https connection: | Add to Home Screen (iOS) | ✓ | ✓ | ✓ | | notifications | ✓ | usually blocked (browser-dependent) | no | -To get everything, give the installer a certificate the phones trust, e.g. a -Let's Encrypt certificate for a name in a domain you own (obtained at home via -DNS challenge), and point that name at the router: -`uci add_list dhcp.@dnsmasq[0].address='/chat.example.com/192.168.8.1'`. -Checking a certificate needs no internet, so this works in the air. +To get everything, use a certificate the phones trust, e.g. a Let's Encrypt +certificate for a domain you own (obtained at home via DNS challenge, a +wildcard `*.example.com` is handy). On GL.iNet, install it as the web admin's +certificate (`/etc/nginx/nginx.cer` with the full chain, and `nginx.key`), +then run the installer with `--name chat.example.com`. nginx then also +serves the chat on `https://chat.example.com/` (port 443, next to the web +admin, which keeps answering on the IP address and other names), redirects +http to https, and the installer adds the name to the router's DNS so it +resolves without internet. Checking a certificate needs no internet either, +so this works in the air. If the router's DNS already has a name that +contains "chat" and matches the certificate, the installer picks it without +`--name`. Elsewhere, pass the certificate with `--cert`/`--key`. + +**Firmware updates** (GL.iNet, with "keep settings"): the installer adds +`/www-chat/` and the nginx config file to `/etc/sysupgrade.conf`, so they are +kept along with the uhttpd and DNS settings and the web admin's certificate. +If a future firmware's nginx should reject the chat's config file, run the +installer again (or delete `/etc/nginx/conf.d/zz-cabinchat.conf`). Notifications work while the chat is open in the background: in another tab, or minimized with the screen on. Real push to a closed app needs the browser @@ -117,7 +147,8 @@ router. On the router's flash, outside the web root: `/www-chat/data/messages` (one line per message), `seq`, `gen`, and `accounts/`, `names/`, `sessions/`, `avatars/` (one small file per account, name, logged-in device and profile -picture); photos in `/www-chat/htdocs/img/` with random file names. Messages +picture); photos in `/www-chat/htdocs/img/` with random file names. Installer settings: +`hostname`, `http-only`, `no-nginx`, `tls/` in the same data folder. Messages older than 24 h are dropped on the fly. Photos are refused once they pass 50 MB. Presence, typing notices, device-link codes and the long-poll event token live in RAM under `/tmp/cabinchat/`. Limits are at the top of diff --git a/dist/install-cabinchat.sh b/dist/install-cabinchat.sh index c48d1bf..503cca4 100755 --- a/dist/install-cabinchat.sh +++ b/dist/install-cabinchat.sh @@ -1,20 +1,24 @@ #!/bin/sh -# Cabin Chat 4.0.0 - self-contained installer for OpenWrt / GL.iNet routers. -# Uses only what the firmware already ships (uhttpd, busybox, uci). +# Cabin Chat 4.0.1 - self-contained installer for OpenWrt / GL.iNet routers. +# Uses only what the firmware already ships (uhttpd, busybox, uci; nginx if there). # # sh install-cabinchat.sh install, or upgrade an existing install -# (keeps accounts, messages, photos, port) +# (keeps accounts, messages, photos, settings) # sh install-cabinchat.sh -p 8090 install/upgrade on a specific port +# sh install-cabinchat.sh --name chat.example.com also serve https:/// # sh install-cabinchat.sh --http plain http instead of https # sh install-cabinchat.sh --cert C --key K https with your own certificate +# sh install-cabinchat.sh --no-nginx don't use nginx (GL.iNet), only uhttpd # sh install-cabinchat.sh wipe delete all messages and photos # sh install-cabinchat.sh uninstall remove Cabin Chat completely # # https uses the certificate of the router's web admin unless --cert is given. +# On GL.iNet firmware nginx serves https and passes the chat requests on to +# uhttpd on 127.0.0.1. # # Generated by build.sh - edit www/ and installer.sh.in instead of this file. -VERSION="4.0.0" +VERSION="4.0.1" APP=/www-chat WEB=$APP/htdocs DATA=$APP/data @@ -22,14 +26,16 @@ RUN=/tmp/cabinchat CLOCK=/etc/cabinchat.clock DNSNAME=chat.lan DEFAULT_PORT=8088 +# after gl.conf: the first server on port 443 is nginx's default (the GL admin) +NGINX_CONF=/etc/nginx/conf.d/zz-cabinchat.conf STAGE=/tmp/cabinchat-stage.$$ info() { echo ">> $*"; } warn() { echo "WARNING: $*" >&2; } die() { echo "ERROR: $*" >&2; rm -rf "$STAGE"; exit 1; } usage() { - if [ -f "$0" ]; then sed -n '4,13p' "$0" | sed 's/^# \{0,1\}//' - else echo "usage: sh install-cabinchat.sh [-p ] [--http | --cert --key ] [wipe | uninstall]"; fi + if [ -f "$0" ]; then sed -n '4,18p' "$0" | sed 's/^# \{0,1\}//' + else echo "usage: sh install-cabinchat.sh [-p ] [--name ] [--http | --cert --key ] [--no-nginx] [wipe | uninstall]"; fi } listening() { netstat -tln 2>/dev/null | grep -qE "[:.]$1[[:space:]]"; } @@ -78,8 +84,105 @@ EOF return 1 } +# DNS names in a certificate, one per line +cert_names() { + command -v openssl >/dev/null || return 0 + openssl x509 -in "$1" -noout -text 2>/dev/null | grep -o 'DNS:[^,[:space:]]*' | sed 's/^DNS://' +} + +# does host name $1 match a certificate name $2 (exactly or via *.)? +name_match() { + case $2 in + "$1") return 0 ;; + \*.*) [ "${1#*.}" != "$1" ] && [ "${1#*.}" = "${2#\*.}" ] ;; + *) return 1 ;; + esac +} + +# a name that points to this router (dnsmasq), is covered by the certificate +# and looks like it is meant for the chat -> NAME +guess_name() { + names=$(cert_names "$CERT") + [ -n "$names" ] || return 1 + for a in $(uci -q get dhcp.@dnsmasq[0].address); do + h=${a#/}; h=${h%%/*} + [ "${a##*/}" = "$LANIP" ] || continue + case ${h%%.*} in *chat*) ;; *) continue ;; esac + set -f # "*.example.com" is no file pattern + for n in $names; do + name_match "$h" "$n" && { set +f; NAME=$h; return 0; } + done + set +f + done + return 1 +} + # uhttpd does TLS through libustream-ssl -have_tls() { ls /lib/libustream-ssl.so* /usr/lib/libustream-ssl.so* >/dev/null 2>&1; } +have_tls() { + for f in /lib/libustream-ssl.so* /usr/lib/libustream-ssl.so*; do + [ -e "$f" ] && return 0 + done + return 1 +} + +# GL.iNet firmware: nginx is the web server and reads conf.d/*.conf +have_nginx() { + [ -x /usr/sbin/nginx ] && [ -d /etc/nginx/conf.d ] && + grep -qs 'include[[:space:]]*/etc/nginx/conf.d/\*\.conf' /etc/nginx/nginx.conf +} + +# nginx terminates https (port $PORT and, with a name, 443) and passes the +# chat requests to uhttpd on 127.0.0.1:$PORT; static files come from $WEB +write_nginx() { + # as a regex: GL's nginx has a small server_names_hash_bucket_size, and + # raising it would mean an http-level setting that may clash after updates + NAMERE="~^$(echo "$NAME" | sed 's/\./\\./g')\$" + { + echo "# Cabin Chat - written by install-cabinchat.sh, overwritten on upgrades" + echo "server {" + echo " listen $LANIP:$PORT ssl;" + if [ -n "$NAME" ]; then + echo " listen 443 ssl;" + echo " listen [::]:443 ssl;" + echo " server_name $NAMERE;" + fi + cat < "$NGINX_CONF" +} + +nginx_reload() { nginx -s reload 2>/dev/null || /etc/init.d/nginx restart; } remove_config() { if uci -q get uhttpd.chat >/dev/null; then @@ -89,8 +192,19 @@ remove_config() { for a in $(uci -q get dhcp.@dnsmasq[0].address); do case $a in "/$DNSNAME/"*) uci del_list dhcp.@dnsmasq[0].address="$a" ;; esac done + if [ -f "$DATA/dns-added" ]; then + n=$(cat "$DATA/dns-added") + for a in $(uci -q get dhcp.@dnsmasq[0].address); do + case $a in "/$n/"*) uci del_list dhcp.@dnsmasq[0].address="$a" ;; esac + done + rm -f "$DATA/dns-added" + fi uci commit dhcp - [ -f /etc/sysupgrade.conf ] && sed -i "\#^$APP/\$#d" /etc/sysupgrade.conf + if [ -f "$NGINX_CONF" ]; then + rm -f "$NGINX_CONF" + nginx_reload >/dev/null 2>&1 + fi + [ -f /etc/sysupgrade.conf ] && sed -i -e "\#^$APP/\$#d" -e "\#^$NGINX_CONF\$#d" /etc/sysupgrade.conf } extract_payload() { @@ -626,7 +740,7 @@ needs_prune() { del_photos() { [ -f "$DATA/.del" ] || return 0 while IFS= read -r f; do - case $f in .*|*[!0-9a-f.-]*) ;; *.jpg) rm -f "$IMG/$f" ;; esac + case $f in .*|*[!0-9a-z.-]*) ;; *.jpg) rm -f "$IMG/$f" ;; esac done < "$DATA/.del" rm -f "$DATA/.del" } @@ -4196,7 +4310,7 @@ self.addEventListener('notificationclick', e => { }); __CABINCHAT_EOF__ cat > "$STAGE/VERSION" <<'__CABINCHAT_EOF__' -4.0.0 +4.0.1 __CABINCHAT_EOF__ [ -f "$STAGE/index.html" ] && [ -f "$STAGE/cgi-bin/chat" ] && [ -f "$STAGE/VERSION" ] \ || die "installer payload is incomplete" @@ -4221,7 +4335,9 @@ do_install() { [ -n "$LANIP" ] || die "could not read the LAN IP (network.lan.ipaddr)" OLDVER= - if [ -f "$APP/VERSION" ]; then + if [ -f "$WEB/VERSION" ]; then + OLDVER=$(cat "$WEB/VERSION") + elif [ -f "$APP/VERSION" ]; then OLDVER=$(cat "$APP/VERSION") elif [ -e "$APP/index.html" ] || uci -q get uhttpd.chat >/dev/null; then OLDVER="1.x" @@ -4234,7 +4350,6 @@ do_install() { fi # 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 @@ -4244,20 +4359,35 @@ do_install() { if [ "$TLS" = 1 ]; then if [ -n "$CERT_ARG" ]; then [ -s "$CERT_ARG" ] && [ -s "$KEY_ARG" ] || die "--cert and --key need two existing files (PEM)" - elif [ "$OLDCERT" = "$DATA/tls/cert.pem" ] && [ -s "$OLDCERT" ] && [ -s "$DATA/tls/key.pem" ]; then - CERT=$OLDCERT; KEY=$DATA/tls/key.pem # keep the certificate given last time + CERT=$CERT_ARG; KEY=$KEY_ARG + elif [ -s "$DATA/tls/cert.pem" ] && [ -s "$DATA/tls/key.pem" ]; then + CERT=$DATA/tls/cert.pem; KEY=$DATA/tls/key.pem # the certificate given last time elif ! admin_cert; then warn "no certificate of the router's web admin found - using plain http" TLS=0 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 + # who does https: nginx (GL.iNet) or uhttpd itself + [ -n "$NGINX_ARG" ] || { [ -f "$DATA/no-nginx" ] && NGINX_ARG=0; } + FRONT=uhttpd + if [ "$TLS" = 1 ] && [ "$NGINX_ARG" != 0 ] && have_nginx; then + FRONT=nginx + elif [ "$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 [ "$TLS" = 1 ] && SCHEME=https || SCHEME=http + # host name for https:/// (nginx only): --name, the one from last time, or a guess + if [ "$FRONT" = nginx ]; then + [ -n "$NAME" ] || NAME=$(cat "$DATA/hostname" 2>/dev/null) + [ -n "$NAME" ] || guess_name + [ "$NAME" = - ] && NAME= + else + NAME= + fi + if [ -n "$OLDVER" ]; then info "upgrading Cabin Chat $OLDVER -> $VERSION" else @@ -4299,6 +4429,7 @@ do_install() { [ -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 [ "$NGINX_ARG" = 0 ]; then touch "$DATA/no-nginx"; elif [ "$NGINX_ARG" = 1 ]; then rm -f "$DATA/no-nginx"; 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" @@ -4306,14 +4437,31 @@ do_install() { CERT=$DATA/tls/cert.pem; KEY=$DATA/tls/key.pem fi - info "configuring web server on $SCHEME://$LANIP:$PORT" + if [ "$FRONT" = nginx ]; then + info "configuring nginx for https://$LANIP:$PORT${NAME:+ and https://$NAME}" + info "certificate: $CERT" + write_nginx + if ! nginx -t >/dev/null 2>&1; then + nginx -t + rm -f "$NGINX_CONF" + warn "nginx did not accept the configuration - using uhttpd alone" + FRONT=uhttpd + NAME= + have_tls || { TLS=0; SCHEME=http; } + fi + fi uci set uhttpd.chat=uhttpd - if [ "$TLS" = 1 ]; then + if [ "$FRONT" = nginx ]; then + echo "${NAME:--}" > "$DATA/hostname" + uci add_list uhttpd.chat.listen_http="127.0.0.1:$PORT" # only for nginx + elif [ "$TLS" = 1 ]; then + info "configuring web server on https://$LANIP:$PORT" info "certificate: $CERT" uci add_list uhttpd.chat.listen_https="$LANIP:$PORT" uci set uhttpd.chat.cert="$CERT" uci set uhttpd.chat.key="$KEY" else + info "configuring web server on http://$LANIP:$PORT" uci add_list uhttpd.chat.listen_http="$LANIP:$PORT" fi uci set uhttpd.chat.home="$WEB" @@ -4328,35 +4476,57 @@ do_install() { uci commit uhttpd /etc/init.d/uhttpd enable /etc/init.d/uhttpd restart + [ "$FRONT" = nginx ] && nginx_reload info "adding DNS name $DNSNAME" uci add_list dhcp.@dnsmasq[0].address="/$DNSNAME/$LANIP" + # the name must resolve without internet too + if [ -n "$NAME" ] && ! uci -q get dhcp.@dnsmasq[0].address | tr ' ' '\n' | grep -q "^/$NAME/"; then + info "adding DNS name $NAME" + uci add_list dhcp.@dnsmasq[0].address="/$NAME/$LANIP" + echo "$NAME" > "$DATA/dns-added" + fi uci commit dhcp /etc/init.d/dnsmasq restart >/dev/null 2>&1 touch /etc/sysupgrade.conf grep -qx "$APP/" /etc/sysupgrade.conf || echo "$APP/" >> /etc/sysupgrade.conf + if [ "$FRONT" = nginx ]; then + grep -qx "$NGINX_CONF" /etc/sysupgrade.conf || echo "$NGINX_CONF" >> /etc/sysupgrade.conf + fi sleep 1 - listening "$PORT" || die "uhttpd is not listening on port $PORT - check: logread | grep uhttpd" + listening "$PORT" || die "nothing is listening on port $PORT - check: logread | grep -E 'uhttpd|nginx'" if command -v wget >/dev/null; then case $(wget -q --no-check-certificate -O - "$SCHEME://$LANIP:$PORT/cgi-bin/chat?a=ping" 2>/dev/null | head -n 1) in @cabinchat*) info "self-test passed" ;; - *) warn "the chat backend did not answer as expected - check: logread | grep uhttpd" + *) warn "the chat backend did not answer as expected - check: logread | grep -E 'uhttpd|nginx'" [ "$TLS" = 1 ] && warn "if https is the problem, run the installer again with --http" ;; esac + if [ -n "$NAME" ]; then + case $(wget -q -O - "https://$NAME/cgi-bin/chat?a=ping" 2>/dev/null | head -n 1) in + @cabinchat*) info "self-test of https://$NAME passed (certificate valid)" ;; + *) warn "https://$NAME did not answer - does the certificate cover that name?" ;; + esac + fi fi n=$(grep -c . "$DATA/messages" 2>/dev/null); n=${n:-0} a=$(ls "$DATA/accounts" 2>/dev/null | grep -c .); a=${a:-0} echo 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 ] && ! 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" + if [ -n "$NAME" ]; then + echo " https://$NAME" + echo " (also $SCHEME://$LANIP:$PORT and $SCHEME://$DNSNAME:$PORT, with a certificate warning)" + else + echo " $SCHEME://$LANIP:$PORT" + echo " $SCHEME://$DNSNAME:$PORT" + 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 " For a warning-free https:/// run the installer again with --name " + echo " (a name the certificate covers: $(cert_names "$CERT" | tr '\n' ' '))" + fi fi } @@ -4387,6 +4557,8 @@ PORT= TLS= CERT_ARG= KEY_ARG= +NAME= +NGINX_ARG= while [ $# -gt 0 ]; do case $1 in -p|--port) [ -n "$2" ] || die "-p needs a port number"; PORT=$2; shift 2 ;; @@ -4394,6 +4566,10 @@ while [ $# -gt 0 ]; do --https) TLS=1; shift ;; --cert) [ -n "$2" ] || die "--cert needs a file"; CERT_ARG=$2; TLS=1; shift 2 ;; --key) [ -n "$2" ] || die "--key needs a file"; KEY_ARG=$2; shift 2 ;; + --name) [ -n "$2" ] || die "--name needs a host name"; NAME=$2; shift 2 ;; + --no-name) NAME=-; shift ;; + --no-nginx) NGINX_ARG=0; shift ;; + --nginx) NGINX_ARG=1; shift ;; [0-9]*) PORT=$1; shift ;; install|upgrade) CMD=install; shift ;; uninstall|remove) CMD=uninstall; shift ;; diff --git a/installer.sh.in b/installer.sh.in index 6c2d431..deb89a2 100644 --- a/installer.sh.in +++ b/installer.sh.in @@ -1,16 +1,20 @@ #!/bin/sh # Cabin Chat @@VERSION@@ - self-contained installer for OpenWrt / GL.iNet routers. -# Uses only what the firmware already ships (uhttpd, busybox, uci). +# Uses only what the firmware already ships (uhttpd, busybox, uci; nginx if there). # # sh install-cabinchat.sh install, or upgrade an existing install -# (keeps accounts, messages, photos, port) +# (keeps accounts, messages, photos, settings) # sh install-cabinchat.sh -p 8090 install/upgrade on a specific port +# sh install-cabinchat.sh --name chat.example.com also serve https:/// # sh install-cabinchat.sh --http plain http instead of https # sh install-cabinchat.sh --cert C --key K https with your own certificate +# sh install-cabinchat.sh --no-nginx don't use nginx (GL.iNet), only uhttpd # sh install-cabinchat.sh wipe delete all messages and photos # sh install-cabinchat.sh uninstall remove Cabin Chat completely # # https uses the certificate of the router's web admin unless --cert is given. +# On GL.iNet firmware nginx serves https and passes the chat requests on to +# uhttpd on 127.0.0.1. # # Generated by build.sh - edit www/ and installer.sh.in instead of this file. @@ -22,14 +26,16 @@ RUN=/tmp/cabinchat CLOCK=/etc/cabinchat.clock DNSNAME=chat.lan DEFAULT_PORT=8088 +# after gl.conf: the first server on port 443 is nginx's default (the GL admin) +NGINX_CONF=/etc/nginx/conf.d/zz-cabinchat.conf STAGE=/tmp/cabinchat-stage.$$ info() { echo ">> $*"; } warn() { echo "WARNING: $*" >&2; } die() { echo "ERROR: $*" >&2; rm -rf "$STAGE"; exit 1; } usage() { - if [ -f "$0" ]; then sed -n '4,13p' "$0" | sed 's/^# \{0,1\}//' - else echo "usage: sh install-cabinchat.sh [-p ] [--http | --cert --key ] [wipe | uninstall]"; fi + if [ -f "$0" ]; then sed -n '4,18p' "$0" | sed 's/^# \{0,1\}//' + else echo "usage: sh install-cabinchat.sh [-p ] [--name ] [--http | --cert --key ] [--no-nginx] [wipe | uninstall]"; fi } listening() { netstat -tln 2>/dev/null | grep -qE "[:.]$1[[:space:]]"; } @@ -78,8 +84,105 @@ EOF return 1 } +# DNS names in a certificate, one per line +cert_names() { + command -v openssl >/dev/null || return 0 + openssl x509 -in "$1" -noout -text 2>/dev/null | grep -o 'DNS:[^,[:space:]]*' | sed 's/^DNS://' +} + +# does host name $1 match a certificate name $2 (exactly or via *.)? +name_match() { + case $2 in + "$1") return 0 ;; + \*.*) [ "${1#*.}" != "$1" ] && [ "${1#*.}" = "${2#\*.}" ] ;; + *) return 1 ;; + esac +} + +# a name that points to this router (dnsmasq), is covered by the certificate +# and looks like it is meant for the chat -> NAME +guess_name() { + names=$(cert_names "$CERT") + [ -n "$names" ] || return 1 + for a in $(uci -q get dhcp.@dnsmasq[0].address); do + h=${a#/}; h=${h%%/*} + [ "${a##*/}" = "$LANIP" ] || continue + case ${h%%.*} in *chat*) ;; *) continue ;; esac + set -f # "*.example.com" is no file pattern + for n in $names; do + name_match "$h" "$n" && { set +f; NAME=$h; return 0; } + done + set +f + done + return 1 +} + # uhttpd does TLS through libustream-ssl -have_tls() { ls /lib/libustream-ssl.so* /usr/lib/libustream-ssl.so* >/dev/null 2>&1; } +have_tls() { + for f in /lib/libustream-ssl.so* /usr/lib/libustream-ssl.so*; do + [ -e "$f" ] && return 0 + done + return 1 +} + +# GL.iNet firmware: nginx is the web server and reads conf.d/*.conf +have_nginx() { + [ -x /usr/sbin/nginx ] && [ -d /etc/nginx/conf.d ] && + grep -qs 'include[[:space:]]*/etc/nginx/conf.d/\*\.conf' /etc/nginx/nginx.conf +} + +# nginx terminates https (port $PORT and, with a name, 443) and passes the +# chat requests to uhttpd on 127.0.0.1:$PORT; static files come from $WEB +write_nginx() { + # as a regex: GL's nginx has a small server_names_hash_bucket_size, and + # raising it would mean an http-level setting that may clash after updates + NAMERE="~^$(echo "$NAME" | sed 's/\./\\./g')\$" + { + echo "# Cabin Chat - written by install-cabinchat.sh, overwritten on upgrades" + echo "server {" + echo " listen $LANIP:$PORT ssl;" + if [ -n "$NAME" ]; then + echo " listen 443 ssl;" + echo " listen [::]:443 ssl;" + echo " server_name $NAMERE;" + fi + cat < "$NGINX_CONF" +} + +nginx_reload() { nginx -s reload 2>/dev/null || /etc/init.d/nginx restart; } remove_config() { if uci -q get uhttpd.chat >/dev/null; then @@ -89,8 +192,19 @@ remove_config() { for a in $(uci -q get dhcp.@dnsmasq[0].address); do case $a in "/$DNSNAME/"*) uci del_list dhcp.@dnsmasq[0].address="$a" ;; esac done + if [ -f "$DATA/dns-added" ]; then + n=$(cat "$DATA/dns-added") + for a in $(uci -q get dhcp.@dnsmasq[0].address); do + case $a in "/$n/"*) uci del_list dhcp.@dnsmasq[0].address="$a" ;; esac + done + rm -f "$DATA/dns-added" + fi uci commit dhcp - [ -f /etc/sysupgrade.conf ] && sed -i "\#^$APP/\$#d" /etc/sysupgrade.conf + if [ -f "$NGINX_CONF" ]; then + rm -f "$NGINX_CONF" + nginx_reload >/dev/null 2>&1 + fi + [ -f /etc/sysupgrade.conf ] && sed -i -e "\#^$APP/\$#d" -e "\#^$NGINX_CONF\$#d" /etc/sysupgrade.conf } extract_payload() { @@ -120,7 +234,9 @@ do_install() { [ -n "$LANIP" ] || die "could not read the LAN IP (network.lan.ipaddr)" OLDVER= - if [ -f "$APP/VERSION" ]; then + if [ -f "$WEB/VERSION" ]; then + OLDVER=$(cat "$WEB/VERSION") + elif [ -f "$APP/VERSION" ]; then OLDVER=$(cat "$APP/VERSION") elif [ -e "$APP/index.html" ] || uci -q get uhttpd.chat >/dev/null; then OLDVER="1.x" @@ -133,7 +249,6 @@ do_install() { fi # 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 @@ -143,20 +258,35 @@ do_install() { if [ "$TLS" = 1 ]; then if [ -n "$CERT_ARG" ]; then [ -s "$CERT_ARG" ] && [ -s "$KEY_ARG" ] || die "--cert and --key need two existing files (PEM)" - elif [ "$OLDCERT" = "$DATA/tls/cert.pem" ] && [ -s "$OLDCERT" ] && [ -s "$DATA/tls/key.pem" ]; then - CERT=$OLDCERT; KEY=$DATA/tls/key.pem # keep the certificate given last time + CERT=$CERT_ARG; KEY=$KEY_ARG + elif [ -s "$DATA/tls/cert.pem" ] && [ -s "$DATA/tls/key.pem" ]; then + CERT=$DATA/tls/cert.pem; KEY=$DATA/tls/key.pem # the certificate given last time elif ! admin_cert; then warn "no certificate of the router's web admin found - using plain http" TLS=0 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 + # who does https: nginx (GL.iNet) or uhttpd itself + [ -n "$NGINX_ARG" ] || { [ -f "$DATA/no-nginx" ] && NGINX_ARG=0; } + FRONT=uhttpd + if [ "$TLS" = 1 ] && [ "$NGINX_ARG" != 0 ] && have_nginx; then + FRONT=nginx + elif [ "$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 [ "$TLS" = 1 ] && SCHEME=https || SCHEME=http + # host name for https:/// (nginx only): --name, the one from last time, or a guess + if [ "$FRONT" = nginx ]; then + [ -n "$NAME" ] || NAME=$(cat "$DATA/hostname" 2>/dev/null) + [ -n "$NAME" ] || guess_name + [ "$NAME" = - ] && NAME= + else + NAME= + fi + if [ -n "$OLDVER" ]; then info "upgrading Cabin Chat $OLDVER -> $VERSION" else @@ -198,6 +328,7 @@ do_install() { [ -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 [ "$NGINX_ARG" = 0 ]; then touch "$DATA/no-nginx"; elif [ "$NGINX_ARG" = 1 ]; then rm -f "$DATA/no-nginx"; 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" @@ -205,14 +336,31 @@ do_install() { CERT=$DATA/tls/cert.pem; KEY=$DATA/tls/key.pem fi - info "configuring web server on $SCHEME://$LANIP:$PORT" + if [ "$FRONT" = nginx ]; then + info "configuring nginx for https://$LANIP:$PORT${NAME:+ and https://$NAME}" + info "certificate: $CERT" + write_nginx + if ! nginx -t >/dev/null 2>&1; then + nginx -t + rm -f "$NGINX_CONF" + warn "nginx did not accept the configuration - using uhttpd alone" + FRONT=uhttpd + NAME= + have_tls || { TLS=0; SCHEME=http; } + fi + fi uci set uhttpd.chat=uhttpd - if [ "$TLS" = 1 ]; then + if [ "$FRONT" = nginx ]; then + echo "${NAME:--}" > "$DATA/hostname" + uci add_list uhttpd.chat.listen_http="127.0.0.1:$PORT" # only for nginx + elif [ "$TLS" = 1 ]; then + info "configuring web server on https://$LANIP:$PORT" info "certificate: $CERT" uci add_list uhttpd.chat.listen_https="$LANIP:$PORT" uci set uhttpd.chat.cert="$CERT" uci set uhttpd.chat.key="$KEY" else + info "configuring web server on http://$LANIP:$PORT" uci add_list uhttpd.chat.listen_http="$LANIP:$PORT" fi uci set uhttpd.chat.home="$WEB" @@ -227,35 +375,57 @@ do_install() { uci commit uhttpd /etc/init.d/uhttpd enable /etc/init.d/uhttpd restart + [ "$FRONT" = nginx ] && nginx_reload info "adding DNS name $DNSNAME" uci add_list dhcp.@dnsmasq[0].address="/$DNSNAME/$LANIP" + # the name must resolve without internet too + if [ -n "$NAME" ] && ! uci -q get dhcp.@dnsmasq[0].address | tr ' ' '\n' | grep -q "^/$NAME/"; then + info "adding DNS name $NAME" + uci add_list dhcp.@dnsmasq[0].address="/$NAME/$LANIP" + echo "$NAME" > "$DATA/dns-added" + fi uci commit dhcp /etc/init.d/dnsmasq restart >/dev/null 2>&1 touch /etc/sysupgrade.conf grep -qx "$APP/" /etc/sysupgrade.conf || echo "$APP/" >> /etc/sysupgrade.conf + if [ "$FRONT" = nginx ]; then + grep -qx "$NGINX_CONF" /etc/sysupgrade.conf || echo "$NGINX_CONF" >> /etc/sysupgrade.conf + fi sleep 1 - listening "$PORT" || die "uhttpd is not listening on port $PORT - check: logread | grep uhttpd" + listening "$PORT" || die "nothing is listening on port $PORT - check: logread | grep -E 'uhttpd|nginx'" if command -v wget >/dev/null; then case $(wget -q --no-check-certificate -O - "$SCHEME://$LANIP:$PORT/cgi-bin/chat?a=ping" 2>/dev/null | head -n 1) in @cabinchat*) info "self-test passed" ;; - *) warn "the chat backend did not answer as expected - check: logread | grep uhttpd" + *) warn "the chat backend did not answer as expected - check: logread | grep -E 'uhttpd|nginx'" [ "$TLS" = 1 ] && warn "if https is the problem, run the installer again with --http" ;; esac + if [ -n "$NAME" ]; then + case $(wget -q -O - "https://$NAME/cgi-bin/chat?a=ping" 2>/dev/null | head -n 1) in + @cabinchat*) info "self-test of https://$NAME passed (certificate valid)" ;; + *) warn "https://$NAME did not answer - does the certificate cover that name?" ;; + esac + fi fi n=$(grep -c . "$DATA/messages" 2>/dev/null); n=${n:-0} a=$(ls "$DATA/accounts" 2>/dev/null | grep -c .); a=${a:-0} echo 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 ] && ! 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" + if [ -n "$NAME" ]; then + echo " https://$NAME" + echo " (also $SCHEME://$LANIP:$PORT and $SCHEME://$DNSNAME:$PORT, with a certificate warning)" + else + echo " $SCHEME://$LANIP:$PORT" + echo " $SCHEME://$DNSNAME:$PORT" + 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 " For a warning-free https:/// run the installer again with --name " + echo " (a name the certificate covers: $(cert_names "$CERT" | tr '\n' ' '))" + fi fi } @@ -286,6 +456,8 @@ PORT= TLS= CERT_ARG= KEY_ARG= +NAME= +NGINX_ARG= while [ $# -gt 0 ]; do case $1 in -p|--port) [ -n "$2" ] || die "-p needs a port number"; PORT=$2; shift 2 ;; @@ -293,6 +465,10 @@ while [ $# -gt 0 ]; do --https) TLS=1; shift ;; --cert) [ -n "$2" ] || die "--cert needs a file"; CERT_ARG=$2; TLS=1; shift 2 ;; --key) [ -n "$2" ] || die "--key needs a file"; KEY_ARG=$2; shift 2 ;; + --name) [ -n "$2" ] || die "--name needs a host name"; NAME=$2; shift 2 ;; + --no-name) NAME=-; shift ;; + --no-nginx) NGINX_ARG=0; shift ;; + --nginx) NGINX_ARG=1; shift ;; [0-9]*) PORT=$1; shift ;; install|upgrade) CMD=install; shift ;; uninstall|remove) CMD=uninstall; shift ;; diff --git a/www/VERSION b/www/VERSION index fcdb2e1..1454f6e 100644 --- a/www/VERSION +++ b/www/VERSION @@ -1 +1 @@ -4.0.0 +4.0.1 diff --git a/www/cgi-bin/chat b/www/cgi-bin/chat index c9eccf0..439306a 100755 --- a/www/cgi-bin/chat +++ b/www/cgi-bin/chat @@ -213,7 +213,7 @@ needs_prune() { del_photos() { [ -f "$DATA/.del" ] || return 0 while IFS= read -r f; do - case $f in .*|*[!0-9a-f.-]*) ;; *.jpg) rm -f "$IMG/$f" ;; esac + case $f in .*|*[!0-9a-z.-]*) ;; *.jpg) rm -f "$IMG/$f" ;; esac done < "$DATA/.del" rm -f "$DATA/.del" }