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