| 1 | package Zonemaster::Zone v1.0.0; | ||
| 2 | |||
| 3 | use 5.14.2; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Moose; | ||
| 8 | use Carp; | ||
| 9 | use List::MoreUtils qw[uniq]; | ||
| 10 | |||
| 11 | use Zonemaster::DNSName; | ||
| 12 | use Zonemaster::Recursor; | ||
| 13 | use Zonemaster::NSArray; | ||
| 14 | |||
| 15 | has 'name' => ( is => 'ro', isa => 'Zonemaster::DNSName', required => 1, coerce => 1 ); | ||
| 16 | has 'parent' => ( is => 'ro', isa => 'Maybe[Zonemaster::Zone]', lazy_build => 1 ); | ||
| 17 | has [ 'ns', 'glue' ] => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1 ); | ||
| 18 | has [ 'ns_names', 'glue_names' ] => ( is => 'ro', isa => 'ArrayRef[Zonemaster::DNSName]', lazy_build => 1 ); | ||
| 19 | has 'glue_addresses' => ( is => 'ro', isa => 'ArrayRef[Net::LDNS::RR]', lazy_build => 1 ); | ||
| 20 | |||
| 21 | ### | ||
| 22 | ### Builders | ||
| 23 | ### | ||
| 24 | |||
| 25 | sub _build_parent { | ||
| 26 | my ( $self ) = @_; | ||
| 27 | |||
| 28 | if ( $self->name eq '.' ) { | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | my $pname = Zonemaster::Recursor->parent( '' . $self->name ); | ||
| 33 | return if not $pname; | ||
| 34 | ## no critic (Modules::RequireExplicitInclusion) | ||
| 35 | return __PACKAGE__->new( { name => $pname } ); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _build_glue_names { | ||
| 39 | my ( $self ) = @_; | ||
| 40 | |||
| 41 | if ( not $self->parent ) { | ||
| 42 | return []; | ||
| 43 | } | ||
| 44 | |||
| 45 | my $p = $self->parent->query_one( $self->name, 'NS' ); | ||
| 46 | |||
| 47 | return [] if not defined $p; | ||
| 48 | |||
| 49 | return [ uniq sort map { Zonemaster::DNSName->new( lc( $_->nsdname ) ) } | ||
| 50 | $p->get_records_for_name( 'ns', $self->name->string ) ]; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _build_glue { | ||
| 54 | my ( $self ) = @_; | ||
| 55 | |||
| 56 | my $aref = []; | ||
| 57 | tie @$aref, 'Zonemaster::NSArray', @{ $self->glue_names }; | ||
| 58 | |||
| 59 | return $aref; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _build_ns_names { | ||
| 63 | my ( $self ) = @_; | ||
| 64 | |||
| 65 | if ( $self->name eq '.' ) { | ||
| 66 | my %u; | ||
| 67 | $u{$_} = $_ for map { $_->name } @{ $self->ns }; | ||
| 68 | return [ sort values %u ]; | ||
| 69 | } | ||
| 70 | |||
| 71 | my $p; | ||
| 72 | my $i = 0; | ||
| 73 | while ( my $s = $self->glue->[$i] ) { | ||
| 74 | $p = $s->query( $self->name, 'NS' ); | ||
| 75 | last if ( defined( $p ) and ( $p->type eq 'answer' ) and ( $p->rcode eq 'NOERROR' ) ); | ||
| 76 | $i += 1; | ||
| 77 | } | ||
| 78 | return [] if not defined $p; | ||
| 79 | |||
| 80 | return [ uniq sort map { Zonemaster::DNSName->new( lc( $_->nsdname ) ) } | ||
| 81 | $p->get_records_for_name( 'ns', $self->name->string ) ]; | ||
| 82 | } ## end sub _build_ns_names | ||
| 83 | |||
| 84 | sub _build_ns { | ||
| 85 | my ( $self ) = @_; | ||
| 86 | |||
| 87 | if ( $self->name eq '.' ) { # Root is a special case | ||
| 88 | return [ Zonemaster::Recursor->root_servers ]; | ||
| 89 | } | ||
| 90 | |||
| 91 | my $aref = []; | ||
| 92 | tie @$aref, 'Zonemaster::NSArray', @{ $self->ns_names }; | ||
| 93 | |||
| 94 | return $aref; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _build_glue_addresses { | ||
| 98 | my ( $self ) = @_; | ||
| 99 | |||
| 100 | if ( not $self->parent ) { | ||
| 101 | return []; | ||
| 102 | } | ||
| 103 | |||
| 104 | my $p = $self->parent->query_one( $self->name, 'NS' ); | ||
| 105 | croak "Failed to get glue addresses" if not defined( $p ); | ||
| 106 | |||
| 107 | return [ $p->get_records( 'a' ), $p->get_records( 'aaaa' ) ]; | ||
| 108 | } | ||
| 109 | |||
| 110 | ### | ||
| 111 | ### Public Methods | ||
| 112 | ### | ||
| 113 | |||
| 114 | sub query_one { | ||
| 115 | my ( $self, $name, $type, $flags ) = @_; | ||
| 116 | |||
| 117 | # Return response from the first server that gives one | ||
| 118 | my $i = 0; | ||
| 119 | while ( my $ns = $self->ns->[$i] ) { | ||
| 120 | if ( not Zonemaster->config->ipv4_ok and $ns->address->version == 4 ) { | ||
| 121 | Zonemaster->logger->add( SKIP_IPV4_DISABLED => { ns => "$ns" } ); | ||
| 122 | next; | ||
| 123 | } | ||
| 124 | |||
| 125 | if ( not Zonemaster->config->ipv6_ok and $ns->address->version == 6 ) { | ||
| 126 | Zonemaster->logger->add( SKIP_IPV6_DISABLED => { ns => "$ns" } ); | ||
| 127 | next; | ||
| 128 | } | ||
| 129 | |||
| 130 | my $p = $ns->query( $name, $type, $flags ); | ||
| 131 | return $p if defined( $p ); | ||
| 132 | } | ||
| 133 | continue { | ||
| 134 | $i += 1; | ||
| 135 | } | ||
| 136 | |||
| 137 | return; | ||
| 138 | } ## end sub query_one | ||
| 139 | |||
| 140 | sub query_all { | ||
| 141 | my ( $self, $name, $type, $flags ) = @_; | ||
| 142 | |||
| 143 | my @servers = @{ $self->ns }; | ||
| 144 | |||
| 145 | if ( not Zonemaster->config->ipv4_ok ) { | ||
| 146 | my @nope = grep { $_->address->version == 4 } @servers; | ||
| 147 | @servers = grep { $_->address->version != 4 } @servers; | ||
| 148 | Zonemaster->logger->add( SKIP_IPV4_DISABLED => { ns => ( join ';', map { "$_" } @nope ) } ); | ||
| 149 | } | ||
| 150 | |||
| 151 | if ( not Zonemaster->config->ipv6_ok ) { | ||
| 152 | my @nope = grep { $_->address->version == 6 } @servers; | ||
| 153 | @servers = grep { $_->address->version != 6 } @servers; | ||
| 154 | Zonemaster->logger->add( SKIP_IPV6_DISABLED => { ns => ( join ';', map { "$_" } @nope ) } ); | ||
| 155 | } | ||
| 156 | |||
| 157 | return [ map { $_->query( $name, $type, $flags ) } @servers ]; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub query_auth { | ||
| 161 | my ( $self, $name, $type, $flags ) = @_; | ||
| 162 | |||
| 163 | # Return response from the first server that replies with AA set | ||
| 164 | my $i = 0; | ||
| 165 | while ( my $ns = $self->ns->[$i] ) { | ||
| 166 | if ( not Zonemaster->config->ipv4_ok and $ns->address->version == 4 ) { | ||
| 167 | Zonemaster->logger->add( SKIP_IPV4_DISABLED => { ns => "$ns" } ); | ||
| 168 | next; | ||
| 169 | } | ||
| 170 | |||
| 171 | if ( not Zonemaster->config->ipv6_ok and $ns->address->version == 6 ) { | ||
| 172 | Zonemaster->logger->add( SKIP_IPV6_DISABLED => { ns => "$ns" } ); | ||
| 173 | next; | ||
| 174 | } | ||
| 175 | |||
| 176 | my $p = $ns->query( $name, $type, $flags ); | ||
| 177 | if ( $p and $p->aa ) { | ||
| 178 | return $p; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | continue { | ||
| 182 | $i += 1; | ||
| 183 | } | ||
| 184 | |||
| 185 | return; | ||
| 186 | } ## end sub query_auth | ||
| 187 | |||
| 188 | sub query_persistent { | ||
| 189 | my ( $self, $name, $type, $flags ) = @_; | ||
| 190 | |||
| 191 | # Return response from the first server that has a record like the one asked for | ||
| 192 | my $i = 0; | ||
| 193 | while ( my $ns = $self->ns->[$i] ) { | ||
| 194 | if ( not Zonemaster->config->ipv4_ok and $ns->address->version == 4 ) { | ||
| 195 | Zonemaster->logger->add( SKIP_IPV4_DISABLED => { ns => "$ns" } ); | ||
| 196 | next; | ||
| 197 | } | ||
| 198 | |||
| 199 | if ( not Zonemaster->config->ipv6_ok and $ns->address->version == 6 ) { | ||
| 200 | Zonemaster->logger->add( SKIP_IPV6_DISABLED => { ns => "$ns" } ); | ||
| 201 | next; | ||
| 202 | } | ||
| 203 | |||
| 204 | my $p = $ns->query( $name, $type, $flags ); | ||
| 205 | if ( $p and scalar( $p->get_records_for_name( $type, $name ) ) > 0 ) { | ||
| 206 | return $p; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | continue { | ||
| 210 | $i += 1; | ||
| 211 | } | ||
| 212 | |||
| 213 | return; | ||
| 214 | } ## end sub query_persistent | ||
| 215 | |||
| 216 | sub is_in_zone { | ||
| 217 | my ( $self, $name ) = @_; | ||
| 218 | |||
| 219 | if ( not ref( $name ) or ref( $name ) ne 'Zonemaster::DNSName' ) { | ||
| 220 | $name = Zonemaster::DNSName->new( $name ); | ||
| 221 | } | ||
| 222 | |||
| 223 | if ( scalar( @{ $self->name->labels } ) != $self->name->common( $name ) ) { | ||
| 224 | return 0; # Zone name cannot be a suffix of tested name | ||
| 225 | } | ||
| 226 | |||
| 227 | my $p = $self->query_auth( "$name", 'SOA' ); | ||
| 228 | if ( not $p ) { | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | |||
| 232 | if ( $p->is_redirect ) { | ||
| 233 | return 0; # Authoritative servers redirect us, so name must be out-of-zone | ||
| 234 | } | ||
| 235 | |||
| 236 | my ( $soa ) = $p->get_records( 'SOA' ); | ||
| 237 | |||
| 238 | if ( not $soa ) { | ||
| 239 | return 0; # Auth server is broken, call it a "no". | ||
| 240 | } | ||
| 241 | |||
| 242 | if ( Zonemaster::DNSName->new( $soa->name ) eq $self->name ) { | ||
| 243 | return 1; | ||
| 244 | } | ||
| 245 | else { | ||
| 246 | return 0; | ||
| 247 | } | ||
| 248 | } ## end sub is_in_zone | ||
| 249 | |||
| 250 | no Moose; | ||
| 251 | __PACKAGE__->meta->make_immutable; | ||
| 252 | |||
| 253 | 1; | ||
| 254 | |||
| 255 | =head1 NAME | ||
| 256 | |||
| 257 | Zonemaster::Zone - Object representing a DNS zone | ||
| 258 | |||
| 259 | =head1 SYNOPSIS | ||
| 260 | |||
| 261 | my $zone = Zonemaster::Zone->new({ name => 'nic.se' }); | ||
| 262 | my $packet = $zone->parent->query_one($zone->name, 'NS'); | ||
| 263 | |||
| 264 | |||
| 265 | =head1 DESCRIPTION | ||
| 266 | |||
| 267 | Objects of this class represent zones in DNS. As far as possible, test | ||
| 268 | implementations should access information about zones via these | ||
| 269 | objects. Doing so will provide lazy-loading of the information, | ||
| 270 | well-defined methods in which the information is fetched, logging and | ||
| 271 | the ability to do things like testing zones that have not yet been | ||
| 272 | delegated. | ||
| 273 | |||
| 274 | =head1 ATTRIBUTES | ||
| 275 | |||
| 276 | =over | ||
| 277 | |||
| 278 | =item name | ||
| 279 | |||
| 280 | A L<Zonemaster::DNSName> object representing the name of the zone. | ||
| 281 | |||
| 282 | =item parent | ||
| 283 | |||
| 284 | A L<Zonemaster::Zone> object for this domain's parent domain. As a | ||
| 285 | special case, the root zone is considered to be its own parent (so | ||
| 286 | look for that if you recurse up the tree). | ||
| 287 | |||
| 288 | =item ns_names | ||
| 289 | |||
| 290 | A reference to an array of L<Zonemaster::DNSName> objects, holding the | ||
| 291 | names of the nameservers for the domain, as returned by the first | ||
| 292 | responding nameserver in the glue list. | ||
| 293 | |||
| 294 | =item ns | ||
| 295 | |||
| 296 | A reference to an array of L<Zonemaster::Nameserver> objects for the | ||
| 297 | domain, built by taking the list returned from L<ns_names()> and | ||
| 298 | looking up addresses for the names. One element will be added to this | ||
| 299 | list for each unique name/IP pair. Names for which no addresses could | ||
| 300 | be found will not be in this list. The list is lazy-loading, so take | ||
| 301 | care to only look at as many entries as you really need. There are | ||
| 302 | zones with more than 20 nameserver, and looking up the addresses of | ||
| 303 | them all can take som considerable time. | ||
| 304 | |||
| 305 | =item glue_names | ||
| 306 | |||
| 307 | A reference to a an array of L<Zonemaster::DNSName> objects, holding the names | ||
| 308 | of this zones nameservers as listed at the first responding nameserver of the | ||
| 309 | parent zone. | ||
| 310 | |||
| 311 | =item glue | ||
| 312 | |||
| 313 | A reference to an array of L<Zonemaster::Nameserver> objects for the | ||
| 314 | domain, built by taking the list returned from L<glue_names()> and | ||
| 315 | looking up addresses for the names. One element will be added to this | ||
| 316 | list for each unique name/IP pair. Names for which no addresses could | ||
| 317 | be found will not be in this list. The list is lazy-loading, so take | ||
| 318 | care to only look at as many entries as you really need. | ||
| 319 | |||
| 320 | =item glue_addresses | ||
| 321 | |||
| 322 | A list of L<Net::LDNS::RR::A> and L<Net::LDNS::RR::AAAA> records returned in | ||
| 323 | the Additional section of an NS query to the first listed nameserver for the | ||
| 324 | parent domain. | ||
| 325 | |||
| 326 | =back | ||
| 327 | |||
| 328 | =head1 METHODS | ||
| 329 | |||
| 330 | =over | ||
| 331 | |||
| 332 | =item query_one($name[, $type[, $flags]]) | ||
| 333 | |||
| 334 | 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 | ||
| 335 | response, it will be returned in a L<Zonemaster::Packet> object. If the type arguments is not given, it defaults to 'A'. If the flags are not given, they default to C<class> IN and C<dnssec>, C<usevc> and C<recurse> according to configuration (which is by default off on all three). | ||
| 336 | |||
| 337 | =item query_persistent($name[, $type[, $flags]]) | ||
| 338 | |||
| 339 | Identical to L<query_one>, except that instead of returning the packet from the | ||
| 340 | first server that returns one, it returns the first packet that actually | ||
| 341 | contains a resource record matching the requested name and type. | ||
| 342 | |||
| 343 | =item query_auth($name[, $type[, $flags]]) | ||
| 344 | |||
| 345 | Identical to L<query_one>, except that instead of returning the packet from the | ||
| 346 | first server that returns one, it returns the first packet that has the AA flag set. | ||
| 347 | |||
| 348 | =item query_all($name, $type, $flags) | ||
| 349 | |||
| 350 | 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 | ||
| 351 | responses. The responses can be either L<Zonemaster::Packet> objects or C<undef> values. The arguments are the same as for L<query_one>. | ||
| 352 | |||
| 353 | =item is_in_zone($name) | ||
| 354 | |||
| 355 | Returns true if the given name is in the zone, false if not. If it could not be | ||
| 356 | determined with a sufficient degree of certainty if the name is in the zone or | ||
| 357 | not, C<undef> is returned. | ||
| 358 | |||
| 359 | =back | ||
| 360 | |||
| 361 | =cut |