2023-10-29 07:58:53 +00:00
|
|
|
#!/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
|
|
|
|
)
|
|
|
|
|
2023-11-04 07:12:33 +00:00
|
|
|
post_install+=(30:acme:post_install) # must be after '20:nginx:post_install' and '10:osi-mk:hostname'
|
2023-10-30 21:05:53 +00:00
|
|
|
acme:post_install() {
|
2023-10-29 07:58:53 +00:00
|
|
|
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;
|
|
|
|
|
2024-01-25 10:27:59 +00:00
|
|
|
error_log /var/log/nginx/cleartext-error.log error;
|
2023-10-29 07:58:53 +00:00
|
|
|
|
|
|
|
events {
|
|
|
|
worker_connections 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
http {
|
2024-01-25 10:27:59 +00:00
|
|
|
error_log /var/log/nginx/cleartext-error.http.log error;
|
|
|
|
access_log /var/log/nginx/cleartext-access.http.log combined;
|
2023-10-29 07:58:53 +00:00
|
|
|
|
2024-01-25 10:28:46 +00:00
|
|
|
types_hash_max_size 4096;
|
2023-10-29 07:58:53 +00:00
|
|
|
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
|
2023-12-13 03:03:48 +00:00
|
|
|
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
|
2023-10-29 07:58:53 +00:00
|
|
|
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.
|
2024-01-25 10:28:37 +00:00
|
|
|
install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-init" <<-'EOF'
|
2023-10-29 07:58:53 +00:00
|
|
|
#!/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[@]}"
|
2024-01-25 10:28:37 +00:00
|
|
|
EOF
|
|
|
|
install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-renew" <<-'EOF'
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -eu
|
|
|
|
. /etc/ssl/misc/certbot-init
|
2023-10-29 07:58:53 +00:00
|
|
|
sudo /etc/ssl/misc/certbot-hook
|
|
|
|
EOF
|
2024-01-25 10:28:37 +00:00
|
|
|
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.
|
2023-10-29 07:58:53 +00:00
|
|
|
[Service]
|
|
|
|
ExecStart=
|
|
|
|
ExecStart=/etc/ssl/misc/certbot-renew
|
|
|
|
|
2024-01-25 10:28:37 +00:00
|
|
|
# Add the dependencies with how we have it configured.
|
2023-10-29 07:58:53 +00:00
|
|
|
[Unit]
|
|
|
|
After=network-online.target nginx-cleartext.service
|
|
|
|
Wants=network-online.target nginx-cleartext.service
|
|
|
|
EOF
|
2024-01-25 10:28:37 +00:00
|
|
|
|
2023-10-29 07:58:53 +00:00
|
|
|
systemctl --root="$arg_mountpoint" enable certbot-renew.timer
|
|
|
|
|
2024-01-25 10:28:37 +00:00
|
|
|
# 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"
|
|
|
|
|
2023-10-29 07:58:53 +00:00
|
|
|
########################################################################
|
|
|
|
# Configure the main nginx to play nice #
|
|
|
|
########################################################################
|
|
|
|
|
2023-12-13 03:03:48 +00:00
|
|
|
install -Dm644 /dev/stdin "$arg_mountpoint"/etc/sysusers.d/nginx.conf <<-EOF
|
|
|
|
m nginx keys
|
|
|
|
EOF
|
2023-10-29 07:58:53 +00:00
|
|
|
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]
|
2024-01-25 10:28:37 +00:00
|
|
|
After=certbot-init.service
|
|
|
|
Wants=certbot-init.service
|
2023-10-29 07:58:53 +00:00
|
|
|
EOF
|
|
|
|
|
|
|
|
cat >"$arg_mountpoint/etc/nginx/snippets/listen.conf" <<-'EOF'
|
|
|
|
# -*- mode: nginx -*-
|
|
|
|
listen 443 ssl http2;
|
|
|
|
listen [::]:443 ssl http2;
|
|
|
|
|
2024-01-25 10:27:59 +00:00
|
|
|
access_log /var/log/nginx/main-access.http.$server_name.log combined;
|
2023-10-29 07:58:53 +00:00
|
|
|
|
|
|
|
# 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
|
2023-12-07 18:00:32 +00:00
|
|
|
grep -hoE 'server_name\s+([-.a-z0-9]+)\s*;' /etc/nginx/sites/*.conf | sed -E -e 's/server_name\s+//' -e 's/\s*;//'
|
2023-10-29 07:58:53 +00:00
|
|
|
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
|
|
|
|
}
|