Recent Changes - Search:

Accueil

OpenSSL

SyncML

Apache Portable Runtime

Libxml2

Net-snmp

CUrl

Boost

Perl

ZLib

Samba

VPN

Serveurs de messagerie

edit

Boost/Regex-postfix-log

Ci-dessous un exemple de code permettant l'analyse d'une log Postfix. Le but de cette analyse étant de réunir sur une seule ligne l'ensemble des informations générées par la réception d'un email. La réception d'un mail légal est la tâche la plus complexe car cela nécessite de recouper les informatioons présentes sur deux lignes, un structure temporaire est chargée de mémoriser l'information, plusieurs expressions régulières sont chargées d'analyser le type de ligne courante.


A partir de cette log postfix:

Apr 2 00:03:15 fwl postfix/smtpd[5120]: connect from emailer112-172.emv2.net[81.92.112.172]
Apr 2 00:03:21 fwl postfix/smtpd[5120]: D39F2142BF: client=emailer112-172.emv2.net[81.92.112.172]
Apr 2 00:03:22 fwl postfix/cleanup[19258]: D39F2142BF: message-id=<5389267050.1074062.1175464995060@sch1>
Apr 2 00:03:22 fwl postfix/qmgr[8659]: D39F2142BF: from=<trendcorner@fr.emv1.com>, size=9197, nrcpt=1 (queue active)
Apr 2 00:03:22 fwl postfix/smtpd[5120]: disconnect from emailer112-172.emv2.net[81.92.112.172]
Apr 2 00:03:22 fwl postfix/smtp[30707]: D39F2142BF: to=<oostan@internal.domain.com>, orig_to=<oostan@domain.com>, relay=192.168.2.254[192.168.2.254]:25, delay=6.4, delays=6.2/0.02/0/0.27, dsn=2.6.0, status=sent (250 2.6.0 <5389267050.1074062.1175464995060@sch1> Queued mail for delivery)
Apr 2 00:03:22 fwl postfix/qmgr[8659]: D39F2142BF: removed
...
Apr 2 12:16:30 fwl postfix/smtpd[20161]: connect from unknown[122.168.7.222]
Apr 2 12:16:37 fwl postfix/smtpd[20161]: NOQUEUE: reject: RCPT from unknown[122.168.7.222]: 554 5.7.1 Service unavailable; Client host [122.168.7.222] blocked using bl.spamcop.net; Blocked - see http://www.spamcop.net/bl.shtml?122.168.7.222; from=<sudhakarh@auracom.com> to=<ccinpon@internal.domain.com> proto=SMTP helo=<localhost.localdomain>
Apr 2 12:16:38 fwl postfix/smtpd[20161]: lost connection after RCPT from unknown[122.168.7.222]
Apr 2 12:16:38 fwl postfix/smtpd[20161]: disconnect from unknown[122.168.7.222]
...
Apr 2 12:46:51 fwl postfix/smtpd[30601]: connect from wx-out-0506.google.com[66.249.82.237]
Apr 2 12:46:56 fwl postfix/smtpd[30601]: NOQUEUE: reject: RCPT from wx-out-0506.google.com[66.249.82.237]: 554 5.7.1 <agrandville@gmail.com>: Sender address rejected: Access denied; from=<agdv@mydomain.com> to=<cdolfl@domain.com> proto=ESMTP helo=<wx-out-0506.google.com>
Apr 2 12:46:56 fwl postfix/smtpd[30601]: disconnect from wx-out-0506.google.com[66.249.82.237]

est générée cette log:

Apr 2 00:03:22;trendcorner@fr.emv1.com;oostan@internal.domain.com;OK
Apr 2 12:16:37;sudhakarh@auracom.com;ccinpon@internal.domain.com;bl.spamcop.net
Apr 2 12:46:56;agdv@mydomain.com;cdolfl@domain.com;BL


/*
 * Analyseur de log Postfix
 * Arnaud Grandville
 * V1.0  15/04/07
 *
 * Parcours de la log Postfix, renseignement d'une structure temporaire
 * affichage lorsque les informations sont toutes disponibles et vidage
 * de la structure lors que les informations ne sont plus utiles.
 * Les spams et autres erreurs de protocole ne nécessitent
 * pas de stockage temporaire.
 *
 */


#include <string>
#include <iostream>
#include <fstream>
#include <boost/regex.hpp>
#include <vector>

