#!/usr/bin/perl # For Debugging purposes only!! $DEBUG = 1; @key_entries = ( "options", "controls", "zone \".\" IN", "zone \"localhost\"", "zone \"0.0.127.in-addr.arpa\"" ); @keep_zone_entries; @keep_config_entries; # Sub routine: removeHostsFile # Passes : String containing the record entry. # Returns : Nothing # Description: removeHostsFile searches through the record looking for the path of the hosts file. # Once found it will remove the file. sub removeHostsFile { $test_entry = $_; if($DEBUG) { print("\nTest Entry: $_"); } $begin = index($test_entry, "file \""); $end = index($test_entry, "\";", $begin); $off = $begin + length("file \""); $len = ($end - $begin)-6; $hosts_file = substr($test_entry, $off, $len ); if($DEBUG) { print "\nBegin: $begin"; print "\nEnd: $end"; print "\nOffset: $off"; print "\nlength: $len"; print "\n" . substr($test_entry, $off, $len ); print "\n"; } if($DEBUG) { print("\nRemoving $_\nand Deleteing: $hosts_file"); } system("rm -f $hosts_file"); } # Sub routine: ProcessDNSRecord # Passes : String containing a domain name, Array containing the lines of the record entry for the domain (ie paulslinuxbox.net) # Returns : Nothing # Description: Takes a string and then digs the domain name and dumps # the output of dig into a file with the name of the domain it was diging for. sub ProcessDNSRecord { my($d_name, $r_entry) = @_; my($add_to_array) = 0; if( $DEBUG ) { print( "Domain Name: " . $d_name . "\nnamed.conf Entry:\n" . $r_entry . "\n-----------------------\n"); print( "Running: dig 169.207.1.3 " . $d_name . " >> ./temp/" . $d_name ); } # Run the dig program and look up information on the domain name in $_ system("dig 169.207.1.3 " . $d_name . " >> ./temp/" . $d_name ); open(output_file, "./temp/$d_name"); @array = ; close(output_file); if( $DEBUG ) { print("\nContents of temporary file:\n"); foreach(@array) { print("CONTENTS: $_\n"); } } # Read each line and look for the strings 'NS' and milwaukeepc.com foreach(@array) { if( $DEBUG ) { print( $_ ); } if( index($_, "milwaukeepc.com") > 0) { if($DEBUG) { print("NS RECORD: \n$domain_name"); } if($DEBUG) { print("\nThis is being writen to the new file: \n$record_entry"); } $add_to_array++; } } if( $add_to_array > 0) { if ($DEBUG) { print("\nrecord entry going into array: $record_entry"); } push(@keep_zone_entries, $r_entry); } if( $add_to_array <= 0 ) { print("\nRecord Entry: $r_entry"); } } # Sub routine: GetDomainName # Passes : String containing an entire zone entry # Returns : String containing a domain name (ie paulslinuxbox.net) # Description: Takes a string that contains a zone record from the /etc/named.conf sub GetDomainName { if($DEBUG) { print "\nGetDomainName passed $_") $i = index($_[0], "zone"); if($DEBUG) { print "\nzone position: $i"; } if($i == 0) { @zone_array = split("\"", $_[0], 3); if( $DEBUG ) { print("\nThe domain name that was extracted is: $zone_array[1]"); } if( ( $_ ne "." ) || ( index($_, "localhost") > 0 ) || ( index($_, "in-addr.arpa") > 0) ) { if($DEBUG) { print("\nI am going to be returning $zone_array[1]"); } return $zone_array[1]; } } else { return ""; } } # Sub routine: GetZoneEntriesOnly # Passes : Array containing all entries from the /etc/named.conf file. # Returns : Array containing all the zone entries. # Description: Takes a string which is the entire /etc/named.conf file and searches for all zone entries and returns an array with all zone # entries. sub GetZoneEntriesOnly { @return_array; foreach ( @_ ) { $i = index($_, "zone"); if($DEBUG) { print("\n\"zone\" found at $i"); } if( ( $i >= 0 ) ) { push(@return_array, $_); if($DEBUG) { print("\nDEBUG(GetZoneEntriesOnly): Pushing $_ onto array."); } } else { # foreach(@array_entries) # { # $current=$_; # foreach(@key_entries) # { # if( index($current,$_) >= 0) # { push(@keep_config_entries,$_); # } # } # } } } return @return_array; } # Sub routine: writeEntriesToFile # Passes : Nothing # Returns : Nothing # Description: Writes all entries stored in arrays to file. sub writeEntriesToFile { open(ncf, ">>new_named.conf"); foreach(@keep_config_entries) { print ncf "$_\n\n"; } foreach(@keep_zone_entries) { print ncf "$_\n\n"; } close(ncf); } # Sub routine: SeparateEntries # Passes : Array containing the entire /etc/named.conf file line by line. # Returns : Array containing all the entries. # Description: Takes a string which is the entire /etc/named.conf file and searches for all zone entries and returns an array with all zone # entries. sub ReadConfFile { open(named_conf); @contents_named_conf = ; close(named_conf); # Since the contents were read line by line into an array we want to put it into one massive var so it's easier to parse. foreach (@contents_named_conf) { $content .= $_; } @named_records = split("\n\n", $content); return @named_records; } # Sub outine: Creates a new Associative Array # Passes : String, String # Returns : Associative Array # Description: The first argument is a string that will be the key, the second string is the actual record found in the /etc/named.conf. sub CreateAssociativeArray { my($key,$value) = @_; $array{$key} = $value; return %array; } ################################### # Begin of Main script ################################### # Back up existing configuration files: system("mkdir ./named.bak-`date +%F`"); system("cp /etc/named.conf ./named.bak-`date +%F`"); system("cp -R /var/named/ ./named.bak-`date +%F`"); $named_conf = "/etc/named.conf"; @named_records = ReadConfFile(); @r_array = GetZoneEntriesOnly(@named_records); foreach (@r_array) { if($DEBUG) { print("\nCurrent Entry: $_"); print("\nCurrent Domain: " . GetDomainName($_) ); } ProcessDNSRecord( GetDomainName($_), $_ ); } writeEntriesToFile(); #start cleaning up after ourselves system("rm -rf temp/*"); system("mv new_named.conf /etc/named.conf");