| 1 | package App::DNS::Zonewalk; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use feature qw(switch); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use parent 'Net::DNS::Resolver'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = 0.05; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | App::DNS::Zonewalk - helper library for recursive DNS zone walks. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 ABSTRACT | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | Helper library for B<zonewalk>. Adds a B<raxfr()> method to Net::DNS::Resolver. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | use App::DNS::Zonewalk; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | my $resolver = App::DNS::Zonewalk->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | my @net_dns_rrs = $resolver->raxfr($start_zone) | ||||||||||||||||||||||||||||||||||||||||||
| 25 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | See the B<zonewalk> documentation for more details, a cli program included in this distribution for recusive DNS zonewalks. | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | =head2 raxfr($start_zone) | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | my @resource_records = $resolver->raxfr($start_zone); | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | Walks the $start_zone recursively and returns all DNS resource records. The DNS server from $resolver must be authoritative for the zone und sub-zones and the client must be allowed to fetch the zones via AXFR. | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub raxfr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
raxfr: called CC=15 SCAR=42.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | my ( $self, $start_zone ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | unless ($start_zone) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | print ";; ERROR: raxfr: no zone specified\n" if $self->{'debug'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $self->errorstring('no zone'); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | # housekeeping for recursion | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | my $dyn_zone_list = {}; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | my $zones_done = {}; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | my @zone; | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | $dyn_zone_list->{$start_zone}++; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | ZONE: | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | while ( my ($zone) = sort keys %$dyn_zone_list ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | print ";; processing '$zone' ...\n" if $self->{'debug'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | delete $dyn_zone_list->{$zone}; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | next ZONE if exists $zones_done->{$zone}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # mark current zone as done | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | $zones_done->{$zone}++; | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # skip zone if resolvers nameserver isn't autoritative | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | next ZONE unless $self->_check_is_auth($zone); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my @zone_records = $self->axfr($zone); | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | unless (@zone_records) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | print ";; skipping $zone: ", $self->errorstring, "\n" | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if $self->{'debug'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | next ZONE; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | foreach my $rr (@zone_records) { | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | push @zone, $rr; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ( $rr->type eq 'NS' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $new_zone = lc $rr->name; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # push to dyn_zone_list when index('foo.bar.baz', 'bar.baz') | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # not already handled and not already stored for handling | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ( index( $new_zone, $zone ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
index $new_zone, $zone and not exists $zones_done->{$new_zone} && !exists $dyn_zone_list->{$new_zone}
exists $zones_done->{$new_zone} && !exists $dyn_zone_list->{$new_zone}
MC/DC0%0/3
index $new_zone, $zone and not exists $zones_done->{$new_zone} && !exists $dyn_zone_list->{$new_zone}
index $new_zone, $zone
exists $zones_done->{$new_zone}
!exists $dyn_zone_list->{$new_zone}
Truth table0%0/4
A and not(B && not(C))
A= index $new_zone, $zoneB= exists $zones_done->{$new_zone}C= !exists $dyn_zone_list->{$new_zone}
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | && not exists $zones_done->{$new_zone} | ||||||||||||||||||||||||||||||||||||||||||
| 90 | && not exists $dyn_zone_list->{$new_zone} ) | ||||||||||||||||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $dyn_zone_list->{$new_zone}++; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | return wantarray ? @zone : \@zone; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | ############################################### | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # check if nameserver is authoritative for zone | ||||||||||||||||||||||||||||||||||||||||||
| 105 | ############################################### | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | sub _check_is_auth { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_is_auth: called CC=12 SCAR=34.5
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | my ( $self, $zone ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # get the nameservers for this zone | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | my $ans = $self->send( $zone, 'NS' ); | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | # uups, something bad happened | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | unless ( defined $ans ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | print ';; ERROR: ', $self->errorstring, "\n" | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if $self->{'debug'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | # store the nameserver FQDN names | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | my @ns_names; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | foreach my $rr ( $ans->answer ) { | |||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | push @ns_names, $rr->nsdname; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | # but we need the addresses for comparison, sigh | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | my @ns_addresses; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | foreach my $ns_name (@ns_names) { | |||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | my $any_packet = $self->query( $ns_name, 'ANY' ); | |||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | ▶ | next unless defined $any_packet; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | foreach my $rr ( $any_packet->answer ) { | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | next unless defined $rr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | next unless ( $rr->type eq 'A' || $rr->type eq 'AAAA' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$rr->type eq "A" or $rr->type eq "AAAA"
MC/DC0%0/2
$rr->type eq "A" or $rr->type eq "AAAA"
$rr->type eq "A"
$rr->type eq "AAAA"
Truth table0%0/3
A or B
A= $rr->type eq "A"B= $rr->type eq "AAAA"
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | push @ns_addresses, $rr->address; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | # now compare the resolvers nameserver with the authoritattive | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # nameservers for this zone | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | my ($resolvers_ns) = $self->nameservers; | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | ▶ | unless ( $resolvers_ns ~~ @ns_addresses ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | $self->errorstring("NS $resolvers_ns is nonauth for $zone"); | |||||||||||||||||||||||||||||||||||||||||
| 146 | print ';; ERROR: ', $self->errorstring, "\n" | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | ▶ | if $self->{'debug'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | # our resolvers first nameserver is authoritative | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | Karl Gaissmaier, C<< <gaissmai(at)cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | Please report any bugs or feature requests to C<bug-app-dns-zonewalk at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||
| 163 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-DNS-Zonewalk>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 164 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | perldoc App::DNS::Zonewalk | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-DNS-Zonewalk> | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | L<http://annocpan.org/dist/App-DNS-Zonewalk> | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | L<http://cpanratings.perl.org/d/App-DNS-Zonewalk> | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | L<http://search.cpan.org/dist/App-DNS-Zonewalk/> | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | Copyright 2012 Karl Gaissmaier. | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 200 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||||||||||||
| 201 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 1 | 1; # End of App::DNS::Zonewalk | |||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | # vim: sw=4 ft=perl | ||||||||||||||||||||||||||||||||||||||||||