[15] | 1 | #!/usr/bin/perl
|
---|
| 2 | # -T
|
---|
| 3 | # ipdb/cgi-bin/extras/db2rwhois.pl
|
---|
[2] | 4 | # Pull data from ipdb and mangle it into RWHOIS
|
---|
[15] | 5 | # Initial version 03/26/2004 kdeugau against IPDB v1
|
---|
| 6 | ###
|
---|
| 7 | # Revision info
|
---|
| 8 | # $Date: 2007-11-27 17:27:45 +0000 (Tue, 27 Nov 2007) $
|
---|
| 9 | # SVN revision $Rev: 371 $
|
---|
| 10 | # Last update by $Author: kdeugau $
|
---|
| 11 | ###
|
---|
[371] | 12 | # Copyright (C) 2004-2007 - Kris Deugau
|
---|
[2] | 13 |
|
---|
[15] | 14 | use strict;
|
---|
| 15 | use warnings;
|
---|
[2] | 16 | use DBI;
|
---|
| 17 | use NetAddr::IP;
|
---|
[143] | 18 | use MyIPDB;
|
---|
[371] | 19 | use File::Path 'rmtree';
|
---|
[2] | 20 |
|
---|
| 21 | $ENV{"PATH"} = "/bin;/usr/bin";
|
---|
| 22 |
|
---|
[15] | 23 | my $rwhoisDataPath = "/etc/rwhoisd";
|
---|
| 24 |
|
---|
[371] | 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 | }
|
---|
| 35 |
|
---|
[143] | 36 | my ($dbh,$msg) = connectDB_My;
|
---|
[2] | 37 |
|
---|
[143] | 38 | # For WHOIS purposes this may not be very useful. YMMV, we'll see.
|
---|
| 39 | #initIPDBGlobals($dbh);
|
---|
| 40 |
|
---|
[15] | 41 | my @masterblocks;
|
---|
[324] | 42 | my %netnameprefix;
|
---|
[328] | 43 | my %def_custids;
|
---|
[15] | 44 |
|
---|
[371] | 45 | # Get the list of live directories for potential deletion
|
---|
| 46 | opendir RWHOISROOT, $rwhoisDataPath;
|
---|
| 47 | my %rwhoisdirs;
|
---|
| 48 | foreach (readdir RWHOISROOT) {
|
---|
| 49 | $rwhoisdirs{$_} = 1 if /^net-/;
|
---|
| 50 | }
|
---|
| 51 | closedir RWHOISROOT;
|
---|
| 52 |
|
---|
| 53 | # prefetch alloctype data
|
---|
| 54 | my $sth = $dbh->prepare("select type,def_custid,arin_netname from alloctypes");
|
---|
| 55 | $sth->execute;
|
---|
| 56 | while (my @data = $sth->fetchrow_array) {
|
---|
| 57 | $netnameprefix{$data[0]} = $data[2];
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | # Get the list of masters to export
|
---|
| 61 | my $msth = $dbh->prepare("select cidr,ctime,mtime from masterblocks where rwhois='y'");
|
---|
| 62 | $msth->execute;
|
---|
| 63 |
|
---|
| 64 | # Prepare to select subblocks for each master
|
---|
| 65 | # Make sure to remove the private netblocks from this,
|
---|
| 66 | # no use or point in broadcasting our use of them.
|
---|
| 67 | # Also remove the details of our "reserved CORE/WAN" blocks; they're not critical.
|
---|
| 68 | my $ssth = $dbh->prepare("select cidr,custid,type,city,description,createstamp,modifystamp,swip ".
|
---|
| 69 | "from allocations where ".
|
---|
| 70 | "not (cidr <<= '192.168.0.0/16') and ".
|
---|
| 71 | "not (cidr <<= '172.16.0.0/12') and ".
|
---|
| 72 | "not (cidr <<= '10.0.0.0/8') and ".
|
---|
| 73 | "not (type = 'wr') and ".
|
---|
| 74 | "masklen(cidr) <=30 and ".
|
---|
| 75 | "cidr <<= ?");
|
---|
| 76 |
|
---|
| 77 | # Customer data, for those rare blocks we really need to delegate.
|
---|
| 78 | my $custsth = $dbh->prepare("select name,street,city,province,country,pocode,phone,tech_handle,special ".
|
---|
| 79 | "from customers where custid=?");
|
---|
| 80 |
|
---|
[2] | 81 | # Fill in data about our master blocks as allocated from ARIN
|
---|
| 82 | # We open separate files for each of these as appropriate.
|
---|
[371] | 83 | # Changes in master blocks are treated as complete new masters - since we're exporting
|
---|
| 84 | # all data every time, this isn't so terrible as it might seem.
|
---|
[15] | 85 | my $i=0;
|
---|
[371] | 86 | while (my @data = $msth->fetchrow_array()) {
|
---|
[2] | 87 |
|
---|
[15] | 88 | $masterblocks[$i] = new NetAddr::IP $data[0];
|
---|
[311] | 89 | my ($ctime,undef) = split /\s/, $data[1];
|
---|
[324] | 90 | my ($mtime,undef) = split /\s/, $data[2];
|
---|
[2] | 91 |
|
---|
[371] | 92 | print "$masterblocks[$i] $ctime $mtime\n";
|
---|
| 93 |
|
---|
[15] | 94 | my $date;
|
---|
[311] | 95 | chomp ($date = `/bin/date +"%Y-%m-%d"`);
|
---|
[2] | 96 |
|
---|
[371] | 97 | my $rwnet = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen;
|
---|
[2] | 98 |
|
---|
[371] | 99 | # unflag the directory for deletion. Whee! Roundabout!
|
---|
| 100 | delete $rwhoisdirs{$rwnet};
|
---|
[2] | 101 |
|
---|
[371] | 102 | # Hokay. Gonna do checks *here* to see if we need to create new master trees
|
---|
| 103 | my $netdatadir = "$rwhoisDataPath/$rwnet";
|
---|
| 104 | if (! -e $netdatadir) {
|
---|
| 105 | print " New master $masterblocks[$i]!\n";
|
---|
| 106 | print " Creating directories...\n";
|
---|
| 107 | mkdir $netdatadir;
|
---|
| 108 | mkdir "$netdatadir/attribute_defs";
|
---|
| 109 | mkdir "$netdatadir/data";
|
---|
| 110 | mkdir "$netdatadir/data/network";
|
---|
| 111 | mkdir "$netdatadir/data/org";
|
---|
| 112 | mkdir "$netdatadir/data/referral";
|
---|
| 113 |
|
---|
| 114 | my $serial;
|
---|
| 115 | chomp ($serial = `/bin/date '+%Y%m%d'000000000`);
|
---|
| 116 |
|
---|
| 117 | print " Creating SOA...\n";
|
---|
| 118 | open SOAFILE, ">$netdatadir/soa";
|
---|
| 119 | print SOAFILE qq(Serial-Number: $serial
|
---|
| 120 | Refresh-Interval: 3600
|
---|
| 121 | Increment-Interval: 1800
|
---|
| 122 | Retry-Interval: 1800
|
---|
| 123 | Time-To-Live: 86400
|
---|
| 124 | Primary-Server: rwhois.example.com:4321
|
---|
| 125 | Hostmaster: dns\@example.com
|
---|
| 126 | );
|
---|
| 127 | close SOAFILE;
|
---|
| 128 |
|
---|
| 129 | print " Creating Schema...\n";
|
---|
| 130 | open SCHEMAFILE, ">$netdatadir/schema";
|
---|
| 131 | print SCHEMAFILE qq(name: network
|
---|
| 132 | attributedef: $rwnet/attribute_defs/network.tmpl
|
---|
| 133 | dbdir: $rwnet/data/network
|
---|
| 134 | Schema-Version: $serial
|
---|
| 135 | ---
|
---|
| 136 | name: organization
|
---|
| 137 | attributedef: $rwnet/attribute_defs/org.tmpl
|
---|
| 138 | dbdir: $rwnet/data/org
|
---|
| 139 | description: Organization object
|
---|
| 140 | Schema-Version: $serial
|
---|
| 141 | ---
|
---|
| 142 | name: referral
|
---|
| 143 | attributedef:$rwnet/attribute_defs/referral.tmpl
|
---|
| 144 | dbdir:$rwnet/data/referral
|
---|
| 145 | Schema-Version: $serial
|
---|
| 146 | );
|
---|
| 147 | close SCHEMAFILE;
|
---|
| 148 |
|
---|
| 149 | print " Copying template files...\n";
|
---|
| 150 | qx { /bin/cp $rwhoisDataPath/skel/attribute_defs/* $netdatadir/attribute_defs/ };
|
---|
| 151 |
|
---|
| 152 | print " Creating org data...\n";
|
---|
| 153 | open ORGDATAFILE, ">$netdatadir/data/org/friendlyisp.txt";
|
---|
| 154 | print ORGDATAFILE qq(ID: NETBLK-ISP.$masterblocks[$i]
|
---|
| 155 | Auth-Area: $masterblocks[$i]
|
---|
| 156 | Org-Name: Friendly ISP
|
---|
| 157 | Street-Address: 123 4th Street
|
---|
| 158 | City: Anytown
|
---|
| 159 | State: ON
|
---|
| 160 | Postal-Code: H0H 0H0
|
---|
| 161 | Country-Code: CA
|
---|
| 162 | Phone: 000-555-1234
|
---|
| 163 | Created: 20040308
|
---|
| 164 | Updated: 20040308
|
---|
| 165 | );
|
---|
| 166 | close ORGDATAFILE;
|
---|
| 167 |
|
---|
| 168 | # Generate auth_area record, and add it to the array.
|
---|
| 169 | $authrw = 1; # Flag for rewrite and daemon reload/restart
|
---|
| 170 |
|
---|
| 171 | } # new master
|
---|
| 172 |
|
---|
| 173 | # do this for all masters, so that we can use this array to export the data
|
---|
| 174 | # to rwhoisd.auth_area later if we need to
|
---|
| 175 | push @autharea, qq(type:master
|
---|
| 176 | name:$masterblocks[$i]
|
---|
| 177 | data-dir: $rwnet/data
|
---|
| 178 | schema-file: $rwnet/schema
|
---|
| 179 | soa-file: $rwnet/soa
|
---|
| 180 | );
|
---|
| 181 |
|
---|
| 182 | # Recreate the net-nnn.nnn.nnn.nnn-nn.txt data file
|
---|
| 183 | my $masterfilename = "$rwnet/data/network/".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.".txt";
|
---|
| 184 |
|
---|
[15] | 185 | open MASTERFILE,">$rwhoisDataPath/$masterfilename";
|
---|
| 186 |
|
---|
[2] | 187 | print MASTERFILE "ID: NETBLK-ISP.$masterblocks[$i]\n".
|
---|
| 188 | "Auth-Area: $masterblocks[$i]\n".
|
---|
| 189 | "Network-Name: ISP-".$masterblocks[$i]->network."\n".
|
---|
| 190 | "IP-Network: $masterblocks[$i]\n".
|
---|
| 191 | "IP-Network-Block: ".$masterblocks[$i]->range."\n".
|
---|
[324] | 192 | "Org-Name: Friendly ISP\n".
|
---|
| 193 | "Street-Address: 123 4th Street\n".
|
---|
| 194 | "City: Anytown\n".
|
---|
| 195 | "StateProv: Ontario\n".
|
---|
| 196 | "Postal-Code: H0H 0H0\n".
|
---|
| 197 | "Country-Code: CA\n".
|
---|
| 198 | "Tech-Contact: ISP-ARIN-HANDLE\n".
|
---|
[218] | 199 | "Created: $ctime\n".
|
---|
[324] | 200 | "Updated: $mtime\n".
|
---|
[2] | 201 | "Updated-By: noc\@example.com\n";
|
---|
| 202 |
|
---|
[371] | 203 | # And now the subblocks
|
---|
| 204 | $ssth->execute("$masterblocks[$i]");
|
---|
| 205 | while (my ($cidr, $custid, $type, $city, $desc, $ctime, $mtime, $swip) = $ssth->fetchrow_array) {
|
---|
[2] | 206 |
|
---|
| 207 | # We get master block info from @masterblocks.
|
---|
| 208 | # ID: NETBLK-ISP.10.0.0.0/8
|
---|
| 209 | # Auth-Area: 10.0.0.0/8
|
---|
| 210 | # Network-Name: ISP-10.0.2.144
|
---|
| 211 | # IP-Network: 10.0.2.144.144/29
|
---|
| 212 | # IP-Network-Block: 10.0.2.144 - 10.0.2.151
|
---|
| 213 | # Organization: WidgetCorp
|
---|
| 214 | # Tech-Contact: bob@widgetcorp.com
|
---|
| 215 | # Admin-Contact: ISP-ARIN-HANDLE
|
---|
| 216 | # Created: 20040314
|
---|
| 217 | # Updated: 20040314
|
---|
| 218 | # Updated-By: noc@example.com
|
---|
| 219 |
|
---|
[371] | 220 | # Get the "full" network number
|
---|
| 221 | my $net = new NetAddr::IP $cidr;
|
---|
[2] | 222 |
|
---|
| 223 | # Assumptions: All data in ipdb is public
|
---|
| 224 | # If not, we need another field to indicate "public/private".
|
---|
| 225 |
|
---|
[311] | 226 | # cidr custid type city description notes maskbits
|
---|
| 227 |
|
---|
| 228 | # Fill in a generic entry for nameless allocations
|
---|
| 229 | if ($desc =~ /^\s*$/) { $desc = 'Friendly ISP'; }
|
---|
| 230 |
|
---|
[371] | 231 | # Fix up datestamps. We don't *really* need sub-microsecond resolution on our exports...
|
---|
| 232 | ($ctime) = ($ctime =~ /^(\d+-\d+-\d+)\s+/);
|
---|
| 233 | ($mtime) = ($mtime =~ /^(\d+-\d+-\d+)\s+/);
|
---|
[311] | 234 |
|
---|
[324] | 235 | # Notes:
|
---|
| 236 | # Network-name should contain some component of "description"
|
---|
| 237 | # Cust address/contact data should be included; NB, no phone for ARIN!
|
---|
| 238 | # network:ID: NET-WIDGET
|
---|
| 239 | # network:Network-Name: WIDGET [IPDB description, sort of]
|
---|
| 240 | # network:IP-Network: 10.1.1.0/24
|
---|
| 241 | # network:Org-Name: Widget Corp [Cust name; from billing?]
|
---|
| 242 | # network:Street-Address: 211 Oak Drive [May need more than one line, OR...]
|
---|
| 243 | # network:City: Pineville [...this line...]
|
---|
| 244 | # network:StateProv: WI [...and this line...]
|
---|
| 245 | # network:Postal-Code: 48888 [...and this line]
|
---|
| 246 | # network:Country-Code: US
|
---|
| 247 | # network:Tech-Contact: BZ142-MYRWHOIS [ARIN handle?]
|
---|
| 248 | # network:Updated: 19991221 [timestamp from db]
|
---|
| 249 | # network:Updated-By: jo@myrwhois.net [noc@example, since that's our POC for IP netspace issues]
|
---|
| 250 | # network:Class-Name:network [Provided by rWHOIS protocol]
|
---|
[2] | 251 |
|
---|
[371] | 252 | my $netname = $netnameprefix{$type};
|
---|
[2] | 253 |
|
---|
[371] | 254 | if ($swip eq 'n') {
|
---|
| 255 | print MASTERFILE "---\nID: NETBLK-ISP.$masterblocks[$i]\n".
|
---|
| 256 | "Auth-Area: $masterblocks[$i]\n".
|
---|
| 257 | "Network-Name: $netname-".$net->network."\n".
|
---|
| 258 | "IP-Network: $net\n".
|
---|
| 259 | "IP-Network-Block: ".$net->range."\n".
|
---|
| 260 | "Org-Name: Friendly ISP\n".
|
---|
| 261 | "Street-Address: 123 4th Street\n".
|
---|
| 262 | "City: Anytown\n".
|
---|
| 263 | "StateProv: Ontario\n".
|
---|
| 264 | "Postal-Code: H0H 0H0\n".
|
---|
| 265 | "Country-Code: CA\n".
|
---|
| 266 | "Tech-Contact: ISP-ARIN-HANDLE\n".
|
---|
| 267 | "Created: $ctime\n".
|
---|
| 268 | "Updated: $mtime\n".
|
---|
| 269 | "Updated-By: noc\@example.com\n";
|
---|
| 270 | } else {
|
---|
| 271 | $custsth->execute($custid);
|
---|
| 272 | my ($name, $street, $city, $prov, $country, $pocode, $phone, $tech, $special) = $custsth->fetchrow_array;
|
---|
| 273 | $custsth->finish;
|
---|
| 274 | if ($special && $special =~ /NetName/ && $special =~ /$cidr/) {
|
---|
| 275 | ($netname) = ($special =~ /NetName$cidr: ([A-Z0-9_-]+)/);
|
---|
[328] | 276 | } else {
|
---|
[371] | 277 | $netname .= "-".$net->network;
|
---|
| 278 | }
|
---|
| 279 | print MASTERFILE "---\nID: NETBLK-ISP.$masterblocks[$i]\n".
|
---|
| 280 | "Auth-Area: $masterblocks[$i]\n".
|
---|
| 281 | "Network-Name: $netname\n".
|
---|
| 282 | "IP-Network: $net\n".
|
---|
| 283 | "IP-Network-Block: ".$net->range."\n".
|
---|
| 284 | "Org-Name: $name\n".
|
---|
| 285 | "Street-Address: $street\n".
|
---|
| 286 | "City: $city\n".
|
---|
| 287 | "StateProv: $prov\n".
|
---|
| 288 | "Postal-Code: $pocode\n".
|
---|
| 289 | "Country-Code: $country\n".
|
---|
| 290 | "Tech-Contact: $tech\n".
|
---|
| 291 | "Created: $ctime\n".
|
---|
| 292 | "Updated: $mtime\n".
|
---|
| 293 | "Updated-By: noc\@example.com\n";
|
---|
| 294 | } # swip
|
---|
[324] | 295 |
|
---|
[371] | 296 | } # while $ssth->fetchrow_array()
|
---|
[324] | 297 |
|
---|
[371] | 298 | close MASTERFILE;
|
---|
[324] | 299 |
|
---|
[311] | 300 | $i++;
|
---|
[371] | 301 | } # while $msth->fetchrow_array()
|
---|
[2] | 302 |
|
---|
[371] | 303 | # Now we see if there's obsolete netdata directories to be deleted,
|
---|
| 304 | # and therefore an auth-area file to regenerate
|
---|
| 305 | foreach my $netdir (keys %rwhoisdirs) {
|
---|
| 306 | print "deleting obsolete directory $netdir...\n";
|
---|
| 307 | rmtree ( "$rwhoisDataPath/$netdir", { verbose => 1, error => \my $errlist } );
|
---|
| 308 | for my $diag (@$errlist) {
|
---|
| 309 | my ($file, $message) = each %$diag;
|
---|
| 310 | if ($file eq '') {
|
---|
| 311 | print "general error: $message\n";
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
| 314 | $authrw = 1; # there's probably a more efficient place to put this. Feh.
|
---|
| 315 | }
|
---|
[2] | 316 |
|
---|
[371] | 317 | # Regenerate rwhoisd.auth_area if needed
|
---|
| 318 | if ($authrw) {
|
---|
| 319 | print "Regenerating auth_area\n";
|
---|
| 320 | open RWHOISDAUTH, ">$rwhoisDataPath/rwhoisd.auth_area";
|
---|
| 321 | print RWHOISDAUTH "# WARNING: This file is autogenerated! Any static nodes should\n".
|
---|
| 322 | "# be entered in /etc/rwhoisd/rwhoisd.auth_template\n";
|
---|
| 323 | if ($template_persist) {
|
---|
| 324 | print RWHOISDAUTH shift @autharea;
|
---|
| 325 | print RWHOISDAUTH "---\n";
|
---|
| 326 | }
|
---|
| 327 | # feh. we need to know when we're at the end of the loop, because then
|
---|
| 328 | # we DON'T want to write the separator...
|
---|
| 329 | for (;@autharea;) { # my head hurts.
|
---|
| 330 | print RWHOISDAUTH shift @autharea;
|
---|
| 331 | print RWHOISDAUTH "---\n" if @autharea;
|
---|
| 332 | }
|
---|
| 333 | close RWHOISDAUTH;
|
---|
| 334 |
|
---|
| 335 | # restart/reload rwhoisd
|
---|
| 336 | if (-e "$rwhoisDataPath/rwhoisd.pid") { # no pidfile, no restart.
|
---|
| 337 | print "Restarting rwhoisd\n";
|
---|
| 338 | open PIDFILE, "<$rwhoisDataPath/rwhoisd.pid";
|
---|
| 339 | my ($rwpid) = (<PIDFILE> =~ /^(\d+)/);
|
---|
| 340 | close PIDFILE;
|
---|
| 341 | kill 'HUP', $rwpid;
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | # and finally
|
---|
[2] | 346 | $dbh->disconnect;
|
---|