blib/lib/Mail/Milter/Authentication/Handler/ARC.pm

filter: Statementstmt 10.0% 53 / 527 Branchbran 0.0% 0 / 188 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 42.5% 17 / 40 Podpod 4.7% 1 / 21 totaltotal 8.5% 71 / 832 CC 133 SCAR 49.3
CC
133
Cov
7%
inherit_trusted_dkim_results
342.0
inherit_trusted_spf_results
272.0
addheader_callback
240.0
CRAP 138.2
↑ Summary
1 package Mail::Milter::Authentication::Handler::ARC;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mail::Milter::Authentication 2.20180903;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = '2.20181013'; # VERSION
7 # ABSTRACT: Authentication Milter Module for validation of ARC signatures
8 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Clone qw{ clone };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use English qw{ -no_match_vars };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Sys::Syslog qw{:standard :macros};
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Mail::DKIM 0.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Mail::DKIM::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Mail::DKIM::TextWrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Mail::DKIM::ARC::Signer;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Mail::DKIM::ARC::Verifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Mail::AuthenticationResults 1.20180518;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Mail::AuthenticationResults::Header::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Mail::AuthenticationResults::Header::SubEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Mail::AuthenticationResults::Header::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 sub default_config {
24 return {
25 0 ▶ 'hide_none' => 0,
Subroutine default_config: not called CC=1 SCAR=6.9
Pod undocumented
26 'arcseal_domain' => undef,
27 'arcseal_selector' => undef,
28 'arcseal_algorithm' => 'rsa-sha256',
29 'arcseal_key' => undef,
30 'arcseal_keyfile' => undef,
31 'arcseal_headers' => undef,
32 'trusted_domains' => [],
33 'rbl_whitelist' => '',
34 'no_strict' => 0,
35 };
36 }
37  
38 sub grafana_rows {
39 0 ▶ my ( $self ) = @_;
Subroutine grafana_rows: not called CC=1 SCAR=6.9
Pod undocumented
40 0 my @rows;
41 0 push @rows, $self->get_json( 'ARC_metrics' );
42 0 return \@rows;
43 }
44  
45 sub register_metrics {
46 return {
47 0 ▶ 'arc_total' => 'The number of emails processed for ARC',
Subroutine register_metrics: not called CC=1 SCAR=6.9
Pod documented
48 'arc_signatures' => 'The number of signatures processed for ARC',
49 'arcseal_total' => 'The number of ARC seals added',
50 };
51 }
52  
53 sub is_domain_trusted {
54 0 ▶ my ( $self, $domain ) = @_;
Subroutine is_domain_trusted: not called CC=7 SCAR=40.3
Pod undocumented
55 0 ▶ return 0 if ! defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain 0 0 0
56 0 $domain = lc $domain;
57 0 my $config = $self->handler_config();
58  
59 0 my $trusted_domains = $config->{ 'trusted_domains' };
60 0 ▶ if ( $trusted_domains ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trusted_domains) 0 0 0
61 0 foreach my $trusted_domain ( @$trusted_domains ) {
62 0 ▶ if ( $domain eq lc $trusted_domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain eq lc $trusted_domain) 0 0 0
63 #$self->dbgout( 'ARCResult', 'ARC domain trusted by static list', LOG_INFO );
64 0 return 1;
65 }
66 }
67 }
68  
69 0 my $rbl_whitelist = $config->{ 'rbl_whitelist' };
70 0 ▶ if ( $rbl_whitelist ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rbl_whitelist) 0 0 0
71 0 ▶ if ( $self->rbl_check_domain( $domain, $rbl_whitelist ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rbl_check_domain($domain, $rbl_whitelist)) 0 0 0
72 #$self->dbgout( 'ARCResult', 'ARC domain trusted by dns list', LOG_INFO );
73 0 return 1;
74 }
75 }
76  
77 0 return 0;
78 }
79  
80 sub get_trusted_spf_results {
81 0 ▶ my ( $self ) = @_;
Subroutine get_trusted_spf_results: not called CC=6 SCAR=37.4
Pod undocumented
82  
83 0 my $aar = $self->get_trusted_arc_authentication_results();
84 0 ▶ return if ! $aar;
Branch: 1 branch
BranchTrueFalseTotal
unless $aar 0 0 0
85  
86 0 my @trusted_results;
87  
88 0 foreach my $instance ( sort keys %$aar ) {
89 0 eval {
90 0 my $results = $aar->{$instance}->search({ 'isa' => 'entry', 'key' => 'spf' })->children();
91 RESULT:
92 0 foreach my $result ( @$results ) {
93 0 my $smtp_mailfrom = eval{ $result->search({ 'isa' => 'subentry', 'key' => 'smtp.mailfrom' })->children()->[0]->value() };
94 0 $self->handle_exception( $@ );
95 0 ▶ next RESULT if ! $smtp_mailfrom;
Branch: 1 branch
BranchTrueFalseTotal
unless $smtp_mailfrom 0 0 0
96 0 my $result_domain = $self->get_domain_from( $smtp_mailfrom );
97 0 push @trusted_results, {
98 'domain' => $result_domain,
99 'scope' => 'mfrom',
100 'result' => $result->value(),
101 };
102 }
103 };
104 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
105 0 $self->handle_exception( $error );
106 0 $self->log_error( 'ARC Inherit Error ' . $error );
107 }
108 }
109 0 return \@trusted_results;
110 }
111  
112 sub get_trusted_dkim_results {
113 0 ▶ my ( $self ) = @_;
Subroutine get_trusted_dkim_results: not called CC=11 SCAR=48.8
Pod undocumented
114  
115 0 my $aar = $self->get_trusted_arc_authentication_results();
116 0 ▶ return if ! $aar;
Branch: 1 branch
BranchTrueFalseTotal
unless $aar 0 0 0
117  
118 0 my @trusted_results;
119  
120 0 foreach my $instance ( sort keys %$aar ) {
121 0 eval {
122 0 my $results = $aar->{$instance}->search({ 'isa' => 'entry', 'key' => 'dkim' })->children();
123 RESULT:
124 0 foreach my $result ( @$results ) {
125 0 my $entry_domain = eval{ $result->search({ 'isa' => 'subentry', 'key' => 'header.d' })->children()->[0]->value() };
126 0 $self->handle_exception( $@ );
127 0 ▶ if ( ! $entry_domain ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry_domain) 0 0 0
128 # No domain, check for an identifier instead
129 0 my $entry_domain = eval{ $result->search({ 'isa' => 'subentry', 'key' => 'header.i' })->children()->[0]->value() };
130 0 $self->handle_exception( $@ );
131 0 ▶ if ( $entry_domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry_domain) 0 0 0
132 0 $entry_domain =~ s/^.*\@//;
133 }
134 }
135 0 ▶ next RESULT if ! $entry_domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry_domain 0 0 0
136 0 $entry_domain = lc $entry_domain;
137  
138 0 my $entry_selector = eval{ $result->search({ 'isa' => 'subentry', 'key' => 'x-selector' })->children()->[0]->value() };
139 0 $self->handle_exception( $@ );
140 0 ▶ if ( ! $entry_selector ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry_selector) 0 0 0
141 # Google are using header.s
142 0 $entry_selector = eval{ $result->search({ 'isa' => 'subentry', 'key' => 'header.s' })->children()->[0]->value() };
143 0 $self->handle_exception( $@ );
144 }
145 # If we don't have a selector then we fake it.
146 0 ▶ $entry_selector = 'x-arc-chain' if ! $entry_selector;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry_selector 0 0 0
147 ## TODO If we can't find this in the ar header then we could
148 ## try looking for the Signature and pull it from there.
149 ## But let's not do that right now.
150 0 ▶ next RESULT if ! $entry_selector;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry_selector 0 0 0
151  
152 #my $result_domain = $self->get_domain_from( $smtp_mailfrom );
153 0 push @trusted_results, {
154 'domain' => $entry_domain,
155 'selector' => $entry_selector,,
156 'result' => $result->value(),
157 'human_result' => 'Trusted ARC entry',
158 };
159 }
160 };
161 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
162 0 $self->handle_exception( $error );
163 0 $self->log_error( 'ARC Inherit Error ' . $error );
164 }
165 }
166 0 return \@trusted_results;
167 }
168  
169 sub inherit_trusted_spf_results {
170 0 ▶ my ( $self ) = @_;
Subroutine inherit_trusted_spf_results: not called CC=16 SCAR=56.1
Pod undocumented
171  
172 0 ▶ return if ( ! $self->is_handler_loaded( 'SPF' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_handler_loaded("SPF") 0 0 0
173  
174 0 my $aar = $self->get_trusted_arc_authentication_results();
175 0 ▶ return if ! $aar;
Branch: 1 branch
BranchTrueFalseTotal
unless $aar 0 0 0
176  
177 0 foreach my $instance ( sort keys %$aar ) {
178 0 eval {
179 # Find all ARC SPF results which passed
180 0 my $results = $aar->{$instance}->search({ 'isa' => 'entry', 'key' => 'spf', 'value' => 'pass' })->children();
181 RESULT:
182 0 foreach my $result ( @$results ) {
183  
184 # Does the entry have an x-arc-domain entry? if do then leave it alone.
185 0 ▶ next RESULT if ( scalar @{ $result->search({ 'isa' => 'subentry', 'key' => 'x-arc-domain' })->children() }> 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$result->search({"isa", "subentry", "key", "x-arc-domain"})->children;} > 0 0 0 0
186  
187 # Does the entry have a smtp.mailfrom entry we can match on?
188 0 my $smtp_mailfrom = eval{ $result->search({ 'isa' => 'subentry', 'key' => 'smtp.mailfrom' })->children()->[0]->value() };
189 0 $self->handle_exception( $@ );
190 0 ▶ next RESULT if ! $smtp_mailfrom;
Branch: 1 branch
BranchTrueFalseTotal
unless $smtp_mailfrom 0 0 0
191 0 $smtp_mailfrom = lc $smtp_mailfrom;
192  
193 # Do we have an existing entry for this spf record with the same smtp.mailfrom?
194 0 my $top_handler = $self->get_top_handler();
195 0 my $existing_auth_headers = $top_handler->{'auth_headers'};
196 0 my $found_passing = 0;
197  
198 HEADER:
199 0 foreach my $header ( @$existing_auth_headers ) {
200 0 ▶ next if $header->key() ne 'spf';
Branch: 1 branch
BranchTrueFalseTotal
if $header->key ne "spf" 0 0 0
201  
202 0 my $quoted = quotemeta($smtp_mailfrom);
203 0 my $regex = qr{$quoted}i;
204 0 my $this_mailfrom = eval{ $header->search({ 'isa' => 'subentry', 'key' => 'smtp.mailfrom', 'value' => $regex })->children()->[0]->value() };
205 0 $self->handle_exception( $@ );
206 0 ▶ next HEADER if ! $this_mailfrom;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_mailfrom 0 0 0
207  
208 # We already have a pass, leave it alone
209 0 ▶ $found_passing = 1 if $header->value() eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $header->value eq "pass" 0 0 0
210  
211 }
212  
213 # We found a passing result for this mailfrom, leave it alone
214 0 ▶ next RESULT if $found_passing;
Branch: 1 branch
BranchTrueFalseTotal
if $found_passing 0 0 0
215  
216 # We didn't find a passing result, so rename the existing ones.....
217 HEADER:
218 0 foreach my $header ( @$existing_auth_headers ) {
219 0 ▶ next if $header->key() ne 'spf';
Branch: 1 branch
BranchTrueFalseTotal
if $header->key ne "spf" 0 0 0
220  
221 0 my $quoted = quotemeta($smtp_mailfrom);
222 0 my $regex = qr{$quoted}i;
223 0 my $this_mailfrom = eval{ $header->search({ 'isa' => 'subentry', 'key' => 'smtp.mailfrom', 'value' => $regex })->children()->[0]->value() };
224 0 $self->handle_exception( $@ );
225 0 ▶ next HEADER if ! $this_mailfrom;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_mailfrom 0 0 0
226  
227 # Rename the existing header
228 0 $header->set_key( 'x-local-spf' );
229 }
230  
231 # And add the new one
232 0 $result->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-arc-instance' )->safe_set_value( $instance ) );
233 0 $result->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-arc-domain' )->safe_set_value( $self->{ 'arc_domain'}->{ $instance } ) );
234 0 $result->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'Trusted from aar.' . $instance . '.' . $self->{ 'arc_domain' }->{ $instance } ) );
235 0 $result->orphan();
236 0 $self->add_auth_header( $result );
237  
238 }
239 };
240 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
241 0 $self->handle_exception( $error );
242 0 $self->log_error( 'ARC Inherit Error ' . $error );
243 }
244 }
245 0 return;
246 }
247  
248 sub inherit_trusted_dkim_results {
249 0 ▶ my ( $self ) = @_;
Subroutine inherit_trusted_dkim_results: not called CC=18 SCAR=58.3
Pod undocumented
250  
251 0 ▶ return if ( ! $self->is_handler_loaded( 'DKIM' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_handler_loaded("DKIM") 0 0 0
252  
253 0 my $aar = $self->get_trusted_arc_authentication_results();
254 0 ▶ return if ! $aar;
Branch: 1 branch
BranchTrueFalseTotal
unless $aar 0 0 0
255  
256 0 foreach my $instance ( sort keys %$aar ) {
257 0 eval {
258 # Find all ARC DKIM results which passed
259 0 my $results = $aar->{$instance}->search({ 'isa' => 'entry', 'key' => 'dkim', 'value' => 'pass' })->children();
260 RESULT:
261 0 foreach my $result ( @$results ) {
262  
263 # Does the entry have an x-arc-domain entry? if do then leave it alone.
264 0 ▶ next RESULT if ( scalar @{ $result->search({ 'isa' => 'subentry', 'key' => 'x-arc-domain' })->children() }> 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$result->search({"isa", "subentry", "key", "x-arc-domain"})->children;} > 0 0 0 0
265  
266 # Does the entry have a domain identifier we can match on?
267 0 my $entry_domain = eval{ $result->search({ 'isa' => 'subentry', 'key' => 'header.d' })->children()->[0]->value() };
268 0 $self->handle_exception( $@ );
269 0 ▶ if ( ! $entry_domain ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry_domain) 0 0 0
270 # No domain, check for an identifier instead
271 0 my $entry_domain = eval{ $result->search({ 'isa' => 'subentry', 'key' => 'header.i' })->children()->[0]->value() };
272 0 $self->handle_exception( $@ );
273 0 ▶ if ( $entry_domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry_domain) 0 0 0
274 0 $entry_domain =~ s/^.*\@//;
275 }
276 }
277 0 ▶ next RESULT if ! $entry_domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry_domain 0 0 0
278 0 $entry_domain = lc $entry_domain;
279  
280 # Do we have an existing entry for this spf record with the same domain?
281 0 my $top_handler = $self->get_top_handler();
282 0 my $existing_auth_headers = $top_handler->{'auth_headers'};
283 0 my $found_passing = 0;
284  
285 HEADER:
286 0 foreach my $header ( @$existing_auth_headers ) {
287 0 ▶ next if $header->key() ne 'dkim';
Branch: 1 branch
BranchTrueFalseTotal
if $header->key ne "dkim" 0 0 0
288  
289 0 my $quoted = quotemeta($entry_domain);
290 0 my $regex = qr{$quoted}i;
291 0 my $this_domain = eval{ $header->search({ 'isa' => 'subentry', 'key' => 'header.d', 'value' => $regex })->children()->[0]->value() };
292 0 $self->handle_exception( $@ );
293 0 ▶ next HEADER if ! $this_domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_domain 0 0 0
294  
295 # We already have a pass, leave it alone
296 0 ▶ $found_passing = 1 if $header->value() eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $header->value eq "pass" 0 0 0
297  
298 }
299  
300 # We found a passing result for this mailfrom, leave it alone
301 0 ▶ next RESULT if $found_passing;
Branch: 1 branch
BranchTrueFalseTotal
if $found_passing 0 0 0
302  
303 # We didn't find a passing result, so rename the existing ones.....
304 HEADER:
305 0 foreach my $header ( @$existing_auth_headers ) {
306 0 ▶ next if $header->key() ne 'dkim';
Branch: 1 branch
BranchTrueFalseTotal
if $header->key ne "dkim" 0 0 0
307  
308 0 my $quoted = quotemeta($entry_domain);
309 0 my $regex = qr{$quoted}i;
310 0 my $this_domain = eval{ $header->search({ 'isa' => 'subentry', 'key' => 'header.d', 'value' => $regex })->children()->[0]->value() };
311 0 $self->handle_exception( $@ );
312 0 ▶ next HEADER if ! $this_domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_domain 0 0 0
313  
314 # Rename the existing header
315 0 $header->set_key( 'x-local-dkim' );
316 }
317  
318 # And add the new one
319 0 $result->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-arc-instance' )->safe_set_value( $instance ) );
320 0 $result->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-arc-domain' )->safe_set_value( $self->{ 'arc_domain'}->{ $instance } ) );
321 0 $result->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'Trusted from aar.' . $instance . '.' . $self->{ 'arc_domain' }->{ $instance } ) );
322 0 $result->orphan();
323 0 $self->add_auth_header( $result );
324  
325 }
326 };
327 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
328 0 $self->handle_exception( $error );
329 0 $self->log_error( 'ARC Inherit Error ' . $error );
330 }
331 }
332 0 return;
333 }
334  
335 sub inherit_trusted_ip_results {
336 0 ▶ my ( $self ) = @_;
Subroutine inherit_trusted_ip_results: not called CC=6 SCAR=37.4
Pod undocumented
337  
338 0 my $aar = $self->get_trusted_arc_authentication_results();
339 0 ▶ return if ! $aar;
Branch: 1 branch
BranchTrueFalseTotal
unless $aar 0 0 0
340  
341 # Add result from first trusted ingress hop
342 0 my ( $instance ) = sort keys %$aar;
343 0 foreach my $thing ( sort qw { iprev x-ptr } ) {
344 0 eval {
345 0 my $results = $aar->{$instance}->search({ 'isa' => 'entry', 'key' => $thing })->children();
346 RESULT:
347 0 foreach my $result ( @$results ) {
348 0 ▶ next RESULT if ( scalar @{ $result->search({ 'isa' => 'subentry', 'key' => 'x-arc-domain' })->children() }> 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$result->search({"isa", "subentry", "key", "x-arc-domain"})->children;} > 0 0 0 0
349 0 $result->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-arc-instance' )->safe_set_value( $instance ) );
350 0 $result->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-arc-domain' )->safe_set_value( $self->{ 'arc_domain'}->{ $instance } ) );
351 0 $result->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'Trusted from aar.' . $instance . '.' . $self->{ 'arc_domain' }->{ $instance } ) );
352 0 $result->orphan();
353 0 $self->add_auth_header( $result );
354 }
355 };
356 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
357 0 $self->handle_exception( $error );
358 0 $self->log_error( 'ARC Inherit Error ' . $error );
359 }
360 }
361  
362 0 return;
363 }
364  
365 sub get_trusted_arc_authentication_results {
366 0 ▶ my ( $self ) = @_;
Subroutine get_trusted_arc_authentication_results: not called CC=6 SCAR=37.4
Pod undocumented
367  
368 # First, we need an arc pass or we trust nothing!
369 0 ▶ return if $self->{ 'arc_result' } ne 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"arc_result"} ne "pass" 0 0 0
370  
371 0 my $trusted_aar = {};
372 INSTANCE:
373 0 foreach my $instance ( reverse sort keys %{$self->{ 'arc_auth_results' } } ) {
374 0 ▶ my $signature_domain = $self->{'arc_domain'}->{ $instance } // q{};
Condition0%0/2
$self->{"arc_domain"}{$instance} // ""
l!l
00
MC/DC0%0/1
$self->{"arc_domain"}{$instance} // ""
$self->{"arc_domain"}{$instance}
Truth table0%0/2
A
A= $self->{"arc_domain"}{$instance}
Aresult
11
00
375 0 ▶ if ( $self->is_domain_trusted( $signature_domain ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_domain_trusted($signature_domain)) { } 0 0 0
376 # Clone this, so we can safely modify entries later
377 0 $trusted_aar->{ $instance } = clone $self->{ 'arc_auth_results' }->{ $instance };
378 }
379 else {
380 # We don't trust this host, we can't trust anything before it!
381 0 last INSTANCE;
382 }
383 }
384  
385 0 ▶ if ( scalar keys %$trusted_aar == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$trusted_aar == 0) 0 0 0
386 0 return;
387 }
388 0 return $trusted_aar;
389 }
390  
391 # Do we trust the entire chain
392 sub is_chain_trusted {
393 0 ▶ my ( $self ) = @_;
Subroutine is_chain_trusted: not called CC=5 SCAR=34.0
Pod undocumented
394 0 ▶ return 0 if $self->{ 'arc_result' } ne 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"arc_result"} ne "pass" 0 0 0
395 0 foreach my $instance ( reverse sort keys %{$self->{ 'arc_auth_results' } } ) {
396 0 ▶ my $signature_domain = $self->{'arc_domain'}->{ $instance } // q{};
Condition0%0/2
$self->{"arc_domain"}{$instance} // ""
l!l
00
MC/DC0%0/1
$self->{"arc_domain"}{$instance} // ""
$self->{"arc_domain"}{$instance}
Truth table0%0/2
A
A= $self->{"arc_domain"}{$instance}
Aresult
11
00
397 0 ▶ return 0 if ! $self->is_domain_trusted( $signature_domain );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_domain_trusted($signature_domain) 0 0 0
398 }
399 0 return 1;
400 }
401  
402 # Get the trusted ingress IP
403 sub get_arc_trusted_ingress_ip {
404 0 ▶ my ( $self ) = @_;
Subroutine get_arc_trusted_ingress_ip: not called CC=6 SCAR=37.4
Pod undocumented
405 0 my $aar = $self->get_trusted_arc_authentication_results();
406 0 ▶ return if ! $aar;
Branch: 1 branch
BranchTrueFalseTotal
unless $aar 0 0 0
407 0 my ( $first_instance ) = sort keys %$aar;
408 0 ▶ return if ! $first_instance;
Branch: 1 branch
BranchTrueFalseTotal
unless $first_instance 0 0 0
409  
410 0 my $ip;
411  
412 0 $ip = eval{ $aar->{$first_instance}->search({ 'isa' => 'entry', 'key' => 'iprev' })->children()->[0]->search({ 'isa' => 'subentry', 'key' => 'smtp.remote-ip'})->children()->[0]->value(); };
413 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
414 0 $self->handle_exception( $error );
415 0 $self->log_error( 'ARC Inherit Error ' . $error );
416 }
417 0 ▶ return $ip if $ip;
Branch: 1 branch
BranchTrueFalseTotal
if $ip 0 0 0
418  
419 0 $ip = eval{ $aar->{$first_instance}->search({ 'isa' => 'entry', 'key' => 'iprev' })->children()->[0]->search({ 'isa' => 'subentry', 'key' => 'policy.iprev'})->children()->[0]->value(); };
420 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
421 0 $self->handle_exception( $error );
422 0 $self->log_error( 'ARC Inherit Error ' . $error );
423 }
424 0 return $ip;
425 }
426  
427 # Find the earliest instance in the trusted chain
428 sub search_trusted_aar {
429 0 ▶ my ( $self, $search ) = @_;
Subroutine search_trusted_aar: not called CC=4 SCAR=30.0
Pod undocumented
430 0 my $trusted_aar = $self->get_trusted_arc_authentication_results();
431 0 ▶ return if ! $trusted_aar;
Branch: 1 branch
BranchTrueFalseTotal
unless $trusted_aar 0 0 0
432 0 foreach my $instance ( sort keys %{$trusted_aar} ) {
433 0 my $found = $trusted_aar->{ $instance }->search( $search );
434 0 ▶ if ( scalar @{ $found->children() } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$found->children;}) 0 0 0
435 0 return $found;
436 }
437 }
438 0 return;
439 }
440  
441 sub envfrom_callback {
442 0 ▶ my ( $self, $env_from ) = @_;
Subroutine envfrom_callback: not called CC=1 SCAR=6.9
Pod undocumented
443 0 $self->{'failmode'} = 0;
444 0 $self->{'headers'} = [];
445 0 $self->{'body'} = [];
446 0 $self->{'has_arc'} = 0;
447 0 $self->{'valid_domains'} = {};
448 0 $self->{'carry'} = q{};
449 0 $self->{'arc_auth_results'} = {};
450 0 $self->{'arc_domain'} = {};
451 0 $self->destroy_object('arc');
452 0 return;
453 }
454  
455 sub header_callback {
456 0 ▶ my ( $self, $header, $value ) = @_;
Subroutine header_callback: not called CC=4 SCAR=30.0
Pod undocumented
457 0 my $EOL = "\015\012";
458 0 my $arc_chunk = $header . ': ' . $value . $EOL;
459 0 $arc_chunk =~ s/\015?\012/$EOL/g;
460 0 push @{$self->{'headers'}} , $arc_chunk;
461  
462 0 ▶ if ( lc($header) eq 'arc-authentication-results' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "arc-authentication-results") 0 0 0
463 0 $self->{'has_arc'} = 1;
464 0 my ( $instance, $aar ) = split( ';', $value, 2 );
465 0 $instance =~ s/.*i=(\d+).*$/$1/;
466 0 my $parsed = eval{ Mail::AuthenticationResults->parser()->parse( $aar ) };
467 0 $self->handle_exception( $@ );
468 0 $self->{'arc_auth_results'}->{ $instance } = $parsed;
469 }
470  
471 0 ▶ if ( lc($header) eq 'arc-seal' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "arc-seal") 0 0 0
472 0 $self->{'has_arc'} = 1;
473 }
474  
475 0 ▶ if ( lc($header) eq 'arc-message-signature' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "arc-message-signature") 0 0 0
476 0 $self->{'has_arc'} = 1;
477 }
478  
479 0 return;
480 }
481  
482 sub eoh_callback {
483 0 ▶ my ($self) = @_;
Subroutine eoh_callback: not called CC=13 SCAR=52.0
Pod undocumented
484 0 my $config = $self->handler_config();
485  
486 0 $self->{'carry'} = q{};
487  
488 0 ▶ if ($config->{arcseal_domain} and
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"arcseal_domain"} and $config->{"arcseal_selector"} and $config->{"arcseal_key"} || $config->{"arcseal_keyfile"}) 0 0 0
Condition0%0/9
$config->{"arcseal_domain"} and $config->{"arcseal_selector"} and $config->{"arcseal_key"} || $config->{"arcseal_keyfile"}
!ll&&!rl&&r
000
$config->{"arcseal_domain"} and $config->{"arcseal_selector"}
!ll&&!rl&&r
000
$config->{"arcseal_key"} or $config->{"arcseal_keyfile"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$config->{"arcseal_domain"} and $config->{"arcseal_selector"} and $config->{"arcseal_key"} || $config->{"arcseal_keyfile"}
$config->{"arcseal_domain"} $config->{"arcseal_selector"} $config->{"arcseal_key"} $config->{"arcseal_keyfile"}
Truth table0%0/5
A and B and C or D
A= $config->{"arcseal_domain"}B= $config->{"arcseal_selector"}C= $config->{"arcseal_key"}D= $config->{"arcseal_keyfile"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
489 $config->{arcseal_selector} and
490 ($config->{arcseal_key} || $config->{arcseal_keyfile}))
491 {
492 0 $self->{has_arcseal} = 1;
493 }
494  
495 0 ▶ unless ($self->{'has_arc'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"has_arc"}) 0 0 0
496 0 $self->metric_count( 'arc_total', { 'result' => 'none' } );
497 0 $self->dbgout( 'ARCResult', 'No ARC headers', LOG_INFO );
498 0 ▶ unless ($config->{'hide_none'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"}) 0 0 0
499 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( 'none' );
500 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'no signatures found' ) );
501 0 $self->add_auth_header( $header );
502 }
503 0 $self->{arc_result} = 'none';
504 0 ▶ delete $self->{headers} unless $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arcseal"} 0 0 0
505 0 return;
506 }
507  
508 0 my $arc;
509 0 eval {
510 0 my $UseStrict = 1;
511 0 ▶ if ( $config->{ 'no_strict' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"no_strict"}) 0 0 0
512 0 $UseStrict = 0;
513 }
514 0 $arc = Mail::DKIM::ARC::Verifier->new( 'Strict' => $UseStrict );
515 # The following requires Mail::DKIM > 0.4
516 0 my $resolver = $self->get_object('resolver');
517 0 Mail::DKIM::DNS::resolver($resolver);
518 0 $self->set_object('arc', $arc, 1);
519 };
520 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
521 0 $self->handle_exception( $error );
522 0 $self->log_error( 'ARC Setup Error ' . $error );
523 0 $self->_check_error( $error );
524 0 $self->metric_count( 'arc_total', { 'result' => 'error' } );
525 0 $self->{'failmode'} = 1;
526 0 $self->{arc_result} = 'fail'; # XXX - handle tempfail better
527 0 ▶ delete $self->{headers} unless $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arcseal"} 0 0 0
528 0 return;
529 }
530  
531 0 eval {
532 $arc->PRINT( join q{},
533 0 @{ $self->{'headers'} },
534 "\015\012",
535 );
536 };
537 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
538 0 $self->handle_exception( $error );
539 0 $self->log_error( 'ARC Headers Error ' . $error );
540 0 $self->_check_error( $error );
541 0 $self->metric_count( 'arc_total', { 'result' => 'error' } );
542 0 $self->{'failmode'} = 1;
543 0 $self->{arc_result} = 'fail'; # XXX - handle tempfail better
544 0 ▶ delete $self->{headers} unless $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arcseal"} 0 0 0
545 0 return;
546 }
547 }
548  
549 sub body_callback {
550 0 ▶ my ( $self, $body_chunk ) = @_;
Subroutine body_callback: not called CC=8 SCAR=42.8
Pod undocumented
551 0 my $EOL = "\015\012";
552  
553 0 my $arc_chunk;
554 0 ▶ if ( $self->{'carry'} ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'carry'} ne '') { } 0 0 0
555 0 $arc_chunk = $self->{'carry'} . $body_chunk;
556 0 $self->{'carry'} = q{};
557 }
558 else {
559 0 $arc_chunk = $body_chunk;
560 }
561  
562 0 ▶ if ( substr( $arc_chunk, -1 ) eq "\015" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($arc_chunk, -1) eq "\r") 0 0 0
563 0 $self->{'carry'} = "\015";
564 0 $arc_chunk = substr( $arc_chunk, 0, -1 );
565 }
566  
567 0 $arc_chunk =~ s/\015?\012/$EOL/g;
568 0 ▶ push @{$self->{body}}, $arc_chunk if $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"has_arcseal"} 0 0 0
569  
570 0 ▶ if ($self->{has_arc} and not $self->{failmode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"has_arc"} and not $self->{"failmode"}) 0 0 0
Condition0%0/3
$self->{"has_arc"} and not $self->{"failmode"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"has_arc"} and not $self->{"failmode"}
$self->{"has_arc"} not $self->{"failmode"}
Truth table0%0/3
A and not(B)
A= $self->{"has_arc"}B= not $self->{"failmode"}
ABresult
0X0
100
111
571 0 my $arc = $self->get_object('arc');
572 0 eval {
573 0 $arc->PRINT( $arc_chunk );
574 };
575 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
576 0 $self->handle_exception( $error );
577 0 $self->log_error( 'ARC Body Error ' . $error );
578 0 $self->_check_error( $error );
579 0 $self->metric_count( 'arc_total', { 'result' => 'error' } );
580 0 $self->{'failmode'} = 1;
581 0 $self->{arc_result} = 'fail'; # XXX - handle tempfail better
582 0 ▶ delete $self->{headers} unless $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arcseal"} 0 0 0
583 }
584 }
585 }
586  
587 sub eom_requires {
588 0 ▶ my ( $self ) = @_;
Subroutine eom_requires: not called CC=2 SCAR=17.9
Pod undocumented
589 0 my @requires;
590  
591 0 ▶ if ( $self->is_handler_loaded( 'DKIM' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_handler_loaded("DKIM")) 0 0 0
592 0 push @requires, 'DKIM';
593 }
594  
595 0 return \@requires;
596 }
597  
598 sub eom_callback {
599 0 ▶ my ($self) = @_;
Subroutine eom_callback: not called CC=14 SCAR=53.5
Pod undocumented
600  
601 0 ▶ push @{$self->{body}}, $self->{carry} if ($self->{carry} and $self->{has_arcseal});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"carry"} and $self->{"has_arcseal"} 0 0 0
Condition0%0/3
$self->{"carry"} and $self->{"has_arcseal"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"carry"} and $self->{"has_arcseal"}
$self->{"carry"} $self->{"has_arcseal"}
Truth table0%0/3
A and B
A= $self->{"carry"}B= $self->{"has_arcseal"}
ABresult
0X0
100
111
602  
603 # the rest of eom is only used for arc, not arcseal
604 0 ▶ return unless $self->{'has_arc'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arc"} 0 0 0
605 0 ▶ return if $self->{'failmode'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 0 0
606  
607 0 my $config = $self->handler_config();
608  
609 0 my $arc = $self->get_object('arc');
610  
611 0 eval {
612 0 $arc->PRINT( $self->{'carry'} );
613 0 $arc->CLOSE();
614 0 $self->check_timeout();
615  
616 0 my $arc_result = $arc->result;
617 0 my $arc_result_detail = $arc->result_detail;
618  
619 0 $self->metric_count( 'arc_total', { 'result' => $arc_result } );
620  
621 0 $self->dbgout( 'ARCResult', $arc_result_detail, LOG_INFO );
622  
623 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( $arc_result );
624  
625 0 my @items;
626 0 foreach my $signature ( @{ $arc->{signatures} } ) {
627 0 ▶ my $type =
Branch: 2 branches
BranchTrueFalseTotal
ref $signature eq 'Mail::DKIM::ARC::Seal' ? : 0 0 0
ref $signature eq 'Mail::DKIM::ARC::MessageSignature' ? : 0 0 0
628 ref($signature) eq 'Mail::DKIM::ARC::Seal' ? 'as'
629 : ref($signature) eq 'Mail::DKIM::ARC::MessageSignature' ? 'ams'
630 : ref($signature);
631 0 ▶ push @items,
Condition0%0/6
$signature->instance || ''
l!l
00
$signature->domain || '(none)'
l!l
00
$signature->result_detail || '?'
l!l
00
MC/DC0%0/1
$signature->instance || ''
$signature->instance
MC/DC0%0/1
$signature->domain || '(none)'
$signature->domain
MC/DC0%0/1
$signature->result_detail || '?'
$signature->result_detail
Truth table0%0/2
A
A= $signature->instance
Aresult
11
00
Truth table0%0/2
A
A= $signature->domain
Aresult
11
00
Truth table0%0/2
A
A= $signature->result_detail
Aresult
11
00
632 "$type."
633 . ( $signature->instance() || '' ) . '.'
634 . ( $signature->domain() || '(none)' ) . '='
635 . ( $signature->result_detail() || '?' );
636 0 $self->{ 'arc_domain' }->{ $signature->instance() } = $signature->domain();
637 }
638  
639 0 ▶ if ( @items ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items) 0 0 0
640 0 my $header_comment = Mail::AuthenticationResults::Header::Comment->new();
641 0 my $header_comment_text = join( ', ', @items );
642 # Try set_value first (required for potential nested comment), if this fails then
643 # set using safe_set_value
644 0 eval { $header_comment->set_value( $header_comment_text ); };
645 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
646 0 $self->handle_exception( $error );
647 0 $header_comment->safe_set_value( $header_comment_text );
648 }
649 0 $header->add_child( $header_comment );
650 }
651  
652 0 my $ip_address = $self->ip_address();
653 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.remote-ip' )->safe_set_value( $ip_address ) );
654  
655 0 $self->add_auth_header( $header );
656  
657 0 $self->{arc_result} = $arc_result;
658 };
659 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
660 0 $self->handle_exception( $error );
661 0 $self->log_error( 'ARC EOM Error ' . $error );
662 0 $self->_check_error( $error );
663 0 $self->metric_count( 'arc_total', { 'result' => 'error' } );
664 0 $self->{'failmode'} = 1;
665 0 $self->{arc_result} = 'fail';
666 }
667  
668 0 $self->inherit_trusted_spf_results();
669 0 $self->inherit_trusted_dkim_results();
670 0 $self->inherit_trusted_ip_results();
671  
672 0 return;
673  
674 }
675  
676 sub close_callback {
677 0 ▶ my ( $self ) = @_;
Subroutine close_callback: not called CC=1 SCAR=6.9
Pod undocumented
678 0 delete $self->{'failmode'};
679 0 delete $self->{'headers'};
680 0 delete $self->{'body'};
681 0 delete $self->{'carry'};
682 0 delete $self->{'has_arc'};
683 0 delete $self->{'valid_domains'};
684 0 delete $self->{'arc_domain'};
685 0 delete $self->{'arc_auth_results'};
686 0 $self->destroy_object('arc');
687 0 return;
688 }
689  
690 sub _check_error {
691 0 ▶ my ( $self, $error ) = @_;
Subroutine _check_error: not called CC=8 SCAR=42.8
692 0 ▶ if ( $error =~ /^DNS error: query timed out/
Branch: 3 branches
BranchTrueFalseTotal
if ($error =~ /^DNS error: query timed out/ or $error =~ /^DNS query timeout/) { } 0 0 0
elsif ($error =~ /^DNS error: SERVFAIL/) { } 0 0 0
elsif ($error =~ /^no domain to fetch policy for$/ or $error =~ /^policy syntax error$/ or $error =~ /^empty domain label/ or $error =~ /^invalid name /) { } 0 0 0
Condition0%0/3
$error =~ /^DNS error: query timed out/ or $error =~ /^DNS query timeout/
l!l&&r!l&&!r
000
MC/DC0%0/2
$error =~ /^DNS error: query timed out/ or $error =~ /^DNS query timeout/
$error =~ /^DNS error: query timed out/ $error =~ /^DNS query timeout/
Truth table0%0/3
A or B
A= $error =~ /^DNS error: query timed out/B= $error =~ /^DNS query timeout/
ABresult
1X1
011
000
693 or $error =~ /^DNS query timeout/
694 ){
695 0 $self->log_error( 'Temp ARC Error - ' . $error );
696 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( 'temperror' );
697 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'dns timeout' ) );
698 0 $self->add_auth_header( $header );
699 }
700 elsif ( $error =~ /^DNS error: SERVFAIL/ ){
701 0 $self->log_error( 'Temp ARC Error - ' . $error );
702 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( 'temperror' );
703 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'dns servfail' ) );
704 0 $self->add_auth_header( $header );
705 }
706 ▶ elsif ( $error =~ /^no domain to fetch policy for$/
Condition0%0/9
$error =~ /^no domain to fetch policy for$/ or $error =~ /^policy syntax error$/ or $error =~ /^empty domain label/ or $error =~ /^invalid name /
l!l&&r!l&&!r
000
$error =~ /^no domain to fetch policy for$/ or $error =~ /^policy syntax error$/ or $error =~ /^empty domain label/
l!l&&r!l&&!r
000
$error =~ /^no domain to fetch policy for$/ or $error =~ /^policy syntax error$/
l!l&&r!l&&!r
000
MC/DC0%0/4
$error =~ /^no domain to fetch policy for$/ or $error =~ /^policy syntax error$/ or $error =~ /^empty domain label/ or $error =~ /^invalid name /
$error =~ /^no domain to fetch policy for$/ $error =~ /^policy syntax error$/ $error =~ /^empty domain label/ $error =~ /^invalid name /
Truth table0%0/5
A or B or C or D
A= $error =~ /^no domain to fetch policy for$/B= $error =~ /^policy syntax error$/C= $error =~ /^empty domain label/D= $error =~ /^invalid name /
ABCDresult
1XXX1
01XX1
001X1
00011
00000
707 or $error =~ /^policy syntax error$/
708 or $error =~ /^empty domain label/
709 or $error =~ /^invalid name /
710 ){
711 0 $self->log_error( 'Perm ARC Error - ' . $error );
712 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( 'permerror' );
713 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'syntax or domain error' ) );
714 0 $self->add_auth_header( $header );
715 }
716 else {
717 0 $self->log_error( 'Unexpected ARC Error - ' . $error );
718 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( 'temperror' );
719 0 $self->add_auth_header( $header );
720 # Fill these in as they occur, but for unknowns err on the side of caution
721 # and tempfail/exit
722 0 $self->exit_on_close();
723 0 $self->tempfail_on_error();
724 }
725 0 return;
726 }
727  
728 sub _fmtheader {
729 0 ▶ my $header = shift;
Subroutine _fmtheader: not called CC=1 SCAR=6.9
730 0 my $value = $header->{value};
731 0 $value =~ s/\015?\012/\015\012/gs; # make sure line endings are right
732 0 return "$header->{field}: $value\015\012";
733 }
734  
735 sub addheader_callback {
736 0 ▶ my $self = shift;
Subroutine addheader_callback: not called CC=15 SCAR=54.8
Pod undocumented
737 0 my $handler = shift;
738  
739 0 ▶ return unless $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arcseal"} 0 0 0
740  
741 0 my $config = $self->handler_config();
742  
743 0 eval {
744 0 my %KeyOpts;
745 0 ▶ if ($config->{arcseal_keyfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'arcseal_keyfile'}) { } 0 0 0
746 0 $KeyOpts{KeyFile} = $config->{arcseal_keyfile};
747 }
748 else {
749 $KeyOpts{Key} = Mail::DKIM::PrivateKey->load(
750 0 Data => $config->{arcseal_key});
751 }
752 my $arcseal = Mail::DKIM::ARC::Signer->new(
753 Algorithm => $config->{arcseal_algorithm},
754 Domain => $config->{arcseal_domain},
755 SrvId => $self->get_my_hostname(),
756 Selector => $config->{arcseal_selector},
757 Headers => $config->{arcseal_headers},
758 # chain value is arc_result from previous seal validation
759 Chain => $self->{arc_result},
760 0 Timestamp => time(),
761 %KeyOpts,
762 );
763  
764 # pre-headers from handler (reversed as they will add in reverse)
765 0 ▶ foreach my $header (reverse @{$handler->{pre_headers} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$handler->{"pre_headers"} or [] 0 0 0
766 0 $arcseal->PRINT(_fmtheader($header));
767 }
768  
769 # then all the original headers: XXX - this doesn't deal with
770 # the change_header command, but only sanitize uses that.
771 # It would be a massive pain to make that work consistently,
772 # as it would need to modify the already cached headers in
773 # each handler with the current architecture
774 0 ▶ foreach my $chunk (@{$self->{headers} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"headers"} or [] 0 0 0
775 0 $arcseal->PRINT($chunk);
776 }
777  
778 # post-headers from handler (these are in order)
779 0 ▶ foreach my $header (@{$handler->{add_headers} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$handler->{"add_headers"} or [] 0 0 0
780 0 $arcseal->PRINT(_fmtheader($header));
781 0 $self->check_timeout();
782 }
783  
784 # finish header block with a blank line
785 0 $arcseal->PRINT("\015\012");
786  
787 # all the body chunks
788 0 foreach my $chunk (@{$self->{body}}) {
789 0 $arcseal->PRINT($chunk);
790 }
791  
792 # and we're done
793 0 $arcseal->CLOSE;
794 0 $self->check_timeout();
795  
796 0 my $arcseal_result = $arcseal->result();
797 0 my $arcseal_result_detail = $arcseal->result_detail();
798  
799 0 $self->metric_count( 'arcseal_total', { 'result' => $arcseal_result } );
800  
801 0 $self->dbgout( 'ARCSealResult', $arcseal_result_detail, LOG_INFO );
802  
803 # we need to extract the headers from ARCSeal and re-format them
804 # back to the format that pre_headers expects
805 0 my $headers = $arcseal->as_string();
806 0 my @list;
807  
808 0 my $current_header = q{};
809 0 my $current_value = q{};
810 0 foreach my $header_line ( (split ( /\015?\012/, $headers ) ) ) {
811 0 ▶ if ( $header_line =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_line =~ /^\s/) { } 0 0 0
812 # Line begins with whitespace, add to previous header
813 0 $header_line =~ s/^\s+/ /; # for consistency
814 0 $current_value .= "\n" . $header_line;
815 }
816 else {
817 # This is a brand new header!
818 0 ▶ if ( $current_header ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_header ne "") 0 0 0
819 # We have a cached header, add it now.
820 0 push @list, { 'field' => $current_header, 'value' => $current_value };
821 0 $current_value = q{};
822 }
823 0 ( $current_header, $current_value ) = split ( ':', $header_line, 2 );
824 0 $current_value =~ s/^ +//;
825 }
826 }
827 0 ▶ if ( $current_header ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_header ne "") 0 0 0
828 # We have a cached header, add it now.
829 0 push @list, { 'field' => $current_header, 'value' => $current_value };
830 0 $current_value = q{};
831 }
832  
833 # these will prepend in reverse
834 0 push @{$handler->{pre_headers}}, reverse @list;
835 };
836  
837 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
838 0 $self->handle_exception( $error );
839 0 $self->log_error( 'ARCSeal Error ' . $error );
840 0 $self->metric_count( 'arcseal_total', { 'result' => 'error' } );
841 0 return;
842 }
843 }
844  
845 1 1;
846  
847 __END__
848  
849 =pod
850  
851 =encoding UTF-8
852  
853 =head1 NAME
854  
855 Mail::Milter::Authentication::Handler::ARC - Authentication Milter Module for validation of ARC signatures
856  
857 =head1 VERSION
858  
859 version 2.20181013
860  
861 =head1 DESCRIPTION
862  
863 Module for validation of ARC signatures
864  
865 =head1 CONFIGURATION
866  
867 "ARC" : { | Config for the ARC Module
868 "hide_none" : 0, | Hide auth line if the result is 'none'
869 "arcseal_domain" : "example.com", | Domain to sign ARC Seal with (not sealed if blank)
870 "arcseal_selector" : undef, | Selector to use for ARC Seal (not sealed if blank)
871 "arcseal_algorithm" : 'rsa-sha256', | Algorithm to use on ARC Seal (default rsa-sha256)
872 "arcseal_key" : undef, | Key (base64) string to sign ARC Seal with; or
873 "arcseal_keyfile" : undef, | File containing ARC Seal key
874 "arcseal_headers" : undef, | Additional headers to cover in ARC-Message-Signature
875 "trusted_domains" : [], | Trust these domains when traversing ARC chains
876 "rbl_whitelist" : undef, | rhs list for looking up trusted signing domains
877 "no_strict" : 0, | Ignore rfc 8301 security considerations (not recommended)
878 },
879  
880 =head1 AUTHORS
881  
882 =over 4
883  
884 =item *
885  
886 Bron Gondwana <brong@fastmailteam.com>
887  
888 =item *
889  
890 Marc Bradshaw <marc@marcbradshaw.net>
891  
892 =back
893  
894 =head1 COPYRIGHT AND LICENSE
895  
896 This software is copyright (c) 2018 by FastMail Pty Ltd.
897  
898 This is free software; you can redistribute it and/or modify it under
899 the same terms as the Perl 5 programming language system itself.
900  
901 =cut
↑ Summary