[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: 2005-05-30 20:09:05 +0000 (Mon, 30 May 2005) $
|
---|
| 9 | # SVN revision $Rev: 253 $
|
---|
| 10 | # Last update by $Author: kdeugau $
|
---|
| 11 | ###
|
---|
[158] | 12 | # Copyright (C) 2004,2005 - Kris Deugau
|
---|
[2] | 13 |
|
---|
[15] | 14 | use strict;
|
---|
| 15 | use warnings;
|
---|
[2] | 16 | use DBI;
|
---|
| 17 | use NetAddr::IP;
|
---|
[158] | 18 | use MyIPDB;
|
---|
[2] | 19 |
|
---|
| 20 | $ENV{"PATH"} = "/bin;/usr/bin";
|
---|
| 21 |
|
---|
[15] | 22 | my $rwhoisDataPath = "/etc/rwhoisd";
|
---|
| 23 |
|
---|
[2] | 24 | # We'll pull this out of the database instead. Just make sure our
|
---|
| 25 | # master blocks are actually listed.... <g>
|
---|
| 26 | #@masterblocks = (
|
---|
[15] | 27 | # (new NetAddr::IP "209.91.128.0/18"),
|
---|
| 28 | # (new NetAddr::IP "66.186.64.0/19"),
|
---|
| 29 | # (new NetAddr::IP "192.168.99.0/24")
|
---|
[2] | 30 | # );
|
---|
| 31 |
|
---|
[158] | 32 | my ($dbh,$msg) = connectDB_My;
|
---|
[2] | 33 |
|
---|
[158] | 34 | # For WHOIS purposes this may not be very useful. YMMV, we'll see.
|
---|
| 35 | #initIPDBGlobals($dbh);
|
---|
| 36 |
|
---|
[15] | 37 | my @masterblocks;
|
---|
| 38 |
|
---|
[2] | 39 | # Fill in data about our master blocks as allocated from ARIN
|
---|
| 40 | # We open separate files for each of these as appropriate.
|
---|
| 41 | # Note that this ASS-U-MEs that we do not add master IP blocks-
|
---|
| 42 | # there should probably be a separate system for doing that.
|
---|
[253] | 43 | my $sth = $dbh->prepare("select cidr,ctime from masterblocks;");
|
---|
[2] | 44 | $sth->execute;
|
---|
[15] | 45 | my $i=0;
|
---|
| 46 | GETMASTERS: while (my @data = $sth->fetchrow_array()) {
|
---|
[2] | 47 |
|
---|
[253] | 48 | # Techically, we only need to exclude 204.138.172.0/24, as we "own" all of the other blocks.
|
---|
| 49 | if ($data[0] =~ /^(192.168.0.0|172.16.0.0|10.0.0.0|20[456])/) {
|
---|
[15] | 50 | next GETMASTERS;
|
---|
| 51 | }
|
---|
| 52 | $masterblocks[$i] = new NetAddr::IP $data[0];
|
---|
[253] | 53 | my ($ctime,undef) = split /\s/, $data[1];
|
---|
[2] | 54 |
|
---|
[253] | 55 | print "$masterblocks[$i] $data[1]\n";
|
---|
[15] | 56 |
|
---|
| 57 | my $date;
|
---|
[253] | 58 | chomp ($date = `/bin/date +"%Y-%m-%d"`);
|
---|
[2] | 59 |
|
---|
| 60 | # Whew! Ugly little varmint.
|
---|
[15] | 61 | my $masterfilename = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.
|
---|
[2] | 62 | "/data/network/".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.".txt";
|
---|
| 63 |
|
---|
[15] | 64 | # Need check here to create tree for netblock?
|
---|
[2] | 65 |
|
---|
[15] | 66 | open MASTERFILE,">$rwhoisDataPath/$masterfilename";
|
---|
| 67 |
|
---|
[2] | 68 | print MASTERFILE "ID: NETBLK-ISP.$masterblocks[$i]\n".
|
---|
| 69 | "Auth-Area: $masterblocks[$i]\n".
|
---|
| 70 | "Network-Name: ISP-".$masterblocks[$i]->network."\n".
|
---|
| 71 | "IP-Network: $masterblocks[$i]\n".
|
---|
| 72 | "IP-Network-Block: ".$masterblocks[$i]->range."\n".
|
---|
[15] | 73 | "Organization: Friendly ISP\n".
|
---|
| 74 | "Tech-Contact: noc\@example.com\n".
|
---|
[2] | 75 | "Admin-Contact: ISP-ARIN-HANDLE\n".
|
---|
[15] | 76 | "Abuse-Contact: abuse\@example.com\n".
|
---|
[253] | 77 | "Created: $ctime\n".
|
---|
[2] | 78 | "Updated: $date\n".
|
---|
| 79 | "Updated-By: noc\@example.com\n";
|
---|
| 80 |
|
---|
| 81 | close MASTERFILE;
|
---|
| 82 | $i++;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | # Now read out the data in the "main" delegation list, and check it
|
---|
| 86 | # with the master blocks. We need to do this to decide which rWHOIS
|
---|
| 87 | # "net-xxx.xxx.xxx.xxx-mask" tree the data belongs in.
|
---|
| 88 |
|
---|
[15] | 89 | # Make sure to remove the private netblocks from this.
|
---|
| 90 | # No use or point in broadcasting our use of them.
|
---|
[253] | 91 | $sth = $dbh->prepare("select cidr,custid,type,city,description,createstamp,modifystamp ".
|
---|
| 92 | "from allocations where ".
|
---|
| 93 | "not (cidr <<= '192.168.0.0/16') and ".
|
---|
| 94 | "not (cidr <<= '172.16.0.0/12') and ".
|
---|
| 95 | "not (cidr <<= '10.0.0.0/8') and ".
|
---|
| 96 | "masklen(cidr) <=30");
|
---|
| 97 | #" and (custid='6750400' or custid like '%-RES' or custid like '%-BUS')");
|
---|
[2] | 98 | $sth->execute;
|
---|
| 99 |
|
---|
[253] | 100 | $i=0;
|
---|
| 101 | while (my ($cidr, $custid, $type, $city, $desc, $ctime, $mtime) = $sth->fetchrow_array) {
|
---|
[2] | 102 |
|
---|
| 103 | # We get master block info from @masterblocks.
|
---|
| 104 | # ID: NETBLK-ISP.10.0.0.0/8
|
---|
| 105 | # Auth-Area: 10.0.0.0/8
|
---|
| 106 | # Network-Name: ISP-10.0.2.144
|
---|
| 107 | # IP-Network: 10.0.2.144.144/29
|
---|
| 108 | # IP-Network-Block: 10.0.2.144 - 10.0.2.151
|
---|
| 109 | # Organization: WidgetCorp
|
---|
| 110 | # Tech-Contact: bob@widgetcorp.com
|
---|
| 111 | # Admin-Contact: ISP-ARIN-HANDLE
|
---|
| 112 | # Created: 20040314
|
---|
| 113 | # Updated: 20040314
|
---|
| 114 | # Updated-By: noc@example.com
|
---|
| 115 |
|
---|
| 116 | # Get the "full" network number
|
---|
[253] | 117 | my $net = new NetAddr::IP $cidr;
|
---|
[2] | 118 |
|
---|
| 119 | # Assumptions: All data in ipdb is public
|
---|
| 120 | # If not, we need another field to indicate "public/private".
|
---|
| 121 |
|
---|
[15] | 122 | foreach my $master (@masterblocks) {
|
---|
[2] | 123 | if ($master->contains($net)) {
|
---|
| 124 |
|
---|
| 125 | # Whew! Ugly little varmint.
|
---|
[15] | 126 | my $masterfilename = "net-".$master->addr."-".$master->masklen.
|
---|
[2] | 127 | "/data/network/".$master->addr."-".$master->masklen.".txt";
|
---|
| 128 |
|
---|
[15] | 129 | open MASTERFILE,">>$rwhoisDataPath/$masterfilename"
|
---|
| 130 | or print "File open error: '$!' on '$rwhoisDataPath/$masterfilename'\n";
|
---|
[2] | 131 |
|
---|
[15] | 132 | # cidr custid type city description notes maskbits
|
---|
| 133 |
|
---|
| 134 | # Creation date in record to eventually be "correct"; near-term will
|
---|
| 135 | # be master's creation date; immediate is today's date.
|
---|
| 136 | my $date;
|
---|
[2] | 137 | chomp ($date = `/bin/date +"%Y%m%d"`);
|
---|
[253] | 138 | if ($desc =~ /^\s*$/) { $desc = 'Friendly ISP'; }
|
---|
| 139 | #print "$i:\t$net in $master\n";
|
---|
| 140 |
|
---|
[2] | 141 | print MASTERFILE "---\nID: NETBLK-ISP.$master\n".
|
---|
| 142 | "Auth-Area: $master\n".
|
---|
| 143 | "Network-Name: ISP-".$net->network."\n".
|
---|
| 144 | "IP-Network: $net\n".
|
---|
| 145 | "IP-Network-Block: ".$net->range."\n".
|
---|
[253] | 146 | "Organization: $desc\n".
|
---|
[2] | 147 | # "Tech-Contact: $data[9]\n".
|
---|
| 148 | "Tech-Contact: abuse\@example.com\n".
|
---|
| 149 | "Admin-Contact: ISP-ARIN-HANDLE\n".
|
---|
[253] | 150 | "Created: $ctime\n".
|
---|
| 151 | "Updated: $mtime\n".
|
---|
[2] | 152 | "Updated-By: noc\@example.com\n";
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | # print "$data[0]\t| $data[1]\t| $data[2]\t| $data[3]\t| $data[4]\t| ".
|
---|
| 159 | # "$data[5]\t| $data[6]\t| $data[7]\t| $data[8]\t| $data[9]\n";
|
---|
| 160 | # print "$data[0]\t| $data[1]\t| $data[2]\t| $data[3]\t| $data[4]\t| ".
|
---|
| 161 | # "$data[5]\t| $data[6]\t| $data[7]\t| $data[8]\n";
|
---|
[253] | 162 | $i++;
|
---|
[2] | 163 | } # while fetchrow_array()
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 | $dbh->disconnect;
|
---|