settings.php 12 KB

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