blib/lib/Mail/DMARC/Report/Sender.pm untested

filter: Statementstmt 0.0% 0 / 342 Branchbran 0.0% 0 / 132 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 633 CC 67 SCAR 45.5
CC
67
Cov
0%
email
306.0
run
132.0
smarthost_transports
110.0
CRAP 94.8
1 package Mail::DMARC::Report::Sender;
2  
3 use strict;
4 use warnings;
5 use feature 'signatures';
6 use feature 'try';
7 no warnings 'experimental::try'; ## no critic (ProhibitNoWarnings)
8  
9 use Carp;
10 use Encode;
11 use Getopt::Long;
12 use Sys::Syslog qw(:standard :macros);
13 use Mail::DMARC::Report;
14 use Email::Sender::Simple qw{ sendmail };
15 use Email::Sender::Transport::SMTP;
16 use Email::Sender::Transport::SMTP::Persistent;
17 use Module::Load;
18 use Scalar::Util qw(blessed refaddr);
19  
20 sub new( $class, $args = undef ) {
21 $args ||= {};
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 verbose => $args->{verbose} // 0,
32 };
33 return bless $self, $class;
34 }
35  
36 sub set_transports_object( $self, $transports_object ) {
37 $self->{transports_object} = $transports_object;
38 return;
39 }
40  
41 sub set_transports_method( $self, $transports_method ) {
42 $self->{transports_method} = $transports_method;
43 return;
44  
45 # Transports method is a sub which returns
46 # a list of transports for the given args.
47 }
48  
49 my %SMART_SSL = map { $_ => 1 } qw/ starttls maybestarttls ssl /;
50  
51 # The submission ports each imply how TLS is negotiated on them.
52 my %PORT_TLS = ( 465 => 'ssl', 587 => 'starttls' );
53  
54 sub smarthost_transports( $self, $report ) {
55 my $smtp = $report->config->{smtp};
56  
57 my %common = (
58 host => $smtp->{smarthost},
59 helo => $report->sendit->smtp->get_helo_hostname,
60 timeout => 32,
61 );
62  
63 my %auth;
64 if ( $smtp->{smartuser} ) {
65 $auth{sasl_username} = $smtp->{smartuser};
66 $auth{sasl_password} = $smtp->{smartpass} if $smtp->{smartpass};
67 }
68  
69 my $configured = $self->smarthost_ssl_configured($report);
70  
71 if ( my $port = $smtp->{smartport} ) {
72 return $self->smarthost_route( \%common, \%auth, $port,
73 $self->smarthost_ssl($report) )
74 if $configured;
75 return $self->smarthost_route( \%common, \%auth, $port,
76 $PORT_TLS{$port} )
77 if $PORT_TLS{$port};
78 return $self->smarthost_route( \%common, \%auth, $port,
79 %auth ? 'starttls' : 'maybestarttls' );
80 }
81  
82 if ($configured) {
83 my $ssl = $self->smarthost_ssl($report);
84 return $self->smarthost_route( \%common, \%auth,
85 ( 'ssl' eq ( $ssl || q{} ) ? 465 : 25 ), $ssl );
86 }
87  
88 return (
89 $self->smarthost_route( \%common, \%auth, 465, 'ssl' ),
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 $self->smarthost_route( \%common, {}, 25, 'maybestarttls' ),
102 $self->smarthost_route( \%common, {}, 587, 'starttls' ),
103 $self->smarthost_route( \%common, {}, 25, 0 ),
104 );
105 }
106  
107 sub smarthost_route( $self, $common, $auth, $port, $ssl ) {
108 return Email::Sender::Transport::SMTP::Persistent->new(
109 { %$common, %$auth, port => $port, ssl => $ssl } );
110 }
111  
112 sub smarthost_ssl_configured( $self, $report ) {
113 my $ssl = $report->config->{smtp}{smartssl};
114 return defined $ssl && $ssl =~ /\S/x ? 1 : 0;
115 }
116  
117 sub smarthost_ssl( $self, $report ) {
118 my $ssl = lc $report->config->{smtp}{smartssl};
119 $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 if 'none' ne $ssl && !$SMART_SSL{$ssl};
124  
125 return 0 if 'none' eq $ssl;
126 return $ssl;
127 }
128  
129 sub builds_smarthost_routes( $self, $report ) {
130 return 0 if $self->{transports_method} || $self->{transports_object};
131 return 0 if $self->{smarthost};
132 return $report->config->{smtp}{smarthost} ? 1 : 0;
133 }
134  
135 # A route that never answers costs a connect timeout on every message.
136 sub keep_smarthost_route( $self, $transport ) {
137 return if 'ARRAY' ne ref $self->{smarthost};
138 return if !grep { refaddr($_) == refaddr($transport) } @{ $self->{smarthost} };
139 $self->{smarthost} = [$transport];
140 return;
141 }
142  
143 # Return a list of transports to try in order.
144 sub get_transports_for( $self, $args ) {
145  
146 # Have we passed a custom transports generation class?
147 if ( $self->{transports_method} ) {
148 my @transports = &{ $self->{transports_method} }($args);
149 return @transports;
150 }
151 if ( $self->{transports_object} ) {
152 my @transports = $self->{transports_object}->get_transports_for($args);
153 return @transports;
154 }
155  
156 my $report = $args->{report};
157  
158 if ( $report->config->{smtp}{smarthost} ) {
159 return @{ $self->{smarthost} } if 'ARRAY' eq ref $self->{smarthost};
160 return ( $self->{smarthost} ) if $self->{smarthost};
161 $self->{smarthost} = [ $self->smarthost_transports($report) ];
162 return @{ $self->{smarthost} };
163 }
164  
165 my @smtp_hosts = $report->sendit->smtp->get_smtp_hosts( $args->{to} );
166  
167 my $log_data = $args->{log_data};
168 $log_data->{smtp_host} = join( ',', @smtp_hosts );
169  
170 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 if ( Email::Sender::Transport::SMTP->can('hosts') ) {
180 return map {
181 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 my @transports;
188 foreach my $ssl ( 'maybestarttls', 0 ) {
189 push @transports,
190 Email::Sender::Transport::SMTP->new(
191 { %common, host => $_, ssl => $ssl } )
192 foreach @smtp_hosts;
193 }
194  
195 return @transports;
196 }
197  
198 sub get_dkim_key($self) {
199 my $report = $self->{report};
200 return $self->{dkim_key} if $self->{dkim_key};
201 if ( $report->config->{report_sign}->{keyfile} ) {
202 try {
203 require Mail::DKIM::PrivateKey;
204 require Mail::DKIM::Signer;
205 require Mail::DKIM::TextWrap;
206 } catch ($e) { };
207 if ( UNIVERSAL::can( 'Mail::DKIM::Signer', "new" ) ) {
208 my $file = $report->config->{report_sign}->{keyfile};
209 $self->{dkim_key} = Mail::DKIM::PrivateKey->load( 'File' => $file, );
210 if ( !$self->{dkim_key} ) {
211 die "Could not load DKIM key $file";
212 }
213 }
214 else {
215 die
216 'DKIM signing requested but Mail::DKIM could not be loaded. Please check that Mail::DKIM is installed.';
217 }
218 $self->log_output('DKIM signing key loaded');
219 return $self->{dkim_key};
220 }
221 }
222  
223 sub run($self) {
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 );
232  
233 openlog( 'dmarc_send_reports', 'pid', LOG_MAIL ) if $self->{syslog};
234 $self->log_output('dmarc_send_reports starting up');
235  
236 $|++;
237 my $report = Mail::DMARC::Report->new();
238 $self->{report} = $report;
239 $report->verbose( $self->{verbose} ) if defined $self->{verbose};
240  
241 # If we have defined a custom transports generation class then
242 # load and instantiate it here.
243 if ( $report->config->{smtp}->{transports} ) {
244 load $report->config->{smtp}->{transports};
245 my $package = $report->config->{smtp}->{transports};
246 my $transports_object = $package->new();
247 $self->set_transports_object($transports_object);
248 }
249  
250 $self->smarthost_ssl($report)
251 if $self->builds_smarthost_routes($report)
252 && $self->smarthost_ssl_configured($report);
253  
254 local $SIG{'ALRM'} = sub { die "timeout\n" };
255  
256 my $batch_do = 1;
257  
258 # 1. get reports, one at a time
259 REPORT:
260 while ( my $aggregate = $report->store->next_todo() ) {
261 try {
262 $self->send_report( $aggregate, $report );
263 }
264 catch ($error) {
265 $self->log_output( 'error sending report: ' . $error );
266 }
267  
268 if ( $batch_do++ > $self->{batch_size} ) {
269 $batch_do = 1;
270 if ( $self->{send_delay} > 0 ) {
271 print "sleeping " . $self->{send_delay} if $self->{verbose};
272 foreach ( 1 .. $self->{send_delay} ) {
273 print '.' if $self->{verbose};
274 sleep 1;
275 }
276 print "done.\n" if $self->{verbose};
277 }
278 }
279  
280 }
281  
282 alarm(0);
283  
284 $self->log_output('dmarc_send_reports done');
285 closelog() if $self->{syslog};
286  
287 return;
288 }
289  
290 # PODNAME: dmarc_send_reports
291 # ABSTRACT: send aggregate reports
292  
293 sub send_report( $self, $aggregate, $report ) {
294  
295 alarm( $self->{alarm_at} );
296  
297 $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 my $report_receivers;
306 try {
307 $report_receivers = $report->uri->parse( $aggregate->policy_published->rua );
308 }
309 catch ($error) {
310 $self->log_output(
311 { 'id' => $aggregate->metadata->report_id,
312 'error' => 'No valid ruas found - deleting report - ' . $error,
313 }
314 );
315 $report->store->delete_report( $aggregate->metadata->report_id );
316 alarm(0);
317 return;
318 }
319  
320 # Check we have some receivers
321 if ( !@$report_receivers ) {
322 $self->log_output(
323 { 'id' => $aggregate->metadata->report_id,
324 'error' => 'No valid ruas found - deleting report',
325 }
326 );
327 $report->store->delete_report( $aggregate->metadata->report_id );
328 alarm(0);
329 return;
330 }
331  
332 # Generate the XML data and associated metadata
333 my $xml = $aggregate->as_xml();
334 my $xml_compressed = $report->compress( \$xml );
335 my $xml_compressed_bytes = length Encode::encode_utf8($xml_compressed);
336  
337 my $sent = 0;
338 my $cc_sent = 0;
339 my @too_big;
340 URI:
341 foreach my $receiver (@$report_receivers) {
342 my $method = $receiver->{uri};
343 my $max = $receiver->{max_bytes};
344  
345 if ( $max && $xml_compressed_bytes > $max ) {
346 $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 push @too_big, $method;
353 next URI;
354 }
355 elsif ( 'mailto:' eq substr( $method, 0, 7 ) ) {
356 my ($to) = ( split /:/, $method )[-1];
357 my $cc = $report->config->{smtp}{cc};
358 if ( $cc && $cc ne 'set.this@for.a.while.example.com' && !$cc_sent ) {
359 $self->email(
360 { to => $cc,
361 compressed => $xml_compressed,
362 aggregate => \$aggregate
363 }
364 );
365 $cc_sent = 1;
366 }
367 $self->email(
368 { to => $to,
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 $sent++;
381 }
382 elsif ( 'https:' eq substr( $method, 0, 6 ) ) {
383  
384 #$report->sendit->http->post( $method, \$aggregate, $shrunk );
385 $sent++;
386 }
387 }
388  
389 if ($sent) {
390 $report->store->delete_report( $aggregate->metadata->report_id );
391 }
392 else {
393 my $send_errors = $report->config->{smtp}->{send_errors} // 1;
394 $self->send_too_big_email( \@too_big, $xml_compressed_bytes, $aggregate )
395 if $send_errors;
396 $report->store->delete_report( $aggregate->metadata->report_id );
397 }
398  
399 alarm(0);
400 return;
401 }
402  
403 sub send_too_big_email( $self, $too_big, $bytes, $aggregate ) {
404 my $report = $self->{report};
405  
406 BIGURI:
407 foreach my $uri (@$too_big) {
408 next BIGURI if 'mailto:' ne substr( $uri, 0, 7 );
409 my ($to) = ( split /:/, $uri )[-1];
410 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 my $mime_object
418 = $report->sendit->smtp->assemble_too_big_message_object( $to, $body );
419 $self->email( { to => $to, mime => $mime_object } );
420 }
421 return;
422 }
423  
424 sub email( $self, $args ) {
425  
426 my $to = $args->{to};
427 if ( !$to ) {
428 $self->log_output( { 'error' => 'No recipient for email' } );
429 croak 'No recipient for email';
430 }
431 my $mime = $args->{mime} // undef;
432 my $compressed = $args->{compressed} // undef;
433 my $agg_ref = $args->{aggregate} // undef;
434 my $report = $self->{report};
435  
436 my $rid;
437 $rid = $$agg_ref->metadata->report_id if $agg_ref;
438  
439 my $log_data = { deliver_to => $to, };
440  
441 my $body;
442 if ($rid) {
443 my $mime_object
444 = $report->sendit->smtp->assemble_message_object( $agg_ref, $to,
445 $compressed );
446 $body = $mime_object->as_string;
447 $log_data->{id} = $rid;
448 $log_data->{to_domain} = $$agg_ref->policy_published->domain;
449 }
450 elsif ($mime) {
451 $body = $mime->as_string;
452 }
453 else {
454 croak 'No email content';
455 }
456  
457 my $dkim_key = $self->get_dkim_key();
458 if ($dkim_key) {
459 my $dkim_algorithm = $report->config->{report_sign}{algorithm};
460 my $dkim_method = $report->config->{report_sign}{method};
461 my $dkim_domain = $report->config->{report_sign}{domain};
462 my $dkim_selector = $report->config->{report_sign}{selector};
463 try {
464 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 $body =~ s/\015?\012/\015\012/g;
472 $dkim->PRINT($body);
473 $dkim->CLOSE;
474 my $signature = $dkim->signature;
475 $body = $signature->as_string . "\015\012" . $body;
476 $log_data->{dkim} = 1;
477 }
478 catch ($error) {
479 print "DKIM Signing error\n\t$error\n" if $self->{verbose};
480 $log_data->{error} = 'DKIM Signing error';
481 $log_data->{error_detail} = $error;
482 $self->log_output($log_data);
483 return;
484 }
485 }
486  
487 my @transports = $self->get_transports_for(
488 { report => $report,
489 log_data => $log_data,
490 to => $to,
491 }
492 );
493 my $success;
494 my $permanent = 0;
495 my $answered = 0;
496 while ( my $transport = shift @transports ) {
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 next if $answered && $transport->can('ssl') && !$transport->ssl;
503  
504 my $done = 0;
505 try {
506 $success = sendmail(
507 $body,
508 { from => $report->config->{organization}{email},
509 to => $to,
510 transport => $transport,
511 }
512 );
513 if ($success) {
514 $log_data->{success} = $success->{message};
515 $done = 1;
516 $self->keep_smarthost_route($transport);
517 }
518 }
519 catch ($error) {
520 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 if ( blessed($error) && $error->isa('Email::Sender::Failure') ) {
526 $code = $error->code;
527 $message = $error->message;
528 }
529 else {
530 $code = 'error';
531 $message = "$error";
532 chomp $message;
533 }
534 $code //= 'error';
535 $message //= 'unknown send failure';
536  
537 # Connect and STARTTLS failures carry no code; anything else means
538 # the host answered.
539 $answered ||= $code ne 'error';
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 $permanent ||= $code =~ /^5/x;
544  
545 my $timed_out = 'timeout' eq $message;
546  
547 $code = join( ', ', $log_data->{send_error_code}, $code )
548 if exists $log_data->{send_error_code};
549 $message = join( ', ', $log_data->{send_error}, $message )
550 if exists $log_data->{send_error};
551 $log_data->{send_error} = $message;
552 $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 if ($timed_out) {
558 $report->store->error( $rid, $message );
559 last;
560 }
561  
562 next if @transports;
563  
564 if ($permanent) {
565  
566 # Perma error
567 $log_data->{deleted} = 1;
568 $report->store->delete_report($rid);
569 $success = 0;
570 last;
571 }
572 $report->store->error( $rid, $message );
573 }
574 last if $done;
575 }
576  
577 $self->log_output($log_data);
578  
579 if ($success) {
580 return 1;
581 }
582 return 0;
583 }
584  
585 sub log_output( $self, $args ) {
586  
587 my $log_level = LOG_INFO;
588 my $log_entry = '';
589  
590 if ( ref $args eq 'HASH' ) {
591  
592 if ( $args->{'log_level'} ) {
593 $log_level = $args->{'log_level'};
594 delete $args->{'log_level'};
595 }
596  
597 my @parts;
598 foreach my $key ( sort keys %$args ) {
599 my $value = $args->{$key} // '';
600 $value =~ s/,/#044/g; # Encode commas
601 push @parts, join( '=', $key, $value );
602 }
603 $log_entry = join( ', ', @parts );
604 }
605 else {
606 $log_entry = $args;
607 }
608  
609 syslog( $log_level, $log_entry ) if $self->{syslog};
610 print "$log_entry\n" if $self->{verbose};
611  
612 return;
613 }
614  
615 1;