Zonemaster/Engine/TestMethodsV2.pm untested

filter: Statementstmt 0.0% 0 / 382 Branchbran 0.0% 0 / 152 Conditioncond 0.0% 0 / 147 MC/DCmcdc 0.0% 0 / 84 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 801 CC 80 SCAR 56.7
CC
80
Cov
0%
get_parent_ns_names_and_ips
870.0
_get_oob_ips
110.0
_get_delegation
110.0
CRAP 291.5
1 package Zonemaster::Engine::TestMethodsV2;
2  
3 use v5.26.0;
4 use warnings;
5  
6 use version; our $VERSION = version->declare("v1.0.0");
7  
8 use Carp;
9 use List::MoreUtils qw[uniq];
10 use Memoize;
11  
12 use Zonemaster::Engine::NameserverSet;
13 use Zonemaster::Engine::Util;
14  
15 =head1 NAME
16  
17 Zonemaster::Engine::TestMethodsV2 - Version 2 of Methods common to Test Specifications used in Test modules
18  
19 =head1 SYNOPSIS
20  
21 my @results = Zonemaster::Engine::TestMethodsV2->get_parent_ns_ips($zone);
22  
23 =head1 METHODS
24  
25 For details on what these Methods implement, see the Test Specifications document
26 (https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/MethodsV2.md).
27  
28 =over
29  
30 =item get_parent_ns_names_and_ips($zone)
31  
32 [External]
33  
34 This Method obtains the name server names and IP addresses that serve the parent zone, i.e. the zone from which the Child Zone is delegated.
35  
36 Takes a L<Zonemaster::Engine::Zone> object.
37  
38 Returns an empty arrayref if C<$zone> is the root zone or if an undelegated test is in progress. Else, returns an arrayref of L<Zonemaster::Engine::Nameserver> objects, or C<undef> if no parent zone was found.
39  
40 The result of this Method is cached for performance reasons. This cache can be invalidated by calling C<clear_cache()> if necessary.
41  
42 =back
43  
44 =cut
45  
46 sub get_parent_ns_names_and_ips {
47 my ( $class, $zone ) = @_;
48  
49 my $is_undelegated = Zonemaster::Engine::Recursor->has_fake_addresses( $zone->name->string );
50  
51 if ( $zone->name->string eq "." or $is_undelegated ) {
52 return [];
53 }
54  
55 my %handled_servers;
56 my @parent_ns;
57  
58 my %rrs_ns;
59 my $type_soa = q{SOA};
60 my $type_ns = q{NS};
61  
62 my %remaining_servers = ( '.' => [ Zonemaster::Engine::Recursor->root_servers ] );
63  
64 my sub push_to_remaining_servers {
65 my ( $ns, $zone_name ) = @_;
66  
67 unless ( exists $handled_servers{$zone_name}{"$ns"} ) {
68 unless ( grep { $_ eq $ns } @{ $remaining_servers{$zone_name} } ) {
69 push @{ $remaining_servers{$zone_name} }, $ns;
70 }
71 }
72 }
73  
74  
75 while ( my $zone_name = ( sort keys %remaining_servers )[0] ) {
76 CUR_SERVERS:
77 while ( my $ns = shift @{ $remaining_servers{$zone_name} } ) {
78 my $addr = $ns->address->short;
79 if ( exists $handled_servers{$zone_name}{"$ns"} ) {
80 push @parent_ns, $ns if ( grep { $_->address->short eq $addr and $_ ne $ns } @parent_ns );
81 next CUR_SERVERS;
82 }
83 $handled_servers{$zone_name}{"$ns"} = 1;
84  
85 if ( ( $ns->address->version == 4 and not Zonemaster::Engine::Profile->effective->get( q{net.ipv4} ) )
86 or ( $ns->address->version == 6 and not Zonemaster::Engine::Profile->effective->get( q{net.ipv6} ) ) ) {
87 next CUR_SERVERS;
88 }
89  
90 my $p_soa = $ns->query( $zone_name, $type_soa );
91  
92 unless ( $p_soa and $p_soa->rcode eq 'NOERROR' and $p_soa->aa and scalar $p_soa->get_records_for_name( $type_soa, $zone_name, 'answer' ) == 1 ) {
93 next CUR_SERVERS;
94 }
95  
96 my $p_ns = $ns->query( $zone_name, $type_ns );
97  
98 unless ( $p_ns and $p_ns->rcode eq 'NOERROR' and $p_ns->aa and scalar $p_ns->get_records( $type_ns, 'answer' ) > 0
99 and scalar $p_ns->get_records( $type_ns, 'answer' ) == scalar $p_ns->get_records_for_name( $type_ns, $zone_name, 'answer' )
100 ) {
101 next CUR_SERVERS;
102 }
103  
104 %rrs_ns = map { name( $_->nsdname )->string => [] } $p_ns->get_records_for_name( $type_ns, $zone_name, 'answer' );
105  
106 foreach my $rr ( $p_ns->get_records( 'A', 'additional' ), $p_ns->get_records( 'AAAA', 'additional' ) ) {
107 if ( exists $rrs_ns{name( $rr->owner )->string} ) {
108 push @{ $rrs_ns{name( $rr->owner )->string} }, $rr->address;
109 }
110 }
111  
112 foreach my $ns_name ( keys %rrs_ns ) {
113 unless ( scalar @{ $rrs_ns{$ns_name} } ) {
114 for my $qtype ( q{A}, q{AAAA} ) {
115 my $p = Zonemaster::Engine::Recursor->recurse( $ns_name, $qtype );
116  
117 if ( $p and $p->rcode eq 'NOERROR' ) {
118 push @{ $rrs_ns{$ns_name} }, $_->address for $p->get_records_for_name( $qtype, $ns_name );
119 }
120 }
121 }
122  
123 foreach my $ns_ip ( @{ $rrs_ns{$ns_name} } ) {
124 push_to_remaining_servers ns( $ns_name, $ns_ip ), $zone_name;
125 }
126 }
127  
128 my $intermediate_query_name = name( $zone_name );
129 my $loop_zone_name = $zone_name;
130 my $loop_counter = 0;
131  
132 LOOP:
133 while() {
134 $loop_counter += 1;
135 if ( $loop_counter >= 1000 ) {
136 Zonemaster::Engine->logger->add( LOOP_PROTECTION => {
137 caller => 'Zonemaster::Engine::TestMethodsV2->get_parent_ns_ips',
138 child_zone_name => $zone->name,
139 name => $loop_zone_name,
140 intermediate_query_name => $intermediate_query_name
141 }
142 );
143  
144 return undef;
145 }
146  
147 last if scalar @{ $intermediate_query_name->labels } >= scalar @{ $zone->name->labels };
148 $intermediate_query_name = name( @{ $zone->name->labels }[ ( scalar @{ $zone->name->labels } - scalar @{ $intermediate_query_name->labels } ) - 1 ] . '.' . $intermediate_query_name->string );
149  
150 $p_soa = $ns->query( $intermediate_query_name, $type_soa );
151  
152 unless ( $p_soa ) {
153 next CUR_SERVERS;
154 }
155  
156 if ( $p_soa->rcode eq 'NOERROR' and $p_soa->aa and scalar $p_soa->get_records_for_name( $type_soa, $intermediate_query_name, 'answer' ) == 1 ) {
157 if ( $intermediate_query_name->string eq $zone->name->string ) {
158 push @parent_ns, $ns;
159 }
160 else {
161 $p_ns = $ns->query( $intermediate_query_name, $type_ns );
162  
163 unless ( $p_ns and $p_ns->rcode eq 'NOERROR' and $p_ns->aa and scalar $p_ns->get_records( $type_ns, 'answer' ) > 0
164 and scalar $p_ns->get_records( $type_ns, 'answer' ) == scalar $p_ns->get_records_for_name( $type_ns, $intermediate_query_name, 'answer' )
165 ) {
166 next CUR_SERVERS;
167 }
168  
169 my %rrs_ns_bis = map { name( $_->nsdname )->string => [] } $p_ns->get_records_for_name( $type_ns, $intermediate_query_name, 'answer' );
170  
171 foreach my $rr ( $p_ns->get_records( 'A', 'additional' ), $p_ns->get_records( 'AAAA', 'additional' ) ) {
172 if ( exists $rrs_ns_bis{name( $rr->owner )->string} ) {
173 push @{ $rrs_ns_bis{name( $rr->owner )->string} }, $rr->address;
174 }
175 }
176  
177 foreach my $ns_name ( keys %rrs_ns_bis ) {
178 unless ( scalar @{ $rrs_ns_bis{$ns_name} } > 0 ) {
179 for my $qtype ( q{A}, q{AAAA} ) {
180 my $p = Zonemaster::Engine::Recursor->recurse( $ns_name, $qtype );
181  
182 if ( $p and $p->rcode eq 'NOERROR' ) {
183 push @{ $rrs_ns_bis{$ns_name} }, $_->address for $p->get_records_for_name( $qtype, $ns_name );
184 }
185 }
186 }
187  
188 foreach my $ns_ip ( @{ $rrs_ns_bis{$ns_name} } ) {
189 push_to_remaining_servers ns( $ns_name, $ns_ip ), $intermediate_query_name;
190 }
191 }
192  
193 $loop_zone_name = $intermediate_query_name->string;
194 next LOOP;
195 }
196 }
197 elsif ( $p_soa->is_redirect and scalar $p_soa->get_records_for_name( $type_ns, $intermediate_query_name, 'authority' ) ) {
198 if ( $intermediate_query_name->string eq $zone->name->string ) {
199 push @parent_ns, $ns;
200 }
201 else {
202 my %rrs_ns_bis = map { name( $_->nsdname )->string => [] } $p_soa->get_records_for_name( $type_ns, $intermediate_query_name, 'authority' );
203  
204 foreach my $rr ( $p_soa->get_records( 'A', 'additional' ), $p_soa->get_records( 'AAAA', 'additional' ) ) {
205 if ( exists $rrs_ns_bis{name( $rr->owner )->string} ) {
206 push @{ $rrs_ns_bis{name( $rr->owner )->string} }, $rr->address;
207 }
208 }
209  
210 foreach my $ns_name ( keys %rrs_ns_bis ) {
211 unless ( scalar @{ $rrs_ns_bis{$ns_name} } > 0 ) {
212 for my $qtype ( q{A}, q{AAAA} ) {
213 my $p = Zonemaster::Engine::Recursor->recurse( $ns_name, $qtype );
214  
215 if ( $p and $p->rcode eq 'NOERROR' ) {
216 push @{ $rrs_ns_bis{$ns_name} }, $_->address for $p->get_records_for_name( $qtype, $ns_name );
217 }
218 }
219 }
220  
221 foreach my $ns_ip ( @{ $rrs_ns_bis{$ns_name} } ) {
222 push_to_remaining_servers ns( $ns_name, $ns_ip ), $intermediate_query_name;
223 }
224 }
225 }
226 }
227 elsif ( $p_soa->rcode eq 'NOERROR' and $p_soa->aa ) {
228 next LOOP if $intermediate_query_name->string ne $zone->name->string;
229 }
230  
231 next CUR_SERVERS;
232 }
233 }
234  
235 delete $remaining_servers{$zone_name};
236 }
237  
238 if ( scalar @parent_ns ) {
239 return [ uniq sort @parent_ns ]
240 }
241 else {
242 return undef;
243 }
244 }
245  
246 # Memoize get_parent_ns_names_and_ips() because it is expensive and gets called a few
247 # times with identical parameters.
248  
249 memoize('get_parent_ns_names_and_ips',
250 NORMALIZER => sub {
251 my ( $class, $zone ) = @_;
252 join "\034", ( $class, $zone->name );
253 });
254  
255 =over
256  
257 =item get_parent_ns_ips($zone)
258  
259 [External]
260  
261 This Method obtains the name servers that serve the parent zone, i.e. the zone from which the Child Zone is delegated. If more than one name server share the same IP address, only one among them is kept.
262  
263 Takes a L<Zonemaster::Engine::Zone> object.
264  
265 Returns an empty arrayref if C<$zone> is the root zone or if an undelegated test is in progress. Else, returns an arrayref of L<Zonemaster::Engine::Nameserver> objects, or C<undef> if no parent zone was found.
266  
267 =back
268  
269 =cut
270  
271 sub get_parent_ns_ips {
272 my ( $class, $zone ) = @_;
273  
274 # FIXME: We really should just be outputting name server IPs here, as the
275 # specification says. Instead we output name server objects (but filtered
276 # on unique IP addresses) because these objects are required to perform
277 # queries.
278  
279 my $nameservers = $class->get_parent_ns_names_and_ips( $zone );
280  
281 return undef unless defined $nameservers;
282  
283 my %ns_by_ip = ();
284 foreach my $ns ( @$nameservers ) {
285 my $ip = $ns->address->short;
286 $ns_by_ip{$ip} = $ns unless exists $ns_by_ip{$ip};
287 }
288  
289 return [ sort values %ns_by_ip ];
290 }
291  
292 =over
293  
294 =item _get_oob_ips($zone, $ns_names_ref)
295  
296 [Internal]
297  
298 This Method will obtain the IP addresses of the Out-Of-Bailiwick name servers for the given zone and a given set of name server names.
299  
300 Takes a L<Zonemaster::Engine::Zone> object and an arrayref of L<Zonemaster::Engine::Nameserver> objects.
301  
302 Returns an arrayref of L<Zonemaster::Engine::Nameserver> objects for each name server name that was successfully resolved to an IP address,
303 and L<Zonemaster::Engine::DNSName> objects for each name server name that could not be resolved to an IP address.
304  
305 =back
306  
307 =cut
308  
309 sub _get_oob_ips {
310 my ( $class, $zone, $ns_names_ref ) = @_;
311  
312 unless ( defined $ns_names_ref and scalar @{ $ns_names_ref } ) {
313 return [];
314 }
315  
316 my $is_undelegated = Zonemaster::Engine::Recursor->has_fake_addresses( $zone->name->string );
317 my @oob_ns;
318 my $found_ip;
319  
320 for my $ns_name ( @{ $ns_names_ref } ) {
321 $found_ip = 0;
322  
323 unless ( $zone->name->is_in_bailiwick( $ns_name ) ) {
324 if ( $is_undelegated and scalar Zonemaster::Engine::Recursor->get_fake_addresses( $zone->name->string, $ns_name->string ) ) {
325 for my $ip ( Zonemaster::Engine::Recursor->get_fake_addresses( $zone->name->string, $ns_name->string ) ) {
326 push @oob_ns, ns( $ns_name->string, $ip );
327 $found_ip = 1;
328 }
329 }
330 else {
331 for my $qtype ( q{A}, q{AAAA} ) {
332 my $p = Zonemaster::Engine::Recursor->recurse( $ns_name, $qtype );
333  
334 if ( $p and $p->rcode eq q{NOERROR} ) {
335 if ( $p->has_rrs_of_type_for_name( q{CNAME}, $ns_name, q{answer} ) ) {
336 my %cnames = map { name( $_->owner ) => name( $_->cname ) } $p->get_records( q{CNAME}, q{answer} );
337 my $target = $ns_name;
338 $target = $cnames{$target} while $cnames{$target};
339  
340 for my $rr ( $p->get_records_for_name( $qtype, $target ) ) {
341 push @oob_ns, ns( $ns_name, $rr->address );
342 $found_ip = 1;
343 }
344 }
345 # CNAME was followed in a new recursive query
346 elsif ( name( ($p->question)[0]->owner ) ne $ns_name and grep { $_->tag eq 'CNAME_FOLLOWED_OUT_OF_ZONE' and grep /^$ns_name$/, values %{ $_->args } } @{ Zonemaster::Engine->logger->entries } ) {
347 my $cname_ns_name = name( ($p->question)[0]->owner );
348 my $target = $cname_ns_name;
349  
350 if ( $p->has_rrs_of_type_for_name( q{CNAME}, $cname_ns_name, q{answer} ) ) {
351 my %cnames = map { name( $_->owner ) => name( $_->cname ) } $p->get_records( q{CNAME}, q{answer} );
352 $target = $cnames{$target} while $cnames{$target};
353 }
354  
355 for my $rr ( $p->get_records_for_name( $qtype, $target ) ) {
356 push @oob_ns, ns( $ns_name, $rr->address );
357 $found_ip = 1;
358 }
359 }
360 elsif ( $p->has_rrs_of_type_for_name( $qtype, $ns_name ) ) {
361 for my $rr ( $p->get_records_for_name( $qtype, $ns_name ) ) {
362 push @oob_ns, ns( $ns_name, $rr->address );
363 $found_ip = 1;
364 }
365 }
366 }
367 }
368 }
369  
370 push @oob_ns, $ns_name unless $found_ip;
371 }
372 }
373  
374 return [ uniq sort @oob_ns ];
375 }
376  
377 =over
378  
379 =item _get_delegation($zone)
380  
381 [Internal]
382  
383 This Method will obtain the name server names (from the NS records) and the IP addresses (from Glue records) from the delegation of the given zone from the parent zone.
384 Glue Records are address records for In-Bailiwick name server names, if any.
385  
386 Takes a L<Zonemaster::Engine::Zone> object.
387  
388 Returns an arrayref of L<Zonemaster::Engine::Nameserver> objects, or C<undef> if no parent zone was found.
389  
390 =back
391  
392 =cut
393  
394 sub _get_delegation {
395 my ( $class, $zone ) = @_;
396  
397 # Takes the names from NS resource records from a packet ($p)’s section ($ns_section), plus the
398 # address records from the additional section belonging to those NS names that are in-domain
399 # with respect to the zone name, and returns the combined names and possibly IPs as a
400 # Zonemaster::Engine::NameserverSet.
401 my sub packet_ns_names_and_in_domain_ips {
402 my ( $p, $ns_section ) = @_;
403  
404 # Set of NS names from $ns_section ('authority' or 'additional') section
405 my %ns_names = map { lc name( $_->nsdname() ) => 1 }
406 $p->get_records_for_name( 'NS', $zone, $ns_section );
407  
408 # List of A/AAAA records from additional section for NS names
409 # that are in-domain wrt. the zone name
410 my @additional =
411 map {
412 my $name = name( $_->owner() );
413  
414 # Filters out A and AAAA records with invalid IP addresses
415 my $addr = Net::IP::XS->new( $_->address );
416 ( defined $addr ) ? ns( $name, $addr ) : ()
417 }
418 grep {
419 my $name = name( lc $_->owner() );
420 exists $ns_names{$name} and
421 $zone->name->is_in_bailiwick($name) and
422 ( $_->type eq 'A' or $_->type eq 'AAAA' )
423 }
424 $p->additional;
425  
426 return Zonemaster::Engine::NameserverSet->new( keys %ns_names, @additional );
427 }
428  
429 my $is_undelegated = Zonemaster::Engine::Recursor->has_fake_addresses( $zone->name->string );
430 my $result = Zonemaster::Engine::NameserverSet->new();
431  
432 if ( $is_undelegated ) {
433 for my $ns_name ( Zonemaster::Engine::Recursor->get_fake_names( $zone->name->string ) ) {
434 my @ns_ips = Zonemaster::Engine::Recursor->get_fake_addresses( $zone->name->string, $ns_name );
435 if ( $zone->name->is_in_bailiwick( name( $ns_name ) ) and scalar @ns_ips ) {
436 $result->push( map { ns( $ns_name, $_ ) } @ns_ips );
437 }
438 else {
439 $result->push( name( $ns_name ) );
440 }
441 }
442 }
443 elsif ( $zone->name->string eq '.' ) {
444 return [ uniq sort Zonemaster::Engine::Recursor->root_servers() ];
445 }
446 else {
447 my $parent_ref = $class->get_parent_ns_ips( $zone );
448  
449 return undef unless defined $parent_ref;
450  
451 for my $ns ( @{ $parent_ref } ) {
452 my $p = $ns->query( $zone->name, q{NS} );
453  
454 next unless $p and $p->rcode eq q{NOERROR};
455  
456 if ( $p->is_redirect ) {
457 $result->push( packet_ns_names_and_in_domain_ips( $p, 'authority' )->items() );
458 }
459 elsif ( $p->aa and scalar $p->get_records_for_name( q{NS}, $zone->name->string, q{answer} ) ) {
460 $result->push( packet_ns_names_and_in_domain_ips( $p, 'answer' )->items() );
461  
462 for my $ns_name ( grep { not $result->get_ips( $_ ) } $result->names() ) {
463 for my $qtype ( q{A}, q{AAAA} ) {
464 my $p = Zonemaster::Engine::Recursor->recurse( $ns_name, $qtype );
465  
466 next unless $p and $p->rcode eq q{NOERROR};
467  
468 # Follow possible CNAME chain starting from the QNAME in the answer packet.
469 # That QNAME might be different from $ns_name if, to follow the CNAME chain
470 # to the end, the recursor had to restart the query multiple times. This can
471 # happen if CNAME chains cross zones.
472 my %cnames = map {
473 lc name( $_->owner ) => lc name( $_->cname )
474 } $p->get_records( q{CNAME}, q{answer} );
475  
476 my $target = name( ($p->question)[0]->owner );
477 $target = $cnames{$target} while exists $cnames{$target};
478  
479 $result->push(
480 map { ns( $ns_name, $_->address ) }
481 $p->get_records_for_name( $qtype, $target ) );
482 }
483 }
484 }
485 }
486 }
487  
488 return [ $result->items() ];
489 }
490  
491 =over
492  
493 =item get_del_ns_names_and_ips($zone)
494  
495 [External]
496  
497 This Method will obtain the name server names (from the NS records) and the IP addresses (from Glue Records) from the delegation of the given zone from the parent zone.
498 Glue Records, if any, are address records for name server names. Also obtain the IP addresses for the Out-Of-Bailiwick name server names, if any.
499 If the Glue Records include address records for Out-Of-Bailiwick name servers they will be included twice, unless identical.
500  
501 Takes a L<Zonemaster::Engine::Zone> object.
502  
503 Returns an arrayref of L<Zonemaster::Engine::Nameserver> objects for each name server name that was successfully resolved to an IP address,
504 and L<Zonemaster::Engine::DNSName> objects for each name server name that could not be resolved to an IP address, or C<undef> if no parent zone was found.
505  
506 =back
507  
508 =cut
509  
510 sub get_del_ns_names_and_ips {
511 my ( $class, $zone ) = @_;
512  
513 my $ns_ref = $class->_get_delegation( $zone );
514  
515 return undef unless defined $ns_ref;
516  
517 my @ns_names = grep { $_->isa('Zonemaster::Engine::DNSName') and ! $zone->name->is_in_bailiwick( $_ ) } @{ $ns_ref };
518  
519 my $oob_ns_ref = $class->_get_oob_ips( $zone, \@ns_names );
520  
521 return [ Zonemaster::Engine::NameserverSet->new( @$ns_ref, @$oob_ns_ref )->items() ];
522 }
523  
524 =over
525  
526 =item get_del_ns_names($zone)
527  
528 [External]
529  
530 This Method will obtain the name server names of the given zone as defined in the delegation from parent zone.
531  
532 Takes a L<Zonemaster::Engine::Zone> object.
533  
534 Returns an arrayref of strings, or C<undef> if no parent zone was found.
535  
536 =back
537  
538 =cut
539  
540 sub get_del_ns_names {
541 my ( $class, $zone ) = @_;
542  
543 my $ns_ref = $class->get_del_ns_names_and_ips( $zone );
544  
545 return undef unless defined $ns_ref;
546  
547 return [ uniq sort map { $_->isa('Zonemaster::Engine::Nameserver') ? $_->name : $_ } @{ $ns_ref } ];
548 }
549  
550 =over
551  
552 =item get_del_ns_ips($zone)
553  
554 [External]
555  
556 This Method will obtain the IP addresses (from Glue Records) from the delegation of the given zone from the parent zone.
557 Glue Records are address records for In-Bailiwick name server names, if any. Also obtain the IP addresses for the Out-Of-Bailiwick name server names, if any.
558  
559 Takes a L<Zonemaster::Engine::Zone> object.
560  
561 Returns an arrayref of L<Zonemaster::Engine::Nameserver> objects, or C<undef> if no parent zone was found.
562  
563 =back
564  
565 =cut
566  
567 sub get_del_ns_ips {
568 my ( $class, $zone ) = @_;
569  
570 # FIXME: We really should just be outputting name server IPs here, as the
571 # specification says. Instead we output name server objects (but filtered
572 # on unique IP addresses) because these objects are required to perform
573 # queries.
574  
575 my $nameservers = $class->get_del_ns_names_and_ips( $zone );
576  
577 return undef unless defined $nameservers;
578  
579 my %ns_by_ip = ();
580 foreach my $ns ( @$nameservers ) {
581 next if ! $ns->isa('Zonemaster::Engine::Nameserver');
582 my $ip = $ns->address->short;
583 $ns_by_ip{$ip} = $ns unless exists $ns_by_ip{$ip};
584 }
585  
586 return [ sort values %ns_by_ip ];
587 }
588  
589 =over
590  
591 =item get_zone_ns_names($zone)
592  
593 [External]
594  
595 This Method will obtain the names of the authoritative name servers for the given zone as defined in the NS records in the zone itself.
596  
597 Takes a L<Zonemaster::Engine::Zone> object.
598  
599 Returns an arrayref of strings, or C<undef> if no parent zone was found.
600  
601 =back
602  
603 =cut
604  
605 sub get_zone_ns_names {
606 my ( $class, $zone ) = @_;
607  
608 # 'get_del_ns_names_and_ips' instead of 'get_del_ns_ips', because we need Zonemaster::Engine::Nameserver objects to be able to do queries.
609 my $ns_ref = $class->get_del_ns_names_and_ips( $zone );
610  
611 return undef unless defined $ns_ref;
612  
613 my @ns_names;
614  
615 for my $ns ( @{ $ns_ref } ) {
616 if ( $ns->isa('Zonemaster::Engine::Nameserver') ) {
617 my $p = $ns->query( $zone->name, q{NS} );
618  
619 if ( $p and $p->aa and $p->rcode eq q{NOERROR} ) {
620 push @ns_names, $p->get_records_for_name( q{NS}, $zone->name->string, q{answer} );
621 }
622 }
623 }
624  
625 return [ uniq sort map { name( lc( $_->nsdname ) ) } @ns_names ];
626 }
627  
628 =over
629  
630 =item _get_ib_addr_in_zone($zone)
631  
632 [Internal]
633  
634 This Method will obtain the address records matching the In-Bailiwick name server names from the given zone.
635  
636 Takes a L<Zonemaster::Engine::Zone> object.
637  
638 Returns an arrayref of L<Zonemaster::Engine::Nameserver> objects, or C<undef> if no parent zone was found.
639  
640 =back
641  
642 =cut
643  
644 sub _get_ib_addr_in_zone {
645 my ( $class, $zone ) = @_;
646  
647 # 'get_del_ns_names_and_ips' instead of 'get_del_ns_ips', because we need Zonemaster::Engine::Nameserver objects to be able to do queries.
648 my $del_ips_ref = $class->get_del_ns_names_and_ips( $zone );
649 my $ns_names_ref = $class->get_zone_ns_names( $zone );
650  
651 return undef unless defined $del_ips_ref or defined $ns_names_ref or scalar @{ $del_ips_ref } or scalar @{ $ns_names_ref };
652  
653 return [] unless scalar grep { $zone->name->is_in_bailiwick( $_ ) } @{ $ns_names_ref };
654  
655 my %ib_ns;
656  
657 for my $ns_name ( @{ $ns_names_ref } ) {
658 if ( $zone->name->is_in_bailiwick( $ns_name ) ) {
659 for my $ns ( @{ $del_ips_ref } ) {
660 for my $qtype ( q{A}, q{AAAA} ) {
661 my $p = Zonemaster::Engine::Recursor->recurse( $ns_name, $qtype, q{IN}, [ $ns ] );
662  
663 if ( $p and $p->aa and $p->rcode eq q{NOERROR} ) {
664 if ( $p->has_rrs_of_type_for_name( q{CNAME}, $ns_name, q{answer} ) ) {
665 my %cnames = map { name( $_->owner ) => name( $_->cname ) } $p->get_records( q{CNAME}, q{answer} );
666 my $target = $ns_name;
667 $target = $cnames{$target} while $cnames{$target};
668  
669 for my $rr ( $p->get_records_for_name( $qtype, $target ) ) {
670 push @{ $ib_ns{$ns_name} }, $rr->address;
671 }
672 }
673 # CNAME was followed in a new recursive query
674 elsif ( name( ($p->question)[0]->owner ) ne $ns_name and grep { $_->tag eq 'CNAME_FOLLOWED_OUT_OF_ZONE' and grep /^$ns_name$/, values %{ $_->args } } @{ Zonemaster::Engine->logger->entries } ) {
675 my $cname_ns_name = name( ($p->question)[0]->owner );
676 my $target = $cname_ns_name;
677  
678 if ( $p->has_rrs_of_type_for_name( q{CNAME}, $cname_ns_name, q{answer} ) ) {
679 my %cnames = map { name( $_->owner ) => name( $_->cname ) } $p->get_records( q{CNAME}, q{answer} );
680 $target = $cnames{$target} while $cnames{$target};
681 }
682  
683 for my $rr ( $p->get_records_for_name( $qtype, $target ) ) {
684 push @{ $ib_ns{$ns_name} }, $rr->address;
685 }
686 }
687 elsif ( $p->has_rrs_of_type_for_name( $qtype, $ns_name ) ) {
688 for my $rr ( $p->get_records_for_name( $qtype, $ns_name ) ) {
689 push @{ $ib_ns{$ns_name} }, $rr->address;
690 }
691 }
692 }
693 }
694 }
695 }
696 }
697  
698 my @ib_ns_array;
699  
700 for my $ns_name ( keys %ib_ns ) {
701 for my $ns_ip ( uniq @{ $ib_ns{$ns_name} } ) {
702 push @ib_ns_array, ns( $ns_name, $ns_ip );
703 }
704 }
705  
706 return [ uniq sort @ib_ns_array ];
707 }
708  
709 =over
710  
711 =item get_zone_ns_names_and_ips($zone)
712  
713 [External]
714  
715 This Method will obtain the name server names (extracted from the NS records) from the apex of the given zone.
716 For the In-Bailiwick name server names obtain the IP addresses from the given zone. For the Out-Of-Bailiwick name server names obtain the IP addresses from recursive lookup.
717  
718 Takes a L<Zonemaster::Engine::Zone> object.
719  
720 Returns an arrayref of L<Zonemaster::Engine::Nameserver> objects for each name server name that was successfully resolved to an IP address,
721 and L<Zonemaster::Engine::DNSName> objects for each name server name that could not be resolved to an IP address, or C<undef> if no parent zone was found.
722  
723 =back
724  
725 =cut
726  
727 sub get_zone_ns_names_and_ips {
728 my ( $class, $zone ) = @_;
729  
730 my $ns_names_ref = $class->get_zone_ns_names( $zone );
731  
732 return undef unless defined $ns_names_ref;
733  
734 return [] unless scalar @{ $ns_names_ref };
735  
736 my $ib_ns_ref = $class->_get_ib_addr_in_zone( $zone );
737 my $oob_ns_ref = $class->_get_oob_ips( $zone, $ns_names_ref );
738  
739 my @zone_ns;
740  
741 for my $ns_name ( @{ $ns_names_ref } ) {
742 if ( $zone->name->is_in_bailiwick( $ns_name ) ) {
743 if ( $ib_ns_ref and scalar @{ $ib_ns_ref } ) {
744 for my $ib_ns ( @{ $ib_ns_ref } ) {
745 if ( $ns_name->string eq $ib_ns->name->string ) {
746 push @zone_ns, ns( $ns_name, $ib_ns->address->short);
747 }
748 }
749 }
750 else {
751 push @zone_ns, $ns_name;
752 }
753 }
754 else {
755 if ( $oob_ns_ref and scalar @{ $oob_ns_ref } ) {
756 for my $oob_ns ( @{ $oob_ns_ref } ) {
757 if ( $oob_ns->isa('Zonemaster::Engine::Nameserver') and $ns_name->string eq $oob_ns->name->string ) {
758 push @zone_ns, ns( $ns_name, $oob_ns->address->short );
759 }
760 elsif ( $oob_ns->isa('Zonemaster::Engine::DNSName') and $ns_name->string eq $oob_ns->string ) {
761 push @zone_ns, $ns_name;
762 }
763 }
764 }
765 else {
766 push @zone_ns, $ns_name;
767 }
768 }
769 }
770  
771 return [ uniq sort @zone_ns ];
772 }
773  
774 =over
775  
776 =item get_zone_ns_ips($zone)
777  
778 [External]
779  
780 This Method will obtain the IP addresses of the name servers, as extracted from the NS records of apex of the given zone.
781  
782 Takes a L<Zonemaster::Engine::Zone> object.
783  
784 Returns an arrayref of L<Zonemaster::Engine::Nameserver> objects, or C<undef> if no parent zone was found.
785  
786 =back
787  
788 =cut
789  
790 sub get_zone_ns_ips {
791 my ( $class, $zone ) = @_;
792  
793 my $nameservers = $class->get_zone_ns_names_and_ips( $zone );
794  
795 return undef unless defined $nameservers;
796  
797 my %ns_by_ip = ();
798 foreach my $ns ( @$nameservers ) {
799 next if ! $ns->isa('Zonemaster::Engine::Nameserver');
800 my $ip = $ns->address->short;
801 $ns_by_ip{$ip} = $ns unless exists $ns_by_ip{$ip};
802 }
803  
804 return [ sort values %ns_by_ip ];
805 }
806  
807  
808 =over
809  
810 =item clear_cache()
811  
812 Clears previously cached results of the C<get_parent_ns_names_and_ips()> method.
813  
814 =back
815  
816 =cut
817  
818 sub clear_cache() {
819 Memoize::flush_cache(\&get_parent_ns_names_and_ips);
820 }
821  
822 1;