Index: branches/stable/cgi-bin/extras/db2rwhois.pl
===================================================================
--- branches/stable/cgi-bin/extras/db2rwhois.pl	(revision 366)
+++ branches/stable/cgi-bin/extras/db2rwhois.pl	(revision 367)
@@ -10,5 +10,5 @@
 # Last update by $Author$
 ###
-# Copyright (C) 2004,2005 - Kris Deugau
+# Copyright (C) 2004-2007 - Kris Deugau
 
 use strict;
@@ -17,8 +17,20 @@
 use NetAddr::IP;
 use MyIPDB;
+use File::Path 'rmtree';
 
 $ENV{"PATH"} = "/bin;/usr/bin";
 
 my $rwhoisDataPath = "/etc/rwhoisd";
+
+my @autharea;
+my $authrw;
+# Use the template file to allow us to keep persistent nodes aside from netblock data
+open AUTHTEMPLATE, "<$rwhoisDataPath/rwhoisd.auth_template";
+my $template_persist;
+while (<AUTHTEMPLATE>) {
+  next if /^##/;
+  $template_persist = 1 if /^[a-z]/i;
+  $autharea[0] .= $_;
+}
 
 my ($dbh,$msg) = connectDB_My;
@@ -30,31 +42,117 @@
 my %netnameprefix;
 
+# Get the list of live directories for potential deletion
+opendir RWHOISROOT, $rwhoisDataPath;
+my %rwhoisdirs;
+foreach (readdir RWHOISROOT) {
+  $rwhoisdirs{$_} = 1 if /^net-/;
+}
+closedir RWHOISROOT;
+
 # Fill in data about our master blocks as allocated from ARIN
 # We open separate files for each of these as appropriate.
