Zonemaster/Engine/Nameserver.pm

filter: Statementstmt 93.1% 27 / 29 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 94.8% 37 / 39 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Zonemaster/Engine.pm ↑ Summary
1 package Zonemaster::Engine::Nameserver;
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.16");
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Class::Accessor qw[ antlers ];
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Zonemaster::Engine::DNSName;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Zonemaster::Engine::Packet;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Zonemaster::Engine::Nameserver::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Zonemaster::Engine::Recursor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Zonemaster::Engine::Constants qw( :ip :misc );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶use Zonemaster::LDNS;
Subroutine BEGIN: called CC=1 SCAR=2.6
16  
17 use Carp qw( confess croak );
18 use CBOR::XS;
19 use Digest::MD5;
20 use Fcntl qw( SEEK_SET );
21 use List::Util qw( max min sum );
22 use MIME::Base64;
23 use Module::Find qw( useall );
24 use Net::IP::XS;
25 use POSIX ();
26 use Scalar::Util qw( blessed );
27 use Time::HiRes qw( gettimeofday time tv_interval );
28  
29 our @ISA = qw( Class::Accessor );
30  
31 use overload
32 '""' => \&string,
33 'cmp' => \&compare;
34  
35 has 'name' => ( is => 'ro' );
36 has 'address' => ( is => 'ro' );
37  
38 has 'dns' => ( is => 'ro' );
39 has 'cache' => ( is => 'ro' );
40 has 'times' => ( is => 'ro' );
41  
42 has 'fake_delegations' => ( is => 'ro' );
43 has 'fake_ds' => ( is => 'ro' );
44  
45 has 'blacklisted' => ( is => 'rw' );
46  
47 ###
48 ### Variables
49 ###
50  
51 our %object_cache;
52 our %address_object_cache;
53 our %address_repr_cache;
54  
55 ###
56 ### Build methods for attributes
57 ###
58  
59 sub new {
60 my $class = shift;
61 my $attrs = shift;
62  
63 my %lazy_attrs;
64 $lazy_attrs{dns} = delete $attrs->{dns} if exists $attrs->{dns};
65 $lazy_attrs{cache} = delete $attrs->{cache} if exists $attrs->{cache};
66  
67 # Required arguments
68 confess "Attribute \(address\) is required"
69 if !defined $attrs->{address};
70  
71 # Type coercions
72 $attrs->{name} = Zonemaster::Engine::DNSName->from_string( $attrs->{name} )
73 if !blessed $attrs->{name} || !$attrs->{name}->isa( 'Zonemaster::Engine::DNSName' );
74  
75 my $name = lc( q{} . $attrs->{name} );
76 $name = '$$$NONAME' if $name eq q{};
77  
78 my $address;
79  
80 # Use a object cache for IP type coercion (don't parse IP unless it is needed)
81 if (!blessed $attrs->{address} || !$attrs->{address}->isa( 'Net::IP::XS' )) {
82 if (!exists $address_object_cache{$attrs->{address}}) {
83 $address_object_cache{$attrs->{address}} = Net::IP::XS->new($attrs->{address});
84 $address_repr_cache{$attrs->{address}} = $address_object_cache{$attrs->{address}}->short;
85 }
86 # Fetch IP object from the address cache (avoid object creation and method call)
87 $address = $address_repr_cache{$attrs->{address}};
88 $attrs->{address} = $address_object_cache{$attrs->{address}};
89 } else {
90 $address = $attrs->{address}->short;
91 }
92  
93 # Return Nameserver object as soon as possible
94 if ( exists $object_cache{$name}{$address} ) {
95 return $object_cache{$name}{$address};
96 }
97  
98 # Type constraints
99 confess "Argument must be coercible into a Zonemaster::Engine::DNSName: name"
100 if !$attrs->{name}->isa( 'Zonemaster::Engine::DNSName' );
101 confess "Argument must be coercible into a Net::IP::XS: address"
102 if exists $attrs->{address}
103 && !$attrs->{address}->isa( 'Net::IP::XS' );
104 confess "Argument must be an ARRAYREF: times"
105 if exists $attrs->{times}
106 && ref $attrs->{times} ne 'ARRAY';
107 confess "Argument must be a HASHREF: fake_delegations"
108 if exists $attrs->{fake_delegations}
109 && ref $attrs->{fake_delegations} ne 'HASH';
110 confess "Argument must be a HASHREF: fake_ds"
111 if exists $attrs->{fake_ds}
112 && ref $attrs->{fake_ds} ne 'HASH';
113 confess "Argument must be a HASHREF: blacklisted"
114 if exists $attrs->{blacklisted}
115 && ref $attrs->{blacklisted} ne 'HASH';
116 confess "Argument must be a Zonemaster::LDNS: dns"
117 if exists $lazy_attrs{dns}
118 && ( !blessed $lazy_attrs{dns} || !$lazy_attrs{dns}->isa( 'Zonemaster::LDNS' ) );
119 confess "Argument must be a Zonemaster::Engine::Nameserver::Cache: cache"
120 if exists $lazy_attrs{cache}
121 && ( !blessed $lazy_attrs{cache} || !$lazy_attrs{cache}->isa( 'Zonemaster::Engine::Nameserver::Cache' ) );
122  
123 # Default values
124 $attrs->{blacklisted} //= {};
125 $attrs->{fake_delegations} //= {};
126 $attrs->{fake_ds} //= {};
127 $attrs->{times} //= [];
128  
129 my $obj = Class::Accessor::new( $class, $attrs );
130 $obj->{_dns} = $lazy_attrs{dns} if exists $lazy_attrs{dns};
131 $obj->{_cache} = $lazy_attrs{cache} if exists $lazy_attrs{cache};
132  
133 $obj->{_string} = $name . q{/} . $address;
134  
135 Zonemaster::Engine->logger->add( NS_CREATED => { name => $name, ip => $address } );
136 $object_cache{$name}{$address} = $obj;
137  
138 return $obj;
139 }
140  
141 sub dns {
142 my $self = shift;
143  
144 # Lazy default value
145 if ( !exists $self->{_dns} ) {
146 $self->{_dns} = $self->_build_dns();
147 }
148  
149 return $self->{_dns};
150 }
151  
152 sub cache {
153 my $self = shift;
154  
155 # Lazy default value
156 if ( !exists $self->{_cache} ) {
157 $self->{_cache} = $self->_build_cache();
158 }
159  
160 return $self->{_cache};
161 }
162  
163 sub _build_dns {
164 my ( $self ) = @_;
165  
166 my $res = Zonemaster::LDNS->new( $self->address->ip );
167  
168 $res->recurse( 0 );
169 $res->dnssec( 0 );
170 $res->edns_size( 0 );
171  
172 $res->retry( Zonemaster::Engine::Profile->effective->get( q{resolver.defaults.retry} ) );
173 $res->retrans( Zonemaster::Engine::Profile->effective->get( q{resolver.defaults.retrans} ) );
174 $res->debug( Zonemaster::Engine::Profile->effective->get( q{resolver.defaults.debug} ) );
175 $res->timeout( Zonemaster::Engine::Profile->effective->get( q{resolver.defaults.timeout} ) );
176  
177 my $src_address = $self->source_address();
178 if ( defined( $src_address ) ) {
179 $res->source( $src_address );
180 }
181  
182 return $res;
183 }
184  
185 sub _build_cache {
186 my ( $self ) = @_;
187  
188 my $cache_type = Zonemaster::Engine::Nameserver::Cache->get_cache_type( Zonemaster::Engine::Profile->effective );
189 my $cache_class = Zonemaster::Engine::Nameserver::Cache->get_cache_class( $cache_type );
190  
191 $cache_class->new( { address => $self->address } );
192 }
193  
194 ###
195 ### Public Methods (and helpers)
196 ###
197  
198 sub query {
199 my ( $self, $name, $type, $href ) = @_;
200 $type //= 'A';
201  
202 my $address = $self->address;
203 my $profile = Zonemaster::Engine::Profile->effective;
204  
205 if ( $address->version == 4 and not $profile->get( q{net.ipv4} ) ) {
206 Zonemaster::Engine->logger->add( IPV4_BLOCKED => { ns => $self->string } );
207 return;
208 }
209  
210 if ( $address->version == 6 and not $profile->get( q{net.ipv6} ) ) {
211 Zonemaster::Engine->logger->add( IPV6_BLOCKED => { ns => $self->string } );
212 return;
213 }
214  
215 Zonemaster::Engine->logger->add(
216 'QUERY',
217 {
218 name => "$name",
219 type => $type,
220 flags => $href,
221 ip => $address->short
222 }
223 );
224  
225 my $class = $href->{class} // 'IN';
226 my $dnssec = $href->{dnssec} // 0;
227 my $usevc = $href->{usevc} // 0;
228 my $recurse = $href->{recurse} // 0;
229  
230 if ( exists $href->{edns_details} and exists $href->{edns_details}{do} ) {
231 $dnssec = $href->{edns_details}{do};
232 }
233  
234 my $edns_size = $href->{edns_size} // ( $dnssec ? $EDNS_UDP_PAYLOAD_DNSSEC_DEFAULT : 0 );
235  
236 # Fake a DS answer
237 if ( $type eq 'DS' and $class eq 'IN' and $self->fake_ds->{ lc( $name ) } ) {
238 my $p = Zonemaster::LDNS::Packet->new( $name, $type, $class );
239  
240 $p->qr( 1 );
241 $p->aa( 1 );
242 $p->do( $dnssec );
243 $p->rd( $recurse );
244  
245 foreach my $rr ( @{ $self->fake_ds->{ lc( $name ) } } ) {
246 $p->unique_push( 'answer', $rr );
247 }
248  
249 my $res = Zonemaster::Engine::Packet->new( { packet => $p } );
250 Zonemaster::Engine->logger->add( FAKE_DS_RETURNED => { name => "$name", type => $type, class => $class, from => "$self" } );
251 Zonemaster::Engine->logger->add( FAKE_PACKET_RETURNED => { packet => $res->string } );
252 return $res;
253 }
254  
255 # Fake a delegation
256 foreach my $fname ( sort keys %{ $self->fake_delegations } ) {
257 if ( $name =~ m/([.]|\A)\Q$fname\E\z/xi ) {
258 my $p = Zonemaster::LDNS::Packet->new( $name, $type, $class );
259  
260 if ( lc( $name ) eq lc( $fname ) and $type eq 'NS' ) {
261 my $name = $self->fake_delegations->{$fname}{authority};
262 my $addr = $self->fake_delegations->{$fname}{additional};
263 $p->unique_push( 'answer', $_ ) for @{$name};
264 $p->unique_push( 'additional', $_ ) for @{$addr};
265 }
266 elsif ( $type eq 'DS' ) {
267 $p->aa( 1 );
268 }
269 else {
270 while ( my ( $section, $aref ) = each %{ $self->fake_delegations->{$fname} } ) {
271 $p->unique_push( $section, $_ ) for @{$aref};
272 }
273 }
274  
275 $p->aa( 0 ) unless ( $type eq 'DS' );
276 $p->qr( 1 );
277 $p->do( $dnssec );
278 $p->rd( $recurse );
279 $p->answerfrom( $address->ip );
280  
281 Zonemaster::Engine->logger->add( FAKE_DELEGATION_RETURNED => { name => "$name", type => $type, class => $class, from => "$self" } );
282  
283 my $res = Zonemaster::Engine::Packet->new( { packet => $p } );
284 Zonemaster::Engine->logger->add( FAKE_PACKET_RETURNED => { packet => $res->string } );
285 return $res;
286 } ## end if ( $name =~ m/([.]|\A)\Q$fname\E\z/xi)
287 } ## end foreach my $fname ( sort keys...)
288  
289 my $idx = $self->_key_for_query_cache( $name, $type, $href );
290  
291 croak "edns_size (or edns_details->size) parameter must be a value between 0 and 65535" if $edns_size > 65535 or $edns_size < 0;
292  
293 my ( $in_cache, $p ) = $self->cache->get_key( $idx );
294 if ( not $in_cache ) {
295 $p = $self->_query( $name, $type, $href );
296 $self->cache->set_key( $idx, $p );
297 }
298  
299 Zonemaster::Engine->logger->add( CACHED_RETURN => { packet => ( $p ? $p->string : 'undef' ) } );
300  
301 return $p;
302 } ## end sub query
303  
304 sub add_fake_delegation {
305 my ( $self, $domain, $href ) = @_;
306 my %delegation;
307  
308 $domain = q{} . Zonemaster::Engine::DNSName->new( $domain );
309 foreach my $name ( keys %{$href} ) {
310 push @{ $delegation{authority} }, Zonemaster::LDNS::RR->new( sprintf( '%s IN NS %s', $domain, $name ) );
311 foreach my $ip ( @{ $href->{$name} } ) {
312 if ( Net::IP::XS->new( $ip )->ip eq $self->address->ip ) {
313 Zonemaster::Engine->logger->add( FAKE_DELEGATION_TO_SELF => { ns => "$self", domain => $domain, data => $href } );
314 return;
315 }
316  
317 push @{ $delegation{additional} },
318 Zonemaster::LDNS::RR->new( sprintf( '%s IN %s %s', $name, ( Net::IP::XS::ip_is_ipv6( $ip ) ? 'AAAA' : 'A' ), $ip ) );
319 }
320 }
321  
322 $self->fake_delegations->{$domain} = \%delegation;
323 Zonemaster::Engine->logger->add( FAKE_DELEGATION_ADDED => { ns => "$self", domain => $domain, data => $href } );
324  
325 # We're changing the world, so the cache can't be trusted
326 Zonemaster::Engine::Recursor->clear_cache;
327  
328 return;
329 } ## end sub add_fake_delegation
330  
331 sub add_fake_ds {
332 my ( $self, $domain, $aref ) = @_;
333 my @ds;
334  
335 if ( not ref $domain ) {
336 $domain = Zonemaster::Engine::DNSName->new( $domain );
337 }
338  
339 foreach my $href ( @{$aref} ) {
340 push @ds,
341 Zonemaster::LDNS::RR->new(
342 sprintf(
343 '%s IN DS %d %d %d %s',
344 "$domain", $href->{keytag}, $href->{algorithm}, $href->{type}, $href->{digest}
345 )
346 );
347 }
348  
349 $self->fake_ds->{ lc( "$domain" ) } = \@ds;
350 Zonemaster::Engine->logger->add( FAKE_DS_ADDED => { domain => lc( "$domain" ), data => $aref, ns => "$self" } );
351  
352 # We're changing the world, so the cache can't be trusted
353 Zonemaster::Engine::Recursor->clear_cache;
354  
355 return;
356 } ## end sub add_fake_ds
357  
358  
359 # Builds the Zonemaster::LDNS::Packet object that would be sent for a query,
360 # taking options into account.
361  
362 sub _make_query_packet {
363 my ( $self, $qname, $qtype, $opts ) = @_;
364  
365 $qtype //= 'A';
366 my $qclass = $opts->{class} //= 'IN';
367  
368 my $dnssec = do {
369 if ( exists $opts->{edns_details} and exists $opts->{edns_details}{do} ) {
370 $opts->{edns_details}{do};
371 }
372 elsif ( exists $opts->{dnssec} ) {
373 $opts->{dnssec};
374 }
375 else {
376 0;
377 }
378 };
379  
380 my $edns_size = do {
381 if ( exists $opts->{edns_details} and exists $opts->{edns_details}{size} ) {
382 $opts->{edns_details}{size};
383 }
384 elsif ( exists $opts->{edns_size} ) {
385 $opts->{edns_size};
386 }
387 elsif ( $dnssec ) {
388 $EDNS_UDP_PAYLOAD_DNSSEC_DEFAULT;
389 }
390 elsif ( exists $opts->{edns_details} ) {
391 $EDNS_UDP_PAYLOAD_DEFAULT;
392 }
393 else {
394 0;
395 }
396 };
397  
398 die "Invalid value $edns_size for EDNS payload size (should be in 0..65535 range)"
399 unless $edns_size >= 0 and $edns_size <= 65535;
400  
401 my $packet = Zonemaster::LDNS::Packet->new( "$qname", $qtype, $qclass );
402 $packet->rd($opts->{recurse} // 0);
403  
404 if ( exists $opts->{edns_details} ) {
405 $packet->set_edns_present();
406  
407 if ( exists $opts->{edns_details}{version} ) {
408 $packet->edns_version($opts->{edns_details}{version});
409 }
410 if ( exists $opts->{edns_details}{z} ) {
411 $packet->edns_z($opts->{edns_details}{z});
412 }
413 if ( exists $opts->{edns_details}{rcode} ) {
414 $packet->edns_rcode($opts->{edns_details}{rcode});
415 }
416 if ( exists $opts->{edns_details}{data} ) {
417 $packet->edns_data($opts->{edns_details}{data});
418 }
419 }
420  
421 $packet->do($dnssec);
422 $packet->edns_size($edns_size);
423  
424 return $packet;
425 }
426  
427  
428 # Computes the key to use to search the cache for a packet corresponding to a
429 # query we have previously sent.
430  
431 sub _key_for_query_cache {
432 my ( $self, $name, $type, $href ) = @_;
433  
434 my $usevc = $href->{usevc} // 0;
435  
436 my $pkt = $self->_make_query_packet( $name, $type, $href );
437  
438 # Repurpose the ID field. We want this field to be zeroed out
439 # in order to match queries with the exact same contents (except
440 # transaction ID), but we may want cache keys to differ on other
441 # attributes, for example transport.
442 #
443 # Currently the layout is as follows:
444 #
445 # 15 8
446 # +-----+-----+-----+-----+-----+-----+-----+-----+
447 # | TCP | (set to 0) |
448 # +-----+-----+-----+-----+-----+-----+-----+-----+
449 #
450 # 7 0
451 # +-----+-----+-----+-----+-----+-----+-----+-----+
452 # | (set to 0) |
453 # +-----+-----+-----+-----+-----+-----+-----+-----+
454 #
455 # where:
456 # * bit 15 is set to 1 if the query is sent over TCP, 0 otherwise
457  
458 my $fake_id = 0;
459 $fake_id |= (1 << 15) if $usevc;
460  
461 $pkt->id($fake_id);
462  
463 return $pkt->wireformat();
464 }
465  
466 sub _query {
467 my ( $self, $name, $type, $href ) = @_;
468 my %flags;
469  
470 $type //= 'A';
471 $href->{class} //= 'IN';
472  
473 if ( Zonemaster::Engine::Profile->effective->get( q{no_network} ) ) {
474 croak sprintf
475 "External query for %s, %s attempted to %s while running with no_network",
476 $name, $type, $self->string;
477 }
478  
479 Zonemaster::Engine->logger->add(
480 'external_query',
481 {
482 name => "$name",
483 type => $type,
484 flags => $href,
485 ip => $self->address->short
486 }
487 );
488  
489 # Make sure we have a value for each flag
490 $flags{q{retry}} = $href->{q{retry}} // Zonemaster::Engine::Profile->effective->get( q{resolver.defaults.retry} );
491 $flags{q{retrans}} = $href->{q{retrans}}
492 // Zonemaster::Engine::Profile->effective->get( q{resolver.defaults.retrans} );
493 $flags{q{dnssec}} = $href->{q{dnssec}} // 0;
494 $flags{q{usevc}} = $href->{q{usevc}} // 0;
495 $flags{q{igntc}} = $href->{q{igntc}} // 0;
496 $flags{q{fallback}} = $href->{q{fallback}}
497 // Zonemaster::Engine::Profile->effective->get( q{resolver.defaults.fallback} );
498 $flags{q{recurse}} = $href->{q{recurse}} // 0;
499 $flags{q{timeout}} = $href->{q{timeout}}
500 // Zonemaster::Engine::Profile->effective->get( q{resolver.defaults.timeout} );
501  
502 if ( exists $href->{edns_details} ) {
503 $flags{q{dnssec}} = $href->{edns_details}{do} // $flags{q{dnssec}};
504 $flags{q{edns_size}} = $href->{edns_details}{size} // ( $href->{q{edns_size}} // ( $flags{q{dnssec}} ? $EDNS_UDP_PAYLOAD_DNSSEC_DEFAULT : $EDNS_UDP_PAYLOAD_DEFAULT ) );
505 }
506 else {
507 $flags{q{edns_size}} = $href->{q{edns_size}} // ( $flags{q{dnssec}} ? $EDNS_UDP_PAYLOAD_DNSSEC_DEFAULT : 0 );
508 }
509  
510 # Set flags for this query
511 foreach my $flag ( keys %flags ) {
512 $self->dns->$flag( $flags{$flag} );
513 }
514  
515 my $before = time();
516 my $res;
517 if ( $BLACKLISTING_ENABLED and $self->blacklisted->{ $flags{usevc} } ) {
518 Zonemaster::Engine->logger->add(
519 IS_BLACKLISTED => {
520 message => "Server transport has been blacklisted due to previous failure",
521 ns => "$self",
522 name => "$name",
523 type => $type,
524 class => $href->{class},
525 proto => $flags{usevc} ? q{TCP} : q{UDP},
526 dnssec => $flags{dnssec},
527 edns_size => $flags{q{edns_size}}
528 }
529 );
530 }
531 else {
532 my $pkt = $self->_make_query_packet( $name, $type, $href );
533 $res = eval { $self->dns->query_with_pkt( $pkt ) };
534  
535 if ( $@ ) {
536 my $msg = "$@";
537 my $trailing_info = " at ".__FILE__;
538  
539 chomp( $msg );
540 $msg =~ s/$trailing_info.*/\./;
541  
542 Zonemaster::Engine->logger->add( LOOKUP_ERROR =>
543 { message => $msg, ns => "$self", domain => "$name", type => $type, class => $href->{class} } );
544  
545 if ( not $href->{q{blacklisting_disabled}} and $type eq q{SOA} and $flags{q{edns_size}} == 0 ) {
546 $self->blacklisted->{ $flags{usevc} } = 1;
547 Zonemaster::Engine->logger->add( BLACKLISTING =>
548 { ns => "$self", proto => $flags{usevc} ? q{TCP} : q{UDP} } );
549 }
550 }
551 }
552  
553 push @{ $self->times }, ( time() - $before );
554  
555 if ( $res ) {
556 my $p = Zonemaster::Engine::Packet->new( { packet => $res } );
557 my $size = length( $p->data );
558 if ( $size > $EDNS_UDP_PAYLOAD_COMMON_LIMIT ) {
559 my $command = sprintf q{dig @%s %s%s %s}, $self->address->short, $flags{dnssec} ? q{+dnssec } : q{},
560 "$name", $type;
561 Zonemaster::Engine->logger->add(
562 PACKET_BIG => { size => $size, command => $command } );
563 }
564 Zonemaster::Engine->logger->add( EXTERNAL_RESPONSE => { packet => $p->string } );
565 return $p;
566 }
567 else {
568 Zonemaster::Engine->logger->add( EMPTY_RETURN => {} );
569 return;
570 }
571 } ## end sub _query
572  
573 sub string {
574 return $_[0]->{_string};
575 }
576  
577 sub compare {
578 my ( $self, $other, $reverse ) = @_;
579  
580 my $comparison = do {
581 if ( ref $other and $other->isa('Zonemaster::Engine::Nameserver') ) {
582 $self->name cmp $other->name ||
583 $self->address->version <=> $other->address->version ||
584 $self->address->intip <=> $other->address->intip;
585 }
586 else {
587 $self->string cmp $other
588 }
589 };
590  
591 $comparison = -$comparison if $reverse;
592  
593 return $comparison;
594 }
595  
596  
597 # Converts a Zonemaster::Engine::Packet object to a predictable representation
598 # as an array, which can then be turned into CBOR.
599  
600 sub _serialize_packet {
601 my ( $packet ) = @_;
602  
603 return undef if not defined $packet;
604  
605 return [
606 $packet->packet->wireformat(),
607 $packet->packet->answerfrom(),
608 $packet->packet->timestamp(),
609 $packet->packet->querytime(),
610 ];
611 }
612  
613 sub save {
614 my ( $class, $filename ) = @_;
615  
616 open my $fh, '>', $filename or die "Cache save failed: $!";
617  
618 my $dumped_contents = {
619 format_version => 1,
620 engine_version => Zonemaster::Engine->VERSION(),
621 packets => do {
622 # Gives an array of (nameserver, cached packets) pairs.
623 # A nameserver is itself a (name, IP) pair and each cached packet
624 # is represented as a (bytes, source IP, timestamp, query time) tuple,
625 # or undef if there was no response.
626 my @result;
627 foreach my $name ( sort keys %object_cache ) {
628 foreach my $addr ( sort keys %{ $object_cache{$name} } ) {
629 my @ns_packets;
630 my $cached_data = $object_cache{$name}{$addr}->cache->data;
631  
632 next if scalar %$cached_data == 0;
633  
634 foreach my $entry_key ( sort keys %$cached_data ) {
635 push @ns_packets, [ $entry_key, _serialize_packet($cached_data->{$entry_key}) ];
636 }
637 push @result, [ [ $name, $addr ], \@ns_packets ];
638 }
639 };
640 \@result;
641 }
642 };
643  
644 my $cbor = CBOR::XS->new();
645  
646 # We begin the file with the CBOR magic value deliberately, so that we can
647 # reject files that do not start with that signature on loading.
648 print $fh $CBOR::XS::MAGIC, $cbor->encode($dumped_contents);
649  
650 close $fh or die $!;
651  
652 Zonemaster::Engine->logger->add( SAVED_NS_CACHE => { file => $filename } );
653 }
654  
655  
656 # Performs the inverse operation of _serialize_packet(): from a deserialized
657 # CBOR array, reconstructs a Zonemaster::Engine::Packet object.
658  
659 sub _deserialize_packet {
660 my ( $cbor_packet ) = @_;
661  
662 return undef if not defined $cbor_packet;
663  
664 my ( $bytes, $answerfrom, $timestamp, $querytime ) = @$cbor_packet;
665 my $packet = Zonemaster::Engine::Packet->new(
666 { packet => Zonemaster::LDNS::Packet->new_from_wireformat( $bytes ) }
667 );
668 $packet->answerfrom( $answerfrom ) if defined $answerfrom;
669 $packet->timestamp( $timestamp ) if defined $timestamp;
670 $packet->querytime( $querytime ) if defined $querytime;
671  
672 return $packet;
673 }
674  
675 sub restore {
676 my ( $class, $filename ) = @_;
677  
678 open my $fh, '<', $filename or die "Failed to open restore data file: $!\n";
679  
680  
681 # Expect CBOR magic string at beginning of file.
682 my $found_magic = do {
683 my $buf;
684 my $len = read($fh, $buf, length($CBOR::XS::MAGIC));
685 seek($fh, 0, SEEK_SET) or die "seek: $!";
686 ($len == 3 and $buf eq $CBOR::XS::MAGIC);
687 };
688 croak "The restore data file seems to be corrupted" if not $found_magic;
689  
690 my $cbor = CBOR::XS->new();
691 my $saved_contents = $cbor->decode(do { local $/; <$fh> });
692 close $fh;
693  
694 my $format_version = $saved_contents->{format_version};
695 croak "Unsupported format version $format_version" if $format_version != 1;
696  
697 my $cache_type = Zonemaster::Engine::Nameserver::Cache->get_cache_type( Zonemaster::Engine::Profile->effective );
698 my $cache_class = Zonemaster::Engine::Nameserver::Cache->get_cache_class( $cache_type );
699  
700 foreach my $entry ( @{ $saved_contents->{packets} } ) {
701 my ( $ns_pair, $ns_packets ) = @$entry;
702  
703 my $data = {};
704 foreach my $ns_cache_entry ( @$ns_packets ) {
705 my ( $key, $value ) = @$ns_cache_entry;
706 $data->{$key} = _deserialize_packet($value);
707 }
708  
709 my $addr = Net::IP::XS->new( $ns_pair->[1] );
710 my $ns = Zonemaster::Engine::Nameserver->new(
711 {
712 name => $ns_pair->[0],
713 address => $addr,
714 cache => $cache_class->new( { data => $data, address => $addr } )
715 }
716 );
717 }
718  
719 Zonemaster::Engine->logger->add( RESTORED_NS_CACHE => { file => $filename } );
720  
721 return;
722 }
723  
724 sub max_time {
725 my ( $self ) = @_;
726  
727 return max( @{ $self->times } ) // 0;
728 }
729  
730 sub min_time {
731 my ( $self ) = @_;
732  
733 return min( @{ $self->times } ) // 0;
734 }
735  
736 sub sum_time {
737 my ( $self ) = @_;
738  
739 return sum( @{ $self->times } ) // 0;
740 }
741  
742 sub average_time {
743 my ( $self ) = @_;
744  
745 return 0 if @{ $self->times } == 0;
746  
747 return ( $self->sum_time / scalar( @{ $self->times } ) );
748 }
749  
750 sub median_time {
751 my ( $self ) = @_;
752  
753 my @t = sort { $a <=> $b } @{ $self->times };
754 my $c = scalar( @t );
755 if ( $c == 0 ) {
756 return 0;
757 }
758 elsif ( $c % 2 == 0 ) {
759 return ( $t[ $c / 2 ] + $t[ ( $c / 2 ) - 1 ] ) / 2;
760 }
761 else {
762 return $t[ int( $c / 2 ) ];
763 }
764 }
765  
766 sub stddev_time {
767 my ( $self ) = @_;
768  
769 my $avg = $self->average_time;
770 my $c = scalar( @{ $self->times } );
771  
772 return 0 if $c == 0;
773  
774 return sqrt( sum( map { ( $_ - $avg )**2 } @{ $self->times } ) / $c );
775 }
776  
777 sub all_known_nameservers {
778 my @res;
779  
780 foreach my $n ( values %object_cache ) {
781 push @res, values %{$n};
782 }
783  
784 return @res;
785 }
786  
787 sub axfr {
788 my ( $self, $domain, $callback, $class ) = @_;
789 $class //= 'IN';
790  
791 my $idx = $self->_key_for_query_cache( $domain, 'AXFR', { class => $class, usevc => 1 } );
792 my ( $in_cache, $p ) = $self->cache->get_key( $idx );
793  
794 if ( $in_cache ) {
795 if ( $p->rcode() ne 'NOERROR' ) {
796 # Croak with the same error message the real AXFR croaked with.
797 my ( undef, $ede_text ) = $p->packet->first_ede();
798 croak $ede_text // "AXFR transfer error: REFUSED";
799 }
800  
801 my $last_ret = 1;
802 foreach my $rr ( $p->answer() ) {
803 $last_ret = $callback->($rr);
804 last if $last_ret == 0;
805 }
806 return $last_ret;
807 }
808 else {
809 my ( $ret, $error, $p ) = $self->_axfr( $domain, $callback, $class );
810 $self->cache->set_key( $idx, $p );
811 croak $error if defined $error;
812 return $ret;
813 }
814 }
815  
816 sub _axfr {
817 my ( $self, $domain, $callback, $class ) = @_;
818  
819 if ( Zonemaster::Engine::Profile->effective->get( q{no_network} ) ) {
820 croak sprintf
821 "External AXFR query for %s attempted to %s while running with no_network",
822 $domain, $self->string;
823 }
824  
825 if ( $self->address->version == 4 and not Zonemaster::Engine::Profile->effective->get( q{net.ipv4} ) ) {
826 Zonemaster::Engine->logger->add( IPV4_BLOCKED => { ns => $self->string } );
827 return;
828 }
829  
830 if ( $self->address->version == 6 and not Zonemaster::Engine::Profile->effective->get( q{net.ipv6} ) ) {
831 Zonemaster::Engine->logger->add( IPV6_BLOCKED => { ns => $self->string } );
832 return;
833 }
834  
835 my @rrs;
836 my $wrapped_callback = sub {
837 push @rrs, $_[0];
838 return $callback->( @_ );
839 };
840  
841 my $t0 = [gettimeofday];
842 my $ret = eval { $self->dns->axfr( $domain, $wrapped_callback, $class ) };
843 my $error = $@ if not defined $ret;
844 my $querytime = tv_interval($t0);
845  
846 # Build a synthetic packet containing all the resource records we
847 # collected, so that this AXFR can be cached (and therefore replayed)
848 # adequately.
849 my $p = Zonemaster::Engine::Packet->new({
850 packet => Zonemaster::LDNS::Packet->new( $domain, 'AXFR', $class )
851 });
852  
853 $p->timestamp(time());
854 $p->querytime($querytime * 1000);
855 $p->answerfrom($self->address->short);
856 $p->id(0);
857 $p->packet->qr(1);
858 if ( defined $error ) {
859 $p->rcode('REFUSED');
860 # Use an Extended DNS Error 13 (Cached Error) in the synthetic packet
861 # to store the original error message.
862 my $file = __FILE__;
863 chomp $error;
864 $error =~ s/ at $file line \d+\.$//;
865 $p->packet->first_ede( 13, $error );
866 }
867 $p->packet->aa(1);
868 $p->unique_push( 'answer', $_ ) foreach @rrs;
869  
870 return $ret, $error, $p;
871 }
872  
873 sub source_address {
874 my ( $self ) = @_;
875  
876 my $src_address = Zonemaster::Engine::Profile->effective->get( "resolver.source" . Net::IP::XS::ip_get_version( $self->address->ip ) );
877  
878 return $src_address eq '' ? undef : $src_address;
879 }
880  
881 sub empty_cache {
882 %object_cache = ();
883 %address_object_cache = ();
884 %address_repr_cache = ();
885  
886 Zonemaster::Engine::Nameserver::Cache::empty_cache();
887  
888 return;
889 }
890  
891 1;
892  
893 =head1 NAME
894  
895 Zonemaster::Engine::Nameserver - object representing a DNS nameserver
896  
897 =head1 SYNOPSIS
898  
899 my $ns = Zonemaster::Engine::Nameserver->new({ name => 'ns.nic.se', address => '212.247.7.228' });
900 my $p = $ns->query('www.iis.se', 'AAAA');
901  
902 =head1 DESCRIPTION
903  
904 This is a very central object in the L<Zonemaster::Engine> framework. All DNS
905 communications with the outside world pass through here, so we can do
906 things like synthesizing and recording traffic. All the objects are
907 also unique per name/IP pair, and creating a new one with an already
908 existing pair will return the existing object instead of creating a
909 new one. Queries and their responses are cached by IP address, so that
910 a specific query will only be sent once to each address (even if there
911 are multiple objects for that address with different names).
912  
913 Class methods on this class allows saving and loading cache contents.
914  
915 =head1 ATTRIBUTES
916  
917 =over
918  
919 =item name
920  
921 A L<Zonemaster::Engine::DNSName> object holding the nameserver's name.
922  
923 =item address
924  
925 A L<Net::IP::XS> object holding the nameserver's address.
926  
927 =item dns
928  
929 The L<Zonemaster::LDNS> object used to actually send and receive DNS queries.
930  
931 =item cache
932  
933 A reference to a L<Zonemaster::Engine::Nameserver::Cache> object holding the cache of sent queries. Not meant for external use.
934  
935 =item times
936  
937 A reference to a list with elapsed time values for the queries made through this nameserver.
938  
939 =item blacklisted
940  
941 A reference to a hash used to prevent sending subsequent queries to the name server after specific queries have failed.
942  
943 The mechanism will only trigger on no response from non-EDNS SOA queries and is protocol dependent (i.e. TCP/UDP). It can be disabled
944 on a per query basis with L<blacklisting_disabled>, or globally with L<Zonemaster::Engine::Constants/$BLACKLISTING_ENABLED>.
945  
946 =back
947  
948 =head1 CLASS METHODS
949  
950 =over
951  
952 =item new
953  
954 Construct a new object.
955  
956 =item save($filename)
957  
958 Save the entire object cache to the given filename, using the
959 byte-order-independent Storable format.
960  
961 =item restore($filename)
962  
963 Replace the entire object cache with the contents of the named file.
964  
965 =item all_known_nameservers()
966  
967 Class method that returns a list of all nameserver objects in the global cache.
968  
969 =item empty_cache()
970  
971 Remove all cached nameserver objects and queries.
972  
973 =back
974  
975 =head1 INSTANCE METHODS
976  
977 =over
978  
979 =item query($name, $type, $flagref)
980  
981 Send a DNS query to the nameserver the object represents. C<$name> and C<$type> are the name and type that will be queried for (C<$type> defaults
982 to 'A' if it's left undefined). C<$flagref> is a reference to a hash, the keys of which are flags and the values are their corresponding values.
983 The available flags are as follows. All but 'class' and 'edns_details' directly correspond to methods in the L<Zonemaster::LDNS> object.
984  
985 =over
986  
987 =item class
988  
989 Defaults to 'IN' if not set.
990  
991 =item usevc
992  
993 Send the query via TCP (only).
994  
995 =item retrans
996  
997 The retransmission interval.
998  
999 =item dnssec
1000  
1001 Set the DO flag in the query. Defaults to false.
1002  
1003 If set to true, it becomes an EDNS query.
1004 Value overridden by C<edns_details{do}> (if also given). More details in L<edns_details> below.
1005  
1006 =item debug
1007  
1008 Set the debug flag in the resolver, producing output on STDERR as the query process proceeds.
1009  
1010 =item recurse
1011  
1012 Set the RD flag in the query.
1013  
1014 =item timeout
1015  
1016 Set the timeout for the outgoing sockets. May or may not be observed by the underlying network stack.
1017  
1018 =item retry
1019  
1020 Set the number of times the query is tried.
1021  
1022 =item igntc
1023  
1024 If set to true, incoming response packets with the TC flag set are not automatically retried over TCP.
1025  
1026 =item fallback
1027  
1028 If set to true, incoming response packets with the TC flag set fall back to EDNS and/or TCP.
1029  
1030 =item blacklisting_disabled
1031  
1032 If set to true, prevents a name server from being blacklisted.
1033  
1034 =item edns_size
1035  
1036 Set the EDNS0 UDP maximum size. The value must be comprised between 0 and 65535.
1037 Defaults to 0, or 512 if the query is a non-DNSSEC EDNS query, or 1232 if the query is a DNSSEC query.
1038  
1039 Setting a value other than 0 will also implicitly enable EDNS for the query.
1040 Value overridden by C<edns_details-E<gt>{size}> (if also given). More details in L<edns_details> below.
1041  
1042 =item edns_details
1043  
1044 A hash. An empty hash or a hash with any keys below will enable EDNS for the query.
1045  
1046 The currently supported keys are 'version', 'z', 'do', 'rcode', 'size' and 'data'.
1047 See L<Zonemaster::LDNS::Packet> for more details (key names prefixed with 'edns_').
1048  
1049 Note that flag L<edns_size> also exists (see above) and has the same effect as C<edns_details-E<gt>{size}>, although the value of the
1050 latter will take precedence if both are given.
1051  
1052 Similarly, note that flag L<dnssec> also exists (see above) and has the same effect as C<edns_details-E<gt>{do}>, although the value of the
1053 latter will take precedence if both are given.
1054  
1055 =back
1056  
1057 =item string()
1058  
1059 Returns a string representation of the object. Normally this is just the name and IP address separated by a slash.
1060  
1061 =item compare($other)
1062  
1063 Used for overloading comparison operators.
1064  
1065 =item sum_time()
1066  
1067 Returns the total time spent sending queries and waiting for responses.
1068  
1069 =item min_time()
1070  
1071 Returns the shortest time spent on a query.
1072  
1073 =item max_time()
1074  
1075 Returns the longest time spent on a query.
1076  
1077 =item average_time()
1078  
1079 Returns the average time spent on queries.
1080  
1081 =item median_time()
1082  
1083 Returns the median query time.
1084  
1085 =item stddev_time()
1086  
1087 Returns the standard deviation for the whole set of query times.
1088  
1089 =item add_fake_delegation($domain,$data)
1090  
1091 Adds fake delegation information to this specific nameserver object. Takes the
1092 same arguments as the similarly named method in L<Zonemaster::Engine>. This is
1093 primarily used for internal information, and using it directly will likely give
1094 confusing results (but may be useful to model certain kinds of
1095 misconfigurations).
1096  
1097 =item add_fake_ds($domain, $data)
1098  
1099 Adds fake DS information to this nameserver object. Takes the same arguments as
1100 the similarly named method in L<Zonemaster::Engine>.
1101  
1102 =item axfr( $domain, $callback, $class )
1103  
1104 Does an AXFR for the requested domain from the nameserver. The callback
1105 function will be called once for each received RR, with that RR as its only
1106 argument. To continue getting more RRs, the callback must return a true value.
1107 If it returns a true value, the AXFR will be aborted. See L<Zonemaster::LDNS::axfr>
1108 for more details.
1109  
1110 =item source_address()
1111  
1112 my $src_address = source_address();
1113  
1114 Returns the configured IPv4 or IPv6 source address to be used by the underlying DNS resolver for sending queries,
1115 or C<undef> if the source address is the empty string.
1116  
1117 =item empty_cache()
1118  
1119 Clears the caches of Zonemaster::Engine::Nameserver (name server names and IP addresses) and Zonemaster::Engine::Nameserver::Cache (query and response packets) objects.
1120  
1121 =back
1122  
1123 =cut
« Zonemaster/Engine.pm ↑ Summary