1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
#!/usr/local/bin/perl -w
$main::VERSION = '0.16';
#
=head1 NAME
B<check_soa> - Check nameservers for a domain in parallel
=head1 SYNOPSIS
B<check_soa> [B<-d>] [B<-t>] [B<-v>] domain [server]
=head1 DESCRIPTION
Each nameserver for the specified domain is queried for the relevant SOA record
and the zone serial number printed.
Error reports are generated for nameservers which reply with non-authoritative,
outdated or incorrect information.
SOA query packets are sent to the nameservers as rapidly as the underlying
hardware will allow. The program waits for a response only when it is needed
for analysis. Execution time is determined by the slowest nameserver.
This program is based on the check_soa idea described by Albitz and Liu.
=head1 OPTIONS AND ARGUMENTS
=over 8
=item -d
Turn on resolver diagnostics.
=item -t
Ignore UDP datagram truncation.
=item -v
Verbose output including address records for each server.
=item domain
Fully qualified name of domain to be tested.
=item server
Name or list of IP addresses of DNS nameserver to be tested.
=back
=head1 BUGS
The timeout code exploits the 4 argument form of select() function.
This is not guaranteed to work in non-Unix environments.
=head1 COPYRIGHT
Copyright (c) 2003-2006, Dick Franks E<lt>rwfranks@acm.orgE<gt>
This program is free software;
you may use or redistribute it under the same terms as Perl itself.
=head1 SEE ALSO
Paul Albitz, Cricket Liu.
DNS and BIND, 5th Edition.
O'Reilly & Associates, 2006.
M. Andrews.
Negative Caching of DNS Queries.
RFC2308, IETF Network Working Group, 1998.
Tom Christiansen, Jon Orwant, Larry Wall.
Programming Perl, 3rd Edition.
O'Reilly & Associates, 2000.
R. Elz, R. Bush.
Clarifications to the DNS Specification.
RFC2181, IETF Network Working Group, 1997.
P. Mockapetris.
Domain Names - Concepts and Facilities.
RFC1034, IETF Network Working Group, 1987.
=cut
use strict;
use Getopt::Std;
my $self = $0; # script
my $version = $main::VERSION;
my %option;
my $options = 'dtv'; # options
getopts("$options", \%option); # also --help --version
my ($domain, @server) = @ARGV; # arguments
my $synopsis = "Synopsis:\t$self [-$options] domain [server]";
die eval{ system("perldoc -F $self"); "" }, "\n$synopsis\n\n" unless @ARGV;
require Net::DNS;
my @conf = ( debug => ($option{'d'} || 0), # -d enable diagnostics
igntc => ($option{'t'} || 0), # -t ignore truncation
recurse => 0,
retry => 2 );
my $verbose = $option{'v'}; # -v verbose
my $udp_timeout = 5; # timeout for parallel operations
my $udp_wait = 0.010; # minimum polling interval
my $resolver = Net::DNS::Resolver->new(@conf, recurse => 1 ); # create resolver object
my @ip = $resolver->nameservers(@server);
my @ns = NS($domain); # find NS serving domain
my @nsdname = sort map{lc $_->nsdname} @ns; # extract server names from NS records
my $zone = @ns ? $ns[0]->name : ''; # find zone name
my @soa = listRR($zone, 'SOA'); # show SOA
report("SOA query fails for $zone.") unless @soa;
foreach ( @soa ) { # simple sanity check
my $mname = lc $_->mname; # primary server
report('no retry when zone transfer fails') if ($_->refresh + $_->retry) > $_->expire;
report('minimum TTL exceeds zone expiry time') if $_->minimum > $_->expire;
next if grep{$_ eq $mname} @nsdname; # NS list includes primary server
next if $resolver->query($mname, 'A'); # or address records exist
next if $resolver->query($mname, 'AAAA'); # (unlisted primary is never tested)
report('unresolved MNAME field:', "$mname."); # RFC2181, 7.3
}
for ( "$zone." ) { # show RR for domain if relevant
listRR($domain, 'ANY') unless /$domain/i;
}
print "----\n";
if ( @server ) {
my @nominated = map{@server > 1 ? $_ : "@server $_"} sort @ip;
checkNS($zone, @nominated);
} else {
my $n = @nsdname || $resolver->print; # suspect resolver if no NS
my ($errors, @etc) = checkNS($zone, @nsdname); # report status
print "\nUnexpected response from $errors of $n nameservers\n" if $errors;
}
print "\n";
exit;
sub catnap { # short duration sleep
my $duration = shift; # seconds
sleep(1+$duration) unless eval { defined select(undef, undef, undef, $duration) };
}
sub checkNS { # check nameservers (in parallel) and report status
my $zone = shift;
my $index = @_ || return (0,0); # server list empty
my ($ns, $if) = split / /, pop @_||return(0,0); # name/interface at end of list
my $res = Net::DNS::Resolver->new(@conf); # use clean resolver for each test
my @xip = sort $res->nameservers($if || $ns); # point at nameserver
my $ip = pop @xip; # last (or only) interface
$res->nameservers($ip) if @xip;
# send query packet to nameserver
my ($socket, $sent) = ($res->bgsend($zone,'SOA'), time) if $ip and not @server;
my @pass = checkNS($zone, @_); # recurse to query others in parallel
# pick up response as recursion unwinds
my ($fail, $latest, %soa) = @pass; # use prebuilt return values
my @fail = @pass; $fail[0]++;
if ( @xip and $socket ) { # special handling for multihomed server
until ($res->bgisready($socket)) { # wait for outstanding query to complete
last if time > ($sent + $udp_timeout);
catnap($udp_wait);
}
}
foreach (@xip) { # iterate over remaining interfaces
my ($f, @etc) = checkNS($zone, (undef)x@_, "$ns $_"); # pass name/IP pair
@pass = @fail if $f; # propagate failure to caller
}
my %nsname; # identify nameserver
unless ( $ip ) {
print "\n[$index]\t$ns\n";
report('unresolved server name');
return @fail;
} elsif ( $ns eq $ip ) {
print "\n[$index]\t$ip\n";
} else {
print "\n[$index]\t$ns ($ip)\n";
$nsname{$ns}++;
}
if ( $verbose ) {
foreach ( grep{$_->type eq 'PTR'} listRR($ip) ) {
$nsname{lc $_->ptrdname}++;
}
foreach my $ns ( sort keys %nsname ) { # show address records
listRR($ns, 'A');
listRR($ns, 'AAAA');
}
}
my $packet;
if ( $socket ) {
until ($_ = $res->bgisready($socket)) { # timed wait on socket
last if time > ($sent + $udp_timeout);
catnap($udp_wait); # snatch a few milliseconds sleep
}
$packet = $res->bgread($socket) if $_; # read response if available
} else {
$packet = $res->send($zone, 'SOA'); # use sequential query model
}
unless ( $packet ) { # ... is no more! It has ceased to be!
report('no response');
return @fail;
}
unless ( $packet->header->rcode eq 'NOERROR' ) {
report($packet->header->rcode); # NXDOMAIN or fault at nameserver
return @fail; # RFC2308, 2.1
}
my $aa = $packet->header->aa; # authoritative answer
my $tc = $packet->header->tc ? 'tc' : ''; # truncated response
my @answer = $packet->answer; # answer section
my @soa = grep{$_->type eq 'SOA'} @answer; # SOA records (plural!)
my @result = @fail; # analyse response
if ( @soa ) {
@result = @pass if $aa; # RFC2181, 6.1
report('non-authoritative answer') unless $aa; # RFC1034, 6.2.1 (2)
} elsif ( @soa = grep{$_->type eq 'SOA'} $packet->authority ) {
my $ttl = $soa[0]->ttl; # RFC2308, 2.2 (1)(2)
report("NCACHE response (ttl $ttl seconds)");
} elsif ( grep{$_->type eq 'NS'} $packet->authority ) {
report('referral received from nameserver'); # RFC2308, 2.2 (4)
return @fail; # RFC2181, 6.1
} else {
report('NODATA response from nameserver'); # RFC2308, 2.2 (3)
return @fail; # RFC2181, 6.1
}
my $serial; # zone serial number
foreach ( @soa ) {
print "$tc\t\t\tzone serial\t", ($serial = $_->serial), "\n";
$_->serial(0); # key on constant fields only
$_->ttl(0);
next if $soa{lc $_->string}++; # skip repeated occurrences
next unless keys %soa > 1; # zone should have unique SOA
report('SOA record not unique'); # RFC2181, 6.1
@result = (@fail, %soa);
}
return @result if $serial == $latest; # server has latest data
unless ( $aa and ($serial > $latest) ) { # unexpected serial number
report('serial number not current') if $latest;
return (@fail, %soa);
}
my $unrep = $latest ? (@_ - $fail) : 0; # all previous out of date
my $s = $unrep > 1 ? 's' : ''; # pedants really are revolting!
report("at least $unrep previously unreported stale serial number$s") if $unrep;
return ($result[0]+$unrep, $serial, %soa); # restate partial result
}
sub listRR { # print all RR for specified name
my $packet = $resolver->send(@_) || return (); # get specified RRs
my $na = $packet->header->tc ? 'tc' : ''; # non-auth response
my $aa = $packet->header->aa ? "aa $na" : $na; # authoritative answer
my $qname = ($packet->question)[0]->qname;
my @answer = $packet->answer;
foreach ( @answer ) { # print RR with status flags
my $string = $_->string; # display IPv6 compact form
$string =~ s/(:[:0]*:)(?!.*::|.+\1)/::/o if $_->type eq 'AAAA';
print $_->name eq $qname ? $aa : $na, "\t$string\n";
}
return @answer;
}
sub NS { # find nameservers for domain
my $domain = shift || '.'; # name or IP address
my @ns = ();
my $version = Net::DNS::version();
while ( $domain ) {
my $packet = $resolver->send($domain, 'NS') || return ();
last if @ns = grep{$_->type eq 'NS'} $packet->answer;
($_, $domain) = split /\./, ($packet->question)[0]->qname, 2;
die "\tIPv6 feature not implemented in Net::DNS $version\n" if /:/;
if ( my @soa = grep{$_->type eq 'SOA'} $packet->authority ) {
$domain = $soa[0]->name; # zone cut
}
}
return @ns;
}
sub report { # concatenate strings into fault report
print join(' ', '*'x4, @_, "\n");
}
__END__
|