lib/Mail/DMARC/Report/Store/SQL.pm

filter: Statementstmt 95.4% 572 / 599 Branchbran 68.9% 193 / 280 Conditioncond 59.3% 76 / 128 MC/DCmcdc 33.3% 25 / 75 Subroutinesub 98.5% 70 / 71 Podpod 0.0% 0 / 44 totaltotal 78.2% 936 / 1197 CC 235 SCAR 23.4
CC
235
Cov
84%
get_rr
30.0
populate_agg_records
24.5
get_report
22.7
CRAP 10.3
« blib/lib/Mail/DMARC/ConfigData.pm ↑ Summary lib/Mail/DMARC/Report/URI.pm »
1 package Mail::DMARC::Report::Store::SQL;
2 6 our $VERSION = '2.20261110';
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶no warnings 'experimental::try'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use DBIx::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use Mail::DMARC::Report::Store::SQL::Grammars::MySQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Mail::DMARC::Report::Store::SQL::Grammars::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Mail::DMARC::Report::Store::SQL::Grammars::PostgreSQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 require Mail::DMARC::Result::Reason;
18  
19 6 ▶use parent 'Mail::DMARC::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use Mail::DMARC::Report::Aggregate;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use Mail::DMARC::Report::Aggregate::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use Mail::DMARC::Policy;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 6 my @AGG_FIELDS = qw/ messages aligned_both aligned_dkim aligned_spf aligned_none
25 disp_none disp_quarantine disp_reject /;
26  
27 my %FORWARDING_REASON
28 6 = map { $_ => 1 } qw/ forwarded mailing_list trusted_forwarder /;
29  
30 # A source is called aligned while its failures stay under this share of its
31 # own volume. Requiring exactly zero would label almost every real source as
32 # failing: retries, odd forwarders and low volume abuse of your domain put a
33 # few failures behind nearly everyone, and a bucket that catches everything
34 # sorts nothing.
35 6 my $FAILING_TOLERANCE = 0.02;
36  
37 # Forwarding rarely accounts for every failure at a source that also has other
38 # problems, so the reasons only have to explain the bulk of them.
39 6 my $FORWARDING_SHARE = 0.5;
40  
41 # ARC exists to carry authentication across a forwarding hop, so a receiver
42 # that overrode its policy on a passing ARC chain is telling you the mail was
43 # legitimately forwarded. The aggregate format has no field for that, so
44 # receivers report it as local_policy with the result in free text.
45 6 my $ARC_PASS = qr/\barc = pass\b/xi;
46  
47 12 ▶sub _explains_forwarding($reason) {
Subroutine _explains_forwarding: called CC=7 SCAR=21.1
48 12 ▶ return 1 if $FORWARDING_REASON{ $reason->{type} // '' };
Branch: 1 branch
BranchTrueFalseTotal
if $FORWARDING_REASON{$reason->{"type"} // ""} 8 4 12
Condition50%1/2
$reason->{"type"} // ""
l!l
120
MC/DC0%0/1
$reason->{"type"} // ""
$reason->{"type"}
Truth table50%1/2
A
A= $reason->{"type"}
Aresult
11
00
49 return 1
50 if 'local_policy' eq ( $reason->{type} // '' )
51 && defined $reason->{comment}
52 4 ▶ && $reason->{comment} =~ $ARC_PASS;
Branch: 1 branch
BranchTrueFalseTotal
if "local_policy" eq ($reason->{"type"} // "") and defined $reason->{"comment"} and $reason->{"comment"} =~ /$ARC_PASS/ 2 2 4
Condition50%4/8
"local_policy" eq ($reason->{"type"} // "") and defined $reason->{"comment"} and $reason->{"comment"} =~ /$ARC_PASS/
!ll&&!rl&&r
022
"local_policy" eq ($reason->{"type"} // "") and defined $reason->{"comment"}
!ll&&!rl&&r
004
$reason->{"type"} // ""
l!l
40
MC/DC33%1/3
"local_policy" eq ($reason->{"type"} // "") and defined $reason->{"comment"} and $reason->{"comment"} =~ /$ARC_PASS/
"local_policy" eq ($reason->{"type"} // "") defined $reason->{"comment"} $reason->{"comment"} =~ /$ARC_PASS/
MC/DC0%0/1
$reason->{"type"} // ""
$reason->{"type"}
Truth table50%2/4
A and B and C
A= "local_policy" eq ($reason->{"type"} // "")B= defined $reason->{"comment"}C= $reason->{"comment"} =~ /$ARC_PASS/
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $reason->{"type"}
Aresult
11
00
53 2 return 0;
54 }
55  
56 12 ▶sub save_aggregate( $self, $agg ) {
Subroutine save_aggregate: called CC=7 SCAR=21.2
Pod undocumented
57  
58 12 $self->db_connect();
59  
60 12 ▶ croak "policy_published must be a Mail::DMARC::Policy object"
Branch: 1 branch
BranchTrueFalseTotal
if "Mail::DMARC::Policy" ne ref $agg->policy_published 0 12 12
61 if 'Mail::DMARC::Policy' ne ref $agg->policy_published;
62  
63 #warn Dumper($meta); ## no critic (Carp)
64 12 foreach my $f (qw/ org_name email begin end /) {
65 48 ▶ croak "meta field $f required" if !$agg->metadata->$f;
Branch: 1 branch
BranchTrueFalseTotal
unless $agg->metadata->$f 0 48 48
66 }
67  
68 12 ▶ my $rid = $self->get_report_id($agg)
Branch: 1 branch
BranchTrueFalseTotal
my $rid = $self->get_report_id($agg) or croak("failed to create report!") 12 0 12
69 or croak "failed to create report!";
70  
71 # on 6/8/2013, Microsoft spat out a bunch of reports with zero records.
72 12 ▶ if ( !$agg->record ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($agg->record) 0 12 12
73 0 warn "\ta report with ZERO records! Illegal.\n"; ## no critic (Carp)
74 0 return $rid;
75 }
76  
77 12 foreach my $rec ( @{ $agg->record } ) {
78 12 $self->insert_agg_record( $rid, $rec );
79 }
80  
81 12 return $rid;
82 }
83  
84 13 ▶sub retrieve( $self, @args ) {
Subroutine retrieve: called CC=16 SCAR=27.7
Pod undocumented
85 13 my %args = @args;
86  
87 13 my $query = $self->grammar->select_report_query;
88 13 my @params;
89  
90 13 ▶ if ( $args{rid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"rid"}) 1 12 13
91 1 $query .= $self->grammar->and_arg('r.id');
92 1 push @params, $args{rid};
93 }
94 13 ▶ if ( $args{begin} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"begin"}) 1 12 13
95 1 $query .= $self->grammar->and_arg( 'r.begin', '>=' );
96 1 push @params, $args{begin};
97 }
98 13 ▶ if ( $args{end} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"end"}) 1 12 13
99 1 $query .= $self->grammar->and_arg( 'r.end', '<=' );
100 1 push @params, $args{end};
101 }
102 13 ▶ if ( $args{author} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"author"}) 3 10 13
103 3 my ( $op, $val ) = $self->_negate_arg( $args{author} );
104 3 $query .= $self->grammar->and_arg( 'a.org_name', $op );
105 3 push @params, $val;
106 }
107 13 ▶ if ( $args{from_domain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"from_domain"}) 2 11 13
108 2 my ( $op, $val ) = $self->_negate_arg( $args{from_domain} );
109 2 $query .= $self->grammar->and_arg( 'fd.domain', $op );
110 2 push @params, $val;
111 }
112  
113 13 ▶ my $sort_by = lc( $args{sort_by} || 'rid' );
Condition100%2/2
$args{'sort_by'} || 'rid'
l!l
211
MC/DC100%1/1
$args{'sort_by'} || 'rid'
$args{'sort_by'}
Truth table100%2/2
A
A= $args{'sort_by'}
Aresult
11
00
114 13 ▶ $sort_by = 'rid' if $sort_by eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
if $sort_by eq "id" 0 13 13
115  
116 13 my %allowed_sort = map { $_ => 1 } qw( rid author from_domain begin end );
117 13 ▶ $sort_by = 'rid' if !$allowed_sort{$sort_by};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed_sort{$sort_by} 1 12 13
118  
119 13 ▶ my $sort_order = uc( $args{sort_order} || 'DESC' );
Condition100%2/2
$args{'sort_order'} || 'DESC'
l!l
211
MC/DC100%1/1
$args{'sort_order'} || 'DESC'
$args{'sort_order'}
Truth table100%2/2
A
A= $args{'sort_order'}
Aresult
11
00
120 13 ▶ $sort_order = 'DESC' if $sort_order ne 'ASC' && $sort_order ne 'DESC';
Branch: 1 branch
BranchTrueFalseTotal
if $sort_order ne "ASC" and $sort_order ne "DESC" 1 12 13
Condition100%3/3
$sort_order ne "ASC" and $sort_order ne "DESC"
!ll&&!rl&&r
1111
MC/DC100%2/2
$sort_order ne "ASC" and $sort_order ne "DESC"
$sort_order ne "ASC" $sort_order ne "DESC"
Truth table100%3/3
A and B
A= $sort_order ne "ASC"B= $sort_order ne "DESC"
ABresult
0X0
100
111
121  
122 13 $query .= $self->grammar->order_by( $sort_by, $sort_order );
123  
124 13 ▶ if ( defined $args{limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"limit"}) 4 9 13
125 croak "limit must be a positive integer"
126 4 ▶ if $args{limit} !~ /^\d+$/ || $args{limit} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $args{"limit"} =~ /^\d+$/ or $args{"limit"} < 1 2 2 4
Condition100%3/3
not $args{"limit"} =~ /^\d+$/ or $args{"limit"} < 1
l!l&&r!l&&!r
112
MC/DC100%2/2
not $args{"limit"} =~ /^\d+$/ or $args{"limit"} < 1
not $args{"limit"} =~ /^\d+$/ $args{"limit"} < 1
Truth table100%3/3
not(A) or B
A= not $args{"limit"} =~ /^\d+$/B= $args{"limit"} < 1
ABresult
1X1
011
000
127 2 $query .= $self->grammar->limit( $args{limit} );
128 }
129  
130 11 my $reports = $self->query( $query, \@params );
131  
132 11 foreach (@$reports) {
133 10 $_->{begin} = join( " ", split( /T/, $self->epoch_to_iso( $_->{begin} ) ) );
134 10 $_->{end} = join( " ", split( /T/, $self->epoch_to_iso( $_->{end} ) ) );
135 }
136 11 return $reports;
137 }
138  
139 5 ▶sub _negate_arg( $self, $val ) {
Subroutine _negate_arg: called CC=2 SCAR=6.9
140 5 ▶ if ( substr( $val, 0, 1 ) eq '!' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($val, 0, 1) eq "!") 3 2 5
141 3 return ( '!=', substr( $val, 1 ) );
142 }
143 2 return ( '=', $val );
144 }
145  
146 23 ▶sub next_todo($self) {
Subroutine next_todo: called CC=4 SCAR=13.9
Pod undocumented
147  
148 23 ▶ if ( !exists $self->{_todo_list} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_todo_list"}) 12 11 23
149 $self->{_todo_list}
150 12 = $self->query( $self->grammar->select_todo_query, [ $self->time ] );
151 12 ▶ return if !$self->{_todo_list};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_todo_list"} 0 12 12
152 }
153  
154 23 my $next_todo = shift @{ $self->{_todo_list} };
155 23 ▶ if ( !$next_todo ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($next_todo) 12 11 23
156 12 delete $self->{_todo_list};
157 12 return;
158 }
159  
160 11 my $agg = Mail::DMARC::Report::Aggregate->new();
161 11 $self->populate_agg_metadata( \$agg, \$next_todo );
162  
163 11 my $pp = $self->get_report_policy_published( $next_todo->{rid} );
164 11 $pp->{domain} = $next_todo->{from_domain};
165 11 $agg->policy_published( Mail::DMARC::Policy->new(%$pp) );
166  
167 11 $self->populate_agg_records( \$agg, $next_todo->{rid} );
168 11 return $agg;
169 }
170  
171 2 ▶sub retrieve_todo( $self, @args ) {
Subroutine retrieve_todo: called CC=3 SCAR=11.0
Pod undocumented
172  
173 # this method extracts the data from the SQL tables and populates a
174 # list of Aggregate report objects with them.
175 2 my $reports
176 = $self->query( $self->grammar->select_todo_query, [ $self->time ] );
177 2 my @reports_todo;
178 2 ▶ return \@reports_todo if !@$reports;
Branch: 1 branch
BranchTrueFalseTotal
unless @$reports 0 2 2
179  
180 2 foreach my $report ( @{$reports} ) {
181  
182 2 my $agg = Mail::DMARC::Report::Aggregate->new();
183 2 $self->populate_agg_metadata( \$agg, \$report );
184  
185 2 my $pp = $self->get_report_policy_published( $report->{rid} );
186 2 $pp->{domain} = $report->{from_domain};
187 2 $agg->policy_published( Mail::DMARC::Policy->new(%$pp) );
188  
189 2 $self->populate_agg_records( \$agg, $report->{rid} );
190 2 push @reports_todo, $agg;
191 }
192 2 return \@reports_todo;
193 }
194  
195 19 ▶sub delete_report( $self, $report_id = undef ) {
Subroutine delete_report: called CC=9 SCAR=22.3
Pod undocumented
196 19 ▶ $report_id or croak "missing report ID";
Branch: 1 branch
BranchTrueFalseTotal
$report_id or croak("missing report ID") 19 0 19
197 19 ▶ print "deleting report $report_id\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 19 19
198  
199 # deletes with FK don't cascade in SQLite? Clean each table manually
200 19 my $rows = $self->query( $self->grammar->report_record_id, [$report_id] );
201 19 my @row_ids = map { $_->{id} } @$rows;
202  
203 19 ▶ if (@row_ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (@row_ids) 15 4 19
204 15 foreach my $table (
205 qw/ report_record_spf report_record_dkim report_record_reason /)
206 {
207 45 ▶ print "deleting $table rows " . join( ',', @row_ids ) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 45 45
208 if $self->verbose;
209 45 eval {
210 45 $self->query( $self->grammar->delete_from_where_record_in($table),
211 \@row_ids );
212 };
213  
214 # warn $@ if $@;
215 }
216 }
217 19 foreach my $table (qw/ report_policy_published report_record report_error /) {
218 57 ▶ print "deleting $table rows for report $report_id\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 57 57
219 57 eval {
220 57 $self->query( $self->grammar->delete_from_where_report($table),
221 [$report_id] );
222 };
223  
224 # warn $@ if $@;
225 }
226  
227 # In MySQL, where FK constraints DO cascade, this is the only query needed
228 19 $self->query( $self->grammar->delete_report, [$report_id] );
229 19 return 1;
230 }
231  
232 190 ▶sub get_domain_id( $self, $domain ) {
Subroutine get_domain_id: called CC=4 SCAR=14.0
Pod undocumented
233 190 ▶ croak "missing domain calling " . ( caller(0) )[3] if !$domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain 0 190 190
234 190 my $r = $self->query( $self->grammar->select_domain_id, [$domain] );
235 190 ▶ if ( $r && @$r ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r and @$r) 147 43 190
Condition66%2/3
$r and @$r
!ll&&!rl&&r
043147
MC/DC50%1/2
$r and @$r
$r @$r
Truth table66%2/3
A and B
A= $rB= @$r
ABresult
0X0
100
111
236 147 return $r->[0]{id};
237 }
238 43 return $self->query( $self->grammar->insert_domain, [$domain] );
239 }
240  
241 23 ▶sub get_author_id( $self, $meta ) {
Subroutine get_author_id: called CC=5 SCAR=16.5
Pod undocumented
242 23 ▶ croak "missing author name" if !$meta->org_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->org_name 0 23 23
243 23 my $r = $self->query( $self->grammar->select_author_id, [ $meta->org_name ] );
244 23 ▶ if ( $r && @$r ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r and @$r) 3 20 23
Condition66%2/3
$r and @$r
!ll&&!rl&&r
0203
MC/DC50%1/2
$r and @$r
$r @$r
Truth table66%2/3
A and B
A= $rB= @$r
ABresult
0X0
100
111
245 3 return $r->[0]{id};
246 }
247 20 ▶ carp "missing email" if !$meta->email;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->email 0 20 20
248 20 return $self->query( $self->grammar->insert_author,
249 [ $meta->org_name, $meta->email, $meta->extra_contact_info ] );
250 }
251  
252 20 ▶sub get_report_id( $self, $aggr ) {
Subroutine get_report_id: called CC=6 SCAR=19.5
Pod undocumented
253  
254 20 my $meta = $aggr->metadata;
255 20 my $pol = $aggr->policy_published;
256  
257 # check if report exists
258 20 ▶ my $author_id = $self->get_author_id($meta) or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $author_id = $self->get_author_id($meta) or croak() 20 0 20
259 20 ▶ my $from_dom_id = $self->get_domain_id( $pol->domain ) or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $from_dom_id = $self->get_domain_id($pol->domain) or croak() 20 0 20
260  
261 20 my $ids;
262 20 ▶ if ( $meta->report_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->report_id) { } 0 20 20
263  
264 # reports arriving via the wire will have an author ID & report ID
265 0 $ids = $self->query( $self->grammar->select_report_id,
266 [ $meta->report_id, $author_id ] );
267 }
268 else {
269 # Reports submitted by our local MTA will not have a report ID
270 # They aggregate on the From domain, where the DMARC policy was discovered
271 20 $ids = $self->query(
272 $self->grammar->select_id_with_end,
273 [ $from_dom_id, $self->time, $author_id ]
274 );
275 }
276  
277 20 ▶ if (@$ids) { # report already exists
Branch: 1 branch
BranchTrueFalseTotal
if (@$ids) 0 20 20
278 0 return $self->{report_id} = $ids->[0]{id};
279 }
280  
281 20 ▶ my $rid = $self->{report_id} = $self->query( $self->grammar->insert_report,
Branch: 1 branch
BranchTrueFalseTotal
my $rid = $self->{"report_id"} = $self->query($self->grammar->insert_report, [$from_dom_id, $meta->begin, $meta->end, $author_id, $meta->uuid]) or return 20 0 20
282 [ $from_dom_id, $meta->begin, $meta->end, $author_id, $meta->uuid ] )
283 or return;
284  
285 20 $self->insert_policy_published( $rid, $pol );
286 20 return $rid;
287 }
288  
289 14 ▶sub get_report( $self, @args ) {
Subroutine get_report: called CC=20 SCAR=31.2
Pod undocumented
290 14 ▶ croak "invalid parameters" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 14 14
291 14 my %args = @args;
292  
293 14 my @known = qw/ r.id a.org_name fd.domain r.begin r.end /;
294 14 my %known = map { $_ => 1 } @known;
295  
296 14 my $where = '';
297 14 my @where_params;
298  
299 # Optional reporting window, so a caller can scope the list the same way
300 # the aggregate views are scoped. Compared against begin, matching how
301 # those views attribute a report to a day.
302 14 for my $pair ( [ since => 'r.begin', '>=' ], [ until => 'r.begin', '<=' ] ) {
303 28 my ( $param, $column, $op ) = @$pair;
304 28 ▶ next if !defined $args{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$param} 19 9 28
305 9 ▶ next if $args{$param} !~ /\A-?[0-9]+\z/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{$param} =~ /\A-?[0-9]+\z/x 1 8 9
306 8 $where .= $self->grammar->and_arg( $column, $op );
307 8 push @where_params, $args{$param};
308 }
309  
310 my ( $origin, $origin_params )
311 14 ▶ = $self->_origin_filter( $args{reports} // 'all' );
Condition100%2/2
$args{"reports"} // "all"
l!l
113
MC/DC100%1/1
$args{"reports"} // "all"
$args{"reports"}
Truth table100%2/2
A
A= $args{"reports"}
Aresult
11
00
312 14 $where .= $origin;
313 14 push @where_params, @$origin_params;
314  
315 # Per-column LIKE searches
316 14 for my $pair ( [ search_domain => 'fd.domain' ],
317 [ search_author => 'a.org_name' ] )
318 {
319 28 my ( $param, $col_name ) = @$pair;
320 28 ▶ next unless $args{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{$param} 21 7 28
321 7 my $safe = $args{$param};
322 7 $safe =~ s/([%_!])/!$1/g; # escape LIKE metacharacters using !
323 7 $where .= " AND $col_name LIKE ? ESCAPE '!'";
324 7 push @where_params, '%' . $safe . '%';
325 }
326  
327 14 foreach my $known (@known) {
328 70 ▶ next if !defined $args{$known};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$known} 70 0 70
329 0 $where .= $self->grammar->and_arg($known);
330 0 push @where_params, $args{$known};
331 }
332  
333 14 my $total_recs = $self->dbix->query( $self->grammar->count_reports )->list;
334 14 my $filtered_recs = $total_recs;
335 14 ▶ if ($where) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where) 7 7 14
336 7 $filtered_recs
337 = $self->dbix->query(
338 $self->grammar->count_filtered_report_query . $where,
339 @where_params )->list;
340 }
341  
342 14 my $order = '';
343 14 ▶ if ( $args{sort_col} && $known{ $args{sort_col} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"sort_col"} and $known{$args{"sort_col"}}) 0 14 14
Condition33%1/3
$args{"sort_col"} and $known{$args{"sort_col"}}
!ll&&!rl&&r
1400
MC/DC0%0/2
$args{"sort_col"} and $known{$args{"sort_col"}}
$args{"sort_col"} $known{$args{"sort_col"}}
Truth table33%1/3
A and B
A= $args{"sort_col"}B= $known{$args{"sort_col"}}
ABresult
0X0
100
111
344 0 ▶ if ( $args{sort_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"sort_dir"}) 0 0 0
345 $order = $self->grammar->order_by( $args{sort_col},
346 0 ▶ $args{sort_dir} eq 'desc' ? ' DESC' : ' ASC' );
Branch: 1 branch
BranchTrueFalseTotal
$args{'sort_dir'} eq 'desc' ? : 0 0 0
347 }
348 }
349  
350 14 my $query = $self->grammar->select_report_query . $where . $order;
351 14 my @params = @where_params;
352 14 ▶ if ( $args{length} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"length"}) 1 13 14
353 1 ▶ my $start = $args{start} || 0;
Condition50%1/2
$args{'start'} || 0
l!l
01
MC/DC0%0/1
$args{'start'} || 0
$args{'start'}
Truth table50%1/2
A
A= $args{'start'}
Aresult
11
00
354 1 ▶ $start = 0 if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 0 1 1
355 1 $query .= $self->grammar->limit_args(2);
356 1 push @params, $start, $args{length};
357 }
358  
359 # warn "query: $query\n" . join(", ", @params) . "\n";
360 14 my $reports = $self->query( $query, \@params );
361 14 foreach (@$reports) {
362 22 $_->{begin} = $self->epoch_to_iso( $_->{begin} );
363 22 $_->{end} = $self->epoch_to_iso( $_->{end} );
364 }
365  
366 14 ▶ $self->attach_report_summaries($reports) if $args{summary};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"summary"} 1 13 14
367  
368 return {
369 14 recordsTotal => $total_recs,
370 recordsFiltered => $filtered_recs,
371 data => $reports,
372 };
373 }
374  
375 # What each report contains, so a viewer has less reason to expand every row.
376 # Opt in from get_report: the callers that only want the report envelope should
377 # not pay for a second query.
378 1 ▶sub attach_report_summaries( $self, $reports ) {
Subroutine attach_report_summaries: called CC=12 SCAR=26.4
Pod undocumented
379 1 my @rids = map { $_->{rid} } @$reports;
380 1 ▶ return if !@rids;
Branch: 1 branch
BranchTrueFalseTotal
unless @rids 0 1 1
381  
382 1 my $rows = $self->query( $self->grammar->select_report_summary_query, \@rids );
383  
384 1 my %by_rid;
385 1 foreach my $row (@$rows) {
386 my $summary = $by_rid{ $row->{rid} } ||= {
387 1 ▶ ( map { $_ => 0 } @AGG_FIELDS ),
Condition50%1/2
$by_rid{$row->{"rid"}} ||= {map({$_, 0;} @AGG_FIELDS), "header_from", {}, "envelope_to", {}}
l!l
01
MC/DC0%0/1
$by_rid{$row->{"rid"}} ||= {map({$_, 0;} @AGG_FIELDS), "header_from", {}, "envelope_to", {}}
$by_rid{$row->{"rid"}}
Truth table50%1/2
A
A= $by_rid{$row->{"rid"}}
Aresult
11
00
388 header_from => {},
389 envelope_to => {},
390 };
391 1 ▶ $summary->{$_} += $row->{$_} || 0 foreach @AGG_FIELDS;
Condition100%2/2
$row->{$_} || 0
l!l
71
MC/DC100%1/1
$row->{$_} || 0
$row->{$_}
Truth table100%2/2
A
A= $row->{$_}
Aresult
11
00
392  
393 # A report names one domain per record; the set is usually a single
394 # entry, occasionally a handful.
395 1 foreach my $field (qw/ header_from envelope_to /) {
396 2 ▶ next if !defined $row->{$field} || '' eq $row->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $row->{$field} or "" eq $row->{$field} 0 2 2
Condition33%1/3
not defined $row->{$field} or "" eq $row->{$field}
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $row->{$field} or "" eq $row->{$field}
not defined $row->{$field} "" eq $row->{$field}
Truth table33%1/3
not(A) or B
A= not defined $row->{$field}B= "" eq $row->{$field}
ABresult
1X1
011
000
397 2 $summary->{$field}{ $row->{$field} } = 1;
398 }
399 }
400  
401 1 foreach my $report (@$reports) {
402 1 my $found = $by_rid{ $report->{rid} };
403 1 ▶ if ( !$found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 1 1
404 $report->{summary}
405 0 = { ( map { $_ => 0 } @AGG_FIELDS ),
406 header_from => [], envelope_to => [] };
407 0 next;
408 }
409 1 my %summary = map { $_ => $found->{$_} + 0 } @AGG_FIELDS;
410 2 $summary{$_} = [ sort keys %{ $found->{$_} } ]
411 1 foreach qw/ header_from envelope_to /;
412 1 $report->{summary} = \%summary;
413 }
414 1 return;
415 }
416  
417 15 ▶sub get_report_policy_published( $self, $rid ) {
Subroutine get_report_policy_published: called CC=2 SCAR=7.0
Pod undocumented
418 15 my $pp = $self->query( $self->grammar->select_report_policy_published, [$rid] )
419 ->[0];
420 15 ▶ $pp->{p} ||= 'none';
Condition50%1/2
$pp->{"p"} ||= "none"
l!l
150
MC/DC0%0/1
$pp->{"p"} ||= "none"
$pp->{"p"}
Truth table50%1/2
A
A= $pp->{"p"}
Aresult
11
00
421 15 $pp->{t} = delete $pp->{testing};
422 15 delete @$pp{ grep { !defined $pp->{$_} } keys %$pp };
423 15 $pp = Mail::DMARC::Policy->new( v => 'DMARC1', %$pp );
424 15 return $pp;
425 }
426  
427 0 ▶sub get_rr( $self, @args ) {
Subroutine get_rr: not called CC=5 SCAR=34.0
Pod undocumented
428 0 ▶ croak "invalid parameters" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 0 0
429 0 my %args = @args;
430  
431 # warn Dumper(\%args);
432 0 ▶ croak "missing report ID (rid)!" if !defined $args{rid};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"rid"} 0 0 0
433  
434 0 my $rows = $self->query( $self->grammar->select_rr_query, [ $args{rid} ] );
435 0 foreach (@$rows) {
436 $_->{source_ip} = $self->any_inet_ntop( $_->{source_ip} )
437 0 ▶ if $self->grammar->language ne 'postgresql';
Branch: 1 branch
BranchTrueFalseTotal
if $self->grammar->language ne "postgresql" 0 0 0
438 $_->{reasons}
439 0 = $self->query( $self->grammar->select_report_reason, [ $_->{id} ] );
440 }
441 0 return { data => $rows, };
442 }
443  
444 14 ▶sub populate_agg_metadata( $self, $agg_ref, $report_ref ) {
Subroutine populate_agg_metadata: called CC=3 SCAR=11.0
Pod undocumented
445  
446 14 $$agg_ref->metadata->report_id( $$report_ref->{rid} );
447  
448 14 foreach my $f (qw/ org_name email extra_contact_info /) {
449 42 $$agg_ref->metadata->$f( $self->config->{organization}{$f} );
450 }
451 14 foreach my $f (qw/ begin end /) {
452 28 $$agg_ref->metadata->$f( $$report_ref->{$f} );
453 }
454  
455 # report_error logs our delivery failures, not report content (RFC 9990 3.1.5)
456 14 return 1;
457 }
458  
459 14 ▶sub populate_agg_records( $self, $agg_ref, $rid ) {
Subroutine populate_agg_records: called CC=16 SCAR=32.0
Pod undocumented
460  
461 14 my $recs = $self->query( $self->grammar->select_rr_query, [$rid] );
462  
463 # aggregate the connections per IP-Disposition-DKIM-SPF uniqueness
464 14 my ( %ips, %uniq, %pe, %auth, %ident, %reasons, %other );
465 14 foreach my $rec (@$recs) {
466 14 my $ip = $rec->{source_ip};
467 $ip = $self->any_inet_ntop( $rec->{source_ip} )
468 14 ▶ if $self->grammar->language ne 'postgresql';
Branch: 1 branch
BranchTrueFalseTotal
if $self->grammar->language ne "postgresql" 14 0 14
469 14 my $key = join( '-', $ip, @$rec{qw/ disposition dkim spf /} ); # hash slice
470 14 $uniq{$key}++;
471 14 $ips{$key} = $rec->{source_ip};
472 14 ▶ $ident{$key}{header_from} ||= $rec->{header_from};
Condition33%1/3
$ident{$key}{"header_from"} ||= $rec->{"header_from"}
l!l&&r!l&&!r
0140
MC/DC0%0/2
$ident{$key}{"header_from"} ||= $rec->{"header_from"}
$ident{$key}{"header_from"} $rec->{"header_from"}
Truth table33%1/3
A ||= B
A= $ident{$key}{"header_from"}B= $rec->{"header_from"}
ABresult
1X1
011
000
473 14 ▶ $ident{$key}{envelope_from} ||= $rec->{envelope_from};
Condition33%1/3
$ident{$key}{"envelope_from"} ||= $rec->{"envelope_from"}
l!l&&r!l&&!r
0140
MC/DC0%0/2
$ident{$key}{"envelope_from"} ||= $rec->{"envelope_from"}
$ident{$key}{"envelope_from"} $rec->{"envelope_from"}
Truth table33%1/3
A ||= B
A= $ident{$key}{"envelope_from"}B= $rec->{"envelope_from"}
ABresult
1X1
011
000
474 14 ▶ $ident{$key}{envelope_to} ||= $rec->{envelope_to};
Condition33%1/3
$ident{$key}{"envelope_to"} ||= $rec->{"envelope_to"}
l!l&&r!l&&!r
0140
MC/DC0%0/2
$ident{$key}{"envelope_to"} ||= $rec->{"envelope_to"}
$ident{$key}{"envelope_to"} $rec->{"envelope_to"}
Truth table33%1/3
A ||= B
A= $ident{$key}{"envelope_to"}B= $rec->{"envelope_to"}
ABresult
1X1
011
000
475  
476 14 ▶ $pe{$key}{disposition} ||= $rec->{disposition};
Condition33%1/3
$pe{$key}{"disposition"} ||= $rec->{"disposition"}
l!l&&r!l&&!r
0140
MC/DC0%0/2
$pe{$key}{"disposition"} ||= $rec->{"disposition"}
$pe{$key}{"disposition"} $rec->{"disposition"}
Truth table33%1/3
A ||= B
A= $pe{$key}{"disposition"}B= $rec->{"disposition"}
ABresult
1X1
011
000
477 14 ▶ $pe{$key}{dkim} ||= $rec->{dkim};
Condition33%1/3
$pe{$key}{"dkim"} ||= $rec->{"dkim"}
l!l&&r!l&&!r
0140
MC/DC0%0/2
$pe{$key}{"dkim"} ||= $rec->{"dkim"}
$pe{$key}{"dkim"} $rec->{"dkim"}
Truth table33%1/3
A ||= B
A= $pe{$key}{"dkim"}B= $rec->{"dkim"}
ABresult
1X1
011
000
478 14 ▶ $pe{$key}{spf} ||= $rec->{spf};
Condition33%1/3
$pe{$key}{"spf"} ||= $rec->{"spf"}
l!l&&r!l&&!r
0140
MC/DC0%0/2
$pe{$key}{"spf"} ||= $rec->{"spf"}
$pe{$key}{"spf"} $rec->{"spf"}
Truth table33%1/3
A ||= B
A= $pe{$key}{"spf"}B= $rec->{"spf"}
ABresult
1X1
011
000
479  
480 14 ▶ $auth{$key}{spf} ||= $self->get_row_spf( $rec->{id} );
Condition33%1/3
$auth{$key}{"spf"} ||= $self->get_row_spf($rec->{"id"})
l!l&&r!l&&!r
0140
MC/DC0%0/2
$auth{$key}{"spf"} ||= $self->get_row_spf($rec->{"id"})
$auth{$key}{"spf"} $self->get_row_spf($rec->{"id"})
Truth table33%1/3
A ||= B
A= $auth{$key}{"spf"}B= $self->get_row_spf($rec->{"id"})
ABresult
1X1
011
000
481 14 ▶ $auth{$key}{dkim} ||= $self->get_row_dkim( $rec->{id} );
Condition33%1/3
$auth{$key}{"dkim"} ||= $self->get_row_dkim($rec->{"id"})
l!l&&r!l&&!r
0140
MC/DC0%0/2
$auth{$key}{"dkim"} ||= $self->get_row_dkim($rec->{"id"})
$auth{$key}{"dkim"} $self->get_row_dkim($rec->{"id"})
Truth table33%1/3
A ||= B
A= $auth{$key}{"dkim"}B= $self->get_row_dkim($rec->{"id"})
ABresult
1X1
011
000
482  
483 14 my $reasons = $self->get_row_reason( $rec->{id} );
484 14 foreach my $reason (@$reasons) {
485 18 ▶ my $type = $reason->{type} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $reason->{"type"} or next 18 0 18
486 18 $reasons{$key}{$type} = $reason->{comment}; # flatten reasons
487 }
488 }
489  
490 14 foreach my $u ( keys %uniq ) {
491 my $record = Mail::DMARC::Report::Aggregate::Record->new(
492 identifiers => $ident{$u},
493 auth_results => $auth{$u},
494 row => {
495 source_ip => $self->grammar->language eq 'postgresql' ? $ips{$u}
496 : $self->any_inet_ntop( $ips{$u} ),
497 count => $uniq{$u},
498 policy_evaluated => {
499 14 ▶ %{ $pe{$u} },
Branch: 1 branch
BranchTrueFalseTotal
$reasons{$u} ? : 3 11 14
500 $reasons{$u}
501 ? ( reason => [
502 18 map { { type => $_, comment => $reasons{$u}{$_} } }
503 14 ▶ sort keys %{ $reasons{$u} }
Branch: 1 branch
BranchTrueFalseTotal
$self->grammar->language eq 'postgresql' ? : 0 14 14
504 ]
505 )
506 : (),
507 },
508 }
509 );
510 14 $$agg_ref->record($record);
511 }
512 14 return $$agg_ref->record;
513 }
514  
515 12 ▶sub row_exists( $self, $rid, $rec ) {
Subroutine row_exists: called CC=4 SCAR=19.7
Pod undocumented
516  
517 12 ▶ if ( !defined $rec->{row}{count} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rec->{"row"}{"count"}) 12 0 12
518 12 ▶ print "new record\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 12 12
519 12 return;
520 }
521  
522 my $rows = $self->query( $self->grammar->select_report_record,
523 0 [ $rid, $rec->{row}{source_ip}, $rec->{row}{count}, ] );
524  
525 0 ▶ return 1 if @$rows;
Branch: 1 branch
BranchTrueFalseTotal
if @$rows 0 0 0
526 0 return;
527 }
528  
529 12 ▶sub insert_agg_record( $self, $report_id, $rec ) {
Subroutine insert_agg_record: called CC=13 SCAR=28.6
Pod undocumented
530  
531 12 ▶ return 1 if $self->row_exists( $report_id, $rec );
Branch: 1 branch
BranchTrueFalseTotal
if $self->row_exists($report_id, $rec) 0 12 12
532  
533 12 ▶ my $row_id = $self->insert_rr( $report_id, $rec )
Branch: 1 branch
BranchTrueFalseTotal
my $row_id = $self->insert_rr($report_id, $rec) or croak("failed to insert report row") 12 0 12
534 or croak "failed to insert report row";
535  
536 12 my $reasons = $rec->row->policy_evaluated->reason;
537 12 ▶ if ($reasons) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reasons) 12 0 12
538 12 foreach my $reason (@$reasons) {
539 2 ▶ next if !$reason || !$reason->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $reason and $reason->{"type"} 0 2 2
Condition33%1/3
$reason and $reason->{"type"}
!ll&&!rl&&r
002
MC/DC0%0/2
$reason and $reason->{"type"}
$reason $reason->{"type"}
Truth table33%1/3
A and B
A= $reasonB= $reason->{"type"}
ABresult
0X0
100
111
540 2 $self->insert_rr_reason( $row_id, $reason->{type}, $reason->{comment} );
541 }
542 }
543  
544 12 my $spf_ref = $rec->auth_results->spf;
545 12 ▶ if ($spf_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf_ref) 12 0 12
546 12 foreach my $spf (@$spf_ref) {
547 18 $self->insert_rr_spf( $row_id, $spf );
548 }
549 }
550  
551 12 my $dkim = $rec->auth_results->dkim;
552 12 ▶ if ($dkim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim) 12 0 12
553 12 foreach my $sig (@$dkim) {
554 12 ▶ next if !$sig || !$sig->{domain};
Branch: 1 branch
BranchTrueFalseTotal
unless $sig and $sig->{"domain"} 0 12 12
Condition33%1/3
$sig and $sig->{"domain"}
!ll&&!rl&&r
0012
MC/DC0%0/2
$sig and $sig->{"domain"}
$sig $sig->{"domain"}
Truth table33%1/3
A and B
A= $sigB= $sig->{"domain"}
ABresult
0X0
100
111
555 12 $self->insert_rr_dkim( $row_id, $sig );
556 }
557 }
558 12 return 1;
559 }
560  
561 4 ▶sub insert_error( $self, $rid, $error ) {
Subroutine insert_error: called CC=1 SCAR=0.0
Pod undocumented
562  
563 # wait >5m before trying to deliver this report again
564 4 $self->query( $self->grammar->insert_error(0),
565 [ $self->time + ( 5 * 60 ), $rid ] );
566  
567 4 return $self->query( $self->grammar->insert_error(1), [ $rid, $error ] );
568 }
569  
570 19 ▶sub insert_rr_reason( $self, $row_id, $type, $comment ) {
Subroutine insert_rr_reason: called CC=3 SCAR=11.1
Pod undocumented
571  
572 # MySQL enforces fk_disposition_reason, which other reporters and
573 # un-upgraded databases don't always match
574 19 my $known = $self->known_reason_types;
575 19 ▶ ( $type, $comment )
Branch: 1 branch
BranchTrueFalseTotal
if $known 19 0 19
576 = Mail::DMARC::Result::Reason::as_known_type( $type, $comment, $known )
577 if $known;
578 19 ▶ return $self->query( $self->grammar->insert_rr_reason,
Condition100%2/2
$comment || ''
l!l
145
MC/DC100%1/1
$comment || ''
$comment
Truth table100%2/2
A
A= $comment
Aresult
11
00
579 [ $row_id, $type, ( $comment || '' ) ] );
580 }
581  
582 20 ▶sub known_reason_types($self) {
Subroutine known_reason_types: called CC=4 SCAR=14.0
Pod undocumented
583 20 ▶ return $self->{_reason_types} if exists $self->{_reason_types};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_reason_types"} 18 2 20
584 2 my %known;
585 2 ▶ if ( $self->grammar->language ne 'postgresql' ) { # it has no fk_disposition_reason
Branch: 1 branch
BranchTrueFalseTotal
if ($self->grammar->language ne "postgresql") 2 0 2
586 2 try {
587 2 %known = map { $_->{type} => 1 }
588 $self->db_connect->query('SELECT type FROM fk_disposition_reason')->hashes;
589 }
590 catch ($e) { };
591 }
592 2 ▶ return $self->{_reason_types} = %known ? \%known : undef;
Branch: 1 branch
BranchTrueFalseTotal
%known ? : 2 0 2
593 }
594  
595 28 ▶sub insert_rr_dkim( $self, $row_id, $dkim ) {
Subroutine insert_rr_dkim: called CC=4 SCAR=13.9
Pod undocumented
596 28 my ( @fields, @values );
597 28 foreach (qw/ domain selector result human_result /) {
598 112 ▶ next if !defined $dkim->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dkim->{$_} 15 97 112
599 97 ▶ if ( 'domain' eq $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("domain" eq $_) 28 69 97
600 28 push @fields, 'domain_id';
601 28 push @values, $self->get_domain_id( $dkim->{domain} );
602 28 next;
603 }
604 69 push @fields, $_;
605 69 push @values, $dkim->{$_};
606 }
607 28 my $query = $self->grammar->insert_rr_dkim( \@fields );
608 28 $self->query( $query, [ $row_id, @values ] );
609 28 return 1;
610 }
611  
612 34 ▶sub insert_rr_spf( $self, $row_id, $spf ) {
Subroutine insert_rr_spf: called CC=4 SCAR=13.9
Pod undocumented
613 34 my ( @fields, @values );
614 34 for (qw/ domain scope result /) {
615 102 ▶ next if !defined $spf->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spf->{$_} 0 102 102
616 102 ▶ if ( 'domain' eq $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("domain" eq $_) 34 68 102
617 34 push @fields, 'domain_id';
618 34 push @values, $self->get_domain_id( $spf->{domain} );
619 34 next;
620 }
621 68 push @fields, $_;
622 68 push @values, $spf->{$_};
623 }
624 34 my $query = $self->grammar->insert_rr_spf( \@fields );
625 34 $self->query( $query, [ $row_id, @values ] );
626 34 return 1;
627 }
628  
629 36 ▶sub insert_rr( $self, $report_id, $rec ) {
Subroutine insert_rr: called CC=6 SCAR=18.5
Pod undocumented
630 36 ▶ $report_id or croak "report ID required?!";
Branch: 1 branch
BranchTrueFalseTotal
$report_id or croak("report ID required?!") 36 0 36
631 36 my $query = $self->grammar->insert_rr;
632  
633 36 my $ip = $rec->row->source_ip;
634 36 ▶ $ip = $self->any_inet_pton($ip) if $self->grammar->language ne 'postgresql';
Branch: 1 branch
BranchTrueFalseTotal
if $self->grammar->language ne "postgresql" 36 0 36
635 36 my @args = ( $report_id, $ip, $rec->{row}{count}, );
636 36 foreach my $f (qw/ header_from envelope_to envelope_from /) {
637 108 ▶ push @args,
Branch: 1 branch
BranchTrueFalseTotal
$rec->identifiers->$f ? : 108 0 108
638 $rec->identifiers->$f
639 ? $self->get_domain_id( $rec->identifiers->$f )
640 : undef;
641 }
642 36 push @args, map { $rec->row->policy_evaluated->$_ } qw/ disposition dkim spf /;
643 36 ▶ my $rr_id = $self->query( $query, \@args ) or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $rr_id = $self->query($query, \@args) or croak() 36 0 36
644 36 return $self->{report_row_id} = $rr_id;
645 }
646  
647 6 my @rfc9990_columns = qw/ np fo testing discovery_method /;
648  
649 23 ▶sub insert_policy_published( $self, $id, $pub ) {
Subroutine insert_policy_published: called CC=1 SCAR=0.0
Pod undocumented
650 23 my %value = ( %$pub, testing => $pub->{t} );
651  
652 # un-upgraded databases lack the RFC 9990 columns
653 23 my %has = map { $_ => 1 } $self->table_columns('report_policy_published');
654 23 my @fields = grep { $has{$_} }
655 ( qw/ adkim aspf p sp pct rua /, @rfc9990_columns );
656  
657 23 my $query = $self->grammar->insert_into( 'report_policy_published',
658 [ 'report_id', @fields ] );
659 23 $self->query( $query, [ $id, @value{@fields} ] );
660 23 return 1;
661 }
662  
663 28 ▶sub table_columns( $self, $table ) {
Subroutine table_columns: called CC=2 SCAR=6.9
Pod undocumented
664 28 ▶ $self->{_columns}{$table} //=
Condition100%2/2
$self->{"_columns"}{$table} //= [$self->db_connect->query("SELECT * FROM $table WHERE 1=0")->columns]
l!l
1414
MC/DC100%1/1
$self->{"_columns"}{$table} //= [$self->db_connect->query("SELECT * FROM $table WHERE 1=0")->columns]
$self->{"_columns"}{$table}
Truth table100%2/2
A
A= $self->{"_columns"}{$table}
Aresult
11
00
665 [ $self->db_connect->query("SELECT * FROM $table WHERE 1=0")->columns ];
666 28 return @{ $self->{_columns}{$table} };
667 }
668  
669 1923 ▶sub db_connect($self) {
Subroutine db_connect: called CC=14 SCAR=29.9
Pod undocumented
670  
671 1923 ▶ my $dsn = $self->config->{report_store}{dsn} or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $dsn = $self->config->{"report_store"}{"dsn"} or croak() 1923 0 1923
672 1923 my $user = $self->config->{report_store}{user};
673 1923 my $pass = $self->config->{report_store}{pass};
674  
675 # cacheing
676 1923 ▶ if ( $self->{grammar} && $self->{dbix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"grammar"} and $self->{"dbix"}) 1897 26 1923
Condition66%2/3
$self->{"grammar"} and $self->{"dbix"}
!ll&&!rl&&r
2601897
MC/DC50%1/2
$self->{"grammar"} and $self->{"dbix"}
$self->{"grammar"} $self->{"dbix"}
Truth table66%2/3
A and B
A= $self->{"grammar"}B= $self->{"dbix"}
ABresult
0X0
100
111
677 1897 my $cached_grammar_type = $self->{grammar}->dsn;
678 1897 ▶ if ( $dsn =~ /$cached_grammar_type/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /$cached_grammar_type/) 1897 0 1897
679 1897 return $self->{dbix}; # caching
680 }
681 }
682  
683 26 my $needs_tables;
684  
685 26 $self->{grammar} = undef;
686 26 delete @$self{qw/ _columns _reason_types /};
687 26 my %opts;
688  
689 26 ▶ if ( $dsn =~ /sqlite/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($dsn =~ /sqlite/i) { } 26 0 26
elsif ($dsn =~ /mysql/i) { } 0 0 0
elsif ($dsn =~ /pg/i) { } 0 0 0
690 26 my ($db) = ( split /=/, $dsn )[-1];
691 26 ▶ if ( !$db || $db eq ':memory:' || !-e $db ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $db or $db eq ":memory:" or not -e $db) 14 12 26
Condition50%3/6
not $db or $db eq ":memory:" or not -e $db
l!l&&r!l&&!r
01412
not $db or $db eq ":memory:"
l!l&&r!l&&!r
0026
MC/DC33%1/3
not $db or $db eq ":memory:" or not -e $db
not $db $db eq ":memory:" not -e $db
Truth table50%2/4
not(A) or B or not(C)
A= not $dbB= $db eq ":memory:"C= not -e $db
ABCresult
1XX1
01X1
0011
0000
692 14 my $schema = 'mail_dmarc_schema.sqlite';
693 14 ▶ $needs_tables = $self->get_db_schema($schema)
Branch: 1 branch
BranchTrueFalseTotal
$needs_tables = $self->get_db_schema($schema) or croak("can't locate DB $db AND can't find $schema! Create $db manually.\n") 14 0 14
694 or croak
695 "can't locate DB $db AND can't find $schema! Create $db manually.\n";
696 }
697 26 $self->{grammar} = Mail::DMARC::Report::Store::SQL::Grammars::SQLite->new();
698 }
699 elsif ( $dsn =~ /mysql/i ) {
700 0 $opts{'mysql_enable_utf8mb4'} = 1;
701 0 $self->{grammar} = Mail::DMARC::Report::Store::SQL::Grammars::MySQL->new();
702 }
703 elsif ( $dsn =~ /pg/i ) {
704 $self->{grammar}
705 0 = Mail::DMARC::Report::Store::SQL::Grammars::PostgreSQL->new();
706 }
707 else {
708 0 croak "can't determine database type, so unable to load grammar.\n";
709 }
710  
711 26 ▶ $self->{dbix} = DBIx::Simple->connect( $dsn, $user, $pass, \%opts )
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbix"} = "DBIx::Simple"->connect($dsn, $user, $pass, \%opts) or return $self->error("DBIx::Simple"->error) 26 0 26
712 or return $self->error( DBIx::Simple->error );
713  
714 26 ▶ if ($needs_tables) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_tables) { } 14 12 26
715 14 $self->apply_db_schema($needs_tables);
716 }
717 else {
718 12 $self->warn_if_schema_outdated($dsn);
719 }
720  
721 26 return $self->{dbix};
722 }
723  
724 6 my %schema_checked;
725 6 my %engine_suffix = ( sqlite => 'sqlite', mysql => 'mysql', postgresql => 'pgsql' );
726 6 my %engine_client = ( sqlite => 'sqlite3', mysql => 'mysql', postgresql => 'psql' );
727  
728 # nothing fails without the share/ upgrades, so say so once per database
729 15 ▶sub warn_if_schema_outdated( $self, $dsn ) {
Subroutine warn_if_schema_outdated: called CC=6 SCAR=17.9
Pod undocumented
730 15 ▶ return if $schema_checked{$dsn}++;
Branch: 1 branch
BranchTrueFalseTotal
if $schema_checked{$dsn}++ 12 3 15
731  
732 3 my $language = $self->grammar->language;
733 3 my $suffix = $engine_suffix{$language};
734 3 my @outdated;
735  
736 3 try {
737 3 my %has = map { $_ => 1 } $self->table_columns('report_policy_published');
738 3 my @missing = grep { !$has{$_} } @rfc9990_columns;
739 3 ▶ push @outdated, [ "mail_dmarc_rfc9990.$suffix",
Branch: 1 branch
BranchTrueFalseTotal
if @missing 1 2 3
740 "report_policy_published lacks the RFC 9990 columns @missing" ]
741 if @missing;
742 }
743 catch ($e) { };
744  
745 3 try {
746 3 my $file = $self->get_db_schema("mail_dmarc_indexes.$suffix");
747 3 my @want = $self->slurp($file)
748 =~ /(?:CREATE INDEX IF NOT EXISTS |mail_dmarc_add_index\('\w+', ')(\w+)/g;
749 51 my %have = map { $_->{name} => 1 }
750 3 $self->{dbix}->query( $self->grammar->select_index_names )->hashes;
751 3 my $missing = grep { !$have{$_} } @want;
752 3 ▶ push @outdated, [ "mail_dmarc_indexes.$suffix",
Branch: 1 branch
BranchTrueFalseTotal
if $missing 0 3 3
753 "$missing of the indexes dmarc_httpd needs are missing" ]
754 if $missing;
755 }
756 catch ($e) { };
757  
758 3 my ($db) = $dsn =~ /(?:dbname|database|db)=([^;]+)/i;
759 3 foreach (@outdated) {
760 1 my ( $file, $why ) = @$_;
761 1 my $path = $self->get_db_schema($file);
762 1 ▶ warn "Mail::DMARC: $why. Upgrade with:\n"
Condition50%1/2
$db // "<database>"
l!l
01
MC/DC0%0/1
$db // "<database>"
$db
Truth table50%1/2
A
A= $db
Aresult
11
00
763 . "\t$engine_client{$language} " . ( $db // '<database>' ) . " < $path\n";
764 }
765 3 return scalar @outdated;
766 }
767  
768 757 ▶sub db_check_err( $self, $err ) {
Subroutine db_check_err: called CC=4 SCAR=14.2
Pod undocumented
769 ## no critic (PackageVars)
770 757 ▶ return if !defined $DBI::errstr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $DBI::errstr 756 1 757
771 1 ▶ return if !$DBI::errstr;
Branch: 1 branch
BranchTrueFalseTotal
unless $DBI::errstr 0 1 1
772 1 ▶ return if $DBI::errstr eq 'DBI error: ';
Branch: 1 branch
BranchTrueFalseTotal
if $DBI::errstr eq "DBI error: " 0 1 1
773 1 croak $err . $DBI::errstr;
774 }
775  
776 1989 ▶sub dbix($self) {
Subroutine dbix: called CC=2 SCAR=6.9
Pod undocumented
777 1989 ▶ return $self->{dbix} if $self->{dbix};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dbix"} 1987 2 1989
778 2 return $self->db_connect();
779 }
780  
781 14 ▶sub apply_db_schema( $self, $file ) {
Subroutine apply_db_schema: called CC=2 SCAR=6.9
Pod undocumented
782 14 my $setup = $self->slurp($file);
783 14 foreach ( split /;/, $setup ) {
784  
785 # warn "$_\n";
786 938 $self->dbix->query($_);
787 }
788 14 return;
789 }
790  
791 18 ▶sub get_db_schema( $self, $file ) {
Subroutine get_db_schema: called CC=2 SCAR=9.2
Pod undocumented
792 18 ▶ return "share/$file" if -f "share/$file"; # when testing
Branch: 1 branch
BranchTrueFalseTotal
if -f "share/$file" 18 0 18
793 0 return File::ShareDir::dist_file( 'Mail-DMARC', $file ); # when installed
794 }
795  
796 796 ▶sub query( $self, $query, $params = undef, @extra ) {
Subroutine query: called CC=9 SCAR=22.1
Pod undocumented
797  
798 796 my @c = caller;
799 796 my $err = sprintf( "query called by %s, %s\n", $c[0], $c[2] ) . "\t$query\n\t";
800  
801 796 my @params;
802 796 ▶ if ( defined $params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params) 787 9 796
803 787 ▶ @params = ref $params eq 'ARRAY' ? @$params : $params;
Branch: 1 branch
BranchTrueFalseTotal
ref $params eq 'ARRAY' ? : 787 0 787
804 6 ▶ no warnings; ## no critic (NoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
805 787 $err .= join( ', ', @params );
806 }
807  
808 796 ▶ croak "too many arguments to exec_query!" if @extra;
Branch: 1 branch
BranchTrueFalseTotal
if @extra 0 796 796
809  
810 796 ▶ my $dbix = $self->db_connect() or croak DBIx::Simple->error;
Branch: 1 branch
BranchTrueFalseTotal
my $dbix = $self->db_connect or "DBIx::Simple"->croak->error 796 0 796
811  
812 796 ▶ return $self->query_insert( $query, $err, @params ) if $query =~ /^INSERT/ix;
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^INSERT/xi 236 560 796
813 560 ▶ return $self->query_replace( $query, $err, @params )
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^(?:REPLACE|UPDATE)/xi 6 554 560
814 if $query =~ /^(?:REPLACE|UPDATE)/ix;
815 554 ▶ return $self->query_delete( $query, $err, @params )
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^(?:DELETE|TRUNCATE)/xi 127 427 554
816 if $query =~ /^(?:DELETE|TRUNCATE)/ix;
817 427 return $self->query_any( $query, $err, @params );
818 }
819  
820 427 ▶sub query_any( $self, $query, $err, @params ) {
Subroutine query_any: called CC=3 SCAR=11.0
Pod undocumented
821  
822 # warn "query: $query\n" . join(", ", @params) . "\n";
823 427 my $r;
824 427 ▶ eval { $r = $self->dbix->query( $query, @params )->hashes; } or print '';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $r = $self->dbix->query($query, @params)->hashes } } or print "" 426 1 427
825 427 $self->db_check_err($err);
826 426 ▶ die "something went wrong with: $err\n" if !$r; ## no critic (Carp)
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 426 426
827 426 return $r;
828 }
829  
830 236 ▶sub query_insert( $self, $query, $err, @params ) {
Subroutine query_insert: called CC=4 SCAR=13.9
Pod undocumented
831 236 ▶ eval { $self->dbix->query( $query, @params ) } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->dbix->query($query, @params) } } or do { warn "DBIx::Simple"->error . "\n"; croak($err) } 234 2 236
832 2 warn DBIx::Simple->error . "\n";
833 2 croak $err;
834 };
835 234 $self->db_check_err($err);
836  
837 # If the table has no autoincrement field, last_insert_id is zero
838 234 my ( undef, undef, $table ) = split /\s+/, $query;
839 234 ▶ ($table) = split( /\(/, $table ) if $table =~ /\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $table =~ /\(/ 0 234 234
840 234 $table =~ s/^"|"$//g;
841 234 ▶ croak "unable to determine table in query: $query" if !$table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 234 234
842 234 return $self->dbix->last_insert_id( undef, undef, $table, undef );
843 }
844  
845 6 ▶sub query_replace( $self, $query, $err, @params ) {
Subroutine query_replace: called CC=2 SCAR=7.1
Pod undocumented
846 6 ▶ $self->dbix->query( $query, @params ) or croak $err;
Branch: 1 branch
BranchTrueFalseTotal
$self->dbix->query($query, @params) or croak($err) 5 0 5
847 5 $self->db_check_err($err);
848 5 return 1; # sorry, no indication of success
849 }
850  
851 127 ▶sub query_delete( $self, $query, $err, @params ) {
Subroutine query_delete: called CC=2 SCAR=6.9
Pod undocumented
852 127 ▶ my $affected = $self->dbix->query( $query, @params )->rows or croak $err;
Branch: 1 branch
BranchTrueFalseTotal
my $affected = $self->dbix->query($query, @params)->rows or croak($err) 91 35 126
853 91 $self->db_check_err($err);
854 91 return $affected;
855 }
856  
857 15 ▶sub get_row_spf( $self, $rowid ) {
Subroutine get_row_spf: called CC=1 SCAR=0.0
Pod undocumented
858 15 return $self->query( $self->grammar->select_row_spf, [$rowid] );
859 }
860  
861 15 ▶sub get_row_dkim( $self, $rowid ) {
Subroutine get_row_dkim: called CC=1 SCAR=0.0
Pod undocumented
862 15 return $self->query( $self->grammar->select_row_dkim, [$rowid] );
863 }
864  
865 15 ▶sub get_row_reason( $self, $rowid ) {
Subroutine get_row_reason: called CC=1 SCAR=0.0
Pod undocumented
866 15 return $self->query( $self->grammar->select_row_reason, [$rowid] );
867 }
868  
869 # A store doubles as the queue for reports this installation is preparing to
870 # send, and those are about other people's domains as seen by us. Reports about
871 # our domains as seen by others are a different population, and blending the
872 # two makes the headline pass rate answer neither question. They are told apart
873 # by who authored the report, which is the only signal the schema carries; an
874 # installation that has changed its org_name will read its older outgoing
875 # reports as received.
876 49 ▶sub _origin_filter( $self, $kind ) {
Subroutine _origin_filter: called CC=6 SCAR=18.4
877 49 ▶ return ( '', [] ) if 'all' eq ( $kind // '' );
Branch: 1 branch
BranchTrueFalseTotal
if "all" eq ($kind // "") 16 33 49
Condition50%1/2
$kind // ""
l!l
490
MC/DC0%0/1
$kind // ""
$kind
Truth table50%1/2
A
A= $kind
Aresult
11
00
878  
879 33 my $org = $self->config->{organization}{org_name};
880 33 ▶ return ( '', [] ) if !$org;
Branch: 1 branch
BranchTrueFalseTotal
unless $org 0 33 33
881  
882 33 ▶ my $op = 'outgoing' eq ( $kind // '' ) ? '=' : '<>';
Branch: 1 branch
BranchTrueFalseTotal
'outgoing' eq ($kind // '') ? : 4 29 33
Condition50%1/2
$kind // ""
l!l
330
MC/DC0%0/1
$kind // ""
$kind
Truth table50%1/2
A
A= $kind
Aresult
11
00
883 33 return ( $self->grammar->and_arg( 'a.org_name', $op ), [$org] );
884 }
885  
886 # Reports are attributed to the day their window begins, matching the day
887 # bucketing in the grammar, so a window filter compares against begin alone.
888 32 ▶sub _agg_where( $self, $args ) {
Subroutine _agg_where: called CC=7 SCAR=19.5
889 32 my ( $where, @params ) = ('');
890  
891 32 my @filters = (
892 [ since => 'r.begin', '>=' ],
893 [ until => 'r.begin', '<=' ],
894 [ from_domain => 'fd.domain', '=' ],
895 [ author => 'a.org_name', '=' ],
896 );
897  
898 32 foreach my $filter (@filters) {
899 128 my ( $arg, $column, $op ) = @$filter;
900 128 ▶ next if !defined $args->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{$arg} 44 84 128
901 84 $where .= $self->grammar->and_arg( $column, $op );
902 84 push @params, $args->{$arg};
903 }
904  
905 32 ▶ if ( defined $args->{source_ip} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"source_ip"}) 8 24 32
906 8 $where .= $self->grammar->and_arg('rr.source_ip');
907 push @params,
908 $self->grammar->language eq 'postgresql'
909 ? $args->{source_ip}
910 8 ▶ : $self->any_inet_pton( $args->{source_ip} );
Branch: 1 branch
BranchTrueFalseTotal
$self->grammar->language eq 'postgresql' ? : 0 8 8
911 }
912  
913 32 ▶ $where .= $self->grammar->and_failing if $args->{failing_only};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"failing_only"} 11 21 32
914  
915 my ( $origin, $origin_params )
916 32 ▶ = $self->_origin_filter( $args->{reports} // 'received' );
Condition100%2/2
$args->{"reports"} // "received"
l!l
626
MC/DC100%1/1
$args->{"reports"} // "received"
$args->{"reports"}
Truth table100%2/2
A
A= $args->{"reports"}
Aresult
11
00
917 32 $where .= $origin;
918 32 push @params, @$origin_params;
919  
920 32 return ( $where, \@params );
921 }
922  
923 # DBI hands back aggregates as strings; the JSON views need numbers.
924 32 ▶sub _agg_numify($rows) {
Subroutine _agg_numify: called CC=4 SCAR=13.9
925 32 foreach my $row (@$rows) {
926 55 foreach my $field ( @AGG_FIELDS,
927 qw/ day reporters first_seen last_seen reports / )
928 {
929 715 ▶ $row->{$field} += 0 if defined $row->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->{$field} 403 312 715
930 }
931 }
932 32 return $rows;
933 }
934  
935 14 ▶sub _agg_ips_to_text( $self, $rows ) {
Subroutine _agg_ips_to_text: called CC=4 SCAR=14.5
936 14 ▶ return $rows if $self->grammar->language eq 'postgresql';
Branch: 1 branch
BranchTrueFalseTotal
if $self->grammar->language eq "postgresql" 0 14 14
937 14 foreach my $row (@$rows) {
938 $row->{source_ip} = $self->any_inet_ntop( $row->{source_ip} )
939 41 ▶ if $row->{source_ip};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"source_ip"} 41 0 41
940 }
941 14 return $rows;
942 }
943  
944 11 ▶sub get_timeseries( $self, @args ) {
Subroutine get_timeseries: called CC=2 SCAR=6.9
Pod undocumented
945 11 ▶ croak "invalid parameters" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 1 10 11
946 10 my %args = @args;
947  
948 10 my ( $where, $params ) = $self->_agg_where( \%args );
949 10 my $rows
950 = $self->query( $self->grammar->select_timeseries_query($where), $params );
951  
952 10 return { data => _agg_numify($rows) };
953 }
954  
955 # Totals are summed from the daily buckets rather than queried separately, so
956 # the tiles and the chart can never disagree.
957 9 ▶sub _sum_timeseries( $self, %args ) {
Subroutine _sum_timeseries: called CC=4 SCAR=13.9
958 9 my $days = $self->get_timeseries(%args)->{data};
959  
960 9 my %totals = map { $_ => 0 } @AGG_FIELDS;
961 9 foreach my $day (@$days) {
962 4 ▶ $totals{$_} += $day->{$_} || 0 for @AGG_FIELDS;
Condition100%2/2
$day->{$_} || 0
l!l
2012
MC/DC100%1/1
$day->{$_} || 0
$day->{$_}
Truth table100%2/2
A
A= $day->{$_}
Aresult
11
00
963 }
964 9 $totals{dmarc_pass} = $totals{messages} - $totals{aligned_none};
965 9 $totals{days} = scalar @$days;
966  
967 9 return \%totals;
968 }
969  
970 5 ▶sub get_summary( $self, @args ) {
Subroutine get_summary: called CC=4 SCAR=14.0
Pod undocumented
971 5 ▶ croak "invalid parameters" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 5 5
972 5 my %args = @args;
973  
974 5 my %summary = ( current => $self->_sum_timeseries(%args) );
975  
976 # An equal-length window immediately before this one, for the deltas.
977 5 ▶ if ( defined $args{since} && defined $args{until} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"since"} and defined $args{"until"}) 4 1 5
Condition66%2/3
defined $args{"since"} and defined $args{"until"}
!ll&&!rl&&r
104
MC/DC50%1/2
defined $args{"since"} and defined $args{"until"}
defined $args{"since"} defined $args{"until"}
Truth table66%2/3
A and B
A= defined $args{"since"}B= defined $args{"until"}
ABresult
0X0
100
111
978 4 my $span = $args{until} - $args{since};
979 $summary{previous} = $self->_sum_timeseries( %args,
980 since => $args{since} - $span - 1,
981 4 until => $args{since} - 1,
982 );
983 }
984  
985 5 return \%summary;
986 }
987  
988 7 ▶sub get_sources( $self, @args ) {
Subroutine get_sources: called CC=13 SCAR=25.9
Pod undocumented
989 7 ▶ croak "invalid parameters" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 7 7
990 7 my %args = @args;
991  
992 7 my ( $where, $params ) = $self->_agg_where( \%args );
993 7 my $sources
994 = $self->query( $self->grammar->select_sources_query($where), $params );
995 7 my ( $fwhere, $fparams )
996 = $self->_agg_where( { %args, failing_only => 1 } );
997 7 my $reasons = $self->query(
998 $self->grammar->select_source_reasons_query($fwhere), $fparams );
999  
1000 7 $self->_agg_ips_to_text($sources);
1001 7 $self->_agg_ips_to_text($reasons);
1002 7 _agg_numify($sources);
1003  
1004 7 my %by_ip;
1005 7 foreach my $reason (@$reasons) {
1006 12 ▶ push @{ $by_ip{ $reason->{source_ip} } },
Condition50%1/2
$reason->{'messages'} || 0
l!l
120
MC/DC0%0/1
$reason->{'messages'} || 0
$reason->{'messages'}
Truth table50%1/2
A
A= $reason->{'messages'}
Aresult
11
00
1007 {
1008 type => $reason->{type},
1009 comment => $reason->{comment},
1010 12 messages => ( $reason->{messages} || 0 ) + 0,
1011 };
1012 }
1013  
1014 7 foreach my $source (@$sources) {
1015 29 ▶ $source->{reasons} = $by_ip{ $source->{source_ip} } || [];
Condition100%2/2
$by_ip{$source->{'source_ip'}} || []
l!l
1118
MC/DC100%1/1
$by_ip{$source->{'source_ip'}} || []
$by_ip{$source->{'source_ip'}}
Truth table100%2/2
A
A= $by_ip{$source->{'source_ip'}}
Aresult
11
00
1016 29 $source->{bucket} = _classify_source($source);
1017 }
1018  
1019 my $sort = $args{sort_col} && $args{sort_col} eq 'messages'
1020 11 ▶ ? sub { $b->{messages} <=> $a->{messages} }
Subroutine __ANON__: called CC=1 SCAR=0.0
1021 28 ▶ : sub { $b->{aligned_none} <=> $a->{aligned_none}
Subroutine __ANON__: called CC=2 SCAR=7.0
1022 7 ▶ || $b->{messages} <=> $a->{messages} };
Branch: 2 branches
BranchTrueFalseTotal
$args{'sort_col'} && $args{'sort_col'} eq 'messages' ? : 1 6 7
$Mail::DMARC::Report::Store::SQL::b->{"aligned_none"} <=> $Mail::DMARC::Report::Store::SQL::a->{"aligned_none"} or $Mail::DMARC::Report::Store::SQL::b->{"messages"} <=> $Mail::DMARC::Report::Store::SQL::a->{"messages"} 22 6 28
Condition66%2/3
$args{"sort_col"} and $args{"sort_col"} eq "messages"
!ll&&!rl&&r
601
MC/DC50%1/2
$args{"sort_col"} and $args{"sort_col"} eq "messages"
$args{"sort_col"} $args{"sort_col"} eq "messages"
Truth table66%2/3
A and B
A= $args{"sort_col"}B= $args{"sort_col"} eq "messages"
ABresult
0X0
100
111
1023 7 my @sorted = sort $sort @$sources;
1024  
1025 7 my $total = scalar @sorted;
1026  
1027 # Summed before paging: a caller showing each source's share of the window
1028 # cannot derive the denominator from one page.
1029 7 my $messages = 0;
1030 7 ▶ $messages += $_->{messages} || 0 foreach @sorted;
Condition50%1/2
$_->{'messages'} || 0
l!l
290
MC/DC0%0/1
$_->{'messages'} || 0
$_->{'messages'}
Truth table50%1/2
A
A= $_->{'messages'}
Aresult
11
00
1031  
1032 7 ▶ if ( $args{length} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"length"}) 1 6 7
1033 1 ▶ my $start = $args{start} || 0;
Condition50%1/2
$args{'start'} || 0
l!l
10
MC/DC0%0/1
$args{'start'} || 0
$args{'start'}
Truth table50%1/2
A
A= $args{'start'}
Aresult
11
00
1034 1 ▶ $start = 0 if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 0 1 1
1035 1 @sorted = splice @sorted, $start, $args{length};
1036 }
1037  
1038 return {
1039 7 recordsTotal => $total,
1040 messagesTotal => $messages,
1041 data => \@sorted,
1042 };
1043 }
1044  
1045 # Ranks a source by whether it needs attention. Telling a misconfigured sender
1046 # apart from an unauthenticated one needs the auth detail tables, which the
1047 # list query does not join; get_source_detail makes that distinction.
1048 29 ▶sub _classify_source($source) {
Subroutine _classify_source: called CC=10 SCAR=23.1
1049 29 ▶ my $failing = $source->{aligned_none} || 0;
Condition100%2/2
$source->{'aligned_none'} || 0
l!l
227
MC/DC100%1/1
$source->{'aligned_none'} || 0
$source->{'aligned_none'}
Truth table100%2/2
A
A= $source->{'aligned_none'}
Aresult
11
00
1050 29 ▶ my $total = $source->{messages} || 0;
Condition50%1/2
$source->{'messages'} || 0
l!l
290
MC/DC0%0/1
$source->{'messages'} || 0
$source->{'messages'}
Truth table50%1/2
A
A= $source->{'messages'}
Aresult
11
00
1051  
1052 29 ▶ return 'aligned' if !$failing;
Branch: 1 branch
BranchTrueFalseTotal
unless $failing 7 22 29
1053 22 ▶ return 'aligned' if $total && ( $failing / $total ) < $FAILING_TOLERANCE;
Branch: 1 branch
BranchTrueFalseTotal
if $total and $failing / $total < $FAILING_TOLERANCE 3 19 22
Condition66%2/3
$total and $failing / $total < $FAILING_TOLERANCE
!ll&&!rl&&r
0193
MC/DC50%1/2
$total and $failing / $total < $FAILING_TOLERANCE
$total $failing / $total < $FAILING_TOLERANCE
Truth table66%2/3
A and B
A= $totalB= $failing / $total < $FAILING_TOLERANCE
ABresult
0X0
100
111
1054  
1055 # The largest single reason, not their sum: one record may carry several,
1056 # and each row already counts that record's whole volume.
1057 19 my $forwarded = 0;
1058 19 foreach my $reason ( @{ $source->{reasons} } ) {
1059 12 ▶ next if !_explains_forwarding($reason);
Branch: 1 branch
BranchTrueFalseTotal
unless _explains_forwarding($reason) 2 10 12
1060 10 ▶ $forwarded = $reason->{messages} if $reason->{messages} > $forwarded;
Branch: 1 branch
BranchTrueFalseTotal
if $reason->{"messages"} > $forwarded 9 1 10
1061 }
1062 19 ▶ return 'forwarded' if $forwarded >= $failing * $FORWARDING_SHARE;
Branch: 1 branch
BranchTrueFalseTotal
if $forwarded >= $failing * $FORWARDING_SHARE 8 11 19
1063  
1064 11 return 'failing';
1065 }
1066  
1067 5 ▶sub get_source_detail( $self, @args ) {
Subroutine get_source_detail: called CC=11 SCAR=24.3
Pod undocumented
1068 5 ▶ croak "invalid parameters" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 5 5
1069 5 my %args = @args;
1070 5 ▶ croak "missing source_ip" if !defined $args{source_ip};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"source_ip"} 1 4 5
1071  
1072 4 my ( $where, $params ) = $self->_agg_where( \%args );
1073  
1074 4 my $records
1075 = $self->query( $self->grammar->select_source_detail_query($where), $params );
1076 4 my $dkim
1077 = $self->query( $self->grammar->select_source_dkim_query($where), $params );
1078 4 my $spf
1079 = $self->query( $self->grammar->select_source_spf_query($where), $params );
1080  
1081 4 _agg_numify($_) for $records, $dkim, $spf;
1082  
1083 4 my $failing = 0;
1084 $failing += $_->{messages}
1085 4 ▶ foreach grep { 'pass' ne ( $_->{dkim} // '' ) && 'pass' ne ( $_->{spf} // '' ) }
Condition57%4/7
'pass' ne ($_->{'dkim'} // '') && 'pass' ne ($_->{'spf'} // '')
!ll&&!rl&&r
204
$_->{"dkim"} // ""
l!l
60
$_->{"spf"} // ""
l!l
40
MC/DC50%1/2
'pass' ne ($_->{'dkim'} // '') && 'pass' ne ($_->{'spf'} // '')
'pass' ne ($_->{'dkim'} // '') 'pass' ne ($_->{'spf'} // '')
MC/DC0%0/1
$_->{"dkim"} // ""
$_->{"dkim"}
MC/DC0%0/1
$_->{"spf"} // ""
$_->{"spf"}
Truth table66%2/3
A && B
A= 'pass' ne ($_->{'dkim'} // '')B= 'pass' ne ($_->{'spf'} // '')
ABresult
0X0
100
111
Truth table50%1/2
A
A= $_->{"dkim"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"spf"}
Aresult
11
00
1086 @$records;
1087  
1088 4 my ( $fwhere, $fparams )
1089 = $self->_agg_where( { %args, failing_only => 1 } );
1090 my $failing_auth
1091 4 = @{ $self->query( $self->grammar->select_source_dkim_query($fwhere),
1092 $fparams ) }
1093 4 ▶ || @{ $self->query( $self->grammar->select_source_spf_query($fwhere),
Condition66%2/3
@{$self->query($self->grammar->select_source_dkim_query($fwhere), $fparams);} || @{$self->query($self->grammar->select_source_spf_query($fwhere), $fparams);}
l!l&&r!l&&!r
103
MC/DC50%1/2
@{$self->query($self->grammar->select_source_dkim_query($fwhere), $fparams);} || @{$self->query($self->grammar->select_source_spf_query($fwhere), $fparams);}
@{$self->query($self->grammar->select_source_dkim_query($fwhere), $fparams);} @{$self->query($self->grammar->select_source_spf_query($fwhere), $fparams);}
Truth table66%2/3
A || B
A= @{$self->query($self->grammar->select_source_dkim_query($fwhere), $fparams);}B= @{$self->query($self->grammar->select_source_spf_query($fwhere), $fparams);}
ABresult
1X1
011
000
1094 $fparams ) };
1095  
1096 4 ▶ my $bucket
Branch: 2 branches
BranchTrueFalseTotal
!$failing ? : 1 3 4
$failing_auth ? : 1 2 3
1097 = !$failing ? 'aligned'
1098 : $failing_auth ? 'broken'
1099 : 'unauthenticated';
1100  
1101 return {
1102 source_ip => $args{source_ip},
1103 4 bucket => $bucket,
1104 records => $records,
1105 dkim => $dkim,
1106 spf => $spf,
1107 };
1108 }
1109  
1110 3 ▶sub get_report_domains( $self, @args ) {
Subroutine get_report_domains: called CC=3 SCAR=11.0
Pod undocumented
1111 3 ▶ croak "invalid parameters" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 3 3
1112 3 my %args = @args;
1113  
1114 my ( $where, $params )
1115 3 ▶ = $self->_origin_filter( $args{reports} // 'received' );
Condition100%2/2
$args{"reports"} // "received"
l!l
12
MC/DC100%1/1
$args{"reports"} // "received"
$args{"reports"}
Truth table100%2/2
A
A= $args{"reports"}
Aresult
11
00
1116 3 my $rows
1117 = $self->query( $self->grammar->select_report_domains_query($where),
1118 $params );
1119  
1120 3 return { data => _agg_numify($rows) };
1121 }
1122  
1123 1096 ▶sub grammar($self) {
Subroutine grammar: called CC=1 SCAR=0.0
Pod undocumented
1124 1096 $self->db_connect();
1125 1096 return $self->{grammar};
1126 }
1127  
1128 6 1;
1129  
1130 __END__
« blib/lib/Mail/DMARC/ConfigData.pm ↑ Summary lib/Mail/DMARC/Report/URI.pm »