/usr/bin/dnswalk is in dnswalk 2.0.2.dfsg.1-0.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | #!/usr/bin/perl
#
# dnswalk Walk through a DNS tree, pulling out zone data and
# dumping it in a directory tree
#
# $Id: dnswalk,v 1.18 1997/10/06 13:23:58 barr Exp barr $
#
# check data collected for legality using standard resolver
#
# invoke as dnswalk domain > logfile
# Options:
# -r Recursively descend subdomains of domain
# -i Suppress check for invalid characters in a domain name.
# -a turn on warning of duplicate A records.
# -d Debugging
# -m Check only if the domain has been modified. (Useful only if
# dnswalk has been run previously.)
# -F Enable "facist" checking. (See man page)
# -l Check lame delegations
use Getopt::Std;
use IO::Socket;
use Net::DNS;
getopts("D:rfiadmFl");
$num_error{'FAIL'}=0; # failures to access data
$num_error{'WARN'}=0; # questionable data
$num_error{'BAD'}=0; # bad data
# Where all zone transfer information is saved. You can change this to
# something like /tmp/dnswalk if you don't want to clutter up the current
# directory
if ($opt_D) {
$basedir = $opt_D;
} else {
$basedir = ".";
}
($domain = $ARGV[0]) =~ tr/A-Z/a-z/;
if ($domain !~ /\.$/) {
die "Usage: dnswalk domain\ndomain MUST end with a '.'\n";
}
if (! -d $basedir) {
mkdir($basedir,0777) || die "FAIL: Cannot create $basedir: $!\n";
}
&dowalk($domain);
print STDERR "$num_error{'FAIL'} failures, $num_error{'WARN'} warnings, $num_error{'BAD'} errors.\n";
exit $num_error{'BAD'};
sub dowalk {
my (@subdoms);
my (@sortdoms);
my ($domain)=$_[0];
$modified=0;
return unless $domain;
print "Checking $domain\n";
@subdoms=&doaxfr($domain);
&check_zone($domain) if (defined(@zone) && @zone);
undef @zone;
return if (!(defined(@subdoms) && @subdoms));
@sortdoms = sort byhostname @subdoms;
local ($subdom);
if ($opt_r) {
foreach $subdom (@sortdoms) {
&dowalk($subdom);
}
}
}
# try to get a zone transfer, trying each listed authoritative server if
# if fails.
sub doaxfr {
local ($domain)=@_[0];
local (%subdoms)=();
local ($subdom);
local(@servers) = &getauthservers($domain);
&printerr("BAD", "$domain has only one authoritative nameserver\n")
if (scalar(@servers) == 1);
&printerr("BAD", "$domain has NO authoritative nameservers!\n")
if (scalar(@servers) == 0);
SERVER:
foreach $server (@servers) {
print STDERR "Getting zone transfer of $domain from $server...";
my $res = new Net::DNS::Resolver;
$res->nameservers($server);
@zone=$res->axfr($domain);
unless (defined(@zone) && @zone) {
print STDERR "failed\n";
&printerr("FAIL",
"Zone transfer of $domain from $server failed: ".
$res->errorstring. "\n");
next SERVER;
}
@subdoms=undef;
foreach $rr (@zone) {
if ($rr->type eq "NS") {
$subdom = $rr->name;
$subdom =~ tr/A-Z/a-z/;
if ((!&equal($subdom,$domain)) && ( !$subdoms{$subdom})) {
$subdoms{$subdom}=1;
}
}
}
print STDERR "done.\n";
last SERVER;
} # foreach #
unless (defined(@zone) && @zone) {
&printerr("BAD","All zone transfer attempts of $domain failed!\n");
return undef;
}
return (keys %subdoms);
}
sub getauthservers {
my ($domain)=$_[0];
my ($master)=&getmaster($domain);
my ($foundmaster)=0;
my ($ns);
my ($ns_tmp);
my ($res);
my ($ns_req);
my (@servers);
my (%servhash);
return if (!$master); # this is null if there is no SOA or not found
return if (!$domain);
$res = new Net::DNS::Resolver;
$ns_req = $res->query($domain, "NS");
&printerr("FAIL", "No nameservers found for $domain: ".
$res->errorstring ."\n")
unless (defined($ns_req) and ($ns_req->header->ancount > 0));
foreach $ns ($ns_req->answer) {
$ns_tmp = $ns->nsdname;
$ns_tmp =~ tr/A-Z/a-z/;
if (&equal($ns_tmp,$master)) {
$foundmaster=1; # make sure the master is at the top
} else {
push(@servers,$ns_tmp) if ($servhash{$ns_tmp}++<1);
}
}
if ($foundmaster) {
unshift(@servers,$master);
}
return @servers;
}
# return 'master' server for zone
sub getmaster {
my ($zone)=$_[0];
my ($res) = new Net::DNS::Resolver;
my ($packet) = new Net::DNS::Packet($zone, "SOA", "IN");
my ($soa_req) = $res->send($packet);
unless (defined($soa_req)) {
&printerr("FAIL", "Cannot get SOA record for $zone:".
$res->errorstring ."\n");
return "";
}
unless (($soa_req->header->ancount >= 1) &&
(($soa_req->answer)[0]->type eq "SOA")) {
&printerr("BAD", "SOA record not found for $zone\n");
return "";
}
return ($soa_req->answer)[0]->mname;
}
# open result of zone tranfer and check lots of nasty things
# here's where the fun begins
sub check_zone {
my ($domain)=$_[0];
local (%glues)=(); # look for duplicate glue (A) records
local ($name, $aliases, $addrtype, $length, @addrs);
local ($prio,$mx);
local ($soa,$contact);
local ($lastns); # last NS record we saw
local (@keys); # temp variable
foreach $rr (@zone) {
# complain about invalid chars only for mail names
if ((($rr->type eq "A") || ($rr->type eq "MX")) && (!$opt_i) &&
($rr->name =~ /[^\*][^-A-Za-z0-9.]/)) {
&printerr("WARN", $rr->name .": invalid character(s) in name\n");
}
if ($rr->type eq "SOA") {
print STDERR 's' if $opt_d;
print "SOA=". $rr->mname ." contact=". $rr->rname ."\n";
# basic address check. No "@", and user.dom.ain (two or more dots)
if (($rr->rname =~ /@/)||!($rr->rname =~ /\..*\./)) {
&printerr("WARN", "SOA contact name (".
$rr->rname .") is invalid\n");
}
} elsif ($rr->type eq "PTR") {
print STDERR 'p' if $opt_d;
if (scalar((@keys=split(/\./,$rr->name))) == 6 ) {
# check if forward name exists, but only if reverse is
# a full IP addr
# skip ".0" networks
if ($keys[0] ne "0") {
($name, $aliases, $addrtype, $length,
@addrs)=gethostbyname($rr->ptrdname);
# if (!(($name, $aliases, $addrtype, $length,
# @addrs)=gethostbyname($rr->ptrdname))) {
# &printerr("FAIL", "gethostbyname(".
# $rr->ptrdname ."): $!\n");
# }
# else {
if (!$name) {
&printerr("WARN", $rr->name
." PTR ". $rr->ptrdname .": unknown host\n");
}
elsif (!&equal($name,$rr->ptrdname)) {
&printerr("WARN", $rr->name
." PTR ". $rr->ptrdname .": CNAME (to $name)\n");
}
elsif (!&matchaddrlist($rr->name)) {
&printerr("WARN", $rr->name
." PTR ". $rr->ptrdname .": A record not found\n");
}
# }
}
}
} elsif (($rr->type eq "A") ) {
print STDERR 'a' if $opt_d;
# check to see that a reverse PTR record exists
($name,$aliases,$addrtype,$length,@addrs)=gethostbyaddr(pack('C4',
split(/\./,$rr->address)),2);
if (!$name) {
# hack - allow RFC 1101 netmasks encoding
if ($rr->address !=~ /^255/) {
&printerr("WARN", $rr->name ." A ".
$rr->address .": no PTR record\n");
}
}
elsif ($opt_F && !&equal($name,$rr->name)) {
# Filter out "hostname-something" (like "neptune-le0")
if (index(split (/\./, $rr->name, 2) . "-",
split (/\./, $name, 2)) == -1 ) {
&printerr("WARN", $rr->name ." A ".
$rr->address .": points to $name\n")
if ((split(/\./,$name))[0] ne "localhost");
}
}
if ($main'opt_a) {
# keep list in %glues, report any duplicates
if ($glues{$rr->address} eq "") {
$glues{$rr->address}=$rr->name;
}
elsif (($glues{$rr->address} eq $rr->name) &&
(!&equal($lastns,$domain))) {
&printerr("WARN", $rr->name
.": possible duplicate A record (glue of $lastns?)\n");
}
}
} elsif ($rr->type eq "NS") {
$lastns=$rr->name;
print STDERR 'n' if $opt_d;
# check to see if object of NS is real
&checklamer($rr->name,$rr->nsdname) if ($main'opt_l);
# check for bogusnesses like NS->IP addr
if (&isipv4addr($rr->nsdname)) {
&printerr("BAD", $rr->name
." NS ". $rr->nsdname .": Nameserver must be a hostname\n");
}
($name, $aliases, $addrtype, $length,
@addrs)=gethostbyname($rr->nsdname);
# if (!(($name, $aliases, $addrtype, $length,
# @addrs)=gethostbyname($rr->nsdname))) {
# &printerr("FAIL", "gethostbyname(". $rr->nsdname ."): $!\n");
# }
# else {
if (!$name) {
&printerr("BAD", $rr->name
." NS ". $rr->nsdname .": unknown host\n");
} elsif (!&equal($name,$rr->nsdname)) {
&printerr("BAD", $rr->name
." NS ". $rr->nsdname .": CNAME (to $name)\n");
}
# }
} elsif ($rr->type eq "MX") {
print STDERR 'm' if $opt_d;
# check to see if object of mx is real
if (&isipv4addr($rr->exchange)) {
&printerr("BAD", $rr->name
." MX ". $rr->exchange .": Mail exchange must be a hostname\n");
}
($name, $aliases, $addrtype, $length,
@addrs)=gethostbyname($rr->exchange);
# if (!(($name, $aliases, $addrtype, $length,
# @addrs)=gethostbyname($rr->exchange))) {
# &printerr("FAIL", "gethostbyname(". $rr->exchange ."): $!\n");
# }
# else {
if (!$name) {
&printerr("WARN", $rr->name
." MX ". $rr->exchange .": unknown host\n");
}
elsif (!&equal($name,$rr->exchange)) {
&printerr("WARN", $rr->name
." MX ". $rr->exchange .": CNAME (to $name)\n");
}
# }
} elsif ($rr->type eq "CNAME") {
print STDERR 'c' if $opt_d;
($name, $aliases, $addrtype, $length,
@addrs)=gethostbyname($rr->cname);
if (&isipv4addr($rr->cname)) {
&printerr("BAD", $rr->name
." CNAME ". $rr->cname .": alias must be a hostname\n");
}
# if (!(($name, $aliases, $addrtype, $length,
# @addrs)=gethostbyname($rr->cname))) {
# &printerr("FAIL", "gethostbyname(". $rr->cname ."): $!\n");
# }
# else {
if (!$name) {
&printerr("WARN", $rr->name
." CNAME ". $rr->cname .": unknown host\n");
} elsif (!&equal($name,$rr->cname)) {
&printerr("WARN", $rr->name
." CNAME ". $rr->cname .": CNAME (to $name)\n");
}
# }
}
}
print STDERR "\n" if $opt_d;
close(FILE);
}
# prints an error message, suppressing duplicates
sub printerr {
my ($type, $err)=@_;
if ($errlist{$err}==undef) {
print "$type: $err";
$num_error{$type}++;
print STDERR "!" if $opt_d;
$errlist{$err}=1;
} else {
print STDERR "." if $opt_d;
}
}
sub equal {
# Do case-insensitive string comparisons
local ($one)= $_[0];
local ($two)= $_[1];
$stripone=$one;
if (chop($stripone) eq '.') {
$one=$stripone;
}
$striptwo=$two;
if (chop($striptwo) eq '.') {
$two=$striptwo;
}
$one =~ tr/A-Z/a-z/;
$two =~ tr/A-Z/a-z/;
return ($one eq $two);
}
# check if argument looks like an IPv4 address
sub isipv4addr {
my ($host)=$_[0];
my ($one,$two,$three,$four);
($one,$two,$three,$four)=split(/\./,$host);
my $whole="$one$two$three$four";
# strings evaluated as numbers are zero
return (($whole+0) eq $whole);
}
sub matchaddrlist {
local($match)=pack('C4', reverse(split(/\./,$_[0],4)));
local($found)=0;
foreach $i (@addrs) {
$found=1 if ($i eq $match);
}
return $found;
}
# there's a better way to do this, it just hasn't evolved from
# my brain to this program yet.
sub byhostname {
@c = reverse(split(/\./,$a));
@d = reverse(split(/\./,$b));
for ($i=0;$i<=(($#c > $#d) ? $#c : $#d) ;$i++) {
next if $c[$i] eq $d[$i];
return -1 if $c[$i] eq "";
return 1 if $d[$i] eq "";
if ($c[$i] eq int($c[$i])) {
# numeric
return $c[$i] <=> $d[$i];
}
else {
# string
return $c[$i] cmp $d[$i];
}
}
return 0;
}
sub checklamer {
my ($zone,$nameserver)=@_;
my ($packet) = new Net::DNS::Packet($zone, "SOA", "IN");
my ($soa_req);
my ($res) = new Net::DNS::Resolver;
unless ($res->nameservers($nameserver)) {
&printerr("FAIL", "Cannot find address for nameserver: ".
$res->errorstring. "\n");
}
$soa_req = $res->send($packet);
unless (defined($soa_req)) {
&printerr("FAIL",
"Cannot get SOA record for $zone from $nameserver (lame?): ".
$res->errorstring ."\n");
return;
}
&printerr("BAD", "$zone NS $nameserver: lame NS delegation\n")
unless ($soa_req->header->aa);
return;
}
|