Implement dynamic rspamd map

This commit is contained in:
danwin1210 committed 2026-10-12 13:10:34 +02:00
1 parent b0790a5345
commit bdbe50e9e0
7 files changed
+141 -13

No files matched your search

+2
View File
@@ -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
+11 -1
View File
@@ -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
+2
View File
@@ -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";
+7 -1
View File
@@ -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]);
+18 -5
View File
@@ -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 .= '<div class="red" role="alert">'.htmlspecialchars(_('Please select a valid inactivity period.')).'</div>';
$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 .= '<div class="red" role="alert">'.htmlspecialchars(_('Please select valid mailbox settings.')).'</div>';
} 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 .= '<div class="green" role="status">'.htmlspecialchars(_('Successfully updated mailbox.')).'</div>';
}
}
@@ -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
</select>
</div>
</div>
<div class="row">
<div class="col"><label for="spam_reject_score"><?php echo htmlspecialchars(_('Spam reject score for incoming mail')); ?></label></div>
<div class="col">
<select name="spam_reject_score" id="spam_reject_score">
<?php foreach ( SPAM_REJECT_SCORE_OPTIONS as $score ) { ?>
<option value="<?php echo $score; ?>"<?php echo (int) $email[ 'spam_reject_score' ] === $score ? ' selected' : ''; ?>><?php echo $score === 0 ? htmlspecialchars(_('Disabled')) : sprintf(htmlspecialchars(_('%d points')), $score); ?></option>
<?php } ?>
</select>
</div>
</div>
<p><?php echo htmlspecialchars(_('Disabling spam filtering by setting the score to 0 bypasses all spam checks for incoming mail. The higher the score, the more spam you may receive.')); ?></p>
<div class="row">
<div class="col">
<button name="action" value="save_edit_mailbox" type="submit"><?php echo htmlspecialchars(_('Save mailbox')); ?></button>
+32 -6
View File
@@ -133,6 +133,15 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
$stmt->execute( [ $inactivity_period, $_SESSION[ 'email_user' ] ] );
$msg .= '<div class="green" role="status">'.htmlspecialchars(_('Successfully updated the inactivity period.')).'</div>';
}
} 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 .= '<div class="red" role="alert">'.htmlspecialchars(_('Please select a valid spam reject score.')).'</div>';
} else {
$stmt = $db->prepare( 'UPDATE mailbox SET spam_reject_score = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ $spam_reject_score, $_SESSION[ 'email_user' ] ] );
$msg .= '<div class="green" role="status">'.htmlspecialchars(_('Successfully updated spam filter settings, changes can take up to 10 minutes to take effect.')).'</div>';
}
} 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 .= '<div class="red" role="alert">'.htmlspecialchars(_('Passwords empty or don\'t match')).'</div>';
@@ -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 );
?>
<form class="form_limit" action="manage_account.php" method="post">
<h2><?php echo htmlspecialchars(_('Settings')); ?></h2>
@@ -400,13 +409,13 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
<div class="row">
<div class="col"><label for="enforce_tls_in"><?php echo htmlspecialchars(_('Enforce encryption for incoming mail')); ?></label></div>
<div class="col"><input type="checkbox" name="enforce_tls_in"
id="enforce_tls_in"<?php echo ! empty( $tls_status[ 'enforce_tls_in' ] ) ? ' checked' : ''; ?>>
id="enforce_tls_in"<?php echo ! empty( $mailbox_options[ 'enforce_tls_in' ] ) ? ' checked' : ''; ?>>
</div>
</div>
<div class="row">
<div class="col"><label for="enforce_tls_out"><?php echo htmlspecialchars(_('Enforce encryption for outgoing mail')); ?></label></div>
<div class="col"><input type="checkbox" name="enforce_tls_out"
id="enforce_tls_out"<?php echo ! empty( $tls_status[ 'enforce_tls_out' ] ) ? ' checked' : ''; ?>>
id="enforce_tls_out"<?php echo ! empty( $mailbox_options[ 'enforce_tls_out' ] ) ? ' checked' : ''; ?>>
</div>
</div>
<div class="row">
@@ -416,6 +425,23 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
</div>
</form>
<form class="form_limit" action="manage_account.php" method="post">
<h3><?php echo htmlspecialchars(_('Spam filtering')); ?></h3>
<p><?php echo htmlspecialchars(_('Disabling spam filtering by setting the score to 0 bypasses all spam checks for incoming mail. The higher the score, the more spam you may receive.')); ?></p>
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION[ 'csrf_token' ]; ?>">
<div class="row">
<div class="col"><label for="spam_reject_score"><?php echo htmlspecialchars(_('Spam reject score for incoming mail')); ?></label></div>
<div class="col">
<select name="spam_reject_score" id="spam_reject_score">
<?php foreach ( SPAM_REJECT_SCORE_OPTIONS as $score ) { ?>
<option value="<?php echo $score; ?>"<?php echo (int) $mailbox_options[ 'spam_reject_score' ] === $score ? ' selected' : ''; ?>><?php echo $score === 0 ? htmlspecialchars(_('Disabled')) : sprintf(htmlspecialchars(_('%d points')), $score); ?></option>
<?php } ?>
</select>
</div>
</div>
<button name="action" value="update_spam_settings" type="submit"><?php echo htmlspecialchars(_('Update spam filter settings')); ?></button>
</form>
<form class="form_limit" action="manage_account.php" method="post">
<h3><?php echo htmlspecialchars(_('Account inactivity')); ?></h3>
<p><?php echo htmlspecialchars(_('Choose how long your account can remain inactive before it and its data are automatically deleted. The period is measured from your last login, or from account creation if you have never logged in.')); ?></p>
@@ -425,10 +451,10 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
<div class="col">
<select name="inactivity_period" id="inactivity_period">
<?php if ( ALLOW_ACCOUNT_NEVER_DELETE ) { ?>
<option value="0"<?php echo (int) $tls_status[ 'inactivity_period' ] === 0 ? ' selected' : ''; ?>><?php echo htmlspecialchars(_('Never')); ?></option>
<option value="0"<?php echo (int) $mailbox_options[ 'inactivity_period' ] === 0 ? ' selected' : ''; ?>><?php echo htmlspecialchars(_('Never')); ?></option>
<?php } ?>
<?php foreach ( get_inactivity_period_options() as $months => $label ) { ?>
<option value="<?php echo $months; ?>"<?php echo (int) $tls_status[ 'inactivity_period' ] === $months ? ' selected' : ''; ?>><?php echo htmlspecialchars($label); ?></option>
<option value="<?php echo $months; ?>"<?php echo (int) $mailbox_options[ 'inactivity_period' ] === $months ? ' selected' : ''; ?>><?php echo htmlspecialchars($label); ?></option>
<?php } ?>
</select>
</div>
+69
View File
@@ -0,0 +1,69 @@
<?php
require_once( '../common_config.php' );
header( 'Content-Type: text/plain; charset=utf-8' );
header( 'Cache-Control: no-store' );
header( 'Expires: 600' );
$token = $_GET[ 'token' ] ?? '';
if ( $_SERVER[ 'REQUEST_METHOD' ] !== 'GET' || ! hash_equals( RSPAMD_SETTINGS_MAP_TOKEN, $token ) ) {
http_response_code( 403 );
exit;
}
$db = get_db_instance();
$stmt = $db->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";