Zonemaster/Engine/Zone.pm untested

filter: Statementstmt 0.0% 0 / 202 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 358 CC 36 SCAR 26.6
CC
36
Cov
0%
is_in_zone
56.0
_build_ns_names
20.0
new
12.0
CRAP 14.2
1 package Zonemaster::Engine::Zone;
2  
3 use v5.16.0;
4 use warnings;
5  
6 use version; our $VERSION = version->declare("v1.1.9");
7  
8 use Carp qw( confess croak );
9 use List::MoreUtils qw[uniq];
10  
11 use Zonemaster::Engine::DNSName;
12 use Zonemaster::Engine::Recursor;
13 use Zonemaster::Engine::NSArray;
14 use Zonemaster::Engine::Constants qw[:ip];
15  
16 sub new {
17 my ( $class, $attrs ) = @_;
18  
19 my $name = delete $attrs->{name} // confess "required argument 'name' not found";
20 if ( %$attrs ) {
21 confess "unexpected arguments: " . join ', ', sort keys %$attrs;
22 }
23  
24 if ( blessed $name ne 'Zonemaster::Engine::DNSName' ) {
25 confess "argument 'name' must be a Zonemaster::Engine::DNSName";
26 }
27  
28 my $obj = { _name => $name };
29  
30 return bless $obj, $class;
31 }
32  
33 sub name {
34 my ( $self ) = @_;
35  
36 return $self->{_name};
37 }
38  
39 sub parent {
40 my ( $self ) = @_;
41  
42 if ( !exists $self->{_parent} ) {
43 $self->{_parent} = $self->_build_parent;
44 }
45  
46 return $self->{_parent};
47 }
48  
49 sub glue_names {
50 my ( $self ) = @_;
51  
52 if ( !exists $self->{_glue_names} ) {
53 $self->{_glue_names} = $self->_build_glue_names;
54 }
55  
56 return $self->{_glue_names};
57 }
58  
59 sub glue {
60 my ( $self ) = @_;
61  
62 if ( !exists $self->{_glue} ) {
63 $self->{_glue} = $self->_build_glue;
64 }
65  
66 return $self->{_glue};
67 }
68  
69 sub ns_names {
70 my ( $self ) = @_;
71  
72 if ( !exists $self->{_ns_names} ) {
73 $self->{_ns_names} = $self->_build_ns_names;
74 }
75  
76 return $self->{_ns_names};
77 }
78  
79 sub ns {
80 my ( $self ) = @_;
81  
82 if ( !exists $self->{_ns} ) {
83 $self->{_ns} = $self->_build_ns;
84 }
85  
86 return $self->{_ns};
87 }
88  
89 sub glue_addresses {
90 my ( $self ) = @_;
91  
92 if ( !exists $self->{_glue_addresses} ) {
93 $self->{_glue_addresses} = $self->_build_glue_addresses;
94 }
95  
96 return $self->{_glue_addresses};
97 }
98  
99 ###
100 ### Builders
101 ###
102  
103 sub _build_parent {
104 my ( $self ) = @_;
105  
106 if ( $self->name eq '.' ) {
107 return $self;
108 }
109  
110 my $pname = Zonemaster::Engine::Recursor->parent( q{} . $self->name );
111 return if not $pname;
112 ## no critic (Modules::RequireExplicitInclusion)
113 return __PACKAGE__->new( { name => $pname } );
114 }
115  
116 sub _build_glue_names {
117 my ( $self ) = @_;
118  
119 if ( not $self->parent ) {
120 return [];
121 }
122  
123 my $p = $self->parent->query_persistent( $self->name, 'NS' );
124  
125 return [] if not defined $p;
126  
127 return [ uniq sort map { Zonemaster::Engine::DNSName->new( lc( $_->nsdname ) ) }
128 $p->get_records_for_name( 'ns', $self->name->string ) ];
129 }
130  
131 sub _build_glue {
132 my ( $self ) = @_;
133 my @glue_names = @{ $self->glue_names };
134 my $zname = $self->name->string;
135  
136 if ( Zonemaster::Engine::Recursor->has_fake_addresses( $zname ) ) {
137 my @ns_list;
138 foreach my $ns ( @glue_names ) {
139 foreach my $ip ( Zonemaster::Engine::Recursor->get_fake_addresses( $zname, $ns ) ) {
140 push @ns_list, Zonemaster::Engine::Nameserver->new( { name => $ns, address => $ip } );
141 }
142 }
143 return \@ns_list;
144 }
145 else {
146  
147 my $aref = [];
148 tie @$aref, 'Zonemaster::Engine::NSArray', @glue_names;
149  
150 return $aref;
151 }
152 }
153  
154 sub _build_ns_names {
155 my ( $self ) = @_;
156  
157 if ( $self->name eq '.' ) {
158 my %u;
159 $u{$_} = $_ for map { $_->name } @{ $self->ns };
160 return [ sort values %u ];
161 }
162  
163 my $p;
164 my $i = 0;
165 while ( my $s = $self->glue->[$i] ) {
166 $p = $s->query( $self->name, 'NS' );
167 last if ( defined( $p ) and ( $p->type eq 'answer' ) and ( $p->rcode eq 'NOERROR' ) );
168 $i += 1;
169 }
170 return [] if not defined $p;
171  
172 return [ uniq sort map { Zonemaster::Engine::DNSName->new( lc( $_->nsdname ) ) }
173 $p->get_records_for_name( 'ns', $self->name->string ) ];
174 } ## end sub _build_ns_names
175  
176 sub _build_ns {
177 my ( $self ) = @_;
178  
179 if ( $self->name eq '.' ) { # Root is a special case
180 return [ Zonemaster::Engine::Recursor->root_servers ];
181 }
182  
183 my $aref = [];
184 tie @$aref, 'Zonemaster::Engine::NSArray', @{ $self->ns_names };
185  
186 return $aref;
187 }
188  
189 sub _build_glue_addresses {
190 my ( $self ) = @_;
191  
192 if ( not $self->parent ) {
193 return [];
194 }
195  
196 my $p = $self->parent->query_one( $self->name, 'NS' );
197 croak "Failed to get glue addresses" if not defined( $p );
198  
199 return [ $p->get_records( 'a' ), $p->get_records( 'aaaa' ) ];
200 }
201  
202 sub _is_ip_version_disabled {
203 my ( $ns, $type ) = @_;
204  
205 if ( not Zonemaster::Engine::Profile->effective->get(q{net.ipv4}) and $ns->address->version == $IP_VERSION_4 ) {
206 Zonemaster::Engine->logger->add(
207 SKIP_IPV4_DISABLED => {
208 ns => $ns->string,
209 rrtype => $type
210 }
211 );
212 return 1;
213 }
214  
215 if ( not Zonemaster::Engine::Profile->effective->get(q{net.ipv6}) and $ns->address->version == $IP_VERSION_6 ) {
216 Zonemaster::Engine->logger->add(
217 SKIP_IPV6_DISABLED => {
218 ns => $ns->string,
219 rrtype => $type
220 }
221 );
222 return 1;
223 }
224  
225 return 0;
226 }
227  
228 ###
229 ### Public Methods
230 ###
231  
232 sub query_one {
233 my ( $self, $name, $type, $flags ) = @_;
234  
235 # Return response from the first server that gives one
236 my $i = 0;
237 while ( my $ns = $self->ns->[$i] ) {
238 if ( _is_ip_version_disabled( $ns, $type ) ) {
239 next;
240 }
241  
242 my $p = $ns->query( $name, $type, $flags );
243 return $p if defined( $p );
244 }
245 continue {
246 $i += 1;
247 }
248  
249 return;
250 } ## end sub query_one
251  
252 sub query_all {
253 my ( $self, $name, $type, $flags ) = @_;
254  
255 my @servers = @{ $self->ns };
256  
257 if ( not Zonemaster::Engine::Profile->effective->get(q{net.ipv4}) ) {
258 my @nope = grep { $_->address->version == $IP_VERSION_4 } @servers;
259 @servers = grep { $_->address->version == $IP_VERSION_6 } @servers;
260 map {
261 Zonemaster::Engine->logger->add(
262 SKIP_IPV4_DISABLED => {
263 ns => $_->string,
264 rrtype => $type
265 }
266 )
267 } @nope;
268 }
269  
270 if ( not Zonemaster::Engine::Profile->effective->get(q{net.ipv6}) ) {
271 my @nope = grep { $_->address->version == $IP_VERSION_6 } @servers;
272 @servers = grep { $_->address->version == $IP_VERSION_4 } @servers;
273 map {
274 Zonemaster::Engine->logger->add(
275 SKIP_IPV6_DISABLED => {
276 ns => $_->string,
277 rrtype => $type
278 }
279 )
280 } @nope;
281 }
282  
283 return [ map { $_->query( $name, $type, $flags ) } @servers ];
284 }
285  
286 sub query_auth {
287 my ( $self, $name, $type, $flags ) = @_;
288  
289 # Return response from the first server that replies with AA set
290 my $i = 0;
291 while ( my $ns = $self->ns->[$i] ) {
292 if ( _is_ip_version_disabled( $ns, $type ) ) {
293 next;
294 }
295  
296 my $p = $ns->query( $name, $type, $flags );
297 if ( $p and $p->aa ) {
298 return $p;
299 }
300 }
301 continue {
302 $i += 1;
303 }
304  
305 return;
306 } ## end sub query_auth
307  
308 sub query_persistent {
309 my ( $self, $name, $type, $flags ) = @_;
310  
311 # Return response from the first server that has a record like the one asked for
312 my $i = 0;
313 while ( my $ns = $self->ns->[$i] ) {
314 if ( _is_ip_version_disabled( $ns, $type ) ) {
315 next;
316 }
317  
318 my $p = $ns->query( $name, $type, $flags );
319 if ( $p and scalar( $p->get_records_for_name( $type, $name ) ) > 0 ) {
320 return $p;
321 }
322 }
323 continue {
324 $i += 1;
325 }
326  
327 return;
328 } ## end sub query_persistent
329  
330 sub is_in_zone {
331 my ( $self, $name ) = @_;
332  
333 if ( not ref( $name ) or ref( $name ) ne 'Zonemaster::Engine::DNSName' ) {
334 $name = Zonemaster::Engine::DNSName->new( $name );
335 }
336  
337 if ( scalar( @{ $self->name->labels } ) != $self->name->common( $name ) ) {
338 return 0; # Zone name cannot be a suffix of tested name
339 }
340  
341 my $p = $self->query_auth( "$name", 'SOA' );
342 if ( not $p ) {
343 return;
344 }
345  
346 if ( $p->is_redirect ) {
347 return 0; # Authoritative servers redirect us, so name must be out-of-zone
348 }
349  
350 my ( $soa ) = $p->get_records( 'SOA' );
351  
352 if ( not $soa ) {
353 return 0; # Auth server is broken, call it a "no".
354 }
355  
356 if ( Zonemaster::Engine::DNSName->new( $soa->name ) eq $self->name ) {
357 return 1;
358 }
359 else {
360 return 0;
361 }
362 } ## end sub is_in_zone
363  
364 1;
365  
366 =head1 NAME
367  
368 Zonemaster::Engine::Zone - Object representing a DNS zone
369  
370 =head1 SYNOPSIS
371  
372 my $zone = Zonemaster::Engine::Zone->new({ name => 'nic.se' });
373 my $packet = $zone->parent->query_one($zone->name, 'NS');
374  
375  
376 =head1 DESCRIPTION
377  
378 Objects of this class represent zones in DNS. As far as possible, test
379 implementations should access information about zones via these
380 objects. Doing so will provide lazy-loading of the information,
381 well-defined methods in which the information is fetched, logging and
382 the ability to do things like testing zones that have not yet been
383 delegated.
384  
385 =head1 CONSTRUCTORS
386  
387 =over
388  
389 =item new
390  
391 Construct a new instance.
392  
393 =back
394  
395 =head1 ATTRIBUTES
396  
397 =over
398  
399 =item name
400  
401 A L<Zonemaster::Engine::DNSName> object representing the name of the zone.
402  
403 =item parent
404  
405 A L<Zonemaster::Engine::Zone> object for this domain's parent domain. As a
406 special case, the root zone is considered to be its own parent (so
407 look for that if you recurse up the tree).
408  
409 =item ns_names
410  
411 A reference to an array of L<Zonemaster::Engine::DNSName> objects, holding the
412 names of the nameservers for the domain, as returned by the first
413 responding nameserver in the glue list.
414  
415 =item ns
416  
417 A reference to an array of L<Zonemaster::Engine::Nameserver> objects for the
418 domain, built by taking the list returned from L<ns_names()> and
419 looking up addresses for the names. One element will be added to this
420 list for each unique name/IP pair. Names for which no addresses could
421 be found will not be in this list. The list is lazy-loading, so take
422 care to only look at as many entries as you really need. There are
423 zones with more than 20 nameserver, and looking up the addresses of
424 them all can take som considerable time.
425  
426 =item glue_names
427  
428 A reference to a an array of L<Zonemaster::Engine::DNSName> objects, holding the names
429 of this zones nameservers as listed at the first responding nameserver of the
430 parent zone.
431  
432 =item glue
433  
434 A reference to an array of L<Zonemaster::Engine::Nameserver> objects for the
435 domain, built by taking the list returned from L<glue_names()> and
436 looking up addresses for the names. One element will be added to this
437 list for each unique name/IP pair. Names for which no addresses could
438 be found will not be in this list. In this case, the list is lazy-loading, so take
439 care to only look at as many entries as you really need. In case of
440 undelegated tests and fake delegation the IP associated with name servers
441 for the tested zone will be the ones set by users (saved in
442 %Zonemaster::Engine::Recursor::fake_addresses_cache), instead of the ones
443 found recursively.
444  
445 =item glue_addresses
446  
447 A list of L<Zonemaster::LDNS::RR::A> and L<Zonemaster::LDNS::RR::AAAA> records returned in
448 the Additional section of an NS query to the first listed nameserver for the
449 parent domain.
450  
451 =back
452  
453 =head1 METHODS
454  
455 =over
456  
457 =item query_one($name[, $type[, $flags]])
458  
459 Sends (or retrieves from cache) a query for the given name, type and flags sent to the first nameserver in the zone's ns list. If there is a
460 response, it will be returned in a L<Zonemaster::Engine::Packet> object. Regarding defaults for optional arguments "type" and "flags",
461 see L<Zonemaster::Engine::Nameserver/query>.
462  
463 =item query_persistent($name[, $type[, $flags]])
464  
465 Identical to L<query_one>, except that instead of returning the packet from the
466 first server that returns one, it returns the first packet that actually
467 contains a resource record matching the requested name and type.
468  
469 =item query_auth($name[, $type[, $flags]])
470  
471 Identical to L<query_one>, except that instead of returning the packet from the
472 first server that returns one, it returns the first packet that has the AA flag set.
473  
474 =item query_all($name, $type, $flags)
475  
476 Sends (or retrieves from cache) queries to all the nameservers listed in the zone's ns list, and returns a reference to an array with the
477 responses. The responses can be either L<Zonemaster::Engine::Packet> objects or C<undef> values. The arguments are the same as for L<query_one>.
478  
479 =item is_in_zone($name)
480  
481 Returns true if the given name is in the zone, false if not. If it could not be
482 determined with a sufficient degree of certainty if the name is in the zone or
483 not, C<undef> is returned.
484  
485 =back
486  
487 =cut