diff --git a/deploy/certbot/deploy-hook.sh b/deploy/certbot/deploy-hook.sh new file mode 100755 index 0000000..9dd5b09 --- /dev/null +++ b/deploy/certbot/deploy-hook.sh @@ -0,0 +1,23 @@ +#!/bin/sh +set -eu + +: "${RENEWED_LINEAGE:?Certbot must provide RENEWED_LINEAGE}" + +bundle_dir="${MEOVV_BUNDLE_DIR:-/opt/meovv-mail}" +tls_dir="$bundle_dir/secrets/tls" + +if [ ! -f "$bundle_dir/compose.yaml" ]; then + echo "MEOVV bundle not found at $bundle_dir" >&2 + exit 1 +fi + +install -d -m 700 "$tls_dir" +install -m 644 "$RENEWED_LINEAGE/fullchain.pem" "$tls_dir/fullchain.pem" +install -m 600 "$RENEWED_LINEAGE/privkey.pem" "$tls_dir/privkey.pem" + +# Once the Certificate object points at /etc/stalwart/tls, restarting Stalwart +# makes renewed files active for SMTP submission and IMAP TLS as well as HTTPS. +if [ -n "$(cd "$bundle_dir" && docker compose ps -q stalwart 2>/dev/null || true)" ]; then + cd "$bundle_dir" + docker compose restart stalwart +fi diff --git a/deploy/nginx/meovv-mail.conf.example b/deploy/nginx/meovv-mail.conf.example new file mode 100644 index 0000000..b464546 --- /dev/null +++ b/deploy/nginx/meovv-mail.conf.example @@ -0,0 +1,71 @@ +# Replace every __MAIL_HOSTNAME__ token before enabling this file. +# The upstream ports are loopback-only bindings from compose.yaml. + +server { + listen 80; + listen [::]:80; + server_name __MAIL_HOSTNAME__; + + # Keep this location if Certbot uses the webroot authenticator. + location ^~ /.well-known/acme-challenge/ { + root /var/www/certbot; + default_type text/plain; + } + + location / { + return 301 https://$host$request_uri; + } +} + +server { + listen 443 ssl http2; + listen [::]:443 ssl http2; + server_name __MAIL_HOSTNAME__; + + ssl_certificate /etc/letsencrypt/live/__MAIL_HOSTNAME__/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/__MAIL_HOSTNAME__/privkey.pem; + + client_max_body_size 35m; + + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; + add_header X-Content-Type-Options "nosniff" always; + add_header Referrer-Policy "strict-origin-when-cross-origin" always; + + # Stalwart owns public discovery, OAuth, JMAP, and client-autoconfiguration + # routes. The HTTP upstream is safe because it is bound to loopback only. + location ~* ^/(?:\.well-known(?:/|$)|jmap(?:/|$)|auth(?:/|$)|api/(?:auth(?:/|$)|discover(?:/|$)|account(?:/|$)|token(?:/|$))|mail/config-v1\.1\.xml$|autodiscover(?:/|$)) { + proxy_pass http://127.0.0.1:8081; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + proxy_buffering off; + proxy_cache off; + proxy_read_timeout 75s; + } + + # JMAP push is proxied by MEOVV as a server-sent event stream. + location = /api/mail/events { + proxy_pass http://127.0.0.1:8080; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + proxy_buffering off; + proxy_cache off; + proxy_read_timeout 75s; + } + + # The product UI, sessions, REST API, administration, health, and metrics. + location / { + proxy_pass http://127.0.0.1:8080; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + proxy_read_timeout 75s; + } +} diff --git a/docs/nginx-certbot.md b/docs/nginx-certbot.md new file mode 100644 index 0000000..29271a6 --- /dev/null +++ b/docs/nginx-certbot.md @@ -0,0 +1,97 @@ +# Nginx and Certbot integration + +Nginx and Certbot are host infrastructure. They are intentionally not part of +the MEOVV Compose project. Compose publishes two HTTP upstreams on loopback: + +- `127.0.0.1:8080` — MEOVV web application and product API +- `127.0.0.1:8081` — Stalwart discovery, OAuth, JMAP, and autoconfiguration + +Mail protocols remain directly exposed by Stalwart on 25, 465, 587, and 993. + +## Nginx + +Copy `deploy/nginx/meovv-mail.conf.example` into the host's Nginx configuration, +replace `__MAIL_HOSTNAME__`, and change the two upstream ports if the bind values +in `.env` were customized. The routing split is required: sending every path to +MEOVV breaks OAuth/JMAP discovery, while sending every path to Stalwart hides the +MEOVV interface and REST API. + +Validate and reload using the host's normal Nginx process. The example keeps +ACME HTTP-01 challenges under `/var/www/certbot`; it is compatible with Certbot's +webroot authenticator. If your existing Certbot integration uses the Nginx +authenticator, retain its generated challenge configuration instead. + +The complete example references the final Certbot lineage and therefore should +be enabled only after that lineage exists. For a new hostname, obtain the first +certificate through your existing Nginx integration or a temporary port-80-only +challenge server, then enable the HTTPS server block. + +Both Compose HTTP bindings default to `127.0.0.1`. Do not change them to a public +address. The Stalwart management/recovery endpoint must never be internet-facing. + +## One certificate, two termination points + +Nginx uses the Certbot certificate for HTTPS. Stalwart must use the same +certificate independently for SMTP submission and IMAP TLS. MEOVV mounts: + +```text +secrets/tls/fullchain.pem -> /etc/stalwart/tls/fullchain.pem +secrets/tls/privkey.pem -> /etc/stalwart/tls/privkey.pem +``` + +After Certbot has issued the hostname certificate, make the initial controlled +copy (replace the two paths as needed): + +```bash +sudo env \ + MEOVV_BUNDLE_DIR=/opt/meovv-mail \ + RENEWED_LINEAGE=/etc/letsencrypt/live/mail.example.com \ + /opt/meovv-mail/deploy/certbot/deploy-hook.sh +``` + +Start the two containers and complete the MEOVV browser setup. Before removing +the temporary recovery account, register the mounted files with Stalwart: + +```bash +docker compose up -d --build --remove-orphans +./mailctl configure-tls +./mailctl harden +``` + +`configure-tls` sends the recovery credential only to the loopback Stalwart +endpoint. It creates or updates a Stalwart `Certificate` object using file +references, selects it as `defaultCertificateId`, records the object id in +`secrets/tls/certificate_id`, and restarts Stalwart. If the Stalwart loopback +port was changed, pass a loopback URL explicitly: + +```bash +./mailctl configure-tls --url http://127.0.0.1:9081/api +``` + +## Renewal hook + +Install the hook in Certbot's renewal deploy-hook directory and set the bundle +location inside a small wrapper or Certbot service environment. For example: + +```bash +sudo install -m 0755 \ + /opt/meovv-mail/deploy/certbot/deploy-hook.sh \ + /etc/letsencrypt/renewal-hooks/deploy/meovv-mail +``` + +The default hook expects the bundle at `/opt/meovv-mail`; set +`MEOVV_BUNDLE_DIR` if it lives elsewhere. Certbot supplies `RENEWED_LINEAGE`. +The hook copies only the certificate chain and private key, applies restrictive +permissions, and restarts Stalwart if it is running. Nginx renewal/reload remains +owned by your existing Certbot integration. + +Test the entire renewal path before production: + +```bash +sudo certbot renew --dry-run +openssl s_client -connect mail.example.com:465 -servername mail.example.com