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: 2006-04-06 19:47:21 +0000 (Thu, 06 Apr 2006) $
|
---|
9 | # SVN revision $Rev: 324 $
|
---|
10 | # Last update by $Author: kdeugau $
|
---|
11 | ###
|
---|
12 | # Copyright (C) 2004,2005 - Kris Deugau
|
---|
13 |
|
---|
14 | use strict;
|
---|
15 | use warnings;
|
---|
16 | use DBI;
|
---|
17 | use NetAddr::IP;
|
---|
18 | use MyIPDB;
|
---|
19 |
|
---|
20 | $ENV{"PATH"} = "/bin;/usr/bin";
|
---|
21 |
|
---|
22 | my $rwhoisDataPath = "/etc/rwhoisd";
|
---|
23 |
|
---|
24 | my ($dbh,$msg) = connectDB_My;
|
---|
25 |
|
---|
26 | # For WHOIS purposes this may not be very useful. YMMV, we'll see.
|
---|
27 | #initIPDBGlobals($dbh);
|
---|
28 |
|
---|
29 | my @masterblocks;
|
---|
30 | my %netnameprefix;
|
---|
31 |
|
---|
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.
|
---|
36 | my $sth = $dbh->prepare("select cidr,ctime,mtime from masterblocks;");
|
---|
37 | $sth->execute;
|
---|
38 | my $i=0;
|
---|
39 | GETMASTERS: while (my @data = $sth->fetchrow_array()) {
|
---|
40 |
|
---|
41 | # Techically, we only need to exclude 204.138.172.0/24, as we "own" all of the other blocks.
|
---|
42 | # However, 205.207.184.0/23 and 206.130.64.0/24 are, um, awkward.
|
---|
43 | if ($data[0] =~ /^(192.168.0.0|172.16.0.0|10.0.0.0|20[456])/) {
|
---|
44 | next GETMASTERS;
|
---|
45 | }
|
---|
46 | $masterblocks[$i] = new NetAddr::IP $data[0];
|
---|
47 | my ($ctime,undef) = split /\s/, $data[1];
|
---|
48 | my ($mtime,undef) = split /\s/, $data[2];
|
---|
49 | print "$masterblocks[$i] $ctime $mtime\n";
|
---|
50 |
|
---|
51 | my $date;
|
---|
52 | chomp ($date = `/bin/date +"%Y-%m-%d"`);
|
---|
53 |
|
---|
54 | # Whew! Ugly little varmint.
|
---|
55 | my $masterfilename = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.
|
---|
56 | "/data/network/".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.".txt";
|
---|
57 |
|
---|
58 | # Need check here to create tree for netblock?
|
---|
59 |
|
---|
60 | open MASTERFILE,">$rwhoisDataPath/$masterfilename";
|
---|
61 |
|
---|
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".
|
---|
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".
|
---|
74 | "Created: $ctime\n".
|
---|
75 | "Updated: $mtime\n".
|
---|
76 | "Updated-By: noc\@example.com\n";
|
---|
77 |
|
---|
78 | close MASTERFILE;
|
---|
79 | $i++;
|
---|
80 | }
|
---|
81 |
|
---|
82 | # prefetch alloctype data
|
---|
83 | $sth = $dbh->prepare("select type,def_custid,arin_netname from alloctypes where listorder <500");
|
---|
84 | $sth->execute;
|
---|
85 | while (my @data = $sth->fetchrow_array) {
|
---|
86 | $netnameprefix{$data[0]} = $data[2];
|
---|
87 | }
|
---|
88 |
|
---|
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 |
|
---|
93 | # Make sure to remove the private netblocks from this.
|
---|
94 | # No use or point in broadcasting our use of them.
|
---|
95 | # Also remove the details of our "reserved CORE/WAN" blocks; they're not critical.
|
---|
96 | $sth = $dbh->prepare("select cidr,custid,type,city,description,createstamp,modifystamp,swip ".
|
---|
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 ".
|
---|
101 | "not (type = 'wr') and ".
|
---|
102 | "masklen(cidr) <=30");
|
---|
103 | #" and (custid='6750400' or custid like '%-RES' or custid like '%-BUS')");
|
---|
104 | $sth->execute;
|
---|
105 |
|
---|
106 | my $custsth = $dbh->prepare("select name,street,city,province,country,pocode,phone,tech_handle,special from customers where custid=?");
|
---|
107 |
|
---|
108 | $i=0;
|
---|
109 | while (my ($cidr, $custid, $type, $city, $desc, $ctime, $mtime, $swip) = $sth->fetchrow_array) {
|
---|
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
|
---|
125 | my $net = new NetAddr::IP $cidr;
|
---|
126 |
|
---|
127 | # Assumptions: All data in ipdb is public
|
---|
128 | # If not, we need another field to indicate "public/private".
|
---|
129 |
|
---|
130 | foreach my $master (@masterblocks) {
|
---|
131 | if ($master->contains($net)) {
|
---|
132 |
|
---|
133 | # Whew! Ugly little varmint.
|
---|
134 | my $masterfilename = "net-".$master->addr."-".$master->masklen.
|
---|
135 | "/data/network/".$master->addr."-".$master->masklen.".txt";
|
---|
136 |
|
---|
137 | open MASTERFILE,">>$rwhoisDataPath/$masterfilename"
|
---|
138 | or print "File open error: '$!' on '$rwhoisDataPath/$masterfilename'\n";
|
---|
139 |
|
---|
140 | # cidr custid type city description notes maskbits
|
---|
141 |
|
---|
142 | # Fill in a generic entry for nameless allocations
|
---|
143 | if ($desc =~ /^\s*$/) { $desc = 'Friendly ISP'; }
|
---|
144 |
|
---|
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 |
|
---|
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]
|
---|
165 |
|
---|
166 | my $netname = $netnameprefix{$type};
|
---|
167 |
|
---|
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
|
---|
209 |
|
---|
210 | } # net in master
|
---|
211 | } # foreach master
|
---|
212 |
|
---|
213 |
|
---|
214 |
|
---|
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";
|
---|
219 | $i++;
|
---|
220 | } # while fetchrow_array()
|
---|
221 |
|
---|
222 |
|
---|
223 | $dbh->disconnect;
|
---|