settings.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /*
  3. The match section performs AND operation on different matches: for example, if you have from and rcpt in the same rule,
  4. then the rule matches only when from AND rcpt match. For similar matches, the OR rule applies: if you have multiple rcpt matches,
  5. then any of these will trigger the rule. If a rule is triggered then no more rules are matched.
  6. */
  7. function parse_email($email) {
  8. if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
  9. $a = strrpos($email, '@');
  10. return array('local' => substr($email, 0, $a), 'domain' => substr($email, $a));
  11. }
  12. header('Content-Type: text/plain');
  13. require_once "vars.inc.php";
  14. ini_set('error_reporting', 0);
  15. $dsn = $database_type . ':host=' . $database_host . ';dbname=' . $database_name;
  16. $opt = [
  17. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  18. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  19. PDO::ATTR_EMULATE_PREPARES => false,
  20. ];
  21. try {
  22. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  23. $stmt = $pdo->query("SELECT * FROM `filterconf`");
  24. }
  25. catch (PDOException $e) {
  26. echo 'settings { }';
  27. exit;
  28. }
  29. ?>
  30. settings {
  31. <?php
  32. /*
  33. // Start whitelist for forwarding hosts
  34. */
  35. try {
  36. $stmt = $pdo->query("SELECT `host` FROM `forwarding_hosts`");
  37. $rows = $stmt->fetchAll(PDO::FETCH_COLUMN);
  38. }
  39. catch (PDOException $e) {
  40. $rows = array();
  41. }
  42. if (!empty($rows)) {
  43. ?>
  44. whitelist_forwarding_hosts {
  45. priority = high;
  46. <?php
  47. foreach ($rows as $host):
  48. ?>
  49. ip = "<?=$host;?>";
  50. <?php
  51. endforeach;
  52. ?>
  53. want_spam = yes;
  54. }
  55. <?php
  56. }
  57. /*
  58. // Start custom scores for users
  59. */
  60. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'highspamlevel' OR `option` = 'lowspamlevel'");
  61. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  62. while ($row = array_shift($rows)) {
  63. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  64. ?>
  65. score_<?=$username_sane;?> {
  66. priority = low;
  67. <?php
  68. $stmt = $pdo->prepare("SELECT `option`, `value` FROM `filterconf`
  69. WHERE (`option` = 'highspamlevel' OR `option` = 'lowspamlevel')
  70. AND `object`= :object");
  71. $stmt->execute(array(':object' => $row['object']));
  72. $spamscore = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
  73. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  74. WHERE (`object`= :object OR `object`= :object_domain)
  75. AND (`option` = 'blacklist_from' OR `option` = 'whitelist_from')");
  76. $stmt->execute(array(':object' => $row['object'], ':object_domain' => substr(strrchr($row['object'], "@"), 1)));
  77. $grouped_lists = $stmt->fetchAll(PDO::FETCH_ASSOC);
  78. array_filter($grouped_lists);
  79. while ($grouped_list = array_shift($grouped_lists)) {
  80. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_list['value'])));
  81. if (!empty($value_sane)) {
  82. ?>
  83. from = "/^((?!<?=$value_sane;?>).)*$/";
  84. <?php
  85. }
  86. }
  87. $local = parse_email($row['object'])['local'];
  88. $domain = parse_email($row['object'])['domain'];
  89. if (!empty($local) && !empty($local)) {
  90. ?>
  91. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  92. <?php
  93. }
  94. ?>
  95. rcpt = "<?=$row['object'];?>";
  96. <?php
  97. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` LIKE :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  98. $stmt->execute(array(':object_goto' => '%' . $row['object'] . '%', ':object_address' => $row['object']));
  99. $rows_aliases_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  100. while ($row_aliases_1 = array_shift($rows_aliases_1)) {
  101. $local = parse_email($row_aliases_1['address'])['local'];
  102. $domain = parse_email($row_aliases_1['address'])['domain'];
  103. if (!empty($local) && !empty($local)) {
  104. ?>
  105. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  106. <?php
  107. }
  108. ?>
  109. rcpt = "<?=$row_aliases_1['address'];?>";
  110. <?php
  111. }
  112. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  113. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  114. WHERE `mailbox`.`username` = :object");
  115. $stmt->execute(array(':object' => $row['object']));
  116. $rows_aliases_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  117. array_filter($rows_aliases_2);
  118. while ($row_aliases_2 = array_shift($rows_aliases_2)) {
  119. if (!empty($row_aliases_2['aliases'])) {
  120. $local = parse_email($row_aliases_2['aliases'])['local'];
  121. $domain = parse_email($row_aliases_2['aliases'])['domain'];
  122. if (!empty($local) && !empty($local)) {
  123. ?>
  124. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  125. <?php
  126. }
  127. ?>
  128. rcpt = "<?=$row_aliases_2['aliases'];?>";
  129. <?php
  130. }
  131. }
  132. ?>
  133. apply "default" {
  134. actions {
  135. reject = <?=$spamscore['highspamlevel'][0];?>;
  136. greylist = <?=$spamscore['lowspamlevel'][0] - 1;?>;
  137. "add header" = <?=$spamscore['lowspamlevel'][0];?>;
  138. }
  139. }
  140. }
  141. <?php
  142. }
  143. /*
  144. // Start whitelist
  145. */
  146. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'whitelist_from'");
  147. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  148. while ($row = array_shift($rows)) {
  149. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  150. ?>
  151. whitelist_<?=$username_sane;?> {
  152. <?php
  153. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  154. WHERE `object`= :object
  155. AND `option` = 'whitelist_from'");
  156. $stmt->execute(array(':object' => $row['object']));
  157. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  158. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  159. ?>
  160. from = "/(<?=$value_sane;?>)/";
  161. <?php
  162. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  163. ?>
  164. priority = medium;
  165. rcpt = "/.*@<?=$row['object'];?>/";
  166. <?php
  167. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  168. WHERE `target_domain` = :object");
  169. $stmt->execute(array(':object' => $row['object']));
  170. $rows_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  171. array_filter($rows_domain_aliases);
  172. while ($row_domain_aliases = array_shift($rows_domain_aliases)) {
  173. ?>
  174. rcpt = "/.*@<?=$row_domain_aliases['alias_domain'];?>/";
  175. <?php
  176. }
  177. }
  178. else {
  179. ?>
  180. priority = high;
  181. <?php
  182. $local = parse_email($row['object'])['local'];
  183. $domain = parse_email($row['object'])['domain'];
  184. if (!empty($local) && !empty($local)) {
  185. ?>
  186. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  187. <?php
  188. }
  189. ?>
  190. rcpt = "<?=$row['object'];?>";
  191. <?php
  192. }
  193. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` LIKE :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  194. $stmt->execute(array(':object_goto' => '%' . $row['object'] . '%', ':object_address' => $row['object']));
  195. $rows_aliases_wl_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  196. array_filter($rows_aliases_wl_1);
  197. while ($row_aliases_wl_1 = array_shift($rows_aliases_wl_1)) {
  198. $local = parse_email($row_aliases_wl_1['address'])['local'];
  199. $domain = parse_email($row_aliases_wl_1['address'])['domain'];
  200. if (!empty($local) && !empty($local)) {
  201. ?>
  202. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  203. <?php
  204. }
  205. ?>
  206. rcpt = "<?=$row_aliases_wl_1['address'];?>";
  207. <?php
  208. }
  209. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  210. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  211. WHERE `mailbox`.`username` = :object");
  212. $stmt->execute(array(':object' => $row['object']));
  213. $rows_aliases_wl_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  214. array_filter($rows_aliases_wl_2);
  215. while ($row_aliases_wl_2 = array_shift($rows_aliases_wl_2)) {
  216. if (!empty($row_aliases_wl_2['aliases'])) {
  217. $local = parse_email($row_aliases_wl_2['aliases'])['local'];
  218. $domain = parse_email($row_aliases_wl_2['aliases'])['domain'];
  219. if (!empty($local) && !empty($local)) {
  220. ?>
  221. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  222. <?php
  223. }
  224. ?>
  225. rcpt = "<?=$row_aliases_wl_2['aliases'];?>";
  226. <?php
  227. }
  228. }
  229. ?>
  230. apply "default" {
  231. MAILCOW_WHITE = -999.0;
  232. }
  233. symbols [
  234. "MAILCOW_WHITE"
  235. ]
  236. }
  237. <?php
  238. }
  239. /*
  240. // Start blacklist
  241. */
  242. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'blacklist_from'");
  243. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  244. while ($row = array_shift($rows)) {
  245. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  246. ?>
  247. blacklist_<?=$username_sane;?> {
  248. <?php
  249. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  250. WHERE `object`= :object
  251. AND `option` = 'blacklist_from'");
  252. $stmt->execute(array(':object' => $row['object']));
  253. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  254. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  255. ?>
  256. from = "/(<?=$value_sane;?>)/";
  257. <?php
  258. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  259. ?>
  260. priority = medium;
  261. rcpt = "/.*@<?=$row['object'];?>/";
  262. <?php
  263. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  264. WHERE `target_domain` = :object");
  265. $stmt->execute(array(':object' => $row['object']));
  266. $rows_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  267. array_filter($rows_domain_aliases);
  268. while ($row_domain_aliases = array_shift($rows_domain_aliases)) {
  269. ?>
  270. rcpt = "/.*@<?=$row_domain_aliases['alias_domain'];?>/";
  271. <?php
  272. }
  273. }
  274. else {
  275. ?>
  276. priority = high;
  277. <?php
  278. $local = parse_email($row['object'])['local'];
  279. $domain = parse_email($row['object'])['domain'];
  280. if (!empty($local) && !empty($local)) {
  281. ?>
  282. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  283. <?php
  284. }
  285. ?>
  286. rcpt = "<?=$row['object'];?>";
  287. <?php
  288. }
  289. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` LIKE :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  290. $stmt->execute(array(':object_goto' => '%' . $row['object'] . '%', ':object_address' => $row['object']));
  291. $rows_aliases_bl_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  292. array_filter($rows_aliases_bl_1);
  293. while ($row_aliases_bl_1 = array_shift($rows_aliases_bl_1)) {
  294. $local = parse_email($row_aliases_bl_1['address'])['local'];
  295. $domain = parse_email($row_aliases_bl_1['address'])['domain'];
  296. if (!empty($local) && !empty($local)) {
  297. ?>
  298. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  299. <?php
  300. }
  301. ?>
  302. rcpt = "<?=$row_aliases_bl_1['address'];?>";
  303. <?php
  304. }
  305. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  306. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  307. WHERE `mailbox`.`username` = :object");
  308. $stmt->execute(array(':object' => $row['object']));
  309. $rows_aliases_bl_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  310. array_filter($rows_aliases_bl_2);
  311. while ($row_aliases_bl_2 = array_shift($rows_aliases_bl_2)) {
  312. if (!empty($row_aliases_bl_2['aliases'])) {
  313. $local = parse_email($row_aliases_bl_2['aliases'])['local'];
  314. $domain = parse_email($row_aliases_bl_2['aliases'])['domain'];
  315. if (!empty($local) && !empty($local)) {
  316. ?>
  317. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  318. <?php
  319. }
  320. ?>
  321. rcpt = "<?=$row_aliases_bl_2['aliases'];?>";
  322. <?php
  323. }
  324. }
  325. ?>
  326. apply "default" {
  327. MAILCOW_BLACK = 999.0;
  328. }
  329. symbols [
  330. "MAILCOW_BLACK"
  331. ]
  332. }
  333. <?php
  334. }
  335. ?>
  336. }