Authentication/Handler/ReturnOK.pm

filter: Statementstmt 46.3% 94 / 203 Branchbran 29.0% 18 / 62 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 87.5% 14 / 16 Podpod 14.2% 1 / 7 totaltotal 42.6% 127 / 298 CC 43 SCAR 53.0
CC
43
Cov
41%
_check_domain
395.9
_check_domain_rr
15.5
envfrom_callback
3.0
CRAP 201.0
« Authentication/Handler/BIMI.pm ↑ Summary Authentication/App/Blocker/App/Command/add.pm »
1 package Mail::Milter::Authentication::Handler::ReturnOK;
2 2 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Handler class for Checking Return address validity
7 2 our $VERSION = '4.20261116'; # VERSION
8 2 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub default_config {
Subroutine default_config: not called CC=1 SCAR=6.9
Pod undocumented
12 0 return {};
13 }
14  
15 0 ▶sub grafana_rows {
Subroutine grafana_rows: not called CC=1 SCAR=6.9
Pod undocumented
16 0 my ( $self ) = @_;
17 0 my @rows;
18 0 push @rows, $self->get_json( 'ReturnOK_metrics' );
19 0 return \@rows;
20 }
21  
22 1 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
23 return {
24 1 'returnok_total' => 'The number of emails processed for ReturnOK',
25 };
26 }
27  
28 2 ▶sub _check_address {
Subroutine _check_address: called CC=2 SCAR=7.2
29 2 my ( $self, $address, $type ) = @_;
30  
31 2 my $email = $self->get_address_from( $address );
32  
33 2 ▶ if ( ! $email ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email) 0 2 2
34 0 $self->log_error( "ReturnOK: No Address for $type" );
35 }
36  
37 2 my $domain = lc $self->get_domain_from( $email );
38  
39 2 $self->_check_domain ( $domain, $type );
40 }
41  
42 2 ▶sub _check_domain_rr {
Subroutine _check_domain_rr: called CC=9 SCAR=27.4
43 2 my ( $self, $domain, $rrtype ) = @_;
44 2 my $resolver = $self->get_object('resolver');
45 2 my $return = {
46 'result' => 0,
47 'error' => '',
48 'values' => [],
49 };
50 2 eval {
51 2 my $packet = $resolver->query( $domain, $rrtype );
52 2 ▶ if ($packet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet) { } 2 0 2
53 2 foreach my $rr ( $packet->answer ) {
54 8 ▶ next unless $rr->type eq $rrtype;
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq $rrtype 0 8 8
55 8 $return->{ 'result' } = 1;
56 8 ▶ push @{$return->{'values'}}, $rr->exchange if $rrtype eq 'MX';
Branch: 1 branch
BranchTrueFalseTotal
if $rrtype eq "MX" 8 0 8
57 0 ▶ push @{$return->{'values'}}, $rr->address if $rrtype eq 'A';
Branch: 1 branch
BranchTrueFalseTotal
if $rrtype eq "A" 0 8 8
58 0 ▶ push @{$return->{'values'}}, $rr->address if $rrtype eq 'AAAA';
Branch: 1 branch
BranchTrueFalseTotal
if $rrtype eq "AAAA" 0 8 8
59 ## TODO Check the returned record is in fact valid for its type
60 }
61 }
62 else {
63 0 my $error = $resolver->errorstring;
64 0 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
65 0 $return->{ 'error' } = $error;
66 }
67 }
68 };
69 2 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 2 2
70 0 $self->handle_exception( $error );
71 0 $return->{ 'error' } = 'lookup_error';
72 0 $self->log_error( "ReturnOK: Domain lookup fatal error $error for $domain $rrtype" );
73 }
74 2 return $return;
75 }
76  
77 2 ▶sub _check_domain {
Subroutine _check_domain: called CC=29 SCAR=59.8
78 2 my ( $self, $domain, $type ) = @_;
79  
80 2 ▶ return if exists $self->{ 'done' }->{ join(':',$domain,$type) };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"done"}{join ":", $domain, $type} 0 2 2
81 2 $self->{ 'done' }->{ join(':',$domain,$type) } = 1;
82  
83 2 my $metrics = {};
84 2 my @details;
85  
86 2 ▶ if ( ! $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($domain) 0 2 2
87 0 $self->log_error( "ReturnOK: No Domain for $type" );
88 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-return-mx' )->safe_set_value( 'none' );
89 0 $metrics->{ $type . '_result' } = 'none';
90 0 $self->dbgout( 'ReturnOKCheck', 'none', LOG_DEBUG );
91 0 $self->add_auth_header($header);
92 0 push @{ $self->{ 'metrics' } }, $metrics;
93 0 return;
94 }
95  
96 2 push @details, Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $type . '.domain' )->safe_set_value( $domain );
97  
98 # Get Org domain and check that if different.
99 2 my $is_org = -1;
100 2 my $org_domain;
101 2 ▶ if ( $self->is_handler_loaded( 'DMARC' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_handler_loaded("DMARC")) 2 0 2
102 2 my $dmarc_handler = $self->get_handler('DMARC');
103 2 my $dmarc_object = $dmarc_handler->get_dmarc_object();
104 2 ▶ if ( $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain) 2 0 2
105 2 $org_domain = eval{ $dmarc_object->get_organizational_domain( $domain ); };
106 2 $self->handle_exception( $@ );
107 2 ▶ if ( $org_domain eq $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($org_domain eq $domain) { } 2 0 2
108 2 $is_org = 1;
109 2 $metrics->{ $type . '_is_org_domain' } = 'yes';
110 }
111 else {
112 0 $is_org = 0;
113 0 $metrics->{ $type . '_is_org_domain' } = 'no';
114 0 push @details, Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.org_domain' )->safe_set_value( $org_domain );
115 }
116 2 ▶ push @details, Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.is_org' )->safe_set_value( $is_org ? 'yes' : 'no' );
Branch: 1 branch
BranchTrueFalseTotal
$is_org ? : 2 0 2
117 }
118 }
119  
120 2 my $lookup_mx = $self->_check_domain_rr( $domain, 'MX' );
121 2 ▶ if ( $lookup_mx->{ 'error' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_mx->{"error"}) 0 2 2
122 0 push @details, Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.mx_error' )->safe_set_value( $lookup_mx->{ 'error' } );
123 }
124  
125 # If MX passed then that's it, stop checking
126 2 ▶ if ( $lookup_mx->{ 'result' } == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_mx->{"result"} == 1) 2 0 2
127 2 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-return-mx' )->safe_set_value( 'pass' );
128 2 push @details, Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'MX Records found: '.join(',',@{$lookup_mx->{'values'}}) );
129 2 $metrics->{ $type . '_result' } = 'pass';
130 2 $metrics->{ $type . '_has_mx' } = 'yes';
131 2 $self->dbgout( 'ReturnOKCheck', 'pass', LOG_DEBUG );
132 2 foreach my $detail ( @details ) {
133 6 $header->add_child( $detail );
134 }
135 2 $self->add_auth_header($header);
136 2 push @{ $self->{ 'metrics' } }, $metrics;
137 2 return;
138 }
139 0 $metrics->{ $type . '_has_mx' } = 'no';
140  
141 0 my $lookup_a = $self->_check_domain_rr( $domain, 'A' );
142 0 ▶ if ( $lookup_a->{ 'error' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_a->{"error"}) 0 0 0
143 0 push @details, Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.a_error' )->safe_set_value( $lookup_a->{ 'error' } );
144 }
145 0 my $lookup_aaaa = $self->_check_domain_rr( $domain, 'AAAA' );
146 0 ▶ if ( $lookup_aaaa->{ 'error' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_aaaa->{"error"}) 0 0 0
147 0 push @details, Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.aaaa_error' )->safe_set_value( $lookup_aaaa->{ 'error' } );
148 }
149  
150 # If we have an A or AAAA recoed then consider this a warn.
151 0 ▶ if ( $lookup_a->{ 'result' } || $lookup_aaaa->{ 'result' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_a->{"result"} or $lookup_aaaa->{"result"}) 0 0 0
Condition0%0/3
$lookup_a->{"result"} or $lookup_aaaa->{"result"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$lookup_a->{"result"} or $lookup_aaaa->{"result"}
$lookup_a->{"result"} $lookup_aaaa->{"result"}
Truth table0%0/3
A or B
A= $lookup_a->{"result"}B= $lookup_aaaa->{"result"}
ABresult
1X1
011
000
152 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-return-mx' )->safe_set_value( 'warn' );
153 0 $metrics->{ $type . '_result' } = 'warn';
154 0 $self->dbgout( 'ReturnOKCheck', 'warn', LOG_DEBUG );
155 0 ▶ if ( $lookup_a->{ 'result' } == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_a->{'result'} == 1) { } 0 0 0
156 0 $metrics->{ $type . '_has_a' } = 'yes';
157 0 push @details, Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'A Records found: '.join(',',@{$lookup_a->{'values'}}) );
158 }
159 else {
160 0 $metrics->{ $type . '_has_a' } = 'no';
161 }
162 0 ▶ if ( $lookup_aaaa->{ 'result' } == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_aaaa->{'result'} == 1) { } 0 0 0
163 0 $metrics->{ $type . '_has_aaaa' } = 'yes';
164 0 push @details, Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'AAAA Records found: '.join(',',@{$lookup_aaaa->{'values'}}) );
165 }
166 else {
167 0 $metrics->{ $type . '_has_aaaa' } = 'no';
168 }
169 0 foreach my $detail ( @details ) {
170 0 $header->add_child( $detail );
171 }
172 0 $self->add_auth_header($header);
173 0 push @{ $self->{ 'metrics' } }, $metrics;
174 0 return;
175 }
176 0 $metrics->{ $type . '_has_a' } = 'no';
177 0 $metrics->{ $type . '_has_aaaa' } = 'no';
178  
179 0 ▶ if ( $is_org == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_org == 0) 0 0 0
180 # We have DMARC to look this up, have done so, and found that we are NOT the org domain, so recheck at the org domain
181  
182 0 my $lookup_mx = $self->_check_domain_rr( $org_domain, 'MX' );
183 0 ▶ if ( $lookup_mx->{ 'error' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_mx->{"error"}) 0 0 0
184 0 push @details, Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.org_mx_error' )->safe_set_value( $lookup_mx->{ 'error' } );
185 }
186  
187 # If MX passed then that's it, stop checking
188 0 ▶ if ( $lookup_mx->{ 'result' } == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_mx->{"result"} == 1) 0 0 0
189 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-return-mx' )->safe_set_value( 'warn' );
190 0 $self->dbgout( 'ReturnOKCheck', 'warn', LOG_DEBUG );
191 0 push @details, Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'Org Domain MX Records found: '.join(',',@{$lookup_mx->{'values'}}) );
192 0 foreach my $detail ( @details ) {
193 0 $metrics->{ $type . '_result' } = 'warn';
194 0 $metrics->{ $type . '_has_org_mx' } = 'yes';
195 0 $header->add_child( $detail );
196 }
197 0 $self->add_auth_header($header);
198 0 push @{ $self->{ 'metrics' } }, $metrics;
199 0 return;
200 }
201 0 $metrics->{ $type . '_has_org_mx' } = 'no';
202  
203 0 my $lookup_a = $self->_check_domain_rr( $org_domain, 'A' );
204 0 ▶ if ( $lookup_a->{ 'error' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_a->{"error"}) 0 0 0
205 0 push @details, Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.org_a_error' )->safe_set_value( $lookup_a->{ 'error' } );
206 }
207 0 my $lookup_aaaa = $self->_check_domain_rr( $org_domain, 'AAAA' );
208 0 ▶ if ( $lookup_aaaa->{ 'error' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_aaaa->{"error"}) 0 0 0
209 0 push @details, Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.org_aaaa_error' )->safe_set_value( $lookup_aaaa->{ 'error' } );
210 }
211  
212 # If we have an A or AAAA recoed then consider this a warn.
213 0 ▶ if ( $lookup_a->{ 'result' } || $lookup_aaaa->{ 'result' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_a->{"result"} or $lookup_aaaa->{"result"}) 0 0 0
Condition0%0/3
$lookup_a->{"result"} or $lookup_aaaa->{"result"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$lookup_a->{"result"} or $lookup_aaaa->{"result"}
$lookup_a->{"result"} $lookup_aaaa->{"result"}
Truth table0%0/3
A or B
A= $lookup_a->{"result"}B= $lookup_aaaa->{"result"}
ABresult
1X1
011
000
214 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-return-mx' )->safe_set_value( 'warn' );
215 0 $metrics->{ $type . '_result' } = 'warn';
216 0 $self->dbgout( 'ReturnOKCheck', 'warn', LOG_DEBUG );
217 0 ▶ if ( $lookup_a->{ 'result' } == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_a->{'result'} == 1) { } 0 0 0
218 0 $metrics->{ $type . '_has_org_a' } = 'yes';
219 0 push @details, Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'Org Domain A Records found: '.join(',',@{$lookup_a->{'values'}}) );
220 }
221 else {
222 0 $metrics->{ $type . '_has_org_a' } = 'no';
223 }
224 0 ▶ if ( $lookup_aaaa->{ 'result' } == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_aaaa->{'result'} == 1) { } 0 0 0
225 0 $metrics->{ $type . '_has_org_aaaa' } = 'yes';
226 0 push @details, Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'Org Domain AAAA Records found: '.join(',',@{$lookup_aaaa->{'values'}}) );
227 }
228 else {
229 0 $metrics->{ $type . '_has_org_aaaa' } = 'no';
230 }
231 0 foreach my $detail ( @details ) {
232 0 $header->add_child( $detail );
233 }
234 0 $self->add_auth_header($header);
235 0 push @{ $self->{ 'metrics' } }, $metrics;
236 0 return;
237 }
238 0 $metrics->{ $type . '_has_org_a' } = 'no';
239 0 $metrics->{ $type . '_has_org_aaaa' } = 'no';
240  
241 }
242  
243 # We got here, we fail!
244 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-return-mx' )->safe_set_value( 'fail' );
245 0 $metrics->{ $type . '_result' } = 'fail';
246 0 $self->dbgout( 'ReturnOKCheck', 'fail', LOG_DEBUG );
247 0 foreach my $detail ( @details ) {
248 0 $header->add_child( $detail );
249 }
250 0 $self->add_auth_header($header);
251  
252 0 push @{ $self->{ 'metrics' } }, $metrics;
253 }
254  
255 1 ▶sub envfrom_callback {
Subroutine envfrom_callback: called CC=3 SCAR=11.0
Pod undocumented
256 1 my ( $self, $env_from ) = @_;
257  
258 1 $self->{ 'metrics' } = [];
259 1 $self->{ 'done' } = {};
260  
261 1 ▶ $env_from = q{} if $env_from eq '<>';
Branch: 1 branch
BranchTrueFalseTotal
if $env_from eq "<>" 0 1 1
262 1 my $addresses = $self->get_addresses_from( $env_from );
263 1 foreach my $address ( @$addresses ) {
264 1 $self->_check_address( $address, 'smtp' );
265 }
266 }
267  
268 6 ▶sub header_callback {
Subroutine header_callback: called CC=3 SCAR=11.0
Pod undocumented
269 6 my ( $self, $header, $value ) = @_;
270  
271 6 ▶ if ( $header eq 'From' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header eq "From") 1 5 6
272 1 my $addresses = $self->get_addresses_from( $value );
273 1 foreach my $address ( @$addresses ) {
274 1 $self->_check_address( $address, 'header' );
275 }
276 }
277 }
278  
279 1 ▶sub close_callback {
Subroutine close_callback: called CC=1 SCAR=0.0
Pod undocumented
280 1 my ( $self ) = @_;
281 1 delete $self->{ 'done' };
282 1 delete $self->{ 'metrics' };
283 }
284  
285 1 ▶sub eom_callback {
Subroutine eom_callback: called CC=2 SCAR=6.9
Pod undocumented
286 1 my ( $self ) = @_;
287  
288 1 my $metrics = $self->{ 'metrics' };
289  
290 1 foreach my $metric ( @$metrics ) {
291 2 $self->metric_count( 'returnok_total', $metric );
292 }
293 }
294  
295 2 1;
296  
297 __END__
« Authentication/Handler/BIMI.pm ↑ Summary Authentication/App/Blocker/App/Command/add.pm »