BlacklistDetector/Postfix.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 70 CC 5 SCAR 17.9
CC
5
Cov
0%
preconfig
12.0
parse_logs
12.0
config_prefix
2.0
CRAP 6.0
1  
2 package SyslogScan::Daemon::BlacklistDetector::Postfix;
3  
4 use strict;
5 use warnings;
6 use SyslogScan::Daemon::BlacklistDetector::Plugin;
7 use Plugins::SimpleConfig;
8 our(@ISA) = qw(SyslogScan::Daemon::BlacklistDetector::Plugin);
9  
10 my %defaults = (
11 rx_month => '(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)',
12 rx_date => '',
13 rx_ourIP => '',
14 logpath => '/var/log/mail.log',
15 debug => 0,
16 );
17  
18 sub config_prefix { 'bldpostfix_' }
19  
20 sub parse_config_line { simple_config_line(\%defaults, @_); }
21  
22 sub new { simple_new(\%defaults, @_); }
23  
24 our $Mon;
25 our $Date;
26 our $iprx;
27  
28 sub preconfig
29 {
30 my $self = shift;
31 $self->{Mon} = qr/$self->{rx_month}/;
32 $self->{Date} = $self->{rx_date} ? qr/$self->{rx_date}/ : qr/$self->{Mon} [ 1-3][0-9] \d\d:\d\d:\d\d/;
33 if ($self->{rx_ourIP}) {
34 $self->{iprx} = qr/$self->{rx_ourIP}/;
35 } else {
36 require Sys::Hostname;
37 import Sys::Hostname;
38 my $host = hostname();
39 my $iaddr = gethostbyname($host);
40 my $addr = join('.', unpack('C4', $iaddr));
41 $self->{iprx} = qr/\b\Q$addr\E\b/;
42 }
43 }
44  
45 sub get_logs
46 {
47 my $self = shift;
48 my $Date = $self->{Date};
49 return (
50 $self->{logpath} => [
51 qr{^$Date \S+ postfix(?:-(\S+))?/smtp\[\d+\]: \w+: to=<([^@]+@([^>]+))>, .*, status=(bounced)(.*\b(?i:blacklist(ed)?|spamming|spam list|removal|remove|block list|blocked for abuse|Spam source|rejected (by .* )?for policy reasons)\b.*)},
52 qr{^$Date \S+ postfix(?:-(\S+))?/smtp\[\d+\]: \w+: to=<([^@]+@([^>]+))>, .*, status=(deferred)(.*Rejected: \S+ listed at http.*)},
53 qr{^$Date \S+ postfix(?:-(\S+))?/smtp\[\d+\]: \w+: to=<([^@]+@([^>]+))>, .*, status=(deferred)(.* blocked using .* Please see http.*)},
54 qr{^$Date \S+ postfix(?:-(\S+))?/smtp\[\d+\]: \w+: to=<([^@]+@([^>]+))>, .*, status=(deferred)(.*421-:\s*postmaster.info.aol.com/errors.*)},
55 qr{^$Date \S+ postfix(?:-(\S+))?/smtp\[\d+\]: \w+: to=<([^@]+@([^>]+))>, .*, status=(deferred)(.*server refused to talk to me: 550 Access denied\.\.\.[0-9a-f]{65}.*)},
56 qr{^$Date \S+ postfix(?:-(\S+))?/smtp\[\d+\]: \w+: to=<([^@]+@([^>]+))>, .*, status=(deferred|bounced)(.*http://postmaster.info.aol.com/errors/.*)},
57 qr{^$Date \S+ postfix(?:-(\S+))?/smtp\[\d+\]: \w+: to=<([^@]+@([^>]+))>, .*, status=(deferred)(.*\b(?i:greylisted)\b.*)},
58 ],
59 );
60 }
61  
62 sub parse_logs
63 {
64 my ($self, $logfile, $rx) = @_;
65 my $debug = $self->{debug};
66 my $logline = $_;
67 my ($prefix, $to_address, $destdomain, $status, $error) = ($1, $2, $3, $4, $5);
68 my $sourceip = ($logline =~ m/($self->{iprx})/)
69 ? $1
70 : 'unknown';
71 print STDERR "FROM $sourceip TO $destdomain $status: $error\n" if $debug;
72 my %info = (
73 prefix => $prefix,
74 sourceip => $sourceip,
75 to_address => $to_address,
76 logline => $logline,
77 mobj => $self,
78 destdomain => $destdomain,
79 status => $status,
80 logfile => $logfile,
81 rx => $rx,
82 error => $error,
83 );
84 return %info;
85 }
86  
87 1;
88  
89 =head1 NAME
90  
91 SyslogScan::Daemon::BlacklistDetector::Postfix - recognize the postfix mailer's bounce lines
92  
93 =head1 SYNOPSIS
94  
95 bld_plugin SyslogScan::Daemon::BlacklistDetector::Postfix
96 debug 1
97 rx_ourIP 216\.240\.47\.\d+
98 logpath /var/log/mail.log
99  
100 =head1 DESCRIPTION
101  
102 SyslogScan::Daemon::BlacklistDetector::Postfix
103 knows where to find the postfix MTAs log files and how
104 to parse them for bounce information.
105  
106 SyslogScan::Daemon::BlacklistDetector::Postfix is a plugin for
107 L<SyslogScan::Daemon::BlacklistDetector>.
108 The SYNOPSIS shows the configuration
109 lines you might use in C</etc/syslogscand.conf> to turn on
110 the postfix parsing.
111  
112 =head1 CONFIGURATION PARAMETERS
113  
114 SyslogScan::Daemon::BlacklistDetector::Postfix defines the following configuration
115 parameters which may be given in indented lines that follow
116 C<plugin SyslogScan::Daemon::BlacklistDetector::Postfix> or with the
117 confuration prefix (C<blden_>) anywhere in the configuration file after the
118 C<plugin SyslogScan::Daemon::BlacklistDetector::Postfix> line.
119  
120 =over 15
121  
122 =item debug
123  
124 (default 0) Turn on debugging.
125  
126 =item rx_ourIP
127  
128 (no default, optional) A regular expression to match the part of the log line that
129 would represent the sending IP address.
130  
131 =item logpath
132  
133 (default C</var/log/mail.log>)
134  
135 =back
136  
137 =head1 parse_logs() INFO
138  
139 In addition to the required return elements, parse_logs() also returns:
140  
141 =over 15
142  
143 =item prefix
144  
145 If postfix is logging itself as C<postfix-somthing> instead of C<postfix>, then
146 C<prefix> will be the C<something>.
147  
148 =item rx
149  
150 The regular expression that matched.
151  
152 =item mobj
153  
154 The SyslogScan::Daemon::BlacklistDetector::Postfix object.
155  
156 =back
157  
158 =head1 SEE ALSO
159  
160 The context for the blacklist detector:
161 L<SyslogScan::Daemon::BlacklistDetector>
162  
163 =head1 LICENSE
164  
165 Copyright (C) 2006, David Muir Sharnoff <muir@idiom.com>
166  
167 This module may be used and copied on the same terms as Perl
168 itself.
169