lib/Mail/Decency/Policy/Association.pm

filter: Statementstmt 26.8% 32 / 119 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 43.7% 7 / 16 Podpod 100% 4 / 4 totaltotal 19.9% 43 / 216 CC 40 SCAR 57.7
CC
40
Cov
17%
handle
650.0
get_records
90.0
init
72.0
CRAP 321.0
« lib/Mail/Decency/LogParser/Core/PostfixParser.pm ↑ Summary lib/Mail/Decency/Core/POEForking/SMTP.pm »
1 package Mail::Decency::Policy::Association;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends qw/
5 Mail::Decency::Policy::Core
6 /;
7  
8 1 ▶use version 0.74; our $VERSION = qv( "v0.1.5" );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Mail::Decency::Helper::IP qw/ is_local_host /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Net::Netmask;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::Domain::TLD qw/ tlds /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Mail::Decency::Policy::Association
19  
20 =head1 DESCRIPTION
21  
22 This module tries to figure out wheter the sender domain is somewhat related to the sending server.
23  
24 There are three methods to determine wheter this is the case:
25  
26 =over
27  
28 =item 1. direct IP hit
29  
30 The sender IP name is equal to the A or AAAA of the domain or the resolved (CNAME, MX) record.
31  
32 Example 1: sender ip is 123.123.123.123 and the first MX record of the sender domain sender.tld is points to mail.sender.tld which has the IP 123.123.123.123
33  
34 Example 2: sender ip is 123.123.213.123 and the A record of sender.tld is 123.123.123.123
35  
36 =item 2. domain hit
37  
38 Tries to identify the association via the domain name.
39  
40 Example: sender domain is something.sender.tld and the reverse hostname of the sender IP is mail.domain.tld -> both under sender.tld.
41  
42 =item 3. IP range hit
43  
44 Tries to identify the relation via ip ranges.
45  
46 Example: ip of the sender is 123.123.123.123 and ip of the A record of the sender domain is 123.123.123.122 which is within a /31 subnet
47  
48 The bigger the subnet, the lower the positive weight (can be adjusted).
49  
50 =back
51  
52 If no relation could be determined, the session will be scored negatively.
53  
54 Keep in mind: the association of the sender ip to the sender domain does not imply at all that the mail is not spam. Only the opposite gives a hint, that the mail might be forged - or one of those webservers not associated to the domain sending a (maybe even valid) mail.
55  
56 =head1 CONFIG
57  
58 ---
59
60 disable: 0
61
62 weight_direct_hit: 20
63 weight_domain_hit: 15
64 weight_range_hit:
65 31: 20
66 30: 20
67 29: 10
68 28: 10
69 27: 10
70 26: 5
71 25: 5
72 24: 5
73 weight_no_hit: -20
74
75  
76  
77 =head1 CLASS ATTRIBUTES
78  
79 =head2 resolver : Net::DNS::Resolver
80  
81 Will be created automatically.
82  
83 =cut
84  
85 0 ▶has resolver => ( is => 'ro', isa => 'Net::DNS::Resolver', default => sub { Net::DNS::Resolver->new } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
86  
87 =head2 rx_tlds* : RegexpRef
88  
89 Pre-compiled regex containing tlds.
90  
91 =cut
92  
93 0 ▶has rx_tlds => ( is => 'rw', isa => 'RegexpRef', default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 0 my $s = join( "|", sort { length($b) <=> length($a) } tlds() );
95 0 return qr/\.($s)$/;
96 1 } );
97 0 ▶has rx_tlds_com => ( is => 'rw', isa => 'RegexpRef', default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 0 my $s = join( "|", map { "com\\.$_" } sort { length($b) <=> length($a) } tlds() );
99 0 return qr/\.($s)$/;
100 1 } );
101 0 ▶has rx_tlds_co => ( is => 'rw', isa => 'RegexpRef', default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 my $s = join( "|", map { "co\\.$_" } sort { length($b) <=> length($a) } tlds() );
103 0 return qr/\.($s)$/;
104 1 } );
105  
106 =head2 weight_direct_hit : Int
107  
108 The sender domain is directly associated to the client address
109  
110 =cut
111  
112 1 has weight_direct_hit => ( is => 'rw', isa => 'Int', default => 20 );
113  
114 =head2 weight_range_hit : HashRef[Int]
115  
116 The sender domain is associated via an ip range to the client address ..
117  
118 =cut
119  
120 0 ▶has weight_range_hit => ( is => 'rw', isa => 'HashRef[Int]', default => sub { {
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 0 31 => 20,
122 30 => 20,
123 29 => 10,
124 28 => 10,
125 27 => 10,
126 26 => 5,
127 25 => 5,
128 24 => 5
129 1 } } );
130  
131 =head2 weight_domain_hit : Int
132  
133 The sender domain is via a shared domain name to the client address. Eg the client address resolves to smtp.somedomain.tld and the sender is somedomain.tld or the sender's mx is mx.somedomain.tld whereas they at least somedomain.tld
134  
135 =cut
136  
137 1 has weight_domain_hit => ( is => 'rw', isa => 'Int', default => 15 );
138  
139 =head2 weight_no_hit
140  
141 Negative score. No match found.
142  
143 =cut
144  
145 1 has weight_no_hit => ( is => 'rw', isa => 'Int', default => -20 );
146  
147  
148  
149  
150  
151 =head1 METHODS
152  
153  
154 =head2 init
155  
156 Checks weight_range_hit for correctness, reads config
157  
158 =cut
159  
160 0 ▶sub init {
Subroutine init: not called CC=8 SCAR=42.8
Pod documented
161 0 my ( $self ) = @_;
162
163 0 ▶ if ( defined $self->config->{ weight_range_hit } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->config->{"weight_range_hit"}) 0 0 0
164 die "Association: weight_range_hit has to be a HashRef\n"
165 0 ▶ unless ref( $self->config->{ weight_range_hit } ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->config->{"weight_range_hit"} eq "HASH" 0 0 0
166 0 ▶ while ( my ( $k, $v ) = each %{ $self->config->{ weight_range_hit } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->config->{"weight_range_hit"};}) 0 0 0
167 0 ▶ die "Association: weight_range_hit key '$k' is not an integer\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^\d+$/ 0 0 0
168 unless $k =~ /^\d+$/;
169 0 ▶ die "Association: weight_range_hit value '$v' for key '$k' is not an integer\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /^\d+$/ 0 0 0
170 unless $v =~ /^\d+$/;
171 }
172 }
173
174 0 foreach my $key( qw/ weight_direct_hit weight_domain_hit weight_no_hit weight_range_hit / ) {
175 $self->$key( $self->config->{ $key } )
176 0 ▶ if defined $self->config->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->config->{$key} 0 0 0
177 }
178
179 0 return;
180 }
181  
182  
183 =head2 handle
184  
185 Never handle anything from localhost. Handle results are cached. First checks for exact match, then domain hit, then range hit.
186  
187 =cut
188  
189 0 ▶sub handle {
Subroutine handle: not called CC=25 SCAR=64.8
Pod documented
190 0 my ( $self, $server, $attrs_ref ) = @_;
191
192 # don bother with loopback addresses! EVEN IF ENABLED BY FORCE!
193 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
194
195 # if spf module is used and passes -> no need to verify this again
196 0 ▶ return if $self->session_data->has_flag( 'spf_pass' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->session_data->has_flag("spf_pass") 0 0 0
197
198 #
199 # CACHES
200 #
201
202 0 my $cache_name = "Association-$attrs_ref->{ sender_domain }-$attrs_ref->{ client_address }";
203 0 ▶ if ( my $cached = $self->cache->get( $cache_name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached = $self->cache->get($cache_name)) 0 0 0
204 0 return $self->add_spam_score( @$cached );
205 }
206
207
208 #
209 # GET DOMAIN DNS
210 #
211
212 0 my $domain_ref = $self->get_records( $attrs_ref->{ sender_domain } );
213
214 # not a valid domain, no records found!
215 return $self->finish( $cache_name => $self->weight_no_hit => "Association: No hit, sender domain not found", "Sender domain could not be resolved" )
216 0 ▶ if scalar keys %{ $domain_ref } == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$domain_ref;} == 0 0 0 0
217
218
219
220 #
221 # EXACT IP MATCH
222 #
223
224 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain_ref->{"A"} and defined $domain_ref->{"A"}{$attrs_ref->{"client_address"}} or defined $domain_ref->{"AAAA"} and defined $domain_ref->{"AAAA"}{$attrs_ref->{"client_address"}}) 0 0 0
Condition0%0/9
defined $domain_ref->{"A"} and defined $domain_ref->{"A"}{$attrs_ref->{"client_address"}} or defined $domain_ref->{"AAAA"} and defined $domain_ref->{"AAAA"}{$attrs_ref->{"client_address"}}
l!l&&r!l&&!r
000
defined $domain_ref->{"A"} and defined $domain_ref->{"A"}{$attrs_ref->{"client_address"}}
!ll&&!rl&&r
000
defined $domain_ref->{"AAAA"} and defined $domain_ref->{"AAAA"}{$attrs_ref->{"client_address"}}
!ll&&!rl&&r
000
MC/DC0%0/4
defined $domain_ref->{"A"} and defined $domain_ref->{"A"}{$attrs_ref->{"client_address"}} or defined $domain_ref->{"AAAA"} and defined $domain_ref->{"AAAA"}{$attrs_ref->{"client_address"}}
defined $domain_ref->{"A"} defined $domain_ref->{"A"}{$attrs_ref->{"client_address"}} defined $domain_ref->{"AAAA"} defined $domain_ref->{"AAAA"}{$attrs_ref->{"client_address"}}
Truth table0%0/7
A and B or C and D
A= defined $domain_ref->{"A"}B= defined $domain_ref->{"A"}{$attrs_ref->{"client_address"}}C= defined $domain_ref->{"AAAA"}D= defined $domain_ref->{"AAAA"}{$attrs_ref->{"client_address"}}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
225 ( defined $domain_ref->{ A } && defined $domain_ref->{ A }->{ $attrs_ref->{ client_address } } )
226 || ( defined $domain_ref->{ AAAA } && defined $domain_ref->{ AAAA }->{ $attrs_ref->{ client_address } } )
227 ) {
228 0 $self->finish( $cache_name => $self->weight_direct_hit => "Association: Direct IP hit" );
229 }
230
231
232 #
233 # IP RANGE MATCHING
234 # only with ipv4
235 #
236 0 ▶ if ( defined ( my $ips_ref = $domain_ref->{ A } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ips_ref = $domain_ref->{"A"})) 0 0 0
237 0 foreach my $ip( keys %$ips_ref ) {
238 0 foreach my $net( keys %{ $self->weight_range_hit } ) {
239 0 my $mask = Net::Netmask->new( "$ip/$net" );
240 0 ▶ if ( $mask->match( $attrs_ref->{ client_address } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask->match($attrs_ref->{"client_address"})) 0 0 0
241 0 my $weight = $self->weight_range_hit->{ $net };
242 0 return $self->finish( $cache_name => $weight => "Association: Range hit in /$net-network" );
243 }
244 }
245 }
246 }
247
248
249 #
250 # DOMAIN PREFIX MATCHING
251 #
252
253 0 my $client_ref = $self->get_records( $attrs_ref->{ client_address } );
254 my @rdns = defined $client_ref && defined $client_ref->{ PTR }
255 0 ▶ ? keys %{ $client_ref->{ PTR } }
Branch: 1 branch
BranchTrueFalseTotal
defined $client_ref && defined $client_ref->{'PTR'} ? : 0 0 0
Condition0%0/3
defined $client_ref and defined $client_ref->{"PTR"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $client_ref and defined $client_ref->{"PTR"}
defined $client_ref defined $client_ref->{"PTR"}
Truth table0%0/3
A and B
A= defined $client_refB= defined $client_ref->{"PTR"}
ABresult
0X0
100
111
256 : ()
257 ;
258
259 CHECK_RDNS:
260 0 foreach my $rdns( @rdns ) {
261 0 my ( $rx, $prefix );
262
263 # get all regular expressions for matching
264 0 my $rx_tlds = $self->rx_tlds;
265 0 my $rx_tlds_co = $self->rx_tlds_co;
266 0 my $rx_tlds_com = $self->rx_tlds_com;
267
268 # is a prefixed com domain (eg somedomain.com.asia)
269 0 ▶ if ( $rdns =~ /^(.+?)$rx_tlds_com/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rdns =~ /^(.+?)$rx_tlds_com/) { } 0 0 0
elsif ($rdns =~ /^(.+?)$rx_tlds_co/) { } 0 0 0
elsif ($rdns =~ /^(.+?)$rx_tlds/) { } 0 0 0
270 0 $prefix = $1;
271 0 $rx = $rx_tlds_com;
272 }
273
274 # is a prefixed co domain (eg somedomain.co.uk)
275 elsif ( $rdns =~ /^(.+?)$rx_tlds_co/ ) {
276 0 $prefix = $1;
277 0 $rx = $rx_tlds_co;
278 }
279
280 # is not prefix, but has a tld (eg somedomain.com)
281 elsif ( $rdns =~ /^(.+?)$rx_tlds/ ) {
282 0 $prefix = $1;
283 0 $rx = $rx_tlds;
284 }
285
286 0 ▶ next CHECK_RDNS unless $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 0 0 0
287
288 # found prefix (orig: domain.tld -> domain)
289
290 # get last prefix .. from mail.somewhere it would be somewhere
291 0 my @prefix = split( /\./, $prefix );
292 0 $prefix = pop @prefix;
293
294 ALL_RECORDS:
295 0 foreach my $rr( qw/ PTR MX CNAME / ) {
296 0 ▶ my $records_ref = $domain_ref->{ $rr } || next ALL_RECORDS;
Condition0%0/2
$domain_ref->{$rr} || (next ALL_RECORDS)
l!l
00
MC/DC0%0/1
$domain_ref->{$rr} || (next ALL_RECORDS)
$domain_ref->{$rr}
Truth table0%0/2
A
A= $domain_ref->{$rr}
Aresult
11
00
297
298 # check all found records
299 0 foreach my $dns( keys %$records_ref ) {
300
301 # does match ?
302 0 ▶ if ( $dns =~ /^(.+?)$rx/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dns =~ /^(.+?)$rx/) 0 0 0
303
304 # extract the last prefix (from some.domain.tld that would be domain)
305 0 my $dns_prefix = $1;
306 0 my @dns_prefix = split( /\./, $dns_prefix );
307 0 $dns_prefix = pop @dns_prefix;
308
309 # is actually matching, we got a winner
310 0 ▶ if ( $dns_prefix eq $prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dns_prefix eq $prefix) 0 0 0
311
312 # return here
313 0 return $self->finish( $cache_name =>
314 $self->weight_domain_hit => "Association: Domain hit" );
315 }
316 }
317 }
318 }
319 }
320
321 #
322 # NO MATCH AT ALL!
323 #
324 0 return $self->finish( $cache_name => $self->weight_no_hit => "Association: No hit", "Sender IP not associated with sender domain" );
325 }
326  
327  
328 =head2 finish
329  
330 Write to cache, add spam score.
331  
332 =cut
333  
334 0 ▶sub finish {
Subroutine finish: not called CC=1 SCAR=6.9
Pod documented
335 0 my ( $self, $cache_name, $weight, $details, $reject_message ) = @_;
336
337 # cache result
338 0 $self->cache->set( $cache_name => [
339 $weight => $details => $reject_message
340 ] );
341
342 # return here
343 0 return $self->add_spam_score( $weight => $details => $reject_message );
344 }
345  
346  
347 =head2 get_records
348  
349 Retreive records for a hostname (A, CNAME, MX) / ip (PTR)
350  
351 =cut
352  
353 0 ▶sub get_records {
Subroutine get_records: not called CC=9 SCAR=45.0
Pod documented
354 0 my ( $self, $domain, $seen_ref, $records_ref ) = @_;
355 0 ▶ $seen_ref ||= {};
Condition0%0/2
$seen_ref ||= {}
l!l
00
MC/DC0%0/1
$seen_ref ||= {}
$seen_ref
Truth table0%0/2
A
A= $seen_ref
Aresult
11
00
356 0 $seen_ref->{ $domain } ++;
357 0 ▶ $records_ref ||= {};
Condition0%0/2
$records_ref ||= {}
l!l
00
MC/DC0%0/1
$records_ref ||= {}
$records_ref
Truth table0%0/2
A
A= $records_ref
Aresult
11
00
358
359 # init methods for record types
360 0 my %meth = qw(
361 PTR ptrdname
362 A address
363 CNAME cname
364 MX exchange
365 );
366
367 0 foreach my $class( qw/ A CNAME MX PTR / ) {
368
369 # retreive records of this type
370 0 my $resolver_res = $self->resolver->search( $domain, $class );
371
372 # not any -> bye
373 0 ▶ next unless $resolver_res;
Branch: 1 branch
BranchTrueFalseTotal
unless $resolver_res 0 0 0
374
375 0 foreach my $rr( $resolver_res->answer ) {
376
377 # get method
378 0 ▶ my $meth = $meth{ $rr->type } || 'address';
Condition0%0/2
$meth{$rr->type} || 'address'
l!l
00
MC/DC0%0/1
$meth{$rr->type} || 'address'
$meth{$rr->type}
Truth table0%0/2
A
A= $meth{$rr->type}
Aresult
11
00
379
380 # init empty, if not existing
381 0 ▶ $records_ref->{ $rr->type } ||= {};
Condition0%0/2
$$records_ref{$rr->type} ||= {}
l!l
00
MC/DC0%0/1
$$records_ref{$rr->type} ||= {}
$$records_ref{$rr->type}
Truth table0%0/2
A
A= $$records_ref{$rr->type}
Aresult
11
00
382
383 # remember this
384 0 $records_ref->{ $rr->type }->{ $rr->$meth } = 1;
385
386 # get next records, unless already seen
387 $self->get_records( $rr->$meth, $seen_ref, $records_ref )
388 0 ▶ unless $seen_ref->{ $rr->$meth };
Branch: 1 branch
BranchTrueFalseTotal
unless $$seen_ref{$rr->$meth} 0 0 0
389 }
390 }
391
392 0 return $records_ref;
393 }
394  
395  
396 =head1 AUTHOR
397  
398 Ulrich Kautz <uk@fortrabbit.de>
399  
400 =head1 COPYRIGHT
401  
402 Copyright (c) 2010 the L</AUTHOR> as listed above
403  
404 =head1 LICENCSE
405  
406 This library is free software and may be distributed under the same terms as perl itself.
407  
408 =cut
409  
410  
411  
412 1 1;
« lib/Mail/Decency/LogParser/Core/PostfixParser.pm ↑ Summary lib/Mail/Decency/Core/POEForking/SMTP.pm »