Changeset 367
- Timestamp:
- 10/09/07 17:43:44 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/cgi-bin/extras/db2rwhois.pl
r326 r367 10 10 # Last update by $Author$ 11 11 ### 12 # Copyright (C) 2004 ,2005- Kris Deugau12 # Copyright (C) 2004-2007 - Kris Deugau 13 13 14 14 use strict; … … 17 17 use NetAddr::IP; 18 18 use MyIPDB; 19 use File::Path 'rmtree'; 19 20 20 21 $ENV{"PATH"} = "/bin;/usr/bin"; 21 22 22 23 my $rwhoisDataPath = "/etc/rwhoisd"; 24 25 my @autharea; 26 my $authrw; 27 # Use the template file to allow us to keep persistent nodes aside from netblock data 28 open AUTHTEMPLATE, "<$rwhoisDataPath/rwhoisd.auth_template"; 29 my $template_persist; 30 while (<AUTHTEMPLATE>) { 31 next if /^##/; 32 $template_persist = 1 if /^[a-z]/i; 33 $autharea[0] .= $_; 34 } 23 35 24 36 my ($dbh,$msg) = connectDB_My; … … 30 42 my %netnameprefix; 31 43 44 # Get the list of live directories for potential deletion 45 opendir RWHOISROOT, $rwhoisDataPath; 46 my %rwhoisdirs; 47 foreach (readdir RWHOISROOT) { 48 $rwhoisdirs{$_} = 1 if /^net-/; 49 } 50 closedir RWHOISROOT; 51 32 52 # Fill in data about our master blocks as allocated from ARIN 33 53 # 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. 56 my $sth = $dbh->prepare("select cidr,ctime,mtime from masterblocks where rwhois='y'"); 37 57 $sth->execute; 38 58 my $i=0; 39 59 GETMASTERS: while (my @data = $sth->fetchrow_array()) { 40 60 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 }46 61 $masterblocks[$i] = new NetAddr::IP $data[0]; 47 62 my ($ctime,undef) = split /\s/, $data[1]; 48 63 my ($mtime,undef) = split /\s/, $data[2]; 49 print "$masterblocks[$i] $ctime $mtime\n"; 64 65 print "$masterblocks[$i] $ctime $mtime\n"; 50 66 51 67 my $date; 52 68 chomp ($date = `/bin/date +"%Y-%m-%d"`); 53 69 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 93 Refresh-Interval: 3600 94 Increment-Interval: 1800 95 Retry-Interval: 1800 96 Time-To-Live: 86400 97 Primary-Server: rwhois.example.com:4321 98 Hostmaster: dns\@example.com 99 ); 100 close SOAFILE; 101 102 print " Creating Schema...\n"; 103 open SCHEMAFILE, ">$netdatadir/schema"; 104 print SCHEMAFILE qq(name: network 105 attributedef: $rwnet/attribute_defs/network.tmpl 106 dbdir: $rwnet/data/network 107 Schema-Version: $serial 108 --- 109 name: organization 110 attributedef: $rwnet/attribute_defs/org.tmpl 111 dbdir: $rwnet/data/org 112 description: Organization object 113 Schema-Version: $serial 114 --- 115 name: referral 116 attributedef:$rwnet/attribute_defs/referral.tmpl 117 dbdir:$rwnet/data/referral 118 Schema-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] 128 Auth-Area: $masterblocks[$i] 129 Org-Name: Friendly ISP 130 Street-Address: 123 4th Street 131 City: Anytown 132 State: ON 133 Postal-Code: H0H 0H0 134 Country-Code: CA 135 Phone: 000-555-1234 136 Created: 20040308 137 Updated: 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 149 name:$masterblocks[$i] 150 data-dir: $rwnet/data 151 schema-file: $rwnet/schema 152 soa-file: $rwnet/soa 153 ); 154 54 155 # 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"; 59 157 60 158 open MASTERFILE,">$rwhoisDataPath/$masterfilename"; … … 211 309 } # foreach master 212 310 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";219 311 $i++; 220 312 } # while fetchrow_array() 221 313 222 314 foreach 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 327 if ($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 223 355 $dbh->disconnect;
Note:
See TracChangeset
for help on using the changeset viewer.