public
nobgit
read
NobMail
Based on mailcow: dockerized
Languages
Repository composition by tracked source files.
PHP
49%
JavaScript
35%
HTML
9%
CSS
4%
Shell
2%
Python
1%
Lua
0%
Perl
0%
Ruby
0%
SCSS
0%
Create file
Wiki Documentation
Clone
https://nobgit.com/orgs/nobgit/nobmail.git
ssh://[email protected]:2222/orgs/nobgit/nobmail.git
Trace
data/conf/rspamd/dynmaps/aliasexp.php
Trace helps you understand code history line by line. See who changed each line, when it changed, and which commit introduced it.
Author
Date
Commit
Line
Code
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
}
25
// Init Redis
26
$redis = new Redis();
27
$redis->connect('redis-mailcow', 6379);
28
$redis->auth(getenv("REDISPASS"));
30
function parse_email($email) {
31
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
32
$a = strrpos($email, '@');
33
return array('local' => substr($email, 0, $a), 'domain' => substr(substr($email, $a), 1));
34
}
35
if (!function_exists('getallheaders')) {
36
function getallheaders() {
37
if (!is_array($_SERVER)) {
38
return array();
39
}
40
$headers = array();
41
foreach ($_SERVER as $name => $value) {
42
if (substr($name, 0, 5) == 'HTTP_') {
43
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
44
}
45
}
46
return $headers;
47
}
48
}
50
// Read headers
51
$headers = getallheaders();
52
// Get rcpt
53
$rcpt = $headers['Rcpt'];
54
// Remove tag
55
$rcpt = preg_replace('/^(.*?)\+.*(@.*)$/', '$1$2', $rcpt);
56
// Parse email address
57
$parsed_rcpt = parse_email($rcpt);
58
// Create array of final mailboxes
59
$rcpt_final_mailboxes = array();
61
// Skip if not a mailcow handled domain
62
try {
63
if (!$redis->hGet('DOMAIN_MAP', $parsed_rcpt['domain'])) {
64
exit;
65
}
66
}
67
catch (RedisException $e) {
68
error_log("ALIASEXP: " . $e . PHP_EOL);
69
http_response_code(504);
70
exit;
71
}
73
// Always assume rcpt is not a final mailbox but an alias for a mailbox or further aliases
74
//
75
// rcpt
76
// |
77
// mailbox <-- goto ---> alias1, alias2, mailbox2
78
// | |
79
// mailbox3 |
80
// |
81
// alias3 ---> mailbox4
82
//
83
try {
84
$stmt = $pdo->prepare("SELECT `goto` FROM `alias` WHERE `address` = :rcpt AND `active` = '1'");
85
$stmt->execute(array(
86
':rcpt' => $rcpt
87
));
88
$gotos = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
89
if (empty($gotos)) {
90
$stmt = $pdo->prepare("SELECT `goto` FROM `alias` WHERE `address` = :rcpt AND `active` = '1'");
91
$stmt->execute(array(
92
':rcpt' => '@' . $parsed_rcpt['domain']
93
));
94
$gotos = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
95
}
96
if (empty($gotos)) {
97
$stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :rcpt AND `active` = '1'");
98
$stmt->execute(array(':rcpt' => $parsed_rcpt['domain']));
99
$goto_branch = $stmt->fetch(PDO::FETCH_ASSOC)['target_domain'];
100
if ($goto_branch) {
101
$gotos = $parsed_rcpt['local'] . '@' . $goto_branch;
102
}
103
}
104
$gotos_array = explode(',', $gotos);
106
$loop_c = 0;
108
while (count($gotos_array) != 0 && $loop_c <= 20) {
110
// Loop through all found gotos
111
foreach ($gotos_array as $index => &$goto) {
112
error_log("ALIAS EXPANDER: http pipe: query " . $goto . " as username from mailbox" . PHP_EOL);
113
$stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `username` = :goto AND (`active`= '1' OR `active`= '2');");
114
$stmt->execute(array(':goto' => $goto));
115
$username = $stmt->fetch(PDO::FETCH_ASSOC)['username'];
116
if (!empty($username)) {
117
error_log("ALIAS EXPANDER: http pipe: mailbox found: " . $username . PHP_EOL);
118
// Current goto is a mailbox, save to rcpt_final_mailboxes if not a duplicate
119
if (!in_array($username, $rcpt_final_mailboxes)) {
120
$rcpt_final_mailboxes[] = $username;
121
}
122
}
123
else {
124
$parsed_goto = parse_email($goto);
125
if (!$redis->hGet('DOMAIN_MAP', $parsed_goto['domain'])) {
126
error_log("ALIAS EXPANDER:" . $goto . " is not a mailcow handled mailbox or alias address" . PHP_EOL);
127
}
128
else {
129
$stmt = $pdo->prepare("SELECT `goto` FROM `alias` WHERE `address` = :goto AND `active` = '1'");
130
$stmt->execute(array(':goto' => $goto));
131
$goto_branch = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
132
if ($goto_branch) {
133
error_log("ALIAS EXPANDER: http pipe: goto address " . $goto . " is an alias branch for " . $goto_branch . PHP_EOL);
134
$goto_branch_array = explode(',', $goto_branch);
135
} else {
136
$stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain AND `active` = '1'");
137
$stmt->execute(array(':domain' => $parsed_goto['domain']));
138
$goto_branch = $stmt->fetch(PDO::FETCH_ASSOC)['target_domain'];
139
if ($goto_branch) {
140
error_log("ALIAS EXPANDER: http pipe: goto domain " . $parsed_goto['domain'] . " is a domain alias branch for " . $goto_branch . PHP_EOL);
141
$goto_branch_array = array($parsed_goto['local'] . '@' . $goto_branch);
142
}
143
}
144
}
145
}
146
// goto item was processed, unset
147
unset($gotos_array[$index]);
148
}
150
// Merge goto branch array derived from previous loop (if any), filter duplicates and unset goto branch array
151
if (!empty($goto_branch_array)) {
152
$gotos_array = array_unique(array_merge($gotos_array, $goto_branch_array));
153
unset($goto_branch_array);
154
}
156
// Reindex array
157
$gotos_array = array_values($gotos_array);
159
// Force exit if loop cannot be solved
160
// Postfix does not allow for alias loops, so this should never happen.
161
$loop_c++;
162
error_log("ALIAS EXPANDER: http pipe: goto array count on loop #". $loop_c . " is " . count($gotos_array) . PHP_EOL);
163
}
164
}
165
catch (PDOException $e) {
166
error_log("ALIAS EXPANDER: " . $e->getMessage() . PHP_EOL);
167
http_response_code(502);
168
exit;
169
}
171
// Does also return the mailbox name if question == answer (query == mailbox)
172
if (count($rcpt_final_mailboxes) == 1) {
173
error_log("ALIASEXP: direct alias " . $rcpt . " expanded to " . $rcpt_final_mailboxes[0] . PHP_EOL);
174
echo trim($rcpt_final_mailboxes[0]);
175
}