Move sender checks to rspamd to allow receiving authenticated emails from hosted domains

This commit is contained in:
danwin1210 committed 2026-10-18 13:30:41 +02:00
1 parent 83414d5f44
commit 5cb35b52b4
6 files changed
+109 -9

No files matched your search

+2
View File
@@ -90,6 +90,8 @@ Mailbox owners can add identity addresses on their mailbox domain from account s
Registering a mailbox on `CLEARNET_SERVER` automatically creates its matching `local_part@ONION_SERVER` identity. The database upgrade backfills this identity for existing clearnet mailboxes.
Rspamd rejects unauthenticated inbound messages using a hosted envelope-sender domain unless SPF passes or DMARC passes for a hosted From domain. Configure the hosted-domain map URL and token in `/etc/rspamd/rspamd.local.lua` alongside the other Rspamd map settings.
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
@@ -65,7 +65,7 @@ smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_client_restrictions = reject_unauth_pipelining
smtpd_recipient_restrictions = check_policy_service inet:127.0.0.1:12340, permit_sasl_authenticated, check_recipient_access proxy:mysql:/etc/postfix/sql/mysql_tls_policy_in.cf
smtpd_sender_restrictions = reject_sender_login_mismatch, check_sender_access inline:{{<> = REJECT}}, check_policy_service { unix:private/last-login, timeout=5s, default_action=DUNNO }, permit_sasl_authenticated
smtpd_sender_restrictions = reject_authenticated_sender_login_mismatch, check_sender_access inline:{{<> = REJECT}}, check_policy_service { unix:private/last-login, timeout=5s, default_action=DUNNO }, permit_sasl_authenticated
smtpd_relay_restrictions = permit_sasl_authenticated, permit_auth_destination, reject_unauth_destination
smtpd_sender_login_maps = proxy:mysql:/etc/postfix/sql/mysql_virtual_auth_maps.cf
-2
View File
@@ -11,12 +11,10 @@
# ==========================================================================
smtp inet n - n - 2 smtpd
-o smtpd_sasl_auth_enable=no
-o { smtpd_sender_restrictions = check_sender_access inline:{double-bounce@mail.danwin1210.de=OK} check_sender_access mysql:/etc/postfix/sql/mysql_virtual_domain_unauth_block.cf }
last-login unix - n n - 0 spawn
user=www-data argv=/usr/bin/php /var/www/mail/tools/postfix_last_login_policy.php
26 inet n - n - 2 smtpd
-o smtpd_sasl_auth_enable=no
-o { smtpd_sender_restrictions = check_sender_access mysql:/etc/postfix/sql/mysql_virtual_domain_unauth_block.cf }
-o smtpd_tls_security_level=may
#smtp inet n - y - 1 postscreen
#smtpd pass - - y - - smtpd
@@ -1,6 +0,0 @@
user = postfix_readonly
password = YOUR_PASSWORD
hosts = localhost
dbname = postfix
query = SELECT 'REJECT' FROM domain WHERE domain='%d' AND active = '1'
+78
View File
@@ -20,6 +20,16 @@ if not feedback_ids then
error('cannot load Google Feedback Loop sender ID map')
end
local local_domains = rspamd_config:add_map{
type = 'map',
url = 'https://mail.danwin1210.de/rspamd_local_domains.php?token=CHANGE_ME_TO_A_LONG_RANDOM_SECRET',
description = 'Hosted mail domains',
}
if not local_domains then
error('cannot load hosted mail domains map')
end
rspamd_config:register_symbol{
name = 'GOOGLE_FEEDBACK_ID',
type = 'prefilter',
@@ -107,3 +117,71 @@ rspamd_config:register_symbol{
end
end,
}
rspamd_config:register_symbol{
name = 'LOCAL_DOMAIN_AUTH_POLICY',
type = 'postfilter',
priority = lua_util.symbols_priorities.high,
flags = 'empty,nostat',
score = 0.0,
callback = function(task)
if task:get_user() then
return
end
local spf_allowed = task:get_symbol('R_SPF_ALLOW') ~= nil
local dmarc_allowed = task:get_symbol('DMARC_POLICY_ALLOW') ~= nil
local dkim_allowed = task:get_symbol('R_DKIM_ALLOW') ~= nil
local dkim_aligned = task:get_symbol('R_DKIM_ALIGNED') ~= nil
local smtp_senders = task:get_from('smtp') or {}
local mime_senders = task:get_from('mime') or {}
local local_envelope_sender = false
local local_header_sender = false
local matching_senders = false
for _, sender in ipairs(mime_senders) do
local domain = sender.domain
if domain then
domain = domain:lower():gsub('%.$', '')
if local_domains:get_key(domain) then
local_header_sender = true
break
end
end
end
for _, sender in ipairs(smtp_senders) do
local domain = sender.domain
if domain then
domain = domain:lower():gsub('%.$', '')
if local_domains:get_key(domain) then
local_envelope_sender = true
break
end
end
end
for _, smtp_sender in ipairs(smtp_senders) do
for _, mime_sender in ipairs(mime_senders) do
if smtp_sender.domain and mime_sender.domain
and smtp_sender.domain:lower():gsub('%.$', '') == mime_sender.domain:lower():gsub('%.$', '') then
matching_senders = true
break
end
end
if matching_senders then
break
end
end
local authenticated_envelope = not local_envelope_sender or spf_allowed or ( ( dmarc_allowed or ( dkim_aligned and dkim_allowed ) ) and matching_senders )
local authenticated_header = not local_header_sender or dmarc_allowed or ( dkim_aligned and dkim_allowed ) or ( matching_senders and spf_allowed )
if not authenticated_envelope or not authenticated_header then
task:set_pre_result(
'reject',
'Messages using hosted sender domains require SPF, DKIM, or DMARC authentication',
'LOCAL_DOMAIN_AUTH_POLICY'
)
return
end
end,
}
+28
View File
@@ -0,0 +1,28 @@
<?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 domain FROM domain WHERE active = 1 ORDER BY domain;' );
$domains = [];
while ( $domain = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
$ascii_domain = idn_to_ascii( $domain[ 'domain' ], IDNA_NONTRANSITIONAL_TO_ASCII );
if ( ! is_string( $ascii_domain ) || ! preg_match( '/^[a-z0-9.-]+$/iD', $ascii_domain ) ) {
http_response_code( 500 );
error_log( 'Invalid local domain configured for Rspamd: ' . $domain[ 'domain' ] );
exit;
}
$domains[ mb_strtolower( $ascii_domain ) ] = true;
}
foreach ( array_keys( $domains ) as $domain ) {
echo $domain . "\t1\n";
}