Implement reputation based sending limits

This commit is contained in:
danwin1210 committed 2026-10-17 22:20:06 +02:00
1 parent 304a48b231
commit df186e33d7
7 files changed
+94 -19

No files matched your search

+2
View File
@@ -82,6 +82,8 @@ Google FBL reporting requires mail to be DKIM-signed by a domain verified in Goo
Configure DKIM signing maps by replacing the example hostname and token in `/etc/rspamd/local.d/arc.conf` and `/etc/rspamd/local.d/dkim_signing.conf` with the same hostname and token used for the other Rspamd maps. An RSA DKIM key is generated automatically when a domain is added. The domain editor shows the selector and public DNS TXT record to publish; regenerating the key changes the selector and requires updating that record. Private keys are stored in the database and should be protected accordingly.
The authenticated-user ratelimit map is configured in `/etc/rspamd/custom_ratelimit.lua`; replace its example hostname and token with your map endpoint settings. Accounts registered within the last 14 days receive lower sending limits. A mailbox marked as trusted in the admin editor receives higher limits; other authenticated mailboxes receive the default limits.
Create a password used for your TURN server and replace all `YOUR_SECRET` in `/etc/prosody/prosody.cfg.lua` with it.
Install [acme.sh](https://github.com/acmesh-official/acme.sh) or [certbot](https://certbot.eff.org/) to obtain a free letsencrypt SSL certificate, then update the path to this new certificate in the following files:
+1 -1
View File
@@ -9,7 +9,7 @@ const DBHOST = 'localhost'; // Database host
const DBUSER = 'postfix'; // Database user
const DBPASS = 'YOUR_PASSWORD'; // Database password
const DBNAME = 'postfix'; // Database
const DBVERSION = 10; // Database schema version
const DBVERSION = 11; // 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"
+51
View File
@@ -0,0 +1,51 @@
local custom_keywords = {}
local ratelimit_users = rspamd_config:add_map({
type = 'map',
url = 'https://mail.danwin1210.de/rspamd_ratelimits.php?token=CHANGE_ME_TO_A_LONG_RANDOM_SECRET',
description = 'Ratelimits for authenticated users',
})
if not ratelimit_users then
error('cannot load authenticated user ratelimit map')
end
local default_limits = {
per_minute = { burst = 20, rate = '2 / 1m' },
per_three_hours = { burst = 400, rate = '50 / 3h' },
}
local new_user_limits = {
per_minute = { burst = 10, rate = '1 / 1m' },
per_three_hours = { burst = 100, rate = '20 / 3h' },
}
local trusted_user_limits = {
per_minute = { burst = 50, rate = '10 / 1m' },
per_three_hours = { burst = 1000, rate = '100 / 3h' },
}
local function get_user_limit(task, limit_period)
local user = task:get_user()
if not user then
return nil
end
local normalized_user = user:lower()
local classification = ratelimit_users:get_key(normalized_user)
local limits = default_limits
if classification == 'NEW' then
limits = new_user_limits
elseif classification == 'TRUSTED' then
limits = trusted_user_limits
end
return normalized_user .. '_sending_limits_' .. limit_period, limits[limit_period]
end
custom_keywords.sending_limits_per_minute = function(task)
return get_user_limit(task, 'per_minute')
end
custom_keywords.sending_limits_per_three_hours = function(task)
return get_user_limit(task, 'per_three_hours')
end
return custom_keywords
+1 -14
View File
@@ -1,5 +1,6 @@
whitelisted_rcpts = ["postmaster", "mailer-daemon", "daniel@danwin1210.de"]
whitelisted_user = ["daniel@danwin1210.de"]
custom_keywords = "/etc/rspamd/custom_ratelimit.lua";
rates {
to = {
bucket = {
@@ -7,18 +8,4 @@ rates {
rate = 1 / 1m;
}
}
sending_limit_2_per_min {
selector = 'user.lower.append("sending_limit_2_per_min")';
bucket = {
burst = 20;
rate = 2 / 1m;
}
}
sending_limit_500_per_day {
selector = 'user.lower.append("sending_limit_500_per_day")';
bucket = {
burst = 400;
rate = 50 / 3h;
}
}
}
+13 -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, `spam_reject_score` tinyint unsigned NOT NULL DEFAULT 15, `blocked_senders` text NOT NULL DEFAULT '', `whitelisted_senders` text NOT NULL DEFAULT '', `feedback_id` char(15) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), UNIQUE KEY `feedback_id` (`feedback_id`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), KEY `inactivity_period` (`inactivity_period`), KEY `spam_reject_score` (`spam_reject_score`), 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, `blocked_senders` text NOT NULL DEFAULT '', `whitelisted_senders` text NOT NULL DEFAULT '', `feedback_id` char(15) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, `trusted` tinyint(1) NOT NULL DEFAULT 0, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), UNIQUE KEY `feedback_id` (`feedback_id`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), KEY `inactivity_period` (`inactivity_period`), KEY `spam_reject_score` (`spam_reject_score`), KEY `created` (`created`), KEY `trusted` (`trusted`), 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;",
@@ -126,6 +126,18 @@ try{
$update_dkim->execute( [ $dkim[ 'key' ], $dkim[ 'selector' ], $domain[ 'domain' ] ] );
}
}
if ( $version < 11 ) {
$stmt = $db->query( "SHOW COLUMNS FROM `mailbox` LIKE 'trusted';" );
if ( ! $stmt->fetch() ) {
$db->exec( 'ALTER TABLE `mailbox` ADD COLUMN `trusted` tinyint(1) NOT NULL DEFAULT 0;' );
}
foreach ( [ 'created', 'trusted' ] as $indexName ) {
$stmt = $db->query( "SHOW INDEX FROM `mailbox` WHERE `Key_name` = '$indexName';" );
if ( ! $stmt->fetch() ) {
$db->exec( "ALTER TABLE `mailbox` ADD INDEX `$indexName` (`$indexName`);" );
}
}
}
$db->beginTransaction();
$stmt=$db->prepare("UPDATE `settings` SET `value`=? WHERE `setting`='version';");
$stmt->execute([DBVERSION]);
+8 -3
View File
@@ -393,8 +393,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 = ?, spam_reject_score = ?, blocked_senders = ?, whitelisted_senders = ?, 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, implode( "\n", $blocked_senders ), implode( "\n", $whitelisted_senders ), $_POST[ 'user' ] ] );
$stmt = $db->prepare( 'UPDATE mailbox SET enforce_tls_in = ?, enforce_tls_out = ?, active = ?, trusted = ?, quota = ?, inactivity_period = ?, spam_reject_score = ?, blocked_senders = ?, whitelisted_senders = ?, 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 ), ( isset( $_POST[ 'trusted' ] ) ? 1 : 0 ), DEFAULT_QUOTA, $inactivity_period, $spam_reject_score, implode( "\n", $blocked_senders ), implode( "\n", $whitelisted_senders ), $_POST[ 'user' ] ] );
$msg .= '<div class="green" role="status">'.htmlspecialchars(_('Successfully updated mailbox.')).'</div>';
}
}
@@ -1229,7 +1229,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, m.spam_reject_score, m.blocked_senders, m.whitelisted_senders, m.feedback_id 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.trusted, m.enforce_tls_in, m.enforce_tls_out, m.inactivity_period, m.spam_reject_score, m.blocked_senders, m.whitelisted_senders, m.feedback_id 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' ] );
@@ -1256,6 +1256,11 @@ function send_edit_mailbox(): void
value="1"<?php echo $email[ 'active' ] === 1 ? ' checked' : ''; ?>><?php echo htmlspecialchars(_('Active')); ?></label>
</div>
</div>
<div class="row">
<div class="col"><label><input type="checkbox" name="trusted"
value="1"<?php echo (int) $email[ 'trusted' ] === 1 ? ' checked' : ''; ?>><?php echo htmlspecialchars(_('Trusted sender (higher sending limits)')); ?></label>
</div>
</div>
<div class="row">
<div class="col"><label><input type="checkbox" name="enforce_tls_in"
value="1"<?php echo $email[ 'enforce_tls_in' ] === 1 ? ' checked' : ''; ?>><?php echo htmlspecialchars(_('Enforce encryption for incoming mail')); ?></label>
+18
View File
@@ -0,0 +1,18 @@
<?php
require_once( '../common_config.php' );
header( 'Content-Type: text/plain; charset=utf-8' );
header( 'Cache-Control: no-store' );
header( 'Expires: 600' );
$token = is_string( $_GET[ 'token' ] ?? null ) ? $_GET[ 'token' ] : '';
if ( ! in_array( $_SERVER[ 'REQUEST_METHOD' ], [ 'GET', 'HEAD' ], true ) || ! hash_equals( RSPAMD_SETTINGS_MAP_TOKEN, $token ) ) {
http_response_code( 403 );
exit;
}
$stmt = get_db_instance()->query( "SELECT username, trusted FROM mailbox WHERE active = 1 AND (trusted = 1 OR created >= DATE_SUB(NOW(), INTERVAL 14 DAY)) ORDER BY username;" );
while ( $mailbox = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
$classification = (int) $mailbox[ 'trusted' ] === 1 ? 'TRUSTED' : 'NEW';
echo mb_strtolower( $mailbox[ 'username' ] ) . "\t" . $classification . "\n";
}