Zonemaster/Engine/Recursor.pm

filter: Statementstmt 15.6% 48 / 307 Branchbran 0.0% 0 / 112 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 46.8% 15 / 32 Podpod 100% 10 / 10 totaltotal 13.5% 73 / 540 CC 91 SCAR 52.9
CC
91
Cov
10%
_resolve_cname
506.0
_recurse
420.0
_do_query
156.0
CRAP 199.0
« Zonemaster/Engine/Test/Zone.pm ↑ Summary Zonemaster/Engine/Test/Syntax.pm »
1 package Zonemaster::Engine::Recursor;
2  
3 1 ▶use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use version; our $VERSION = version->declare("v1.1.0");
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Class::Accessor "antlers";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::ShareDir qw[dist_file];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Slurp qw( read_file );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::IP::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use List::MoreUtils qw[uniq];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use Zonemaster::Engine::Nameserver;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Zonemaster::Engine::DNSName;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Zonemaster::Engine::Util qw( name ns parse_hints );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Zonemaster::Engine::Constants ":cname";
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 our %_fake_addresses_cache;
23  
24 0 ▶sub init_recursor {
Subroutine init_recursor: not called CC=1 SCAR=6.9
Pod documented
25 0 my $hints_path = dist_file( 'Zonemaster-Engine', 'named.root' );
26 0 my $hints_text = read_file( $hints_path );
27 0 my $hints_data = parse_hints( $hints_text );
28 0 Zonemaster::Engine::Recursor->add_fake_addresses( '.', $hints_data );
29 }
30  
31 0 ▶sub add_fake_addresses {
Subroutine add_fake_addresses: not called CC=3 SCAR=24.8
Pod documented
32 0 my ( $class, $domain, $href ) = @_;
33 0 $domain = lc $domain;
34  
35 0 foreach my $name ( keys %{$href} ) {
36 0 my @ips = uniq @{ $href->{$name} };
37 0 $name = lc $name;
38  
39 0 push @{ $_fake_addresses_cache{$domain}{$name} }, ();
40 0 foreach my $ip ( @ips ) {
41 0 push @{ $_fake_addresses_cache{$domain}{$name} }, $ip;
42 }
43 }
44  
45 0 return;
46 }
47  
48 0 ▶sub has_fake_addresses {
Subroutine has_fake_addresses: not called CC=1 SCAR=6.9
Pod documented
49 0 my ( $class, $domain ) = @_;
50 0 $domain = lc $domain;
51  
52 0 return !!$_fake_addresses_cache{$domain};
53 }
54  
55 0 ▶sub get_fake_addresses {
Subroutine get_fake_addresses: not called CC=4 SCAR=30.0
Pod documented
56 0 my ( $class, $domain, $nsname ) = @_;
57 0 ▶ ( defined $domain ) or croak 'Argument must be defined: $domain';
Branch: 1 branch
BranchTrueFalseTotal
defined $domain or croak("Argument must be defined: \$domain") 0 0 0
58  
59 0 $domain = lc $domain;
60 0 ▶ $nsname = ( defined $nsname ) ? lc $nsname : q{};
Branch: 1 branch
BranchTrueFalseTotal
defined $nsname ? : 0 0 0
61  
62 0 ▶ if ( exists $_fake_addresses_cache{$domain}{$nsname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Zonemaster::Engine::Recursor::_fake_addresses_cache{$domain}{$nsname}) { } 0 0 0
63 0 return @{ $_fake_addresses_cache{$domain}{$nsname} };
64 }
65 else {
66 0 return ();
67 }
68 }
69  
70 0 ▶sub get_fake_names {
Subroutine get_fake_names: not called CC=2 SCAR=17.9
Pod documented
71 0 my ( $class, $domain ) = @_;
72 0 $domain = lc $domain;
73  
74 0 ▶ if ( exists $_fake_addresses_cache{$domain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Zonemaster::Engine::Recursor::_fake_addresses_cache{$domain}) { } 0 0 0
75 0 return keys %{$_fake_addresses_cache{$domain}};
76 }
77 else {
78 0 return ();
79 }
80 }
81  
82 0 ▶sub remove_fake_addresses {
Subroutine remove_fake_addresses: not called CC=1 SCAR=6.9
Pod documented
83 0 my ( $class, $domain ) = @_;
84 0 $domain = lc $domain;
85  
86 0 delete $_fake_addresses_cache{$domain};
87  
88 0 return;
89 }
90  
91 0 ▶sub recurse {
Subroutine recurse: not called CC=6 SCAR=37.4
92 0 my ( $class, $name, $type, $dns_class, $ns ) = @_;
93 0 $name = name( $name );
94 0 ▶ $type //= 'A';
Condition0%0/2
$type //= "A"
l!l
00
MC/DC0%0/1
$type //= "A"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
95 0 ▶ $dns_class //= 'IN';
Condition0%0/2
$dns_class //= "IN"
l!l
00
MC/DC0%0/1
$dns_class //= "IN"
$dns_class
Truth table0%0/2
A
A= $dns_class
Aresult
11
00
96  
97 0 Zonemaster::Engine->logger->add( RECURSE => { name => $name, type => $type, class => $dns_class } );
98  
99 0 ▶ if ( defined $ns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns) 0 0 0
100 0 ▶ ref( $ns ) eq 'ARRAY' or croak 'Argument $ns must be an arrayref';
Branch: 1 branch
BranchTrueFalseTotal
ref $ns eq "ARRAY" or croak("Argument \$ns must be an arrayref") 0 0 0
101 }
102  
103 0 ▶ my %state = ( ns => defined $ns ? [ @$ns ] : [ root_servers() ], count => 0, common => 0, seen => {}, glue => {} );
Branch: 1 branch
BranchTrueFalseTotal
defined $ns ? : 0 0 0
104  
105 0 my ( $p, $state_final ) = $class->_recurse( $name, $type, $dns_class, \%state );
106  
107 0 return $p;
108 }
109  
110 # Cache the results of recurse() to speed up repeated queries for the same name, type,
111 # class and set of name servers. Parameters are normalized to ensure that the cache is
112 # hit for semantically identical queries.
113 0 ▶memoize('recurse', NORMALIZER => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
114 0 my ( $class, $name, $type, $dns_class, $ns ) = @_;
115  
116 0 $name = name( $name );
117 0 ▶ $type //= 'A';
Condition0%0/2
$type //= "A"
l!l
00
MC/DC0%0/1
$type //= "A"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
118 0 ▶ $dns_class //= 'IN';
Condition0%0/2
$dns_class //= "IN"
l!l
00
MC/DC0%0/1
$dns_class //= "IN"
$dns_class
Truth table0%0/2
A
A= $dns_class
Aresult
11
00
119 0 ▶ $ns //= [ root_servers() ];
Condition0%0/2
$ns //= [root_servers()]
l!l
00
MC/DC0%0/1
$ns //= [root_servers()]
$ns
Truth table0%0/2
A
A= $ns
Aresult
11
00
120  
121 0 my $nss = join( ',', sort @$ns );
122  
123 0 ▶ return join( '|',
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
124 ref($class) || $class,
125 $name,
126 $type,
127 $dns_class,
128 $nss
129 );
130 1 });
131  
132 0 ▶sub parent {
Subroutine parent: not called CC=8 SCAR=42.8
Pod documented
133 0 my ( $class, $name ) = @_;
134 0 $name = name( $name );
135  
136 0 my ( $p, $state ) =
137 $class->_recurse( $name, 'SOA', 'IN',
138 { ns => [ root_servers() ], count => 0, common => 0, seen => {}, glue => {} } );
139  
140 0 my $pname;
141 0 ▶ if ( name( $state->{trace}[0][0] ) eq name( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (name($state->{'trace'}[0][0]) eq name($name)) { } 0 0 0
142 0 $pname = name( $state->{trace}[1][0] );
143 }
144 else {
145 0 $pname = name( $state->{trace}[0][0] );
146 }
147  
148 # Extra check that parent really is parent.
149 0 ▶ if ( $name->next_higher ne $pname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->next_higher ne $pname) 0 0 0
150 0 my $source_ns = $state->{trace}[0][1];
151 0 my $source_ip = $state->{trace}[0][2];
152  
153 # No $source_ns means we're looking at root taken from priming
154 0 ▶ if ( $source_ns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_ns) 0 0 0
155 0 my $pp;
156 0 ▶ if ( $source_ns->can( 'query' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_ns->can('query')) { } 0 0 0
157 0 $pp = $source_ns->query( $name->next_higher->string, 'SOA' );
158 }
159 else {
160 0 my $n = ns( $source_ns, $source_ip );
161 0 $pp = $n->query( $name->next_higher->string, 'SOA' );
162 }
163 0 ▶ if ( $pp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pp) 0 0 0
164 0 my ( $rr ) = $pp->get_records( 'SOA', 'answer' );
165 0 ▶ if ( $rr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr) 0 0 0
166 0 $pname = name( $rr->owner );
167 }
168 }
169 }
170 } ## end if ( $name->next_higher...)
171  
172 0 ▶ if ( wantarray() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
173 0 return ( $pname, $p );
174 }
175 else {
176 0 return $pname;
177 }
178 } ## end sub parent
179  
180 0 ▶sub _resolve_cname {
Subroutine _resolve_cname: not called CC=22 SCAR=62.3
181 0 my ( $class, $name, $type, $dns_class, $p, $state ) = @_;
182 0 $name = name( $name );
183 0 Zonemaster::Engine->logger->add( CNAME_START => { name => $name, type => $type, dns_class => $dns_class } );
184  
185 0 my @cname_rrs = $p->get_records( 'CNAME', 'answer' );
186  
187 # Remove duplicate CNAME RRs
188 0 my ( %duplicate_cname_rrs, @original_rrs );
189 0 for my $rr ( @cname_rrs ) {
190 0 my $rr_hash = $rr->class . '/CNAME/' . lc($rr->owner) . '/' . lc($rr->cname);
191  
192 0 ▶ if ( exists $duplicate_cname_rrs{$rr_hash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $duplicate_cname_rrs{$rr_hash}) { } 0 0 0
193 0 $duplicate_cname_rrs{$rr_hash}++;
194 }
195 else {
196 0 $duplicate_cname_rrs{$rr_hash} = 0;
197 0 push @original_rrs, $rr;
198 }
199 }
200  
201 0 ▶ unless ( scalar @original_rrs == scalar @cname_rrs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @original_rrs == scalar @cname_rrs) 0 0 0
202 Zonemaster::Engine->logger->add( CNAME_RECORDS_DUPLICATES => {
203 0 ▶ records => join(';', map { "$_ => $duplicate_cname_rrs{$_}" if $duplicate_cname_rrs{$_} > 0 } keys %duplicate_cname_rrs )
Branch: 1 branch
BranchTrueFalseTotal
if $duplicate_cname_rrs{$_} > 0 0 0 0
204 }
205 );
206 0 @cname_rrs = @original_rrs;
207 }
208  
209 # Break if there are too many records
210 0 ▶ if ( scalar @cname_rrs > $CNAME_MAX_RECORDS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @cname_rrs > $Zonemaster::Engine::Recursor::CNAME_MAX_RECORDS) 0 0 0
211 0 Zonemaster::Engine->logger->add( CNAME_RECORDS_TOO_MANY => { name => $name, count => scalar @cname_rrs, max => $CNAME_MAX_RECORDS } );
212 0 return ( undef, $state );
213 }
214  
215 0 my ( %cnames, %seen_targets, %forbidden_targets );
216 0 for my $rr ( @cname_rrs ) {
217 0 my $rr_owner = lc( name( $rr->owner ) );
218 0 my $rr_target = lc( name( $rr->cname ) );
219  
220 # Multiple CNAME records with same owner name
221 0 ▶ if ( exists $forbidden_targets{$rr_owner} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $forbidden_targets{$rr_owner}) 0 0 0
222 0 Zonemaster::Engine->logger->add( CNAME_RECORDS_MULTIPLE_FOR_NAME => { name => $rr_owner } );
223 0 return ( undef, $state );
224 }
225  
226 # CNAME owner name is target, or target has already been seen in this response, or owner name cannot be a target
227 0 ▶ if ( $rr_owner eq $rr_target or exists $seen_targets{$rr_target} or grep { $_ eq $rr_target } ( keys %forbidden_targets ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr_owner eq $rr_target or exists $seen_targets{$rr_target} or grep {$_ eq $rr_target;} keys %forbidden_targets) 0 0 0
Condition0%0/6
$rr_owner eq $rr_target or exists $seen_targets{$rr_target} or grep {$_ eq $rr_target;} keys %forbidden_targets
l!l&&r!l&&!r
000
$rr_owner eq $rr_target or exists $seen_targets{$rr_target}
l!l&&r!l&&!r
000
MC/DC0%0/3
$rr_owner eq $rr_target or exists $seen_targets{$rr_target} or grep {$_ eq $rr_target;} keys %forbidden_targets
$rr_owner eq $rr_target exists $seen_targets{$rr_target} grep {$_ eq $rr_target;} keys %forbidden_targets
Truth table0%0/4
A or B or C
A= $rr_owner eq $rr_targetB= exists $seen_targets{$rr_target}C= grep {$_ eq $rr_target;} keys %forbidden_targets
ABCresult
1XX1
01X1
0011
0000
228 0 Zonemaster::Engine->logger->add( CNAME_LOOP_INNER => { name => join( ';', map { $_->owner } @cname_rrs ), target => join( ';', map { $_->cname } @cname_rrs ) } );
229 0 return ( undef, $state );
230 }
231  
232 0 $seen_targets{$rr_target} = 1;
233 0 $forbidden_targets{$rr_owner} = 1;
234 0 $cnames{$rr_owner} = name( $rr->cname ); # Preserve case of $rr_target
235 }
236  
237 # Get final CNAME target
238 0 my $target = $name;
239 0 my $cname_counter = 0;
240 0 ▶ while ( $cnames{$target} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnames{$target}) 0 0 0
241 0 ▶ return ( undef, $state ) if $cname_counter > $CNAME_MAX_RECORDS; # Loop protection (for good measure only - data in %cnames is sanitized already)
Branch: 1 branch
BranchTrueFalseTotal
if $cname_counter > $Zonemaster::Engine::Recursor::CNAME_MAX_RECORDS 0 0 0
242 0 $target = $cnames{$target};
243 0 $cname_counter++;
244 }
245  
246 # Make sure that the CNAME chain from the RRs is not broken
247 0 ▶ if ( $cname_counter != scalar @cname_rrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cname_counter != scalar @cname_rrs) 0 0 0
248 0 Zonemaster::Engine->logger->add( CNAME_RECORDS_CHAIN_BROKEN => { name => $name, cname_rrs => scalar @cname_rrs, cname_counter => $cname_counter } );
249 0 return ( undef, $state );
250 }
251  
252 # CNAME target has already been followed previously (outer loop); no need to recurse
253 0 ▶ if ( exists $state->{in_progress}{lc( $target )}{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"in_progress"}{lc $target}{$type}) 0 0 0
254 0 Zonemaster::Engine->logger->add( CNAME_LOOP_OUTER => { name => $name, target => $target, targets_seen => join( ';', keys %{ $state->{tseen} } ) } );
255 0 return ( undef, $state );
256 }
257  
258 # Safeguard against abnormally long consecutive CNAME lookups; no need to recurse
259 0 $state->{tseen}{lc( $target )} = 1;
260 0 $state->{tcount} += 1;
261  
262 0 ▶ if ( $state->{tcount} > $CNAME_MAX_CHAIN_LENGTH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"tcount"} > $Zonemaster::Engine::Recursor::CNAME_MAX_CHAIN_LENGTH) 0 0 0
263 0 Zonemaster::Engine->logger->add( CNAME_CHAIN_TOO_LONG => { count => $state->{tcount}, max => $CNAME_MAX_CHAIN_LENGTH } );
264 0 return ( undef, $state );
265 }
266  
267 # Check if there are RRs of queried type (QTYPE) in the answer section of the response;
268 0 ▶ if ( scalar $p->get_records( $type, 'answer' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $p->get_records($type, "answer")) 0 0 0
269 # RR of type QTYPE for CNAME target is already in the response; no need to recurse
270 0 ▶ if ( $p->has_rrs_of_type_for_name( $type, $target ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->has_rrs_of_type_for_name($type, $target)) 0 0 0
271 0 Zonemaster::Engine->logger->add( CNAME_FOLLOWED_IN_ZONE => { name => $name, type => $type, target => $target } );
272 0 return ( $p, $state );
273 }
274  
275 # There is a record of type QTYPE but with different owner name than CNAME target; we have to recurse again
276 0 Zonemaster::Engine->logger->add( CNAME_NO_MATCH => { name => $name, type => $type, target => $target, owner_names => join( ';', map { $_->owner } $p->get_records( $type ) ) } );
277 }
278  
279 # CNAME target is not in the same zone as the CNAME owner name, so make a new recursive lookup
280 0 ▶ unless ( $name->is_in_bailiwick( $target ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->is_in_bailiwick($target)) { } 0 0 0
281 0 Zonemaster::Engine->logger->add( CNAME_FOLLOWED_OUT_OF_ZONE => { name => $name, target => $target } );
282 ( $p, $state ) = $class->_recurse( $target, $type, $dns_class,
283 0 { ns => [ root_servers() ], count => 0, common => 0, seen => {}, tseen => $state->{tseen}, tcount => $state->{tcount}, glue => {}, in_progress => $state->{in_progress} });
284 0 return ( $p, $state );
285 }
286 # Final attempt to resolve the CNAME target for in-domain names
287 else {
288 ( $p, $state ) = $class->_recurse( $target, $type, $dns_class,
289 0 { ns => [ root_servers() ], count => 0, common => 0, seen => {}, tseen => $state->{tseen}, tcount => $state->{tcount}, glue => {}, in_progress => $state->{in_progress} });
290  
291 0 ▶ if ( $p and $p->aa and $p->rcode eq 'NOERROR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p and $p->aa and $p->rcode eq "NOERROR") 0 0 0
Condition0%0/6
$p and $p->aa and $p->rcode eq "NOERROR"
!ll&&!rl&&r
000
$p and $p->aa
!ll&&!rl&&r
000
MC/DC0%0/3
$p and $p->aa and $p->rcode eq "NOERROR"
$p $p->aa $p->rcode eq "NOERROR"
Truth table0%0/4
A and B and C
A= $pB= $p->aaC= $p->rcode eq "NOERROR"
ABCresult
0XX0
10X0
1100
1111
292 0 Zonemaster::Engine->logger->add( CNAME_TO_NODATA => { name => $name, type => $type, target => $target } );
293 0 return ( $p, $state );
294 }
295 }
296  
297 # Catch-all; unforeseen problem in CNAME resolution
298 0 Zonemaster::Engine->logger->add( CNAME_UNRESOLVABLE => { name => $name, type => $type, target => $target } );
299 0 return ( undef, $state );
300 }
301  
302 0 ▶sub _recurse {
Subroutine _recurse: not called CC=20 SCAR=60.4
303 0 my ( $class, $name, $type, $dns_class, $state ) = @_;
304 0 $name = q{} . name( $name );
305 0 ▶ $state->{qname} //= $name;
Condition0%0/3
$state->{"qname"} //= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$state->{"qname"} //= $name
$state->{"qname"} $name
Truth table0%0/3
A //= B
A= $state->{"qname"}B= $name
ABresult
1X1
011
000
306  
307 0 ▶ if ( $state->{in_progress}{$name}{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"in_progress"}{$name}{$type}) 0 0 0
308 0 return;
309 }
310 0 $state->{in_progress}{$name}{$type} = 1;
311  
312 0 ▶ while ( my $ns = pop @{ $state->{ns} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = pop @{$$state{"ns"};}) 0 0 0
313 0 ▶ my $nsname = $ns->can( 'name' ) ? q{} . $ns->name : q{};
Branch: 1 branch
BranchTrueFalseTotal
$ns->can('name') ? : 0 0 0
314 0 ▶ my $nsaddress = $ns->can( 'address' ) ? $ns->address->ip : q{};
Branch: 1 branch
BranchTrueFalseTotal
$ns->can('address') ? : 0 0 0
315 0 Zonemaster::Engine->logger->add(
316 RECURSE_QUERY => {
317 source => "$ns",
318 ns => $nsname,
319 address => $nsaddress,
320 name => $name,
321 type => $type,
322 class => $dns_class,
323 }
324 );
325 0 my $p = $class->_do_query( $ns, $name, $type, { class => $dns_class }, $state );
326  
327 0 ▶ next if not $p; # Ask next server if no response
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 0 0
328  
329 0 ▶ if ( $p->rcode eq 'REFUSED' or $p->rcode eq 'SERVFAIL' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->rcode eq "REFUSED" or $p->rcode eq "SERVFAIL") 0 0 0
Condition0%0/3
$p->rcode eq "REFUSED" or $p->rcode eq "SERVFAIL"
l!l&&r!l&&!r
000
MC/DC0%0/2
$p->rcode eq "REFUSED" or $p->rcode eq "SERVFAIL"
$p->rcode eq "REFUSED" $p->rcode eq "SERVFAIL"
Truth table0%0/3
A or B
A= $p->rcode eq "REFUSED"B= $p->rcode eq "SERVFAIL"
ABresult
1X1
011
000
330 # Respond with these if we can't get a better response
331 0 $state->{candidate} = $p;
332 0 next;
333 }
334  
335 0 ▶ if ( $p->no_such_record ) { # Node exists, but not record
Branch: 1 branch
BranchTrueFalseTotal
if ($p->no_such_record) 0 0 0
336 0 return ( $p, $state );
337 }
338  
339 0 ▶ if ( $p->no_such_name ) { # Node does not exist
Branch: 1 branch
BranchTrueFalseTotal
if ($p->no_such_name) 0 0 0
340 0 return ( $p, $state );
341 }
342  
343 0 ▶ if ( $class->_is_answer( $p ) ) { # Return answer, or resolve CNAME
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_answer($p)) 0 0 0
344 0 ▶ if ( not $p->has_rrs_of_type_for_name( $type, $name ) and scalar $p->get_records_for_name( 'CNAME', $name, 'answer' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $p->has_rrs_of_type_for_name($type, $name) and scalar $p->get_records_for_name("CNAME", $name, "answer")) 0 0 0
Condition0%0/2
not $p->has_rrs_of_type_for_name($type, $name) and scalar $p->get_records_for_name("CNAME", $name, "answer")
!ll
00
MC/DC0%0/1
not $p->has_rrs_of_type_for_name($type, $name) and scalar $p->get_records_for_name("CNAME", $name, "answer")
not $p->has_rrs_of_type_for_name($type, $name)
Truth table0%0/2
not(A)
A= not $p->has_rrs_of_type_for_name($type, $name)
Aresult
00
11
345 0 ( $p, $state ) = $class->_resolve_cname( $name, $type, $dns_class, $p, $state );
346 }
347  
348 0 return ( $p, $state );
349 }
350  
351 # So it's not an error, not an empty response and not an answer
352  
353 0 ▶ if ( $p->is_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->is_redirect) 0 0 0
354 0 my $zname = name( lc( ( $p->get_records( 'ns' ) )[0]->name ) );
355  
356 0 ▶ next if $zname eq '.'; # Redirect to root is never right.
Branch: 1 branch
BranchTrueFalseTotal
if $zname eq "." 0 0 0
357  
358 0 ▶ next if $state->{seen}{$zname}; # We followed this redirect before
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"seen"}{$zname} 0 0 0
359  
360 0 $state->{seen}{$zname} = 1;
361 0 my $common = name( $zname )->common( name( $state->{qname} ) );
362  
363 0 ▶ next if $common < $state->{common}; # Redirect going up the hierarchy is not OK
Branch: 1 branch
BranchTrueFalseTotal
if $common < $state->{"common"} 0 0 0
364  
365 0 $state->{common} = $common;
366 0 $state->{ns} = $class->_get_ns_from( $p, $state ); # Follow redirect
367 0 $state->{count} += 1;
368 0 ▶ if ( $state->{count} > 20 ) { # Loop protection
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"count"} > 20) 0 0 0
369 0 Zonemaster::Engine->logger->add( LOOP_PROTECTION => {
370 caller => 'Zonemaster::Engine::Recursor->_recurse',
371 child_zone_name => $name,
372 name => $zname
373 }
374 );
375  
376 0 return ( undef, $state );
377 }
378 0 unshift @{ $state->{trace} }, [ $zname, $ns, $p->answerfrom ];
379  
380 0 next;
381 } ## end if ( $p->is_redirect )
382 } ## end while ( my $ns = pop @{ $state...})
383 0 ▶ return ( $state->{candidate}, $state ) if $state->{candidate};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"candidate"} 0 0 0
384  
385 0 return ( undef, $state );
386 } ## end sub _recurse
387  
388 0 ▶sub _do_query {
Subroutine _do_query: not called CC=12 SCAR=50.5
389 0 my ( $class, $ns, $name, $type, $opts, $state ) = @_;
390  
391 0 ▶ if ( ref( $ns ) and $ns->can( 'query' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ns and $ns->can('query')) { } 0 0 0
elsif (my $href = $state->{'glue'}{lc name($ns)}) { } 0 0 0
Condition0%0/3
ref $ns and $ns->can("query")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $ns and $ns->can("query")
ref $ns $ns->can("query")
Truth table0%0/3
A and B
A= ref $nsB= $ns->can("query")
ABresult
0X0
100
111
392 0 my $p = $ns->query( $name, $type, $opts );
393  
394 0 ▶ if ( $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 0 0 0
395 0 ▶ for my $rr ( grep { $_->type eq 'A' or $_->type eq 'AAAA' } $p->answer, $p->additional ) {
Condition0%0/3
$_->type eq 'A' || $_->type eq 'AAAA'
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->type eq 'A' || $_->type eq 'AAAA'
$_->type eq 'A' $_->type eq 'AAAA'
Truth table0%0/3
A || B
A= $_->type eq 'A'B= $_->type eq 'AAAA'
ABresult
1X1
011
000
396 0 $state->{glue}{ lc( Zonemaster::Engine::DNSName->from_string( $rr->name ) ) }{ $rr->address } = 1;
397 }
398 }
399 0 return $p;
400 }
401 elsif ( my $href = $state->{glue}{ lc( name( $ns ) ) } ) {
402 0 foreach my $addr ( keys %$href ) {
403 0 my $realns = ns( $ns, $addr );
404 0 my $p = $class->_do_query( $realns, $name, $type, $opts, $state );
405 0 ▶ if ( $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 0 0 0
406 0 return $p;
407 }
408 }
409 0 return;
410 }
411 else {
412 0 $state->{glue}{ lc( name( $ns ) ) } = {};
413 0 my @addr = $class->get_addresses_for( $ns, $state );
414 0 ▶ if ( @addr > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@addr > 0) { } 0 0 0
415 0 foreach my $addr ( @addr ) {
416 0 $state->{glue}{ lc( name( $ns ) ) }{ $addr->short } = 1;
417 0 my $new = ns( $ns, $addr->short );
418 0 my $p = $new->query( $name, $type, $opts );
419 0 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 0 0 0
420 }
421 0 return;
422 }
423 else {
424 0 return;
425 }
426 }
427 } ## end sub _do_query
428  
429 0 ▶sub _get_ns_from {
Subroutine _get_ns_from: not called CC=6 SCAR=37.4
430 0 my ( $class, $p, $state ) = @_;
431 0 my ( @new, @extra );
432  
433 0 my @names = sort map { Zonemaster::Engine::DNSName->from_string( lc( $_->nsdname ) ) } $p->get_records( 'ns' );
434  
435 $state->{glue}{ lc( Zonemaster::Engine::DNSName->from_string( $_->name ) ) }{ $_->address } = 1
436 0 for ( $p->get_records( 'a' ), $p->get_records( 'aaaa' ) );
437  
438 0 foreach my $name ( @names ) {
439 0 ▶ if ( exists $state->{glue}{ lc( $name ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{'glue'}{lc $name}) { } 0 0 0
440 0 for my $addr ( keys %{ $state->{glue}{ lc( $name ) } } ) {
441 0 push @new, ns( $name, $addr );
442 }
443 }
444 else {
445 0 push @extra, $name;
446 }
447 }
448  
449 0 ▶ @new = sort { $a->name cmp $b->name or $a->address->ip cmp $b->address->ip } @new;
Condition0%0/3
$a->name cmp $b->name || $a->address->ip cmp $b->address->ip
l!l&&r!l&&!r
000
MC/DC0%0/2
$a->name cmp $b->name || $a->address->ip cmp $b->address->ip
$a->name cmp $b->name $a->address->ip cmp $b->address->ip
Truth table0%0/3
A || B
A= $a->name cmp $b->nameB= $a->address->ip cmp $b->address->ip
ABresult
1X1
011
000
450 0 @extra = sort { $a cmp $b } @extra;
451  
452 0 return [ @new, @extra ];
453 } ## end sub _get_ns_from
454  
455 0 ▶sub get_addresses_for {
Subroutine get_addresses_for: not called CC=11 SCAR=48.8
Pod documented
456 0 my ( $class, $name, $state ) = @_;
457 0 my @res;
458 0 ▶ $state //=
Condition0%0/2
$state //= {"ns", [root_servers()], "count", 0, "common", 0, "seen", {}}
l!l
00
MC/DC0%0/1
$state //= {"ns", [root_servers()], "count", 0, "common", 0, "seen", {}}
$state
Truth table0%0/2
A
A= $state
Aresult
11
00
459 { ns => [ root_servers() ], count => 0, common => 0, seen => {} };
460  
461 my ( $pa ) = $class->_recurse(
462 "$name", 'A', 'IN',
463 {
464 ns => [ root_servers() ],
465 count => $state->{count},
466 common => 0,
467 in_progress => $state->{in_progress},
468 glue => $state->{glue}
469 }
470 0 );
471  
472 # Name does not exist, just stop
473 0 ▶ if ( $pa and $pa->no_such_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pa and $pa->no_such_name) 0 0 0
Condition0%0/3
$pa and $pa->no_such_name
!ll&&!rl&&r
000
MC/DC0%0/2
$pa and $pa->no_such_name
$pa $pa->no_such_name
Truth table0%0/3
A and B
A= $paB= $pa->no_such_name
ABresult
0X0
100
111
474 0 return;
475 }
476  
477 my ( $paaaa ) = $class->_recurse(
478 "$name", 'AAAA', 'IN',
479 {
480 ns => [ root_servers() ],
481 count => $state->{count},
482 common => 0,
483 in_progress => $state->{in_progress},
484 glue => $state->{glue}
485 }
486 0 );
487  
488 0 my @rrs;
489 my %cname;
490 0 ▶ if ( $pa ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pa) 0 0 0
491 0 push @rrs, $pa->get_records( 'a' );
492 0 $cname{ $_->cname } = 1 for $pa->get_records_for_name( 'CNAME', $name );
493 }
494 0 ▶ if ( $paaaa ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paaaa) 0 0 0
495 0 push @rrs, $paaaa->get_records( 'aaaa' );
496 0 $cname{ $_->cname } = 1 for $paaaa->get_records_for_name( 'CNAME', $name );
497 }
498  
499 0 foreach my $rr ( sort { $a->address cmp $b->address } @rrs ) {
500 0 ▶ if ( name( $rr->name ) eq $name or $cname{ $rr->name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (name($rr->name) eq $name or $cname{$rr->name}) 0 0 0
Condition0%0/3
name($rr->name) eq $name or $cname{$rr->name}
l!l&&r!l&&!r
000
MC/DC0%0/2
name($rr->name) eq $name or $cname{$rr->name}
name($rr->name) eq $name $cname{$rr->name}
Truth table0%0/3
A or B
A= name($rr->name) eq $nameB= $cname{$rr->name}
ABresult
1X1
011
000
501 0 push @res, Net::IP::XS->new( $rr->address );
502 }
503 }
504 0 return @res;
505 } ## end sub get_addresses_for
506  
507 0 ▶sub _is_answer {
Subroutine _is_answer: not called CC=1 SCAR=6.9
508 0 my ( $class, $packet ) = @_;
509  
510 0 return ( $packet->type eq 'answer' );
511 }
512  
513 0 ▶sub clear_cache {
Subroutine clear_cache: not called CC=1 SCAR=6.9
Pod documented
514 0 Memoize::flush_cache(\&recurse);
515 0 return;
516 }
517  
518 0 ▶sub root_servers {
Subroutine root_servers: not called CC=3 SCAR=24.8
Pod documented
519 0 my $root_addresses = $_fake_addresses_cache{'.'};
520  
521 0 my @servers;
522 0 for my $name ( sort keys %{ $root_addresses } ) {
523 0 for my $address ( @{ $root_addresses->{$name} } ) {
524 0 push @servers, ns( $name, $address );
525 }
526 }
527  
528 0 return @servers;
529 }
530  
531 1 1;
532  
533 =head1 NAME
534  
535 Zonemaster::Engine::Recursor - recursive resolver for Zonemaster
536  
537 =head1 SYNOPSIS
538  
539 my $packet = Zonemaster::Engine::Recursor->recurse( $name, $type, $dns_class );
540 my $pname = Zonemaster::Engine::Recursor->parent( 'example.org' );
541  
542 =head1 CLASS VARIABLES
543  
544 =head2 %_fake_addresses_cache
545  
546 A hash of hashrefs of arrayrefs.
547 The keys of the top level hash are domain names.
548 The keys of the second level hashes are name server names (normalized to lower
549 case).
550 The elements of the third level arrayrefs are IP addresses.
551  
552 The IP addresses are those of the nameservers which are used in case of fake
553 delegations (pre-publication tests).
554  
555 =head1 CLASS METHODS
556  
557 =head2 init_recursor()
558  
559 Initialize the recursor by loading the root hints.
560  
561 =head2 recurse($name[, $type, $class, $ns])
562  
563 Does a recursive resolution for the given name down from the root servers (or for the given name server(s), if any).
564 Only the first argument is mandatory. The rest are optional and default to, respectively: 'A', 'IN', and L</root_servers()>.
565  
566 Takes a string or a L<Zonemaster::Engine::DNSName> object (name); and optionally a string (query type), a string (query class),
567 and an arrayref of L<Zonemaster::Engine::Nameserver> objects.
568  
569 Returns a L<Zonemaster::Engine::Packet> object (which can be C<undef>).
570  
571 =head2 parent($name)
572  
573 Does a recursive resolution from the root down for the given name (using type C<SOA> and class C<IN>). If the resolution is successful, it returns
574 the domain name of the second-to-last step. If the resolution is unsuccessful, it returns the domain name of the last step.
575  
576 =head2 get_addresses_for($name[, $state])
577  
578 Takes a name and returns a (possibly empty) list of IP addresses for
579 that name (in the form of L<Net::IP::XS> objects). When used
580 internally by the recursor it's passed a recursion state as its second
581 argument.
582  
583 =head2 add_fake_addresses($domain, $data)
584  
585 Class method to create fake addresses for fake delegations for a specified domain from data provided.
586  
587 =head2 has_fake_addresses($domain)
588  
589 Check if there is at least one fake nameserver specified for the given domain.
590  
591 =head2 get_fake_addresses($domain, $nsname)
592  
593 Returns a list of all cached fake addresses for the given domain and name server name.
594 Returns an empty list if no data is cached for the given arguments.
595  
596 =head2 get_fake_names($domain)
597  
598 Returns a list of all cached fake name server names for the given domain.
599 Returns an empty list if no data is cached for the given argument.
600  
601 =head2 remove_fake_addresses($domain)
602  
603 Remove fake delegation data for a specified domain.
604  
605 =head2 clear_cache()
606  
607 Class method to empty the cache of responses to recursive queries (but not the ones for fake delegations).
608  
609 N.B. This method does not affect fake delegation data.
610  
611 =head2 root_servers()
612  
613 Returns a list of L<Zonemaster::Engine::Nameserver> objects representing the root servers.
614  
615 my @name_servers = Zonemaster::Engine::Recursor->root_servers();
616  
617 The default list of root servers is read from a file installed in the shared data directory.
618 This list can be replaced like so:
619  
620 Zonemaster::Engine::Recursor->remove_fake_addresses( '.' );
621 Zonemaster::Engine::Recursor->add_fake_addresses(
622 '.',
623 {
624 'ns1.example' => ['192.0.2.1'],
625 'ns2.example' => ['192.0.2.2'],
626 }
627 );
628  
629 =head1 INTERNAL METHODS
630  
631 =head2 _recurse()
632  
633 my ( $p, $state_hash ) = _recurse( $name, $type_string, $dns_class_string, $p, $state_hash );
634  
635 Performs a recursive lookup resolution for the given arguments. Used by the L<recursive lookup|/recurse($name[, $type, $class, $ns])> method in this module.
636  
637 Takes a L<Zonemaster::Engine::DNSName> object, a string (query type), a string (DNS class), a L<Zonemaster::Engine::Packet> object, and a reference to a hash.
638 The mandatory keys for that hash are 'ns' (arrayref), 'count' (integer), 'common' (integer), 'seen' (hash), 'glue' (hash) and optional keys are 'in_progress'
639 (hash), 'candidate' (L<Zonemaster::Engine::Packet> object or C<undef>), 'trace' (array), 'tseen' (hash), 'tcount' (integer).
640  
641 Returns a L<Zonemaster::Engine::Packet> (or C<undef>) and a hash.
642  
643 =head2 _get_ns_from()
644  
645 my @ns = _get_ns_from( $packet, $state );
646  
647 Used to follow redirections by the L<recursive lookup|/_recurse()> helper method in this module.
648  
649 Takes a L<Zonemaster::Engine::Packet> object and a reference to a hash.
650  
651 Returns a list of L<Zonemaster::Engine::Nameserver> objects.
652  
653 =head2 _resolve_cname()
654  
655 my ( $p, $state_hash ) = _resolve_cname( $name, $type_string, $dns_class_string, $p, $state_hash );
656  
657 Performs a CNAME resolution for the given arguments. Used by the L<recursive lookup|/_recurse()> helper method in this module.
658 If the CNAME resolution is successful, the first return value will be a L<packet|Zonemaster::Engine::Packet> (which could be C<undef>)
659 and at least one of the following message tags is logged:
660  
661 =over
662  
663 =item CNAME_FOLLOWED_IN_ZONE
664  
665 This message tag indicates that the CNAME target was found in the current response and has the requested record type,
666 so no additional recursion is needed.
667  
668 =item CNAME_FOLLOWED_OUT_OF_ZONE
669  
670 This message tag indicates that the CNAME target is outside the queried name's zone and is resolved through a new recursive lookup.
671  
672 =item CNAME_TO_NODATA
673  
674 This message tag indicates that the CNAME target was resolved authoritatively. The name exists but not with the requested record type.
675  
676 =back
677  
678 Note that the resolution has multiple validation steps and, in case of an error, the first return value will be C<undef> and at least
679 one of the following message tags is logged:
680  
681 =over
682  
683 =item CNAME_CHAIN_TOO_LONG
684  
685 This message tag indicates that the number of distinct lookups made to resolve a CNAME chain is longer than the maximum allowed length
686 with respect to L<$CNAME_MAX_CHAIN_LENGTH|Zonemaster::Engine::Constants/CNAME_MAX_CHAIN_LENGTH>.
687  
688 =item CNAME_LOOP_INNER
689  
690 This message tag indicates that there is a loop in the CNAME chain within the same zone.
691  
692 =item CNAME_LOOP_OUTER
693  
694 This message tag indicates that there is a loop in the CNAME chain across different zones.
695  
696 =item CNAME_RECORDS_CHAIN_BROKEN
697  
698 This message tag indicates that the CNAME chain in the current response is broken, i.e. that with multiple CNAME records in a response
699 there is at least one CNAME record with a target that does not point to a corresponding CNAME record in the same response.
700  
701 =item CNAME_RECORDS_MULTIPLE_FOR_NAME
702  
703 This message tag indicates that there are more than one CNAME record with the same owner name in the current response.
704  
705 =item CNAME_RECORDS_TOO_MANY
706  
707 This message tag indicates that there are too many, with respect to L<$CNAME_MAX_RECORDS|Zonemaster::Engine::Constants/CNAME_MAX_RECORDS>,
708 non-duplicate CNAME records in the current response.
709  
710 =item CNAME_UNRESOLVABLE
711  
712 This message tag indicates that the CNAME resolution could not be completed and is there as a catch-all to handle unforeseen problems
713 in the CNAME resolution process.
714  
715 =back
716  
717 Finally the following message tags can be also be logged alongside all of the above message tags:
718  
719 =over
720  
721 =item CNAME_RECORDS_DUPLICATES
722  
723 This message tag indicates that there are duplicate CNAME records in the current response.
724  
725 =item CNAME_NO_MATCH
726  
727 This message tag indicates that there is a record of the requested type in the answer section of the response but with a different owner name
728 than the CNAME target within that same response.
729  
730 =back
731  
732 Takes a L<Zonemaster::Engine::DNSName> object, a string (query type), a string (DNS class), a L<Zonemaster::Engine::Packet>, and a reference to a hash.
733  
734 Returns a L<Zonemaster::Engine::Packet> (or C<undef>) and a reference to a hash.
735  
736 =cut
« Zonemaster/Engine/Test/Zone.pm ↑ Summary Zonemaster/Engine/Test/Syntax.pm »