Skip to content

Commit 536e907

Browse files
author
Marius Burkard
committed
- implemented per-domain-dkim, including custom selector
1 parent f5b9582 commit 536e907

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

install/lib/installer_base.lib.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,20 @@ public function configure_rspamd() {
14561456
}
14571457
}
14581458
exec("postconf -e 'smtpd_recipient_restrictions = ".implode(", ", $new_options)."'");
1459+
1460+
if ( substr($mail_config['dkim_path'], strlen($mail_config['dkim_path'])-1) == '/' ) {
1461+
$mail_config['dkim_path'] = substr($mail_config['dkim_path'], 0, strlen($mail_config['dkim_path'])-1);
1462+
}
1463+
$dkim_domains = $this->db->queryAllRecords('SELECT `dkim_selector`, `domain` FROM `mail_domain` WHERE `dkim` = ? ORDER BY `domain` ASC', 'y');
1464+
$fpp = fopen('/etc/rspamd/local.d/dkim_domains.map', 'w');
1465+
$fps = fopen('/etc/rspamd/local.d/dkim_selectors.map', 'w');
1466+
foreach($dkim_domains as $dkim_domain) {
1467+
fwrite($fpp, $dkim_domain['domain'] . ' ' . $mail_config['dkim_path'] . '/' . $dkim_domain['domain'] . '.private' . "\n");
1468+
fwrite($fps, $dkim_domain['domain'] . ' ' . $dkim_domain['dkim_selector']);
1469+
}
1470+
fclose($fpp);
1471+
fclose($fps);
1472+
unset($dkim_domains);
14591473
}
14601474

14611475
if(is_user('_rspamd') && is_group('amavis')) {
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
path = "<tmpl_var name='dkim_path'>/$domain.private";
2-
selector = "default";
1+
try_fallback = false;
2+
path_map = "/etc/rspamd/local.d/dkim_domains.map";
3+
selector_map = "/etc/rspamd/local.d/dkim_selectors.map";";

server/lib/classes/system.inc.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,14 @@ function replaceLine($filename, $search_pattern, $new_line, $strict = 0, $append
15351535
$found = 0;
15361536
if(is_array($lines)) {
15371537
foreach($lines as $line) {
1538-
if($strict == 0) {
1538+
if($strict == 0 && preg_match('/^REGEX:(.*)$/', $search_pattern)) {
1539+
if(preg_match(substr($search_pattern, 6), $line)) {
1540+
$out .= $new_line."\n";
1541+
$found = 1;
1542+
} else {
1543+
$out .= $line;
1544+
}
1545+
} elseif($strict == 0) {
15391546
if(stristr($line, $search_pattern)) {
15401547
$out .= $new_line."\n";
15411548
$found = 1;
@@ -1573,7 +1580,14 @@ function removeLine($filename, $search_pattern, $strict = 0) {
15731580
if($lines = @file($filename)) {
15741581
$out = '';
15751582
foreach($lines as $line) {
1576-
if($strict == 0) {
1583+
if($strict == 0 && preg_match('/^REGEX:(.*)$/', $search_pattern)) {
1584+
if(preg_match(substr($search_pattern, 6), $line)) {
1585+
$out .= $new_line."\n";
1586+
$found = 1;
1587+
} else {
1588+
$out .= $line;
1589+
}
1590+
} elseif($strict == 0) {
15771591
if(!stristr($line, $search_pattern)) {
15781592
$out .= $line;
15791593
}

server/plugins-available/mail_plugin_dkim.inc.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ private function add_dkim($data) {
347347
}
348348
if ($this->write_dkim_key($mail_config['dkim_path']."/".$data['new']['domain'], $data['new']['dkim_private'], $data['new']['domain'])) {
349349
if($mail_config['content_filter'] == 'rspamd') {
350+
$app->system->replaceLine('/etc/rspamd/local.d/dkim_domains.map', 'REGEX:/^' . preg_quote($data['new']['domain'], '/') . ' /', $data['new']['domain'] . ' ' . $mail_config['dkim_path']."/".$data['new']['domain'] . '.private');
351+
$app->system->replaceLine('/etc/rspamd/local.d/dkim_selectors.map', 'REGEX:/^' . preg_quote($data['new']['domain'], '/') . ' /', $data['new']['domain'] . ' ' . $data['new']['dkim_selector']);
352+
350353
$app->services->restartServiceDelayed('rspamd', 'reload');
351354
} elseif ($this->add_to_amavis($data['new']['domain'], $data['new']['dkim_selector'], $data['old']['dkim_selector'] )) {
352355
$this->restart_amavis();
@@ -373,6 +376,8 @@ private function remove_dkim($_data) {
373376
$this->remove_dkim_key($mail_config['dkim_path']."/".$_data['domain'], $_data['domain']);
374377

375378
if($mail_config['content_filter'] == 'rspamd') {
379+
$app->system->removeLine('/etc/rspamd/local.d/dkim_domains.map', 'REGEX:/^' . preg_quote($_data['domain'], '/') . ' /');
380+
$app->system->removeLine('/etc/rspamd/local.d/dkim_selectors.map', 'REGEX:/^' . preg_quote($_data['domain'], '/') . ' /');
376381
$app->services->restartServiceDelayed('rspamd', 'reload');
377382
} elseif ($this->remove_from_amavis($_data['domain'])) {
378383
$this->restart_amavis();

server/plugins-available/postfix_server_plugin.inc.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,21 @@ function update($event_name, $data) {
200200
}
201201
}
202202
exec("postconf -e 'smtpd_recipient_restrictions = ".implode(", ", $new_options)."'");
203+
204+
// get all domains that have dkim enabled
205+
if ( substr($mail_config['dkim_path'], strlen($mail_config['dkim_path'])-1) == '/' ) {
206+
$mail_config['dkim_path'] = substr($mail_config['dkim_path'], 0, strlen($mail_config['dkim_path'])-1);
207+
}
208+
$dkim_domains = $app->db->queryAllRecords('SELECT `dkim_selector`, `domain` FROM `mail_domain` WHERE `dkim` = ? ORDER BY `domain` ASC', 'y');
209+
$fpp = fopen('/etc/rspamd/local.d/dkim_domains.map', 'w');
210+
$fps = fopen('/etc/rspamd/local.d/dkim_selectors.map', 'w');
211+
foreach($dkim_domains as $dkim_domain) {
212+
fwrite($fpp, $dkim_domain['domain'] . ' ' . $mail_config['dkim_path'] . '/' . $dkim_domain['domain'] . '.private' . "\n");
213+
fwrite($fps, $dkim_domain['domain'] . ' ' . $dkim_domain['dkim_selector']);
214+
}
215+
fclose($fpp);
216+
fclose($fps);
217+
unset($dkim_domains);
203218
}
204219
if($mail_config['content_filter'] == 'amavisd'){
205220
exec("postconf -X 'smtpd_milters'");

0 commit comments

Comments
 (0)