#!/usr/bin/perl # Name: Messy Create Reverse DNS Script # Description: Takes all forward dns records and attempts to make reverse dns records # Version: 1.0 # Authors: Jason Jorgensen # Copyright (C) 2003 Jason Jorgensen # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # 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. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ########################################################################### #Assumes master forward and records are in subdirectories from the script $forwardpath = "master/"; $reversepath = "reverse/"; my $debug = 0; my $line; my %addrs; my @inarpa; my $inarpa; opendir FORWARDFILES, "$forwardpath"; my @forwardfiles = grep /^db\./, readdir FORWARDFILES; foreach $file (@forwardfiles) { my $headerstate = 2; my @header; my $domain; my $host; my $name; my $fullip; print "Opening file '$forwardpath$file'\n"; open FORWARD, "<$forwardpath$file" || die "Cant open file '$file'"; while () { $line = $_; chomp $line; print "DEBUG: line: $line\n" if $debug; if ($line =~ /;DEFAULTDOMAIN/) { (undef, $domain) = split /\s+/, $line; chomp $domain; } if ($line =~ /;HEADEREND/) { $headerstate = 0; } if ($headerstate == 1) { push @header, $line."\n"; } if ($line =~ /;HEADERSTART/) { $headerstate = 1; } if ($line =~ /^(\w+)\s+IN\s+A\s+(\d+\.\d+\.\d+\.\d+)$/) { $name = $1; $fullip = $2; ($inarpa[2],$inarpa[1],$inarpa[0], $host) = split /\./, $2; $inarpa = join '.', @inarpa; print "DEBUG: \$addrs{$inarpa}{$1} = $host\n" if $debug; if ($addrs{$inarpa}{$host}) { $inarpa =~ /(\d+)\.(\d+)\.(\d+)/; my $subnet = "$3.$2.$1"; print "Error, duplicate IP, resolution confliction: '$fullip-$name' overwriting '$subnet.$host-".$addrs{$inarpa}{$host}."\n"; } if ($name =~ /.*\..*\.\s+$/) { $addrs{$inarpa}{$host} = $name; } elsif ($name =~ /\.\s+$/) { $addrs{$inarpa}{$host} = "$name."; } else { $addrs{$inarpa}{$host} = "$name.$domain."; } } $addrs{$inarpa}{header} = join '', @header; } if (! $domain) { print "Never saw domain name, make sure you specify ';DEFAULTDOMAIN example.com'\n"; next; } if ($headerstate == 2) { print "Never saw start of header, make sure you start your header with ';HEADERSTART'"; next; } if ($headerstate == 1) { print "Never saw end of header, make sure you end your header with ';HEADEREND'"; next; } #$addrs{$inarpa}{domain} .= " $domain"; close FORWARD; } closedir FORWARDFILES; print "DEBUG: domain: $domain\n" if $debug; foreach $inarpa (sort keys(%addrs)) { if ($inarpa =~ /(\d+)\.(\d+)\.(\d+)/) { my $subnet = "$3.$2.$1"; open REVERSE, ">".$reversepath."db.$inarpa"; print REVERSE ";Reverse DNS for $subnet\n"; print REVERSE $addrs{$inarpa}{header}; print REVERSE ";HOSTS\n"; foreach $addr (sort numerical keys(%{$addrs{$inarpa}})) { if ($addr =~ /\d+/) { print REVERSE "$addr\t\tIN\tPTR\t".$addrs{$inarpa}{$addr}."\n"; } } close REVERSE; } } exit; sub numerical { $a <=> $b; }