/
sendmailanalyzer
executable file
·3348 lines (3131 loc) · 108 KB
/
sendmailanalyzer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
#
# SendmailAnalyzer: maillog parser and statistics reports tool for Sendmail
# Copyright (C) 2002-2020 Gilles Darold
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
use vars qw($VERSION $AUTHOR $COPYRIGHT $PROGRAM @ARGS);
use strict;
use Getopt::Long qw(:config no_ignore_case bundling);
use POSIX qw(:sys_wait_h :errno_h :fcntl_h :signal_h);
use MIME::Base64;
use MIME::QuotedPrint;
use IO::File;
$VERSION = '9.4';
$AUTHOR = "Gilles Darold <gilles\@darold.net>";
$COPYRIGHT = "(c) 2002-2020 - Gilles Darold <gilles\@darold.net>";
$SIG{'CHLD'} = 'DEFAULT';
# Keep command line arguments in case we received SIGHUP
$PROGRAM = $0;
push(@ARGS, @ARGV);
my $LINE_DEBUG = 0;
# Month translation
my %MONTH_TO_NUM = (
'Jan' => '01',
'Feb' => '02',
'Mar' => '03',
'Apr' => '04',
'May' => '05',
'Jun' => '06',
'Jul' => '07',
'Aug' => '08',
'Sep' => '09',
'Oct' => '10',
'Nov' => '11',
'Dec' => '12'
);
# Configuration storage hash
my %CONFIG = ();
# Other configuration directives
my $CONFIG_FILE = "/usr/local/sendmailanalyzer/sendmailanalyzer.conf";
my $SHOW_VER = 0;
my $INTERACTIVE = 0;
my $HELP = 0;
my $LAST_PARSE_FILE = 'LAST_PARSED';
my $PID_FILE = 'sendmailanalyzer.pid';
my $SA_PIPE = new IO::File;
my $EXIM_REGEX = qr/^(\d+\-\d+\-\d+) (\d+:\d+:\d+) ([A-Za-z0-9]{6}\-[A-Za-z0-9]{6}\-[A-Za-z0-9]{2}) ([\-\*=><]+) /;
my $HOSTNAME = '';
# Global variable to store temporary parsed data
my %SYSERR = ();
my %DSN = ();
my %FROM = ();
my %TO = ();
my %REJECT = ();
my %SPAM = ();
my %VIRUS = ();
my $LAST_PARSED = '';
my %ERRMSG = ();
my %OTHER = ();
my %SPAMDETAIL = ();
my %AUTH = ();
my %GREYLIST = ();
my $KID = '';
my %POSTGREY = ();
my %MSGID = ();
my %SKIPMSG = ();
my %POSTFIX_PLUGIN_TEMP_DELIVERY = ();
my %AMAVIS_ID = ();
my %STARTTLS = ();
my %SPAMPD = ();
my %KEEP_TEMPORARY = ();
my %SPF_DKIM = ();
# Collect command line arguments
GetOptions (
'a|args=s' => \$CONFIG{TAIL_ARGS},
'b|break!' => \$CONFIG{BREAK},
'c|config=s' => \$CONFIG_FILE,
'd|debug!' => \$CONFIG{DEBUG},
'f|full!' => \$CONFIG{FULL},
'F|force!' => \$CONFIG{FORCE},
'g|postgrey=s' => \$CONFIG{POSTGREY_NAME},
'h|help!' => \$HELP,
'i|interactive!' => \$INTERACTIVE,
'j|journalctl=s' => \$CONFIG{JOURNALCTL_CMD},
'l|log=s' => \$CONFIG{LOG_FILE},
'm|mailscanner=s' => \$CONFIG{MAILSCAN_NAME},
'n|clamd=s' => \$CONFIG{CLAMD_NAME},
'o|output=s' => \$CONFIG{OUT_DIR},
'p|piddir=s' => \$CONFIG{PID_DIR},
's|sendmail=s' => \$CONFIG{MTA_NAME},
't|tail=s' => \$CONFIG{TAIL_PROG},
'v|version!' => \$SHOW_VER,
'w|write-delay=i' => \$CONFIG{DELAY},
'z|zcat=s' => \$CONFIG{ZCAT_PROG},
'y|year=s' => \$CONFIG{DEFAULT_YEAR},
'spamd=s' => \$CONFIG{SPAMD_NAME},
'spf=s' => \$CONFIG{SPF_DKIM_NAME},
'hostname=s' => \$HOSTNAME,
);
$CONFIG{FULL} = 1 if ($CONFIG{FORCE});
&usage if ($HELP);
# Read configuration file
&read_config($CONFIG_FILE);
# Checked forced year syntax
if ($CONFIG{DEFAULT_YEAR} && ($CONFIG{DEFAULT_YEAR} !~ /^\d{4}$/)) {
die "FATAL: Default year $CONFIG{DEFAULT_YEAR} should be 4 digits!\n";
}
# Check if output dir exist and we can write file
if (!-d $CONFIG{OUT_DIR}) {
die "FATAL: Output directory $CONFIG{OUT_DIR} should exists !\n";
} else {
open(OUT, ">$CONFIG{OUT_DIR}/test.dat") or die "FATAL: Output directory $CONFIG{OUT_DIR} should be writable !\n";
close(OUT);
unlink("$CONFIG{OUT_DIR}/test.dat");
}
if ($CONFIG{DEBUG}) {
print STDERR "Running in verbose mode...\n";
}
if ($SHOW_VER || $CONFIG{DEBUG}) {
print STDERR "\n\tsendmailanalyzer v$VERSION. $COPYRIGHT\n\n";
exit 0 if ($SHOW_VER);
}
####
# Install signal handlers
####
# Die cleanly on signal
sub terminate
{
close($SA_PIPE) if ($SA_PIPE >= 0);
&flush_data(1);
&dprint("Received terminating signal.", 1);
}
# Restart on signal
sub restart_sa
{
close($SA_PIPE) if ($SA_PIPE >= 0);
&dprint("Received SIGHUP signal: reloading configuration file and reopening log file.");
&flush_data(1);
&clean_globals();
exec($^X, $PROGRAM, @ARGS) or die "FATAL: Couldn't restart: $!\n";
}
# Reload configuration and reopen log file on kill -1
my $sigset_hup = POSIX::SigSet->new();
my $action_hup = POSIX::SigAction->new('restart_sa', $sigset_hup, &POSIX::SA_NODEFER);
POSIX::sigaction(&POSIX::SIGHUP, $action_hup);
# Terminate on kill -15
my $sigset_term = POSIX::SigSet->new();
my $action_term = POSIX::SigAction->new('terminate', $sigset_term, &POSIX::SA_NODEFER);
POSIX::sigaction(&POSIX::SIGTERM, $action_term);
# Terminate on kill -9
my $sigset_int = POSIX::SigSet->new();
my $action_int = POSIX::SigAction->new('terminate', $sigset_int, &POSIX::SA_NODEFER);
POSIX::sigaction(&POSIX::SIGKILL, $action_int);
my $CURRENT_TIME = &format_time(localtime(time));
# Run in interactive mode if required
if ($INTERACTIVE) {
# Start in interactive mode
print "\n*** sendmailanalyzer v$VERSION (pid:$$) started at " . localtime(time) . "\n";
} else {
# detach from terminal
my $pid = fork;
exit 0 if ($pid);
die "Couldn't fork: $!" unless defined($pid);
POSIX::setsid() or die "Can't detach: \$!";
&dprint("Detach from terminal with pid: $$");
}
# Set name of the program without path*
my $orig_name = $0;
$0 = 'sendmailanalyzer';
# Continuously read the maillog file using a pipe to tail program
&dprint("Entering main loop...");
&start_loop;
exit 0;
#-------------------------------- ROUTINES ------------------------------------
####
# Dump usage to STDERR
####
sub usage
{
print STDERR qq{
sendmailanalyzer v$VERSION usage:
-a | --args "tail_args": tail command line arguments. Default "-n 0 -f".
-b | --break : do not run tail after parsing full maillog and exit.
-c | --config file : path to configuration file. Default is to read it from
/etc/sendmailanalyzer.conf.
-d | --debug : turn on debug mode.
-f | --full : parse full maillog and compute stat. Default is to read
LAST_PARSED file to start from last collected event.
-F | --force : same as --full but don't take care of LAST_PARSED file,
that means that log file always contains new entries.
-h | --help : show this short help and exit.
-i | --interactive : run in interactive mode useful if you want day to day
: report. Default is daemon mode, real time statistics.
-j | --journalctl cmd : set the journalctl command to use to replace logfile.
-l | --log file : path to maillog file. Default is /var/log/maillog.
-m | --mailscanner name: syslog MailScanner program name. Default: Mailscanner.
-o | --output dir : path to the output directory where data file will be
written. Default /var/www/htdocs.
-p | --piddir dir : path where pid file will be stored. Default /var/run/.
-s | --sendmail name : syslog sendmail program name. Default sm-mta|sendmail.
-t | --tail tail_prog : path to the tail system command. Default /usr/bin/tail.
-v | --version : show version and exit
-w | --write-delay sec : memory storage delay in second before saving data
to disk. Default: 5 seconds.
-y | --year 2001 : force the years date part of the log to given value.
Default is current year or previous year if log lines
appear in the future.
-z | --zcat zcat_prog : path to the zcat command for compressed maillog.
Default /usr/bin/zcat.
--spamd name : syslog Spamd program name. Default: spamd.
--hostname name : set a hostname for exim logs. Default: unknown.
--spf name : syslog SPF and DKIM program name list.
Default: opendmarc|opendkim.
};
exit 0;
}
####
# Function used to dump debugging information
####
sub dprint
{
my $msg = shift;
my $exit = shift;
print STDERR "DEBUG: $msg\n" if ($exit || $CONFIG{DEBUG});
if ($exit) {
unlink("$CONFIG{PID_DIR}/$PID_FILE");
exit 1;
}
}
####
# Start reading maillog file
####
my $OLD_LAST_PARSED = '';
my $OLD_OFFSET = 0;
sub start_loop
{
if ($CONFIG{FULL}) {
if (!$CONFIG{FORCE}) {
if (-e "$CONFIG{OUT_DIR}/$LAST_PARSE_FILE") {
if ( not open(IN, "$CONFIG{OUT_DIR}/$LAST_PARSE_FILE")) {
&logerror("Can't read file $CONFIG{OUT_DIR}/$LAST_PARSE_FILE: $!");
} else {
my $tmp = <IN>;
chomp($tmp);
($OLD_LAST_PARSED,$OLD_OFFSET) = split(/[\t]/, $tmp);
close(IN);
}
}
}
if ($CONFIG{JOURNALCTL_CMD}) {
$OLD_OFFSET = 0;
my $since = '';
if ( ($CONFIG{JOURNALCTL_CMD} !~ /--since|-S/) && ($OLD_LAST_PARSED =~ /^(\d+)-(\d+)-(\d+).(\d+):(\d+):(\d+)/) ) {
$since = " --since=\"$1-$2-$3 $4:$5:$6\"";
}
&dprint("Parsing full entries from command: $CONFIG{JOURNALCTL_CMD}$since --output=\"short-iso\"");
if (!($KID = open(SA_FILE, "$CONFIG{JOURNALCTL_CMD}$since --output=\"short-iso\" |"))) {
&dprint("$0: cannot read input from command: $CONFIG{JOURNALCTL_CMD}$since --output=\"short-iso\", $!", 1);
}
} else {
&dprint("Parsing full $CONFIG{LOG_FILE}");
if ($CONFIG{LOG_FILE} !~ /\.gz/) {
if (!($KID = open(SA_FILE, "$CONFIG{LOG_FILE}"))) {
&dprint("$0: cannot read $CONFIG{LOG_FILE}: $!", 1);
}
} else {
# Open a pipe to zcat program for compressed log
if (!($KID = open(SA_FILE, "$CONFIG{ZCAT_PROG} $CONFIG{LOG_FILE}|"))) {
&dprint("$0: cannot read from pipe to \"$CONFIG{ZCAT_PROG} $CONFIG{LOG_FILE}\": $!", 1);
}
}
}
# Write pid file
if (open(OUT, ">$CONFIG{PID_DIR}/$PID_FILE")) {
print OUT "$$";
close(OUT);
}
# Move to the last position
if ($OLD_OFFSET && ($CONFIG{LOG_FILE} !~ /\.gz/)) {
if ((lstat($CONFIG{LOG_FILE}))[7] < $OLD_OFFSET) {
&dprint("Log file may have changed, rereading from start of the log file.") if ($CONFIG{DEBUG} > 0);
$OLD_OFFSET = 0;
} else {
&dprint("Jumping to last log offset ($OLD_OFFSET).") if ($CONFIG{DEBUG} > 0);
my $ret = seek(SA_FILE, $OLD_OFFSET, 0);
if (!$ret) {
&dprint("Wrong offset reread from start of the log file.") if ($CONFIG{DEBUG} > 0);
seek(SA_FILE, 0, 0);
$OLD_OFFSET = 0;
}
# Exceptional case of a file with same size, read the first line
# to see if the data has changed, otherwise this is the same file
if ($OLD_OFFSET && (lstat($CONFIG{LOG_FILE}))[7] == $OLD_OFFSET) {
# Rewind at begining of the line
my $c = '';
my $count = 1;
while ($c ne "\n" && $count < 10000) {
$count++;
seek(SA_FILE, $OLD_OFFSET - $count, 0);
read(SA_FILE, $c, 1);
}
seek(SA_FILE, $OLD_OFFSET - $count, 0);
if ($count < 10000) {
while (my $l = <SA_FILE>) {
chomp($l);
$l =~ s/ ID \d+ mail.\w//;
next if ($l =~ /policy-spf|You are still greylisted/);
my $tmp_last_parsed = $l;
# Only catch relevant logs
next if ($CONFIG{EXCLUDE_LINE} && $tmp_last_parsed =~ m#$CONFIG{EXCLUDE_LINE}#);
if ( ($tmp_last_parsed =~ /($CONFIG{MTA_NAME}|$CONFIG{MAILSCAN_NAME}|$CONFIG{AMAVIS_NAME}|$CONFIG{MD_NAME}|$CONFIG{CLAMD_NAME}|$CONFIG{POSTGREY_NAME}|$CONFIG{SPAMD_NAME}|$CONFIG{CLAMSMTPD_NAME}|$CONFIG{SPF_DKIM_NAME})[\/\[:]/) || ($LAST_PARSED =~ $EXIM_REGEX) ) {
if ($tmp_last_parsed ne $OLD_LAST_PARSED) {
&dprint("Size is identique but data are more recent than the one at old offset. Rereading from start of the log file.") if ($CONFIG{DEBUG} > 0);
seek(SA_FILE, 0, 0);
$OLD_OFFSET = 0;
$OLD_LAST_PARSED = '';
} else {
&dprint("Size is identique and data are the same, nothing to do.") if ($CONFIG{DEBUG} > 0);
# Go back to current offset
seek(SA_FILE, $OLD_OFFSET, 0);
}
last;
}
}
} else {
# Go back to current offset
seek(SA_FILE, $OLD_OFFSET, 0);
}
}
}
} else {
$OLD_OFFSET = 0;
}
while (my $l = <SA_FILE>) {
chomp($l);
$l =~ s/ [\[]*ID \d+ mail.\w+[\]]*//;
next if ($l =~ /policy-spf|You are still greylisted/);
$LAST_PARSED = $l;
$l = '';
# Only catch relevant logs
next if ($CONFIG{EXCLUDE_LINE} && $LAST_PARSED =~ m#$CONFIG{EXCLUDE_LINE}#);
if ( ($LAST_PARSED =~ /($CONFIG{MTA_NAME}|$CONFIG{MAILSCAN_NAME}|$CONFIG{AMAVIS_NAME}|$CONFIG{MD_NAME}|$CONFIG{CLAMD_NAME}|$CONFIG{POSTGREY_NAME}|$CONFIG{SPAMD_NAME}|$CONFIG{CLAMSMTPD_NAME}|$CONFIG{SPF_DKIM_NAME})[\/\[:]/) || ($LAST_PARSED =~ $EXIM_REGEX) ) {
my $tmpos = tell(SA_FILE);
if ($OLD_LAST_PARSED) {
# Store the last position in the log
# Line already parsed ? If yes, go to retrieve next log line
if (&incremental_check($LAST_PARSED, $OLD_LAST_PARSED, $OLD_OFFSET) != 0) {
# Search if this is really the same log file
if ($OLD_OFFSET && ($tmpos > $OLD_OFFSET)) {
&dprint("Data are more recent than the one at old offset, maybe the file has changed. Rereading from start of the log file.") if ($CONFIG{DEBUG} > 0);
seek(SA_FILE, 0, 0);
$OLD_OFFSET = 0;
$OLD_LAST_PARSED = '';
}
next;
}
# The line have not already been parsed so erase last date
# to not go back to this block again
$OLD_LAST_PARSED = '';
}
if ($CONFIG{LOG_FILE} !~ /\.gz/) {
$OLD_OFFSET = $tmpos;
}
# Extract common fields, store data in memory and retrieve current time
my $check_time = &store_data(&parse_common_fields(split(/\s+/, $LAST_PARSED)));
# Flush data to disk each kind of 5 seconds at least by default
if ($check_time > $CURRENT_TIME+$CONFIG{DELAY}) {
$CURRENT_TIME = $check_time;
&dprint("Flushing data to disk...");
&flush_data();
}
}
}
&dprint("Flushing data to disk...");
&flush_data();
if ($CONFIG{BREAK}) {
unlink("$CONFIG{PID_DIR}/$PID_FILE");
exit 0;
}
}
# Daemon mode is not possible with compressed log file
if ($CONFIG{LOG_FILE} =~ /\.gz/) {
&dprint("Daemon mode is not possible with compressed log file", 1);
}
# Open a pipe to the tail program or to the journalctl command
my $since = '';
if ($CONFIG{JOURNALCTL_CMD} !~ /--since|-S/) {
if ($LAST_PARSED) {
if ($LAST_PARSED =~ /^(\d+)-(\d+)-(\d+).(\d+):(\d+):(\d+)/) {
$since = " --since=\"$1-$2-$3 $4:$5:$6\"";
}
} else {
if ($OLD_LAST_PARSED =~ /^(\d+)-(\d+)-(\d+).(\d+):(\d+):(\d+)/) {
$since = " --since=\"$1-$2-$3 $4:$5:$6\"";
}
}
}
if ($CONFIG{JOURNALCTL_CMD}) {
&dprint("Opening pipe to command: $CONFIG{JOURNALCTL_CMD}$since --output=\"short-iso\" -f");
if (!($KID = $SA_PIPE->open("$CONFIG{JOURNALCTL_CMD}$since --output=\"short-iso\" -f |"))) {
&dprint("$0: cannot read input from command: $CONFIG{JOURNALCTL_CMD}$since --output=\"short-iso\" -f, $!", 1);
}
} else {
&dprint("Opening pipe to $CONFIG{TAIL_PROG} $CONFIG{TAIL_ARGS} $CONFIG{LOG_FILE}");
if ( !($KID = $SA_PIPE->open("$CONFIG{TAIL_PROG} $CONFIG{TAIL_ARGS} $CONFIG{LOG_FILE}|"))) {
&dprint("$0: cannot read from pipe to \"$CONFIG{TAIL_PROG} $CONFIG{TAIL_ARGS} $CONFIG{LOG_FILE}\": $!");
}
}
# Write pid file
if (open(OUT, ">$CONFIG{PID_DIR}/$PID_FILE")) {
print OUT "$$";
close(OUT);
}
# We'll need a non blocking read to be able to intercept signal
# when there's no new entry in the log file. But for now
# SIGTERM should be use to interrupt the blocking read.
while (my $l = <$SA_PIPE>) {
chomp($l);
$l =~ s/ [\[]*ID \d+ mail.\w+[\]]*//;
next if ($l =~ /policy-spf|You are still greylisted/);
$LAST_PARSED = $l;
$l = '';
# Only catch relevant logs
next if ($CONFIG{EXCLUDE_LINE} && $LAST_PARSED =~ m#$CONFIG{EXCLUDE_LINE}#);
my $check_time = '';
if ( ($LAST_PARSED =~ /($CONFIG{MTA_NAME}|$CONFIG{MAILSCAN_NAME}|$CONFIG{AMAVIS_NAME}|$CONFIG{MD_NAME}|$CONFIG{CLAMD_NAME}|$CONFIG{POSTGREY_NAME}|$CONFIG{SPAMD_NAME}|$CONFIG{CLAMSMTPD_NAME}|$CONFIG{SPF_DKIM_NAME})[\/\[]/) || ($LAST_PARSED =~ $EXIM_REGEX) ) {
# Extract common fields and store data in memory
$check_time = &store_data(&parse_common_fields(split(/\s+/, $LAST_PARSED)));
} else {
$check_time = &format_time(localtime(time));
}
# Flush data to disk if write delay is over
if ($check_time > $CURRENT_TIME+$CONFIG{DELAY}) {
$CURRENT_TIME = $check_time;
&dprint("Flushing data to disk ($check_time > $CURRENT_TIME+$CONFIG{DELAY})...");
&flush_data();
# Avoid memory leak
%POSTFIX_PLUGIN_TEMP_DELIVERY = ();
%AMAVIS_ID = ();
}
}
&dprint("Flushing last data to disk...");
&flush_data(1);
}
####
# Routine used to extract common field on maillog lines
####
sub parse_common_fields
{
my ($month,$day,$time,$host,$type,@other) = @_;
# Get current system time
my $ctime = &format_time(localtime(time));
# Remove domain part of the host
if ($CONFIG{NO_HOST_DOMAIN}) {
$host =~ s/\..*//;
}
my $date = '';
if ($month =~ /(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/) {
$date = "$1$2$3";
unshift(@other, $type);
unshift(@other, $host);
$host = $day;
$type = $time;
$time = "$4$5$6";
} elsif ($month =~ /(\d+)-(\d+)-(\d+)/) {
$date = "$1$2$3";
unshift(@other, $type);
$type = $host;
$host = $time;
if ($day =~ /(\d+):(\d+):(\d+)/) {
$time = "$1$2$3";
}
} else {
my $current_year = 0;
if ($CONFIG{DEFAULT_YEAR}) {
$current_year = $CONFIG{DEFAULT_YEAR};
} else {
$current_year = (localtime(time))[5]+1900;
}
$date = $current_year . sprintf("%02d",$MONTH_TO_NUM{"$month"}) . sprintf("%02d",$day);
my @f = split(/:/, $time);
$f[0] = sprintf("%02d",$f[0]);
$f[1] = sprintf("%02d",$f[1]);
$f[2] = sprintf("%02d",$f[2]);
$time = "$f[0]$f[1]$f[2]";
if ("$date$time" > $ctime) {
# If log timestamp is in the future, use the given one
if (!$CONFIG{DEFAULT_YEAR}) {
$date = ($current_year - 1) . sprintf("%02d",$MONTH_TO_NUM{"$month"}) . sprintf("%02d",$day);
}
}
}
$type =~ s/\[.*\]\://;
$host = $CONFIG{MERGING_HOST} if ($CONFIG{MERGING_HOST});
my $line = join(' ', @other);
$line =~ s/^\[ID.*\] //;
return ($date,$time,$host,$type,$line,$ctime);
}
####
# Routine used to store collected data
####
sub store_data
{
my ($date,$time,$host,$type,$other,$ctime) = @_;
if ($type =~ /^[<=>\*\-]+$/) {
&parse_exim("$date","$time",$host,$type,$other);
} elsif (($CONFIG{MAILSCAN_NAME} || $CONFIG{CLAMD_NAME}) && ($type =~ /^$CONFIG{MAILSCAN_NAME}|$CONFIG{CLAMD_NAME}/i)) {
&parse_mailscanner("$date","$time",$host,$other);
} elsif ($CONFIG{AMAVIS_NAME} && ($type =~ /^$CONFIG{AMAVIS_NAME}/i)) {
&parse_amavis("$date","$time",$host,$other);
} elsif ($CONFIG{CLAMSMTPD_NAME} && ($type =~ /^$CONFIG{CLAMSMTPD_NAME}/i)) {
&parse_clamsmtpd("$date","$time",$host,$other);
} elsif ($CONFIG{MTA_NAME} && ($type =~ /^$CONFIG{MTA_NAME}/i)) {
&parse_sendmail("$date","$time",$host,$other,$type);
} elsif ($CONFIG{MD_NAME} && ($type =~ /^$CONFIG{MD_NAME}/i)) {
&parse_mimedefang("$date","$time",$host,$other);
} elsif ($CONFIG{POSTGREY_NAME} && ($type =~ /^$CONFIG{POSTGREY_NAME}/i)) {
&parse_postgrey("$date","$time",$host,$other);
} elsif ($CONFIG{SPAMD_NAME} && ($type =~ /^$CONFIG{SPAMD_NAME}/i)) {
&parse_spamd("$date","$time",$host,$other);
} elsif ($CONFIG{SPF_DKIM_NAME} && ($type =~ /^$CONFIG{SPF_DKIM_NAME}/i)) {
&parse_spf_dkim("$date","$time",$host,$other);
} else {
&dprint("Skipping unknown syslog report => $date $time $host [$type]: $other") if ($CONFIG{DEBUG} > 1);
}
return $ctime;
}
sub parse_exim
{
my ($date,$time,$id,$type,$other) = @_;
my $time_st = "$date$time";
my $host = $HOSTNAME || 'unknown';
if ($type eq '<=') {
if ($other =~ m#^(.*?) H=(.*?) P=.* S=(\d+) id=(.*)#) {
my $size = $3;
my $msgid = $4;
my $relay = &clean_relay(lc($2));
$FROM{$host}{$id}{from} = &edecode($1);
$msgid =~ s/[<>]//g;
$FROM{$host}{$id}{date} = $time_st;
$FROM{$host}{$id}{size} = $size;
$FROM{$host}{$id}{nrcpts} = 1;
$FROM{$host}{$id}{msgid} = $msgid;
$FROM{$host}{$id}{relay} = $relay;
}
} elsif ($type =~ /^(=|-)>$/) {
if ($other =~ m#^(.*?) R=(.*?) T=(.*?) H=(.*)#) {
my $to = &edecode($1);
my $relay = &clean_relay(lc($4));
if ($relay eq $CONFIG{'SKIP_RCPT_RELAY'}) {
return;
}
next if ($CONFIG{EXCLUDE_TO} && ($to =~ /^$CONFIG{EXCLUDE_TO}$/));
push(@{$TO{$host}{$id}{date}}, $time_st);
push(@{$TO{$host}{$id}{relay}}, $relay);
push(@{$TO{$host}{$id}{to}}, $to);
push(@{$TO{$host}{$id}{status}}, 'Sent');
}
} elsif ($type eq '==') {
if ($other =~ m#^(.*?) R=(.*?) T=([^:]+): (.*)#) {
$REJECT{$host}{$id}{relay} = $FROM{$host}{$id}{relay};
$REJECT{$host}{$id}{status} = &clear_status($2);
$REJECT{$host}{$id}{date} = $time_st;
$REJECT{$host}{$id}{arg1} = &edecode($1);
my $rule = $3;
$rule =~ s/\s*\(.*//;
$REJECT{$host}{$id}{rule} = $rule;
}
} elsif ($type eq '**') {
if ( !exists $REJECT{$host}{$id}{rule} && ($other =~ m#^([^:]+): (.*)#) ) {
$REJECT{$host}{$id}{relay} = $FROM{$host}{$id}{relay};
$REJECT{$host}{$id}{rule} = 'error';
$REJECT{$host}{$id}{status} = &clear_status($2);
$REJECT{$host}{$id}{date} = $time_st;
$REJECT{$host}{$id}{arg1} = &edecode($1);
}
}
}
sub debug_rule
{
my $id = shift;
my $str = shift;
if ($LINE_DEBUG) {
my ($package, $filename, $line) = caller;
print STDERR "DEBUG_RULE: line: $line => id=$id, line: $str\n";
}
}
####
# Parse Sendmail syslog output
####
sub parse_sendmail
{
my ($date,$time,$host,$str,$type) = @_;
my $time_st = "$date$time";
# Skip unwanted Postfix lines
if ($str =~ m#^([^:\s]+): .*#) {
my $id = $1;
return if ($id =~ /^(warning|(match|dns|rewrite|generic|mail|maps|ctable|smtp)_.*)$/); # Skip debug message
return if (exists $SKIPMSG{$id});
}
#### Store each relevant information per host and id
# Some spampd line must be skipped
return if (($type eq 'spampd') && ($str =~ /(processing|clean) message/));
# Parse MTA system error
if ($str =~ m#^([^:\s]+): SYSERR[^:]+: (.*)# ) {
#debug_rule($1);
$SYSERR{$host}{$1}{date} = $time_st;
$SYSERR{$host}{$1}{message} = &clear_status($2);
# Skip message related to MCI caching module
} elsif ($str =~ m#^([^:\s]+): MCI\@#) {
return;
# Skip Debug message
} elsif ($str =~ m#^([^:\s]+):\s+\d+: fl=#) {
return;
# POSTFIX: Skip connect/disconnect message
} elsif ($str =~ m#^(DIS)?CONNECT #i) {
return;
# POSTFIX temporary blacklist/whitelist messsage
} elsif ($str =~ m#^(PASS OLD|PASS NEW|WHITELISTED|BLACKLISTED)#i) {
return;
# POSTFIX: Skip postscreen messages
} elsif ($str =~ m#^(WHITELIST VETO|BARE NEWLINE)#i) {
return;
# POSTFIX pregreet test
} elsif ($str =~ m#^(PREGREET|HANGUP)#i) {
return;
# POSTFIX dnsbl message
# Aug 30 10:15:29 mail postfix/postscreen[1474]: DNSBL rank 19 for [103.207.38.153]:51622
} elsif ($str =~ m#^DNSBL rank (\d+) for \[(.*)\]:\d+#) {
return if ($CONFIG{POSTSCREEN_DNSBL_THRESHOLD} > $1);
my $id = &get_uniqueid();
$REJECT{$host}{$id}{relay} = $2;
$REJECT{$host}{$id}{rule} = 'RBL checks';
$REJECT{$host}{$id}{status} = 'postscreen reject';
$REJECT{$host}{$id}{date} = $time_st;
$REJECT{$host}{$id}{arg1} = $1;
return;
# Debug and info messages from POSTFIX
} elsif ($str =~ /^(DEBUG|INFO) /) {
return;
# POSFIX TLS connexion
} elsif ($str =~ /(connect from|setting up TLS connection from)/) {
return;
} elsif ($str =~ /(connect to|setting up TLS connection to|Untrusted TLS connection established)/) {
return;
# POSTFIX dnsbl message ???
} elsif ($str =~ m#addr [a-fA-F0-9\.\:]+ listed#) {
return;
# POSTFIX postscreen messages: COMMAND (PIPELINING|COUNT LIMIT|TIME LIMIT)???
} elsif ($str =~ m#^COMMAND #i) {
return;
# POSTFIX: error messages
} elsif ($str =~ m#^(lost connection|timeout|too many errors) after ([^\s]+)#) {
$SYSERR{$host}{"$date$time"}{date} = $time_st;
$SYSERR{$host}{"$date$time"}{message} = $1 . ' after ' . $2;
} elsif ($str =~ m#^connect to [^:]+: (.*)#) {
$SYSERR{$host}{"$date$time"}{date} = $time_st;
$SYSERR{$host}{"$date$time"}{message} = $1;
} elsif ($str =~ m#^(certificate verification failed for).*:( untrusted issuer| self-signed certificate).*#) {
$SYSERR{$host}{"$date$time"}{date} = $time_st;
$SYSERR{$host}{"$date$time"}{message} = $1 . $2;
} elsif ($str =~ m#^(SSL_connect error).*#) {
$SYSERR{$host}{"$date$time"}{date} = $time_st;
$SYSERR{$host}{"$date$time"}{message} = $1;
# Sendmail Milter change subject
} elsif ($str =~ m#^([^:\s]+): Milter change: header Subject: from (.*?) to (.*)$#) {
#debug_rule($1);
my $id = $KEEP_TEMPORARY{$1} || $1;
$FROM{$host}{$id}{subject} = &decode_subject($2);
# Postfix subject information
} elsif ($str =~ m#^([^:\s]+): (warning|info): header Subject: (.*?) from ([^;]+);#) {
#debug_rule($1);
my $id = $KEEP_TEMPORARY{$1} || $1;
$FROM{$host}{$id}{subject} = &decode_subject($3);
} elsif ($str =~ m#^warning: .*#) {
$SYSERR{$host}{"$date$time"}{date} = $time_st;
$SYSERR{$host}{"$date$time"}{message} = &clear_status($str);
# POSTFIX spampd pass
} elsif ($str =~ m#identified spam (<[^>]+>) \(([^\)]+)\) from ([^\s]+) for ([^\s]+) .* (\d+) bytes.#) {
#debug_rule($1);
my $id = $1;
my $rule = 'spampd';
my $from = &edecode($3);
my $to = &edecode($4);
my $status = $2;
my $size = $5;
foreach my $i (keys %MSGID) {
if ($i eq $id) {
$id = $MSGID{$i}{id};
last;
}
}
return if ($CONFIG{EXCLUDE_TO} && ($to =~ /^$CONFIG{EXCLUDE_TO}$/));
$SPAM{$host}{$id}{relay} = $FROM{$host}{$id}{relay};
$SPAM{$host}{$id}{rule} = 'reject';
$SPAM{$host}{$id}{spam} = $status;
$SPAM{$host}{$id}{date} = $time_st;
$SPAM{$host}{$id}{from} = $from;
push(@{$SPAM{$host}{$id}{to}}, $to);
$SPAM{$host}{$id}{status} = $status;
# POSTFIX spampd pass
} elsif ($str =~ m#identified spam \(([^\)]+)\) \(([^\)]+)\) from ([^\s]+) for ([^\s]+) .* (\d+) bytes.#) {
#debug_rule($1);
my $id = $1;
my $rule = 'spampd';
my $from = &edecode($3);
my $to = &edecode($4);
my $status = $2;
my $size = $5;
return if ($CONFIG{EXCLUDE_TO} && ($to =~ /^$CONFIG{EXCLUDE_TO}$/));
$status =~ s/.*\///;
$SPAMPD{$host}{$from}{$to}{rule} = 'reject';
$SPAMPD{$host}{$from}{$to}{spam} = $status;
$SPAMPD{$host}{$from}{$to}{date} = $time_st;
$SPAMPD{$host}{$from}{$to}{status} = $status;
# Sendmail authid single message
} elsif ($str =~ m#^([^:\s]+): authid=#) {
return;
# POSTFIX remove message id
} elsif ($str =~ m#^([^:\s]+): removed$#) {
#debug_rule($1);
my $id = $1;
foreach (keys %MSGID) {
delete $MSGID{$_} if ($MSGID{$_}{id} eq $id);
}
delete $SKIPMSG{$id};
delete $KEEP_TEMPORARY{$id};
delete $DSN{$host}{$id} if (exists $DSN{$host}{$id});
return;
# Sendmail subject information
} elsif ($str =~ m#^([^:\s]+): Subject:(.*)#) {
#debug_rule($1);
my $id = $1;
$FROM{$host}{$id}{subject} = &decode_subject($2);
# Parse protocole error
} elsif ($str =~ m#^([^:\s]+): ([^:\s]+): (.*protocol error:.*)#) {
#debug_rule($1);
$SYSERR{$host}{$1}{date} = $time_st;
$SYSERR{$host}{$1}{message} = $3;
# Parse virus found by clamav-milter
} elsif ($str =~ m#^([^:\s]+): Milter add: header: X-Virus-Status: Infected with (.*)#) {
#debug_rule($1);
$VIRUS{$host}{$1}{virus} = $2;
$VIRUS{$host}{$1}{file} = 'Inline';
$VIRUS{$host}{$1}{date} = $time_st;
} elsif ($str =~ m#^([^:\s]+): Milter [^:]+: header: X-Virus-Status: Infected \((.*)\)#) {
#debug_rule($1);
$VIRUS{$host}{$1}{virus} = $2;
$VIRUS{$host}{$1}{file} = 'Inline';
$VIRUS{$host}{$1}{date} = $time_st;
# Parse Spam found by ESET
} elsif ($str =~ m#^([^:\s]+): Milter change \([^\)]+\): header: X-Eset-AntiSpam: SPAM#) {
#debug_rule($1);
$SPAM{$host}{$1}{spam} = 'eset';
$SPAM{$host}{$1}{date} = $time_st;
$SPAM{$host}{$1}{from} = $FROM{$host}{$1}{sender} if (exists $FROM{$host}{$1}{sender});
$SPAM{$host}{$1}{relay} = $FROM{$host}{$1}{relay} if (exists $FROM{$host}{$1}{relay});
# Parse Virus found by ESET
} elsif ($str =~ m#^([^:\s]+): Milter: data, reject=.* Rejected by ESETS_SMFI \((.*)\)#) {
#debug_rule($1);
$VIRUS{$host}{$1}{virus} = 'Eset, '.$2;
$VIRUS{$host}{$1}{file} = 'Inline';
$VIRUS{$host}{$1}{date} = $time_st;
} elsif ($str =~ m#^([^:\s]+): Milter (add|change): header: X-Spam-Status: Yes, score=([^\s]+) required=([^\s]+) tests=([^\s]+)#) {
#debug_rule($1);
my $id = $1;
$SPAM{$host}{$id}{spam} = 'spamdmilter';
$SPAM{$host}{$id}{date} = $time_st;
if ($CONFIG{SPAM_DETAIL}) {
$SPAMDETAIL{$host}{$id}{date} = $time_st;
$SPAMDETAIL{$host}{$id}{type} = 'spamdmilter';
$SPAMDETAIL{$host}{$id}{spam} = $5;
$SPAMDETAIL{$host}{$id}{required} = $4;
$SPAMDETAIL{$host}{$id}{score} = $3;
if ($SPAMDETAIL{$host}{$id}{spam} =~ s/(nt|\s)autolearn=([^\s,]+)//) {
$SPAMDETAIL{$host}{$id}{autolearn} = $2;
}
$SPAMDETAIL{$host}{$id}{spam} =~ s/,nt//g;
}
# Parse spam found by postfix/policyd-weight
} elsif ($str =~ m# action=DUNNO\s+(.*); rate: ([^;]+); <client=([^>]+)> <helo=([^>]+)> <from=([^>]+)> <to=([^>]+)>; delay.*#) {
my $spam = $1;
my $score = $2;
my $relay = $3;
my $from = $5;
my $to = $6;
my $id = &get_uniqueid();
$SPAM{$host}{$id}{spam} = 'policydweight';
$SPAM{$host}{$id}{date} = $time_st;
$SPAM{$host}{$id}{relay} = &clean_relay($relay);
$SPAM{$host}{$id}{from} = &edecode($from);
push(@{$SPAM{$host}{$id}{to}}, &edecode($to));
if ($CONFIG{SPAM_DETAIL}) {
$SPAMDETAIL{$host}{$id}{date} = $time_st;
$SPAMDETAIL{$host}{$id}{type} = 'policydweight';
$SPAMDETAIL{$host}{$id}{spam} = $spam;
$SPAMDETAIL{$host}{$id}{score} = $score;
}
# Parse spam found in cache by postfix/policyd-weight
} elsif ($str =~ m# action=DUNNO\s+(.*); rate: ([^;]+); <client=([^>]+)> <helo=([^>]+)> <from=([^>]+)> <to=([^>]+)>; delay.*#) {
my $spam = $1;
my $score = $2;
my $relay = $3;
my $from = $5;
my $to = $6;
my $id = &get_uniqueid();
$SPAM{$host}{$id}{spam} = 'policydweight';
$SPAM{$host}{$id}{date} = $time_st;
$SPAM{$host}{$id}{relay} = &clean_relay($relay);
$SPAM{$host}{$id}{from} = &edecode($from);
push(@{$SPAM{$host}{$id}{to}}, &edecode($to));
if ($CONFIG{SPAM_DETAIL}) {
$SPAMDETAIL{$host}{$id}{date} = $time_st;
$SPAMDETAIL{$host}{$id}{type} = 'policydweight';
$SPAMDETAIL{$host}{$id}{spam} = $spam;
$SPAMDETAIL{$host}{$id}{score} = $score;
}
# Parse spam found by dnsbl-milter
} elsif ($str =~ m#^([^:\s]+): Milter: from=([^,]+), reject=(.*)#) {
#debug_rule($1);
my $id = $1;
my $spam = $3;
$spam =~ s/ See .*//;
$spam =~ s/\/.*//;
$SPAM{$host}{$id}{spam} = 'dnsblmilter';
$SPAM{$host}{$id}{date} = $time_st;
$SPAM{$host}{$id}{from} = &edecode($2);
if ($CONFIG{SPAM_DETAIL}) {
$SPAMDETAIL{$host}{$id}{date} = $time_st;
$SPAMDETAIL{$host}{$id}{type} = 'dnsblmilter';
$SPAMDETAIL{$host}{$id}{spam} = $spam;
}
# Parse spam found by jchkmail
} elsif ($str =~ m#^([^:\s]+): Milter (add|change): header: X-j-chkmail-Status: (Spam|Unsure)(.*)#) {
#debug_rule($1);
$SPAM{$host}{$1}{spam} = 'jchkmail';
$SPAM{$host}{$1}{date} = $time_st;
if ($CONFIG{SPAM_DETAIL}) {
$SPAMDETAIL{$host}{$1}{type} = 'jchkmail';
$SPAMDETAIL{$host}{$1}{spam} = $3 . $4;
$SPAMDETAIL{$host}{$1}{date} = $time_st;
}
} elsif ($str =~ m#^([^:\s]+): Milter (add|change): header: X-j-chkmail-Score: .*> S=(.*)#) {
#debug_rule($1);
if ($CONFIG{SPAM_DETAIL}) {
$SPAMDETAIL{$host}{$1}{type} = 'jchkmail';
$SPAMDETAIL{$host}{$1}{score} = $3;
}
} elsif ($str =~ m#^([^:\s]+): Milter (delete|add|change): #) {
#debug_rule($1);
# Skip other milter header modication notice
return;
} elsif ($str =~ m#^([^:\s]+): Milter insert \(\d+\): #) {
#debug_rule($1);
# Skip other milter header insertion notice
return;
} elsif ($str =~ m#^([^:\s]+): Milter: connect: .*, ([^,]*)#) {
#debug_rule($1);
$SYSERR{$host}{$1}{date} = $time_st;
$SYSERR{$host}{$1}{message} = &clear_status($2);
} elsif ($str =~ m#^([^:\s]+): Milter (delete|add|change) #) {
#debug_rule($1);
# Skip other milter modication notice
return;
} elsif ($str =~ m#^([^:\s]+): Milter: data, reject=(.*)#) {
#debug_rule($1);
my $id = $1;
my $status = $2;
if ($status =~ /Blocked by SpamAssassin/) {
$SPAM{$host}{$id}{spam} = 'Blocked by SpamAssassin';
} elsif ($status =~ /554 5\.7\.1 (.*) detected by ClamAV.*/) {
$SPAM{$host}{$id}{spam} = "ClamAv $1"
} elsif ($status =~ /550 5\.7\.0 (.*) id=/) {
$SPAM{$host}{$id}{spam} = "eXpurGate $1"
} elsif ($status =~ /554 5\.7\.1 Command rejected/) {
return; # Already handle at Clamav virus report
} elsif ($status =~ /Please try again later/) {
return; # Skip greylist rejection
} elsif ($status =~ /Rejected by ESETS/) {
return; # handled above
} else {
$SPAM{$host}{$id}{spam} = $status;
}
# Local failure
} elsif ($str =~ m#^([^:\s]+): <([^>]+)>\.\.\. (.*)#) {
#debug_rule($1);
my $id = $KEEP_TEMPORARY{$1} || $1;
my $to = $2;
my $status = &clear_status($3);
delete $TO{$host}{$id}{queue_date};
delete $TO{$host}{$id}{queue_to};
$to = &edecode($to);
return if ($CONFIG{EXCLUDE_TO} && ($to =~ /^$CONFIG{EXCLUDE_TO}$/));
push(@{$TO{$host}{$id}{to}}, $to);
push(@{$TO{$host}{$id}{date}}, $time_st);
push(@{$TO{$host}{$id}{status}}, $status);
# POSTFIX queue From clause
} elsif ($str =~ m#^([^:\s]+): from=([^,]*), size=(\d+), nrcpt=(\d+).*#) {
#debug_rule($1);
my $id = $KEEP_TEMPORARY{$1} || $1;
my $from = lc($2);
$from ||= 'empty';
my $size = $3;
my $nrcpts = $4;
$FROM{$host}{$id}{date} = $time_st;
$FROM{$host}{$id}{from} = &edecode($from);
$FROM{$host}{$id}{size} = $size;
$FROM{$host}{$id}{nrcpts} = $nrcpts;
if (exists $SPAMPD{$host}{$from}) {
foreach my $to (keys %{$SPAMPD{$host}{$from}}) {
$SPAM{$host}{$id}{relay} = $FROM{$host}{$id}{relay};
$SPAM{$host}{$id}{rule} = $SPAMPD{$host}{$from}{$to}{rule};
$SPAM{$host}{$id}{spam} = $SPAMPD{$host}{$from}{$to}{spam};
$SPAM{$host}{$id}{date} = $SPAMPD{$host}{$from}{$to}{date};
$SPAM{$host}{$id}{status} = $SPAMPD{$host}{$from}{$to}{status};
}
delete $SPAMPD{$host}{$from};
}
# Catch POSTFIX SASL AUTH method
} elsif ($str =~ m#^([^:\s]+): client=([^,]+), sasl_method=([^,]+), sasl_username=(.*)#) {