lib/Mail/Decency/Policy/Basic.pm

filter: Statementstmt 32.1% 37 / 115 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 61.5% 8 / 13 Podpod 100% 2 / 2 totaltotal 19.5% 47 / 241 CC 44 SCAR 54.9
CC
44
Cov
18%
handle
506.0
_resolute_domain_to_ip
132.0
_has_a_or_mx
90.0
CRAP 242.0
« lib/Mail/Decency/LogParser/Stats.pm ↑ Summary lib/Mail/Decency/Core/ExportImport.pm »
1 package Mail::Decency::Policy::Basic;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends 'Mail::Decency::Policy::Core';
5  
6 1 ▶use version 0.74; our $VERSION = qv( "v0.1.5" );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Net::DNS::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Regexp::Common qw/ net /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Email::Valid;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Mail::Decency::Helper::IP qw/ is_local_host /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Mail::Decency::Policy::Basic
19  
20 =head1 CONFIG
21  
22 ---
23
24 disable: 0
25
26 weight_invalid_helo_hostname: -100
27
28 weight_non_fqdn_helo_hostname: -100
29 weight_non_fqdn_recipient: -100
30 weight_non_fqdn_sender: -100
31
32 weight_unknown_helo_hostname: -50
33 weight_unknown_recipient_domain: -50
34 weight_unknown_sender_domain: -50
35
36 #weight_unknown_client_hostname: -50
37 weight_unknown_reverse_client_hostname: -50
38  
39  
40 =head1 DESCRIPTION
41  
42 Re-implementation of postfix's restriction directives but with scoring.
43  
44 The following directives are re-implemented:
45  
46 =over
47  
48 =item * reject_invalid_helo_hostname -> weight_invalid_helo_hostname
49  
50 Syntax of helo hostname is invalid (eg "???" or "#@%@@" or whatever is not syntactically correct)
51  
52  
53 =item * reject_non_fqdn_helo_hostname -> weight_non_fqdn_helo_hostname
54  
55 Syntax is correct, but not in FQDN form (eg localhost, but not localhost.tld)
56  
57 =item * reject_non_fqdn_recipient -> weight_non_fqdn_recipient
58  
59 Recipient address is not FDQN (eg: "user" without domain or anything or "user@localhost" but not "user@localhost.tld").
60  
61 =item * reject_non_fqdn_sender -> weight_non_fqdn_sender
62  
63 Same as above but for sender address.
64  
65  
66 =item * reject_unknown_helo_hostname -> weight_unknown_helo_hostname
67  
68 If the syntax is correct and in FQDN form but NOT an existing domain (has no A or MX record)
69  
70 =item * reject_unknown_recipient_domain -> weight_unknown_recipient_domain
71  
72 Recipient is in correct FQDN but recipient domain does not have an A or MX record.
73  
74 =item * reject_unknown_sender_domain -> weight_unknown_sender_domain
75  
76 Same as above, but for sender.
77  
78  
79 =item * reject_unknown_client_hostname -> weight_unknown_client_hostname
80  
81 This matches if: 1) the client IP address->name mapping fails, 2) the name->address mapping fails, or 3) the name->address mapping does not match the client IP address
82  
83 Stronger then weight_unknown_reverse_client_hostname which matches only 1)
84  
85 =item * reject_unknown_reverse_client_hostname -> weight_unknown_reverse_client_hostname
86  
87 See above.
88  
89 =back
90  
91 The order the tests will be performed is in as they are listed above.
92  
93 =cut
94  
95 =head1 CLASS ATTRIBUTES
96  
97 =head2 weight_non_fqdn_sender : Int
98  
99 Default: 0
100  
101 =cut
102  
103 1 has weight_non_fqdn_sender => ( is => 'rw', isa => 'Int', default => 0 );
104  
105 =head2 weight_unknown_sender_domain : Int
106  
107 Default: 0
108  
109 =cut
110  
111 1 has weight_unknown_sender_domain => ( is => 'rw', isa => 'Int', default => 0 );
112  
113 =head2 weight_non_fqdn_recipient : Int
114  
115 Default: 0
116  
117 =cut
118  
119 1 has weight_non_fqdn_recipient => ( is => 'rw', isa => 'Int', default => 0 );
120  
121 =head2 weight_unknown_recipient_domain : Int
122  
123 Default: 0
124  
125 =cut
126  
127 1 has weight_unknown_recipient_domain => ( is => 'rw', isa => 'Int', default => 0 );
128  
129 =head2 weight_invalid_helo_hostname : Int
130  
131 Default: 0
132  
133 =cut
134  
135 1 has weight_invalid_helo_hostname => ( is => 'rw', isa => 'Int', default => 0 );
136  
137 =head2 weight_non_fqdn_helo_hostname : Int
138  
139 Default: 0
140  
141 =cut
142  
143 1 has weight_non_fqdn_helo_hostname => ( is => 'rw', isa => 'Int', default => 0 );
144  
145 =head2 weight_unknown_helo_hostname : Int
146  
147 Default: 0
148  
149 =cut
150  
151 1 has weight_unknown_helo_hostname => ( is => 'rw', isa => 'Int', default => 0 );
152  
153 =head2 weight_unknown_client_hostname : Int
154  
155 Default: 0
156  
157 =cut
158  
159 1 has weight_unknown_client_hostname => ( is => 'rw', isa => 'Int', default => 0 );
160  
161 =head2 weight_unknown_reverse_client_hostname : Int
162  
163 Default: 0
164  
165 =cut
166  
167 1 has weight_unknown_reverse_client_hostname => ( is => 'rw', isa => 'Int', default => 0 );
168  
169 =head2 resolver : Net::DNS::Resolver
170  
171 Will be created automatically.
172  
173 =cut
174  
175 0 ▶has resolver => ( is => 'ro', isa => 'Net::DNS::Resolver', default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
176 0 Net::DNS::Resolver->new
177 1 } );
178  
179 =head1 METHODS
180  
181  
182 =head2 init
183  
184 =cut
185  
186 0 ▶sub init {
Subroutine init: not called CC=5 SCAR=34.0
Pod documented
187 0 my ( $self ) = @_;
188
189 0 foreach my $attr( qw/
190 weight_invalid_helo_hostname
191
192 weight_non_fqdn_helo_hostname
193 weight_non_fqdn_recipient
194 weight_non_fqdn_sender
195
196 weight_unknown_helo_hostname
197 weight_unknown_sender_domain
198 weight_unknown_recipient_domain
199
200
201 weight_unknown_reverse_client_hostname
202 weight_unknown_client_hostname
203
204 / ) {
205 $self->$attr( $self->config->{ $attr } )
206 0 ▶ if defined $self->config->{ $attr };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->config->{$attr} 0 0 0
207 }
208
209 # check ...
210 0 ▶ die "Do not use weight_unknown_reverse_client_hostname AND weight_unknown_client_hostname.. only one of those\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->weight_unknown_reverse_client_hostname and $self->weight_unknown_client_hostname 0 0 0
Condition0%0/3
$self->weight_unknown_reverse_client_hostname and $self->weight_unknown_client_hostname
!ll&&!rl&&r
000
MC/DC0%0/2
$self->weight_unknown_reverse_client_hostname and $self->weight_unknown_client_hostname
$self->weight_unknown_reverse_client_hostname $self->weight_unknown_client_hostname
Truth table0%0/3
A and B
A= $self->weight_unknown_reverse_client_hostnameB= $self->weight_unknown_client_hostname
ABresult
0X0
100
111
211 if $self->weight_unknown_reverse_client_hostname && $self->weight_unknown_client_hostname;
212
213 }
214  
215  
216  
217 =head2 handle
218  
219 Either build stats per country or score with negative or positve weight per country or do both
220  
221 =cut
222  
223 0 ▶sub handle {
Subroutine handle: not called CC=22 SCAR=62.3
Pod documented
224 0 my ( $self, $server, $attrs_ref ) = @_;
225
226 #
227 # INVALID
228 #
229
230 # helo hostname
231 0 ▶ if ( $self->weight_invalid_helo_hostname && $attrs_ref->{ helo_name } !~ /$RE{net}{domain}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->weight_invalid_helo_hostname and not $attrs_ref->{"helo_name"} =~ /$Mail::Decency::Policy::Basic::RE{"net"}{"domain"}/) 0 0 0
Condition0%0/3
$self->weight_invalid_helo_hostname and not $attrs_ref->{"helo_name"} =~ /$Mail::Decency::Policy::Basic::RE{"net"}{"domain"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->weight_invalid_helo_hostname and not $attrs_ref->{"helo_name"} =~ /$Mail::Decency::Policy::Basic::RE{"net"}{"domain"}/
$self->weight_invalid_helo_hostname not $attrs_ref->{"helo_name"} =~ /$Mail::Decency::Policy::Basic::RE{"net"}{"domain"}/
Truth table0%0/3
A and not(B)
A= $self->weight_invalid_helo_hostnameB= not $attrs_ref->{"helo_name"} =~ /$Mail::Decency::Policy::Basic::RE{"net"}{"domain"}/
ABresult
0X0
100
111
232 0 $self->logger->debug0( "Helo hostname is invalid ($attrs_ref->{ helo_name })" );
233 0 $self->add_spam_score(
234 $self->weight_invalid_helo_hostname,
235 "Helo hostname is invalid",
236 "Helo hostname is invalid"
237 );
238 }
239
240 #
241 # FQDN
242 #
243
244 my @fqdn_checks = (
245 [
246 $self->weight_non_fqdn_helo_hostname,
247 'user@'. $attrs_ref->{ helo_name },
248 "Helo hostname is not in FQDN",
249 ],
250 [
251 $self->weight_non_fqdn_recipient,
252 $attrs_ref->{ recipient_address },
253 0 "Recipient address is not in FQDN",
254 ]
255 );
256
257 # MAIL FROM may be empty (bounce)
258 push @fqdn_checks, [
259 $self->weight_non_fqdn_sender,
260 $attrs_ref->{ sender_address },
261 "Sender address is not in FQDN",
262 0 ▶ ] if $attrs_ref->{ sender_address };
Branch: 1 branch
BranchTrueFalseTotal
if $attrs_ref->{"sender_address"} 0 0 0
263
264 0 foreach my $ref( @fqdn_checks ) {
265 0 ▶ if ( $ref->[0] && ! Email::Valid->address( -address => $ref->[1], -fqdn => 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->[0] and not "Email::Valid"->address("-address", $ref->[1], "-fqdn", 1)) 0 0 0
Condition0%0/3
$ref->[0] and not "Email::Valid"->address("-address", $ref->[1], "-fqdn", 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$ref->[0] and not "Email::Valid"->address("-address", $ref->[1], "-fqdn", 1)
$ref->[0] not "Email::Valid"->address("-address", $ref->[1], "-fqdn", 1)
Truth table0%0/3
A and not(B)
A= $ref->[0]B= not "Email::Valid"->address("-address", $ref->[1], "-fqdn", 1)
ABresult
0X0
100
111
266 0 $self->logger->debug0( "$ref->[2] ($ref->[1])" );
267 0 $self->add_spam_score( $ref->[0], $ref->[2], $ref->[2] );
268 }
269 }
270
271
272 # stop here, for local host
273 0 ▶ return if is_local_host( $attrs_ref->{ client_address } );
Branch: 1 branch
BranchTrueFalseTotal
if is_local_host($attrs_ref->{"client_address"}) 0 0 0
274
275 #
276 # UNKNOWN
277 #
278
279
280 my @unknwon_checks = (
281 [
282 $self->weight_unknown_helo_hostname,
283 $attrs_ref->{ helo_name },
284 "Helo hostname is unknown",
285 ],
286 [
287 $self->weight_unknown_sender_domain,
288 $attrs_ref->{ recipient_domain },
289 0 "Recipient domain is unknown",
290 ],
291 );
292
293 # MAIL FROM may be empty (bounce)
294 push @unknwon_checks, [
295 $self->weight_unknown_recipient_domain,
296 $attrs_ref->{ sender_domain },
297 "Sender domain is unknown",
298 0 ▶ ] if $attrs_ref->{ sender_address };
Branch: 1 branch
BranchTrueFalseTotal
if $attrs_ref->{"sender_address"} 0 0 0
299
300
301 0 foreach my $ref( @unknwon_checks ) {
302 0 ▶ next unless $ref->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $ref->[0] 0 0 0
303
304 0 ▶ unless ( $self->_has_a_or_mx( $ref->[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_a_or_mx($ref->[1])) 0 0 0
305 0 $self->logger->debug0( "$ref->[2] ($ref->[1])" );
306 0 $self->add_spam_score( $ref->[0], $ref->[2], $ref->[2] );
307 }
308 }
309
310 #
311 # REVERSE STUFF
312 #
313
314 0 ▶ if ( ( my $weight = $self->weight_unknown_reverse_client_hostname || $self->weight_unknown_client_hostname ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $weight = $self->weight_unknown_reverse_client_hostname || $self->weight_unknown_client_hostname) < 0) 0 0 0
Condition0%0/3
$self->weight_unknown_reverse_client_hostname || $self->weight_unknown_client_hostname
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->weight_unknown_reverse_client_hostname || $self->weight_unknown_client_hostname
$self->weight_unknown_reverse_client_hostname $self->weight_unknown_client_hostname
Truth table0%0/3
A || B
A= $self->weight_unknown_reverse_client_hostnameB= $self->weight_unknown_client_hostname
ABresult
1X1
011
000
315
316 # MX and A for client
317 0 ▶ unless ( $self->_has_a_or_mx( $attrs_ref->{ client_name } ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_has_a_or_mx($attrs_ref->{'client_name'})) { } 0 0 0
if (do { $self->weight_unknown_client_hostname }) 0 0 0
318 0 $self->logger->debug0( "Client domain is unknown ($attrs_ref->{ client_name })" );
319 0 $self->add_spam_score( $weight,
320 "Client domain is unknown",
321 "Client domain is unknown"
322 );
323 }
324
325 # name -> address mapping OR name -> address does not match
326 0 elsif ( $self->weight_unknown_client_hostname ) {
327 0 my $res = $self->resolver->search( $attrs_ref->{ client_address }, 'PTR' );
328 my @ptr = $res ? ( grep {
329 0 ▶ defined $_ && $_->ptrdname eq $attrs_ref->{ client_name };
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 0 0 0
Condition0%0/3
defined $_ && $_->ptrdname eq $attrs_ref->{'client_name'}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $_->ptrdname eq $attrs_ref->{'client_name'}
defined $_ $_->ptrdname eq $attrs_ref->{'client_name'}
Truth table0%0/3
A && B
A= defined $_B= $_->ptrdname eq $attrs_ref->{'client_name'}
ABresult
0X0
100
111
330 } $res->answer ) : ();
331
332 # name -> address fail
333 0 ▶ unless ( @ptr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ptr) { } 0 0 0
334 0 $self->logger->debug0( "Client PTR mapping failed ($attrs_ref->{ client_address }. $attrs_ref->{ client_name })" );
335 0 $self->add_spam_score( $self->weight_unknown_client_hostname,
336 "Client PTR mapping failed",
337 "Client PTR mapping failed"
338 );
339 }
340
341 # check name -> address
342 else {
343
344 0 my $ip_ref = $self->_resolute_domain_to_ip( $attrs_ref->{ client_name } );
345 my $ip_ok = $ip_ref && $#$ip_ref > -1
346 0 ▶ ? scalar grep { $_ eq $attrs_ref->{ client_address } } @$ip_ref > 0
Branch: 1 branch
BranchTrueFalseTotal
$ip_ref && $ip_ref->$#* > -1 ? : 0 0 0
Condition0%0/3
$ip_ref and $ip_ref->$#* > -1
!ll&&!rl&&r
000
MC/DC0%0/2
$ip_ref and $ip_ref->$#* > -1
$ip_ref $ip_ref->$#* > -1
Truth table0%0/3
A and B
A= $ip_refB= $ip_ref->$#* > -1
ABresult
0X0
100
111
347 : 0
348 ;
349
350 0 ▶ unless ( $ip_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_ok) 0 0 0
351 0 $self->logger->debug0( "Client domain -> address mapping wrong ($attrs_ref->{ client_address }. $attrs_ref->{ client_name })" );
352 0 $self->add_spam_score( $self->weight_unknown_client_hostname,
353 "Client domain -> address mapping wrong",
354 "Client domain -> address mapping wrong"
355 );
356 }
357 }
358 }
359 }
360
361 # remember final result
362 0 $self->add_spam_score( 0,
363 "Basic checks passed",
364 ""
365 );
366
367 0 return ;
368 }
369  
370  
371 =head2 _has_a_or_mx
372  
373 Returns bool wheter given domain has A or MX record
374  
375 =cut
376  
377 0 ▶sub _has_a_or_mx {
Subroutine _has_a_or_mx: not called CC=9 SCAR=45.0
378 0 my ( $self, $domain ) = @_;
379
380 0 my $cached = $self->cache->get( "basic-has-a-or-mx-$domain" );
381 0 ▶ return $cached eq 'OK' ? 1 : 0 if $cached;
Branch: 2 branches
BranchTrueFalseTotal
if $cached 0 0 0
$cached eq 'OK' ? : 0 0 0
382
383 # has A records ?
384 0 my $res = $self->resolver->search( $domain, 'A' );
385 0 ▶ my @a_rec = $res ? ( grep { defined $_ && $_->address } $res->answer ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 0 0 0
Condition0%0/3
defined $_ && $_->address
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $_->address
defined $_ $_->address
Truth table0%0/3
A && B
A= defined $_B= $_->address
ABresult
0X0
100
111
386
387 # no A records ..
388 0 ▶ unless ( @a_rec ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@a_rec) 0 0 0
389
390 # has MX records ?
391 0 my $res_mx = $self->resolver->search( $domain, 'MX' );
392 0 ▶ my @mx_rec = $res_mx ? ( grep { defined $_ && $_->exchange } $res_mx->answer ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$res_mx ? : 0 0 0
Condition0%0/3
defined $_ && $_->exchange
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $_->exchange
defined $_ $_->exchange
Truth table0%0/3
A && B
A= defined $_B= $_->exchange
ABresult
0X0
100
111
393
394 # also no MX recors -> fail
395 0 ▶ unless ( @mx_rec ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@mx_rec) 0 0 0
396 0 $self->cache->set( "basic-has-a-or-mx-$domain", "NOPE" );
397 0 return 0;
398 }
399 }
400
401 0 $self->cache->set( "basic-has-a-or-mx-$domain", "OK" );
402 0 return 1;
403 }
404  
405  
406 =head2 _resolute_domain_to_ip
407  
408 Resolutes either A or MX recrod to IP(s)
409  
410 =cut
411  
412 0 ▶sub _resolute_domain_to_ip {
Subroutine _resolute_domain_to_ip: not called CC=11 SCAR=48.8
413 0 my ( $self, $domain ) = @_;
414
415 0 my $cached = $self->cache->get( "basic-resolute-to-ip-$domain" );
416 0 ▶ return $cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 0 0
417
418 # has A records ?
419 0 my $res = $self->resolver->search( $domain, 'A' );
420 0 ▶ my @a_rec = $res ? ( grep { defined $_ && $_->address } $res->answer ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 0 0 0
Condition0%0/3
defined $_ && $_->address
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $_->address
defined $_ $_->address
Truth table0%0/3
A && B
A= defined $_B= $_->address
ABresult
0X0
100
111
421
422 # found those A records
423 0 ▶ if ( @a_rec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a_rec) 0 0 0
424 0 @a_rec = map { $_->address } @a_rec;
425 0 $self->cache->set( "basic-resolute-to-ip-$domain", \@a_rec );
426 0 return \@a_rec;
427 }
428
429 # has MX records ?
430 0 my $res_mx = $self->resolver->search( $domain, 'MX' );
431 0 ▶ my @mx_rec = $res_mx ? ( grep { defined $_ && $_->exchange } $res_mx->answer ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$res_mx ? : 0 0 0
Condition0%0/3
defined $_ && $_->exchange
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $_->exchange
defined $_ $_->exchange
Truth table0%0/3
A && B
A= defined $_B= $_->exchange
ABresult
0X0
100
111
432
433 # found those A records
434 0 ▶ if ( @mx_rec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mx_rec) 0 0 0
435 0 foreach my $mx_rec( @mx_rec ) {
436 0 my $ip_ref = $self->_resolute_domain_to_ip( $mx_rec->exchange );
437 0 ▶ if ( $ip_ref && $#$ip_ref > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip_ref and $ip_ref->$#* > -1) 0 0 0
Condition0%0/3
$ip_ref and $ip_ref->$#* > -1
!ll&&!rl&&r
000
MC/DC0%0/2
$ip_ref and $ip_ref->$#* > -1
$ip_ref $ip_ref->$#* > -1
Truth table0%0/3
A and B
A= $ip_refB= $ip_ref->$#* > -1
ABresult
0X0
100
111
438 0 $self->cache->set( "basic-resolute-to-ip-$domain", $ip_ref );
439 0 return $ip_ref;
440 }
441 }
442 }
443
444 0 return ;
445 }
446  
447  
448  
449 =head1 AUTHOR
450  
451 Ulrich Kautz <uk@fortrabbit.de>
452  
453 =head1 COPYRIGHT
454  
455 Copyright (c) 2010 the L</AUTHOR> as listed above
456  
457 =head1 LICENCSE
458  
459 This library is free software and may be distributed under the same terms as perl itself.
460  
461 =cut
462  
463  
464  
465 1 1;
« lib/Mail/Decency/LogParser/Stats.pm ↑ Summary lib/Mail/Decency/Core/ExportImport.pm »