| 1 |
#!/usr/local/bin/perl |
| 2 |
|
| 3 |
# Do some sanity checks on the DNS forward and reverse files. |
| 4 |
|
| 5 |
# This checks for duplicate host and ip entries in DNS forward, |
| 6 |
# duplicate ip in DNS reverse, DNS forward entries with no reverse, DNS |
| 7 |
# forward entries that don't match their reverses, and DNS reverse |
| 8 |
# entries with no forward entry. |
| 9 |
|
| 10 |
# |
| 11 |
# This file is copyright (C) Philip Guenther <[email protected]>, 1999 |
| 12 |
# |
| 13 |
# This program is free software; redistribution and modification |
| 14 |
# in any form is explicitly permitted provided that all source |
| 15 |
# code versions retain this copyright notice and the following |
| 16 |
# disclaimer. |
| 17 |
# |
| 18 |
# This program is distributed in the hope that it will be useful, |
| 19 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 21 |
# |
| 22 |
# Questions, ideas and suggestions are enthusiasticly welcomed |
| 23 |
# by the author. |
| 24 |
# |
| 25 |
|
| 26 |
use strict; |
| 27 |
|
| 28 |
# Where is the named.conf file? |
| 29 |
my($named_conf) = "/etc/named.conf"; |
| 30 |
|
| 31 |
if (@ARGV) { |
| 32 |
$named_conf = shift; |
| 33 |
} |
| 34 |
|
| 35 |
sub regularize; |
| 36 |
|
| 37 |
my(%ip, %files, $file, $named_dir, %name, $domain, $name, $ip, %nomatch); |
| 38 |
|
| 39 |
$| = 1; |
| 40 |
$\ = "\n"; |
| 41 |
|
| 42 |
print "Checking DNS forward files..."; |
| 43 |
|
| 44 |
# Parse the named.conf file to find the primary domains |
| 45 |
Parser->parse_file($named_conf); |
| 46 |
|
| 47 |
chdir($named_dir); |
| 48 |
|
| 49 |
while(($domain, $file) = each(%files)) { |
| 50 |
next if $domain =~ /in-addr\.arpa/i; |
| 51 |
$domain =~ tr[A-Z][a-z]; |
| 52 |
print " - Scanning $file..."; |
| 53 |
open(FORW, $file) || die "unable to open $file: $!"; |
| 54 |
while(<FORW>) { |
| 55 |
my($nowarn, $nomatch); |
| 56 |
$nowarn = 1 if /;NOWARN/; |
| 57 |
$nomatch = 1 if /;NOMATCH/; |
| 58 |
next if /;SKIP/; |
| 59 |
s:;.*::; |
| 60 |
next if /^\s*$/; |
| 61 |
if (/\sA\s/) { |
| 62 |
if (/^[\w@]/) { |
| 63 |
if (/^([@\w-.]+)\s+(?:\d+\s+)?(?:IN\s+)?A\s+([\d.]+)/) { |
| 64 |
$ip = $2; |
| 65 |
$name = regularize($1, $domain); |
| 66 |
if ($name{$name}) { |
| 67 |
print "$name occurs multiple times" unless $nowarn; |
| 68 |
} else { |
| 69 |
$name{$name} = $ip; |
| 70 |
$nomatch{$name} = 1 if $nomatch; |
| 71 |
} |
| 72 |
} else { |
| 73 |
chop; |
| 74 |
print "Invalid line?\n$_"; |
| 75 |
} |
| 76 |
} elsif (/^\s+(?:\d+\s+)?(?:IN\s+)?A\s+([\d.]+)/) { |
| 77 |
$ip = $1; |
| 78 |
$name{$name} .= " $ip"; |
| 79 |
} else { |
| 80 |
print "Syntax error on line $.: $_"; |
| 81 |
next; |
| 82 |
} |
| 83 |
if ($ip{$ip}) { |
| 84 |
print "Duplicate ip $ip with hosts $ip{$ip} and $name" |
| 85 |
unless $nowarn; |
| 86 |
next; |
| 87 |
} |
| 88 |
if ($name !~ /\S/ || ! $ip) { |
| 89 |
print "Empty name, say wha? \$. = $., file = $file"; |
| 90 |
next; |
| 91 |
} |
| 92 |
$ip{$ip} = $name; |
| 93 |
} elsif (/^([@\w-]+)\s+(?:\d+\s+)?(?:IN\s+)?PTR\s+(\d+\.){4}in-addr\.arpa/i) { |
| 94 |
# A network tag |
| 95 |
chop($ip = $2); # rip off the trailing dot |
| 96 |
$name = regularize($1, $domain); |
| 97 |
$ip = join(".", reverse split(/\./, $ip)); # reverse the addr |
| 98 |
if ($name{$name}) { |
| 99 |
print "$name occurs multiple times" unless $nowarn; |
| 100 |
} else { |
| 101 |
$name{$name} = $ip; |
| 102 |
$nomatch{$name} = 1 if $nomatch; |
| 103 |
} |
| 104 |
if ($ip{$ip}) { |
| 105 |
print "Duplicate ip $ip with hosts $ip{$ip} and $name" |
| 106 |
unless $nowarn; |
| 107 |
next; |
| 108 |
} |
| 109 |
if ($name !~ /\S/ || $ip) { |
| 110 |
print "Empty name, say wha? \$. = $., file = $file"; |
| 111 |
next; |
| 112 |
} |
| 113 |
$ip{$ip} = $name; |
| 114 |
} elsif (/^([@\w-.]+)\s+(?:\d+\s+)?(?:IN\s+)?CNAME\s+([\w-.]+)/) { |
| 115 |
$name = regularize($1, $domain); |
| 116 |
if ($name{$name}) { |
| 117 |
print "$name occurs multiple times" unless $nowarn; |
| 118 |
} else { |
| 119 |
$name{$name} = regularize($2, $domain); |
| 120 |
$nomatch{$name} = 1 if $nomatch; |
| 121 |
} |
| 122 |
} elsif (/^\$ORIGIN\s+([\w-.]+)/i) { |
| 123 |
$domain = regularize($1, $domain); |
| 124 |
} else { |
| 125 |
next if /\sNS\s/; |
| 126 |
next if /\sTXT\s/; |
| 127 |
next if /\sHINFO\s/; |
| 128 |
next if /\sMX\s/; |
| 129 |
if (/\sSOA\s/) { |
| 130 |
if (/\(/) { |
| 131 |
while (<FORW>) { |
| 132 |
s/;.*//; |
| 133 |
last if /\)/; |
| 134 |
} |
| 135 |
} |
| 136 |
next; |
| 137 |
} |
| 138 |
print "Syntax error on line $.: unknown format: $_"; |
| 139 |
next; |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
close(FORW); |
| 144 |
} |
| 145 |
|
| 146 |
my(%revname, %revip); |
| 147 |
print "Checking DNS reverse files..."; |
| 148 |
|
| 149 |
while(($domain, $file) = each(%files)) { |
| 150 |
next unless $domain =~ s/\.in-addr\.arpa\.?//i; |
| 151 |
# flip the domain around |
| 152 |
$domain = join(".", reverse split(/\./, $domain)); |
| 153 |
print " - Scanning $file..."; |
| 154 |
open(REV, $file) || die "unable to open $file: $!"; |
| 155 |
while(<REV>) { |
| 156 |
my($nowarn, $nomatch); |
| 157 |
$nowarn = 1 if /;NOWARN/; |
| 158 |
$nomatch = 1 if /;NOMATCH/; |
| 159 |
next if /;SKIP/; |
| 160 |
s:;.*::; |
| 161 |
next if /^\s*$/; |
| 162 |
if (/\sPTR\s/) { |
| 163 |
if (/^(\d+)(\.\d+)?\s+(?:IN\s+)?PTR\s+([\w-.]+)\.\s*$/) { |
| 164 |
my($host, $subnet, $name) = ($1, $2, $3); |
| 165 |
$ip = "$domain$subnet.$host"; |
| 166 |
if ($revname{$name}) { |
| 167 |
$revname{$name} .= " $ip"; |
| 168 |
} else { |
| 169 |
$revname{$name} = $ip; |
| 170 |
} |
| 171 |
if ($revip{$ip}) { |
| 172 |
print "$ip occurs multiple times"; |
| 173 |
} else { |
| 174 |
$revip{$ip} = $name; |
| 175 |
$nomatch{$ip} = 1 if $nomatch; |
| 176 |
} |
| 177 |
} else { |
| 178 |
print "Unknown PTR format on line $.: $_" unless $nowarn; |
| 179 |
next; |
| 180 |
} |
| 181 |
} elsif (/^\$ORIGIN\s+([\w-.]+)/i) { |
| 182 |
if (substr($1, -1, 1) eq '.') { |
| 183 |
$domain = $1; |
| 184 |
chop($domain); |
| 185 |
if ($domain =~ s/\.in-addr\.arpa//i) { |
| 186 |
$domain = join(".", reverse split(/\./, $domain)); |
| 187 |
} else { |
| 188 |
print "\$ORIGIN set to out of zone: $file"; |
| 189 |
} |
| 190 |
} else { |
| 191 |
$domain .= join(".", '', reverse split(/\./, $1)); |
| 192 |
} |
| 193 |
next; |
| 194 |
} else { |
| 195 |
next if /\sNS\s/; |
| 196 |
next if /\sA\s/; # netmask declaration |
| 197 |
next if /\sTXT\s/; |
| 198 |
next if /\sHINFO\s/; |
| 199 |
if (/\sSOA\s/) { |
| 200 |
if (/\(/) { |
| 201 |
while (<REV>) { |
| 202 |
s/;.*//; |
| 203 |
last if /\)/; |
| 204 |
} |
| 205 |
} |
| 206 |
next; |
| 207 |
} |
| 208 |
print "Syntax error on line $.: unknown format: $_"; |
| 209 |
next; |
| 210 |
} |
| 211 |
} |
| 212 |
close(REV); |
| 213 |
} |
| 214 |
|
| 215 |
print "Checking DNS reverse against DNS forward..."; |
| 216 |
|
| 217 |
while(($ip, $name) = each %revip) { |
| 218 |
next if $nomatch{$ip}; |
| 219 |
if (defined $ip{$ip}) { |
| 220 |
if (!defined($name{$name})) { |
| 221 |
print <<EOM |
| 222 |
* Name for ip $ip in reverse ($name) not in forward. |
| 223 |
Forward file thinks it should be $ip{$ip} |
| 224 |
EOM |
| 225 |
} else { |
| 226 |
if ($name{$name} !~ /\b$ip\b/) { |
| 227 |
print " * $ip isn't an address for $name in the DNS forward"; |
| 228 |
} |
| 229 |
if ($name ne $ip{$ip}) { |
| 230 |
print <<EOM |
| 231 |
* Name for ip $ip in reverse ($name) is in the forward, |
| 232 |
but the forward think its address(es) is/are $name{$name}. |
| 233 |
EOM |
| 234 |
} |
| 235 |
} |
| 236 |
delete $ip{$ip}; |
| 237 |
} else { |
| 238 |
print " * No forward entry for $ip (name = $name)"; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
print "Checking for DNS forward entries with no reverse..."; |
| 243 |
foreach (sort keys %ip) { |
| 244 |
if (/^(?:127\..*|\d+)$/) { |
| 245 |
# These should be fine |
| 246 |
} else { |
| 247 |
print " * No reverse entry for $ip{$_}, ip = $_" |
| 248 |
unless $nomatch{$ip{$_}}; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
sub regularize { |
| 253 |
my($name, $domain) = @_; |
| 254 |
if ($name eq "@") { |
| 255 |
$name = $domain; |
| 256 |
} elsif (substr($name, -1, 1) ne '.') { |
| 257 |
$name .= ".$domain"; |
| 258 |
} else { |
| 259 |
chop($name); |
| 260 |
} |
| 261 |
$name |
| 262 |
} |
| 263 |
|
| 264 |
package Parser; |
| 265 |
|
| 266 |
use BIND::Conf_Parser; |
| 267 |
use vars qw(@ISA); |
| 268 |
BEGIN{ @ISA = qw(BIND::Conf_Parser); } |
| 269 |
|
| 270 |
sub handle_option { |
| 271 |
my($self, $option, $argument) = @_; |
| 272 |
return unless $option eq "directory"; |
| 273 |
$named_dir = $argument; |
| 274 |
} |
| 275 |
|
| 276 |
sub handle_zone { |
| 277 |
my($self, $name, $class, $type, $options) = @_; |
| 278 |
return unless $type eq "master" && $class eq "in"; |
| 279 |
$files{$name} = $options->{file}; |
| 280 |
} |