| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # Standalone DKIM2 verifier CLI (draft-ietf-dkim-dkim2-spec-09). | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # Reads a message, verifies its DKIM2-Signature chain and Message-Instance | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # chain with Mail::DKIM2::Verifier, prints the result, and exits with the | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # verdict: 0 for pass, 75 (EX_TEMPFAIL) for temperror, 1 for anything else. | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # The counterpart of dkim2sign, and of python/dkim2verify.py and c/dkim2verify. | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # dkim2verify [--dns-json PATH] [--ignore-timestamps] [--ignore-prefix P]... | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # [--allow-unsigned-mi] [MESSAGE|-] | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # Keys come from DNS unless --dns-json names an interop dns.json, in which case | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # domains listed there are answered from the file and the rest still from DNS. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use 5.020; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use Getopt::Long qw(GetOptions); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use FindBin; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use lib -d "$FindBin::Bin/../lib/Mail/DKIM2" ? "$FindBin::Bin/../lib" : (); # running from a checkout | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use Mail::DKIM2::Key qw(parse_dkim_pubkey); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | ▶ | use Mail::DKIM2::Verifier; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 5 | my ($dns_json, $ignore_ts, $allow_unsigned, @prefixes); | |||||||||||||||||||||||||||||||||||||||||
| 27 | GetOptions( | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 'dns-json=s' => \$dns_json, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 'ignore-timestamps' => \$ignore_ts, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 'ignore-prefix=s' => \@prefixes, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 'allow-unsigned-mi' => \$allow_unsigned, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | 'help|h' => sub { print _usage(); exit 0 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | ▶ | ) or die _usage(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 5 | ▶ | my $file = shift // '-'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift @ARGV // "-"
MC/DC0%0/1
shift @ARGV // "-"
shift @ARGV
Truth table50%1/2
A
A= shift @ARGV
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | my $raw = $file eq '-' | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | ? do { local $/; binmode STDIN; <STDIN> } | |||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | ▶ | : do { open my $fh, '<:raw', $file or die "$file: $!\n"; local $/; <$fh> }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | die "empty message\n" unless defined $raw && length $raw; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $raw and length $raw
MC/DC0%0/2
defined $raw and length $raw
defined $raw
length $raw
Truth table33%1/3
A and B
A= defined $rawB= length $raw
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | my %opts = ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | SkipTimestampCheck => $ignore_ts ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | AllowUnsignedMI => $allow_unsigned ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | (@prefixes ? (IgnorePrefixes => \@prefixes) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | ▶ | if ($dns_json) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | require JSON; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | my $dns = do { | |||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | ▶ | open my $fh, '<', $dns_json or die "$dns_json: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | local $/; JSON::decode_json(<$fh>); | |||||||||||||||||||||||||||||||||||||||||
| 52 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | $opts{PubkeyCallback} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=20.2
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | my ($sig, $idx, $verifier) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | my ($sel, $dom) = ($sig->selector($idx), $sig->domain); | |||||||||||||||||||||||||||||||||||||||||
| 56 | my $txt = $dns->{$dom}{"$sel._domainkey"}[0][1] | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | ▶ | if $dom && $sel && $dns->{$dom}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$dom and $sel and $dns->{$dom}
$dom and $sel
MC/DC0%0/3
$dom and $sel and $dns->{$dom}
$dom
$sel
$dns->{$dom}
Truth table25%1/4
A and B and C
A= $domB= $selC= $dns->{$dom}
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 4 | ▶ | return parse_dkim_pubkey($txt) if $txt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | return $verifier->fetch_public_key($sig, $idx); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | my $v = Mail::DKIM2::Verifier->new(%opts)->load($raw); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | my $result = $v->result; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | say $v->result_detail; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 5 | ▶ | exit($result eq 'pass' ? 0 : $result eq 'temperror' ? 75 : 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub _usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | return <<"USAGE"; | |||||||||||||||||||||||||||||||||||||||||
| 70 | usage: $0 [options] [MESSAGE|-] | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | --dns-json PATH answer key lookups for domains in this interop dns.json | ||||||||||||||||||||||||||||||||||||||||||
| 73 | --ignore-timestamps do not fail a signature for its t= age | ||||||||||||||||||||||||||||||||||||||||||
| 74 | --ignore-prefix P treat header fields starting with P as unhashed (repeatable) | ||||||||||||||||||||||||||||||||||||||||||
| 75 | --allow-unsigned-mi permit a Message-Instance above the top signature | ||||||||||||||||||||||||||||||||||||||||||
| 76 | -h, --help | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | Exit status: 0 pass, 75 temperror, 1 otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||