aliasexp.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // File size is limited by Nginx site to 10M
  3. // To speed things up, we do not include prerequisites
  4. header('Content-Type: text/plain');
  5. require_once "vars.inc.php";
  6. // Do not show errors, we log to using error_log
  7. ini_set('error_reporting', 0);
  8. // Init database
  9. //$dsn = $database_type . ':host=' . $database_host . ';dbname=' . $database_name;
  10. $dsn = $database_type . ":unix_socket=" . $database_sock . ";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. }
  19. catch (PDOException $e) {
  20. error_log("ALIASEXP: " . $e . PHP_EOL);
  21. http_response_code(501);
  22. exit;
  23. }
  24. // Init Redis
  25. $redis = new Redis();
  26. $redis->connect('redis-mailcow', 6379);
  27. $redis->auth(getenv("REDISPASS"));
  28. function parse_email($email) {
  29. if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
  30. $a = strrpos($email, '@');
  31. return array('local' => substr($email, 0, $a), 'domain' => substr(substr($email, $a), 1));
  32. }
  33. if (!function_exists('getallheaders')) {
  34. function getallheaders() {
  35. if (!is_array($_SERVER)) {
  36. return array();
  37. }
  38. $headers = array();
  39. foreach ($_SERVER as $name => $value) {
  40. if (substr($name, 0, 5) == 'HTTP_') {
  41. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  42. }
  43. }
  44. return $headers;
  45. }
  46. }
  47. // Read headers
  48. $headers = getallheaders();
  49. // Get rcpt
  50. $rcpt = $headers['Rcpt'];
  51. // Remove tag
  52. $rcpt = preg_replace('/^(.*?)\+.*(@.*)$/', '$1$2', $rcpt);
  53. // Parse email address
  54. $parsed_rcpt = parse_email($rcpt);
  55. // Create array of final mailboxes
  56. $rcpt_final_mailboxes = array();
  57. // Skip if not a mailcow handled domain
  58. try {
  59. if (!$redis->hGet('DOMAIN_MAP', $parsed_rcpt['domain'])) {
  60. exit;
  61. }
  62. }
  63. catch (RedisException $e) {
  64. error_log("ALIASEXP: " . $e . PHP_EOL);
  65. http_response_code(504);
  66. exit;
  67. }
  68. // Always assume rcpt is not a final mailbox but an alias for a mailbox or further aliases
  69. //
  70. // rcpt
  71. // |
  72. // mailbox <-- goto ---> alias1, alias2, mailbox2
  73. // | |
  74. // mailbox3 |
  75. // |
  76. // alias3 ---> mailbox4
  77. //
  78. try {
  79. $stmt = $pdo->prepare("SELECT `goto` FROM `alias` WHERE `address` = :rcpt AND `active` = '1'");
  80. $stmt->execute(array(
  81. ':rcpt' => $rcpt
  82. ));
  83. $gotos = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
  84. if (empty($gotos)) {
  85. $stmt = $pdo->prepare("SELECT `goto` FROM `alias` WHERE `address` = :rcpt AND `active` = '1'");
  86. $stmt->execute(array(
  87. ':rcpt' => '@' . $parsed_rcpt['domain']
  88. ));
  89. $gotos = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
  90. }
  91. if (empty($gotos)) {
  92. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :rcpt AND `active` = '1'");
  93. $stmt->execute(array(':rcpt' => $parsed_rcpt['domain']));
  94. $goto_branch = $stmt->fetch(PDO::FETCH_ASSOC)['target_domain'];
  95. if ($goto_branch) {
  96. $gotos = $parsed_rcpt['local'] . '@' . $goto_branch;
  97. }
  98. }
  99. $gotos_array = explode(',', $gotos);
  100. $loop_c = 0;
  101. while (count($gotos_array) != 0 && $loop_c <= 20) {
  102. // Loop through all found gotos
  103. foreach ($gotos_array as $index => &$goto) {
  104. error_log("ALIAS EXPANDER: http pipe: query " . $goto . " as username from mailbox" . PHP_EOL);
  105. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `username` = :goto AND (`active`= '1' OR `active`= '2');");
  106. $stmt->execute(array(':goto' => $goto));
  107. $username = $stmt->fetch(PDO::FETCH_ASSOC)['username'];
  108. if (!empty($username)) {
  109. error_log("ALIAS EXPANDER: http pipe: mailbox found: " . $username . PHP_EOL);
  110. // Current goto is a mailbox, save to rcpt_final_mailboxes if not a duplicate
  111. if (!in_array($username, $rcpt_final_mailboxes)) {
  112. $rcpt_final_mailboxes[] = $username;
  113. }
  114. }
  115. else {
  116. $parsed_goto = parse_email($goto);
  117. if (!$redis->hGet('DOMAIN_MAP', $parsed_goto['domain'])) {
  118. error_log("ALIAS EXPANDER:" . $goto . " is not a mailcow handled mailbox or alias address" . PHP_EOL);
  119. }
  120. else {
  121. $stmt = $pdo->prepare("SELECT `goto` FROM `alias` WHERE `address` = :goto AND `active` = '1'");
  122. $stmt->execute(array(':goto' => $goto));
  123. $goto_branch = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
  124. if ($goto_branch) {
  125. error_log("ALIAS EXPANDER: http pipe: goto address " . $goto . " is an alias branch for " . $goto_branch . PHP_EOL);
  126. $goto_branch_array = explode(',', $goto_branch);
  127. } else {
  128. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain AND `active` AND '1'");
  129. $stmt->execute(array(':domain' => $parsed_goto['domain']));
  130. $goto_branch = $stmt->fetch(PDO::FETCH_ASSOC)['target_domain'];
  131. if ($goto_branch) {
  132. error_log("ALIAS EXPANDER: http pipe: goto domain " . $parsed_goto['domain'] . " is a domain alias branch for " . $goto_branch . PHP_EOL);
  133. $goto_branch_array = array($parsed_goto['local'] . '@' . $goto_branch);
  134. }
  135. }
  136. }
  137. }
  138. // goto item was processed, unset
  139. unset($gotos_array[$index]);
  140. }
  141. // Merge goto branch array derived from previous loop (if any), filter duplicates and unset goto branch array
  142. if (!empty($goto_branch_array)) {
  143. $gotos_array = array_unique(array_merge($gotos_array, $goto_branch_array));
  144. unset($goto_branch_array);
  145. }
  146. // Reindex array
  147. $gotos_array = array_values($gotos_array);
  148. // Force exit if loop cannot be solved
  149. // Postfix does not allow for alias loops, so this should never happen.
  150. $loop_c++;
  151. error_log("ALIAS EXPANDER: http pipe: goto array count on loop #". $loop_c . " is " . count($gotos_array) . PHP_EOL);
  152. }
  153. }
  154. catch (PDOException $e) {
  155. error_log("ALIAS EXPANDER: " . $e->getMessage() . PHP_EOL);
  156. http_response_code(502);
  157. exit;
  158. }
  159. // Does also return the mailbox name if question == answer (query == mailbox)
  160. if (count($rcpt_final_mailboxes) == 1) {
  161. error_log("ALIASEXP: direct alias " . $rcpt . " expanded to " . $rcpt_final_mailboxes[0] . PHP_EOL);
  162. echo trim($rcpt_final_mailboxes[0]);
  163. }