MtPolicyd/Plugin/DBL.pm

filter: Statementstmt 70.6% 53 / 75 Branchbran 41.6% 10 / 24 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 61.3% 73 / 119 CC 16 SCAR 23.9
CC
16
Cov
61%
run
18.2
_get_hostname
12.1
_get_reject_action
6.0
CRAP 10.9
« MtPolicyd/Client.pm ↑ Summary MtPolicyd/Client/App.pm »
1 package Mail::MtPolicyd::Plugin::DBL;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '1.12'; # VERSION
7 # ABSTRACT: mtpolicyd plugin for checking helo,sender domain,rdns against an DBL
8  
9 2 extends 'Mail::MtPolicyd::Plugin';
10 2 with 'Mail::MtPolicyd::Plugin::Role::Scoring';
11 2 with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => {
12 'uc_attributes' => [ 'enabled' ],
13 };
14  
15 2 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Mail::RBL;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 2 has 'domain' => ( is => 'rw', isa => 'Str', required => 1 );
21  
22 2 has 'enabled' => ( is => 'rw', isa => 'Str', default => 'on' );
23  
24 2 has 'sender_mode' => ( is => 'rw', isa => 'Str', default => 'reject' );
25 2 has 'sender_score' => ( is => 'rw', isa => 'Maybe[Num]', default => 5 );
26  
27 2 has 'reverse_client_name_mode' => ( is => 'rw', isa => 'Str', default => 'reject' );
28 2 has 'reverse_client_name_score' => ( is => 'rw', isa => 'Maybe[Num]', default => 2.5 );
29  
30 2 has 'helo_name_mode' => ( is => 'rw', isa => 'Str', default => 'passive' );
31 2 has 'helo_name_score' => ( is => 'rw', isa => 'Maybe[Num]', default => 1 );
32  
33 2 has 'reject_message' => ( is => 'rw', isa => 'Str',
34 default => '%CHECK% rejected (%HOSTNAME%%INFO%)' );
35  
36 has '_rbl' => (
37 is => 'ro', isa => 'Mail::RBL', lazy => 1,
38 default => sub {
39 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 Mail::RBL->new($self->domain)
41 },
42 2 );
43  
44 sub run {
45 2 ▶ my ( $self, $r ) = @_;
Subroutine run: called CC=8 SCAR=29.0
Pod documented
46 2 my $session = $r->session;
47  
48 2 my $enabled = $self->get_uc( $session, 'enabled' );
49 2 ▶ if( $enabled eq 'off' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enabled eq "off") 0 2 2
50 0 return;
51 }
52  
53 2 foreach my $check ( 'sender', 'reverse_client_name', 'helo_name') {
54 6 my $hostname = $self->_get_hostname($r, $check);
55 6 ▶ if( ! defined $hostname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hostname) 0 6 6
56 0 next;
57 }
58  
59 my ( $ip_result, $info ) = $r->do_cached( $self->name.'-'.$check.'-result',
60 6 ▶ sub { $self->_rbl->check_rhsbl( $hostname ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
61 6 ▶ if( ! defined $ip_result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip_result) 6 0 6
62 6 $self->log($r, 'domain '.$hostname.' not on '.$self->domain.' blacklist');
63 6 next;
64 }
65  
66 0 $self->log($r, 'domain '.$hostname.' is on '.$self->domain.' blacklist ('.$info.')');
67  
68 0 my $score_attr = $check.'_score';
69 0 ▶ if( defined $self->$score_attr &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->$score_attr and not $r->is_already_done($self->name . '-' . $check . '-score')) 0 0 0
Condition0%0/3
defined $self->$score_attr and not $r->is_already_done($self->name . '-' . $check . '-score')
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->$score_attr and not $r->is_already_done($self->name . '-' . $check . '-score')
defined $self->$score_attr not $r->is_already_done($self->name . '-' . $check . '-score')
Truth table0%0/3
A and not(B)
A= defined $self->$score_attrB= not $r->is_already_done($self->name . '-' . $check . '-score')
ABresult
0X0
100
111
70 ! $r->is_already_done($self->name.'-'.$check.'-score') ) {
71 0 $self->add_score($r, $self->name.'-'.$check => $self->$score_attr );
72 }
73  
74 0 my $mode_field = $check.'_mode';
75 0 ▶ if( $self->$mode_field eq 'reject' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$mode_field eq "reject") 0 0 0
76 0 return Mail::MtPolicyd::Plugin::Result->new(
77 action => $self->_get_reject_action($check, $hostname, $info),
78 abort => 1,
79 );
80 }
81 }
82  
83 2 return;
84 }
85  
86 sub _get_hostname {
87 6 ▶ my ( $self, $r, $field ) = @_;
Subroutine _get_hostname: called CC=8 SCAR=24.9
88 6 my $value = $r->attr($field);
89 6 ▶ if( ! defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 6 6
90 0 die($field.' not defined in request!');
91 }
92 # skip unknown and empty fields
93 6 ▶ if( $value eq 'unknown' || $value eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "unknown" or $value eq "") 0 6 6
Condition33%1/3
$value eq "unknown" or $value eq ""
l!l&&r!l&&!r
006
MC/DC0%0/2
$value eq "unknown" or $value eq ""
$value eq "unknown" $value eq ""
Truth table33%1/3
A or B
A= $value eq "unknown"B= $value eq ""
ABresult
1X1
011
000
94 0 return;
95 }
96 # skip ip addresses
97 6 ▶ if( $value =~ m/^\d+\.\d+\.\d+\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\d+\.\d+\.\d+\.\d+$/) 0 6 6
98 0 return;
99 }
100 # skip ip6 addresses
101 6 ▶ if( $value =~ m/:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /:/) 0 6 6
102 0 return;
103 }
104 # skip unqualified hostnames
105 6 ▶ if( $value !~ m/\./) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /\./) 0 6 6
106 0 return;
107 }
108  
109 6 ▶ if( $field eq 'sender') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "sender") 2 4 6
110 2 $value =~ s/^[^@]*@//;
111 }
112 6 return($value);
113 }
114  
115 sub _get_reject_action {
116 0 ▶ my ( $self, $check, $hostname, $info ) = @_;
Subroutine _get_reject_action: not called CC=2 SCAR=17.9
117 0 my $msg = $self->reject_message;
118 0 $msg =~ s/%CHECK%/$check/;
119 0 $msg =~ s/%HOSTNAME%/$hostname/;
120 0 ▶ if( defined $info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info) { } 0 0 0
121 0 $msg =~ s/%INFO%/, $info/;
122 } else {
123 0 $msg =~ s/%INFO%//;
124 }
125  
126 0 return 'reject '.$msg;
127 }
128  
129 2 __PACKAGE__->meta->make_immutable;
130  
131 2 1;
132  
133  
134 __END__
135 =pod
136  
137 =head1 NAME
138  
139 Mail::MtPolicyd::Plugin::DBL - mtpolicyd plugin for checking helo,sender domain,rdns against an DBL
140  
141 =head1 VERSION
142  
143 version 1.12
144  
145 =head1 DESCRIPTION
146  
147 Will check the sender, helo and reverse_client_name against an domain black list.
148  
149 =head1 PARAMETERS
150  
151 =over
152  
153 =item domain (required)
154  
155 The domain of the blacklist to query.
156  
157 =item enabled (default: on)
158  
159 Set to 'off' to disable plugin.
160  
161 Possible values: on,off
162  
163 =item uc_enabled (default: empty)
164  
165 If specified the give variable within the session will overwrite the value of 'enabled' if set.
166  
167 =item sender_mode (default: reject)
168  
169 =item helo_mode (default: passive)
170  
171 =item reverse_client_name_mode (default: reject)
172  
173 Should the plugin return an reject if the check matches (reject) or
174 just add an score (passive).
175  
176 Possible values: reject, passive
177  
178 =item sender_score (default: 5)
179  
180 =item helo_score (default: 1)
181  
182 =item reverse_client_name_score (default: 2.5)
183  
184 Add the given score if check matched.
185  
186 =item score_field (default: score)
187  
188 Name of the session variable the score is stored in.
189 Could be used if multiple scores are needed.
190  
191 =back
192  
193 =head1 EXAMPLE
194  
195 Only the sender and the reverse_client_name check will cause an
196 action to be executed (mode).
197 The helo check will only add an score.
198  
199 <Plugin sh_dbl>
200 module = "RBL"
201 #enabled = "on"
202 uc_enabled = "spamhaus"
203 domain="dbl.spamhaus.org"
204  
205 # do not reject based on helo
206 #helo_name_mode=passive
207 #helo_name_score=1
208 #sender_mode=reject
209 #sender_score=5
210 #reverse_client_name_mode=reject
211 #reverse_client_name_score=2.5
212 </Plugin>
213  
214 =head1 AUTHOR
215  
216 Markus Benning <ich@markusbenning.de>
217  
218 =head1 COPYRIGHT AND LICENSE
219  
220 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
221  
222 This is free software, licensed under:
223  
224 The GNU General Public License, Version 2, June 1991
225  
226 =cut
227  
« MtPolicyd/Client.pm ↑ Summary MtPolicyd/Client/App.pm »