settings.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. $opt = [
  12. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  13. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  14. PDO::ATTR_EMULATE_PREPARES => false,
  15. ];
  16. try {
  17. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  18. $stmt = $pdo->query("SELECT '1' FROM `filterconf`");
  19. }
  20. catch (PDOException $e) {
  21. echo 'settings { }';
  22. exit;
  23. }
  24. function parse_email($email) {
  25. if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
  26. $a = strrpos($email, '@');
  27. return array('local' => substr($email, 0, $a), 'domain' => substr($email, $a));
  28. }
  29. function ucl_rcpts($object, $type) {
  30. global $pdo;
  31. if ($type == 'mailbox') {
  32. // Standard aliases
  33. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  34. WHERE `goto` = :object_goto
  35. AND `address` NOT LIKE '@%'");
  36. $stmt->execute(array(
  37. ':object_goto' => $object
  38. ));
  39. $standard_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  40. while ($row = array_shift($standard_aliases)) {
  41. $local = parse_email($row['address'])['local'];
  42. $domain = parse_email($row['address'])['domain'];
  43. if (!empty($local) && !empty($domain)) {
  44. $rcpt[] = '/' . str_replace('/', '\/', $local) . '[+].*' . str_replace('/', '\/', $domain) . '/i';
  45. }
  46. $rcpt[] = str_replace('/', '\/', $row['address']);
  47. }
  48. // Aliases by alias domains
  49. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias` FROM `mailbox`
  50. LEFT OUTER JOIN `alias_domain` ON `mailbox`.`domain` = `alias_domain`.`target_domain`
  51. WHERE `mailbox`.`username` = :object");
  52. $stmt->execute(array(
  53. ':object' => $object
  54. ));
  55. $by_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  56. array_filter($by_domain_aliases);
  57. while ($row = array_shift($by_domain_aliases)) {
  58. if (!empty($row['alias'])) {
  59. $local = parse_email($row['alias'])['local'];
  60. $domain = parse_email($row['alias'])['domain'];
  61. if (!empty($local) && !empty($domain)) {
  62. $rcpt[] = '/' . $local . '[+].*' . $domain . '/i';
  63. }
  64. $rcpt[] = $row['alias'];
  65. }
  66. }
  67. }
  68. elseif ($type == 'domain') {
  69. // Domain self
  70. $rcpt[] = '/.*@' . $object . '/i';
  71. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  72. WHERE `target_domain` = :object");
  73. $stmt->execute(array(':object' => $object));
  74. $alias_domains = $stmt->fetchAll(PDO::FETCH_ASSOC);
  75. array_filter($alias_domains);
  76. while ($row = array_shift($alias_domains)) {
  77. $rcpt[] = '/.*@' . $row['alias_domain'] . '/i';
  78. }
  79. }
  80. if (!empty($rcpt)) {
  81. return $rcpt;
  82. }
  83. return false;
  84. }
  85. ?>
  86. settings {
  87. watchdog {
  88. priority = 10;
  89. rcpt = "/null@localhost/i";
  90. from = "/watchdog@localhost/i";
  91. apply "default" {
  92. actions {
  93. reject = 9999.0;
  94. greylist = 9998.0;
  95. "add header" = 9997.0;
  96. }
  97. }
  98. }
  99. <?php
  100. /*
  101. // Start custom scores for users
  102. */
  103. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'highspamlevel' OR `option` = 'lowspamlevel'");
  104. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  105. while ($row = array_shift($rows)) {
  106. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  107. ?>
  108. score_<?=$username_sane;?> {
  109. priority = 4;
  110. <?php
  111. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  112. ?>
  113. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  114. <?php
  115. }
  116. $stmt = $pdo->prepare("SELECT `option`, `value` FROM `filterconf`
  117. WHERE (`option` = 'highspamlevel' OR `option` = 'lowspamlevel')
  118. AND `object`= :object");
  119. $stmt->execute(array(':object' => $row['object']));
  120. $spamscore = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
  121. ?>
  122. apply "default" {
  123. actions {
  124. reject = <?=$spamscore['highspamlevel'][0];?>;
  125. greylist = <?=$spamscore['lowspamlevel'][0] - 1;?>;
  126. "add header" = <?=$spamscore['lowspamlevel'][0];?>;
  127. }
  128. }
  129. }
  130. <?php
  131. }
  132. /*
  133. // Start whitelist
  134. */
  135. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'whitelist_from'");
  136. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  137. while ($row = array_shift($rows)) {
  138. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  139. ?>
  140. whitelist_<?=$username_sane;?> {
  141. <?php
  142. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(CONCAT('^', `value`, '$'), '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  143. WHERE `object`= :object
  144. AND `option` = 'whitelist_from'");
  145. $stmt->execute(array(':object' => $row['object']));
  146. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  147. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  148. ?>
  149. from = "/(<?=$value_sane;?>)/i";
  150. <?php
  151. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  152. ?>
  153. priority = 5;
  154. <?php
  155. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  156. ?>
  157. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  158. <?php
  159. }
  160. }
  161. else {
  162. ?>
  163. priority = 6;
  164. <?php
  165. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  166. ?>
  167. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  168. <?php
  169. }
  170. }
  171. ?>
  172. apply "default" {
  173. MAILCOW_WHITE = -999.0;
  174. }
  175. symbols [
  176. "MAILCOW_WHITE"
  177. ]
  178. }
  179. whitelist_header_<?=$username_sane;?> {
  180. <?php
  181. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(CONCAT('\<', `value`, '\>'), '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  182. WHERE `object`= :object
  183. AND `option` = 'whitelist_from'");
  184. $stmt->execute(array(':object' => $row['object']));
  185. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  186. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  187. ?>
  188. header = {
  189. "From" = "/(<?=$value_sane;?>)/i";
  190. }
  191. <?php
  192. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  193. ?>
  194. priority = 5;
  195. <?php
  196. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  197. ?>
  198. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  199. <?php
  200. }
  201. }
  202. else {
  203. ?>
  204. priority = 6;
  205. <?php
  206. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  207. ?>
  208. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  209. <?php
  210. }
  211. }
  212. ?>
  213. apply "default" {
  214. MAILCOW_WHITE = -999.0;
  215. }
  216. symbols [
  217. "MAILCOW_WHITE"
  218. ]
  219. }
  220. <?php
  221. }
  222. /*
  223. // Start blacklist
  224. */
  225. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'blacklist_from'");
  226. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  227. while ($row = array_shift($rows)) {
  228. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  229. ?>
  230. blacklist_<?=$username_sane;?> {
  231. <?php
  232. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(CONCAT('^', `value`, '$'), '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  233. WHERE `object`= :object
  234. AND `option` = 'blacklist_from'");
  235. $stmt->execute(array(':object' => $row['object']));
  236. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  237. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  238. ?>
  239. from = "/(<?=$value_sane;?>)/i";
  240. <?php
  241. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  242. ?>
  243. priority = 5;
  244. <?php
  245. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  246. ?>
  247. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  248. <?php
  249. }
  250. }
  251. else {
  252. ?>
  253. priority = 6;
  254. <?php
  255. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  256. ?>
  257. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  258. <?php
  259. }
  260. }
  261. ?>
  262. apply "default" {
  263. MAILCOW_BLACK = 999.0;
  264. }
  265. symbols [
  266. "MAILCOW_BLACK"
  267. ]
  268. }
  269. blacklist_header_<?=$username_sane;?> {
  270. <?php
  271. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(CONCAT('\<', `value`, '\>'), '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  272. WHERE `object`= :object
  273. AND `option` = 'blacklist_from'");
  274. $stmt->execute(array(':object' => $row['object']));
  275. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  276. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  277. ?>
  278. header = {
  279. "From" = "/(<?=$value_sane;?>)/i";
  280. }
  281. <?php
  282. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  283. ?>
  284. priority = 5;
  285. <?php
  286. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  287. ?>
  288. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  289. <?php
  290. }
  291. }
  292. else {
  293. ?>
  294. priority = 6;
  295. <?php
  296. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  297. ?>
  298. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  299. <?php
  300. }
  301. }
  302. ?>
  303. apply "default" {
  304. MAILCOW_BLACK = 999.0;
  305. }
  306. symbols [
  307. "MAILCOW_BLACK"
  308. ]
  309. }
  310. <?php
  311. }
  312. // Start additional content
  313. $stmt = $pdo->query("SELECT `id`, `content` FROM `settingsmap` WHERE `active` = '1'");
  314. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  315. while ($row = array_shift($rows)) {
  316. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['id']);
  317. ?>
  318. additional_settings_<?=intval($row['id']);?> {
  319. <?php
  320. $content = preg_split('/\r\n|\r|\n/', $row['content']);
  321. foreach ($content as $line) {
  322. echo ' ' . $line . PHP_EOL;
  323. }
  324. }
  325. ?>
  326. }