lib/Mail/Send/DKIM.pm

filter: Statementstmt 92.3% 132 / 143 Branchbran 81.9% 59 / 72 Conditioncond 72.9% 35 / 48 MC/DCmcdc 54.1% 13 / 24 Subroutinesub 100% 16 / 16 Podpod 100% 6 / 6 totaltotal 84.4% 261 / 309 CC 58 SCAR 23.6
CC
58
Cov
86%
log_send
19.6
new
14.5
sign
9.1
CRAP 10.6
↑ Summary bin/dmarc-report.pl »
1 package Mail::Send::DKIM;
2  
3 # The signing half of bin/mail-dkim: given an object configured with the
4 # domains this host signs for, a message either comes back signed or comes back
5 # as undef and the caller sends it as it is. Nothing here dies on a signing
6 # problem, because a missing key must never stop mail being delivered.
7  
8 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use Mail::DKIM::Signer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Params::Get;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Sys::Syslog ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Object::Configure;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶use version; our $VERSION = version->declare('0.002.0');
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 Mail::Send::DKIM - sign a message with DKIM and hand it to sendmail
21  
22 =head1 VERSION
23  
24 0.002.0
25  
26 =head1 SYNOPSIS
27  
28 use Mail::Send::DKIM;
29  
30 my $dkim = Mail::Send::DKIM->new(
31 domains => ['example.org'],
32 keydir => "$ENV{HOME}/dkim",
33 );
34  
35 my $signed = $dkim->sign($message);
36 $message = $signed if(defined($signed));
37  
38 exit($dkim->send($message, '-t', '-f', 'alerts@example.org'));
39  
40 =head1 DESCRIPTION
41  
42 Signs a message with DKIM when its C<From:> domain is one this host signs for,
43 and hands it to sendmail with whatever arguments sendmail should have. The
44 signed form is C<rsa-sha256> with C<relaxed> canonicalization, which is the
45 shape receiving sites expect.
46  
47 The signature covers the message in the form DKIM defines, where every line
48 ends CRLF. What is handed to sendmail is the local form instead: LF line
49 endings, and no carriage return anywhere, whatever arrived. A CR would be data
50 to the MTA that keeps the message - Postfix and Exim both write the CRLF of
51 SMTP themselves - so a message that had travelled with CRLF in it would reach
52 the receiver with the CRs doubled and fail the signature it had just been given.
53  
54 Nothing here dies because a key is missing: signing either produces a signed
55 message or produces nothing and says why on standard error, so a caller can
56 always fall back to sending the message as it is.
57  
58 =head1 PLATFORMS
59  
60 Windows is not supported. L<Mail::DKIM> needs L<Crypt::OpenSSL::RSA>, and that
61 module and its prerequisites, L<Crypt::OpenSSL::Random> and
62 L<Crypt::OpenSSL::Bignum>, do not link on Windows: Strawberry Perl's static
63 OpenSSL needs the C<crypt32> library, which their F<Makefile.PL> files do not
64 ask for. F<Makefile.PL> stops with C<OS unsupported> there, rather than failing
65 part-way through the install.
66  
67 =head1 ENVIRONMENT
68  
69 B<DKIM_DOMAINS>, B<DKIM_KEYDIR>, B<DKIM_SELECTOR>, B<DKIM_KEYFILE> and
70 B<SENDMAIL> are read by B<new> when the corresponding option is not given, so
71 the command line programs needs no configuration of its own.
72  
73 =head1 PROGRAMS
74  
75 The distribution is named after this module and installs four programs with
76 it, each with a page of its own: C<perldoc bin/mail-dkim>,
77 C<perldoc bin/dmarc-report.pl>, C<perldoc bin/dmarc-check-mail.pl> and
78 C<perldoc bin/dmarc-weekly.sh> in a checkout, and the same pages as manual
79 pages once installed.
80  
81 =over 4
82  
83 =item L<mail-dkim|https://github.com/nigelhorne/Mail-Send-DKIM/blob/master/bin/mail-dkim>
84  
85 The front end to this module: a message on standard input is signed when its
86 C<From:> domain is one named in C<DKIM_DOMAINS>, and handed to sendmail(1) with
87 the arguments it was given. Put it where a script or a crontab calls sendmail
88 and the mail is signed without the script being changed. Its page has the
89 environment variables, the key layout and the exit status.
90  
91 =item L<dmarc-report|https://github.com/nigelhorne/Mail-Send-DKIM/blob/master/bin/dmarc-report.pl>
92  
93 Reads a directory of DMARC aggregate reports and says who sent mail for each
94 domain, marking every source that is not an expected one (C<DMARC_EXPECTED>),
95 every expected one whose mail did not authenticate, and every message that was
96 quarantined or rejected by policy despite passing. The exit status is non-zero
97 when anything needs looking at. Its page has the options.
98  
99 =item L<dmarc-weekly.sh|https://github.com/nigelhorne/Mail-Send-DKIM/blob/master/bin/dmarc-weekly.sh>
100  
101 The cron wrapper around it: silent when the week is clean, a mail with the full
102 output when it is not, and the reports moved aside to an archive afterwards.
103 C<DRY_RUN=1> rehearses a run and C<--crontab> prints the line to paste into a
104 crontab; see L</A CRONTAB ENTRY> below.
105  
106 =item L<dmarc-check-mail.pl|https://github.com/nigelhorne/Mail-Send-DKIM/blob/master/bin/dmarc-check-mail.pl>
107  
108 Sends one message, signed with this module, to a mailbox that can be read, so
109 that the C<Authentication-Results> header at the far end shows what receivers
110 make of this host's mail before the DMARC policy is tightened. C<--plain>
111 sends the same message unsigned, which is what a cron job without the module
112 looks like.
113  
114 =back
115  
116 The three DMARC programs are the reason the module exists: they report on the
117 mail it signs, and the weekly one sends its own report through it.
118  
119 =head1 METHODS
120  
121 =head2 B<new> %options
122  
123 =over 4
124  
125 =item C<domains>
126  
127 The domains to sign for, either an array reference or a string of them
128 separated by commas or spaces. Defaults to C<$ENV{DKIM_DOMAINS}>. A message
129 from any other domain is left alone.
130  
131 =item C<keydir>
132  
133 Where the private keys are. Defaults to C<$ENV{DKIM_KEYDIR}>, or F<~/dkim>.
134 The value is used as it stands: a relative path is taken from the working
135 directory, and C<~> is not expanded - the shell does that, and only when the
136 assignment is written unquoted.
137  
138 =item C<selector>
139  
140 The selector the key is published under, which names the key file
141 F<< <keydir>/<selector>.private >>. Defaults to C<$ENV{DKIM_SELECTOR}>; with
142 neither set, the key file's own name is the selector when B<keyfile> is given,
143 and otherwise the only F<*.private> file in B<keydir> is used and its name is
144 the selector.
145  
146 =item C<keyfile>
147  
148 The private key to sign with, when it does not live at
149 F<< <keydir>/<selector>.private >> - a tester given one key to try, for
150 instance. Defaults to C<$ENV{DKIM_KEYFILE}>. It is used as it stands, and
151 the selector comes from its name unless one is given.
152  
153 =item C<sendmail>
154  
155 The program B<send> hands the message to. Defaults to C<$ENV{SENDMAIL}>, or
156 F</usr/sbin/sendmail>.
157  
158 =back
159  
160 =cut
161  
162 sub new
163 23 ▶{
Subroutine new: called CC=14 SCAR=26.8
Pod documented
164 23 my $class = shift;
165  
166 # Handle hash or hashref arguments
167 23 ▶ my $params = Params::Get::get_params(undef, \@_) // {};
Condition100%2/2
Params::Get::get_params(undef, \@_) // {}
l!l
185
MC/DC100%1/1
Params::Get::get_params(undef, \@_) // {}
Params::Get::get_params(undef, \@_)
Truth table100%2/2
A
A= Params::Get::get_params(undef, \@_)
Aresult
11
00
168  
169 23 $params = Object::Configure::configure($class, $params);
170  
171 23 ▶ my $home = $params->{home} || $ENV{'HOME'} || (getpwuid($<))[7] || '.';
Condition37%3/8
$params->{'home'} || $ENV{'HOME'} || (getpwuid $<)[7] || '.'
l!l
230
$params->{"home"} or $ENV{"HOME"} or (getpwuid $<)[7]
l!l&&r!l&&!r
2300
$params->{"home"} or $ENV{"HOME"}
l!l&&r!l&&!r
0230
MC/DC0%0/3
$params->{'home'} || $ENV{'HOME'} || (getpwuid $<)[7] || '.'
$params->{"home"} $ENV{"HOME"} (getpwuid $<)[7]
Truth table25%1/4
A or B or C
A= $params->{"home"}B= $ENV{"HOME"}C= (getpwuid $<)[7]
ABCresult
1XX1
01X1
0011
0000
172  
173 23 ▶ my $domains = $params->{'domains'} // $ENV{'DKIM_DOMAINS'} // '';
Condition100%5/5
($params->{"domains"} // $ENV{"DKIM_DOMAINS"}) // ""
l!l
176
$params->{"domains"} // $ENV{"DKIM_DOMAINS"}
l!l&&r!l&&!r
1256
MC/DC100%2/2
($params->{"domains"} // $ENV{"DKIM_DOMAINS"}) // ""
$params->{"domains"} $ENV{"DKIM_DOMAINS"}
Truth table100%3/3
A // B
A= $params->{"domains"}B= $ENV{"DKIM_DOMAINS"}
ABresult
1X1
011
000
174 23 ▶ my @domains = ref($domains) eq 'ARRAY' ? @$domains : split(/[\s,]+/, $domains);
Branch: 1 branch
BranchTrueFalseTotal
ref $domains eq 'ARRAY' ? : 11 12 23
175  
176 my $self = {
177 20 ▶ 'domains' => { map { lc($_) => 1 } grep { length } @domains },
Condition93%15/16
($params->{"keydir"} // $ENV{"DKIM_KEYDIR"}) // "$home/dkim"
l!l
212
$params->{"keydir"} // $ENV{"DKIM_KEYDIR"}
l!l&&r!l&&!r
1652
$params->{"selector"} // $ENV{"DKIM_SELECTOR"}
l!l&&r!l&&!r
3119
$params->{"keyfile"} // $ENV{"DKIM_KEYFILE"}
l!l&&r!l&&!r
3020
($params->{"sendmail"} // $ENV{"SENDMAIL"}) // "/usr/sbin/sendmail"
l!l
815
$params->{"sendmail"} // $ENV{"SENDMAIL"}
l!l&&r!l&&!r
4415
MC/DC100%2/2
($params->{"keydir"} // $ENV{"DKIM_KEYDIR"}) // "$home/dkim"
$params->{"keydir"} $ENV{"DKIM_KEYDIR"}
MC/DC100%2/2
$params->{"selector"} // $ENV{"DKIM_SELECTOR"}
$params->{"selector"} $ENV{"DKIM_SELECTOR"}
MC/DC50%1/2
$params->{"keyfile"} // $ENV{"DKIM_KEYFILE"}
$params->{"keyfile"} $ENV{"DKIM_KEYFILE"}
MC/DC100%2/2
($params->{"sendmail"} // $ENV{"SENDMAIL"}) // "/usr/sbin/sendmail"
$params->{"sendmail"} $ENV{"SENDMAIL"}
Truth table100%3/3
A // B
A= $params->{"keydir"}B= $ENV{"DKIM_KEYDIR"}
ABresult
1X1
011
000
Truth table100%3/3
A // B
A= $params->{"selector"}B= $ENV{"DKIM_SELECTOR"}
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $params->{"keyfile"}B= $ENV{"DKIM_KEYFILE"}
ABresult
1X1
011
000
Truth table100%3/3
A // B
A= $params->{"sendmail"}B= $ENV{"SENDMAIL"}
ABresult
1X1
011
000
178 'keydir' => $params->{'keydir'} // $ENV{'DKIM_KEYDIR'} // "$home/dkim",
179 'selector' => $params->{'selector'} // $ENV{'DKIM_SELECTOR'},
180 'keyfile' => $params->{'keyfile'} // $ENV{'DKIM_KEYFILE'},
181 23 'sendmail' => $params->{'sendmail'} // $ENV{'SENDMAIL'} // '/usr/sbin/sendmail',
182 };
183  
184 23 return bless($self, $class);
185 }
186  
187 =head2 B<sign> $message
188  
189 Returns the message with a C<DKIM-Signature> header added, or C<undef> when
190 there is nothing to do or nothing that can be done: the domain is not one of
191 B<domains>, there is no C<From:> header, no key can be found or read, or the
192 signer failed. The reasons are warnings on standard error; the message itself
193 is never altered in the undef case, so the caller sends it unchanged.
194 Carriage returns are folded to LF on the way, so what comes back is in the
195 local form.
196  
197 =cut
198  
199 sub sign
200 18 ▶{
Subroutine sign: called CC=8 SCAR=22.1
Pod documented
201 18 my $self = shift;
202  
203 18 my $params = Params::Get::get_params('message', \@_);
204 18 my $message = $params->{message};
205  
206 18 my $domain = from_domain($message);
207 18 ▶ return unless(defined($domain));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain 1 17 18
208 17 ▶ return unless($self->{'domains'}->{$domain});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"domains"}{$domain} 3 14 17
209  
210 14 my $selector = $self->selector;
211 14 ▶ return unless(defined($selector));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $selector 2 12 14
212  
213 # A key file named outright needs neither the directory nor the naming
214 # convention; otherwise the key is <keydir>/<selector>.private.
215 12 ▶ my $keyfile = $self->{'keyfile'} // $self->{'keydir'} . "/$selector.private";
Condition100%2/2
$self->{"keyfile"} // $self->{"keydir"} . "/$selector.private"
l!l
39
MC/DC100%1/1
$self->{"keyfile"} // $self->{"keydir"} . "/$selector.private"
$self->{"keyfile"}
Truth table100%2/2
A
A= $self->{"keyfile"}
Aresult
11
00
216 12 ▶ unless(-r $keyfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $keyfile) 0 12 12
217 0 warn "Mail::Send::DKIM: no readable key at $keyfile; sending $domain unsigned";
218 0 return;
219 }
220  
221 # DKIM hashes the message in the form it travels in, where every line
222 # ends CRLF, so the signature is made over a CRLF copy of what arrived.
223 12 (my $wire = _local($message)) =~ s/\n/\r\n/g;
224  
225 12 my $signed;
226 eval {
227 12 my $signer = Mail::DKIM::Signer->new(
228 Algorithm => 'rsa-sha256',
229 Method => 'relaxed',
230 Domain => $domain,
231 Selector => $selector,
232 KeyFile => $keyfile,
233 );
234 12 $signer->PRINT($wire);
235 12 $signer->CLOSE;
236 12 $signed = $signer->signature->as_string . "\n" . _local($message);
237 12 1;
238 12 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $signer = "Mail::DKIM::Signer"->new("Algorithm", "rsa-sha256", "Method", "relaxed", "Domain", $domain, "Selector", $selector, "KeyFile", $keyfile); $signer->PRINT($wire); $signer->CLOSE; $signed = $signer->signature->as_string . "\n" . _local($message); 1 } } or do { my $error = $@ || "unknown error"; chomp $error; warn "Mail::Send::DKIM: cannot sign for $domain ($error); sending unsigned"; return } 12 0 12
239 0 ▶ my $error = $@ || 'unknown error';
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
240 0 chomp($error);
241 0 warn "Mail::Send::DKIM: cannot sign for $domain ($error); sending unsigned";
242 0 return;
243 };
244  
245 12 return $signed;
246 }
247  
248 =head2 B<send> $message, @arguments
249  
250 Pipes the message to B<sendmail> with the arguments given - C<-t>, C<-f> and
251 the rest are sendmail's own - and returns C<0> when sendmail accepted the
252 message, sendmail's exit status when it did not, and C<1> when sendmail cannot
253 be run at all. The message is normalised to LF first, so nothing that arrived
254 with CRLF - signed or passed through unsigned - carries a carriage return to
255 the MTA.
256  
257 =cut
258  
259 sub send
260 9 ▶{
Subroutine send: called CC=4 SCAR=13.9
Pod documented
261 9 my ($self, $message, @args) = @_;
262  
263 9 ▶ open(my $sendmail, '|-', $self->{'sendmail'}, @args) or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $sendmail, "|-", $self->{"sendmail"}, @args or do { warn qq[Mail::Send::DKIM: cannot run $self->{"sendmail"}: $!]; return 1 } 8 1 9
264 1 warn "Mail::Send::DKIM: cannot run $self->{'sendmail'}: $!";
265 1 return 1;
266 };
267 8 print $sendmail _local($message);
268  
269 8 ▶ return 0 if(close($sendmail));
Branch: 1 branch
BranchTrueFalseTotal
if close $sendmail 7 1 8
270  
271 1 my $status = $? >> 8;
272  
273 1 ▶ return $status || 1;
Condition50%1/2
$status || 1
l!l
10
MC/DC0%0/1
$status || 1
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
274 }
275  
276 =head2 B<log_send> $message, @arguments
277  
278 Records in syslog, under the identity C<mail-dkim> and the C<mail> facility,
279 the sender, the recipients and the subject of a message that has been sent.
280 The arguments are those given to B<send>. The sender is the address after
281 C<-f> when there is one, and the address in the C<From:> header otherwise. The
282 recipients are the arguments that are not options and, when C<-t> was given,
283 the addresses in the C<To:>, C<Cc:> and C<Bcc:> headers too. A value that is
284 missing is logged as C<->, and control characters in the subject are replaced
285 by spaces so that a message cannot forge a log line.
286  
287 Returns true when the line was written. Logging never stands between a message
288 and its recipient: a syslog that cannot be reached is a warning, and a false
289 return.
290  
291 =cut
292  
293 sub log_send
294 9 ▶{
Subroutine log_send: called CC=18 SCAR=29.7
Pod documented
295 9 my ($self, $message, @args) = @_;
296  
297 9 my $sender;
298 my @recipients;
299 9 my $tflag;
300 9 my $skip; # the argument after an option that takes a value
301 9 foreach my $arg (@args) {
302 16 ▶ if($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) 4 12 16
303 4 ▶ $sender = $arg if($skip eq 'f');
Branch: 1 branch
BranchTrueFalseTotal
if $skip eq "f" 4 0 4
304 4 $skip = 0;
305 4 next;
306 }
307 12 ▶ if($arg =~ /\A-f(.+)/s) {
Branch: 5 branches
BranchTrueFalseTotal
if ($arg =~ /\A-f(.+)/s) { } 0 12 12
elsif ($arg eq '-f') { } 4 8 12
elsif ($arg =~ /\A-[BboprRVNLXAqFC]\z/) { } 0 8 8
elsif ($arg eq '--') { } 0 8 8
elsif ($arg =~ /\A-/) { } 5 3 8
308 0 $sender = $1;
309 } elsif($arg eq '-f') {
310 4 $skip = 'f';
311 } elsif($arg =~ /\A-[BboprRVNLXAqFC]\z/) {
312 0 $skip = 'x';
313 } elsif($arg eq '--') {
314 0 next;
315 } elsif($arg =~ /\A-/) {
316 5 ▶ $tflag = 1 if($arg =~ /\A-[A-Za-z]*t/);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A-[A-Za-z]*t/ 5 0 5
317 } else {
318 3 push(@recipients, $arg);
319 }
320 }
321 9 ▶ $sender //= _addresses(_header($message, 'From'));
Condition66%2/3
$sender //= _addresses(_header($message, "From"))
l!l&&r!l&&!r
450
MC/DC0%0/2
$sender //= _addresses(_header($message, "From"))
$sender _addresses(_header($message, "From"))
Truth table66%2/3
A //= B
A= $senderB= _addresses(_header($message, "From"))
ABresult
1X1
011
000
322 9 ▶ push(@recipients, map { _addresses(_header($message, $_)) } qw(To Cc Bcc)) if($tflag);
Branch: 1 branch
BranchTrueFalseTotal
if $tflag 5 4 9
323  
324 9 my $subject = _header($message, 'Subject');
325 9 ▶ $subject = '-' unless(defined($subject) && length($subject));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subject and length $subject 1 8 9
Condition66%2/3
defined $subject and length $subject
!ll&&!rl&&r
108
MC/DC50%1/2
defined $subject and length $subject
defined $subject length $subject
Truth table66%2/3
A and B
A= defined $subjectB= length $subject
ABresult
0X0
100
111
326 9 $subject =~ s/[[:cntrl:]]/ /g;
327  
328 9 ▶ my $line = sprintf('sender=%s recipient=%s subject=%s',
Branch: 1 branch
BranchTrueFalseTotal
@recipients ? : 7 2 9
Condition50%1/2
$sender || '-'
l!l
90
MC/DC0%0/1
$sender || '-'
$sender
Truth table50%1/2
A
A= $sender
Aresult
11
00
329 $sender || '-', @recipients ? join(',', @recipients) : '-', $subject);
330 9 $line =~ s/[[:cntrl:]]/ /g;
331  
332 9 my $ok = eval {
333 9 Sys::Syslog::openlog('mail-dkim', 'pid,ndelay', 'mail');
334 8 Sys::Syslog::syslog('info', '%s', $line);
335 8 Sys::Syslog::closelog();
336 8 1;
337 };
338 9 ▶ warn "Mail::Send::DKIM: cannot write to syslog: $@" unless($ok);
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 8 9
339  
340 9 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 8 1 9
341 }
342  
343 # The unfolded value of the first header of that name, or undef.
344 sub _header
345 29 ▶{
Subroutine _header: called CC=3 SCAR=11.0
346 29 my ($message, $name) = @_;
347  
348 29 my ($head) = $message =~ /\A(.*?)\r?\n\r?\n/s;
349 29 ▶ $head = $message unless(defined($head));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $head 0 29 29
350 29 $head =~ s/\r?\n[ \t]+/ /g;
351  
352 29 ▶ return $head =~ /^\Q$name\E:[ \t]*(.*?)\s*$/mi ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$head =~ /^\Q$name\E:[ \t]*(.*?)\s*$/im ? : 18 11 29
353 }
354  
355 # The addresses in a header value: the one in angle brackets when there is
356 # one, else the bare address, for each of the comma separated items.
357 sub _addresses
358 20 ▶{
Subroutine _addresses: called CC=6 SCAR=17.9
359 20 my $value = shift;
360  
361 20 ▶ return unless(defined($value));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 10 10 20
362  
363 10 my @found;
364 10 foreach my $item (split(/,(?=(?:[^"]*"[^"]*")*[^"]*\z)/, $value)) {
365 11 ▶ if($item =~ /<([^>]*)>/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /<([^>]*)>/) { } 2 9 11
elsif ($item =~ /([^\s<>()"]+\@[^\s<>()"]+)/) { } 9 0 9
366 2 push(@found, $1);
367 } elsif($item =~ /([^\s<>()"]+\@[^\s<>()"]+)/) {
368 9 push(@found, $1);
369 }
370 }
371  
372 10 ▶ return wantarray ? @found : $found[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 5 10
373 }
374  
375 #
376 # What is handed to sendmail is the local form of the message: LF line endings
377 # and no carriage return anywhere, whatever arrived. A CR is data to the MTA
378 # that keeps the message - Postfix's sendmail_fix_line_endings and Exim both
379 # write the CRLF of SMTP themselves - so one that survived would reach the
380 # receiver doubled, and the message would fail the signature it was just given.
381 sub _local
382 32 ▶{
Subroutine _local: called CC=1 SCAR=0.0
383 32 my $message = shift;
384  
385 32 $message =~ s/\r\n/\n/g;
386 32 $message =~ s/\r//g;
387  
388 32 return $message;
389 }
390  
391 =head2 B<from_domain> $message
392  
393 The domain of the address in the first C<From:> header, in lower case, or
394 C<undef> when there is no such header. Folded headers and display names are
395 understood.
396  
397 =cut
398  
399 #
400 # The domain an address is at, from the first From: header. A header field can
401 # be folded over several lines, so the continuation lines are joined first, and
402 # a display name is dropped in favour of the address in angle brackets.
403 sub from_domain
404 20 ▶{
Subroutine from_domain: called CC=5 SCAR=16.2
Pod documented
405 20 my $message = shift;
406  
407 20 my ($head) = $message =~ /\A(.*?)\r?\n\r?\n/s;
408 20 ▶ $head = $message unless(defined($head));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $head 0 20 20
409 20 $head =~ s/\r?\n[ \t]+/ /g;
410 20 ▶ return unless($head =~ /^From:[ \t]*(.*?)\s*$/mi);
Branch: 1 branch
BranchTrueFalseTotal
unless $head =~ /^From:[ \t]*(.*?)\s*$/im 1 19 20
411  
412 19 my $value = $1;
413 19 my ($address) = $value =~ /<([^>]*)>/;
414 19 ▶ $address = $value unless(defined($address));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $address 18 1 19
415  
416 # An address can be followed by a comment, so the domain is the last
417 # thing that looks like one rather than the end of the string.
418 19 my @domains = $address =~ /\@([A-Za-z0-9.-]+)/g;
419 19 ▶ return unless(@domains);
Branch: 1 branch
BranchTrueFalseTotal
unless @domains 0 19 19
420  
421 19 return lc($domains[-1]);
422 }
423  
424 =head2 B<selector>
425  
426 The selector the message will be signed under: the one given to B<new> or read
427 from C<DKIM_SELECTOR>, otherwise the name of the B<keyfile> when one is given,
428 otherwise the name of the only F<*.private> file in B<keydir>. C<undef> when it
429 cannot be decided - no key, or several keys and no name - and the reason is a
430 warning on standard error. B<sign> asks this before doing anything else.
431  
432 =cut
433  
434 # The key file is <keydir>/<selector>.private. With no selector named the
435 # selector is the name of the only key in the directory, which is the usual
436 # case: one key, published for every domain this host sends as. Anything
437 # ambiguous is a warning rather than a guess.
438 sub selector
439 14 ▶{
Subroutine selector: called CC=7 SCAR=19.7
Pod documented
440 14 my $self = shift;
441  
442 14 my $selector = $self->{'selector'};
443 14 ▶ return $selector if(defined($selector) && length($selector));
Branch: 1 branch
BranchTrueFalseTotal
if defined $selector and length $selector 4 10 14
Condition66%2/3
defined $selector and length $selector
!ll&&!rl&&r
1004
MC/DC50%1/2
defined $selector and length $selector
defined $selector length $selector
Truth table66%2/3
A and B
A= defined $selectorB= length $selector
ABresult
0X0
100
111
444  
445 # A named key file carries its selector in its name, which is the layout
446 # the keys are kept in: <keydir>/<selector>.private.
447 10 ▶ if(defined($self->{'keyfile'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"keyfile"}) 1 9 10
448 1 (my $from_name = $self->{'keyfile'}) =~ s{.*[\\/]}{};
449 1 $from_name =~ s/\.(?:private|key|pem)\z//;
450 1 ▶ return $from_name if(length($from_name));
Branch: 1 branch
BranchTrueFalseTotal
if length $from_name 1 0 1
451 }
452  
453 9 ▶ opendir(my $dh, $self->{'keydir'}) or do {
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $self->{"keydir"} or do { warn qq[Mail::Send::DKIM: cannot read $self->{"keydir"}: $!]; return } 9 0 9
454 0 warn "Mail::Send::DKIM: cannot read $self->{'keydir'}: $!";
455 0 return;
456 };
457 9 my @keys = sort grep { /\.private\z/ } readdir($dh);
458 9 closedir($dh);
459  
460 9 ▶ unless(@keys == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@keys == 1) 2 7 9
461 warn sprintf("Mail::Send::DKIM: %s holds %d key(s); name the selector to choose one",
462 2 $self->{'keydir'}, scalar(@keys));
463 2 return;
464 }
465  
466 7 (my $found = $keys[0]) =~ s/\.private\z//;
467  
468 7 return $found;
469 }
470  
471 7 1;
472  
473 __END__
↑ Summary bin/dmarc-report.pl »