settings.php 13 KB

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