2017-12-09 16:08:18 +08:00
|
|
|
<?php
|
|
|
|
// File size is limited by Nginx site to 10M
|
|
|
|
// To speed things up, we do not include prerequisites
|
|
|
|
header('Content-Type: text/plain');
|
|
|
|
require_once "vars.inc.php";
|
|
|
|
// Do not show errors, we log to using error_log
|
|
|
|
ini_set('error_reporting', 0);
|
|
|
|
// Init database
|
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-30 04:01:23 +08:00
|
|
|
//$dsn = $database_type . ':host=' . $database_host . ';dbname=' . $database_name;
|
|
|
|
$dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
|
2017-12-09 16:08:18 +08:00
|
|
|
$opt = [
|
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
|
|
];
|
|
|
|
try {
|
|
|
|
$pdo = new PDO($dsn, $database_user, $database_pass, $opt);
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("QUARANTINE: " . $e . PHP_EOL);
|
2017-12-09 16:08:18 +08:00
|
|
|
http_response_code(501);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
// Init Redis
|
|
|
|
$redis = new Redis();
|
|
|
|
$redis->connect('redis-mailcow', 6379);
|
|
|
|
|
|
|
|
// Functions
|
|
|
|
function parse_email($email) {
|
|
|
|
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
|
|
|
|
$a = strrpos($email, '@');
|
|
|
|
return array('local' => substr($email, 0, $a), 'domain' => substr(substr($email, $a), 1));
|
|
|
|
}
|
|
|
|
if (!function_exists('getallheaders')) {
|
|
|
|
function getallheaders() {
|
|
|
|
if (!is_array($_SERVER)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$headers = array();
|
|
|
|
foreach ($_SERVER as $name => $value) {
|
|
|
|
if (substr($name, 0, 5) == 'HTTP_') {
|
|
|
|
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 19:51:55 +08:00
|
|
|
$raw_data_content = file_get_contents('php://input');
|
|
|
|
$raw_data = mb_convert_encoding($raw_data_content, 'HTML-ENTITIES', "UTF-8");
|
2017-12-09 16:08:18 +08:00
|
|
|
$headers = getallheaders();
|
|
|
|
|
|
|
|
$qid = $headers['X-Rspamd-Qid'];
|
2019-01-18 05:00:18 +08:00
|
|
|
$subject = $headers['X-Rspamd-Subject'];
|
2017-12-09 16:08:18 +08:00
|
|
|
$score = $headers['X-Rspamd-Score'];
|
|
|
|
$rcpts = $headers['X-Rspamd-Rcpt'];
|
|
|
|
$user = $headers['X-Rspamd-User'];
|
|
|
|
$ip = $headers['X-Rspamd-Ip'];
|
|
|
|
$action = $headers['X-Rspamd-Action'];
|
|
|
|
$sender = $headers['X-Rspamd-From'];
|
|
|
|
$symbols = $headers['X-Rspamd-Symbols'];
|
|
|
|
|
|
|
|
$raw_size = (int)$_SERVER['CONTENT_LENGTH'];
|
|
|
|
|
2020-03-04 02:10:28 +08:00
|
|
|
if (empty($sender)) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("QUARANTINE: Unknown sender, assuming empty-env-from@localhost" . PHP_EOL);
|
2020-04-09 03:55:17 +08:00
|
|
|
$sender = 'empty-env-from@localhost';
|
2020-03-04 02:10:28 +08:00
|
|
|
}
|
|
|
|
|
2017-12-09 16:08:18 +08:00
|
|
|
try {
|
2018-10-11 17:55:52 +08:00
|
|
|
$max_size = (int)$redis->Get('Q_MAX_SIZE');
|
|
|
|
if (($max_size * 1048576) < $raw_size) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log(sprintf("QUARANTINE: Message too large: %d b exceeds %d b", $raw_size, ($max_size * 1048576)) . PHP_EOL);
|
2018-10-11 17:55:52 +08:00
|
|
|
http_response_code(505);
|
|
|
|
exit;
|
2017-12-09 16:08:18 +08:00
|
|
|
}
|
|
|
|
if ($exclude_domains = $redis->Get('Q_EXCLUDE_DOMAINS')) {
|
|
|
|
$exclude_domains = json_decode($exclude_domains, true);
|
|
|
|
}
|
|
|
|
$retention_size = (int)$redis->Get('Q_RETENTION_SIZE');
|
|
|
|
}
|
|
|
|
catch (RedisException $e) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("QUARANTINE: " . $e . PHP_EOL);
|
2017-12-09 16:08:18 +08:00
|
|
|
http_response_code(504);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-01-17 22:22:11 +08:00
|
|
|
$rcpt_final_mailboxes = array();
|
|
|
|
|
|
|
|
// Loop through all rcpts
|
2017-12-09 16:08:18 +08:00
|
|
|
foreach (json_decode($rcpts, true) as $rcpt) {
|
2019-03-11 22:29:30 +08:00
|
|
|
// Remove tag
|
|
|
|
$rcpt = preg_replace('/^(.*?)\+.*(@.*)$/', '$1$2', $rcpt);
|
|
|
|
|
2018-01-17 22:22:11 +08:00
|
|
|
// Break rcpt into local part and domain part
|
|
|
|
$parsed_rcpt = parse_email($rcpt);
|
|
|
|
|
|
|
|
// Skip if not a mailcow handled domain
|
|
|
|
try {
|
|
|
|
if (!$redis->hGet('DOMAIN_MAP', $parsed_rcpt['domain'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (RedisException $e) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("QUARANTINE: " . $e . PHP_EOL);
|
2018-01-17 22:22:11 +08:00
|
|
|
http_response_code(504);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if domain is excluded
|
|
|
|
if (in_array($parsed_rcpt['domain'], $exclude_domains)) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log(sprintf("QUARANTINE: Skipped domain %s", $parsed_rcpt['domain']) . PHP_EOL);
|
2017-12-09 16:08:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-17 22:22:11 +08:00
|
|
|
|
|
|
|
// Always assume rcpt is not a final mailbox but an alias for a mailbox or further aliases
|
|
|
|
//
|
|
|
|
// rcpt
|
|
|
|
// |
|
|
|
|
// mailbox <-- goto ---> alias1, alias2, mailbox2
|
|
|
|
// | |
|
|
|
|
// mailbox3 |
|
|
|
|
// |
|
|
|
|
// alias3 ---> mailbox4
|
|
|
|
//
|
2017-12-09 16:08:18 +08:00
|
|
|
try {
|
2020-09-27 03:58:28 +08:00
|
|
|
$stmt = $pdo->prepare("SELECT `goto` FROM `alias` WHERE `address` = :rcpt AND `active` = '1'");
|
2017-12-09 16:08:18 +08:00
|
|
|
$stmt->execute(array(
|
2018-01-17 22:22:11 +08:00
|
|
|
':rcpt' => $rcpt
|
2017-12-09 16:08:18 +08:00
|
|
|
));
|
|
|
|
$gotos = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
|
2018-01-17 22:22:11 +08:00
|
|
|
if (empty($gotos)) {
|
2020-09-27 03:58:28 +08:00
|
|
|
$stmt = $pdo->prepare("SELECT `goto` FROM `alias` WHERE `address` = :rcpt AND `active` = '1'");
|
2018-01-17 22:22:11 +08:00
|
|
|
$stmt->execute(array(
|
|
|
|
':rcpt' => '@' . $parsed_rcpt['domain']
|
|
|
|
));
|
|
|
|
$gotos = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
|
|
|
|
}
|
2019-05-01 06:56:12 +08:00
|
|
|
if (empty($gotos)) {
|
|
|
|
$stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :rcpt AND `active` = '1'");
|
|
|
|
$stmt->execute(array(':rcpt' => $parsed_rcpt['domain']));
|
|
|
|
$goto_branch = $stmt->fetch(PDO::FETCH_ASSOC)['target_domain'];
|
|
|
|
if ($goto_branch) {
|
|
|
|
$gotos = $parsed_rcpt['local'] . '@' . $goto_branch;
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 22:22:11 +08:00
|
|
|
$gotos_array = explode(',', $gotos);
|
|
|
|
|
|
|
|
$loop_c = 0;
|
|
|
|
|
|
|
|
while (count($gotos_array) != 0 && $loop_c <= 20) {
|
|
|
|
|
|
|
|
// Loop through all found gotos
|
|
|
|
foreach ($gotos_array as $index => &$goto) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("RCPT RESOVLER: http pipe: query " . $goto . " as username from mailbox" . PHP_EOL);
|
2020-09-27 03:58:49 +08:00
|
|
|
$stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `username` = :goto AND (`active`= '1' OR `active`= '2');");
|
2018-01-17 22:22:11 +08:00
|
|
|
$stmt->execute(array(':goto' => $goto));
|
|
|
|
$username = $stmt->fetch(PDO::FETCH_ASSOC)['username'];
|
|
|
|
if (!empty($username)) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("RCPT RESOVLER: http pipe: mailbox found: " . $username . PHP_EOL);
|
2018-01-17 22:22:11 +08:00
|
|
|
// Current goto is a mailbox, save to rcpt_final_mailboxes if not a duplicate
|
|
|
|
if (!in_array($username, $rcpt_final_mailboxes)) {
|
|
|
|
$rcpt_final_mailboxes[] = $username;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$parsed_goto = parse_email($goto);
|
|
|
|
if (!$redis->hGet('DOMAIN_MAP', $parsed_goto['domain'])) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("RCPT RESOVLER:" . $goto . " is not a mailcow handled mailbox or alias address" . PHP_EOL);
|
2018-01-17 22:22:11 +08:00
|
|
|
}
|
|
|
|
else {
|
2020-09-27 03:58:28 +08:00
|
|
|
$stmt = $pdo->prepare("SELECT `goto` FROM `alias` WHERE `address` = :goto AND `active` = '1'");
|
2018-01-17 22:22:11 +08:00
|
|
|
$stmt->execute(array(':goto' => $goto));
|
|
|
|
$goto_branch = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
|
2019-05-01 06:56:12 +08:00
|
|
|
if ($goto_branch) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("RCPT RESOVLER: http pipe: goto address " . $goto . " is a alias branch for " . $goto_branch . PHP_EOL);
|
2019-05-01 06:56:12 +08:00
|
|
|
$goto_branch_array = explode(',', $goto_branch);
|
|
|
|
} else {
|
|
|
|
$stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain AND `active` AND '1'");
|
|
|
|
$stmt->execute(array(':domain' => $parsed_goto['domain']));
|
|
|
|
$goto_branch = $stmt->fetch(PDO::FETCH_ASSOC)['target_domain'];
|
|
|
|
if ($goto_branch) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("RCPT RESOVLER: http pipe: goto domain " . $parsed_gto['domain'] . " is a domain alias branch for " . $goto_branch . PHP_EOL);
|
2019-05-01 06:56:12 +08:00
|
|
|
$goto_branch_array = array($parsed_gto['local'] . '@' . $goto_branch);
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 22:22:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// goto item was processed, unset
|
|
|
|
unset($gotos_array[$index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge goto branch array derived from previous loop (if any), filter duplicates and unset goto branch array
|
|
|
|
if (!empty($goto_branch_array)) {
|
|
|
|
$gotos_array = array_unique(array_merge($gotos_array, $goto_branch_array));
|
|
|
|
unset($goto_branch_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reindex array
|
|
|
|
$gotos_array = array_values($gotos_array);
|
|
|
|
|
|
|
|
// Force exit if loop cannot be solved
|
|
|
|
// Postfix does not allow for alias loops, so this should never happen.
|
|
|
|
$loop_c++;
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("RCPT RESOVLER: http pipe: goto array count on loop #". $loop_c . " is " . count($gotos_array) . PHP_EOL);
|
2017-12-09 16:08:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("RCPT RESOVLER: " . $e->getMessage() . PHP_EOL);
|
2017-12-09 16:08:18 +08:00
|
|
|
http_response_code(502);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 22:22:11 +08:00
|
|
|
|
2020-04-11 14:02:15 +08:00
|
|
|
foreach ($rcpt_final_mailboxes as $rcpt_final) {
|
|
|
|
error_log("QUARANTINE: quarantine pipe: processing quarantine message for rcpt " . $rcpt_final . PHP_EOL);
|
2017-12-09 16:08:18 +08:00
|
|
|
try {
|
2019-01-16 17:50:34 +08:00
|
|
|
$stmt = $pdo->prepare("INSERT INTO `quarantine` (`qid`, `subject`, `score`, `sender`, `rcpt`, `symbols`, `user`, `ip`, `msg`, `action`)
|
2019-01-18 05:00:18 +08:00
|
|
|
VALUES (:qid, :subject, :score, :sender, :rcpt, :symbols, :user, :ip, :msg, :action)");
|
2017-12-09 16:08:18 +08:00
|
|
|
$stmt->execute(array(
|
|
|
|
':qid' => $qid,
|
2019-01-16 17:50:34 +08:00
|
|
|
':subject' => $subject,
|
2017-12-09 16:08:18 +08:00
|
|
|
':score' => $score,
|
|
|
|
':sender' => $sender,
|
2020-04-11 14:02:15 +08:00
|
|
|
':rcpt' => $rcpt_final,
|
2017-12-09 16:08:18 +08:00
|
|
|
':symbols' => $symbols,
|
|
|
|
':user' => $user,
|
|
|
|
':ip' => $ip,
|
|
|
|
':msg' => $raw_data,
|
|
|
|
':action' => $action
|
|
|
|
));
|
2018-02-09 03:13:36 +08:00
|
|
|
$stmt = $pdo->prepare('DELETE FROM `quarantine` WHERE `rcpt` = :rcpt AND `id` NOT IN (
|
2017-12-09 16:08:18 +08:00
|
|
|
SELECT `id`
|
|
|
|
FROM (
|
|
|
|
SELECT `id`
|
2018-02-09 03:13:36 +08:00
|
|
|
FROM `quarantine`
|
2018-01-17 22:22:11 +08:00
|
|
|
WHERE `rcpt` = :rcpt2
|
2017-12-09 16:08:18 +08:00
|
|
|
ORDER BY id DESC
|
|
|
|
LIMIT :retention_size
|
|
|
|
) x
|
|
|
|
);');
|
|
|
|
$stmt->execute(array(
|
2020-04-11 14:02:15 +08:00
|
|
|
':rcpt' => $rcpt_final,
|
|
|
|
':rcpt2' => $rcpt_final,
|
2017-12-09 16:08:18 +08:00
|
|
|
':retention_size' => $retention_size
|
|
|
|
));
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {
|
2020-04-11 02:55:49 +08:00
|
|
|
error_log("QUARANTINE: " . $e->getMessage() . PHP_EOL);
|
2017-12-09 16:08:18 +08:00
|
|
|
http_response_code(503);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|