File: check_zone

package info (click to toggle)
libnet-dns-perl 0.19-0.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 544 kB
  • ctags: 323
  • sloc: perl: 5,191; makefile: 54
file content (174 lines) | stat: -rwxr-xr-x 3,478 bytes parent folder | download
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
#!/usr/bin/perl -w
# $Id: check_zone,v 1.6 2000/11/10 16:02:20 mfuhr Exp mfuhr $

=head1 NAME

check_zone - Check a DNS zone for errors

=head1 SYNOPSIS

C<check_zone> [ C<-r> ] I<domain> [ I<class> ]

=head1 DESCRIPTION

Checks a DNS zone for errors.  Current checks are:

=over 4

=item *

Checks that all A records have corresponding PTR records.

=item *

Checks that hosts listed in NS, MX, and CNAME records have
A records.

=back

=head1 OPTIONS

=over 4

=item C<-r>

Perform a recursive check on subdomains.

=back

=head1 AUTHOR

Michael Fuhr <mike@fuhr.org>

=head1 SEE ALSO

L<perl(1)>, L<axfr>, L<check_soa>, L<mresolv>, L<mx>, L<perldig>, L<Net::DNS>

=cut

use strict;
use vars qw($opt_r);

use Getopt::Std;
use File::Basename;
use IO::Socket;
use Net::DNS;

getopts("r");

die "Usage: ", basename($0), " [ -r ] domain [ class ]\n"
	unless (@ARGV >= 1) && (@ARGV <= 2);

check_domain(@ARGV);
exit;

sub check_domain {
	my ($domain, $class) = @_;
	$class ||= "IN";

	print "-" x 70, "\n";
	print "$domain (class $class)\n";
	print "\n";

	my $res = Net::DNS::Resolver->new;
	$res->defnames(0);
	$res->retry(2);

	my $nspack = $res->query($domain, "NS", $class);

	unless (defined($nspack)) {
		warn "Couldn't find nameservers for $domain: ",
		     $res->errorstring, "\n";
		return;
	}

	print "nameservers (will request zone from first available):\n";
	my $ns;
	foreach $ns (grep { $_->type eq "NS" } $nspack->answer) {
		print "\t", $ns->nsdname, "\n";
	}
	print "\n";
		
	$res->nameservers(map  { $_->nsdname }
			  grep { $_->type eq "NS" }
			  $nspack->answer);

	my @zone = $res->axfr($domain, $class);
	unless (@zone) {
		warn "Zone transfer failed: ", $res->errorstring, "\n";
		return;
	}

	print "checking PTR records\n";
	check_ptr($domain, $class, @zone);
	print "\n";

	print "checking NS records\n";
	check_ns($domain, $class, @zone);
	print "\n";

	print "checking MX records\n";
	check_mx($domain, $class, @zone);
	print "\n";

	print "checking CNAME records\n";
	check_cname($domain, $class, @zone);
	print "\n";

	if ($opt_r) {
		print "checking subdomains\n\n";
		my %subdomains;
		foreach (grep { $_->type eq "NS" and $_->name ne $domain } @zone) {
			$subdomains{$_->name} = 1;
		}
		foreach (sort keys %subdomains) {
			check_domain($_, $class);
		}
	}
}

sub check_ptr {
	my ($domain, $class, @zone) = @_;
	my $res = Net::DNS::Resolver->new;
	my $rr;
	foreach $rr (grep { $_->type eq "A" } @zone) {
		my $host = $rr->name;
		my $addr = $rr->address;
		my $ans = $res->send($addr, "A", $class);
		print "\t$host ($addr) has no PTR record\n"
			if ($ans->header->ancount < 1);
	}
}

sub check_ns {
	my ($domain, $class, @zone) = @_;
	my $res = Net::DNS::Resolver->new;
	my $rr;
	foreach $rr (grep { $_->type eq "NS" } @zone) {
		my $ans = $res->send($rr->nsdname, "A", $class);
		print "\t", $rr->nsdname, " has no A record\n"
			if ($ans->header->ancount < 1);
	}
}

sub check_mx {
	my ($domain, $class, @zone) = @_;
	my $res = Net::DNS::Resolver->new;
	my $rr;
	foreach $rr (grep { $_->type eq "MX" } @zone) {
		my $ans = $res->send($rr->exchange, "A", $class);
		print "\t", $rr->exchange, " has no A record\n"
			if ($ans->header->ancount < 1);
	}
}

sub check_cname {
	my ($domain, $class, @zone) = @_;
	my $res = Net::DNS::Resolver->new;
	my $rr;
	foreach $rr (grep { $_->type eq "CNAME" } @zone) {
		my $ans = $res->send($rr->cname, "A", $class);
		print "\t", $rr->cname, " has no A record\n"
			if ($ans->header->ancount < 1);
	}
}