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. // 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. actions {
  128. reject = 9999.0;
  129. greylist = 9998.0;
  130. "add header" = 9997.0;
  131. }
  132. }
  133. }
  134. <?php
  135. /*
  136. // Start custom scores for users
  137. */
  138. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'highspamlevel' OR `option` = 'lowspamlevel'");
  139. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  140. while ($row = array_shift($rows)) {
  141. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  142. ?>
  143. score_<?=$username_sane;?> {
  144. priority = 4;
  145. <?php
  146. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  147. ?>
  148. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  149. <?php
  150. }
  151. $stmt = $pdo->prepare("SELECT `option`, `value` FROM `filterconf`
  152. WHERE (`option` = 'highspamlevel' OR `option` = 'lowspamlevel')
  153. AND `object`= :object");
  154. $stmt->execute(array(':object' => $row['object']));
  155. $spamscore = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
  156. ?>
  157. apply "default" {
  158. actions {
  159. reject = <?=$spamscore['highspamlevel'][0];?>;
  160. greylist = <?=$spamscore['lowspamlevel'][0] - 1;?>;
  161. "add header" = <?=$spamscore['lowspamlevel'][0];?>;
  162. }
  163. }
  164. }
  165. <?php
  166. }
  167. /*
  168. // Start SOGo contacts whitelist
  169. // Priority 4, lower than a domain whitelist (5) and lower than a mailbox whitelist (6)
  170. */
  171. foreach (wl_by_sogo() as $user => $contacts) {
  172. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $user);
  173. ?>
  174. whitelist_sogo_<?=$username_sane;?> {
  175. <?php
  176. foreach ($contacts as $contact) {
  177. ?>
  178. from = <?=json_encode($contact, JSON_UNESCAPED_SLASHES);?>;
  179. <?php
  180. }
  181. ?>
  182. priority = 4;
  183. <?php
  184. foreach (ucl_rcpts($user, 'mailbox') as $rcpt) {
  185. ?>
  186. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  187. <?php
  188. }
  189. ?>
  190. apply "default" {
  191. SOGO_CONTACT = -99.0;
  192. }
  193. symbols [
  194. "SOGO_CONTACT"
  195. ]
  196. }
  197. <?php
  198. }
  199. /*
  200. // Start whitelist
  201. */
  202. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'whitelist_from'");
  203. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  204. while ($row = array_shift($rows)) {
  205. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  206. ?>
  207. whitelist_<?=$username_sane;?> {
  208. <?php
  209. $list_items = array();
  210. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf`
  211. WHERE `object`= :object
  212. AND `option` = 'whitelist_from'");
  213. $stmt->execute(array(':object' => $row['object']));
  214. $list_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
  215. foreach ($list_items as $item) {
  216. ?>
  217. from = "/<?='^' . str_replace('\*', '.*', preg_quote($item['value'], '/')) . '$' ;?>/i";
  218. <?php
  219. }
  220. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  221. ?>
  222. priority = 5;
  223. <?php
  224. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  225. ?>
  226. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  227. <?php
  228. }
  229. }
  230. else {
  231. ?>
  232. priority = 6;
  233. <?php
  234. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  235. ?>
  236. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  237. <?php
  238. }
  239. }
  240. ?>
  241. apply "default" {
  242. MAILCOW_WHITE = -999.0;
  243. }
  244. symbols [
  245. "MAILCOW_WHITE"
  246. ]
  247. }
  248. whitelist_mime_<?=$username_sane;?> {
  249. <?php
  250. foreach ($list_items as $item) {
  251. ?>
  252. from_mime = "/<?='^' . str_replace('\*', '.*', preg_quote($item['value'], '/')) . '$' ;?>/i";
  253. <?php
  254. }
  255. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  256. ?>
  257. priority = 5;
  258. <?php
  259. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  260. ?>
  261. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  262. <?php
  263. }
  264. }
  265. else {
  266. ?>
  267. priority = 6;
  268. <?php
  269. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  270. ?>
  271. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  272. <?php
  273. }
  274. }
  275. ?>
  276. apply "default" {
  277. MAILCOW_WHITE = -999.0;
  278. }
  279. symbols [
  280. "MAILCOW_WHITE"
  281. ]
  282. }
  283. <?php
  284. }
  285. /*
  286. // Start blacklist
  287. */
  288. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'blacklist_from'");
  289. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  290. while ($row = array_shift($rows)) {
  291. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  292. ?>
  293. blacklist_<?=$username_sane;?> {
  294. <?php
  295. $list_items = array();
  296. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf`
  297. WHERE `object`= :object
  298. AND `option` = 'blacklist_from'");
  299. $stmt->execute(array(':object' => $row['object']));
  300. $list_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
  301. foreach ($list_items as $item) {
  302. ?>
  303. from = "/<?='^' . str_replace('\*', '.*', preg_quote($item['value'], '/')) . '$' ;?>/i";
  304. <?php
  305. }
  306. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  307. ?>
  308. priority = 5;
  309. <?php
  310. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  311. ?>
  312. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  313. <?php
  314. }
  315. }
  316. else {
  317. ?>
  318. priority = 6;
  319. <?php
  320. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  321. ?>
  322. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  323. <?php
  324. }
  325. }
  326. ?>
  327. apply "default" {
  328. MAILCOW_BLACK = 999.0;
  329. }
  330. symbols [
  331. "MAILCOW_BLACK"
  332. ]
  333. }
  334. blacklist_header_<?=$username_sane;?> {
  335. <?php
  336. foreach ($list_items as $item) {
  337. ?>
  338. from_mime = "/<?='^' . str_replace('\*', '.*', preg_quote($item['value'], '/')) . '$' ;?>/i";
  339. <?php
  340. }
  341. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  342. ?>
  343. priority = 5;
  344. <?php
  345. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  346. ?>
  347. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  348. <?php
  349. }
  350. }
  351. else {
  352. ?>
  353. priority = 6;
  354. <?php
  355. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  356. ?>
  357. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  358. <?php
  359. }
  360. }
  361. ?>
  362. apply "default" {
  363. MAILCOW_BLACK = 999.0;
  364. }
  365. symbols [
  366. "MAILCOW_BLACK"
  367. ]
  368. }
  369. <?php
  370. }
  371. /*
  372. // Start traps
  373. */
  374. ?>
  375. traps {
  376. <?php
  377. foreach (ucl_rcpts('spam@localhost', 'mailbox') as $rcpt) {
  378. ?>
  379. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  380. <?php
  381. }
  382. foreach (ucl_rcpts('ham@localhost', 'mailbox') as $rcpt) {
  383. ?>
  384. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  385. <?php
  386. }
  387. ?>
  388. priority = 9;
  389. want_spam = yes;
  390. }
  391. <?php
  392. // Start additional content
  393. $stmt = $pdo->query("SELECT `id`, `content` FROM `settingsmap` WHERE `active` = '1'");
  394. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  395. while ($row = array_shift($rows)) {
  396. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['id']);
  397. ?>
  398. additional_settings_<?=intval($row['id']);?> {
  399. <?php
  400. $content = preg_split('/\r\n|\r|\n/', $row['content']);
  401. foreach ($content as $line) {
  402. echo ' ' . $line . PHP_EOL;
  403. }
  404. ?>
  405. }
  406. <?php
  407. }
  408. ?>
  409. }