From afc8c93c07ecfa60723b7ad0918fe8707919f9cd Mon Sep 17 00:00:00 2001 From: andryyy Date: Sat, 1 Jul 2017 23:14:27 +0200 Subject: [PATCH] [Rspamd] Cleanup settings map --- data/conf/rspamd/dynmaps/settings.php | 269 +++++++++----------------- 1 file changed, 93 insertions(+), 176 deletions(-) diff --git a/data/conf/rspamd/dynmaps/settings.php b/data/conf/rspamd/dynmaps/settings.php index 3976954c..c05eeb0e 100644 --- a/data/conf/rspamd/dynmaps/settings.php +++ b/data/conf/rspamd/dynmaps/settings.php @@ -4,15 +4,10 @@ The match section performs AND operation on different matches: for example, if y then the rule matches only when from AND rcpt match. For similar matches, the OR rule applies: if you have multiple rcpt matches, then any of these will trigger the rule. If a rule is triggered then no more rules are matched. */ -function parse_email($email) { - if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false; - $a = strrpos($email, '@'); - return array('local' => substr($email, 0, $a), 'domain' => substr($email, $a)); -} header('Content-Type: text/plain'); require_once "vars.inc.php"; -ini_set('error_reporting', 0); +ini_set('error_reporting', 1); $dsn = $database_type . ':host=' . $database_host . ';dbname=' . $database_name; $opt = [ @@ -29,6 +24,77 @@ catch (PDOException $e) { exit; } +function parse_email($email) { + if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false; + $a = strrpos($email, '@'); + return array('local' => substr($email, 0, $a), 'domain' => substr($email, $a)); +} + +function ucl_rcpts($object, $type) { + global $pdo; + if ($type == 'mailbox') { + // Standard aliases + $stmt = $pdo->prepare("SELECT `address` FROM `alias` + WHERE `goto` LIKE :object_goto + AND `address` NOT LIKE '@%' + AND `address` != :object_address"); + $stmt->execute(array( + ':object_goto' => '%' . $object . '%', + ':object_address' => $object + )); + $standard_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC); + while ($row = array_shift($standard_aliases)) { + $local = parse_email($row['address'])['local']; + $domain = parse_email($row['address'])['domain']; + if (!empty($local) && !empty($domain)) { + $rcpt[] = '/' . $local . '\+.*' . $domain . '/'; + } + $rcpt[] = $row['address']; + } + // Aliases by alias domains + $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias` FROM `mailbox` + LEFT OUTER JOIN `alias_domain` ON `mailbox`.`domain` = `alias_domain`.`target_domain` + WHERE `mailbox`.`username` = :object"); + $stmt->execute(array( + ':object' => $object + )); + $by_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC); + array_filter($by_domain_aliases); + while ($row = array_shift($by_domain_aliases)) { + if (!empty($row['alias'])) { + $local = parse_email($row['alias'])['local']; + $domain = parse_email($row['alias'])['domain']; + if (!empty($local) && !empty($domain)) { + $rcpt[] = '/' . $local . '\+.*' . $domain . '/'; + } + $rcpt[] = $row['alias']; + } + } + // Mailbox self + $local = parse_email($row['object'])['local']; + $domain = parse_email($row['object'])['domain']; + if (!empty($local) && !empty($domain)) { + $rcpt[] = '/' . $local . '\+.*' . $domain . '/'; + } + $rcpt[] = $object; + } + elseif ($type == 'domain') { + // Domain self + $rcpt[] = '/.*@' . $object . '/'; + $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` + WHERE `target_domain` = :object"); + $stmt->execute(array(':object' => $row['object'])); + $alias_domains = $stmt->fetchAll(PDO::FETCH_ASSOC); + array_filter($alias_domains); + while ($row = array_shift($alias_domains)) { + $rcpt[] = '/.*@' . $row['alias_domain'] . '/'; + } + } + if (!empty($rcpt)) { + return $rcpt; + } + return false; +} ?> settings { score_ { - priority = low; + priority = 4; + rcpt = ""; +prepare("SELECT `option`, `value` FROM `filterconf` WHERE (`option` = 'highspamlevel' OR `option` = 'lowspamlevel') AND `object`= :object"); $stmt->execute(array(':object' => $row['object'])); $spamscore = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP); - - $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf` - WHERE (`object`= :object OR `object`= :object_domain) - AND (`option` = 'blacklist_from' OR `option` = 'whitelist_from')"); - $stmt->execute(array(':object' => $row['object'], ':object_domain' => substr(strrchr($row['object'], "@"), 1))); - $grouped_lists = $stmt->fetchAll(PDO::FETCH_ASSOC); - array_filter($grouped_lists); - while ($grouped_list = array_shift($grouped_lists)) { - $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_list['value']))); - if (!empty($value_sane)) { -?> - from = "/^((?!).)*$/"; - - rcpt = "/\+.*/"; - - rcpt = ""; -prepare("SELECT `address` FROM `alias` WHERE `goto` LIKE :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address"); - $stmt->execute(array(':object_goto' => '%' . $row['object'] . '%', ':object_address' => $row['object'])); - $rows_aliases_1 = $stmt->fetchAll(PDO::FETCH_ASSOC); - while ($row_aliases_1 = array_shift($rows_aliases_1)) { - $local = parse_email($row_aliases_1['address'])['local']; - $domain = parse_email($row_aliases_1['address'])['domain']; - if (!empty($local) && !empty($local)) { -?> - rcpt = "/\+.*/"; - - rcpt = ""; -prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox` - LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain` - WHERE `mailbox`.`username` = :object"); - $stmt->execute(array(':object' => $row['object'])); - $rows_aliases_2 = $stmt->fetchAll(PDO::FETCH_ASSOC); - array_filter($rows_aliases_2); - while ($row_aliases_2 = array_shift($rows_aliases_2)) { - if (!empty($row_aliases_2['aliases'])) { - $local = parse_email($row_aliases_2['aliases'])['local']; - $domain = parse_email($row_aliases_2['aliases'])['domain']; - if (!empty($local) && !empty($local)) { -?> - rcpt = "/\+.*/"; - - rcpt = ""; - apply "default" { actions { @@ -145,70 +156,23 @@ while ($row = array_shift($rows)) { - priority = medium; - rcpt = "/.*@/"; + priority = 5; prepare("SELECT `alias_domain` FROM `alias_domain` - WHERE `target_domain` = :object"); - $stmt->execute(array(':object' => $row['object'])); - $rows_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC); - array_filter($rows_domain_aliases); - while ($row_domain_aliases = array_shift($rows_domain_aliases)) { + foreach (ucl_rcpts($row['object'], 'mailbox') as $rcpt) { ?> - rcpt = "/.*@/"; + rcpt = ""; - priority = high; + priority = 6; - rcpt = "/\+.*/"; + rcpt = ""; - rcpt = ""; -prepare("SELECT `address` FROM `alias` WHERE `goto` LIKE :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address"); - $stmt->execute(array(':object_goto' => '%' . $row['object'] . '%', ':object_address' => $row['object'])); - $rows_aliases_wl_1 = $stmt->fetchAll(PDO::FETCH_ASSOC); - array_filter($rows_aliases_wl_1); - while ($row_aliases_wl_1 = array_shift($rows_aliases_wl_1)) { - $local = parse_email($row_aliases_wl_1['address'])['local']; - $domain = parse_email($row_aliases_wl_1['address'])['domain']; - if (!empty($local) && !empty($local)) { -?> - rcpt = "/\+.*/"; - - rcpt = ""; -prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox` - LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain` - WHERE `mailbox`.`username` = :object"); - $stmt->execute(array(':object' => $row['object'])); - $rows_aliases_wl_2 = $stmt->fetchAll(PDO::FETCH_ASSOC); - array_filter($rows_aliases_wl_2); - while ($row_aliases_wl_2 = array_shift($rows_aliases_wl_2)) { - if (!empty($row_aliases_wl_2['aliases'])) { - $local = parse_email($row_aliases_wl_2['aliases'])['local']; - $domain = parse_email($row_aliases_wl_2['aliases'])['domain']; - if (!empty($local) && !empty($local)) { -?> - rcpt = "/\+.*/"; - - rcpt = ""; - apply "default" { @@ -243,70 +207,23 @@ while ($row = array_shift($rows)) { - priority = medium; - rcpt = "/.*@/"; + priority = 5; prepare("SELECT `alias_domain` FROM `alias_domain` - WHERE `target_domain` = :object"); - $stmt->execute(array(':object' => $row['object'])); - $rows_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC); - array_filter($rows_domain_aliases); - while ($row_domain_aliases = array_shift($rows_domain_aliases)) { + foreach (ucl_rcpts($row['object'], 'mailbox') as $rcpt) { ?> - rcpt = "/.*@/"; + rcpt = ""; - priority = high; + priority = 6; - rcpt = "/\+.*/"; + rcpt = ""; - rcpt = ""; -prepare("SELECT `address` FROM `alias` WHERE `goto` LIKE :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address"); - $stmt->execute(array(':object_goto' => '%' . $row['object'] . '%', ':object_address' => $row['object'])); - $rows_aliases_bl_1 = $stmt->fetchAll(PDO::FETCH_ASSOC); - array_filter($rows_aliases_bl_1); - while ($row_aliases_bl_1 = array_shift($rows_aliases_bl_1)) { - $local = parse_email($row_aliases_bl_1['address'])['local']; - $domain = parse_email($row_aliases_bl_1['address'])['domain']; - if (!empty($local) && !empty($local)) { -?> - rcpt = "/\+.*/"; - - rcpt = ""; -prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox` - LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain` - WHERE `mailbox`.`username` = :object"); - $stmt->execute(array(':object' => $row['object'])); - $rows_aliases_bl_2 = $stmt->fetchAll(PDO::FETCH_ASSOC); - array_filter($rows_aliases_bl_2); - while ($row_aliases_bl_2 = array_shift($rows_aliases_bl_2)) { - if (!empty($row_aliases_bl_2['aliases'])) { - $local = parse_email($row_aliases_bl_2['aliases'])['local']; - $domain = parse_email($row_aliases_bl_2['aliases'])['domain']; - if (!empty($local) && !empty($local)) { -?> - rcpt = "/\+.*/"; - - rcpt = ""; - apply "default" { @@ -319,4 +236,4 @@ while ($row = array_shift($rows)) { -} \ No newline at end of file +}