#!/usr/bin/perl # # Script for updating my local primary and secondary servers # You may have to change paths for your config files but use # this script wisely. The basic rules apply. # Created 06-04-99 # admin@wickedmind.com # # use Fcntl; # use DB_File; $ENV{PATH} = '/usr/bin:/usr/local/bin'; $ENV{ENV} = ''; ($>,$<) = (0,0); $|=42; $path = '/etc/dns'; $ip = '10.0.0.1'; $factor = 10; print "Chowning $path/domains/db.*...\n"; system "/usr/bin/chown root:root $path/domains/db.*"; system "/usr/bin/chown root:root $path/domains"; print "Chmoding $path/domains/db.*...\n"; system "/usr/bin/chmod 660 $path/domains/db.*"; system "/usr/bin/chmod 770 $path/domains"; # $new_path = $path; # open FD, "<$path/named.bak"; # undef $count; print "Scanning named.bak."; # while () { # if (/^;/) { next } # if (/^directory/) { # chomp; # s/^directory\s+//; # $new_path = $_; # next; # } # unless (/^primary/) { next } # $count++; if ($count%$factor == 0) { print "." } # chomp; # ($type, $zone, $file) = split " "; # push @file_list, "$new_path/$file"; # next if ($zone =~ /0\.0\.127/); # next if ($zone =~ /localhost/); # $new_secondary .= qq! # zone "$zone" { # type slave; # file "$file"; # masters { # $ip; # }; # }; # !; # # } open FD, ") { # parse file and get $zone, $file, $ip for $new_slave # # syntax # # zone domain_name [ ( in | hs | hesiod | chaos ) ] { # type master; # file path_name; # [ check-names ( warn | fail | ignore ); ] # [ allow-update { address_match_list }; ] # [ allow-query { address_match_list }; ] # [ allow-transfer { address_match_list }; ] # [ notify yes_or_no; ] # [ also-notify { ip_addr; [ ip_addr; ... ] }; # }; # # assume /^zone/ is always true # assume /^};$/ is always true # # both are bad assumptions. next if (m#^//# or /^#/); if (/^zone /) { $start++; chomp; $block = $_; } elsif (/^};$/) { $start = 0; chomp; $block .= $_; &parse_block($block); } elsif ($start) { chomp; s/\t/ /; $block .= $_; } } sub parse_block { # $block ~= /zone "$zone" { type master; file "dir/$file";};/ my $block = shift; $count++; if ($count%$factor == 0) { print '.' } return unless ($block =~ /type\s+master/); ($zone, $file) = ($block =~ /zone\s+"(.*?)".*file\s+"(.*?)"/); next if ($zone =~ /localhost/ or $zone =~ /0.0.127/); $file =~ s#.*/##; $new_slave .= qq! zone "$zone" { type slave; file "domains/$file"; masters { $ip; }; }; !; } print "\n"; undef $count; print "Updating serial numbers has been disabled."; print "\nBe sure to update serial numbers when editing files."; close FD; # Uncomment if you want to keep logs of failed updates. # # tie(%update, DB_File, "$path/update.db", O_CREAT|O_RDWR, 0644, $DB_HASH); # foreach $file (@file_list) { # $modified = -M $file; # if ($modified < $update{$file} or !$update{$file}) { # $count++; if ($count%$factor == 0) { print "." } # tie(@db, DB_File, "$file", O_RDWR, 0644, $DB_RECNO); # if ($db[1] =~ /serial/) { # $db[1] =~ s/(\d+)/$1+1/e; # } # else { # open FD, ">>$path/failed-update.log"; # print FD scalar localtime(time), " Unable to update $file: unrecognized # format\n"; # close FD; # } # untie @db; # $update{$file} = $modified; # } # } # untie %update; print "\nBuilding new slave named.conf.\n"; open FD, ">$path/slave.conf"; print FD qq! options { directory "/etc/dns"; check-names master warn; /* default */ datasize unlimited; }; zone "localhost" { type master; file "db.localhost"; check-names fail; allow-update { none; }; allow-transfer { any; }; }; zone "0.0.127.IN-ADDR.ARPA" { type master; file "db.127.0.0"; check-names fail; allow-update { none; }; allow-transfer { any; }; }; zone "." { type hint; file "db.cache"; }; $new_slave !; close FD; print "Copying new slave named.conf to ns1.wickedmind.com.\n"; system "/usr/local/bin/scp $path/slave.conf ns2.wickedmind.com:/etc/named.conf"; print "Copying new slave named.conf to ns2.wickedmind.com.:/etc/named.conf "; open FD, ") { ($month, $day, $time, $hostname, $process, @rest) = split " "; $message = join (" ", @rest); next unless ($process =~ /^named\[\d+\]:$/); print "\t$message\n"; if ($message =~ /^Ready/) { $exit++; last; } } if ($exit) { last } sleep 1; seek FD, 0, 1; } print "Reloading nameserver on ns1.wickedmind.com.\n"; system "/usr/local/bin/ssh dns\@ns1.wickedmind.com /bin/dns restart"; print "DNS has been updated.\n";