MtPolicyd/Plugin/SPF.pm

filter: Statementstmt 75.3% 52 / 69 Branchbran 33.3% 8 / 24 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 56.8% 71 / 125 CC 18 SCAR 37.0
CC
18
Cov
58%
run
62.8
_get_reject_action
5.7
BEGIN
1.0
CRAP 40.4
« MtPolicyd.pm ↑ Summary MtPolicyd/Plugin/GeoIPAction.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 = '1.12'; # 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' ],
15 };
16  
17 2 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use Mail::SPF;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 has 'enabled' => ( is => 'rw', isa => 'Str', default => 'on' );
22  
23 2 has 'pass_score' => ( is => 'rw', isa => 'Maybe[Num]' );
24 2 has 'pass_mode' => ( is => 'rw', isa => 'Str', default => 'passive' );
25  
26 2 has 'fail_score' => ( is => 'rw', isa => 'Maybe[Num]' );
27 2 has 'fail_mode' => ( is => 'rw', isa => 'Str', default => 'reject' );
28  
29 2 has 'reject_message' => ( is => 'rw', isa => 'Str',
30 default => 'SPF validation failed: %LOCAL_EXPL%' );
31  
32 2 has 'default_authority_explanation' => ( is => 'ro', isa => 'Str',
33 default => 'See http://www.%{d}/why/id=%{S};ip=%{I};r=%{R}' );
34 2 has 'hostname' => ( is => 'ro', isa => 'Str', default => '' );
35  
36 has '_spf' => ( is => 'ro', isa => 'Mail::SPF::Server', lazy => 1,
37 default => sub {
38 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 return Mail::SPF::Server->new(
40 default_authority_explanation => $self->default_authority_explanation,
41 hostname => $self->hostname,
42 );
43 },
44 2 );
45  
46 sub run {
47 2 ▶ my ( $self, $r ) = @_;
Subroutine run: called CC=15 SCAR=41.4
Pod documented
48 2 my $session = $r->session;
49  
50 2 my $enabled = $self->get_uc($session, 'enabled');
51 2 ▶ if( $enabled eq 'off' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enabled eq "off") 0 2 2
52 0 return;
53 }
54  
55 2 my $ip = $r->attr('client_address');
56 2 my $sender = $r->attr('sender');
57 2 my $helo = $r->attr('helo_name');
58  
59 2 ▶ if( ! defined $ip || ! defined $sender || ! defined $helo ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip and defined $sender and defined $helo) 0 2 2
Condition33%2/6
defined $ip and defined $sender and defined $helo
!ll&&!rl&&r
002
defined $ip and defined $sender
!ll&&!rl&&r
002
MC/DC0%0/3
defined $ip and defined $sender and defined $helo
defined $ip defined $sender defined $helo
Truth table25%1/4
A and B and C
A= defined $ipB= defined $senderC= defined $helo
ABCresult
0XX0
10X0
1100
1111
60 0 $self->logdie('request atttributes client_address, sender, helo_name required!');
61 }
62  
63 2 my $request = Mail::SPF::Request->new(
64 scope => 'mfrom',
65 identity => $sender,
66 ip_address => $ip,
67 helo_identity => $helo,
68 );
69 2 my $result = $self->_spf->process($request);
70  
71 2 ▶ if( $result->code eq 'neutral') {
Branch: 3 branches
BranchTrueFalseTotal
if ($result->code eq 'neutral') { } 0 2 2
elsif ($result->code eq 'fail') { } 2 0 2
elsif ($result->code eq 'pass') { } 0 0 0
72 0 $self->log( $r, 'SPF status neutral. (no SPF records)');
73 0 return;
74 } elsif( $result->code eq 'fail') {
75 2 $self->log( $r, 'SPF check failed: '.$result->local_explanation);
76 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
77 2 $self->add_score( $r, $self->name => $self->fail_score );
78 }
79 2 ▶ if( $self->fail_mode eq 'reject') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fail_mode eq "reject") 2 0 2
80 2 return Mail::MtPolicyd::Plugin::Result->new(
81 action => $self->_get_reject_action($result),
82 abort => 1,
83 );
84 }
85 0 return;
86 } elsif( $result->code eq 'pass' ) {
87 0 $self->log( $r, 'SPF check passed');
88 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
89 0 $self->add_score( $r, $self->name => $self->pass_score );
90 }
91 0 ▶ if( $self->pass_mode eq 'accept' || $self->pass_mode eq 'dunno') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pass_mode eq "accept" or $self->pass_mode eq "dunno") 0 0 0
Condition0%0/3
$self->pass_mode eq "accept" or $self->pass_mode eq "dunno"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->pass_mode eq "accept" or $self->pass_mode eq "dunno"
$self->pass_mode eq "accept" $self->pass_mode eq "dunno"
Truth table0%0/3
A or B
A= $self->pass_mode eq "accept"B= $self->pass_mode eq "dunno"
ABresult
1X1
011
000
92 0 return Mail::MtPolicyd::Plugin::Result->new_dunno;
93 }
94 0 return;
95 }
96  
97 0 $self->log( $r, 'spf check failed: '.$result->local_explanation );
98 0 return;
99 }
100  
101 sub _get_reject_action {
102 2 ▶ my ( $self, $result ) = @_;
Subroutine _get_reject_action: called CC=4 SCAR=17.3
103 2 my $message = $self->reject_message;
104  
105 2 ▶ if( $message =~ /%LOCAL_EXPL%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /%LOCAL_EXPL%/) 2 0 2
106 2 my $expl = $result->local_explanation;
107 2 $message =~ s/%LOCAL_EXPL%/$expl/;
108 }
109 2 ▶ if( $message =~ /%AUTH_EXPL%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /%AUTH_EXPL%/) 0 2 2
110 0 my $expl = '';
111 0 ▶ if( $result->can('authority_explanation') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->can("authority_explanation")) 0 0 0
112 0 $expl = $result->authority_explanation;
113 }
114 0 $message =~ s/%AUTH_EXPL%/$expl/;
115 }
116  
117 2 return('reject '.$message);
118 }
119  
120 2 __PACKAGE__->meta->make_immutable;
121  
122 2 1;
123  
124  
125 __END__
126 =pod
127  
128 =head1 NAME
129  
130 Mail::MtPolicyd::Plugin::SPF - mtpolicyd plugin to apply SPF checks
131  
132 =head1 VERSION
133  
134 version 1.12
135  
136 =head1 DESCRIPTION
137  
138 This plugin applies Sender Policy Framework(SPF) checks.
139  
140 Checks are implemented using the Mail::SPF perl module.
141  
142 =head1 PARAMETERS
143  
144 =over
145  
146 =item (uc_)enabled (default: on)
147  
148 Enable/disable the plugin.
149  
150 =item pass_mode (default: passive)
151  
152 How to behave if the SPF checks passed successfully:
153  
154 =over
155  
156 =item passive
157  
158 Just apply score. Do not return an action.
159  
160 =item accept, dunno
161  
162 Will return an 'dunno' action.
163  
164 =back
165  
166 =item pass_score (default: empty)
167  
168 Score to apply when the sender has been successfully checked against SPF.
169  
170 =item fail_mode (default: reject)
171  
172 =over
173  
174 =item reject
175  
176 Return an reject action.
177  
178 =item passive
179  
180 Just apply score and do not return an action.
181  
182 =back
183  
184 =item reject_message (default: )
185  
186 If fail_mode is set to 'reject' this message is used in the reject.
187  
188 The following pattern will be replaced in the string:
189  
190 =over
191  
192 =item %LOCAL_EXPL%
193  
194 Will be replaced with a (local) explanation of the check result.
195  
196 =item %AUTH_EXPL%
197  
198 Will be replaced with a URL to the explanation of the result.
199  
200 This URL could be configured with 'default_authority_explanation'.
201  
202 =back
203  
204 =item fail_score (default: empty)
205  
206 Score to apply if the sender failed the SPF checks.
207  
208 =item default_authority_explanation (default: See http://www.%{d}/why/id=%{S};ip=%{I};r=%{R})
209  
210 String to return as an URL pointing to an explanation of the SPF check result.
211  
212 See Mail::SPF::Server for details.
213  
214 =item hostname (default: empty)
215  
216 An hostname to show in the default_authority_explanation as generating server.
217  
218 =back
219  
220 =head1 EXAMPLE
221  
222 <Plugin spf>
223 module = "SPF"
224 pass_mode = passive
225 pass_score = -10
226 fail_mode = reject
227 #fail_score = 10
228 </Plugin>
229  
230 =head1 AUTHOR
231  
232 Markus Benning <ich@markusbenning.de>
233  
234 =head1 COPYRIGHT AND LICENSE
235  
236 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
237  
238 This is free software, licensed under:
239  
240 The GNU General Public License, Version 2, June 1991
241  
242 =cut
243  
« MtPolicyd.pm ↑ Summary MtPolicyd/Plugin/GeoIPAction.pm »