settings.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. header('Content-Type: text/plain');
  8. require_once "vars.inc.php";
  9. ini_set('error_reporting', 0);
  10. //$dsn = $database_type . ':host=' . $database_host . ';dbname=' . $database_name;
  11. $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
  12. $opt = [
  13. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  14. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  15. PDO::ATTR_EMULATE_PREPARES => false,
  16. ];
  17. try {
  18. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  19. $stmt = $pdo->query("SELECT '1' FROM `filterconf`");
  20. }
  21. catch (PDOException $e) {
  22. echo 'settings { }';
  23. exit;
  24. }
  25. function parse_email($email) {
  26. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
  27. $a = strrpos($email, '@');
  28. return array('local' => substr($email, 0, $a), 'domain' => substr($email, $a));
  29. }
  30. function wl_by_sogo() {
  31. global $pdo;
  32. $rcpt = array();
  33. $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`
  34. INNER JOIN `sogo_quick_contact` ON `sogo_quick_contact`.`c_folder_id` = `sogo_folder_info`.`c_folder_id`
  35. GROUP BY `c_path2`");
  36. $sogo_contacts = $stmt->fetchAll(PDO::FETCH_ASSOC);
  37. while ($row = array_shift($sogo_contacts)) {
  38. foreach (explode(',', $row['contacts']) as $contact) {
  39. if (!filter_var($contact, FILTER_VALIDATE_EMAIL)) {
  40. continue;
  41. }
  42. $rcpt[$row['user']][] = '/^' . str_replace('/', '\/', $contact) . '$/i';
  43. }
  44. }
  45. return $rcpt;
  46. }
  47. function ucl_rcpts($object, $type) {
  48. global $pdo;
  49. $rcpt = array();
  50. if ($type == 'mailbox') {
  51. // Standard aliases
  52. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  53. WHERE `goto` = :object_goto
  54. AND `address` NOT LIKE '@%'");
  55. $stmt->execute(array(
  56. ':object_goto' => $object
  57. ));
  58. $standard_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  59. while ($row = array_shift($standard_aliases)) {
  60. $local = parse_email($row['address'])['local'];
  61. $domain = parse_email($row['address'])['domain'];
  62. if (!empty($local) && !empty($domain)) {
  63. $rcpt[] = '/^' . str_replace('/', '\/', $local) . '[+].*' . str_replace('/', '\/', $domain) . '$/i';
  64. }
  65. $rcpt[] = '/^' . str_replace('/', '\/', $row['address']) . '$/i';
  66. }
  67. // Aliases by alias domains
  68. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias` FROM `mailbox`
  69. LEFT OUTER JOIN `alias_domain` ON `mailbox`.`domain` = `alias_domain`.`target_domain`
  70. WHERE `mailbox`.`username` = :object");
  71. $stmt->execute(array(
  72. ':object' => $object
  73. ));
  74. $by_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  75. array_filter($by_domain_aliases);
  76. while ($row = array_shift($by_domain_aliases)) {
  77. if (!empty($row['alias'])) {
  78. $local = parse_email($row['alias'])['local'];
  79. $domain = parse_email($row['alias'])['domain'];
  80. if (!empty($local) && !empty($domain)) {
  81. $rcpt[] = '/^' . str_replace('/', '\/', $local) . '[+].*' . str_replace('/', '\/', $domain) . '$/i';
  82. }
  83. $rcpt[] = '/^' . str_replace('/', '\/', $row['alias']) . '$/i';
  84. }
  85. }
  86. }
  87. elseif ($type == 'domain') {
  88. // Domain self
  89. $rcpt[] = '/.*@' . $object . '/i';
  90. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  91. WHERE `target_domain` = :object");
  92. $stmt->execute(array(':object' => $object));
  93. $alias_domains = $stmt->fetchAll(PDO::FETCH_ASSOC);
  94. array_filter($alias_domains);
  95. while ($row = array_shift($alias_domains)) {
  96. $rcpt[] = '/.*@' . $row['alias_domain'] . '/i';
  97. }
  98. }
  99. return $rcpt;
  100. }
  101. ?>
  102. settings {
  103. watchdog {
  104. priority = 10;
  105. rcpt = "/null@localhost/i";
  106. from = "/watchdog@localhost/i";
  107. apply "default" {
  108. actions {
  109. reject = 9999.0;
  110. greylist = 9998.0;
  111. "add header" = 9997.0;
  112. }
  113. }
  114. }
  115. <?php
  116. /*
  117. // Start custom scores for users
  118. */
  119. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'highspamlevel' OR `option` = 'lowspamlevel'");
  120. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  121. while ($row = array_shift($rows)) {
  122. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  123. ?>
  124. score_<?=$username_sane;?> {
  125. priority = 4;
  126. <?php
  127. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  128. ?>
  129. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  130. <?php
  131. }
  132. $stmt = $pdo->prepare("SELECT `option`, `value` FROM `filterconf`
  133. WHERE (`option` = 'highspamlevel' OR `option` = 'lowspamlevel')
  134. AND `object`= :object");
  135. $stmt->execute(array(':object' => $row['object']));
  136. $spamscore = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
  137. ?>
  138. apply "default" {
  139. actions {
  140. reject = <?=$spamscore['highspamlevel'][0];?>;
  141. greylist = <?=$spamscore['lowspamlevel'][0] - 1;?>;
  142. "add header" = <?=$spamscore['lowspamlevel'][0];?>;
  143. }
  144. }
  145. }
  146. <?php
  147. }
  148. /*
  149. // Start SOGo contacts whitelist
  150. // Priority 4, lower than a domain whitelist (5) and lower than a mailbox whitelist (6)
  151. */
  152. foreach (wl_by_sogo() as $user => $contacts) {
  153. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $user);
  154. ?>
  155. whitelist_sogo_<?=$username_sane;?> {
  156. <?php
  157. foreach ($contacts as $contact) {
  158. ?>
  159. from = <?=json_encode($contact, JSON_UNESCAPED_SLASHES);?>;
  160. <?php
  161. }
  162. ?>
  163. priority = 4;
  164. <?php
  165. foreach (ucl_rcpts($user, 'mailbox') as $rcpt) {
  166. ?>
  167. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  168. <?php
  169. }
  170. ?>
  171. apply "default" {
  172. SOGO_CONTACT = -999.0;
  173. }
  174. symbols [
  175. "SOGO_CONTACT"
  176. ]
  177. }
  178. <?php
  179. }
  180. /*
  181. // Start whitelist
  182. */
  183. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'whitelist_from'");
  184. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  185. while ($row = array_shift($rows)) {
  186. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  187. ?>
  188. whitelist_<?=$username_sane;?> {
  189. <?php
  190. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf`
  191. WHERE `object`= :object
  192. AND `option` = 'whitelist_from'");
  193. $stmt->execute(array(':object' => $row['object']));
  194. $list_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
  195. while ($item = array_shift($list_items)) {
  196. ?>
  197. from = "/<?='^' . str_replace('\*', '.*', preg_quote($item['value'], '/')) . '$' ;?>/i";
  198. <?php
  199. }
  200. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  201. ?>
  202. priority = 5;
  203. <?php
  204. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  205. ?>
  206. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  207. <?php
  208. }
  209. }
  210. else {
  211. ?>
  212. priority = 6;
  213. <?php
  214. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  215. ?>
  216. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  217. <?php
  218. }
  219. }
  220. ?>
  221. apply "default" {
  222. MAILCOW_WHITE = -999.0;
  223. }
  224. symbols [
  225. "MAILCOW_WHITE"
  226. ]
  227. }
  228. whitelist_header_<?=$username_sane;?> {
  229. <?php
  230. $header_from = array();
  231. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf`
  232. WHERE `object`= :object
  233. AND `option` = 'whitelist_from'");
  234. $stmt->execute(array(':object' => $row['object']));
  235. $list_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
  236. ?>
  237. header = {
  238. <?php
  239. while ($item = array_shift($list_items)) {
  240. $header_from[] = str_replace('\*', '.*', preg_quote($item['value'], '/'));
  241. }
  242. ?>
  243. "From" = "/(<?=implode('|', $header_from);?>)/i";
  244. }
  245. <?php
  246. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  247. ?>
  248. priority = 5;
  249. <?php
  250. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  251. ?>
  252. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  253. <?php
  254. }
  255. }
  256. else {
  257. ?>
  258. priority = 6;
  259. <?php
  260. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  261. ?>
  262. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  263. <?php
  264. }
  265. }
  266. ?>
  267. apply "default" {
  268. MAILCOW_WHITE = -999.0;
  269. }
  270. symbols [
  271. "MAILCOW_WHITE"
  272. ]
  273. }
  274. <?php
  275. }
  276. /*
  277. // Start blacklist
  278. */
  279. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'blacklist_from'");
  280. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  281. while ($row = array_shift($rows)) {
  282. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  283. ?>
  284. blacklist_<?=$username_sane;?> {
  285. <?php
  286. $items[] = array();
  287. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf`
  288. WHERE `object`= :object
  289. AND `option` = 'blacklist_from'");
  290. $stmt->execute(array(':object' => $row['object']));
  291. $list_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
  292. while ($item = array_shift($list_items)) {
  293. ?>
  294. from = "/<?='^' . str_replace('\*', '.*', preg_quote($item['value'], '/')) . '$' ;?>/i";
  295. <?php
  296. }
  297. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  298. ?>
  299. priority = 5;
  300. <?php
  301. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  302. ?>
  303. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  304. <?php
  305. }
  306. }
  307. else {
  308. ?>
  309. priority = 6;
  310. <?php
  311. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  312. ?>
  313. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  314. <?php
  315. }
  316. }
  317. ?>
  318. apply "default" {
  319. MAILCOW_BLACK = 999.0;
  320. }
  321. symbols [
  322. "MAILCOW_BLACK"
  323. ]
  324. }
  325. blacklist_header_<?=$username_sane;?> {
  326. <?php
  327. $header_from = array();
  328. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf`
  329. WHERE `object`= :object
  330. AND `option` = 'blacklist_from'");
  331. $stmt->execute(array(':object' => $row['object']));
  332. $list_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
  333. ?>
  334. header = {
  335. <?php
  336. while ($item = array_shift($list_items)) {
  337. $header_from[] = str_replace('\*', '.*', preg_quote($item['value'], '/'));
  338. }
  339. ?>
  340. "From" = "/(<?=implode('|', $header_from);?>)/i";
  341. }
  342. <?php
  343. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  344. ?>
  345. priority = 5;
  346. <?php
  347. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  348. ?>
  349. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  350. <?php
  351. }
  352. }
  353. else {
  354. ?>
  355. priority = 6;
  356. <?php
  357. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  358. ?>
  359. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  360. <?php
  361. }
  362. }
  363. ?>
  364. apply "default" {
  365. MAILCOW_BLACK = 999.0;
  366. }
  367. symbols [
  368. "MAILCOW_BLACK"
  369. ]
  370. }
  371. <?php
  372. }
  373. /*
  374. // Start traps
  375. */
  376. ?>
  377. traps {
  378. <?php
  379. foreach (ucl_rcpts('spam@localhost', 'mailbox') as $rcpt) {
  380. ?>
  381. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  382. <?php
  383. }
  384. foreach (ucl_rcpts('ham@localhost', 'mailbox') as $rcpt) {
  385. ?>
  386. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  387. <?php
  388. }
  389. ?>
  390. priority = 9;
  391. want_spam = yes;
  392. }
  393. <?php
  394. // Start additional content
  395. $stmt = $pdo->query("SELECT `id`, `content` FROM `settingsmap` WHERE `active` = '1'");
  396. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  397. while ($row = array_shift($rows)) {
  398. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['id']);
  399. ?>
  400. additional_settings_<?=intval($row['id']);?> {
  401. <?php
  402. $content = preg_split('/\r\n|\r|\n/', $row['content']);
  403. foreach ($content as $line) {
  404. echo ' ' . $line . PHP_EOL;
  405. }
  406. ?>
  407. }
  408. <?php
  409. }
  410. ?>
  411. }