Zonemaster/Engine/Recursor.pm

filter: Statementstmt 15.2% 45 / 296 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 46.6% 14 / 30 Podpod 100% 12 / 12 totaltotal 14.2% 71 / 498 CC 84 SCAR 51.9
CC
84
Cov
10%
_recurse
420.0
_resolve_cname
380.0
_do_query
156.0
CRAP 178.9
« 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  
16 1 ▶use Zonemaster::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Zonemaster::Engine::DNSName;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Zonemaster::Engine::Util qw( name ns parse_hints );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Zonemaster::Engine::Constants ":cname";
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our %recurse_cache;
22 1 our %_fake_addresses_cache;
23  
24 sub init_recursor {
25 0 ▶ my $hints_path = dist_file( 'Zonemaster-Engine', 'named.root' );
Subroutine init_recursor: not called CC=1 SCAR=6.9
Pod documented
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 sub add_fake_addresses {
32 0 ▶ my ( $class, $domain, $href ) = @_;
Subroutine add_fake_addresses: not called CC=3 SCAR=24.8
Pod documented
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 sub has_fake_addresses {
49 0 ▶ my ( $class, $domain ) = @_;
Subroutine has_fake_addresses: not called CC=1 SCAR=6.9
Pod documented
50 0 $domain = lc $domain;
51  
52 0 return !!$_fake_addresses_cache{$domain};
53 }
54  
55 sub get_fake_addresses {
56 0 ▶ my ( $class, $domain, $nsname ) = @_;
Subroutine get_fake_addresses: not called CC=4 SCAR=30.0
Pod documented
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 sub get_fake_names {
71 0 ▶ my ( $class, $domain ) = @_;
Subroutine get_fake_names: not called CC=2 SCAR=17.9
Pod documented
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 sub remove_fake_addresses {
83 0 ▶ my ( $class, $domain ) = @_;
Subroutine remove_fake_addresses: not called CC=1 SCAR=6.9
Pod documented
84 0 $domain = lc $domain;
85  
86 0 delete $_fake_addresses_cache{$domain};
87  
88 0 return;
89 }
90  
91 sub recurse {
92 0 ▶ my ( $class, $name, $type, $dns_class, $ns ) = @_;
Subroutine recurse: not called CC=6 SCAR=37.4
Pod documented
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 0 ▶ if ( exists $recurse_cache{$name}{$type}{$dns_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Zonemaster::Engine::Recursor::recurse_cache{$name}{$type}{$dns_class}) 0 0 0
99 0 return $recurse_cache{$name}{$type}{$dns_class};
100 }
101  
102 0 my %state = ( ns => [ root_servers() ], count => 0, common => 0, seen => {}, glue => {} );
103 0 ▶ if ( defined $ns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns) 0 0 0
104 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
105 0 $state{ns} = $ns;
106 }
107  
108 0 my ( $p, $state ) = $class->_recurse( $name, $type, $dns_class, \%state );
109 0 $recurse_cache{$name}{$type}{$dns_class} = $p;
110  
111 0 return $p;
112 }
113  
114 sub parent {
115 0 ▶ my ( $class, $name ) = @_;
Subroutine parent: not called CC=8 SCAR=42.8
Pod documented
116 0 $name = name( $name );
117  
118 0 my ( $p, $state ) =
119 $class->_recurse( $name, 'SOA', 'IN',
120 { ns => [ root_servers() ], count => 0, common => 0, seen => {}, glue => {} } );
121  
122 0 my $pname;
123 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
124 0 $pname = name( $state->{trace}[1][0] );
125 }
126 else {
127 0 $pname = name( $state->{trace}[0][0] );
128 }
129  
130 # Extra check that parent really is parent.
131 0 ▶ if ( $name->next_higher ne $pname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->next_higher ne $pname) 0 0 0
132 0 my $source_ns = $state->{trace}[0][1];
133 0 my $source_ip = $state->{trace}[0][2];
134  
135 # No $source_ns means we're looking at root taken from priming
136 0 ▶ if ( $source_ns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_ns) 0 0 0
137 0 my $pp;
138 0 ▶ if ( $source_ns->can( 'query' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_ns->can('query')) { } 0 0 0
139 0 $pp = $source_ns->query( $name->next_higher->string, 'SOA' );
140 }
141 else {
142 0 my $n = ns( $source_ns, $source_ip );
143 0 $pp = $n->query( $name->next_higher->string, 'SOA' );
144 }
145 0 ▶ if ( $pp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pp) 0 0 0
146 0 my ( $rr ) = $pp->get_records( 'SOA', 'answer' );
147 0 ▶ if ( $rr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr) 0 0 0
148 0 $pname = name( $rr->owner );
149 }
150 }
151 }
152 } ## end if ( $name->next_higher...)
153  
154 0 ▶ if ( wantarray() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
155 0 return ( $pname, $p );
156 }
157 else {
158 0 return $pname;
159 }
160 } ## end sub parent
161  
162 sub _resolve_cname {
163 0 ▶ my ( $class, $name, $type, $dns_class, $p, $state ) = @_;
Subroutine _resolve_cname: not called CC=19 SCAR=59.4
164 0 $name = name( $name );
165 0 Zonemaster::Engine->logger->add( CNAME_START => { name => $name, type => $type, dns_class => $dns_class } );
166  
167 0 my @cname_rrs = $p->get_records( 'CNAME', 'answer' );
168  
169 # Remove duplicate CNAME RRs
170 0 my ( %duplicate_cname_rrs, @original_rrs );
171 0 for my $rr ( @cname_rrs ) {
172 0 my $rr_hash = $rr->class . '/CNAME/' . lc($rr->owner) . '/' . lc($rr->cname);
173  
174 0 ▶ if ( exists $duplicate_cname_rrs{$rr_hash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $duplicate_cname_rrs{$rr_hash}) { } 0 0 0
175 0 $duplicate_cname_rrs{$rr_hash}++;
176 }
177 else {
178 0 $duplicate_cname_rrs{$rr_hash} = 0;
179 0 push @original_rrs, $rr;
180 }
181 }
182  
183 0 ▶ unless ( scalar @original_rrs == scalar @cname_rrs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @original_rrs == scalar @cname_rrs) 0 0 0
184 Zonemaster::Engine->logger->add( CNAME_RECORDS_DUPLICATES => {
185 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
186 }
187 );
188 0 @cname_rrs = @original_rrs;
189 }
190  
191 # Break if there are too many records
192 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
193 0 Zonemaster::Engine->logger->add( CNAME_RECORDS_TOO_MANY => { name => $name, count => scalar @cname_rrs, max => $CNAME_MAX_RECORDS } );
194 0 return ( undef, $state );
195 }
196  
197 0 my ( %cnames, %seen_targets, %forbidden_targets );
198 0 for my $rr ( @cname_rrs ) {
199 0 my $rr_owner = name( $rr->owner );
200 0 my $rr_target = name( $rr->cname );
201  
202 # Multiple CNAME records with same owner name
203 0 ▶ if ( exists $forbidden_targets{lc( $rr_owner )} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $forbidden_targets{lc $rr_owner}) 0 0 0
204 0 Zonemaster::Engine->logger->add( CNAME_RECORDS_MULTIPLE_FOR_NAME => { name => $rr_owner } );
205 0 return ( undef, $state );
206 }
207  
208 # CNAME owner name is target, or target has already been seen in this response, or owner name cannot be a target
209 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
210 0 Zonemaster::Engine->logger->add( CNAME_LOOP_INNER => { name => join( ';', map { $_->owner } @cname_rrs ), target => join( ';', map { $_->cname } @cname_rrs ) } );
211 0 return ( undef, $state );
212 }
213  
214 0 $seen_targets{lc( $rr_target )} = 1;
215 0 $forbidden_targets{lc( $rr_owner )} = 1;
216 0 $cnames{$rr_owner} = $rr_target;
217 }
218  
219 # Get final CNAME target
220 0 my $target = $name;
221 0 my $cname_counter = 0;
222 0 ▶ while ( $cnames{$target} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnames{$target}) 0 0 0
223 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
224 0 $target = $cnames{$target};
225 0 $cname_counter++;
226 }
227  
228 # Make sure that the CNAME chain from the RRs is not broken
229 0 ▶ if ( $cname_counter != scalar @cname_rrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cname_counter != scalar @cname_rrs) 0 0 0
230 0 Zonemaster::Engine->logger->add( CNAME_RECORDS_CHAIN_BROKEN => { name => $name, cname_rrs => scalar @cname_rrs, cname_counter => $cname_counter } );
231 0 return ( undef, $state );
232 }
233  
234 # Check if there are RRs of queried type (QTYPE) in the answer section of the response;
235 0 ▶ if ( scalar $p->get_records( $type, 'answer' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $p->get_records($type, "answer")) 0 0 0
236 # RR of type QTYPE for CNAME target is already in the response; no need to recurse
237 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
238 0 Zonemaster::Engine->logger->add( CNAME_FOLLOWED_IN_ZONE => { name => $name, type => $type, target => $target } );
239 0 return ( $p, $state );
240 }
241  
242 # There is a record of type QTYPE but with different owner name than CNAME target; no need to recurse
243 0 Zonemaster::Engine->logger->add( CNAME_NO_MATCH => { name => $name, type => $type, target => $target, owner_names => join( ';', map { $_->owner } $p->get_records( $type ) ) } );
244 0 return ( undef, $state );
245 }
246  
247 # CNAME target has already been followed (outer loop); no need to recurse
248 0 ▶ if ( exists $state->{in_progress}{lc( $target )}{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"in_progress"}{lc $target}{$type}) 0 0 0
249 0 Zonemaster::Engine->logger->add( CNAME_LOOP_OUTER => { name => $name, target => $target, targets_seen => join( ';', keys %{ $state->{tseen} } ) } );
250 0 return ( undef, $state );
251 }
252  
253 # Safe-guard against anormaly long consecutive CNAME chains; no need to recurse
254 0 $state->{tseen}{lc( $target )} = 1;
255 0 $state->{tcount} += 1;
256  
257 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
258 0 Zonemaster::Engine->logger->add( CNAME_CHAIN_TOO_LONG => { count => $state->{tcount}, max => $CNAME_MAX_CHAIN_LENGTH } );
259 0 return ( undef, $state );
260 }
261  
262 # Make sure that the CNAME target is out of zone before making a new recursive lookup for CNAME target
263 0 ▶ unless ( $name->is_in_bailiwick( $target ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->is_in_bailiwick($target)) { } 0 0 0
264 0 Zonemaster::Engine->logger->add( CNAME_FOLLOWED_OUT_OF_ZONE => { name => $name, target => $target } );
265 ( $p, $state ) = $class->_recurse( $target, $type, $dns_class,
266 0 { ns => [ root_servers() ], count => 0, common => 0, seen => {}, tseen => $state->{tseen}, tcount => $state->{tcount}, glue => {}, in_progress => $state->{in_progress} });
267 }
268 else {
269 # What do do here?
270 }
271  
272 0 return ( $p, $state );
273 }
274  
275 sub _recurse {
276 0 ▶ my ( $class, $name, $type, $dns_class, $state ) = @_;
Subroutine _recurse: not called CC=20 SCAR=60.4
277 0 $name = q{} . name( $name );
278 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
279  
280 0 ▶ if ( $state->{in_progress}{$name}{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"in_progress"}{$name}{$type}) 0 0 0
281 0 return;
282 }
283 0 $state->{in_progress}{$name}{$type} = 1;
284  
285 0 ▶ while ( my $ns = pop @{ $state->{ns} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = pop @{$$state{"ns"};}) 0 0 0
286 0 ▶ my $nsname = $ns->can( 'name' ) ? q{} . $ns->name : q{};
Branch: 1 branch
BranchTrueFalseTotal
$ns->can('name') ? : 0 0 0
287 0 ▶ my $nsaddress = $ns->can( 'address' ) ? $ns->address->ip : q{};
Branch: 1 branch
BranchTrueFalseTotal
$ns->can('address') ? : 0 0 0
288 0 Zonemaster::Engine->logger->add(
289 RECURSE_QUERY => {
290 source => "$ns",
291 ns => $nsname,
292 address => $nsaddress,
293 name => $name,
294 type => $type,
295 class => $dns_class,
296 }
297 );
298 0 my $p = $class->_do_query( $ns, $name, $type, { class => $dns_class }, $state );
299  
300 0 ▶ next if not $p; # Ask next server if no response
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 0 0
301  
302 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
303 # Respond with these if we can't get a better response
304 0 $state->{candidate} = $p;
305 0 next;
306 }
307  
308 0 ▶ if ( $p->no_such_record ) { # Node exists, but not record
Branch: 1 branch
BranchTrueFalseTotal
if ($p->no_such_record) 0 0 0
309 0 return ( $p, $state );
310 }
311  
312 0 ▶ if ( $p->no_such_name ) { # Node does not exist
Branch: 1 branch
BranchTrueFalseTotal
if ($p->no_such_name) 0 0 0
313 0 return ( $p, $state );
314 }
315  
316 0 ▶ if ( $class->_is_answer( $p ) ) { # Return answer, or resolve CNAME
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_answer($p)) 0 0 0
317 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
318 0 ( $p, $state ) = $class->_resolve_cname( $name, $type, $dns_class, $p, $state );
319 }
320  
321 0 return ( $p, $state );
322 }
323  
324 # So it's not an error, not an empty response and not an answer
325  
326 0 ▶ if ( $p->is_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->is_redirect) 0 0 0
327 0 my $zname = name( lc( ( $p->get_records( 'ns' ) )[0]->name ) );
328  
329 0 ▶ next if $zname eq '.'; # Redirect to root is never right.
Branch: 1 branch
BranchTrueFalseTotal
if $zname eq "." 0 0 0
330  
331 0 ▶ next if $state->{seen}{$zname}; # We followed this redirect before
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"seen"}{$zname} 0 0 0
332  
333 0 $state->{seen}{$zname} = 1;
334 0 my $common = name( $zname )->common( name( $state->{qname} ) );
335  
336 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
337  
338 0 $state->{common} = $common;
339 0 $state->{ns} = $class->get_ns_from( $p, $state ); # Follow redirect
340 0 $state->{count} += 1;
341 0 ▶ if ( $state->{count} > 20 ) { # Loop protection
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"count"} > 20) 0 0 0
342 0 Zonemaster::Engine->logger->add( LOOP_PROTECTION => {
343 caller => 'Zonemaster::Engine::Recursor->_recurse',
344 child_zone_name => $name,
345 name => $zname
346 }
347 );
348  
349 0 return ( undef, $state );
350 }
351 0 unshift @{ $state->{trace} }, [ $zname, $ns, $p->answerfrom ];
352  
353 0 next;
354 } ## end if ( $p->is_redirect )
355 } ## end while ( my $ns = pop @{ $state...})
356 0 ▶ return ( $state->{candidate}, $state ) if $state->{candidate};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"candidate"} 0 0 0
357  
358 0 return ( undef, $state );
359 } ## end sub _recurse
360  
361 sub _do_query {
362 0 ▶ my ( $class, $ns, $name, $type, $opts, $state ) = @_;
Subroutine _do_query: not called CC=12 SCAR=50.5
363  
364 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
365 0 my $p = $ns->query( $name, $type, $opts );
366  
367 0 ▶ if ( $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 0 0 0
368 0 ▶ for my $rr ( grep { $_->type eq 'A' or $_->type eq 'AAAA' } $p->answer, $p->additional ) {
Branch: 1 branch
BranchTrueFalseTotal
$_->type eq "A" or $_->type eq "AAAA" 0 0 0
369 0 $state->{glue}{ lc( Zonemaster::Engine::DNSName->from_string( $rr->name ) ) }{ $rr->address } = 1;
370 }
371 }
372 0 return $p;
373 }
374 elsif ( my $href = $state->{glue}{ lc( name( $ns ) ) } ) {
375 0 foreach my $addr ( keys %$href ) {
376 0 my $realns = ns( $ns, $addr );
377 0 my $p = $class->_do_query( $realns, $name, $type, $opts, $state );
378 0 ▶ if ( $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 0 0 0
379 0 return $p;
380 }
381 }
382 0 return;
383 }
384 else {
385 0 $state->{glue}{ lc( name( $ns ) ) } = {};
386 0 my @addr = $class->get_addresses_for( $ns, $state );
387 0 ▶ if ( @addr > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@addr > 0) { } 0 0 0
388 0 foreach my $addr ( @addr ) {
389 0 $state->{glue}{ lc( name( $ns ) ) }{ $addr->short } = 1;
390 0 my $new = ns( $ns, $addr->short );
391 0 my $p = $new->query( $name, $type, $opts );
392 0 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 0 0 0
393 }
394 0 return;
395 }
396 else {
397 0 return;
398 }
399 }
400 } ## end sub _do_query
401  
402 sub get_ns_from {
403 0 ▶ my ( $class, $p, $state ) = @_;
Subroutine get_ns_from: not called CC=6 SCAR=37.4
Pod documented
404 0 my ( @new, @extra );
405  
406 0 my @names = sort map { Zonemaster::Engine::DNSName->from_string( lc( $_->nsdname ) ) } $p->get_records( 'ns' );
407  
408 $state->{glue}{ lc( Zonemaster::Engine::DNSName->from_string( $_->name ) ) }{ $_->address } = 1
409 0 for ( $p->get_records( 'a' ), $p->get_records( 'aaaa' ) );
410  
411 0 foreach my $name ( @names ) {
412 0 ▶ if ( exists $state->{glue}{ lc( $name ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{'glue'}{lc $name}) { } 0 0 0
413 0 for my $addr ( keys %{ $state->{glue}{ lc( $name ) } } ) {
414 0 push @new, ns( $name, $addr );
415 }
416 }
417 else {
418 0 push @extra, $name;
419 }
420 }
421  
422 0 ▶ @new = sort { $a->name cmp $b->name or $a->address->ip cmp $b->address->ip } @new;
Branch: 1 branch
BranchTrueFalseTotal
$a->name cmp $b->name or $a->address->ip cmp $b->address->ip 0 0 0
423 0 @extra = sort { $a cmp $b } @extra;
424  
425 0 return [ @new, @extra ];
426 } ## end sub get_ns_from
427  
428 sub get_addresses_for {
429 0 ▶ my ( $class, $name, $state ) = @_;
Subroutine get_addresses_for: not called CC=11 SCAR=48.8
Pod documented
430 0 my @res;
431 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
432 { ns => [ root_servers() ], count => 0, common => 0, seen => {} };
433  
434 my ( $pa ) = $class->_recurse(
435 "$name", 'A', 'IN',
436 {
437 ns => [ root_servers() ],
438 count => $state->{count},
439 common => 0,
440 in_progress => $state->{in_progress},
441 glue => $state->{glue}
442 }
443 0 );
444  
445 # Name does not exist, just stop
446 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
447 0 return;
448 }
449  
450 my ( $paaaa ) = $class->_recurse(
451 "$name", 'AAAA', 'IN',
452 {
453 ns => [ root_servers() ],
454 count => $state->{count},
455 common => 0,
456 in_progress => $state->{in_progress},
457 glue => $state->{glue}
458 }
459 0 );
460  
461 0 my @rrs;
462 my %cname;
463 0 ▶ if ( $pa ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pa) 0 0 0
464 0 push @rrs, $pa->get_records( 'a' );
465 0 $cname{ $_->cname } = 1 for $pa->get_records_for_name( 'CNAME', $name );
466 }
467 0 ▶ if ( $paaaa ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paaaa) 0 0 0
468 0 push @rrs, $paaaa->get_records( 'aaaa' );
469 0 $cname{ $_->cname } = 1 for $paaaa->get_records_for_name( 'CNAME', $name );
470 }
471  
472 0 foreach my $rr ( sort { $a->address cmp $b->address } @rrs ) {
473 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
474 0 push @res, Net::IP::XS->new( $rr->address );
475 }
476 }
477 0 return @res;
478 } ## end sub get_addresses_for
479  
480 sub _is_answer {
481 0 ▶ my ( $class, $packet ) = @_;
Subroutine _is_answer: not called CC=1 SCAR=6.9
482  
483 0 return ( $packet->type eq 'answer' );
484 }
485  
486 sub clear_cache {
487 0 ▶ %recurse_cache = ();
Subroutine clear_cache: not called CC=1 SCAR=6.9
Pod documented
488 0 return;
489 }
490  
491 sub root_servers {
492 0 ▶ my $root_addresses = $_fake_addresses_cache{'.'};
Subroutine root_servers: not called CC=3 SCAR=24.8
Pod documented
493  
494 0 my @servers;
495 0 for my $name ( sort keys %{ $root_addresses } ) {
496 0 for my $address ( @{ $root_addresses->{$name} } ) {
497 0 push @servers, ns( $name, $address );
498 }
499 }
500  
501 0 return @servers;
502 }
503  
504 1 1;
505  
506 =head1 NAME
507  
508 Zonemaster::Engine::Recursor - recursive resolver for Zonemaster
509  
510 =head1 SYNOPSIS
511  
512 my $packet = Zonemaster::Engine::Recursor->recurse( $name, $type, $dns_class );
513 my $pname = Zonemaster::Engine::Recursor->parent( 'example.org' );
514  
515 =head1 CLASS VARIABLES
516  
517 =head2 %recurse_cache
518  
519 Will cache result of previous queries.
520  
521 =head2 %_fake_addresses_cache
522  
523 A hash of hashrefs of arrayrefs.
524 The keys of the top level hash are domain names.
525 The keys of the second level hashes are name server names (normalized to lower
526 case).
527 The elements of the third level arrayrefs are IP addresses.
528  
529 The IP addresses are those of the nameservers which are used in case of fake
530 delegations (pre-publication tests).
531  
532 =head1 CLASS METHODS
533  
534 =head2 init_recursor()
535  
536 Initialize the recursor by loading the root hints.
537  
538 =head2 recurse($name[, $type, $class, $ns])
539  
540 Does a recursive resolution for the given name down from the root servers (or for the given name server(s), if any).
541 Only the first argument is mandatory. The rest are optional and default to, respectively: 'A', 'IN', and L</root_servers()>.
542  
543 Takes a string or a L<Zonemaster::Engine::DNSName> object (name); and optionally a string (query type), a string (query class),
544 and an arrayref of L<Zonemaster::Engine::Nameserver> objects.
545  
546 Returns a L<Zonemaster::Engine::Packet> object (which can be C<undef>).
547  
548 =head2 parent($name)
549  
550 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
551 the domain name of the second-to-last step. If the resolution is unsuccessful, it returns the domain name of the last step.
552  
553 =head2 get_ns_from($packet, $state)
554  
555 Internal method. Takes a packet and a recursion state and returns a list of ns objects. Used to follow redirections.
556  
557 =head2 get_addresses_for($name[, $state])
558  
559 Takes a name and returns a (possibly empty) list of IP addresses for
560 that name (in the form of L<Net::IP::XS> objects). When used
561 internally by the recursor it's passed a recursion state as its second
562 argument.
563  
564 =head2 add_fake_addresses($domain, $data)
565  
566 Class method to create fake addresses for fake delegations for a specified domain from data provided.
567  
568 =head2 has_fake_addresses($domain)
569  
570 Check if there is at least one fake nameserver specified for the given domain.
571  
572 =head2 get_fake_addresses($domain, $nsname)
573  
574 Returns a list of all cached fake addresses for the given domain and name server name.
575 Returns an empty list if no data is cached for the given arguments.
576  
577 =head2 get_fake_names($domain)
578  
579 Returns a list of all cached fake name server names for the given domain.
580 Returns an empty list if no data is cached for the given argument.
581  
582 =head2 remove_fake_addresses($domain)
583  
584 Remove fake delegation data for a specified domain.
585  
586 =head2 clear_cache()
587  
588 Class method to empty the cache of responses to recursive queries (but not the ones for fake delegations).
589  
590 N.B. This method does not affect fake delegation data.
591  
592 =head2 root_servers()
593  
594 Returns a list of ns objects representing the root servers.
595  
596 my @name_servers = Zonemaster::Engine::Recursor->root_servers();
597  
598 The default list of root servers is read from a file installed in the shared data directory.
599 This list can be replaced like so:
600  
601 Zonemaster::Engine::Recursor->remove_fake_addresses( '.' );
602 Zonemaster::Engine::Recursor->add_fake_addresses(
603 '.',
604 {
605 'ns1.example' => ['192.0.2.1'],
606 'ns2.example' => ['192.0.2.2'],
607 }
608 );
609  
610 =head1 INTERNAL METHODS
611  
612 =head2 _recurse()
613  
614 my ( $p, $state_hash ) = _recurse( $name, $type_string, $dns_class_string, $p, $state_hash );
615  
616 Performs a recursive lookup resolution for the given arguments. Used by the L<recursive lookup|/recurse($name, $type, $class)> method in this module.
617  
618 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.
619 The mandatory keys for that hash are 'ns' (arrayref), 'count' (integer), 'common' (integer), 'seen' (hash), 'glue' (hash) and optional keys are 'in_progress'
620 (hash), 'candidate' (L<Zonemaster::Engine::Packet> object or C<undef>), 'trace' (array), 'tseen' (hash), 'tcount' (integer).
621  
622 Returns a L<Zonemaster::Engine::Packet> (or C<undef>) and a hash.
623  
624 =head2 _resolve_cname()
625  
626 my ( $p, $state_hash ) = _resolve_cname( $name, $type_string, $dns_class_string, $p, $state_hash );
627  
628 Performs CNAME resolution for the given arguments. Used by the L<recursive lookup|/_recurse()> helper method in this module.
629 If CNAMEs are successfully resolved, a L<packet|Zonemaster::Engine::Packet> (which could be C<undef>) is returned and
630 one of the following message tags is logged:
631  
632 =over
633  
634 =item CNAME_FOLLOWED_IN_ZONE
635  
636 =item CNAME_FOLLOWED_OUT_OF_ZONE
637  
638 =back
639  
640 Note that CNAME records are also validated and, in case of an error, an empty (C<undef>) L<packet|Zonemaster::Engine::Packet>
641 is returned and one of the following message tags will be logged:
642  
643 =over
644  
645 =item CNAME_CHAIN_TOO_LONG
646  
647 =item CNAME_LOOP_INNER
648  
649 =item CNAME_LOOP_OUTER
650  
651 =item CNAME_NO_MATCH
652  
653 =item CNAME_RECORDS_CHAIN_BROKEN
654  
655 =item CNAME_RECORDS_MULTIPLE_FOR_NAME
656  
657 =item CNAME_RECORDS_TOO_MANY
658  
659 =back
660  
661 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.
662  
663 Returns a L<Zonemaster::Engine::Packet> (or C<undef>) and a reference to a hash.
664  
665 =cut
« Zonemaster/Engine/Test/Zone.pm ↑ Summary Zonemaster/Engine/Test/Syntax.pm »