-# Note that this ASS-U-MEs that we do not add master IP blocks-
-# there should probably be a separate system for doing that.
-my $sth = $dbh->prepare("select cidr,ctime,mtime from masterblocks;");
+# Changes in master blocks are treated as complete new masters - since we're exporting
+# all data every time, this isn't so terrible as it might seem.
+my $sth = $dbh->prepare("select cidr,ctime,mtime from masterblocks where rwhois='y'");
 $sth->execute;
 my $i=0;
 GETMASTERS: while (my @data = $sth->fetchrow_array()) {
 
-# Techically, we only need to exclude 204.138.172.0/24, as we "own" all of the other blocks.
-# However, 205.207.184.0/23 and 206.130.64.0/24 are, um, awkward.
-  if ($data[0] =~ /^(192.168.0.0|172.16.0.0|10.0.0.0|20[456])/) {
-    next GETMASTERS;
-  }
   $masterblocks[$i] = new NetAddr::IP $data[0];
   my ($ctime,undef) = split /\s/, $data[1];
   my ($mtime,undef) = split /\s/, $data[2];
-print "$masterblocks[$i] $ctime $mtime\n";
+
+  print "$masterblocks[$i] $ctime $mtime\n";
 
   my $date;
   chomp ($date = `/bin/date +"%Y-%m-%d"`);
 
+  my $rwnet = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen;
+
+  # unflag the directory for deletion.  Whee!  Roundabout!
+  delete $rwhoisdirs{$rwnet};
+
+# Hokay.  Gonna do checks *here* to see if we need to create new master trees
+  my $netdatadir = "$rwhoisDataPath/$rwnet";
+  if (! -e $netdatadir) {
+    print " New master $masterblocks[$i]!\n";
+    print "  Creating directories...\n";
+    mkdir $netdatadir;
+    mkdir "$netdatadir/attribute_defs";
+    mkdir "$netdatadir/data";
+    mkdir "$netdatadir/data/network";
+    mkdir "$netdatadir/data/org";
+    mkdir "$netdatadir/data/referral";
+
+    my $serial;
+    chomp ($serial = `/bin/date '+%Y%m%d'000000000`);
+
+    print "  Creating SOA...\n";
+    open SOAFILE, ">$netdatadir/soa";
+    print SOAFILE qq(Serial-Number: $serial
+Refresh-Interval: 3600
+Increment-Interval: 1800
+Retry-Interval: 1800
+Time-To-Live: 86400
+Primary-Server: rwhois.example.com:4321
+Hostmaster: dns\@example.com
+);
+    close SOAFILE;
+
+    print "  Creating Schema...\n";
+    open SCHEMAFILE, ">$netdatadir/schema";
+    print SCHEMAFILE qq(name: network
+attributedef: $rwnet/attribute_defs/network.tmpl
+dbdir: $rwnet/data/network
+Schema-Version: $serial
+---
+name: organization
+attributedef: $rwnet/attribute_defs/org.tmpl
+dbdir: $rwnet/data/org
+description: Organization object
+Schema-Version: $serial
+---
+name: referral
+attributedef:$rwnet/attribute_defs/referral.tmpl
+dbdir:$rwnet/data/referral
+Schema-Version: $serial
+);
+    close SCHEMAFILE;
+
+    print "  Copying template files...\n";
+    qx { /bin/cp $rwhoisDataPath/skel/attribute_defs/* $netdatadir/attribute_defs/ };
+
+    print "  Creating org data...\n";
+    open ORGDATAFILE, ">$netdatadir/data/org/friendlyisp.txt";
+    print ORGDATAFILE qq(ID: NETBLK-ISP.$masterblocks[$i]
+Auth-Area: $masterblocks[$i]
+Org-Name: Friendly ISP
+Street-Address: 123 4th Street
+City: Anytown
+State: ON
+Postal-Code: H0H 0H0
+Country-Code: CA
+Phone: 000-555-1234
+Created: 20040308
+Updated: 20040308
+);
+    close ORGDATAFILE;
+
+    # Generate auth_area record, and add it to the array.
+    $authrw = 1;	# Flag for rewrite and daemon reload/restart
+
+  } # new master
+
+  # do this for all masters, so that we can use this array to export the data
+  # to rwhoisd.auth_area later if we need to
+  push @autharea, qq(type:master
+name:$masterblocks[$i]
+data-dir: $rwnet/data
+schema-file: $rwnet/schema
+soa-file: $rwnet/soa
+);
+
 # Whew!  Ugly little varmint.
-  my $masterfilename = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.
-    "/data/network/".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.".txt";
-
-# Need check here to create tree for netblock?
+  my $masterfilename = "$rwnet/data/network/$rwnet.txt";
 
   open MASTERFILE,">$rwhoisDataPath/$masterfilename";
@@ -211,13 +309,47 @@
   } # foreach master
 
-
-
-  #  print "$data[0]\t| $data[1]\t| $data[2]\t| $data[3]\t| $data[4]\t| ".
-  #	"$data[5]\t| $data[6]\t| $data[7]\t| $data[8]\t| $data[9]\n";
-  #  print "$data[0]\t| $data[1]\t| $data[2]\t| $data[3]\t| $data[4]\t| ".
-  #	"$data[5]\t| $data[6]\t| $data[7]\t| $data[8]\n";
   $i++;
 } # while fetchrow_array()
 
-
+foreach my $netdir (keys %rwhoisdirs) {
+  print "deleting obsolete directory $netdir...\n";
+  rmtree ( "$rwhoisDataPath/$netdir", { verbose => 1, error => \my $errlist } );
+  for my $diag (@$errlist) {
+    my ($file, $message) = each %$diag;
+    if ($file eq '') {
+      print "general error: $message\n";
+    }
+  }
+  $authrw = 1;	# there's probably a more efficient place to put this.  Feh.
+}
+
+# Regenerate rwhoisd.auth_area if needed
+if ($authrw) {
+  print "Regenerating auth_area\n";
+  open RWHOISDAUTH, ">$rwhoisDataPath/rwhoisd.auth_area";
+  print RWHOISDAUTH "# WARNING: This file is autogenerated!  Any static nodes should\n".
+		"# be entered in /etc/rwhoisd/rwhoisd.auth_template\n";
+  if ($template_persist) {
+    print RWHOISDAUTH shift @autharea;
+    print RWHOISDAUTH "---\n";
+  }
+  # feh.  we need to know when we're at the end of the loop, because then
+  # we DON'T want to write the separator...
+  for (;@autharea;) {	# my head hurts.
+    print RWHOISDAUTH shift @autharea;
+    print RWHOISDAUTH "---\n" if @autharea;
+  }
+  close RWHOISDAUTH;
+
+  # restart/reload rwhoisd
+  if (-e "$rwhoisDataPath/rwhoisd.pid") {	# no pidfile, no restart.
+    print "Restarting rwhoisd\n";
+    open PIDFILE, "<$rwhoisDataPath/rwhoisd.pid";
+    my ($rwpid) = (<PIDFILE> =~ /^(\d+)/);
+    close PIDFILE;
+    kill 'HUP', $rwpid;
+  }
+}
+
+# and finally
 $dbh->disconnect;