using namespace std;

typedef struct {
        string strDate;
        string strFrom;
        string strDeny;
        string strID;
} mailStruct;


typedef vector<mailStruct> mailVector;

const string DateTime("(\\w{3})  (\\d{1,2}) (\\d{2}:\\d{2}:\\d{2}) ");
const string Hostname("\\w* ");
const string smtpd("postfix\\/smtpd\\[(\\w+)\\]: ");
const string smtp("postfix\\/smtp\\[(\\w+)\\]: ");
const string qmgr("postfix\\/qmgr\\[(\\w+)\\]: (\\w{10}): from=<(.*)>,.*");
const string removed("postfix\\/qmgr\\[(\\w+)\\]: (\\w{10}): removed");
const string accept("(\\w{10}): to=<(.*)>, orig_to=<.*>,.*");
const string noqueue("NOQUEUE: .*blocked using ([\\w.]*).*from=<(.*)> to=<(.*)> .*");
const string BlackList("NOQUEUE: .*Sender address rejected\\: Access denied.*from=<(.*)> to=<(.*)> .*");


boost::regex acceptFrom(DateTime+Hostname+qmgr);
boost::regex acceptTo(DateTime+Hostname+smtp+accept);
boost::regex RBLrefused(DateTime+Hostname+smtpd+noqueue);
boost::regex senderRefused(DateTime+Hostname+smtpd+BlackList);
boost::regex endReceived(DateTime+Hostname+removed);



int GetMailPos(string strMailID,mailVector* List){
    for(unsigned int i=List->size()-1;i>=0;i--){
        if((List->at(i)).strID == strMailID)
            return i;
    }
    return -1;
}

bool isAcceptFrom(string& strLine,mailVector* List){
        boost::match_results<std::string::const_iterator> what;
        if(boost::regex_search(strLine,what,acceptFrom,boost::match_default)){
                List->resize(List->size() + 1);
                List->at(List->size() - 1).strID = what[5];
                List->at(List->size() - 1).strFrom = what[6];
                List->at(List->size() - 1).strDate = what[1]+" "+what[2]+" "+what[3];
                return true;
        }
        return false;
}

bool isAcceptTo(string& strLine,mailVector* List){
        boost::match_results<std::string::const_iterator> what;
        if(boost::regex_search(strLine,what,acceptTo,boost::match_default)){
                int iPos = GetMailPos(what[5],List);
                if(iPos>-1){
                        cout << List->at(iPos).strDate << ";" << List->at(iPos).strFrom << ";"<< what[6] << ";OK" << endl;
                }
                return true;
        }
        return false;
}


bool isBlackListed(string& strLine){
        boost::match_results<std::string::const_iterator> what;
        if(boost::regex_search(strLine,what,senderRefused,boost::match_default)){
                cout <<what[1]<< " " <<what[2]<<" " <<what[3]<<";"<<what[5]<<";"<<what[6]<<";"<<what[7]<< ";BL" << endl;
                return true;
        }
        return false;
}

bool isRefused(string& strLine){
        boost::match_results<std::string::const_iterator> what;
        if(boost::regex_search(strLine,what,RBLrefused,boost::match_default)){
                cout<<what[1]<< " " <<what[2]<<" " <<what[3]<<";"<<what[6]<<";" <<what[7]<<";"<<what[5]<< endl;

                return true;
        }
        return false;
}

bool isEndReceive(string& strLine,mailVector* List){
        boost::match_results<std::string::const_iterator> what;
        if(boost::regex_search(strLine,what,endReceived,boost::match_default)){
                mailVector::iterator i;
                int iPos = GetMailPos(what[5],List);
                if(iPos>=0){
                        i=List->begin();
                        List->erase(i+iPos);
                }

                return true;
        }
        return false;
}



int main(int argc, char* argv[])
{
        ifstream in("c://logs//posfix//maillog");
        string line;
        mailVector MailsList(0);
        int iCount=0;

        while(getline(in,line) && iCount++<5000) {
                if(not isAcceptFrom(line,&MailsList))
                        if(not isAcceptTo(line,&MailsList))
                                if(not isEndReceive(line,&MailsList))
                                        if(not isRefused(line))
                                                isBlackListed(line);
        }

        return 0;
}
 
Edit - History - Print - Recent Changes - Search
Page last modified on June 11, 2007, at 12:12 PM