| 1 | |||
| 2 | package SyslogScan::Daemon::SpamDetector::BadAddr::Sendmail; | ||
| 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(@ISA) = qw(SyslogScan::Daemon::SpamDetector::BadAddr::Plugin); | ||
| 10 | |||
| 11 | our $msgcachesize = 3_000; | ||
| 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 | ); | ||
| 32 | |||
| 33 | sub config_prefix { 'badsendmail_' } | ||
| 34 | |||
| 35 | sub parse_config_line { simple_config_line(\%defaults, @_); } | ||
| 36 | |||
| 37 | sub new | ||
| 38 | { | ||
| 39 | my $self = simple_new(\%defaults, @_); | ||
| 40 | $self->{msgcache} = {}; | ||
| 41 | die if ref($self->{msgcachesize}); | ||
| 42 | tie %{$self->{msgcache}}, 'Tie::Cache::LRU', $self->{msgcachesize} || $msgcachesize; | ||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | our $Mon; | ||
| 47 | our $Date; | ||
| 48 | our $iprx; | ||
| 49 | |||
| 50 | sub preconfig | ||
| 51 | { | ||
| 52 | my $self = shift; | ||
| 53 | $self->{Mon} = qr/$self->{rx_month}/; | ||
| 54 | $self->{Date} = $self->{rx_date} ? qr/$self->{rx_date}/ : qr/$self->{Mon} [ 1-3][0-9] \d\d:\d\d:\d\d/; | ||
| 55 | $self->{Extra} = qr/$self->{rx_extra}/; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub get_logs | ||
| 59 | { | ||
| 60 | my $self = shift; | ||
| 61 | my $Date = $self->{Date}; | ||
| 62 | return ( | ||
| 63 | $self->{logfile} => [ | ||
| 64 | #1st Oct 31 09:44:04 idiom sm-mta[48031]: k9VHi04a048031: <reineant@design-universe.com>... User unknown | ||
| 65 | #2nd Oct 20 00:00:12 idiom sm-mta[16655]: k9K6xf1f016655: from=<declarator@embrace.org.uk>, size=771, class=0, nrcpts=1, msgid=<46600514770495.39A8A178A2@SLDS5JHS>, proto=ESMTP, daemon=Daemon0, relay=pool-151-205-120-16.ny325.east.verizon.net [151.205.120.16] | ||
| 66 | qr{^$Date (\S+) sm-mta\[\d+\]: (\w+): from=<(.*?)>, size=\d+, class=\d+, nrcpts=\d+, (msgid)=<(.*?)>, proto=\S+, daemon=\S+, relay=(\S+) \[([\d\.]{8,40})\]}, | ||
| 67 | qr{^$Date (\S+) sm-mta\[\d+\]: (\w+): <(\S+?)>\.\.\. User (unknown)}, | ||
| 68 | ], | ||
| 69 | ); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub parse_logs | ||
| 73 | { | ||
| 74 | my ($self, $logfile, $rx) = @_; | ||
| 75 | my $debug = $self->{debug}; | ||
| 76 | my $logline = $_; | ||
| 77 | my ($host, $qid, $fromto, $what, $id, $relayname, $relayip) = ($1, $2, $3, $4, $5, $6, $7); | ||
| 78 | |||
| 79 | print "Matched $logline\n" if $debug >= 3; | ||
| 80 | my $queueid = "$host/$qid"; | ||
| 81 | if ($self->{Extra} && ! /$self->{Extra}/) { | ||
| 82 | # ignore | ||
| 83 | } elsif ($what eq 'unknown') { | ||
| 84 | push(@{$self->{msgcache}{$queueid}}, $fromto); | ||
| 85 | print "Unknown user: $queueid: $fromto\n" if $debug >= 2; | ||
| 86 | } elsif ($what eq 'msgid') { | ||
| 87 | for my $to (@{$self->{msgcache}{$queueid}}) { | ||
| 88 | print "Will report $queueid: $fromto -> $to from $relayip\n" if $debug; | ||
| 89 | $self->process_badaddr_match( | ||
| 90 | id => $id, | ||
| 91 | ip => $relayip, | ||
| 92 | from => $fromto, | ||
| 93 | relayname => $relayname, | ||
| 94 | match => 'BadAddr::Sendmail', | ||
| 95 | to => $to, | ||
| 96 | host => $host, | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | delete $self->{msgcache}{$queueid}; | ||
| 100 | } else { | ||
| 101 | warn "This should not happen"; | ||
| 102 | } | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | |||
| 106 | 1; | ||
| 107 | |||
| 108 | =head1 NAME | ||
| 109 | |||
| 110 | SyslogScan::Daemon::SpamDetector::BadAddr::Sendmail - notice bad email addresses in sendmail log files | ||
| 111 | |||
| 112 | =head1 SYNOPSIS | ||
| 113 | |||
| 114 | plugin SyslogScan::Daemon::SpamDetector as sd_ | ||
| 115 | |||
| 116 | sd_plugin SyslogScan::Daemon::SpamDetector::BadAddr as bad_ | ||
| 117 | |||
| 118 | bad_plugin SyslogScan::Daemon::SpamDetector::BadAddr::Sendmail | ||
| 119 | debug 1 | ||
| 120 | logfile /var/log/mail.info | ||
| 121 | msgcachesize 3000 | ||
| 122 | |||
| 123 | =head1 DESCRIPTION | ||
| 124 | |||
| 125 | Read Sendmail logs and notice attempts to send to addresses that don't exist. | ||
| 126 | |||
| 127 | =head1 CONFIGURATION PARAMETERS | ||
| 128 | |||
| 129 | The following configuration parameters are supported: | ||
| 130 | |||
| 131 | =over 4 | ||
| 132 | |||
| 133 | =item debug | ||
| 134 | |||
| 135 | Debugging on (1) or off (0). | ||
| 136 | |||
| 137 | =item logfile | ||
| 138 | |||
| 139 | Which logfile to watch (default: C</var/log/mail.log>). | ||
| 140 | |||
| 141 | =item msgcachesize | ||
| 142 | |||
| 143 | To do this mapping, multiple log lines must be matched. Partial | ||
| 144 | matches will be stored in a cache. This parameter sets the | ||
| 145 | size of the cache (default: 3000). | ||
| 146 | |||
| 147 | =back | ||
| 148 | |||
| 149 | =head1 SEE ALSO | ||
| 150 | |||
| 151 | L<SyslogScan::Daemon::SpamDetector> | ||
| 152 | L<SyslogScan::Daemon::SpamDetector::BadAddr> | ||
| 153 | L<SyslogScan::Daemon::SpamDetector::BadAddr::Postfix> | ||
| 154 | |||
| 155 | =head1 THANK THE AUTHOR | ||
| 156 | |||
| 157 | If you need high-speed internet services (T1, T3, OC3 etc), please | ||
| 158 | send me your request-for-quote. I have access to very good pricing: | ||
| 159 | you'll save money and get a great service. | ||
| 160 | |||
| 161 | =head1 LICENSE | ||
| 162 | |||
| 163 | Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com>. | ||
| 164 | This module may be used and distributed on the same terms | ||
| 165 | as Perl itself. | ||
| 166 |