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 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 15 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 15 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 15 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 15 ▶use IO::Uncompress::Unzip qw($UnzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 15 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 15 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 15 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 15 ▶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 15 ▶my $home = $ENV{'HOME'} || (getpwuid($<))[7] || '.';
Condition40%2/5
$ENV{'HOME'} || (getpwuid $<)[7] || '.'
l!l
150
$ENV{"HOME"} or (getpwuid $<)[7]
l!l&&r!l&&!r
1500
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 15 ▶my $DEFAULT_DIR = $ENV{'DMARC_DIR'} || "$home/Downloads";
Condition100%2/2
$ENV{'DMARC_DIR'} || "$home/Downloads"
l!l
123
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 15 my (@dirs, @expected, $file_list, $help);
38 15 ▶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) 15 0 15
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 15 ▶pod2usage(-exitval => 0, -verbose => 1) if($help);
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 15 15
45  
46 15 ▶@dirs = ($DEFAULT_DIR) unless(@dirs);
Branch: 1 branch
BranchTrueFalseTotal
unless @dirs 0 15 15
47 15 ▶unless(@expected) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@expected) 15 0 15
48 15 ▶ @expected = grep { length } split(/[\s,]+/, $ENV{'DMARC_EXPECTED'} // '');
Condition50%1/2
$ENV{"DMARC_EXPECTED"} // ""
l!l
150
MC/DC0%0/1
$ENV{"DMARC_EXPECTED"} // ""
$ENV{"DMARC_EXPECTED"}
Truth table50%1/2
A
A= $ENV{"DMARC_EXPECTED"}
Aresult
11
00
49 15 ▶ pod2usage(
Branch: 1 branch
BranchTrueFalseTotal
unless @expected 0 15 15
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 15 my %expected = map { $_ => 1 } @expected;
55  
56 15 my @files;
57 15 foreach my $dir (@dirs) {
58 15 ▶ die "Not a directory: $dir\n" unless(-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 14 15
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 14 foreach my $pattern ('*.xml', '*.xml.gz', '*.zip') {
63 42 push @files, glob(File::Spec->catfile($dir, $pattern));
64 }
65 }
66 14 my %seen;
67 14 @files = sort grep { !$seen{$_}++ } @files;
68 14 ▶die "No reports found in: @dirs\n" unless(@files);
Branch: 1 branch
BranchTrueFalseTotal
unless @files 1 13 14
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 13 Readonly::Scalar my $MAX_FILE_BYTES => 10 * 1024 * 1024;
76 13 Readonly::Scalar my $MAX_XML_BYTES => 20 * 1024 * 1024;
77  
78 sub read_report
79 14 ▶{
Subroutine read_report: called CC=9 SCAR=23.2
80 14 my $path = shift;
81  
82 14 my $size = -s $path;
83 14 ▶ 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 14 14
Condition33%1/3
defined $size and $size > $MAX_FILE_BYTES
!ll&&!rl&&r
0140
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 14 my $xml;
86 14 ▶ if($path =~ /\.zip\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ /\.zip\z/) { } 1 13 14
elsif ($path =~ /\.gz\z/) { } 1 12 13
87 1 $xml = read_zipped_xml($path);
88 } elsif($path =~ /\.gz\z/) {
89 1 $xml = read_gzipped_xml($path);
90 } else {
91 12 ▶ open(my $fh, '<', $path) or die "Cannot read $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Cannot read ${path}: $!\n" 12 0 12
92 12 local $/;
93 12 $xml = <$fh>;
94 12 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 14 ▶ die "not a DMARC report\n" unless(index($xml, '<feedback') >= 0);
Branch: 1 branch
BranchTrueFalseTotal
unless (index($xml, "<feedback") != -1) 0 14 14
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 14 my $root = XML::PP->new()->parse($xml);
105 14 ▶ 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 14 14
Condition33%1/3
ref $root eq "HASH" and defined $root->{"name"}
!ll&&!rl&&r
0014
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 14 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 4032 ▶{
Subroutine descendants: called CC=5 SCAR=16.1
115 4032 my ($node, $name) = @_;
116  
117 4032 my @found;
118 4032 ▶ foreach my $child (@{$node->{'children'} || []}) {
Condition50%1/2
$node->{'children'} || []
l!l
40320
MC/DC0%0/1
$node->{'children'} || []
$node->{'children'}
Truth table50%1/2
A
A= $node->{'children'}
Aresult
11
00
119 6384 ▶ next unless(defined($child->{'name'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child->{"name"} 2758 3626 6384
120 3626 ▶ push @found, $child if($child->{'name'} eq $name);
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"name"} eq $name 406 3220 3626
121 3626 push @found, descendants($child, $name);
122 }
123  
124 4032 return @found;
125 }
126  
127 # Only the direct children with the given name.
128 sub children_named
129 28 ▶{
Subroutine children_named: called CC=3 SCAR=11.3
130 28 my ($node, $name) = @_;
131  
132 28 ▶ return grep { defined($_->{'name'}) && $_->{'name'} eq $name } @{$node->{'children'} || []};
Condition60%3/5
defined $_->{'name'} && $_->{'name'} eq $name
!ll&&!rl&&r
02828
$node->{'children'} || []
l!l
280
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 308 ▶{
Subroutine text_content: called CC=4 SCAR=14.9
138 308 my $node = shift;
139  
140 308 ▶ return join('', map { defined($_->{'name'}) ? text_content($_) : ($_->{'text'} // '') } @{$node->{'children'} || []});
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'name'} ? : 0 308 308
Condition50%2/4
$_->{"text"} // ""
l!l
3080
$node->{'children'} || []
l!l
3080
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 308 ▶{
Subroutine text_in: called CC=4 SCAR=14.5
193 308 my ($node, $name) = @_;
194  
195 308 ▶ return '' unless($node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 308 308
196  
197 308 foreach my $found (descendants($node, $name)) {
198 308 my $text = text_content($found);
199 308 $text =~ s/\A\s+//;
200 308 $text =~ s/\s+\z//;
201 308 ▶ return $text if(length($text));
Branch: 1 branch
BranchTrueFalseTotal
if length $text 308 0 308
202 }
203  
204 0 return '';
205 }
206  
207 sub when
208 28 ▶{
Subroutine when: called CC=3 SCAR=12.5
209 28 my $epoch = shift;
210  
211 28 ▶ return '?' unless(defined($epoch) && $epoch =~ /\A\d+\z/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch and $epoch =~ /\A\d+\z/ 0 28 28
Condition33%1/3
defined $epoch and $epoch =~ /\A\d+\z/
!ll&&!rl&&r
0028
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 28 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 13 my %by_domain;
222 my @read_files;
223 13 my ($total_messages, $total_records, $reports_read) = (0, 0, 0);
224 13 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 13 my $misfiled_messages = 0;
228 13 my %misfiled;
229  
230 13 foreach my $path (@files) {
231 14 my $doc = eval { read_report($path) };
232 14 ▶ if(!$doc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc) 0 14 14
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 14 $reports_read++;
240 14 push @read_files, File::Spec->rel2abs($path);
241  
242 14 my ($meta) = descendants($doc, 'report_metadata');
243 14 my ($policy) = descendants($doc, 'policy_published');
244  
245 14 ▶ my ($date_range) = $meta ? descendants($meta, 'date_range') : ();
Branch: 1 branch
BranchTrueFalseTotal
$meta ? : 14 0 14
246 14 my $begin = text_in($date_range, 'begin');
247 14 my $end = text_in($date_range, 'end');
248  
249 14 print '=' x 78, "\n";
250 14 print basename($path), "\n";
251 14 printf " reporter : %s report_id: %s\n", text_in($meta, 'org_name'), text_in($meta, 'report_id');
252 14 printf " period : %s -> %s\n", when($begin), when($end);
253 14 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 14 my $reported_domain = text_in($policy, 'domain');
258  
259 14 my @records = descendants($doc, 'record');
260 14 ▶ if(!@records) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@records) 0 14 14
261 0 print " (no records in this report)\n";
262 0 next;
263 }
264  
265 14 foreach my $record (@records) {
266 14 my ($row) = descendants($record, 'row');
267 14 my ($evaluated) = descendants($record, 'policy_evaluated');
268  
269 14 my $ip = text_in($row, 'source_ip');
270 14 ▶ my $count = text_in($row, 'count') || 0;
Condition50%1/2
text_in($row, 'count') || 0
l!l
140
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 14 my $disposition = text_in($evaluated, 'disposition');
272 14 my $dkim_verdict = text_in($evaluated, 'dkim');
273 14 my $spf_verdict = text_in($evaluated, 'spf');
274 14 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 14 my (@dkim_auth, @spf_auth);
279 14 foreach my $results (descendants($record, 'auth_results')) {
280 14 foreach my $dkim (children_named($results, 'dkim')) {
281 14 ▶ push @dkim_auth, sprintf('%s/%s=%s',
Condition50%1/2
text_in($dkim, 'selector') || '-'
l!l
140
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 14 foreach my $spf (children_named($results, 'spf')) {
285 14 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 14 ▶ my $domain = $reported_domain || $header_from || '(unknown)';
Condition40%2/5
$reported_domain || $header_from || '(unknown)'
l!l
140
$reported_domain or $header_from
l!l&&r!l&&!r
1400
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 14 ▶ 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
1400
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 14 ▶ my $filed = ($disposition eq 'quarantine' || $disposition eq 'reject');
Condition33%1/3
$disposition eq 'quarantine' || $disposition eq 'reject'
l!l&&r!l&&!r
0014
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 14 ▶ my $word = ($disposition eq 'reject') ? 'rejected' : 'quarantined';
Branch: 1 branch
BranchTrueFalseTotal
$disposition eq 'reject' ? : 0 14 14
300  
301 14 my @flags;
302 14 ▶ if(!$expected{$ip}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $expected{$ip}) { } 6 8 14
elsif (not $passed) { } 0 8 8
303 6 push @flags, 'NOT ' . join('/', @expected);
304 6 $unexpected_messages += $count;
305 } elsif(!$passed) {
306 0 push @flags, 'expected host, not authenticating';
307 }
308  
309 14 ▶ if($filed && $passed && $expected{$ip}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($filed and $passed and $expected{$ip}) { } 0 14 14
elsif ($filed and $passed) { } 0 14 14
elsif ($filed and $expected{$ip}) { } 0 28 28
Condition33%2/6
$filed and $passed and $expected{$ip}
!ll&&!rl&&r
1400
$filed and $passed
!ll&&!rl&&r
1400
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
1400
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
1400
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 14 ▶ if($filed && ($passed || $expected{$ip})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filed and $passed || $expected{$ip}) 0 14 14
Condition16%1/6
$filed and $passed || $expected{$ip}
!ll&&!rl&&r
1400
$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 14 ▶ my $flag = @flags ? ' <== ' . join('; ', @flags) : '';
Branch: 1 branch
BranchTrueFalseTotal
@flags ? : 6 8 14
332  
333 14 printf " %-16s %5d dkim=%-5s spf=%-8s %-9s from=%s%s\n",
334 $ip, $count, $dkim_verdict, $spf_verdict, $disposition, $header_from, $flag;
335 14 ▶ printf " dkim seen: %s\n", (@dkim_auth ? join(', ', @dkim_auth) : 'none');
Branch: 1 branch
BranchTrueFalseTotal
@dkim_auth ? : 14 0 14
336 14 ▶ printf " spf seen: %s\n", (@spf_auth ? join(', ', @spf_auth) : 'none');
Branch: 1 branch
BranchTrueFalseTotal
@spf_auth ? : 14 0 14
337  
338 14 $total_records++;
339 14 $total_messages += $count;
340  
341 my $entry = $by_domain{$domain}{$ip} ||= {
342 'count' => 0, 'pass' => 0, 'fail' => 0,
343 14 ▶ 'unexpected' => (!$expected{$ip} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
!$expected{$ip} ? : 6 8 14
Condition50%1/2
$by_domain{$domain}{$ip} ||= {"count", 0, "pass", 0, "fail", 0, "unexpected", !$expected{$ip} ? 1 : 0, "dkim_seen", {}}
l!l
014
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 14 $entry->{'count'} += $count;
347 14 ▶ $entry->{'pass'} += $count if($passed);
Branch: 1 branch
BranchTrueFalseTotal
if $passed 14 0 14
348 14 ▶ $entry->{'fail'} += $count if(!$passed);
Branch: 1 branch
BranchTrueFalseTotal
unless $passed 0 14 14
349 14 $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 13 ▶if(defined($file_list)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_list) 10 3 13
356 10 ▶ 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" 10 0 10
357 10 print $fh map { "$_\n" } @read_files;
358 10 ▶ close($fh) or die "Cannot close $file_list: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${file_list}: $!\n" 10 0 10
359 }
360  
361 13 print '=' x 78, "\n";
362 13 printf "Totals over %d report(s): %d record(s), %d message(s)\n",
363 $reports_read, $total_records, $total_messages;
364 13 foreach my $domain (sort keys %by_domain) {
365 13 my $by_ip = $by_domain{$domain};
366  
367 13 my ($messages, $unexpected) = (0, 0);
368 13 foreach my $ip (keys %{$by_ip}) {
369 14 $messages += $by_ip->{$ip}{'count'};
370 14 ▶ $unexpected += $by_ip->{$ip}{'count'} if($by_ip->{$ip}{'unexpected'});
Branch: 1 branch
BranchTrueFalseTotal
if $by_ip->{$ip}{"unexpected"} 6 8 14
371 }
372  
373 13 printf "\n%s: %d message(s)", $domain, $messages;
374 13 ▶ printf ", %d not from %s", $unexpected, join('/', @expected) if($unexpected);
Branch: 1 branch
BranchTrueFalseTotal
if $unexpected 6 7 13
375 13 print "\n";
376  
377 13 printf " %-16s %8s %8s %8s %s\n", 'source_ip', 'messages', 'passed', 'failed', 'status';
378 13 ▶ 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 14 my $entry = $by_ip->{$ip};
380 14 ▶ my $status = $entry->{'unexpected'} ? 'UNEXPECTED SOURCE' : ($entry->{'fail'} ? 'expected, failing auth' : 'expected, authenticating');
Branch: 2 branches
BranchTrueFalseTotal
$entry->{'unexpected'} ? : 6 8 14
$entry->{'fail'} ? : 0 8 8
381 14 printf " %-16s %8d %8d %8d %s\n", $ip, $entry->{'count'}, $entry->{'pass'}, $entry->{'fail'}, $status;
382 14 my @dkim = sort keys %{$entry->{'dkim_seen'}};
383 14 ▶ printf " %-16s dkim: %s\n", '', (@dkim ? join(', ', @dkim) : 'no signature seen');
Branch: 1 branch
BranchTrueFalseTotal
@dkim ? : 14 0 14
384 }
385 }
386  
387 # Named after the totals, so the figures that explain them come first.
388 13 ▶if(%misfiled) {
Branch: 1 branch
BranchTrueFalseTotal
if (%misfiled) 0 13 13
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 13 print "\n";
407 13 ▶if(%misfiled) {
Branch: 1 branch
BranchTrueFalseTotal
if (%misfiled) 0 13 13
408 0 printf "%d message(s) were quarantined or rejected although they should not have been.\n", $misfiled_messages;
409 }
410 13 ▶if($unexpected_messages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unexpected_messages) 6 7 13
411 6 printf "%d message(s) came from addresses that are not %s.\n", $unexpected_messages, join('/', @expected);
412 6 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 »