Changeset 367 for branches/stable


Ignore:
Timestamp:
10/09/07 17:43:44 (17 years ago)
Author:
Kris Deugau
Message:

/branches/stable

Major update to db2rwhois.pl so that it can

a) take advantage of updates to the masterblocks table to determine

which masters get exported to rwhois

b) create and delete rwhois directory trees as needed for

new/changed/removed master blocks

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/cgi-bin/extras/db2rwhois.pl

    r326 r367  
    1010# Last update by $Author$
    1111###
    12 # Copyright (C) 2004,2005 - Kris Deugau
     12# Copyright (C) 2004-2007 - Kris Deugau
    1313
    1414use strict;
     
    1717use NetAddr::IP;
    1818use MyIPDB;
     19use File::Path 'rmtree';
    1920
    2021$ENV{"PATH"} = "/bin;/usr/bin";
    2122
    2223my $rwhoisDataPath = "/etc/rwhoisd";
     24
     25my @autharea;
     26my $authrw;
     27# Use the template file to allow us to keep persistent nodes aside from netblock data
     28open AUTHTEMPLATE, "<$rwhoisDataPath/rwhoisd.auth_template";
     29my $template_persist;
     30while (<AUTHTEMPLATE>) {
     31  next if /^##/;
     32  $template_persist = 1 if /^[a-z]/i;
     33  $autharea[0] .= $_;
     34}
    2335
    2436my ($dbh,$msg) = connectDB_My;
     
    3042my %netnameprefix;
    3143
     44# Get the list of live directories for potential deletion
     45opendir RWHOISROOT, $rwhoisDataPath;
     46my %rwhoisdirs;
     47foreach (readdir RWHOISROOT) {
     48  $rwhoisdirs{$_} = 1 if /^net-/;
     49}
     50closedir RWHOISROOT;
     51
    3252# Fill in data about our master blocks as allocated from ARIN
    3353# We open separate files for each of these as appropriate.
    34 # Note that this ASS-U-MEs that we do not add master IP blocks-
    35 # there should probably be a separate system for doing that.
    36 my $sth = $dbh->prepare("select cidr,ctime,mtime from masterblocks;");
     54# Changes in master blocks are treated as complete new masters - since we're exporting
     55# all data every time, this isn't so terrible as it might seem.
     56my $sth = $dbh->prepare("select cidr,ctime,mtime from masterblocks where rwhois='y'");
    3757$sth->execute;
    3858my $i=0;
    3959GETMASTERS: while (my @data = $sth->fetchrow_array()) {
    4060
    41 # Techically, we only need to exclude 204.138.172.0/24, as we "own" all of the other blocks.
    42 # However, 205.207.184.0/23 and 206.130.64.0/24 are, um, awkward.
    43   if ($data[0] =~ /^(192.168.0.0|172.16.0.0|10.0.0.0|20[456])/) {
    44     next GETMASTERS;
    45   }
    4661  $masterblocks[$i] = new NetAddr::IP $data[0];
    4762  my ($ctime,undef) = split /\s/, $data[1];
    4863  my ($mtime,undef) = split /\s/, $data[2];
    49 print "$masterblocks[$i] $ctime $mtime\n";
     64
     65  print "$masterblocks[$i] $ctime $mtime\n";
    5066
    5167  my $date;
    5268  chomp ($date = `/bin/date +"%Y-%m-%d"`);
    5369
     70  my $rwnet = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen;
     71
     72  # unflag the directory for deletion.  Whee!  Roundabout!
     73  delete $rwhoisdirs{$rwnet};
     74
     75# Hokay.  Gonna do checks *here* to see if we need to create new master trees
     76  my $netdatadir = "$rwhoisDataPath/$rwnet";
     77  if (! -e $netdatadir) {
     78    print " New master $masterblocks[$i]!\n";
     79    print "  Creating directories...\n";
     80    mkdir $netdatadir;
     81    mkdir "$netdatadir/attribute_defs";
     82    mkdir "$netdatadir/data";
     83    mkdir "$netdatadir/data/network";
     84    mkdir "$netdatadir/data/org";
     85    mkdir "$netdatadir/data/referral";
     86
     87    my $serial;
     88    chomp ($serial = `/bin/date '+%Y%m%d'000000000`);
     89
     90    print "  Creating SOA...\n";
     91    open SOAFILE, ">$netdatadir/soa";
     92    print SOAFILE qq(Serial-Number: $serial
     93Refresh-Interval: 3600
     94Increment-Interval: 1800
     95Retry-Interval: 1800
     96Time-To-Live: 86400
     97Primary-Server: rwhois.example.com:4321
     98Hostmaster: dns\@example.com
     99);
     100    close SOAFILE;
     101
     102    print "  Creating Schema...\n";
     103    open SCHEMAFILE, ">$netdatadir/schema";
     104    print SCHEMAFILE qq(name: network
     105attributedef: $rwnet/attribute_defs/network.tmpl
     106dbdir: $rwnet/data/network
     107Schema-Version: $serial
     108---
     109name: organization
     110attributedef: $rwnet/attribute_defs/org.tmpl
     111dbdir: $rwnet/data/org
     112description: Organization object
     113Schema-Version: $serial
     114---
     115name: referral
     116attributedef:$rwnet/attribute_defs/referral.tmpl
     117dbdir:$rwnet/data/referral
     118Schema-Version: $serial
     119);
     120    close SCHEMAFILE;
     121
     122    print "  Copying template files...\n";
     123    qx { /bin/cp $rwhoisDataPath/skel/attribute_defs/* $netdatadir/attribute_defs/ };
     124
     125    print "  Creating org data...\n";
     126    open ORGDATAFILE, ">$netdatadir/data/org/friendlyisp.txt";
     127    print ORGDATAFILE qq(ID: NETBLK-ISP.$masterblocks[$i]
     128Auth-Area: $masterblocks[$i]
     129Org-Name: Friendly ISP
     130Street-Address: 123 4th Street
     131City: Anytown
     132State: ON
     133Postal-Code: H0H 0H0
     134Country-Code: CA
     135Phone: 000-555-1234
     136Created: 20040308
     137Updated: 20040308
     138);
     139    close ORGDATAFILE;
     140
     141    # Generate auth_area record, and add it to the array.
     142    $authrw = 1;        # Flag for rewrite and daemon reload/restart
     143
     144  } # new master
     145
     146  # do this for all masters, so that we can use this array to export the data
     147  # to rwhoisd.auth_area later if we need to
     148  push @autharea, qq(type:master
     149name:$masterblocks[$i]
     150data-dir: $rwnet/data
     151schema-file: $rwnet/schema
     152soa-file: $rwnet/soa
     153);
     154
    54155# Whew!  Ugly little varmint.
    55   my $masterfilename = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.
    56     "/data/network/".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.".txt";
    57 
    58 # Need check here to create tree for netblock?
     156  my $masterfilename = "$rwnet/data/network/$rwnet.txt";
    59157
    60158  open MASTERFILE,">$rwhoisDataPath/$masterfilename";
     
    211309  } # foreach master
    212310
    213 
    214 
    215   #  print "$data[0]\t| $data[1]\t| $data[2]\t| $data[3]\t| $data[4]\t| ".
    216   #     "$data[5]\t| $data[6]\t| $data[7]\t| $data[8]\t| $data[9]\n";
    217   #  print "$data[0]\t| $data[1]\t| $data[2]\t| $data[3]\t| $data[4]\t| ".
    218   #     "$data[5]\t| $data[6]\t| $data[7]\t| $data[8]\n";
    219311  $i++;
    220312} # while fetchrow_array()
    221313
    222 
     314foreach my $netdir (keys %rwhoisdirs) {
     315  print "deleting obsolete directory $netdir...\n";
     316  rmtree ( "$rwhoisDataPath/$netdir", { verbose => 1, error => \my $errlist } );
     317  for my $diag (@$errlist) {
     318    my ($file, $message) = each %$diag;
     319    if ($file eq '') {
     320      print "general error: $message\n";
     321    }
     322  }
     323  $authrw = 1;  # there's probably a more efficient place to put this.  Feh.
     324}
     325
     326# Regenerate rwhoisd.auth_area if needed
     327if ($authrw) {
     328  print "Regenerating auth_area\n";
     329  open RWHOISDAUTH, ">$rwhoisDataPath/rwhoisd.auth_area";
     330  print RWHOISDAUTH "# WARNING: This file is autogenerated!  Any static nodes should\n".
     331                "# be entered in /etc/rwhoisd/rwhoisd.auth_template\n";
     332  if ($template_persist) {
     333    print RWHOISDAUTH shift @autharea;
     334    print RWHOISDAUTH "---\n";
     335  }
     336  # feh.  we need to know when we're at the end of the loop, because then
     337  # we DON'T want to write the separator...
     338  for (;@autharea;) {   # my head hurts.
     339    print RWHOISDAUTH shift @autharea;
     340    print RWHOISDAUTH "---\n" if @autharea;
     341  }
     342  close RWHOISDAUTH;
     343
     344  # restart/reload rwhoisd
     345  if (-e "$rwhoisDataPath/rwhoisd.pid") {       # no pidfile, no restart.
     346    print "Restarting rwhoisd\n";
     347    open PIDFILE, "<$rwhoisDataPath/rwhoisd.pid";
     348    my ($rwpid) = (<PIDFILE> =~ /^(\d+)/);
     349    close PIDFILE;
     350    kill 'HUP', $rwpid;
     351  }
     352}
     353
     354# and finally
    223355$dbh->disconnect;
Note: See TracChangeset for help on using the changeset viewer.