diff --git a/README.md b/README.md index 98519fd..bb65f1e 100644 --- a/README.md +++ b/README.md @@ -62,8 +62,8 @@ Without automatic DNS setup, the hostname's A/AAAA record must already point to the server. Inbound port 80 must always be reachable so Let's Encrypt can issue the certificate. During an interactive installation, the script can configure Cloudflare DNS after asking for a scoped API token through a hidden prompt. It -previews the records before making changes and never stores the token. The token -needs `Zone:Read` and `DNS:Edit` for the selected zone. +previews the records before making changes. The token needs `Zone:Read` and +`DNS:Edit` for the selected zone. Create the token from the [Cloudflare API Tokens dashboard](https://dash.cloudflare.com/profile/api-tokens), @@ -72,6 +72,17 @@ choose **Create Custom Token**, grant `Zone → Zone → Read` and not use the Global API Key. The installer prints these instructions before its hidden token prompt. +The token is used only for DNS configuration and is never stored. Certificate +issuance and renewal deliberately use HTTP-01 so installation proves the public +hostname reaches this server and renewals do not depend on Cloudflare access. +Before invoking Certbot, the installer publishes a random challenge probe and +requires the public HTTP URL to return its exact content with status `200` and +no redirect. If another proxy, NAT rule, or virtual host intercepts the request, +installation stops with a report covering the active Nginx site, public A/AAAA +answers, local-origin result, public result, and redirect target. If this host's +Nginx does not load `sites-enabled`, the installer automatically tries its +managed `conf.d` fallback before reporting a configuration problem. + Automatic DNS setup creates or updates the mail-host A/AAAA record, the primary MX record, and autoconfiguration CNAMEs. It adds monitoring-mode DMARC and a soft-fail SPF default only when those policies do not already exist. It cannot diff --git a/docs/nginx-certbot.md b/docs/nginx-certbot.md index 7b8bbb1..22ea077 100644 --- a/docs/nginx-certbot.md +++ b/docs/nginx-certbot.md @@ -42,10 +42,9 @@ and explains the files managed by the installer. ### Optional Cloudflare DNS setup When accepted, the installer requests a Cloudflare API token with `Zone:Read` -and `DNS:Edit` through a hidden prompt. The token is used only for the current -process and is not written to disk or placed in a command argument. Before -changing DNS, the installer shows the selected zone, detected public addresses, -and complete record plan for confirmation. +and `DNS:Edit` through a hidden prompt. Before changing DNS, the installer shows +the selected zone, detected public addresses, and complete record plan for +confirmation. The prompt links directly to the [Cloudflare API Tokens dashboard](https://dash.cloudflare.com/profile/api-tokens). @@ -53,6 +52,23 @@ Choose **Create Custom Token**, add `Zone → Zone → Read` and `Zone → DNS → Edit`, and restrict **Zone Resources** to the selected zone. A Global API Key is neither required nor recommended. +The token is used only for the confirmed DNS changes and is then discarded. +Certificate issuance and renewal deliberately use Certbot HTTP-01, proving that +the public hostname and port 80 reach this server without retaining Cloudflare +credentials. + +Immediately before Certbot, the installer creates a random file under +`/var/www/certbot/.well-known/acme-challenge/` and requests it through the public +mail hostname without following redirects. It proceeds only when the response +is `200` with exact content. This detects unloaded Nginx site configuration, +incorrect public IP/NAT routing, canonical-host redirects, and intercepted port +80 traffic before consuming an ACME validation attempt. The report separates a +loopback request pinned to local Nginx from a normal public-hostname request and +includes public A/AAAA answers and any redirect location. If `sites-enabled` is +not loaded, the installer safely retries its managed site through +`/etc/nginx/conf.d/meovv-mail.conf`; it does not edit `nginx.conf` or unrelated +virtual hosts. + It manages the mail hostname's unproxied A/AAAA records, the zone MX record, and the `autoconfig` and `autodiscover` CNAMEs. Existing SPF and DMARC policies are preserved; conservative defaults are created only when no corresponding policy diff --git a/scripts/install-server.sh b/scripts/install-server.sh index f4bf923..0952848 100755 --- a/scripts/install-server.sh +++ b/scripts/install-server.sh @@ -6,6 +6,7 @@ readonly SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" readonly REPOSITORY_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd -P)" readonly NGINX_AVAILABLE="/etc/nginx/sites-available/meovv-mail" readonly NGINX_ENABLED="/etc/nginx/sites-enabled/meovv-mail" +readonly NGINX_CONFD="/etc/nginx/conf.d/meovv-mail.conf" readonly CERTBOT_WEBROOT="/var/www/certbot" readonly CERTBOT_HOOK="/etc/letsencrypt/renewal-hooks/deploy/meovv-mail" readonly MAILCTL_BIN="/usr/local/bin/mailctl" @@ -25,6 +26,7 @@ TEMP_CONTAINER="" TEMP_FILES=() CF_API_TOKEN="" CF_ZONE_ID="" +NGINX_ACTIVE_LINK="$NGINX_ENABLED" log() { printf '\n\033[1;35m==>\033[0m %s\n' "$*" @@ -576,20 +578,66 @@ assert_hostname_is_available() { if [[ -e "$NGINX_AVAILABLE" ]] && ! grep -Fq "$MANAGED_MARKER" "$NGINX_AVAILABLE"; then die "$NGINX_AVAILABLE exists but is not managed by this installer" fi + if [[ -e "$NGINX_CONFD" || -L "$NGINX_CONFD" ]]; then + if [[ "$(readlink -f "$NGINX_CONFD" 2>/dev/null || true)" != "$(readlink -f "$NGINX_AVAILABLE" 2>/dev/null || true)" ]] && \ + ! grep -Fq "$MANAGED_MARKER" "$NGINX_CONFD" 2>/dev/null; then + die "$NGINX_CONFD exists but is not managed by this installer" + fi + NGINX_ACTIVE_LINK="$NGINX_CONFD" + fi +} + +ensure_managed_nginx_site_loaded() { + local nginx_configuration + nginx_configuration="$(nginx -T 2>&1)" || die "could not inspect the active Nginx configuration" + if grep -Fq "# $MANAGED_MARKER" <<< "$nginx_configuration"; then + return + fi + + warn "$NGINX_ENABLED is not loaded by nginx.conf; trying the standard conf.d location" + if [[ -e "$NGINX_CONFD" || -L "$NGINX_CONFD" ]]; then + die "$NGINX_CONFD is unavailable for the managed Nginx fallback" + fi + install -d -m 0755 /etc/nginx/conf.d + ln -s "$NGINX_AVAILABLE" "$NGINX_CONFD" + if [[ -L "$NGINX_ENABLED" ]] && \ + [[ "$(readlink -f "$NGINX_ENABLED")" == "$(readlink -f "$NGINX_AVAILABLE")" ]]; then + rm -f -- "$NGINX_ENABLED" + fi + NGINX_ACTIVE_LINK="$NGINX_CONFD" + + if ! nginx -t || ! systemctl reload nginx; then + rm -f -- "$NGINX_CONFD" + ln -sfn "$NGINX_AVAILABLE" "$NGINX_ENABLED" + die "Nginx could not load the managed site from sites-enabled or conf.d" + fi + nginx_configuration="$(nginx -T 2>&1)" || die "could not re-inspect the active Nginx configuration" + if ! grep -Fq "# $MANAGED_MARKER" <<< "$nginx_configuration"; then + rm -f -- "$NGINX_CONFD" + ln -sfn "$NGINX_AVAILABLE" "$NGINX_ENABLED" + NGINX_ACTIVE_LINK="$NGINX_ENABLED" + die "nginx.conf loads neither /etc/nginx/sites-enabled/* nor /etc/nginx/conf.d/*.conf" + fi + log "Loaded the managed Nginx site through $NGINX_CONFD" } install_challenge_site() { install -d -m 0755 "$CERTBOT_WEBROOT" install -d -m 0755 /etc/nginx/sites-available /etc/nginx/sites-enabled + if [[ -L "$NGINX_CONFD" ]] && \ + [[ "$(readlink -f "$NGINX_CONFD")" == "$(readlink -f "$NGINX_AVAILABLE" 2>/dev/null || true)" ]]; then + NGINX_ACTIVE_LINK="$NGINX_CONFD" + fi if [[ -r "/etc/letsencrypt/live/$MAIL_HOSTNAME/fullchain.pem" ]] && \ [[ -r "/etc/letsencrypt/live/$MAIL_HOSTNAME/privkey.pem" ]] && \ [[ -f "$NGINX_AVAILABLE" ]] && grep -Fq "listen 443 ssl" "$NGINX_AVAILABLE"; then log "Keeping the existing managed HTTPS site during certificate renewal" - ln -sfn "$NGINX_AVAILABLE" "$NGINX_ENABLED" + ln -sfn "$NGINX_AVAILABLE" "$NGINX_ACTIVE_LINK" nginx -t systemctl enable --now nginx systemctl reload nginx + ensure_managed_nginx_site_loaded return fi @@ -611,10 +659,95 @@ server { } } EOF - ln -sfn "$NGINX_AVAILABLE" "$NGINX_ENABLED" + ln -sfn "$NGINX_AVAILABLE" "$NGINX_ACTIVE_LINK" nginx -t systemctl enable --now nginx systemctl reload nginx + ensure_managed_nginx_site_loaded +} + +verify_http_challenge_route() { + log "Running the complete pre-Certbot HTTP-01 check" + + local challenge_directory challenge_name challenge_file challenge_url + local local_body local_headers local_status="000" local_curl_ok local_content_ok + local public_body public_headers public_status="000" public_curl_ok public_content_ok + local public_redirect public_ipv4 public_ipv6 + challenge_directory="$CERTBOT_WEBROOT/.well-known/acme-challenge" + challenge_name="meovv-preflight-$(openssl rand -hex 12)" + challenge_file="$challenge_directory/$challenge_name" + local_body="$(mktemp /tmp/meovv-http-local-body.XXXXXX)" + local_headers="$(mktemp /tmp/meovv-http-local-headers.XXXXXX)" + public_body="$(mktemp /tmp/meovv-http-public-body.XXXXXX)" + public_headers="$(mktemp /tmp/meovv-http-public-headers.XXXXXX)" + TEMP_FILES+=("$challenge_file" "$local_body" "$local_headers" "$public_body" "$public_headers") + install -d -m 0755 "$challenge_directory" + printf '%s' "$challenge_name" > "$challenge_file" + chmod 0644 "$challenge_file" + challenge_url="http://$MAIL_HOSTNAME/.well-known/acme-challenge/$challenge_name" + + local_curl_ok=true + if ! local_status="$(curl \ + --silent \ + --show-error \ + --max-time 8 \ + --max-redirs 0 \ + --noproxy '*' \ + --resolve "$MAIL_HOSTNAME:80:127.0.0.1" \ + --dump-header "$local_headers" \ + --output "$local_body" \ + --write-out '%{http_code}' \ + "$challenge_url")"; then + local_curl_ok=false + fi + local_content_ok=false + cmp -s "$challenge_file" "$local_body" && local_content_ok=true + + public_ipv4="$(dig +short A "$MAIL_HOSTNAME" @1.1.1.1 | paste -sd ',' - || true)" + public_ipv6="$(dig +short AAAA "$MAIL_HOSTNAME" @1.1.1.1 | paste -sd ',' - || true)" + public_curl_ok=true + if ! public_status="$(curl \ + --silent \ + --show-error \ + --max-time 15 \ + --max-redirs 0 \ + --noproxy '*' \ + --dump-header "$public_headers" \ + --output "$public_body" \ + --write-out '%{http_code}' \ + "$challenge_url")"; then + public_curl_ok=false + fi + public_content_ok=false + cmp -s "$challenge_file" "$public_body" && public_content_ok=true + public_redirect="$(sed -n 's/^[Ll]ocation:[[:space:]]*//p' "$public_headers" | tr -d '\r' | tail -n 1)" + + cat < "$NGINX_AVAILABLE" - ln -sfn "$NGINX_AVAILABLE" "$NGINX_ENABLED" + ln -sfn "$NGINX_AVAILABLE" "$NGINX_ACTIVE_LINK" nginx -t systemctl reload nginx } @@ -747,6 +880,7 @@ install_command() { initialize_bundle configure_dns_if_requested install_challenge_site + verify_http_challenge_route obtain_certificate copy_certificate_to_stalwart install_certbot_hook