lib/Mail/Decency/Policy/DNSBL.pm

filter: Statementstmt 44.6% 25 / 56 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 60.0% 6 / 10 Podpod 100% 2 / 2 totaltotal 31.7% 33 / 104 CC 15 SCAR 38.8
CC
15
Cov
30%
init
72.0
handle
72.0
__ANON__
1.1
CRAP 48.3
« lib/Mail/Decency/LogParser/Core/CSV.pm ↑ Summary lib/Mail/Decency/Helper/Logger.pm »
1 package Mail::Decency::Policy::DNSBL;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 extends 'Mail::Decency::Policy::Core';
6  
7 1 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Net::DNSBL::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mail::Decency::Helper::IP qw/ is_local_host /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Mail::Decency::Policy::DNSBL
16  
17 =head1 DESCRIPTION
18  
19 Implementation of a DNS-based Blackhole List using L<Net::DNSBL::Client>.
20  
21 =head2 CONFIG
22  
23 ---
24
25 disable: 0
26
27 harsh: 0
28
29 blacklist:
30
31 -
32 host: ix.dnsbl.manitu.net
33 weight: -100
34 -
35 host: psbl.surriel.com
36 weight: -80
37 -
38 host: dnsbl.sorbs.net
39 weight: -70
40
41  
42 =head1 DESCRIPTION
43  
44 Check external DNS blacklists (DNSBL). Allows weighting per blacklis or harsh policies (first hit serves).
45  
46  
47 =head1 CLASS ATTRIBUTES
48  
49 =head2 blacklist
50  
51 ArrayRef of blacklists
52  
53 =head2 weight
54  
55 HashRef of ( domain => weight ) for each blacklist
56  
57 =head2 dnsbl
58  
59 Instance of L<Net::DNSBL::Client>
60  
61 =head2 harsh
62  
63 Bool value determining wheter first blacklist hit rejects mail
64  
65 =cut
66  
67 0 ▶has blacklist => ( is => 'rw', isa => 'ArrayRef[HashRef]', default => sub { [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
68 0 ▶has weight => ( is => 'rw', isa => 'HashRef[Int]', default => sub { {} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
69 1 has dnsbl => ( is => 'ro' );
70 1 has harsh => ( is => 'ro', isa => 'Bool' );
71  
72 =head1 METHODS
73  
74  
75  
76 =head2 init
77  
78 =cut
79  
80 0 ▶sub init {
Subroutine init: not called CC=8 SCAR=42.8
Pod documented
81 0 my ( $self ) = @_;
82
83 # @@@@@@@@@@@@@ TODO @@@@@@@@@@@@@@@@@@
84 # >> Deactivate for localhost
85 # >> Test performance of Net::DNS
86 # @@@@@@@@@@@@@ TODO @@@@@@@@@@@@@@@@@@
87
88 # check blacklists
89 die "DNSBL: Require 'blacklist' as array\n"
90 0 ▶ unless defined $self->config->{ blacklist } && ref( $self->config->{ blacklist } ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->config->{"blacklist"} and ref $self->config->{"blacklist"} eq "ARRAY" 0 0 0
Condition0%0/3
defined $self->config->{"blacklist"} and ref $self->config->{"blacklist"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->config->{"blacklist"} and ref $self->config->{"blacklist"} eq "ARRAY"
defined $self->config->{"blacklist"} ref $self->config->{"blacklist"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $self->config->{"blacklist"}B= ref $self->config->{"blacklist"} eq "ARRAY"
ABresult
0X0
100
111
91
92 # build blacklists
93 0 my $num = 1;
94 0 my @blacklists = ();
95 0 foreach my $ref( @{ $self->config->{ blacklist } } ) {
96 0 ▶ die "DNSBL: Blacklist $num is not a hashref\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref eq "HASH" 0 0 0
97 unless ref( $ref ) eq 'HASH';
98 push @blacklists, {
99 domain => $ref->{ host }
100 0 };
101 0 ▶ $self->weight->{ $ref->{ host } } = $ref->{ weight } || -100;
Condition0%0/2
$ref->{'weight'} || -100
l!l
00
MC/DC0%0/1
$ref->{'weight'} || -100
$ref->{'weight'}
Truth table0%0/2
A
A= $ref->{'weight'}
Aresult
11
00
102 0 $num++;
103 }
104
105 # remember blacklists
106 0 $self->blacklist( \@blacklists );
107
108 # setup new dnsbl client
109 0 ▶ $self->{ dnsbl } = Net::DNSBL::Client->new( { timeout => $self->config->{ timeout } || 3 } );
Condition0%0/2
$self->config->{'timeout'} || 3
l!l
00
MC/DC0%0/1
$self->config->{'timeout'} || 3
$self->config->{'timeout'}
Truth table0%0/2
A
A= $self->config->{'timeout'}
Aresult
11
00
110
111 # wheter use harash policy ?
112 0 ▶ $self->{ harsh } = $self->config->{ harsh } || 0;
Condition0%0/2
$self->config->{'harsh'} || 0
l!l
00
MC/DC0%0/1
$self->config->{'harsh'} || 0
$self->config->{'harsh'}
Truth table0%0/2
A
A= $self->config->{'harsh'}
Aresult
11
00
113
114 0 return $self->next::method();
115 }
116  
117  
118 =head2 handle
119  
120 Checks wheter incoming mail is whilist for final recipient
121  
122 =cut
123  
124 0 ▶sub handle {
Subroutine handle: not called CC=8 SCAR=42.8
Pod documented
125 0 my ( $self, $server, $attrs_ref ) = @_;
126
127 0 my @reject_info;
128
129 # go through all blacklists one bye one
130 # don't stress all blacklists, if not required!
131 0 foreach my $list_ref( @{ $self->blacklist } ) {
132
133 # query blacklist now
134 0 $self->dnsbl->query_ip( $attrs_ref->{ client_address }, [ $list_ref ] );
135
136 # retreive anwer
137 0 my $result_ref = $self->dnsbl->get_answers;
138 0 ▶ $result_ref = $result_ref->[0] if ref( $result_ref ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result_ref eq "ARRAY" 0 0 0
139
140 # any hit ??
141 0 ▶ if ( $result_ref && ref( $result_ref ) eq 'HASH' && $result_ref->{ hit } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_ref and ref $result_ref eq 'HASH' and $result_ref->{'hit'}) { } 0 0 0
Condition0%0/6
$result_ref and ref $result_ref eq "HASH" and $result_ref->{"hit"}
!ll&&!rl&&r
000
$result_ref and ref $result_ref eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$result_ref and ref $result_ref eq "HASH" and $result_ref->{"hit"}
$result_ref ref $result_ref eq "HASH" $result_ref->{"hit"}
Truth table0%0/4
A and B and C
A= $result_refB= ref $result_ref eq "HASH"C= $result_ref->{"hit"}
ABCresult
0XX0
10X0
1100
1111
142 # collect weight
143 0 ▶ my $add_weight = $self->weight->{ $list_ref->{ domain } } || 0;
Condition0%0/2
$self->weight->{$list_ref->{'domain'}} || 0
l!l
00
MC/DC0%0/1
$self->weight->{$list_ref->{'domain'}} || 0
$self->weight->{$list_ref->{'domain'}}
Truth table0%0/2
A
A= $self->weight->{$list_ref->{'domain'}}
Aresult
11
00
144
145 # log out ..
146 0 $self->logger->debug0( "Hit on $list_ref->{ domain } for $attrs_ref->{ client_address }, weight $add_weight ('$attrs_ref->{ sender_address }' -> '$attrs_ref->{ recipient_address }')" );
147
148 # update reject details..
149 0 push @reject_info, "$result_ref->{ domain }";
150 0 my $reject_info = "Blacklisted on ". join( ", ", @reject_info );
151
152 # add weight .. (throws exception if final state)
153 0 $self->add_spam_score(
154 $add_weight, "$list_ref->{ domain }: hit ($add_weight)", $reject_info );
155
156 # final state if harsh policy
157 0 ▶ $self->go_final_state( REJECT => $reject_info ) if $self->harsh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->harsh 0 0 0
158 }
159
160 # no hit -> pass
161 else {
162 0 $self->logger->debug3( "Pass on $list_ref->{ domain } for $attrs_ref->{ client_address } ('$attrs_ref->{ sender_address }' -> '$attrs_ref->{ recipient_address }')" );
163 }
164 }
165
166 }
167  
168 =head1 AUTHOR
169  
170 Ulrich Kautz <uk@fortrabbit.de>
171  
172 =head1 COPYRIGHT
173  
174 Copyright (c) 2010 the L</AUTHOR> as listed above
175  
176 =head1 LICENCSE
177  
178 This library is free software and may be distributed under the same terms as perl itself.
179  
180 =cut
181  
182 1 1;
« lib/Mail/Decency/LogParser/Core/CSV.pm ↑ Summary lib/Mail/Decency/Helper/Logger.pm »