#!/usr/bin/perl
# -T
# ipdb/cgi-bin/extras/db2rwhois.pl
# Pull data from ipdb and mangle it into RWHOIS
# Initial version 03/26/2004 kdeugau against IPDB v1
###
# Revision info
# $Date: 2005-01-28 18:41:07 +0000 (Fri, 28 Jan 2005) $
# SVN revision $Rev: 141 $
# Last update by $Author: kdeugau $
###

use strict;
use warnings;
use DBI;
use NetAddr::IP;
use IPDB qw(:ALL);

$ENV{"PATH"} = "/bin;/usr/bin";

my $rwhoisDataPath = "/etc/rwhoisd";

# We'll pull this out of the database instead.  Just make sure our
# master blocks are actually listed....  <g>
#@masterblocks = (
#		(new NetAddr::IP "209.91.128.0/18"),
#		(new NetAddr::IP "66.186.64.0/19"),
#		(new NetAddr::IP "192.168.99.0/24")
#		);

my $dbh = connectDB;

my @masterblocks;

# Fill in data about our master blocks as allocated from ARIN
# We open separate files for each of these as appropriate.
# Note that this ASS-U-MEs that we do not add master IP blocks-
# there should probably be a separate system for doing that.
my $sth = $dbh->prepare("select * from masterblocks;");
$sth->execute;
my $i=0;
GETMASTERS: while (my @data = $sth->fetchrow_array()) {

  if ($data[0] =~ /^(192.168.0.0|172.16.0.0|10.0.0.0)/) {
    next GETMASTERS;
  }
  $masterblocks[$i] = new NetAddr::IP $data[0];

print "$masterblocks[$i]\n";

  my $date;
  chomp ($date = `/bin/date +"%Y%m%d"`);

# Whew!  Ugly little varmint.
  my $masterfilename = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.
    "/data/network/".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.".txt";

# Need check here to create tree for netblock?

  open MASTERFILE,">$rwhoisDataPath/$masterfilename";

  print MASTERFILE "ID: NETBLK-ISP.$masterblocks[$i]\n".
  	"Auth-Area: $masterblocks[$i]\n".
	"Network-Name: ISP-".$masterblocks[$i]->network."\n".
	"IP-Network: $masterblocks[$i]\n".
	"IP-Network-Block: ".$masterblocks[$i]->range."\n".
	"Organization: Friendly ISP\n".
	"Tech-Contact: noc\@example.com\n".
	"Admin-Contact: ISP-ARIN-HANDLE\n".
	"Abuse-Contact: abuse\@example.com\n".
	"Created: 20040308\n".
	"Updated: $date\n".
	"Updated-By: noc\@example.com\n";

  close MASTERFILE;
  $i++;
}

# Now read out the data in the "main" delegation list, and check it
# with the master blocks.  We need to do this to decide which rWHOIS
# "net-xxx.xxx.xxx.xxx-mask" tree the data belongs in.

# Make sure to remove the private netblocks from this.
# No use or point in broadcasting our use of them.
$sth = $dbh->prepare("select * from allocations where not (cidr <<= '192.168.0.0/16') and not (cidr <<= '172.16.0.0/12') and not (cidr <<= '10.0.0.0/8') and custid='6750400'");
$sth->execute;

while (my @data = $sth->fetchrow_array()) {

# We get master block info from @masterblocks.
 # ID: NETBLK-ISP.10.0.0.0/8
 # Auth-Area: 10.0.0.0/8
 # Network-Name: ISP-10.0.2.144
 # IP-Network: 10.0.2.144.144/29
 # IP-Network-Block: 10.0.2.144 - 10.0.2.151
 # Organization: WidgetCorp
 # Tech-Contact: bob@widgetcorp.com
 # Admin-Contact: ISP-ARIN-HANDLE
 # Created: 20040314
 # Updated: 20040314
 # Updated-By: noc@example.com

  # Get the "full" network number
  my $net = new NetAddr::IP $data[0];

# Assumptions:  All data in ipdb is public
# If not, we need another field to indicate "public/private".

  foreach my $master (@masterblocks) {
    if ($master->contains($net)) {

# Whew!  Ugly little varmint.
      my $masterfilename = "net-".$master->addr."-".$master->masklen.
	"/data/network/".$master->addr."-".$master->masklen.".txt";

      open MASTERFILE,">>$rwhoisDataPath/$masterfilename"
        or print "File open error: '$!' on '$rwhoisDataPath/$masterfilename'\n";

# cidr custid type city description notes maskbits

# Creation date in record to eventually be "correct";  near-term will
# be master's creation date;  immediate is today's date.
my $date;
      chomp ($date = `/bin/date +"%Y%m%d"`);
if ($data[4] =~ /^\s*$/) { $data[4] = 'Friendly ISP'; }
      print MASTERFILE "---\nID: NETBLK-ISP.$master\n".
  	"Auth-Area: $master\n".
	"Network-Name: ISP-".$net->network."\n".
	"IP-Network: $net\n".
	"IP-Network-Block: ".$net->range."\n".
	"Organization: $data[4]\n".
#	"Tech-Contact: $data[9]\n".
	"Tech-Contact: abuse\@example.com\n".
	"Admin-Contact: ISP-ARIN-HANDLE\n".
	"Created: $date\n".
	"Updated: $date\n".
	"Updated-By: noc\@example.com\n";
    }
  }



  #  print "$data[0]\t| $data[1]\t| $data[2]\t| $data[3]\t| $data[4]\t| ".
  #	"$data[5]\t| $data[6]\t| $data[7]\t| $data[8]\t| $data[9]\n";
  #  print "$data[0]\t| $data[1]\t| $data[2]\t| $data[3]\t| $data[4]\t| ".
  #	"$data[5]\t| $data[6]\t| $data[7]\t| $data[8]\n";

} # while fetchrow_array()


$dbh->disconnect;
