bin/dmarc-report.pl

filter: Statementstmt 88.8% 223 / 251 Branchbran 63.2% 81 / 128 Conditioncond 36.3% 28 / 77 MC/DCmcdc 4.7% 2 / 42 Subroutinesub 100% 21 / 21 Podpod n/a totaltotal 68.4% 355 / 519 CC 32 SCAR 15.1
CC
32
Cov
73%
read_report
9.8
read_zipped_xml
6.3
descendants
5.0
CRAP 4.5
« 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 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 18 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 18 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 18 ▶use IO::Uncompress::Unzip qw($UnzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 18 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 18 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 18 ▶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 18 ▶my $home = $ENV{'HOME'} || (getpwuid($<))[7] || '.';
Condition40%2/5
$ENV{'HOME'} || (getpwuid $<)[7] || '.'
l!l
180
$ENV{"HOME"} or (getpwuid $<)[7]
l!l&&r!l&&!r
1800
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 18 ▶my $DEFAULT_DIR = $ENV{'DMARC_DIR'} || "$home/Downloads";
Condition100%2/2
$ENV{'DMARC_DIR'} || "$home/Downloads"
l!l
153
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 18 my (@dirs, @expected, $file_list, $help);
38 18 ▶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) 18 0 18
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 18 ▶pod2usage(-exitval => 0, -verbose => 1) if($help);
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 18 18
45  
46 18 ▶@dirs = ($DEFAULT_DIR) unless(@dirs);
Branch: 1 branch
BranchTrueFalseTotal
unless @dirs 0 18 18
47 18 ▶unless(@expected) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@expected) 18 0 18
48 18 ▶ @expected = grep { length } split(/[\s,]+/, $ENV{'DMARC_EXPECTED'} // '');
Condition50%1/2
$ENV{"DMARC_EXPECTED"} // ""
l!l
180
MC/DC0%0/1
$ENV{"DMARC_EXPECTED"} // ""
$ENV{"DMARC_EXPECTED"}
Truth table50%1/2
A
A= $ENV{"DMARC_EXPECTED"}
Aresult
11
00
49 18 ▶ pod2usage(
Branch: 1 branch
BranchTrueFalseTotal
unless @expected 0 18 18
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 18 my %expected = map { $_ => 1 } @expected;
55  
56 18 my @files;
57 18 foreach my $dir (@dirs) {
58 18 ▶ die "Not a directory: $dir\n" unless(-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 17 18
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 17 foreach my $pattern ('*.xml', '*.xml.gz', '*.zip') {
63 51 push @files, glob(File::Spec->catfile($dir, $pattern));
64 }
65 }
66 17 my %seen;
67 17 @files = sort grep { !$seen{$_}++ } @files;
68 17 ▶die "No reports found in: @dirs\n" unless(@files);
Branch: 1 branch
BranchTrueFalseTotal
unless @files 1 16 17
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 16 Readonly::Scalar my $MAX_FILE_BYTES => 10 * 1024 * 1024;
76 16 Readonly::Scalar my $MAX_XML_BYTES => 20 * 1024 * 1024;
77  
78 sub read_report
79 17 ▶{
Subroutine read_report: called CC=9 SCAR=22.8
80 17 my $path = shift;
81  
82 17 my $size = -s $path;
83 17 ▶ 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 17 17
Condition33%1/3
defined $size and $size > $MAX_FILE_BYTES
!ll&&!rl&&r
0170
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 17 my $xml;
86 17 ▶ if($path =~ /\.zip\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ /\.zip\z/) { } 1 16 17
elsif ($path =~ /\.gz\z/) { } 2 14 16
87 1 $xml = read_zipped_xml($path);
88 } elsif($path =~ /\.gz\z/) {
89 2 $xml = read_gzipped_xml($path);
90 } else {
91 14 ▶ open(my $fh, '<', $path) or die "Cannot read $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Cannot read ${path}: $!\n" 14 0 14
92 14 local $/;
93 14 $xml = <$fh>;
94 14 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 16 ▶ die "not a DMARC report\n" unless($xml =~ /<(?:[A-Za-z_][\w.-]*:)?feedback[\s\/>]/);
Branch: 1 branch
BranchTrueFalseTotal
unless $xml =~ m[<(?:[A-Za-z_][\w.-]*:)?feedback[\s/>]] 1 15 16
100  
101 # XML::PP returns the root element; wrap it so that, as with a document, the root
102 # is itself something to search for.
103 15 my $root = XML::PP->new()->parse($xml);
104 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
105  
106 15 return { 'children' => [ $root ] };
107 }
108  
109 # Every element below $node, in document order, with the given name. The
110 # namespace prefix is not part of the name XML::PP gives, so this matches on
111 # the local name, as the reports' differing prefixes require.
112 sub descendants
113 4320 ▶{
Subroutine descendants: called CC=5 SCAR=16.1
114 4320 my ($node, $name) = @_;
115  
116 4320 my @found;
117 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
118 6840 ▶ next unless(defined($child->{'name'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child->{"name"} 2955 3885 6840
119 3885 ▶ push @found, $child if($child->{'name'} eq $name);
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"name"} eq $name 435 3450 3885
120 3885 push @found, descendants($child, $name);
121 }
122  
123 4320 return @found;
124 }
125  
126 # Only the direct children with the given name.
127 sub children_named
128 30 ▶{
Subroutine children_named: called CC=3 SCAR=11.3
129 30 my ($node, $name) = @_;
130  
131 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
132 }
133  
134 # All the text under a node, as textContent gave it.
135 sub text_content
136 330 ▶{
Subroutine text_content: called CC=4 SCAR=14.9
137 330 my $node = shift;
138  
139 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
140 }
141  
142 sub read_gzipped_xml
143 2 ▶{
Subroutine read_gzipped_xml: called CC=2 SCAR=6.9
144 2 my $path = shift;
145  
146 2 ▶ 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 1 2
147  
148 1 return _read_bounded($gz, $path);
149 }
150  
151 # Reporters that send a .zip put the report inside, sometimes beside a
152 # signature file, so the first member ending in .xml is the one to read.
153 sub read_zipped_xml
154 1 ▶{
Subroutine read_zipped_xml: called CC=5 SCAR=18.4
155 1 my $path = shift;
156  
157 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
158  
159 1 my $status = 1;
160 1 ▶ while($status > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status > 0) 1 0 1
161 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
162 1 ▶ return _read_bounded($zip, $path) if($name =~ /\.xml\z/i);
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\.xml\z/i 1 0 1
163 0 $status = $zip->nextStream();
164 }
165  
166 0 die "No XML inside $path\n";
167 }
168  
169 # Read to the end of the current stream, giving up rather than filling memory
170 # with something that turned out not to be a report.
171 sub _read_bounded
172 2 ▶{
Subroutine _read_bounded: called CC=4 SCAR=14.0
173 2 my ($handle, $path) = @_;
174  
175 2 my $xml = '';
176 2 while(1) {
177 4 my $read = $handle->read(my $chunk, 65536);
178 4 ▶ die "Cannot read $path: ", $handle->error(), "\n" if(!defined($read));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read 0 4 4
179 4 ▶ last if($read == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $read == 0 2 2 4
180 2 $xml .= $chunk;
181 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
182 }
183  
184 2 return $xml;
185 }
186  
187 # The first non-empty text under a named child. Used for the single-valued
188 # fields, where a reporter that repeats an element would otherwise make the
189 # concatenated text look like a value.
190 sub text_in
191 330 ▶{
Subroutine text_in: called CC=4 SCAR=14.5
192 330 my ($node, $name) = @_;
193  
194 330 ▶ return '' unless($node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 330 330
195  
196 330 foreach my $found (descendants($node, $name)) {
197 330 my $text = text_content($found);
198 330 $text =~ s/\A\s+//;
199 330 $text =~ s/\s+\z//;
200 330 ▶ return _clean($text) if(length($text));
Branch: 1 branch
BranchTrueFalseTotal
if length $text 330 0 330
201 }
202  
203 0 return '';
204 }
205  
206 # Text from a report, or from whois, is somebody else's: control characters in
207 # it must not reach a terminal or a mail.
208 sub _clean
209 347 ▶{
Subroutine _clean: called CC=2 SCAR=7.4
210 347 my $text = shift;
211  
212 347 ▶ if(utf8::is_utf8($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($text)) { } 0 347 347
213 0 $text =~ s/[\x00-\x1f\x7f-\x9f]/ /g;
214 } else {
215 347 $text =~ s/[\x00-\x1f\x7f]|\xc2[\x80-\x9f]/ /g;
216 }
217  
218 347 return $text;
219 }
220  
221 sub when
222 30 ▶{
Subroutine when: called CC=3 SCAR=12.5
223 30 my $epoch = shift;
224  
225 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
226  
227 30 return strftime('%Y-%m-%d %H:%M UTC', gmtime($epoch));
228 }
229  
230 # Totals per reported domain and source address, across every report. One
231 # address spread over several reports is one problem, not several - but the
232 # reports for every domain arrive at the same mailbox, so their figures must
233 # not be added together: an address that legitimately sends for one domain is
234 # not thereby legitimate for another.
235 16 my %by_domain;
236 my @read_files;
237 16 my ($total_messages, $total_records, $reports_read) = (0, 0, 0);
238 16 my $unexpected_messages = 0;
239 16 my $unreadable = 0;
240 # Messages the policy quarantined or rejected that should not have been:
241 # either they authenticated, or they came from an expected host.
242 16 my $misfiled_messages = 0;
243 16 my %misfiled;
244  
245 16 foreach my $path (@files) {
246 17 my $doc = eval { read_report($path) };
247 17 ▶ if(!$doc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc) 2 15 17
248 2 my ($why) = split(/\n/, "$@");
249 # "not a report" is the normal case in a downloads directory, so it is
250 # passed over; anything else means a report we could not read.
251 2 ▶ warn 'Skipping ', _clean(basename($path)), ': ', _clean($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/ 1 1 2
252 # A zip of something else is not a report that went unread.
253 2 ▶ $unreadable++ unless($why =~ /not a (DMARC )?report|too large|expands beyond|No XML inside/);
Branch: 1 branch
BranchTrueFalseTotal
unless $why =~ /not a (DMARC )?report|too large|expands beyond|No XML inside/ 1 1 2
254 2 next;
255 }
256 15 $reports_read++;
257 15 push @read_files, File::Spec->rel2abs($path);
258  
259 15 my ($meta) = descendants($doc, 'report_metadata');
260 15 my ($policy) = descendants($doc, 'policy_published');
261  
262 15 ▶ my ($date_range) = $meta ? descendants($meta, 'date_range') : ();
Branch: 1 branch
BranchTrueFalseTotal
$meta ? : 15 0 15
263 15 my $begin = text_in($date_range, 'begin');
264 15 my $end = text_in($date_range, 'end');
265  
266 15 print '=' x 78, "\n";
267 15 print _clean(basename($path)), "\n";
268 15 printf " reporter : %s report_id: %s\n", text_in($meta, 'org_name'), text_in($meta, 'report_id');
269 15 printf " period : %s -> %s\n", when($begin), when($end);
270 15 printf " domain : %s p=%s sp=%s adkim=%s aspf=%s pct=%s\n",
271 text_in($policy, 'domain'), text_in($policy, 'p'), text_in($policy, 'sp'),
272 text_in($policy, 'adkim'), text_in($policy, 'aspf'), text_in($policy, 'pct');
273  
274 15 my $reported_domain = text_in($policy, 'domain');
275  
276 15 my @records = descendants($doc, 'record');
277 15 ▶ if(!@records) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@records) 0 15 15
278 0 print " (no records in this report)\n";
279 0 next;
280 }
281  
282 15 foreach my $record (@records) {
283 15 my ($row) = descendants($record, 'row');
284 15 my ($evaluated) = descendants($record, 'policy_evaluated');
285  
286 15 my $ip = text_in($row, 'source_ip');
287 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
288 15 my $disposition = text_in($evaluated, 'disposition');
289 15 my $dkim_verdict = text_in($evaluated, 'dkim');
290 15 my $spf_verdict = text_in($evaluated, 'spf');
291 15 my $header_from = text_in($record, 'header_from');
292  
293 # The addresses actually seen, which is what says whether a message
294 # carried a signature at all rather than merely failing its check.
295 15 my (@dkim_auth, @spf_auth);
296 15 foreach my $results (descendants($record, 'auth_results')) {
297 15 foreach my $dkim (children_named($results, 'dkim')) {
298 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
299 text_in($dkim, 'domain'), (text_in($dkim, 'selector') || '-'), text_in($dkim, 'result'));
300 }
301 15 foreach my $spf (children_named($results, 'spf')) {
302 15 push @spf_auth, sprintf('%s=%s', text_in($spf, 'domain'), text_in($spf, 'result'));
303 }
304 }
305  
306 # A reporter that leaves policy_published out is grouped by the From
307 # domain of the record instead, so nothing falls into a shared bucket.
308 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
309  
310 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
311  
312 # Two failures worth naming as they happen, because neither shows up in
313 # the per-address totals: mail that authenticated and was filed anyway,
314 # and mail of ours that the policy filed.
315 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
316 15 ▶ my $word = ($disposition eq 'reject') ? 'rejected' : 'quarantined';
Branch: 1 branch
BranchTrueFalseTotal
$disposition eq 'reject' ? : 0 15 15
317  
318 15 my @flags;
319 15 ▶ if(!$expected{$ip}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $expected{$ip}) { } 7 8 15
elsif (not $passed) { } 0 8 8
320 7 push @flags, 'NOT ' . join('/', @expected);
321 7 $unexpected_messages += $count;
322 } elsif(!$passed) {
323 0 push @flags, 'expected host, not authenticating';
324 }
325  
326 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
327 0 push @flags, "expected sender, $word though authenticated";
328 ▶ } 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
329 0 push @flags, "authenticated, but $word";
330 ▶ } 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
331 0 push @flags, "expected sender, $word";
332 }
333  
334 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
335 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
336 'domain' => $domain, 'ip' => $ip, 'disposition' => $disposition,
337 'count' => 0, 'passed' => 0, 'expected' => 0,
338 'dkim' => {}, 'spf' => {},
339 };
340 0 $finding->{'count'} += $count;
341 0 ▶ $finding->{'passed'} = 1 if($passed);
Branch: 1 branch
BranchTrueFalseTotal
if $passed 0 0 0
342 0 ▶ $finding->{'expected'} = 1 if($expected{$ip});
Branch: 1 branch
BranchTrueFalseTotal
if $expected{$ip} 0 0 0
343 0 $finding->{'dkim'}{$dkim_verdict} = 1;
344 0 $finding->{'spf'}{$spf_verdict} = 1;
345 0 $misfiled_messages += $count;
346 }
347  
348 15 ▶ my $flag = @flags ? ' <== ' . join('; ', @flags) : '';
Branch: 1 branch
BranchTrueFalseTotal
@flags ? : 7 8 15
349  
350 15 printf " %-16s %5d dkim=%-5s spf=%-8s %-9s from=%s%s\n",
351 $ip, $count, $dkim_verdict, $spf_verdict, $disposition, $header_from, $flag;
352 15 ▶ printf " dkim seen: %s\n", (@dkim_auth ? join(', ', @dkim_auth) : 'none');
Branch: 1 branch
BranchTrueFalseTotal
@dkim_auth ? : 15 0 15
353 15 ▶ printf " spf seen: %s\n", (@spf_auth ? join(', ', @spf_auth) : 'none');
Branch: 1 branch
BranchTrueFalseTotal
@spf_auth ? : 15 0 15
354  
355 15 $total_records++;
356 15 $total_messages += $count;
357  
358 my $entry = $by_domain{$domain}{$ip} ||= {
359 'count' => 0, 'pass' => 0, 'fail' => 0,
360 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
361 'dkim_seen' => {},
362 };
363 15 $entry->{'count'} += $count;
364 15 ▶ $entry->{'pass'} += $count if($passed);
Branch: 1 branch
BranchTrueFalseTotal
if $passed 15 0 15
365 15 ▶ $entry->{'fail'} += $count if(!$passed);
Branch: 1 branch
BranchTrueFalseTotal
unless $passed 0 15 15
366 15 $entry->{'dkim_seen'}{$_} = 1 foreach(@dkim_auth);
367 }
368 }
369  
370 # Named before the summary is printed, so a caller that moves the files aside
371 # knows exactly which ones were summarised - and which were merely in the way.
372 16 ▶if(defined($file_list)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_list) 13 3 16
373 13 ▶ 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" 13 0 13
374 13 print $fh map { "$_\n" } @read_files;
375 13 ▶ close($fh) or die "Cannot close $file_list: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${file_list}: $!\n" 13 0 13
376 }
377  
378 # Nothing read is not a clean result. The wording of the first case is what
379 # dmarc-weekly.sh takes for a quiet week, so the second must not use it.
380 16 ▶if(!$reports_read) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reports_read) 2 14 16
381 2 ▶ print STDERR $unreadable
Branch: 1 branch
BranchTrueFalseTotal
$unreadable ? : 1 1 2
382 ? "$unreadable file(s) could not be read, and no report was, in: @dirs\n"
383 : "No reports found in: @dirs\n";
384 2 exit(1);
385 }
386  
387 14 print '=' x 78, "\n";
388 14 printf "Totals over %d report(s): %d record(s), %d message(s)\n",
389 $reports_read, $total_records, $total_messages;
390 14 foreach my $domain (sort keys %by_domain) {
391 14 my $by_ip = $by_domain{$domain};
392  
393 14 my ($messages, $unexpected) = (0, 0);
394 14 foreach my $ip (keys %{$by_ip}) {
395 15 $messages += $by_ip->{$ip}{'count'};
396 15 ▶ $unexpected += $by_ip->{$ip}{'count'} if($by_ip->{$ip}{'unexpected'});
Branch: 1 branch
BranchTrueFalseTotal
if $by_ip->{$ip}{"unexpected"} 7 8 15
397 }
398  
399 14 printf "\n%s: %d message(s)", $domain, $messages;
400 14 ▶ printf ", %d not from %s", $unexpected, join('/', @expected) if($unexpected);
Branch: 1 branch
BranchTrueFalseTotal
if $unexpected 7 7 14
401 14 print "\n";
402  
403 14 printf " %-16s %8s %8s %8s %s\n", 'source_ip', 'messages', 'passed', 'failed', 'status';
404 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
405 15 my $entry = $by_ip->{$ip};
406 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
407 15 printf " %-16s %8d %8d %8d %s\n", $ip, $entry->{'count'}, $entry->{'pass'}, $entry->{'fail'}, $status;
408 15 my @dkim = sort keys %{$entry->{'dkim_seen'}};
409 15 ▶ printf " %-16s dkim: %s\n", '', (@dkim ? join(', ', @dkim) : 'no signature seen');
Branch: 1 branch
BranchTrueFalseTotal
@dkim ? : 15 0 15
410 }
411 }
412  
413 # Named after the totals, so the figures that explain them come first.
414 14 ▶if(%misfiled) {
Branch: 1 branch
BranchTrueFalseTotal
if (%misfiled) 0 14 14
415 0 print '=' x 78, "\n";
416 0 printf "Filed by policy although it should not have been: %d message(s)\n", $misfiled_messages;
417 0 printf " %-22s %-16s %-11s %8s %s\n", 'domain', 'source_ip', 'disposition', 'messages', 'why';
418 0 foreach my $finding (sort {
419 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
420 } values %misfiled) {
421 my $reason = $finding->{'expected'}
422 0 ▶ ? ($finding->{'passed'} ? 'expected sender, and authenticated' : 'expected sender')
Branch: 2 branches
BranchTrueFalseTotal
$finding->{'expected'} ? : 0 0 0
$finding->{'passed'} ? : 0 0 0
423 : 'authenticated';
424 0 ▶ $reason .= ($finding->{'disposition'} eq 'reject') ? ', yet rejected' : ', yet quarantined';
Branch: 1 branch
BranchTrueFalseTotal
$finding->{'disposition'} eq 'reject' ? : 0 0 0
425 printf " %-22s %-16s %-11s %8d %s (dkim=%s spf=%s)\n",
426 $finding->{'domain'}, $finding->{'ip'}, $finding->{'disposition'},
427 $finding->{'count'}, $reason,
428 0 join('/', sort keys %{$finding->{'dkim'}}), join('/', sort keys %{$finding->{'spf'}});
429 }
430 }
431  
432 14 print "\n";
433 14 ▶if(%misfiled) {
Branch: 1 branch
BranchTrueFalseTotal
if (%misfiled) 0 14 14
434 0 printf "%d message(s) were quarantined or rejected although they should not have been.\n", $misfiled_messages;
435 }
436 14 ▶if($unexpected_messages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unexpected_messages) 7 7 14
437 7 printf "%d message(s) came from addresses that are not %s.\n", $unexpected_messages, join('/', @expected);
438 7 exit(1);
439 }
440 7 ▶exit(1) if(%misfiled);
Branch: 1 branch
BranchTrueFalseTotal
if %misfiled 0 7 7
441 7 print "Every message came from ", join('/', @expected), ", and none was filed by policy.\n";
442 7 exit(0);
443  
444 __END__
« lib/Mail/Send/DKIM.pm ↑ Summary bin/dmarc-check-mail.pl »