File: verify_zones

package info (click to toggle)
libbind-confparser-perl 0.95-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 208 kB
  • sloc: perl: 1,096; makefile: 2
file content (280 lines) | stat: -rwxr-xr-x 6,913 bytes parent folder | download | duplicates (4)
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
#!/usr/local/bin/perl

# Do some sanity checks on the DNS forward and reverse files.

# This checks for duplicate host and ip entries in DNS forward,
# duplicate ip in DNS reverse, DNS forward entries with no reverse, DNS
# forward entries that don't match their reverses, and DNS reverse
# entries with no forward entry.

#
# This file is copyright (C)  Philip Guenther <guenther@gac.edu>, 1999
# 
# This program is free software; redistribution and modification
# in any form is explicitly permitted provided that all source
# code versions retain this copyright notice and the following
# disclaimer.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Questions, ideas and suggestions are enthusiasticly welcomed
# by the author.
#

use strict;

# Where is the named.conf file?
my($named_conf) = "/etc/named.conf";

if (@ARGV) {
    $named_conf = shift;
}

sub regularize;

my(%ip, %files, $file, $named_dir, %name, $domain, $name, $ip, %nomatch);

$| = 1;
$\ = "\n";

print "Checking DNS forward files...";

# Parse the named.conf file to find the primary domains
Parser->parse_file($named_conf);

chdir($named_dir);

while(($domain, $file) = each(%files)) {
    next if $domain =~ /in-addr\.arpa/i;
    $domain =~ tr[A-Z][a-z];
    print " - Scanning $file...";
    open(FORW, $file) || die "unable to open $file: $!";
    while(<FORW>) {
	my($nowarn, $nomatch);
	$nowarn = 1 if /;NOWARN/;
	$nomatch = 1 if /;NOMATCH/;
	next if /;SKIP/;
	s:;.*::;
	next if /^\s*$/;
	if (/\sA\s/) {
	    if (/^[\w@]/) {
		if (/^([@\w-.]+)\s+(?:\d+\s+)?(?:IN\s+)?A\s+([\d.]+)/) {
		    $ip = $2;
		    $name = regularize($1, $domain);
		    if ($name{$name}) {
			print "$name occurs multiple times" unless $nowarn;
		    } else {
			$name{$name} = $ip;
			$nomatch{$name} = 1 if $nomatch;
		    }
		} else {
		    chop;
		    print "Invalid line?\n$_";
		}
	    } elsif (/^\s+(?:\d+\s+)?(?:IN\s+)?A\s+([\d.]+)/) {
		$ip = $1;
		$name{$name} .= " $ip";
	    } else {
		print "Syntax error on line $.: $_";
		next;
	    }
	    if ($ip{$ip}) {
		print "Duplicate ip $ip with hosts $ip{$ip} and $name"
						unless $nowarn;
		next;
	    }
	    if ($name !~ /\S/ || ! $ip) {
		print "Empty name, say wha?  \$. = $., file = $file";
		next;
	    }
	    $ip{$ip} = $name;
	} elsif (/^([@\w-]+)\s+(?:\d+\s+)?(?:IN\s+)?PTR\s+(\d+\.){4}in-addr\.arpa/i) {
	    # A network tag
	    chop($ip = $2);		# rip off the trailing dot
	    $name = regularize($1, $domain);
	    $ip = join(".", reverse split(/\./, $ip));	# reverse the addr
	    if ($name{$name}) {
		print "$name occurs multiple times" unless $nowarn;
	    } else {
		$name{$name} = $ip;
		$nomatch{$name} = 1 if $nomatch;
	    }
	    if ($ip{$ip}) {
		print "Duplicate ip $ip with hosts $ip{$ip} and $name"
						unless $nowarn;
		next;
	    }
	    if ($name !~ /\S/ || $ip) {
		print "Empty name, say wha?  \$. = $., file = $file";
		next;
	    }
	    $ip{$ip} = $name;
	} elsif (/^([@\w-.]+)\s+(?:\d+\s+)?(?:IN\s+)?CNAME\s+([\w-.]+)/) {
	    $name = regularize($1, $domain);
	    if ($name{$name}) {
		print "$name occurs multiple times" unless $nowarn;
	    } else {
		$name{$name} = regularize($2, $domain);
		$nomatch{$name} = 1 if $nomatch;
	    }
	} elsif (/^\$ORIGIN\s+([\w-.]+)/i) {
	    $domain = regularize($1, $domain);
	} else {
	    next if /\sNS\s/;
	    next if /\sTXT\s/;
	    next if /\sHINFO\s/;
	    next if /\sMX\s/;
	    if (/\sSOA\s/) {
		if (/\(/) {
		    while (<FORW>) {
			s/;.*//;
			last if /\)/;
		    }
		}
		next;
	    }
	    print "Syntax error on line $.: unknown format: $_";
	    next;
	}

    }
    close(FORW);
}

