lib/Mail/DMARC/Report/Sender.pm

filter: Statementstmt 76.8% 239 / 311 Branchbran 68.1% 94 / 138 Conditioncond 70.8% 51 / 72 MC/DCmcdc 47.5% 19 / 40 Subroutinesub 96.8% 31 / 32 Podpod 0.0% 0 / 16 totaltotal 71.2% 434 / 609 CC 107 SCAR 30.1
CC
107
Cov
74%
email
47.3
send_report
32.0
run
25.3
CRAP 20.3
« lib/Mail/DMARC/Base.pm ↑ Summary lib/Mail/DMARC/HTTP.pm »
1 package Mail::DMARC::Report::Sender;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶no warnings 'experimental::try'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Sys::Syslog qw(:standard :macros);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Mail::DMARC::Report;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Email::Sender::Simple qw{ sendmail };
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Email::Sender::Transport::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Email::Sender::Transport::SMTP::Persistent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 44 ▶sub new( $class, $args = undef ) {
Subroutine new: called CC=12 SCAR=28.5
Pod undocumented
21 44 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
143
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
22 my $self = {
23 send_delay => $args->{delay} // 5,
24 batch_size => $args->{batch} // 1,
25 alarm_at => $args->{timeout} // 120,
26 syslog => $args->{syslog} // 0,
27 smarthost => $args->{smarthost} // undef,
28 transports_method => $args->{transports_method} // undef,
29 transports_object => $args->{transports_object} // undef,
30 dkim_key => $args->{dkim_key} // undef,
31 44 ▶ verbose => $args->{verbose} // 0,
Condition55%10/18
$args->{"delay"} // 5
l!l
044
$args->{"batch"} // 1
l!l
044
$args->{"timeout"} // 120
l!l
044
$args->{"syslog"} // 0
l!l
044
$args->{"smarthost"} // undef
l!l
143
$args->{"transports_method"} // undef
l!l
044
$args->{"transports_object"} // undef
l!l
044
$args->{"dkim_key"} // undef
l!l
044
$args->{"verbose"} // 0
l!l
044
MC/DC0%0/1
$args->{"delay"} // 5
$args->{"delay"}
MC/DC0%0/1
$args->{"batch"} // 1
$args->{"batch"}
MC/DC0%0/1
$args->{"timeout"} // 120
$args->{"timeout"}
MC/DC0%0/1
$args->{"syslog"} // 0
$args->{"syslog"}
MC/DC100%1/1
$args->{"smarthost"} // undef
$args->{"smarthost"}
MC/DC0%0/1
$args->{"transports_method"} // undef
$args->{"transports_method"}
MC/DC0%0/1
$args->{"transports_object"} // undef
$args->{"transports_object"}
MC/DC0%0/1
$args->{"dkim_key"} // undef
$args->{"dkim_key"}
MC/DC0%0/1
$args->{"verbose"} // 0
$args->{"verbose"}
Truth table50%1/2
A
A= $args->{"delay"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"batch"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"timeout"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"syslog"}
Aresult
11
00
Truth table100%2/2
A
A= $args->{"smarthost"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"transports_method"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"transports_object"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"dkim_key"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"verbose"}
Aresult
11
00
32 };
33 44 return bless $self, $class;
34 }
35  
36 11 ▶sub set_transports_object( $self, $transports_object ) {
Subroutine set_transports_object: called CC=1 SCAR=0.0
Pod undocumented
37 11 $self->{transports_object} = $transports_object;
38 11 return;
39 }
40  
41 8 ▶sub set_transports_method( $self, $transports_method ) {
Subroutine set_transports_method: called CC=1 SCAR=0.0
Pod undocumented
42 8 $self->{transports_method} = $transports_method;
43 8 return;
44  
45 # Transports method is a sub which returns
46 # a list of transports for the given args.
47 }
48  
49 2 my %SMART_SSL = map { $_ => 1 } qw/ starttls maybestarttls ssl /;
50  
51 # The submission ports each imply how TLS is negotiated on them.
52 2 my %PORT_TLS = ( 465 => 'ssl', 587 => 'starttls' );
53  
54 29 ▶sub smarthost_transports( $self, $report ) {
Subroutine smarthost_transports: called CC=11 SCAR=24.0
Pod undocumented
55 29 my $smtp = $report->config->{smtp};
56  
57 my %common = (
58 host => $smtp->{smarthost},
59 29 helo => $report->sendit->smtp->get_helo_hostname,
60 timeout => 32,
61 );
62  
63 29 my %auth;
64 29 ▶ if ( $smtp->{smartuser} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"smartuser"}) 10 19 29
65 10 $auth{sasl_username} = $smtp->{smartuser};
66 10 ▶ $auth{sasl_password} = $smtp->{smartpass} if $smtp->{smartpass};
Branch: 1 branch
BranchTrueFalseTotal
if $smtp->{"smartpass"} 10 0 10
67 }
68  
69 29 my $configured = $self->smarthost_ssl_configured($report);
70  
71 29 ▶ if ( my $port = $smtp->{smartport} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $port = $smtp->{"smartport"}) 10 19 29
72 10 ▶ return $self->smarthost_route( \%common, \%auth, $port,
Branch: 1 branch
BranchTrueFalseTotal
if $configured 2 8 10
73 $self->smarthost_ssl($report) )
74 if $configured;
75 return $self->smarthost_route( \%common, \%auth, $port,
76 $PORT_TLS{$port} )
77 8 ▶ if $PORT_TLS{$port};
Branch: 1 branch
BranchTrueFalseTotal
if $PORT_TLS{$port} 4 4 8
78 4 ▶ return $self->smarthost_route( \%common, \%auth, $port,
Branch: 1 branch
BranchTrueFalseTotal
%auth ? : 2 2 4
79 %auth ? 'starttls' : 'maybestarttls' );
80 }
81  
82 19 ▶ if ($configured) {
Branch: 1 branch
BranchTrueFalseTotal
if ($configured) 12 7 19
83 12 my $ssl = $self->smarthost_ssl($report);
84 7 ▶ return $self->smarthost_route( \%common, \%auth,
Branch: 1 branch
BranchTrueFalseTotal
'ssl' eq ($ssl || '') ? : 2 5 7
Condition100%2/2
$ssl || ''
l!l
43
MC/DC100%1/1
$ssl || ''
$ssl
Truth table100%2/2
A
A= $ssl
Aresult
11
00
85 ( 'ssl' eq ( $ssl || q{} ) ? 465 : 25 ), $ssl );
86 }
87  
88 return (
89 7 ▶ $self->smarthost_route( \%common, \%auth, 465, 'ssl' ),
Branch: 1 branch
BranchTrueFalseTotal
if %auth 4 3 7
90 $self->smarthost_route( \%common, \%auth, 587, 'starttls' ),
91 $self->smarthost_route( \%common, \%auth, 25, 'starttls' ),
92 ) if %auth;
93  
94 # maybestarttls fails closed when STARTTLS is advertised but the handshake
95 # does not complete, which a relay with a self signed certificate does
96 # every time. Cleartext last is what opportunistic delivery means
97 # (RFC 7435): the alternative is not TLS, it is the report being dropped.
98 # Credentials never reach here, and never get maybestarttls either, which
99 # authenticates over plaintext when STARTTLS is not advertised.
100 return (
101 3 $self->smarthost_route( \%common, {}, 25, 'maybestarttls' ),
102 $self->smarthost_route( \%common, {}, 587, 'starttls' ),
103 $self->smarthost_route( \%common, {}, 25, 0 ),
104 );
105 }
106  
107 38 ▶sub smarthost_route( $self, $common, $auth, $port, $ssl ) {
Subroutine smarthost_route: called CC=1 SCAR=0.0
Pod undocumented
108 38 return Email::Sender::Transport::SMTP::Persistent->new(
109 { %$common, %$auth, port => $port, ssl => $ssl } );
110 }
111  
112 30 ▶sub smarthost_ssl_configured( $self, $report ) {
Subroutine smarthost_ssl_configured: called CC=3 SCAR=11.0
Pod undocumented
113 30 my $ssl = $report->config->{smtp}{smartssl};
114 30 ▶ return defined $ssl && $ssl =~ /\S/x ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $ssl && $ssl =~ /\S/x ? : 15 15 30
Condition100%3/3
defined $ssl and $ssl =~ /\S/x
!ll&&!rl&&r
14115
MC/DC100%2/2
defined $ssl and $ssl =~ /\S/x
defined $ssl $ssl =~ /\S/x
Truth table100%3/3
A and B
A= defined $sslB= $ssl =~ /\S/x
ABresult
0X0
100
111
115 }
116  
117 15 ▶sub smarthost_ssl( $self, $report ) {
Subroutine smarthost_ssl: called CC=4 SCAR=13.9
Pod undocumented
118 15 my $ssl = lc $report->config->{smtp}{smartssl};
119 15 $ssl =~ s/\A\s+|\s+\z//gx;
120  
121 croak "unknown smtp.smartssl '$ssl', expected one of: none, "
122 . join( ', ', sort keys %SMART_SSL )
123 15 ▶ if 'none' ne $ssl && !$SMART_SSL{$ssl};
Branch: 1 branch
BranchTrueFalseTotal
if "none" ne $ssl and not $SMART_SSL{$ssl} 6 9 15
Condition100%3/3
"none" ne $ssl and not $SMART_SSL{$ssl}
!ll&&!rl&&r
456
MC/DC100%2/2
"none" ne $ssl and not $SMART_SSL{$ssl}
"none" ne $ssl not $SMART_SSL{$ssl}
Truth table100%3/3
A and not(B)
A= "none" ne $sslB= not $SMART_SSL{$ssl}
ABresult
0X0
100
111
124  
125 9 ▶ return 0 if 'none' eq $ssl;
Branch: 1 branch
BranchTrueFalseTotal
if "none" eq $ssl 4 5 9
126 5 return $ssl;
127 }
128  
129 12 ▶sub builds_smarthost_routes( $self, $report ) {
Subroutine builds_smarthost_routes: called CC=5 SCAR=16.1
Pod undocumented
130 12 ▶ return 0 if $self->{transports_method} || $self->{transports_object};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transports_method"} or $self->{"transports_object"} 10 2 12
Condition100%3/3
$self->{"transports_method"} or $self->{"transports_object"}
l!l&&r!l&&!r
822
MC/DC100%2/2
$self->{"transports_method"} or $self->{"transports_object"}
$self->{"transports_method"} $self->{"transports_object"}
Truth table100%3/3
A or B
A= $self->{"transports_method"}B= $self->{"transports_object"}
ABresult
1X1
011
000
131 2 ▶ return 0 if $self->{smarthost};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"smarthost"} 1 1 2
132 1 ▶ return $report->config->{smtp}{smarthost} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$report->config->{'smtp'}{'smarthost'} ? : 1 0 1
133 }
134  
135 # A route that never answers costs a connect timeout on every message.
136 7 ▶sub keep_smarthost_route( $self, $transport ) {
Subroutine keep_smarthost_route: called CC=3 SCAR=11.0
Pod undocumented
137 7 ▶ return if 'ARRAY' ne ref $self->{smarthost};
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" ne ref $self->{"smarthost"} 4 3 7
138 3 ▶ return if !grep { refaddr($_) == refaddr($transport) } @{ $self->{smarthost} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {builtin::refaddr($_) == builtin::refaddr($transport);} @{$$self{"smarthost"};} 1 2 3
139 2 $self->{smarthost} = [$transport];
140 2 return;
141 }
142  
143 # Return a list of transports to try in order.
144 42 ▶sub get_transports_for( $self, $args ) {
Subroutine get_transports_for: called CC=9 SCAR=22.2
Pod undocumented
145  
146 # Have we passed a custom transports generation class?
147 42 ▶ if ( $self->{transports_method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"transports_method"}) 8 34 42
148 8 my @transports = &{ $self->{transports_method} }($args);
149 8 return @transports;
150 }
151 34 ▶ if ( $self->{transports_object} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"transports_object"}) 1 33 34
152 1 my @transports = $self->{transports_object}->get_transports_for($args);
153 1 return @transports;
154 }
155  
156 33 my $report = $args->{report};
157  
158 33 ▶ if ( $report->config->{smtp}{smarthost} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($report->config->{"smtp"}{"smarthost"}) 32 1 33
159 32 ▶ return @{ $self->{smarthost} } if 'ARRAY' eq ref $self->{smarthost};
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $self->{"smarthost"} 2 30 32
160 30 ▶ return ( $self->{smarthost} ) if $self->{smarthost};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"smarthost"} 1 29 30
161 29 $self->{smarthost} = [ $self->smarthost_transports($report) ];
162 24 return @{ $self->{smarthost} };
163 }
164  
165 1 my @smtp_hosts = $report->sendit->smtp->get_smtp_hosts( $args->{to} );
166  
167 1 my $log_data = $args->{log_data};
168 1 $log_data->{smtp_host} = join( ',', @smtp_hosts );
169  
170 1 my %common = (
171 port => 25,
172 helo => $report->sendit->smtp->get_helo_hostname,
173 timeout => 32,
174 );
175  
176 # Every MX is tried encrypted before any is tried in the clear. A receiver
177 # whose certificate cannot be verified still fails the handshake closed,
178 # and its reports would otherwise be dropped undelivered.
179 1 ▶ if ( Email::Sender::Transport::SMTP->can('hosts') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Email::Sender::Transport::SMTP"->can("hosts")) 1 0 1
180 return map {
181 1 Email::Sender::Transport::SMTP->new(
182 { %common, hosts => \@smtp_hosts, ssl => $_ } )
183 } ( 'maybestarttls', 0 );
184 }
185  
186 # Older transports take one host, so each MX needs its own.
187 0 my @transports;
188 0 foreach my $ssl ( 'maybestarttls', 0 ) {
189 push @transports,
190 Email::Sender::Transport::SMTP->new(
191 { %common, host => $_, ssl => $ssl } )
192 0 foreach @smtp_hosts;
193 }
194  
195 0 return @transports;
196 }
197  
198 10 ▶sub get_dkim_key($self) {
Subroutine get_dkim_key: called CC=5 SCAR=28.3
Pod undocumented
199 10 my $report = $self->{report};
200 10 ▶ return $self->{dkim_key} if $self->{dkim_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dkim_key"} 0 10 10
201 10 ▶ if ( $report->config->{report_sign}->{keyfile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($report->config->{"report_sign"}{"keyfile"}) 0 10 10
202 0 try {
203 0 require Mail::DKIM::PrivateKey;
204 0 require Mail::DKIM::Signer;
205 0 require Mail::DKIM::TextWrap;
206 } catch ($e) { };
207 0 ▶ if ( UNIVERSAL::can( 'Mail::DKIM::Signer', "new" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can('Mail::DKIM::Signer', 'new')) { } 0 0 0
208 0 my $file = $report->config->{report_sign}->{keyfile};
209 0 $self->{dkim_key} = Mail::DKIM::PrivateKey->load( 'File' => $file, );
210 0 ▶ if ( !$self->{dkim_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dkim_key"}) 0 0 0
211 0 die "Could not load DKIM key $file";
212 }
213 }
214 else {
215 0 die
216 'DKIM signing requested but Mail::DKIM could not be loaded. Please check that Mail::DKIM is installed.';
217 }
218 0 $self->log_output('DKIM signing key loaded');
219 0 return $self->{dkim_key};
220 }
221 }
222  
223 12 ▶sub run($self) {
Subroutine run: called CC=14 SCAR=32.3
Pod undocumented
224  
225 GetOptions(
226 'verbose+' => \$self->{verbose},
227 'delay=i' => \$self->{send_delay},
228 'batch=i' => \$self->{batch_size},
229 'timeout=i' => \$self->{alarm_at},
230 'syslog+' => \$self->{syslog},
231 12 );
232  
233 12 ▶ openlog( 'dmarc_send_reports', 'pid', LOG_MAIL ) if $self->{syslog};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"syslog"} 0 12 12
234 12 $self->log_output('dmarc_send_reports starting up');
235  
236 12 $|++;
237 12 my $report = Mail::DMARC::Report->new();
238 12 $self->{report} = $report;
239 12 ▶ $report->verbose( $self->{verbose} ) if defined $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"verbose"} 12 0 12
240  
241 # If we have defined a custom transports generation class then
242 # load and instantiate it here.
243 12 ▶ if ( $report->config->{smtp}->{transports} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($report->config->{"smtp"}{"transports"}) 10 2 12
244 10 load $report->config->{smtp}->{transports};
245 10 my $package = $report->config->{smtp}->{transports};
246 10 my $transports_object = $package->new();
247 10 $self->set_transports_object($transports_object);
248 }
249  
250 12 ▶ $self->smarthost_ssl($report)
Branch: 1 branch
BranchTrueFalseTotal
if $self->builds_smarthost_routes($report) and $self->smarthost_ssl_configured($report) 1 11 12
Condition66%2/3
$self->builds_smarthost_routes($report) and $self->smarthost_ssl_configured($report)
!ll&&!rl&&r
1101
MC/DC50%1/2
$self->builds_smarthost_routes($report) and $self->smarthost_ssl_configured($report)
$self->builds_smarthost_routes($report) $self->smarthost_ssl_configured($report)
Truth table66%2/3
A and B
A= $self->builds_smarthost_routes($report)B= $self->smarthost_ssl_configured($report)
ABresult
0X0
100
111
251 if $self->builds_smarthost_routes($report)
252 && $self->smarthost_ssl_configured($report);
253  
254 0 ▶ local $SIG{'ALRM'} = sub { die "timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
255  
256 11 my $batch_do = 1;
257  
258 # 1. get reports, one at a time
259 REPORT:
260 11 ▶ while ( my $aggregate = $report->store->next_todo() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $aggregate = $report->store->next_todo) 10 11 21
261 10 try {
262 10 $self->send_report( $aggregate, $report );
263 }
264 catch ($error) {
265 0 $self->log_output( 'error sending report: ' . $error );
266 }
267  
268 10 ▶ if ( $batch_do++ > $self->{batch_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($batch_do++ > $self->{"batch_size"}) 0 10 10
269 0 $batch_do = 1;
270 0 ▶ if ( $self->{send_delay} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"send_delay"} > 0) 0 0 0
271 0 ▶ print "sleeping " . $self->{send_delay} if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
272 0 foreach ( 1 .. $self->{send_delay} ) {
273 0 ▶ print '.' if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
274 0 sleep 1;
275 }
276 0 ▶ print "done.\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
277 }
278 }
279  
280 }
281  
282 11 alarm(0);
283  
284 11 $self->log_output('dmarc_send_reports done');
285 11 ▶ closelog() if $self->{syslog};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"syslog"} 0 11 11
286  
287 11 return;
288 }
289  
290 # PODNAME: dmarc_send_reports
291 # ABSTRACT: send aggregate reports
292  
293 10 ▶sub send_report( $self, $aggregate, $report ) {
Subroutine send_report: called CC=15 SCAR=34.6
Pod undocumented
294  
295 10 alarm( $self->{alarm_at} );
296  
297 10 $self->log_output(
298 { 'id' => $aggregate->metadata->report_id,
299 'domain' => $aggregate->policy_published->domain,
300 'rua' => $aggregate->policy_published->rua,
301 }
302 );
303  
304 # Generate the list of report receivers
305 10 my $report_receivers;
306 10 try {
307 10 $report_receivers = $report->uri->parse( $aggregate->policy_published->rua );
308 }
309 catch ($error) {
310 0 $self->log_output(
311 { 'id' => $aggregate->metadata->report_id,
312 'error' => 'No valid ruas found - deleting report - ' . $error,
313 }
314 );
315 0 $report->store->delete_report( $aggregate->metadata->report_id );
316 0 alarm(0);
317 0 return;
318 }
319  
320 # Check we have some receivers
321 10 ▶ if ( !@$report_receivers ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$report_receivers) 0 10 10
322 0 $self->log_output(
323 { 'id' => $aggregate->metadata->report_id,
324 'error' => 'No valid ruas found - deleting report',
325 }
326 );
327 0 $report->store->delete_report( $aggregate->metadata->report_id );
328 0 alarm(0);
329 0 return;
330 }
331  
332 # Generate the XML data and associated metadata
333 10 my $xml = $aggregate->as_xml();
334 10 my $xml_compressed = $report->compress( \$xml );
335 10 my $xml_compressed_bytes = length Encode::encode_utf8($xml_compressed);
336  
337 10 my $sent = 0;
338 10 my $cc_sent = 0;
339 10 my @too_big;
340 URI:
341 10 foreach my $receiver (@$report_receivers) {
342 10 my $method = $receiver->{uri};
343 10 my $max = $receiver->{max_bytes};
344  
345 10 ▶ if ( $max && $xml_compressed_bytes > $max ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($max and $xml_compressed_bytes > $max) { } 0 10 10
elsif ('mailto:' eq substr($method, 0, 7)) { } 10 0 10
elsif ('http:' eq substr($method, 0, 5)) { } 0 0 0
elsif ('https:' eq substr($method, 0, 6)) { } 0 0 0
Condition33%1/3
$max and $xml_compressed_bytes > $max
!ll&&!rl&&r
1000
MC/DC0%0/2
$max and $xml_compressed_bytes > $max
$max $xml_compressed_bytes > $max
Truth table33%1/3
A and B
A= $maxB= $xml_compressed_bytes > $max
ABresult
0X0
100
111
346 0 $self->log_output(
347 { 'id' => $aggregate->metadata->report_id,
348 'info' =>
349 "skipping $method: report size ($xml_compressed_bytes) larger than $max",
350 }
351 );
352 0 push @too_big, $method;
353 0 next URI;
354 }
355 elsif ( 'mailto:' eq substr( $method, 0, 7 ) ) {
356 10 my ($to) = ( split /:/, $method )[-1];
357 10 my $cc = $report->config->{smtp}{cc};
358 10 ▶ if ( $cc && $cc ne 'set.this@for.a.while.example.com' && !$cc_sent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc and $cc ne "set.this\@for.a.while.example.com" and not $cc_sent) 0 10 10
Condition50%3/6
$cc and $cc ne "set.this\@for.a.while.example.com" and not $cc_sent
!ll&&!rl&&r
1000
$cc and $cc ne "set.this\@for.a.while.example.com"
!ll&&!rl&&r
190
MC/DC0%0/3
$cc and $cc ne "set.this\@for.a.while.example.com" and not $cc_sent
$cc $cc ne "set.this\@for.a.while.example.com" not $cc_sent
Truth table50%2/4
A and B and not(C)
A= $ccB= $cc ne "set.this\@for.a.while.example.com"C= not $cc_sent
ABCresult
0XX0
10X0
1100
1111
359 0 $self->email(
360 { to => $cc,
361 compressed => $xml_compressed,
362 aggregate => \$aggregate
363 }
364 );
365 0 $cc_sent = 1;
366 }
367 $self->email(
368 10 ▶ { to => $to,
Branch: 1 branch
BranchTrueFalseTotal
$self->email({"to", $to, "compressed", $xml_compressed, "aggregate", \$aggregate}) and ++$sent 5 5 10
369 compressed => $xml_compressed,
370 aggregate => \$aggregate
371 }
372 ) and $sent++;
373 }
374  
375 # http(s) sending not yet enabled in module, skip this send and
376 # increment sent to avoid looping
377 elsif ( 'http:' eq substr( $method, 0, 5 ) ) {
378  
379 #$report->sendit->http->post( $method, \$aggregate, $shrunk );
380 0 $sent++;
381 }
382 elsif ( 'https:' eq substr( $method, 0, 6 ) ) {
383  
384 #$report->sendit->http->post( $method, \$aggregate, $shrunk );
385 0 $sent++;
386 }
387 }
388  
389 10 ▶ if ($sent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sent) { } 5 5 10
390 5 $report->store->delete_report( $aggregate->metadata->report_id );
391 }
392 else {
393 5 ▶ my $send_errors = $report->config->{smtp}->{send_errors} // 1;
Condition50%1/2
$report->config->{"smtp"}{"send_errors"} // 1
l!l
05
MC/DC0%0/1
$report->config->{"smtp"}{"send_errors"} // 1
$report->config->{"smtp"}{"send_errors"}
Truth table50%1/2
A
A= $report->config->{"smtp"}{"send_errors"}
Aresult
11
00
394 5 ▶ $self->send_too_big_email( \@too_big, $xml_compressed_bytes, $aggregate )
Branch: 1 branch
BranchTrueFalseTotal
if $send_errors 5 0 5
395 if $send_errors;
396 5 $report->store->delete_report( $aggregate->metadata->report_id );
397 }
398  
399 10 alarm(0);
400 10 return;
401 }
402  
403 5 ▶sub send_too_big_email( $self, $too_big, $bytes, $aggregate ) {
Subroutine send_too_big_email: called CC=3 SCAR=18.1
Pod undocumented
404 5 my $report = $self->{report};
405  
406 BIGURI:
407 5 foreach my $uri (@$too_big) {
408 0 ▶ next BIGURI if 'mailto:' ne substr( $uri, 0, 7 );
Branch: 1 branch
BranchTrueFalseTotal
if "mailto:" ne substr($uri, 0, 7) 0 0 0
409 0 my ($to) = ( split /:/, $uri )[-1];
410 0 my $body = $report->sendit->too_big_report(
411 { uri => $uri,
412 report_bytes => $bytes,
413 report_id => $aggregate->metadata->report_id,
414 report_domain => $aggregate->policy_published->domain,
415 }
416 );
417 0 my $mime_object
418 = $report->sendit->smtp->assemble_too_big_message_object( $to, $body );
419 0 $self->email( { to => $to, mime => $mime_object } );
420 }
421 5 return;
422 }
423  
424 10 ▶sub email( $self, $args ) {
Subroutine email: called CC=28 SCAR=38.6
Pod undocumented
425  
426 10 my $to = $args->{to};
427 10 ▶ if ( !$to ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($to) 0 10 10
428 0 $self->log_output( { 'error' => 'No recipient for email' } );
429 0 croak 'No recipient for email';
430 }
431 10 ▶ my $mime = $args->{mime} // undef;
Condition50%1/2
$args->{"mime"} // undef
l!l
010
MC/DC0%0/1
$args->{"mime"} // undef
$args->{"mime"}
Truth table50%1/2
A
A= $args->{"mime"}
Aresult
11
00
432 10 ▶ my $compressed = $args->{compressed} // undef;
Condition50%1/2
$args->{"compressed"} // undef
l!l
100
MC/DC0%0/1
$args->{"compressed"} // undef
$args->{"compressed"}
Truth table50%1/2
A
A= $args->{"compressed"}
Aresult
11
00
433 10 ▶ my $agg_ref = $args->{aggregate} // undef;
Condition50%1/2
$args->{"aggregate"} // undef
l!l
100
MC/DC0%0/1
$args->{"aggregate"} // undef
$args->{"aggregate"}
Truth table50%1/2
A
A= $args->{"aggregate"}
Aresult
11
00
434 10 my $report = $self->{report};
435  
436 10 my $rid;
437 10 ▶ $rid = $$agg_ref->metadata->report_id if $agg_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $agg_ref 10 0 10
438  
439 10 my $log_data = { deliver_to => $to, };
440  
441 10 my $body;
442 10 ▶ if ($rid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rid) { } 10 0 10
elsif ($mime) { } 0 0 0
443 10 my $mime_object
444 = $report->sendit->smtp->assemble_message_object( $agg_ref, $to,
445 $compressed );
446 10 $body = $mime_object->as_string;
447 10 $log_data->{id} = $rid;
448 10 $log_data->{to_domain} = $$agg_ref->policy_published->domain;
449 }
450 elsif ($mime) {
451 0 $body = $mime->as_string;
452 }
453 else {
454 0 croak 'No email content';
455 }
456  
457 10 my $dkim_key = $self->get_dkim_key();
458 10 ▶ if ($dkim_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_key) 0 10 10
459 0 my $dkim_algorithm = $report->config->{report_sign}{algorithm};
460 0 my $dkim_method = $report->config->{report_sign}{method};
461 0 my $dkim_domain = $report->config->{report_sign}{domain};
462 0 my $dkim_selector = $report->config->{report_sign}{selector};
463 0 try {
464 0 my $dkim = Mail::DKIM::Signer->new(
465 Algorithm => $dkim_algorithm,
466 Method => $dkim_method,
467 Domain => $dkim_domain,
468 Selector => $dkim_selector,
469 Key => $dkim_key,
470 );
471 0 $body =~ s/\015?\012/\015\012/g;
472 0 $dkim->PRINT($body);
473 0 $dkim->CLOSE;
474 0 my $signature = $dkim->signature;
475 0 $body = $signature->as_string . "\015\012" . $body;
476 0 $log_data->{dkim} = 1;
477 }
478 catch ($error) {
479 0 ▶ print "DKIM Signing error\n\t$error\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
480 0 $log_data->{error} = 'DKIM Signing error';
481 0 $log_data->{error_detail} = $error;
482 0 $self->log_output($log_data);
483 0 return;
484 }
485 }
486  
487 10 my @transports = $self->get_transports_for(
488 { report => $report,
489 log_data => $log_data,
490 to => $to,
491 }
492 );
493 10 my $success;
494 10 my $permanent = 0;
495 10 my $answered = 0;
496 10 ▶ while ( my $transport = shift @transports ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $transport = shift @transports) 15 3 18
497  
498 # A cleartext retry is for a handshake that never completed. Once a
499 # host has answered in SMTP it has seen the session, and repeating it
500 # unencrypted would put the report on the wire for a greylisting or a
501 # content rejection.
502 15 ▶ next if $answered && $transport->can('ssl') && !$transport->ssl;
Branch: 1 branch
BranchTrueFalseTotal
if $answered and $transport->can("ssl") and not $transport->ssl 1 14 15
Condition100%6/6
$answered and $transport->can("ssl") and not $transport->ssl
!ll&&!rl&&r
1311
$answered and $transport->can("ssl")
!ll&&!rl&&r
1122
MC/DC100%3/3
$answered and $transport->can("ssl") and not $transport->ssl
$answered $transport->can("ssl") not $transport->ssl
Truth table100%4/4
A and B and not(C)
A= $answeredB= $transport->can("ssl")C= not $transport->ssl
ABCresult
0XX0
10X0
1100
1111
503  
504 14 my $done = 0;
505 14 try {
506 $success = sendmail(
507 $body,
508 { from => $report->config->{organization}{email},
509 14 to => $to,
510 transport => $transport,
511 }
512 );
513 5 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) 5 0 5
514 5 $log_data->{success} = $success->{message};
515 5 $done = 1;
516 5 $self->keep_smarthost_route($transport);
517 }
518 }
519 catch ($error) {
520 9 my $code;
521 my $message;
522  
523 # A connect, TLS or AUTH failure arrives as a plain string, and
524 # ->code on that threw past the code that retires the report.
525 9 ▶ if ( blessed($error) && $error->isa('Email::Sender::Failure') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa('Email::Sender::Failure')) { } 6 3 9
Condition66%2/3
builtin::blessed($error) and $error->isa("Email::Sender::Failure")
!ll&&!rl&&r
306
MC/DC50%1/2
builtin::blessed($error) and $error->isa("Email::Sender::Failure")
builtin::blessed($error) $error->isa("Email::Sender::Failure")
Truth table66%2/3
A and B
A= builtin::blessed($error)B= $error->isa("Email::Sender::Failure")
ABresult
0X0
100
111
526 6 $code = $error->code;
527 6 $message = $error->message;
528 }
529 else {
530 3 $code = 'error';
531 3 $message = "$error";
532 3 chomp $message;
533 }
534 9 ▶ $code //= 'error';
Condition100%2/2
$code //= "error"
l!l
72
MC/DC100%1/1
$code //= "error"
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
535 9 ▶ $message //= 'unknown send failure';
Condition50%1/2
$message //= "unknown send failure"
l!l
90
MC/DC0%0/1
$message //= "unknown send failure"
$message
Truth table50%1/2
A
A= $message
Aresult
11
00
536  
537 # Connect and STARTTLS failures carry no code; anything else means
538 # the host answered.
539 9 ▶ $answered ||= $code ne 'error';
Condition100%3/3
$answered ||= $code ne "error"
l!l&&r!l&&!r
144
MC/DC100%2/2
$answered ||= $code ne "error"
$answered $code ne "error"
Truth table100%3/3
A ||= B
A= $answeredB= $code ne "error"
ABresult
1X1
011
000
540  
541 # Any rung answering 5xx settles the report: the host has refused
542 # it, and the rungs are ports on that same host.
543 9 ▶ $permanent ||= $code =~ /^5/x;
Condition66%2/3
$permanent ||= $code =~ /^5/x
l!l&&r!l&&!r
027
MC/DC50%1/2
$permanent ||= $code =~ /^5/x
$permanent $code =~ /^5/x
Truth table66%2/3
A ||= B
A= $permanentB= $code =~ /^5/x
ABresult
1X1
011
000
544  
545 9 my $timed_out = 'timeout' eq $message;
546  
547 $code = join( ', ', $log_data->{send_error_code}, $code )
548 9 ▶ if exists $log_data->{send_error_code};
Branch: 1 branch
BranchTrueFalseTotal
if exists $log_data->{"send_error_code"} 1 8 9
549 $message = join( ', ', $log_data->{send_error}, $message )
550 9 ▶ if exists $log_data->{send_error};
Branch: 1 branch
BranchTrueFalseTotal
if exists $log_data->{"send_error"} 1 8 9
551 9 $log_data->{send_error} = $message;
552 9 $log_data->{send_error_code} = $code;
553  
554 # The alarm in send_report bounds the report, not each rung, and
555 # is not rearmed; carrying on would leave the ladder unbounded and
556 # could resend a message the server already took.
557 9 ▶ if ($timed_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) 2 7 9
558 2 $report->store->error( $rid, $message );
559 2 last;
560 }
561  
562 7 ▶ next if @transports;
Branch: 1 branch
BranchTrueFalseTotal
if @transports 5 2 7
563  
564 2 ▶ if ($permanent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($permanent) 0 2 2
565  
566 # Perma error
567 0 $log_data->{deleted} = 1;
568 0 $report->store->delete_report($rid);
569 0 $success = 0;
570 0 last;
571 }
572 2 $report->store->error( $rid, $message );
573 }
574 7 ▶ last if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 5 2 7
575 }
576  
577 10 $self->log_output($log_data);
578  
579 10 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) 5 5 10
580 5 return 1;
581 }
582 5 return 0;
583 }
584  
585 43 ▶sub log_output( $self, $args ) {
Subroutine log_output: called CC=7 SCAR=19.7
Pod undocumented
586  
587 43 my $log_level = LOG_INFO;
588 43 my $log_entry = '';
589  
590 43 ▶ if ( ref $args eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq 'HASH') { } 20 23 43
591  
592 20 ▶ if ( $args->{'log_level'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"log_level"}) 0 20 20
593 0 $log_level = $args->{'log_level'};
594 0 delete $args->{'log_level'};
595 }
596  
597 20 my @parts;
598 20 foreach my $key ( sort keys %$args ) {
599 81 ▶ my $value = $args->{$key} // '';
Condition100%2/2
$args->{$key} // ""
l!l
765
MC/DC100%1/1
$args->{$key} // ""
$args->{$key}
Truth table100%2/2
A
A= $args->{$key}
Aresult
11
00
600 81 $value =~ s/,/#044/g; # Encode commas
601 81 push @parts, join( '=', $key, $value );
602 }
603 20 $log_entry = join( ', ', @parts );
604 }
605 else {
606 23 $log_entry = $args;
607 }
608  
609 43 ▶ syslog( $log_level, $log_entry ) if $self->{syslog};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"syslog"} 0 43 43
610 43 ▶ print "$log_entry\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 4 39 43
611  
612 43 return;
613 }
614  
615 2 1;
« lib/Mail/DMARC/Base.pm ↑ Summary lib/Mail/DMARC/HTTP.pm »