lib/Mail/Decency/Policy/Greylist.pm

filter: Statementstmt 27.9% 26 / 93 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 50.0% 5 / 10 Podpod 100% 3 / 3 totaltotal 18.0% 34 / 188 CC 35 SCAR 50.6
CC
35
Cov
16%
update_pass
306.0
init
110.0
handle
72.0
CRAP 157.3
« lib/Mail/Decency/ContentFilter/DKIM.pm ↑ Summary lib/Mail/Decency/ContentFilter/Bogofilter.pm »
1 package Mail::Decency::Policy::Greylist;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends qw/
5 Mail::Decency::Policy::Core
6 /;
7 1 with qw/
8 Mail::Decency::Core::Meta::Database
9 Mail::Decency::Core::Meta::Maintenance
10 /;
11  
12 1 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Mail::Decency::Helper::IP qw/ is_local_host /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 Mail::Decency::Policy::Greylist
21  
22  
23 =head1 DESCRIPTION
24  
25 A greylist implementation (http://www.greylisting.org/) for decency.
26  
27 =head1 CONFIG
28  
29 ---
30
31 disable: 0
32
33 # interval in seconds until a sender is allowed to re-send
34 # and pass
35 min_interval: 60
36
37 # per default, the greylist does not work as a whitelist, but
38 # a blacklist. it will reject (temporary) any mail not on the
39 # list, but does not explicit allow mails which are on the list
40 # to be passed (DUNNO).. you can enable passing by setting this
41 # to OK, thus any mail is on the list will pass.
42 # check with your restriction-classes to determine the better
43 # behavior for your mailserver
44 pass_code: DUNNO
45
46 # scoring awre. will put mails only on the permant whitelist
47 # (host or domain) if it has been scored zero or above
48 # this should keep suspicious mails from the whitelist
49 scoring_aware: 1
50
51 # policy for permanently whitelisting a whole sender server
52 hosts_policy:
53
54 # threshold of different sender mails
55 unique_sender: 5
56
57 # threshold of mails received from ONE address finally
58 # putting the host on the whitelist
59 one_address: 10
60
61 # policy for permanently whitelisting a whole sender domain
62 # use this with care and SPF (beforehand!)
63 domains_policy:
64
65 # threshold of different sender mails
66 unique_sender: 5
67
68 # threshold of mails received from ONE address finally
69 # putting the host on the whitelist
70 one_address: 10
71
72  
73 =head1 DATABASE
74  
75 -- contains all sender host ips, which are or are to be
76 -- whitelisted due to lot's of positives
77 CREATE TABLE greylist_client_addresss (
78 id INTEGER PRIMARY KEY,
79 client_address VARCHAR( 39 ),
80 counter integer,
81 last_seen integer
82 );
83 CREATE UNIQUE INDEX greylist_client_addresss_uk ON greylist_client_addresss( client_address );
84
85 -- contains all sender_domains, which are or are to be
86 -- whitelisted due to lot's of positives
87 CREATE TABLE greylist_sender_domain (
88 id INTEGER PRIMARY KEY,
89 sender_domain varchar( 255 ),
90 counter integer,
91 last_seen integer,
92 unique_sender BLOB
93 );
94 CREATE UNIQUE INDEX greylist_sender_domain_uk ON greylist_sender_domain( sender_domain );
95
96 -- contains all (sender -> recipient) address pairs which
97 -- are used to allow the second send attempt
98 CREATE TABLE greylist_sender_recipient (
99 id INTEGER PRIMARY KEY,
100 sender_address varchar( 255 ),
101 recipient_address varchar( 255 ),
102 counter integer,
103 last_seen integer,
104 unique_sender BLOB
105 );
106 CREATE UNIQUE INDEX greylist_sender_recipient_uk ON greylist_sender_recipient( sender_address, recipient_address );
107  
108 =head1 CLASS ATTRIBUTES
109  
110  
111 =head2 hosts_policy : HashRef[HashRef[Int]]
112  
113 Determines accommodation requirements per host (IP)
114  
115 =cut
116  
117 1 has hosts_policy => ( is => 'rw', isa => 'HashRef[HashRef[Int]]', predicate => 'has_hosts_policy' );
118  
119 =head2 domains_policy : HashRef[HashRef[Int]]
120  
121 Determines accommodation requirements per domain (sender)
122  
123 =cut
124  
125 1 has domains_policy => ( is => 'rw', isa => 'HashRef[HashRef[Int]]', predicate => 'has_domains_policy' );
126  
127 =head2 min_interval : Int
128  
129 Min interval
130  
131 =cut
132  
133 1 has min_interval => ( is => 'rw', isa => 'Int', default => 600 );
134  
135 =head2 reject_message : Str
136  
137 Message for greylisted rejection.
138  
139 Default: "Greylisted - Patience, young jedi"
140  
141 =cut
142  
143 1 has reject_message => ( is => 'rw', isa => 'Str', default => "Greylisted - Patience, young jedi" );
144  
145 =head2 pass_code : Str
146  
147 Set to "OK" if mails on the found on the greylist shall be whitelisted. Per default, they just won't be rejected (DUNNO).
148  
149 =cut
150  
151 1 has pass_code => ( is => 'rw', isa => 'Str', default => "DUNNO" );
152  
153 =head2 scoring_aware : Bool
154  
155 If scoring aware, will not use the host- and domain policies if score is below zero (spammy).
156  
157 =cut
158  
159 1 has scoring_aware => ( is => 'rw', isa => 'Bool', default => 0 );
160  
161 =head2 schema_definition : HashRef[HashRef]
162  
163 Database schema
164  
165 =cut
166  
167 0 ▶has schema_definition => ( is => 'ro', isa => 'HashRef[HashRef]', default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 {
169 0 greylist => {
170 client_address => {
171 client_address => [ varchar => 39 ],
172 counter => 'integer',
173 last_seen => 'integer',
174 -unique => [ 'client_address' ]
175 },
176 sender_domain => {
177 sender_domain => [ varchar => 255 ],
178 counter => 'integer',
179 last_seen => 'integer',
180 max_unique => 'integer',
181 max_one => 'integer',
182 unique_sender => 'blob',
183 -unique => [ 'sender_domain' ]
184 },
185 sender_recipient => {
186 sender_address => [ varchar => 255 ],
187 recipient_address => [ varchar => 255 ],
188 counter => 'integer',
189 last_seen => 'integer',
190 max_unique => 'integer',
191 max_one => 'integer',
192 unique_sender => 'blob',
193 -unique => [ 'sender_address', 'recipient_address' ]
194 }
195 }
196 };
197 1 } );
198  
199  
200  
201 =head1 METHODS
202  
203  
204 =head2 init
205  
206 =cut
207  
208 0 ▶sub init {
Subroutine init: not called CC=10 SCAR=47.0
209 0 my ( $self ) = @_;
210
211 # having sender policies ?
212 0 foreach my $policy( qw/ hosts_policy domains_policy / ) {
213 0 ▶ next unless defined $self->config->{ $policy };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->config->{$policy} 0 0 0
214 die "$policy is not a hashref!\n"
215 0 ▶ unless ref( $self->config->{ $policy } ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->config->{$policy} eq "HASH" 0 0 0
216 die "provide unique_sender and/or one_address for $policy\n"
217 unless $self->config->{ $policy }->{ one_address }
218 0 ▶ && $self->config->{ $policy }->{ unique_sender };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{$policy}{"one_address"} and $self->config->{$policy}{"unique_sender"} 0 0 0
Condition0%0/3
$self->config->{$policy}{"one_address"} and $self->config->{$policy}{"unique_sender"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->config->{$policy}{"one_address"} and $self->config->{$policy}{"unique_sender"}
$self->config->{$policy}{"one_address"} $self->config->{$policy}{"unique_sender"}
Truth table0%0/3
A and B
A= $self->config->{$policy}{"one_address"}B= $self->config->{$policy}{"unique_sender"}
ABresult
0X0
100
111
219 }
220
221 # min interval before re-send is considered ok
222 $self->min_interval( $self->config->{ min_interval } )
223 0 ▶ if defined $self->config->{ min_interval };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->config->{"min_interval"} 0 0 0
224
225 # reject code (temporary)
226 $self->reject_message( $self->config->{ reject_message } )
227 0 ▶ if $self->config->{ reject_message };
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"reject_message"} 0 0 0
228
229 # set pass code .. DUNNO, OK, ..
230 $self->pass_code( $self->config->{ pass_code } )
231 0 ▶ if $self->config->{ pass_code };
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"pass_code"} 0 0 0
232
233 # enable scoring awareness
234 $self->scoring_aware( 1 )
235 0 ▶ if $self->config->{ scoring_aware };
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"scoring_aware"} 0 0 0
236
237 0 return;
238 }
239  
240  
241 =head2 handle
242  
243 =cut
244  
245 0 ▶sub handle {
Subroutine handle: not called CC=8 SCAR=42.8
Pod documented
246 0 my ( $self, $server, $attrs_ref ) = @_;
247
248 # don bother with loopback addresses! EVEN IF ENABLED BY FORCE!
249 #return if is_local_host( $attrs_ref->{ client_address } );
250
251 #
252 # CACHES
253 #
254
255 0 my @caches = ();
256
257 # is on sender->recipient cache (has been send less then min-interval before ?!
258 0 push @caches, "Greylist-SR-$attrs_ref->{ sender_address }-$attrs_ref->{ recipient_address }";
259 0 push @caches, "Greylist-H-$attrs_ref->{ client_address }";
260 0 push @caches, "Greylist-D-$attrs_ref->{ sender_domain }";
261
262 0 my $pass = 0;
263 0 foreach my $cache( @caches ) {
264 0 my $cached = $self->cache->get( $cache );
265 0 ▶ if ( $cached && ( $cached eq 'OK' || $cached - $self->min_interval <= time() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached and $cached eq "OK" || $cached - $self->min_interval <= time) 0 0 0
Condition0%0/6
$cached and $cached eq "OK" || $cached - $self->min_interval <= time
!ll&&!rl&&r
000
$cached eq "OK" or $cached - $self->min_interval <= time
l!l&&r!l&&!r
000
MC/DC0%0/3
$cached and $cached eq "OK" || $cached - $self->min_interval <= time
$cached $cached eq "OK" $cached - $self->min_interval <= time
Truth table0%0/4
A and B or C
A= $cachedB= $cached eq "OK"C= $cached - $self->min_interval <= time
ABCresult
0XX0
1000
11X1
1011
266 0 $pass++;
267 0 last;
268 }
269 }
270
271 # update databases
272 0 ▶ unless ( $pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 0 0 0
273 0 $pass = $self->update_pass( $attrs_ref );
274 }
275
276 # pass
277 0 ▶ if ( $pass ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass) { } 0 0 0
278 0 ▶ $self->go_final_state( $self->pass_code ) if $self->pass_code !~ /^(DUNNO|PREPEND)/;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->pass_code =~ /^(DUNNO|PREPEND)/ 0 0 0
279 }
280 else {
281
282 # or not..
283 0 $self->go_final_state( 450 => $self->reject_message )
284 }
285 }
286  
287  
288 =head2 update_pass
289  
290 Add counters to pass databases
291  
292 =cut
293  
294 0 ▶sub update_pass {
Subroutine update_pass: not called CC=17 SCAR=57.2
Pod documented
295 0 my ( $self, $attrs_ref ) = @_;
296
297 0 my $pass = 0;
298
299 # use host and domain whitelisting only if we don't care for hosting
300 # or the score of the mail looks like hame
301 # remark: in context with SPF beforehand we will not add sender
302 # domains or hosts to the whitelist if the look somewhat bogus
303 0 ▶ if ( ! $self->scoring_aware || $self->session_data->spam_score >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->scoring_aware or $self->session_data->spam_score >= 0) 0 0 0
Condition0%0/3
not $self->scoring_aware or $self->session_data->spam_score >= 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->scoring_aware or $self->session_data->spam_score >= 0
not $self->scoring_aware $self->session_data->spam_score >= 0
Truth table0%0/3
not(A) or B
A= not $self->scoring_awareB= $self->session_data->spam_score >= 0
ABresult
1X1
011
000
304
305 0 my @update_policy;
306 0 ▶ push @update_policy, [ hosts => client_address => 'H' ]
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_hosts_policy 0 0 0
307 if $self->has_hosts_policy;
308 0 ▶ push @update_policy, [ domains => sender_domain => 'D' ]
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_domains_policy 0 0 0
309 if $self->has_domains_policy;
310
311 0 foreach my $ref( @update_policy ) {
312 0 my ( $policy, $attr, $cache ) = @$ref;
313
314 # read existing data .. attr: client_address | sender_domain
315 my $data_ref = $self->database->get( greylist => $attr => {
316 0 ▶ $attr => $attrs_ref->{ $attr }
Condition0%0/2
$self->database->get('greylist', $attr, {$attr, $attrs_ref->{$attr}}) || {'total', 0, 'max_unique', 0, 'max_one', 0, 'unique_sender', {}, 'last_seen', time}
l!l
00
MC/DC0%0/1
$self->database->get('greylist', $attr, {$attr, $attrs_ref->{$attr}}) || {'total', 0, 'max_unique', 0, 'max_one', 0, 'unique_sender', {}, 'last_seen', time}
$self->database->get('greylist', $attr, {$attr, $attrs_ref->{$attr}})
Truth table0%0/2
A
A= $self->database->get('greylist', $attr, {$attr, $attrs_ref->{$attr}})
Aresult
11
00
317 } ) || {
318 total => 0,
319 max_unique => 0,
320 max_one => 0,
321 unique_sender => {},
322 last_seen => time()
323 };
324
325 # convert unique sender to hashref, if given in YAML
326 0 eval {
327 $data_ref->{ unique_sender } = YAML::Load( $data_ref->{ unique_sender } )
328 0 ▶ unless ref( $data_ref->{ unique_sender } );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data_ref->{"unique_sender"} 0 0 0
329 };
330 0 ▶ $data_ref->{ unique_sender } = {} if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
331
332 # increment total
333 0 $data_ref->{ total }++;
334
335 # increment unique sender policy
336 0 ▶ unless ( $data_ref->{ unique_sender }->{ $attrs_ref->{ sender_address } }++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_ref->{"unique_sender"}{$attrs_ref->{"sender_address"}}++) 0 0 0
337 0 $data_ref->{ max_unique }++;
338 }
339
340 # determine MAX "send by one sender"
341 0 ( $data_ref->{ max_one } ) = sort { $b <=> $a } values %{ $data_ref->{ unique_sender } };
342
343 # write back
344 0 $self->logger->debug3( "Write to $attr database: $attrs_ref->{ $attr }" );
345 $self->database->set( greylist => $attr => {
346 0 $attr => $attrs_ref->{ $attr }
347 }, $data_ref );
348
349 # write to cache if positive
350 0 my $policy_meth = "${policy}_policy";
351 my $do_cache = (
352 $self->$policy_meth->{ unique_sender }
353 && $self->$policy_meth->{ unique_sender } <= $data_ref->{ max_unique }
354 ) || (
355 $self->$policy_meth->{ one_sender }
356 && $self->$policy_meth->{ one_sender } <= $data_ref->{ max_one }
357 0 ▶ );
Condition0%0/9
$self->$policy_meth->{'unique_sender'} && $self->$policy_meth->{'unique_sender'} <= $data_ref->{'max_unique'} || $self->$policy_meth->{'one_sender'} && $self->$policy_meth->{'one_sender'} <= $data_ref->{'max_one'}
l!l&&r!l&&!r
000
$self->$policy_meth->{"unique_sender"} and $self->$policy_meth->{"unique_sender"} <= $data_ref->{"max_unique"}
!ll&&!rl&&r
000
$self->$policy_meth->{"one_sender"} and $self->$policy_meth->{"one_sender"} <= $data_ref->{"max_one"}
!ll&&!rl&&r
000
MC/DC0%0/4
$self->$policy_meth->{'unique_sender'} && $self->$policy_meth->{'unique_sender'} <= $data_ref->{'max_unique'} || $self->$policy_meth->{'one_sender'} && $self->$policy_meth->{'one_sender'} <= $data_ref->{'max_one'}
$self->$policy_meth->{"unique_sender"} $self->$policy_meth->{"unique_sender"} <= $data_ref->{"max_unique"} $self->$policy_meth->{"one_sender"} $self->$policy_meth->{"one_sender"} <= $data_ref->{"max_one"}
Truth table0%0/7
A and B || C and D
A= $self->$policy_meth->{"unique_sender"}B= $self->$policy_meth->{"unique_sender"} <= $data_ref->{"max_unique"}C= $self->$policy_meth->{"one_sender"}D= $self->$policy_meth->{"one_sender"} <= $data_ref->{"max_one"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
358 0 ▶ if ( $do_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_cache) 0 0 0
359 0 $self->cache->set( "Greylist-$cache-$attrs_ref->{ $attr }", "OK" );
360 0 $pass++;
361 }
362 }
363 }
364
365 # update sender->recipient database
366 my $sr_ref = $self->database->get( greylist => sender_recipient => {
367 sender_address => $attrs_ref->{ sender_address },
368 recipient_address => $attrs_ref->{ recipient_address },
369 0 ▶ } ) || {
Condition0%0/2
$self->database->get('greylist', 'sender_recipient', {'sender_address', $attrs_ref->{'sender_address'}, 'recipient_address', $attrs_ref->{'recipient_address'}}) || {'counter', 0, 'last_seen', time}
l!l
00
MC/DC0%0/1
$self->database->get('greylist', 'sender_recipient', {'sender_address', $attrs_ref->{'sender_address'}, 'recipient_address', $attrs_ref->{'recipient_address'}}) || {'counter', 0, 'last_seen', time}
$self->database->get('greylist', 'sender_recipient', {'sender_address', $attrs_ref->{'sender_address'}, 'recipient_address', $attrs_ref->{'recipient_address'}})
Truth table0%0/2
A
A= $self->database->get('greylist', 'sender_recipient', {'sender_address', $attrs_ref->{'sender_address'}, 'recipient_address', $attrs_ref->{'recipient_address'}})
Aresult
11
00
370 counter => 0,
371 last_seen => time()
372 };
373
374 # increment, if time passed
375 0 ▶ if ( $sr_ref->{ last_seen } + $self->min_interval <= time() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sr_ref->{"last_seen"} + $self->min_interval <= time) 0 0 0
376 0 $sr_ref->{ counter }++;
377 }
378
379 # write back
380 $self->database->set( greylist => sender_recipient => {
381 sender_address => $attrs_ref->{ sender_address },
382 recipient_address => $attrs_ref->{ recipient_address },
383 0 }, $sr_ref );
384
385 # positive counter -> allow and update cache
386 0 ▶ if ( $sr_ref->{ counter } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sr_ref->{"counter"}) 0 0 0
387 0 $self->cache->set( "Greylist-SR-$attrs_ref->{ sender_address }-$attrs_ref->{ recipient_address }", "OK" );
388 0 $pass++;
389 }
390
391 0 return $pass;
392 }
393  
394  
395 =head2 maintenance
396  
397 Called by policy server in maintenance mode. Cleans up obsolete entries in greylist databsae
398  
399 =cut
400  
401 0 ▶sub maintenance {
Subroutine maintenance: not called CC=3 SCAR=24.8
Pod documented
402 0 my ( $self ) = @_;
403 0 my $obsolete_time = time() - $self->maintenance_ttl;
404 0 ▶ while ( my ( $schema, $tables_ref ) = each %{ $self->schema_definition } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->schema_definition;}) 0 0 0
405 0 ▶ while ( my ( $table, $ref ) = each %{ $tables_ref } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$tables_ref;}) 0 0 0
406 0 $self->database->remove( $schema => $table => {
407 last_seen => {
408 '<' => $obsolete_time
409 }
410 } );
411 }
412 }
413 }
414  
415 =head1 AUTHOR
416  
417 Ulrich Kautz <uk@fortrabbit.de>
418  
419 =head1 COPYRIGHT
420  
421 Copyright (c) 2010 the L</AUTHOR> as listed above
422  
423 =head1 LICENCSE
424  
425 This library is free software and may be distributed under the same terms as perl itself.
426  
427 =cut
428  
429  
430  
431 1 1;
« lib/Mail/Decency/ContentFilter/DKIM.pm ↑ Summary lib/Mail/Decency/ContentFilter/Bogofilter.pm »