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

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