diff --git a/README.md b/README.md
index 5c08feb..3d7d853 100644
--- a/README.md
+++ b/README.md
@@ -48,11 +48,19 @@ This is early. What is built:
- **`--proxy snippets`** -- for a machine that already runs nginx or Caddy:
nothing here takes 80 or 443, and the installer writes
- `
/proxy/nginx-inbuxa.conf` and `/proxy/Caddyfile.fragment` for
- that web server to load, trusts it in front of the suite (its
- X-Forwarded-For, and an exemption from the auto-ban), and leaves loading
- and reloading to you. Once it is loaded, the mail server's own
- certificates are turned on in the console. `--proxy none` writes nothing.
+ `/proxy/nginx-inbuxa.conf` with `/proxy/nginx-inbuxa-stream.conf`,
+ and `/proxy/Caddyfile.fragment`, for that web server to load, trusts
+ it in front of the suite (its X-Forwarded-For, and an exemption from the
+ auto-ban), and leaves loading and reloading to you. nginx routes 443 by
+ name: it terminates the console and the webmail with your certificates and
+ passes every other name, untouched and with the PROXY protocol header, to
+ the mail server's own HTTPS on 127.0.0.1:8443. So the server serves its own
+ certificates for every domain it has, a domain added later included, and
+ its `_443._tcp` TLSA records match. Caddy can't route TLS by name without
+ a plugin, so its fragment terminates the server's names itself. An install
+ made before nginx worked this way keeps its layout until `install` is run
+ again with `--key`. Once it is loaded, the mail server's own certificates
+ are turned on in the console. `--proxy none` writes nothing.
- **`survey`** -- the machine's facts: distribution, init, whether Docker is
usable *by this user*, Node's version, which of the suite's ports are free
diff --git a/cmd/inbuxa/main.go b/cmd/inbuxa/main.go
index 8d2fce1..29eb112 100644
--- a/cmd/inbuxa/main.go
+++ b/cmd/inbuxa/main.go
@@ -83,7 +83,9 @@ install flags:
machine lacks, rather than refusing over it
--key KEY an API key made in the console, needed only to
move the server between a container and the host
- (run install again with the other --server);
+ (run install again with the other --server), and
+ to move a --proxy snippets install made before
+ nginx passed the server's names through to it;
"-" reads it from standard input, and
INBUXA_API_KEY works too
--dry-run print the plan and stop
@@ -252,7 +254,7 @@ func install(args []string) int {
fmt.Fprintln(os.Stderr, "\nNothing has happened yet. Pass --yes to carry this out.")
return 1
}
- if moving {
+ if moving || *key != "" {
k, err := readKey(*key)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
diff --git a/e2e/cases/all-host.sh b/e2e/cases/all-host.sh
index 45058c3..3b0ab1c 100644
--- a/e2e/cases/all-host.sh
+++ b/e2e/cases/all-host.sh
@@ -20,7 +20,7 @@ D=example.test
[ -f /tmp/inbuxa-webmail-test.tar.gz ] && export INBUXA_WEBMAIL_TARBALL=/tmp/inbuxa-webmail-test.tar.gz
echo "==> a machine with nginx, and no container runtime"
-DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nginx openssl >/dev/null 2>&1
+DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nginx libnginx-mod-stream openssl >/dev/null 2>&1
systemctl is-active --quiet nginx && ok "nginx is running" || { bad "no nginx"; exit 1; }
echo
@@ -41,6 +41,7 @@ for n in mail.$D admin.$D webmail.$D; do
-keyout /etc/ssl/inbuxa/$n.key.pem -out /etc/ssl/inbuxa/$n.fullchain.pem 2>/dev/null
done
cp $DIR/proxy/nginx-inbuxa.conf /etc/nginx/conf.d/inbuxa.conf
+echo "include $DIR/proxy/nginx-inbuxa-stream.conf;" >> /etc/nginx/nginx.conf
nginx -t >/dev/null 2>&1 && ok "nginx -t accepts it" || { bad "nginx -t"; nginx -t 2>&1 | sed 's/^/ /'; }
systemctl reload nginx; sleep 3
via() { curl -sk --resolve "$1:443:127.0.0.1" "${@:2}"; }
@@ -53,6 +54,12 @@ via admin.$D -D - -o /dev/null "https://admin.$D$ASSET" | grep -qi "cache-contro
via webmail.$D https://webmail.$D/api/health | grep -q '"ok":true' && ok "the webmail is healthy through nginx" || bad "the webmail through nginx"
CODE=$(via mail.$D -o /dev/null -w '%{http_code}' https://mail.$D/.well-known/jmap)
case "$CODE" in 30*|200|401) ok "the mail server through nginx ($CODE)" ;; *) bad "the server through nginx: $CODE" ;; esac
+# Passed through to the server's own HTTPS on loopback, not nginx's (#48).
+MAIL_FP=$(echo | timeout 10 openssl s_client -connect 127.0.0.1:443 -servername mail.$D 2>/dev/null | openssl x509 -noout -fingerprint -sha256 2>/dev/null)
+[ -n "$MAIL_FP" ] && [ "$MAIL_FP" != "$(openssl x509 -in /etc/ssl/inbuxa/mail.$D.fullchain.pem -noout -fingerprint -sha256)" ] \
+ && ok "mail.$D is served the server's own certificate" || bad "mail.$D is served nginx's certificate"
+ss -Hltn 'sport = :8443' | grep -q '127.0.0.1:8443' && ! ss -Hltn 'sport = :8443' | grep -qv '127.0.0.1:8443' \
+ && ok "the server's HTTPS is on 127.0.0.1:8443 only" || bad "8443: $(ss -Hltn 'sport = :8443' | awk '{print $4}' | tr '\n' ' ')"
# The server gives a code for the webmail's own address only if it knows the
# webmail's client: registered from the secret in its environment.
USER=$(awk '/^first mailbox/ {print $3}' "$DIR/credentials.txt")
diff --git a/e2e/cases/proxy-snippets-upgrade.sh b/e2e/cases/proxy-snippets-upgrade.sh
new file mode 100755
index 0000000..380c037
--- /dev/null
+++ b/e2e/cases/proxy-snippets-upgrade.sh
@@ -0,0 +1,108 @@
+#!/bin/bash
+# SPDX-FileCopyrightText: 2026 Coffey Labs LLC
+# SPDX-License-Identifier: AGPL-3.0-or-later
+#
+# A --proxy snippets install from before nginx routed 443 by name (#48): run
+# again without a key it keeps the layout it has, since a new file in front of
+# a server not fitted for it would break its names; with a key it moves over.
+#
+# Run from the host with: OLD_REF= e2e/vm/run.sh e2e/cases/proxy-snippets-upgrade.sh
+set -uo pipefail
+
+pass=0; fail=0
+ok() { echo " ok $*"; pass=$((pass+1)); }
+bad() { echo " FAIL $*"; fail=$((fail+1)); }
+
+DIR=/var/lib/inbuxa
+D=example.test
+SERVER=http://127.0.0.1:8081
+[ -x /tmp/inbuxa-old ] || { echo "needs OLD_REF: the installer before #48"; exit 1; }
+
+rt() { command -v docker >/dev/null 2>&1 && echo docker || echo podman; }
+compose() {
+ if [ "$(rt)" = docker ]; then
+ docker compose -f "$DIR/compose.yaml" "$@"
+ else
+ DOCKER_HOST=unix:///run/podman/podman.sock \
+ /usr/local/lib/docker/cli-plugins/docker-compose -f "$DIR/compose.yaml" "$@"
+ fi
+}
+jmap() { # jmap TOKEN-or-user:pass JSON-CALLS
+ local auth
+ case "$1" in *:*) auth=(-u "$1") ;; *) auth=(-H "Authorization: Bearer $1") ;; esac
+ curl -s "${auth[@]}" -H 'Content-Type: application/json' \
+ -d "{\"using\":[\"urn:ietf:params:jmap:core\",\"urn:inbuxa:jmap:registry\"],\"methodCalls\":$2}" "$SERVER/jmap/"
+}
+wait_server() { for _ in $(seq 1 60); do [ "$(curl -s -o /dev/null -w '%{http_code}' $SERVER/.well-known/jmap)" != 000 ] && return 0; sleep 2; done; return 1; }
+via() { curl -sk --resolve "$1:443:127.0.0.1" "${@:2}"; }
+fp() { echo | timeout 10 openssl s_client -connect 127.0.0.1:443 -servername "$1" 2>/dev/null | openssl x509 -noout -fingerprint -sha256 2>/dev/null; }
+reload() { OUT="$(nginx -t 2>&1)" && ok "nginx -t accepts it" || { bad "nginx -t"; echo "$OUT" | sed 's/^/ /'; }; systemctl reload nginx; sleep 3; }
+
+echo "==> installed with the installer before #48, behind nginx"
+DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nginx libnginx-mod-stream openssl >/dev/null 2>&1
+OUT="$(/tmp/inbuxa-old install --domain $D --proxy snippets --install-deps --yes 2>&1)"; rc=$?
+[ $rc -eq 0 ] && ok "installed (exit 0)" || { bad "exit $rc"; echo "$OUT" | tail -8 | sed 's/^/ /'; }
+mkdir -p /etc/ssl/inbuxa
+for n in mail.$D admin.$D webmail.$D; do
+ openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -days 2 -subj "/CN=$n" \
+ -addext "subjectAltName=DNS:$n,DNS:autoconfig.$D,DNS:autodiscover.$D,DNS:mta-sts.$D,DNS:ua-auto-config.$D" \
+ -keyout /etc/ssl/inbuxa/$n.key.pem -out /etc/ssl/inbuxa/$n.fullchain.pem 2>/dev/null
+done
+cp $DIR/proxy/nginx-inbuxa.conf /etc/nginx/conf.d/inbuxa.conf
+reload
+NGINX_MAIL_FP=$(openssl x509 -in /etc/ssl/inbuxa/mail.$D.fullchain.pem -noout -fingerprint -sha256)
+[ "$(fp mail.$D)" = "$NGINX_MAIL_FP" ] && ok "mail.$D is served nginx's certificate, as before" || bad "mail.$D before: $(fp mail.$D)"
+
+echo
+echo "==> run again with this installer, without a key: nothing moves"
+OUT="$(INBUXA_API_KEY= /tmp/inbuxa install --domain $D --proxy snippets --yes 2>&1)"; rc=$?
+[ $rc -eq 0 ] && ok "converged (exit 0)" || { bad "exit $rc"; echo "$OUT" | tail -8 | sed 's/^/ /'; }
+grep -q "run this again with --key" <<<"$OUT" && ok "it says how to move over" || bad "no word about --key"
+[ -f $DIR/proxy/nginx-inbuxa-stream.conf ] && bad "a name router was written for a server not fitted for it" || ok "no name router written"
+grep -q "listen 443 ssl;" $DIR/proxy/nginx-inbuxa.conf && grep -q -- "--key" $DIR/proxy/nginx-inbuxa.conf \
+ && ok "nginx-inbuxa.conf keeps its layout, and says how to move" || bad "nginx-inbuxa.conf changed"
+grep -q ':443"' $DIR/compose.yaml && bad "the server's HTTPS was published anyway" || ok "compose.yaml publishes no HTTPS"
+grep -q '"passThrough"' /etc/inbuxa/install.json && bad "the install record says passThrough" || ok "the install record doesn't say passThrough"
+cp $DIR/proxy/nginx-inbuxa.conf /etc/nginx/conf.d/inbuxa.conf
+reload
+[ "$(fp mail.$D)" = "$NGINX_MAIL_FP" ] && ok "mail.$D still works as it did" || bad "mail.$D after a run without a key: $(fp mail.$D)"
+
+echo
+echo "==> an API key, as an administrator makes one in the console"
+AUSER=$(awk '/^administrator/ {print $2}' "$DIR/credentials.txt")
+APASS=$(awk '/^administrator/{getline; print $2}' "$DIR/credentials.txt")
+printf 'services:\n server:\n environment:\n INBUXA_HTTP_BASIC_AUTH: all\n' > /tmp/basic.yaml
+compose -f /tmp/basic.yaml up -d server >/dev/null 2>&1; wait_server
+EXPIRES=$(date -u -d '+1 hour' +%Y-%m-%dT%H:%M:%SZ)
+ANSWER=$(jmap "$AUSER:$APASS" "[[\"x:ApiKey/set\",{\"create\":{\"k\":{\"description\":\"snippets upgrade test\",\"expiresAt\":\"$EXPIRES\",\"permissions\":{\"@type\":\"Inherit\"}}}},\"0\"]]")
+KEY=$(sed -n 's/.*"secret":"\([^"]*\)".*/\1/p' <<<"$ANSWER")
+compose up -d server >/dev/null 2>&1; wait_server
+[ -n "$KEY" ] && ok "an API key for the administrator" || { bad "no API key"; head -c 300 <<<"$ANSWER" | sed 's/^/ /'; }
+
+echo
+echo "==> with the key: moved over"
+OUT="$(echo "$KEY" | /tmp/inbuxa install --domain $D --proxy snippets --key - --yes 2>&1)"; rc=$?
+[ $rc -eq 0 ] && ok "converged (exit 0)" || { bad "exit $rc"; echo "$OUT" | tail -8 | sed 's/^/ /'; }
+grep -q "fitting the server's HTTPS" <<<"$OUT" && ok "it fitted the server's HTTPS listener" || bad "no listener step"
+[ -f $DIR/proxy/nginx-inbuxa-stream.conf ] && ok "the name router is written" || bad "no nginx-inbuxa-stream.conf"
+grep -q '"127.0.0.1:8443:443"' $DIR/compose.yaml && ok "the server's HTTPS is published on 127.0.0.1:8443" || bad "8443 is not published"
+grep -q '"passThrough": true' /etc/inbuxa/install.json && ok "the install record says passThrough" || bad "no passThrough in the install record"
+cp $DIR/proxy/nginx-inbuxa.conf /etc/nginx/conf.d/inbuxa.conf
+echo "include $DIR/proxy/nginx-inbuxa-stream.conf;" >> /etc/nginx/nginx.conf
+reload
+MAIL_FP=$(fp mail.$D)
+[ -n "$MAIL_FP" ] && [ "$MAIL_FP" != "$NGINX_MAIL_FP" ] && ok "mail.$D is now the server's own certificate" || bad "mail.$D after the move: '${MAIL_FP:-nothing}'"
+CODE=$(via mail.$D -o /dev/null -w '%{http_code}' https://mail.$D/.well-known/jmap)
+case "$CODE" in 30*|200|401) ok "and the server answers through it ($CODE)" ;; *) bad "https://mail.$D answered $CODE" ;; esac
+via admin.$D https://admin.$D/ | grep -q "content=\"https://mail.$D\"" && ok "the console still through nginx" || bad "the console"
+via webmail.$D https://webmail.$D/api/health | grep -q '"ok":true' && ok "the webmail still through nginx" || bad "the webmail"
+
+echo
+echo "==> again, with no key: stays moved"
+OUT="$(INBUXA_API_KEY= /tmp/inbuxa install --domain $D --proxy snippets --yes 2>&1)"; rc=$?
+[ $rc -eq 0 ] && [ -f $DIR/proxy/nginx-inbuxa-stream.conf ] && ok "converged, router kept" || { bad "exit $rc"; echo "$OUT" | tail -6 | sed 's/^/ /'; }
+[ "$(fp mail.$D)" = "$MAIL_FP" ] && ok "mail.$D still the server's" || bad "mail.$D changed"
+
+echo
+echo "==> $pass passed, $fail failed"
+[ "$fail" -eq 0 ]
diff --git a/e2e/cases/proxy-snippets.sh b/e2e/cases/proxy-snippets.sh
index d9a591a..ce33ef4 100644
--- a/e2e/cases/proxy-snippets.sh
+++ b/e2e/cases/proxy-snippets.sh
@@ -1,11 +1,9 @@
-#!/bin/bash
-# SPDX-FileCopyrightText: 2026 Coffey Labs LLC
-# SPDX-License-Identifier: AGPL-3.0-or-later
-#
# --proxy snippets on a machine that already runs nginx: the installer takes
# neither 80 nor 443, writes the configuration for nginx, and the suite is
-# then reached through nginx on its real names once that file is loaded --
-# certificates being the operator's, self-signed here.
+# then reached through nginx on its real names once that file is loaded.
+# nginx routes 443 by name (#48): it terminates the console and the webmail,
+# with the operator's certificates (self-signed here), and passes every other
+# name through to the mail server's own HTTPS, a domain added later included.
#
# Run from the host with: e2e/vm/run.sh e2e/cases/proxy-snippets.sh
set -uo pipefail
@@ -16,9 +14,10 @@ bad() { echo " FAIL $*"; fail=$((fail+1)); }
DIR=/var/lib/inbuxa
D=example.test
+SERVER=http://127.0.0.1:8081
echo "==> a machine with nginx already on it"
-DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nginx openssl >/dev/null 2>&1
+DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nginx libnginx-mod-stream openssl >/dev/null 2>&1
systemctl is-active --quiet nginx && ok "nginx is running, holding 80" || { bad "no nginx"; echo "==> $pass passed, $fail failed"; exit 1; }
echo
@@ -27,19 +26,24 @@ OUT="$(/tmp/inbuxa install --domain $D --proxy snippets --install-deps --yes 2>&
echo "$OUT" | grep -v '^ |' | tail -14 | sed 's/^/ /'
[ $rc -eq 0 ] && ok "installed (exit 0)" || bad "exit $rc"
grep -q "proxy config.*$DIR/proxy" <<<"$OUT" && ok "the summary names the proxy configuration" || bad "no proxy config in the summary"
-[ -f $DIR/proxy/nginx-inbuxa.conf ] && [ -f $DIR/proxy/Caddyfile.fragment ] && ok "nginx-inbuxa.conf and Caddyfile.fragment written" || bad "the configuration files are missing"
+[ -f $DIR/proxy/nginx-inbuxa.conf ] && [ -f $DIR/proxy/nginx-inbuxa-stream.conf ] && [ -f $DIR/proxy/Caddyfile.fragment ] \
+ && ok "nginx-inbuxa.conf, nginx-inbuxa-stream.conf and Caddyfile.fragment written" || bad "the configuration files are missing"
+grep -q '"passThrough": true' /etc/inbuxa/install.json && ok "the install record says the server's names pass through" || bad "no passThrough in the install record"
+grep -q '"127.0.0.1:8443:443"' $DIR/compose.yaml && ok "the server's own HTTPS is published on 127.0.0.1:8443" || bad "8443 is not published"
grep -q "caddy" $DIR/compose.yaml && bad "a Caddy was put here anyway" || ok "no Caddy of its own"
systemctl is-active --quiet nginx && ok "nginx was left running, untouched" || bad "nginx is not running any more"
echo
echo "==> load it, as the operator would"
mkdir -p /etc/ssl/inbuxa
-for n in mail.$D admin.$D webmail.$D; do
+# Only the front ends' certificates: the server's names are its own.
+for n in admin.$D webmail.$D; do
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -days 2 -subj "/CN=$n" \
- -addext "subjectAltName=DNS:$n,DNS:autoconfig.$D,DNS:autodiscover.$D,DNS:mta-sts.$D,DNS:ua-auto-config.$D" \
+ -addext "subjectAltName=DNS:$n" \
-keyout /etc/ssl/inbuxa/$n.key.pem -out /etc/ssl/inbuxa/$n.fullchain.pem 2>/dev/null
done
cp $DIR/proxy/nginx-inbuxa.conf /etc/nginx/conf.d/inbuxa.conf
+echo "include $DIR/proxy/nginx-inbuxa-stream.conf;" >> /etc/nginx/nginx.conf
OUT="$(nginx -t 2>&1)" && ok "nginx -t accepts it" || { bad "nginx -t"; echo "$OUT" | sed 's/^/ /'; }
systemctl reload nginx
# A reload returns before nginx's old workers have finished; until they
@@ -49,10 +53,23 @@ sleep 3
echo
echo "==> the suite, through nginx"
via() { curl -sk --resolve "$1:443:127.0.0.1" --resolve "$1:80:127.0.0.1" "${@:2}"; }
+fp() { echo | timeout 10 openssl s_client -connect "${2:-127.0.0.1:443}" -servername "$1" 2>/dev/null | openssl x509 -noout -fingerprint -sha256 2>/dev/null; }
CODE=$(via mail.$D -o /dev/null -w '%{http_code}' https://mail.$D/.well-known/jmap)
case "$CODE" in 30*|200|401) ok "the mail server answers on https://mail.$D ($CODE)" ;; *) bad "https://mail.$D/.well-known/jmap answered $CODE" ;; esac
via admin.$D https://admin.$D/ | grep -q "content=\"https://mail.$D\"" && ok "the console, pointed at https://mail.$D" || bad "the console is not served through nginx"
via webmail.$D https://webmail.$D/api/health | grep -q '"ok":true' && ok "the webmail is healthy through nginx" || bad "the webmail is not healthy through nginx"
+ADMIN_FP=$(openssl x509 -in /etc/ssl/inbuxa/admin.$D.fullchain.pem -noout -fingerprint -sha256)
+WEBMAIL_FP=$(openssl x509 -in /etc/ssl/inbuxa/webmail.$D.fullchain.pem -noout -fingerprint -sha256)
+[ "$(fp admin.$D)" = "$ADMIN_FP" ] && ok "the console is served with nginx's certificate" || bad "the console's certificate is not nginx's"
+MAIL_FP=$(fp mail.$D)
+if [ -n "$MAIL_FP" ] && [ "$MAIL_FP" != "$ADMIN_FP" ] && [ "$MAIL_FP" != "$WEBMAIL_FP" ]; then
+ ok "mail.$D is passed through to the server's own certificate"
+else
+ bad "mail.$D is served '${MAIL_FP:-nothing}'"
+fi
+# The server takes the PROXY protocol header from nginx, and so refuses a
+# connection from there without one.
+fp mail.$D 127.0.0.1:8443 | grep -q Fingerprint && bad "the server's HTTPS answers without the PROXY header" || ok "the server's HTTPS takes nothing without nginx's PROXY header"
# The mail server's own 404 is a JSON problem document; nginx's is a page.
BODY=$(via mail.$D http://mail.$D/.well-known/acme-challenge/probe)
grep -q "/dev/null | grep -n -E "^# configuration file|listen|server_name" | head -40 | sed 's/^/ /'
fi
+echo
+echo "==> a domain added later"
+rt() { command -v docker >/dev/null 2>&1 && echo docker || echo podman; }
+compose() {
+ if [ "$(rt)" = docker ]; then
+ docker compose -f "$DIR/compose.yaml" "$@"
+ else
+ DOCKER_HOST=unix:///run/podman/podman.sock \
+ /usr/local/lib/docker/cli-plugins/docker-compose -f "$DIR/compose.yaml" "$@"
+ fi
+}
+jmap() { # jmap TOKEN-or-user:pass JSON-CALLS
+ local auth
+ case "$1" in *:*) auth=(-u "$1") ;; *) auth=(-H "Authorization: Bearer $1") ;; esac
+ curl -s "${auth[@]}" -H 'Content-Type: application/json' \
+ -d "{\"using\":[\"urn:ietf:params:jmap:core\",\"urn:inbuxa:jmap:registry\"],\"methodCalls\":$2}" "$SERVER/jmap/"
+}
+wait_server() { for _ in $(seq 1 60); do [ "$(curl -s -o /dev/null -w '%{http_code}' $SERVER/.well-known/jmap)" != 000 ] && return 0; sleep 2; done; return 1; }
+AUSER=$(awk '/^administrator/ {print $2}' "$DIR/credentials.txt")
+APASS=$(awk '/^administrator/{getline; print $2}' "$DIR/credentials.txt")
+printf 'services:\n server:\n environment:\n INBUXA_HTTP_BASIC_AUTH: all\n' > /tmp/basic.yaml
+compose -f /tmp/basic.yaml up -d server >/dev/null 2>&1; wait_server
+EXPIRES=$(date -u -d '+1 hour' +%Y-%m-%dT%H:%M:%SZ)
+ANSWER=$(jmap "$AUSER:$APASS" "[[\"x:ApiKey/set\",{\"create\":{\"k\":{\"description\":\"snippets test\",\"expiresAt\":\"$EXPIRES\",\"permissions\":{\"@type\":\"Inherit\"}}}},\"0\"]]")
+KEY=$(sed -n 's/.*"secret":"\([^"]*\)".*/\1/p' <<<"$ANSWER")
+compose up -d server >/dev/null 2>&1; wait_server
+[ -n "$KEY" ] && ok "an API key for the administrator" || { bad "no API key"; head -c 300 <<<"$ANSWER" | sed 's/^/ /'; }
+MADE=$(jmap "$KEY" '[["x:Domain/set",{"create":{"d":{"name":"second.test"}}},"0"]]')
+grep -q '"created"' <<<"$MADE" && ok "second.test added on the server, not in nginx" || { bad "no second.test"; head -c 300 <<<"$MADE" | sed 's/^/ /'; }
+sleep 2
+LATER_FP=$(fp mta-sts.second.test)
+if [ -n "$LATER_FP" ] && [ "$LATER_FP" != "$ADMIN_FP" ] && [ "$LATER_FP" != "$WEBMAIL_FP" ]; then
+ ok "mta-sts.second.test reaches the server's own certificate through nginx"
+else
+ bad "mta-sts.second.test is served '${LATER_FP:-nothing}'"
+fi
+CODE=$(via mta-sts.second.test -o /dev/null -w '%{http_code}' https://mta-sts.second.test/.well-known/mta-sts.txt)
+[ "$CODE" != 000 ] && ok "and the server answers HTTPS for it ($CODE)" || bad "no HTTPS answer for mta-sts.second.test"
+BODY=$(via mta-sts.second.test http://mta-sts.second.test/.well-known/acme-challenge/probe)
+grep -q " again: converges, and rewrites the file for what is here"
-rm $DIR/proxy/nginx-inbuxa.conf
-OUT="$(/tmp/inbuxa install --domain $D --proxy snippets --yes 2>&1)"; rc=$?
-[ $rc -eq 0 ] && [ -f $DIR/proxy/nginx-inbuxa.conf ] && ok "a second run converges and writes it again" || { bad "second run exit $rc"; echo "$OUT" | tail -6 | sed 's/^/ /'; }
+rm $DIR/proxy/nginx-inbuxa.conf $DIR/proxy/nginx-inbuxa-stream.conf
+OUT="$(INBUXA_API_KEY= /tmp/inbuxa install --domain $D --proxy snippets --yes 2>&1)"; rc=$?
+[ $rc -eq 0 ] && [ -f $DIR/proxy/nginx-inbuxa.conf ] && [ -f $DIR/proxy/nginx-inbuxa-stream.conf ] \
+ && ok "a second run converges and writes both again, without a key" || { bad "second run exit $rc"; echo "$OUT" | tail -6 | sed 's/^/ /'; }
+# The server's own certificate here is self-signed, and may be a new one by
+# now: what matters is that it is still the server's, not nginx's.
+for _ in $(seq 1 20); do AGAIN_FP=$(fp mail.$D); [ -n "$AGAIN_FP" ] && break; sleep 3; done
+if [ -n "$AGAIN_FP" ] && [ "$AGAIN_FP" != "$ADMIN_FP" ] && [ "$AGAIN_FP" != "$WEBMAIL_FP" ]; then
+ ok "mail.$D still passes through to the server$([ "$AGAIN_FP" = "$MAIL_FP" ] || echo ', with a new self-signed certificate')"
+else
+ bad "mail.$D after the second run: '${AGAIN_FP:-nothing}'"
+fi
echo
echo "==> $pass passed, $fail failed"
diff --git a/internal/apply/apply.go b/internal/apply/apply.go
index 31859a8..b29517d 100644
--- a/internal/apply/apply.go
+++ b/internal/apply/apply.go
@@ -146,6 +146,23 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro
WebmailBind: "127.0.0.1:8080",
Subnet: DefaultSubnet,
}
+ // With --proxy snippets the operator's nginx routes 443 by name, and the
+ // server serves its own names with its own certificates (#48). A new
+ // install is set up that way. One from before keeps the layout it has
+ // until it is run with an API key, which fitting the server's listener
+ // takes: a rewritten nginx file in front of a server not fitted for it
+ // would break every one of the server's names at the next reload.
+ snippets := !o.Local && o.Proxy == "snippets"
+ fitRouter := false
+ if snippets {
+ prior, err := state.Load()
+ switch {
+ case err != nil || !prior.Installed() || prior.PassThrough:
+ stack.PassThrough = true
+ case o.Key != "":
+ stack.PassThrough, fitRouter = true, true
+ }
+ }
if o.Local {
// Nothing is published and nothing is certified: the addresses are
// the loopback binds, which is what the front ends are told to use.
@@ -289,12 +306,28 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro
// front end used to do, and why it could not.
if existing, err := state.Load(); err == nil && existing.Installed() {
if mv != nil {
- if err := mv.finish(ctx, stack, !o.Local && o.Proxy == "snippets"); err != nil {
+ if err := mv.finish(ctx, stack, snippets); err != nil {
return nil, err
}
// What it reads at start has been read: nothing more to restart for.
serverChanged = false
}
+ // A move fits the listeners to the new shape and leaves no HTTPS
+ // listener on the host, so the router's is fitted again after one.
+ if stack.PassThrough && (fitRouter || mv != nil) {
+ log.Step("fitting the server's HTTPS to your web server's name router")
+ srv := &jmap.Client{BaseURL: "http://" + hostFinalBind, Token: o.Key}
+ if err := routeByName(ctx, srv, stack, onHost); err != nil {
+ return nil, fmt.Errorf("fitting the server's HTTPS listener: %w", err)
+ }
+ if fitRouter {
+ log.Info("its own certificates on 443 from its next start, which is now; load %s/proxy/nginx-inbuxa-stream.conf as it says, then reload nginx",
+ o.Dir)
+ }
+ existing.PassThrough, serverChanged = true, true
+ } else if snippets && !stack.PassThrough {
+ log.Info("your web server still terminates TLS for the server's names: run this again with --key to pass them through to it instead (#48)")
+ }
res, err := converge(ctx, o, stack, cmp, hs, serverChanged, existing, f, log)
if err == nil && hw != nil {
// Its files may be new, or what it reads at start; or it may
@@ -306,7 +339,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro
}
}
}
- if err == nil && !o.Local && o.Proxy == "snippets" {
+ if err == nil && snippets {
// What the web server is told follows what is installed here.
if dir, err := compose.WriteProxySnippets(o.Dir, stack); err == nil {
res.ProxyConfig = dir
@@ -503,7 +536,6 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro
// arrive from. That is loopback for a server on the host; for one in a
// container it is the project network's gateway, because a request to
// a published loopback port reaches the container from there.
- snippets := !o.Local && o.Proxy == "snippets"
if snippets {
log.Step("trusting your web server's X-Forwarded-For")
if err := srv.TrustForwardedFor(ctx); err != nil {
@@ -522,6 +554,12 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro
}
allowed[addr] = true
}
+ if stack.PassThrough {
+ log.Info("its own HTTPS on %s, for the name router in front of it", compose.PassThroughBind)
+ if err := routeByName(ctx, srv, stack, onHost); err != nil {
+ return res, fmt.Errorf("fitting the server's HTTPS listener: %w", err)
+ }
+ }
}
if stack.Webmail || stack.Proxy || snippets {
// Neither setting takes effect on a running server.
@@ -552,7 +590,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro
return res, fmt.Errorf("writing the proxy configuration: %w", err)
}
res.ProxyConfig = dir
- log.Info("%s/nginx-inbuxa.conf and %s/Caddyfile.fragment: load one into your web server", dir, dir)
+ log.Info("%s/nginx-inbuxa.conf with %s/nginx-inbuxa-stream.conf, or %s/Caddyfile.fragment: load one into your web server", dir, dir, dir)
log.Info("once it is loaded, turn on the mail server's own certificates in the console (Settings › Overview, Certificates): its challenges reach it through port 80")
} else if !o.Local && !stack.Proxy {
log.Info("no proxy here, so certificates are yours to arrange: the server's own names are %s",
@@ -597,6 +635,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro
if err == nil {
st.Machine, st.Dir, st.Domain, st.MailHost, st.Local = o.Machine, dir, o.Domain, o.MailHost, o.Local
st.Runtime, st.Topology = f.Runtime.Kind, o.TopologyPath
+ st.PassThrough = stack.PassThrough
st.Shapes = map[string]string{}
for _, c := range []plan.Component{plan.Server, plan.Console, plan.Webmail} {
if sh := o.Shape(c); sh != plan.Skip {
@@ -703,8 +742,16 @@ func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp dock
// The server reads the front-end URLs from its environment, so a front
// end that was added or removed changes what it was started with. Only
// restart when that actually changed.
- if st.Shapes["console"] != string(o.Shape(plan.Console)) || st.Shapes["webmail"] != string(o.Shape(plan.Webmail)) {
- log.Info("the front ends changed, so the server is restarted to see them")
+ //
+ // serverChanged is set for a server in a container only when its
+ // listeners were changed, which it also reads only at start.
+ frontEnds := st.Shapes["console"] != string(o.Shape(plan.Console)) || st.Shapes["webmail"] != string(o.Shape(plan.Webmail))
+ if serverChanged || frontEnds {
+ if frontEnds {
+ log.Info("the front ends changed, so the server is restarted to see them")
+ } else {
+ log.Info("restarting the server, for its new listener")
+ }
if err := cmp.Run(ctx, "restart", "server"); err != nil {
return res, err
}
@@ -723,6 +770,21 @@ func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp dock
return res, nil
}
+// routeByName fits the server's HTTPS listener to the name router in
+// nginx-inbuxa-stream.conf (#48). On the host it is on loopback, where nginx
+// reaches it. In a container it keeps its own 443, which the compose file
+// publishes on loopback, and nginx's connections arrive from the project
+// network's gateway.
+func routeByName(ctx context.Context, srv *jmap.Client, stack compose.Stack, onHost bool) error {
+ if onHost {
+ return srv.PassThroughListener(ctx, compose.PassThroughBind, []string{"127.0.0.1"})
+ }
+ if stack.GatewayIP == "" {
+ return fmt.Errorf("no gateway address for the project network %s", stack.Subnet)
+ }
+ return srv.PassThroughListener(ctx, "[::]:443", []string{stack.GatewayIP})
+}
+
// convergeHost is converge for a server on the host: the binary, the unit
// and its environment are rewritten if they changed, whatever runs in
// containers beside it is brought in line, and the server is restarted only
diff --git a/internal/compose/compose.go b/internal/compose/compose.go
index bd836f0..5519f93 100644
--- a/internal/compose/compose.go
+++ b/internal/compose/compose.go
@@ -44,6 +44,14 @@ type Stack struct {
Webmail bool
Proxy bool
+ // PassThrough is the operator's own web server (--proxy snippets)
+ // routing 443 by name: the console and the webmail it terminates
+ // itself, and every other name it passes through untouched, with the
+ // PROXY protocol header, to the server's own HTTPS listener on
+ // PassThroughBind (#48). Installs from before it terminate the server's
+ // names in that web server, until the installer is run again with --key.
+ PassThrough bool
+
// HostServer is a mail server running on this machine but outside the
// project, under systemd. The front ends and the proxy then reach it on
// loopback, so the ones that talk to it -- the webmail and Caddy -- share
@@ -326,13 +334,34 @@ func secret() (string, error) {
return base64.RawURLEncoding.EncodeToString(b), nil
}
+// Where the operator's web server and the server meet on loopback when
+// 443 is routed by name (Stack.PassThrough): the server's HTTPS listener,
+// published there from a container, and the web server's own TLS for the
+// console and the webmail, which its name router hands on to.
+const (
+ PassThroughBind = "127.0.0.1:8443"
+ FrontTLSBind = "127.0.0.1:8444"
+)
+
+// PassThroughBind and FrontTLSBind, for the templates.
+func (Stack) PassThroughBind() string { return PassThroughBind }
+func (Stack) FrontTLSBind() string { return FrontTLSBind }
+
+// RoutesByName is whether the nginx configuration routes 443 by name: only
+// with a server here to pass names through to.
+func (s Stack) RoutesByName() bool { return s.PassThrough && s.ServesServer() }
+
// ProxySnippets are the files --proxy snippets writes, for the web server the
// operator already runs: an nginx configuration and a Caddyfile fragment.
+// With RoutesByName there is a third, nginx's name router, which goes in its
+// own file because it belongs outside nginx's http block.
var ProxySnippets = map[string]string{
"nginx-inbuxa.conf.tmpl": "nginx-inbuxa.conf",
"Caddyfile.fragment.tmpl": "Caddyfile.fragment",
}
+const streamSnippet = "nginx-inbuxa-stream.conf"
+
// WriteProxySnippets writes them into dir/proxy and returns that directory.
// Nothing is loaded or reloaded: that web server is the operator's.
func WriteProxySnippets(dir string, s Stack) (string, error) {
@@ -342,7 +371,16 @@ func WriteProxySnippets(dir string, s Stack) (string, error) {
}
s.Dir = dir
funcs := template.FuncMap{"join": strings.Join}
+ files := map[string]string{}
for name, file := range ProxySnippets {
+ files[name] = file
+ }
+ if s.RoutesByName() {
+ files[streamSnippet+".tmpl"] = streamSnippet
+ } else if err := os.Remove(filepath.Join(out, streamSnippet)); err != nil && !os.IsNotExist(err) {
+ return "", err
+ }
+ for name, file := range files {
t, err := template.New(name).Funcs(funcs).ParseFS(templates, "templates/"+name)
if err != nil {
return "", err
diff --git a/internal/compose/compose_test.go b/internal/compose/compose_test.go
index 657515e..ece4cbe 100644
--- a/internal/compose/compose_test.go
+++ b/internal/compose/compose_test.go
@@ -205,3 +205,99 @@ func TestFrontEndsOnTheHost(t *testing.T) {
t.Errorf("nginx-inbuxa.conf:\n%s", nginx)
}
}
+
+// #48: with names routed on 443, the server's names are not terminated in
+// nginx at all; the router passes them, and any name it does not know, to the
+// server's own HTTPS with the PROXY protocol header.
+func TestProxySnippetsRouteByName(t *testing.T) {
+ dir := t.TempDir()
+ s := stack(false)
+ s.Proxy, s.PassThrough = false, true
+ out, err := WriteProxySnippets(dir, *s)
+ if err != nil {
+ t.Fatal(err)
+ }
+ nginx, _ := os.ReadFile(filepath.Join(out, "nginx-inbuxa.conf"))
+ stream, err := os.ReadFile(filepath.Join(out, "nginx-inbuxa-stream.conf"))
+ if err != nil {
+ t.Fatal("no stream configuration written")
+ }
+ for _, want := range []string{
+ "stream {", "ssl_preread on;", "proxy_protocol on;",
+ "admin.example.com inbuxa_front;", "webmail.example.com inbuxa_front;", "default inbuxa_server;",
+ "server 127.0.0.1:8443;", "server 127.0.0.1:8444;", "listen 443;",
+ "include " + dir + "/proxy/nginx-inbuxa-stream.conf;", "libnginx-mod-stream",
+ } {
+ if !strings.Contains(string(stream), want) {
+ t.Errorf("nginx-inbuxa-stream.conf lacks %q", want)
+ }
+ }
+ for _, want := range []string{
+ "listen 127.0.0.1:8444 ssl proxy_protocol;", "real_ip_header proxy_protocol;",
+ "~^(autoconfig|autodiscover|mta-sts|ua-auto-config)\\.;",
+ "location /.well-known/acme-challenge/",
+ } {
+ if !strings.Contains(string(nginx), want) {
+ t.Errorf("nginx-inbuxa.conf lacks %q", want)
+ }
+ }
+ for _, bad := range []string{"listen 443", "/etc/ssl/inbuxa/mail.example.com"} {
+ if strings.Contains(string(nginx), bad) {
+ t.Errorf("nginx-inbuxa.conf still has %q: the router holds 443 and the server's names are its own", bad)
+ }
+ }
+ if strings.Count(string(nginx), "listen 127.0.0.1:8444") != 2 {
+ t.Errorf("the console and the webmail are not both behind the router:\n%s", nginx)
+ }
+
+ c, _ := render(t, s)
+ if !strings.Contains(c, `- "127.0.0.1:8443:443"`) {
+ t.Errorf("the server's HTTPS is not published for the router:\n%s", c)
+ }
+}
+
+// An install from before #48, not yet run again with --key: the layout it has
+// stays, and says how to move.
+func TestProxySnippetsWithoutRoutingStayAsTheyWere(t *testing.T) {
+ dir := t.TempDir()
+ s := stack(false)
+ s.Proxy = false
+ os.MkdirAll(filepath.Join(dir, "proxy"), 0o755)
+ os.WriteFile(filepath.Join(dir, "proxy", "nginx-inbuxa-stream.conf"), []byte("stale"), 0o644)
+ out, err := WriteProxySnippets(dir, *s)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(filepath.Join(out, "nginx-inbuxa-stream.conf")); err == nil {
+ t.Error("a stream configuration is left for a layout that has none")
+ }
+ nginx, _ := os.ReadFile(filepath.Join(out, "nginx-inbuxa.conf"))
+ for _, want := range []string{"listen 443 ssl;", "/etc/ssl/inbuxa/mail.example.com.fullchain.pem", "--key", "#48"} {
+ if !strings.Contains(string(nginx), want) {
+ t.Errorf("nginx-inbuxa.conf lacks %q", want)
+ }
+ }
+ c, _ := render(t, s)
+ if strings.Contains(c, ":443\"") {
+ t.Errorf("the server's HTTPS is published without the router:\n%s", c)
+ }
+}
+
+// The front ends joined to a server elsewhere: nothing to pass through, so no
+// router, whatever PassThrough says.
+func TestProxySnippetsForJoinedFrontEndsDoNotRoute(t *testing.T) {
+ s := stack(false)
+ s.Server, s.Proxy, s.PassThrough = false, false, true
+ s.RemoteServerURL = "https://mail.example.com"
+ out, err := WriteProxySnippets(t.TempDir(), *s)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(filepath.Join(out, "nginx-inbuxa-stream.conf")); err == nil {
+ t.Error("a router was written with no server here")
+ }
+ nginx, _ := os.ReadFile(filepath.Join(out, "nginx-inbuxa.conf"))
+ if !strings.Contains(string(nginx), "listen 443 ssl;") {
+ t.Errorf("nginx-inbuxa.conf:\n%s", nginx)
+ }
+}
diff --git a/internal/compose/templates/Caddyfile.fragment.tmpl b/internal/compose/templates/Caddyfile.fragment.tmpl
index 9503cc7..639dd52 100644
--- a/internal/compose/templates/Caddyfile.fragment.tmpl
+++ b/internal/compose/templates/Caddyfile.fragment.tmpl
@@ -13,6 +13,14 @@
# The mail server's names. Caddy uses TLS-ALPN-01 for them and leaves HTTP-01
# on port 80 to the mail server, which needs a certificate of its own for
# IMAP and SMTP: so neither answers the other's challenge.
+#
+# Caddy can't pass TLS through by name without the layer4 plugin, so it
+# terminates these names itself, with certificates of its own. Two things
+# follow. A domain added to the server later is not here: add its names to
+# both blocks below. And with TLSA records published, the server's _443._tcp
+# ones describe its own certificate, not Caddy's, so they don't match on 443
+# (the mail ports are unaffected). The nginx configuration beside this passes
+# the server's names through to the server instead.
{{join .ServerNames ", "}} {
tls {
issuer acme {
diff --git a/internal/compose/templates/compose.yaml.tmpl b/internal/compose/templates/compose.yaml.tmpl
index 4eb0ecb..087a53c 100644
--- a/internal/compose/templates/compose.yaml.tmpl
+++ b/internal/compose/templates/compose.yaml.tmpl
@@ -28,6 +28,11 @@ services:
restart: unless-stopped
ports:
- "{{.ServerBind}}:8080"
+{{- if .PassThrough}}
+ # Its own HTTPS, for your web server, which passes the server's names
+ # through to it untouched.
+ - "{{.PassThroughBind}}:443"
+{{- end}}
{{- range .MailPorts}}
- "{{.}}:{{.}}"
{{- end}}
diff --git a/internal/compose/templates/nginx-inbuxa-stream.conf.tmpl b/internal/compose/templates/nginx-inbuxa-stream.conf.tmpl
new file mode 100644
index 0000000..4c87936
--- /dev/null
+++ b/internal/compose/templates/nginx-inbuxa-stream.conf.tmpl
@@ -0,0 +1,56 @@
+# Written by the inbuxa installer {{.Version}} for {{.Domain}}.
+#
+# nginx's name router for port 443, which nginx-inbuxa.conf beside it needs.
+# It goes outside the http block, so include it from the top of
+# /etc/nginx/nginx.conf, next to `events`, not from conf.d/:
+#
+# include {{.Dir}}/proxy/nginx-inbuxa-stream.conf;
+#
+# It needs nginx's stream module: libnginx-mod-stream on Debian and Ubuntu,
+# nginx-mod-stream on the Red Hat family. Check with `nginx -t` and reload.
+# The installer loads nothing and restarts nothing.
+#
+# It reads the name a client asks for (SNI) and decrypts nothing itself:
+{{- if or .Console .ConsoleFiles .ServesWebmail}}
+# - the console and the webmail go to nginx's own TLS on {{.FrontTLSBind}}
+# (in nginx-inbuxa.conf), with your certificates;
+{{- end}}
+# - every other name goes to the mail server's own HTTPS on
+# {{.PassThroughBind}}, untouched. It has certificates of its own for
+# each of its domains, a domain added later included, and its _443._tcp
+# TLSA records match what it serves.
+# Both are sent the PROXY protocol header first, so they see the client's own
+# address. The server takes that header only from where nginx connects, and
+# refuses a connection from there without it.
+#
+# nginx allows one stream block. If you have one already, move the map, the
+# two upstreams and the server into it. Any site of your own that listens on
+# 443 in the http block has to move off it, to {{.FrontTLSBind}} like the
+# console, with its name added to the map: otherwise this port is taken
+# twice, and its name would be sent to the mail server.
+stream {
+ map $ssl_preread_server_name $inbuxa_upstream {
+{{- if or .Console .ConsoleFiles}}
+ {{.ConsoleHost}} inbuxa_front;
+{{- end}}
+{{- if .ServesWebmail}}
+ {{.WebmailHost}} inbuxa_front;
+{{- end}}
+ default inbuxa_server;
+ }
+
+ upstream inbuxa_server {
+ server {{.PassThroughBind}};
+ }
+ upstream inbuxa_front {
+ server {{.FrontTLSBind}};
+ }
+
+ server {
+ listen 443;
+ listen [::]:443;
+ ssl_preread on;
+ proxy_pass $inbuxa_upstream;
+ proxy_protocol on;
+ }
+}
diff --git a/internal/compose/templates/nginx-inbuxa.conf.tmpl b/internal/compose/templates/nginx-inbuxa.conf.tmpl
index 83f9698..ef0d2b7 100644
--- a/internal/compose/templates/nginx-inbuxa.conf.tmpl
+++ b/internal/compose/templates/nginx-inbuxa.conf.tmpl
@@ -4,24 +4,65 @@
# /etc/nginx/conf.d/ (or sites-enabled/), set each ssl_certificate pair to the
# certificate you have for those names, check it with `nginx -t`, and reload.
# The installer loads nothing and restarts nothing; this is yours to adjust.
+{{- if .RoutesByName}}
+#
+# It works with nginx-inbuxa-stream.conf beside it, which holds port 443 and
+# routes it by name: that one is included from nginx.conf itself, and says
+# how. Port 443 here is that router's, so nothing below listens on it.
+#
+# Everything is passed to loopback, where the installer put each program:
+# 127.0.0.1:8081 the mail server's plain HTTP, for its certificate
+# challenges on port 80
+# {{.PassThroughBind}} the mail server's own HTTPS, which the name router
+# passes its names to untouched
+# 127.0.0.1:8082 the console
+# 127.0.0.1:8080 the webmail
+#
+# The console and the webmail arrive from the router on {{.FrontTLSBind}},
+# with the PROXY protocol header, so $remote_addr is still the client's.
+{{- else}}
#
# Everything is passed to loopback, where the installer put each program:
# 127.0.0.1:8081 the mail server's web side (JMAP, CalDAV, CardDAV,
# autoconfig, MTA-STS)
# 127.0.0.1:8082 the console
# 127.0.0.1:8080 the webmail
+{{- if .ServesServer}}
+#
+# This terminates TLS for the mail server's names with your certificate, so
+# a domain added to the server later has to be added here by hand, and the
+# server's _443._tcp TLSA records describe its own certificate rather than
+# this one. Run the installer again with --key (an administrator's API key)
+# to have nginx pass the server's names through to it instead (#48).
+{{- end}}
+{{- end}}
#
# The mail server trusts X-Forwarded-For from here, so every block sets it to
# the client's own address rather than passing on what the client sent.
+{{- define "tls-listen"}}
+{{- if .RoutesByName}}
+ listen {{.FrontTLSBind}} ssl proxy_protocol;
+ http2 on;
+ set_real_ip_from 127.0.0.1;
+ real_ip_header proxy_protocol;
+{{- else}}
+ listen 443 ssl;
+ listen [::]:443 ssl;
+ http2 on;
+{{- end}}
+{{- end}}
{{- if .ServesServer}}
# Port 80 for the server's names: its own certificate challenges, which it
# answers itself once certificates are turned on in the console, and a
-# redirect for everything else.
+# redirect for everything else. The pattern catches the same four names of
+# any domain added to the server later; another name the server gets a
+# certificate for goes on the server_name line too.
server {
listen 80;
listen [::]:80;
- server_name {{join .ServerNames " "}};
+ server_name {{join .ServerNames " "}}
+ ~^(autoconfig|autodiscover|mta-sts|ua-auto-config)\.;
location /.well-known/acme-challenge/ {
proxy_pass http://127.0.0.1:8081;
@@ -33,6 +74,8 @@ server {
}
}
+{{- if not .RoutesByName}}
+
# The mail server's web side. Push arrives as a long-lived stream, so it is
# not buffered and not timed out early.
server {
@@ -59,6 +102,7 @@ server {
}
}
{{- end}}
+{{- end}}
{{- if or .Console .ConsoleFiles}}
# The console: static files. It talks to the mail server from the browser,
@@ -70,9 +114,7 @@ server {
return 308 https://$host$request_uri;
}
server {
- listen 443 ssl;
- listen [::]:443 ssl;
- http2 on;
+{{- template "tls-listen" .}}
server_name {{.ConsoleHost}};
ssl_certificate /etc/ssl/inbuxa/{{.ConsoleHost}}.fullchain.pem;
@@ -112,9 +154,7 @@ server {
return 308 https://$host$request_uri;
}
server {
- listen 443 ssl;
- listen [::]:443 ssl;
- http2 on;
+{{- template "tls-listen" .}}
server_name {{.WebmailHost}};
ssl_certificate /etc/ssl/inbuxa/{{.WebmailHost}}.fullchain.pem;
diff --git a/internal/jmap/client_test.go b/internal/jmap/client_test.go
index ffd1537..f840331 100644
--- a/internal/jmap/client_test.go
+++ b/internal/jmap/client_test.go
@@ -223,3 +223,47 @@ func TestHostListenersRefusalIsAnError(t *testing.T) {
t.Fatal("a refused destroy was not reported")
}
}
+
+func TestPassThroughListenerTakesPROXYFromTheWebServerOnly(t *testing.T) {
+ c, seen := fake(t, map[string]string{
+ "x:NetworkListener/query": defaultListeners,
+ "x:NetworkListener/set": `{"methodResponses":[["x:NetworkListener/set",{"updated":{"b":null}},"0"]]}`,
+ })
+ if err := c.PassThroughListener(context.Background(), "127.0.0.1:8443", []string{"127.0.0.1"}); err != nil {
+ t.Fatal(err)
+ }
+ args := (*seen)[1]["args"].(map[string]any)
+ update := args["update"].(map[string]any)
+ if len(update) != 1 || args["create"] != nil {
+ t.Fatalf("set %v, want only the HTTPS listener updated", args)
+ }
+ b := update["b"].(map[string]any)
+ if bind := b["bind"].(map[string]any); len(bind) != 1 || bind["127.0.0.1:8443"] != true {
+ t.Errorf("bind = %v", bind)
+ }
+ if nets := b["overrideProxyTrustedNetworks"].(map[string]any); len(nets) != 1 || nets["127.0.0.1/32"] != true {
+ t.Errorf("trusted = %v, want the web server's address alone", nets)
+ }
+}
+
+func TestPassThroughListenerPutsBackAnHTTPSListener(t *testing.T) {
+ c, seen := fake(t, map[string]string{
+ "x:NetworkListener/query": `{"methodResponses":[["x:NetworkListener/query",{"ids":["c"]},"0"],["x:NetworkListener/get",{"list":[
+ {"id":"c","name":"http","protocol":"http","tlsImplicit":false,"bind":{"127.0.0.1:8081":true}}]},"1"]]}`,
+ "x:NetworkListener/set": `{"methodResponses":[["x:NetworkListener/set",{"created":{"https":{"id":"d"}}},"0"]]}`,
+ })
+ if err := c.PassThroughListener(context.Background(), "[::]:443", []string{"172.31.253.1"}); err != nil {
+ t.Fatal(err)
+ }
+ args := (*seen)[1]["args"].(map[string]any)
+ if args["update"] != nil {
+ t.Errorf("the plain HTTP listener was changed: %v", args["update"])
+ }
+ https := args["create"].(map[string]any)["https"].(map[string]any)
+ if https["protocol"] != "http" || https["tlsImplicit"] != true || https["useTls"] != true {
+ t.Errorf("created %v, want an implicit-TLS HTTP listener", https)
+ }
+ if nets := https["overrideProxyTrustedNetworks"].(map[string]any); nets["172.31.253.1/32"] != true {
+ t.Errorf("trusted = %v", nets)
+ }
+}
diff --git a/internal/jmap/setup.go b/internal/jmap/setup.go
index 8d335c1..b6b3099 100644
--- a/internal/jmap/setup.go
+++ b/internal/jmap/setup.go
@@ -382,8 +382,9 @@ func (c *Client) Listeners(ctx context.Context) ([]Listener, error) {
// ContainerListeners undoes HostListeners, for a server moving from the host
// into a container: every listener back on [::], plain HTTP on 8080, which
// is what the compose file publishes. The HTTPS listener HostListeners took
-// away is not put back: the proxy terminates TLS in front of a container as
-// well, and the container's 443 is not published. Like any listener change
+// away is not put back here: a proxy in the project terminates TLS in front
+// of a container as well, and the container's 443 is not published. With
+// --proxy snippets, PassThroughListener puts it back afterwards. Like any listener change
// it takes effect when the server next starts.
func (c *Client) ContainerListeners(ctx context.Context) error {
ls, err := c.Listeners(ctx)
@@ -496,3 +497,55 @@ func (c *Client) HostListeners(ctx context.Context, httpBind string, local bool)
_, err = DecodeSet(rs[0])
return err
}
+
+// PassThroughListener fits the server's HTTPS listener to a web server in
+// front of it that routes TLS by name and passes the server's names through
+// untouched (#48). The listener is bound where that web server sends them,
+// and takes the PROXY protocol header from the addresses in trusted, so the
+// server sees each client's own address. It serves its own certificates
+// then, for every domain it has, and its _443._tcp TLSA records match what
+// is served.
+//
+// A connection from a trusted address that does not start with the header is
+// refused, which is why trusted is only that web server's own address: no
+// one else connects to the listener from there. If there is no HTTPS
+// listener, as after HostListeners, one is created. Like any listener
+// change it takes effect when the server next starts.
+func (c *Client) PassThroughListener(ctx context.Context, bind string, trusted []string) error {
+ ls, err := c.Listeners(ctx)
+ if err != nil {
+ return err
+ }
+ nets := map[string]bool{}
+ for _, t := range trusted {
+ if !strings.Contains(t, "/") {
+ if strings.Contains(t, ":") {
+ t += "/128"
+ } else {
+ t += "/32"
+ }
+ }
+ nets[t] = true
+ }
+ fitted := map[string]any{
+ "bind": map[string]bool{bind: true},
+ "overrideProxyTrustedNetworks": nets,
+ }
+ args := map[string]any{}
+ for _, l := range ls {
+ if l.Protocol == "http" && l.TLSImplicit {
+ args["update"] = map[string]any{l.ID: fitted}
+ break
+ }
+ }
+ if len(args) == 0 {
+ fitted["name"], fitted["protocol"], fitted["useTls"], fitted["tlsImplicit"] = "https", "http", true, true
+ args["create"] = map[string]any{"https": fitted}
+ }
+ rs, err := c.Do(ctx, Call{"x:NetworkListener/set", args, "0"})
+ if err != nil {
+ return err
+ }
+ _, err = DecodeSet(rs[0])
+ return err
+}
diff --git a/internal/plan/plan.go b/internal/plan/plan.go
index bcd9685..35daf75 100644
--- a/internal/plan/plan.go
+++ b/internal/plan/plan.go
@@ -83,7 +83,9 @@ type Options struct {
// come from, when it is not what the webmail's name resolves to.
ExemptAddress string
// Key is an API key for what changes a running server's own settings:
- // moving it between a container and the host. Never written down.
+ // moving it between a container and the host, and fitting its HTTPS
+ // listener to --proxy snippets' name router on an install from before
+ // it (#48). Never written down.
Key string
// Where this came from, when it came from a topology file: the machine's
@@ -413,7 +415,8 @@ func Build(f host.Facts, o Options) (Plan, error) {
p.Steps = append(p.Steps, Step{
Title: "Write proxy configuration for the web server already here",
Detail: []string{
- o.Dir + "/proxy/nginx-inbuxa.conf and " + o.Dir + "/proxy/Caddyfile.fragment",
+ o.Dir + "/proxy/nginx-inbuxa.conf with nginx-inbuxa-stream.conf, and " + o.Dir + "/proxy/Caddyfile.fragment",
+ "nginx routes 443 by name: the console and the webmail with your certificates, the mail server's names passed through to its own HTTPS on 127.0.0.1:8443",
"nothing is loaded or restarted: that stays yours",
"the server trusts your web server's X-Forwarded-For and exempts it from the auto-ban",
"the mail server's own certificates: turned on in the console once it is loaded",
diff --git a/internal/state/state.go b/internal/state/state.go
index 0388264..39349ea 100644
--- a/internal/state/state.go
+++ b/internal/state/state.go
@@ -38,6 +38,11 @@ type State struct {
Updated time.Time `json:"updated"` //
Installer string `json:"installer,omitempty"` // the version that wrote this
+ // PassThrough is whether the server's HTTPS listener was fitted to the
+ // name router --proxy snippets writes for nginx (#48). Installs from
+ // before it are moved over by a run with --key.
+ PassThrough bool `json:"passThrough,omitempty"`
+
// RemovedAt is when `inbuxa uninstall` took the programs away and kept
// the data. The shapes stay, so `install` run again picks the data up
// rather than trying a first boot on a server configured long ago.