bin/dmarc-check-mail.pl

filter: Statementstmt 84.9% 90 / 106 Branchbran 75.0% 36 / 48 Conditioncond 66.6% 16 / 24 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 93.7% 15 / 16 Podpod n/a totaltotal 78.3% 163 / 208 CC 15 SCAR 13.5
CC
15
Cov
80%
candidate_keys
8.4
find_keyfile
4.8
unusable_address
3.0
CRAP 3.9
« bin/dmarc-report.pl ↑ Summary bin/mail-dkim »
1 #!/usr/bin/env perl
2 #
3 # The POD at the end of this file is this programme's description: perldoc
4 # dmarc-check-mail.pl.
5  
6 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Sys::Hostname qw(hostname);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 # Beside the module in a checkout, and under lib/perl5 when installed with
16 # INSTALL_BASE into a prefix that @INC does not reach on its own.
17 17 ▶use lib grep { -d } map { "$Bin/../$_" } qw(lib lib/perl5);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Kept apart from what the options become, so that the usage text goes on
20 # offering these however the programme was called.
21 my %default = (
22 'to' => $ENV{'DMARC_TEST_TO'} || 'nigel@example.org',
23 'from' => $ENV{'DMARC_TEST_FROM'} || 'alerts@example.org',
24 'domain' => $ENV{'DMARC_TEST_DOMAIN'} || 'example.org',
25 'selector' => $ENV{'DMARC_TEST_SELECTOR'} || 'selector1',
26 'sendmail' => $ENV{'SENDMAIL'} || '/usr/sbin/sendmail',
27 17 ▶ 'keyfile' => $ENV{'DKIM_KEYFILE'},
Condition50%5/10
$ENV{'DMARC_TEST_TO'} || 'nigel@example.org'
l!l
017
$ENV{'DMARC_TEST_FROM'} || 'alerts@example.org'
l!l
017
$ENV{'DMARC_TEST_DOMAIN'} || 'example.org'
l!l
017
$ENV{'DMARC_TEST_SELECTOR'} || 'selector1'
l!l
017
$ENV{'SENDMAIL'} || '/usr/sbin/sendmail'
l!l
017
MC/DC0%0/1
$ENV{'DMARC_TEST_TO'} || 'nigel@example.org'
$ENV{'DMARC_TEST_TO'}
MC/DC0%0/1
$ENV{'DMARC_TEST_FROM'} || 'alerts@example.org'
$ENV{'DMARC_TEST_FROM'}
MC/DC0%0/1
$ENV{'DMARC_TEST_DOMAIN'} || 'example.org'
$ENV{'DMARC_TEST_DOMAIN'}
MC/DC0%0/1
$ENV{'DMARC_TEST_SELECTOR'} || 'selector1'
$ENV{'DMARC_TEST_SELECTOR'}
MC/DC0%0/1
$ENV{'SENDMAIL'} || '/usr/sbin/sendmail'
$ENV{'SENDMAIL'}
Truth table50%1/2
A
A= $ENV{'DMARC_TEST_TO'}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'DMARC_TEST_FROM'}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'DMARC_TEST_DOMAIN'}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'DMARC_TEST_SELECTOR'}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'SENDMAIL'}
Aresult
11
00
28 );
29  
30 17 my %opt = (
31 %default,
32 'plain' => 0,
33 'dry-run' => 0,
34 'save' => undef,
35 );
36  
37 # Nothing that goes wrong once the options have been read is a usage error, and
38 # die() decides a status of its own - whatever errno was left over (2 for a
39 # directory that is not there, 13 for one that is not writable) or sendmail's
40 # own - which would hand out the 2 that means "the options were not usable".
41 # 1 is the status for having sent nothing, whichever failure it was.
42 sub fail
43 6 ▶{
Subroutine fail: called CC=1 SCAR=0.0
44 6 print STDERR @_;
45  
46 6 exit(1);
47 }
48  
49 GetOptions(
50 'to|t=s' => \$opt{'to'},
51 'from|f=s' => \$opt{'from'},
52 'domain=s' => \$opt{'domain'},
53 'selector=s' => \$opt{'selector'},
54 'keyfile|k=s' => \$opt{'keyfile'},
55 'sendmail=s' => \$opt{'sendmail'},
56 'plain' => \$opt{'plain'},
57 'dry-run|n' => \$opt{'dry-run'},
58 'save=s' => \$opt{'save'},
59 0 ▶ 'help|h' => sub { usage(0) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 17 ▶) or usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("to|t=s", \$opt{"to"}, "from|f=s", \$opt{"from"}, "domain=s", \$opt{"domain"}, "selector=s", \$opt{"selector"}, "keyfile|k=s", \$opt{"keyfile"}, "sendmail=s", \$opt{"sendmail"}, "plain", \$opt{"plain"}, "dry-run|n", \$opt{"dry-run"}, "save=s", \$opt{"save"}, "help|h", sub { usage(0); } ) or usage(2) 16 1 17
61  
62 16 ▶usage(2) unless($opt{'to'} && $opt{'from'});
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"to"} and $opt{"from"} 2 14 16
Condition100%3/3
$opt{"to"} and $opt{"from"}
!ll&&!rl&&r
1114
MC/DC100%2/2
$opt{"to"} and $opt{"from"}
$opt{"to"} $opt{"from"}
Truth table100%3/3
A and B
A= $opt{"to"}B= $opt{"from"}
ABresult
0X0
100
111
63 # Both addresses become headers, and --from is sendmail's envelope sender as
64 # well, so neither may hold a space or a control character - the shape of a
65 # header injection - or begin with a - that sendmail would read as an option.
66 14 ▶usage(2) if(unusable_address($opt{'to'}) || unusable_address($opt{'from'}));
Branch: 1 branch
BranchTrueFalseTotal
if unusable_address($opt{"to"}) or unusable_address($opt{"from"}) 5 9 14
Condition100%3/3
unusable_address($opt{"to"}) or unusable_address($opt{"from"})
l!l&&r!l&&!r
329
MC/DC100%2/2
unusable_address($opt{"to"}) or unusable_address($opt{"from"})
unusable_address($opt{"to"}) unusable_address($opt{"from"})
Truth table100%3/3
A or B
A= unusable_address($opt{"to"})B= unusable_address($opt{"from"})
ABresult
1X1
011
000
67  
68 9 my $host = hostname();
69 9 my $token = sprintf('%s-%d', $host, time());
70 9 my $now = strftime('%Y-%m-%d %H:%M:%S %z', localtime());
71  
72 # Sending unsigned from the signing path is a mistake worth stopping: the
73 # whole point is to see a signature arrive. --plain asks for it deliberately.
74 9 my ($keyfile, $mailer);
75 9 ▶if($opt{'plain'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'plain'}) { } 5 4 9
76 5 $mailer = 'sendmail(1), unsigned';
77 } else {
78 4 ▶ $keyfile = $opt{'keyfile'} || find_keyfile();
Condition100%3/3
$opt{'keyfile'} || find_keyfile()
l!l&&r!l&&!r
211
MC/DC100%2/2
$opt{'keyfile'} || find_keyfile()
$opt{'keyfile'} find_keyfile()
Truth table100%3/3
A || B
A= $opt{'keyfile'}B= find_keyfile()
ABresult
1X1
011
000
79 4 ▶ if(!$keyfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keyfile) 1 3 4
80 1 my @candidates = candidate_keys();
81 1 ▶ fail "No DKIM key found: name one with --keyfile or set DKIM_KEYFILE.\n"
Branch: 1 branch
BranchTrueFalseTotal
@candidates ? : 0 1 1
82 . (@candidates ? 'Looked at: ' . join(', ', @candidates) . "\n" : '')
83 . "Use --plain to send unsigned on purpose.\n";
84 }
85 3 ▶ fail "Cannot read $keyfile\n" unless(-r $keyfile);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $keyfile 1 2 3
86 # Loaded here rather than with use, so that --plain still works on a host
87 # without Mail::DKIM: sending unsigned is exactly what it is for there.
88 fail "Mail::DKIM is not installed for $^X, so nothing can be signed.\n"
89 . "Install it, or use --plain to send unsigned on purpose.\n"
90 2 ▶ unless(eval { require Mail::DKIM::Signer; require Mail::Send::DKIM; 1 });
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Mail::DKIM::Signer; require Mail::Send::DKIM; 1 } } 0 2 2
91 2 $mailer = "Mail::Send::DKIM, signed with d=$opt{'domain'} s=$opt{'selector'}";
92 }
93  
94 7 my $subject = "DKIM check from $host at $now";
95 7 my $body = <<"BODY";
96 This is a test message from $host at $now.
97  
98 It was sent by $mailer.
99  
100 Its only purpose is to show, in the headers at the far end, whether mail from
101 this host authenticates. The token below is unique to this run, so the message
102 can be found again among others:
103  
104 $token
105  
106 Nothing replies to it and nothing depends on it.
107 BODY
108  
109 7 my $message = join('',
110 "From: $opt{'from'}\n",
111 "To: $opt{'to'}\n",
112 "Subject: $subject\n",
113 "Date: $now\n",
114 "Message-ID: <$token\@$host>\n",
115 "MIME-Version: 1.0\n",
116 "Content-Type: text/plain; charset=utf-8\n",
117 "Content-Transfer-Encoding: 8bit\n",
118 "\n",
119 $body,
120 );
121  
122 # Sign first, then decide what to do with the result, so that what is printed,
123 # saved and sent are the same bytes - and so that a rehearsal exercises the
124 # signing rather than pretending to.
125 7 ▶my $wire = $opt{'plain'} ? $message : sign($message);
Branch: 1 branch
BranchTrueFalseTotal
$opt{'plain'} ? : 5 2 7
126  
127 7 ▶if($opt{'save'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"save"}) 1 6 7
128 1 ▶ open(my $fh, '>', $opt{'save'}) or fail "Cannot write $opt{'save'}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $opt{"save"} or fail(qq[Cannot write $opt{"save"}: $!\n]) 0 1 1
129 0 print $fh $wire;
130 0 ▶ close($fh) or fail "Cannot close $opt{'save'}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or fail(qq[Cannot close $opt{"save"}: $!\n]) 0 0 0
131 0 print "Message as sent is in $opt{'save'}\n\n";
132 }
133  
134 6 ▶if($opt{'dry-run'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"dry-run"}) 3 3 6
135 3 print $wire;
136 3 print "\n--- nothing was sent; run again without --dry-run ---\n";
137 3 exit(0);
138 }
139  
140 3 ▶open(my $sendmail, '|-', $opt{'sendmail'}, '-t', '-f', $opt{'from'})
Branch: 1 branch
BranchTrueFalseTotal
open my $sendmail, "|-", $opt{"sendmail"}, "-t", "-f", $opt{"from"} or fail(qq[Cannot run $opt{"sendmail"}: $!\n]) 2 1 3
141 or fail "Cannot run $opt{'sendmail'}: $!\n";
142 2 print $sendmail $wire;
143 2 ▶unless(close($sendmail)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $sendmail) 2 0 2
144 2 fail "sendmail reported a failure; the message was not delivered.\n";
145 }
146  
147 0 print "Sent to $opt{'to'} as $opt{'from'}\n";
148 0 print " via : $mailer\n";
149 0 print " token : $token\n\n";
150 0 print "In the mailbox, open that message and choose \"Show original\". You want:\n";
151 0 print " SPF: pass (ip= the sending host)\n";
152 0 print " DKIM: pass (domain=$opt{'domain'}, selector=$opt{'selector'})\n";
153 0 print " DMARC: pass\n\n";
154 0 print "If DKIM is missing, nothing downstream can pass: check that the key is\n";
155 0 print "the one published at $opt{'selector'}._domainkey.$opt{'domain'}, and that\n";
156 0 print "Mail::DKIM is installed for $^X.\n";
157  
158 0 exit(0);
159  
160 # One signing implementation for the repository: Mail::Send::DKIM, which is
161 # what bin/mail-dkim uses, so what this sends is what the site's mail looks
162 # like. The message's own From: domain is allowed as well as --domain, so a
163 # test sent as a different address than the one being signed for still signs.
164 sub sign
165 2 ▶{
Subroutine sign: called CC=2 SCAR=7.2
166 2 my $message = shift;
167  
168 2 require Mail::Send::DKIM;
169  
170 my $dkim = Mail::Send::DKIM->new(
171 'domains' => [$opt{'domain'}, Mail::Send::DKIM::from_domain($message)],
172 'keyfile' => $keyfile,
173 2 'selector' => $opt{'selector'},
174 );
175  
176 2 my $signed = $dkim->sign($message);
177 2 ▶ return $signed if(defined($signed));
Branch: 1 branch
BranchTrueFalseTotal
if defined $signed 2 0 2
178  
179 # The module has already said what went wrong on standard error; this adds
180 # what the tester knows and it does not: which key was meant.
181 0 fail "Cannot sign with $keyfile.\n"
182 . "Check that it is the key published at $opt{'selector'}._domainkey.$opt{'domain'}.\n";
183 }
184  
185 # The site's key is wherever the site keeps it; these are the places it has
186 # lived, so the common case needs no argument.
187 sub candidate_keys
188 3 ▶{
Subroutine candidate_keys: called CC=8 SCAR=21.3
189 9 ▶ my @dirs = grep { defined } (
Branch: 3 branches
BranchTrueFalseTotal
$ENV{'HOME'} ? : 3 0 3
$ENV{'HOME'} ? : 3 0 3
$ENV{'HOME'} ? : 3 0 3
190 $ENV{'HOME'} ? File::Spec->catdir($ENV{'HOME'}, 'dkim') : undef,
191 $ENV{'HOME'} ? File::Spec->catdir($ENV{'HOME'}, '.dkim') : undef,
192 3 $ENV{'HOME'} ? File::Spec->catdir($ENV{'HOME'}, 'lib', 'dkim') : undef,
193 );
194  
195 3 my @keys;
196 3 foreach my $dir (@dirs) {
197 9 ▶ next unless(-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 8 1 9
198 17 ▶ foreach my $name (glob(File::Spec->catfile($dir, '*'))) {
Subroutine BEGIN: called CC=1 SCAR=0.0
199 1 ▶ next unless($name =~ /\.(?:private|key|pem)\z/);
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\.(?:private|key|pem)\z/ 0 1 1
200 1 push @keys, $name;
201 }
202 }
203  
204 3 return @keys;
205 }
206  
207 sub find_keyfile
208 2 ▶{
Subroutine find_keyfile: called CC=4 SCAR=15.8
209 2 foreach my $key (candidate_keys()) {
210 # A directory can hold keys for several domains; the selector names
211 # the one this message must be signed with.
212 1 ▶ return $key if($key =~ /\Q$opt{'selector'}\E/ || $key =~ /\Q$opt{'domain'}\E/);
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\Q$opt{"selector"}\E/ or $key =~ /\Q$opt{"domain"}\E/ 1 0 1
Condition33%1/3
$key =~ /\Q$opt{"selector"}\E/ or $key =~ /\Q$opt{"domain"}\E/
l!l&&r!l&&!r
100
MC/DC0%0/2
$key =~ /\Q$opt{"selector"}\E/ or $key =~ /\Q$opt{"domain"}\E/
$key =~ /\Q$opt{"selector"}\E/ $key =~ /\Q$opt{"domain"}\E/
Truth table33%1/3
A or B
A= $key =~ /\Q$opt{"selector"}\E/B= $key =~ /\Q$opt{"domain"}\E/
ABresult
1X1
011
000
213 }
214  
215 1 return undef;
216 }
217  
218 # An address that cannot be written into a header as it stands, and that would
219 # be read as an option by sendmail if it began with a dash.
220 sub unusable_address
221 25 ▶{
Subroutine unusable_address: called CC=3 SCAR=11.0
222 25 my $address = shift;
223  
224 25 ▶ return 1 if($address =~ /[\x00-\x20\x7f]/);
Branch: 1 branch
BranchTrueFalseTotal
if $address =~ /[\x00-\x20\x7f]/ 3 22 25
225 22 ▶ return 1 if($address =~ /\A-/);
Branch: 1 branch
BranchTrueFalseTotal
if $address =~ /\A-/ 2 20 22
226  
227 20 return 0;
228 }
229  
230 sub usage
231 8 ▶{
Subroutine usage: called CC=2 SCAR=7.1
232 8 ▶ my $status = shift // 0;
Condition50%1/2
shift() // 0
l!l
80
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
233  
234 8 print <<"USAGE";
235 $0 - send one test message from this host, so its authentication can be read
236  
237 Usage:
238 $0 [options]
239  
240 --to ADDR where to send it (default $default{'to'})
241 --from ADDR envelope and header From (default $default{'from'})
242 --domain DOMAIN the domain to sign for (default $default{'domain'})
243 --selector SEL the DKIM selector (default $default{'selector'})
244 --keyfile FILE the DKIM private key (default: found under ~/dkim)
245 --plain send unsigned via sendmail(1), the way cron does
246 --dry-run sign the message, print it, send nothing
247 --save FILE also write the message, as it is sent, to FILE
248 --sendmail PATH sendmail to use (default $default{'sendmail'})
249 --help this text
250  
251 Sent through Mail::Send::DKIM, the signer bin/mail-dkim uses, so that what
252 arrives is what the site's mail actually looks like.
253 USAGE
254  
255 8 exit($status);
256 }
257  
258 =pod
259  
260 =head1 NAME
261  
262 dmarc-check-mail.pl - send one test message from this host, so its authentication can be read
263  
264 =head1 SYNOPSIS
265  
266 dmarc-check-mail.pl
267  
268 dmarc-check-mail.pl --plain
269  
270 dmarc-check-mail.pl --keyfile ~/dkim/selector1.private --domain example.org
271  
272 =head1 DESCRIPTION
273  
274 Sends one message through sendmail(1) to a mailbox that can be read, and says
275 what the C<Authentication-Results> header at the far end should contain. The
276 message is signed with L<Mail::Send::DKIM>, the signer C<mail-dkim> uses, so
277 that what arrives is what this site's mail really looks like, rather than a test
278 of a signing path nothing else uses. C<--plain> sends the same message
279 unsigned, which is what a cron job without the module looks like, so that both
280 can be compared in the same mailbox.
281  
282 The subject and body carry a token made of the host name and the time, so that
283 the message can be found again among others.
284  
285 =head1 OPTIONS
286  
287 =over 4
288  
289 =item B<--to> ADDR (C<-t>)
290  
291 Where to send it. Default C<DMARC_TEST_TO>, or C<nigel@example.org>. It is
292 written into the message's C<To:> header, so an address holding a space or a
293 control character, or beginning with a C<->, is refused, as C<--from> is.
294  
295 =item B<--from> ADDR (C<-f>)
296  
297 The envelope sender and the header C<From:>. Default C<DMARC_TEST_FROM>, or
298 C<alerts@example.org>. An address holding a space or a control character, or
299 beginning with a C<->, is refused.
300  
301 =item B<--domain> DOMAIN
302  
303 The domain to sign for. Default C<DMARC_TEST_DOMAIN>, or C<example.org>. The
304 domain in the message's own C<From:> is signed for as well, so that a test sent
305 as an address from the domain being signed for still signs.
306  
307 =item B<--selector> SEL
308  
309 The selector to sign with. Default C<DMARC_TEST_SELECTOR>, or C<selector1>. It
310 also chooses the key when C<--keyfile> is not given.
311  
312 =item B<--keyfile> FILE (C<-k>)
313  
314 The private key. Default C<DKIM_KEYFILE>, or the first file under C<~/dkim>,
315 C<~/.dkim> or C<~/lib/dkim> named C<*.private>, C<*.key> or C<*.pem> whose name
316 holds the selector or the domain.
317  
318 =item B<--plain>
319  
320 Send the message unsigned, through sendmail(1) alone, which is what cron does.
321 No key is looked for and Mail::DKIM need not be installed, so this still works
322 on a host that cannot sign at all.
323  
324 =item B<--dry-run> (C<-n>)
325  
326 Sign the message, print it, and send nothing.
327  
328 =item B<--save> FILE
329  
330 Also write the message, as it is sent, to FILE.
331  
332 =item B<--sendmail> PATH
333  
334 The sendmail the message is handed to. Default C<SENDMAIL>, or
335 C</usr/sbin/sendmail>.
336  
337 =item B<--help> (C<-h>)
338  
339 Print the usage summary and exit 0.
340  
341 =back
342  
343 Each default above is set by the variable named with it, and nothing else is
344 read.
345  
346 =head1 ENVIRONMENT
347  
348 =over 4
349  
350 =item C<DMARC_TEST_TO>
351  
352 The default for C<--to>.
353  
354 =item C<DMARC_TEST_FROM>
355  
356 The default for C<--from>.
357  
358 =item C<DMARC_TEST_DOMAIN>
359  
360 The default for C<--domain>.
361  
362 =item C<DMARC_TEST_SELECTOR>
363  
364 The default for C<--selector>, which also chooses the key.
365  
366 =item C<SENDMAIL>
367  
368 The default for C<--sendmail>.
369  
370 =item C<DKIM_KEYFILE>
371  
372 The key, when C<--keyfile> is not given.
373  
374 =item C<HOME>
375  
376 Where the key is looked for when neither is given: C<~/dkim>, C<~/.dkim> and
377 C<~/lib/dkim>.
378  
379 =back
380  
381 =head1 EXIT STATUS
382  
383 C<0> when the message was handed to sendmail, printed by C<--dry-run>, or
384 C<--help> was asked for - and the first two do not mean it arrived, or that it
385 authenticated: that is what the headers at the far end say. C<1> when nothing
386 was sent: no key was found, the key could not be read, Mail::DKIM is not
387 installed for this perl, C<--save> could not be written, or sendmail could not
388 be run or reported a failure. Standard error says which. C<2> when the
389 options were not usable - an unknown option, no C<--to> or C<--from>, or a
390 C<--to> or C<--from> that cannot be an address - so nothing was signed or sent,
391 and no transport was asked to do anything. C<2> never means that a message was
392 attempted and failed.
393  
394 =head1 SEE ALSO
395  
396 L<Mail::Send::DKIM>, the signer, and C<mail-dkim>, the sendmail front end that
397 C<dmarc-weekly.sh> sends its alerts through.
398  
399 =cut
« bin/dmarc-report.pl ↑ Summary bin/mail-dkim »