| 1 | package Zonemaster::Engine; | ||
| 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("v8.1.1"); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | BEGIN { | ||
| 9 | # Locale::TextDomain (<= 1.20) doesn't know about File::ShareDir so give a helping hand. | ||
| 10 | # This is a hugely simplified version of the reference implementation located here: | ||
| 11 | # https://metacpan.org/source/GUIDO/libintl-perl-1.21/lib/Locale/TextDomain.pm | ||
| 12 | 1 | ▶ | require File::ShareDir; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | require Locale::TextDomain; | |
| 14 | 1 | my $share = File::ShareDir::dist_dir( 'Zonemaster-Engine' ); | |
| 15 | 1 | Locale::TextDomain->import( 'Zonemaster-Engine', "$share/locale" ); | |
| 16 | } | ||
| 17 | |||
| 18 | 1 | ▶ | use Class::Accessor "antlers"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | |||
| 21 | 1 | ▶ | use Zonemaster::Engine::Nameserver; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 22 | use Zonemaster::Engine::Logger; | ||
| 23 | use Zonemaster::Engine::Profile; | ||
| 24 | use Zonemaster::Engine::Zone; | ||
| 25 | use Zonemaster::Engine::Test; | ||
| 26 | use Zonemaster::Engine::Recursor; | ||
| 27 | use Zonemaster::Engine::ASNLookup; | ||
| 28 | |||
| 29 | INIT { | ||
| 30 | init_engine(); | ||
| 31 | } | ||
| 32 | |||
| 33 | our $logger; | ||
| 34 | our $recursor = Zonemaster::Engine::Recursor->new; | ||
| 35 | |||
| 36 | my $init_done = 0; | ||
| 37 | |||
| 38 | sub init_engine { | ||
| 39 | return if $init_done++; | ||
| 40 | Zonemaster::Engine::Recursor::init_recursor(); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub logger { | ||
| 44 | return $logger //= Zonemaster::Engine::Logger->new; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub profile { | ||
| 48 | return Zonemaster::Engine::Profile->effective; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub ns { | ||
| 52 | my ( $class, $name, $address ) = @_; | ||
| 53 | |||
| 54 | return Zonemaster::Engine::Nameserver->new( { name => $name, address => $address } ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub zone { | ||
| 58 | my ( $class, $name ) = @_; | ||
| 59 | |||
| 60 | return Zonemaster::Engine::Zone->new( { name => Zonemaster::Engine::DNSName->new( $name ) } ); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub test_zone { | ||
| 64 | my ( $class, $zname ) = @_; | ||
| 65 | |||
| 66 | return Zonemaster::Engine::Test->run_all_for( $class->zone( $zname ) ); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub test_module { | ||
| 70 | my ( $class, $module, $zname ) = @_; | ||
| 71 | |||
| 72 | return Zonemaster::Engine::Test->run_module( $module, $class->zone( $zname ) ); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub test_method { | ||
| 76 | my ( $class, $module, $method, $zname ) = @_; | ||
| 77 | |||
| 78 | return Zonemaster::Engine::Test->run_one( $module, $method, $class->zone( $zname ) ); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub all_tags { | ||
| 82 | my ( $class ) = @_; | ||
| 83 | my @res; | ||
| 84 | |||
| 85 | foreach my $module ( sort { $a cmp $b } Zonemaster::Engine::Test->modules ) { | ||
| 86 | my $full = "Zonemaster::Engine::Test::$module"; | ||
| 87 | my $ref = $full->metadata; | ||
| 88 | foreach my $list ( values %{$ref} ) { | ||
| 89 | push @res, map { uc( $module ) . q{:} . $_ } sort { $a cmp $b } @{$list}; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | return @res; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub all_methods { | ||
| 97 | my ( $class ) = @_; | ||
| 98 | my %res; | ||
| 99 | |||
| 100 | foreach my $module ( Zonemaster::Engine::Test->modules ) { | ||
| 101 | my $full = "Zonemaster::Engine::Test::$module"; | ||
| 102 | my $ref = $full->metadata; | ||
| 103 | foreach my $method ( sort { $a cmp $b } keys %{$ref} ) { | ||
| 104 | push @{ $res{$module} }, $method; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | return %res; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub recurse { | ||
| 112 | my ( $class, $qname, $qtype, $qclass ) = @_; | ||
| 113 | $qtype //= 'A'; | ||
| 114 | $qclass //= 'IN'; | ||
| 115 | |||
| 116 | return $recursor->recurse( $qname, $qtype, $qclass ); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub add_fake_delegation { | ||
| 120 | my ( $class, $domain, $href, %flags ) = @_; | ||
| 121 | my $fill_in_empty_oob_glue = exists $flags{fill_in_empty_oob_glue} ? delete $flags{fill_in_empty_oob_glue} : 1; | ||
| 122 | croak 'Unrecognized flags: ' . join( ', ', keys %flags ) | ||
| 123 | if %flags; | ||
| 124 | undef %flags; | ||
| 125 | |||
| 126 | # Validate arguments | ||
| 127 | $domain =~ /[^.]$|^\.$/ | ||
| 128 | or croak 'Argument $domain must omit the trailing dot, or it must be a single dot'; | ||
| 129 | foreach my $name ( keys %{$href} ) { | ||
| 130 | $name =~ /[^.]$|^\.$/ | ||
| 131 | or croak 'Each key of argument $href must omit the trailing dot, or it must be a single dot'; | ||
| 132 | ( !defined $href->{$name} or ref $href->{$name} eq 'ARRAY' ) | ||
| 133 | or croak 'Each value of argument $href must be an arrayref or undef'; | ||
| 134 | $href->{$name} //= []; # normalize undef to empty arrayref | ||
| 135 | } | ||
| 136 | |||
| 137 | # Check fake delegation | ||
| 138 | my $incomplete_delegation; | ||
| 139 | if ( $fill_in_empty_oob_glue ) { | ||
| 140 | foreach my $name ( keys %{$href} ) { | ||
| 141 | if ( !@{ $href->{$name} } | ||
| 142 | && !$class->zone( $domain )->is_in_zone( $name ) ) | ||
| 143 | { | ||
| 144 | my @ips = map { $_->ip } Zonemaster::Engine::Recursor->get_addresses_for( $name ); | ||
| 145 | push @{ $href->{$name} }, @ips; | ||
| 146 | if ( !@ips ) { | ||
| 147 | $incomplete_delegation = 1; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | foreach my $name ( keys %{$href} ) { | ||
| 153 | if ( not @{ $href->{$name} } ) { | ||
| 154 | if ( $class->zone( $domain )->is_in_zone( $name ) ) { | ||
| 155 | Zonemaster::Engine->logger->add( # | ||
| 156 | FAKE_DELEGATION_IN_ZONE_NO_IP => { domain => $domain, nsname => $name } | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | Zonemaster::Engine->logger->add( # | ||
| 161 | FAKE_DELEGATION_NO_IP => { domain => $domain, nsname => $name } | ||
| 162 | ); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | $recursor->add_fake_addresses( $domain, $href ); | ||
| 168 | my $parent = $class->zone( $recursor->parent( $domain ) ); | ||
| 169 | foreach my $ns ( @{ $parent->ns } ) { | ||
| 170 | $ns->add_fake_delegation( $domain => $href ); | ||
| 171 | } | ||
| 172 | |||
| 173 | if ( $incomplete_delegation ) { | ||
| 174 | return; | ||
| 175 | } | ||
| 176 | return 1; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub add_fake_ds { | ||
| 180 | my ( $class, $domain, $aref ) = @_; | ||
| 181 | |||
| 182 | my $parent = $class->zone( scalar( $recursor->parent( $domain ) ) ); | ||
| 183 | if ( not $parent ) { | ||
| 184 | die "Failed to find parent for $domain"; | ||
| 185 | } | ||
| 186 | |||
| 187 | foreach my $ns ( @{ $parent->ns } ) { | ||
| 188 | $ns->add_fake_ds( $domain => $aref ); | ||
| 189 | } | ||
| 190 | |||
| 191 | return; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub can_continue { | ||
| 195 | my ( $class ) = @_; | ||
| 196 | |||
| 197 | return 1; | ||
| 198 | |||
| 199 | } | ||
| 200 | |||
| 201 | sub save_cache { | ||
| 202 | my ( $class, $filename ) = @_; | ||
| 203 | |||
| 204 | return Zonemaster::Engine::Nameserver->save( $filename ); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub preload_cache { | ||
| 208 | my ( $class, $filename ) = @_; | ||
| 209 | |||
| 210 | return Zonemaster::Engine::Nameserver->restore( $filename ); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub asn_lookup { | ||
| 214 | my ( undef, $ip ) = @_; | ||
| 215 | |||
| 216 | return Zonemaster::Engine::ASNLookup->get( $ip ); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub modules { | ||
| 220 | return Zonemaster::Engine::Test->modules; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub start_time_now { | ||
| 224 | Zonemaster::Engine::Logger->start_time_now(); | ||
| 225 | return; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub reset { | ||
| 229 | Zonemaster::Engine::Logger->start_time_now(); | ||
| 230 | Zonemaster::Engine::Logger->reset_config(); | ||
| 231 | Zonemaster::Engine::Nameserver->empty_cache(); | ||
| 232 | $logger->clear_history() if $logger; | ||
| 233 | Zonemaster::Engine::Recursor->clear_cache(); | ||
| 234 | Zonemaster::Engine::TestMethodsV2->clear_cache(); | ||
| 235 | return; | ||
| 236 | } | ||
| 237 | |||
| 238 | =head1 NAME | ||
| 239 | |||
| 240 | Zonemaster::Engine - A tool to check the quality of a DNS zone | ||
| 241 | |||
| 242 | =head1 SYNOPSIS | ||
| 243 | |||
| 244 | my @results = Zonemaster::Engine->test_zone('iis.se') | ||
| 245 | |||
| 246 | =head1 INTRODUCTION | ||
| 247 | |||
| 248 | This manual describes the main L<Zonemaster::Engine> module. If what you're after is documentation on the Zonemaster test engine as a whole, see L<Zonemaster::Engine::Overview>. | ||
| 249 | |||
| 250 | =head1 METHODS | ||
| 251 | |||
| 252 | =over | ||
| 253 | |||
| 254 | =item init_engine() | ||
| 255 | |||
| 256 | Run the initialization tasks if they have not been run already. This method is called automatically in INIT block. | ||
| 257 | |||
| 258 | =item test_zone($name) | ||
| 259 | |||
| 260 | Runs all available tests and returns a list of L<Zonemaster::Engine::Logger::Entry> objects. | ||
| 261 | |||
| 262 | =item test_module($module, $name) | ||
| 263 | |||
| 264 | Runs all available tests for the zone with the given name in the specified module. | ||
| 265 | |||
| 266 | =item test_method($module, $method, $name) | ||
| 267 | |||
| 268 | Run one particular test method in one particular module for one particular zone. | ||
| 269 | The requested module must be in the list of currently enabled modules (that is, | ||
| 270 | not a module disabled by the current profile), and the method must be listed in | ||
| 271 | the metadata of the module exports. | ||
| 272 | If those requirements are fulfilled, the method will be called with the provided | ||
| 273 | arguments. | ||
| 274 | |||
| 275 | =item zone($name) | ||
| 276 | |||
| 277 | Returns a L<Zonemaster::Engine::Zone> object for the given name. | ||
| 278 | |||
| 279 | =item ns($name, $address) | ||
| 280 | |||
| 281 | Returns a L<Zonemaster::Engine::Nameserver> object for the given name and address. | ||
| 282 | |||
| 283 | =item profile() | ||
| 284 | |||
| 285 | Returns the effective profile (L<Zonemaster::Engine::Profile> object). | ||
| 286 | |||
| 287 | =item logger() | ||
| 288 | |||
| 289 | Returns the global L<Zonemaster::Engine::Logger> object. | ||
| 290 | |||
| 291 | =item all_tags() | ||
| 292 | |||
| 293 | Returns a list of all the tags that can be logged for all available test modules. | ||
| 294 | |||
| 295 | =item all_methods() | ||
| 296 | |||
| 297 | Returns a hash, where the keys are test module names and the values are lists with the names of the test methods in that module. | ||
| 298 | |||
| 299 | =item recurse($name, $type, $class) | ||
| 300 | |||
| 301 | Does a recursive lookup for the given name, type and class, and returns the resulting packet (if any). Simply calls | ||
| 302 | L<Zonemaster::Engine::Recursor/recurse> on a globally stored object. | ||
| 303 | |||
| 304 | =item can_continue() | ||
| 305 | |||
| 306 | In case of critical condition that prevents tool to process tests, add test here and return False. | ||
| 307 | |||
| 308 | =item save_cache($filename) | ||
| 309 | |||
| 310 | After running the tests, save the accumulated cache to a file with the given name. | ||
| 311 | |||
| 312 | =item preload_cache($filename) | ||
| 313 | |||
| 314 | Before running the tests, load the cache with information from a file with the given name. This file must have the same format as is produced by | ||
| 315 | L</save_cache()>. | ||
| 316 | |||
| 317 | =item asn_lookup($ip) | ||
| 318 | |||
| 319 | Takes a single IP address (string or L<Net::IP::XS> object) and returns a list of AS numbers, if any. | ||
| 320 | |||
| 321 | =item modules() | ||
| 322 | |||
| 323 | Returns a list of the loaded test modules. Exactly the same as L<Zonemaster::Engine::Test/modules>. | ||
| 324 | |||
| 325 | =item add_fake_delegation($domain, $data, %flags) | ||
| 326 | |||
| 327 | This method adds some fake delegation information to the system. | ||
| 328 | |||
| 329 | The arguments are a domain name, and a hashref with delegation information. | ||
| 330 | The keys in the hash are nameserver names, and the values are arrayrefs of IP | ||
| 331 | addresses for their corresponding nameserver. | ||
| 332 | Alternatively the IP addresses may be specified as an `undef` which is handled | ||
| 333 | the same as an empty arrayref. | ||
| 334 | |||
| 335 | For each provided nameserver with an empty list of addresses, either a | ||
| 336 | C<FAKE_DELEGATION_NO_IP> or a C<FAKE_DELEGATION_IN_ZONE_NO_IP> message is | ||
| 337 | emitted. | ||
| 338 | |||
| 339 | The only recognized flag is C<fill_in_empty_oob_glue>. | ||
| 340 | This flag is boolean and defaults to true. | ||
| 341 | If this flag is true, this method updates the given C<$data> by looking up and | ||
| 342 | filling in some glue addresses. | ||
| 343 | Specifically the glue addresses for any nameserver name that are | ||
| 344 | out-of-bailiwick of the given C<$domain> and that comes with an empty list of | ||
| 345 | addresses. | ||
| 346 | |||
| 347 | Returns `1` if all name servers in C<$data> have non-empty lists of | ||
| 348 | glue (after they've been filled in) or if `fill_in_empty_oob_glue` is false. | ||
| 349 | Otherwise it returns `undef`. | ||
| 350 | |||
| 351 | Examples: | ||
| 352 | |||
| 353 | Zonemaster::Engine->add_fake_delegation( | ||
| 354 | 'lysator.liu.se' => { | ||
| 355 | 'ns1.nic.fr' => [ ], | ||
| 356 | 'ns.nic.se' => [ '212.247.7.228', '2a00:801:f0:53::53' ], | ||
| 357 | 'i.ns.se' => [ '194.146.106.22', '2001:67c:1010:5::53' ], | ||
| 358 | 'ns3.nic.se' => [ '212.247.8.152', '2a00:801:f0:211::152' ] | ||
| 359 | }, | ||
| 360 | ); | ||
| 361 | |||
| 362 | returns 1. | ||
| 363 | |||
| 364 | Zonemaster::Engine->add_fake_delegation( | ||
| 365 | 'lysator.liu.se' => { | ||
| 366 | 'ns1.lysator.liu.se' => [ ], | ||
| 367 | 'ns.nic.se' => [ '212.247.7.228', '2a00:801:f0:53::53' ], | ||
| 368 | 'i.ns.se' => [ '194.146.106.22', '2001:67c:1010:5::53' ], | ||
| 369 | 'ns3.nic.se' => [ '212.247.8.152', '2a00:801:f0:211::152' ] | ||
| 370 | } | ||
| 371 | ); | ||
| 372 | |||
| 373 | returns C<undef> (signalling that fake delegation with empty glue was added to | ||
| 374 | the system). | ||
| 375 | |||
| 376 | Zonemaster::Engine->add_fake_delegation( | ||
| 377 | 'lysator.liu.se' => { | ||
| 378 | 'ns1.nic.fr' => [ ], | ||
| 379 | 'ns.nic.se' => [ '212.247.7.228', '2a00:801:f0:53::53' ], | ||
| 380 | 'i.ns.se' => [ '194.146.106.22', '2001:67c:1010:5::53' ], | ||
| 381 | 'ns3.nic.se' => [ '212.247.8.152', '2a00:801:f0:211::152' ] | ||
| 382 | }, | ||
| 383 | fill_in_empty_oob_glue => 0, | ||
| 384 | ); | ||
| 385 | |||
| 386 | returns 1. It does not even attempt to fill in glue for ns1.nic.fr. | ||
| 387 | |||
| 388 | =item add_fake_ds($domain, $data) | ||
| 389 | |||
| 390 | This method adds fake DS records to the system. The arguments are a domain | ||
| 391 | name, and a reference to a list of references to hashes. The hashes in turn | ||
| 392 | must have the keys C<keytag>, C<algorithm>, C<type> and C<digest>, with the | ||
| 393 | values holding the corresponding data. The digest data should be a single | ||
| 394 | unbroken string of hexadecimal digits. | ||
| 395 | |||
| 396 | Example: | ||
| 397 | |||
| 398 | Zonemaster::Engine->add_fake_ds( | ||
| 399 | 'nic.se' => [ | ||
| 400 | { keytag => 16696, algorithm => 5, type => 2, digest => '40079DDF8D09E7F10BB248A69B6630478A28EF969DDE399F95BC3B39F8CBACD7' }, | ||
| 401 | { keytag => 16696, algorithm => 5, type => 1, digest => 'EF5D421412A5EAF1230071AFFD4F585E3B2B1A60' }, | ||
| 402 | ] | ||
| 403 | ); | ||
| 404 | |||
| 405 | =item start_time_now() | ||
| 406 | |||
| 407 | Set the logger's start time to the current time. | ||
| 408 | |||
| 409 | =item reset() | ||
| 410 | |||
| 411 | Reset logger start time to current time, empty the list of log messages, clear | ||
| 412 | nameserver object cache, clear recursor cache and clear all cached results of | ||
| 413 | MethodsV2. | ||
| 414 | |||
| 415 | =back | ||
| 416 | |||
| 417 | =head1 AUTHORS | ||
| 418 | |||
| 419 | Vincent Levigneron <vincent.levigneron at nic.fr> | ||
| 420 | - Current maintainer | ||
| 421 | |||
| 422 | Calle Dybedahl <calle at init.se> | ||
| 423 | - Original author | ||
| 424 | |||
| 425 | =head1 LICENSE | ||
| 426 | |||
| 427 | This is free software under a 2-clause BSD license. The full text of the license can | ||
| 428 | be found in the F<LICENSE> file included with this distribution. | ||
| 429 | |||
| 430 | =cut | ||
| 431 | |||
| 432 | 1; | ||