| 1 | # Copyright (C) 2006, 2007; David Muir Sharnoff | ||
| 2 | |||
| 3 | package SyslogScan::Daemon::SpamDetector::Postfix; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use SyslogScan::Daemon::SpamDetector::Plugin; | ||
| 8 | use Plugins::SimpleConfig; | ||
| 9 | use Tie::Cache::LRU; | ||
| 10 | our $msgcachesize = 3_000; | ||
| 11 | our $badnscachesize = 1_000; | ||
| 12 | our(@ISA) = qw(SyslogScan::Daemon::SpamDetector::Plugin); | ||
| 13 | |||
| 14 | my %defaults = ( | ||
| 15 | rx_extra => '', | ||
| 16 | rx_month => '(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)', | ||
| 17 | rx_date => '', | ||
| 18 | logfile => '/var/log/mail.log', | ||
| 19 | debug => 0, | ||
| 20 | msgcachesize => sub { | ||
| 21 | my ($pkgself, $key, $value) = @_; | ||
| 22 | if (ref($pkgself)) { | ||
| 23 | $pkgself->{msgcachesize} = $value; | ||
| 24 | if ($pkgself->{msgcache}) { | ||
| 25 | my $t = tied(%{$pkgself->{msgcache}}); | ||
| 26 | $t->max_size($value); | ||
| 27 | } | ||
| 28 | } else { | ||
| 29 | $msgcachesize = $value; | ||
| 30 | } | ||
| 31 | }, | ||
| 32 | badnscachesize => sub { | ||
| 33 | my ($pkgself, $key, $value) = @_; | ||
| 34 | if (ref($pkgself)) { | ||
| 35 | $pkgself->{badnscachesize} = $value; | ||
| 36 | if ($pkgself->{badnscache}) { | ||
| 37 | my $t = tied(%{$pkgself->{badnscache}}); | ||
| 38 | $t->max_size($value); | ||
| 39 | } | ||
| 40 | } else { | ||
| 41 | $badnscachesize = $value; | ||
| 42 | } | ||
| 43 | }, | ||
| 44 | ); | ||
| 45 | |||
| 46 | sub config_prefix { 'sdpostfix_' } | ||
| 47 | |||
| 48 | sub parse_config_line { simple_config_line(\%defaults, @_); } | ||
| 49 | |||
| 50 | sub new | ||
| 51 | { | ||
| 52 | my $self = simple_new(\%defaults, @_); | ||
| 53 | $self->{msgcache} = {}; | ||
| 54 | die if ref($self->{msgcachesize}); | ||
| 55 | tie %{$self->{msgcache}}, 'Tie::Cache::LRU', $self->{msgcachesize} || $msgcachesize; | ||
| 56 | $self->{badnscache} = {}; | ||
| 57 | die if ref($self->{badnscachesize}); | ||
| 58 | tie %{$self->{badnscache}}, 'Tie::Cache::LRU', $self->{badnscachesize} || $badnscachesize; | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | our $Mon; | ||
| 63 | our $Date; | ||
| 64 | our $iprx; | ||
| 65 | |||
| 66 | sub preconfig | ||
| 67 | { | ||
| 68 | my $self = shift; | ||
| 69 | $self->{Mon} = qr/$self->{rx_month}/; | ||
| 70 | $self->{Date} = $self->{rx_date} ? qr/$self->{rx_date}/ : qr/$self->{Mon} [ 1-3][0-9] \d\d:\d\d:\d\d/; | ||
| 71 | $self->{Extra} = qr/$self->{rx_extra}/; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub get_logs | ||
| 75 | { | ||
| 76 | my $self = shift; | ||
| 77 | my $Date = $self->{Date}; | ||
| 78 | return ( | ||
| 79 | # Oct 24 14:19:27 ravel postfix/smtpd[968]: D4E5E1DC379: client=outbound1.internet-mail-service.net[216.240.47.192] | ||
| 80 | # Oct 24 14:19:27 ravel postfix/cleanup[94003]: D4E5E1DC379: message-id=<200610242119.k9OLJR7l031900@idiom.com> | ||
| 81 | # Mar 27 15:56:32 sh postfix/smtpd[6043]: AD87BC04EFAA6: client=27.232.68-86.rev.gaoland.net[86.68.232.27] | ||
| 82 | $self->{logfile} => [ | ||
| 83 | qr{^$Date (\S+) postfix\S*/smtpd\[\d+\]: ([A-Z0-9]{9,18}): (client)=(\S*)\[([\d\.]{8,20})\]}, | ||
| 84 | qr{^$Date (\S+) postfix\S*/cleanup\[\d+\]: ([A-Z0-9]{9,18}): (message-id)=<(.*?)>}, | ||
| 85 | qr{^$Date (\S+) postfix\S*/smtpd\[\d+\](:) (warning): (\S+): hostname \S+ verification failed:}, | ||
| 86 | ], | ||
| 87 | ); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub parse_logs | ||
| 91 | { | ||
| 92 | my ($self, $logfile, $rx) = @_; | ||
| 93 | my $debug = $self->{debug}; | ||
| 94 | my $logline = $_; | ||
| 95 | my ($host, $msg, $what, $more1, $more2) = ($1, $2, $3, $4, $5); | ||
| 96 | |||
| 97 | my $queueid = "$host/$msg"; | ||
| 98 | |||
| 99 | if ($self->{Extra} && ! /$self->{Extra}/) { | ||
| 100 | print "Ignoring match of $logline\n" if $debug >= 2; | ||
| 101 | } elsif ($what eq 'client') { | ||
| 102 | my ($hostname, $ip) = ($more1, $more2); | ||
| 103 | $self->{msgcache}{$queueid}{ip} = [ $ip, $hostname ]; | ||
| 104 | print "Postfix: match client line, ip = $ip\n" if $debug; | ||
| 105 | if ($self->{msgcache}{$queueid}{msgid}) { | ||
| 106 | for my $msgid (@{$self->{msgcache}{$queueid}{msgid}}) { | ||
| 107 | print "Postfix IDMAP: $msgid -> $ip\n" if $debug; | ||
| 108 | $self->process_spam_match( | ||
| 109 | id => $msgid, | ||
| 110 | ip => $ip, | ||
| 111 | status => 'idmap', | ||
| 112 | match => 'Postfix', | ||
| 113 | host => $host, | ||
| 114 | relayname => $hostname, | ||
| 115 | DNSbad => $self->{badnscache}{$ip} || 0, | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } elsif ($what eq 'message-id') { | ||
| 120 | my ($id) = ($more1); | ||
| 121 | print "Postfix: match message id line, id = $id\n" if $debug >= 2; | ||
| 122 | if ($self->{msgcache}{$queueid}{ip}) { | ||
| 123 | my ($ip, $hostname) = @{$self->{msgcache}{$queueid}{ip}}; | ||
| 124 | print "Postfix IDMAP: $id -> $ip\n" if $debug; | ||
| 125 | $self->process_spam_match( | ||
| 126 | id => $id, | ||
| 127 | ip => $ip, | ||
| 128 | status => 'idmap', | ||
| 129 | match => 'Postfix', | ||
| 130 | host => $host, | ||
| 131 | relayname => $hostname, | ||
| 132 | DNSbad => $self->{badnscache}{$ip} || 0, | ||
| 133 | ); | ||
| 134 | } else { | ||
| 135 | push(@{$self->{msgcache}{$queueid}{msgid}}, $id); | ||
| 136 | } | ||
| 137 | } elsif ($what eq 'warning') { | ||
| 138 | my $ip = $more1; | ||
| 139 | $self->{badnscache}{$ip} = 1; | ||
| 140 | } else { | ||
| 141 | print "OOPS!, Postfix module broken\n"; | ||
| 142 | } | ||
| 143 | return (); | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | =head1 NAME | ||
| 149 | |||
| 150 | SyslogScan::Daemon::SpamDetector::Postfix - record incomming messages | ||
| 151 | |||
| 152 | =head1 SYNOPSIS | ||
| 153 | |||
| 154 | plugin SyslogScan::Daemon::SpamDetector as sd_ | ||
| 155 | |||
| 156 | sd_plugin SyslogScan::Daemon::SpamDetector::Postfix | ||
| 157 | debug 0 | ||
| 158 | logfile /var/log/mail.info | ||
| 159 | msgcachesize 3000 | ||
| 160 | rx_extra 'ingore_lines_without_this_string' | ||
| 161 | |||
| 162 | =head1 DESCRIPTION | ||
| 163 | |||
| 164 | SyslogScan::Daemon::SpamDetector::Postfix watches the mail log file and | ||
| 165 | notices which Message-IDs came from which IP address. | ||
| 166 | |||
| 167 | =head1 CONFIGURATION PARAMETERS | ||
| 168 | |||
| 169 | The following configuration parameters are supported: | ||
| 170 | |||
| 171 | =over 4 | ||
| 172 | |||
| 173 | =item debug | ||
| 174 | |||
| 175 | Debugging on (1) or off (0). | ||
| 176 | |||
| 177 | =item logfile | ||
| 178 | |||
| 179 | Which logfile to watch (default: C</var/log/mail.log>). | ||
| 180 | |||
| 181 | =item msgcachesize | ||
| 182 | |||
| 183 | To do this mapping, multiple log lines must be matched. Partial | ||
| 184 | matches will be stored in a cache. This parameter sets the | ||
| 185 | size of the cache (default: 3000). | ||
| 186 | |||
| 187 | =item rx_extra | ||
| 188 | |||
| 189 | Ignore log lines that don't match a regular expression. Note: this module | ||
| 190 | looks for two different kinds of Postfix log lines the regular expression | ||
| 191 | needs to work for both sets. One kind of log line always includes | ||
| 192 | C<: client=> and the other always includes C<: message-id=>. | ||
| 193 | |||
| 194 | =back | ||
| 195 | |||
| 196 | =head1 SEE ALSO | ||
| 197 | |||
| 198 | L<SyslogScan::Daemon::SpamDetector> | ||
| 199 | L<SyslogScan::Daemon::SpamDetector::Sendmail> | ||
| 200 | |||
| 201 | =head1 THANK THE AUTHOR | ||
| 202 | |||
| 203 | If you need high-speed internet services (T1, T3, OC3 etc), please | ||
| 204 | send me your request-for-quote. I have access to very good pricing: | ||
| 205 | you'll save money and get a great service. | ||
| 206 | |||
| 207 | =head1 LICENSE | ||
| 208 | |||
| 209 | Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com>. | ||
| 210 | This module may be used and distributed on the same terms | ||
| 211 | as Perl itself. | ||
| 212 |