diff --git a/docs/content/docs/en/config/ssl-certificates.mdx b/docs/content/docs/en/config/ssl-certificates.mdx index dd8027656..f7cd90a88 100644 --- a/docs/content/docs/en/config/ssl-certificates.mdx +++ b/docs/content/docs/en/config/ssl-certificates.mdx @@ -119,8 +119,9 @@ uses `/root/cert/ip/fullchain.pem` (mode `644`) and `/root/cert/ip/privkey.pem` previous generation and its renewal schedule. If reloading the panel fails after activation, the valid certificate stays installed and reload is retried. -The root crontab checks the versioned local renewal helper hourly. Manual setup -and cron share a file lock for the same panel paths, even when the primary IP +The root crontab checks the versioned local renewal helper hourly. On Alpine, +IP setup enables the OpenRC cron service when no `crond` service is running. +Manual setup and cron share a file lock for the same panel paths, even when the primary IP changes. The first successful setup retires legacy acme.sh renewal entries for those paths. Existing domain certificates and the ACME account are retained. Renew managed IP certificates from the `x-ui` menu. Standard acme.sh cron diff --git a/install.sh b/install.sh index 59edfc4bb..89f2a1967 100644 --- a/install.sh +++ b/install.sh @@ -280,6 +280,15 @@ ip_ssl_shell_quote() { printf "'%s'" "$value" } +ip_ssl_enable_openrc_cron() { + [[ "$1" == alpine ]] || return 0 + if rc-service crond status >/dev/null 2>&1; then return 0; fi + if ! rc-update add dcron default || ! rc-service dcron start; then + printf 'Could not enable the OpenRC cron service for IP certificate renewal.\n' >&2 + return 1 + fi +} + # The public paths always resolve through one generation pointer after migration. ip_ssl_link() { local state="$1" target="$2" link="$3" @@ -344,12 +353,14 @@ ip_ssl_retire_legacy() { fi done <<< "$destinations" done - # The requested entries can also be unfinished orders with no destinations. + # Retire unfinished orders for this IP. A completed cert without saved + # destinations may be in use directly by another service. for name in "$ipv4" "${ipv4}_ecc"; do entry="$home/$name" conf="$entry/$ipv4.conf" [[ -d "$entry" && ! -L "$entry" ]] || continue - if [[ ! -f "$conf" ]] || ! grep -Eq "^Le_(Real(Cert|Key|CACert|FullChain)Path|ReloadCmd|DeployHook)='[^']+" "$conf"; then + [[ ! -s "$entry/fullchain.cer" && ! -s "$entry/$ipv4.cer" ]] || continue + if [[ ! -f "$conf" ]] || ! grep -Eq "^Le_(Real(Cert|Key|CACert|FullChain)Path|(Pre|Post|Renew|Deploy)Hook|ReloadCmd)='[^']+" "$conf"; then retire+=("$name") fi done @@ -1244,6 +1255,7 @@ setup_ip_certificate() { fi done + ip_ssl_enable_openrc_cron "$release" || return 1 if [[ -n "${XUI_ACME_EMAIL:-}" ]]; then ~/.acme.sh/acme.sh --register-account --server letsencrypt -m "${XUI_ACME_EMAIL}" \ || echo -e "${yellow}Could not register ${XUI_ACME_EMAIL} with Let's Encrypt; continuing without it.${plain}" diff --git a/scripts/lib/ip-ssl.sh b/scripts/lib/ip-ssl.sh index dddddb058..7fdd6b00e 100644 --- a/scripts/lib/ip-ssl.sh +++ b/scripts/lib/ip-ssl.sh @@ -223,6 +223,15 @@ ip_ssl_shell_quote() { printf "'%s'" "$value" } +ip_ssl_enable_openrc_cron() { + [[ "$1" == alpine ]] || return 0 + if rc-service crond status >/dev/null 2>&1; then return 0; fi + if ! rc-update add dcron default || ! rc-service dcron start; then + printf 'Could not enable the OpenRC cron service for IP certificate renewal.\n' >&2 + return 1 + fi +} + # The public paths always resolve through one generation pointer after migration. ip_ssl_link() { local state="$1" target="$2" link="$3" @@ -287,12 +296,14 @@ ip_ssl_retire_legacy() { fi done <<< "$destinations" done - # The requested entries can also be unfinished orders with no destinations. + # Retire unfinished orders for this IP. A completed cert without saved + # destinations may be in use directly by another service. for name in "$ipv4" "${ipv4}_ecc"; do entry="$home/$name" conf="$entry/$ipv4.conf" [[ -d "$entry" && ! -L "$entry" ]] || continue - if [[ ! -f "$conf" ]] || ! grep -Eq "^Le_(Real(Cert|Key|CACert|FullChain)Path|ReloadCmd|DeployHook)='[^']+" "$conf"; then + [[ ! -s "$entry/fullchain.cer" && ! -s "$entry/$ipv4.cer" ]] || continue + if [[ ! -f "$conf" ]] || ! grep -Eq "^Le_(Real(Cert|Key|CACert|FullChain)Path|(Pre|Post|Renew|Deploy)Hook|ReloadCmd)='[^']+" "$conf"; then retire+=("$name") fi done diff --git a/scripts/test/ip-ssl-certificate-test.sh b/scripts/test/ip-ssl-certificate-test.sh index 346a69d50..6aacedaff 100644 --- a/scripts/test/ip-ssl-certificate-test.sh +++ b/scripts/test/ip-ssl-certificate-test.sh @@ -254,6 +254,9 @@ if ! live_is new || [[ ! -f "$state/current/reload-pending" ]]; then fail 'reloa [[ "$(stat -Lc %a "$cert_dir/privkey.pem")" == 600 ]] || fail 'private key permissions changed' [[ "$(stat -Lc %a "$cert_dir/fullchain.pem")" == 644 ]] || fail 'certificate permissions changed' [[ ! -e "$legacy/$ipv4" && ! -e "$legacy/${ipv4}_ecc" ]] || fail 'old cron can still target live paths' +mkdir "$legacy/$ipv4" +cp "$TEMP/old.pem" "$legacy/$ipv4/fullchain.cer" +printf "Le_Alt='no'\n" > "$legacy/$ipv4/$ipv4.conf" [[ "$(grep -cx 'arg=--listen-v4' "$ACME_LOG")" == 1 && "$(grep -cx 'arg=--listen-v6' "$ACME_LOG")" == 1 ]] || fail 'dual-stack flags absent' [[ "$(grep -cx "arg=$ipv6" "$ACME_LOG")" == 1 ]] || fail 'IPv6 SAN duplicated or missing' grep -Fq '/unrelated-cron' "$CRON_FILE" || fail 'crontab lost an unrelated entry' @@ -365,6 +368,7 @@ if wait "$failed_pid"; then fail 'injected promotion failure reported success'; wait "$waiting_pid" || { cat "$TEMP/waiting-promotion"; fail 'second promotion failed'; } unset -f mv live_is later || fail 'rollback overwrote a newer promotion' +cmp "$TEMP/old.pem" "$legacy/$ipv4/fullchain.cer" || fail 'an unrelated certificate using the same IP was retired' printf 'PASS: a failed promotion cannot roll back another operation on the same paths\n' before=$(wc -l < "$ACME_LOG") diff --git a/scripts/test/ip-ssl-entrypoint-test.sh b/scripts/test/ip-ssl-entrypoint-test.sh index b21d0b839..9881f21a3 100644 --- a/scripts/test/ip-ssl-entrypoint-test.sh +++ b/scripts/test/ip-ssl-entrypoint-test.sh @@ -48,7 +48,14 @@ printf 'PASS: installer consent, explicit configuration, no unattended prompts, setup_ip_certificate() { return 1; } systemctl() { :; } -rc-service() { :; } +rc-service() { + case "$*" in + 'x-ui stop') : ;; + 'crond status') [[ "${CROND_RUNNING:-0}" == 1 ]] ;; + 'dcron start') printf 'rc-service %s\n' "$*" >> "$TEMP/cron" ;; + *) return 1 ;; + esac +} NONINTERACTIVE=1 export XUI_SSL_IPV6='2001:4860::1' for release in ubuntu alpine; do @@ -58,3 +65,11 @@ for release in ubuntu alpine; do [[ "$SSL_HOST" == 198.51.100.8 ]] || fail 'the installer lost its access host after a failed certificate' done printf 'PASS: a failed IP certificate is reported and the installer carries on on systemd/OpenRC\n' + +rc-update() { printf 'rc-update %s\n' "$*" >> "$TEMP/cron"; } +ip_ssl_enable_openrc_cron ubuntu +CROND_RUNNING=1 ip_ssl_enable_openrc_cron alpine +[[ ! -e "$TEMP/cron" ]] || fail 'an existing cron service was disturbed' +ip_ssl_enable_openrc_cron alpine +[[ "$(cat "$TEMP/cron")" == $'rc-update add dcron default\nrc-service dcron start' ]] || fail 'Alpine cron was not enabled and started' +printf 'PASS: IP setup keeps an existing daemon and enables Alpine cron when needed\n' diff --git a/x-ui.sh b/x-ui.sh index 04b4e41a9..46cc67c50 100644 --- a/x-ui.sh +++ b/x-ui.sh @@ -266,6 +266,15 @@ ip_ssl_shell_quote() { printf "'%s'" "$value" } +ip_ssl_enable_openrc_cron() { + [[ "$1" == alpine ]] || return 0 + if rc-service crond status >/dev/null 2>&1; then return 0; fi + if ! rc-update add dcron default || ! rc-service dcron start; then + printf 'Could not enable the OpenRC cron service for IP certificate renewal.\n' >&2 + return 1 + fi +} + # The public paths always resolve through one generation pointer after migration. ip_ssl_link() { local state="$1" target="$2" link="$3" @@ -330,12 +339,14 @@ ip_ssl_retire_legacy() { fi done <<< "$destinations" done - # The requested entries can also be unfinished orders with no destinations. + # Retire unfinished orders for this IP. A completed cert without saved + # destinations may be in use directly by another service. for name in "$ipv4" "${ipv4}_ecc"; do entry="$home/$name" conf="$entry/$ipv4.conf" [[ -d "$entry" && ! -L "$entry" ]] || continue - if [[ ! -f "$conf" ]] || ! grep -Eq "^Le_(Real(Cert|Key|CACert|FullChain)Path|ReloadCmd|DeployHook)='[^']+" "$conf"; then + [[ ! -s "$entry/fullchain.cer" && ! -s "$entry/$ipv4.cer" ]] || continue + if [[ ! -f "$conf" ]] || ! grep -Eq "^Le_(Real(Cert|Key|CACert|FullChain)Path|(Pre|Post|Renew|Deploy)Hook|ReloadCmd)='[^']+" "$conf"; then retire+=("$name") fi done @@ -2352,7 +2363,7 @@ ssl_cert_issue_for_ip() { zypper refresh > /dev/null 2>&1 && zypper -q install -y socat > /dev/null 2>&1 ;; alpine) - apk add socat curl openssl flock > /dev/null 2>&1 + apk add socat curl openssl flock dcron > /dev/null 2>&1 ;; *) LOGW "Unsupported OS for automatic socat installation" @@ -2398,6 +2409,7 @@ ssl_cert_issue_for_ip() { # Reload command - restarts panel after renewal local reloadCmd="systemctl restart x-ui 2>/dev/null || rc-service x-ui restart 2>/dev/null" + ip_ssl_enable_openrc_cron "$release" || return 1 if ! ip_ssl_issue_and_install "$HOME/.acme.sh/acme.sh" "$server_ip" "$ipv6_addr" "$WebPort" "$certPath" "$reloadCmd"; then return 1 fi