Exemplo n.º 1
0
function archive_process($file)
{
    $unix = new unix();
    $timeMessage = filemtime($file);
    $fullmessagesdir = "/opt/artica/share/www/original_messages";
    $target_file = $file;
    $filename = basename($target_file);
    if (!isset($GLOBALS["GREP"])) {
        $GLOBALS["GREP"] = $unix->find_program("grep");
    }
    if (!isset($GLOBALS["MYHOSTNAME"])) {
        $GLOBALS["MYHOSTNAME"] = $unix->hostname_g();
    }
    $grep = $GLOBALS["GREP"];
    $ARCHIVED = false;
    $MailArchiverEnabled = $GLOBALS["MailArchiverEnabled"];
    $MailArchiverToMySQL = $GLOBALS["MailArchiverToMySQL"];
    $MailArchiverToMailBox = $GLOBALS["MailArchiverToMailBox"];
    $MailArchiverMailBox = $GLOBALS["MailArchiverMailBox"];
    $MailArchiverToSMTP = $GLOBALS["MailArchiverToSMTP"];
    $MailArchiverSMTP = $GLOBALS["MailArchiverSMTP"];
    $MailArchiverSMTPINcoming = $GLOBALS["MailArchiverSMTPINcoming"];
    if (!is_numeric($MailArchiverSMTP)) {
        $MailArchiverSMTP = 0;
    }
    $realmailfrom = null;
    $realmailto = null;
    exec("{$grep} X-REAL- {$file} 2>&1", $resultsgrep);
    while (list($num, $line) = each($resultsgrep)) {
        events("[{$num}] {$line}", __LINE__);
        if (preg_match("#X-REAL-MAILFROM:\\s+<(.*?)>#", $line, $re)) {
            $realmailfrom = trim($re[1]);
            continue;
        }
        if (preg_match("#X-REAL-RCPTTO:\\s+<(.*?)>#", $line, $re)) {
            $realmailto = trim($re[1]);
            continue;
        }
        if ($realmailto == null) {
            if (preg_match("#X-REAL-RCPTTO:\\s+(.*)#", $line, $re)) {
                $realmailto = trim($re[1]);
                continue;
            }
        }
        if ($realmailfrom == null) {
            if (preg_match("#X-REAL-MAILFROM:\\s+(.*)#", $line, $re)) {
                $realmailfrom = trim($re[1]);
                continue;
            }
        }
        if (preg_match("#X-REAL-ARCHIVED#", $line, $re)) {
            events("{$file} detected as already archived...", __LINE__);
            $ARCHIVED = true;
        }
    }
    $realmailfrom = str_replace("<", "", $realmailfrom);
    $realmailfrom = str_replace(">", "", $realmailfrom);
    $realmailto = str_replace(">", "", $realmailto);
    $realmailto = str_replace("<", "", $realmailto);
    if ($GLOBALS["VERBOSE"]) {
        echo "X-REAL-MAILFROM: `{$realmailfrom}` X-REAL-RCPTTO: `{$realmailto}`\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "MailArchiverToMailBox = {$MailArchiverToMailBox};MailArchiverSMTP={$MailArchiverSMTP}; \n";
    }
    if ($MailArchiverToMailBox == 1) {
        if ($GLOBALS["VERBOSE"]) {
            echo "archive_process_smtp({$fullmessagesdir},{$realmailfrom})\n";
        }
        if (!$ARCHIVED) {
            if (!archive_process_smtp($file, $realmailfrom)) {
                return false;
            }
        }
    }
    if ($MailArchiverSMTP == 1) {
        if (!$ARCHIVED) {
            if (!archive_process_smtpsrv($file, $realmailfrom, $realmailto)) {
                return false;
            }
        }
    }
    if (!$ARCHIVED) {
        if (!archive_process_copyto($file, $realmailfrom, $realmailto)) {
            return false;
        }
    }
    if ($MailArchiverToMySQL == 0) {
        return true;
    }
    $ldap = new clladp();
    $q = new mysql_mailarchive_builder();
    events("Unpack {$target_file}");
    $mm = new demime($target_file);
    if (!$mm->unpack()) {
        events("Failed unpack with error \"{$mm->error}\"");
        if ($mm->MustkillMail) {
            @unlink($target_file);
        }
        return false;
    }
    $message_html = $mm->ExportToHtml($target_file);
    if (strlen($message_html) == 0) {
        system_admin_events("{$target_file}: HTML FAILED...", __FUNCTION__, __FILE__, __LINE__, "archive");
        return false;
    }
    if (count($mm->mailto_array) == 0) {
        if ($realmailto != null) {
            $mm->mailto_array[] = $realmailto;
        }
    }
    if (count($mm->mailto_array) == 0) {
        system_admin_events("{$target_file}: Fatal No recipients Aborting", __FUNCTION__, __FILE__, __LINE__, "archive");
        return true;
    }
    $filesize = @filesize($target_file);
    events("Message with " . count($mm->mailto_array) . " recipients html file:" . strlen($message_html) . " bytes");
    if ($realmailfrom != null) {
        $mm->mailfrom = $realmailfrom;
    }
    if (preg_match("#(.+?)@(.+)#", $mm->mailfrom, $re)) {
        $domain_from = $re[2];
    }
    $message_html = addslashes($message_html);
    $mm->message_date = date("Y-m-d H:i:s", $timeMessage);
    $tableDest = date("Ymd", $timeMessage);
    if (!$q->BuildDayTable($tableDest)) {
        system_admin_events("Fatal unable to create {$tableDest} date...", __FUNCTION__, __FILE__, __LINE__, "archive");
        return false;
    }
    $SubjectMysql = addslashes(mime_decode($mm->subject));
    while (list($num, $recipient) = each($mm->mailto_array)) {
        if (preg_match("#(.+?)@(.+)#", $recipient, $re)) {
            $recipient_domain = $re[2];
        }
        $ou = $mm->GetOuFromEmail($recipient);
        $sql_source_file = $target_file;
        events("(New message)time={$mm->message_date} message-id=<{$mm->message_id}> from=<{$mm->mailfrom}> to=<{$recipient}> size={$filesize}");
        $newmessageid = md5($mm->message_id . $recipient);
        $sqlfilesize = @filesize($target_file);
        $BinMessg = addslashes(fread(fopen($target_file, "r"), $sqlfilesize));
        $sql = "INSERT IGNORE INTO `{$tableDest}` (\n\t\t\t\tMessageID,\n\t\t\t\tzDate,\n\t\t\t\tmailfrom,\n\t\t\t\tmailfrom_domain,\n\t\t\t\tsubject,\n\t\t\t\tMessageBody,\n\t\t\t\torganization,\n\t\t\t\tmailto,\n\t\t\t\tfile_path,\n\t\t\t\toriginal_messageid,\n\t\t\t\tmessage_size,\n\t\t\t\tBinMessg,filename,filesize\n\t\t\t\t)\n\t\t\tVALUES(\n\t\t\t\t'{$newmessageid}',\n\t\t\t\t'{$mm->message_date}',\n\t\t\t\t'{$mm->mailfrom}',\n\t\t\t\t'{$domain_from}',\n\t\t\t\t'{$SubjectMysql}',\n\t\t\t\t'{$message_html}',\n\t\t\t\t'{$ou}',\n\t\t\t\t'{$recipient}',\n\t\t\t\t'{$sql_source_file}',\n\t\t\t\t'{$mm->message_id}',\n\t\t\t\t'{$filesize}','{$BinMessg}','{$filename}','{$sqlfilesize}')";
        if (!$q->QUERY_SQL($sql)) {
            system_admin_events("Fatal {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "archive");
            return false;
        }
    }
    events("Analyze sender {$mm->mailfrom}...");
    $ou = $mm->GetOuFromEmail($mm->mailfrom);
    if ($ou == null) {
        events("Not organization found for {$mm->mailfrom}...");
        return true;
    }
    $recipients = $mm->mailto_array;
    $impled_rctp = implode(";", $recipients);
    $sql = "INSERT IGNORE INTO `{$tableDest}` (\n\t\t\t\tMessageID,\n\t\t\t\tzDate,\n\t\t\t\tmailfrom,\n\t\t\t\tmailfrom_domain,\n\t\t\t\tsubject,\n\t\t\t\tMessageBody,\n\t\t\t\torganization,\n\t\t\t\tmailto,\n\t\t\t\tfile_path,\n\t\t\t\toriginal_messageid,\n\t\t\t\tmessage_size,BinMessg,filename,filesize\n\t\t\t\t)\n\t\t\tVALUES(\n\t\t\t\t'{$newmessageid}',\n\t\t\t\t'{$mm->message_date}',\n\t\t\t\t'{$mm->mailfrom}',\n\t\t\t\t'{$domain_from}',\n\t\t\t\t'{$SubjectMysql}',\n\t\t\t\t'{$message_html}',\n\t\t\t\t'{$ou}',\n\t\t\t\t'{$impled_rctp}',\n\t\t\t\t'{$sql_source_file}',\n\t\t\t\t'{$mm->message_id}',\n\t\t\t\t'{$filesize}','{$BinMessg}','{$filename}','{$sqlfilesize}')";
    $q->QUERY_SQL($sql);
    if (!$q->ok) {
        system_admin_events("Fatal {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "archive");
        return false;
    }
    WriteToSyslogMail("{$mm->message_id}: <{$mm->mailfrom}> to: <{$impled_rctp}> size={$filesize} bytes (saved into backup area)", __FILE__);
    events("time={$mm->message_date} message-id=<{$mm->message_id}> from=<{$mm->mailfrom}> to=<{$impled_rctp}> size={$filesize}");
    return true;
}
Exemplo n.º 2
0
function quarantine_process($file)
{
    if (!is_numeric($GLOBALS["ArticaMetaEnabled"])) {
        $sock = new sockets();
        $ArticaMetaEnabled = $sock->GET_INFO("ArticaMetaEnabled");
        if ($ArticaMetaEnabled == null) {
            $ArticaMetaEnabled = 0;
        }
        $GLOBALS["ArticaMetaEnabled"] = $ArticaMetaEnabled;
        $meta = new artica_meta();
        $GLOBALS["UUID"] = $meta->uuid;
        $GLOBALS["SERIAL"] = $meta->serial;
    }
    $fullmessagesdir = "/opt/artica/share/www/original_messages";
    $target_file = $file . ".eml";
    $decompress = true;
    if (preg_match("#\\.gz\$#", $file)) {
        events("gunzip compressed, decompressing {$file} to {$target_file}");
        @mkdir("/tmp/amavis-quar");
        $cmd = "/bin/gunzip -d -c \"{$file}\" >{$target_file} 2>&1";
        $values = system($cmd);
        if (!is_file($target_file)) {
            events("Failed decompress {$file} \"{$values}\"");
            return false;
        }
    } else {
        $target_file = $file;
        $decompress = false;
    }
    $ldap = new clladp();
    $q = new mysql();
    events("Unpack {$target_file} " . @filesize($target_file) . " bytes");
    $mm = new demime($target_file);
    if (!$mm->unpack()) {
        events("Failed unpack with error \"{$mm->error}\"");
        @unlink($target_file);
        return false;
    }
    $message_html = $mm->ExportToHtml($target_file);
    if (strlen($message_html) == 0) {
        if ($decompress) {
            @unlink($target_file);
        }
        return false;
    }
    if (count($mm->mailto_array) == 0) {
        events("No recipients Aborting");
        if ($decompress) {
            @unlink($target_file);
        }
        return true;
    }
    $filesize = filesize($target_file);
    events("Message with " . count($mm->mailto_array) . " recipients html file:" . strlen($message_html) . " bytes");
    if (preg_match("#(.+?)@(.+)#", $mm->mailfrom, $re)) {
        $domain_from = $re[2];
    }
    $message_html = addslashes($message_html);
    $filename = basename($target_file);
    $newmessageid = md5($mm->message_id . $recipient);
    $sqlfilesize = @filesize($target_file);
    if ($sqlfilesize == 0) {
        events("error \"{$target_file}\" filesize=0!!!");
        WriteToSyslogMail("message-id=<{$mm->message_id}> from=<{$mm->mailfrom}> to=<{$impled_rctp}> size={$filesize} filesize error", __FILE__, true);
        return false;
    }
    $BinMessg = addslashes(fread(fopen($target_file, "r"), $sqlfilesize));
    if (strlen($BinMessg) == 0) {
        events("error \"{$target_file}\" BinMessg=0!!!");
        WriteToSyslogMail("message-id=<{$mm->message_id}> from=<{$mm->mailfrom}> to=<{$impled_rctp}> size={$filesize} filesize error", __FILE__, true);
        return false;
    }
    if (count($mm->mailto_array) == 0) {
        WriteToSyslogMail("message-id=<{$mm->message_id}> from=<{$mm->mailfrom}> size={$filesize} recipient error", __FILE__, true);
        return false;
    }
    reset($mm->mailto_array);
    while (list($num, $recipient) = each($mm->mailto_array)) {
        if (preg_match("#(.+?)@(.+)#", $recipient, $re)) {
            $recipient_domain = $re[2];
        }
        $ou = $mm->GetOuFromEmail($recipient);
        if ($ou == null) {
            events("Failed to get organization name from  \"{$recipient_domain}\"");
        }
        $sql = "INSERT IGNORE INTO quarantine (\n\t\t\t\tMessageID,\n\t\t\t\tzDate,\n\t\t\t\tmailfrom,\n\t\t\t\tmailfrom_domain,\n\t\t\t\tsubject,\n\t\t\t\tMessageBody,\n\t\t\t\torganization,\n\t\t\t\tmailto,\n\t\t\t\toriginal_messageid,\n\t\t\t\tmessage_size,BinMessg,filename,filesize\n\t\t\t\t)\n\t\t\tVALUES(\n\t\t\t\t'{$newmessageid}',\n\t\t\t\t'{$mm->message_date}',\n\t\t\t\t'{$mm->mailfrom}',\n\t\t\t\t'{$domain_from}',\n\t\t\t\t'{$mm->subject}',\n\t\t\t\t'{$message_html}',\n\t\t\t\t'{$ou}',\n\t\t\t\t'{$recipient}',\n\t\t\t\t'{$mm->message_id}',\n\t\t\t\t'{$filesize}','{$BinMessg}','{$filename}','{$sqlfilesize}')";
        if (!$q->QUERY_SQL($sql, "artica_backup")) {
            events($q->mysql_error);
            file_put_contents("/var/log/artica-postfix/mysql-error." . md5($sql) . ".err", "{$sql}\n\n{$q->mysql_error}");
            events("error saved into  /var/log/artica-postfix/mysql-error." . md5($sql) . ".err");
            if ($decompress) {
                @unlink($target_file);
            }
            return false;
        } else {
            if ($GLOBALS["ArticaMetaEnabled"] == 1) {
                if ($GLOBALS["SERIAL"] != null) {
                    $md5 = md5("{$newmessageid}{$mm->message_date}");
                    $sqlmeta = "('{$GLOBALS["UUID"]}','{$GLOBALS["SERIAL"]}','{$newmessageid}','{$mm->message_date}',";
                    $sqlmeta = $sqlmeta . "'{$mm->mailfrom}','{$domain_from}','{$recipient}','{$mm->subject}','{$ou}','{$mm->message_id}',";
                    $sqlmeta = $sqlmeta . "'{$filesize}','{$sqlfilesize}')";
                    @file_put_contents("/var/log/artica-postfix/artica-meta-msgs/{$md5}.quar", $sqlmeta);
                }
            }
            WriteToSyslogMail("{$mm->message_id}: <{$mm->mailfrom}> to: <{$recipient}> size={$filesize} bytes (saved into quarantine area)", __FILE__);
            events("time={$mm->message_date} message-id=<{$mm->message_id}> from=<{$mm->mailfrom}> to=<{$recipient}> size={$filesize}");
        }
    }
    if ($decompress) {
        @unlink($target_file);
    }
    return true;
}
Exemplo n.º 3
0
function archive_process($file)
{
    $fullmessagesdir = "/opt/artica/share/www/original_messages";
    $target_file = $file;
    $filename = basename($target_file);
    $ldap = new clladp();
    $q = new mysql();
    events("Unpack {$target_file}");
    $mm = new demime($target_file);
    if (!$mm->unpack()) {
        events("Failed unpack with error \"{$mm->error}\"");
        if ($mm->MustkillMail) {
            @unlink($target_file);
        }
        return false;
    }
    $message_html = $mm->ExportToHtml($target_file);
    if (strlen($message_html) == 0) {
        return false;
    }
    if (count($mm->mailto_array) == 0) {
        events("No recipients Aborting");
        return true;
    }
    $filesize = filesize($target_file);
    events("Message with " . count($mm->mailto_array) . " recipients html file:" . strlen($message_html) . " bytes");
    if (preg_match("#(.+?)@(.+)#", $mm->mailfrom, $re)) {
        $domain_from = $re[2];
    }
    $message_html = addslashes($message_html);
    while (list($num, $recipient) = each($mm->mailto_array)) {
        if (preg_match("#(.+?)@(.+)#", $recipient, $re)) {
            $recipient_domain = $re[2];
        }
        $ou = $mm->GetOuFromEmail($recipient);
        $sql_source_file = $target_file;
        events("(New message)time={$mm->message_date} message-id=<{$mm->message_id}> from=<{$mm->mailfrom}> to=<{$recipient}> size={$filesize}");
        $newmessageid = md5($mm->message_id . $recipient);
        $sqlfilesize = @filesize($target_file);
        $BinMessg = addslashes(fread(fopen($target_file, "r"), $sqlfilesize));
        $sql = "INSERT IGNORE INTO storage (\n\t\t\t\tMessageID,\n\t\t\t\tzDate,\n\t\t\t\tmailfrom,\n\t\t\t\tmailfrom_domain,\n\t\t\t\tsubject,\n\t\t\t\tMessageBody,\n\t\t\t\torganization,\n\t\t\t\tmailto,\n\t\t\t\tfile_path,\n\t\t\t\toriginal_messageid,\n\t\t\t\tmessage_size,\n\t\t\t\tBinMessg,filename,filesize\n\t\t\t\t)\n\t\t\tVALUES(\n\t\t\t\t'{$newmessageid}',\n\t\t\t\t'{$mm->message_date}',\n\t\t\t\t'{$mm->mailfrom}',\n\t\t\t\t'{$domain_from}',\n\t\t\t\t'{$mm->subject}',\n\t\t\t\t'{$message_html}',\n\t\t\t\t'{$ou}',\n\t\t\t\t'{$recipient}',\n\t\t\t\t'{$sql_source_file}',\n\t\t\t\t'{$mm->message_id}',\n\t\t\t\t'{$filesize}','{$BinMessg}','{$filename}','{$sqlfilesize}')";
        if (!$q->QUERY_SQL($sql, "artica_backup")) {
            events($q->mysql_error);
            file_put_contents("/var/log/artica-postfix/mysql-error." . md5($sql) . ".err", "{$sql}\n\n{$q->mysql_error}");
            events("error saved into  /var/log/artica-postfix/mysql-error." . md5($sql) . ".err");
            return false;
        } else {
            events("Success saved in mysql...");
        }
    }
    events("Analyze sender {$mm->mailfrom}...");
    $ou = $mm->GetOuFromEmail($mm->mailfrom);
    if ($ou == null) {
        events("Not organization found for {$mm->mailfrom}...");
        return true;
    }
    $recipients = $mm->mailto_array;
    $impled_rctp = implode(";", $recipients);
    $sql = "INSERT IGNORE INTO storage (\n\t\t\t\tMessageID,\n\t\t\t\tzDate,\n\t\t\t\tmailfrom,\n\t\t\t\tmailfrom_domain,\n\t\t\t\tsubject,\n\t\t\t\tMessageBody,\n\t\t\t\torganization,\n\t\t\t\tmailto,\n\t\t\t\tfile_path,\n\t\t\t\toriginal_messageid,\n\t\t\t\tmessage_size,BinMessg,filename,filesize\n\t\t\t\t)\n\t\t\tVALUES(\n\t\t\t\t'{$newmessageid}',\n\t\t\t\t'{$mm->message_date}',\n\t\t\t\t'{$mm->mailfrom}',\n\t\t\t\t'{$domain_from}',\n\t\t\t\t'{$mm->subject}',\n\t\t\t\t'{$message_html}',\n\t\t\t\t'{$ou}',\n\t\t\t\t'{$impled_rctp}',\n\t\t\t\t'{$sql_source_file}',\n\t\t\t\t'{$mm->message_id}',\n\t\t\t\t'{$filesize}','{$BinMessg}','{$filename}','{$sqlfilesize}')";
    if (!$q->QUERY_SQL($sql, "artica_backup")) {
        events($q->mysql_error);
        file_put_contents("/var/log/artica-postfix/mysql-error." . md5($sql) . ".err", "{$sql}\n\n{$q->mysql_error}");
        WriteToSyslogMail("error saved into  /var/log/artica-postfix/mysql-error." . md5($sql) . ".err", __FILE__);
        return false;
    }
    WriteToSyslogMail("{$mm->message_id}: <{$mm->mailfrom}> to: <{$impled_rctp}> size={$filesize} bytes (saved into backup area)", __FILE__);
    events("time={$mm->message_date} message-id=<{$mm->message_id}> from=<{$mm->mailfrom}> to=<{$impled_rctp}> size={$filesize}");
    return true;
}