From bdbe50e9e0fd64b5c635225f1340184373027202 Mon Sep 17 00:00:00 2001 From: Daniel Winzen Date: Mon, 12 Oct 2026 12:31:04 +0200 Subject: [PATCH] Implement dynamic rspamd map --- README.md | 2 + common_config.php | 12 +++++- etc/rspamd/rspamd.conf.override | 2 + setup.php | 8 +++- www/admin.php | 23 ++++++++--- www/manage_account.php | 38 +++++++++++++++--- www/rspamd_settings.php | 69 +++++++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 etc/rspamd/rspamd.conf.override create mode 100644 www/rspamd_settings.php diff --git a/README.md b/README.md index d31796f..aa7281c 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,8 @@ Generate a keypair for rspamd with `rspamadm keypair gen` and add it to /etc/rsp Set a password for the web interface with `rspamadm pw` and add the hash for it to /etc/rspamd/override.d/worker-controller.inc +Configure the Rspamd dynamic settings map by replacing `CHANGE_ME_TO_A_LONG_RANDOM_SECRET` with the same value in `/var/www/mail/common_config.php` and `/etc/rspamd/rspamd.conf.override`. Generate a secret with `openssl rand -hex 32`. Also replace the example hostname in the Rspamd override URL with your mail server's public hostname. The endpoint returns the settings map in Rspamd's UCL format and is authenticated because the map contains mailbox addresses and filtering preferences. + Generate DKIM signing keys and add them to /etc/rspamd/local.d/arc.conf /etc/rspamd/local.d/dkim_signing.conf, then add the printed DNS records to your domain: ``` rspamadm dkim_keygen -d YOUR_DOMAIN -s $(date +"%Y%m%d")-rsa -b 4096 -t rsa -k /var/lib/rspamd/dkim/YOUR_DOMAIN-rsa diff --git a/common_config.php b/common_config.php index 3049fb2..d6fedb8 100644 --- a/common_config.php +++ b/common_config.php @@ -9,10 +9,20 @@ const DBHOST = 'localhost'; // Database host const DBUSER = 'postfix'; // Database user const DBPASS = 'YOUR_PASSWORD'; // Database password const DBNAME = 'postfix'; // Database -const DBVERSION = 4; // Database schema version +const DBVERSION = 5; // Database schema version const INACTIVITY_PERIOD_MONTHS = [1, 6, 12, 24]; // Allowed inactivity periods in months - don't forget to update get_inactivity_period_options on changes here and if an option is removed, you should also update accounts in the database mailbox table const DEFAULT_INACTIVITY_PERIOD = 12; // Default inactivity period in months const ALLOW_ACCOUNT_NEVER_DELETE = false; // Whether to allow users to set their inactivity period to "never delete" +const RSPAMD_SETTINGS_MAP_TOKEN = 'CHANGE_ME_TO_A_LONG_RANDOM_SECRET'; // Token used to authenticate requests to the Rspamd settings map endpoint +const SPAM_REJECT_SCORE_OPTIONS = [0, 5, 8, 10, 12, 15, 20, 25, 30]; // Allowed spam reject scores +const DEFAULT_SPAM_REJECT_SCORE = 15; // Default spam reject score +const CUSTOM_RSPAMD_SETTINGS = 'whitelist_contact_form { + priority = low; + rcpt = "daniel@danwin1210.de"; + want_spam = yes; + from = "www-data@danwin1210.de"; +} +'; // Custom Rspamd settings to be added to the Rspamd settings map const PERSISTENT = true; // persistent database connection const CAPTCHA_DIFFICULTY = 1; // captcha difficulty from 0 to 4 const RESERVED_USERNAMES = ['about', 'abuse', 'admin', 'administrator', 'billing', 'contact', 'daemon', 'ftp', 'help', 'hostmaster', 'info', 'legal', 'list', 'list-request', 'lists', 'maildaemon', 'mailerdaemon', 'mailer-daemon', 'marketing', 'media', 'news', 'newsletter', 'nobody', 'noc', 'noreply', 'no-reply', 'notification', 'notifications', 'notify', 'offer', 'offers', 'office', 'official', 'order', 'orders', 'phish', 'phishing', 'postmaster', 'root', 'sale', 'sales', 'security', 'service', 'services', 'shop', 'shopping', 'spam', 'staff', 'support', 'survey', 'system', 'team', 'teams', 'unsubscribe', 'uucp', 'usenet', 'user', 'username', 'users', 'web', 'webmail', 'webmaster', 'webmasters', 'welcome', 'www']; // list of reserved usernames that can not be used on public registration diff --git a/etc/rspamd/rspamd.conf.override b/etc/rspamd/rspamd.conf.override new file mode 100644 index 0000000..4d188fc --- /dev/null +++ b/etc/rspamd/rspamd.conf.override @@ -0,0 +1,2 @@ +# Replace this token with the same random secret configured in common_config.php. +settings = "https://mail.danwin1210.de/rspamd_settings.php?token=CHANGE_ME_TO_A_LONG_RANDOM_SECRET"; diff --git a/setup.php b/setup.php index d226770..c8915a1 100644 --- a/setup.php +++ b/setup.php @@ -36,7 +36,7 @@ $createTableStatements = [ 'dmarc_report_record_reason' => "CREATE TABLE IF NOT EXISTS `dmarc_report_record_reason` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `record_id` bigint(20) unsigned NOT NULL, `type` varchar(255) NOT NULL, `comment` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `record_id` (`record_id`), CONSTRAINT `dmarc_report_record_reason_ibfk_1` FOREIGN KEY (`record_id`) REFERENCES `dmarc_report_records` (`id`) ON DELETE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'dmarc_report_record_spf_result' => "CREATE TABLE IF NOT EXISTS `dmarc_report_record_spf_result` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `record_id` bigint(20) unsigned NOT NULL, `domain` varchar(255) NOT NULL, `result` varchar(255) NOT NULL, `scope` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `record_id` (`record_id`), CONSTRAINT `dmarc_report_record_spf_result_ibfk_2` FOREIGN KEY (`record_id`) REFERENCES `dmarc_report_records` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;", 'domain_admins' => "CREATE TABLE IF NOT EXISTS `domain_admins` (`username` varchar(255) NOT NULL, `domain` varchar(255) NOT NULL, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `id` bigint(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), KEY `username` (`username`), KEY `active` (`active`), KEY `domain` (`domain`), CONSTRAINT `domain_admins_ibfk_1` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `domain_admins_ibfk_2` FOREIGN KEY (`username`) REFERENCES `admin` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", - 'mailbox' => "CREATE TABLE IF NOT EXISTS `mailbox` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL DEFAULT '', `quota` bigint(20) NOT NULL DEFAULT 0, `local_part` varchar(255) NOT NULL DEFAULT '', `domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT current_timestamp(), `modified` datetime NOT NULL DEFAULT current_timestamp(), `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '', `openpgpkey_wkd` char(32) NOT NULL DEFAULT '', `pgp_key` text DEFAULT NULL, `pgp_verified` tinyint(1) NOT NULL DEFAULT 0, `tfa` tinyint(1) NOT NULL DEFAULT 0, `totp_secret` varchar(64) DEFAULT NULL, `last_login` bigint(20) unsigned DEFAULT NULL, `inactivity_period` tinyint unsigned NOT NULL DEFAULT 12, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", + 'mailbox' => "CREATE TABLE IF NOT EXISTS `mailbox` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL DEFAULT '', `quota` bigint(20) NOT NULL DEFAULT 0, `local_part` varchar(255) NOT NULL DEFAULT '', `domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT current_timestamp(), `modified` datetime NOT NULL DEFAULT current_timestamp(), `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '', `openpgpkey_wkd` char(32) NOT NULL DEFAULT '', `pgp_key` text DEFAULT NULL, `pgp_verified` tinyint(1) NOT NULL DEFAULT 0, `tfa` tinyint(1) NOT NULL DEFAULT 0, `totp_secret` varchar(64) DEFAULT NULL, `last_login` bigint(20) unsigned DEFAULT NULL, `inactivity_period` tinyint unsigned NOT NULL DEFAULT 12, `spam_reject_score` tinyint unsigned NOT NULL DEFAULT 15, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'monitored_mailboxes' => "CREATE TABLE IF NOT EXISTS `monitored_mailboxes` (`username` varchar(255) NOT NULL, `last_login` int(11) NOT NULL, PRIMARY KEY (`username`), KEY `lastlogin` (`last_login`), CONSTRAINT `monitored_mailboxes_ibfk_1` FOREIGN KEY (`username`) REFERENCES `mailbox` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'tls_report' => "CREATE TABLE IF NOT EXISTS `tls_report` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `report_id` varchar(255) NOT NULL, `organization` varchar(255) NOT NULL, `contact` varchar(255) NOT NULL, `start` datetime NOT NULL, `end` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `report_id_organization` (`report_id`,`organization`), KEY `start_end` (`start`,`end`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'tls_report_policy' => "CREATE TABLE IF NOT EXISTS `tls_report_policy` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `report_id` bigint(20) unsigned NOT NULL, `policy_type` varchar(255) NOT NULL, `policy_string` text NOT NULL, `policy_domain` varchar(255) NOT NULL, `mx_host` text NOT NULL, `success` int(11) NOT NULL, `failure` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `report_id` (`report_id`), KEY `policy_domain` (`policy_domain`), CONSTRAINT `tls_report_policy_ibfk_1` FOREIGN KEY (`report_id`) REFERENCES `tls_report` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `tls_report_policy_ibfk_3` FOREIGN KEY (`policy_domain`) REFERENCES `domain` (`domain`) ON DELETE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", @@ -65,6 +65,12 @@ try{ $db->exec( 'ALTER TABLE `mailbox` ADD COLUMN `inactivity_period` tinyint unsigned NOT NULL DEFAULT 12 AFTER `last_login`;' ); } } + if ( $version < 5 ) { + $stmt = $db->query( "SHOW COLUMNS FROM `mailbox` LIKE 'spam_reject_score';" ); + if ( ! $stmt->fetch() ) { + $db->exec( 'ALTER TABLE `mailbox` ADD COLUMN `spam_reject_score` tinyint unsigned NOT NULL DEFAULT 15 AFTER `inactivity_period`;' ); + } + } $db->beginTransaction(); $stmt=$db->prepare("UPDATE `settings` SET `value`=? WHERE `setting`='version';"); $stmt->execute([DBVERSION]); diff --git a/www/admin.php b/www/admin.php index 0749372..5bbc541 100644 --- a/www/admin.php +++ b/www/admin.php @@ -339,8 +339,10 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { in_array( $inactivity_period, INACTIVITY_PERIOD_MONTHS, true ) || ( ALLOW_ACCOUNT_NEVER_DELETE && $inactivity_period === 0 ) ); - if ( ! $valid_inactivity_period ) { - $msg .= ''; + $spam_reject_score = filter_var( $_POST[ 'spam_reject_score' ] ?? null, FILTER_VALIDATE_INT ); + $valid_spam_settings = in_array( $spam_reject_score, SPAM_REJECT_SCORE_OPTIONS, true ); + if ( ! $valid_inactivity_period || ! $valid_spam_settings ) { + $msg .= ''; } else { $alias_goto = ''; if ( isset( $_POST[ 'alias_keep_copy' ] ) ) { @@ -353,8 +355,8 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { $alias_goto = rtrim( $alias_goto, ',' ); $stmt = $db->prepare( 'UPDATE alias SET goto = ?, enforce_tls_in = ?, active = ? WHERE address = ?;' ); $stmt->execute( [ $alias_goto, ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ), $_POST[ 'user' ] ] ); - $stmt = $db->prepare( 'UPDATE mailbox SET enforce_tls_in = ?, enforce_tls_out = ?, active = ?, quota = ?, inactivity_period = ?, modified = NOW() WHERE username = ?;' ); - $stmt->execute( [ ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'enforce_tls_out' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ), DEFAULT_QUOTA, $inactivity_period, $_POST[ 'user' ] ] ); + $stmt = $db->prepare( 'UPDATE mailbox SET enforce_tls_in = ?, enforce_tls_out = ?, active = ?, quota = ?, inactivity_period = ?, spam_reject_score = ?, modified = NOW() WHERE username = ?;' ); + $stmt->execute( [ ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'enforce_tls_out' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ), DEFAULT_QUOTA, $inactivity_period, $spam_reject_score, $_POST[ 'user' ] ] ); $msg .= '
'.htmlspecialchars(_('Successfully updated mailbox.')).'
'; } } @@ -1161,7 +1163,7 @@ function send_new_mailbox(): void function send_edit_mailbox(): void { $db = get_db_instance(); - $stmt = $db->prepare( 'SELECT a.goto, m.active, m.enforce_tls_in, m.enforce_tls_out, m.inactivity_period FROM alias AS a INNER JOIN mailbox AS m ON (m.username=a.address) WHERE m.username = ?;' ); + $stmt = $db->prepare( 'SELECT a.goto, m.active, m.enforce_tls_in, m.enforce_tls_out, m.inactivity_period, m.spam_reject_score FROM alias AS a INNER JOIN mailbox AS m ON (m.username=a.address) WHERE m.username = ?;' ); $stmt->execute( [ $_REQUEST[ 'user' ] ] ); if ( $email = $stmt->fetch( PDO::FETCH_ASSOC ) ) { $aliases = explode( ',', $email[ 'goto' ] ); @@ -1210,6 +1212,17 @@ function send_edit_mailbox(): void +
+
+
+ +
+
+

diff --git a/www/manage_account.php b/www/manage_account.php index 66cecd2..29e7a23 100644 --- a/www/manage_account.php +++ b/www/manage_account.php @@ -133,6 +133,15 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { $stmt->execute( [ $inactivity_period, $_SESSION[ 'email_user' ] ] ); $msg .= '
'.htmlspecialchars(_('Successfully updated the inactivity period.')).'
'; } + } elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'update_spam_settings' ) { + $spam_reject_score = filter_var( $_POST[ 'spam_reject_score' ] ?? null, FILTER_VALIDATE_INT ); + if ( ! in_array( $spam_reject_score, SPAM_REJECT_SCORE_OPTIONS, true ) ) { + $msg .= ''; + } else { + $stmt = $db->prepare( 'UPDATE mailbox SET spam_reject_score = ? WHERE username = ? AND active = 1;' ); + $stmt->execute( [ $spam_reject_score, $_SESSION[ 'email_user' ] ] ); + $msg .= '
'.htmlspecialchars(_('Successfully updated spam filter settings, changes can take up to 10 minutes to take effect.')).'
'; + } } elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'update_password' ) { if ( empty( $_POST[ 'pass_update' ] ) || empty( $_POST[ 'pass_update2' ] ) || $_POST[ 'pass_update' ] !== $_POST[ 'pass_update2' ] ) { $msg .= ''; @@ -375,9 +384,9 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?> $aliases = explode( ',', $tmp[ 'goto' ] ); } $aliases_to = implode( "\n", array_diff( $aliases, [ $_SESSION[ 'email_user' ] ] ) ); - $stmt = $db->prepare( 'SELECT enforce_tls_in, enforce_tls_out, inactivity_period FROM mailbox WHERE username = ?;' ); + $stmt = $db->prepare( 'SELECT enforce_tls_in, enforce_tls_out, inactivity_period, spam_reject_score FROM mailbox WHERE username = ?;' ); $stmt->execute( [ $_SESSION[ 'email_user' ] ] ); - $tls_status = $stmt->fetch( PDO::FETCH_ASSOC ); + $mailbox_options = $stmt->fetch( PDO::FETCH_ASSOC ); ?>

@@ -400,13 +409,13 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
> + id="enforce_tls_in">
> + id="enforce_tls_out">
@@ -416,6 +425,23 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
+
+

+

+ +
+
+
+ +
+
+ +
+

@@ -425,10 +451,10 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
diff --git a/www/rspamd_settings.php b/www/rspamd_settings.php new file mode 100644 index 0000000..9d8b6ae --- /dev/null +++ b/www/rspamd_settings.php @@ -0,0 +1,69 @@ +query( 'SELECT username, spam_reject_score FROM mailbox WHERE active = 1 AND spam_reject_score != ?;' ); +$stmt->execute( [ DEFAULT_SPAM_REJECT_SCORE ] ); +$recipients_by_policy = []; +while ( $mailbox = $stmt->fetch( PDO::FETCH_ASSOC ) ) { + if ( (int) $mailbox[ 'spam_reject_score' ] > 0 ) { + $policy = 'enabled_' . (int) $mailbox[ 'spam_reject_score' ]; + } else { + $policy = 'disabled'; + } + $recipients_by_policy[ $policy ][] = mb_strtolower( $mailbox[ 'username' ] ); +} + +$settings = []; +foreach ( $recipients_by_policy as $policy => $recipients ) { + $setting = [ + 'id' => 'mailbox_' . $policy, + 'priority' => 'low', + 'rcpt' => $recipients, + ]; + if ( $policy === 'disabled' ) { + $setting[ 'want_spam' ] = true; + } else { + $setting[ 'apply' ] = [ + 'actions' => [ + 'reject' => (float) substr( $policy, strlen( 'enabled_' ) ), + ], + ]; + } + $settings[ $policy ] = $setting; +} + +$output = [ 'settings {' ]; +foreach ( $settings as $name => $setting ) { + $output[] = "\t" . $name . ' {'; + $output[] = "\t\tid = " . json_encode( $setting[ 'id' ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ) . ';'; + $output[] = "\t\tpriority = " . $setting[ 'priority' ] . ';'; + $recipients = array_map( + static fn( string $recipient ): string => json_encode( $recipient, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ), + $setting[ 'rcpt' ] + ); + $output[] = "\t\trcpt = [" . implode( ', ', $recipients ) . '];'; + if ( isset( $setting[ 'want_spam' ] ) ) { + $output[] = "\t\twant_spam = yes;"; + } else { + $output[] = "\t\tapply {"; + $output[] = "\t\t\tactions {"; + $output[] = "\t\t\t\treject = " . $setting[ 'apply' ][ 'actions' ][ 'reject' ] . ';'; + $output[] = "\t\t\t}"; + $output[] = "\t\t}"; + } + $output[] = "\t}"; +} +$output[] = '}'; + +echo implode( "\n", $output ) . "\n" . CUSTOM_RSPAMD_SETTINGS . "\n";