| 1 | package Zonemaster v1.0.3; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.014002; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Zonemaster::Nameserver; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Zonemaster::Logger; | ||
| 8 | use Zonemaster::Config; | ||
| 9 | use Zonemaster::Zone; | ||
| 10 | use Zonemaster::Test; | ||
| 11 | use Zonemaster::Recursor; | ||
| 12 | use Zonemaster::ASNLookup; | ||
| 13 | |||
| 14 | our $logger; | ||
| 15 | our $config; | ||
| 16 | our $recursor = Zonemaster::Recursor->new; | ||
| 17 | |||
| 18 | sub logger { | ||
| 19 | return $logger //= Zonemaster::Logger->new; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub config { | ||
| 23 | if ( not defined $config ) { | ||
| 24 | $config = Zonemaster::Config->new; | ||
| 25 | } | ||
| 26 | |||
| 27 | return $config; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub ns { | ||
| 31 | my ( $class, $name, $address ) = @_; | ||
| 32 | |||
| 33 | return Zonemaster::Nameserver->new( { name => $name, address => $address } ); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub zone { | ||
| 37 | my ( $class, $name ) = @_; | ||
| 38 | |||
| 39 | return Zonemaster::Zone->new( { name => $name } ); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub test_zone { | ||
| 43 | my ( $class, $zname ) = @_; | ||
| 44 | |||
| 45 | return Zonemaster::Test->run_all_for( $class->zone( $zname ) ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub test_module { | ||
| 49 | my ( $class, $module, $zname ) = @_; | ||
| 50 | |||
| 51 | return Zonemaster::Test->run_module( $module, $class->zone( $zname ) ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub test_method { | ||
| 55 | my ( $class, $module, $method, @arguments ) = @_; | ||
| 56 | |||
| 57 | return Zonemaster::Test->run_one( $module, $method, @arguments ); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub all_tags { | ||
| 61 | my ( $class ) = @_; | ||
| 62 | my @res; | ||
| 63 | |||
| 64 | foreach my $module ( 'Basic', sort { $a cmp $b } Zonemaster::Test->modules ) { | ||
| 65 | my $full = "Zonemaster::Test::$module"; | ||
| 66 | my $ref = $full->metadata; | ||
| 67 | foreach my $list ( values %$ref ) { | ||
| 68 | push @res, map { uc( $module ) . ':' . $_ } sort { $a cmp $b } @$list; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | return @res; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub all_methods { | ||
| 76 | my ( $class ) = @_; | ||
| 77 | my %res; | ||
| 78 | |||
| 79 | foreach my $module ( 'Basic', Zonemaster::Test->modules ) { | ||
| 80 | my $full = "Zonemaster::Test::$module"; | ||
| 81 | my $ref = $full->metadata; | ||
| 82 | foreach my $method ( sort { $a cmp $b } keys %$ref ) { | ||
| 83 | push @{ $res{$module} }, $method; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | return %res; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub recurse { | ||
| 91 | my ( $class, $qname, $qtype, $qclass ) = @_; | ||
| 92 | $qtype //= 'A'; | ||
| 93 | $qclass //= 'IN'; | ||
| 94 | |||
| 95 | return $recursor->recurse( $qname, $qtype, $qclass ); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub add_fake_delegation { | ||
| 99 | my ( $class, $domain, $href ) = @_; | ||
| 100 | |||
| 101 | my $parent = $class->zone( $recursor->parent( $domain ) ); | ||
| 102 | foreach my $ns ( @{ $parent->ns } ) { | ||
| 103 | $ns->add_fake_delegation( $domain => $href ); | ||
| 104 | } | ||
| 105 | |||
| 106 | return; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub add_fake_ds { | ||
| 110 | my ( $class, $domain, $aref ) = @_; | ||
| 111 | |||
| 112 | my $parent = $class->zone( scalar( $recursor->parent( $domain ) ) ); | ||
| 113 | if ( not $parent ) { | ||
| 114 | die "Failed to find parent for $domain"; | ||
| 115 | } | ||
| 116 | |||
| 117 | foreach my $ns ( @{ $parent->ns } ) { | ||
| 118 | $ns->add_fake_ds( $domain => $aref ); | ||
| 119 | } | ||
| 120 | |||
| 121 | return; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub save_cache { | ||
| 125 | my ( $class, $filename ) = @_; | ||
| 126 | |||
| 127 | return Zonemaster::Nameserver->save( $filename ); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub preload_cache { | ||
| 131 | my ( $class, $filename ) = @_; | ||
| 132 | |||
| 133 | return Zonemaster::Nameserver->restore( $filename ); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub asn_lookup { | ||
| 137 | my ( undef, $ip ) = @_; | ||
| 138 | |||
| 139 | return Zonemaster::ASNLookup->get( $ip ); | ||
| 140 | } | ||
| 141 | |||
| 142 | sub modules { | ||
| 143 | return Zonemaster::Test->modules; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub start_time_now { | ||
| 147 | Zonemaster::Logger->start_time_now(); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub reset { | ||
| 151 | Zonemaster::Logger->start_time_now(); | ||
| 152 | Zonemaster::Nameserver->empty_cache(); | ||
| 153 | $logger->clear_history() if $logger; | ||
| 154 | Zonemaster::Recursor->clear_cache(); | ||
| 155 | |||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | =head1 NAME | ||
| 160 | |||
| 161 | Zonemaster - A tool to check the quality of a DNS zone | ||
| 162 | |||
| 163 | =head1 SYNOPSIS | ||
| 164 | |||
| 165 | my @results = Zonemaster->test_zone('iis.se') | ||
| 166 | |||
| 167 | =head1 INTRODUCTION | ||
| 168 | |||
| 169 | This manual describes the main L<Zonemaster> module. If what you're after is documentation on the Zonemaster test engine as a whole, see L<Zonemaster::Overview>. | ||
| 170 | |||
| 171 | =head1 METHODS | ||
| 172 | |||
| 173 | =over | ||
| 174 | |||
| 175 | =item test_zone($name) | ||
| 176 | |||
| 177 | Runs all available tests and returns a list of L<Zonemaster::Logger::Entry> objects. | ||
| 178 | |||
| 179 | =item test_module($module, $name) | ||
| 180 | |||
| 181 | Runs all available tests for the zone with the given name in the specified module. | ||
| 182 | |||
| 183 | =item test_method($module, $method, @arguments) | ||
| 184 | |||
| 185 | Run one particular test method in one particular module. The requested module must be in the list of active loaded modules (that is, not the Basic | ||
| 186 | module and not a module disabled by the current policy), and the method must be listed in the metadata the module exports. If those requirements | ||
| 187 | are fulfilled, the method will be called with the provided arguments. | ||
| 188 | |||
| 189 | =item zone($name) | ||
| 190 | |||
| 191 | Returns a L<Zonemaster::Zone> object for the given name. | ||
| 192 | |||
| 193 | =item ns($name, $address) | ||
| 194 | |||
| 195 | Returns a L<Zonemaster::Nameserver> object for the given name and address. | ||
| 196 | |||
| 197 | =item config() | ||
| 198 | |||
| 199 | Returns the global L<Zonemaster::Config> object. | ||
| 200 | |||
| 201 | =item logger() | ||
| 202 | |||
| 203 | Returns the global L<Zonemaster::Logger> object. | ||
| 204 | |||
| 205 | =item all_tags() | ||
| 206 | |||
| 207 | Returns a list of all the tags that can be logged for all avilable test modules. | ||
| 208 | |||
| 209 | =item all_methods() | ||
| 210 | |||
| 211 | 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. | ||
| 212 | |||
| 213 | =item recurse($name, $type, $class) | ||
| 214 | |||
| 215 | Does a recursive lookup for the given name, type and class, and returns the resulting packet (if any). Simply calls | ||
| 216 | L<Zonemaster::Recursor::recurse()> on a globally stored object. | ||
| 217 | |||
| 218 | =item save_cache($filename) | ||
| 219 | |||
| 220 | After running the tests, save the accumulated cache to a file with the given name. | ||
| 221 | |||
| 222 | =item preload_cache($filename) | ||
| 223 | |||
| 224 | 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 | ||
| 225 | L<save_cache()>. | ||
| 226 | |||
| 227 | =item asn_lookup($ip) | ||
| 228 | |||
| 229 | Takes a single IP address and returns one of three things: | ||
| 230 | |||
| 231 | =over | ||
| 232 | |||
| 233 | =item | ||
| 234 | |||
| 235 | Nothing, if the IP address is not in any AS. | ||
| 236 | |||
| 237 | =item | ||
| 238 | |||
| 239 | If called in list context, a list of AS number and a L<Net::IP::XS> object representing the prefix it's in. | ||
| 240 | |||
| 241 | =item | ||
| 242 | |||
| 243 | If called in scalar context, only the AS number. | ||
| 244 | |||
| 245 | =back | ||
| 246 | |||
| 247 | =item modules() | ||
| 248 | |||
| 249 | Returns a list of the loaded test modules. Exactly the same as L<Zonemaster::Test::modules>. | ||
| 250 | |||
| 251 | =item add_fake_delegation($domain, $data) | ||
| 252 | |||
| 253 | This method adds some fake delegation information to the system. The arguments are a domain name, and a reference to a hash with delegation | ||
| 254 | information. The keys in the hash must be nameserver names, and the values references to lists of IP addresses for the corresponding nameserver. | ||
| 255 | |||
| 256 | Example: | ||
| 257 | |||
| 258 | Zonemaster->add_fake_delegation( | ||
| 259 | 'lysator.liu.se' => { | ||
| 260 | 'ns.nic.se' => [ '212.247.7.228', '2a00:801:f0:53::53' ], | ||
| 261 | 'i.ns.se' => [ '194.146.106.22', '2001:67c:1010:5::53' ], | ||
| 262 | 'ns3.nic.se' => [ '212.247.8.152', '2a00:801:f0:211::152' ] | ||
| 263 | } | ||
| 264 | ); | ||
| 265 | |||
| 266 | =item add_fake_ds($domain, $data) | ||
| 267 | |||
| 268 | This method adds fake DS records to the system. The arguments are a domain | ||
| 269 | name, and a reference to a list of references to hashes. The hashes in turn | ||
| 270 | must have the keys C<keytag>, C<algorithm>, C<type> and C<digest>, with the | ||
| 271 | values holding the corresponding data. The digest data should be a single | ||
| 272 | unbroken string of hexadecimal digits. | ||
| 273 | |||
| 274 | Example: | ||
| 275 | |||
| 276 | Zonemaster->add_fake_ds( | ||
| 277 | 'nic.se' => [ | ||
| 278 | { keytag => 16696, algorithm => 5, type => 2, digest => '40079DDF8D09E7F10BB248A69B6630478A28EF969DDE399F95BC3B39F8CBACD7' }, | ||
| 279 | { keytag => 16696, algorithm => 5, type => 1, digest => 'EF5D421412A5EAF1230071AFFD4F585E3B2B1A60' }, | ||
| 280 | ] | ||
| 281 | ); | ||
| 282 | |||
| 283 | =item start_time_now() | ||
| 284 | |||
| 285 | Set the logger's start time to the current time. | ||
| 286 | |||
| 287 | =item reset() | ||
| 288 | |||
| 289 | Reset logger start time to current time, empty the list of log messages, clear | ||
| 290 | nameserver object cache and recursor cache. | ||
| 291 | |||
| 292 | =back | ||
| 293 | |||
| 294 | =head1 AUTHOR | ||
| 295 | |||
| 296 | Calle Dybedahl, C<< <calle at init.se> >> | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | no Moose; | ||
| 301 | __PACKAGE__->meta->make_immutable; | ||
| 302 | |||
| 303 | 1; | ||