| 1 | |||
| 2 | package SyslogScan::Daemon::SpamDetector::BadAddr::Postfix; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use SyslogScan::Daemon::SpamDetector::BadAddr::Plugin; | ||
| 7 | use Plugins::SimpleConfig; | ||
| 8 | use Tie::Cache::LRU; | ||
| 9 | our $msgcachesize = 3_000; | ||
| 10 | our $badnscachesize = 1_000; | ||
| 11 | our(@ISA) = qw(SyslogScan::Daemon::SpamDetector::BadAddr::Plugin); | ||
| 12 | |||
| 13 | my %defaults = ( | ||
| 14 | rx_extra => '.', | ||
| 15 | rx_month => '(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)', | ||
| 16 | rx_date => '', | ||
| 17 | logfile => '/var/log/mail.log', | ||
| 18 | debug => 0, | ||
| 19 | msgcachesize => sub { | ||
| 20 | my ($pkgself, $key, $value) = @_; | ||
| 21 | if (ref($pkgself)) { | ||
| 22 | $pkgself->{msgcachesize} = $value; | ||
| 23 | if ($pkgself->{msgcache}) { | ||
| 24 | my $t = tied(%{$pkgself->{msgcache}}); | ||
| 25 | $t->max_size($value); | ||
| 26 | } | ||
| 27 | } else { | ||
| 28 | $msgcachesize = $value; | ||
| 29 | } | ||
| 30 | }, | ||
| 31 | badnscachesize => sub { | ||
| 32 | my ($pkgself, $key, $value) = @_; | ||
| 33 | if (ref($pkgself)) { | ||
| 34 | $pkgself->{badnscachesize} = $value; | ||
| 35 | if ($pkgself->{badnscache}) { | ||
| 36 | my $t = tied(%{$pkgself->{badnscache}}); | ||
| 37 | $t->max_size($value); | ||
| 38 | } | ||
| 39 | } else { | ||
| 40 | $badnscachesize = $value; | ||
| 41 | } | ||
| 42 | }, | ||
| 43 | ); | ||
| 44 | |||
| 45 | sub config_prefix { 'badpostfix_' } | ||
| 46 | |||
| 47 | sub parse_config_line { simple_config_line(\%defaults, @_); } | ||
| 48 | |||
| 49 | sub new | ||
| 50 | { | ||
| 51 | my $self = simple_new(\%defaults, @_); | ||
| 52 | $self->{msgcache} = {}; | ||
| 53 | die if ref($self->{msgcachesize}); | ||
| 54 | tie %{$self->{msgcache}}, 'Tie::Cache::LRU', $self->{msgcachesize} || $msgcachesize; | ||
| 55 | $self->{badnscache} = {}; | ||
| 56 | die if ref($self->{badnscachesize}); | ||
| 57 | tie %{$self->{badnscache}}, 'Tie::Cache::LRU', $self->{badnscachesize} || $badnscachesize; | ||
| 58 | return $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | our $Mon; | ||
| 62 | our $Date; | ||
| 63 | our $iprx; | ||
| 64 | |||
| 65 | sub preconfig | ||
| 66 | { | ||
| 67 | my $self = shift; | ||
| 68 | $self->{Mon} = qr/$self->{rx_month}/; | ||
| 69 | $self->{Date} = $self->{rx_date} ? qr/$self->{rx_date}/ : qr/$self->{Mon} [ 1-3][0-9] \d\d:\d\d:\d\d/; | ||
| 70 | $self->{Extra} = qr/$self->{rx_extra}/; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub get_logs | ||
| 74 | { | ||
| 75 | my $self = shift; | ||
| 76 | my $Date = $self->{Date}; | ||
| 77 | return ( | ||
| 78 | |||
| 79 | $self->{logfile} => [ | ||
| 80 | # Oct 31 05:02:11 ravel postfix/smtpd[89130]: 14CF41DBEFB: client=customer.optindirectmail.74.sls-hosting.com[204.14.1.74] | ||
| 81 | $self->rx_invoke(qr{^$Date (\S+) postfix\S*/smtpd\[\d+\]: ([A-Z0-9]{9,15}): client=(\S*)\[([\d\.]{8,20})\]}, sub { | ||
| 82 | my ($self, $logfile, $rx) = @_; | ||
| 83 | my ($host, $msg, $name, $ip) = ($1, $2, $3); | ||
| 84 | my $qid = "$host/$msg"; | ||
| 85 | print "badaddr postfix $qid is from $ip\n" if $self->{debug} >= 2; | ||
| 86 | $self->{msgcache}{$qid} = { | ||
| 87 | ip => $ip, | ||
| 88 | relayname => $name, | ||
| 89 | }; | ||
| 90 | return (); | ||
| 91 | }), | ||
| 92 | # Oct 31 05:02:11 ravel postfix/cleanup[78647]: 14CF41DBEFB: message-id=<5889984462.20061031070153@baraskaka.com> | ||
| 93 | $self->rx_invoke(qr{^$Date (\S+) postfix\S*/cleanup\[\d+\]: ([A-Z0-9]{9,15}): message-id=<(.*?)>}, sub { | ||
| 94 | my ($self, $logfile, $rx) = @_; | ||
| 95 | my ($host, $msg, $id) = ($1, $2, $3); | ||
| 96 | my $qid = "$host/$msg"; | ||
| 97 | print "badaddr postfix $qid msgid is $id\n" if $self->{debug} >= 2; | ||
| 98 | $self->{msgcache}{$qid}{id} = $id; | ||
| 99 | return (); | ||
| 100 | }), | ||
| 101 | # Oct 31 05:02:11 ravel postfix/qmgr[84199]: 14CF41DBEFB: from=<shannon@baraskaka.com>, size=18688, nrcpt=1 (queue active) | ||
| 102 | $self->rx_invoke(qr{^$Date (\S+) postfix\S*/\S+\[\d+\]: ([A-Z0-9]{9,15}): from=<(\S+?)>,}, sub { | ||
| 103 | my ($self, $logfile, $rx) = @_; | ||
| 104 | my ($host, $msg, $from) = ($1, $2, $3); | ||
| 105 | my $qid = "$host/$msg"; | ||
| 106 | print "badaddr postfix $qid message from $from\n" if $self->{debug} >= 2; | ||
| 107 | $self->{msgcache}{$qid}{from} = $from; | ||
| 108 | return (); | ||
| 109 | }), | ||
| 110 | # Oct 31 05:02:11 ravel postfix/local[87055]: 14CF41DBEFB: to=<nosuchuser@n2.net>, orig_to=<lakas@n2.net>, relay=local, delay=1, status=bounced (unknown user: "nosuchuser") | ||
| 111 | $self->rx_invoke(qr{^$Date (\S+) postfix\S*/\S+\[\d+\]: ([A-Z0-9]{9,15}): to=<\S+?>, orig_to=<(\S+?)>, .*?status=bounced \(unknown user: ".*?"\)}, sub { | ||
| 112 | my ($self, $logfile, $rx) = @_; | ||
| 113 | my ($host, $msg, $to) = ($1, $2, $3); | ||
| 114 | my $qid = "$host/$msg"; | ||
| 115 | my $ip = $self->{msgcache}{$qid}{ip}; | ||
| 116 | if ($ip) { | ||
| 117 | print "Postfix badaddr $to\n" if $self->{debug}; | ||
| 118 | return ( | ||
| 119 | %{$self->{msgcache}{$qid}}, | ||
| 120 | to => $to, | ||
| 121 | host => $host, | ||
| 122 | match => 'BadAddr::Postfix', | ||
| 123 | DNSbad => $self->{badnscache}{$ip} || 0, | ||
| 124 | ); | ||
| 125 | } else { | ||
| 126 | print "No IP mapping for postfix bad addr $to\n" if $self->{debug}; | ||
| 127 | return (); | ||
| 128 | } | ||
| 129 | }), | ||
| 130 | # Or | ||
| 131 | # | ||
| 132 | # Oct 31 06:27:43 internetmailservice postfix/smtpd[21369]: NOQUEUE: reject: RCPT from 189.Red-81-37-230.dynamicIP.rima-tde.net[81.37.230.189]: 550 <tonna@mindsync.com>: Recipient address rejected: User unknown in virtual mailbox table; from=<romellor@aol.com> to=<tonna@mindsync.com> proto=SMTP helo=<mail.mindsync.com> | ||
| 133 | $self->rx_invoke(qr{^$Date (\S+) postfix\S*/smtpd\[\d+\]: NOQUEUE: reject: RCPT from \S+?\[([\d\.]+)\]: 550 <(\S+)>: Recipient address rejected: User unknown in virtual mailbox table}, sub { | ||
| 134 | my ($self, $logfile, $rx) = @_; | ||
| 135 | my ($host, $ip, $to) = ($1, $2, $3); | ||
| 136 | print "Postfix NOQUEUE reject to $to from $ip\n" if $self->{debug}; | ||
| 137 | return ( | ||
| 138 | host => $host, | ||
| 139 | ip => $ip, | ||
| 140 | to => $to, | ||
| 141 | DNSbad => $self->{badnscache}{$ip} || 0, | ||
| 142 | match => 'BadAddr::Postfix(NOQUEUE)', | ||
| 143 | ); | ||
| 144 | }), | ||
| 145 | # Jan 24 14:47:56 eatspam2 postfix/smtpd[3425]: warning: 88.226.249.119: hostname dsl88-226-63863.ttnet.net.tr verification failed: Name or service not known | ||
| 146 | $self->rx_invoke(qr{^$Date (\S+) postfix\S*/smtpd\[\d+\]: warning: (\S+): hostname \S+ verification failed:}, sub { | ||
| 147 | my ($self, $logfile, $rx) = @_; | ||
| 148 | my ($host, $ip) = ($1, $2); | ||
| 149 | $self->{badnscache}{$ip} = 1; | ||
| 150 | return (); | ||
| 151 | }), | ||
| 152 | ], | ||
| 153 | ); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub rx_invoke | ||
| 157 | { | ||
| 158 | my ($self, $rx, $code) = @_; | ||
| 159 | $self->{rxmap}{$rx} = $code; | ||
| 160 | return $rx; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub parse_logs | ||
| 164 | { | ||
| 165 | my ($self, $logfile, $rx) = @_; | ||
| 166 | my $code = $self->{rxmap}{$rx} || do { warn "map missing for $rx"; return() }; | ||
| 167 | &$code($self, $logfile, $rx); | ||
| 168 | } | ||
| 169 | |||
| 170 | 1; | ||
| 171 | |||
| 172 | =head1 NAME | ||
| 173 | |||
| 174 | SyslogScan::Daemon::SpamDetector::BadAddr::Postfix - notice bad email addresses in postfix log files | ||
| 175 | |||
| 176 | =head1 SYNOPSIS | ||
| 177 | |||
| 178 | plugin SyslogScan::Daemon::SpamDetector as sd_ | ||
| 179 | |||
| 180 | sd_plugin SyslogScan::Daemon::SpamDetector::BadAddr as bad_ | ||
| 181 | |||
| 182 | bad_plugin SyslogScan::Daemon::SpamDetector::BadAddr::Postfix | ||
| 183 | debug 1 | ||
| 184 | logfile /var/log/mail.info | ||
| 185 | msgcachesize 3000 | ||
| 186 | badnscachesize 1000 | ||
| 187 | |||
| 188 | =head1 DESCRIPTION | ||
| 189 | |||
| 190 | Read Postfix logs and notice attempts to send to addresses that don't exist. | ||
| 191 | |||
| 192 | =head1 CONFIGURATION PARAMETERS | ||
| 193 | |||
| 194 | The following configuration parameters are supported: | ||
| 195 | |||
| 196 | =over 4 | ||
| 197 | |||
| 198 | =item debug | ||
| 199 | |||
| 200 | Debugging on (1) or off (0). | ||
| 201 | |||
| 202 | =item logfile | ||
| 203 | |||
| 204 | Which logfile to watch (default: C</var/log/mail.log>). | ||
| 205 | |||
| 206 | =item msgcachesize | ||
| 207 | |||
| 208 | To do this mapping, multiple log lines must be matched. Partial | ||
| 209 | matches will be stored in a cache. This parameter sets the | ||
| 210 | size of the cache (default: 3000). | ||
| 211 | |||
| 212 | =item badnscachesize | ||
| 213 | |||
| 214 | To keep track of which clients don't have correct DNS entries, | ||
| 215 | a cache is kept. This configuration item sets the size of the | ||
| 216 | cache (default: 1000). | ||
| 217 | |||
| 218 | =back | ||
| 219 | |||
| 220 | =head1 SEE ALSO | ||
| 221 | |||
| 222 | L<SyslogScan::Daemon::SpamDetector> | ||
| 223 | L<SyslogScan::Daemon::SpamDetector::BadAddr> | ||
| 224 | |||
| 225 | =head1 THANK THE AUTHOR | ||
| 226 | |||
| 227 | If you need high-speed internet services (T1, T3, OC3 etc), please | ||
| 228 | send me your request-for-quote. I have access to very good pricing: | ||
| 229 | you'll save money and get a great service. | ||
| 230 | |||
| 231 | =head1 LICENSE | ||
| 232 | |||
| 233 | Copyright(C) 2006,2007 David Muir Sharnoff <muir@idiom.com>. | ||
| 234 | This module may be used and distributed on the same terms | ||
| 235 | as Perl itself. | ||
| 236 |