settings.php 12 KB

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