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

filter: Statementstmt 10.2% 53 / 518 Branchbran 0.0% 0 / 184 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 42.5% 17 / 40 Podpod 4.7% 1 / 21 totaltotal 8.6% 71 / 819 SCAR 95.4
CC
131
Cov
7%
inherit_trusted_dkim_results
342.0
inherit_trusted_spf_results
272.0
addheader_callback
240.0
CRAP 13858.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.20180911'; # 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=4 SCAR=30.0
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 0 my $ip = eval{ $aar->{$first_instance}->search({ 'isa' => 'entry', 'key' => 'iprev' })->children()->[0]->search({ 'isa' => 'subentry', 'key' => 'policy.iprev'})->children()->[0]->value(); };
410 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
411 0 $self->handle_exception( $error );
412 0 $self->log_error( 'ARC Inherit Error ' . $error );
413 }
414 0 return $ip;
415 }
416  
417 # Find the earliest instance in the trusted chain
418 sub search_trusted_aar {
419 0 ▶ my ( $self, $search ) = @_;
Subroutine search_trusted_aar: not called CC=4 SCAR=30.0
Pod undocumented
420 0 my $trusted_aar = $self->get_trusted_arc_authentication_results();
421 0 ▶ return if ! $trusted_aar;
Branch: 1 branch
BranchTrueFalseTotal
unless $trusted_aar 0 0 0
422 0 foreach my $instance ( sort keys %{$trusted_aar} ) {
423 0 my $found = $trusted_aar->{ $instance }->search( $search );
424 0 ▶ if ( scalar @{ $found->children() } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$found->children;}) 0 0 0
425 0 return $found;
426 }
427 }
428 0 return;
429 }
430  
431 sub envfrom_callback {
432 0 ▶ my ( $self, $env_from ) = @_;
Subroutine envfrom_callback: not called CC=1 SCAR=6.9
Pod undocumented
433 0 $self->{'failmode'} = 0;
434 0 $self->{'headers'} = [];
435 0 $self->{'body'} = [];
436 0 $self->{'has_arc'} = 0;
437 0 $self->{'valid_domains'} = {};
438 0 $self->{'carry'} = q{};
439 0 $self->{'arc_auth_results'} = {};
440 0 $self->{'arc_domain'} = {};
441 0 $self->destroy_object('arc');
442 0 return;
443 }
444  
445 sub header_callback {
446 0 ▶ my ( $self, $header, $value ) = @_;
Subroutine header_callback: not called CC=4 SCAR=30.0
Pod undocumented
447 0 my $EOL = "\015\012";
448 0 my $arc_chunk = $header . ': ' . $value . $EOL;
449 0 $arc_chunk =~ s/\015?\012/$EOL/g;
450 0 push @{$self->{'headers'}} , $arc_chunk;
451  
452 0 ▶ if ( lc($header) eq 'arc-authentication-results' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "arc-authentication-results") 0 0 0
453 0 $self->{'has_arc'} = 1;
454 0 my ( $instance, $aar ) = split( ';', $value, 2 );
455 0 $instance =~ s/.*i=(\d+).*$/$1/;
456 0 my $parsed = eval{ Mail::AuthenticationResults->parser()->parse( $aar ) };
457 0 $self->handle_exception( $@ );
458 0 $self->{'arc_auth_results'}->{ $instance } = $parsed;
459 }
460  
461 0 ▶ if ( lc($header) eq 'arc-seal' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "arc-seal") 0 0 0
462 0 $self->{'has_arc'} = 1;
463 }
464  
465 0 ▶ if ( lc($header) eq 'arc-message-signature' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "arc-message-signature") 0 0 0
466 0 $self->{'has_arc'} = 1;
467 }
468  
469 0 return;
470 }
471  
472 sub eoh_callback {
473 0 ▶ my ($self) = @_;
Subroutine eoh_callback: not called CC=13 SCAR=52.0
Pod undocumented
474 0 my $config = $self->handler_config();
475  
476 0 $self->{'carry'} = q{};
477  
478 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
479 $config->{arcseal_selector} and
480 ($config->{arcseal_key} || $config->{arcseal_keyfile}))
481 {
482 0 $self->{has_arcseal} = 1;
483 }
484  
485 0 ▶ unless ($self->{'has_arc'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"has_arc"}) 0 0 0
486 0 $self->metric_count( 'arc_total', { 'result' => 'none' } );
487 0 $self->dbgout( 'ARCResult', 'No ARC headers', LOG_INFO );
488 0 ▶ unless ($config->{'hide_none'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"}) 0 0 0
489 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( 'none' );
490 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'no signatures found' ) );
491 0 $self->add_auth_header( $header );
492 }
493 0 $self->{arc_result} = 'none';
494 0 ▶ delete $self->{headers} unless $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arcseal"} 0 0 0
495 0 return;
496 }
497  
498 0 my $arc;
499 0 eval {
500 0 my $UseStrict = 1;
501 0 ▶ if ( $config->{ 'no_strict' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"no_strict"}) 0 0 0
502 0 $UseStrict = 0;
503 }
504 0 $arc = Mail::DKIM::ARC::Verifier->new( 'Strict' => $UseStrict );
505 # The following requires Mail::DKIM > 0.4
506 0 my $resolver = $self->get_object('resolver');
507 0 Mail::DKIM::DNS::resolver($resolver);
508 0 $self->set_object('arc', $arc, 1);
509 };
510 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
511 0 $self->handle_exception( $error );
512 0 $self->log_error( 'ARC Setup Error ' . $error );
513 0 $self->_check_error( $error );
514 0 $self->metric_count( 'arc_total', { 'result' => 'error' } );
515 0 $self->{'failmode'} = 1;
516 0 $self->{arc_result} = 'fail'; # XXX - handle tempfail better
517 0 ▶ delete $self->{headers} unless $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arcseal"} 0 0 0
518 0 return;
519 }
520  
521 0 eval {
522 $arc->PRINT( join q{},
523 0 @{ $self->{'headers'} },
524 "\015\012",
525 );
526 };
527 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
528 0 $self->handle_exception( $error );
529 0 $self->log_error( 'ARC Headers Error ' . $error );
530 0 $self->_check_error( $error );
531 0 $self->metric_count( 'arc_total', { 'result' => 'error' } );
532 0 $self->{'failmode'} = 1;
533 0 $self->{arc_result} = 'fail'; # XXX - handle tempfail better
534 0 ▶ delete $self->{headers} unless $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arcseal"} 0 0 0
535 0 return;
536 }
537 }
538  
539 sub body_callback {
540 0 ▶ my ( $self, $body_chunk ) = @_;
Subroutine body_callback: not called CC=8 SCAR=42.8
Pod undocumented
541 0 my $EOL = "\015\012";
542  
543 0 my $arc_chunk;
544 0 ▶ if ( $self->{'carry'} ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'carry'} ne '') { } 0 0 0
545 0 $arc_chunk = $self->{'carry'} . $body_chunk;
546 0 $self->{'carry'} = q{};
547 }
548 else {
549 0 $arc_chunk = $body_chunk;
550 }
551  
552 0 ▶ if ( substr( $arc_chunk, -1 ) eq "\015" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($arc_chunk, -1) eq "\r") 0 0 0
553 0 $self->{'carry'} = "\015";
554 0 $arc_chunk = substr( $arc_chunk, 0, -1 );
555 }
556  
557 0 $arc_chunk =~ s/\015?\012/$EOL/g;
558 0 ▶ push @{$self->{body}}, $arc_chunk if $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"has_arcseal"} 0 0 0
559  
560 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
561 0 my $arc = $self->get_object('arc');
562 0 eval {
563 0 $arc->PRINT( $arc_chunk );
564 };
565 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
566 0 $self->handle_exception( $error );
567 0 $self->log_error( 'ARC Body Error ' . $error );
568 0 $self->_check_error( $error );
569 0 $self->metric_count( 'arc_total', { 'result' => 'error' } );
570 0 $self->{'failmode'} = 1;
571 0 $self->{arc_result} = 'fail'; # XXX - handle tempfail better
572 0 ▶ delete $self->{headers} unless $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arcseal"} 0 0 0
573 }
574 }
575 }
576  
577 sub eom_requires {
578 0 ▶ my ( $self ) = @_;
Subroutine eom_requires: not called CC=2 SCAR=17.9
Pod undocumented
579 0 my @requires;
580  
581 0 ▶ if ( $self->is_handler_loaded( 'DKIM' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_handler_loaded("DKIM")) 0 0 0
582 0 push @requires, 'DKIM';
583 }
584  
585 0 return \@requires;
586 }
587  
588 sub eom_callback {
589 0 ▶ my ($self) = @_;
Subroutine eom_callback: not called CC=14 SCAR=53.5
Pod undocumented
590  
591 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
592  
593 # the rest of eom is only used for arc, not arcseal
594 0 ▶ return unless $self->{'has_arc'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arc"} 0 0 0
595 0 ▶ return if $self->{'failmode'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 0 0
596  
597 0 my $config = $self->handler_config();
598  
599 0 my $arc = $self->get_object('arc');
600  
601 0 eval {
602 0 $arc->PRINT( $self->{'carry'} );
603 0 $arc->CLOSE();
604 0 $self->check_timeout();
605  
606 0 my $arc_result = $arc->result;
607 0 my $arc_result_detail = $arc->result_detail;
608  
609 0 $self->metric_count( 'arc_total', { 'result' => $arc_result } );
610  
611 0 $self->dbgout( 'ARCResult', $arc_result_detail, LOG_INFO );
612  
613 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( $arc_result );
614  
615 0 my @items;
616 0 foreach my $signature ( @{ $arc->{signatures} } ) {
617 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
618 ref($signature) eq 'Mail::DKIM::ARC::Seal' ? 'as'
619 : ref($signature) eq 'Mail::DKIM::ARC::MessageSignature' ? 'ams'
620 : ref($signature);
621 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
622 "$type."
623 . ( $signature->instance() || '' ) . '.'
624 . ( $signature->domain() || '(none)' ) . '='
625 . ( $signature->result_detail() || '?' );
626 0 $self->{ 'arc_domain' }->{ $signature->instance() } = $signature->domain();
627 }
628  
629 0 ▶ if ( @items ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items) 0 0 0
630 0 my $header_comment = Mail::AuthenticationResults::Header::Comment->new();
631 0 my $header_comment_text = join( ', ', @items );
632 # Try set_value first (required for potential nested comment), if this fails then
633 # set using safe_set_value
634 0 eval { $header_comment->set_value( $header_comment_text ); };
635 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
636 0 $self->handle_exception( $error );
637 0 $header_comment->safe_set_value( $header_comment_text );
638 }
639 0 $header->add_child( $header_comment );
640 }
641  
642 0 $self->add_auth_header( $header );
643  
644 0 $self->{arc_result} = $arc_result;
645 };
646 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
647 0 $self->handle_exception( $error );
648 0 $self->log_error( 'ARC EOM Error ' . $error );
649 0 $self->_check_error( $error );
650 0 $self->metric_count( 'arc_total', { 'result' => 'error' } );
651 0 $self->{'failmode'} = 1;
652 0 $self->{arc_result} = 'fail';
653 }
654  
655 0 $self->inherit_trusted_spf_results();
656 0 $self->inherit_trusted_dkim_results();
657 0 $self->inherit_trusted_ip_results();
658  
659 0 return;
660  
661 }
662  
663 sub close_callback {
664 0 ▶ my ( $self ) = @_;
Subroutine close_callback: not called CC=1 SCAR=6.9
Pod undocumented
665 0 delete $self->{'failmode'};
666 0 delete $self->{'headers'};
667 0 delete $self->{'body'};
668 0 delete $self->{'carry'};
669 0 delete $self->{'has_arc'};
670 0 delete $self->{'valid_domains'};
671 0 delete $self->{'arc_domain'};
672 0 delete $self->{'arc_auth_results'};
673 0 $self->destroy_object('arc');
674 0 return;
675 }
676  
677 sub _check_error {
678 0 ▶ my ( $self, $error ) = @_;
Subroutine _check_error: not called CC=8 SCAR=42.8
679 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
680 or $error =~ /^DNS query timeout/
681 ){
682 0 $self->log_error( 'Temp ARC Error - ' . $error );
683 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( 'temperror' );
684 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'dns timeout' ) );
685 0 $self->add_auth_header( $header );
686 }
687 elsif ( $error =~ /^DNS error: SERVFAIL/ ){
688 0 $self->log_error( 'Temp ARC Error - ' . $error );
689 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( 'temperror' );
690 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'dns servfail' ) );
691 0 $self->add_auth_header( $header );
692 }
693 ▶ 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
694 or $error =~ /^policy syntax error$/
695 or $error =~ /^empty domain label/
696 or $error =~ /^invalid name /
697 ){
698 0 $self->log_error( 'Perm ARC Error - ' . $error );
699 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( 'permerror' );
700 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'syntax or domain error' ) );
701 0 $self->add_auth_header( $header );
702 }
703 else {
704 0 $self->log_error( 'Unexpected ARC Error - ' . $error );
705 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'arc' )->safe_set_value( 'temperror' );
706 0 $self->add_auth_header( $header );
707 # Fill these in as they occur, but for unknowns err on the side of caution
708 # and tempfail/exit
709 0 $self->exit_on_close();
710 0 $self->tempfail_on_error();
711 }
712 0 return;
713 }
714  
715 sub _fmtheader {
716 0 ▶ my $header = shift;
Subroutine _fmtheader: not called CC=1 SCAR=6.9
717 0 my $value = $header->{value};
718 0 $value =~ s/\015?\012/\015\012/gs; # make sure line endings are right
719 0 return "$header->{field}: $value\015\012";
720 }
721  
722 sub addheader_callback {
723 0 ▶ my $self = shift;
Subroutine addheader_callback: not called CC=15 SCAR=54.8
Pod undocumented
724 0 my $handler = shift;
725  
726 0 ▶ return unless $self->{has_arcseal};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_arcseal"} 0 0 0
727  
728 0 my $config = $self->handler_config();
729  
730 0 eval {
731 0 my %KeyOpts;
732 0 ▶ if ($config->{arcseal_keyfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'arcseal_keyfile'}) { } 0 0 0
733 0 $KeyOpts{KeyFile} = $config->{arcseal_keyfile};
734 }
735 else {
736 $KeyOpts{Key} = Mail::DKIM::PrivateKey->load(
737 0 Data => $config->{arcseal_key});
738 }
739 my $arcseal = Mail::DKIM::ARC::Signer->new(
740 Algorithm => $config->{arcseal_algorithm},
741 Domain => $config->{arcseal_domain},
742 SrvId => $self->get_my_hostname(),
743 Selector => $config->{arcseal_selector},
744 Headers => $config->{arcseal_headers},
745 # chain value is arc_result from previous seal validation
746 Chain => $self->{arc_result},
747 0 Timestamp => time(),
748 %KeyOpts,
749 );
750  
751 # pre-headers from handler (reversed as they will add in reverse)
752 0 ▶ foreach my $header (reverse @{$handler->{pre_headers} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$handler->{"pre_headers"} or [] 0 0 0
753 0 $arcseal->PRINT(_fmtheader($header));
754 }
755  
756 # then all the original headers: XXX - this doesn't deal with
757 # the change_header command, but only sanitize uses that.
758 # It would be a massive pain to make that work consistently,
759 # as it would need to modify the already cached headers in
760 # each handler with the current architecture
761 0 ▶ foreach my $chunk (@{$self->{headers} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"headers"} or [] 0 0 0
762 0 $arcseal->PRINT($chunk);
763 }
764  
765 # post-headers from handler (these are in order)
766 0 ▶ foreach my $header (@{$handler->{add_headers} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$handler->{"add_headers"} or [] 0 0 0
767 0 $arcseal->PRINT(_fmtheader($header));
768 0 $self->check_timeout();
769 }
770  
771 # finish header block with a blank line
772 0 $arcseal->PRINT("\015\012");
773  
774 # all the body chunks
775 0 foreach my $chunk (@{$self->{body}}) {
776 0 $arcseal->PRINT($chunk);
777 }
778  
779 # and we're done
780 0 $arcseal->CLOSE;
781 0 $self->check_timeout();
782  
783 0 my $arcseal_result = $arcseal->result();
784 0 my $arcseal_result_detail = $arcseal->result_detail();
785  
786 0 $self->metric_count( 'arcseal_total', { 'result' => $arcseal_result } );
787  
788 0 $self->dbgout( 'ARCSealResult', $arcseal_result_detail, LOG_INFO );
789  
790 # we need to extract the headers from ARCSeal and re-format them
791 # back to the format that pre_headers expects
792 0 my $headers = $arcseal->as_string();
793 0 my @list;
794  
795 0 my $current_header = q{};
796 0 my $current_value = q{};
797 0 foreach my $header_line ( (split ( /\015?\012/, $headers ) ) ) {
798 0 ▶ if ( $header_line =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_line =~ /^\s/) { } 0 0 0
799 # Line begins with whitespace, add to previous header
800 0 $header_line =~ s/^\s+/ /; # for consistency
801 0 $current_value .= "\n" . $header_line;
802 }
803 else {
804 # This is a brand new header!
805 0 ▶ if ( $current_header ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_header ne "") 0 0 0
806 # We have a cached header, add it now.
807 0 push @list, { 'field' => $current_header, 'value' => $current_value };
808 0 $current_value = q{};
809 }
810 0 ( $current_header, $current_value ) = split ( ':', $header_line, 2 );
811 0 $current_value =~ s/^ +//;
812 }
813 }
814 0 ▶ if ( $current_header ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_header ne "") 0 0 0
815 # We have a cached header, add it now.
816 0 push @list, { 'field' => $current_header, 'value' => $current_value };
817 0 $current_value = q{};
818 }
819  
820 # these will prepend in reverse
821 0 push @{$handler->{pre_headers}}, reverse @list;
822 };
823  
824 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
825 0 $self->handle_exception( $error );
826 0 $self->log_error( 'ARCSeal Error ' . $error );
827 0 $self->metric_count( 'arcseal_total', { 'result' => 'error' } );
828 0 return;
829 }
830 }
831  
832 1 1;
833  
834 __END__
835  
836 =pod
837  
838 =encoding UTF-8
839  
840 =head1 NAME
841  
842 Mail::Milter::Authentication::Handler::ARC - Authentication Milter Module for validation of ARC signatures
843  
844 =head1 VERSION
845  
846 version 2.20180911
847  
848 =head1 DESCRIPTION
849  
850 Module for validation of ARC signatures
851  
852 =head1 CONFIGURATION
853  
854 "ARC" : { | Config for the ARC Module
855 "hide_none" : 0, | Hide auth line if the result is 'none'
856 "arcseal_domain" : "example.com", | Domain to sign ARC Seal with (not sealed if blank)
857 "arcseal_selector" : undef, | Selector to use for ARC Seal (not sealed if blank)
858 "arcseal_algorithm" : 'rsa-sha256', | Algorithm to use on ARC Seal (default rsa-sha256)
859 "arcseal_key" : undef, | Key (base64) string to sign ARC Seal with; or
860 "arcseal_keyfile" : undef, | File containing ARC Seal key
861 "arcseal_headers" : undef, | Additional headers to cover in ARC-Message-Signature
862 "trusted_domains" : [], | Trust these domains when traversing ARC chains
863 "rbl_whitelist" : undef, | rhs list for looking up trusted signing domains
864 "no_strict" : 0, | Ignore rfc 8301 security considerations (not recommended)
865 },
866  
867 =head1 AUTHORS
868  
869 =over 4
870  
871 =item *
872  
873 Bron Gondwana <brong@fastmailteam.com>
874  
875 =item *
876  
877 Marc Bradshaw <marc@marcbradshaw.net>
878  
879 =back
880  
881 =head1 COPYRIGHT AND LICENSE
882  
883 This software is copyright (c) 2018 by FastMail Pty Ltd.
884  
885 This is free software; you can redistribute it and/or modify it under
886 the same terms as the Perl 5 programming language system itself.
887  
888 =cut
↑ Summary