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