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