bin/dmarc-summary

filter: Statementstmt 94.7% 235 / 248 Branchbran 70.3% 76 / 108 Conditioncond 45.3% 58 / 128 MC/DCmcdc 11.4% 8 / 70 Subroutinesub 100% 29 / 29 Podpod n/a totaltotal 69.6% 406 / 583 CC 106 SCAR 32.0
CC
106
Cov
76%
note
68.4
summarise
52.4
owner
12.3
CRAP 24.4
↑ Summary lib/Mail/Send/DKIM.pm »
1 #!/usr/bin/env perl
2  
3 # Summarise DMARC aggregate reports: one row per source IP.
4 #
5 # What matters in a report is whether a message was ours. Our own failures are
6 # bugs to fix; anybody else's "fail" is a forgery attempt, which is the traffic
7 # a quarantine or reject policy is there to catch. For that reason every row
8 # says whether the source address is one of ours, and the NOTE column separates
9 # the two ways DKIM can fail: no signature at all (somebody else claiming our
10 # domain) from a signature that does not verify (our own key or body).
11  
12 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 21 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 ▶use IO::Uncompress::Gunzip qw($GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 21 ▶use IO::Uncompress::Unzip qw($UnzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 21 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 21 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 21 ▶use Socket qw(inet_aton AF_INET);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 21 ▶use XML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 21 ▶my $home = $ENV{'HOME'} || (getpwuid($<))[7] || '.';
Condition40%2/5
$ENV{'HOME'} || (getpwuid $<)[7] || '.'
l!l
210
$ENV{"HOME"} or (getpwuid $<)[7]
l!l&&r!l&&!r
2100
MC/DC0%0/2
$ENV{'HOME'} || (getpwuid $<)[7] || '.'
$ENV{"HOME"} (getpwuid $<)[7]
Truth table33%1/3
A or B
A= $ENV{"HOME"}B= (getpwuid $<)[7]
ABresult
1X1
011
000
24  
25 # Reporters send plain, gzipped and zipped XML, so all three are candidates.
26 21 my @DEFAULT_GLOBS = map { "$home/Downloads/$_" } qw(*.xml *.xml.gz *.zip);
27  
28 # Where dmarc-weekly.sh puts the reports it has dealt with.
29 21 ▶my $ARCHIVE_DIR = $ENV{'DMARC_ARCHIVE_DIR'} || "$home/dmarc-archive";
Condition50%1/2
$ENV{'DMARC_ARCHIVE_DIR'} || "$home/dmarc-archive"
l!l
021
MC/DC0%0/1
$ENV{'DMARC_ARCHIVE_DIR'} || "$home/dmarc-archive"
$ENV{'DMARC_ARCHIVE_DIR'}
Truth table50%1/2
A
A= $ENV{'DMARC_ARCHIVE_DIR'}
Aresult
11
00
30  
31 # Reports are kilobytes. A file, or what it expands to, that is far bigger is
32 # not one, and is not worth filling memory with.
33 21 my $MAX_FILE_BYTES = 10 * 1024 * 1024;
34 21 my $MAX_XML_BYTES = 20 * 1024 * 1024;
35  
36 # Company forms a registry appends to the same provider's name.
37 21 my @LEGAL_SUFFIXES = qw(llc inc incorporated ltd limited gmbh bv b v sl s l ab oy as sas sarl
38 pty corp corporation co company plc ag srl spa kk pte llp);
39  
40 # Our sending addresses, so that our own failures can be told from forgeries.
41 21 ▶my @ours = grep { length } split(/[\s,]+/, $ENV{'DMARC_EXPECTED'} // '');
Condition50%1/2
$ENV{"DMARC_EXPECTED"} // ""
l!l
210
MC/DC0%0/1
$ENV{"DMARC_EXPECTED"} // ""
$ENV{"DMARC_EXPECTED"}
Truth table50%1/2
A
A= $ENV{"DMARC_EXPECTED"}
Aresult
11
00
42 21 my $whois = 1;
43  
44 # Reports are cut daily, so the newest window any of them covers is the only
45 # evidence this has that reporting is still happening: a corpus that stops
46 # moving looks like a quiet domain unless somebody reads the dates. Three days
47 # leaves room for a reporter to be late and is short enough to notice a silence.
48 21 my $max_age = 3; # days
49  
50 GetOptions(
51 1 ▶ 'ours=s' => sub { push @ours, grep { length } split(/,/, $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 1 ▶ 'no-whois' => sub { $whois = 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 'max-age=i' => \$max_age,
54 1 ▶ 'help|h' => sub { pod2usage(-verbose => 1, -exitval => 0) },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 21 ▶) or pod2usage(-verbose => 0, -exitval => 2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("ours=s", sub { push @ours, grep({length $_;} split(/,/, $_[1], 0)); } , "no-whois", sub { $whois = 0; } , "max-age=i", \$max_age, "help|h", sub { pod2usage("-verbose", 1, "-exitval", 0); } ) or pod2usage("-verbose", 0, "-exitval", 2) 20 0 20
56  
57 20 ▶my @files = expand(@ARGV ? @ARGV : @DEFAULT_GLOBS);
Branch: 1 branch
BranchTrueFalseTotal
@ARGV ? : 17 3 20
58 20 ▶if(!@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 2 18 20
59 # The reports may all have been archived, and the last of them long ago.
60 2 ▶ _warn_if_stale(undef) unless(@ARGV);
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 2 0 2
61 2 ▶ print STDERR 'No report files found',
Branch: 1 branch
BranchTrueFalseTotal
@ARGV ? : 0 2 2
62 (@ARGV ? '' : ' (looked for ' . join(' ', @DEFAULT_GLOBS) . ')'), "\n";
63 2 exit(2);
64 }
65  
66 18 my %OWNERS;
67 my %BY_OWNER;
68 18 my %LABEL;
69 18 my %TOTAL = ('files' => 0, 'messages' => 0, 'ours' => 0, 'failing' => 0);
70 18 my $newest; # the end of the newest report window read, as an epoch
71  
72 18 foreach my $file (@files) {
73 23 summarise($file);
74 }
75  
76 18 _warn_if_stale($newest);
77  
78 18 ▶if(@files > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files > 1) 5 13 18
79 printf("\nTOTAL: %d files, %d messages, %d from our IPs, %d failing DMARC\n",
80 5 @TOTAL{qw(files messages ours failing)});
81 5 rollup();
82 }
83  
84 18 exit(0);
85  
86 23 ▶sub summarise {
Subroutine summarise: called CC=32 SCAR=39.6
87 23 my $file = shift;
88  
89 23 my $xml = eval { _read_report($file) };
90 23 ▶ if(!defined($xml)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $xml) 2 21 23
91 2 my $error = $@;
92 2 $error =~ s/\s+\z//;
93 2 warn 'Skipping ', _clean($file), ': ', _clean($error), "\n";
94 2 return;
95 }
96  
97 # Anything that is not XML is turned away before it reaches the parser,
98 # which would otherwise complain about a CSV or a tarball parked in the
99 # same directory.
100 21 ▶ if($xml !~ /\A\s*</) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xml =~ /\A\s*</) 0 21 21
101 0 warn 'Skipping ', _clean($file), ": not a DMARC aggregate report\n";
102 0 return;
103 }
104  
105 # XML::PP is pure Perl, so it installs where the C parsers do not; a
106 # parse that does not leave a root element is not a report.
107 21 my $root = eval { XML::PP->new()->parse($xml) };
108 21 ▶ if(!$root || ref($root) ne 'HASH' || !defined($root->{'name'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $root or ref $root ne "HASH" or not defined $root->{"name"}) 0 21 21
Condition33%2/6
not $root or ref $root ne "HASH" or not defined $root->{"name"}
l!l&&r!l&&!r
0021
not $root or ref $root ne "HASH"
l!l&&r!l&&!r
0021
MC/DC0%0/3
not $root or ref $root ne "HASH" or not defined $root->{"name"}
not $root ref $root ne "HASH" not defined $root->{"name"}
Truth table25%1/4
not(A) or B or not(C)
A= not $rootB= ref $root ne "HASH"C= not defined $root->{"name"}
ABCresult
1XX1
01X1
0011
0000
109 0 warn 'Skipping ', _clean($file), ": not a DMARC aggregate report\n";
110 0 return;
111 }
112  
113 21 my $metadata = _child($root, 'report_metadata');
114 21 my $policy = _child($root, 'policy_published');
115 21 ▶ my $domain = _field($policy, 'domain') // '-';
Condition50%1/2
_field($policy, "domain") // "-"
l!l
210
MC/DC0%0/1
_field($policy, "domain") // "-"
_field($policy, "domain")
Truth table50%1/2
A
A= _field($policy, "domain")
Aresult
11
00
116 21 my $dates = _child($metadata, 'date_range');
117 21 my $end = _field($dates, 'end');
118 21 ▶ if(defined($end) && $end =~ /\A\d+\z/ && (!defined($newest) || $end > $newest)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end and $end =~ /\A\d+\z/ and !defined($newest) || $end > $newest) 17 4 21
Condition55%5/9
defined $end and $end =~ /\A\d+\z/ and !defined($newest) || $end > $newest
!ll&&!rl&&r
0417
defined $end and $end =~ /\A\d+\z/
!ll&&!rl&&r
0021
not defined $newest or $end > $newest
l!l&&r!l&&!r
1704
MC/DC25%1/4
defined $end and $end =~ /\A\d+\z/ and !defined($newest) || $end > $newest
defined $end $end =~ /\A\d+\z/ not defined $newest $end > $newest
Truth table40%2/5
A and B and not(C) or D
A= defined $endB= $end =~ /\A\d+\z/C= not defined $newestD= $end > $newest
ABCDresult
0XXX0
10XX0
11000
111X1
11011
119 17 $newest = $end;
120 }
121  
122 21 printf("\n%s\n", _clean($file));
123 21 ▶ printf("\t%s <%s> domain %s p=%s sp=%s pct=%s adkim=%s aspf=%s\n",
Condition50%7/14
_field($metadata, "org_name") // "?"
l!l
210
_field($metadata, "email") // "?"
l!l
210
_field($policy, "p") // "-"
l!l
210
_field($policy, "sp") // "-"
l!l
210
_field($policy, "pct") // "-"
l!l
210
_field($policy, "adkim") // "-"
l!l
210
_field($policy, "aspf") // "-"
l!l
210
MC/DC0%0/1
_field($metadata, "org_name") // "?"
_field($metadata, "org_name")
MC/DC0%0/1
_field($metadata, "email") // "?"
_field($metadata, "email")
MC/DC0%0/1
_field($policy, "p") // "-"
_field($policy, "p")
MC/DC0%0/1
_field($policy, "sp") // "-"
_field($policy, "sp")
MC/DC0%0/1
_field($policy, "pct") // "-"
_field($policy, "pct")
MC/DC0%0/1
_field($policy, "adkim") // "-"
_field($policy, "adkim")
MC/DC0%0/1
_field($policy, "aspf") // "-"
_field($policy, "aspf")
Truth table50%1/2
A
A= _field($metadata, "org_name")
Aresult
11
00
Truth table50%1/2
A
A= _field($metadata, "email")
Aresult
11
00
Truth table50%1/2
A
A= _field($policy, "p")
Aresult
11
00
Truth table50%1/2
A
A= _field($policy, "sp")
Aresult
11
00
Truth table50%1/2
A
A= _field($policy, "pct")
Aresult
11
00
Truth table50%1/2
A
A= _field($policy, "adkim")
Aresult
11
00
Truth table50%1/2
A
A= _field($policy, "aspf")
Aresult
11
00
124 _field($metadata, 'org_name') // '?',
125 _field($metadata, 'email') // '?',
126 $domain,
127 _field($policy, 'p') // '-',
128 _field($policy, 'sp') // '-',
129 _field($policy, 'pct') // '-',
130 _field($policy, 'adkim') // '-',
131 _field($policy, 'aspf') // '-');
132 21 ▶ printf("\treport %s %s -> %s\n\n",
Condition50%1/2
_field($metadata, "report_id") // "?"
l!l
210
MC/DC0%0/1
_field($metadata, "report_id") // "?"
_field($metadata, "report_id")
Truth table50%1/2
A
A= _field($metadata, "report_id")
Aresult
11
00
133 _field($metadata, 'report_id') // '?',
134 _window(_field($dates, 'begin')),
135 _window($end));
136  
137 21 printf("\t%-15s %3s %-6s %-9s %-11s %-4s %-31s %s\n",
138 'IP', 'N', 'DKIM', 'SPF', 'DISPOSITION', 'OURS', 'OWNER', 'NOTE');
139  
140 21 my ($messages, $mine, $failing) = (0, 0, 0);
141  
142 21 foreach my $record (_children($root, 'record')) {
143 29 my $row = _child($record, 'row');
144 29 my $evaluated = _child($row, 'policy_evaluated');
145 29 ▶ my $ip = _field($row, 'source_ip') // '?';
Condition50%1/2
_field($row, "source_ip") // "?"
l!l
290
MC/DC0%0/1
_field($row, "source_ip") // "?"
_field($row, "source_ip")
Truth table50%1/2
A
A= _field($row, "source_ip")
Aresult
11
00
146 29 ▶ my $count = _field($row, 'count') // 1;
Condition50%1/2
_field($row, "count") // 1
l!l
290
MC/DC0%0/1
_field($row, "count") // 1
_field($row, "count")
Truth table50%1/2
A
A= _field($row, "count")
Aresult
11
00
147 29 ▶ my $dkim = _field($evaluated, 'dkim') // '-';
Condition50%1/2
_field($evaluated, "dkim") // "-"
l!l
290
MC/DC0%0/1
_field($evaluated, "dkim") // "-"
_field($evaluated, "dkim")
Truth table50%1/2
A
A= _field($evaluated, "dkim")
Aresult
11
00
148 29 ▶ my $spf = _field($evaluated, 'spf') // '-';
Condition50%1/2
_field($evaluated, "spf") // "-"
l!l
290
MC/DC0%0/1
_field($evaluated, "spf") // "-"
_field($evaluated, "spf")
Truth table50%1/2
A
A= _field($evaluated, "spf")
Aresult
11
00
149 29 ▶ my $disposition = _field($evaluated, 'disposition') // '-';
Condition50%1/2
_field($evaluated, "disposition") // "-"
l!l
290
MC/DC0%0/1
_field($evaluated, "disposition") // "-"
_field($evaluated, "disposition")
Truth table50%1/2
A
A= _field($evaluated, "disposition")
Aresult
11
00
150  
151 29 my $is_ours = grep { $_ eq $ip } @ours;
152 29 my $who = owner($ip);
153 29 my $key = _owner_key($who);
154  
155 29 $messages += $count;
156 29 ▶ $mine += $count if($is_ours);
Branch: 1 branch
BranchTrueFalseTotal
if $is_ours 2 27 29
157 29 ▶ $failing += $count if($dkim ne 'pass' && $spf ne 'pass');
Branch: 1 branch
BranchTrueFalseTotal
if $dkim ne "pass" and $spf ne "pass" 29 0 29
Condition33%1/3
$dkim ne "pass" and $spf ne "pass"
!ll&&!rl&&r
0029
MC/DC0%0/2
$dkim ne "pass" and $spf ne "pass"
$dkim ne "pass" $spf ne "pass"
Truth table33%1/3
A and B
A= $dkim ne "pass"B= $spf ne "pass"
ABresult
0X0
100
111
158 29 $BY_OWNER{$key} += $count;
159 $LABEL{$key} = $who
160 29 ▶ if(!defined($LABEL{$key}) || length($who) > length($LABEL{$key}));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $LABEL{$key} or length $who > length $LABEL{$key} 23 6 29
Condition66%2/3
not defined $LABEL{$key} or length $who > length $LABEL{$key}
l!l&&r!l&&!r
2306
MC/DC50%1/2
not defined $LABEL{$key} or length $who > length $LABEL{$key}
not defined $LABEL{$key} length $who > length $LABEL{$key}
Truth table66%2/3
not(A) or B
A= not defined $LABEL{$key}B= length $who > length $LABEL{$key}
ABresult
1X1
011
000
161  
162 29 ▶ printf("\t%-15s %3d %-6s %-9s %-11s %-4s %-31.31s %s\n",
Branch: 1 branch
BranchTrueFalseTotal
$is_ours ? : 2 27 29
163 $ip, $count, $dkim, $spf, $disposition,
164 $is_ours ? 'yes' : 'no', $who, note($record, $domain));
165 }
166  
167 21 ▶ printf("\n\t%d message%s, %d from our IPs, %d failing DMARC\n",
Branch: 1 branch
BranchTrueFalseTotal
$messages == 1 ? : 5 16 21
168 $messages, ($messages == 1 ? '' : 's'), $mine, $failing);
169  
170 21 $TOTAL{'files'}++;
171 21 $TOTAL{'messages'} += $messages;
172 21 $TOTAL{'ours'} += $mine;
173 21 $TOTAL{'failing'} += $failing;
174 }
175  
176 # One network can be most of the corpus without any single address being the
177 # busiest, so the totals are also given by owner: the per-IP rows say who sent a
178 # message, and this says how much of the traffic that owner accounts for. The
179 # name is not truncated, because a rollup that cuts two owners down to the same
180 # 31 columns would silently add them together.
181 5 ▶sub rollup {
Subroutine rollup: called CC=4 SCAR=14.1
182 5 ▶ my $total = $TOTAL{'messages'} || 1;
Condition100%2/2
$TOTAL{'messages'} || 1
l!l
41
MC/DC100%1/1
$TOTAL{'messages'} || 1
$TOTAL{'messages'}
Truth table100%2/2
A
A= $TOTAL{'messages'}
Aresult
11
00
183  
184 5 printf("\n\t%-43s %8s %s\n", 'OWNER', 'MESSAGES', 'SHARE');
185  
186 5 ▶ foreach my $key (sort { $BY_OWNER{$b} <=> $BY_OWNER{$a} || $LABEL{$a} cmp $LABEL{$b} } keys %BY_OWNER) {
Condition33%1/3
$BY_OWNER{$b} <=> $BY_OWNER{$a} || $LABEL{$a} cmp $LABEL{$b}
l!l&&r!l&&!r
400
MC/DC0%0/2
$BY_OWNER{$b} <=> $BY_OWNER{$a} || $LABEL{$a} cmp $LABEL{$b}
$BY_OWNER{$b} <=> $BY_OWNER{$a} $LABEL{$a} cmp $LABEL{$b}
Truth table33%1/3
A || B
A= $BY_OWNER{$b} <=> $BY_OWNER{$a}B= $LABEL{$a} cmp $LABEL{$b}
ABresult
1X1
011
000
187 8 printf("\t%-43s %8d %5.1f%%\n", $LABEL{$key}, $BY_OWNER{$key}, 100 * $BY_OWNER{$key} / $total);
188 }
189  
190 5 return;
191 }
192  
193 # One provider is spelled several ways by the registries: a range with an
194 # OrgName gives DigitalOcean, LLC and the next, with only a NetName, gives
195 # DIGITALOCEAN. The rollup groups on this form - case, punctuation and a
196 # trailing legal suffix taken off - so the provider is one line, while names
197 # that describe different organisations stay apart. The list lives with the
198 # other settings at the top, because nothing below exit(0) is ever run.
199 29 ▶sub _owner_key {
Subroutine _owner_key: called CC=4 SCAR=13.9
200 29 my $name = shift;
201  
202 29 ▶ my $key = lc($name // '');
Condition50%1/2
$name // ""
l!l
290
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
203 29 $key =~ s/[^a-z0-9]+/ /g;
204 29 $key =~ s/\A\s+//;
205 29 $key =~ s/\s+\z//;
206  
207 # Whole trailing words only, and repeatedly, so "Foo Co., Ltd." loses both.
208 29 my $suffix = join('|', map { quotemeta } @LEGAL_SUFFIXES);
209 29 ▶ 1 while($key =~ s/(?:\A|\s)(?:$suffix)\z//);
Branch: 1 branch
BranchTrueFalseTotal
$key =~ s/(?:\A|\s)(?:$suffix)\z// and do { '???' } 16 29 45
210  
211 29 $key =~ s/\s+\z//;
212  
213 29 ▶ return length($key) ? $key : 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
length $key ? : 29 0 29
214 }
215  
216 # The signature evidence behind the verdicts. A dkim=fail with no signature
217 # listed means the message was not signed at all; a signature that is listed
218 # and did not verify is ours to investigate.
219 29 ▶sub note {
Subroutine note: called CC=15 SCAR=42.3
220 29 my ($record, $domain) = @_;
221  
222 29 my @notes;
223 my @signatures;
224  
225 29 foreach my $results (_children($record, 'auth_results')) {
226 29 push @signatures, _children($results, 'dkim');
227 }
228  
229 29 ▶ if(@signatures) {
Branch: 1 branch
BranchTrueFalseTotal
if (@signatures) { } 0 29 29
230 0 foreach my $signature (@signatures) {
231 0 ▶ push @notes, sprintf('signed d=%s s=%s %s',
Condition0%0/6
_field($signature, "domain") // "?"
l!l
00
_field($signature, "selector") // "?"
l!l
00
_field($signature, "result") // "?"
l!l
00
MC/DC0%0/1
_field($signature, "domain") // "?"
_field($signature, "domain")
MC/DC0%0/1
_field($signature, "selector") // "?"
_field($signature, "selector")
MC/DC0%0/1
_field($signature, "result") // "?"
_field($signature, "result")
Truth table0%0/2
A
A= _field($signature, "domain")
Aresult
11
00
Truth table0%0/2
A
A= _field($signature, "selector")
Aresult
11
00
Truth table0%0/2
A
A= _field($signature, "result")
Aresult
11
00
232 _field($signature, 'domain') // '?',
233 _field($signature, 'selector') // '?',
234 _field($signature, 'result') // '?');
235 }
236 } else {
237 29 push @notes, 'unsigned';
238 }
239  
240 29 my $header_from = _field(_child($record, 'identifiers'), 'header_from');
241 29 ▶ push @notes, "header_from=$header_from" if(defined($header_from) && $header_from ne $domain);
Branch: 1 branch
BranchTrueFalseTotal
if defined $header_from and $header_from ne $domain 0 29 29
Condition33%1/3
defined $header_from and $header_from ne $domain
!ll&&!rl&&r
0290
MC/DC0%0/2
defined $header_from and $header_from ne $domain
defined $header_from $header_from ne $domain
Truth table33%1/3
A and B
A= defined $header_fromB= $header_from ne $domain
ABresult
0X0
100
111
242  
243 29 my $row = _child($record, 'row');
244 29 my $evaluated = _child($row, 'policy_evaluated');
245 29 foreach my $reason (_children($evaluated, 'reason')) {
246 0 ▶ my $type = _field($reason, 'type') // $reason->{'attributes'}{'type'};
Condition0%0/3
_field($reason, "type") // $reason->{"attributes"}{"type"}
l!l&&r!l&&!r
000
MC/DC0%0/2
_field($reason, "type") // $reason->{"attributes"}{"type"}
_field($reason, "type") $reason->{"attributes"}{"type"}
Truth table0%0/3
A // B
A= _field($reason, "type")B= $reason->{"attributes"}{"type"}
ABresult
1X1
011
000
247 0 ▶ $type = _clean($type) if(defined($type));
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 0 0 0
248 0 ▶ push @notes, "reason=$type" if(defined($type) && length($type));
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and length $type 0 0 0
Condition0%0/3
defined $type and length $type
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and length $type
defined $type length $type
Truth table0%0/3
A and B
A= defined $typeB= length $type
ABresult
0X0
100
111
249 }
250  
251 29 ▶ return join('; ', @notes) || '-';
Condition50%1/2
join('; ', @notes) || '-'
l!l
290
MC/DC0%0/1
join('; ', @notes) || '-'
join('; ', @notes)
Truth table50%1/2
A
A= join('; ', @notes)
Aresult
11
00
252 }
253  
254 # Registries disagree about the field: ARIN has OrgName, RIPE org-name or
255 # descr, and the whois command itself has to be given a bare address.
256 29 ▶sub owner {
Subroutine owner: called CC=11 SCAR=25.1
257 29 my $ip = shift;
258  
259 29 ▶ return $OWNERS{$ip} if(exists($OWNERS{$ip}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $OWNERS{$ip} 0 29 29
260  
261 29 ▶ unless($ip =~ /\A[0-9a-fA-F:.]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip =~ /\A[0-9a-fA-F:.]+\z/) 0 29 29
262 0 return $OWNERS{$ip} = '?';
263 }
264  
265 29 my %seen;
266 29 ▶ if($whois) {
Branch: 1 branch
BranchTrueFalseTotal
if ($whois) 28 1 29
267 28 foreach my $line (split(/\n/, qx{timeout 10 whois $ip 2>/dev/null})) {
268 32 $line =~ s/\A\s+//;
269 32 ▶ next unless($line =~ /\A([A-Za-z][A-Za-z -]*?)\s*:\s*(\S.*?)\s*\z/);
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A([A-Za-z][A-Za-z -]*?)\s*:\s*(\S.*?)\s*\z/ 0 32 32
270 32 my ($key, $value) = (lc($1), $2);
271 32 $value =~ s/\s*#.*\z//;
272 32 ▶ $seen{$key} = $value unless(exists($seen{$key}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $seen{$key} 32 0 32
273 }
274 }
275  
276 29 foreach my $key (qw(orgname org-name owner organization descr netname)) {
277 78 ▶ return $OWNERS{$ip} = _clean($seen{$key}) if($seen{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key} 28 50 78
278 }
279  
280 1 ▶ my $name = $ip =~ /\A\d+\./ ? gethostbyaddr(inet_aton($ip), AF_INET) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A\d+\./ ? : 1 0 1
281  
282 1 ▶ return $OWNERS{$ip} = _clean($name || 'unknown');
Condition50%1/2
$name || 'unknown'
l!l
01
MC/DC0%0/1
$name || 'unknown'
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
283 }
284  
285 # Reports are named after their domain and window, so a pattern is at least as
286 # likely as a list of paths; directories of reports are the third case.
287 23 ▶sub expand {
Subroutine expand: called CC=4 SCAR=13.9
288 23 my @found;
289  
290 23 foreach my $pattern (@_) {
291 34 ▶ if(-d $pattern) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $pattern) { } 3 31 34
elsif ($pattern =~ /[*?\[]/) { } 9 22 31
292 3 push @found, glob("$pattern/*.xml"), glob("$pattern/*.xml.gz"), glob("$pattern/*.zip");
293 } elsif($pattern =~ /[*?\[]/) {
294 9 push @found, glob($pattern);
295 } else {
296 22 push @found, $pattern;
297 }
298 }
299  
300 23 my @reports = sort grep { -f $_ } @found;
301  
302 23 return @reports;
303 }
304  
305 # Reporters send plain XML, a gzip stream or a zip. Compression is detected
306 # from the leading bytes rather than the name, because the name is not evidence
307 # of anything.
308 28 ▶sub _read_report {
Subroutine _read_report: called CC=11 SCAR=24.8
309 28 my $file = shift;
310  
311 28 my $size = -s $file;
312 28 ▶ die "too large to be a report\n" if(defined($size) && $size > $MAX_FILE_BYTES);
Branch: 1 branch
BranchTrueFalseTotal
if defined $size and $size > $MAX_FILE_BYTES 1 27 28
Condition66%2/3
defined $size and $size > $MAX_FILE_BYTES
!ll&&!rl&&r
0271
MC/DC50%1/2
defined $size and $size > $MAX_FILE_BYTES
defined $size $size > $MAX_FILE_BYTES
Truth table66%2/3
A and B
A= defined $sizeB= $size > $MAX_FILE_BYTES
ABresult
0X0
100
111
313  
314 27 ▶ open(my $handle, '<', $file) or die "cannot open: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<", $file or die "cannot open: $!\n" 27 0 27
315 27 binmode($handle);
316 27 my $magic = '';
317 27 read($handle, $magic, 2);
318  
319 27 ▶ if($magic eq "\x1f\x8b") {
Branch: 1 branch
BranchTrueFalseTotal
if ($magic eq "\37\213") 2 25 27
320 2 close($handle);
321 2 ▶ my $gz = IO::Uncompress::Gunzip->new($file) or die "cannot decompress: $GunzipError\n";
Branch: 1 branch
BranchTrueFalseTotal
my $gz = "IO::Uncompress::Gunzip"->new($file) or die "cannot decompress: $main::GunzipError\n" 2 0 2
322 2 return _read_bounded($gz);
323 }
324  
325 25 ▶ if($magic eq 'PK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($magic eq "PK") 1 24 25
326 1 close($handle);
327 1 ▶ my $zip = IO::Uncompress::Unzip->new($file) or die "cannot unzip: $UnzipError\n";
Branch: 1 branch
BranchTrueFalseTotal
my $zip = "IO::Uncompress::Unzip"->new($file) or die "cannot unzip: $main::UnzipError\n" 1 0 1
328 1 my $status = 1;
329 1 ▶ while($status > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status > 0) 1 0 1
330 1 ▶ return _read_bounded($zip) if(($zip->getHeaderInfo()->{'Name'} // '') =~ /\.xml\z/i);
Branch: 1 branch
BranchTrueFalseTotal
if ($zip->getHeaderInfo->{"Name"} // "") =~ /\.xml\z/i 1 0 1
Condition50%1/2
$zip->getHeaderInfo->{"Name"} // ""
l!l
10
MC/DC0%0/1
$zip->getHeaderInfo->{"Name"} // ""
$zip->getHeaderInfo->{"Name"}
Truth table50%1/2
A
A= $zip->getHeaderInfo->{"Name"}
Aresult
11
00
331 0 $status = $zip->nextStream();
332 }
333 0 die "no XML inside\n";
334 }
335  
336 24 seek($handle, 0, 0);
337 24 local $/;
338 24 my $xml = <$handle>;
339 24 close($handle);
340  
341 24 return $xml;
342 }
343  
344 # The rest of a compressed stream, giving up when it is too big to be a report.
345 3 ▶sub _read_bounded {
Subroutine _read_bounded: called CC=5 SCAR=16.3
346 3 my $stream = shift;
347  
348 3 my $xml = '';
349 3 while(1) {
350 325 my $read = $stream->read(my $chunk, 65536);
351 325 ▶ die "cannot decompress\n" if(!defined($read) || $read < 0);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $read or $read < 0 0 325 325
Condition33%1/3
not defined $read or $read < 0
l!l&&r!l&&!r
00325
MC/DC0%0/2
not defined $read or $read < 0
not defined $read $read < 0
Truth table33%1/3
not(A) or B
A= not defined $readB= $read < 0
ABresult
1X1
011
000
352 325 ▶ last if($read == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $read == 0 2 323 325
353 323 $xml .= $chunk;
354 323 ▶ die "expands too far to be a report\n" if(length($xml) > $MAX_XML_BYTES);
Branch: 1 branch
BranchTrueFalseTotal
if length $xml > $MAX_XML_BYTES 1 322 323
355 }
356  
357 2 return $xml;
358 }
359  
360 # The element children of a node, by local name: a report from one reporter
361 # prefixes its elements and one from another does not, and XML::PP keeps the
362 # prefix apart from the name either way.
363 736 ▶sub _children {
Subroutine _children: called CC=5 SCAR=17.8
364 736 my ($node, $name) = @_;
365  
366 736 ▶ return () unless($node && ref($node) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless $node and ref $node eq "HASH" 0 736 736
Condition33%1/3
$node and ref $node eq "HASH"
!ll&&!rl&&r
00736
MC/DC0%0/2
$node and ref $node eq "HASH"
$node ref $node eq "HASH"
Truth table33%1/3
A and B
A= $nodeB= ref $node eq "HASH"
ABresult
0X0
100
111
367  
368 736 ▶ return grep { defined($_->{'name'}) && $_->{'name'} eq $name } @{$node->{'children'} || []};
Condition60%3/5
defined $_->{'name'} && $_->{'name'} eq $name
!ll&&!rl&&r
01918686
$node->{'children'} || []
l!l
7360
MC/DC50%1/2
defined $_->{'name'} && $_->{'name'} eq $name
defined $_->{'name'} $_->{'name'} eq $name
MC/DC0%0/1
$node->{'children'} || []
$node->{'children'}
Truth table66%2/3
A && B
A= defined $_->{'name'}B= $_->{'name'} eq $name
ABresult
0X0
100
111
Truth table50%1/2
A
A= $node->{'children'}
Aresult
11
00
369 }
370  
371 628 ▶sub _child {
Subroutine _child: called CC=1 SCAR=0.0
372 628 my ($node, $name) = @_;
373  
374 628 my ($child) = _children($node, $name);
375  
376 628 return $child;
377 }
378  
379 # All the text under a node, as textContent would give it; text nodes are the
380 # children that have no element name.
381 410 ▶sub _text {
Subroutine _text: called CC=6 SCAR=20.7
382 410 my $node = shift;
383  
384 410 ▶ return '' unless($node && ref($node) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless $node and ref $node eq "HASH" 0 410 410
Condition33%1/3
$node and ref $node eq "HASH"
!ll&&!rl&&r
00410
MC/DC0%0/2
$node and ref $node eq "HASH"
$node ref $node eq "HASH"
Truth table33%1/3
A and B
A= $nodeB= ref $node eq "HASH"
ABresult
0X0
100
111
385  
386 410 ▶ return join('', map { defined($_->{'name'}) ? _text($_) : ($_->{'text'} // '') } @{$node->{'children'} || []});
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'name'} ? : 0 410 410
Condition50%2/4
$_->{"text"} // ""
l!l
4100
$node->{'children'} || []
l!l
4100
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
MC/DC0%0/1
$node->{'children'} || []
$node->{'children'}
Truth table50%1/2
A
A= $_->{"text"}
Aresult
11
00
Truth table50%1/2
A
A= $node->{'children'}
Aresult
11
00
387 }
388  
389 410 ▶sub _field {
Subroutine _field: called CC=2 SCAR=7.0
390 410 my ($node, $name) = @_;
391  
392 410 my $child = _child($node, $name);
393 410 ▶ return unless($child);
Branch: 1 branch
BranchTrueFalseTotal
unless $child 0 410 410
394  
395 410 my $text = _text($child);
396 410 $text =~ s/\A\s+//;
397 410 $text =~ s/\s+\z//;
398  
399 410 return _clean($text);
400 }
401  
402 # Text from a report, or from whois, is somebody else's: control characters in
403 # it must not reach a terminal or a mail.
404 464 ▶sub _clean {
Subroutine _clean: called CC=2 SCAR=7.4
405 464 my $text = shift;
406  
407 464 ▶ if(utf8::is_utf8($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($text)) { } 0 464 464
408 0 $text =~ s/[\x00-\x1f\x7f-\x9f]/ /g;
409 } else {
410 464 $text =~ s/[\x00-\x1f\x7f]|\xc2[\x80-\x9f]/ /g;
411 }
412  
413 464 return $text;
414 }
415  
416 # The end of the report window in a file, or nothing when it has none to give.
417 5 ▶sub _report_end {
Subroutine _report_end: called CC=5 SCAR=18.4
418 5 my $file = shift;
419  
420 5 my $root = eval { XML::PP->new()->parse(_read_report($file)) };
421 5 ▶ return unless($root && ref($root) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless $root and ref $root eq "HASH" 0 5 5
Condition33%1/3
$root and ref $root eq "HASH"
!ll&&!rl&&r
005
MC/DC0%0/2
$root and ref $root eq "HASH"
$root ref $root eq "HASH"
Truth table33%1/3
A and B
A= $rootB= ref $root eq "HASH"
ABresult
0X0
100
111
422  
423 5 my $end = _field(_child(_child($root, 'report_metadata'), 'date_range'), 'end');
424  
425 5 ▶ return (defined($end) && $end =~ /\A\d+\z/) ? $end : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $end && $end =~ /\A\d+\z/ ? : 5 0 5
Condition33%1/3
defined $end and $end =~ /\A\d+\z/
!ll&&!rl&&r
005
MC/DC0%0/2
defined $end and $end =~ /\A\d+\z/
defined $end $end =~ /\A\d+\z/
Truth table33%1/3
A and B
A= defined $endB= $end =~ /\A\d+\z/
ABresult
0X0
100
111
426 }
427  
428 # Warn when reporting looks to have stopped. A run over the default places
429 # looks in the archive too before saying so, since that is where the weekly
430 # run leaves the newest reports.
431 20 ▶sub _warn_if_stale {
Subroutine _warn_if_stale: called CC=9 SCAR=22.1
432 20 my $end = shift;
433  
434 20 ▶ if(!@ARGV && (!defined($end) || defined(_staleness($end, $max_age)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @ARGV and !defined($end) || defined _staleness($end, $max_age)) 3 17 20
Condition66%4/6
not @ARGV and !defined($end) || defined _staleness($end, $max_age)
!ll&&!rl&&r
1703
not defined $end or defined _staleness($end, $max_age)
l!l&&r!l&&!r
210
MC/DC33%1/3
not @ARGV and !defined($end) || defined _staleness($end, $max_age)
not @ARGV not defined $end defined _staleness($end, $max_age)
Truth table75%3/4
not(A) and not(B) or C
A= not @ARGVB= not defined $endC= defined _staleness($end, $max_age)
ABCresult
0XX0
1000
11X1
1011
435 3 foreach my $file (expand($ARCHIVE_DIR)) {
436 5 my $archived = _report_end($file);
437 5 ▶ $end = $archived if(defined($archived) && (!defined($end) || $archived > $end));
Branch: 1 branch
BranchTrueFalseTotal
if defined $archived and !defined($end) || $archived > $end 3 2 5
Condition83%5/6
defined $archived and !defined($end) || $archived > $end
!ll&&!rl&&r
023
not defined $end or $archived > $end
l!l&&r!l&&!r
212
MC/DC66%2/3
defined $archived and !defined($end) || $archived > $end
defined $archived not defined $end $archived > $end
Truth table75%3/4
A and not(B) or C
A= defined $archivedB= not defined $endC= $archived > $end
ABCresult
0XX0
1000
11X1
1011
438 }
439 }
440  
441 20 ▶ if(my $stale = _staleness($end, $max_age)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stale = _staleness($end, $max_age)) 3 17 20
442 3 warn "$stale\n";
443 }
444  
445 20 return;
446 }
447  
448 # A report covers one day and ends when that day did, so the newest end is when
449 # a reporter last had something to say. Undef while the corpus is current,
450 # and the warning to give once it is not.
451 21 ▶sub _staleness {
Subroutine _staleness: called CC=3 SCAR=11.0
452 21 my ($end, $max_age) = @_;
453  
454 21 ▶ return unless(defined($end));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end 1 20 21
455  
456 20 my $age = time() - $end;
457 20 ▶ return unless($age > $max_age * 86400);
Branch: 1 branch
BranchTrueFalseTotal
unless $age > $max_age * 86400 16 4 20
458  
459 4 return sprintf('Warning: the newest report period ends %s, %d days ago; DMARC reporting may have stopped (--max-age=%d)',
460 _window($end), int($age / 86400), $max_age);
461 }
462  
463 46 ▶sub _window {
Subroutine _window: called CC=3 SCAR=12.5
464 46 my $epoch = shift;
465  
466 46 ▶ return '?' unless(defined($epoch) && $epoch =~ /\A\d+\z/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch and $epoch =~ /\A\d+\z/ 0 46 46
Condition33%1/3
defined $epoch and $epoch =~ /\A\d+\z/
!ll&&!rl&&r
0046
MC/DC0%0/2
defined $epoch and $epoch =~ /\A\d+\z/
defined $epoch $epoch =~ /\A\d+\z/
Truth table33%1/3
A and B
A= defined $epochB= $epoch =~ /\A\d+\z/
ABresult
0X0
100
111
467  
468 46 return strftime('%Y-%m-%d %H:%MZ', gmtime($epoch));
469 }
470  
471 __END__
↑ Summary lib/Mail/Send/DKIM.pm »