update-serial
changeset 31 1cea07056124
parent 30 5ac92c1ffdf9
--- a/update-serial	Wed Aug 11 14:08:33 2010 +0200
+++ b/update-serial	Thu Aug 12 10:18:58 2010 +0200
@@ -4,16 +4,15 @@
 # … work in progress do integrate dnssec (branch suess)
 #
 # Update the serial numbers in zone files
-# The serial number needs to match a specified pattern (see 
+# The serial number needs to match a specified pattern (see
 # the line marked w/ PATTERN.
-# 
+#
 # ToDo:
 # . test against an md5 sum, not just the date of the stamp file
-# . FIXME: handle `/' in file names (currently only working in 
+# . FIXME: handle `/' in file names (currently only working in
 #   the current directory)
 # . optionally reload the named
 
-
 use strict;
 use warnings;
 
@@ -22,9 +21,11 @@
 use FindBin;
 
 my @configs = ( "$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf" );
-my @dnssec_signs = ( "$FindBin::Bin/dnssec-sign", "/usr/bin/dnstools/dnssec-sign");
+my @dnssec_signs
+    = ( "$FindBin::Bin/dnssec-sign", "/usr/bin/dnstools/dnssec-sign" );
 my %config;
 my $dnssec_sign;
+my @change_names = ();
 
 foreach ( grep {-f} @configs ) {
     open( CONFIG, $_ ) or die "Can't open $_: $!\n";
@@ -34,15 +35,14 @@
     die "Can't open config (searched: @configs)\n";
 }
 foreach ( grep {-f} @dnssec_signs ) {
-	if (-x $_) {
-		$dnssec_sign = $_;
-	}
-	else {
-		die "Can't run $_\n"
-	}
+    if ( -x $_ ) {
+        $dnssec_sign = $_;
+    }
+    else {
+        die "Can't run $_\n";
+    }
 }
 
