2
0

settings.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 custom scores for users
  34. */
  35. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'highspamlevel' OR `option` = 'lowspamlevel'");
  36. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  37. while ($row = array_shift($rows)) {
  38. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  39. ?>
  40. score_<?=$username_sane;?> {
  41. priority = low;
  42. <?php
  43. $stmt = $pdo->prepare("SELECT `option`, `value` FROM `filterconf`
  44. WHERE (`option` = 'highspamlevel' OR `option` = 'lowspamlevel')
  45. AND `object`= :object");
  46. $stmt->execute(array(':object' => $row['object']));
  47. $spamscore = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
  48. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  49. WHERE (`object`= :object OR `object`= :object_domain)
  50. AND (`option` = 'blacklist_from' OR `option` = 'whitelist_from')");
  51. $stmt->execute(array(':object' => $row['object'], ':object_domain' => substr(strrchr($row['object'], "@"), 1)));
  52. $grouped_lists = $stmt->fetchAll(PDO::FETCH_ASSOC);
  53. array_filter($grouped_lists);
  54. while ($grouped_list = array_shift($grouped_lists)) {
  55. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_list['value'])));
  56. if (!empty($value_sane)) {
  57. ?>
  58. from = "/^((?!<?=$value_sane;?>).)*$/";
  59. <?php
  60. }
  61. }
  62. $local = parse_email($row['object'])['local'];
  63. $domain = parse_email($row['object'])['domain'];
  64. if (!empty($local) && !empty($local)) {
  65. ?>
  66. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  67. <?php
  68. }
  69. ?>
  70. rcpt = "<?=$row['object'];?>";
  71. <?php
  72. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` LIKE :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  73. $stmt->execute(array(':object_goto' => '%' . $row['object'] . '%', ':object_address' => $row['object']));
  74. $rows_aliases_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  75. while ($row_aliases_1 = array_shift($rows_aliases_1)) {
  76. $local = parse_email($row_aliases_1['address'])['local'];
  77. $domain = parse_email($row_aliases_1['address'])['domain'];
  78. if (!empty($local) && !empty($local)) {
  79. ?>
  80. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  81. <?php
  82. }
  83. ?>
  84. rcpt = "<?=$row_aliases_1['address'];?>";
  85. <?php
  86. }
  87. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  88. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  89. WHERE `mailbox`.`username` = :object");
  90. $stmt->execute(array(':object' => $row['object']));
  91. $rows_aliases_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  92. array_filter($rows_aliases_2);
  93. while ($row_aliases_2 = array_shift($rows_aliases_2)) {
  94. if (!empty($row_aliases_2['aliases'])) {
  95. $local = parse_email($row_aliases_2['aliases'])['local'];
  96. $domain = parse_email($row_aliases_2['aliases'])['domain'];
  97. if (!empty($local) && !empty($local)) {
  98. ?>
  99. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  100. <?php
  101. }
  102. ?>
  103. rcpt = "<?=$row_aliases_2['aliases'];?>";
  104. <?php
  105. }
  106. }
  107. ?>
  108. apply "default" {
  109. actions {
  110. reject = <?=$spamscore['highspamlevel'][0];?>;
  111. greylist = <?=$spamscore['lowspamlevel'][0] - 1;?>;
  112. "add header" = <?=$spamscore['lowspamlevel'][0];?>;
  113. }
  114. }
  115. }
  116. <?php
  117. }
  118. /*
  119. // Start whitelist
  120. */
  121. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'whitelist_from'");
  122. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  123. while ($row = array_shift($rows)) {
  124. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  125. ?>
  126. whitelist_<?=$username_sane;?> {
  127. <?php
  128. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  129. WHERE `object`= :object
  130. AND `option` = 'whitelist_from'");
  131. $stmt->execute(array(':object' => $row['object']));
  132. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  133. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  134. ?>
  135. from = "/(<?=$value_sane;?>)/";
  136. <?php
  137. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  138. ?>
  139. priority = medium;
  140. rcpt = "/.*@<?=$row['object'];?>/";
  141. <?php
  142. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  143. WHERE `target_domain` = :object");
  144. $stmt->execute(array(':object' => $row['object']));
  145. $rows_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  146. array_filter($rows_domain_aliases);
  147. while ($row_domain_aliases = array_shift($rows_domain_aliases)) {
  148. ?>
  149. rcpt = "/.*@<?=$row_domain_aliases['alias_domain'];?>/";
  150. <?php
  151. }
  152. }
  153. else {
  154. ?>
  155. priority = high;
  156. <?php
  157. $local = parse_email($row['object'])['local'];
  158. $domain = parse_email($row['object'])['domain'];
  159. if (!empty($local) && !empty($local)) {
  160. ?>
  161. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  162. <?php
  163. }
  164. ?>
  165. rcpt = "<?=$row['object'];?>";
  166. <?php
  167. }
  168. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` LIKE :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  169. $stmt->execute(array(':object_goto' => '%' . $row['object'] . '%', ':object_address' => $row['object']));
  170. $rows_aliases_wl_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  171. array_filter($rows_aliases_wl_1);
  172. while ($row_aliases_wl_1 = array_shift($rows_aliases_wl_1)) {
  173. $local = parse_email($row_aliases_wl_1['address'])['local'];
  174. $domain = parse_email($row_aliases_wl_1['address'])['domain'];
  175. if (!empty($local) && !empty($local)) {
  176. ?>
  177. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  178. <?php
  179. }
  180. ?>
  181. rcpt = "<?=$row_aliases_wl_1['address'];?>";
  182. <?php
  183. }
  184. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  185. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  186. WHERE `mailbox`.`username` = :object");
  187. $stmt->execute(array(':object' => $row['object']));
  188. $rows_aliases_wl_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  189. array_filter($rows_aliases_wl_2);
  190. while ($row_aliases_wl_2 = array_shift($rows_aliases_wl_2)) {
  191. if (!empty($row_aliases_wl_2['aliases'])) {
  192. $local = parse_email($row_aliases_wl_2['aliases'])['local'];
  193. $domain = parse_email($row_aliases_wl_2['aliases'])['domain'];
  194. if (!empty($local) && !empty($local)) {
  195. ?>
  196. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  197. <?php
  198. }
  199. ?>
  200. rcpt = "<?=$row_aliases_wl_2['aliases'];?>";
  201. <?php
  202. }
  203. }
  204. ?>
  205. apply "default" {
  206. MAILCOW_WHITE = -999.0;
  207. }
  208. symbols [
  209. "MAILCOW_WHITE"
  210. ]
  211. }
  212. <?php
  213. }
  214. /*
  215. // Start blacklist
  216. */
  217. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'blacklist_from'");
  218. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  219. while ($row = array_shift($rows)) {
  220. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  221. ?>
  222. blacklist_<?=$username_sane;?> {
  223. <?php
  224. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  225. WHERE `object`= :object
  226. AND `option` = 'blacklist_from'");
  227. $stmt->execute(array(':object' => $row['object']));
  228. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  229. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  230. ?>
  231. from = "/(<?=$value_sane;?>)/";
  232. <?php
  233. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  234. ?>
  235. priority = medium;
  236. rcpt = "/.*@<?=$row['object'];?>/";
  237. <?php
  238. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  239. WHERE `target_domain` = :object");
  240. $stmt->execute(array(':object' => $row['object']));
  241. $rows_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  242. array_filter($rows_domain_aliases);
  243. while ($row_domain_aliases = array_shift($rows_domain_aliases)) {
  244. ?>
  245. rcpt = "/.*@<?=$row_domain_aliases['alias_domain'];?>/";
  246. <?php
  247. }
  248. }
  249. else {
  250. ?>
  251. priority = high;
  252. <?php
  253. $local = parse_email($row['object'])['local'];
  254. $domain = parse_email($row['object'])['domain'];
  255. if (!empty($local) && !empty($local)) {
  256. ?>
  257. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  258. <?php
  259. }
  260. ?>
  261. rcpt = "<?=$row['object'];?>";
  262. <?php
  263. }
  264. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` LIKE :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  265. $stmt->execute(array(':object_goto' => '%' . $row['object'] . '%', ':object_address' => $row['object']));
  266. $rows_aliases_bl_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  267. array_filter($rows_aliases_bl_1);
  268. while ($row_aliases_bl_1 = array_shift($rows_aliases_bl_1)) {
  269. $local = parse_email($row_aliases_bl_1['address'])['local'];
  270. $domain = parse_email($row_aliases_bl_1['address'])['domain'];
  271. if (!empty($local) && !empty($local)) {
  272. ?>
  273. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  274. <?php
  275. }
  276. ?>
  277. rcpt = "<?=$row_aliases_bl_1['address'];?>";
  278. <?php
  279. }
  280. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  281. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  282. WHERE `mailbox`.`username` = :object");
  283. $stmt->execute(array(':object' => $row['object']));
  284. $rows_aliases_bl_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  285. array_filter($rows_aliases_bl_2);
  286. while ($row_aliases_bl_2 = array_shift($rows_aliases_bl_2)) {
  287. if (!empty($row_aliases_bl_2['aliases'])) {
  288. $local = parse_email($row_aliases_bl_2['aliases'])['local'];
  289. $domain = parse_email($row_aliases_bl_2['aliases'])['domain'];
  290. if (!empty($local) && !empty($local)) {
  291. ?>
  292. rcpt = "/<?=$local;?>\+.*<?=$domain;?>/";
  293. <?php
  294. }
  295. ?>
  296. rcpt = "<?=$row_aliases_bl_2['aliases'];?>";
  297. <?php
  298. }
  299. }
  300. ?>
  301. apply "default" {
  302. MAILCOW_BLACK = 999.0;
  303. }
  304. symbols [
  305. "MAILCOW_BLACK"
  306. ]
  307. }
  308. <?php
  309. }
  310. ?>
  311. }