Authentication/Handler/DMARC.pm

filter: Statementstmt 77.8% 653 / 839 Branchbran 62.0% 253 / 408 Conditioncond 63.2% 67 / 106 MC/DCmcdc 43.3% 26 / 60 Subroutinesub 100% 43 / 43 Podpod 9.0% 2 / 22 totaltotal 70.6% 1044 / 1478 CC 267 SCAR 47.7
CC
267
Cov
72%
_process_dmarc_for
296.6
is_whitelisted
169.2
dequeue_callback
83.9
CRAP 118.1
« Authentication/Resolver.pm ↑ Summary Authentication.pm »
1 package Mail::Milter::Authentication::Handler::DMARC;
2 42 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 42 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Handler class for DMARC
7 42 our $VERSION = '4.20261116'; # VERSION
8 42 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 42 ▶use List::MoreUtils qw{ uniq };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 42 ▶use List::Util qw{ min };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 42 ▶use Email::Address::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 42 ▶use Mail::DMARC::PurePerl 1.20160612;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 42 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 42 my $PSL_CHECKED_TIME;
16  
17 1 ▶sub default_config {
Subroutine default_config: called CC=1 SCAR=0.0
Pod undocumented
18 return {
19 1 'hide_none' => 0,
20 'use_arc' => 1,
21 'hard_reject' => 0,
22 'no_list_reject' => 1,
23 'arc_before_list' => 0,
24 'whitelisted' => [],
25 'policy_rbl_lookup' => {},
26 'detect_list_id' => 1,
27 'report_skip_to' => [ 'my_report_from_address@example.com' ],
28 'report_suppression_list' => 'rbl.example.com',
29 'report_suppression_email_list' => 'rbl.example.com',
30 'no_report' => 0,
31 'hide_report_to' => 0,
32 'config_file' => '/etc/mail-dmarc.ini',
33 'no_reject_disposition' => 'quarantine',
34 'no_list_reject_disposition' => 'none',
35 'reject_on_multifrom' => 30,
36 'quarantine_on_multifrom' => 20,
37 'skip_on_multifrom' => 10,
38 'strict_multifrom' => 0,
39 };
40 }
41  
42 1 ▶sub grafana_rows {
Subroutine grafana_rows: called CC=1 SCAR=0.0
Pod undocumented
43 1 my ( $self ) = @_;
44 1 my @rows;
45 1 push @rows, $self->get_json( 'DMARC_metrics' );
46 1 return \@rows;
47 }
48  
49 270 ▶sub is_whitelisted {
Subroutine is_whitelisted: called CC=26 SCAR=51.3
Pod undocumented
50 270 my ( $self ) = @_;
51 270 my $config = $self->handler_config();
52 270 ▶ return 0 if not exists( $config->{'whitelisted'} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{"whitelisted"} 262 8 270
53 8 my $top_handler = $self->get_top_handler();
54 8 my $ip_obj = $top_handler->{'ip_object'};
55 8 my $whitelisted = 0;
56 8 foreach my $entry ( @{ $config->{'whitelisted'} } ) {
57 # This does not consider dkim/spf results added by a passing arc chain
58 # we consider this out of scope at this point.
59 10 ▶ if ( $entry =~ /^dnswl:/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($entry =~ /^dnswl:/u) { } 0 10 10
elsif ($entry =~ /^dkim:/u) { } 2 8 10
elsif ($entry =~ /^spf:/u) { } 0 8 8
60 0 my ( $dummy, $type, $rbl ) = split( /:/, $entry, 3 );
61 0 ▶ if ( $type eq 'spf' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'spf') { } 0 0 0
elsif ($type eq 'dkim') { } 0 0 0
elsif ($type eq 'ip') { } 0 0 0
62 0 eval {
63 0 my $spf = $self->get_handler('SPF');
64 0 ▶ if ( $spf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf) 0 0 0
65 0 my $got_spf_result = $spf->{'dmarc_result'};
66 0 ▶ if ( $got_spf_result eq 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_spf_result eq "pass") 0 0 0
67 0 my $got_spf_domain = $spf->{'dmarc_domain'};
68 0 ▶ if ( $self->rbl_check_domain( $got_spf_domain, $rbl ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rbl_check_domain($got_spf_domain, $rbl)) 0 0 0
69 0 $self->dbgout( 'DMARCReject', "Whitelist hit " . $entry, LOG_DEBUG );
70 0 $whitelisted = 1;
71 }
72 }
73 }
74 };
75 0 $self->handle_exception( $@ );
76 }
77 elsif ( $type eq 'dkim' ) {
78 0 my $dkim_handler = $self->get_handler('DKIM');
79 0 foreach my $dkim_domain( sort keys %{ $dkim_handler->{'valid_domains'}} ) {
80 0 ▶ if ( $self->rbl_check_domain( $dkim_domain, $rbl ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rbl_check_domain($dkim_domain, $rbl)) 0 0 0
81 0 $self->dbgout( 'DMARCReject', "Whitelist hit " . $entry, LOG_DEBUG );
82 0 $whitelisted = 1;
83 }
84 }
85 }
86 elsif ( $type eq 'ip' ) {
87 0 ▶ if ( $self->rbl_check_ip( $ip_obj, $rbl ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rbl_check_ip($ip_obj, $rbl)) 0 0 0
88 0 $self->dbgout( 'DMARCReject', "Whitelist hit " . $entry, LOG_DEBUG );
89 0 $whitelisted = 1;
90 }
91 }
92 }
93 elsif ( $entry =~ /^dkim:/ ) {
94 2 my ( $dummy, $dkim_domain ) = split( /:/, $entry, 2 );
95 2 my $dkim_handler = $self->get_handler('DKIM');
96 2 ▶ if ( exists( $dkim_handler->{'valid_domains'}->{ lc $dkim_domain } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dkim_handler->{"valid_domains"}{lc $dkim_domain}) 2 0 2
97 2 $self->dbgout( 'DMARCReject', "Whitelist hit " . $entry, LOG_DEBUG );
98 2 $whitelisted = 1;
99 }
100 }
101 elsif ( $entry =~ /^spf:/ ) {
102 0 my ( $dummy, $spf_domain ) = split( /:/, $entry, 2 );
103 0 eval {
104 0 my $spf = $self->get_handler('SPF');
105 0 ▶ if ( $spf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf) 0 0 0
106 0 my $got_spf_result = $spf->{'dmarc_result'};
107 0 ▶ if ( $got_spf_result eq 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_spf_result eq "pass") 0 0 0
108 0 my $got_spf_domain = $spf->{'dmarc_domain'};
109 0 ▶ if ( lc $got_spf_domain eq lc $spf_domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $got_spf_domain eq lc $spf_domain) 0 0 0
110 0 $self->dbgout( 'DMARCReject', "Whitelist hit " . $entry, LOG_DEBUG );
111 0 $whitelisted = 1;
112 }
113 }
114 }
115 };
116 0 $self->handle_exception( $@ );
117 }
118 else {
119 8 my $whitelisted_obj = Net::IP->new($entry);
120 8 ▶ if ( !$whitelisted_obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $whitelisted_obj) { } 0 8 8
121 0 $self->log_error( 'DMARC: Could not parse whitelist IP '.$entry );
122 }
123 else {
124 8 ▶ my $is_overlap = $ip_obj->overlaps($whitelisted_obj) || 0;
Condition100%2/2
$ip_obj->overlaps($whitelisted_obj) || 0
l!l
26
MC/DC100%1/1
$ip_obj->overlaps($whitelisted_obj) || 0
$ip_obj->overlaps($whitelisted_obj)
Truth table100%2/2
A
A= $ip_obj->overlaps($whitelisted_obj)
Aresult
11
00
125 8 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_A_IN_B_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_B_IN_A_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_PARTIAL_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_IDENTICAL) 2 6 8
Condition66%6/9
$is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_A_IN_B_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_B_IN_A_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_PARTIAL_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_IDENTICAL
l!l&&r!l&&!r
206
$is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_A_IN_B_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_B_IN_A_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_PARTIAL_OVERLAP
l!l&&r!l&&!r
206
$is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_A_IN_B_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_B_IN_A_OVERLAP
l!l&&r!l&&!r
206
MC/DC25%1/4
$is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_A_IN_B_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_B_IN_A_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_PARTIAL_OVERLAP or $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_IDENTICAL
$is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_A_IN_B_OVERLAP $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_B_IN_A_OVERLAP $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_PARTIAL_OVERLAP $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_IDENTICAL
Truth table40%2/5
A or B or C or D
A= $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_A_IN_B_OVERLAPB= $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_B_IN_A_OVERLAPC= $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_PARTIAL_OVERLAPD= $is_overlap == $Mail::Milter::Authentication::Handler::DMARC::IP_IDENTICAL
ABCDresult
1XXX1
01XX1
001X1
00011
00000
126 $is_overlap == $IP_A_IN_B_OVERLAP
127 || $is_overlap == $IP_B_IN_A_OVERLAP # Should never happen
128 || $is_overlap == $IP_PARTIAL_OVERLAP # Should never happen
129 || $is_overlap == $IP_IDENTICAL
130 )
131 {
132 2 $self->dbgout( 'DMARCReject', "Whitelist hit " . $entry, LOG_DEBUG );
133 2 $whitelisted = 1;
134 }
135 }
136 }
137 10 ▶ return $whitelisted if $whitelisted;
Branch: 1 branch
BranchTrueFalseTotal
if $whitelisted 4 6 10
138 }
139 4 return $whitelisted;
140 }
141  
142 64 ▶sub pre_loop_setup {
Subroutine pre_loop_setup: called CC=4 SCAR=14.0
Pod undocumented
143 64 my ( $self ) = @_;
144 64 $PSL_CHECKED_TIME = time;
145 64 my $dmarc = Mail::DMARC::PurePerl->new();
146 64 my $config = $self->handler_config();
147 64 ▶ if ( exists ( $config->{ 'config_file' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"config_file"}) 1 63 64
148 1 ▶ $self->log_error( 'DMARC config file does not exist' ) if ! -e $config->{ 'config_file' };
Branch: 1 branch
BranchTrueFalseTotal
unless -e $config->{"config_file"} 0 1 1
149 1 $dmarc->config( $config->{ 'config_file' } );
150 }
151 64 my $psl = eval { $dmarc->get_public_suffix_list(); };
152 64 $self->handle_exception( $@ );
153 64 ▶ if ( $psl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psl) { } 64 0 64
154 64 $self->{'thischild'}->loginfo( 'DMARC Preloaded PSL' );
155 }
156 else {
157 0 $self->{'thischild'}->logerror( 'DMARC Could not preload PSL' );
158 }
159 }
160  
161 31 ▶sub pre_fork_setup {
Subroutine pre_fork_setup: called CC=6 SCAR=27.4
Pod undocumented
162 31 my ( $self ) = @_;
163 31 my $now = time;
164 31 my $dmarc = Mail::DMARC::PurePerl->new();
165 31 my $config = $self->handler_config();
166 31 ▶ if ( exists ( $config->{ 'config_file' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"config_file"}) 0 31 31
167 0 ▶ $self->log_error( 'DMARC config file does not exist' ) if ! -e $config->{ 'config_file' };
Branch: 1 branch
BranchTrueFalseTotal
unless -e $config->{"config_file"} 0 0 0
168 0 $dmarc->config( $config->{ 'config_file' } );
169 }
170 31 my $check_time = 60*10; # Check no more often than every 10 minutes
171 31 ▶ if ( $now > $PSL_CHECKED_TIME + $check_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now > $PSL_CHECKED_TIME + $check_time) 0 31 31
172 0 $PSL_CHECKED_TIME = $now;
173 0 ▶ if ( $dmarc->can( 'check_public_suffix_list' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc->can('check_public_suffix_list')) { } 0 0 0
174 0 ▶ if ( $dmarc->check_public_suffix_list() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc->check_public_suffix_list) { } 0 0 0
175 0 $self->{'thischild'}->loginfo( 'DMARC PSL file has changed and has been reloaded' );
176 }
177 else {
178 0 $self->{'thischild'}->loginfo( 'DMARC PSL file has not changed since last loaded' );
179 }
180 }
181 else {
182 0 $self->{'thischild'}->loginfo( 'DMARC PSL file update checking not available' );
183 }
184 }
185 }
186  
187 64 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
188 return {
189 64 'dmarc_total' => 'The number of emails processed for DMARC',
190 'dmarc_reports_total' => { type => 'gauge', help => 'The number of pending DMARC reports' },
191 };
192 }
193  
194 10 ▶sub metrics_callback {
Subroutine metrics_callback: called CC=2 SCAR=8.3
Pod undocumented
195 10 my ( $self ) = @_;
196 10 my $config = $self->handler_config();
197 10 ▶ return if $config->{'no_report'};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"no_report"} 0 10 10
198  
199 10 eval {
200 10 my $time = time;
201 10 my $backend = Mail::DMARC::Report::Store->new()->backend;
202 0 my $current = $backend->query("SELECT COUNT(1) AS c FROM report WHERE end >= $time")->[0]->{c};
203 0 my $pending = $backend->query("SELECT COUNT(1) AS c FROM report WHERE end < $time")->[0]->{c};
204 0 $self->metric_set( 'dmarc_reports_total', { 'state' => 'current' }, $current );
205 0 $self->metric_set( 'dmarc_reports_total', { 'state' => 'pending' }, $pending );
206 };
207 }
208  
209 2 ▶sub _process_arc_dmarc_for {
Subroutine _process_arc_dmarc_for: called CC=16 SCAR=33.7
210 2 my ( $self, $env_domain_from, $header_domain ) = @_;
211  
212 2 my $config = $self->handler_config();
213 2 my $original_dmarc = $self->get_object('dmarc');
214 2 my $dmarc = $self->new_dmarc_object();
215 2 $dmarc->source_ip( $self->ip_address() );
216  
217 # Set the DMARC Envelope From Domain
218 2 ▶ if ( $env_domain_from ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_domain_from ne "") 2 0 2
219 2 eval {
220 2 $dmarc->envelope_from( $env_domain_from );
221 };
222 2 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 2 2
223 0 $self->handle_exception( $error );
224 0 $self->set_object('dmarc', $original_dmarc,1 ); # Restore original saved DMARC object
225 0 return;
226 }
227 }
228  
229 # Add the Envelope To
230 2 ▶ unless ( $config->{'hide_report_to'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_report_to"}) 2 0 2
231 2 eval {
232 2 $dmarc->envelope_to( lc $self->get_domain_from( $self->{'env_to'} ) );
233 };
234 2 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 2 2
235 0 $self->handle_exception( $error );
236 }
237 }
238  
239 # Add the From Header
240 2 eval { $dmarc->header_from( $header_domain ) };
241 2 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 2 2
242 0 $self->handle_exception( $error );
243 0 $self->set_object('dmarc', $original_dmarc,1 ); # Restore original saved DMARC object
244 0 return;
245 }
246  
247 # Add the SPF Results Object
248 2 eval {
249 2 my $spf = $self->get_handler('SPF');
250 2 ▶ if ( $spf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf) { } 2 0 2
251  
252 2 ▶ if ( $spf->{'spfu_detected' } ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($spf->{'spfu_detected'}) { } 0 2 2
elsif ($spf->{'dmarc_result'} eq 'pass' and lc $spf->{'dmarc_domain'} eq lc $header_domain) { } 0 2 2
elsif (my $arc_spf = $self->get_handler('ARC')->get_trusted_spf_results) { } 1 1 2
253 # We detected a possible SPF upgrade, do not trust any SPF results for re-evaluation
254 0 $dmarc->{'spf'} = [];
255 }
256 ▶ elsif ( $spf->{'dmarc_result'} eq 'pass' && lc $spf->{'dmarc_domain'} eq lc $header_domain ) {
Condition33%1/3
$spf->{"dmarc_result"} eq "pass" and lc $spf->{"dmarc_domain"} eq lc $header_domain
!ll&&!rl&&r
200
MC/DC0%0/2
$spf->{"dmarc_result"} eq "pass" and lc $spf->{"dmarc_domain"} eq lc $header_domain
$spf->{"dmarc_result"} eq "pass" lc $spf->{"dmarc_domain"} eq lc $header_domain
Truth table33%1/3
A and B
A= $spf->{"dmarc_result"} eq "pass"B= lc $spf->{"dmarc_domain"} eq lc $header_domain
ABresult
0X0
100
111
257 # Have a matching local entry, use it.
258 ## TODO take org domains into consideration here
259 $dmarc->spf(
260 'domain' => $spf->{'dmarc_domain'},
261 'scope' => $spf->{'dmarc_scope'},
262 0 'result' => $spf->{'dmarc_result'},
263 );
264 }
265 elsif ( my $arc_spf = $self->get_handler('ARC')->get_trusted_spf_results() ) {
266 # Pull from ARC if we can
267 push @$arc_spf, {
268 'domain' => $spf->{'dmarc_domain'},
269 'scope' => $spf->{'dmarc_scope'},
270 1 'result' => $spf->{'dmarc_result'},
271 };
272 1 $dmarc->spf( $arc_spf );
273 }
274 else {
275 # Nothing else matched, use the local entry anyway
276 $dmarc->spf(
277 'domain' => $spf->{'dmarc_domain'},
278 'scope' => $spf->{'dmarc_scope'},
279 1 'result' => $spf->{'dmarc_result'},
280 );
281 }
282  
283 }
284 else {
285 0 $dmarc->{'spf'} = [];
286 }
287 };
288 2 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 2 2
289 0 $self->handle_exception( $error );
290 0 $dmarc->{'spf'} = [];
291 }
292  
293 # Add the DKIM Results
294 2 my $dkim_handler = $self->get_handler('DKIM');
295 2 my @dkim_values;
296 2 my $arc_values = $self->get_handler('ARC')->get_trusted_dkim_results();
297 2 ▶ if ( $arc_values ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arc_values) 1 1 2
298 1 foreach my $arc_value ( @$arc_values ) {
299 1 push @dkim_values, $arc_value;
300 }
301 }
302 2 $dmarc->{'dkim'} = \@dkim_values;
303 # Add the local DKIM object is it exists
304 2 ▶ if ( $dkim_handler->{'has_dkim'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_handler->{"has_dkim"}) 0 2 2
305 0 my $dkim_object = $self->get_object('dkim');
306 0 ▶ if ( $dkim_object ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_object) 0 0 0
307 0 $dmarc->dkim( $dkim_object );
308 }
309 }
310  
311 # Run the Validator
312 2 my $dmarc_result = $dmarc->validate();
313 2 return $dmarc_result;
314 }
315  
316 281 ▶sub _process_dmarc_for {
Subroutine _process_dmarc_for: called CC=96 SCAR=56.9
317 281 my ( $self, $env_domain_from, $header_domain ) = @_;
318  
319 281 my $config = $self->handler_config();
320  
321 281 ▶ if ( exists $self->{'processed'}->{ "$env_domain_from $header_domain" } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"processed"}{"$env_domain_from $header_domain"}) 0 281 281
322 0 $self->log_error( "DMARC already processed for $env_domain_from $header_domain" );
323 0 return;
324 }
325 281 $self->{'processed'}->{ "$env_domain_from $header_domain" } = 1;
326  
327 # Get a fresh DMARC object each time.
328 281 $self->destroy_object('dmarc');
329 281 my $dmarc = $self->get_dmarc_object();
330 281 $dmarc->source_ip( $self->ip_address() );
331  
332 # Set the DMARC Envelope From Domain
333 281 ▶ if ( $env_domain_from ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_domain_from ne "") 272 9 281
334 272 eval {
335 272 $dmarc->envelope_from( $env_domain_from );
336 };
337 272 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 272 272
338 0 $self->handle_exception( $error );
339 0 $self->log_error( 'DMARC Mail From Error for <' . $env_domain_from . '> ' . $error );
340 }
341 }
342  
343 # Add the Envelope To
344 281 ▶ unless ( $config->{'hide_report_to'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_report_to"}) 281 0 281
345 281 eval {
346 281 $dmarc->envelope_to( lc $self->get_domain_from( $self->{'env_to'} ) );
347 };
348 281 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 281 281
349 0 $self->handle_exception( $error );
350 0 $self->log_error( 'DMARC Rcpt To Error ' . $error );
351 }
352 }
353  
354 # Add the From Header
355 281 eval { $dmarc->header_from( $header_domain ) };
356 281 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 11 270 281
357 11 $self->handle_exception( $error );
358 11 $self->log_error( 'DMARC Header From Error ' . $error );
359 11 $self->metric_count( 'dmarc_total', { 'result' => 'permerror' } );
360 11 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dmarc' )->safe_set_value( 'permerror' );
361 11 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'from header invalid' ) );
362 11 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.from' )->safe_set_value( $header_domain ) );
363 11 $self->_add_dmarc_header( $header );
364 11 return;
365 }
366  
367 270 my $have_arc = ( $self->is_handler_loaded( 'ARC' ) );
368 270 ▶ if ( $have_arc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_arc) 11 259 270
369 # Does our ARC handler have the necessary methods?
370 11 ▶ $have_arc = 0 unless $self->get_handler('ARC')->can( 'get_trusted_arc_authentication_results' );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_handler("ARC")->can("get_trusted_arc_authentication_results") 0 11 11
371 }
372 270 ▶ $have_arc = 0 if ! $config->{ 'use_arc' };
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"use_arc"} 260 10 270
373  
374 # Add the SPF Results Object
375 270 eval {
376 270 my $spf = $self->get_handler('SPF');
377 270 ▶ if ( $spf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf) { } 270 0 270
378 $dmarc->spf(
379 'domain' => $spf->{'dmarc_domain'},
380 'scope' => $spf->{'dmarc_scope'},
381 270 'result' => $spf->{'dmarc_result'},
382 );
383 }
384 else {
385 0 $dmarc->{'spf'} = [];
386 }
387 };
388 270 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 270 270
389 0 $self->handle_exception( $error );
390 0 $self->log_error( 'DMARC SPF Error: ' . $error );
391 0 $dmarc->{'spf'} = [];
392 }
393  
394 # Add the DKIM Results
395 270 my $dkim_handler = $self->get_handler('DKIM');
396 270 ▶ if ( $dkim_handler->{'failmode'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dkim_handler->{'failmode'}) { } 0 270 270
elsif ($dkim_handler->{'has_dkim'}) { } 43 227 270
397 0 $dmarc->{'dkim'} = [];
398 }
399 elsif ( $dkim_handler->{'has_dkim'} ) {
400 43 my $dkim_object = $self->get_object('dkim');
401 43 ▶ if ( $dkim_object ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_object) { } 43 0 43
402 43 $dmarc->dkim( $dkim_object );
403 }
404 else {
405 0 $dmarc->{'dkim'} = [];
406 }
407 }
408 else {
409 227 $dmarc->{'dkim'} = [];
410 }
411  
412 # Run the Validator
413 270 my $dmarc_result = $dmarc->validate();
414 270 my $is_subdomain = $dmarc->is_subdomain();
415  
416 270 $self->set_object('dmarc_result', $dmarc_result, 1 );
417 270 my $dmarc_results = $self->get_object('dmarc_results');
418 270 ▶ $dmarc_results = [] if ! $dmarc_results;
Branch: 1 branch
BranchTrueFalseTotal
unless $dmarc_results 150 120 270
419 270 push @$dmarc_results, $dmarc_result;
420 270 $self->set_object('dmarc_results',$dmarc_results,1);
421  
422 270 my $dmarc_code = $dmarc_result->result;
423 270 $self->dbgout( 'DMARCCode', $dmarc_code, LOG_DEBUG );
424  
425 # Work around Mail::DMARC::PurePerl returning 'none' instead of 'permerror'
426 # when multiple DMARC records are found (RFC 7489 Section 6.6.3)
427 270 ▶ if ( my $reasons = $dmarc_result->reason ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reasons = $dmarc_result->reason) 270 0 270
428 270 for my $reason ( @$reasons ) {
429 174 ▶ if ( ( $reason->comment // '' ) eq 'too many policies' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($reason->comment // "") eq "too many policies") 0 174 174
Condition50%1/2
$reason->comment // ""
l!l
1740
MC/DC0%0/1
$reason->comment // ""
$reason->comment
Truth table50%1/2
A
A= $reason->comment
Aresult
11
00
430 0 $self->log_error( 'DMARC permerror: multiple DMARC records found for ' . $header_domain );
431 0 $self->metric_count( 'dmarc_total', { 'result' => 'permerror' } );
432 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dmarc' )->safe_set_value( 'permerror' );
433 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'multiple DMARC records' ) );
434 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.from' )->safe_set_value( $header_domain ) );
435 0 $self->_add_dmarc_header( $header );
436 0 return;
437 }
438 }
439 }
440  
441 270 my $dmarc_disposition = eval { $dmarc_result->disposition() };
442 270 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 270 270
443 0 $self->handle_exception( $error );
444 0 ▶ if ( $dmarc_code ne 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_code ne "pass") 0 0 0
445 0 $self->log_error( 'DMARCPolicyError ' . $error );
446 }
447 }
448 270 $self->dbgout( 'DMARCDisposition', $dmarc_disposition, LOG_DEBUG );
449 270 my $dmarc_disposition_evaluated = $dmarc_disposition;
450  
451 270 ▶ $self->dbgout( 'DMARCSubdomain', $is_subdomain ? 'yes' : 'no', LOG_DEBUG );
Branch: 1 branch
BranchTrueFalseTotal
$is_subdomain ? : 137 133 270
452  
453 270 my $dmarc_policy = eval{ $dmarc_result->published()->p(); };
454 270 $self->handle_exception( $@ );
455 # If we didn't get a result, set to none.
456 270 ▶ $dmarc_policy = 'none' if ! $dmarc_policy;
Branch: 1 branch
BranchTrueFalseTotal
unless $dmarc_policy 174 96 270
457 270 my $dmarc_sub_policy = eval{ $dmarc_result->published()->sp(); };
458 270 $self->handle_exception( $@ );
459 # If we didn't get a result, set to none.
460 270 ▶ $dmarc_sub_policy = 'default' if ! $dmarc_sub_policy;
Branch: 1 branch
BranchTrueFalseTotal
unless $dmarc_sub_policy 270 0 270
461 270 $self->dbgout( 'DMARCPolicy', "$dmarc_policy $dmarc_sub_policy", LOG_DEBUG );
462  
463 270 my $policy_override;
464  
465 270 my $arc_aware_result = '';
466 # Re-evaluate non passes taking ARC into account if possible.
467 270 ▶ if ( $have_arc && $dmarc_code eq 'fail' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_arc and $dmarc_code eq "fail") 2 268 270
Condition100%3/3
$have_arc and $dmarc_code eq "fail"
!ll&&!rl&&r
26082
MC/DC100%2/2
$have_arc and $dmarc_code eq "fail"
$have_arc $dmarc_code eq "fail"
Truth table100%3/3
A and B
A= $have_arcB= $dmarc_code eq "fail"
ABresult
0X0
100
111
468 2 my $arc_result = $self->_process_arc_dmarc_for( $env_domain_from, $header_domain );
469 2 $arc_aware_result = eval{$arc_result->result};
470 2 $self->handle_exception( $@ );
471 2 ▶ $arc_aware_result = '' if not defined $arc_aware_result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arc_aware_result 0 2 2
472 }
473  
474 270 my $have_arc_dmarc_pass = 0;
475 270 ▶ if ( $have_arc && $dmarc_code eq 'fail' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_arc and $dmarc_code eq "fail") 2 268 270
Condition100%3/3
$have_arc and $dmarc_code eq "fail"
!ll&&!rl&&r
26082
MC/DC100%2/2
$have_arc and $dmarc_code eq "fail"
$have_arc $dmarc_code eq "fail"
Truth table100%3/3
A and B
A= $have_arcB= $dmarc_code eq "fail"
ABresult
0X0
100
111
476 2 ▶ if (my $arc_dmarc_results = $self->get_handler('ARC')->get_trusted_dmarc_results() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arc_dmarc_results = $self->get_handler("ARC")->get_trusted_dmarc_results) 1 1 2
477 1 for my $arc_dmarc_result ($arc_dmarc_results->@*) {
478 0 ▶ next unless $arc_dmarc_result->{result} eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
unless $arc_dmarc_result->{"result"} eq "pass" 0 0 0
479 0 $have_arc_dmarc_pass = 1;
480 }
481 }
482 }
483  
484 # Re-evaluate in the case of detected SPF upgrade
485 270 my $spfu_mitigation_triggered = 0;
486 270 my $spfu_mitigation = 0;
487 270 eval {
488 270 my $spf = $self->get_handler('SPF');
489 270 ▶ if ( $spf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf) 270 0 270
490 270 $spfu_mitigation = $spf->{'dmarc_spfu_downgrade'};
491 }
492 };
493 270 $self->handle_exception( $@ );
494 270 ▶ if ( $dmarc_code eq 'pass' && $spfu_mitigation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_code eq "pass" and $spfu_mitigation) 6 264 270
Condition100%3/3
$dmarc_code eq "pass" and $spfu_mitigation
!ll&&!rl&&r
214506
MC/DC100%2/2
$dmarc_code eq "pass" and $spfu_mitigation
$dmarc_code eq "pass" $spfu_mitigation
Truth table100%3/3
A and B
A= $dmarc_code eq "pass"B= $spfu_mitigation
ABresult
0X0
100
111
495 # We have a pass, and also detected a possible spfu attack
496 # and we are configured to mitigate such attacks.
497  
498 # save the original dmarc object so we can reinstate it when done
499 6 my $original_dmarc = $self->get_object('dmarc');
500 6 my $spf_dmarc = $self->new_dmarc_object();
501 6 $spf_dmarc->source_ip( $self->ip_address() );
502 6 ▶ if ( $env_domain_from ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_domain_from ne "") 6 0 6
503 6 eval { $spf_dmarc->envelope_from( $env_domain_from ) };
504 6 $self->handle_exception( $@ );
505 }
506 6 eval { $spf_dmarc->header_from( $header_domain ) };
507 6 $self->handle_exception( $@ );
508 6 $spf_dmarc->{'spf'} = [];
509 6 my $dkim_handler = $self->get_handler('DKIM');
510 6 ▶ if ( $dkim_handler->{'failmode'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dkim_handler->{'failmode'}) { } 0 6 6
elsif ($dkim_handler->{'has_dkim'}) { } 0 6 6
511 0 $spf_dmarc->{'dkim'} = [];
512 } elsif ( $dkim_handler->{'has_dkim'} ) {
513 0 my $dkim_object = $self->get_object('dkim');
514 0 ▶ if ( $dkim_object ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_object) { } 0 0 0
515 0 $spf_dmarc->dkim( $dkim_object );
516 } else {
517 0 $spf_dmarc->{'dkim'} = [];
518 }
519 } else {
520 6 $spf_dmarc->{'dkim'} = [];
521 }
522 6 my $spfu_result = $spf_dmarc->validate;
523 6 ▶ if ( $spfu_result->result ne 'pass' && $dmarc_disposition ne $spfu_result->disposition ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spfu_result->result ne "pass" and $dmarc_disposition ne $spfu_result->disposition) 6 0 6
Condition33%1/3
$spfu_result->result ne "pass" and $dmarc_disposition ne $spfu_result->disposition
!ll&&!rl&&r
006
MC/DC0%0/2
$spfu_result->result ne "pass" and $dmarc_disposition ne $spfu_result->disposition
$spfu_result->result ne "pass" $dmarc_disposition ne $spfu_result->disposition
Truth table33%1/3
A and B
A= $spfu_result->result ne "pass"B= $dmarc_disposition ne $spfu_result->disposition
ABresult
0X0
100
111
524 # spfu mitigation has changed the resulting disposition
525 6 $self->dbgout( 'DMARCReject', "Policy downgraded by spfu mitigation", LOG_DEBUG );
526 6 $dmarc_disposition = $spfu_result->disposition;
527 6 $policy_override = 'local_policy';
528 6 $dmarc_result->reason( 'type' => $policy_override, 'comment' => 'Policy downgraded due to SPF upgrade mitigation' );
529 # Note, this may be overridden further below, for example, local policy reject->quarantine
530 # Reporting of DMARC overrides is not rich enough to support all cases, so we do the best we can.
531 6 $dmarc_result->disposition($dmarc_disposition);
532 6 $spfu_mitigation_triggered = 1;
533 }
534 6 $self->set_object('dmarc', $original_dmarc,1 ); # Restore original saved DMARC object
535 }
536  
537 270 my $is_whitelisted = $self->is_whitelisted();
538  
539 # Reject mail and/or set policy override reasons
540 270 ▶ if ( $dmarc_code eq 'fail' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_code eq "fail") 40 230 270
541 # Policy override decisions.
542 40 ▶ if ( $arc_aware_result eq 'pass' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($arc_aware_result eq 'pass') { } 1 39 40
elsif ($have_arc_dmarc_pass) { } 0 39 39
elsif ($is_whitelisted) { } 4 35 39
elsif ($config->{'no_list_reject'} and $self->{'is_list'}) { } 0 70 70
543 1 $dmarc_result->disposition('none');
544 1 $dmarc_disposition = 'none';
545 1 my $comment = 'Policy overriden using trusted ARC chain';
546 # arc=pass as[2].d=d2.example as[2].s=s2 as[1].d=d1.example as[1].s=s3 remote-ip[1]=2001:DB8::1A
547 1 my $arc_object = $self->get_object('arc');
548 1 my $arc_signatures = $arc_object->{'signatures'};
549  
550 1 my $arc_handler = $self->get_handler('ARC');
551 1 ▶ if ( $arc_handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arc_handler) 1 0 1
552 1 ▶ if ( $arc_handler->{ 'arc_result' } eq 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arc_handler->{"arc_result"} eq "pass") 1 0 1
553 # If it wasn't a pass then we wouldn't be in here.
554 1 $comment = 'arc=pass';
555 1 my $arc_auth_results = $arc_handler->{'arc_auth_results'};
556 0 foreach my $instance ( reverse sort { $a <=> $b } keys %$arc_auth_results ) {
557 1 my $domain = '';
558 1 my $selector = '';
559 1 my $remote_ip = '';
560 1 foreach my $signature ( @$arc_signatures ) {
561 2 ▶ next if $signature->instance() ne $instance;
Branch: 1 branch
BranchTrueFalseTotal
if $signature->instance ne $instance 0 2 2
562 2 $domain = $signature->domain();
563 2 $selector = $signature->selector();
564 }
565 1 my $aar = $arc_auth_results->{$instance};
566 1 $remote_ip = eval{ $aar->search({ 'isa' => 'entry', 'key' => 'iprev' })->children()->[0]->search({ 'isa' => 'subentry', 'key' => 'smtp.remote-ip'})->children()->[0]->value(); };
567 1 $self->handle_exception( $@ );
568 1 ▶ $remote_ip //= eval{ $aar->search({ 'isa' => 'entry', 'key' => 'iprev' })->children()->[0]->search({ 'isa' => 'subentry', 'key' => 'policy.iprev'})->children()->[0]->value(); };
Condition33%1/3
$remote_ip //= eval { do { $aar->search({"isa", "entry", "key", "iprev"})->children->[0]->search({"isa", "subentry", "key", "policy.iprev"})->children->[0]->value } }
l!l&&r!l&&!r
001
MC/DC0%0/2
$remote_ip //= eval { do { $aar->search({"isa", "entry", "key", "iprev"})->children->[0]->search({"isa", "subentry", "key", "policy.iprev"})->children->[0]->value } }
$remote_ip eval { do { $aar->search({"isa", "entry", "key", "iprev"})->children->[0]->search({"isa", "subentry", "key", "policy.iprev"})->children->[0]->value } }
Truth table33%1/3
A //= B
A= $remote_ipB= eval { do { $aar->search({"isa", "entry", "key", "iprev"})->children->[0]->search({"isa", "subentry", "key", "policy.iprev"})->children->[0]->value } }
ABresult
1X1
011
000
569 1 $self->handle_exception( $@ );
570  
571 1 ▶ $domain //= '';
Condition50%1/2
$domain //= ""
l!l
10
MC/DC0%0/1
$domain //= ""
$domain
Truth table50%1/2
A
A= $domain
Aresult
11
00
572 1 ▶ $selector //= '';
Condition50%1/2
$selector //= ""
l!l
10
MC/DC0%0/1
$selector //= ""
$selector
Truth table50%1/2
A
A= $selector
Aresult
11
00
573 1 ▶ $remote_ip //= '';
Condition50%1/2
$remote_ip //= ""
l!l
01
MC/DC0%0/1
$remote_ip //= ""
$remote_ip
Truth table50%1/2
A
A= $remote_ip
Aresult
11
00
574  
575 1 $comment .= ' as['.$instance.'].d='.$domain.' as['.$instance.'].s='.$selector.' remote-ip['.$instance.']='.$remote_ip;
576 }
577 }
578 }
579 1 $self->dbgout( 'DMARCReject', "Policy overridden using ARC Chain: $comment", LOG_DEBUG );
580 1 $dmarc_result->reason( 'type' => 'local_policy', 'comment' => $comment );
581 }
582 elsif ( $have_arc_dmarc_pass ) {
583 0 $self->dbgout( 'DMARCReject', "Policy reject overridden by DMARC pass in trusted ARC chain", LOG_DEBUG );
584 0 $policy_override = 'trusted_forwarder';
585 0 $dmarc_result->reason( 'type' => $policy_override, 'comment' => 'Policy ignored due to DMARC pass in trusted ARC chain' );
586 0 $dmarc_result->disposition('none');
587 0 $dmarc_disposition = 'none';
588 }
589 elsif ( $is_whitelisted ) {
590 4 $self->dbgout( 'DMARCReject', "Policy reject overridden by whitelist", LOG_DEBUG );
591 4 $policy_override = 'trusted_forwarder';
592 4 $dmarc_result->reason( 'type' => $policy_override, 'comment' => 'Policy ignored due to local white list' );
593 4 $dmarc_result->disposition('none');
594 4 $dmarc_disposition = 'none';
595 }
596 ▶ elsif ( $config->{'no_list_reject'} && $self->{'is_list'} ) {
Condition33%1/3
$config->{"no_list_reject"} and $self->{"is_list"}
!ll&&!rl&&r
3500
MC/DC0%0/2
$config->{"no_list_reject"} and $self->{"is_list"}
$config->{"no_list_reject"} $self->{"is_list"}
Truth table33%1/3
A and B
A= $config->{"no_list_reject"}B= $self->{"is_list"}
ABresult
0X0
100
111
597 0 ▶ if ( $config->{'arc_before_list'} && $have_arc && $self->get_handler('ARC')->get_trusted_arc_authentication_results ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'arc_before_list'} and $have_arc and $self->get_handler('ARC')->get_trusted_arc_authentication_results) { } 0 0 0
Condition0%0/6
$config->{"arc_before_list"} and $have_arc and $self->get_handler("ARC")->get_trusted_arc_authentication_results
!ll&&!rl&&r
000
$config->{"arc_before_list"} and $have_arc
!ll&&!rl&&r
000
MC/DC0%0/3
$config->{"arc_before_list"} and $have_arc and $self->get_handler("ARC")->get_trusted_arc_authentication_results
$config->{"arc_before_list"} $have_arc $self->get_handler("ARC")->get_trusted_arc_authentication_results
Truth table0%0/4
A and B and C
A= $config->{"arc_before_list"}B= $have_arcC= $self->get_handler("ARC")->get_trusted_arc_authentication_results
ABCresult
0XX0
10X0
1100
1111
598 0 $self->dbgout( 'DMARCReject', "Policy reject not overridden for list mail with trusted ARC chain", LOG_DEBUG );
599 }
600 else {
601 0 $self->dbgout( 'DMARCReject', "Policy reject overridden for list mail", LOG_DEBUG );
602 0 $policy_override = 'mailing_list';
603 0 $dmarc_result->reason( 'type' => $policy_override, 'comment' => 'Policy ignored due to local mailing list policy' );
604 0 ▶ my $no_list_reject_disposition = $config->{ 'no_list_reject_disposition' } // 'none';
Condition0%0/2
$config->{"no_list_reject_disposition"} // "none"
l!l
00
MC/DC0%0/1
$config->{"no_list_reject_disposition"} // "none"
$config->{"no_list_reject_disposition"}
Truth table0%0/2
A
A= $config->{"no_list_reject_disposition"}
Aresult
11
00
605 0 $dmarc_result->disposition( $no_list_reject_disposition );
606 0 $dmarc_disposition = $no_list_reject_disposition;
607 }
608 }
609  
610 40 ▶ if ( $dmarc_disposition eq 'reject' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_disposition eq "reject") 23 17 40
611 23 ▶ if ( $config->{'hard_reject'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'hard_reject'}) { } 10 13 23
612 10 $self->reject_mail( '550 5.7.0 DMARC policy violation' );
613 10 $self->dbgout( 'DMARCReject', "Policy reject", LOG_DEBUG );
614 }
615 else {
616 13 $policy_override = 'local_policy';
617 13 $dmarc_result->reason( 'type' => $policy_override, 'comment' => 'Reject ignored due to local policy' );
618 13 ▶ my $no_reject_disposition = $config->{ 'no_reject_disposition' } // 'quarantine';
Condition50%1/2
$config->{"no_reject_disposition"} // "quarantine"
l!l
013
MC/DC0%0/1
$config->{"no_reject_disposition"} // "quarantine"
$config->{"no_reject_disposition"}
Truth table50%1/2
A
A= $config->{"no_reject_disposition"}
Aresult
11
00
619 13 $dmarc_result->disposition( $no_reject_disposition );
620 13 $dmarc_disposition = $no_reject_disposition;
621 }
622 }
623 }
624  
625 270 ▶ if ($self->{strict_multifrom_disposition}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict_multifrom_disposition"}) 11 259 270
626 11 $self->dbgout( 'DMARCReject', "Policy downgraded by strict rfc5322 from checks", LOG_DEBUG );
627 11 $dmarc_disposition = $self->{strict_multifrom_disposition};
628 11 $policy_override = 'local_policy';
629 11 $dmarc_result->reason( 'type' => $policy_override, 'comment' => 'Policy downgraded due to multiple rfc5322 from entries' );
630 11 $dmarc_result->disposition($dmarc_disposition);
631 }
632  
633 270 ▶ if ( $dmarc_disposition eq 'quarantine' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_disposition eq "quarantine") 19 251 270
634 19 $self->quarantine_mail( 'Quarantined due to DMARC policy' );
635 }
636  
637 # Add the AR Header
638 270 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dmarc' )->safe_set_value( $dmarc_code );
639  
640 # Do any RBL lookups
641 270 ▶ if ( $config->{'policy_rbl_lookup'} && ( $dmarc_code eq 'pass' || $arc_aware_result eq 'pass' )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"policy_rbl_lookup"} and $dmarc_code eq "pass" || $arc_aware_result eq "pass") 0 270 270
Condition16%1/6
$config->{"policy_rbl_lookup"} and $dmarc_code eq "pass" || $arc_aware_result eq "pass"
!ll&&!rl&&r
27000
$dmarc_code eq "pass" or $arc_aware_result eq "pass"
l!l&&r!l&&!r
000
MC/DC0%0/3
$config->{"policy_rbl_lookup"} and $dmarc_code eq "pass" || $arc_aware_result eq "pass"
$config->{"policy_rbl_lookup"} $dmarc_code eq "pass" $arc_aware_result eq "pass"
Truth table25%1/4
A and B or C
A= $config->{"policy_rbl_lookup"}B= $dmarc_code eq "pass"C= $arc_aware_result eq "pass"
ABCresult
0XX0
1000
11X1
1011
642 0 foreach my $rbl ( sort keys $config->{'policy_rbl_lookup'}->%* ) {
643 0 my $rbl_data = $config->{'policy_rbl_lookup'}->{$rbl};
644 0 my $rbl_domain = $rbl_data->{'rbl'};
645 0 my $rbl_result = $self->rbl_check_domain( $header_domain, $rbl_domain );
646 0 ▶ if ( $rbl_result ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rbl_result) 0 0 0
647 my $txt_result
648 = exists( $rbl_data->{'results'}->{$rbl_result} ) ? $rbl_data->{'results'}->{$rbl_result}
649 0 ▶ : exists( $rbl_data->{'results'}->{'*'} ) ? $rbl_data->{'results'}->{'*'}
Branch: 2 branches
BranchTrueFalseTotal
exists $rbl_data->{'results'}{$rbl_result} ? : 0 0 0
exists $rbl_data->{'results'}{'*'} ? : 0 0 0
650 : 'pass';
651 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.'.$rbl )->safe_set_value( $txt_result ) );
652 }
653 }
654 }
655  
656 # What comments can we add?
657 270 my @comments;
658 270 ▶ if ( $dmarc_policy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_policy) 270 0 270
659 270 push @comments, $self->format_header_entry( 'p', $dmarc_policy );
660 270 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.published-domain-policy' )->safe_set_value( $dmarc_policy ) );
661 }
662 270 ▶ if ( $dmarc_sub_policy ne 'default' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_sub_policy ne "default") 0 270 270
663 0 push @comments, $self->format_header_entry( 'sp', $dmarc_sub_policy );
664 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.published-subdomain-policy' )->safe_set_value( $dmarc_sub_policy ) );
665 }
666 270 ▶ if ( $config->{'detect_list_id'} && $self->{'is_list'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"detect_list_id"} and $self->{"is_list"}) 1 269 270
Condition100%3/3
$config->{"detect_list_id"} and $self->{"is_list"}
!ll&&!rl&&r
179901
MC/DC100%2/2
$config->{"detect_list_id"} and $self->{"is_list"}
$config->{"detect_list_id"} $self->{"is_list"}
Truth table100%3/3
A and B
A= $config->{"detect_list_id"}B= $self->{"is_list"}
ABresult
0X0
100
111
667 1 push @comments, 'has-list-id=yes';
668 }
669 270 ▶ if ( $dmarc_disposition ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_disposition) 270 0 270
670 270 push @comments, $self->format_header_entry( 'd', $dmarc_disposition );
671 270 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.applied-disposition' )->safe_set_value( $dmarc_disposition ) );
672 }
673 270 ▶ if ( $dmarc_disposition_evaluated ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_disposition_evaluated) 270 0 270
674 270 push @comments, $self->format_header_entry( 'd.eval', $dmarc_disposition_evaluated );
675 270 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.evaluated-disposition' )->safe_set_value( $dmarc_disposition_evaluated ) );
676 }
677 270 ▶ if ( $policy_override ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy_override) 34 236 270
678 34 push @comments, $self->format_header_entry( 'override', $policy_override );
679 34 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.override-reason' )->safe_set_value( $policy_override ) );
680 }
681 270 ▶ if ( $arc_aware_result ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arc_aware_result) 2 268 270
682 2 push @comments, $self->format_header_entry( 'arc_aware_result', $arc_aware_result );
683 2 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.arc-aware-result' )->safe_set_value( $arc_aware_result ) );
684 }
685  
686 270 ▶ if ( @comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@comments) 270 0 270
687 270 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( join( ',', @comments ) ) );
688 }
689  
690 270 ▶ my $policy_used = ( $is_subdomain && $dmarc_sub_policy ne 'default' ) ? 'sp' : 'p';
Branch: 1 branch
BranchTrueFalseTotal
$is_subdomain && $dmarc_sub_policy ne 'default' ? : 0 270 270
Condition66%2/3
$is_subdomain and $dmarc_sub_policy ne "default"
!ll&&!rl&&r
1331370
MC/DC0%0/2
$is_subdomain and $dmarc_sub_policy ne "default"
$is_subdomain $dmarc_sub_policy ne "default"
Truth table66%2/3
A and B
A= $is_subdomainB= $dmarc_sub_policy ne "default"
ABresult
0X0
100
111
691 270 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.policy-from' )->safe_set_value( $policy_used ) );
692  
693 270 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.from' )->safe_set_value( $header_domain ) );
694 270 ▶ if ( $config->{'log_dns_record'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"log_dns_record"}) 2 268 270
695 2 eval {
696 2 my $dmarc_dns_name = '_dmarc.' . $header_domain;
697 2 my $resolver = $self->get_object('resolver');
698 2 my $reply = $resolver->query( $dmarc_dns_name, 'TXT' );
699 2 ▶ if ( $reply ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply) 2 0 2
700 2 my @txt_parts;
701 2 for my $rr ( $reply->answer ) {
702 2 ▶ next unless $rr->type eq 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "TXT" 0 2 2
703 2 push @txt_parts, join( '', $rr->txtdata );
704 }
705 2 ▶ if ( @txt_parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@txt_parts) 2 0 2
706 2 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'x-dns-record=' . $self->escape_non_printable_ascii( join( ' ', @txt_parts ) ) ) );
707 }
708 }
709 };
710 2 $self->handle_exception( $@ );
711 }
712 270 $self->_add_dmarc_header( $header );
713  
714 # Write Metrics
715 my $metric_data = {
716 'result' => $dmarc_code,
717 'disposition' => $dmarc_disposition,
718 'policy' => $dmarc_policy,
719 270 ▶ 'is_list' => ( $self->{'is_list'} ? '1' : '0' ),
Branch: 4 branches
BranchTrueFalseTotal
$self->{'is_list'} ? : 1 269 270
$is_whitelisted ? : 4 266 270
$arc_aware_result ? : 2 268 270
$is_subdomain ? : 137 133 270
720 'is_whitelisted' => ( $is_whitelisted ? '1' : '0'),
721 'arc_aware_result' => $arc_aware_result,
722 'used_arc' => ( $arc_aware_result ? '1' : '0' ),
723 'is_subdomain' => ( $is_subdomain ? '1' : '0' ),
724 };
725 270 $self->metric_count( 'dmarc_total', $metric_data );
726  
727 # Try as best we can to save a report, but don't stress if it fails.
728 270 my $rua = eval { $dmarc_result->published()->rua(); };
729 270 $self->handle_exception( $@ );
730 270 ▶ if ($rua) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rua) 77 193 270
731 77 ▶ if ( ! $config->{'no_report'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $config->{'no_report'}) { } 77 0 77
732 77 ▶ if ( ! $self->{'skip_report'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'skip_report'}) { } 77 0 77
733 77 $self->dbgout( 'DMARCReportTo', $rua, LOG_INFO );
734 77 push @{ $self->{'report_queue'} }, $dmarc;
735 }
736 else {
737 0 $self->dbgout( 'DMARCReportTo (skipped flag)', $rua, LOG_INFO );
738 }
739 }
740 else {
741 0 $self->dbgout( 'DMARCReportTo (skipped)', $rua, LOG_INFO );
742 }
743 }
744 }
745  
746 302 ▶sub get_dmarc_object {
Subroutine get_dmarc_object: called CC=2 SCAR=6.9
Pod undocumented
747 302 my ( $self ) = @_;
748 302 my $dmarc = $self->get_object('dmarc');
749 302 ▶ if ( $dmarc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc) 20 282 302
750 20 return $dmarc;
751 }
752  
753 282 $dmarc = $self->new_dmarc_object();
754 282 $self->set_object('dmarc', $dmarc,1 );
755 282 return $dmarc;
756 }
757  
758 552 ▶sub _build_dmarc_object {
Subroutine _build_dmarc_object: called CC=6 SCAR=18.0
759 552 my ( $self ) = @_;
760  
761 552 my $config = $self->handler_config();
762 552 my $dmarc = Mail::DMARC::PurePerl->new();
763 552 ▶ if ( exists ( $config->{ 'config_file' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"config_file"}) 5 547 552
764 5 ▶ $self->log_error( 'DMARC config file does not exist' ) if ! -e $config->{ 'config_file' };
Branch: 1 branch
BranchTrueFalseTotal
unless -e $config->{"config_file"} 0 5 5
765 5 $dmarc->config( $config->{ 'config_file' } );
766 }
767 552 ▶ if ( $dmarc->can('set_resolver') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc->can("set_resolver")) 552 0 552
768 552 my $resolver = $self->get_object('resolver');
769 552 $dmarc->set_resolver($resolver);
770 }
771 552 ▶ if ( $self->config()->{'debug'} && $self->config()->{'logtoerr'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"debug"} and $self->config->{"logtoerr"}) 385 167 552
Condition66%2/3
$self->config->{"debug"} and $self->config->{"logtoerr"}
!ll&&!rl&&r
1670385
MC/DC50%1/2
$self->config->{"debug"} and $self->config->{"logtoerr"}
$self->config->{"debug"} $self->config->{"logtoerr"}
Truth table66%2/3
A and B
A= $self->config->{"debug"}B= $self->config->{"logtoerr"}
ABresult
0X0
100
111
772 385 $dmarc->verbose(1);
773 }
774 552 return $dmarc;
775 }
776  
777 512 ▶sub new_dmarc_object {
Subroutine new_dmarc_object: called CC=2 SCAR=9.2
Pod undocumented
778 512 my ( $self ) = @_;
779  
780 512 my $dmarc;
781  
782 512 eval {
783 512 $dmarc = $self->_build_dmarc_object();
784 512 $self->set_object('dmarc', $dmarc,1 );
785 };
786 512 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 512 512
787 0 $self->handle_exception( $error );
788 0 $self->log_error( 'DMARC IP Error ' . $error );
789 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dmarc' )->safe_set_value( 'permerror' );
790 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'DMARC IP error' ) );
791 0 $self->add_auth_header( $header );
792 0 $self->metric_count( 'dmarc_total', { 'result' => 'permerror' } );
793 0 $self->{'failmode'} = 1;
794 }
795  
796 512 return $dmarc;
797 }
798  
799 166 ▶sub helo_callback {
Subroutine helo_callback: called CC=2 SCAR=7.1
Pod undocumented
800 166 my ( $self, $helo_host ) = @_;
801 166 $self->{'helo_name'} = $helo_host;
802 166 ▶ $self->{'report_queue'} = [] if ! $self->{'report_queue'};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"report_queue"} 166 0 166
803 }
804  
805 54 ▶sub envfrom_requires {
Subroutine envfrom_requires: called CC=1 SCAR=0.0
Pod undocumented
806 54 my ($self) = @_;
807 54 my @requires = qw{ SPF };
808 54 return \@requires;
809 }
810  
811 166 ▶sub envfrom_callback {
Subroutine envfrom_callback: called CC=8 SCAR=22.3
Pod undocumented
812 166 my ( $self, $env_from ) = @_;
813 166 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 8 158 166
814 158 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 2 156 158
815 156 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 156 156
816 156 delete $self->{'from_header'};
817 156 $self->{'processed'} = {};
818 156 $self->{'from_lookup_count'} = 0;
819 156 $self->{'is_list'} = 0;
820 156 $self->{'skip_report'} = 0;
821 156 $self->{'failmode'} = 0;
822 156 $self->{'strict_multifrom_disposition'} = '';
823  
824 156 ▶ $env_from = q{} if $env_from eq '<>';
Branch: 1 branch
BranchTrueFalseTotal
if $env_from eq "<>" 1 155 156
825  
826 156 ▶ if ( ! $self->is_handler_loaded( 'SPF' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_handler_loaded("SPF")) 0 156 156
827 0 $self->log_error( 'DMARC Config Error: SPF is missing ');
828 0 $self->metric_count( 'dmarc_total', { 'result' => 'error' } );
829 0 $self->{'failmode'} = 1;
830 0 return;
831 }
832 156 ▶ if ( ! $self->is_handler_loaded( 'DKIM' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_handler_loaded("DKIM")) 0 156 156
833 0 $self->log_error( 'DMARC Config Error: DKIM is missing ');
834 0 $self->metric_count( 'dmarc_total', { 'result' => 'error' } );
835 0 $self->{'failmode'} = 1;
836 0 return;
837 }
838  
839 156 ▶ if ( $env_from ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_from) { } 151 5 156
840 151 $self->{ 'env_from' } = $env_from;
841 }
842 else {
843 5 $self->{ 'env_from' } = q{};
844 }
845  
846 156 $self->{ 'from_headers' } = [];
847 }
848  
849 156 ▶sub check_skip_address {
Subroutine check_skip_address: called CC=4 SCAR=20.4
Pod undocumented
850 156 my ( $self, $env_to ) = @_;
851 156 $env_to = lc $self->get_address_from( $env_to );
852 156 my $config = $self->handler_config();
853 156 ▶ return 0 if not exists( $config->{'report_skip_to'} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{"report_skip_to"} 156 0 156
854 0 foreach my $address ( @{ $config->{'report_skip_to'} } ) {
855 0 ▶ if ( lc $address eq lc $env_to ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $address eq lc $env_to) 0 0 0
856 0 $self->dbgout( 'DMARCReportSkip', 'Skip address detected: ' . $env_to, LOG_INFO );
857 0 $self->{'skip_report'} = 1;
858 }
859 }
860 }
861  
862 166 ▶sub envrcpt_callback {
Subroutine envrcpt_callback: called CC=4 SCAR=13.9
Pod undocumented
863 166 my ( $self, $env_to ) = @_;
864 166 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 8 158 166
865 158 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 2 156 158
866 156 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 156 156
867  
868 156 $self->{ 'env_to' } = $env_to;
869 156 $self->check_skip_address( $env_to );
870 }
871  
872 1359 ▶sub header_callback {
Subroutine header_callback: called CC=14 SCAR=26.5
Pod undocumented
873 1359 my ( $self, $header, $value ) = @_;
874 1359 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 117 1242 1359
875 1242 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 30 1212 1242
876 1212 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 1212 1212
877 1212 ▶ return if ( $self->{'failmode'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 1212 1212
878  
879 1212 ▶ if ( lc $header eq 'list-id' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "list-id") 1 1211 1212
880 1 $self->dbgout( 'DMARCListId', 'List ID detected: ' . $value, LOG_INFO );
881 1 $self->{'is_list'} = 1;
882 }
883 1212 ▶ if ( lc $header eq 'list-post' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "list-post") 0 1212 1212
884 0 $self->dbgout( 'DMARCListId', 'List Post detected: ' . $value, LOG_INFO );
885 0 $self->{'is_list'} = 1;
886 }
887  
888 1212 ▶ if ( lc $header eq 'from' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "from") 311 901 1212
889 311 ▶ if ( exists $self->{'from_header'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"from_header"}) 157 154 311
890 157 $self->dbgout( 'DMARCFail', 'Multiple RFC5322 from fields', LOG_DEBUG );
891 }
892 311 $self->{'from_header'} = $value;
893 311 push @{ $self->{ 'from_headers' } }, $value;
894 311 my $domain = lc $self->get_domain_from( $value );
895 311 my $config = $self->handler_config();
896 # Unlike DKIM and XGoogleDKIM there is no separate early_lookup_limit
897 # here. skip_on_multifrom already bounds how many From domains are
898 # evaluated, and a message with more is quarantined or rejected, so
899 # early lookups are only started for domains we will go on to look up
900 # anyway, at most two (domain and org domain) per From header.
901 # Duplicates are not skipped, they count towards the same bound.
902 311 ▶ my $lookup_limit = $config->{'skip_on_multifrom'} || 10;
Condition100%2/2
$config->{'skip_on_multifrom'} || 10
l!l
143168
MC/DC100%1/1
$config->{'skip_on_multifrom'} || 10
$config->{'skip_on_multifrom'}
Truth table100%2/2
A
A= $config->{'skip_on_multifrom'}
Aresult
11
00
903 311 ▶ if ( $domain && $self->{'from_lookup_count'}++ < $lookup_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain and $self->{"from_lookup_count"}++ < $lookup_limit) 222 89 311
Condition100%3/3
$domain and $self->{"from_lookup_count"}++ < $lookup_limit
!ll&&!rl&&r
2663222
MC/DC100%2/2
$domain and $self->{"from_lookup_count"}++ < $lookup_limit
$domain $self->{"from_lookup_count"}++ < $lookup_limit
Truth table100%3/3
A and B
A= $domainB= $self->{"from_lookup_count"}++ < $lookup_limit
ABresult
0X0
100
111
904 222 my $lookup = '_dmarc.'.$domain;
905 222 my $resolver = $self->get_object('resolver');
906 222 eval{ $resolver->bgsend( $lookup, 'TXT' ) };
907 222 $self->handle_exception( $@ );
908 222 $self->dbgout( 'DNSEarlyLookup', "$lookup TXT", LOG_DEBUG );
909 222 my $dmarc = $self->new_dmarc_object();
910 222 my $org_domain = eval{ $dmarc->get_organizational_domain( $domain ) };
911 222 $self->handle_exception( $@ );
912 222 ▶ if ( $org_domain && ($org_domain ne $domain) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($org_domain and $org_domain ne $domain) 101 121 222
Condition66%2/3
$org_domain and $org_domain ne $domain
!ll&&!rl&&r
0121101
MC/DC50%1/2
$org_domain and $org_domain ne $domain
$org_domain $org_domain ne $domain
Truth table66%2/3
A and B
A= $org_domainB= $org_domain ne $domain
ABresult
0X0
100
111
913 101 my $lookup = '_dmarc.'.$org_domain;
914 101 my $resolver = $self->get_object('resolver');
915 101 eval{ $resolver->bgsend( $lookup, 'TXT' ) };
916 101 $self->handle_exception( $@ );
917 101 $self->dbgout( 'DNSEarlyLookup', "$lookup TXT", LOG_DEBUG );
918 }
919 }
920  
921 }
922 }
923  
924 54 ▶sub eom_requires {
Subroutine eom_requires: called CC=2 SCAR=6.9
Pod undocumented
925 54 my ($self) = @_;
926 54 my @requires = qw{ DKIM };
927  
928 54 ▶ if ( $self->is_handler_loaded( 'ARC' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_handler_loaded("ARC")) 2 52 54
929 2 push @requires, 'ARC';
930 }
931  
932 54 return \@requires;
933 }
934  
935 166 ▶sub eom_callback {
Subroutine eom_callback: called CC=25 SCAR=32.9
Pod undocumented
936 166 my ($self) = @_;
937 166 my $config = $self->handler_config();
938  
939 166 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 8 158 166
940 158 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 2 156 158
941 156 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 156 156
942 156 ▶ return if ( $self->{'failmode'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 156 156
943  
944 156 my $env_from = $self->{ 'env_from' };
945 156 my $env_domains_from = $self->get_domains_from($env_from);
946 156 ▶ $env_domains_from = [''] if ! @$env_domains_from;
Branch: 1 branch
BranchTrueFalseTotal
unless @$env_domains_from 10 146 156
947  
948 156 ▶ my $from_headers = $self->{ 'from_headers' } // [];
Condition50%1/2
$self->{"from_headers"} // []
l!l
1560
MC/DC0%0/1
$self->{"from_headers"} // []
$self->{"from_headers"}
Truth table50%1/2
A
A= $self->{"from_headers"}
Aresult
11
00
949  
950 # Build a list of all from header domains used
951 156 my @header_domains;
952 156 my $from_incomplete = 0;
953 156 foreach my $from_header ( @$from_headers ) {
954 311 my ( $from_header_header_domains, $complete ) = $self->_get_from_header_domains( $from_header );
955 311 ▶ $from_incomplete = 1 if ! $complete;
Branch: 1 branch
BranchTrueFalseTotal
unless $complete 16 295 311
956 311 push @header_domains, @$from_header_header_domains;
957 }
958  
959 156 ▶ if ( $config->{strict_multifrom} && ( scalar @$from_headers > 1 || scalar @header_domains > 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"strict_multifrom"} and scalar @$from_headers > 1 || scalar @header_domains > 1) 8 148 156
Condition100%6/6
$config->{"strict_multifrom"} and scalar @$from_headers > 1 || scalar @header_domains > 1
!ll&&!rl&&r
14628
scalar @$from_headers > 1 or scalar @header_domains > 1
l!l&&r!l&&!r
712
MC/DC100%3/3
$config->{"strict_multifrom"} and scalar @$from_headers > 1 || scalar @header_domains > 1
$config->{"strict_multifrom"} scalar @$from_headers > 1 scalar @header_domains > 1
Truth table100%4/4
A and B or C
A= $config->{"strict_multifrom"}B= scalar @$from_headers > 1C= scalar @header_domains > 1
ABCresult
0XX0
1000
11X1
1011
960 # Multiple from headers, or domains, in strict mode?
961 # Don't allow anything to have a passing disposition
962 8 ▶ if ( $config->{'hard_reject'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'hard_reject'}) { } 3 5 8
963 3 $self->{strict_multifrom_disposition} = 'reject';
964 3 $self->reject_mail( '550 5.7.0 DMARC policy violation' );
965 3 $self->dbgout( 'DMARCReject', "Strict Multifrom reject", LOG_DEBUG );
966 } else {
967 5 $self->{strict_multifrom_disposition} = 'quarantine';
968 5 $self->quarantine_mail( 'Quarantined due to DMARC policy' );
969 }
970 }
971  
972 156 $self->{ 'dmarc_ar_headers' } = [];
973  
974 156 my @unique_env_domains_from = uniq sort @$env_domains_from;
975 156 my @unique_header_domains = uniq sort @header_domains;
976 156 my $pair_count = scalar @unique_env_domains_from * scalar @unique_header_domains;
977 156 my $process_limit = $self->_check_multifrom_limits( $pair_count );
978 156 my $process_count = 0;
979  
980 # There will usually be only one, however this could be a source route
981 # so we consider multiples just incase
982 156 foreach my $env_domain_from ( @unique_env_domains_from ) {
983 157 foreach my $header_domain ( @unique_header_domains ) {
984 355 ▶ if ( defined $process_limit && $process_count >= $process_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $process_limit and $process_count >= $process_limit) 74 281 355
Condition100%3/3
defined $process_limit and $process_count >= $process_limit
!ll&&!rl&&r
16911274
MC/DC100%2/2
defined $process_limit and $process_count >= $process_limit
defined $process_limit $process_count >= $process_limit
Truth table100%3/3
A and B
A= defined $process_limitB= $process_count >= $process_limit
ABresult
0X0
100
111
985 # Never skip silently, a skipped domain must still be visible in the results
986 74 $self->log_error( "DMARC limit reached, skipping processing for $env_domain_from $header_domain" );
987 74 $self->metric_count( 'dmarc_total', { 'result' => 'permerror' } );
988 74 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dmarc' )->safe_set_value( 'permerror' );
989 74 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'too many from domains' ) );
990 74 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.from' )->safe_set_value( $header_domain ) );
991 74 $self->_add_dmarc_header( $header );
992 74 next;
993 }
994 281 $process_count++;
995 281 eval {
996 281 $self->_process_dmarc_for( $env_domain_from, $header_domain );
997 };
998 281 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 281 281
999 0 $self->handle_exception( $error );
1000 0 ▶ if ( $error =~ /invalid header_from at / ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ /invalid header_from at /u) { } 0 0 0
1001 0 $self->log_error( 'DMARC Error invalid header_from <' . $self->{'from_header'} . '>' );
1002 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dmarc' )->safe_set_value( 'permerror' );
1003 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.from' )->safe_set_value( $header_domain ) );
1004 0 $self->_add_dmarc_header( $header );
1005 }
1006 else {
1007 0 $self->log_error( 'DMARC Error ' . $error );
1008 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dmarc' )->safe_set_value( 'temperror' );
1009 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.from' )->safe_set_value( $header_domain ) );
1010 0 $self->_add_dmarc_header( $header );
1011 }
1012 }
1013 281 $self->check_timeout();
1014 }
1015 }
1016  
1017 156 ▶ if ( $from_incomplete || ! @unique_header_domains ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_incomplete or not @unique_header_domains) 18 138 156
Condition100%3/3
$from_incomplete or not @unique_header_domains
l!l&&r!l&&!r
162138
MC/DC100%2/2
$from_incomplete or not @unique_header_domains
$from_incomplete not @unique_header_domains
Truth table100%3/3
A or not(B)
A= $from_incompleteB= not @unique_header_domains
ABresult
1X1
011
000
1018 # A from header we could not fully parse may hide a domain which would
1019 # otherwise have been evaluated, so we can not let this mail through unscathed
1020 18 ▶ my $comment = @unique_header_domains ? 'unparseable from header' : 'no parseable from domain';
Branch: 1 branch
BranchTrueFalseTotal
@unique_header_domains ? : 13 5 18
1021 18 $self->log_error( "DMARC $comment, quarantining" );
1022 18 $self->metric_count( 'dmarc_total', { 'result' => 'permerror' } );
1023 18 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dmarc' )->safe_set_value( 'permerror' );
1024 18 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( $comment ) );
1025 18 $self->_add_dmarc_header( $header );
1026 18 $self->quarantine_mail( 'Quarantined due to DMARC policy' );
1027 }
1028  
1029 156 foreach my $dmarc_header ( @{ $self->_get_unique_dmarc_headers() } ) {
1030 370 ▶ if ( !( $config->{'hide_none'} && $dmarc_header->value() eq 'none' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"} and $dmarc_header->value eq "none") 365 5 370
Condition100%3/3
$config->{"hide_none"} and $dmarc_header->value eq "none"
!ll&&!rl&&r
348175
MC/DC100%2/2
$config->{"hide_none"} and $dmarc_header->value eq "none"
$config->{"hide_none"} $dmarc_header->value eq "none"
Truth table100%3/3
A and B
A= $config->{"hide_none"}B= $dmarc_header->value eq "none"
ABresult
0X0
100
111
1031 365 $self->add_auth_header( $dmarc_header );
1032 }
1033 }
1034  
1035 156 delete $self->{ 'dmarc_ar_headers' };
1036 }
1037  
1038 # Apply the multifrom limits based on the total number of DMARC entities in the
1039 # message, before any are processed, so that the outcome can not depend on which
1040 # domains sort into the processed set.
1041 # Returns the maximum number of entities to process, or undef for no limit.
1042 156 ▶sub _check_multifrom_limits {
Subroutine _check_multifrom_limits: called CC=5 SCAR=16.1
1043 156 my ( $self, $pair_count ) = @_;
1044 156 my $config = $self->handler_config();
1045  
1046 156 my @limits = grep { $_ } map { $config->{ $_ } } qw{ skip_on_multifrom quarantine_on_multifrom reject_on_multifrom };
1047 156 ▶ return undef unless @limits; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
unless @limits 143 13 156
1048 13 my $process_limit = min @limits;
1049  
1050 13 ▶ if ( $config->{'reject_on_multifrom'} && $pair_count > $config->{'reject_on_multifrom'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config->{'reject_on_multifrom'} and $pair_count > $config->{'reject_on_multifrom'}) { } 1 12 13
elsif ($pair_count > $process_limit) { } 8 4 12
Condition66%2/3
$config->{"reject_on_multifrom"} and $pair_count > $config->{"reject_on_multifrom"}
!ll&&!rl&&r
0121
MC/DC50%1/2
$config->{"reject_on_multifrom"} and $pair_count > $config->{"reject_on_multifrom"}
$config->{"reject_on_multifrom"} $pair_count > $config->{"reject_on_multifrom"}
Truth table66%2/3
A and B
A= $config->{"reject_on_multifrom"}B= $pair_count > $config->{"reject_on_multifrom"}
ABresult
0X0
100
111
1051 1 $self->log_error( "DMARC limit reached ($pair_count entities), rejecting" );
1052 1 $self->reject_mail( '550 5.7.0 DMARC policy violation' );
1053 }
1054 elsif ( $pair_count > $process_limit ) {
1055 # We can not evaluate every entity, so we can not let this mail through unscathed
1056 8 $self->log_error( "DMARC limit reached ($pair_count entities), quarantining" );
1057 8 $self->quarantine_mail( 'Quarantined due to DMARC policy' );
1058 }
1059  
1060 13 return $process_limit;
1061 }
1062  
1063 520 ▶sub can_sort_header {
Subroutine can_sort_header: called CC=2 SCAR=6.9
Pod documented
1064 520 my ( $self, $header ) = @_;
1065 520 ▶ return 1 if $header eq 'dmarc';
Branch: 1 branch
BranchTrueFalseTotal
if $header eq "dmarc" 519 1 520
1066 1 return 0;
1067 }
1068  
1069  
1070 519 ▶sub handler_header_sort {
Subroutine handler_header_sort: called CC=11 SCAR=24.9
Pod undocumented
1071 519 my ( $self, $pa, $pb ) = @_;
1072  
1073 # ToDo, do this without stringify
1074 519 my ( $result_a, $policy_a ) = $pa->as_string() =~ /^dmarc=([a-z]+) .*policy\.applied\-disposition=([a-z]+)/;
1075 519 my ( $result_b, $policy_b ) = $pb->as_string() =~ /^dmarc=([a-z]+) .*policy\.applied\-disposition=([a-z]+)/;
1076  
1077 # Fail then None then Pass
1078 519 ▶ if ( $result_a ne $result_b ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_a ne $result_b) 76 443 519
1079 76 ▶ return -1 if $result_a eq 'fail';
Branch: 1 branch
BranchTrueFalseTotal
if $result_a eq "fail" 3 73 76
1080 73 ▶ return 1 if $result_b eq 'fail';
Branch: 1 branch
BranchTrueFalseTotal
if $result_b eq "fail" 6 67 73
1081 67 ▶ return -1 if $result_a eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $result_a eq "none" 20 47 67
1082 47 ▶ return 1 if $result_b eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $result_b eq "none" 47 0 47
1083 }
1084  
1085 # Reject then Quarantine then None
1086 443 ▶ if ( $policy_a ne $policy_b ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy_a ne $policy_b) 2 441 443
1087 2 ▶ return -1 if $policy_a eq 'reject';
Branch: 1 branch
BranchTrueFalseTotal
if $policy_a eq "reject" 0 2 2
1088 2 ▶ return 1 if $policy_b eq 'reject';
Branch: 1 branch
BranchTrueFalseTotal
if $policy_b eq "reject" 0 2 2
1089 2 ▶ return -1 if $policy_a eq 'quarantine';
Branch: 1 branch
BranchTrueFalseTotal
if $policy_a eq "quarantine" 2 0 2
1090 0 ▶ return 1 if $policy_b eq 'quarantine';
Branch: 1 branch
BranchTrueFalseTotal
if $policy_b eq "quarantine" 0 0 0
1091 }
1092  
1093 441 return $pa cmp $pb;
1094 }
1095  
1096 156 ▶sub _get_unique_dmarc_headers {
Subroutine _get_unique_dmarc_headers: called CC=3 SCAR=11.0
1097 156 my ( $self ) = @_;
1098  
1099 156 my $unique_strings = {};
1100 156 my @unique_headers;
1101  
1102 # Returns unique headers based on as_string for each header
1103 156 foreach my $header ( @{ $self->{ 'dmarc_ar_headers' } } ) {
1104 373 my $as_string = $header->as_string();
1105 373 ▶ next if exists $unique_strings->{ $as_string };
Branch: 1 branch
BranchTrueFalseTotal
if exists $unique_strings->{$as_string} 3 370 373
1106 370 $unique_strings->{ $as_string } = 1;
1107 370 push @unique_headers, $header;
1108 }
1109  
1110 156 return \@unique_headers;
1111 }
1112  
1113 373 ▶sub _add_dmarc_header {
Subroutine _add_dmarc_header: called CC=1 SCAR=0.0
1114 373 my ( $self, $header ) = @_;
1115 373 push @{ $self->{ 'dmarc_ar_headers' } }, $header;
1116 }
1117  
1118 # Extract the domains from a from header for DMARC evaluation, returning
1119 # ( \@domains, $complete ).
1120 #
1121 # get_domains_from returns nothing if any part of the header fails to parse,
1122 # which would let a sender hide a domain from evaluation. Here we keep every
1123 # domain we can find, including from mailboxes the parser flags as invalid,
1124 # and report whether the whole header was accounted for so the caller can
1125 # act on the remainder. Empty list members (RFC 5322 obs-mbox-list) are
1126 # skipped, they must be accepted and carry no mailbox.
1127 311 ▶sub _get_from_header_domains {
Subroutine _get_from_header_domains: called CC=10 SCAR=23.0
1128 311 my ( $self, $from_header ) = @_;
1129 311 ▶ return ( [], 0 ) if ! defined $from_header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_header 0 311 311
1130  
1131 # Restore the RFC 5322 CRLF folding which milter header values lose
1132 311 $from_header =~ s/\015?\012/\015\012/g;
1133  
1134 311 my @domains;
1135 311 my $complete = 1;
1136 311 foreach my $address_object ( Email::Address::XS->parse( $from_header ) ) {
1137 366 my $host = $address_object->host;
1138 366 ▶ if ( ! defined $host ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host) 12 354 366
1139 12 ▶ next if ! defined $address_object->user && ( $address_object->original // q{} ) =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $address_object->user and ($address_object->original // "") =~ /^\s*$/u 3 9 12
Condition80%4/5
not defined $address_object->user and ($address_object->original // "") =~ /^\s*$/u
!ll&&!rl&&r
543
$address_object->original // ""
l!l
70
MC/DC50%1/2
not defined $address_object->user and ($address_object->original // "") =~ /^\s*$/u
not defined $address_object->user $address_object->original
Truth table66%2/3
not(A) and B
A= not defined $address_object->userB= $address_object->original
ABresult
0X0
100
111
1140 9 $complete = 0;
1141 9 next;
1142 }
1143 354 $host = lc $host;
1144 # As per get_address_xs_from
1145 354 ▶ if ( $host !~ /^[a-z0-9_\-]+(?:\.[a-z0-9_\-]+)*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($host =~ /^[a-z0-9_\-]+(?:\.[a-z0-9_\-]+)*$/u) 1 353 354
1146 1 $complete = 0;
1147 1 next;
1148 }
1149 353 ▶ $complete = 0 if ! $address_object->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $address_object->is_valid 4 349 353
1150 353 push @domains, $host;
1151 }
1152 311 ▶ $complete = 0 if ! @domains;
Branch: 1 branch
BranchTrueFalseTotal
unless @domains 6 305 311
1153  
1154 311 return ( \@domains, $complete );
1155 }
1156  
1157 246 ▶sub addheader_callback {
Subroutine addheader_callback: called CC=1 SCAR=0.0
Pod undocumented
1158 246 my $self = shift;
1159 246 my $handler = shift;
1160 }
1161  
1162 # Split a DMARC rua tag value into deliverable and malformed entries,
1163 # returning ( \@kept, \@bad ). Mail::DMARC::Policy and ::URI accept any
1164 # "mailto:..." string regardless of address shape, so junk like
1165 # "rua=mailto:0" from a publisher's _dmarc record would otherwise wedge
1166 # the sender queue: Sender::email croaks on a falsy $to ("0" is falsy in
1167 # Perl), the croak is uncaught, the row stays, and no report_error is
1168 # written.
1169 43 ▶sub _filter_rua {
Subroutine _filter_rua: called CC=6 SCAR=18.0
1170 43 my ( $self, $rua ) = @_;
1171 43 my @kept;
1172 my @bad;
1173 43 ▶ for my $rua_entry ( split /,/, $rua // '' ) {
Condition50%1/2
$rua // ""
l!l
430
MC/DC0%0/1
$rua // ""
$rua
Truth table50%1/2
A
A= $rua
Aresult
11
00
1174 44 $rua_entry =~ s/\s+//g;
1175 # RFC 7489 6.2: Remove the suffix before matching
1176 # e.g. "mailto:foo@bar.com!10m"
1177 44 my ($uri) = split /!/, $rua_entry, 2;
1178 44 ▶ if ( $uri =~ /^mailto:(.+)$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($uri =~ /^mailto:(.+)$/iu) { } 43 1 44
elsif ($uri =~ /^https?:/iu) { } 1 0 1
1179 43 ▶ if ( $self->get_address_from($1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_address_from($1)) { } 41 2 43
1180 41 push @kept, $rua_entry;
1181 }
1182 else {
1183 2 push @bad, $rua_entry;
1184 }
1185 }
1186 elsif ( $uri =~ m{^https?:}i ) {
1187 1 push @kept, $rua_entry;
1188 }
1189 }
1190 43 return ( \@kept, \@bad );
1191 }
1192  
1193 # Reports are queued as plain data holding only what save_aggregate needs
1194 42 my @queued_report_fields = qw{ source_ip envelope_to envelope_from header_from is_subdomain };
1195 42 my @queued_result_fields = qw{ result disposition dkim dkim_align spf spf_align };
1196  
1197 60 ▶sub _dmarc_report_to_queue {
Subroutine _dmarc_report_to_queue: called CC=4 SCAR=14.0
1198 60 my ( $self, $report ) = @_;
1199 60 my $result = $report->result();
1200 return {
1201 298 ( map { $_ => $report->{$_} } grep { defined $report->{$_} } @queued_report_fields ),
1202 41 ▶ 'dkim' => [ map { +{ %$_ } } @{ $report->dkim() // [] } ],
Condition50%1/2
$report->dkim // []
l!l
600
MC/DC0%0/1
$report->dkim // []
$report->dkim
Truth table50%1/2
A
A= $report->dkim
Aresult
11
00
1203 60 ▶ 'spf' => [ map { +{ %$_ } } @{ $report->spf() // [] } ],
Condition50%1/2
$report->spf // []
l!l
600
MC/DC0%0/1
$report->spf // []
$report->spf
Truth table50%1/2
A
A= $report->spf
Aresult
11
00
1204 293 'result' => { map { $_ => $result->{$_} } grep { defined $result->{$_} } @queued_result_fields },
1205 14 ▶ 'reason' => [ map { +{ %$_ } } @{ $result->reason() // [] } ],
Condition50%1/2
$result->reason // []
l!l
600
MC/DC0%0/1
$result->reason // []
$result->reason
Truth table50%1/2
A
A= $result->reason
Aresult
11
00
1206 60 'published' => { %{ $result->published() } },
1207 };
1208 }
1209  
1210 40 ▶sub _dmarc_report_from_queue {
Subroutine _dmarc_report_from_queue: called CC=7 SCAR=19.5
1211 40 my ( $self, $data ) = @_;
1212 40 ▶ return if ! $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 40 40
1213 40 my $report = $self->_build_dmarc_object();
1214 40 for my $field ( @queued_report_fields ) {
1215 200 ▶ $report->$field( $data->{$field} ) if defined $data->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{$field} 198 2 200
1216 }
1217 # Plain hashes, as the handler supplies them, are converted on save
1218 40 $report->{'dkim'} = $data->{'dkim'};
1219 40 $report->{'spf'} = $data->{'spf'};
1220 40 my $result = $report->result();
1221 40 for my $field ( @queued_result_fields ) {
1222 240 ▶ $result->$field( $data->{'result'}->{$field} ) if defined $data->{'result'}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{"result"}{$field} 197 43 240
1223 }
1224 40 $result->reason( %$_ ) for @{ $data->{'reason'} };
1225 # As Mail::DMARC::Policy->parse builds it, without the stricter validation of new()
1226 40 $result->published( bless { %{ $data->{'published'} } }, 'Mail::DMARC::Policy' );
1227 40 return $report;
1228 }
1229  
1230 2 ▶sub dequeue_callback {
Subroutine dequeue_callback: called CC=18 SCAR=44.3
Pod undocumented
1231 2 my ($self) = @_;
1232 2 my $dequeue_list = $self->get_dequeue_list('dmarc_report');
1233 REPORT:
1234 2 for my $id ( $dequeue_list->@* ) {
1235 38 my $report = eval { $self->_dmarc_report_from_queue( $self->get_dequeue($id) ) };
1236  
1237 38 ▶ unless ($report) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($report) 0 38 38
1238 0 ▶ my $error = $@ ? ": $@" : q{};
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
1239 0 $self->log_error("DMARC Report dequeue failed for $id$error");
1240 0 $self->error_dequeue($id);
1241 0 next REPORT;
1242 }
1243  
1244 38 my $save_report = 1;
1245 38 eval {
1246 38 $self->set_handler_alarm( 5 * 1000000 ); # Allow no longer than 5 seconds for this!
1247  
1248 38 my $domain = $report->{ 'header_from' };
1249 38 my $org_domain = eval{ $report->get_organizational_domain( $domain ) };
1250 38 my $rua = $report->result()->published()->rua();
1251  
1252 38 my ( $kept_rua, $bad_rua ) = $self->_filter_rua($rua);
1253 38 ▶ if ( @$bad_rua ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$bad_rua) 0 38 38
1254 0 $self->dbgout( 'DMARC Report rua has malformed entries', "$domain, $rua, bad: @$bad_rua", LOG_INFO );
1255 }
1256 38 ▶ if ( ! @$kept_rua ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @$kept_rua) { } 0 38 38
elsif (@$bad_rua) { } 0 38 38
1257 0 $self->dbgout( 'DMARC Report skipped, no valid rua', "$domain, $rua", LOG_INFO );
1258 0 $save_report = 0;
1259 }
1260 elsif ( @$bad_rua ) {
1261 0 my $cleaned_rua = join ',', @$kept_rua;
1262 0 $report->result()->published()->rua($cleaned_rua);
1263 0 $self->dbgout( 'DMARC Report rua rewritten', "$domain, $rua -> $cleaned_rua", LOG_INFO );
1264 }
1265  
1266 38 my $config = $self->handler_config();
1267 38 ▶ if ( exists ( $config->{ 'report_suppression_list' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"report_suppression_list"}) 0 38 38
1268 0 ▶ if ( $self->rbl_check_domain( $org_domain, $config->{ 'report_suppression_list' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rbl_check_domain($org_domain, $config->{"report_suppression_list"})) 0 0 0
1269 0 $self->dbgout( 'Queued DMARC Report suppressed for', "$domain, $rua", LOG_INFO );
1270 0 $save_report = 0;
1271 }
1272 }
1273  
1274 38 ▶ if ( exists ( $config->{ 'report_suppression_email_list' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"report_suppression_email_list"}) 0 38 38
1275 RUA:
1276 0 for my $rua_entry (split /,/ , $rua) {
1277 0 $rua_entry = lc $rua_entry;
1278 0 $rua_entry =~ s/ //g;
1279 0 ▶ next RUA unless $rua_entry =~ /^mailto:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rua_entry =~ /^mailto:/u 0 0 0
1280 0 $rua_entry =~ s/^mailto://;
1281 0 ▶ if ( $self->rbl_check_email_address( $rua_entry, $config->{ 'report_suppression_email_list' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rbl_check_email_address($rua_entry, $config->{"report_suppression_email_list"})) 0 0 0
1282 0 $self->dbgout( 'Queued DMARC Report suppressed for', "$domain, $rua :: $rua_entry listed", LOG_INFO );
1283 0 $save_report = 0;
1284 }
1285 }
1286 }
1287  
1288 38 ▶ if ($save_report) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save_report) 38 0 38
1289 38 $report->save_aggregate();
1290 38 $self->dbgout( 'Queued DMARC Report saved for', "$domain, $rua", LOG_INFO );
1291 }
1292 38 $self->delete_dequeue($id);
1293 38 $self->reset_alarm();
1294 };
1295 38 ▶ if ( my $Error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $Error = $@) 1 37 38
1296 1 $self->reset_alarm();
1297 0 my $Type = $self->is_exception_type( $Error );
1298 0 ▶ if ( $Type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Type) 0 0 0
1299 0 ▶ if ( $Type eq 'Timeout' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Type eq "Timeout") 0 0 0
1300 # We have a timeout, is it global or is it ours?
1301 0 ▶ if ( $self->get_time_remaining() > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_time_remaining > 0) { } 0 0 0
1302 # We have time left, but this aggregate save timed out
1303 # Log this and move on!
1304 0 $self->log_error("DMARC timeout saving reports for $id");
1305 }
1306 else {
1307 0 $self->handle_exception( $Error );
1308 }
1309 }
1310 }
1311 0 $self->log_error("DMARC Report save failed for $id: $Error");
1312 0 $self->error_dequeue($id);
1313 }
1314  
1315 }
1316 }
1317  
1318 120 ▶sub _save_aggregate_reports {
Subroutine _save_aggregate_reports: called CC=7 SCAR=26.2
1319 120 my ( $self ) = @_;
1320 120 ▶ return if ! $self->{'report_queue'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"report_queue"} 43 77 120
1321 # Try as best we can to save a report, but don't stress if it fails.
1322 77 eval {
1323 77 $self->set_handler_alarm( 2 * 1000000 ); # Allow no longer than 2 seconds for this!
1324 77 ▶ while ( my $report = shift @{ $self->{'report_queue'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $report = shift @{$$self{"report_queue"};}) 58 77 135
1325 58 $self->add_dequeue('dmarc_report',$self->_dmarc_report_to_queue($report));
1326 58 $self->dbgout( 'DMARC Report queued for', $report->result()->published()->rua(), LOG_INFO );
1327 }
1328 77 $self->reset_alarm();
1329 };
1330 77 ▶ if ( my $Error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $Error = $@) 0 77 77
1331 0 $self->reset_alarm();
1332 0 my $Type = $self->is_exception_type( $Error );
1333 0 ▶ if ( $Type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Type) 0 0 0
1334 0 ▶ if ( $Type eq 'Timeout' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Type eq "Timeout") 0 0 0
1335 # We have a timeout, is it global or is it ours?
1336 0 ▶ if ( $self->get_time_remaining() > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_time_remaining > 0) 0 0 0
1337 # We have time left, but the aggregate save timed out
1338 # Log this and move on!
1339 0 $self->log_error( 'DMARC timeout saving reports' );
1340 0 return;
1341 }
1342 }
1343 }
1344 0 $self->handle_exception( $Error );
1345 0 $self->log_error( 'DMARC Report Error ' . $Error );
1346 }
1347 }
1348  
1349 120 ▶sub close_callback {
Subroutine close_callback: called CC=1 SCAR=0.0
Pod undocumented
1350 120 my ( $self ) = @_;
1351 120 $self->_save_aggregate_reports();
1352 120 delete $self->{'helo_name'};
1353 120 delete $self->{'env_from'};
1354 120 delete $self->{'env_to'};
1355 120 delete $self->{'failmode'};
1356 120 delete $self->{'skip_report'};
1357 120 delete $self->{'is_list'};
1358 120 delete $self->{'from_header'};
1359 120 delete $self->{'from_headers'};
1360 120 delete $self->{'report_queue'};
1361 120 delete $self->{'processed'};
1362 120 delete $self->{'from_lookup_count'};
1363 120 delete $self->{'strict_multifrom_disposition'};
1364 120 $self->destroy_object('dmarc');
1365 120 $self->destroy_object('dmarc_result');
1366 120 $self->destroy_object('dmarc_results');
1367 }
1368  
1369 42 1;
1370  
1371 __END__
« Authentication/Resolver.pm ↑ Summary Authentication.pm »