source: branches/stable/cgi-bin/extras/db2rwhois.pl@ 355

Last change on this file since 355 was 326, checked in by Kris Deugau, 19 years ago

/branches/stable

Just slurp up all alloctypes instead of restricting types for
rWHOIS export.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 7.2 KB
RevLine 
[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: 2006-04-06 20:27:26 +0000 (Thu, 06 Apr 2006) $
9# SVN revision $Rev: 326 $
10# Last update by $Author: kdeugau $
11###
[158]12# Copyright (C) 2004,2005 - Kris Deugau
[2]13
[15]14use strict;
15use warnings;
[2]16use DBI;
17use NetAddr::IP;
[158]18use MyIPDB;
[2]19
20$ENV{"PATH"} = "/bin;/usr/bin";
21
[15]22my $rwhoisDataPath = "/etc/rwhoisd";
23
[158]24my ($dbh,$msg) = connectDB_My;
[2]25
[158]26# For WHOIS purposes this may not be very useful. YMMV, we'll see.
27#initIPDBGlobals($dbh);
28
[15]29my @masterblocks;
[325]30my %netnameprefix;
[15]31
[2]32# Fill in data about our master blocks as allocated from ARIN
33# 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.
[325]36my $sth = $dbh->prepare("select cidr,ctime,mtime from masterblocks;");
[2]37$sth->execute;
[15]38my $i=0;
39GETMASTERS: while (my @data = $sth->fetchrow_array()) {
[2]40
[253]41# Techically, we only need to exclude 204.138.172.0/24, as we "own" all of the other blocks.
[308]42# However, 205.207.184.0/23 and 206.130.64.0/24 are, um, awkward.
[253]43 if ($data[0] =~ /^(192.168.0.0|172.16.0.0|10.0.0.0|20[456])/) {
[15]44 next GETMASTERS;
45 }
46 $masterblocks[$i] = new NetAddr::IP $data[0];
[253]47 my ($ctime,undef) = split /\s/, $data[1];
[325]48 my ($mtime,undef) = split /\s/, $data[2];
49print "$masterblocks[$i] $ctime $mtime\n";
[2]50
[15]51 my $date;
[253]52 chomp ($date = `/bin/date +"%Y-%m-%d"`);
[2]53
54# Whew! Ugly little varmint.
[15]55 my $masterfilename = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.
[2]56 "/data/network/".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.".txt";
57
[15]58# Need check here to create tree for netblock?
[2]59
[15]60 open MASTERFILE,">$rwhoisDataPath/$masterfilename";
61
[2]62 print MASTERFILE "ID: NETBLK-ISP.$masterblocks[$i]\n".
63 "Auth-Area: $masterblocks[$i]\n".
64 "Network-Name: ISP-".$masterblocks[$i]->network."\n".
65 "IP-Network: $masterblocks[$i]\n".
66 "IP-Network-Block: ".$masterblocks[$i]->range."\n".
[325]67 "Org-Name: Friendly ISP\n".
68 "Street-Address: 123 4th Street\n".
69 "City: Anytown\n".
70 "StateProv: Ontario\n".
71 "Postal-Code: H0H 0H0\n".
72 "Country-Code: CA\n".
73 "Tech-Contact: ISP-ARIN-HANDLE\n".
[253]74 "Created: $ctime\n".
[325]75 "Updated: $mtime\n".
[2]76 "Updated-By: noc\@example.com\n";
77
78 close MASTERFILE;
79 $i++;
80}
81
[325]82# prefetch alloctype data
[326]83$sth = $dbh->prepare("select type,def_custid,arin_netname from alloctypes");
[325]84$sth->execute;
85while (my @data = $sth->fetchrow_array) {
86 $netnameprefix{$data[0]} = $data[2];
87}
88
[2]89# Now read out the data in the "main" delegation list, and check it
90# with the master blocks. We need to do this to decide which rWHOIS
91# "net-xxx.xxx.xxx.xxx-mask" tree the data belongs in.
92
[15]93# Make sure to remove the private netblocks from this.
94# No use or point in broadcasting our use of them.
[308]95# Also remove the details of our "reserved CORE/WAN" blocks; they're not critical.
[325]96$sth = $dbh->prepare("select cidr,custid,type,city,description,createstamp,modifystamp,swip ".
[253]97 "from allocations where ".
98 "not (cidr <<= '192.168.0.0/16') and ".
99 "not (cidr <<= '172.16.0.0/12') and ".
100 "not (cidr <<= '10.0.0.0/8') and ".
[308]101 "not (type = 'wr') and ".
[253]102 "masklen(cidr) <=30");
103#" and (custid='6750400' or custid like '%-RES' or custid like '%-BUS')");
[2]104$sth->execute;
105
[325]106my $custsth = $dbh->prepare("select name,street,city,province,country,pocode,phone,tech_handle,special from customers where custid=?");
107
[253]108$i=0;
[325]109while (my ($cidr, $custid, $type, $city, $desc, $ctime, $mtime, $swip) = $sth->fetchrow_array) {
[2]110
111# We get master block info from @masterblocks.
112 # ID: NETBLK-ISP.10.0.0.0/8
113 # Auth-Area: 10.0.0.0/8
114 # Network-Name: ISP-10.0.2.144
115 # IP-Network: 10.0.2.144.144/29
116 # IP-Network-Block: 10.0.2.144 - 10.0.2.151
117 # Organization: WidgetCorp
118 # Tech-Contact: bob@widgetcorp.com
119 # Admin-Contact: ISP-ARIN-HANDLE
120 # Created: 20040314
121 # Updated: 20040314
122 # Updated-By: noc@example.com
123
124 # Get the "full" network number
[253]125 my $net = new NetAddr::IP $cidr;
[2]126
127# Assumptions: All data in ipdb is public
128# If not, we need another field to indicate "public/private".
129
[15]130 foreach my $master (@masterblocks) {
[2]131 if ($master->contains($net)) {
132
133# Whew! Ugly little varmint.
[15]134 my $masterfilename = "net-".$master->addr."-".$master->masklen.
[2]135 "/data/network/".$master->addr."-".$master->masklen.".txt";
136
[15]137 open MASTERFILE,">>$rwhoisDataPath/$masterfilename"
138 or print "File open error: '$!' on '$rwhoisDataPath/$masterfilename'\n";
[2]139
[15]140# cidr custid type city description notes maskbits
141
[308]142# Fill in a generic entry for nameless allocations
[253]143if ($desc =~ /^\s*$/) { $desc = 'Friendly ISP'; }
144
[308]145 # Fix up datestamps. We don't *really* need sub-microsecond resolution on our exports...
146 ($ctime) = ($ctime =~ /^(\d+-\d+-\d+)\s+/);
147 ($mtime) = ($mtime =~ /^(\d+-\d+-\d+)\s+/);
148
[325]149# Notes:
150# Network-name should contain some component of "description"
151# Cust address/contact data should be included; NB, no phone for ARIN!
152# network:ID: NET-WIDGET
153# network:Network-Name: WIDGET [IPDB description, sort of]
154# network:IP-Network: 10.1.1.0/24
155# network:Org-Name: Widget Corp [Cust name; from billing?]
156# network:Street-Address: 211 Oak Drive [May need more than one line, OR...]
157# network:City: Pineville [...this line...]
158# network:StateProv: WI [...and this line...]
159# network:Postal-Code: 48888 [...and this line]
160# network:Country-Code: US
161# network:Tech-Contact: BZ142-MYRWHOIS [ARIN handle?]
162# network:Updated: 19991221 [timestamp from db]
163# network:Updated-By: jo@myrwhois.net [noc@example, since that's our POC for IP netspace issues]
164# network:Class-Name:network [Provided by rWHOIS protocol]
[2]165
[325]166 my $netname = $netnameprefix{$type};
[2]167
[325]168 if ($swip eq 'n') {
169 print MASTERFILE "---\nID: NETBLK-ISP.$master\n".
170 "Auth-Area: $master\n".
171 "Network-Name: $netname-".$net->network."\n".
172 "IP-Network: $net\n".
173 "IP-Network-Block: ".$net->range."\n".
174 "Org-Name: Friendly ISP\n".
175 "Street-Address: 123 4th Street\n".
176 "City: Anytown\n".
177 "StateProv: Ontario\n".
178 "Postal-Code: H0H 0H0\n".
179 "Country-Code: CA\n".
180 "Tech-Contact: ISP-ARIN-HANDLE\n".
181 "Created: $ctime\n".
182 "Updated: $mtime\n".
183 "Updated-By: noc\@example.com\n";
184 } else {
185 $custsth->execute($custid);
186 my ($name, $street, $city, $prov, $country, $pocode, $phone, $tech, $special) = $custsth->fetchrow_array;
187 $custsth->finish;
188 if ($special && $special =~ /NetName/ && $special =~ /$cidr/) {
189 ($netname) = ($special =~ /NetName$cidr: ([A-Z0-9_-]+)/);
190 } else {
191 $netname .= "-".$net->network;
192 }
193 print MASTERFILE "---\nID: NETBLK-ISP.$master\n".
194 "Auth-Area: $master\n".
195 "Network-Name: $netname\n".
196 "IP-Network: $net\n".
197 "IP-Network-Block: ".$net->range."\n".
198 "Org-Name: $name\n".
199 "Street-Address: $street\n".
200 "City: $city\n".
201 "StateProv: $prov\n".
202 "Postal-Code: $pocode\n".
203 "Country-Code: $country\n".
204 "Tech-Contact: $tech\n".
205 "Created: $ctime\n".
206 "Updated: $mtime\n".
207 "Updated-By: noc\@example.com\n";
208 } # swip
[2]209
[325]210 } # net in master
211 } # foreach master
212
213
214
[2]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";
[253]219 $i++;
[2]220} # while fetchrow_array()
221
222
223$dbh->disconnect;
Note: See TracBrowser for help on using the repository browser.