| 1 | package Zonemaster::Test::Address v1.0.1; | ||
| 2 | |||
| 3 | use 5.14.2; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Zonemaster; | ||
| 8 | use Zonemaster::Util; | ||
| 9 | use Zonemaster::TestMethods; | ||
| 10 | use Zonemaster::Constants qw[:addresses :ip]; | ||
| 11 | use List::MoreUtils qw[none any]; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | |||
| 15 | ### | ||
| 16 | ### Entry Points | ||
| 17 | ### | ||
| 18 | |||
| 19 | sub all { | ||
| 20 | my ( $class, $zone ) = @_; | ||
| 21 | my @results; | ||
| 22 | |||
| 23 | push @results, $class->address01( $zone ) if Zonemaster->config->should_run('address01'); | ||
| 24 | push @results, $class->address02( $zone ) if Zonemaster->config->should_run('address02'); | ||
| 25 | # Perform ADDRESS03 if ADDRESS02 passed | ||
| 26 | if ( any { $_->tag eq q{NAMESERVERS_IP_WITH_REVERSE} } @results ) { | ||
| 27 | push @results, $class->address03( $zone ) if Zonemaster->config->should_run('address03'); | ||
| 28 | } | ||
| 29 | |||
| 30 | return @results; | ||
| 31 | } | ||
| 32 | |||
| 33 | ### | ||
| 34 | ### Metadata Exposure | ||
| 35 | ### | ||
| 36 | |||
| 37 | sub metadata { | ||
| 38 | my ( $class ) = @_; | ||
| 39 | |||
| 40 | return { | ||
| 41 | address01 => [ | ||
| 42 | qw( | ||
| 43 | NAMESERVER_IP_PRIVATE_NETWORK | ||
| 44 | NO_IP_PRIVATE_NETWORK | ||
| 45 | ) | ||
| 46 | ], | ||
| 47 | address02 => [ | ||
| 48 | qw( | ||
| 49 | NAMESERVER_IP_WITHOUT_REVERSE | ||
| 50 | NAMESERVERS_IP_WITH_REVERSE | ||
| 51 | NO_RESPONSE_PTR_QUERY | ||
| 52 | ) | ||
| 53 | ], | ||
| 54 | address03 => [ | ||
| 55 | qw( | ||
| 56 | NAMESERVER_IP_WITHOUT_REVERSE | ||
| 57 | NAMESERVER_IP_PTR_MISMATCH | ||
| 58 | NAMESERVER_IP_PTR_MATCH | ||
| 59 | NO_RESPONSE_PTR_QUERY | ||
| 60 | ) | ||
| 61 | ], | ||
| 62 | }; | ||
| 63 | } ## end sub metadata | ||
| 64 | |||
| 65 | sub translation { | ||
| 66 | return { | ||
| 67 | 'NAMESERVER_IP_WITHOUT_REVERSE' => 'Nameserver {ns} has an IP address ({address}) without PTR configured.', | ||
| 68 | 'NAMESERVER_IP_PTR_MISMATCH' => | ||
| 69 | 'Nameserver {ns} has an IP address ({address}) with mismatched PTR result ({names}).', | ||
| 70 | 'NAMESERVER_IP_PRIVATE_NETWORK' => | ||
| 71 | 'Nameserver {ns} has an IP address ({address}) with prefix {prefix} referenced in {reference} as a \'{name}\'.', | ||
| 72 | 'NO_IP_PRIVATE_NETWORK' => 'All Nameserver addresses are in the routable public addressing space.', | ||
| 73 | 'NAMESERVERS_IP_WITH_REVERSE' => 'Reverse DNS entry exist for all Nameserver IP addresses.', | ||
| 74 | 'NAMESERVER_IP_PTR_MATCH' => 'All reverse DNS entry matches name server name.', | ||
| 75 | 'NO_RESPONSE_PTR_QUERY' => 'No response from nameserver(s) on PTR query ({reverse}).', | ||
| 76 | }; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub version { | ||
| 80 | return "$Zonemaster::Test::Address::VERSION"; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub find_special_address { | ||
| 84 | my ( $class, $ip ) = @_; | ||
| 85 | my @special_addresses; | ||
| 86 | |||
| 87 | if ( $ip->version == $IP_VERSION_4 ) { | ||
| 88 | @special_addresses = @IPV4_SPECIAL_ADDRESSES; | ||
| 89 | } | ||
| 90 | elsif ( $ip->version == $IP_VERSION_6 ) { | ||
| 91 | @special_addresses = @IPV6_SPECIAL_ADDRESSES; | ||
| 92 | } | ||
| 93 | |||
| 94 | foreach my $ip_details ( @special_addresses ) { | ||
| 95 | if ( $ip->overlaps( ${$ip_details}{ip} ) ) { | ||
| 96 | return $ip_details; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub address01 { | ||
| 104 | my ( $class, $zone ) = @_; | ||
| 105 | my @results; | ||
| 106 | my %ips; | ||
| 107 | |||
| 108 | foreach | ||
| 109 | my $local_ns ( @{ Zonemaster::TestMethods->method4( $zone ) }, @{ Zonemaster::TestMethods->method5( $zone ) } ) | ||
| 110 | { | ||
| 111 | |||
| 112 | next if $ips{ $local_ns->address->short }; | ||
| 113 | |||
| 114 | my $ip_details_ref = $class->find_special_address( $local_ns->address ); | ||
| 115 | |||
| 116 | if ( $ip_details_ref ) { | ||
| 117 | push @results, | ||
| 118 | info( | ||
| 119 | NAMESERVER_IP_PRIVATE_NETWORK => { | ||
| 120 | ns => $local_ns->name->string, | ||
| 121 | address => $local_ns->address->short, | ||
| 122 | prefix => ${$ip_details_ref}{ip}->print, | ||
| 123 | name => ${$ip_details_ref}{name}, | ||
| 124 | reference => ${$ip_details_ref}{reference}, | ||
| 125 | } | ||
| 126 | ); | ||
| 127 | } | ||
| 128 | |||
| 129 | $ips{ $local_ns->address->short }++; | ||
| 130 | |||
| 131 | } ## end foreach my $local_ns ( @{ Zonemaster::TestMethods...}) | ||
| 132 | |||
| 133 | if ( scalar keys %ips and not scalar @results ) { | ||
| 134 | push @results, info( NO_IP_PRIVATE_NETWORK => {} ); | ||
| 135 | } | ||
| 136 | |||
| 137 | return @results; | ||
| 138 | } ## end sub address01 | ||
| 139 | |||
| 140 | sub address02 { | ||
| 141 | my ( $class, $zone ) = @_; | ||
| 142 | my @results; | ||
| 143 | |||
| 144 | my %ips; | ||
| 145 | |||
| 146 | foreach my $local_ns ( @{ Zonemaster::TestMethods->method5( $zone ) } ) { | ||
| 147 | |||
| 148 | next if $ips{ $local_ns->address->short }; | ||
| 149 | |||
| 150 | my $reverse_ip_query = $local_ns->address->reverse_ip; | ||
| 151 | |||
| 152 | my $p = Zonemaster::Recursor->recurse( $reverse_ip_query, q{PTR} ); | ||
| 153 | |||
| 154 | if ( $p ) { | ||
| 155 | # In case of Classless IN-ADDR.ARPA delegation, query returns | ||
| 156 | # CNAME records. A PTR query is done on the CNAME. | ||
| 157 | if ($p->rcode eq q{NOERROR} and $p->get_records( q{CNAME}, q{answer} ) ) { | ||
| 158 | my ( $cname ) = $p->get_records( q{CNAME}, q{answer} ); | ||
| 159 | $p = Zonemaster::Recursor->recurse( $cname->cname, q{PTR} ); | ||
| 160 | } | ||
| 161 | |||
| 162 | if ( $p->rcode ne q{NOERROR} or not $p->get_records( q{PTR}, q{answer} ) ) { | ||
| 163 | push @results, | ||
| 164 | info( | ||
| 165 | NAMESERVER_IP_WITHOUT_REVERSE => { | ||
| 166 | ns => $local_ns->name->string, | ||
| 167 | address => $local_ns->address->short, | ||
| 168 | } | ||
| 169 | ); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | push @results, | ||
| 174 | info( | ||
| 175 | NO_RESPONSE_PTR_QUERY => { | ||
| 176 | reverse => $reverse_ip_query, | ||
| 177 | } | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | |||
| 181 | $ips{ $local_ns->address->short }++; | ||
| 182 | |||
| 183 | } ## end foreach my $local_ns ( @{ Zonemaster::TestMethods...}) | ||
| 184 | |||
| 185 | if ( scalar keys %ips and not scalar @results ) { | ||
| 186 | push @results, info( NAMESERVERS_IP_WITH_REVERSE => {} ); | ||
| 187 | } | ||
| 188 | |||
| 189 | return @results; | ||
| 190 | } ## end sub address02 | ||
| 191 | |||
| 192 | sub address03 { | ||
| 193 | my ( $class, $zone ) = @_; | ||
| 194 | my @results; | ||
| 195 | my $ptr_query; | ||
| 196 | |||
| 197 | my %ips; | ||
| 198 | |||
| 199 | foreach my $local_ns ( @{ Zonemaster::TestMethods->method5( $zone ) } ) { | ||
| 200 | |||
| 201 | next if $ips{ $local_ns->address->short }; | ||
| 202 | |||
| 203 | my $reverse_ip_query = $local_ns->address->reverse_ip; | ||
| 204 | $ptr_query = $reverse_ip_query; | ||
| 205 | |||
| 206 | my $p = Zonemaster::Recursor->recurse( $ptr_query, q{PTR} ); | ||
| 207 | |||
| 208 | # In case of Classless IN-ADDR.ARPA delegation, query returns | ||
| 209 | # CNAME records. A PTR query is done on the CNAME. | ||
| 210 | if ($p->rcode eq q{NOERROR} and $p->get_records( q{CNAME}, q{answer} ) ) { | ||
| 211 | my ( $cname ) = $p->get_records( q{CNAME}, q{answer} ); | ||
| 212 | $ptr_query = $cname->cname; | ||
| 213 | $p = Zonemaster::Recursor->recurse( $ptr_query, q{PTR} ); | ||
| 214 | } | ||
| 215 | |||
| 216 | if ( $p ) { | ||
| 217 | my @ptr = $p->get_records_for_name( q{PTR}, $ptr_query ); | ||
| 218 | if ( $p->rcode eq q{NOERROR} and scalar @ptr ) { | ||
| 219 | if ( none { name( $_->ptrdname ) eq $local_ns->name->string . q{.} } @ptr ) { | ||
| 220 | push @results, | ||
| 221 | info( | ||
| 222 | NAMESERVER_IP_PTR_MISMATCH => { | ||
| 223 | ns => $local_ns->name->string, | ||
| 224 | address => $local_ns->address->short, | ||
| 225 | names => join( q{/}, map { $_->ptrdname } @ptr ), | ||
| 226 | } | ||
| 227 | ); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | else { | ||
| 231 | push @results, | ||
| 232 | info( | ||
| 233 | NAMESERVER_IP_WITHOUT_REVERSE => { | ||
| 234 | ns => $local_ns->name->string, | ||
| 235 | address => $local_ns->address->short, | ||
| 236 | } | ||
| 237 | ); | ||
| 238 | } | ||
| 239 | } ## end if ( $p ) | ||
| 240 | else { | ||
| 241 | push @results, | ||
| 242 | info( | ||
| 243 | NO_RESPONSE_PTR_QUERY => { | ||
| 244 | reverse => $reverse_ip_query, | ||
| 245 | } | ||
| 246 | ); | ||
| 247 | } | ||
| 248 | |||
| 249 | $ips{ $local_ns->address->short }++; | ||
| 250 | |||
| 251 | } ## end foreach my $local_ns ( @{ Zonemaster::TestMethods...}) | ||
| 252 | |||
| 253 | if ( scalar keys %ips and not scalar @results ) { | ||
| 254 | push @results, info( NAMESERVER_IP_PTR_MATCH => {} ); | ||
| 255 | } | ||
| 256 | |||
| 257 | return @results; | ||
| 258 | } ## end sub address03 | ||
| 259 | |||
| 260 | 1; | ||
| 261 | |||
| 262 | =head1 NAME | ||
| 263 | |||
| 264 | Zonemaster::Test::Address - module implementing tests focused on the Address specific test cases of the DNS tests | ||
| 265 | |||
| 266 | =head1 SYNOPSIS | ||
| 267 | |||
| 268 | my @results = Zonemaster::Test::Address->all($zone); | ||
| 269 | |||
| 270 | =head1 METHODS | ||
| 271 | |||
| 272 | =over | ||
| 273 | |||
| 274 | =item all($zone) | ||
| 275 | |||
| 276 | Runs the default set of tests and returns a list of log entries made by the tests | ||
| 277 | |||
| 278 | =item metadata() | ||
| 279 | |||
| 280 | Returns a reference to a hash, the keys of which are the names of all test methods in the module, and the corresponding values are references to | ||
| 281 | lists with all the tags that the method can use in log entries. | ||
| 282 | |||
| 283 | =item translation() | ||
| 284 | |||
| 285 | Returns a refernce to a hash with translation data. Used by the builtin translation system. | ||
| 286 | |||
| 287 | =item version() | ||
| 288 | |||
| 289 | Returns a version string for the module. | ||
| 290 | |||
| 291 | =back | ||
| 292 | |||
| 293 | =head1 TESTS | ||
| 294 | |||
| 295 | =over | ||
| 296 | |||
| 297 | =item address01($zone) | ||
| 298 | |||
| 299 | Verify that IPv4 addresse are not in private networks. | ||
| 300 | |||
| 301 | =item address02($zone) | ||
| 302 | |||
| 303 | Verify reverse DNS entries exist for nameservers IP addresses. | ||
| 304 | |||
| 305 | =item address03($zone) | ||
| 306 | |||
| 307 | Verify that reverse DNS entries match nameservers names. | ||
| 308 | |||
| 309 | =item find_special_address($ip) | ||
| 310 | |||
| 311 | Verify that an address (Net::IP::XS) given is a special (private, reserved, ...) one. | ||
| 312 | |||
| 313 | =back | ||
| 314 | |||
| 315 | =cut | ||
| 316 |