-
 while (<CONFIG>) {
     chomp;
     s/#.*//;
@@ -69,114 +69,125 @@
 END { cleanup(); }
 
 for (@ARGV) {
-	if ($_ eq "-y") {
-		$opt_yes = 1;
-		shift @ARGV;
-	}
+    if ( $_ eq "-y" ) {
+        $opt_yes = 1;
+        shift @ARGV;
+    }
 }
 
 @Zones = @ARGV ? @ARGV : glob("$master_dir/*");
 
+MAIN: {
+    my $changed;
+    my ( $dd, $mm, $yy ) = ( localtime() )[ 3 .. 5 ];
+    my $date;
+    $mm++;
 
-MAIN: {
-	my $changed;
-	my ($dd, $mm, $yy) =(localtime())[3..5];
-	my $date;
-	$mm++;
+    foreach ( $dd, $mm ) { s/^\d$/0$&/; }
+    $yy += 1900;
+    $date = "$yy$mm$dd";
 
-	foreach ($dd, $mm) { s/^\d$/0$&/; }
-	$yy += 1900;
-	$date = "$yy$mm$dd";
-
+    while ( my $file = shift @Zones ) {
 
-	while (my $file = shift @Zones) {
+        my $file_basename = basename($file);
 
-		my $file_basename = basename($file);
+        $file =~ s#($master_dir)(/.*)#$1$2$2#;
+        local ( *I, *O );
+        my $done = 0;
 
-		$file =~ s#($master_dir)(/.*)#$1$2$2#;
-		local (*I, *O);
-		my $done = 0;
+        my $new   = "$file.$$.tmp";
+        my $bak   = "$file.bak";
+        my $stamp = $master_dir . "/.stamp/" . basename($file);
 
-		my $new = "$file.$$.tmp";
-		my $bak = "$file.bak";
-		my $stamp = $master_dir . "/.stamp/" . basename($file);	
+        $file =~ /(\.bak|~)$/ and next;
+        $file !~ /\./ and next;
+
+        $verbose && print "$file:";
 
-		$file =~ /(\.bak|~)$/ and next;
-		$file !~ /\./ and next;
-
-		$verbose && print "$file:";
+        if ( -f $stamp && ( ( stat($stamp) )[9] >= ( stat($file) )[9] ) ) {
+            $verbose && print " fresh, skipping.\n";
+            next;
+        }
 
-
-		if (-f $stamp && ((stat($stamp))[9] >= (stat($file))[9])) {
-			$verbose && print " fresh, skipping.\n";
-			next;
-		}
+        $done = 0;
+        push @tmpfiles, $new;
+        open( *I, "<$file" ) or die("Can't open < $file: $!\n");
+        open( *O, ">$new" )  or die("Can't open > $new: $!\n");
 
-		$done = 0;
-		push @tmpfiles, $new;
-		open(*I, "<$file") or die("Can't open < $file: $!\n");
-		open(*O, ">$new") or die("Can't open > $new: $!\n");
+        while (<I>) {
+            /^\s+((\d+)(\d{2}))\s*;\s*serial/i and do {    # PATTERN
+                my ( $sdate, $scount, $serial ) = ( $2, $3, $1 );
+                $done = 1;
+                print " [$file] serial $sdate$scount";
+
+                if   ( $date eq $sdate ) { $scount++; }
+                else                     { $sdate = $date; $scount = "00"; }
 
-		while (<I>) {
-			/^\s+((\d+)(\d{2}))\s*;\s*serial/i and do {		# PATTERN
-				my ($sdate, $scount, $serial) = ($2, $3, $1);
-				$done = 1;
-				print " [$file] serial $sdate$scount";
+                print " bumping to $sdate$scount";
+                s/$serial/$sdate$scount/;
 
-				if ($date eq $sdate) { $scount++; }
-				else { $sdate = $date; $scount = "00"; }
+            };
+            print O;
+        }
+
+        close(O);
+        close(I);
 
-				print " bumping to $sdate$scount \n";
-				s/$serial/$sdate$scount/;
+        if ($done) {
+
+            # copy($file, $bak) or die("Can't copy $file -> $bak: $!\n");
 
-			};
-			print O;
-		}
+            open( I, "<$new" )  or die("Can't open <$new: $!\n");
+            open( O, ">$file" ) or die("Can't open >$file: $!\n");
+            while (<I>) { print O or die("Can't write to $file: $!\n"); }
+            close(I) or die("Can't close $new: $!\n");
+            close(O) or die("Can't close $file: $!\n");
 
-		close(O); close(I);
-
-		if ($done) {
-			# copy($file, $bak) or die("Can't copy $file -> $bak: $!\n");
+            unlink $new;
 
-			open(I, "<$new") or die("Can't open <$new: $!\n");
-			open(O, ">$file") or die("Can't open >$file: $!\n");
-			while (<I>) { print O or die("Can't write to $file: $!\n"); }
-			close(I) or die("Can't close $new: $!\n");
-			close(O) or die("Can't close $file: $!\n");
+            open( O, ">$stamp" ) or die("Can't open >$stamp: $!\n");
+            close(O);
+            $changed++;
+
+            push @change_names, $file_basename;
 
-			unlink $new;
+        }
+        else {
+            print " $file: no serial number found: no zone file?";
+        }
+        print "\n";
+    }
 
-			open(O, ">$stamp") or die("Can't open >$stamp: $!\n");
-			close(O);
-			$changed++;
+    if ($changed) {
+        my $pidfile;
 
-			# dnssec - new sign
-			system "$dnssec_sign $file_basename";
-			die "$dnssec_sign not found ($!)" if $? == -1;
-			exit 1 if $?;
+        # dnssec - new sign
+        system "$dnssec_sign @change_names";
+        die "$dnssec_sign not found ($!)" if $? == -1;
+        exit 1 if $?;
 
-		} else {
-			print " $file: no serial number found: no zone file?";
-		}
-		print "\n";
-	}
-
-	if ($changed) {
-		my $pidfile;
+        print
+            "** Changed $changed files, the nameserver needs to be reloaded!\n";
+        foreach (
+            qw(/var/run/bind/run/named.pid /var/run/named.pid /etc/named.pid))
+        {
+            -f $_ and $pidfile = $_ and last;
+        }
 
-		print "** Changed $changed files, the nameserver needs to be reloaded!\n";
-		foreach (qw(/var/run/bind/run/named.pid /var/run/named.pid /etc/named.pid)) { 
-			-f $_ and $pidfile = $_ and last; }
+        if ($pidfile) {
+            if ($opt_yes) {
+                $_ = "y";
+                print "** Nameserver will be reloaded\n";
+            }
+            else { print "** Reload now? [Y/n]: "; $_ = <STDIN>; }
+            /^y|^$/i and system "rndc reload";
+        }
+        else {
+            print
+                "** No PID of a running named found.  Please reload manually.\n";
+        }
 
-		if ($pidfile) {
-			if ($opt_yes) { $_ = "y"; print "** Nameserver will be reloaded\n"; } 
-			else { print "** Reload now? [Y/n]: "; $_ = <STDIN>; }
-			/^y|^$/i and system "rndc reload";
-		} else {
-			print "** No PID of a running named found.  Please reload manually.\n";
-		}
-
-	}
+    }
 }
 
 # vim:ts=4:sw=4:ai:aw: