lib/Mail/Send/DKIM.pm

filter: Statementstmt 100% 311 / 311 Branchbran 97.9% 96 / 98 Conditioncond 92.6% 114 / 123 MC/DCmcdc 80.5% 58 / 72 Subroutinesub 100% 60 / 60 Podpod 100% 7 / 7 totaltotal 96.2% 646 / 671 CC 100 SCAR 18.5
CC
100
Cov
98%
new
14.1
_choose_format
13.0
_parse_sendmail_args
11.0
CRAP 6.4
↑ 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 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 # Not :all - that wraps the socket builtin send(), which this package's
11 # documented send() method would then redefine. Sockets are not used here.
12 19 ▶use autodie qw(:file :filesys :system);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 19 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 19 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 19 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use Mail::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 19 ▶use Mail::DKIM::Signer;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 19 ▶use Object::Configure;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 19 ▶use Params::Get;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 19 ▶use Params::Validate::Strict qw(validate_strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 19 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 19 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 # Helpers are private to this package, enforced at call time so that method
25 # dispatch ($self->_helper) is covered as well as plain function calls.
26 19 ▶use Sub::Private { 'mode' => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
27 19 ▶use Sub::Protected;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 19 ▶use Sys::Syslog ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 19 ▶use version; our $VERSION = version->declare('0.004.0');
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # What the signature is made with: the shape receiving sites expect.
33 19 Readonly my $ALGORITHM => 'rsa-sha256';
34 19 Readonly my $CANONICALIZATION => 'relaxed';
35  
36 # Where keys are kept, and what a key file is called.
37 19 Readonly my $KEY_SUFFIX => '.private';
38 19 Readonly my $KEYFILE_NAME_SUFFIX => qr/\.(?:private|key|pem)\z/;
39 19 Readonly my $DEFAULT_KEYDIR_NAME => 'dkim';
40 19 Readonly my $DEFAULT_SENDMAIL => '/usr/sbin/sendmail';
41 19 Readonly my $DOMAIN_SEPARATOR => qr/[\s,]+/;
42 19 Readonly my $DOMAIN_SYNTAX => qr/\A[A-Za-z0-9.-]+\z/;
43  
44 # The exit statuses of send(): sendmail's own, or ours when it never ran.
45 19 Readonly my $EXIT_ACCEPTED => 0;
46 19 Readonly my $EXIT_CANNOT_RUN => 1;
47  
48 # The syslog line. A missing value is logged as a dash so that every field
49 # is always present and the line can be split on spaces.
50 19 Readonly my $SYSLOG_IDENT => 'mail-dkim';
51 19 Readonly my $SYSLOG_OPTIONS => 'pid,ndelay';
52 19 Readonly my $SYSLOG_FACILITY => 'mail';
53 19 Readonly my $SYSLOG_LEVEL => 'info';
54 19 Readonly my $MISSING_VALUE => '-';
55 19 Readonly my @RECIPIENT_HEADERS => qw(To Cc Bcc);
56  
57 # sendmail options that take a value, either attached (-fuser) or as the next
58 # argument (-f user). Knowing them is what stops a value being taken for a
59 # recipient, or a letter inside a value being taken for a flag.
60 19 Readonly my $VALUE_OPTIONS => 'fBboprRVNLXAqFC';
61  
62 # Every sentence this module can say. An entry is a format with named
63 # placeholders, %{name}s or %{name}d, or a hash of such formats chosen by the
64 # arguments' "count" (zero, one, other) and/or "variant" (a gender or other
65 # context, alone or as "variant.count"). A language is added by passing a
66 # "messages" option to new(), which may be set from an Object::Configure file.
67 19 Readonly my %DEFAULT_MESSAGES => (
68 'key_unreadable' => 'Mail::Send::DKIM: no readable key at %{keyfile}s; sending %{domain}s unsigned',
69 'sign_failed' => 'Mail::Send::DKIM: cannot sign for %{domain}s (%{error}s); sending unsigned',
70 'sendmail_failed' => 'Mail::Send::DKIM: cannot run %{sendmail}s: %{os_error}s',
71 'keydir_unreadable' => 'Mail::Send::DKIM: cannot read %{keydir}s: %{os_error}s',
72 'keys_ambiguous' => {
73 'one' => 'Mail::Send::DKIM: %{keydir}s holds %{count}d key; name the selector to choose one',
74 'other' => 'Mail::Send::DKIM: %{keydir}s holds %{count}d keys; name the selector to choose one',
75 },
76 'syslog_failed' => 'Mail::Send::DKIM: cannot write to syslog: %{error}s',
77 'bad_arguments' => 'Mail::Send::DKIM: the arguments of a message must be a hash reference, not %{type}s',
78 'unknown_message' => 'Mail::Send::DKIM: no message is defined for the key "%{key}s"',
79 'missing_argument' => 'Mail::Send::DKIM: the message "%{key}s" needs the argument "%{argument}s"',
80 );
81  
82 # The options new() accepts. Anything else - Object::Configure adds some of its
83 # own - is ignored rather than refused.
84 19 Readonly my %NEW_SCHEMA => (
85 'domains' => { 'type' => ['string', 'arrayref'], 'optional' => 1 },
86 'signing_domain' => { 'type' => 'string', 'optional' => 1 },
87 'keydir' => { 'type' => 'string', 'optional' => 1 },
88 'selector' => { 'type' => 'string', 'optional' => 1 },
89 'keyfile' => { 'type' => 'string', 'optional' => 1 },
90 'sendmail' => { 'type' => 'string', 'optional' => 1 },
91 'home' => { 'type' => 'string', 'optional' => 1 },
92 'messages' => { 'type' => 'hashref', 'optional' => 1 },
93 );
94  
95 =encoding utf-8
96  
97 =head1 NAME
98  
99 Mail::Send::DKIM - sign a message with DKIM and hand it to sendmail
100  
101 =head1 VERSION
102  
103 0.004.0
104  
105 =head1 SYNOPSIS
106  
107 use Mail::Send::DKIM;
108  
109 my $dkim = Mail::Send::DKIM->new(
110 domains => ['example.org'],
111 keydir => "$ENV{HOME}/dkim",
112 );
113  
114 my $signed = $dkim->sign($message);
115 $message = $signed if(defined($signed));
116  
117 exit($dkim->send($message, '-t', '-f', 'alerts@example.org'));
118  
119 More ways to use it:
120  
121 # 1. Settings from the environment (DKIM_DOMAINS, DKIM_KEYDIR, ...)
122 # This is how the mail-dkim program works.
123 my $dkim = Mail::Send::DKIM->new();
124  
125 # 2. One key file that you name yourself, for a test
126 my $dkim = Mail::Send::DKIM->new(
127 domains => 'example.org example.net',
128 keyfile => '/etc/dkim/2026.private',
129 );
130  
131 # 3. Sign and send, then write a line to the system log
132 my @sendmail_args = ('-t', '-f', 'alerts@example.org');
133 my $signed = $dkim->sign($message);
134 $message = $signed if(defined($signed));
135 if($dkim->send($message, @sendmail_args) == 0) {
136 $dkim->log_send($message, @sendmail_args);
137 }
138  
139 # 4. Only ask whose mail it is, or which key would be used
140 my $domain = Mail::Send::DKIM::from_domain($message);
141 my $selector = $dkim->selector();
142  
143 # 5. Warnings in your own words
144 my $dkim = Mail::Send::DKIM->new(
145 messages => { key_unreadable => 'No key at %{keyfile}s' },
146 );
147  
148 =head1 DESCRIPTION
149  
150 Signs a message with DKIM when its C<From:> domain is one this host signs for,
151 and hands it to sendmail with whatever arguments sendmail should have. The
152 signed form is C<rsa-sha256> with C<relaxed> canonicalization, which is the
153 shape receiving sites expect.
154  
155 The signature covers the message in the form DKIM defines, where every line
156 ends CRLF. What is handed to sendmail is the local form instead: LF line
157 endings, and no carriage return anywhere, whatever arrived. A CR would be data
158 to the MTA that keeps the message - Postfix and Exim both write the CRLF of
159 SMTP themselves - so a message that had travelled with CRLF in it would reach
160 the receiver with the CRs doubled and fail the signature it had just been given.
161  
162 Nothing here dies because a key is missing: signing either produces a signed
163 message or produces nothing and says why on standard error, so a caller can
164 always fall back to sending the message as it is.
165  
166 =head1 PLATFORMS
167  
168 Windows is not supported. L<Mail::DKIM> needs L<Crypt::OpenSSL::RSA>, and that
169 module and its prerequisites, L<Crypt::OpenSSL::Random> and
170 L<Crypt::OpenSSL::Bignum>, do not link on Windows: Strawberry Perl's static
171 OpenSSL needs the C<crypt32> library, which their F<Makefile.PL> files do not
172 ask for. F<Makefile.PL> stops with C<OS unsupported> there, rather than failing
173 part-way through the install.
174  
175 =head1 ENVIRONMENT
176  
177 B<DKIM_DOMAINS>, B<DKIM_SIGNING_DOMAIN>, B<DKIM_KEYDIR>, B<DKIM_SELECTOR>,
178 B<DKIM_KEYFILE> and B<SENDMAIL> are read by B<new> when the corresponding option is not given, so
179 the command line programs needs no configuration of its own.
180  
181 =head1 PROGRAMS
182  
183 The distribution is named after this module and installs five programs with
184 it, each with a page of its own: C<perldoc bin/mail-dkim>,
185 C<perldoc bin/dmarc-report.pl>, C<perldoc bin/dmarc-summary>,
186 C<perldoc bin/dmarc-check-mail.pl> and C<perldoc bin/dmarc-weekly.sh> in a
187 checkout, and the same pages as manual pages once installed.
188  
189 =over 4
190  
191 =item L<mail-dkim|https://github.com/nigelhorne/Mail-Send-DKIM/blob/master/bin/mail-dkim>
192  
193 The front end to this module: a message on standard input is signed when its
194 C<From:> domain is one named in C<DKIM_DOMAINS>, and handed to sendmail(1) with
195 the arguments it was given. Put it where a script or a crontab calls sendmail
196 and the mail is signed without the script being changed. Its page has the
197 environment variables, the key layout and the exit status.
198  
199 =item L<dmarc-report|https://github.com/nigelhorne/Mail-Send-DKIM/blob/master/bin/dmarc-report.pl>
200  
201 Reads a directory of DMARC aggregate reports and says who sent mail for each
202 domain, marking every source that is not an expected one (C<DMARC_EXPECTED>),
203 every expected one whose mail did not authenticate, and every message that was
204 quarantined or rejected by policy despite passing. The exit status is non-zero
205 when anything needs looking at. Its page has the options.
206  
207 =item L<dmarc-summary|https://github.com/nigelhorne/Mail-Send-DKIM/blob/master/bin/dmarc-summary>
208  
209 The shorter reading of the same reports: a line per source address with its
210 DKIM and SPF verdicts, the disposition applied, whether the address is one of
211 ours and who owns it, over every report in F<~/Downloads>, plain or gzipped.
212 Where B<dmarc-report.pl> decides whether anything needs acting on and says so
213 in its exit status, this one is for reading.
214  
215 =item L<dmarc-weekly.sh|https://github.com/nigelhorne/Mail-Send-DKIM/blob/master/bin/dmarc-weekly.sh>
216  
217 The cron wrapper around it: silent when the week is clean, a mail with the full
218 output when it is not, and the reports moved aside to an archive afterwards.
219 C<DRY_RUN=1> rehearses a run and C<--crontab> prints the line to paste into a
220 crontab; see L</A CRONTAB ENTRY> below.
221  
222 =item L<dmarc-check-mail.pl|https://github.com/nigelhorne/Mail-Send-DKIM/blob/master/bin/dmarc-check-mail.pl>
223  
224 Sends one message, signed with this module, to a mailbox that can be read, so
225 that the C<Authentication-Results> header at the far end shows what receivers
226 make of this host's mail before the DMARC policy is tightened. C<--plain>
227 sends the same message unsigned, which is what a cron job without the module
228 looks like.
229  
230 =back
231  
232 The four DMARC programs are the reason the module exists: they report on the
233 mail it signs, and the weekly one sends its own report through it.
234  
235 =head1 METHODS
236  
237 =head2 B<new> %options
238  
239 =over 4
240  
241 =item C<domains>
242  
243 The domains to sign for, either an array reference or a string of them
244 separated by commas or spaces. Defaults to C<$ENV{DKIM_DOMAINS}>. A message
245 from any other domain is left alone.
246  
247 =item C<signing_domain>
248  
249 The domain put in the signature (its C<d=> tag), when it is not the domain in
250 the C<From:> header - for a host that signs with the key of a parent or
251 organisational domain, say. Defaults to C<$ENV{DKIM_SIGNING_DOMAIN}>; with
252 neither set, the C<From:> domain is used. Which messages are signed is still
253 decided by B<domains>, which is matched against the C<From:> domain. The
254 receiver finds the public key at
255 F<< <selector>._domainkey.<signing_domain> >>, and DMARC accepts the signature
256 only if that domain aligns with the C<From:> domain.
257  
258 =item C<keydir>
259  
260 Where the private keys are. Defaults to C<$ENV{DKIM_KEYDIR}>, or F<~/dkim>.
261 The value is used as it stands: a relative path is taken from the working
262 directory, and C<~> is not expanded - the shell does that, and only when the
263 assignment is written unquoted.
264  
265 =item C<selector>
266  
267 The selector the key is published under, which names the key file
268 F<< <keydir>/<selector>.private >>. Defaults to C<$ENV{DKIM_SELECTOR}>; with
269 neither set, the key file's own name is the selector when B<keyfile> is given,
270 and otherwise the only F<*.private> file in B<keydir> is used and its name is
271 the selector.
272  
273 =item C<keyfile>
274  
275 The private key to sign with, when it does not live at
276 F<< <keydir>/<selector>.private >> - a tester given one key to try, for
277 instance. Defaults to C<$ENV{DKIM_KEYFILE}>. It is used as it stands, and
278 the selector comes from its name unless one is given.
279  
280 =item C<sendmail>
281  
282 The program B<send> hands the message to. Defaults to C<$ENV{SENDMAIL}>, or
283 F</usr/sbin/sendmail>.
284  
285 =item C<home>
286  
287 The directory the default B<keydir> is made from, as F<< <home>/dkim >>.
288 Defaults to C<$ENV{HOME}>, then the home directory in the password file entry,
289 then the current directory. It matters only when B<keydir> is not given.
290  
291 =item C<messages>
292  
293 A hash reference of message formats that replace or add to the ones this
294 module speaks with, keyed by message key, for a language other than English or
295 wording of your own. See B<i18n> for the format. Because B<new> reads its
296 options through L<Object::Configure>, the same hash can come from a
297 configuration file.
298  
299 =back
300  
301 =head3 PURPOSE
302  
303 Builds the signer. No file is read and no program is run here, so a mistake
304 in B<keydir> or B<keyfile> shows when a message is signed, not when the object
305 is made.
306  
307 =head3 EXAMPLE
308  
309 # Everything from the environment: DKIM_DOMAINS, DKIM_KEYDIR, ...
310 my $dkim = Mail::Send::DKIM->new();
311  
312 # Everything given, a list of domains, a named key and a test sendmail
313 my $dkim = Mail::Send::DKIM->new(
314 domains => 'example.org, example.net',
315 keyfile => '/etc/dkim/2026.private',
316 sendmail => '/usr/local/bin/capture-sendmail',
317 );
318  
319 # A message in another language
320 my $dkim = Mail::Send::DKIM->new(
321 messages => { sendmail_failed => 'sendmail %{sendmail}s: %{os_error}s' },
322 );
323  
324 =head3 API SPECIFICATION
325  
326 =head4 Input (Params::Validate::Strict)
327  
328 {
329 domains => { type => ['string', 'arrayref'], optional => 1 },
330 signing_domain => { type => 'string', optional => 1 },
331 keydir => { type => 'string', optional => 1 },
332 selector => { type => 'string', optional => 1 },
333 keyfile => { type => 'string', optional => 1 },
334 sendmail => { type => 'string', optional => 1 },
335 home => { type => 'string', optional => 1 },
336 messages => { type => 'hashref', optional => 1 },
337 }
338  
339 Options not listed are ignored. The options may be given as a hash or as a
340 hash reference.
341  
342 =head4 Output (Return::Set)
343  
344 { type => 'object', isa => 'Mail::Send::DKIM' }
345  
346 =head3 MESSAGES
347  
348 An option of the wrong type is refused with the message of
349 L<Params::Validate::Strict>, which names the option. Nothing else is said.
350  
351 =head3 PSEUDOCODE
352  
353 options = get_params(args)
354 options = Object::Configure(options) # config files, environment
355 options = validate(options, NEW_SCHEMA) # croak on a wrong type
356 home = options.home or $HOME or passwd entry or "."
357 domains = split(options.domains or $DKIM_DOMAINS), lower-cased, as a set
358 messages = defaults overlaid with options.messages
359 return object(domains, keydir, selector, keyfile, sendmail, messages)
360  
361 =cut
362  
363 sub new
364 162 ▶{
Subroutine new: called CC=14 SCAR=26.4
Pod documented
365 162 my $class = shift;
366  
367 # Handle hash or hashref arguments
368 162 ▶ my $params = Params::Get::get_params(undef, \@_) // {};
Condition100%2/2
Params::Get::get_params(undef, \@_) // {}
l!l
14714
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
369  
370 161 $params = Object::Configure::configure($class, $params);
371 160 $params = validate_strict(
372 'schema' => \%NEW_SCHEMA,
373 'input' => $params,
374 'unknown_parameter_handler' => 'ignore',
375 );
376  
377 154 ▶ my $home = $params->{'home'} || $ENV{'HOME'} || (getpwuid($<))[7] || '.';
Condition62%5/8
$params->{'home'} || $ENV{'HOME'} || (getpwuid $<)[7] || '.'
l!l
1540
$params->{"home"} or $ENV{"HOME"} or (getpwuid $<)[7]
l!l&&r!l&&!r
15220
$params->{"home"} or $ENV{"HOME"}
l!l&&r!l&&!r
01522
MC/DC0%0/3
$params->{'home'} || $ENV{'HOME'} || (getpwuid $<)[7] || '.'
$params->{"home"} $ENV{"HOME"} (getpwuid $<)[7]
Truth table50%2/4
A or B or C
A= $params->{"home"}B= $ENV{"HOME"}C= (getpwuid $<)[7]
ABCresult
1XX1
01X1
0011
0000
378  
379 my $self = {
380 'domains' => _domain_set($params->{'domains'} // $ENV{'DKIM_DOMAINS'}),
381 'signing_domain' => _signing_domain($params->{'signing_domain'} // $ENV{'DKIM_SIGNING_DOMAIN'}),
382 'keydir' => $params->{'keydir'} // $ENV{'DKIM_KEYDIR'} // "$home/$DEFAULT_KEYDIR_NAME",
383 'selector' => $params->{'selector'} // $ENV{'DKIM_SELECTOR'},
384 'keyfile' => $params->{'keyfile'} // $ENV{'DKIM_KEYFILE'},
385 'sendmail' => $params->{'sendmail'} // $ENV{'SENDMAIL'} // $DEFAULT_SENDMAIL,
386 # Shallow overlay: a caller's entry replaces the default one whole.
387 154 ▶ 'messages' => { %DEFAULT_MESSAGES, %{ $params->{'messages'} // {} } },
Condition100%25/25
$params->{"domains"} // $ENV{"DKIM_DOMAINS"}
l!l&&r!l&&!r
2414116
$params->{"signing_domain"} // $ENV{"DKIM_SIGNING_DOMAIN"}
l!l&&r!l&&!r
22150
($params->{"keydir"} // $ENV{"DKIM_KEYDIR"}) // "$home/$DEFAULT_KEYDIR_NAME"
l!l
13519
$params->{"keydir"} // $ENV{"DKIM_KEYDIR"}
l!l&&r!l&&!r
128719
$params->{"selector"} // $ENV{"DKIM_SELECTOR"}
l!l&&r!l&&!r
93142
$params->{"keyfile"} // $ENV{"DKIM_KEYFILE"}
l!l&&r!l&&!r
172135
($params->{"sendmail"} // $ENV{"SENDMAIL"}) // $DEFAULT_SENDMAIL
l!l&&r!l&&!r
331156
$params->{"sendmail"} // $ENV{"SENDMAIL"}
l!l&&r!l&&!r
267121
$params->{"messages"} // {}
l!l
6148
MC/DC100%2/2
$params->{"domains"} // $ENV{"DKIM_DOMAINS"}
$params->{"domains"} $ENV{"DKIM_DOMAINS"}
MC/DC100%2/2
$params->{"signing_domain"} // $ENV{"DKIM_SIGNING_DOMAIN"}
$params->{"signing_domain"} $ENV{"DKIM_SIGNING_DOMAIN"}
MC/DC100%2/2
($params->{"keydir"} // $ENV{"DKIM_KEYDIR"}) // "$home/$DEFAULT_KEYDIR_NAME"
$params->{"keydir"} $ENV{"DKIM_KEYDIR"}
MC/DC100%2/2
$params->{"selector"} // $ENV{"DKIM_SELECTOR"}
$params->{"selector"} $ENV{"DKIM_SELECTOR"}
MC/DC100%2/2
$params->{"keyfile"} // $ENV{"DKIM_KEYFILE"}
$params->{"keyfile"} $ENV{"DKIM_KEYFILE"}
MC/DC100%3/3
($params->{"sendmail"} // $ENV{"SENDMAIL"}) // $DEFAULT_SENDMAIL
$params->{"sendmail"} $ENV{"SENDMAIL"} $DEFAULT_SENDMAIL
MC/DC100%1/1
$params->{"messages"} // {}
$params->{"messages"}
Truth table100%3/3
A // B
A= $params->{"domains"}B= $ENV{"DKIM_DOMAINS"}
ABresult
1X1
011
000
Truth table100%3/3
A // B
A= $params->{"signing_domain"}B= $ENV{"DKIM_SIGNING_DOMAIN"}
ABresult
1X1
011
000
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 table100%3/3
A // B
A= $params->{"keyfile"}B= $ENV{"DKIM_KEYFILE"}
ABresult
1X1
011
000
Truth table100%4/4
A // B // C
A= $params->{"sendmail"}B= $ENV{"SENDMAIL"}C= $DEFAULT_SENDMAIL
ABCresult
1XX1
01X1
0011
0000
Truth table100%2/2
A
A= $params->{"messages"}
Aresult
11
00
388 };
389  
390 154 return bless($self, $class);
391 }
392  
393 =head2 B<sign> $message
394  
395 Returns the message with a C<DKIM-Signature> header added, or C<undef> when
396 there is nothing to do or nothing that can be done: the domain is not one of
397 B<domains>, there is no C<From:> header, no key can be found or read, or the
398 signer failed. The reasons are warnings on standard error; the message itself
399 is never altered in the undef case, so the caller sends it unchanged.
400 Carriage returns are folded to LF on the way, so what comes back is in the
401 local form.
402  
403 =head3 PURPOSE
404  
405 Signs the message when, and only when, a key for its C<From:> domain can be
406 found and used. Nothing is raised for a failure to sign: the caller sends the
407 message as it is.
408  
409 The key must be a regular file that can be read. A directory whose name ends
410 in F<.private> is not a key, and is reported as one that cannot be read.
411  
412 =head3 EXAMPLE
413  
414 my $signed = $dkim->sign($message);
415 $message = $signed if(defined($signed));
416  
417 =head3 API SPECIFICATION
418  
419 =head4 Input (Params::Validate::Strict)
420  
421 { message => { type => 'string' } }
422  
423 An undefined message is treated as an empty one, which has no C<From:>. It
424 may be given positionally or as C<< message => $text >>.
425  
426 =head4 Output (Return::Set)
427  
428 { type => 'string', optional => 1 }
429  
430 The signed message in the local form, or C<undef>.
431  
432 =head3 MESSAGES
433  
434 Message key Text Meaning and resolution
435 key_unreadable no readable key at FILE; sending DOMAIN The key file is absent or not
436 unsigned readable: fix the path or the mode.
437 sign_failed cannot sign for DOMAIN (REASON); ... Mail::DKIM refused the key or
438 the message: see REASON.
439 keydir_unreadable, keys_ambiguous As for selector, below.
440  
441 Each is a warning, and the return is C<undef>.
442  
443 =cut
444  
445 sub sign
446 61 ▶{
Subroutine sign: called CC=4 SCAR=13.9
Pod documented
447 61 my $self = shift;
448  
449 61 my $params = Params::Get::get_params('message', \@_);
450 60 ▶ my $message = validate_strict('schema' => { 'message' => { 'type' => 'string' } }, 'input' => $params)->{'message'} // '';
Condition100%2/2
validate_strict("schema", {"message", {"type", "string"}}, "input", $params)->{"message"} // ""
l!l
542
MC/DC100%1/1
validate_strict("schema", {"message", {"type", "string"}}, "input", $params)->{"message"} // ""
validate_strict("schema", {"message", {"type", "string"}}, "input", $params)->{"message"}
Truth table100%2/2
A
A= validate_strict("schema", {"message", {"type", "string"}}, "input", $params)->{"message"}
Aresult
11
00
451  
452 56 my $domain = from_domain($message);
453  
454 # Only a domain this host signs for is worth looking for a key.
455 56 ▶ return (defined($domain) && $self->{'domains'}->{$domain}) ? $self->_sign_for($domain, $message) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $domain && $self->{'domains'}{$domain} ? : 23 33 56
Condition100%3/3
defined $domain and $self->{"domains"}{$domain}
!ll&&!rl&&r
191423
MC/DC100%2/2
defined $domain and $self->{"domains"}{$domain}
defined $domain $self->{"domains"}{$domain}
Truth table100%3/3
A and B
A= defined $domainB= $self->{"domains"}{$domain}
ABresult
0X0
100
111
456 }
457  
458 # Purpose: Find the key for a domain we sign for and sign with it.
459 # Entry criteria: $domain is a lower-case domain in the signing set; $message
460 # is the message as it arrived.
461 # Exit status: The signed message in the local form, or undef - after a
462 # warning that says why - when there is no usable key.
463 # Side effects: Reads the key file; may warn.
464 27 ▶sub _sign_for :Private
Subroutine _sign_for: called CC=6 SCAR=17.9
465 {
466 27 my ($self, $domain, $message) = @_;
467  
468 27 my $selector = $self->selector();
469 27 my $signed;
470  
471 27 ▶ if(defined($selector)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $selector) 22 5 27
472 # A key file named outright needs neither the directory nor the naming
473 # convention; otherwise the key is <keydir>/<selector>.private.
474 22 ▶ my $keyfile = $self->{'keyfile'} // "$self->{'keydir'}/$selector$KEY_SUFFIX";
Condition100%2/2
$self->{"keyfile"} // qq[$self->{"keydir"}/$selector$KEY_SUFFIX]
l!l
418
MC/DC100%1/1
$self->{"keyfile"} // qq[$self->{"keydir"}/$selector$KEY_SUFFIX]
$self->{"keyfile"}
Truth table100%2/2
A
A= $self->{"keyfile"}
Aresult
11
00
475  
476 # A directory is readable too, but it is not a key.
477 22 ▶ if(-f $keyfile && -r $keyfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $keyfile and -r $keyfile) { } 18 4 22
Condition100%3/3
-f $keyfile and -r $keyfile
!ll&&!rl&&r
3118
MC/DC100%2/2
-f $keyfile and -r $keyfile
-f $keyfile -r $keyfile
Truth table100%3/3
A and B
A= -f $keyfileB= -r $keyfile
ABresult
0X0
100
111
478 18 ▶ $signed = $self->_dkim_sign($self->{'signing_domain'} // $domain, $selector, $keyfile, $message);
Condition66%2/3
$self->{"signing_domain"} // $domain
l!l&&r!l&&!r
2160
MC/DC0%0/2
$self->{"signing_domain"} // $domain
$self->{"signing_domain"} $domain
Truth table66%2/3
A // B
A= $self->{"signing_domain"}B= $domain
ABresult
1X1
011
000
479 } else {
480 4 $self->_say('key_unreadable', { 'keyfile' => $keyfile, 'domain' => $domain });
481 }
482 }
483  
484 27 return $signed;
485 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
486  
487 # Purpose: Make the DKIM signature and put it on the front of the message.
488 # Entry criteria: $keyfile is readable; $selector and $domain are not empty.
489 # Exit status: The signed message in the local form, or undef after a warning
490 # when Mail::DKIM could not sign (a key it cannot load, say).
491 # Side effects: May warn. Nothing is raised: a failure to sign must not
492 # become a failure to deliver.
493 25 ▶sub _dkim_sign :Private
Subroutine _dkim_sign: called CC=3 SCAR=11.0
494 {
495 25 my ($self, $domain, $selector, $keyfile, $message) = @_;
496  
497 # eval sets $@, which belongs to the caller.
498 25 local $@ = '';
499  
500 # DKIM hashes the message in the form it travels in, where every line
501 # ends CRLF, so the signature is made over a CRLF copy of what arrived,
502 # while what is returned has the local line endings.
503 25 my $local = _local($message);
504 25 (my $wire = $local) =~ s/\n/\r\n/g;
505  
506 25 my $signed = eval {
507 25 my $signer = Mail::DKIM::Signer->new(
508 'Algorithm' => $ALGORITHM,
509 'Method' => $CANONICALIZATION,
510 'Domain' => $domain,
511 'Selector' => $selector,
512 'KeyFile' => $keyfile,
513 );
514 22 $signer->PRINT($wire);
515 21 $signer->CLOSE();
516  
517 20 $signer->signature->as_string() . "\n" . $local;
518 };
519 25 ▶ $self->_say('sign_failed', { 'domain' => $domain, 'error' => _trim_error($@ || 'unknown error') }) unless(defined($signed));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $signed 6 19 25
Condition50%1/2
$@ || 'unknown error'
l!l
60
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
520  
521 25 return $signed;
522 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
523  
524 =head2 B<send> $message, @arguments
525  
526 Pipes the message to B<sendmail> with the arguments given - C<-t>, C<-f> and
527 the rest are sendmail's own - and returns C<0> when sendmail accepted the
528 message, sendmail's exit status when it did not, and C<1> when sendmail cannot
529 be run at all. The message is normalised to LF first, so nothing that arrived
530 with CRLF - signed or passed through unsigned - carries a carriage return to
531 the MTA.
532  
533 =head3 PURPOSE
534  
535 Delivers the message to the local MTA and reports what the MTA said.
536  
537 A B<sendmail> that is an empty string is the same failure as a program that is
538 not there: the return is C<1> and the message is C<sendmail_failed>. The
539 caller's C<$@>, C<$?> and C<$SIG{PIPE}> are the same after the call as before
540 it.
541  
542 =head3 EXAMPLE
543  
544 my $status = $dkim->send($message, '-t', '-f', 'alerts@example.org');
545 exit($status);
546  
547 =head3 API SPECIFICATION
548  
549 =head4 Input (Params::Validate::Strict)
550  
551 {
552 message => { type => 'string' },
553 arguments => { type => 'arrayref', optional => 1 }, # the rest of @_
554 }
555  
556 =head4 Output (Return::Set)
557  
558 { type => 'integer', min => 0, max => 255 }
559  
560 =head3 MESSAGES
561  
562 Message key Meaning and resolution
563 sendmail_failed The sendmail program could not be started (not found,
564 not executable): fix the sendmail option or SENDMAIL.
565 The return is 1.
566  
567 A sendmail that exits before it has read the whole message does not stop the
568 caller with SIGPIPE: its exit status is returned.
569  
570 =head3 PSEUDOCODE
571  
572 ignore SIGPIPE's default action (a handler, so sendmail does not inherit it)
573 open a pipe to sendmail with the arguments, as a list so no shell is involved
574 if that fails: warn sendmail_failed, return 1
575 write the local form of the message, then close the pipe
576 if the write and the close both succeeded: return 0
577 return sendmail's exit status, or 1 when that would have been 0
578  
579 =cut
580  
581 # send() is the documented name of this method, though perl has a builtin of that
582 # name: the builtin is for sockets, which this package does not use.
583 sub send ## no critic (Subroutines::ProhibitBuiltinHomonyms)
584 41 ▶{
Subroutine send: called CC=8 SCAR=20.8
Pod documented
585 41 my ($self, $message, @args) = @_;
586  
587 # A sendmail that cannot be run, or that refuses the message, is a status
588 # for the caller, not an exception, so autodie stands aside for the pipe.
589 19 ▶ no autodie qw(open close);
Subroutine BEGIN: called CC=1 SCAR=0.0
590 # perl would add its own "Can't exec" to the warning given below.
591 19 ▶ no warnings 'exec';
Subroutine BEGIN: called CC=1 SCAR=0.0
592 # A sendmail that refuses a message may exit before reading it, and the
593 # write would then kill us with SIGPIPE before its status could be read.
594 # A handler, not IGNORE: an ignored signal is also ignored by the child.
595 41 ▶ local $SIG{'PIPE'} = sub { };
Subroutine __ANON__: called CC=1 SCAR=0.0
596 # close() leaves sendmail's wait status in $?, which belongs to the caller.
597 41 local $? = 0;
598  
599 41 my $status = $EXIT_CANNOT_RUN;
600  
601 # An empty name would make perl complain about the open itself, not about the program.
602 41 ▶ if(length($self->{'sendmail'}) && open(my $sendmail, '|-', $self->{'sendmail'}, @args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'sendmail'} and open my $sendmail, '|-', $self->{'sendmail'}, @args) { } 31 10 41
Condition100%3/3
length $self->{"sendmail"} and open my $sendmail, "|-", $self->{"sendmail"}, @args
!ll&&!rl&&r
1931
MC/DC100%2/2
length $self->{"sendmail"} and open my $sendmail, "|-", $self->{"sendmail"}, @args
length $self->{"sendmail"} open my $sendmail, "|-", $self->{"sendmail"}, @args
Truth table100%3/3
A and B
A= length $self->{"sendmail"}B= open my $sendmail, "|-", $self->{"sendmail"}, @args
ABresult
0X0
100
111
603 31 ▶ my $written = print {$sendmail} _local($message // '');
Condition100%2/2
$message // ""
l!l
301
MC/DC100%1/1
$message // ""
$message
Truth table100%2/2
A
A= $message
Aresult
11
00
604  
605 # close() waits for sendmail; its exit status is in $? afterwards.
606 31 ▶ $status = close($sendmail) ? ($written ? $EXIT_ACCEPTED : $EXIT_CANNOT_RUN) : (($? >> 8) || $EXIT_CANNOT_RUN);
Branch: 2 branches
BranchTrueFalseTotal
close $sendmail ? : 26 5 31
$written ? : 26 0 26
Condition66%2/3
$? >> 8 or $EXIT_CANNOT_RUN
l!l&&r!l&&!r
410
MC/DC0%0/2
$? >> 8 or $EXIT_CANNOT_RUN
$? >> 8 $EXIT_CANNOT_RUN
Truth table66%2/3
A or B
A= $? >> 8B= $EXIT_CANNOT_RUN
ABresult
1X1
011
000
607 } else {
608 10 ▶ local $! = ENOENT unless(length($self->{'sendmail'}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"sendmail"} 1 9 10
609 10 $self->_say('sendmail_failed', { 'sendmail' => $self->{'sendmail'}, 'os_error' => "$!" });
610 }
611  
612 41 return $status;
613 }
614  
615 =head2 B<log_send> $message, @arguments
616  
617 Records in syslog, under the identity C<mail-dkim> and the C<mail> facility,
618 the sender, the recipients and the subject of a message that has been sent.
619 The arguments are those given to B<send>. The sender is the address after
620 C<-f> when there is one, and the address in the C<From:> header otherwise. The
621 recipients are the arguments that are not options and, when C<-t> was given,
622 the addresses in the C<To:>, C<Cc:> and C<Bcc:> headers too. A value that is
623 missing is logged as C<->, and control characters in the subject are replaced
624 by spaces so that a message cannot forge a log line.
625  
626 Returns true when the line was written. Logging never stands between a message
627 and its recipient: a syslog that cannot be reached is a warning, and a false
628 return.
629  
630 =head3 PURPOSE
631  
632 Leaves a trace, in the system log, of every message that was passed on.
633  
634 The arguments are read the way sendmail reads them. These options take a
635 value, which is the rest of the argument or else the next argument, and the
636 value is never a recipient: C<-f -F -o -b -p -r -R -V -N -L -X -A -q -C -B>.
637 Only C<-f> is used. A C<-f> with an empty value is a null sender, and is
638 logged as C<->. C<-t> counts when it is alone or in a group of flags
639 (C<-ti>), but not when it is inside a value, as in C<-FNigelTest>, and not in
640 C<-bt>. An empty argument names nobody. The members of an empty group, such
641 as C<undisclosed-recipients:;>, are not recipients.
642  
643 =head3 EXAMPLE
644  
645 $dkim->log_send($message, '-t', '-f', 'alerts@example.org')
646 if($dkim->send($message, '-t', '-f', 'alerts@example.org') == 0);
647  
648 =head3 API SPECIFICATION
649  
650 =head4 Input (Params::Validate::Strict)
651  
652 {
653 message => { type => 'string' },
654 arguments => { type => 'arrayref', optional => 1 },
655 }
656  
657 =head4 Output (Return::Set)
658  
659 { type => 'boolean' }
660  
661 =head3 MESSAGES
662  
663 Message key Meaning and resolution
664 syslog_failed openlog or syslog failed: check that a syslog daemon is
665 running and its socket is reachable. The return is 0.
666  
667 =head3 PSEUDOCODE
668  
669 parse the arguments into (sender, recipients, whether -t was given), where an
670 option that takes a value, attached or separate, never yields a recipient
671 parse the headers of the message once
672 sender = the -f value, else the first address of From
673 if -t was given: add the addresses of To, Cc and Bcc to the recipients
674 subject = the Subject, or "-"
675 make the line, replace control characters by spaces
676 write it to syslog; if that fails, warn syslog_failed and return 0
677 return 1
678  
679 =cut
680  
681 sub log_send
682 48 ▶{
Subroutine log_send: called CC=9 SCAR=22.0
Pod documented
683 48 my ($self, $message, @args) = @_;
684  
685 48 my $parsed = _parse_sendmail_args(\@args);
686 48 ▶ my $headers = _parse_headers($message // '');
Condition100%2/2
$message // ""
l!l
471
MC/DC100%1/1
$message // ""
$message
Truth table100%2/2
A
A= $message
Aresult
11
00
687  
688 48 my @recipients = @{ $parsed->{'recipients'} };
689 # With -t sendmail reads the recipients from the headers as well.
690 48 ▶ push(@recipients, map { @{ _addresses($headers->{lc($_)}) } } @RECIPIENT_HEADERS) if($parsed->{'reads_headers'});
Branch: 1 branch
BranchTrueFalseTotal
if $parsed->{"reads_headers"} 14 34 48
691  
692 48 my $subject = $headers->{'subject'};
693 48 ▶ $subject = $MISSING_VALUE unless(defined($subject) && length($subject));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subject and length $subject 19 29 48
Condition100%3/3
defined $subject and length $subject
!ll&&!rl&&r
18129
MC/DC100%2/2
defined $subject and length $subject
defined $subject length $subject
Truth table100%3/3
A and B
A= defined $subjectB= length $subject
ABresult
0X0
100
111
694  
695 # -f wins over the From: header, and an empty -f (a bounce) stays empty.
696 48 ▶ my $sender = $parsed->{'sender'} // _addresses($headers->{'from'})->[0];
Condition100%3/3
$parsed->{"sender"} // _addresses($headers->{"from"})->[0]
l!l&&r!l&&!r
11343
MC/DC100%2/2
$parsed->{"sender"} // _addresses($headers->{"from"})->[0]
$parsed->{"sender"} _addresses($headers->{"from"})->[0]
Truth table100%3/3
A // B
A= $parsed->{"sender"}B= _addresses($headers->{"from"})->[0]
ABresult
1X1
011
000
697  
698 48 ▶ my $line = sprintf('sender=%s recipient=%s subject=%s',
Branch: 1 branch
BranchTrueFalseTotal
@recipients ? : 33 15 48
Condition66%2/3
$sender || $MISSING_VALUE
l!l&&r!l&&!r
4440
MC/DC0%0/2
$sender || $MISSING_VALUE
$sender $MISSING_VALUE
Truth table66%2/3
A || B
A= $senderB= $MISSING_VALUE
ABresult
1X1
011
000
699 $sender || $MISSING_VALUE,
700 @recipients ? join(',', @recipients) : $MISSING_VALUE,
701 $subject);
702  
703 # The subject and the addresses come from the message, so a line break in
704 # any of them would otherwise let it forge a second log line.
705 48 $line =~ s/[[:cntrl:]]/ /g;
706  
707 48 ▶ return $self->_write_syslog($line) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_write_syslog($line) ? : 44 4 48
708 }
709  
710 # Purpose: Split the arguments sendmail was given into what the log needs.
711 # Entry criteria: $args is an array reference of the arguments, as given.
712 # Exit status: A hash reference: sender (the -f value, or absent),
713 # recipients (array reference), reads_headers (true for -t).
714 # Side effects: None.
715 74 ▶sub _parse_sendmail_args :Private
Subroutine _parse_sendmail_args: called CC=11 SCAR=24.0
716 {
717 74 my $args = shift;
718  
719 74 my %parsed = ('recipients' => []);
720 # The letter of an option that is still waiting for its value.
721 74 my $pending;
722  
723 74 foreach my $arg (@{$args}) {
724 10111 ▶ if(defined($pending)) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $pending) { } 15 10096 10111
elsif ($arg eq '--') { } 3 10093 10096
elsif ($arg =~ /\A-([$VALUE_OPTIONS])(.*)\z/s) { } 33 10060 10093
elsif ($arg =~ /\A-(.*)\z/s) { } 21 10039 10060
elsif (length $arg) { } 10036 3 10039
725 # The value of -f is the sender; the others are not logged.
726 15 ▶ $parsed{'sender'} = $arg if($pending eq 'f');
Branch: 1 branch
BranchTrueFalseTotal
if $pending eq "f" 12 3 15
727 15 undef($pending);
728 } elsif($arg eq '--') {
729 3 next;
730 } elsif($arg =~ /\A-([$VALUE_OPTIONS])(.*)\z/s) {
731 # Like getopt, the rest of the argument is the value when there is any.
732 33 my ($letter, $attached) = ($1, $2);
733 33 ▶ if(length($attached)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $attached) { } 16 17 33
734 16 ▶ $parsed{'sender'} = $attached if($letter eq 'f');
Branch: 1 branch
BranchTrueFalseTotal
if $letter eq "f" 7 9 16
735 } else {
736 17 $pending = $letter;
737 }
738 } elsif($arg =~ /\A-(.*)\z/s) {
739 # A cluster of flags, such as -ti, none of which takes a value.
740 21 ▶ $parsed{'reads_headers'} = 1 if($1 =~ /t/);
Branch: 1 branch
BranchTrueFalseTotal
if $1 =~ /t/ 18 3 21
741 } elsif(length($arg)) {
742 # An empty argument names nobody.
743 10036 push(@{ $parsed{'recipients'} }, $arg);
744 }
745 }
746  
747 74 return \%parsed;
748 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
749  
750 # Purpose: Send one line to syslog.
751 # Entry criteria: $line holds no control characters.
752 # Exit status: True when syslog took the line, false - after a warning -
753 # when it could not be reached.
754 # Side effects: Opens and closes the syslog connection.
755 30 ▶sub _write_syslog :Private
Subroutine _write_syslog: called CC=2 SCAR=6.9
756 {
757 30 my ($self, $line) = @_;
758  
759 # eval sets $@, which belongs to the caller.
760 30 local $@ = '';
761  
762 30 my $written = eval {
763 30 Sys::Syslog::openlog($SYSLOG_IDENT, $SYSLOG_OPTIONS, $SYSLOG_FACILITY);
764 28 Sys::Syslog::syslog($SYSLOG_LEVEL, '%s', $line);
765 27 1;
766 };
767 30 my $error = $@;
768  
769 # The connection is closed whether or not the line went in, and a failure
770 # to close it must not turn a line that was written into a failure.
771 30 eval { Sys::Syslog::closelog(); 1 }; ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
772  
773 30 ▶ $self->_say('syslog_failed', { 'error' => _trim_error($error) }) unless($written);
Branch: 1 branch
BranchTrueFalseTotal
unless $written 3 27 30
774  
775 30 return $written;
776 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
777  
778 # Purpose: Read the header block of a message into a hash.
779 # Entry criteria: $message is a string, with LF or CRLF line endings.
780 # Exit status: A hash reference from lower-case header name to the unfolded
781 # value of the first header of that name. A message with no
782 # blank line is all header.
783 # Side effects: None.
784 125 ▶sub _parse_headers :Private
Subroutine _parse_headers: called CC=5 SCAR=16.2
785 {
786 125 my $message = shift;
787  
788 125 my ($block) = $message =~ /\A(.*?)\r?\n\r?\n/s;
789 125 ▶ $block //= $message;
Condition66%2/3
$block //= $message
l!l&&r!l&&!r
112130
MC/DC0%0/2
$block //= $message
$block $message
Truth table66%2/3
A //= B
A= $blockB= $message
ABresult
1X1
011
000
790  
791 # A header can be folded over several lines; join them back.
792 125 $block =~ s/\r?\n[ \t]+/ /g;
793  
794 125 my %headers;
795 125 foreach my $line (split(/\r?\n/, $block)) {
796 1369 ▶ $headers{lc($1)} //= $2 if($line =~ /\A([^\s:]+):[ \t]*(.*?)\s*\z/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A([^\s:]+):[ \t]*(.*?)\s*\z/ 1361 8 1369
Condition66%2/3
$headers{lc $1} //= $2
l!l&&r!l&&!r
10013600
MC/DC0%0/2
$headers{lc $1} //= $2
$headers{lc $1} $2
Truth table66%2/3
A //= B
A= $headers{lc $1}B= $2
ABresult
1X1
011
000
797 }
798  
799 125 return \%headers;
800 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
801  
802 # Purpose: Parse a header value into addresses.
803 # Entry criteria: $value is a string.
804 # Exit status: The list of Mail::Address objects, in order, as many as could
805 # be made of it.
806 # Side effects: None. The parser's complaints about a malformed header are
807 # dropped: a header is the sender's to get wrong, and a
808 # warning in the log for it would be noise.
809 129 ▶sub _parse_addresses :Private
Subroutine _parse_addresses: called CC=1 SCAR=0.0
810 {
811 129 my $value = shift;
812  
813 129 ▶ local $SIG{'__WARN__'} = sub { };
Subroutine __ANON__: called CC=1 SCAR=0.0
814  
815 129 return Mail::Address->parse($value);
816 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
817  
818 # Purpose: The addresses in a header value.
819 # Entry criteria: $value is a header value, or undef.
820 # Exit status: An array reference of the addresses, in order; empty when
821 # there are none. Display names and comments are dropped.
822 # Side effects: None.
823 93 ▶sub _addresses :Private
Subroutine _addresses: called CC=4 SCAR=13.9
824 {
825 93 my $value = shift;
826  
827 # A group with no members, "undisclosed-recipients:;", comes back as its name.
828 93 ▶ return defined($value) ? [ grep { length && !/:\z/ } map { $_->address() // '' } _parse_addresses($value) ] : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 65 28 93
Condition100%5/5
length $_ && !/:\z/
!ll&&!rl&&r
315065
$_->address // ""
l!l
50681
MC/DC100%2/2
length $_ && !/:\z/
length $_ !/:\z/
MC/DC100%1/1
$_->address // ""
$_->address
Truth table100%3/3
A && not(B)
A= length $_B= !/:\z/
ABresult
0X0
100
111
Truth table100%2/2
A
A= $_->address
Aresult
11
00
829 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
830  
831 =head2 B<from_domain> $message
832  
833 The domain of the address in the first C<From:> header, in lower case, or
834 C<undef> when there is no such header. Folded headers and display names are
835 understood.
836  
837 =head3 PURPOSE
838  
839 Says whose domain a message claims to be from, which is what decides whether
840 it is signed. The address is read by L<Mail::Address>, so quoted display
841 names, comments and groups are handled by a parser that has seen them before.
842 Only a plain host name is accepted as a domain (letters, digits, dots and
843 hyphens).
844  
845 A C<From:> header that holds a control character, other than a tab, gives
846 C<undef>. A NUL or an escape character can make two programs read two
847 different addresses from the same header, so no domain is trusted from it.
848  
849 =head3 EXAMPLE
850  
851 my $domain = Mail::Send::DKIM::from_domain($message); # 'example.org'
852 my $same = $dkim->from_domain($message);
853  
854 =head3 API SPECIFICATION
855  
856 =head4 Input (Params::Validate::Strict)
857  
858 { message => { type => 'string' } }
859  
860 It may be called as a function or as a method.
861  
862 =head4 Output (Return::Set)
863  
864 { type => 'string', optional => 1, matches => qr/\A[a-z0-9.-]+\z/ }
865  
866 =head3 MESSAGES
867  
868 None: a message without a usable From: is not an error, the answer is C<undef>.
869  
870 =cut
871  
872 sub from_domain
873 83 ▶{
Subroutine from_domain: called CC=8 SCAR=20.8
Pod documented
874 # Called as a method, the first argument is the object, not the message.
875 83 ▶ shift if(@_ > 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 3 80 83
876 83 ▶ my $message = validate_strict('schema' => { 'message' => { 'type' => 'string' } }, 'input' => { 'message' => $_[0] })->{'message'} // '';
Condition100%2/2
validate_strict("schema", {"message", {"type", "string"}}, "input", {"message", $_[0]})->{"message"} // ""
l!l
793
MC/DC100%1/1
validate_strict("schema", {"message", {"type", "string"}}, "input", {"message", $_[0]})->{"message"} // ""
validate_strict("schema", {"message", {"type", "string"}}, "input", {"message", $_[0]})->{"message"}
Truth table100%2/2
A
A= validate_strict("schema", {"message", {"type", "string"}}, "input", {"message", $_[0]})->{"message"}
Aresult
11
00
877  
878 82 my $from = _parse_headers($message)->{'from'};
879 82 my $host;
880  
881 # A control character in the header, a NUL especially, can make two
882 # parsers read two different addresses, so no domain is trusted from it.
883 82 ▶ if(defined($from) && $from !~ /[\x00-\x08\x0b-\x1f\x7f]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from and not $from =~ /[\x00-\x08\x0b-\x1f\x7f]/) 63 19 82
Condition100%3/3
defined $from and not $from =~ /[\x00-\x08\x0b-\x1f\x7f]/
!ll&&!rl&&r
15463
MC/DC100%2/2
defined $from and not $from =~ /[\x00-\x08\x0b-\x1f\x7f]/
defined $from not $from =~ /[\x00-\x08\x0b-\x1f\x7f]/
Truth table100%3/3
A and not(B)
A= defined $fromB= not $from =~ /[\x00-\x08\x0b-\x1f\x7f]/
ABresult
0X0
100
111
884 # The first address is the author; display names and comments around
885 # it are Mail::Address's to deal with, not ours.
886 63 my ($address) = _parse_addresses($from);
887 63 ▶ $host = $address->host() if(defined($address));
Branch: 1 branch
BranchTrueFalseTotal
if defined $address 60 3 63
888 }
889  
890 # Only a plain host name is a domain; the result is put in the signature.
891 82 ▶ return (defined($host) && $host =~ $DOMAIN_SYNTAX) ? lc($host) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $host && $host =~ /$DOMAIN_SYNTAX/ ? : 46 36 82
Condition100%3/3
defined $host and $host =~ /$DOMAIN_SYNTAX/
!ll&&!rl&&r
251146
MC/DC100%2/2
defined $host and $host =~ /$DOMAIN_SYNTAX/
defined $host $host =~ /$DOMAIN_SYNTAX/
Truth table100%3/3
A and B
A= defined $hostB= $host =~ /$DOMAIN_SYNTAX/
ABresult
0X0
100
111
892 }
893  
894 =head2 B<selector>
895  
896 The selector the message will be signed under: the one given to B<new> or read
897 from C<DKIM_SELECTOR>, otherwise the name of the B<keyfile> when one is given,
898 otherwise the name of the only F<*.private> file in B<keydir>. C<undef> when it
899 cannot be decided - no key, or several keys and no name - and the reason is a
900 warning on standard error. B<sign> asks this before doing anything else.
901  
902 =head3 PURPOSE
903  
904 Settles which of the keys is to be used, once, in one place.
905  
906 A key in B<keydir> is a regular file with a name in front of F<.private>.
907 A directory with such a name, and a file that is only F<.private>, are not
908 keys and are not counted. A selector that is an empty string is the same as
909 none.
910  
911 =head3 EXAMPLE
912  
913 my $selector = $dkim->selector();
914 die 'no usable key' unless(defined($selector));
915  
916 =head3 API SPECIFICATION
917  
918 =head4 Input (Params::Validate::Strict)
919  
920 {}
921  
922 =head4 Output (Return::Set)
923  
924 { type => 'string', optional => 1 }
925  
926 =head3 MESSAGES
927  
928 Message key Meaning and resolution
929 keydir_unreadable keydir cannot be opened: create it, or fix the mode or
930 the keydir option.
931 keys_ambiguous keydir holds no key, or more than one, and no selector
932 was named: add the key, or set DKIM_SELECTOR.
933  
934 =head3 PSEUDOCODE
935  
936 if a selector was given: return it
937 if a key file was given: return its name without .private, .key or .pem
938 list the *.private files in keydir (a warning and undef if it cannot be read)
939 if there is not exactly one: warn, return undef
940 return its name without .private
941  
942 =cut
943  
944 sub selector
945 43 ▶{
Subroutine selector: called CC=7 SCAR=19.5
Pod documented
946 43 my $self = shift;
947  
948 43 my $selector = $self->{'selector'};
949 43 ▶ return $selector if(defined($selector) && length($selector));
Branch: 1 branch
BranchTrueFalseTotal
if defined $selector and length $selector 6 37 43
Condition100%3/3
defined $selector and length $selector
!ll&&!rl&&r
3616
MC/DC100%2/2
defined $selector and length $selector
defined $selector length $selector
Truth table100%3/3
A and B
A= defined $selectorB= length $selector
ABresult
0X0
100
111
950  
951 # A named key file carries its selector in its name, which is the layout
952 # the keys are kept in: <keydir>/<selector>.private.
953 37 ▶ if(defined($self->{'keyfile'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"keyfile"}) 11 26 37
954 11 ($selector = basename($self->{'keyfile'})) =~ s/$KEYFILE_NAME_SUFFIX//;
955 11 ▶ return $selector if(length($selector));
Branch: 1 branch
BranchTrueFalseTotal
if length $selector 9 2 11
956 }
957  
958 28 my $keys = $self->_private_keys();
959 # undef, as the POD says: a bare return would be an empty list when the
960 # result is used in a list, as in a hash of options.
961 28 ▶ return undef unless(defined($keys)); ## no critic (Subroutines::ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keys 8 20 28
962  
963 20 ▶ unless(@{$keys} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$keys;} == 1) 7 13 20
964 7 $self->_say('keys_ambiguous', { 'keydir' => $self->{'keydir'}, 'count' => scalar(@{$keys}) });
965 7 return undef; ## no critic (Subroutines::ProhibitExplicitReturnUndef)
966 }
967  
968 13 ($selector = $keys->[0]) =~ s/\Q$KEY_SUFFIX\E\z//;
969  
970 13 return $selector;
971 }
972  
973 # Purpose: List the keys in the key directory.
974 # Entry criteria: $self->{keydir} is set.
975 # Exit status: An array reference of the names of the files ending .private, sorted;
976 # undef, after a warning, when the directory cannot be read.
977 # Side effects: Reads a directory; may warn.
978 66 ▶sub _private_keys :Private
Subroutine _private_keys: called CC=3 SCAR=11.0
979 {
980 66 my $self = shift;
981  
982 # An unreadable directory is a warning and an unsigned message, so autodie
983 # stands aside.
984 19 ▶ no autodie qw(opendir closedir);
Subroutine BEGIN: called CC=1 SCAR=0.0
985  
986 66 my $keys;
987 66 ▶ if(opendir(my $dh, $self->{'keydir'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $dh, $self->{'keydir'}) { } 36 30 66
988 # A key is a file with a name before the suffix: a directory called
989 # x.private, or a file called just .private, is not one.
990 36 ▶ $keys = [ sort grep { /\A.+\Q$KEY_SUFFIX\E\z/s && -f "$self->{'keydir'}/$_" } readdir($dh) ];
Condition100%3/3
/\A.+\Q$KEY_SUFFIX\E\z/s && -f "$self->{'keydir'}/$_"
!ll&&!rl&&r
761105016
MC/DC100%2/2
/\A.+\Q$KEY_SUFFIX\E\z/s && -f "$self->{'keydir'}/$_"
/\A.+\Q$KEY_SUFFIX\E\z/s -f "$self->{'keydir'}/$_"
Truth table100%3/3
A && B
A= /\A.+\Q$KEY_SUFFIX\E\z/sB= -f "$self->{'keydir'}/$_"
ABresult
0X0
100
111
991 36 closedir($dh);
992 } else {
993 30 $self->_say('keydir_unreadable', { 'keydir' => $self->{'keydir'}, 'os_error' => "$!" });
994 }
995  
996 66 return $keys;
997 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
998  
999 =head2 B<i18n> $key, \%arguments
1000  
1001 Returns the text of a message, in the language this object speaks, with the
1002 arguments put in. This is the one place any sentence is made, so a caller can
1003 change every message by giving B<new> a C<messages> option.
1004  
1005 A message is a format whose placeholders are named, C<%{name}s> or
1006 C<%{name}d>, and which may carry the usual C<sprintf> width and flags
1007 (C<%{name}-8s>). It may instead be a hash of formats, from which one is chosen
1008 by the arguments: C<count> selects C<zero>, C<one> or C<other> (C<other> when
1009 there is no better one), and C<variant> - a grammatical gender, for instance -
1010 selects the entry of that name, or C<variant.one>, C<variant.other> and so on
1011 when the choice is both. An argument a format asks for and was not given is an
1012 error, not an empty string. An argument that is given as C<undef> is an empty
1013 string (C<0> in a C<%{name}d>). A C<count> that is not a number selects no
1014 plural form. C<%%> is a percent sign, and any other C<%> that is not part of a
1015 placeholder is plain text. An entry that is an empty hash says nothing.
1016  
1017 Croaks if C<$key> is not a message that is defined.
1018  
1019 =head3 EXAMPLE
1020  
1021 my $text = $dkim->i18n('keys_ambiguous', { keydir => '/etc/dkim', count => 2 });
1022 # '... /etc/dkim holds 2 keys; name the selector to choose one'
1023  
1024 my $french = Mail::Send::DKIM->new(messages => {
1025 'keys_ambiguous' => {
1026 'one' => '%{keydir}s contient %{count}d cle',
1027 'other' => '%{keydir}s contient %{count}d cles',
1028 },
1029 });
1030  
1031 =head3 API SPECIFICATION
1032  
1033 =head4 Input (Params::Validate::Strict)
1034  
1035 {
1036 key => { type => 'string' },
1037 arguments => { type => 'hashref', optional => 1 },
1038 }
1039  
1040 =head4 Output (Return::Set)
1041  
1042 { type => 'string' }
1043  
1044 =head3 MESSAGES
1045  
1046 Message key Meaning and resolution
1047 unknown_message The key is not in the catalogue: fix the caller, or add it to messages.
1048 bad_arguments The arguments were not a hash reference: pass { name => value }.
1049 missing_argument A placeholder has no argument: pass it.
1050  
1051 =cut
1052  
1053  
1054 sub i18n
1055 96 ▶{
Subroutine i18n: called CC=7 SCAR=19.5
Pod documented
1056 96 my ($self, $key, $args) = @_;
1057  
1058 96 ▶ $args //= {};
Condition100%2/2
$args //= {}
l!l
7125
MC/DC100%1/1
$args //= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
1059 96 ▶ $key //= '';
Condition100%2/2
$key //= ""
l!l
951
MC/DC100%1/1
$key //= ""
$key
Truth table100%2/2
A
A= $key
Aresult
11
00
1060 96 ▶ croak($self->i18n('bad_arguments', { 'type' => ref($args) || 'a plain value' })) unless(ref($args) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 3 93 96
Condition100%2/2
ref $args || 'a plain value'
l!l
21
MC/DC100%1/1
ref $args || 'a plain value'
ref $args
Truth table100%2/2
A
A= ref $args
Aresult
11
00
1061  
1062 # The default catalogue is the fallback, so a caller's catalogue cannot
1063 # take away the message that reports a missing message.
1064 93 ▶ my $entry = $self->{'messages'}->{$key} // $DEFAULT_MESSAGES{$key};
Condition100%3/3
$self->{"messages"}{$key} // $DEFAULT_MESSAGES{$key}
l!l&&r!l&&!r
68322
MC/DC100%2/2
$self->{"messages"}{$key} // $DEFAULT_MESSAGES{$key}
$self->{"messages"}{$key} $DEFAULT_MESSAGES{$key}
Truth table100%3/3
A // B
A= $self->{"messages"}{$key}B= $DEFAULT_MESSAGES{$key}
ABresult
1X1
011
000
1065 93 ▶ croak($self->i18n('unknown_message', { 'key' => $key })) unless(defined($entry));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry 22 71 93
1066  
1067 71 return $self->_interpolate($key, _choose_format($entry, $args), $args);
1068 }
1069  
1070 # Purpose: Pick the format of a message that has variants.
1071 # Entry criteria: $entry is a string, or a hash reference of formats keyed by
1072 # zero, one, other, a variant name, or "variant.count".
1073 # Exit status: The format. The most specific variant that exists wins, and
1074 # "other" is the fall-back, then any entry at all.
1075 # Side effects: None.
1076 94 ▶sub _choose_format :Private
Subroutine _choose_format: called CC=13 SCAR=25.6
1077 {
1078 94 my ($entry, $args) = @_;
1079  
1080 94 my $format = $entry;
1081  
1082 94 ▶ if(ref($entry) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry eq "HASH") 38 56 94
1083 38 my $variant = $args->{'variant'};
1084 38 my $count = $args->{'count'};
1085 # A count that is not a number selects no plural form.
1086 38 ▶ my $plural = !looks_like_number($count // '') ? undef : ($count == 0 ? 'zero' : ($count == 1 ? 'one' : 'other'));
Branch: 3 branches
BranchTrueFalseTotal
!&looks_like_number(scalar($count // '')) ? : 14 24 38
$count == 0 ? : 6 18 24
$count == 1 ? : 9 9 18
Condition100%2/2
$count // ""
l!l
299
MC/DC100%1/1
$count // ""
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
1087  
1088 38 ▶ my @candidates = (
Branch: 3 branches
BranchTrueFalseTotal
defined $variant && defined $plural ? : 6 32 38
defined $variant ? : 10 28 38
defined $plural ? : 24 14 38
Condition100%3/3
defined $variant and defined $plural
!ll&&!rl&&r
2846
MC/DC100%2/2
defined $variant and defined $plural
defined $variant defined $plural
Truth table100%3/3
A and B
A= defined $variantB= defined $plural
ABresult
0X0
100
111
1089 (defined($variant) && defined($plural) ? "$variant.$plural" : ()),
1090 (defined($variant) ? $variant : ()),
1091 (defined($plural) ? $plural : ()),
1092 'other',
1093 );
1094 38 my ($chosen) = grep { exists($entry->{$_}) } @candidates;
1095 # An empty entry says nothing, rather than failing.
1096 38 ▶ my $name = $chosen // (sort keys %{$entry})[0];
Condition100%3/3
$chosen // (sort keys %{$entry;})[0]
l!l&&r!l&&!r
3611
MC/DC100%2/2
$chosen // (sort keys %{$entry;})[0]
$chosen (sort keys %{$entry;})[0]
Truth table100%3/3
A // B
A= $chosenB= (sort keys %{$entry;})[0]
ABresult
1X1
011
000
1097 38 ▶ $format = defined($name) ? ($entry->{$name} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 37 1 38
Condition100%2/2
$entry->{$name} // ""
l!l
361
MC/DC100%1/1
$entry->{$name} // ""
$entry->{$name}
Truth table100%2/2
A
A= $entry->{$name}
Aresult
11
00
1098 }
1099  
1100 94 return $format;
1101 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1102  
1103 # Purpose: Put the arguments into a format with named placeholders.
1104 # Entry criteria: $format uses %{name}s / %{name}d placeholders, and %% for a
1105 # percent sign. Anything else in it is plain text.
1106 # Exit status: The text. Croaks, with the missing_argument message, if a
1107 # placeholder has no argument.
1108 # Side effects: None.
1109 82 ▶sub _interpolate :Private
Subroutine _interpolate: called CC=6 SCAR=17.9
1110 {
1111 82 my ($self, $key, $format, $args) = @_;
1112  
1113 # Only the placeholders are handed to sprintf, one at a time, so a stray
1114 # percent sign in the text, or in a value, can never be taken for a
1115 # directive of its own.
1116 82 my $text = '';
1117 82 foreach my $part (split(/(%\{\w+\}[-+ 0#]*\d*(?:\.\d+)?[sd])/, $format)) {
1118 254 ▶ if($part =~ /\A%\{(\w+)\}(.+)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /\A%\{(\w+)\}(.+)\z/s) { } 106 148 254
1119 106 my ($name, $spec) = ($1, $2);
1120 106 ▶ croak($self->i18n('missing_argument', { 'key' => $key, 'argument' => $name })) unless(exists($args->{$name}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$name} 6 100 106
1121  
1122 # A value that is not a number is a caller's mistake in a %d, not worth a warning.
1123 19 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
1124 100 ▶ $text .= sprintf("%$spec", $args->{$name} // ($spec =~ /d\z/ ? 0 : ''));
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ /d\z/ ? : 0 1 1
Condition100%2/2
$args->{$name} // ($spec =~ /d\z/ ? 0 : "")
l!l
991
MC/DC100%1/1
$args->{$name} // ($spec =~ /d\z/ ? 0 : "")
$args->{$name}
Truth table100%2/2
A
A= $args->{$name}
Aresult
11
00
1125 } else {
1126 148 (my $plain = $part) =~ s/%%/%/g;
1127 148 $text .= $plain;
1128 }
1129 }
1130  
1131 76 return $text;
1132 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1133  
1134 # Purpose: Make the form of a message that is handed to sendmail.
1135 # Entry criteria: $message is a string.
1136 # Exit status: The message with LF line endings and no carriage return
1137 # anywhere, whatever arrived. A CR is data to the MTA that
1138 # keeps the message - Postfix's sendmail_fix_line_endings and
1139 # Exim both write the CRLF of SMTP themselves - so one that
1140 # survived would reach the receiver doubled, and the message
1141 # would fail the signature it was just given.
1142 # Side effects: None.
1143 63 ▶sub _local :Private
Subroutine _local: called CC=1 SCAR=0.0
1144 {
1145 63 my $message = shift;
1146  
1147 63 $message =~ tr/\r//d;
1148  
1149 63 return $message;
1150 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1151  
1152 # Purpose: Normalise the domain to put in the signature.
1153 # Entry criteria: $domain is a string or undef.
1154 # Exit status: The lower-case domain, or undef when none is given (an empty
1155 # or blank value counts as none).
1156 154 ▶sub _signing_domain :Private
Subroutine _signing_domain: called CC=3 SCAR=11.0
1157 {
1158 154 my $domain = shift;
1159  
1160 154 ▶ return undef unless(defined($domain) && $domain =~ /\S/); ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain and $domain =~ /\S/ 151 3 154
Condition100%3/3
defined $domain and $domain =~ /\S/
!ll&&!rl&&r
15013
MC/DC100%2/2
defined $domain and $domain =~ /\S/
defined $domain $domain =~ /\S/
Truth table100%3/3
A and B
A= defined $domainB= $domain =~ /\S/
ABresult
0X0
100
111
1161 3 $domain =~ s/\A\s+|\s+\z//g;
1162  
1163 3 return lc($domain);
1164 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1165  
1166 # Purpose: Make the set of domains this host signs for.
1167 # Entry criteria: $domains is an array reference, a string separated by commas
1168 # or spaces, or undef.
1169 # Exit status: A hash reference whose keys are the lower-case domains.
1170 # Side effects: None.
1171 164 ▶sub _domain_set :Private
Subroutine _domain_set: called CC=4 SCAR=13.9
1172 {
1173 164 my $domains = shift;
1174  
1175 164 ▶ my @domains = ref($domains) eq 'ARRAY' ? @{$domains} : split($DOMAIN_SEPARATOR, $domains // '');
Branch: 1 branch
BranchTrueFalseTotal
ref $domains eq 'ARRAY' ? : 21 143 164
Condition100%2/2
$domains // ""
l!l
26117
MC/DC100%1/1
$domains // ""
$domains
Truth table100%2/2
A
A= $domains
Aresult
11
00
1176  
1177 164 ▶ return { map { lc($_) => 1 } grep { defined && length } @domains };
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
225052
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
1178 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1179  
1180 # Purpose: Tidy an error for use inside a sentence.
1181 # Entry criteria: $text is a string, perhaps with a trailing newline.
1182 # Exit status: The text without its trailing whitespace.
1183 # Side effects: None.
1184 16 ▶sub _trim_error :Private
Subroutine _trim_error: called CC=1 SCAR=0.0
1185 {
1186 16 my $text = shift;
1187  
1188 16 $text =~ s/\s+\z//;
1189  
1190 16 return $text;
1191 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1192  
1193 # Purpose: Warn, in the language of this object.
1194 # Entry criteria: $key is in the message catalogue.
1195 # Exit status: $self, so calls can be chained.
1196 # Side effects: A warning on standard error, reported where the caller of this
1197 # module called it, by Carp. Protected, so that a subclass may
1198 # say things in the same way.
1199 20 ▶sub _say :Protected
Subroutine _say: called CC=1 SCAR=0.0
1200 {
1201 20 my ($self, $key, $args) = @_;
1202  
1203 20 carp($self->i18n($key, $args));
1204  
1205 18 return $self;
1206 19 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1207  
1208 19 1;
1209  
1210 __END__
↑ Summary bin/dmarc-report.pl »