#!/usr/bin/env bash set -uo pipefail failures=0 warnings=0 agent_socket="${SEA_AGENT_SOCKET:-/run/sea-vhost/agent.sock}" panel_broker_socket="${SEA_PANEL_BROKER_SOCKET:-/run/sea-vhost-panel/broker.sock}" panel_broker_state="${SEA_PANEL_BROKER_STATE:-/var/lib/sea-vhost-panel}" config_file="${SEA_CONFIG_FILE:-/var/lib/sea-vhost/config.json}" environment_file="/etc/sea-vhost/sea-vhost.env" nginx_vhost_root="${SEA_NGINX_VHOST_ROOT:-/www/server/panel/vhost/nginx}" nginx_security_global="${nginx_vhost_root}/sea-vhost-security-global.conf" backup_root="${SEA_BACKUP_ROOT:-/www/backup/sea-vhost}" production_mode="${SEA_DIAGNOSE_PRODUCTION:-false}" pass() { printf '[PASS] %s\n' "$1"; } warn() { printf '[WARN] %s\n' "$1"; warnings=$((warnings + 1)); } fail() { printf '[FAIL] %s\n' "$1"; failures=$((failures + 1)); } security_finding() { if [[ "${production_mode}" == "true" ]]; then fail "$1"; else warn "$1"; fi } check_service() { local service="$1" if systemctl is-active --quiet "${service}"; then pass "${service} is active" else fail "${service} is not active" fi } check_mode() { local path="$1" expected="$2" label="$3" actual if [[ ! -e "${path}" ]]; then warn "${label} does not exist: ${path}" return fi actual="$(stat -c '%a' "${path}" 2>/dev/null || true)" if [[ "${actual}" == "${expected}" ]]; then pass "${label} permissions are ${actual}" else fail "${label} permissions are ${actual:-unknown}; expected ${expected}" fi } check_owner() { local path="$1" expected="$2" label="$3" actual if [[ ! -e "${path}" ]]; then warn "${label} does not exist: ${path}" return fi actual="$(stat -c '%U:%G' "${path}" 2>/dev/null || true)" if [[ "${actual}" == "${expected}" ]]; then pass "${label} owner is ${actual}" else fail "${label} owner is ${actual:-unknown}; expected ${expected}" fi } printf 'Sea vHost deployment diagnostics\n' printf '================================\n' if [[ "${production_mode}" == "true" ]]; then pass 'production security gate is enabled' else warn 'production security gate is disabled; set SEA_DIAGNOSE_PRODUCTION=true for release acceptance' fi for command in systemctl stat curl; do if command -v "${command}" >/dev/null 2>&1; then pass "command available: ${command}" else fail "missing command: ${command}" fi done if command -v systemctl >/dev/null 2>&1; then check_service sea-vhost-agent.service check_service sea-vhost-panel-broker.service check_service sea-vhost-api.service fi if id www >/dev/null 2>&1 && id -nG www | tr ' ' '\n' | grep -Eq '^sea-vhost-(control|agent-control|panel-control)$'; then fail 'www user must not belong to a Sea vHost control group' else pass 'www user is excluded from all Sea vHost control groups' fi if id sea-vhost-panel >/dev/null 2>&1 && id -nG sea-vhost-panel | tr ' ' '\n' | grep -qx 'sea-vhost-agent-control'; then fail 'panel broker user must not belong to the root-agent control group' else pass 'panel broker is excluded from the root-agent control group' fi for required_group in sea-vhost-agent-control sea-vhost-panel-control; do if id sea-vhost >/dev/null 2>&1 && id -nG sea-vhost | tr ' ' '\n' | grep -qx "${required_group}"; then pass "API user belongs to ${required_group}" else fail "API user is missing required group: ${required_group}" fi done check_mode "${environment_file}" 640 'environment file' check_owner "${environment_file}" 'root:sea-vhost-control' 'environment file' check_mode "${nginx_security_global}" 644 'Nginx security baseline' check_owner "${nginx_security_global}" 'root:root' 'Nginx security baseline' check_mode "${backup_root}" 700 'lifecycle backup root' check_owner "${backup_root}" 'root:root' 'lifecycle backup root' if [[ -e "${config_file}" ]]; then check_mode "${config_file}" 600 'installed configuration' check_owner "${config_file}" 'sea-vhost:sea-vhost-control' 'installed configuration' else warn "installed configuration is absent; Web setup should be active" fi if [[ -S "${agent_socket}" ]]; then socket_metadata="$(stat -c '%U:%G:%a' "${agent_socket}" 2>/dev/null || true)" if [[ "${socket_metadata}" == "root:sea-vhost-agent-control:660" ]]; then pass "isolation socket ownership is ${socket_metadata}" else fail "isolation socket ownership is ${socket_metadata:-unknown}; expected root:sea-vhost-agent-control:660" fi if curl --silent --show-error --fail --max-time 5 --unix-socket "${agent_socket}" http://localhost/healthz >/dev/null; then pass 'isolation agent health check succeeded' else fail 'isolation agent health check failed' fi else fail "isolation socket is missing: ${agent_socket}" fi if [[ -S "${panel_broker_socket}" ]]; then broker_metadata="$(stat -c '%U:%G:%a' "${panel_broker_socket}" 2>/dev/null || true)" if [[ "${broker_metadata}" == "sea-vhost-panel:sea-vhost-panel-control:660" ]]; then pass "panel broker socket ownership is ${broker_metadata}" else fail "panel broker socket ownership is ${broker_metadata:-unknown}; expected sea-vhost-panel:sea-vhost-panel-control:660" fi broker_health="$(curl --silent --show-error --fail --max-time 5 --unix-socket "${panel_broker_socket}" http://localhost/healthz 2>/dev/null || true)" if [[ "${broker_health}" == *'"status":"ok"'* || "${broker_health}" == *'"status":"unconfigured"'* ]]; then pass 'panel broker health check succeeded' else fail 'panel broker health check failed' fi else fail "panel broker socket is missing: ${panel_broker_socket}" fi if [[ -f "${panel_broker_state}/credentials.json" ]]; then check_mode "${panel_broker_state}/credentials.json" 600 'panel broker credentials' check_owner "${panel_broker_state}/credentials.json" 'sea-vhost-panel:sea-vhost-panel-control' 'panel broker credentials' panel_url="$(sed -nE 's/^[[:space:]]*"url"[[:space:]]*:[[:space:]]*"([^"]+)".*/\1/p' "${panel_broker_state}/credentials.json" | head -n 1)" if [[ "${panel_url}" =~ ^https?://(127\.0\.0\.1|localhost)(:[0-9]{1,5})?/?$ || "${panel_url}" =~ ^https?://\[::1\](:[0-9]{1,5})?/?$ ]]; then pass 'panel broker target is loopback-only' else security_finding 'panel broker target is missing or is not a loopback URL' fi if grep -Eq '"tlsInsecure"[[:space:]]*:[[:space:]]*true' "${panel_broker_state}/credentials.json"; then security_finding 'panel broker accepts an untrusted aaPanel TLS certificate' else pass 'panel broker requires a trusted aaPanel TLS certificate' fi panel_port="$(printf '%s\n' "${panel_url}" | sed -nE 's#^https?://(127\.0\.0\.1|localhost|\[::1\]):([0-9]{1,5})/?$#\2#p')" if [[ -n "${panel_port}" ]] && command -v ss >/dev/null 2>&1; then if ss -ltnH 2>/dev/null | awk '{print $4}' | grep -Eq "(^|0\\.0\\.0\\.0:|\\[::\\]:|\\*:)${panel_port}$"; then security_finding "aaPanel port ${panel_port} listens on a wildcard address; firewall/VPN exposure must be checked externally" else pass "aaPanel port ${panel_port} is not bound to a wildcard address" fi fi fi if [[ -f "${config_file}" ]] && grep -q 'panel_api_key' "${config_file}"; then fail 'Web API configuration still contains a legacy panel API key' else pass 'Web API configuration does not contain the panel API key' fi if grep -Eq '^SEA_COOKIE_SECURE=false([[:space:]]*)$' "${environment_file}" 2>/dev/null; then security_finding 'session cookies are allowed over plain HTTP' else pass 'session cookies require HTTPS' fi api_health="$(curl --silent --show-error --fail --max-time 5 http://127.0.0.1:8080/healthz 2>/dev/null || true)" if [[ "${api_health}" == *'"status":"ok"'* ]]; then pass 'Sea vHost API is running normally' elif [[ "${api_health}" == *'"status":"setup_required"'* ]]; then pass 'Sea vHost API is waiting for Web setup' else fail 'Sea vHost API health check failed' fi nginx_binary="" if command -v nginx >/dev/null 2>&1; then nginx_binary="$(command -v nginx)" elif [[ -x /www/server/nginx/sbin/nginx ]]; then nginx_binary="/www/server/nginx/sbin/nginx" fi if [[ -n "${nginx_binary}" ]]; then if "${nginx_binary}" -t >/dev/null 2>&1; then pass 'Nginx configuration test succeeded' else fail 'Nginx configuration test failed' fi else warn 'Nginx executable was not found in PATH or the standard aaPanel location' fi printf '\nResult: %d failure(s), %d warning(s)\n' "${failures}" "${warnings}" if (( failures > 0 )); then exit 1 fi