source: trunk/cgi-bin/extras/db2rwhois.pl@ 858

Last change on this file since 858 was 858, checked in by Kris Deugau, 8 years ago

/trunk

Add an ORDER BY clause in db2rwhois.pl to make the output more predictable

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