#!/hint/bash -euE # Copyright (C) 2023 Umorpha Systems # SPDX-License-Identifier: AGPL-3.0-or-later load_module "$(dirname -- "${BASH_SOURCE[0]}")/base-nginx.sh" packages+=( certbot sudo ) post_install+=(30:acme:post_install) # must be after '20:nginx:post_install' and '10:osi-mk:hostname' acme:post_install() { local arg_mountpoint=$1 ######################################################################## # First up, we're going to run a separate nginx instance from # # the main one to handle ACME HTTP-01 challenges. # ######################################################################## # Inspired by https://xyrillian.de/thoughts/posts/how-i-run-certbot.html cat >"$arg_mountpoint/etc/nginx/nginx-cleartext.conf" <<-'EOF' # -*- mode: nginx; nginx-indent-level: 4; intent-tabs-mode: nil -*- pid /run/nginx-cleartext.pid; worker_processes 1; error_log /var/log/nginx/cleartext-error.log error; events { worker_connections 1024; } http { error_log /var/log/nginx/cleartext-error.http.log error; access_log /var/log/nginx/cleartext-access.http.log combined; types_hash_max_size 4096; include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80 default_server; listen [::]:80 default_server; location / { return 301 https://$host$request_uri; } location /.well-known/acme-challenge { root /var/lib/letsencrypt; default_type "text/plain"; } } } EOF mkdir -p "$arg_mountpoint/etc/systemd/system/nginx-cleartext.service.d" echo '[Unit]' >"$arg_mountpoint/etc/systemd/system/nginx-cleartext.service" ln -sT /usr/lib/systemd/system/nginx.service "$arg_mountpoint/etc/systemd/system/nginx-cleartext.service.d/00-base.conf" cat >"$arg_mountpoint/etc/systemd/system/nginx-cleartext.service.d/01-base.conf" <<-EOF [Service] PIDFile=/run/nginx-cleartext.pid ExecStart= ExecStart=/usr/bin/nginx -c /etc/nginx/nginx-cleartext.conf ExecReload= ExecReload=/usr/bin/nginx -c /etc/nginx/nginx-cleartext.conf -s reload EOF systemctl --root="$arg_mountpoint" enable nginx-cleartext.service ######################################################################## # Set up Certbot+SSL for the system # ######################################################################## # General SSL curl https://ssl-config.mozilla.org/ffdhe2048.txt > "$arg_mountpoint/etc/ssl/private/dhparam.pem" ln -sT "../../letsencrypt/live/$(cat "$arg_mountpoint/etc/hostname")" "$arg_mountpoint/etc/ssl/private/myhostname" # Have Certbot run as non-root install -Dm644 /dev/stdin "$arg_mountpoint"/etc/sysusers.d/certbot.conf <<-EOF u keys - "Certbot" /etc/ssl EOF install -Dm644 /dev/stdin "$arg_mountpoint"/etc/tmpfiles.d/certbot.conf <<-EOF d /etc/letsencrypt 0755 keys keys d /etc/letsencrypt/archive 0750 keys keys d /etc/letsencrypt/live 0750 keys keys d /var/lib/letsencrypt 0755 keys keys d /var/log/letsencrypt 0700 keys keys EOF install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-renew.service.d/user.conf" <<-EOF [Service] User=keys UMask=0027 EOF install -Dm644 /dev/stdin "$arg_mountpoint/etc/sudoers.d/10-certbot" <<-EOF keys ALL=(ALL) NOPASSWD: /etc/ssl/misc/certbot-hook EOF # Have the normal certbot-renew.service handle changing lists of domains. install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-init" <<-'EOF' #!/usr/bin/env bash set -eu if [[ "$(whoami)" != keys ]]; then echo "$0: this script must be run as user 'keys'" >&2 exit 1 fi cd / # The first name listed should be the canonical host name domains=( $(cat /etc/hostname) $(find -L /etc/ssl/misc/certbot-domains.d -type f -executable -exec {} \;) ) cmd=( certbot certonly --keep-until-expiring # only renew if nescessary --non-interactive --agree-tos --cert-name "${domains[0]}" --email "$(whoami)@${domains[0]}" --webroot -w /var/lib/letsencrypt ) for domain in "${domains[@]}"; do cmd+=(-d "$domain") done umask 0027 "${cmd[@]}" EOF install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-renew" <<-'EOF' #!/usr/bin/env bash set -eu . /etc/ssl/misc/certbot-init sudo /etc/ssl/misc/certbot-hook EOF install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-renew.service.d/10-custom.conf" <<-EOF # Use our wrapper script to handle changing lists of # domains and call our post-renew hooks. [Service] ExecStart= ExecStart=/etc/ssl/misc/certbot-renew # Add the dependencies with how we have it configured. [Unit] After=network-online.target nginx-cleartext.service Wants=network-online.target nginx-cleartext.service EOF systemctl --root="$arg_mountpoint" enable certbot-renew.timer # Create a variant service for use at system boot. install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-init.service" <<-EOF [Unit] EOF install -d "$arg_mountpoint/etc/systemd/system/certbot-init.service.d" ln -s /usr/lib/systemd/system/certbot-renew.service "$arg_mountpoint/etc/systemd/system/certbot-init.service.d/00-base.conf" ln -s ../certbot-renew.service.d/10-custom.conf "$arg_mountpoint/etc/systemd/system/certbot-init.service.d/10-custom.conf" ######################################################################## # Configure the main nginx to play nice # ######################################################################## install -Dm644 /dev/stdin "$arg_mountpoint"/etc/sysusers.d/nginx.conf <<-EOF m nginx keys EOF sed -i 's|/var/log/nginx/http\.|/var/log/nginx/https.|' "$arg_mountpoint/etc/nginx/nginx.conf" install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/nginx.service.d/10-certbot.conf" <<-EOF [Unit] After=certbot-init.service Wants=certbot-init.service EOF cat >"$arg_mountpoint/etc/nginx/snippets/listen.conf" <<-'EOF' # -*- mode: nginx -*- listen 443 ssl http2; listen [::]:443 ssl http2; access_log /var/log/nginx/main-access.http.$server_name.log combined; # generated 2023-10-28, Mozilla Guideline v5.7, nginx 1.24.0, OpenSSL 3.1.4, intermediate configuration # https://ssl-config.mozilla.org/#server=nginx&version=1.24.0&config=intermediate&openssl=3.1.4&guideline=5.7 ssl_certificate /etc/ssl/private/myhostname/fullchain.pem; ssl_certificate_key /etc/ssl/private/myhostname/privkey.pem; ssl_session_timeout 1d; ssl_session_cache shared:MozSSL:10m; # about 40000 sessions ssl_session_tickets off; ssl_dhparam /etc/ssl/private/dhparam.pem; # intermediate configuration ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305; ssl_prefer_server_ciphers off; # HSTS (ngx_http_headers_module is required) (63072000 seconds) add_header Strict-Transport-Security "max-age=63072000" always; # OCSP stapling ssl_stapling on; ssl_stapling_verify on; # verify chain of trust of OCSP response using Root CA and Intermediate certs ssl_trusted_certificate /etc/ssl/private/myhostname/chain.pem; EOF install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-domains.d/10-nginx-auto" <<-'EOF' #!/bin/sh grep -hoE 'server_name\s+([-.a-z0-9]+)\s*;' /etc/nginx/sites/*.conf | sed -E -e 's/server_name\s+//' -e 's/\s*;//' EOF install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-hook" <<-'EOF' #!/bin/sh if systemctl is-active nginx.service; then systemctl reload nginx.service fi EOF }