SpamDetector.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 223 CC 24 SCAR 47.7
CC
24
Cov
0%
process_spam_match
306.0
preconfig
30.0
new
6.0
CRAP 117.9
1 # Copyright (C) 2006, David Muir Sharnoff
2  
3 package SyslogScan::Daemon::SpamDetector;
4  
5 use strict;
6 use warnings;
7 use Carp;
8 use Plugins;
9 use Plugins::SimpleConfig;
10 use SyslogScan::Daemon::Plugin;
11 use Tie::Cache::LRU;
12 use Net::Netmask;
13  
14 our $VERSION = 0.56;
15  
16 our(@ISA) = qw(SyslogScan::Daemon::Plugin);
17  
18 our $idcachesize = 10_000;
19  
20 my %defaults = (
21 debug => 0,
22 configfile => '',
23 idcachesize => sub {
24 my ($pkgself, $key, $value) = @_;
25 if (ref($pkgself)) {
26 $pkgself->{idcachesize} = $value;
27 if ($pkgself->{idcache}) {
28 my $t = tied(%{$pkgself->{idcache}});
29 $t->max_size($value);
30 }
31 } else {
32 $idcachesize = $value;
33 }
34 },
35 spam_command => 'echo found spam from $ip',
36 ham_command => 'echo found ham from $ip',
37 ignoreip => '',
38 );
39  
40 sub config_prefix { 'sd_' }
41  
42 sub parse_config_line { simple_config_line(\%defaults, @_); }
43  
44 sub new
45 {
46 my $self = simple_new(\%defaults, @_);
47 $self->{idcache} = {};
48 die if ref($self->{idcachesize});
49 tie %{$self->{idcache}}, 'Tie::Cache::LRU', $self->{idcachesize} || $idcachesize;
50 return $self;
51 }
52  
53 sub preconfig
54 {
55 my ($self, $ssd_configfile) = @_;
56  
57 $self->set_api($ssd_configfile,
58 process_spam_match => {},
59 is_ourip => { first_defined => 1 },
60 );
61  
62 $self->{ourip} = {};
63 if ($self->{ignoreip} && -s $self->{ignoreip}) {
64 open(IGNORE, "<$self->{ignoreip}") || die "open $self->{ignoreip}: $!";
65 while(<IGNORE>) {
66 next if /^#/;
67 next if /^$/;
68 chomp;
69 my $block = Net::Netmask->new2($_);
70 unless ($block) {
71 warn "could not parse network block $_ in $self->{ignoreip} line $.: $Net::Netmask::error\n";
72 next;
73 }
74 $block->storeNetblock($self->{ourip});
75 }
76 close(IGNORE);
77 }
78 }
79  
80 sub matched_line
81 {
82 my ($self, $logfile, $rx) = @_;
83  
84 for my $plugin (@{$self->{logs}{$logfile}{$rx}}) {
85 my %info = $plugin->invoke('parse_logs', $logfile, $rx);
86 next unless %info;
87 $self->process_spam_match(%info);
88 }
89 }
90  
91 sub process_spam_match
92 {
93 my ($self, %info) = @_;
94 my $status = $info{status};
95  
96 if (my $c = $self->{idcache}{$info{id}}) {
97 for my $k (keys %$c) {
98 $info{$k} = $c->{$k} unless exists $info{$k};
99 }
100 }
101 my $ip = $info{ip};
102  
103 my $filter = $self->{plugins}->invoke_until('filter', sub { defined($_[0]) }, %info);
104 if (defined $filter and ! $filter) {
105 print "FILTERED: $info{status} $info{match}\n" if $self->{debug} > 2;
106 return;
107 }
108  
109 if (! $ip) {
110 print "SPAMDETECTOR: ignoring $status for <$info{id}> ... no ip mapping\n" if $self->{debug};
111 } elsif ($self->{myapi}->is_ourip($ip)) {
112 print "SPAMDETECTOR: ignoring $status for <$info{id}> ... ip is internal\n" if $self->{debug};
113 } elsif ($status eq 'spam') {
114 $self->spam_found(%info, ip => $ip);
115 print "SPAMDETECTOR: SPAM FROM $ip SCORE=$info{score}\n" if $self->{debug};
116 if ($self->{spam_command} && $ip) {
117 system(substitute($self->{spam_command}, %info, ip => $ip));
118 }
119 } elsif ($status eq 'ham') {
120 $self->ham_found(%info, ip => $ip);
121 print "SPAMDETECTOR: HAM FROM $ip SCORE=$info{score}\n" if $self->{debug};
122 if ($self->{ham_command} && $ip) {
123 system(substitute($self->{ham_command}, %info, ip => $ip));
124 }
125 } elsif ($status eq 'idmap') {
126 print "SPAMDETECTOR: IDMAP $info{match} $info{id} => $ip to $info{host}\n" if $self->{debug} >= 2;
127 $self->{idcache}{$info{id}} = \%info;
128 } else {
129 die;
130 }
131 }
132  
133 sub is_ourip
134 {
135 my ($self, $ip) = @_;
136 if (findNetblock($ip, $self->{ourip})) {
137 return 1;
138 }
139 return undef;
140 }
141  
142  
143 sub periodic
144 {
145 my ($self) = @_;
146 $self->{plugins}->invoke('periodic');
147 }
148  
149 sub substitute
150 {
151 my ($string, %info) = @_;
152 for my $i (keys %info) {
153 $string =~ s/\$$i\b/$info{$i}/g;
154 }
155 return $string;
156 }
157  
158 package SyslogScan::Daemon::SpamDetector::Plugin;
159  
160 use strict;
161 use warnings;
162  
163 our @ISA = qw(SyslogScan::Daemon::Plugin);
164  
165 sub parse_logs {}
166 sub spam_found {}
167 sub ham_found {}
168 sub filter { return undef };
169  
170 1;
171  
172 =head1 NAME
173  
174 SyslogScan::Daemon::SpamDetector - Notice spammers in the log files
175  
176 =head1 SYNOPSIS
177  
178 plugin SyslogScan::Daemon::SpamDetector as sd_
179 debug 0
180 ignoreip /etc/postfix/ourip
181  
182 =head1 DESCRIPTION
183  
184 SyslogScan::Daemon::SpamDetector is a plugin for L<SyslogScan::Daemon>
185 that watches log files for indications of Spam.
186  
187 To do it's job it uses additional plugins.
188  
189 =head1 CONFIGURATION PARAMETERS
190  
191 The following configuration parameters are supported:
192  
193 =over 4
194  
195 =item debug
196  
197 Debugging on (1) or off (0).
198  
199 =item configfile
200  
201 Usually defaulted to the config file for SyslogScan::Daemon.
202  
203 =item idcachesize
204  
205 How big should the message id cache be? This is used by some of the
206 plugins to match up what happens to a message. For example, we need
207 to remember the IP address of that a message came from
208 (L<SyslogScan::Daemon::SpamDetector::Sendmail> or L<SyslogScan::Daemon::SpamDetector::Postfix>)
209 and then later decide if it's spam (L<SyslogScan::Daemon::SpamDetector::SpamAssassin>).
210 Default is 10,000.
211  
212 =item spam_command
213  
214 A shell command to run when spam is found.
215 In the command, C<$ip> will be substituted for the
216 IP address the message came from. All of the other
217 keys to the C<%info> array (documented blow) are
218 also available as substitutions.
219  
220 =item ham_command
221  
222 A shell command to run when a non-spam message is found.
223  
224 =item ignoreip
225  
226 A filename that contains a list of IP blocks (one per line)
227 that should be ignored. The blocks are in the format
228 A.B.C.D/bits
229  
230 =back
231  
232 =head1 WRITING PLUGINS
233  
234 Plugins for SyslogScan::Daemon::SpamDetector will either
235 help recognize spam or do something with recognized spam.
236  
237 Either way, they create or use an <%info> hash that describes
238 an event:
239  
240 =over 4
241  
242 =item status
243  
244 What is being reported? Values are:
245  
246 =over 7
247  
248 =item spam
249  
250 A spammy message has been found.
251  
252 =item ham
253  
254 A non-spam message has been found.
255  
256 =item idmap
257  
258 A message has come in, establish a mapping from
259 the C<id> to the C<ip>.
260  
261 =back
262  
263 =item id
264  
265 The message id. Usually required.
266  
267 =item ip
268  
269 The IP address. This is required unless an C<idmap>
270 established an C<id> -E<gt> C<ip> mapping previously
271 in which case an C<id> may be used instead.
272  
273 =item score
274  
275 The spam score from SpamAssassin. If not reporting
276 SpamAssassin, make something else up.
277  
278 =item match
279  
280 What kind of match was made. Example values are: C<spamassassin>,
281 C<spamsink>, C<badaddrs>, etc.
282  
283 =item host
284  
285 Hostname of the system receiving the message.
286  
287 =item hideid
288  
289 If you report message ids to outsiders (like, for example if you're using
290 this information to block mail) then don't report the message id I<this time>
291 because it is sensitive information. Optional.
292  
293 =back
294  
295 SyslogScan::Daemon::SpamDetector invokes the following methods on
296 it's plugins:
297  
298 =over 4
299  
300 =item @logs = get_logs()
301  
302 Inherited from L<SyslogScan::Daemon>.
303  
304 =item %info = parse_logs($logfile, $regex_matched)
305  
306 When this is called, C<$_> will be set to the logfile line
307 that matched. Please leave C<$_> alone so that other plugins
308 that matched the same line can also use it.
309  
310 Return C<()> if not providing an C<%info>.
311  
312 =item preconfig($configfile)
313  
314 Inherited from L<SyslogScan::Daemon>.
315  
316 =item periodic()
317  
318 Inherited from L<SyslogScan::Daemon>.
319  
320 =item spam_found(%info)
321  
322 Called when spam is found. Called though L<Plugins::API>.
323  
324 =item ham_found(%info)
325  
326 Called when non-spam is found. Called though L<Plugins::API>.
327  
328 =back
329  
330 SyslogScan::Daemon::SpamDetector provides some L<Plugins::API>
331 callbacks:
332  
333 =over 4
334  
335 =item process_spam_match(%info)
336  
337 Calling this is the same as returning C<%info> from C<parse_logs()>.
338  
339 =item is_ourip($ip_address)
340  
341 Is this one of our own IP addresses and thus should be ignored?
342 A return of C<undef> doesn't answer but a return of C<0> says that
343 the item is I<not> our IP address.
344  
345 =back
346  
347 =head1 SEE ALSO
348  
349 The context for this:
350 L<SyslogScan::Daemon>,
351 L<Plugins>,
352 L<Plugins::API>,
353 L<Daemon::Generic>.
354  
355 Plugins for this module:
356 L<SyslogScan::Daemon::SpamDetector::BlockList>.
357 L<SyslogScan::Daemon::SpamDetector::Sendmail>,
358 L<SyslogScan::Daemon::SpamDetector::Postfix>,
359 L<SyslogScan::Daemon::SpamDetector::SpamSink>,
360 L<SyslogScan::Daemon::SpamDetector::SpamAssassin>.
361 L<SyslogScan::Daemon::SpamDetector::Bogofilter>.
362 L<SyslogScan::Daemon::SpamDetector::BadAddr>.
363  
364 =head1 THANK THE AUTHOR
365  
366 If you need high-speed internet services (T1, T3, OC3 etc), please
367 send me your request-for-quote. I have access to very good pricing:
368 you'll save money and get a great service.
369  
370 =head1 LICENSE
371  
372 Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com>.
373 This module may be used and distributed on the same terms
374 as Perl itself.
375