Files
cabinchat/installer.sh.in
T
Pascal SuterandClaude Opus 5.5 907a54eb57 4.0.1: serve the chat through nginx on GL.iNet, https://<name>/ support
- GL.iNet: nginx terminates https with the web admin's certificate and
  serves static files (conf.d/zz-cabinchat.conf, after gl.conf so the
  admin stays nginx's default server); /cgi-bin/ goes to uhttpd on
  127.0.0.1 (fcgiwrap is shared with the admin and runs only 4 scripts)
- --name <host>: also serve https://<host>/ on port 443 plus an http
  redirect, add the name to dnsmasq; guessed from dnsmasq entries that
  contain "chat" and match the certificate; server_name as regex since
  GL's server_names_hash_bucket_size is too small
- config checked with nginx -t, falls back to uhttpd; kept on firmware
  updates via /etc/sysupgrade.conf
- fix: TLS detection failed when /usr/lib/libustream-ssl.so is missing
- fix: previous version read from the wrong place ("upgrading 1.x")
- fix: photos of expired messages and deleted accounts were never
  removed (file name check rejected ".jpg")

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:49:56 +01:00

489 lines
17 KiB
Bash

#!/bin/sh
# Cabin Chat @@VERSION@@ - 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, settings)
# sh install-cabinchat.sh -p 8090 install/upgrade on a specific port
# sh install-cabinchat.sh --name chat.example.com also serve https://<name>/
# 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="@@VERSION@@"
APP=/www-chat
WEB=$APP/htdocs
DATA=$APP/data
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,18p' "$0" | sed 's/^# \{0,1\}//'
else echo "usage: sh install-cabinchat.sh [-p <port>] [--name <host>] [--http | --cert <file> --key <file>] [--no-nginx] [wipe | uninstall]"; fi
}
listening() { netstat -tln 2>/dev/null | grep -qE "[:.]$1[[:space:]]"; }
configured_port() {
{ uci -q get uhttpd.chat.listen_https; uci -q get uhttpd.chat.listen_http; } |
tr ' ' '\n' | sed -n 's/.*:\([0-9][0-9]*\)$/\1/p' | head -n 1
}
# "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
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
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() {
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 <<EOF
ssl_certificate $CERT;
ssl_certificate_key $KEY;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_tickets off;
root $WEB;
index index.html;
location / {
add_header Cache-Control "no-cache";
}
location /img/ {
add_header Cache-Control "public, max-age=2592000, immutable";
}
location /cgi-bin/ {
proxy_pass http://127.0.0.1:$PORT;
proxy_set_header Host \$host;
proxy_buffering off;
# long polls wait up to 25 s
proxy_read_timeout 90s;
}
}
EOF
[ -n "$NAME" ] && cat <<EOF
server {
listen 80;
listen [::]:80;
server_name $NAMERE;
return 301 https://\$host\$request_uri;
}
EOF
} > "$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
uci delete uhttpd.chat
uci commit uhttpd
fi
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
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() {
rm -rf "$STAGE"
mkdir -p "$STAGE" || die "cannot create $STAGE"
@@PAYLOAD@@
[ -f "$STAGE/index.html" ] && [ -f "$STAGE/cgi-bin/chat" ] && [ -f "$STAGE/VERSION" ] \
|| die "installer payload is incomplete"
}
# version 2 lines (6 fields) -> version 3 (8 fields: + recipient "*" and no quote)
migrate_messages() {
[ -s "$DATA/messages" ] || return 0
awk -F '\t' 'BEGIN { OFS = "\t" }
NF == 6 { print $1, $2, $3, $4, "*", 0, $5, $6; next }
NF == 8 { print }' "$DATA/messages" > "$DATA/messages.new" &&
mv "$DATA/messages.new" "$DATA/messages"
}
# ------------------------------------------------------------------ install
do_install() {
[ -x /usr/sbin/uhttpd ] || die "uhttpd is not installed.
With internet access run: opkg update && opkg install uhttpd
then run this installer again."
LANIP=$(uci -q get network.lan.ipaddr | cut -d/ -f1)
[ -n "$LANIP" ] || die "could not read the LAN IP (network.lan.ipaddr)"
OLDVER=
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"
fi
OLDPORT=$(configured_port)
[ -n "$PORT" ] || PORT=${OLDPORT:-$DEFAULT_PORT}
case $PORT in ''|*[!0-9]*) die "invalid port: $PORT" ;; esac
if [ "$PORT" != "$OLDPORT" ] && listening "$PORT"; then
die "port $PORT is already in use - choose another one with -p <port>"
fi
# https unless asked otherwise; once --http was given, upgrades stay on http
TLS_ARG=$TLS
if [ -z "$TLS" ]; then
TLS=1
[ -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
if [ -n "$CERT_ARG" ]; then
[ -s "$CERT_ARG" ] && [ -s "$KEY_ARG" ] || die "--cert and --key need two existing files (PEM)"
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
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://<name>/ (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
info "installing Cabin Chat $VERSION"
fi
# unpack first: if anything is wrong with this file, nothing has been touched yet
extract_payload
if [ -n "$OLDVER" ]; then
info "removing the previous version (messages and photos are kept)"
fi
remove_config
mkdir -p "$APP" || die "cannot create $APP"
mkdir -p "$DATA"
# keep the photos aside (version 2 kept them in data/img, version 3 in htdocs/img)
KEEPIMG=$APP/.img-keep
mkdir -p "$KEEPIMG"
for d in "$WEB/img" "$DATA/img"; do
[ -d "$d" ] && mv "$d"/* "$KEEPIMG"/ 2>/dev/null
rm -rf "$d"
done
for f in "$APP"/* "$APP"/.[!.]*; do
[ -e "$f" ] || continue
case $f in "$DATA"|"$KEEPIMG") continue ;; esac
rm -rf "$f"
done
rm -rf /tmp/chat.log /tmp/chat.lock /tmp/cabinchat.lock # leftovers from versions 1 and 2
migrate_messages
info "installing files to $WEB"
mkdir -p "$WEB"
cp -R "$STAGE"/* "$WEB"/ || die "copying files failed"
rm -rf "$STAGE"
chmod 755 "$WEB"/cgi-bin/*
mkdir -p "$WEB/img"
mv "$KEEPIMG"/* "$WEB/img"/ 2>/dev/null
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 [ "$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"
chmod 600 "$DATA/tls/key.pem"
CERT=$DATA/tls/cert.pem; KEY=$DATA/tls/key.pem
fi
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 [ "$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"
uci set uhttpd.chat.cgi_prefix='/cgi-bin'
uci set uhttpd.chat.index_page='index.html'
uci set uhttpd.chat.no_dirlists='1'
# long polling keeps one request per open chat waiting for up to 25 s
uci set uhttpd.chat.script_timeout='60'
uci set uhttpd.chat.network_timeout='60'
uci set uhttpd.chat.max_requests='100'
uci set uhttpd.chat.max_connections='100'
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 "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 -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)"
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://<name>/ run the installer again with --name <name>"
echo " (a name the certificate covers: $(cert_names "$CERT" | tr '\n' ' '))"
fi
fi
}
# ------------------------------------------------------------------ uninstall
do_uninstall() {
info "removing Cabin Chat"
remove_config
rm -rf "$APP" "$CLOCK" "$RUN" /tmp/chat.log /tmp/chat.lock /tmp/cabinchat.lock
/etc/init.d/uhttpd restart >/dev/null 2>&1
/etc/init.d/dnsmasq restart >/dev/null 2>&1
info "done"
}
do_wipe() {
[ -d "$APP" ] || die "Cabin Chat is not installed"
rm -rf "$DATA/messages" "$DATA/seq" "$WEB/img"
mkdir -p "$WEB/img"
g=$(cat "$DATA/gen" 2>/dev/null); case $g in ''|*[!0-9]*) g=0 ;; esac
echo $((g + 1)) > "$DATA/gen"
# wake up waiting browsers so they notice the empty chat
[ -d "$RUN" ] && echo "wipe.$$" > "$RUN/ev"
info "all messages and photos deleted (accounts are kept)"
}
# ------------------------------------------------------------------ main
CMD=install
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 ;;
--http|--no-https) TLS=0; shift ;;
--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 ;;
wipe) CMD=wipe; shift ;;
-h|--help|help) usage; exit 0 ;;
*) usage; die "unknown argument: $1" ;;
esac
done
[ "$(id -u)" = "0" ] || die "please run as root"
command -v uci >/dev/null || die "uci not found - is this OpenWrt?"
case $CMD in
install) do_install ;;
uninstall) do_uninstall ;;
wipe) do_wipe ;;
esac