| 1 | |||
| 2 | package SyslogScan::Daemon::SpamDetector::Filter; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use SyslogScan::Daemon::SpamDetector::Plugin; | ||
| 7 | use Plugins::SimpleConfig; | ||
| 8 | use Tie::Cache::LRU; | ||
| 9 | our $msgcachesize = 3_000; | ||
| 10 | our(@ISA) = qw(SyslogScan::Daemon::SpamDetector::Plugin); | ||
| 11 | |||
| 12 | my %defaults = ( | ||
| 13 | status => 'spam', | ||
| 14 | match => '', | ||
| 15 | field => '', | ||
| 16 | accept => undef, | ||
| 17 | deny => undef, | ||
| 18 | debug => 0, | ||
| 19 | acceptfirst => 1, | ||
| 20 | logname => 'FILTER', | ||
| 21 | ); | ||
| 22 | |||
| 23 | sub config_prefix { 'sdfilter_' } | ||
| 24 | |||
| 25 | sub parse_config_line { simple_config_line(\%defaults, @_); } | ||
| 26 | |||
| 27 | sub new | ||
| 28 | { | ||
| 29 | my $self = simple_new(\%defaults, @_); | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub preconfig | ||
| 34 | { | ||
| 35 | my $self = shift; | ||
| 36 | $self->{rx_match} = qr/$self->{match}/; | ||
| 37 | $self->{rx_accept} = $self->{accept} | ||
| 38 | ? qr/$self->{accept}/ | ||
| 39 | : undef; | ||
| 40 | $self->{rx_deny} = $self->{deny} | ||
| 41 | ? qr/$self->{deny}/ | ||
| 42 | : undef; | ||
| 43 | $self->{rx_status} = qr/$self->{status}/; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub filter | ||
| 47 | { | ||
| 48 | my ($self, %info) = @_; | ||
| 49 | if ($self->{debug} >= 5) { | ||
| 50 | print "$self->{logname}:\n"; | ||
| 51 | for my $k (sort keys %info) { | ||
| 52 | printf "%15s= %s\n", $k, $info{$k}; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | unless ($info{status} =~ /$self->{rx_status}/) { | ||
| 56 | print "$self->{logname}: Status $info{status} not $self->{status}\n" if $self->{debug} >= 4; | ||
| 57 | return undef; | ||
| 58 | } | ||
| 59 | unless ($info{match} =~ /$self->{rx_match}/) { | ||
| 60 | print "$self->{logname}: Match $info{match} not $self->{match}\n" if $self->{debug} >= 3; | ||
| 61 | return undef; | ||
| 62 | } | ||
| 63 | unless (defined $info{$self->{field}}) { | ||
| 64 | print "$self->{logname}: field '$self->{field}' undefined\n" if $self->{debug}; | ||
| 65 | if ($self->{debug} && $self->{debug} < 5 && $info{ip}) { | ||
| 66 | print "$self->{logname}:\n"; | ||
| 67 | for my $k (sort keys %info) { | ||
| 68 | printf "%15s= %s\n", $k, $info{$k}; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | return undef; | ||
| 72 | } | ||
| 73 | if ($self->{acceptfirst} && defined($self->{accept}) && $info{$self->{field}} =~ /$self->{rx_accept}/) { | ||
| 74 | print "$self->{logname}: accept $self->{field} = $info{$self->{field}}\n" if $self->{debug} >= 2; | ||
| 75 | return 1; | ||
| 76 | } | ||
| 77 | if (defined($self->{deny}) and $info{$self->{field}} =~ /$self->{rx_deny}/) { | ||
| 78 | print "$self->{logname}: deny $self->{field} = $info{$self->{field}}\n" if $self->{debug}; | ||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | if (! $self->{acceptfirst} and defined($self->{accept}) and $info{$self->{field}} =~ /$self->{rx_accept}/) { | ||
| 82 | print "$self->{logname}: accept $self->{field} = $info{$self->{field}}\n" if $self->{debug} >= 2; | ||
| 83 | return 1; | ||
| 84 | } | ||
| 85 | return undef; | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | =head1 NAME | ||
| 91 | |||
| 92 | SyslogScan::Daemon::SpamDetector::Filter - filter reports | ||
| 93 | |||
| 94 | =head1 SYNOPSIS | ||
| 95 | |||
| 96 | plugin SyslogScan::Daemon::SpamDetector as sd_ | ||
| 97 | |||
| 98 | sd_plugin SyslogScan::Daemon::SpamDetector::Filter | ||
| 99 | status spam | ||
| 100 | match SpamAssassin | ||
| 101 | field relayname | ||
| 102 | acceptfirst 1 | ||
| 103 | accept '(?:\bdynamic\b|\badsl\d*\b|\bcable\b|\.dhcp\.|\.dyn\.)' | ||
| 104 | deny '.' | ||
| 105 | debug 0 | ||
| 106 | |||
| 107 | =head1 DESCRIPTION | ||
| 108 | |||
| 109 | SyslogScan::Daemon::SpamDetector::Filter looks at spam reported via | ||
| 110 | L<SyslogScan::Daemon::SpamDetector>'s C<process_spam_match()> function. | ||
| 111 | It acts as a filter and can block reports. | ||
| 112 | |||
| 113 | This module can be used with the L<SyslogScan::Daemon::SpamDetector::SpamAssassin> | ||
| 114 | module and the L<SyslogScan::Daemon::SpamDetector::BlockList> | ||
| 115 | module to prevent too many sites from being blocked. The configuration | ||
| 116 | in the L</SYNOPSIS> is an example of how to do this. | ||
| 117 | |||
| 118 | =head1 CONFIGURATION PARAMETERS | ||
| 119 | |||
| 120 | The following configuration parameters are supported: | ||
| 121 | |||
| 122 | =over 4 | ||
| 123 | |||
| 124 | =item debug | ||
| 125 | |||
| 126 | Debugging on (1) or off (0). | ||
| 127 | |||
| 128 | =item logname | ||
| 129 | |||
| 130 | A string to prepend to debug and log output. (Default: C<FILTER>) | ||
| 131 | |||
| 132 | =item status | ||
| 133 | |||
| 134 | What kind of report are we looking at? Choices are: C<ham>, C<spam>, or C<idmap>. | ||
| 135 | (Default: C<spam>). When called from L<SyslogScan::Daemon::SpamDetector::BadAddr>, | ||
| 136 | the status will be C<badaddr>. | ||
| 137 | |||
| 138 | =item match | ||
| 139 | |||
| 140 | What kind of match are we looking at? Each module that makes reports | ||
| 141 | sets this parameter. Current choices are: C<SpamAssassin>, C<Postfix>, | ||
| 142 | C<Sendmail>, C<SpamSink>, and various C<BadAddr::*>. A regualar | ||
| 143 | expression match is done on this field. No default. | ||
| 144 | |||
| 145 | =item field | ||
| 146 | |||
| 147 | Which field from C<%info> will we examine with the accept and deny | ||
| 148 | regular expressions? No default. | ||
| 149 | |||
| 150 | =item accept | ||
| 151 | |||
| 152 | What is the regular expression for passing this filter? | ||
| 153 | |||
| 154 | =item deny | ||
| 155 | |||
| 156 | What is the regular expression for being rejected by this filter? | ||
| 157 | |||
| 158 | =item acceptfirst | ||
| 159 | |||
| 160 | Check the accept expression first? (Default: 1). | ||
| 161 | |||
| 162 | =back | ||
| 163 | |||
| 164 | =head1 SEE ALSO | ||
| 165 | |||
| 166 | L<SyslogScan::Daemon::SpamDetector> | ||
| 167 | L<SyslogScan::Daemon::SpamDetector::SpamAssassin> | ||
| 168 | L<SyslogScan::Daemon::SpamDetector::BlockList> | ||
| 169 | |||
| 170 | =head1 THANK THE AUTHOR | ||
| 171 | |||
| 172 | If you need high-speed internet services (T1, T3, OC3 etc), please | ||
| 173 | send me your request-for-quote. I have access to very good pricing: | ||
| 174 | you'll save money and get a great service. | ||
| 175 | |||
| 176 | =head1 LICENSE | ||
| 177 | |||
| 178 | Copyright(C) 2007 David Muir Sharnoff <muir@idiom.com>. | ||
| 179 | This module may be used and distributed on the same terms | ||
| 180 | as Perl itself. | ||
| 181 |