MtPolicyd/Plugin/SPF.pm

filter: Statementstmt 74.1% 92 / 124 Branchbran 36.9% 17 / 46 Conditioncond 14.2% 3 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 57.7% 127 / 220 CC 31 SCAR 40.2
CC
31
Cov
59%
_check_spf_result
124.5
run
19.1
_get_reject_action
5.7
CRAP 55.8
« MtPolicyd/Plugin/GeoIPAction.pm ↑ Summary MtPolicyd/Plugin/Greylist.pm »
1 package Mail::MtPolicyd::Plugin::SPF;
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 = '2.01'; # VERSION
7 # ABSTRACT: mtpolicyd plugin to apply SPF checks
8  
9  
10 2 extends 'Mail::MtPolicyd::Plugin';
11  
12 2 with 'Mail::MtPolicyd::Plugin::Role::Scoring';
13 2 with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => {
14 'uc_attributes' => [ 'enabled', 'fail_mode', 'softfail_mode', 'pass_mode' ],
15 };
16  
17 2 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Mail::MtPolicyd::AddressList;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Mail::SPF;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use Net::DNS::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 has 'enabled' => ( is => 'rw', isa => 'Str', default => 'on' );
24  
25 2 has 'pass_score' => ( is => 'rw', isa => 'Maybe[Num]' );
26 2 has 'pass_mode' => ( is => 'rw', isa => 'Str', default => 'passive' );
27  
28 2 has 'softfail_score' => ( is => 'rw', isa => 'Maybe[Num]' );
29 2 has 'softfail_mode' => ( is => 'rw', isa => 'Str', default => 'passive' );
30  
31 2 has 'fail_score' => ( is => 'rw', isa => 'Maybe[Num]' );
32 2 has 'fail_mode' => ( is => 'rw', isa => 'Str', default => 'reject' );
33  
34 2 has 'reject_message' => ( is => 'rw', isa => 'Str',
35 default => 'SPF validation failed: %LOCAL_EXPL%' );
36  
37 2 has 'default_authority_explanation' => ( is => 'ro', isa => 'Str',
38 default => 'See http://www.%{d}/why/id=%{S};ip=%{I};r=%{R}' );
39 2 has 'hostname' => ( is => 'ro', isa => 'Str', default => '' );
40  
41 2 has 'whitelist' => ( is => 'rw', isa => 'Str',
42 default => '');
43  
44 has '_whitelist' => ( is => 'ro', isa => 'Mail::MtPolicyd::AddressList',
45 lazy => 1, default => sub {
46 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 my $list = Mail::MtPolicyd::AddressList->new;
48 1 $list->add_localhost;
49 1 $list->add_string( $self->whitelist );
50 1 return $list;
51 },
52 2 );
53  
54 # use a custom resolver to be able to provide a mock in unit tests
55 has '_dns_resolver' => (
56 is => 'ro', isa => 'Net::DNS::Resolver', lazy => 1,
57 1 ▶ default => sub { Net::DNS::Resolver->new; },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 2 );
59  
60 has '_spf' => ( is => 'ro', isa => 'Mail::SPF::Server', lazy => 1,
61 default => sub {
62 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
63 1 return Mail::SPF::Server->new(
64 default_authority_explanation => $self->default_authority_explanation,
65 hostname => $self->hostname,
66 dns_resolver => $self->_dns_resolver,
67 );
68 },
69 2 );
70  
71 2 has 'check_helo' => ( is => 'rw', isa => 'Str', default => 'on');
72  
73 sub run {
74 2 ▶ my ( $self, $r ) = @_;
Subroutine run: called CC=9 SCAR=29.5
Pod documented
75  
76 2 ▶ if( $self->get_uc($r->session, 'enabled') eq 'off' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_uc($r->session, "enabled") eq "off") 0 2 2
77 0 return;
78 }
79  
80 2 ▶ if( ! $r->is_attr_defined('client_address') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->is_attr_defined("client_address")) 0 2 2
81 0 $self->log( $r, 'cant check SPF without client_address');
82 0 return;
83 }
84  
85 2 ▶ if( $self->_whitelist->match_string( $r->attr('client_address') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_whitelist->match_string($r->attr("client_address"))) 0 2 2
86 0 $self->log( $r, 'skipping SPF checks for local or whitelisted ip');
87 0 return;
88 }
89  
90 2 my $sender = $r->attr('sender');
91  
92 2 ▶ if( $r->is_attr_defined('helo_name') && $self->check_helo ne 'off' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->is_attr_defined("helo_name") and $self->check_helo ne "off") 2 0 2
Condition33%1/3
$r->is_attr_defined("helo_name") and $self->check_helo ne "off"
!ll&&!rl&&r
002
MC/DC0%0/2
$r->is_attr_defined("helo_name") and $self->check_helo ne "off"
$r->is_attr_defined("helo_name") $self->check_helo ne "off"
Truth table33%1/3
A and B
A= $r->is_attr_defined("helo_name")B= $self->check_helo ne "off"
ABresult
0X0
100
111
93 2 my $helo_result = $self->_check_helo( $r );
94 2 ▶ if( defined $helo_result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $helo_result) 0 2 2
95 0 return( $helo_result ); # return action if present
96 }
97 2 ▶ if( ! $r->is_attr_defined('sender') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->is_attr_defined("sender")) 0 2 2
98 0 $sender = 'postmaster@'.$r->attr('helo_name');
99 0 $self->log( $r, 'null sender, building sender from HELO: '.$sender );
100 }
101 }
102  
103 2 ▶ if( ! defined $sender ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sender) 0 2 2
104 0 $self->log( $r, 'skipping SPF check because of null sender, consider setting check_helo=on');
105 0 return;
106 }
107  
108 2 return $self->_check_mfrom( $r, $sender );
109 }
110  
111 sub _check_helo {
112 2 ▶ my ( $self, $r ) = @_;
Subroutine _check_helo: called CC=1 SCAR=0.0
113 2 my $ip = $r->attr('client_address');
114 2 my $helo = $r->attr('helo_name');
115 2 my $session = $r->session;
116  
117 2 my $request = Mail::SPF::Request->new(
118 scope => 'helo',
119 identity => $helo,
120 ip_address => $ip,
121 );
122 2 my $result = $self->_spf->process($request);
123  
124 2 return $self->_check_spf_result( $r, $result, 1 );
125 }
126  
127 sub _check_mfrom {
128 2 ▶ my ( $self, $r, $sender ) = @_;
Subroutine _check_mfrom: called CC=3 SCAR=11.6
129 2 my $ip = $r->attr('client_address');
130 2 my $helo = $r->attr('helo_name');
131  
132 2 ▶ my $request = Mail::SPF::Request->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $helo && length $helo ? : 2 0 2
Condition33%1/3
defined $helo and length $helo
!ll&&!rl&&r
002
MC/DC0%0/2
defined $helo and length $helo
defined $helo length $helo
Truth table33%1/3
A and B
A= defined $heloB= length $helo
ABresult
0X0
100
111
133 scope => 'mfrom',
134 identity => $sender,
135 ip_address => $ip,
136 defined $helo && length($helo) ? ( helo_identity => $helo ) : (),
137 );
138 2 my $result = $self->_spf->process($request);
139  
140 2 return $self->_check_spf_result( $r, $result, 0 );
141 }
142  
143 sub _check_spf_result {
144 4 ▶ my ( $self, $r, $result, $no_pass_action ) = @_;
Subroutine _check_spf_result: called CC=18 SCAR=48.2
145 4 my $scope = $result->request->scope;
146 4 my $session = $r->session;
147 4 my $fail_mode = $self->get_uc($session, 'fail_mode');
148 4 my $softfail_mode = $self->get_uc($session, 'softfail_mode');
149 4 my $pass_mode = $self->get_uc($session, 'pass_mode');
150  
151 4 ▶ if( $result->code eq 'neutral') {
Branch: 4 branches
BranchTrueFalseTotal
if ($result->code eq 'neutral') { } 0 4 4
elsif ($result->code eq 'fail') { } 2 2 4
elsif ($result->code eq 'softfail') { } 0 2 2
elsif ($result->code eq 'pass') { } 0 2 2
152 0 $self->log( $r, 'SPF '.$scope.' status neutral. (no SPF records)');
153 0 return;
154 } elsif( $result->code eq 'fail') {
155 2 $self->log( $r, 'SPF '.$scope.' check failed: '.$result->local_explanation);
156 2 ▶ if( defined $self->fail_score && ! $r->is_already_done($self->name.'-score') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->fail_score and not $r->is_already_done($self->name . '-score')) 2 0 2
Condition33%1/3
defined $self->fail_score and not $r->is_already_done($self->name . '-score')
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->fail_score and not $r->is_already_done($self->name . '-score')
defined $self->fail_score not $r->is_already_done($self->name . '-score')
Truth table33%1/3
A and not(B)
A= defined $self->fail_scoreB= not $r->is_already_done($self->name . '-score')
ABresult
0X0
100
111
157 2 $self->add_score( $r, $self->name => $self->fail_score );
158 }
159 2 ▶ if( $fail_mode eq 'reject') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fail_mode eq "reject") 2 0 2
160 2 return Mail::MtPolicyd::Plugin::Result->new(
161 action => $self->_get_reject_action($result),
162 abort => 1,
163 );
164 }
165 0 return;
166 } elsif( $result->code eq 'softfail') {
167 0 $self->log( $r, 'SPF '.$scope.' check returned softfail '.$result->local_explanation);
168 0 ▶ if( defined $self->softfail_score && ! $r->is_already_done($self->name.'-score') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->softfail_score and not $r->is_already_done($self->name . '-score')) 0 0 0
Condition0%0/3
defined $self->softfail_score and not $r->is_already_done($self->name . '-score')
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->softfail_score and not $r->is_already_done($self->name . '-score')
defined $self->softfail_score not $r->is_already_done($self->name . '-score')
Truth table0%0/3
A and not(B)
A= defined $self->softfail_scoreB= not $r->is_already_done($self->name . '-score')
ABresult
0X0
100
111
169 0 $self->add_score( $r, $self->name => $self->softfail_score );
170 }
171 0 ▶ if( $softfail_mode eq 'reject') {
Branch: 2 branches
BranchTrueFalseTotal
if ($softfail_mode eq 'reject') { } 0 0 0
elsif ($softfail_mode eq 'accept' or $softfail_mode eq 'dunno') { } 0 0 0
172 0 return Mail::MtPolicyd::Plugin::Result->new(
173 action => $self->_get_reject_action($result),
174 abort => 1,
175 );
176 ▶ } elsif( $softfail_mode eq 'accept' || $softfail_mode eq 'dunno') {
Condition0%0/3
$softfail_mode eq "accept" or $softfail_mode eq "dunno"
l!l&&r!l&&!r
000
MC/DC0%0/2
$softfail_mode eq "accept" or $softfail_mode eq "dunno"
$softfail_mode eq "accept" $softfail_mode eq "dunno"
Truth table0%0/3
A or B
A= $softfail_mode eq "accept"B= $softfail_mode eq "dunno"
ABresult
1X1
011
000
177 0 return Mail::MtPolicyd::Plugin::Result->new_dunno;
178 }
179 0 return;
180 } elsif( $result->code eq 'pass' ) {
181 0 $self->log( $r, 'SPF '.$scope.' check passed');
182 0 ▶ if( $no_pass_action ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($no_pass_action) 0 0 0
183 0 ▶ if( defined $self->pass_score && ! $r->is_already_done($self->name.'-score') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->pass_score and not $r->is_already_done($self->name . '-score')) 0 0 0
Condition0%0/3
defined $self->pass_score and not $r->is_already_done($self->name . '-score')
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->pass_score and not $r->is_already_done($self->name . '-score')
defined $self->pass_score not $r->is_already_done($self->name . '-score')
Truth table0%0/3
A and not(B)
A= defined $self->pass_scoreB= not $r->is_already_done($self->name . '-score')
ABresult
0X0
100
111
184 0 $self->add_score( $r, $self->name => $self->pass_score );
185 }
186 0 ▶ if( $pass_mode eq 'accept' || $pass_mode eq 'dunno') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass_mode eq "accept" or $pass_mode eq "dunno") 0 0 0
Condition0%0/3
$pass_mode eq "accept" or $pass_mode eq "dunno"
l!l&&r!l&&!r
000
MC/DC0%0/2
$pass_mode eq "accept" or $pass_mode eq "dunno"
$pass_mode eq "accept" $pass_mode eq "dunno"
Truth table0%0/3
A or B
A= $pass_mode eq "accept"B= $pass_mode eq "dunno"
ABresult
1X1
011
000
187 0 return Mail::MtPolicyd::Plugin::Result->new_dunno;
188 }
189 0 return;
190 }
191  
192 2 $self->log( $r, 'spf '.$scope.' check failed: '.$result->local_explanation );
193 2 return;
194 }
195  
196 sub _get_reject_action {
197 2 ▶ my ( $self, $result ) = @_;
Subroutine _get_reject_action: called CC=4 SCAR=17.3
198 2 my $message = $self->reject_message;
199  
200 2 ▶ if( $message =~ /%LOCAL_EXPL%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /%LOCAL_EXPL%/) 2 0 2
201 2 my $expl = $result->local_explanation;
202 2 $message =~ s/%LOCAL_EXPL%/$expl/;
203 }
204 2 ▶ if( $message =~ /%AUTH_EXPL%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /%AUTH_EXPL%/) 0 2 2
205 0 my $expl = '';
206 0 ▶ if( $result->can('authority_explanation') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->can("authority_explanation")) 0 0 0
207 0 $expl = $result->authority_explanation;
208 }
209 0 $message =~ s/%AUTH_EXPL%/$expl/;
210 }
211  
212 2 return('reject '.$message);
213 }
214  
215 2 __PACKAGE__->meta->make_immutable;
216  
217 2 1;
218  
219 __END__
220  
221 =pod
222  
223 =encoding UTF-8
224  
225 =head1 NAME
226  
227 Mail::MtPolicyd::Plugin::SPF - mtpolicyd plugin to apply SPF checks
228  
229 =head1 VERSION
230  
231 version 2.01
232  
233 =head1 DESCRIPTION
234  
235 This plugin applies Sender Policy Framework(SPF) checks.
236  
237 Checks are implemented using the Mail::SPF perl module.
238  
239 Actions based on the SPF result can be applied for:
240  
241 =over
242  
243 =item pass (pass_mode, default: passive)
244  
245 =item softfail (softfail_mode, default: passive)
246  
247 =item fail (fail_mode, default: reject)
248  
249 =back
250  
251 For status 'neutral' no action or score is applied.
252  
253 =head1 PARAMETERS
254  
255 =over
256  
257 =item (uc_)enabled (default: on)
258  
259 Enable/disable the plugin.
260  
261 =item (uc_)pass_mode (default: passive)
262  
263 How to behave if the SPF checks passed successfully:
264  
265 =over
266  
267 =item passive
268  
269 Just apply score. Do not return an action.
270  
271 =item accept, dunno
272  
273 Will return an 'dunno' action.
274  
275 =back
276  
277 =item pass_score (default: empty)
278  
279 Score to apply when the sender has been successfully checked against SPF.
280  
281 =item (uc_)softfail_mode (default: passive)
282  
283 How to behave if the SPF checks returned a softfail status.
284  
285 =over
286  
287 =item passive
288  
289 Just apply score. Do not return an action.
290  
291 =item accept, dunno
292  
293 Will return an 'dunno' action.
294  
295 =item reject
296  
297 Return an reject action.
298  
299 =back
300  
301 =item softfail_score (default: empty)
302  
303 Score to apply when the SPF check returns an softfail status.
304  
305 =item (uc_)fail_mode (default: reject)
306  
307 =over
308  
309 =item reject
310  
311 Return an reject action.
312  
313 =item passive
314  
315 Just apply score and do not return an action.
316  
317 =back
318  
319 =item reject_message (default: )
320  
321 If fail_mode is set to 'reject' this message is used in the reject.
322  
323 The following pattern will be replaced in the string:
324  
325 =over
326  
327 =item %LOCAL_EXPL%
328  
329 Will be replaced with a (local) explanation of the check result.
330  
331 =item %AUTH_EXPL%
332  
333 Will be replaced with a URL to the explanation of the result.
334  
335 This URL could be configured with 'default_authority_explanation'.
336  
337 =back
338  
339 =item fail_score (default: empty)
340  
341 Score to apply if the sender failed the SPF checks.
342  
343 =item default_authority_explanation (default: See http://www.%{d}/why/id=%{S};ip=%{I};r=%{R})
344  
345 String to return as an URL pointing to an explanation of the SPF check result.
346  
347 See Mail::SPF::Server for details.
348  
349 =item hostname (default: empty)
350  
351 An hostname to show in the default_authority_explanation as generating server.
352  
353 =item whitelist (default: '')
354  
355 A comma separated list of IP addresses to skip.
356  
357 =item check_helo (default: "on")
358  
359 Set to 'off' to disable SPF check on helo.
360  
361 =back
362  
363 =head1 EXAMPLE
364  
365 <Plugin spf>
366 module = "SPF"
367 pass_mode = passive
368 pass_score = -10
369 fail_mode = reject
370 #fail_score = 10
371 </Plugin>
372  
373 =head1 SEE ALSO
374  
375 L<Mail::SPF>, OpenSPF L<www.openspf.org/>, RFC 7209 L<https://tools.ietf.org/html/rfc7208>
376  
377 =head1 AUTHOR
378  
379 Markus Benning <ich@markusbenning.de>
380  
381 =head1 COPYRIGHT AND LICENSE
382  
383 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
384  
385 This is free software, licensed under:
386  
387 The GNU General Public License, Version 2, June 1991
388  
389 =cut
« MtPolicyd/Plugin/GeoIPAction.pm ↑ Summary MtPolicyd/Plugin/Greylist.pm »