Authentication/Handler/IPRev.pm

filter: Statementstmt 94.4% 153 / 162 Branchbran 83.7% 67 / 80 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 88.2% 15 / 17 Podpod 16.6% 1 / 6 totaltotal 89.4% 247 / 276 CC 53 SCAR 31.0
CC
53
Cov
91%
connect_callback
38.0
_address_for_domain
8.0
_is_transient_dns_error
5.1
CRAP 22.2
« Authentication/Handler/RBLDNS.pm ↑ Summary Authentication/Handler/XGoogleDKIM.pm »
1 package Mail::Milter::Authentication::Handler::IPRev;
2 30 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Handler class for IPRev
7 30 our $VERSION = '4.20261116'; # VERSION
8 30 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub default_config {
Subroutine default_config: not called CC=1 SCAR=6.9
Pod undocumented
13 0 return {};
14 }
15  
16 47 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
17 return {
18 47 'iprev_total' => 'The number of emails processed for IPRev',
19 };
20 }
21  
22 0 ▶sub grafana_rows {
Subroutine grafana_rows: not called CC=1 SCAR=6.9
Pod undocumented
23 0 my ( $self ) = @_;
24 0 my @rows;
25 0 push @rows, $self->get_json( 'IPRev_metrics' );
26 0 return \@rows;
27 }
28  
29 41 ▶sub _dns_error {
Subroutine _dns_error: called CC=3 SCAR=11.0
30 41 my ( $self, $type, $data, $error ) = @_;
31 41 ▶ if ( $error eq 'NXDOMAIN' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error eq 'NXDOMAIN') { } 3 38 41
elsif ($error eq 'NOERROR') { } 31 7 38
32 3 $self->dbgout( "DNS $type Lookup", "$data gave $error", LOG_DEBUG );
33 }
34 elsif ( $error eq 'NOERROR' ) {
35 31 $self->dbgout( "DNS $type Lookup", "$data gave $error", LOG_DEBUG );
36 }
37 else {
38 # Could be SERVFAIL or something else
39 7 $self->log_error(
40 'DNS ' . $type . ' query failed for '
41 . $data
42 . ' with '
43 . $error );
44 }
45 }
46  
47 41 ▶sub _is_transient_dns_error {
Subroutine _is_transient_dns_error: called CC=5 SCAR=16.2
48 41 my ( $self, $error ) = @_;
49 41 ▶ return 0 if ! $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 0 41 41
50 41 ▶ return 0 if $error eq 'NOERROR';
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "NOERROR" 31 10 41
51 10 ▶ return 0 if $error eq 'NXDOMAIN';
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "NXDOMAIN" 3 7 10
52 7 ▶ return 0 if $error eq 'unknown error or no error';
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "unknown error or no error" 0 7 7
53 # SERVFAIL, query timed out, REFUSED, socket errors etc
54 7 return 1;
55 }
56  
57 39 ▶sub connect_requires {
Subroutine connect_requires: called CC=1 SCAR=0.0
Pod undocumented
58 39 my ($self) = @_;
59 39 my @requires = qw{ LocalIP TrustedIP };
60 39 return \@requires;
61 }
62  
63 89 ▶sub connect_callback {
Subroutine connect_callback: called CC=37 SCAR=36.4
Pod undocumented
64 89 my ( $self, $hostname, $ip ) = @_;
65 89 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 8 81 89
66 81 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 2 79 81
67 79 my $ip_address = $self->ip_address();
68 79 my $i1 = $ip;
69 79 my $resolver = $self->get_object('resolver');
70  
71 79 my $lookup_limit = 10;
72 # Make this a config item
73  
74 79 $self->{'iprev_temperror'} = 0;
75 79 $self->{'iprev_lookup_limit_reached'} = 0;
76  
77 79 my $ptr_list = {};
78 79 my @error_list;
79  
80 my @cname_hosts;
81  
82 79 my $packet = $resolver->query( $ip_address, 'PTR' );
83 79 $lookup_limit--;
84 79 ▶ if ($packet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet) 33 46 79
85 33 foreach my $rr ( $packet->answer ) {
86 43 ▶ if ( $rr->type eq "CNAME" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq "CNAME") 2 41 43
87 2 push @cname_hosts, $rr->rdstring;
88 2 push @error_list, 'Found CNAME in PTR response';
89 }
90 43 ▶ if ( $rr->type eq "PTR" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq "PTR") 41 2 43
91 41 $ptr_list->{ $rr->rdstring } = [];
92 }
93 }
94 }
95  
96 79 ▶ if ( $resolver->errorstring() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolver->errorstring) 15 64 79
97 15 $self->_dns_error( 'PTR', $ip_address, $resolver->errorstring );
98 15 ▶ $self->{'iprev_temperror'} = 1 if $self->_is_transient_dns_error( $resolver->errorstring );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_transient_dns_error($resolver->errorstring) 2 13 15
99 15 ▶ push @error_list, 'Error ' . $resolver->errorstring() . " looking up $ip_address PTR" if ( $resolver->errorstring() ne 'unknown error or no error' );
Branch: 1 branch
BranchTrueFalseTotal
if $resolver->errorstring ne "unknown error or no error" 15 0 15
100 }
101  
102 79 foreach my $cname_host ( @cname_hosts ) {
103 2 my $packet = $resolver->query( $cname_host, 'PTR' );
104 2 $lookup_limit--;
105 2 ▶ if ($packet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet) 2 0 2
106 2 foreach my $rr ( $packet->answer ) {
107 #if ( $rr->type eq "CNAME" ) {
108 # NO! We only follow the first level CNAMES
109 # Because anything more is probably busted anyway
110 #}
111 2 ▶ if ( $rr->type eq "PTR" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq "PTR") 2 0 2
112 2 $ptr_list->{ $rr->rdstring } = [];
113 }
114 }
115 }
116 2 ▶ if ( $resolver->errorstring() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolver->errorstring) 0 2 2
117 0 $self->_dns_error( 'PTR', $cname_host, $resolver->errorstring );
118 0 ▶ $self->{'iprev_temperror'} = 1 if $self->_is_transient_dns_error( $resolver->errorstring );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_transient_dns_error($resolver->errorstring) 0 0 0
119 0 ▶ push @error_list, 'Error ' . $resolver->errorstring() . " looking up $cname_host PTR" if ( $resolver->errorstring() ne 'unknown error or no error' );
Branch: 1 branch
BranchTrueFalseTotal
if $resolver->errorstring ne "unknown error or no error" 0 0 0
120 }
121 2 last; # Because multiple CNAMES for a given record is also busted
122 }
123  
124 79 ▶ if ( ! keys %$ptr_list ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$ptr_list) 46 33 79
125 46 push @error_list, "NOT FOUND";
126 }
127  
128 # Forward lookups use the record type for the connecting IP version first
129 79 ▶ my ( $first_type, $second_type ) = ( $ip_address =~ /:/ ) ? ( 'AAAA', 'A' ) : ( 'A', 'AAAA' );
Branch: 1 branch
BranchTrueFalseTotal
$ip_address =~ /:/u ? : 2 77 79
130  
131 79 my $fwd_errors = {};
132 79 my @lookup_list = sort keys %$ptr_list;
133 79 foreach my $domain ( @lookup_list ) {
134 38 my $ip_list = [];
135 38 my @domain_errors;
136  
137 52 ▶ my $lookup = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
138 52 my ( $type, $name ) = @_;
139 52 ▶ return ( [] ) if $self->{'iprev_lookup_limit_reached'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"iprev_lookup_limit_reached"} 1 51 52
140 51 my ( $ips, $errors, $cname, $transient );
141 51 ( $lookup_limit, $ips, $errors, $cname, $transient ) = $self->_address_for_domain( $type, $name, $lookup_limit );
142 51 push @domain_errors, @$errors;
143 # Only a lookup for the address family of the connecting IP could have produced a match
144 51 ▶ $self->{'iprev_temperror'} = 1 if $transient && $type eq $first_type;
Branch: 1 branch
BranchTrueFalseTotal
if $transient and $type eq $first_type 4 47 51
Condition100%3/3
$transient and $type eq $first_type
!ll&&!rl&&r
4614
MC/DC100%2/2
$transient and $type eq $first_type
$transient $type eq $first_type
Truth table100%3/3
A and B
A= $transientB= $type eq $first_type
ABresult
0X0
100
111
145 51 return ( $ips, $cname );
146 38 };
147  
148 TYPE:
149 38 foreach my $type ( $first_type, $second_type ) {
150 50 my ( $ips, $cname ) = $lookup->( $type, $domain );
151 50 ▶ if ( $cname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cname) 2 48 50
152 2 push @error_list, "Found CNAME in $type response";
153 2 foreach my $cname_type ( $first_type, $second_type ) {
154 2 ( $ips ) = $lookup->( $cname_type, $cname );
155 2 ▶ last if @$ips;
Branch: 1 branch
BranchTrueFalseTotal
if @$ips 2 0 2
156 }
157 }
158 50 $ip_list = $ips;
159 50 ▶ last TYPE if @$ip_list;
Branch: 1 branch
BranchTrueFalseTotal
if @$ip_list 27 23 50
160 }
161  
162 38 $fwd_errors->{ $domain } = \@domain_errors;
163 38 $ptr_list->{ $domain } = $ip_list;
164  
165 # Any further lookups would also hit the limit
166 38 ▶ last if $self->{'iprev_lookup_limit_reached'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"iprev_lookup_limit_reached"} 1 37 38
167 }
168  
169 79 my @match_list;
170 79 foreach my $domain ( sort keys %$ptr_list ) {
171 43 my $domain_matched = 0;
172 43 foreach my $address ( sort @{ $ptr_list->{ $domain } } ) {
173 27 my $i2 = Net::IP->new($address);
174 27 ▶ if ( !$i2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $i2) { } 0 27 27
175 0 $self->log_error( 'IPRev: Could not parse IP '.$address );
176 }
177 else {
178 27 ▶ my $is_overlap = $i1->overlaps($i2) || 0;
Condition100%2/2
$i1->overlaps($i2) || 0
l!l
243
MC/DC100%1/1
$i1->overlaps($i2) || 0
$i1->overlaps($i2)
Truth table100%2/2
A
A= $i1->overlaps($i2)
Aresult
11
00
179 27 ▶ if ( $is_overlap == $IP_IDENTICAL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_overlap == $Mail::Milter::Authentication::Handler::IPRev::IP_IDENTICAL) 24 3 27
180 24 $domain_matched = 1;
181 24 push @match_list, $domain =~ s/\.$//r;
182 }
183 }
184 }
185 # Note why this host did not verify
186 43 ▶ push @error_list, @{ $fwd_errors->{ $domain } // [] } if ! $domain_matched;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain_matched 19 24 43
Condition100%2/2
$fwd_errors->{$domain} // []
l!l
145
MC/DC100%1/1
$fwd_errors->{$domain} // []
$fwd_errors->{$domain}
Truth table100%2/2
A
A= $fwd_errors->{$domain}
Aresult
11
00
187 }
188  
189 79 ▶ if ( ! @match_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @match_list) { } 55 24 79
190 # Failed to match IP against looked up domains
191 # RFC 8601 2.7.3
192 my $result = $self->{'iprev_temperror'} ? 'temperror' # A lookup failed in a way which may succeed later
193 55 ▶ : $self->{'iprev_lookup_limit_reached'} ? 'permerror' # We could not complete the evaluation
Branch: 3 branches
BranchTrueFalseTotal
$self->{'iprev_temperror'} ? : 5 50 55
$self->{'iprev_lookup_limit_reached'} ? : 1 49 50
!keys(%$ptr_list) ? : 44 5 49
194 : ( ! keys %$ptr_list ) ? 'permerror' # No PTR data for this IP
195 : 'fail';
196 55 my $comment = join( ',', @error_list );
197 55 $self->dbgout( 'IPRevCheck', "$result - $comment", LOG_DEBUG );
198 55 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'iprev' )->safe_set_value( $result );
199 55 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.remote-ip' )->safe_set_value( $ip_address ) );
200 55 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( $comment ) );
201 55 $self->add_c_auth_header($header);
202 55 $self->metric_count( 'iprev_total', { 'result' => $result } );
203 }
204 else {
205 # We have a pass
206 24 my $comment = join( ',', @match_list );
207 24 $self->{'verified_ptr'} = $comment;
208 24 $self->dbgout( 'IPRevCheck', "pass - $comment", LOG_DEBUG );
209 24 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'iprev' )->safe_set_value( 'pass' );
210 24 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.remote-ip' )->safe_set_value( $ip_address ) );
211 24 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( $comment ) );
212 24 $self->add_c_auth_header($header);
213 24 $self->metric_count( 'iprev_total', { 'result' => 'pass'} );
214 }
215 }
216  
217 51 ▶sub _address_for_domain {
Subroutine _address_for_domain: called CC=8 SCAR=20.8
218 51 my ( $self, $type, $domain, $lookup_limit ) = @_;
219  
220 51 my @fwd_errors;
221 my @ip_list;
222 51 my $cname;
223 51 my $transient = 0;
224  
225 51 my $resolver = $self->get_object('resolver');
226  
227 51 $lookup_limit--;
228 51 ▶ if ( $lookup_limit <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup_limit <= 0) 1 50 51
229 1 $self->{'iprev_lookup_limit_reached'} = 1;
230 1 return ( 0, \@ip_list, [ 'Lookup limit reached' ] );
231 }
232 50 my $packet = $resolver->query( $domain, $type );
233  
234 50 ▶ if ($packet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet) 29 21 50
235 29 foreach my $rr ( $packet->answer ) {
236 29 ▶ if ( lc $rr->type eq 'cname' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $rr->type eq "cname") 2 27 29
237 2 $cname = $rr->rdstring;
238 # Multiple CNAMES are broken, but we don't check for that
239 # We just take the last one we found
240 }
241 29 ▶ if ( lc $rr->type eq lc $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $rr->type eq lc $type) 27 2 29
242 27 push @ip_list, $rr->rdstring;
243 }
244 }
245 }
246  
247 50 ▶ if ( $resolver->errorstring() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolver->errorstring) 26 24 50
248 26 $self->_dns_error( $type, $domain, $resolver->errorstring );
249 26 ▶ push @fwd_errors, 'Error ' . $resolver->errorstring() . " looking up $domain $type" if ( $resolver->errorstring() ne 'unknown error or no error' );
Branch: 1 branch
BranchTrueFalseTotal
if $resolver->errorstring ne "unknown error or no error" 26 0 26
250 26 $transient = $self->_is_transient_dns_error( $resolver->errorstring );
251 }
252  
253 50 return ( $lookup_limit, \@ip_list, \@fwd_errors, $cname, $transient );
254 }
255  
256 125 ▶sub close_callback {
Subroutine close_callback: called CC=1 SCAR=0.0
Pod undocumented
257 125 my ( $self ) = @_;
258 125 delete $self->{'verified_ptr'};
259 125 delete $self->{'iprev_temperror'};
260 125 delete $self->{'iprev_lookup_limit_reached'};
261 }
262  
263 30 1;
264  
265 __END__
« Authentication/Handler/RBLDNS.pm ↑ Summary Authentication/Handler/XGoogleDKIM.pm »