Zonemaster/Engine/Recursor.pm

filter: Statementstmt 15.8% 48 / 302 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 46.8% 15 / 32 Podpod 100% 10 / 10 totaltotal 13.9% 73 / 524 CC 88 SCAR 51.4
CC
88
Cov
11%
_recurse
420.0
_resolve_cname
380.0
_do_query
156.0
CRAP 171.1
« 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=19 SCAR=59.4
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 = name( $rr->owner );
218 0 my $rr_target = name( $rr->cname );
219  
220 # Multiple CNAME records with same owner name
221 0 ▶ if ( exists $forbidden_targets{lc( $rr_owner )} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $forbidden_targets{lc $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 ( lc( $rr_owner ) eq lc( $rr_target ) or exists $seen_targets{lc( $rr_target )} or grep { $_ eq lc( $rr_target ) } ( keys %forbidden_targets ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $rr_owner eq lc $rr_target or exists $seen_targets{lc $rr_target} or grep {$_ eq lc $rr_target;} keys %forbidden_targets) 0 0 0
Condition0%0/6
lc $rr_owner eq lc $rr_target or exists $seen_targets{lc $rr_target} or grep {$_ eq lc $rr_target;} keys %forbidden_targets
l!l&&r!l&&!r
000
lc $rr_owner eq lc $rr_target or exists $seen_targets{lc $rr_target}
l!l&&r!l&&!r
000
MC/DC0%0/3
lc $rr_owner eq lc $rr_target or exists $seen_targets{lc $rr_target} or grep {$_ eq lc $rr_target;} keys %forbidden_targets
lc $rr_owner eq lc $rr_target exists $seen_targets{lc $rr_target} grep {$_ eq lc $rr_target;} keys %forbidden_targets
Truth table0%0/4
A or B or C
A= lc $rr_owner eq lc $rr_targetB= exists $seen_targets{lc $rr_target}C= grep {$_ eq lc $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{lc( $rr_target )} = 1;
233 0 $forbidden_targets{lc( $rr_owner )} = 1;
234 0 $cnames{$rr_owner} = $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 # Check if there are RRs of queried type (QTYPE) in the answer section of the response;
253 0 ▶ if ( scalar $p->get_records( $type, 'answer' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $p->get_records($type, "answer")) 0 0 0
254 # RR of type QTYPE for CNAME target is already in the response; no need to recurse
255 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
256 0 Zonemaster::Engine->logger->add( CNAME_FOLLOWED_IN_ZONE => { name => $name, type => $type, target => $target } );
257 0 return ( $p, $state );
258 }
259  
260 # There is a record of type QTYPE but with different owner name than CNAME target; no need to recurse
261 0 Zonemaster::Engine->logger->add( CNAME_NO_MATCH => { name => $name, type => $type, target => $target, owner_names => join( ';', map { $_->owner } $p->get_records( $type ) ) } );
262 0 return ( undef, $state );
263 }
264  
265 # CNAME target has already been followed (outer loop); no need to recurse
266 0 ▶ if ( exists $state->{in_progress}{lc( $target )}{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"in_progress"}{lc $target}{$type}) 0 0 0
267 0 Zonemaster::Engine->logger->add( CNAME_LOOP_OUTER => { name => $name, target => $target, targets_seen => join( ';', keys %{ $state->{tseen} } ) } );
268 0 return ( undef, $state );
269 }
270  
271 # Safe-guard against anormaly long consecutive CNAME chains; no need to recurse
272 0 $state->{tseen}{lc( $target )} = 1;
273 0 $state->{tcount} += 1;
274  
275 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
276 0 Zonemaster::Engine->logger->add( CNAME_CHAIN_TOO_LONG => { count => $state->{tcount}, max => $CNAME_MAX_CHAIN_LENGTH } );
277 0 return ( undef, $state );
278 }
279  
280 # Make sure that the CNAME target is out of zone before making a new recursive lookup for CNAME target
281 0 ▶ unless ( $name->is_in_bailiwick( $target ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->is_in_bailiwick($target)) { } 0 0 0
282 0 Zonemaster::Engine->logger->add( CNAME_FOLLOWED_OUT_OF_ZONE => { name => $name, target => $target } );
283 ( $p, $state ) = $class->_recurse( $target, $type, $dns_class,
284 0 { ns => [ root_servers() ], count => 0, common => 0, seen => {}, tseen => $state->{tseen}, tcount => $state->{tcount}, glue => {}, in_progress => $state->{in_progress} });
285 }
286 else {
287 # What do do here?
288 }
289  
290 0 return ( $p, $state );
291 }
292  
293 0 ▶sub _recurse {
Subroutine _recurse: not called CC=20 SCAR=60.4
294 0 my ( $class, $name, $type, $dns_class, $state ) = @_;
295 0 $name = q{} . name( $name );
296 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
297  
298 0 ▶ if ( $state->{in_progress}{$name}{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"in_progress"}{$name}{$type}) 0 0 0
299 0 return;
300 }
301 0 $state->{in_progress}{$name}{$type} = 1;
302  
303 0 ▶ while ( my $ns = pop @{ $state->{ns} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = pop @{$$state{"ns"};}) 0 0 0
304 0 ▶ my $nsname = $ns->can( 'name' ) ? q{} . $ns->name : q{};
Branch: 1 branch
BranchTrueFalseTotal
$ns->can('name') ? : 0 0 0
305 0 ▶ my $nsaddress = $ns->can( 'address' ) ? $ns->address->ip : q{};
Branch: 1 branch
BranchTrueFalseTotal
$ns->can('address') ? : 0 0 0
306 0 Zonemaster::Engine->logger->add(
307 RECURSE_QUERY => {
308 source => "$ns",
309 ns => $nsname,
310 address => $nsaddress,
311 name => $name,
312 type => $type,
313 class => $dns_class,
314 }
315 );
316 0 my $p = $class->_do_query( $ns, $name, $type, { class => $dns_class }, $state );
317  
318 0 ▶ next if not $p; # Ask next server if no response
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 0 0
319  
320 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
321 # Respond with these if we can't get a better response
322 0 $state->{candidate} = $p;
323 0 next;
324 }
325  
326 0 ▶ if ( $p->no_such_record ) { # Node exists, but not record
Branch: 1 branch
BranchTrueFalseTotal
if ($p->no_such_record) 0 0 0
327 0 return ( $p, $state );
328 }
329  
330 0 ▶ if ( $p->no_such_name ) { # Node does not exist
Branch: 1 branch
BranchTrueFalseTotal
if ($p->no_such_name) 0 0 0
331 0 return ( $p, $state );
332 }
333  
334 0 ▶ if ( $class->_is_answer( $p ) ) { # Return answer, or resolve CNAME
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_answer($p)) 0 0 0
335 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
336 0 ( $p, $state ) = $class->_resolve_cname( $name, $type, $dns_class, $p, $state );
337 }
338  
339 0 return ( $p, $state );
340 }
341  
342 # So it's not an error, not an empty response and not an answer
343  
344 0 ▶ if ( $p->is_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->is_redirect) 0 0 0
345 0 my $zname = name( lc( ( $p->get_records( 'ns' ) )[0]->name ) );
346  
347 0 ▶ next if $zname eq '.'; # Redirect to root is never right.
Branch: 1 branch
BranchTrueFalseTotal
if $zname eq "." 0 0 0
348  
349 0 ▶ next if $state->{seen}{$zname}; # We followed this redirect before
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"seen"}{$zname} 0 0 0
350  
351 0 $state->{seen}{$zname} = 1;
352 0 my $common = name( $zname )->common( name( $state->{qname} ) );
353  
354 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
355  
356 0 $state->{common} = $common;
357 0 $state->{ns} = $class->_get_ns_from( $p, $state ); # Follow redirect
358 0 $state->{count} += 1;
359 0 ▶ if ( $state->{count} > 20 ) { # Loop protection
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"count"} > 20) 0 0 0
360 0 Zonemaster::Engine->logger->add( LOOP_PROTECTION => {
361 caller => 'Zonemaster::Engine::Recursor->_recurse',
362 child_zone_name => $name,
363 name => $zname
364 }
365 );
366  
367 0 return ( undef, $state );
368 }
369 0 unshift @{ $state->{trace} }, [ $zname, $ns, $p->answerfrom ];
370  
371 0 next;
372 } ## end if ( $p->is_redirect )
373 } ## end while ( my $ns = pop @{ $state...})
374 0 ▶ return ( $state->{candidate}, $state ) if $state->{candidate};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"candidate"} 0 0 0
375  
376 0 return ( undef, $state );
377 } ## end sub _recurse
378  
379 0 ▶sub _do_query {
Subroutine _do_query: not called CC=12 SCAR=50.5
380 0 my ( $class, $ns, $name, $type, $opts, $state ) = @_;
381  
382 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
383 0 my $p = $ns->query( $name, $type, $opts );
384  
385 0 ▶ if ( $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 0 0 0
386 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
387 0 $state->{glue}{ lc( Zonemaster::Engine::DNSName->from_string( $rr->name ) ) }{ $rr->address } = 1;
388 }
389 }
390 0 return $p;
391 }
392 elsif ( my $href = $state->{glue}{ lc( name( $ns ) ) } ) {
393 0 foreach my $addr ( keys %$href ) {
394 0 my $realns = ns( $ns, $addr );
395 0 my $p = $class->_do_query( $realns, $name, $type, $opts, $state );
396 0 ▶ if ( $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 0 0 0
397 0 return $p;
398 }
399 }
400 0 return;
401 }
402 else {
403 0 $state->{glue}{ lc( name( $ns ) ) } = {};
404 0 my @addr = $class->get_addresses_for( $ns, $state );
405 0 ▶ if ( @addr > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@addr > 0) { } 0 0 0
406 0 foreach my $addr ( @addr ) {
407 0 $state->{glue}{ lc( name( $ns ) ) }{ $addr->short } = 1;
408 0 my $new = ns( $ns, $addr->short );
409 0 my $p = $new->query( $name, $type, $opts );
410 0 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 0 0 0
411 }
412 0 return;
413 }
414 else {
415 0 return;
416 }
417 }
418 } ## end sub _do_query
419  
420 0 ▶sub _get_ns_from {
Subroutine _get_ns_from: not called CC=6 SCAR=37.4
421 0 my ( $class, $p, $state ) = @_;
422 0 my ( @new, @extra );
423  
424 0 my @names = sort map { Zonemaster::Engine::DNSName->from_string( lc( $_->nsdname ) ) } $p->get_records( 'ns' );
425  
426 $state->{glue}{ lc( Zonemaster::Engine::DNSName->from_string( $_->name ) ) }{ $_->address } = 1
427 0 for ( $p->get_records( 'a' ), $p->get_records( 'aaaa' ) );
428  
429 0 foreach my $name ( @names ) {
430 0 ▶ if ( exists $state->{glue}{ lc( $name ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{'glue'}{lc $name}) { } 0 0 0
431 0 for my $addr ( keys %{ $state->{glue}{ lc( $name ) } } ) {
432 0 push @new, ns( $name, $addr );
433 }
434 }
435 else {
436 0 push @extra, $name;
437 }
438 }
439  
440 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
441 0 @extra = sort { $a cmp $b } @extra;
442  
443 0 return [ @new, @extra ];
444 } ## end sub _get_ns_from
445  
446 0 ▶sub get_addresses_for {
Subroutine get_addresses_for: not called CC=11 SCAR=48.8
Pod documented
447 0 my ( $class, $name, $state ) = @_;
448 0 my @res;
449 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
450 { ns => [ root_servers() ], count => 0, common => 0, seen => {} };
451  
452 my ( $pa ) = $class->_recurse(
453 "$name", 'A', 'IN',
454 {
455 ns => [ root_servers() ],
456 count => $state->{count},
457 common => 0,
458 in_progress => $state->{in_progress},
459 glue => $state->{glue}
460 }
461 0 );
462  
463 # Name does not exist, just stop
464 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
465 0 return;
466 }
467  
468 my ( $paaaa ) = $class->_recurse(
469 "$name", 'AAAA', 'IN',
470 {
471 ns => [ root_servers() ],
472 count => $state->{count},
473 common => 0,
474 in_progress => $state->{in_progress},
475 glue => $state->{glue}
476 }
477 0 );
478  
479 0 my @rrs;
480 my %cname;
481 0 ▶ if ( $pa ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pa) 0 0 0
482 0 push @rrs, $pa->get_records( 'a' );
483 0 $cname{ $_->cname } = 1 for $pa->get_records_for_name( 'CNAME', $name );
484 }
485 0 ▶ if ( $paaaa ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paaaa) 0 0 0
486 0 push @rrs, $paaaa->get_records( 'aaaa' );
487 0 $cname{ $_->cname } = 1 for $paaaa->get_records_for_name( 'CNAME', $name );
488 }
489  
490 0 foreach my $rr ( sort { $a->address cmp $b->address } @rrs ) {
491 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
492 0 push @res, Net::IP::XS->new( $rr->address );
493 }
494 }
495 0 return @res;
496 } ## end sub get_addresses_for
497  
498 0 ▶sub _is_answer {
Subroutine _is_answer: not called CC=1 SCAR=6.9
499 0 my ( $class, $packet ) = @_;
500  
501 0 return ( $packet->type eq 'answer' );
502 }
503  
504 0 ▶sub clear_cache {
Subroutine clear_cache: not called CC=1 SCAR=6.9
Pod documented
505 0 Memoize::flush_cache(\&recurse);
506 0 return;
507 }
508  
509 0 ▶sub root_servers {
Subroutine root_servers: not called CC=3 SCAR=24.8
Pod documented
510 0 my $root_addresses = $_fake_addresses_cache{'.'};
511  
512 0 my @servers;
513 0 for my $name ( sort keys %{ $root_addresses } ) {
514 0 for my $address ( @{ $root_addresses->{$name} } ) {
515 0 push @servers, ns( $name, $address );
516 }
517 }
518  
519 0 return @servers;
520 }
521  
522 1 1;
523  
524 =head1 NAME
525  
526 Zonemaster::Engine::Recursor - recursive resolver for Zonemaster
527  
528 =head1 SYNOPSIS
529  
530 my $packet = Zonemaster::Engine::Recursor->recurse( $name, $type, $dns_class );
531 my $pname = Zonemaster::Engine::Recursor->parent( 'example.org' );
532  
533 =head1 CLASS VARIABLES
534  
535 =head2 %_fake_addresses_cache
536  
537 A hash of hashrefs of arrayrefs.
538 The keys of the top level hash are domain names.
539 The keys of the second level hashes are name server names (normalized to lower
540 case).
541 The elements of the third level arrayrefs are IP addresses.
542  
543 The IP addresses are those of the nameservers which are used in case of fake
544 delegations (pre-publication tests).
545  
546 =head1 CLASS METHODS
547  
548 =head2 init_recursor()
549  
550 Initialize the recursor by loading the root hints.
551  
552 =head2 recurse($name[, $type, $class, $ns])
553  
554 Does a recursive resolution for the given name down from the root servers (or for the given name server(s), if any).
555 Only the first argument is mandatory. The rest are optional and default to, respectively: 'A', 'IN', and L</root_servers()>.
556  
557 Takes a string or a L<Zonemaster::Engine::DNSName> object (name); and optionally a string (query type), a string (query class),
558 and an arrayref of L<Zonemaster::Engine::Nameserver> objects.
559  
560 Returns a L<Zonemaster::Engine::Packet> object (which can be C<undef>).
561  
562 =head2 parent($name)
563  
564 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
565 the domain name of the second-to-last step. If the resolution is unsuccessful, it returns the domain name of the last step.
566  
567 =head2 get_addresses_for($name[, $state])
568  
569 Takes a name and returns a (possibly empty) list of IP addresses for
570 that name (in the form of L<Net::IP::XS> objects). When used
571 internally by the recursor it's passed a recursion state as its second
572 argument.
573  
574 =head2 add_fake_addresses($domain, $data)
575  
576 Class method to create fake addresses for fake delegations for a specified domain from data provided.
577  
578 =head2 has_fake_addresses($domain)
579  
580 Check if there is at least one fake nameserver specified for the given domain.
581  
582 =head2 get_fake_addresses($domain, $nsname)
583  
584 Returns a list of all cached fake addresses for the given domain and name server name.
585 Returns an empty list if no data is cached for the given arguments.
586  
587 =head2 get_fake_names($domain)
588  
589 Returns a list of all cached fake name server names for the given domain.
590 Returns an empty list if no data is cached for the given argument.
591  
592 =head2 remove_fake_addresses($domain)
593  
594 Remove fake delegation data for a specified domain.
595  
596 =head2 clear_cache()
597  
598 Class method to empty the cache of responses to recursive queries (but not the ones for fake delegations).
599  
600 N.B. This method does not affect fake delegation data.
601  
602 =head2 root_servers()
603  
604 Returns a list of L<Zonemaster::Engine::Nameserver> objects representing the root servers.
605  
606 my @name_servers = Zonemaster::Engine::Recursor->root_servers();
607  
608 The default list of root servers is read from a file installed in the shared data directory.
609 This list can be replaced like so:
610  
611 Zonemaster::Engine::Recursor->remove_fake_addresses( '.' );
612 Zonemaster::Engine::Recursor->add_fake_addresses(
613 '.',
614 {
615 'ns1.example' => ['192.0.2.1'],
616 'ns2.example' => ['192.0.2.2'],
617 }
618 );
619  
620 =head1 INTERNAL METHODS
621  
622 =head2 _recurse()
623  
624 my ( $p, $state_hash ) = _recurse( $name, $type_string, $dns_class_string, $p, $state_hash );
625  
626 Performs a recursive lookup resolution for the given arguments. Used by the L<recursive lookup|/recurse($name, $type, $class)> method in this module.
627  
628 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.
629 The mandatory keys for that hash are 'ns' (arrayref), 'count' (integer), 'common' (integer), 'seen' (hash), 'glue' (hash) and optional keys are 'in_progress'
630 (hash), 'candidate' (L<Zonemaster::Engine::Packet> object or C<undef>), 'trace' (array), 'tseen' (hash), 'tcount' (integer).
631  
632 Returns a L<Zonemaster::Engine::Packet> (or C<undef>) and a hash.
633  
634 =head2 _get_ns_from()
635  
636 my @ns = _get_ns_from( $packet, $state );
637  
638 Used to follow redirections by the L<recursive lookup|/_recurse()> helper method in this module.
639  
640 Takes a L<Zonemaster::Engine::Packet> object and a reference to a hash.
641  
642 Returns a list of L<Zonemaster::Engine::Nameserver> objects.
643  
644 =head2 _resolve_cname()
645  
646 my ( $p, $state_hash ) = _resolve_cname( $name, $type_string, $dns_class_string, $p, $state_hash );
647  
648 Performs CNAME resolution for the given arguments. Used by the L<recursive lookup|/_recurse()> helper method in this module.
649 If CNAMEs are successfully resolved, a L<packet|Zonemaster::Engine::Packet> (which could be C<undef>) is returned and
650 one of the following message tags is logged:
651  
652 =over
653  
654 =item CNAME_FOLLOWED_IN_ZONE
655  
656 =item CNAME_FOLLOWED_OUT_OF_ZONE
657  
658 =back
659  
660 Note that CNAME records are also validated and, in case of an error, an empty (C<undef>) L<packet|Zonemaster::Engine::Packet>
661 is returned and one of the following message tags will be logged:
662  
663 =over
664  
665 =item CNAME_CHAIN_TOO_LONG
666  
667 =item CNAME_LOOP_INNER
668  
669 =item CNAME_LOOP_OUTER
670  
671 =item CNAME_NO_MATCH
672  
673 =item CNAME_RECORDS_CHAIN_BROKEN
674  
675 =item CNAME_RECORDS_MULTIPLE_FOR_NAME
676  
677 =item CNAME_RECORDS_TOO_MANY
678  
679 =back
680  
681 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.
682  
683 Returns a L<Zonemaster::Engine::Packet> (or C<undef>) and a reference to a hash.
684  
685 =cut
« Zonemaster/Engine/Test/Zone.pm ↑ Summary Zonemaster/Engine/Test/Syntax.pm »