#!/usr/bin/env bash # ============================================================================== # ClassNotify Production Configuration & Security Audit Script # Validates configuration drift, permissions, listening ports, and services # (Zero secret values are ever printed) # ============================================================================== set -euo pipefail PASS_COUNT=0 FAIL_COUNT=0 report() { local name="$1" local status="$2" local detail="${3:-}" if [ "$status" -eq 0 ]; then echo -e "\e[32m[PASS]\e[0m $name" [ -n "$detail" ] && echo -e " \e[90m$detail\e[0m" PASS_COUNT=$((PASS_COUNT + 1)) else echo -e "\e[31m[FAIL]\e[0m $name" [ -n "$detail" ] && echo -e " \e[33m$detail\e[0m" FAIL_COUNT=$((FAIL_COUNT + 1)) fi } echo "==========================================================" echo " ClassNotify Production Configuration Integrity Audit " echo "==========================================================" # 1. systemd Unit Integrity Check SYSTEMD_FILE="/etc/systemd/system/classnotify.service" if [ -f "$SYSTEMD_FILE" ]; then ACTUAL_SYS_HASH=$(sha256sum "$SYSTEMD_FILE" | awk '{print toupper($1)}') EXPECTED_SYS_HASH="BEC7EC0ED3321026978ABDF45131E9F190312D2E951E1AA07CED7287DC28F65F" if [ "$ACTUAL_SYS_HASH" = "$EXPECTED_SYS_HASH" ]; then report "systemd service unit SHA256 matches repository specification" 0 "Hash: $ACTUAL_SYS_HASH" else report "systemd service unit SHA256 matches repository specification" 1 "Expected: $EXPECTED_SYS_HASH, Actual: $ACTUAL_SYS_HASH" fi else report "systemd service unit file exists" 1 "File missing: $SYSTEMD_FILE" fi # 2. Caddy Snippet Integrity Check CADDY_SNIPPET="/etc/caddy/classnotify.caddy" if [ -f "$CADDY_SNIPPET" ]; then ACTUAL_CAD_HASH=$(sha256sum "$CADDY_SNIPPET" | awk '{print toupper($1)}') EXPECTED_CAD_HASH="A704FCB30CADB5DFA4C60FDCB9FE1E018A367557B96AC52820E77BF3DAF94F89" if [ "$ACTUAL_CAD_HASH" = "$EXPECTED_CAD_HASH" ]; then report "Caddy snippet SHA256 matches repository specification" 0 "Hash: $ACTUAL_CAD_HASH" else report "Caddy snippet SHA256 matches repository specification" 1 "Expected: $EXPECTED_CAD_HASH, Actual: $ACTUAL_CAD_HASH" fi else report "Caddy snippet file exists" 1 "File missing: $CADDY_SNIPPET" fi # 3. Environment File Permissions (600 root:root) ENV_FILE="/etc/classnotify/classnotify.env" if [ -f "$ENV_FILE" ]; then ENV_PERM=$(stat -c "%a %U:%G" "$ENV_FILE") if [ "$ENV_PERM" = "600 root:root" ]; then report "classnotify.env permissions strictly 600 root:root" 0 "Mode: $ENV_PERM" else report "classnotify.env permissions strictly 600 root:root" 1 "Actual: $ENV_PERM" fi else report "classnotify.env exists" 1 "File missing: $ENV_FILE" fi # 4. Backup & Monitor Environment File Permissions for conf in "/etc/classnotify/backup.env" "/etc/classnotify/monitor.env"; do if [ -f "$conf" ]; then CONF_PERM=$(stat -c "%a %U:%G" "$conf") if [ "$CONF_PERM" = "600 root:root" ]; then report "$(basename "$conf") permissions strictly 600 root:root" 0 "Mode: $CONF_PERM" else report "$(basename "$conf") permissions strictly 600 root:root" 1 "Actual: $CONF_PERM" fi else report "$(basename "$conf") exists" 1 "File missing: $conf" fi done # 5. DataProtection Key Directory Security KEYS_DIR="/var/lib/classnotify/keys" if [ -d "$KEYS_DIR" ]; then KEYS_PERM=$(stat -c "%a %U:%G" "$KEYS_DIR") if [ "$KEYS_PERM" = "700 www-data:www-data" ]; then report "DataProtection keys directory strictly 700 www-data:www-data" 0 "Mode: $KEYS_PERM" else report "DataProtection keys directory strictly 700 www-data:www-data" 1 "Actual: $KEYS_PERM" fi else report "DataProtection keys directory exists" 1 "Directory missing: $KEYS_DIR" fi # 6. PostgreSQL Local-Only Binding PG_PORTS=$(ss -tlpn | grep ":5432 " || true) if echo "$PG_PORTS" | grep -qE "0\.0\.0\.0:5432|\*:5432|:::5432"; then report "PostgreSQL bound strictly to local loopback (127.0.0.1)" 1 "Exposed on public interface: $PG_PORTS" elif echo "$PG_PORTS" | grep -q "127.0.0.1:5432"; then report "PostgreSQL bound strictly to local loopback (127.0.0.1)" 0 "Listening on 127.0.0.1:5432" else report "PostgreSQL socket verified" 0 "Unix domain socket / loopback only" fi # 7. Kestrel Local-Only Binding KESTREL_PORTS=$(ss -tlpn | grep ":5000 " || true) if echo "$KESTREL_PORTS" | grep -qE "0\.0\.0\.0:5000|\*:5000|:::5000"; then report "Kestrel bound strictly to 127.0.0.1:5000" 1 "Exposed on public interface: $KESTREL_PORTS" elif echo "$KESTREL_PORTS" | grep -q "127.0.0.1:5000"; then report "Kestrel bound strictly to 127.0.0.1:5000" 0 "Listening on 127.0.0.1:5000" else report "Kestrel port 5000 check" 1 "Not listening on 5000" fi # 8. Service Statuses for srv in "classnotify.service" "caddy.service" "postgresql.service"; do if systemctl is-active --quiet "$srv"; then report "$srv is active and running" 0 else report "$srv is active and running" 1 "Status: $(systemctl is-active "$srv" || true)" fi done # 9. Current Release Symlink Validity CURRENT_TARGET=$(readlink -f /var/www/classnotify/current 2>/dev/null || true) if [ -n "$CURRENT_TARGET" ] && [ -d "$CURRENT_TARGET" ] && [ -f "$CURRENT_TARGET/ClassNotify.Server.dll" ]; then report "Current symlink points to valid release with ClassNotify.Server.dll" 0 "Target: $(basename "$CURRENT_TARGET")" else report "Current symlink points to valid release with ClassNotify.Server.dll" 1 "Target: $CURRENT_TARGET" fi echo "==========================================================" echo "Verification Summary: ${PASS_COUNT} Passed, ${FAIL_COUNT} Failed" echo "==========================================================" if [ "$FAIL_COUNT" -eq 0 ]; then exit 0 else exit 1 fi