X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=pmg-log-tracker.c;h=b4ba612944522c234d40e859ae00a76553282764;hb=ee33f650050de386def8cf3066e9f243c9d605f7;hp=5805ceb34fbedc366693045e88e1d4dea723fbf4;hpb=87f77e910c79348acad9e9597812c4b4224df79b;p=pmg-log-tracker.git diff --git a/pmg-log-tracker.c b/pmg-log-tracker.c index 5805ceb..b4ba612 100644 --- a/pmg-log-tracker.c +++ b/pmg-log-tracker.c @@ -34,11 +34,12 @@ #include #include #include +#include #include #include /* - We assume the syslog files belong to one host, i.e. we do not + We assume the syslog files belong to one host, i.e. we do not consider the hostname at all */ @@ -58,7 +59,7 @@ #define EPOOL_BLOCK_SIZE 2048 #define EPOOL_MAX_SIZE 128 #define MAX_LOGFILES 32 -//#define EPOOL_DEBUG +//#define EPOOL_DEBUG //#define DEBUG typedef struct _SList SList; @@ -165,7 +166,7 @@ struct _LogList { LogEntry *log; LogEntry *logs_last; // pointer to last log (speedup add log) }; - + // SEntry: Store SMTPD related logs struct _SEntry { @@ -217,7 +218,7 @@ struct _QEntry { }; // FEntry: Store filter (proxprox) related logs - + struct _FEntry { EPool ep; @@ -252,7 +253,7 @@ void sentry_ref_add (SEntry *sentry, QEntry *qentry); int sentry_ref_del (SEntry *sentry, QEntry *qentry); void sentry_ref_finalize (LParser *parser, SEntry *sentry); int sentry_ref_rem_unneeded (LParser *parser, SEntry *sentry); -void sentry_nqlist_add (SEntry *sentry, time_t ltime, const char *from, int from_len, +void sentry_nqlist_add (SEntry *sentry, time_t ltime, const char *from, int from_len, const char *to, int to_len, char dstatus); void sentry_print (LParser *parser, SEntry *sentry); void sentry_set_connect (SEntry *sentry, const char *connect, int len); @@ -262,15 +263,15 @@ void sentry_cleanup_hash (gpointer key, gpointer value, gpointer user_data) QEntry *qentry_new (const char *qid); -QEntry *qentry_get (LParser *parser, const char *qid); -void qentry_tolist_add (QEntry *qentry, time_t ltime, char dstatus, const char *to, +QEntry *qentry_get (LParser *parser, const char *qid); +void qentry_tolist_add (QEntry *qentry, time_t ltime, char dstatus, const char *to, int to_len, const char *relay, int relay_len); void qentry_set_from (QEntry *qentry, const char *from, int len); void qentry_set_msgid (QEntry *qentry, const char *msgid, int len); void qentry_set_client (QEntry *qentry, const char *client, int len); void qentry_print (LParser *parser, QEntry *qentry); void qentry_finalize (LParser *parser, QEntry *qentry); -void qentry_free_noremove (LParser *parser, QEntry *qentry); +void qentry_free_noremove (QEntry *qentry); void qentry_free (LParser *parser, QEntry *qentry); void qentry_cleanup_hash (gpointer key, gpointer value, gpointer user_data); @@ -279,7 +280,7 @@ FEntry *fentry_new (const char *logid); FEntry *fentry_get (LParser *parser, const char *logid); void fentry_tolist_add (FEntry *fentry, char dstatus, const char *to, int to_len, const char *qid, int qid_len); -void fentry_free_noremove (LParser *parser, FEntry *fentry); +void fentry_free_noremove (FEntry *fentry); void fentry_free (LParser *parser, FEntry *fentry); void fentry_cleanup_hash (gpointer key, gpointer value, gpointer user_data); @@ -290,6 +291,18 @@ void parser_free (LParser *parser); // Implementations +// Checksum Macros +#define PROXPROX 0xE0E4DEF0 +#define PMG_SMTP_FILTER 0x0A85A6B7 +#define POSTFIX_POSTSCREEN 0xD17E2019 +#define POSTFIX_QMGR 0x48465316 +#define POSTFIX_SMTP 0x4A466014 +#define POSTFIX_LMTP 0x43466014 +#define POSTFIX_LOCAL 0x484F05AF +#define POSTFIX_ERROR 0x4B5E13AE +#define POSTFIX_SMTPD 0x466014AE +#define POSTFIX_CLEANUP 0x05A8BAC1 + //#define LOGPATH "./log/" #define LOGPATH "/var/log/" //#define LOGPATH "/root/testlog/" @@ -333,8 +346,8 @@ void debug_error (char *msg, const char *line) { #ifdef DEBUG - fprintf (stderr, "ERROR: %s\n", msg); - if (line) fprintf (stderr, "LINE: %s\n", line); + fprintf (stderr, "ERROR: %s\n", msg); + if (line) fprintf (stderr, "LINE: %s\n", line); G_BREAKPOINT(); @@ -368,7 +381,7 @@ epool_init (EPool *ep) } void -epool_free (EPool *ep) +epool_free (EPool *ep) { SList *l; gpointer data; @@ -381,7 +394,7 @@ epool_free (EPool *ep) #ifdef DEBUG return; #endif - + l = ep->mblocks; while (l) { data = l->data; @@ -407,7 +420,7 @@ epool_alloc (EPool *ep, int size) if (size > EPOOL_MAX_SIZE) { SList *blocks; if (space >= sizeof (SList)) { - blocks = ep->blocks->data + ep->cpos; + blocks = (SList *)((char *)ep->blocks->data + ep->cpos); ep->cpos += sizeof (SList); } else { blocks = (SList *)epool_alloc (ep, sizeof (SList)); @@ -423,17 +436,17 @@ epool_alloc (EPool *ep, int size) blocks->next = ep->mblocks; ep->mblocks = blocks; - + return data; } else if (space >= rs) { - data = ep->blocks->data + ep->cpos; + data = (char *)ep->blocks->data + ep->cpos; ep->cpos += rs; return data; } else { - SList *blocks = ep->blocks->data + ep->cpos; + SList *blocks = (SList *)((char *)ep->blocks->data + ep->cpos); data = g_slice_alloc0 (EPOOL_BLOCK_SIZE); blocks->data = data; @@ -449,11 +462,11 @@ epool_alloc (EPool *ep, int size) #endif return data; - } + } } char * -epool_strndup (EPool *ep, const char *s, int len) +epool_strndup (EPool *ep, const char *s, int len) { int l = len + 1; char *res = epool_alloc (ep, l); @@ -462,7 +475,7 @@ epool_strndup (EPool *ep, const char *s, int len) } char * -epool_strndup0 (EPool *ep, const char *s, int len) +epool_strndup0 (EPool *ep, const char *s, int len) { char *res = epool_alloc (ep, len + 1); strncpy (res, s, len); @@ -472,7 +485,7 @@ epool_strndup0 (EPool *ep, const char *s, int len) } char * -epool_strdup (EPool *ep, const char *s) +epool_strdup (EPool *ep, const char *s) { int l = strlen (s) + 1; char *res = epool_alloc (ep, l); @@ -485,7 +498,7 @@ loglist_print (LogList *loglist) { LogEntry *log = loglist->log; while (log) { - printf ("L%08X %s", log->linenr, log->text); + printf ("L%08lX %s", log->linenr, log->text); log = log->next; } } @@ -498,11 +511,11 @@ loglist_add (EPool *ep, LogList *loglist, const char *text, int len, unsigned lo #ifdef DEBUG if (len != strlen (text)) { - debug_error ("string with wrong len", NULL); + debug_error ("string with wrong len", NULL); } #endif if (text[len] != 0) { - debug_error ("string is not null terminated", NULL); + debug_error ("string is not null terminated", NULL); return; } @@ -557,7 +570,7 @@ sentry_new (int pid, time_t ltime, unsigned long rel_line_nr) blocks = (SList *)((char *)sentry + cpos); cpos += sizeof (SList); - + blocks->data = sentry; blocks->next = NULL; @@ -585,13 +598,13 @@ sentry_get (LParser *parser, int pid, time_t ltime, unsigned long rel_line_nr) } void -sentry_ref_add (SEntry *sentry, QEntry *qentry) +sentry_ref_add (SEntry *sentry, QEntry *qentry) { SList *l; if (qentry->smtpd) { if (qentry->smtpd != sentry) { - debug_error ("qentry ref already set", NULL); + debug_error ("qentry ref already set", NULL); } return; } @@ -613,13 +626,13 @@ sentry_ref_add (SEntry *sentry, QEntry *qentry) } int -sentry_ref_del (SEntry *sentry, QEntry *qentry) +sentry_ref_del (SEntry *sentry, QEntry *qentry) { SList *l = sentry->refs; int count = 0; if (!qentry->smtpd) { - debug_error ("qentry does not hav a qentry ref", NULL); + debug_error ("qentry does not hav a qentry ref", NULL); return 0; } @@ -639,7 +652,7 @@ sentry_ref_del (SEntry *sentry, QEntry *qentry) } void -sentry_ref_finalize (LParser *parser, SEntry *sentry) +sentry_ref_finalize (LParser *parser, SEntry *sentry) { SList *l = sentry->refs; @@ -669,16 +682,16 @@ sentry_ref_finalize (LParser *parser, SEntry *sentry) qe->smtpd = NULL; qentry_free (parser, qe); - + if (fe) fentry_free (parser, fe); - + } if (!count) sentry_free_noremove (sentry); } int -sentry_ref_rem_unneeded (LParser *parser, SEntry *sentry) +sentry_ref_rem_unneeded (LParser *parser, SEntry *sentry) { SList *l = sentry->refs; int count = 0; @@ -703,7 +716,7 @@ sentry_ref_rem_unneeded (LParser *parser, SEntry *sentry) } void -sentry_nqlist_add (SEntry *sentry, time_t ltime, const char *from, int from_len, +sentry_nqlist_add (SEntry *sentry, time_t ltime, const char *from, int from_len, const char *to, int to_len, char dstatus) { NQList *nq = (NQList *)epool_alloc (&sentry->ep, sizeof (NQList)); @@ -748,7 +761,7 @@ sentry_print (LParser *parser, SEntry *sentry) if (!found) return; } - if (parser->from || parser->to || + if (parser->from || parser->to || parser->exclude_greylist || parser->exclude_ndrs) { nq = sentry->nqlist; int found = 0; @@ -784,7 +797,7 @@ sentry_print (LParser *parser, SEntry *sentry) printf ("CTIME: %08lX\n", parser->ctime); - if (sentry->connect) { printf ("CONNECT: %s\n", sentry->connect); } + if (sentry->connect) { printf ("CLIENT: %s\n", sentry->connect); } //printf ("EXTERNAL: %d\n", sentry->external); } @@ -827,16 +840,16 @@ sentry_set_connect (SEntry *sentry, const char *connect, int len) } void -sentry_free_noremove (SEntry *sentry) +sentry_free_noremove (SEntry *sentry) { SList *l; gpointer data; - + #ifdef EPOOL_DEBUG ep_allocated -= sentry->ep.allocated; printf ("MEM: %d\n", ep_allocated); #endif - + #ifdef DEBUG { SEntry *se; @@ -865,7 +878,7 @@ sentry_free_noremove (SEntry *sentry) } void -sentry_free (LParser *parser, SEntry *sentry) +sentry_free (LParser *parser, SEntry *sentry) { g_hash_table_remove (parser->smtpd_h, &sentry->pid); @@ -900,7 +913,7 @@ sentry_debug_alloc (gpointer key, printf ("FOUND ALLOCATED SENTRY:\n"); sentry_print (parser, se); - + exit (-1); } #endif @@ -939,7 +952,7 @@ qentry_new (const char *qid) blocks = (SList *)((char *)qentry + cpos); cpos += sizeof (SList); - + blocks->data = qentry; blocks->next = NULL; @@ -965,7 +978,7 @@ qentry_tolist_add (QEntry *qentry, time_t ltime, char dstatus, const char *to, i tl->dstatus = dstatus; tl->ltime = ltime; tl->next = qentry->tolist; - + qentry->tolist = tl; } @@ -1012,7 +1025,7 @@ qentry_set_client (QEntry *qentry, const char *client, int len) } void -qentry_print (LParser *parser, QEntry *qentry) +qentry_print (LParser *parser, QEntry *qentry) { TOList *tl, *fl; SEntry *se = qentry->smtpd; @@ -1051,7 +1064,7 @@ qentry_print (LParser *parser, QEntry *qentry) if (se && se->connect && strcasestr (se->connect, parser->server)) found = 1; if (qentry->client && strcasestr (qentry->client, parser->server)) found = 1; - if (!found) return; + if (!found) return; } if (parser->from) { @@ -1076,10 +1089,10 @@ qentry_print (LParser *parser, QEntry *qentry) } tl = tl->next; } - if (!found) return; + if (!found) return; } - if (parser->strmatch && + if (parser->strmatch && !(qentry->strmatch || (se && se->strmatch) || (fe && fe->strmatch))) return; @@ -1087,13 +1100,15 @@ qentry_print (LParser *parser, QEntry *qentry) if (parser->verbose) { printf ("QENTRY: %s\n", qentry->qid); - + printf ("CTIME: %08lX\n", parser->ctime); printf ("SIZE: %u\n", qentry->size); - if (se && se->connect) { printf ("CONNECT: %s\n", se->connect); } - - if (qentry->client) { printf ("CLIENT: %s\n", qentry->client); } + if (qentry->client) { + printf ("CLIENT: %s\n", qentry->client); + } else if (se && se->connect) { + printf ("CLIENT: %s\n", se->connect); + } if (qentry->msgid) { printf ("MSGID: %s\n", qentry->msgid); } @@ -1115,7 +1130,7 @@ qentry_print (LParser *parser, QEntry *qentry) char *to; char dstatus; char *relay; - + if (fl) { to = fl->to; dstatus = fl->dstatus; @@ -1162,7 +1177,7 @@ qentry_print (LParser *parser, QEntry *qentry) } QEntry * -qentry_get (LParser *parser, const char *qid) +qentry_get (LParser *parser, const char *qid) { QEntry *qentry; @@ -1178,7 +1193,7 @@ qentry_get (LParser *parser, const char *qid) } void -qentry_free_noremove (LParser *parser, QEntry *qentry) +qentry_free_noremove (QEntry *qentry) { SList *l; gpointer data; @@ -1186,7 +1201,7 @@ qentry_free_noremove (LParser *parser, QEntry *qentry) if ((se = qentry->smtpd)) { if (sentry_ref_del (se, qentry) == 0) { - if (se->disconnect) { + if (se->disconnect) { sentry_free_noremove (se); } } @@ -1208,7 +1223,7 @@ qentry_free_noremove (LParser *parser, QEntry *qentry) } return; #endif - + l = qentry->ep.mblocks; while (l) { data = l->data; @@ -1225,11 +1240,11 @@ qentry_free_noremove (LParser *parser, QEntry *qentry) } void -qentry_free (LParser *parser, QEntry *qentry) +qentry_free (LParser *parser, QEntry *qentry) { g_hash_table_remove (parser->qmgr_h, qentry->qid); - qentry_free_noremove (parser, qentry); + qentry_free_noremove (qentry); } void @@ -1241,7 +1256,7 @@ qentry_cleanup_hash (gpointer key, LParser *parser = (LParser *)user_data; qentry_print (parser, qe); - qentry_free_noremove (parser, qe); + qentry_free_noremove (qe); } void @@ -1297,12 +1312,12 @@ fentry_new (const char *logid) blocks = (SList *)((char *)fentry + cpos); cpos += sizeof (SList); - + blocks->data = fentry; blocks->next = NULL; fentry->logid = logid_cp = (char *)fentry + cpos; - while ((*logid_cp++ = *logid++)) cpos++; + while ((*logid_cp++ = *logid++)) cpos++; cpos = (cpos + 4) & ~3; fentry->ep.blocks = blocks; @@ -1312,7 +1327,7 @@ fentry_new (const char *logid) } FEntry * -fentry_get (LParser *parser, const char *logid) +fentry_get (LParser *parser, const char *logid) { FEntry *fentry; @@ -1342,12 +1357,12 @@ fentry_tolist_add (FEntry *fentry, char dstatus, const char *to, int to_len, } tl->dstatus = dstatus; tl->next = fentry->tolist; - + fentry->tolist = tl; } void -fentry_free_noremove (LParser *parser, FEntry *fentry) +fentry_free_noremove (FEntry *fentry) { SList *l; gpointer data; @@ -1368,7 +1383,7 @@ fentry_free_noremove (LParser *parser, FEntry *fentry) } return; #endif - + l = fentry->ep.mblocks; while (l) { data = l->data; @@ -1385,11 +1400,11 @@ fentry_free_noremove (LParser *parser, FEntry *fentry) } void -fentry_free (LParser *parser, FEntry *fentry) +fentry_free (LParser *parser, FEntry *fentry) { g_hash_table_remove (parser->filter_h, fentry->logid); - fentry_free_noremove (parser, fentry); + fentry_free_noremove (fentry); } void @@ -1398,9 +1413,8 @@ fentry_cleanup_hash (gpointer key, gpointer user_data) { FEntry *fe = value; - LParser *parser = (LParser *)user_data; - fentry_free_noremove (parser, fe); + fentry_free_noremove (fe); } // Parser @@ -1462,7 +1476,7 @@ parser_free (LParser *parser) #if 0 char * -parser_track (LParser *parser, const char *qid, gboolean insert) +parser_track (LParser *parser, const char *qid, gboolean insert) { char *res; @@ -1478,7 +1492,7 @@ parser_track (LParser *parser, const char *qid, gboolean insert) } #endif -static const int linebufsize = 8192; +#define LINEBUFSIZE 8192 static int cur_year; static int cur_month = 0; static int cal_mtod[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; @@ -1507,6 +1521,19 @@ mkgmtime (struct tm *tm) return res; } +#define JAN (('J'<<16)|('a'<<8)|'n') +#define FEB (('F'<<16)|('e'<<8)|'b') +#define MAR (('M'<<16)|('a'<<8)|'r') +#define APR (('A'<<16)|('p'<<8)|'r') +#define MAY (('M'<<16)|('a'<<8)|'y') +#define JUN (('J'<<16)|('u'<<8)|'n') +#define JUL (('J'<<16)|('u'<<8)|'l') +#define AUG (('A'<<16)|('u'<<8)|'g') +#define SEP (('S'<<16)|('e'<<8)|'p') +#define OCT (('O'<<16)|('c'<<8)|'t') +#define NOV (('N'<<16)|('o'<<8)|'v') +#define DEC (('D'<<16)|('e'<<8)|'c') + time_t parse_time (const char **text, int len) { @@ -1523,11 +1550,11 @@ parse_time (const char **text, int len) const char *line = *text; - if (len == (linebufsize - 1)) { + if (len == (LINEBUFSIZE - 1)) { debug_error ("skipping long line data", line); return 0; } - + if (len < 15) { debug_error ("skipping short line data", line); return 0; @@ -1537,25 +1564,25 @@ parse_time (const char **text, int len) int csum = (line[0]<<16) + (line[1]<<8) + line[2]; switch (csum) { - case 4874606: mon = 0; break; - case 4613474: mon = 1; break; - case 5071218: mon = 2; break; - case 4288626: mon = 3; break; - case 5071225: mon = 4; break; - case 4879726: mon = 5; break; - case 4879724: mon = 6; break; - case 4289895: mon = 7; break; - case 5465456: mon = 8; break; - case 5202804: mon = 9; break; - case 5140342: mon = 10; break; - case 4482403: mon = 11; break; - default: + case JAN: mon = 0; break; + case FEB: mon = 1; break; + case MAR: mon = 2; break; + case APR: mon = 3; break; + case MAY: mon = 4; break; + case JUN: mon = 5; break; + case JUL: mon = 6; break; + case AUG: mon = 7; break; + case SEP: mon = 8; break; + case OCT: mon = 9; break; + case NOV: mon = 10; break; + case DEC: mon = 11; break; + default: debug_error ("unable to parse month", line); - return 0; + return 0; } // year change heuristik - if (cur_month == 11 && mon == 0) { + if (cur_month == 11 && mon == 0) { year++; } if (mon > cur_month) cur_month = mon; @@ -1576,7 +1603,7 @@ parse_time (const char **text, int len) return 0; } - found = 0; while (isdigit (*cpos)) { mday = mday*10 + *cpos - 48; cpos++; found++; } + found = 0; while (isdigit (*cpos)) { mday = mday*10 + *cpos - '0'; cpos++; found++; } if (found < 1 || found > 2) { debug_error ("unable to parse day of month", line); return 0; @@ -1590,7 +1617,7 @@ parse_time (const char **text, int len) return 0; } - found = 0; while (isdigit (*cpos)) { hour = hour*10 + *cpos - 48; cpos++; found++; } + found = 0; while (isdigit (*cpos)) { hour = hour*10 + *cpos - '0'; cpos++; found++; } if (found < 1 || found > 2) { debug_error ("unable to parse hour", line); return 0; @@ -1605,7 +1632,7 @@ parse_time (const char **text, int len) } cpos++; - found = 0; while (isdigit (*cpos)) { min = min*10 + *cpos - 48; cpos++; found++; } + found = 0; while (isdigit (*cpos)) { min = min*10 + *cpos - '0'; cpos++; found++; } if (found < 1 || found > 2) { debug_error ("unable to parse minute", line); return 0; @@ -1620,7 +1647,7 @@ parse_time (const char **text, int len) } cpos++; - found = 0; while (isdigit (*cpos)) { sec = sec*10 + *cpos - 48; cpos++; found++; } + found = 0; while (isdigit (*cpos)) { sec = sec*10 + *cpos - '0'; cpos++; found++; } if (found < 1 || found > 2) { debug_error ("unable to parse second", line); return 0; @@ -1642,11 +1669,11 @@ parse_time (const char **text, int len) int -parser_count_files (LParser *parser) +parser_count_files (LParser *parser) { int i; time_t start = parser->start; - char linebuf[linebufsize]; + char linebuf[LINEBUFSIZE]; const char *line; gzFile stream; @@ -1655,7 +1682,7 @@ parser_count_files (LParser *parser) cur_month = 0; if ((stream = gzopen (logfiles[i], "r"))) { - if ((line = gzgets (stream, linebuf, linebufsize))) { + if ((line = gzgets (stream, linebuf, LINEBUFSIZE))) { if (parse_time (&line, strlen (line)) < start) { break; } @@ -1667,12 +1694,12 @@ parser_count_files (LParser *parser) return i; } } - + return i + 1; } static char * -parse_qid (const char **text, char *out, char delim, int maxlen) +parse_qid (const char **text, char *out, char delim, int maxlen) { const char *idx; char *copy = out; @@ -1681,7 +1708,7 @@ parse_qid (const char **text, char *out, char delim, int maxlen) idx = *text; while (isxdigit (*idx)) { *copy++ = *idx++; found++; if (found > maxlen) break; } - if (found > 1 && found < maxlen && + if (found > 1 && found < maxlen && ((delim && (*idx == delim)) || (!delim && isspace (*idx)))) { *copy = 0; idx++; @@ -1717,7 +1744,7 @@ print_usage (const char *name) // gzgets is ways too slow, so we do it our own way -static int +static char mygzgetc (gzFile stream) { int br; @@ -1740,7 +1767,7 @@ mygzgetc (gzFile stream) static char * mygzgets (gzFile stream, char *line, int bufsize) { - int c=0; + char c=0; char *cpos; cpos = line; @@ -1759,19 +1786,19 @@ mygzgets (gzFile stream, char *line, int bufsize) extern char *optarg; extern int optind, opterr, optopt; -int +int main (int argc, char * const argv[]) { - char linebuf[linebufsize]; + char linebuf[LINEBUFSIZE]; char *line; - char *uniqueid = NULL; + char *inputfile = NULL; const char *text; const char *idx1; const char *idx2; const char *cpos; int found = 0; - int csum_prog; + uint32_t csum_prog; unsigned long lines = 0; unsigned long rel_line_nr = 0; char qidbuf[30]; @@ -1798,7 +1825,7 @@ main (int argc, char * const argv[]) exit (-1); } - while ((opt = getopt (argc, argv, "f:t:s:e:h:m:q:x:l:I:vgn")) != -1) { + while ((opt = getopt (argc, argv, "f:t:s:e:h:m:q:x:i:l:vgn")) != -1) { if (opt == 'f') { parser->from = epool_strdup (&parser->ep, optarg); } else if (opt == 't') { @@ -1809,8 +1836,6 @@ main (int argc, char * const argv[]) parser->exclude_greylist = 1; } else if (opt == 'n') { parser->exclude_ndrs = 1; - } else if (opt == 'I') { - uniqueid = optarg; } else if (opt == 'h') { parser->server = epool_strdup (&parser->ep, optarg); } else if (opt == 'm') { @@ -1833,6 +1858,8 @@ main (int argc, char * const argv[]) } } else if (opt == 'x') { parser->strmatch = epool_strdup (&parser->ep, optarg); + } else if (opt == 'i') { + inputfile = optarg; } else if (opt == 'l') { char *l; parser->limit = strtoul (optarg, &l, 0); @@ -1912,16 +1939,14 @@ main (int argc, char * const argv[]) } int filecount; - if ((filecount = parser_count_files (parser)) <= 0) { + if (inputfile) { + filecount = 1; + } else if ((filecount = parser_count_files (parser)) <= 0) { fprintf (stderr, "unable to access log files\n"); exit (-1); } - if (uniqueid) { - printf ("# LogReader: %d %s\n", getpid(), uniqueid); - } else { - printf ("# LogReader: %d\n", getpid()); - } + printf ("# LogReader: %d\n", getpid()); printf ("# Query options\n"); if (parser->from) printf ("# Sender: %s\n", parser->from); @@ -1944,9 +1969,9 @@ main (int argc, char * const argv[]) if (parser->strmatch) printf ("# Match: %s\n", parser->strmatch); strftime (linebuf, 256, "%F %T", gmtime (&parser->start)); - printf ("# Start: %s (%lu)\n", linebuf, parser->start); + printf ("# Start: %s (%lu)\n", linebuf, parser->start); strftime (linebuf, 256, "%F %T", gmtime (&parser->end)); - printf ("# END: %s (%lu)\n", linebuf, parser->end); + printf ("# END: %s (%lu)\n", linebuf, parser->end); printf ("# End Query Options\n\n"); fflush (stdout); @@ -1965,7 +1990,14 @@ main (int argc, char * const argv[]) cur_year = parser->year[i]; if (i <= 1) { - if (!(stream = (gpointer) fopen (logfiles[i], "r"))) continue; + if (inputfile && strlen(inputfile) == 1 && *inputfile == '-') { + stream = (gpointer) stdin; + } else if (inputfile) { + if (!(stream = (gpointer) fopen (inputfile, "r"))) { + fprintf(stderr, "unable to open log file\n"); + exit (-1); + } + } else if (!(stream = (gpointer) fopen (logfiles[i], "r"))) continue; } else { if (!(stream = (gpointer) gzopen (logfiles[i], "r"))) continue; } @@ -1978,9 +2010,9 @@ main (int argc, char * const argv[]) } if (i <= 1) { - line = fgets (linebuf, linebufsize, (FILE *)stream); + line = fgets (linebuf, LINEBUFSIZE, (FILE *)stream); } else { - line = mygzgets ((gzFile)stream, linebuf, linebufsize); + line = mygzgets ((gzFile)stream, linebuf, LINEBUFSIZE); } if (!line) break; @@ -2029,10 +2061,10 @@ main (int argc, char * const argv[]) //const char *prog = cpos; csum_prog = 0; - found = 0; while (*cpos && (*cpos != ':') && (*cpos != '[')) { - csum_prog = (csum_prog <<8) + *cpos; - cpos++; - found++; + found = 0; while (*cpos && (*cpos != ':') && (*cpos != '[')) { + csum_prog = ((csum_prog << 8)|(csum_prog >> 24)) + *cpos; + cpos++; + found++; } //idx1 = g_strndup (prog, found); @@ -2041,10 +2073,10 @@ main (int argc, char * const argv[]) if (*cpos == '[') { cpos++; - found = 0; while (isdigit (*cpos)) { - pid = pid*10 + *cpos - 48; - cpos++; - found++; + found = 0; while (isdigit (*cpos)) { + pid = pid*10 + *cpos - '0'; + cpos++; + found++; } if (found < 1 || found > 15 || *cpos != ']') { debug_error ("unable to parse pid", line); @@ -2052,7 +2084,7 @@ main (int argc, char * const argv[]) } cpos++; } - + if (*cpos++ != ':') { debug_error ("missing collon", line); continue; @@ -2074,8 +2106,8 @@ main (int argc, char * const argv[]) strmatch = 1; } - if ((csum_prog == 0x70726F78) ||// proxprox - (csum_prog == 0x6C746572)) { // pmg-smtp-filter + if ((csum_prog == PROXPROX) || + (csum_prog == PMG_SMTP_FILTER)) { if ((idx1 = parse_qid (&cpos, qidbuf, ':', 25))) { @@ -2094,13 +2126,13 @@ main (int argc, char * const argv[]) //fixme: parse spam score if ((*cpos == 'a') && !strncmp (cpos, "accept mail to <", 16)) { - + const char *to_s, *to_e; to_s = cpos = cpos + 16; - + while (*cpos && (*cpos != '>')) { cpos++; } - + if (*cpos != '>') continue; to_e = cpos; @@ -2108,7 +2140,7 @@ main (int argc, char * const argv[]) cpos++; if ((*cpos++ != ' ') || (*cpos++ != '(')) continue; - + if (!(idx1 = parse_qid (&cpos, qidbuf, ')', 15))) continue; // parser_track (parser, idx1, 1); @@ -2119,9 +2151,9 @@ main (int argc, char * const argv[]) const char *to_s, *to_e; to_s = cpos = cpos + 16; - + while (*cpos && (*cpos != '>')) { cpos++; } - + to_e = cpos; if (strncmp (cpos, "> to ", 5)) continue; @@ -2139,17 +2171,17 @@ main (int argc, char * const argv[]) cpos += 14; if (!(idx1 = parse_qid (&cpos, qidbuf, 0, 25))) continue; - + fentry_tolist_add (fe, 'Q', to_s, to_e - to_s, idx1, strlen (idx1)); } else if ((*cpos == 'b') && !strncmp (cpos, "block mail to <", 15)) { - + const char *to_s; to_s = cpos = cpos + 15; - + while (*cpos && (*cpos != '>')) { cpos++; } - + if (*cpos != '>') continue; fentry_tolist_add (fe, 'B', to_s, cpos - to_s, NULL, 0); @@ -2164,7 +2196,7 @@ main (int argc, char * const argv[]) } - } else if (csum_prog == 0x7265656E) { // postfix/postscreen + } else if (csum_prog == POSTFIX_POSTSCREEN) { SEntry *se; @@ -2198,7 +2230,7 @@ main (int argc, char * const argv[]) const char *to = cpos = cpos + 7; while (*cpos && (*cpos != '>')) { cpos++; } - + if (*cpos != '>') continue; if (!(se = sentry_get (parser, pid, ctime, rel_line_nr))) { @@ -2220,8 +2252,8 @@ main (int argc, char * const argv[]) sentry_print (parser, se); sentry_free (parser, se); } - - } else if (csum_prog == 0x716D6772) { // postfix/qmgr + + } else if (csum_prog == POSTFIX_QMGR) { if ((idx2 = text) && (idx1 = parse_qid (&idx2, qidbuf, ':', 15))) { @@ -2233,10 +2265,10 @@ main (int argc, char * const argv[]) if (strmatch) qe->strmatch = 1; - qe->cleanup = 1; + qe->cleanup = 1; loglist_add (&qe->ep, &qe->loglist, line, len, lines); - + if ((*idx2 == 'f') && !strncmp (idx2, "from=<", 6)) { cpos = idx2 = idx2 + 6; @@ -2255,11 +2287,11 @@ main (int argc, char * const argv[]) int size = 0; cpos += 7; - while (isdigit (*cpos)) { size = size*10 + *cpos++ - 48; } + while (isdigit (*cpos)) { size = size*10 + *cpos++ - '0'; } qe->size = size; } - + } else if ((*idx2 == 'r') && !strncmp (idx2, "removed\n", 8)) { qe->removed = 1; @@ -2268,12 +2300,12 @@ main (int argc, char * const argv[]) } } - } else if ((csum_prog == 0x736D7470) || //postfix/smtp - (csum_prog == 0x6C6D7470) || //postfix/lmtp - (csum_prog == 0x6F63616C) || //postfix/local - (csum_prog == 0x72726F72)) { //postfix/error + } else if ((csum_prog == POSTFIX_SMTP) || + (csum_prog == POSTFIX_LMTP) || + (csum_prog == POSTFIX_LOCAL) || + (csum_prog == POSTFIX_ERROR)) { - int lmtp = (csum_prog == 0x6C6D7470); + int lmtp = (csum_prog == POSTFIX_LMTP); if ((cpos = text) && (idx1 = parse_qid (&cpos, qidbuf, ':', 15))) { @@ -2285,7 +2317,7 @@ main (int argc, char * const argv[]) if (strmatch) qe->strmatch = 1; - qe->cleanup = 1; + qe->cleanup = 1; loglist_add (&qe->ep, &qe->loglist, line, len, lines); @@ -2295,15 +2327,15 @@ main (int argc, char * const argv[]) const char *to_s, *to_e; to_s = cpos; - + while (*cpos && (*cpos != '>')) { cpos++; } - + if (*cpos != '>') continue; to_e = cpos; cpos ++; - + if (!(cpos = strstr (cpos, ", relay="))) continue; cpos += 8; @@ -2312,7 +2344,7 @@ main (int argc, char * const argv[]) relay_s = cpos; while (*cpos && (*cpos != ',')) { cpos++; } - + if (*cpos != ',') continue; relay_e = cpos; @@ -2325,7 +2357,7 @@ main (int argc, char * const argv[]) char dstatus = *cpos; - qentry_tolist_add (qe, ctime, dstatus, to_s, to_e - to_s, + qentry_tolist_add (qe, ctime, dstatus, to_s, to_e - to_s, relay_s, relay_e - relay_s); if (!lmtp) continue; // filter always uses lmtp @@ -2342,7 +2374,7 @@ main (int argc, char * const argv[]) } else { continue; } - + if (!(idx1 = parse_qid (&cpos, qidbuf, ')', 25))) continue; @@ -2351,11 +2383,11 @@ main (int argc, char * const argv[]) qe->filtered = 1; if ((fe = g_hash_table_lookup (parser->filter_h, idx1))) { - qe->filter = fe; + qe->filter = fe; } } - } else if (csum_prog == 0x6D747064) { // postfix/smtpd + } else if (csum_prog == POSTFIX_SMTPD) { SEntry *se; if (!pid) { @@ -2372,7 +2404,7 @@ main (int argc, char * const argv[]) loglist_add (&se->ep, &se->loglist, line, len, lines); if ((*text == 'c') && !strncmp (text, "connect from ", 13)) { - + cpos = idx1 = text + 13; while (*idx1 && !isspace (*idx1)) { idx1++; } @@ -2397,7 +2429,7 @@ main (int argc, char * const argv[]) } else { sentry_ref_finalize (parser, se); } - + } else if ((*text == 'N') && !strncmp (text, "NOQUEUE:", 8)) { cpos = text + 8; @@ -2436,7 +2468,7 @@ main (int argc, char * const argv[]) const char *to = cpos = cpos + 6; while (*cpos && (*cpos != '>')) { cpos++; } - + if (*cpos != '>') continue; sentry_nqlist_add (se, ctime, from, idx1 - from, to, cpos - to, dstatus); @@ -2461,8 +2493,8 @@ main (int argc, char * const argv[]) } } - - } else if (csum_prog == 0x616E7570) { // postfix/cleanup + + } else if (csum_prog == POSTFIX_CLEANUP) { // postfix/cleanup QEntry *qe; @@ -2484,7 +2516,7 @@ main (int argc, char * const argv[]) qe->cleanup = 1; } - } + } } } } @@ -2505,43 +2537,43 @@ main (int argc, char * const argv[]) printf ("MEM QMGR entries: %d\n", g_hash_table_size (parser->qmgr_h)); printf ("MEM FILTER entries: %d\n", g_hash_table_size (parser->filter_h)); - printf ("MEMDEB SMTPD entries: %d %d\n", + printf ("MEMDEB SMTPD entries: %d %d\n", g_hash_table_size (smtpd_debug_alloc), g_hash_table_size (smtpd_debug_free)); - printf ("MEMDEB QMGR entries: %d %d\n", + printf ("MEMDEB QMGR entries: %d %d\n", g_hash_table_size (qmgr_debug_alloc), g_hash_table_size (qmgr_debug_free)); - printf ("MEMDEB FILTER entries: %d %d\n", + printf ("MEMDEB FILTER entries: %d %d\n", g_hash_table_size (filter_debug_alloc), g_hash_table_size (filter_debug_free)); #endif - g_hash_table_foreach (parser->qmgr_h, qentry_cleanup_hash, parser); - g_hash_table_foreach (parser->smtpd_h, sentry_cleanup_hash, parser); - g_hash_table_foreach (parser->filter_h, fentry_cleanup_hash, parser); + g_hash_table_foreach (parser->qmgr_h, qentry_cleanup_hash, parser); + g_hash_table_foreach (parser->smtpd_h, sentry_cleanup_hash, parser); + g_hash_table_foreach (parser->filter_h, fentry_cleanup_hash, parser); #ifdef DEBUG - printf ("MEMDEB SMTPD entries: %d %d\n", + printf ("MEMDEB SMTPD entries: %d %d\n", g_hash_table_size (smtpd_debug_alloc), g_hash_table_size (smtpd_debug_free)); - printf ("MEMDEB QMGR entries: %d %d\n", + printf ("MEMDEB QMGR entries: %d %d\n", g_hash_table_size (qmgr_debug_alloc), g_hash_table_size (qmgr_debug_free)); - printf ("MEMDEB FILTER entries: %d %d\n", + printf ("MEMDEB FILTER entries: %d %d\n", g_hash_table_size (filter_debug_alloc), g_hash_table_size (filter_debug_free)); - g_hash_table_foreach (smtpd_debug_alloc, sentry_debug_alloc, parser); + g_hash_table_foreach (smtpd_debug_alloc, sentry_debug_alloc, parser); #endif #ifdef EPOOL_DEBUG - printf ("MEMMAX %d\n", ep_maxalloc); + printf ("MEMMAX %d\n", ep_maxalloc); #endif parser_free (parser); - + fclose (stdout); exit (0);