123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- <?php
- /*
- The match section performs AND operation on different matches: for example, if you have from and rcpt in the same rule,
- 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.
- */
- header('Content-Type: text/plain');
- require_once "vars.inc.php";
- // Getting headers sent by the client.
- ini_set('error_reporting', 0);
- //$dsn = $database_type . ':host=' . $database_host . ';dbname=' . $database_name;
- $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);
- $stmt = $pdo->query("SELECT '1' FROM `filterconf`");
- }
- catch (PDOException $e) {
- echo 'settings { }';
- exit;
- }
- // Check if db changed and return header
- $stmt = $pdo->prepare("SELECT GREATEST(COALESCE(MAX(UNIX_TIMESTAMP(UPDATE_TIME)), 1), COALESCE(MAX(UNIX_TIMESTAMP(CREATE_TIME)), 1)) AS `db_update_time` FROM `information_schema`.`tables`
- WHERE (`TABLE_NAME` = 'filterconf' OR `TABLE_NAME` = 'settingsmap' OR `TABLE_NAME` = 'sogo_quick_contact' OR `TABLE_NAME` = 'alias')
- AND TABLE_SCHEMA = :dbname;");
- $stmt->execute(array(
- ':dbname' => $database_name
- ));
- $db_update_time = $stmt->fetch(PDO::FETCH_ASSOC)['db_update_time'];
- if (empty($db_update_time)) {
- $db_update_time = 1572048000;
- }
- if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $db_update_time)) {
- header('Last-Modified: '.gmdate('D, d M Y H:i:s', $db_update_time).' GMT', true, 304);
- exit;
- } else {
- header('Last-Modified: '.gmdate('D, d M Y H:i:s', $db_update_time).' GMT', true, 200);
- }
- 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 normalize_email($email) {
- $email = strtolower(str_replace('/', '\/', $email));
- $gm = "@gmail.com";
- if (substr_compare($email, $gm, -strlen($gm)) == 0) {
- $email = explode('@', $email);
- $email[0] = str_replace('.', '', $email[0]);
- $email = implode('@', $email);
- }
- $gm_alt = "@googlemail.com";
- if (substr_compare($email, $gm_alt, -strlen($gm_alt)) == 0) {
- $email = explode('@', $email);
- $email[0] = str_replace('.', '', $email[0]);
- $email[1] = str_replace('@', '', $gm);
- $email = implode('@', $email);
- }
- if (str_contains($email, "+")) {
- $email = explode('@', $email);
- $user = explode('+', $email[0]);
- $email[0] = $user[0];
- $email = implode('@', $email);
- }
- return $email;
- }
- function wl_by_sogo() {
- global $pdo;
- $rcpt = array();
- $stmt = $pdo->query("SELECT DISTINCT(`sogo_folder_info`.`c_path2`) AS `user`, GROUP_CONCAT(`sogo_quick_contact`.`c_mail`) AS `contacts` FROM `sogo_folder_info`
- INNER JOIN `sogo_quick_contact` ON `sogo_quick_contact`.`c_folder_id` = `sogo_folder_info`.`c_folder_id`
- GROUP BY `c_path2`");
- $sogo_contacts = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($sogo_contacts)) {
- foreach (explode(',', $row['contacts']) as $contact) {
- if (!filter_var($contact, FILTER_VALIDATE_EMAIL)) {
- continue;
- }
- // Explicit from, no mime_from, no regex - envelope must match
- // mailcow white and blacklists also cover mime_from
- $rcpt[$row['user']][] = normalize_email($contact);
- }
- }
- return $rcpt;
- }
- function ucl_rcpts($object, $type) {
- global $pdo;
- $rcpt = array();
- if ($type == 'mailbox') {
- // Standard aliases
- $stmt = $pdo->prepare("SELECT `address` FROM `alias`
- WHERE `goto` = :object_goto
- AND `address` NOT LIKE '@%'");
- $stmt->execute(array(
- ':object_goto' => $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[] = '/^' . str_replace('/', '\/', $local) . '[+].*' . str_replace('/', '\/', $domain) . '$/i';
- }
- $rcpt[] = str_replace('/', '\/', $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[] = '/^' . str_replace('/', '\/', $local) . '[+].*' . str_replace('/', '\/', $domain) . '$/i';
- }
- $rcpt[] = str_replace('/', '\/', $row['alias']);
- }
- }
- }
- elseif ($type == 'domain') {
- // Domain self
- $rcpt[] = '/.*@' . $object . '/i';
- $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
- WHERE `target_domain` = :object");
- $stmt->execute(array(':object' => $object));
- $alias_domains = $stmt->fetchAll(PDO::FETCH_ASSOC);
- array_filter($alias_domains);
- while ($row = array_shift($alias_domains)) {
- $rcpt[] = '/.*@' . $row['alias_domain'] . '/i';
- }
- }
- return $rcpt;
- }
- ?>
- settings {
- watchdog {
- priority = 10;
- rcpt_mime = "/null@localhost/i";
- from_mime = "/watchdog@localhost/i";
- apply "default" {
- symbols_disabled = ["HISTORY_SAVE", "ARC", "ARC_SIGNED", "DKIM", "DKIM_SIGNED", "CLAM_VIRUS"];
- want_spam = yes;
- actions {
- reject = 9999.0;
- greylist = 9998.0;
- "add header" = 9997.0;
- }
- }
- }
- <?php
- /*
- // Start custom scores for users
- */
- $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'highspamlevel' OR `option` = 'lowspamlevel'");
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($rows)) {
- $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
- ?>
- score_<?=$username_sane;?> {
- priority = 4;
- <?php
- foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- $stmt = $pdo->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);
- ?>
- apply "default" {
- actions {
- reject = <?=$spamscore['highspamlevel'][0];?>;
- greylist = <?=$spamscore['lowspamlevel'][0] - 1;?>;
- "add header" = <?=$spamscore['lowspamlevel'][0];?>;
- }
- }
- }
- <?php
- }
- /*
- // Start SOGo contacts whitelist
- // Priority 4, lower than a domain whitelist (5) and lower than a mailbox whitelist (6)
- */
- foreach (wl_by_sogo() as $user => $contacts) {
- $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $user);
- ?>
- whitelist_sogo_<?=$username_sane;?> {
- <?php
- foreach ($contacts as $contact) {
- ?>
- from = <?=json_encode($contact, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- ?>
- priority = 4;
- <?php
- foreach (ucl_rcpts($user, 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- ?>
- apply "default" {
- SOGO_CONTACT = -99.0;
- }
- symbols [
- "SOGO_CONTACT"
- ]
- }
- <?php
- }
- /*
- // Start whitelist
- */
- $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'whitelist_from'");
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($rows)) {
- $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
- ?>
- whitelist_<?=$username_sane;?> {
- <?php
- $list_items = array();
- $stmt = $pdo->prepare("SELECT `value` FROM `filterconf`
- WHERE `object`= :object
- AND `option` = 'whitelist_from'");
- $stmt->execute(array(':object' => $row['object']));
- $list_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
- foreach ($list_items as $item) {
- ?>
- from = "/<?='^' . str_replace('\*', '.*', preg_quote($item['value'], '/')) . '$' ;?>/i";
- <?php
- }
- if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
- ?>
- priority = 5;
- <?php
- foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- }
- else {
- ?>
- priority = 6;
- <?php
- foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- }
- ?>
- apply "default" {
- MAILCOW_WHITE = -999.0;
- }
- symbols [
- "MAILCOW_WHITE"
- ]
- }
- whitelist_mime_<?=$username_sane;?> {
- <?php
- foreach ($list_items as $item) {
- ?>
- from_mime = "/<?='^' . str_replace('\*', '.*', preg_quote($item['value'], '/')) . '$' ;?>/i";
- <?php
- }
- if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
- ?>
- priority = 5;
- <?php
- foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- }
- else {
- ?>
- priority = 6;
- <?php
- foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- }
- ?>
- apply "default" {
- MAILCOW_WHITE = -999.0;
- }
- symbols [
- "MAILCOW_WHITE"
- ]
- }
- <?php
- }
- /*
- // Start blacklist
- */
- $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'blacklist_from'");
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($rows)) {
- $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
- ?>
- blacklist_<?=$username_sane;?> {
- <?php
- $list_items = array();
- $stmt = $pdo->prepare("SELECT `value` FROM `filterconf`
- WHERE `object`= :object
- AND `option` = 'blacklist_from'");
- $stmt->execute(array(':object' => $row['object']));
- $list_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
- foreach ($list_items as $item) {
- ?>
- from = "/<?='^' . str_replace('\*', '.*', preg_quote($item['value'], '/')) . '$' ;?>/i";
- <?php
- }
- if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
- ?>
- priority = 5;
- <?php
- foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- }
- else {
- ?>
- priority = 6;
- <?php
- foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- }
- ?>
- apply "default" {
- MAILCOW_BLACK = 999.0;
- }
- symbols [
- "MAILCOW_BLACK"
- ]
- }
- blacklist_header_<?=$username_sane;?> {
- <?php
- foreach ($list_items as $item) {
- ?>
- from_mime = "/<?='^' . str_replace('\*', '.*', preg_quote($item['value'], '/')) . '$' ;?>/i";
- <?php
- }
- if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
- ?>
- priority = 5;
- <?php
- foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- }
- else {
- ?>
- priority = 6;
- <?php
- foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- }
- ?>
- apply "default" {
- MAILCOW_BLACK = 999.0;
- }
- symbols [
- "MAILCOW_BLACK"
- ]
- }
- <?php
- }
- /*
- // Start traps
- */
- ?>
- ham_trap {
- <?php
- foreach (ucl_rcpts('ham@localhost', 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- ?>
- priority = 9;
- apply "default" {
- symbols_enabled = ["HISTORY_SAVE"];
- }
- symbols [
- "HAM_TRAP"
- ]
- }
- spam_trap {
- <?php
- foreach (ucl_rcpts('spam@localhost', 'mailbox') as $rcpt) {
- ?>
- rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
- <?php
- }
- ?>
- priority = 9;
- apply "default" {
- symbols_enabled = ["HISTORY_SAVE"];
- }
- symbols [
- "SPAM_TRAP"
- ]
- }
- <?php
- // Start additional content
- $stmt = $pdo->query("SELECT `id`, `content` FROM `settingsmap` WHERE `active` = '1'");
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($rows)) {
- $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['id']);
- ?>
- additional_settings_<?=intval($row['id']);?> {
- <?php
- $content = preg_split('/\r\n|\r|\n/', $row['content']);
- foreach ($content as $line) {
- echo ' ' . $line . PHP_EOL;
- }
- ?>
- }
- <?php
- }
- ?>
- }
|