my(%revname, %revip);
print "Checking DNS reverse files...";

while(($domain, $file) = each(%files)) {
    next unless $domain =~ s/\.in-addr\.arpa\.?//i;
    # flip the domain around
    $domain = join(".", reverse split(/\./, $domain));
    print " - Scanning $file...";
    open(REV, $file) || die "unable to open $file: $!";
    while(<REV>) {
	my($nowarn, $nomatch);
	$nowarn = 1 if /;NOWARN/;
	$nomatch = 1 if /;NOMATCH/;
	next if /;SKIP/;
	s:;.*::;
	next if /^\s*$/;
	if (/\sPTR\s/) {
	    if (/^(\d+)(\.\d+)?\s+(?:IN\s+)?PTR\s+([\w-.]+)\.\s*$/) {
		my($host, $subnet, $name) = ($1, $2, $3);
		$ip = "$domain$subnet.$host";
		if ($revname{$name}) {
		    $revname{$name} .= " $ip";
		} else {
		    $revname{$name} = $ip;
		}
		if ($revip{$ip}) {
		    print "$ip occurs multiple times";
		} else {
		    $revip{$ip} = $name;
		    $nomatch{$ip} = 1 if $nomatch;
		}
	    } else {
		print "Unknown PTR format on line $.: $_" unless $nowarn;
		next;
	    }
	} elsif (/^\$ORIGIN\s+([\w-.]+)/i) {
	    if (substr($1, -1, 1) eq '.') {
		$domain = $1;
		chop($domain);
		if ($domain =~ s/\.in-addr\.arpa//i) {
		    $domain = join(".", reverse split(/\./, $domain));
		} else {
		    print "\$ORIGIN set to out of zone: $file";
		}
	    } else {
		$domain .= join(".", '', reverse split(/\./, $1));
	    }
	    next;
	} else {
	    next if /\sNS\s/;
	    next if /\sA\s/;		# netmask declaration
	    next if /\sTXT\s/;
	    next if /\sHINFO\s/;
	    if (/\sSOA\s/) {
		if (/\(/) {
		    while (<REV>) {
			s/;.*//;
			last if /\)/;
		    }
		}
		next;
	    }
	    print "Syntax error on line $.: unknown format: $_";
	    next;
	}
    }
    close(REV);
}

print "Checking DNS reverse against DNS forward...";

while(($ip, $name) = each %revip) {
    next if $nomatch{$ip};
    if (defined $ip{$ip}) {
	if (!defined($name{$name})) {
	    print <<EOM
 * Name for ip $ip in reverse ($name) not in forward.
   Forward file thinks it should be $ip{$ip}
EOM
	} else {
	    if ($name{$name} !~ /\b$ip\b/) {
		print " * $ip isn't an address for $name in the DNS forward";
	    }
	    if ($name ne $ip{$ip}) {
		print <<EOM
 * Name for ip $ip in reverse ($name) is in the forward,
   but the forward think its address(es) is/are $name{$name}.
EOM
	    }
	}
	delete $ip{$ip};
    } else {
	print " * No forward entry for $ip (name = $name)";
    }
}

print "Checking for DNS forward entries with no reverse...";
foreach (sort keys %ip) {
    if (/^(?:127\..*|\d+)$/) {
	# These should be fine
    } else {
	print " * No reverse entry for $ip{$_}, ip = $_"
		    unless $nomatch{$ip{$_}};
    }
}

sub regularize {
    my($name, $domain) = @_;
    if ($name eq "@") {
	$name = $domain;
    } elsif (substr($name, -1, 1) ne '.') {
	$name .= ".$domain";
    } else {
	chop($name);
    }
    $name
}

package Parser;

use BIND::Conf_Parser;
use vars qw(@ISA);
BEGIN{ @ISA = qw(BIND::Conf_Parser); }

sub handle_option {
    my($self, $option, $argument) = @_;
    return unless $option eq "directory";
    $named_dir = $argument;
}

sub handle_zone {
    my($self, $name, $class, $type, $options) = @_;
    return unless $type eq "master" && $class eq "in";
    $files{$name} = $options->{file};
}