Merge branch 'feat/mail-policy' into nightly

This commit is contained in:
FreddleSpl0it committed 2026-11-09 03:16:30 +01:00
commit 2afb03dd6a
38 files changed
+3161 -45

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
<?php
// Mail policy check for one recipient, asked by the Postfix policy service (mailcow-policy in master.cf)
// Request: POST with the attributes of the Postfix policy request; answer: the action (DUNNO or the rejection)
header('Content-Type: text/plain');
require_once "vars.inc.php";
require_once __DIR__ . '/../web/inc/lib/vendor/autoload.php';
// Do not show errors, we log to using error_log
ini_set('error_reporting', 0);
// Init database
$dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $database_user, $database_pass, $opt);
}
catch (PDOException $e) {
error_log("MAILPOLICY: " . $e . PHP_EOL);
http_response_code(501);
exit;
}
if (!isset($_POST['request']) || $_POST['request'] !== 'smtpd_access_policy') {
http_response_code(400);
exit;
}
$value = fn(string $key): string => is_string($_POST[$key] ?? null) ? $_POST[$key] : '';
try {
$policy = \Mailcow\MailPolicy\MailPolicyModule::create($pdo, \Mailcow\Core\Context::system());
echo $policy->api->check($value('sender'), $value('recipient'), $value('sasl_username'), (int)$value('server_port'), $value('helo_name'));
}
catch (Throwable $e) {
// the policy service answers with a temporary error
error_log("MAILPOLICY: " . $e->getMessage() . PHP_EOL);
http_response_code(500);
}
-30
View File
@@ -469,35 +469,5 @@ while ($row = array_shift($rows)) {
}
?>
<?php
// Start internal aliases
$stmt = $pdo->query("SELECT `id`, `address`, `domain` FROM `alias` WHERE `active` = '1' AND `internal` = '1'");
$aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
while ($alias = array_shift($aliases)) {
// build allowed_domains regex and add target domain and alias domains
$stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `active` = '1' AND `target_domain` = :target_domain");
$stmt->execute(array(':target_domain' => $alias['domain']));
$allowed_domains = $stmt->fetchAll(PDO::FETCH_ASSOC);
$allowed_domains = array_map(function($item) {
return str_replace('.', '\.', $item['alias_domain']);
}, $allowed_domains);
$allowed_domains[] = str_replace('.', '\.', $alias['domain']);
$allowed_domains = implode('|', $allowed_domains);
?>
internal_alias_<?=$alias['id'];?> {
priority = 10;
rcpt = "<?=$alias['address'];?>";
from = "/^((?!.*@(<?=$allowed_domains;?>)).)*$/";
apply "default" {
MAILCOW_INTERNAL_ALIAS = 9999.0;
}
symbols [
"MAILCOW_INTERNAL_ALIAS"
]
}
<?php
}
?>
}