lib/Mail/Decency/Policy.pm

filter: Statementstmt 24.0% 46 / 191 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 47.8% 11 / 23 Podpod 100% 8 / 8 totaltotal 17.5% 65 / 370 CC 55 SCAR 45.4
CC
55
Cov
15%
__ANON__
210.0
init
182.0
go_final_state
90.0
CRAP 93.4
« lib/Mail/Decency/ContentFilter/Core/Cmd.pm ↑ Summary lib/Mail/Decency/Policy/GeoWeight.pm »
1 package Mail::Decency::Policy;
2  
3 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 extends qw/
5 Mail::Decency::Core::Server
6 /;
7 11 with qw/
8 Mail::Decency::Core::Stats
9 Mail::Decency::Core::ExportImport
10 Mail::Decency::Core::DatabaseCreate
11 Mail::Decency::Core::Excludes
12 /;
13  
14 11 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 ▶use feature qw/ switch /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use Scalar::Util qw/ weaken blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use Time::HiRes qw/ tv_interval gettimeofday /;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 11 ▶use Mail::Decency::Helper::IP qw/ is_local_host /;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶use Mail::Decency::Core::POEForking::Postfix;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 11 ▶use Mail::Decency::Core::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶use Mail::Decency::Core::SessionItem::Policy;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 11 ▶use constant CRLF => qq[\x0D\x0A]; # RFC 2821, 2.3.7
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 NAME
30  
31 Mail::Decency::Policy
32  
33 =head1 SYNOPSIS
34  
35 use Mail::Decency::Policy;
36
37 # run in server mode
38 my $policy = Mail::DecencyPolicy->new( {
39 config => '/etc/decency/policy.yml'
40 } );
41 $policy->run;
42
43 # run in maintenance mode
44 $policy->maintenance;
45
46 # print statistics
47 $policy->print_stats;
48  
49 =head1 DESCRIPTION
50  
51 Policy server for Postfix or other MTAs. Could be combined with L<Mail::Decency::ContentFilter>. Besides the existing Modules it is easy extendable for custom needs.
52  
53 =head1 POSTFIX
54  
55 To implement this in postfix, add a "check_policy_service" directive in one of the restriction classes:
56  
57 smtpd_recipient_restrictions =
58 # ...
59 check_policy_service inet:127.0.0.1:15000
60 # ...
61  
62 If you want to place this anywhere before the smtpd_recipient_restrictions, you probaly have to enable "smtpd_delay_reject" (depending on the modules you use, but to be save..)
63  
64 smtpd_delay_reject = yes
65  
66 =head1 CONFIG
67  
68 The configuration can be provided via YAML file or as HashRef.
69  
70 Example:
71  
72 ---
73
74 include:
75 - database.yml
76 - cache.yml
77 - logging.yml
78
79 weight_threshold: -100
80 force_check_local: 1
81
82 default_reject_message: "use decency"
83 no_reject_detail: 0
84
85 disable_prepend: 0
86 forward_scoring: 1
87 forward_sign_key: sign.key
88
89 enable_stats: 1
90
91
92 server:
93 host: 127.0.0.1
94 port: 15000
95 instances: 3
96
97
98 policy:
99 - CWL: policy/cwl.yml
100 - DNSBL: policy/dnsbl.yml
101 - CBL: policy/cbl.yml
102 - SPF: policy/spf.yml
103 - Association: policy/association.yml
104 - GeoWeight: policy/geo-weight.yml
105 - Honeypot: policy/honeypot.yml
106 - Greylist: policy/greylist.yml
107 - Throttle: policy/throttle.yml
108
109  
110  
111 =head2 DATABASE
112  
113 SQL CREATE statements (SQLite) for the stats role:
114  
115 -- TABLE: stats_contentfilter_performance (SQLITE):
116 CREATE TABLE STATS_CONTENTFILTER_PERFORMANCE (calls varchar(10), runtime real, period varchar(10), type varchar(32), start integer, module varchar(32), id INTEGER PRIMARY KEY);
117
118 CREATE UNIQUE INDEX STATS_CONTENTFILTER_PERFORMANCE_MODULE_PERIOD_START_TYPE ON STATS_CONTENTFILTER_PERFORMANCE (module, period, start, type);
119
120 -- TABLE: stats_contentfilter_response (SQLITE):
121 CREATE TABLE STATS_CONTENTFILTER_RESPONSE (period varchar(10), type varchar(32), start integer, module varchar(32), id INTEGER PRIMARY KEY);
122
123 CREATE UNIQUE INDEX STATS_CONTENTFILTER_RESPONSE_MODULE_PERIOD_START_TYPE ON STATS_CONTENTFILTER_RESPONSE (module, period, start, type);
124  
125  
126 =head1 CLASS ATTRIBUTES
127  
128 See L<Mail::Decency::Policy::Core>
129  
130  
131 =head2 weight_threshold : Int
132  
133 Threshold of spam score before reject ( actual score <= threeshold == spam )
134  
135 =cut
136  
137 11 has weight_threshold => ( is => 'rw', isa => 'Int', default => -100 );
138  
139 =head2 session_data : Mail::Decency::Core::SessionItem::Policy
140  
141 Instance of L<Mail::Decency::Core::SessionItem::Policy>
142  
143 =cut
144  
145 11 has session_data => ( is => 'rw', isa => 'Mail::Decency::Core::SessionItem::Policy' );
146  
147 =head2 pass_localhost : Bool
148  
149 Wheter passing everything from localhost or not
150  
151 Default: 1
152  
153 =cut
154  
155 11 has pass_localhost => ( is => 'rw', isa => 'Bool', default => 1 );
156  
157 =head2 default_reject_message : Str
158  
159 Default reject message string (after the SMTP REJECT command .. "REJECT message")
160  
161 Default: use decency
162  
163 =cut
164  
165 11 has default_reject_message => ( is => 'rw', isa => 'Str', default => "use decency" );
166  
167  
168 =head2 no_reject_detail : Bool
169  
170 Wheter pass detailed information of why a particular REJECT has been thrown to the sender or not (not=always the default message)/
171  
172 Default: 0
173  
174 =cut
175  
176 11 has no_reject_detail => ( is => 'rw', isa => 'Bool', default => 0 );
177  
178 =head2 forward_scoring : Bool
179  
180 Wheter forward scoring informations after policies or not
181  
182 Default: 0
183  
184 =cut
185  
186 11 has forward_scoring => ( is => 'rw', isa => 'Bool', default => 0 );
187  
188 =head2 disable_prepend : Bool
189  
190 Wheter disabling the prepend of instance information fully (implies forward_scoring=0)
191  
192 Default: 0
193  
194 =cut
195  
196 11 has disable_prepend => ( is => 'rw', isa => 'Bool', default => 0 );
197  
198 =head2 forward_sign_key : Str
199  
200 Path to a file containing a private key for signing forwarded
201  
202 =cut
203  
204 0 ▶has forward_sign_key => ( is => 'rw', isa => 'Str', predicate => 'has_forward_sign_key', trigger => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
205 0 my ( $self, $key_file ) = @_;
206
207 # check file
208 0 ▶ $key_file = $self->config_dir . "/$key_file"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_config_dir and not -f $key_file 0 0 0
Condition0%0/3
$self->has_config_dir and not -f $key_file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_config_dir and not -f $key_file
$self->has_config_dir not -f $key_file
Truth table0%0/3
A and not(B)
A= $self->has_config_dirB= not -f $key_file
ABresult
0X0
100
111
209 if $self->has_config_dir && ! -f $key_file;
210 0 ▶ die "Could not access policy_sign_pub key file '$key_file'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $key_file 0 0 0
211 unless -f $key_file;
212
213 # read key
214 0 ▶ open my $fh, '<', $key_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $key_file or die "Cannot open policy_sign_pub key file for read: $!\n" 0 0 0
215 or die "Cannot open policy_sign_pub key file for read: $!\n";
216 0 my $key_content = join( "", <$fh> );
217 0 close $fh;
218
219 # try load rsa and init private key
220 0 my $load_rsa = eval "use Crypt::OpenSSL::RSA; 1;";
221 0 ▶ if ( $load_rsa ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($load_rsa) { } 0 0 0
222 0 $self->forward_sign_key_priv( Crypt::OpenSSL::RSA->new_private_key( $key_content ) );
223 }
224
225 # failure in loading -> bye
226 else {
227 0 die "Could not load Crypt::OpenSSL::RSA, cannot sign headers! Error: $@\n";
228 }
229
230 0 return;
231 11 } );
232  
233 =head2 forward_sign_key_priv : Crypt::OpenSSL::RSA
234  
235 Instance of L<Crypt::OpenSSL::RSA> representing the forward sign key
236  
237 =cut
238  
239 11 has forward_sign_key_priv => ( is => 'rw', isa => 'Crypt::OpenSSL::RSA' );
240  
241  
242 =head1 METHODS
243  
244 =head2 init
245  
246 Loads policy modules, inits caches, inits databases ..
247  
248 =cut
249  
250 0 ▶sub init {
Subroutine init: not called CC=13 SCAR=52.0
251 0 my ( $self ) = @_;
252
253 # init name
254 0 $self->name( "policy" );
255
256 # mark es inited
257 0 $self->{ inited } ++;
258
259 0 $self->init_logger();
260 0 $self->init_postfix_server();
261 0 $self->init_cache();
262 0 $self->init_database();
263
264 0 $self->logger->info( "Startup Phase" );
265
266 # set another default reject message
267 $self->default_reject_message( $self->config->{ default_reject_message } )
268 0 ▶ if $self->config->{ default_reject_message };
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"default_reject_message"} 0 0 0
269
270 # display always default message and no detail ?
271 $self->no_reject_detail( 1 )
272 0 ▶ if $self->config->{ no_reject_detail };
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"no_reject_detail"} 0 0 0
273
274 # check policy..
275 $self->config->{ policy } = []
276 unless defined $self->config->{ policy }
277 && ref( $self->config->{ policy } ) eq 'ARRAY'
278 0 ▶ && scalar @{ $self->config->{ policy } } > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->config->{"policy"} and ref $self->config->{"policy"} eq "ARRAY" and scalar @{$self->config->{"policy"};} > 0 0 0 0
Condition0%0/6
defined $self->config->{"policy"} and ref $self->config->{"policy"} eq "ARRAY" and scalar @{$self->config->{"policy"};} > 0
!ll&&!rl&&r
000
defined $self->config->{"policy"} and ref $self->config->{"policy"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->config->{"policy"} and ref $self->config->{"policy"} eq "ARRAY" and scalar @{$self->config->{"policy"};} > 0
defined $self->config->{"policy"} ref $self->config->{"policy"} eq "ARRAY" scalar @{$self->config->{"policy"};} > 0
Truth table0%0/4
A and B and C
A= defined $self->config->{"policy"}B= ref $self->config->{"policy"} eq "ARRAY"C= scalar @{$self->config->{"policy"};} > 0
ABCresult
0XX0
10X0
1100
1111
279
280 # use weighting ?
281 0 ▶ if ( defined $self->config->{ weight_threshold } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->config->{"weight_threshold"}) 0 0 0
282 0 $self->weight_threshold( $self->config->{ weight_threshold } );
283 }
284
285 # disable local passing ?
286 0 ▶ if ( $self->config->{ force_check_local } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"force_check_local"}) 0 0 0
287 0 $self->pass_localhost( 0 );
288 }
289
290 # disable prepending at all ?
291 0 ▶ if ( $self->config->{ disable_prepend } ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->config->{'disable_prepend'}) { } 0 0 0
elsif ($self->config->{'forward_scoring'}) { } 0 0 0
292 0 $self->disable_prepend( 1 );
293 }
294
295 # prepend scoring info to header ?
296 elsif ( $self->config->{ forward_scoring } ) {
297 0 $self->forward_scoring( 1 );
298
299 # having sign key ?
300 0 ▶ if ( $self->config->{ forward_sign_key } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"forward_sign_key"}) 0 0 0
301 0 $self->forward_sign_key( $self->config->{ forward_sign_key } );
302 }
303 }
304
305 0 weaken( my $self_weak = $self );
306 0 foreach my $policy_ref( @{ $self->config->{ policy } } ) {
307 0 my ( $name, $config_ref ) = %$policy_ref;
308 0 my $policy = $self->gen_child(
309 "Mail::Decency::Policy" => $name => $config_ref );
310 0 ▶ $policy_ref->{ $name } = $policy->config if $policy;
Branch: 1 branch
BranchTrueFalseTotal
if $policy 0 0 0
311 }
312
313
314 0 $self->logger->info( " Setup done" );
315
316 0 return;
317 }
318  
319  
320  
321 =head2 get_handlers
322  
323 Returns subref to handlers, called by L<Mail::Decency::Core::POEForking::Postfix>
324  
325 # all handlers
326 my $handlers_ref = $policy->get_handlers();
327
328 # only the awl handler
329 my $handlers_ref = $policy->get_handlers( qw/ AWL / );
330  
331 =cut
332  
333 0 ▶sub get_handlers {
Subroutine get_handlers: not called CC=3 SCAR=24.8
Pod documented
334 0 my ( $self ) = @_;
335
336 # check wheter having config!
337 0 ▶ die "No config has been set\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_config 0 0 0
338 unless $self->has_config;
339
340 # init policies
341 0 ▶ $self->init() unless $self->inited;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->inited 0 0 0
342
343 0 weaken( my $self_weak = $self );
344
345
346 0 ▶ return sub {
Subroutine __ANON__: not called CC=14 SCAR=53.5
347 0 my ( $server, $attrs_ref ) = @_;
348
349 # don bother with loopback addresses
350 return {
351 action => 'DUNNO'
352 0 ▶ } if $self->pass_localhost && is_local_host( $attrs_ref->{ client_address } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->pass_localhost and is_local_host($attrs_ref->{"client_address"}) 0 0 0
Condition0%0/3
$self->pass_localhost and is_local_host($attrs_ref->{"client_address"})
!ll&&!rl&&r
000
MC/DC0%0/2
$self->pass_localhost and is_local_host($attrs_ref->{"client_address"})
$self->pass_localhost is_local_host($attrs_ref->{"client_address"})
Truth table0%0/3
A and B
A= $self->pass_localhostB= is_local_host($attrs_ref->{"client_address"})
ABresult
0X0
100
111
353
354 # start handling-session
355 0 $self->session_init( $attrs_ref );
356
357 # apply all policies
358 0 foreach my $policy( @{ $self_weak->childs } ) {
359
360 0 ▶ if ( $self->has_exclusions && $self->do_exclude( $policy ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_exclusions and $self->do_exclude($policy)) 0 0 0
Condition0%0/3
$self->has_exclusions and $self->do_exclude($policy)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_exclusions and $self->do_exclude($policy)
$self->has_exclusions $self->do_exclude($policy)
Truth table0%0/3
A and B
A= $self->has_exclusionsB= $self->do_exclude($policy)
ABresult
0X0
100
111
361 0 $self->logger->debug2( "Exclude $policy" );
362 0 next;
363 }
364
365 # determine weight before, so we can increment stats
366 0 my $weight_before = $self->session_data->spam_score;
367 0 my $start_time_ref = [ gettimeofday() ];
368
369 # perform policy
370 0 eval {
371
372 # set timeout for module..
373 0 my $alarm = \( local $SIG{ ALRM } );
374 0 ▶ if ( $policy->timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy->timeout) 0 0 0
375 0 ▶ $$alarm = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
376 0 Mail::Decency::Core::Exception::Timeout
377 ->throw( { message => "Timeout" } );
378 0 };
379 0 alarm( $policy->timeout + 1 );
380 }
381
382 0 $policy->handle( $server, $attrs_ref );
383 };
384 0 my $err = $@;
385 0 alarm( 0 );
386
387 # update stats
388 0 ▶ if ( $self->enable_stats ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enable_stats) 0 0 0
389 0 my ( $response_prefix ) = split( ' ', $self->session_data->response );
390 0 my $weight_diff = $self->session_data->spam_score - $weight_before;
391 0 $self->update_stats( $policy => $response_prefix => $weight_diff, tv_interval( $start_time_ref, [ gettimeofday() ] ) );
392 }
393
394 # handle error, if any
395 0 given ( $err ) {
396
397 # REJECT
398 0 ▶ when( blessed( $_ ) && $_->isa( 'Mail::Decency::Core::Exception::Reject' ) ) {
Condition0%0/3
builtin::blessed($_) && $_->isa('Mail::Decency::Core::Exception::Reject')
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) && $_->isa('Mail::Decency::Core::Exception::Reject')
builtin::blessed($_) $_->isa('Mail::Decency::Core::Exception::Reject')
Truth table0%0/3
A && B
A= builtin::blessed($_)B= $_->isa('Mail::Decency::Core::Exception::Reject')
ABresult
0X0
100
111
399 0 $self->add_response_message( $_->message );
400 0 last;
401 }
402 # OK
403 0 ▶ when( blessed( $_ ) && $_->isa( 'Mail::Decency::Core::Exception::Accept' ) ) {
Condition0%0/3
builtin::blessed($_) && $_->isa('Mail::Decency::Core::Exception::Accept')
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) && $_->isa('Mail::Decency::Core::Exception::Accept')
builtin::blessed($_) $_->isa('Mail::Decency::Core::Exception::Accept')
Truth table0%0/3
A && B
A= builtin::blessed($_)B= $_->isa('Mail::Decency::Core::Exception::Accept')
ABresult
0X0
100
111
404 0 $self->add_response_message( $_->message );
405 0 last;
406 }
407 # PREPEND (finish with response)
408 0 ▶ when( blessed( $_ ) && $_->isa( 'Mail::Decency::Core::Exception::Prepend' ) ) {
Condition0%0/3
builtin::blessed($_) && $_->isa('Mail::Decency::Core::Exception::Prepend')
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) && $_->isa('Mail::Decency::Core::Exception::Prepend')
builtin::blessed($_) $_->isa('Mail::Decency::Core::Exception::Prepend')
Truth table0%0/3
A && B
A= builtin::blessed($_)B= $_->isa('Mail::Decency::Core::Exception::Prepend')
ABresult
0X0
100
111
409 0 $self->add_response_message( $_->message );
410 0 last;
411 }
412 # ERROR
413 0 ▶ when( defined $_ && "$_" ne "" ) {
Condition0%0/3
defined $_ && "$_" ne ''
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && "$_" ne ''
defined $_ "$_" ne ''
Truth table0%0/3
A && B
A= defined $_B= "$_" ne ''
ABresult
0X0
100
111
414 0 $self->logger->error( "Error in $policy: $_" );
415 }
416 # DUNNO
417 0 default {
418 0 $self->logger->debug2( "State after $policy: ". $self->session_data->response );
419 }
420 }
421
422 # finish, if last
423 0 ▶ last if $self->session_data->response ne 'DUNNO';
Branch: 1 branch
BranchTrueFalseTotal
if $self->session_data->response ne "DUNNO" 0 0 0
424 }
425
426
427 # clear info and stash to cache
428 0 my $response = $self->session_cleanup();
429
430 # update global policy stats
431 0 ▶ if ( $self->enable_stats ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enable_stats) 0 0 0
432 0 my ( $response_prefix ) = split( ' ', $response );
433 0 $self->update_stats( PolicyCore => $response_prefix );
434 }
435
436 0 $self->logger->debug3( "Response after policies: $response" );
437
438 # return final answer (REJECT, OK, DUNNO, 4xx, 5xx, ..)
439 return {
440 0 action => $response
441 };
442 0 };
443 }
444  
445  
446 #
447 # RUNTIME
448 #
449  
450  
451 =head2 start
452  
453 Starts all POE servers without calling the POE::Kernel->run
454  
455 =cut
456  
457 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
458 0 my ( $self ) = @_;
459 0 Mail::Decency::Core::POEForking::Postfix->new( $self );
460 }
461  
462  
463 =head2 run
464  
465 Start and run the server via POE::Kernel->run
466  
467 =cut
468  
469 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
470 0 my ( $self ) = @_;
471 0 $self->start;
472
473 0 POE::Kernel->run;
474 }
475  
476  
477  
478 #
479 # SESSION
480 #
481  
482  
483 =head2 session_init $attributes_ref
484  
485 Called at start of every handle cycle. Inits all handle/session-variables
486  
487 =cut
488  
489 0 ▶sub session_init {
Subroutine session_init: not called CC=5 SCAR=34.0
Pod documented
490 0 my ( $self, $attrs_ref ) = @_;
491
492 # assure we have that:
493 0 ▶ $attrs_ref->{ instance } ||= "NOQUEUE-". time(). int( rand() * 999999 );
Condition0%0/2
$attrs_ref->{"instance"} ||= 'NOQUEUE-' . time . int((rand()) * 999999)
l!l
00
MC/DC0%0/1
$attrs_ref->{"instance"} ||= 'NOQUEUE-' . time . int((rand()) * 999999)
$attrs_ref->{"instance"}
Truth table0%0/2
A
A= $attrs_ref->{"instance"}
Aresult
11
00
494
495 # create new session
496 my $session = Mail::Decency::Core::SessionItem::Policy->new(
497 id => $attrs_ref->{ instance },
498 cache => $self->cache,
499 from => $attrs_ref->{ sender },
500 to => $attrs_ref->{ recipient },
501 0 );
502
503 # add the sign key, if we can
504 0 ▶ $session->sign_key( $self->forward_sign_key_priv )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_forward_sign_key 0 0 0
505 if $self->has_forward_sign_key;
506
507 # check wheter session already in cache (policy might have multiple instances
508 0 my $cached;
509 0 ▶ if ( ( $cached = $self->cache->get( "POLICY-$attrs_ref->{ instance }" ) ) && ref( $cached ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached = $self->cache->get(qq[POLICY-$attrs_ref->{"instance"}]) and ref $cached) 0 0 0
Condition0%0/3
$cached = $self->cache->get(qq[POLICY-$attrs_ref->{"instance"}]) and ref $cached
!ll&&!rl&&r
000
MC/DC0%0/2
$cached = $self->cache->get(qq[POLICY-$attrs_ref->{"instance"}]) and ref $cached
$cached = $self->cache->get(qq[POLICY-$attrs_ref->{"instance"}]) ref $cached
Truth table0%0/3
A and B
A= $cached = $self->cache->get(qq[POLICY-$attrs_ref->{"instance"}])B= ref $cached
ABresult
0X0
100
111
510 0 $session->update_from_cache( $cached );
511 }
512
513 # set session
514 0 $self->session_data( $session );
515
516 # get recipient prefix and domain
517 ( $attrs_ref->{ recipient_prefix }, $attrs_ref->{ recipient_domain } )
518 0 = split( /@/, $attrs_ref->{ recipient }, 2 );
519 0 $attrs_ref->{ recipient_address } = $attrs_ref->{ recipient };
520
521 # get sender prefix and domain
522 ( $attrs_ref->{ sender_prefix }, $attrs_ref->{ sender_domain } )
523 0 = split( /@/, $attrs_ref->{ sender }, 2 );
524 0 $attrs_ref->{ sender_address } = $attrs_ref->{ sender };
525
526 0 return $attrs_ref;
527 }
528  
529  
530 =head2 session_cleanup
531  
532 Clears all info from session cache, returns final response
533  
534 =cut
535  
536 0 ▶sub session_cleanup {
Subroutine session_cleanup: not called CC=8 SCAR=42.8
Pod documented
537 0 my ( $self ) = @_;
538
539 # get current response
540 0 my $session = $self->session_data;
541 0 my $response = $session->response;
542
543 # set prepened info
544 0 ▶ if ( ( $response eq 'DUNNO' || $response eq 'PREPEND' ) && ! $self->disable_prepend ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response eq 'DUNNO' || $response eq 'PREPEND' and not $self->disable_prepend) { } 0 0 0
elsif ($response =~ /^(?:[45]\d\d|REJECT)/) { } 0 0 0
Condition0%0/6
$response eq "DUNNO" || $response eq "PREPEND" and not $self->disable_prepend
!ll&&!rl&&r
000
$response eq "DUNNO" or $response eq "PREPEND"
l!l&&r!l&&!r
000
MC/DC0%0/3
$response eq "DUNNO" || $response eq "PREPEND" and not $self->disable_prepend
$response eq "DUNNO" $response eq "PREPEND" not $self->disable_prepend
Truth table0%0/5
A or B and not(C)
A= $response eq "DUNNO"B= $response eq "PREPEND"C= not $self->disable_prepend
ABCresult
00X0
1X00
0100
1X11
0111
545
546 # generate the header ..
547 0 my ( $header, $sign_error )
548 = $session->generate_instance_header( $self->forward_scoring );
549 0 ▶ $self->logger->error( "Sign error: $sign_error" )
Branch: 1 branch
BranchTrueFalseTotal
if $sign_error 0 0 0
550 if $sign_error;
551
552
553 # header will look like this:
554 # X-Decency-Instance: <instance>|<sign>|<weight>|<flag,flag,...>|<detail>|<detail>|...
555 # this is tested under postfix with up to 10_000 characters! Postfix splits the
556 # lines at 989 characters, but transports all of them.
557 0 $response = 'PREPEND X-Decency-Instance: '. $header;
558 }
559
560 elsif ( $response =~ /^(?:[45]\d\d|REJECT)/ ) {
561 # determine reject message (could be from multiple modules)
562 my $message = $self->no_reject_detail
563 ? $self->default_reject_message
564 0 ▶ : join( " / ", @{ $self->session_data->message } )
Branch: 1 branch
BranchTrueFalseTotal
$self->no_reject_detail ? : 0 0 0
Condition0%0/3
join " / ", @{$self->session_data->message;} or $self->default_reject_message
l!l&&r!l&&!r
000
MC/DC0%0/2
join " / ", @{$self->session_data->message;} or $self->default_reject_message
join " / ", @{$self->session_data->message;} $self->default_reject_message
Truth table0%0/3
A or B
A= join " / ", @{$self->session_data->message;}B= $self->default_reject_message
ABresult
1X1
011
000
565 || $self->default_reject_message
566 ;
567 0 $response .= " $message";
568 }
569
570 # update/insert cache
571 0 $self->cache->set( "POLICY-". $session->id => $session->for_cache, time()+ 600 );
572
573 # remove from session
574 0 $session->cleanup;
575
576 # return bool wheter first instance or not
577 0 return $response;
578 }
579  
580  
581  
582 #
583 # SCORING / STATE CHANGE
584 #
585  
586  
587 =head2 add_spam_score $module, $weight, $details, $reject_message
588  
589 Add weight and filter info to current instance.
590  
591 Throws _FinalStateException if weighting indicates spam
592  
593 =over
594  
595 =item * $module
596  
597 The module which called the method.
598  
599 =item * $weight
600  
601 Positive or negative score.
602  
603 =item * $details
604  
605 Details for the MIME header
606  
607 =item * $reject_message
608  
609 If this scoring makes the rejection final, this is the rejection message
610  
611 =back
612  
613 =cut
614  
615 0 ▶sub add_spam_score {
Subroutine add_spam_score: not called CC=3 SCAR=24.8
Pod documented
616 0 my ( $self, $module, $weight, $details, $reject_message ) = @_;
617
618 # get info ref
619 0 my $session = $self->session_data;
620
621 # increment weight
622 0 $session->add_spam_score( $weight );
623
624 # add info
625 0 my @details = ( "Module: $module", "Score: $weight" );
626 0 ▶ push @details, $details if $details;
Branch: 1 branch
BranchTrueFalseTotal
if $details 0 0 0
627 0 $session->add_spam_details( join( "; ", @details ) );
628
629 # being spam -> go to final state
630 0 ▶ if ( $session->spam_score <= $self->weight_threshold ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->spam_score <= $self->weight_threshold) { } 0 0 0
631 0 $self->logger->debug0( "Threshold hit after ". $module->name. " with: ". $session->spam_score. " <= ". $self->weight_threshold );
632
633 # send reject message
634 0 $self->go_final_state( $module => REJECT => $reject_message );
635 }
636
637 # add reject message for future use
638 else {
639 0 $self->add_response_message( $reject_message );
640 }
641
642 # no spam, return ..
643 0 return 0;
644 }
645  
646  
647  
648 =head2 go_final_state $module, $state, $message
649  
650 Throws Mail::Decency::Core::Exception exception if state is not DUNNO.
651  
652 Adds message to list of response messages
653  
654  
655 =cut
656  
657 0 ▶sub go_final_state {
Subroutine go_final_state: not called CC=9 SCAR=45.0
Pod documented
658 0 my ( $self, $module, $state, $message ) = @_;
659 0 ▶ if ( $state && $state ne 'DUNNO' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state and $state ne 'DUNNO') { } 0 0 0
elsif ($message) { } 0 0 0
Condition0%0/3
$state and $state ne "DUNNO"
!ll&&!rl&&r
000
MC/DC0%0/2
$state and $state ne "DUNNO"
$state $state ne "DUNNO"
Truth table0%0/3
A and B
A= $stateB= $state ne "DUNNO"
ABresult
0X0
100
111
660 0 $self->session_data->response( $state );
661 0 $self->logger->debug0( "Final state from $module: $state" );
662
663 0 ▶ if ( $state eq 'OK' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "OK") 0 0 0
664 0 ▶ Mail::Decency::Core::Exception::Accept->throw( { message => $message || "" } );
Condition0%0/2
$message || ''
l!l
00
MC/DC0%0/1
$message || ''
$message
Truth table0%0/2
A
A= $message
Aresult
11
00
665 }
666 0 ▶ if ( $state eq 'PREPEND' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq 'PREPEND') { } 0 0 0
667 0 ▶ Mail::Decency::Core::Exception::Prepend->throw( { message => $message || "" } );
Condition0%0/2
$message || ''
l!l
00
MC/DC0%0/1
$message || ''
$message
Truth table0%0/2
A
A= $message
Aresult
11
00
668 }
669 else {
670 0 ▶ Mail::Decency::Core::Exception::Reject->throw( { message => $message || "" } );
Condition0%0/2
$message || ''
l!l
00
MC/DC0%0/1
$message || ''
$message
Truth table0%0/2
A
A= $message
Aresult
11
00
671 }
672 #die _FinalStateException->new( $message );
673 }
674 elsif( $message ) {
675 0 $self->add_response_message( $message );
676 }
677 }
678  
679  
680 =head2 add_response_message $message
681  
682 Add message (not details) to response
683  
684 =cut
685  
686 0 ▶sub add_response_message {
Subroutine add_response_message: not called CC=2 SCAR=17.9
Pod documented
687 0 my ( $self, $message ) = @_;
688
689 # add reject info (only reject info, no positive nor detailed informations)
690 0 ▶ push @{ $self->session_data->message }, $message
Branch: 1 branch
BranchTrueFalseTotal
if $message 0 0 0
691 if $message;
692
693 0 return ;
694 }
695  
696  
697 =head1 SEE ALSO
698  
699 =over
700  
701 =item * L<Mail::Decency::Policy::Association>
702  
703 =item * L<Mail::Decency::Policy::CBL>
704  
705 =item * L<Mail::Decency::Policy::CWL>
706  
707 =item * L<Mail::Decency::Policy::Core>
708  
709 =item * L<Mail::Decency::Policy::DNSBL>
710  
711 =item * L<Mail::Decency::Policy::Greylist>
712  
713 =item * L<Mail::Decency::Policy::GeoWeight>
714  
715 =item * L<Mail::Decency::Policy::Honeypot>
716  
717 =item * L<Mail::Decency::Policy::SPF>
718  
719 =item * L<Mail::Decency::Policy::Throttle>
720  
721 =item * L<Mail::Decency::Core::Stats>
722  
723 =item * L<Mail::Decency::Core::SessionItem>
724  
725 =item * L<Mail::Decency::Core::SessionItem::Policy>
726  
727 =item * L<Mail::Decency>
728  
729 =back
730  
731  
732 =head1 AUTHOR
733  
734 Ulrich Kautz <uk@fortrabbit.de>
735  
736 =head1 COPYRIGHT
737  
738 Copyright (c) 2010 the L</AUTHOR> as listed above
739  
740 =head1 LICENCSE
741  
742 This library is free software and may be distributed under the same terms as perl itself.
743  
744 =cut
745  
746  
747 11 1;
« lib/Mail/Decency/ContentFilter/Core/Cmd.pm ↑ Summary lib/Mail/Decency/Policy/GeoWeight.pm »