diff --git a/scripts/lib/ip-ssl.sh b/scripts/lib/ip-ssl.sh index 5d3635036..d536a74fe 100644 --- a/scripts/lib/ip-ssl.sh +++ b/scripts/lib/ip-ssl.sh @@ -226,6 +226,7 @@ ip_ssl_shell_quote() { ip_ssl_issue_and_install() ( local acme="$1" ipv4="$2" ipv6="$3" port="$4" cert_dir="$5" reload="$6" local stage='' attempted=0 installed=0 code=0 file restored + local acme_home="${acme%/*}" renewal_backup entry local quoted_cert quoted_key validation check_key reload_guard local -a domain_args=(-d "$ipv4") if ! ip_ssl_ipv4 "$ipv4" || { [[ -n "$ipv6" ]] && ! ip_ssl_public_ipv6 "$ipv6"; }; then @@ -235,11 +236,26 @@ ip_ssl_issue_and_install() ( [[ "$port" =~ ^[0-9]{1,5}$ ]] && ((10#$port >= 1 && 10#$port <= 65535)) || return 1 [[ -z "$ipv6" ]] || domain_args+=(-d "$ipv6") ip_ssl_prepare_listener "$acme" "$port" "$ipv6" || return 1 + renewal_backup=$(mktemp -d "$acme_home/.xui-acme.XXXXXX") || return 1 + for entry in "$ipv4" "${ipv4}_ecc"; do + [[ ! -d "$acme_home/$entry" ]] || cp -a -- "$acme_home/$entry" "$renewal_backup/$entry" || { + rm -rf -- "$renewal_backup" + return 1 + } + done if ! "$acme" --issue "${domain_args[@]}" --standalone "${IP_SSL_LISTEN_ARGS[@]}" \ --server letsencrypt --certificate-profile shortlived --days 6 --httpport "$port" --force; then + # acme.sh saves the requested names before it places the order, so a failed one would + # make cron renew the working certificate with the names that just failed. + for entry in "$ipv4" "${ipv4}_ecc"; do + rm -rf -- "${acme_home:?}/$entry" + [[ ! -d "$renewal_backup/$entry" ]] || mv -- "$renewal_backup/$entry" "$acme_home/$entry" + done + rm -rf -- "$renewal_backup" printf 'IP certificate issuance failed. Existing certificates and ACME data were preserved. Check external port 80 on every selected address.\n' >&2 return 1 fi + rm -rf -- "$renewal_backup" umask 077 mkdir -p "$cert_dir" || return 1 diff --git a/scripts/test/ip-ssl-certificate-test.sh b/scripts/test/ip-ssl-certificate-test.sh index 85630d7a3..d6eb13a02 100644 --- a/scripts/test/ip-ssl-certificate-test.sh +++ b/scripts/test/ip-ssl-certificate-test.sh @@ -26,7 +26,12 @@ printf 'CALL\n' >> "$ACME_LOG" printf 'arg=%s\n' "$@" >> "$ACME_LOG" case "$1" in --version) printf 'v3.1.5\n' ;; - --issue) [[ "$ACME_ISSUE_FAIL" != 1 ]] ;; + --issue) + # Like acme.sh, record the requested SAN set before the order can fail. + mkdir -p "$ACME_HOME/198.51.100.8_ecc" + printf "Le_Alt='%s'\n" "$*" > "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf" + [[ "$ACME_ISSUE_FAIL" != 1 ]] + ;; --upgrade|--register-account) : ;; --installcert) key=''; cert=''; reload='' @@ -45,6 +50,7 @@ esac MOCK chmod +x "$TEMP/mock acme" acme="$TEMP/mock acme" +export ACME_HOME="$TEMP" reload="printf 'reloaded\\n' >> '$RELOAD_LOG'" port=$((20000+RANDOM)) @@ -53,7 +59,18 @@ if ip_ssl_issue_and_install "$acme" 198.51.100.8 '' "$port" "$cert_dir" "$reload cmp "$TEMP/old.pem" "$cert_dir/fullchain.pem" || fail 'issuance failure removed existing certificate' cmp "$TEMP/old.key" "$cert_dir/privkey.pem" || fail 'issuance failure removed existing key' [[ ! -f "$RELOAD_LOG" ]] || fail 'issuance failure reloaded panel' +[[ ! -e "$ACME_HOME/198.51.100.8_ecc" ]] || fail 'a failed first order left a renewal entry that cron would retry every day' printf 'PASS: ACME failure preserves existing certificate and key\n' + +mkdir -p "$ACME_HOME/198.51.100.8_ecc" +printf "Le_Alt='no'\nLe_NextRenewTime='1'\n" > "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf" +cp "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf" "$TEMP/working.conf" +if ip_ssl_issue_and_install "$acme" 198.51.100.8 '2001:4860::1' "$port" "$cert_dir" "$reload"; then fail 'ACME failure reported success'; fi +cmp "$TEMP/working.conf" "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf" || fail 'a failed IPv6 attempt rewrote the working certificate renewal entry' +[[ -z "$(find "$ACME_HOME" -maxdepth 1 -name '.xui-acme.*')" ]] || fail 'the renewal entry backup was left behind' +printf 'PASS: a failed order restores the renewal entry of the working certificate\n' +rm -rf "$ACME_HOME/198.51.100.8_ecc" +: > "$ACME_LOG" ACME_ISSUE_FAIL=0 ip_ssl_issue_and_install "$acme" 198.51.100.8 '2001:4860::1' "$port" "$cert_dir" "$reload" diff --git a/scripts/test/ip-ssl-entrypoint-test.sh b/scripts/test/ip-ssl-entrypoint-test.sh index 895c0409e..259529f51 100644 --- a/scripts/test/ip-ssl-entrypoint-test.sh +++ b/scripts/test/ip-ssl-entrypoint-test.sh @@ -18,9 +18,11 @@ setup_ip_certificate() { captured="${2:-}"; called=1; } systemctl() { stopped=1; } export NONINTERACTIVE=1 XUI_SSL_MODE=ip XUI_SSL_IPV6='not:ipv6' called=0; stopped=0 -if prompt_and_setup_ssl 2053 / 198.51.100.8 > "$TEMP/invalid" 2>&1; then fail 'invalid environment IPv6 accepted by installer'; fi +SSL_HOST='' +prompt_and_setup_ssl 2053 / 198.51.100.8 > "$TEMP/invalid" 2>&1 || fail 'an invalid XUI_SSL_IPV6 aborted the installer instead of skipping the certificate' [[ "$called" == 0 && "$stopped" == 0 ]] || fail 'invalid IPv6 stopped panel or reached issuance' grep -q 'Invalid public IPv6' "$TEMP/invalid" || fail 'invalid environment value has no clear diagnostic' +[[ "$SSL_HOST" == 198.51.100.8 ]] || fail 'the installer lost its access host after skipping the certificate' unset XUI_SSL_IPV6 called=0 prompt_and_setup_ssl 2053 / 198.51.100.8 "$TEMP/unattended" 2>&1 @@ -42,25 +44,14 @@ prompt_and_setup_ssl 2053 / 198.51.100.8 <<< $'\ny\nn\n' > "$TEMP/rejected" 2>&1 printf 'PASS: installer consent, explicit configuration, no unattended prompts, and validation before service stop\n' setup_ip_certificate() { return 1; } -systemctl() { - printf '%s\n' "$*" >> "$TEMP/service-calls" - [[ "$1" != is-active ]] || [[ "$was_running" == 1 ]] -} -rc-service() { - printf '%s\n' "$*" >> "$TEMP/service-calls" - [[ "$2" != status ]] || [[ "$was_running" == 1 ]] -} +systemctl() { :; } +rc-service() { :; } NONINTERACTIVE=1 export XUI_SSL_IPV6='2001:4860::1' for release in ubuntu alpine; do - for was_running in 0 1; do - : > "$TEMP/service-calls" - if prompt_and_setup_ssl 2053 / 198.51.100.8 > /dev/null 2>&1; then fail 'failed IP setup reported success'; fi - if [[ "$was_running" == 1 ]]; then - grep -Eq '^(start x-ui|x-ui start)$' "$TEMP/service-calls" || fail "failed IP setup left the previous $release panel stopped" - elif grep -Eq '^(start x-ui|x-ui start)$' "$TEMP/service-calls"; then - fail "failed IP setup started an inactive $release panel" - fi - done + SSL_HOST='' + prompt_and_setup_ssl 2053 / 198.51.100.8 > "$TEMP/failed" 2>&1 || fail "a failed IP certificate aborted the $release installer: the panel must still be installed over HTTP" + grep -q 'IP certificate setup failed' "$TEMP/failed" || fail 'failed issuance was not reported' + [[ "$SSL_HOST" == 198.51.100.8 ]] || fail 'the installer lost its access host after a failed certificate' done -printf 'PASS: failed issuance restores only a previously running panel on systemd/OpenRC\n' +printf 'PASS: a failed IP certificate is reported and the installer carries on on systemd/OpenRC\n' diff --git a/x-ui.sh b/x-ui.sh index 5d6af4202..b9d5d70a4 100644 --- a/x-ui.sh +++ b/x-ui.sh @@ -269,6 +269,7 @@ ip_ssl_shell_quote() { ip_ssl_issue_and_install() ( local acme="$1" ipv4="$2" ipv6="$3" port="$4" cert_dir="$5" reload="$6" local stage='' attempted=0 installed=0 code=0 file restored + local acme_home="${acme%/*}" renewal_backup entry local quoted_cert quoted_key validation check_key reload_guard local -a domain_args=(-d "$ipv4") if ! ip_ssl_ipv4 "$ipv4" || { [[ -n "$ipv6" ]] && ! ip_ssl_public_ipv6 "$ipv6"; }; then @@ -278,11 +279,26 @@ ip_ssl_issue_and_install() ( [[ "$port" =~ ^[0-9]{1,5}$ ]] && ((10#$port >= 1 && 10#$port <= 65535)) || return 1 [[ -z "$ipv6" ]] || domain_args+=(-d "$ipv6") ip_ssl_prepare_listener "$acme" "$port" "$ipv6" || return 1 + renewal_backup=$(mktemp -d "$acme_home/.xui-acme.XXXXXX") || return 1 + for entry in "$ipv4" "${ipv4}_ecc"; do + [[ ! -d "$acme_home/$entry" ]] || cp -a -- "$acme_home/$entry" "$renewal_backup/$entry" || { + rm -rf -- "$renewal_backup" + return 1 + } + done if ! "$acme" --issue "${domain_args[@]}" --standalone "${IP_SSL_LISTEN_ARGS[@]}" \ --server letsencrypt --certificate-profile shortlived --days 6 --httpport "$port" --force; then + # acme.sh saves the requested names before it places the order, so a failed one would + # make cron renew the working certificate with the names that just failed. + for entry in "$ipv4" "${ipv4}_ecc"; do + rm -rf -- "${acme_home:?}/$entry" + [[ ! -d "$renewal_backup/$entry" ]] || mv -- "$renewal_backup/$entry" "$acme_home/$entry" + done + rm -rf -- "$renewal_backup" printf 'IP certificate issuance failed. Existing certificates and ACME data were preserved. Check external port 80 on every selected address.\n' >&2 return 1 fi + rm -rf -- "$renewal_backup" umask 077 mkdir -p "$cert_dir" || return 1