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