#!/usr/bin/env bash set -euo pipefail certificate_domain="${SEA_FTP_CERTIFICATE_DOMAIN:-}" test_host="${SEA_FTP_TEST_HOST:-}" for domain in "${certificate_domain}" "${test_host}"; do if [[ ! "${domain}" =~ ^[A-Za-z0-9.-]+$ ]] || [[ "${domain}" == .* ]] || [[ "${domain}" == *..* ]]; then echo "The FTP certificate domain or test host is invalid." >&2 exit 1 fi done certificate_directory="/www/server/panel/vhost/cert/${certificate_domain}" certificate_file="${certificate_directory}/fullchain.pem" private_key_file="${certificate_directory}/privkey.pem" for regular_file in "${certificate_file}" "${private_key_file}" /etc/sea-vhost/sea-vhost.env; do if [[ ! -f "${regular_file}" || -L "${regular_file}" ]]; then printf 'Required regular file is missing or unsafe: %s\n' "${regular_file}" >&2 exit 1 fi done openssl x509 -in "${certificate_file}" -noout -checkhost "${certificate_domain}" >/dev/null openssl x509 -in "${certificate_file}" -noout -checkhost "${test_host}" >/dev/null openssl pkey -in "${private_key_file}" -noout -check >/dev/null 2>&1 sync_script=/opt/sea-vhost/bin/sea-vhost-ftps-sync temporary_sync="$(mktemp /opt/sea-vhost/bin/.sea-vhost-ftps-sync-XXXXXX.tmp)" cat >"${temporary_sync}" <<'EOF' #!/usr/bin/env bash set -euo pipefail domain="${1:-}" [[ "${domain}" =~ ^[A-Za-z0-9.-]+$ ]] token="$(sed -n 's/^SEA_INTERNAL_AUTH_TOKEN=//p' /etc/sea-vhost/sea-vhost.env | head -n 1)" [[ "${#token}" -ge 32 ]] response="$(curl --silent --show-error --fail --max-time 45 \ --unix-socket /run/sea-vhost/agent.sock \ --header "Authorization: Bearer ${token}" \ --header 'Content-Type: application/json' \ --request POST \ --data "{\"domain\":\"${domain}\"}" \ http://localhost/v1/ftp/tls)" grep -Fq '"policy":"explicit_tls_required"' <<<"${response}" systemctl stop pure-ftpd.service >/dev/null 2>&1 || true ftp_binary="$(readlink -f /www/server/pure-ftpd/sbin/pure-ftpd)" for proc_exe in /proc/[0-9]*/exe; do [[ "$(readlink -f "${proc_exe}" 2>/dev/null || true)" == "${ftp_binary}" ]] || continue proc_pid="${proc_exe#/proc/}" proc_pid="${proc_pid%/exe}" kill -TERM "${proc_pid}" 2>/dev/null || true done sleep 1 for proc_exe in /proc/[0-9]*/exe; do [[ "$(readlink -f "${proc_exe}" 2>/dev/null || true)" == "${ftp_binary}" ]] || continue proc_pid="${proc_exe#/proc/}" proc_pid="${proc_pid%/exe}" kill -KILL "${proc_pid}" 2>/dev/null || true done systemctl start pure-ftpd.service EOF chown root:root "${temporary_sync}" chmod 0750 "${temporary_sync}" mv -f -- "${temporary_sync}" "${sync_script}" cat >/etc/systemd/system/sea-vhost-ftps-sync.service </etc/systemd/system/sea-vhost-ftps-sync.timer <<'EOF' [Unit] Description=Daily Sea vHost Pure-FTPd certificate synchronization [Timer] OnCalendar=daily Persistent=true RandomizedDelaySec=30m Unit=sea-vhost-ftps-sync.service [Install] WantedBy=timers.target EOF chown root:root /etc/systemd/system/sea-vhost-ftps-sync.service /etc/systemd/system/sea-vhost-ftps-sync.timer chmod 0644 /etc/systemd/system/sea-vhost-ftps-sync.service /etc/systemd/system/sea-vhost-ftps-sync.timer systemctl daemon-reload systemctl enable --now sea-vhost-ftps-sync.timer systemctl start sea-vhost-ftps-sync.service grep -Eq '^[[:space:]]*TLS[[:space:]]+2[[:space:]]*$' /www/server/pure-ftpd/etc/pure-ftpd.conf test "$(stat -Lc '%a' /www/server/pure-ftpd/etc/pure-ftpd.pem)" = 600 timeout 20 openssl s_client -connect 127.0.0.1:21 -starttls ftp -servername "${test_host}" \ -verify_hostname "${test_host}" -verify_return_error &1 | grep -Fq 'Verify return code: 0 (ok)' echo "Sea vHost production FTPS activation result: success"