bin/dmarc-report.pl

filter: Statementstmt 87.5% 211 / 241 Branchbran 57.5% 69 / 120 Conditioncond 36.3% 28 / 77 MC/DCmcdc 4.7% 2 / 42 Subroutinesub 100% 20 / 20 Podpod n/a totaltotal 66.0% 330 / 500 CC 31 SCAR 15.4
CC
31
Cov
70%
read_report
10.2
read_zipped_xml
6.3
descendants
5.0
CRAP 4.7
« lib/Mail/Send/DKIM.pm ↑ Summary bin/dmarc-check-mail.pl »
1 #!/usr/bin/env perl
2 #
3 # dmarc-report - summarise DMARC aggregate reports, and flag every source
4 # address that is not the host we expect to be sending as our domains.
5 #
6 # Aggregate reports arrive as XML, sometimes gzipped, and often several at a
7 # time, so this reads a whole directory rather than one file: every report
8 # that parses is summarised, then the totals per source address are given, and
9 # anything that did not come from the expected host is marked. The exit
10 # status is the part a cron job can act on - 0 when every message came from
11 # the expected host and authenticated, 1 when something else did - so it can
12 # run unattended over a reports directory.
13 #
14 # Namespace: some reporters put the report in a namespace and some do not, so
15 # every lookup is by local-name() rather than by the plain element name.
16  
17 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 16 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 16 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 16 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 16 ▶use IO::Uncompress::Unzip qw($UnzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 16 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 16 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 16 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 16 ▶use XML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # The hosts that are expected to send as these domains; everything else is a
31 # finding. There is no built-in address, so they come from --expected or from
32 # DMARC_EXPECTED, given as a comma or space separated list.
33  
34 16 ▶my $home = $ENV{'HOME'} || (getpwuid($<))[7] || '.';
Condition40%2/5
$ENV{'HOME'} || (getpwuid $<)[7] || '.'
l!l
160
$ENV{"HOME"} or (getpwuid $<)[7]
l!l&&r!l&&!r
1600
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
35 16 ▶my $DEFAULT_DIR = $ENV{'DMARC_DIR'} || "$home/Downloads";
Condition100%2/2
$ENV{'DMARC_DIR'} || "$home/Downloads"
l!l
133
MC/DC100%1/1
$ENV{'DMARC_DIR'} || "$home/Downloads"
$ENV{'DMARC_DIR'}
Truth table100%2/2
A
A= $ENV{'DMARC_DIR'}
Aresult
11
00
36  
37 16 my (@dirs, @expected, $file_list, $help);
38 16 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("dir|d=s", \@dirs, "expected|e=s", \@expected, "file-list=s", \$file_list, "help|h", \$help) or pod2usage("-exitval", 2, "-verbose", 0) 16 0 16
39 'dir|d=s' => \@dirs,
40 'expected|e=s' => \@expected,
41 'file-list=s' => \$file_list,
42 'help|h' => \$help,
43 ) or pod2usage(-exitval => 2, -verbose => 0);
44 16 ▶pod2usage(-exitval => 0, -verbose => 1) if($help);
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 16 16
45  
46 16 ▶@dirs = ($DEFAULT_DIR) unless(@dirs);
Branch: 1 branch
BranchTrueFalseTotal
unless @dirs 0 16 16
47 16 ▶unless(@expected) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@expected) 16 0 16
48 16 ▶ @expected = grep { length } split(/[\s,]+/, $ENV{'DMARC_EXPECTED'} // '');
Condition50%1/2
$ENV{"DMARC_EXPECTED"} // ""
l!l
160
MC/DC0%0/1
$ENV{"DMARC_EXPECTED"} // ""
$ENV{"DMARC_EXPECTED"}
Truth table50%1/2
A
A= $ENV{"DMARC_EXPECTED"}
Aresult
11
00
49 16 ▶ pod2usage(
Branch: 1 branch
BranchTrueFalseTotal
unless @expected 0 16 16
50 -exitval => 2, -verbose => 0,
51 -message => "No expected sender: give --expected IP, or set DMARC_EXPECTED to one or more IPs.\n",
52 ) unless(@expected);
53 }
54 16 my %expected = map { $_ => 1 } @expected;
55  
56 16 my @files;
57 16 foreach my $dir (@dirs) {
58 16 ▶ die "Not a directory: $dir\n" unless(-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 15 16
59 # Only the shapes a report arrives in. A bare *.gz would also match
60 # anything else parked in the same directory - a tarball, a downloaded
61 # data set - and trying to parse those is both slow and misleading.
62 15 foreach my $pattern ('*.xml', '*.xml.gz', '*.zip') {
63 45 push @files, glob(File::Spec->catfile($dir, $pattern));
64 }
65 }
66 15 my %seen;
67 15 @files = sort grep { !$seen{$_}++ } @files;
68 15 ▶die "No reports found in: @dirs\n" unless(@files);
Branch: 1 branch
BranchTrueFalseTotal
unless @files 1 14 15
69  
70 # Read one report, gunzipping it if it is compressed. Reports are small, so
71 # they are parsed in memory and there is no need to unpack them onto disc.
72 # Reports are kilobytes. Capping what is opened at all keeps a downloads
73 # directory usable: it holds installers, archives and data sets, and one of
74 # them is 13 GB, so matching on the extension alone is not enough.
75 14 Readonly::Scalar my $MAX_FILE_BYTES => 10 * 1024 * 1024;
76 14 Readonly::Scalar my $MAX_XML_BYTES => 20 * 1024 * 1024;
77  
78 sub read_report
79 15 ▶{
Subroutine read_report: called CC=9 SCAR=23.2
80 15 my $path = shift;
81  
82 15 my $size = -s $path;
83 15 ▶ die "too large to be a report ($size bytes)\n" if(defined($size) && $size > $MAX_FILE_BYTES);
Branch: 1 branch
BranchTrueFalseTotal
if defined $size and $size > $MAX_FILE_BYTES 0 15 15
Condition33%1/3
defined $size and $size > $MAX_FILE_BYTES
!ll&&!rl&&r
0150
MC/DC0%0/2
defined $size and $size > $MAX_FILE_BYTES
defined $size $size > $MAX_FILE_BYTES
Truth table33%1/3
A and B
A= defined $sizeB= $size > $MAX_FILE_BYTES
ABresult
0X0
100
111
84  
85 15 my $xml;
86 15 ▶ if($path =~ /\.zip\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ /\.zip\z/) { } 1 14 15
elsif ($path =~ /\.gz\z/) { } 1 13 14
87 1 $xml = read_zipped_xml($path);
88 } elsif($path =~ /\.gz\z/) {
89 1 $xml = read_gzipped_xml($path);
90 } else {
91 13 ▶ open(my $fh, '<', $path) or die "Cannot read $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Cannot read ${path}: $!\n" 13 0 13
92 13 local $/;
93 13 $xml = <$fh>;
94 13 close($fh);
95 }
96  
97 # A DMARC report is a <feedback> document. Saying so is clearer than
98 # letting the XML parser complain about a CSV or a tarball.
99 15 ▶ die "not a DMARC report\n" unless(index($xml, '<feedback') >= 0);
Branch: 1 branch
BranchTrueFalseTotal
unless (index($xml, "<feedback") != -1) 0 15 15
100  
101 # XML::PP is pure Perl, so it installs where XML::PP does not (Windows).
102 # It returns the root element; wrap it so that, as with a document, the root
103 # is itself something to search for.
104 15 my $root = XML::PP->new()->parse($xml);
105 15 ▶ die "not a DMARC report\n" unless(ref($root) eq 'HASH' && defined($root->{'name'}));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $root eq "HASH" and defined $root->{"name"} 0 15 15
Condition33%1/3
ref $root eq "HASH" and defined $root->{"name"}
!ll&&!rl&&r
0015
MC/DC0%0/2
ref $root eq "HASH" and defined $root->{"name"}
ref $root eq "HASH" defined $root->{"name"}
Truth table33%1/3
A and B
A= ref $root eq "HASH"B= defined $root->{"name"}
ABresult
0X0
100
111
106  
107 15 return { 'children' => [ $root ] };
108 }
109  
110 # Every element below $node, in document order, with the given name. The
111 # namespace prefix is not part of the name XML::PP gives, so this matches on
112 # the local name, as the reports' differing prefixes require.
113 sub descendants
114 4320 ▶{
Subroutine descendants: called CC=5 SCAR=16.1
115 4320 my ($node, $name) = @_;
116  
117 4320 my @found;
118 4320 ▶ foreach my $child (@{$node->{'children'} || []}) {
Condition50%1/2
$node->{'children'} || []
l!l
43200
MC/DC0%0/1
$node->{'children'} || []
$node->{'children'}
Truth table50%1/2
A
A= $node->{'children'}
Aresult
11
00
119 6840 ▶ next unless(defined($child->{'name'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child->{"name"} 2955 3885 6840
120 3885 ▶ push @found, $child if($child->{'name'} eq $name);
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"name"} eq $name 435 3450 3885
121 3885 push @found, descendants($child, $name);
122 }
123  
124 4320 return @found;
125 }
126  
127 # Only the direct children with the given name.
128 sub children_named
129 30 ▶{
Subroutine children_named: called CC=3 SCAR=11.3
130 30 my ($node, $name) = @_;
131  
132 30 ▶ return grep { defined($_->{'name'}) && $_->{'name'} eq $name } @{$node->{'children'} || []};
Condition60%3/5
defined $_->{'name'} && $_->{'name'} eq $name
!ll&&!rl&&r
03030
$node->{'children'} || []
l!l
300
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
133 }
134  
135 # All the text under a node, as textContent gave it.
136 sub text_content
137 330 ▶{
Subroutine text_content: called CC=4 SCAR=14.9
138 330 my $node = shift;
139  
140 330 ▶ return join('', map { defined($_->{'name'}) ? text_content($_) : ($_->{'text'} // '') } @{$node->{'children'} || []});
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'name'} ? : 0 330 330
Condition50%2/4
$_->{"text"} // ""
l!l
3300
$node->{'children'} || []
l!l
3300
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
141 }
142  
143 sub read_gzipped_xml
144 1 ▶{
Subroutine read_gzipped_xml: called CC=2 SCAR=7.1
145 1 my $path = shift;
146  
147 1 ▶ my $gz = IO::Uncompress::Gunzip->new($path) or die "Cannot gunzip $path: $GunzipError\n";
Branch: 1 branch
BranchTrueFalseTotal
my $gz = "IO::Uncompress::Gunzip"->new($path) or die "Cannot gunzip ${path}: $main::GunzipError\n" 1 0 1
148  
149 1 return _read_bounded($gz, $path);
150 }
151  
152 # Reporters that send a .zip put the report inside, sometimes beside a
153 # signature file, so the first member ending in .xml is the one to read.
154 sub read_zipped_xml
155 1 ▶{
Subroutine read_zipped_xml: called CC=5 SCAR=18.4
156 1 my $path = shift;
157  
158 1 ▶ my $zip = IO::Uncompress::Unzip->new($path) or die "Cannot open $path: $UnzipError\n";
Branch: 1 branch
BranchTrueFalseTotal
my $zip = "IO::Uncompress::Unzip"->new($path) or die "Cannot open ${path}: $main::UnzipError\n" 1 0 1
159  
160 1 my $status = 1;
161 1 ▶ while($status > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status > 0) 1 0 1
162 1 ▶ my $name = $zip->getHeaderInfo()->{'Name'} || '';
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
163 1 ▶ return _read_bounded($zip, $path) if($name =~ /\.xml\z/i);
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\.xml\z/i 1 0 1
164 0 $status = $zip->nextStream();
165 }
166  
167 0 die "No XML inside $path\n";
168 }
169  
170 # Read to the end of the current stream, giving up rather than filling memory
171 # with something that turned out not to be a report.
172 sub _read_bounded
173 2 ▶{
Subroutine _read_bounded: called CC=4 SCAR=14.0
174 2 my ($handle, $path) = @_;
175  
176 2 my $xml = '';
177 2 while(1) {
178 4 my $read = $handle->read(my $chunk, 65536);
179 4 ▶ die "Cannot read $path: ", $handle->error(), "\n" if(!defined($read));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read 0 4 4
180 4 ▶ last if($read == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $read == 0 2 2 4
181 2 $xml .= $chunk;
182 2 ▶ die "expands beyond $MAX_XML_BYTES bytes; not a report\n" if(length($xml) > $MAX_XML_BYTES);
Branch: 1 branch
BranchTrueFalseTotal
if length $xml > $MAX_XML_BYTES 0 2 2
183 }
184  
185 2 return $xml;
186 }
187  
188 # The first non-empty text under a named child. Used for the single-valued
189 # fields, where a reporter that repeats an element would otherwise make the
190 # concatenated text look like a value.
191 sub text_in
192 330 ▶{
Subroutine text_in: called CC=4 SCAR=14.5
193 330 my ($node, $name) = @_;
194  
195 330 ▶ return '' unless($node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 330 330
196  
197 330 foreach my $found (descendants($node, $name)) {
198 330 my $text = text_content($found);
199 330 $text =~ s/\A\s+//;
200 330 $text =~ s/\s+\z//;
201 330 ▶ return $text if(length($text));
Branch: 1 branch
BranchTrueFalseTotal
if length $text 330 0 330
202 }
203  
204 0 return '';
205 }
206  
207 sub when
208 30 ▶{
Subroutine when: called CC=3 SCAR=12.5
209 30 my $epoch = shift;
210  
211 30 ▶ return '?' unless(defined($epoch) && $epoch =~ /\A\d+\z/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch and $epoch =~ /\A\d+\z/ 0 30 30
Condition33%1/3
defined $epoch and $epoch =~ /\A\d+\z/
!ll&&!rl&&r
0030
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
212  
213 30 return strftime('%Y-%m-%d %H:%M UTC', gmtime($epoch));
214 }
215  
216 # Totals per reported domain and source address, across every report. One
217 # address spread over several reports is one problem, not several - but the
218 # reports for every domain arrive at the same mailbox, so their figures must
219 # not be added together: an address that legitimately sends for one domain is
220 # not thereby legitimate for another.
221 14 my %by_domain;
222 my @read_files;
223 14 my ($total_messages, $total_records, $reports_read) = (0, 0, 0);
224 14 my $unexpected_messages = 0;
225 # Messages the policy quarantined or rejected that should not have been:
226 # either they authenticated, or they came from an expected host.
227 14 my $misfiled_messages = 0;
228 14 my %misfiled;
229  
230 14 foreach my $path (@files) {
231 15 my $doc = eval { read_report($path) };
232 15 ▶ if(!$doc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc) 0 15 15
233 0 my ($why) = split(/\n/, "$@");
234 # "not a report" is the normal case in a downloads directory, so it is
235 # passed over; anything else means a report we could not read.
236 0 ▶ warn 'Skipping ', basename($path), ": $why\n" unless($why =~ /not a (DMARC )?report|too large|expands beyond/);
Branch: 1 branch
BranchTrueFalseTotal
unless $why =~ /not a (DMARC )?report|too large|expands beyond/ 0 0 0
237 0 next;
238 }
239 15 $reports_read++;
240 15 push @read_files, File::Spec->rel2abs($path);
241  
242 15 my ($meta) = descendants($doc, 'report_metadata');
243 15 my ($policy) = descendants($doc, 'policy_published');
244  
245 15 ▶ my ($date_range) = $meta ? descendants($meta, 'date_range') : ();
Branch: 1 branch
BranchTrueFalseTotal
$meta ? : 15 0 15
246 15 my $begin = text_in($date_range, 'begin');
247 15 my $end = text_in($date_range, 'end');
248  
249 15 print '=' x 78, "\n";
250 15 print basename($path), "\n";
251 15 printf " reporter : %s report_id: %s\n", text_in($meta, 'org_name'), text_in($meta, 'report_id');
252 15 printf " period : %s -> %s\n", when($begin), when($end);
253 15 printf " domain : %s p=%s sp=%s adkim=%s aspf=%s pct=%s\n",
254 text_in($policy, 'domain'), text_in($policy, 'p'), text_in($policy, 'sp'),
255 text_in($policy, 'adkim'), text_in($policy, 'aspf'), text_in($policy, 'pct');
256  
257 15 my $reported_domain = text_in($policy, 'domain');
258  
259 15 my @records = descendants($doc, 'record');
260 15 ▶ if(!@records) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@records) 0 15 15
261 0 print " (no records in this report)\n";
262 0 next;
263 }
264  
265 15 foreach my $record (@records) {
266 15 my ($row) = descendants($record, 'row');
267 15 my ($evaluated) = descendants($record, 'policy_evaluated');
268  
269 15 my $ip = text_in($row, 'source_ip');
270 15 ▶ my $count = text_in($row, 'count') || 0;
Condition50%1/2
text_in($row, 'count') || 0
l!l
150
MC/DC0%0/1
text_in($row, 'count') || 0
text_in($row, 'count')
Truth table50%1/2
A
A= text_in($row, 'count')
Aresult
11
00
271 15 my $disposition = text_in($evaluated, 'disposition');
272 15 my $dkim_verdict = text_in($evaluated, 'dkim');
273 15 my $spf_verdict = text_in($evaluated, 'spf');
274 15 my $header_from = text_in($record, 'header_from');
275  
276 # The addresses actually seen, which is what says whether a message
277 # carried a signature at all rather than merely failing its check.
278 15 my (@dkim_auth, @spf_auth);
279 15 foreach my $results (descendants($record, 'auth_results')) {
280 15 foreach my $dkim (children_named($results, 'dkim')) {
281 15 ▶ push @dkim_auth, sprintf('%s/%s=%s',
Condition50%1/2
text_in($dkim, 'selector') || '-'
l!l
150
MC/DC0%0/1
text_in($dkim, 'selector') || '-'
text_in($dkim, 'selector')
Truth table50%1/2
A
A= text_in($dkim, 'selector')
Aresult
11
00
282 text_in($dkim, 'domain'), (text_in($dkim, 'selector') || '-'), text_in($dkim, 'result'));
283 }
284 15 foreach my $spf (children_named($results, 'spf')) {
285 15 push @spf_auth, sprintf('%s=%s', text_in($spf, 'domain'), text_in($spf, 'result'));
286 }
287 }
288  
289 # A reporter that leaves policy_published out is grouped by the From
290 # domain of the record instead, so nothing falls into a shared bucket.
291 15 ▶ my $domain = $reported_domain || $header_from || '(unknown)';
Condition40%2/5
$reported_domain || $header_from || '(unknown)'
l!l
150
$reported_domain or $header_from
l!l&&r!l&&!r
1500
MC/DC0%0/2
$reported_domain || $header_from || '(unknown)'
$reported_domain $header_from
Truth table33%1/3
A or B
A= $reported_domainB= $header_from
ABresult
1X1
011
000
292  
293 15 ▶ my $passed = ($dkim_verdict eq 'pass') || ($spf_verdict eq 'pass');
Condition33%1/3
$dkim_verdict eq 'pass' || $spf_verdict eq 'pass'
l!l&&r!l&&!r
1500
MC/DC0%0/2
$dkim_verdict eq 'pass' || $spf_verdict eq 'pass'
$dkim_verdict eq 'pass' $spf_verdict eq 'pass'
Truth table33%1/3
A || B
A= $dkim_verdict eq 'pass'B= $spf_verdict eq 'pass'
ABresult
1X1
011
000
294  
295 # Two failures worth naming as they happen, because neither shows up in
296 # the per-address totals: mail that authenticated and was filed anyway,
297 # and mail of ours that the policy filed.
298 15 ▶ my $filed = ($disposition eq 'quarantine' || $disposition eq 'reject');
Condition33%1/3
$disposition eq 'quarantine' || $disposition eq 'reject'
l!l&&r!l&&!r
0015
MC/DC0%0/2
$disposition eq 'quarantine' || $disposition eq 'reject'
$disposition eq 'quarantine' $disposition eq 'reject'
Truth table33%1/3
A || B
A= $disposition eq 'quarantine'B= $disposition eq 'reject'
ABresult
1X1
011
000
299 15 ▶ my $word = ($disposition eq 'reject') ? 'rejected' : 'quarantined';
Branch: 1 branch
BranchTrueFalseTotal
$disposition eq 'reject' ? : 0 15 15
300  
301 15 my @flags;
302 15 ▶ if(!$expected{$ip}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $expected{$ip}) { } 7 8 15
elsif (not $passed) { } 0 8 8
303 7 push @flags, 'NOT ' . join('/', @expected);
304 7 $unexpected_messages += $count;
305 } elsif(!$passed) {
306 0 push @flags, 'expected host, not authenticating';
307 }
308  
309 15 ▶ if($filed && $passed && $expected{$ip}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($filed and $passed and $expected{$ip}) { } 0 15 15
elsif ($filed and $passed) { } 0 15 15
elsif ($filed and $expected{$ip}) { } 0 30 30
Condition33%2/6
$filed and $passed and $expected{$ip}
!ll&&!rl&&r
1500
$filed and $passed
!ll&&!rl&&r
1500
MC/DC0%0/3
$filed and $passed and $expected{$ip}
$filed $passed $expected{$ip}
Truth table25%1/4
A and B and C
A= $filedB= $passedC= $expected{$ip}
ABCresult
0XX0
10X0
1100
1111
310 0 push @flags, "expected sender, $word though authenticated";
311 ▶ } elsif($filed && $passed) {
Condition33%1/3
$filed and $passed
!ll&&!rl&&r
1500
MC/DC0%0/2
$filed and $passed
$filed $passed
Truth table33%1/3
A and B
A= $filedB= $passed
ABresult
0X0
100
111
312 0 push @flags, "authenticated, but $word";
313 ▶ } elsif($filed && $expected{$ip}) {
Condition33%1/3
$filed and $expected{$ip}
!ll&&!rl&&r
1500
MC/DC0%0/2
$filed and $expected{$ip}
$filed $expected{$ip}
Truth table33%1/3
A and B
A= $filedB= $expected{$ip}
ABresult
0X0
100
111
314 0 push @flags, "expected sender, $word";
315 }
316  
317 15 ▶ if($filed && ($passed || $expected{$ip})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filed and $passed || $expected{$ip}) 0 15 15
Condition16%1/6
$filed and $passed || $expected{$ip}
!ll&&!rl&&r
1500
$passed or $expected{$ip}
l!l&&r!l&&!r
000
MC/DC0%0/3
$filed and $passed || $expected{$ip}
$filed $passed $expected{$ip}
Truth table25%1/4
A and B or C
A= $filedB= $passedC= $expected{$ip}
ABCresult
0XX0
1000
11X1
1011
318 0 ▶ my $finding = $misfiled{join("\t", $domain, $ip, $disposition)} ||= {
Condition0%0/2
$misfiled{join "\t", $domain, $ip, $disposition} ||= {"domain", $domain, "ip", $ip, "disposition", $disposition, "count", 0, "passed", 0, "expected", 0, "dkim", {}, "spf", {}}
l!l
00
MC/DC0%0/1
$misfiled{join "\t", $domain, $ip, $disposition} ||= {"domain", $domain, "ip", $ip, "disposition", $disposition, "count", 0, "passed", 0, "expected", 0, "dkim", {}, "spf", {}}
$misfiled{join "\t", $domain, $ip, $disposition}
Truth table0%0/2
A
A= $misfiled{join "\t", $domain, $ip, $disposition}
Aresult
11
00
319 'domain' => $domain, 'ip' => $ip, 'disposition' => $disposition,
320 'count' => 0, 'passed' => 0, 'expected' => 0,
321 'dkim' => {}, 'spf' => {},
322 };
323 0 $finding->{'count'} += $count;
324 0 ▶ $finding->{'passed'} = 1 if($passed);
Branch: 1 branch
BranchTrueFalseTotal
if $passed 0 0 0
325 0 ▶ $finding->{'expected'} = 1 if($expected{$ip});
Branch: 1 branch
BranchTrueFalseTotal
if $expected{$ip} 0 0 0
326 0 $finding->{'dkim'}{$dkim_verdict} = 1;
327 0 $finding->{'spf'}{$spf_verdict} = 1;
328 0 $misfiled_messages += $count;
329 }
330  
331 15 ▶ my $flag = @flags ? ' <== ' . join('; ', @flags) : '';
Branch: 1 branch
BranchTrueFalseTotal
@flags ? : 7 8 15
332  
333 15 printf " %-16s %5d dkim=%-5s spf=%-8s %-9s from=%s%s\n",
334 $ip, $count, $dkim_verdict, $spf_verdict, $disposition, $header_from, $flag;
335 15 ▶ printf " dkim seen: %s\n", (@dkim_auth ? join(', ', @dkim_auth) : 'none');
Branch: 1 branch
BranchTrueFalseTotal
@dkim_auth ? : 15 0 15
336 15 ▶ printf " spf seen: %s\n", (@spf_auth ? join(', ', @spf_auth) : 'none');
Branch: 1 branch
BranchTrueFalseTotal
@spf_auth ? : 15 0 15
337  
338 15 $total_records++;
339 15 $total_messages += $count;
340  
341 my $entry = $by_domain{$domain}{$ip} ||= {
342 'count' => 0, 'pass' => 0, 'fail' => 0,
343 15 ▶ 'unexpected' => (!$expected{$ip} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
!$expected{$ip} ? : 7 8 15
Condition50%1/2
$by_domain{$domain}{$ip} ||= {"count", 0, "pass", 0, "fail", 0, "unexpected", !$expected{$ip} ? 1 : 0, "dkim_seen", {}}
l!l
015
MC/DC0%0/1
$by_domain{$domain}{$ip} ||= {"count", 0, "pass", 0, "fail", 0, "unexpected", !$expected{$ip} ? 1 : 0, "dkim_seen", {}}
$by_domain{$domain}{$ip}
Truth table50%1/2
A
A= $by_domain{$domain}{$ip}
Aresult
11
00
344 'dkim_seen' => {},
345 };
346 15 $entry->{'count'} += $count;
347 15 ▶ $entry->{'pass'} += $count if($passed);
Branch: 1 branch
BranchTrueFalseTotal
if $passed 15 0 15
348 15 ▶ $entry->{'fail'} += $count if(!$passed);
Branch: 1 branch
BranchTrueFalseTotal
unless $passed 0 15 15
349 15 $entry->{'dkim_seen'}{$_} = 1 foreach(@dkim_auth);
350 }
351 }
352  
353 # Named before the summary is printed, so a caller that moves the files aside
354 # knows exactly which ones were summarised - and which were merely in the way.
355 14 ▶if(defined($file_list)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_list) 11 3 14
356 11 ▶ open(my $fh, '>', $file_list) or die "Cannot write $file_list: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file_list or die "Cannot write ${file_list}: $!\n" 11 0 11
357 11 print $fh map { "$_\n" } @read_files;
358 11 ▶ close($fh) or die "Cannot close $file_list: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${file_list}: $!\n" 11 0 11
359 }
360  
361 14 print '=' x 78, "\n";
362 14 printf "Totals over %d report(s): %d record(s), %d message(s)\n",
363 $reports_read, $total_records, $total_messages;
364 14 foreach my $domain (sort keys %by_domain) {
365 14 my $by_ip = $by_domain{$domain};
366  
367 14 my ($messages, $unexpected) = (0, 0);
368 14 foreach my $ip (keys %{$by_ip}) {
369 15 $messages += $by_ip->{$ip}{'count'};
370 15 ▶ $unexpected += $by_ip->{$ip}{'count'} if($by_ip->{$ip}{'unexpected'});
Branch: 1 branch
BranchTrueFalseTotal
if $by_ip->{$ip}{"unexpected"} 7 8 15
371 }
372  
373 14 printf "\n%s: %d message(s)", $domain, $messages;
374 14 ▶ printf ", %d not from %s", $unexpected, join('/', @expected) if($unexpected);
Branch: 1 branch
BranchTrueFalseTotal
if $unexpected 7 7 14
375 14 print "\n";
376  
377 14 printf " %-16s %8s %8s %8s %s\n", 'source_ip', 'messages', 'passed', 'failed', 'status';
378 14 ▶ foreach my $ip (sort { $by_ip->{$b}{'count'} <=> $by_ip->{$a}{'count'} || $a cmp $b } keys %{$by_ip}) {
Condition33%1/3
$by_ip->{$b}{'count'} <=> $by_ip->{$a}{'count'} || $a cmp $b
l!l&&r!l&&!r
100
MC/DC0%0/2
$by_ip->{$b}{'count'} <=> $by_ip->{$a}{'count'} || $a cmp $b
$by_ip->{$b}{'count'} <=> $by_ip->{$a}{'count'} $a cmp $b
Truth table33%1/3
A || B
A= $by_ip->{$b}{'count'} <=> $by_ip->{$a}{'count'}B= $a cmp $b
ABresult
1X1
011
000
379 15 my $entry = $by_ip->{$ip};
380 15 ▶ my $status = $entry->{'unexpected'} ? 'UNEXPECTED SOURCE' : ($entry->{'fail'} ? 'expected, failing auth' : 'expected, authenticating');
Branch: 2 branches
BranchTrueFalseTotal
$entry->{'unexpected'} ? : 7 8 15
$entry->{'fail'} ? : 0 8 8
381 15 printf " %-16s %8d %8d %8d %s\n", $ip, $entry->{'count'}, $entry->{'pass'}, $entry->{'fail'}, $status;
382 15 my @dkim = sort keys %{$entry->{'dkim_seen'}};
383 15 ▶ printf " %-16s dkim: %s\n", '', (@dkim ? join(', ', @dkim) : 'no signature seen');
Branch: 1 branch
BranchTrueFalseTotal
@dkim ? : 15 0 15
384 }
385 }
386  
387 # Named after the totals, so the figures that explain them come first.
388 14 ▶if(%misfiled) {
Branch: 1 branch
BranchTrueFalseTotal
if (%misfiled) 0 14 14
389 0 print '=' x 78, "\n";
390 0 printf "Filed by policy although it should not have been: %d message(s)\n", $misfiled_messages;
391 0 printf " %-22s %-16s %-11s %8s %s\n", 'domain', 'source_ip', 'disposition', 'messages', 'why';
392 0 foreach my $finding (sort {
393 0 ▶ $a->{'domain'} cmp $b->{'domain'} || $b->{'count'} <=> $a->{'count'} || $a->{'ip'} cmp $b->{'ip'}
Condition0%0/6
$main::a->{'domain'} cmp $main::b->{'domain'} || $main::b->{'count'} <=> $main::a->{'count'} || $main::a->{'ip'} cmp $main::b->{'ip'}
l!l&&r!l&&!r
000
$main::a->{"domain"} cmp $main::b->{"domain"} or $main::b->{"count"} <=> $main::a->{"count"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$main::a->{'domain'} cmp $main::b->{'domain'} || $main::b->{'count'} <=> $main::a->{'count'} || $main::a->{'ip'} cmp $main::b->{'ip'}
$main::a->{"domain"} cmp $main::b->{"domain"} $main::b->{"count"} <=> $main::a->{"count"} $main::a->{'ip'} cmp $main::b->{'ip'}
Truth table0%0/4
A or B || C
A= $main::a->{"domain"} cmp $main::b->{"domain"}B= $main::b->{"count"} <=> $main::a->{"count"}C= $main::a->{'ip'} cmp $main::b->{'ip'}
ABCresult
1XX1
01X1
0011
0000
394 } values %misfiled) {
395 my $reason = $finding->{'expected'}
396 0 ▶ ? ($finding->{'passed'} ? 'expected sender, and authenticated' : 'expected sender')
Branch: 2 branches
BranchTrueFalseTotal
$finding->{'expected'} ? : 0 0 0
$finding->{'passed'} ? : 0 0 0
397 : 'authenticated';
398 0 ▶ $reason .= ($finding->{'disposition'} eq 'reject') ? ', yet rejected' : ', yet quarantined';
Branch: 1 branch
BranchTrueFalseTotal
$finding->{'disposition'} eq 'reject' ? : 0 0 0
399 printf " %-22s %-16s %-11s %8d %s (dkim=%s spf=%s)\n",
400 $finding->{'domain'}, $finding->{'ip'}, $finding->{'disposition'},
401 $finding->{'count'}, $reason,
402 0 join('/', sort keys %{$finding->{'dkim'}}), join('/', sort keys %{$finding->{'spf'}});
403 }
404 }
405  
406 14 print "\n";
407 14 ▶if(%misfiled) {
Branch: 1 branch
BranchTrueFalseTotal
if (%misfiled) 0 14 14
408 0 printf "%d message(s) were quarantined or rejected although they should not have been.\n", $misfiled_messages;
409 }
410 14 ▶if($unexpected_messages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unexpected_messages) 7 7 14
411 7 printf "%d message(s) came from addresses that are not %s.\n", $unexpected_messages, join('/', @expected);
412 7 exit(1);
413 }
414 7 ▶exit(1) if(%misfiled);
Branch: 1 branch
BranchTrueFalseTotal
if %misfiled 0 7 7
415 7 print "Every message came from ", join('/', @expected), ", and none was filed by policy.\n";
416 7 exit(0);
417  
418 __END__
« lib/Mail/Send/DKIM.pm ↑ Summary bin/dmarc-check-mail.pl »