Ignore:
Timestamp:
07/26/10 17:00:00 (14 years ago)
Author:
Kris Deugau
Message:

/branches/stable

Bring /branches/stable up to date with /trunk. See #13.

Location:
branches/stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/stable

    • Property svn:ignore
      •  

        old new  
        11local.css
         2*.tar.gz
    • Property svn:mergeinfo changed
      /trunkmerged: 415-420,​422-443
  • branches/stable/cgi-bin/IPDB.pm

    r394 r445  
    77# Last update by $Author$
    88###
    9 # Copyright (C) 2004-2006 - Kris Deugau
     9# Copyright (C) 2004-2010 - Kris Deugau
    1010
    1111package IPDB;
     
    2020use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
    2121
    22 $VERSION        = 2.0;
     22$VERSION        = 2; ##VERSION##
    2323@ISA            = qw(Exporter);
    2424@EXPORT_OK    = qw(
     
    5353our %IPDBacl;
    5454
     55our $org_name = 'Example Corp';
     56our $smtphost = 'smtp.example.com';
     57our $domain = 'example.com';
     58our $defcustid = '5554242';
     59# mostly for rwhois
     60##fixme:  leave these blank by default?
     61our $rwhoisDataPath = '/usr/local/rwhoisd/etc/rwhoisd'; # to match ./configure defaults from rwhoisd-1.5.9.6
     62our $org_street = '123 4th Street';
     63our $org_city = 'Anytown';
     64our $org_prov_state = 'ON';
     65our $org_pocode = 'H0H 0H0';
     66our $org_country = 'CA';
     67our $org_phone = '000-555-1234';
     68our $org_techhandle = 'ISP-ARIN-HANDLE';
     69our $org_email = 'noc@example.com';
     70our $hostmaster = 'dns@example.com';
     71
     72our $syslog_facility = 'local2';
     73
    5574# Let's initialize the globals.
    5675## IPDB::initIPDBGlobals()
     
    113132# Set up for a PostgreSQL db;  could be any transactional DBMS with the
    114133# right changes.
    115 # This definition should be sub connectDB($$$) to be technically correct,
    116 # but this breaks.  GRR.
    117134sub connectDB {
    118   my ($dbname,$user,$pass) = @_;
     135  my $dbname = shift;
     136  my $user = shift;
     137  my $pass = shift;
     138  my $dbhost = shift;
     139
    119140  my $dbh;
    120   my $DSN = "DBI:Pg:host=ipdb-db;dbname=$dbname";
    121 #  my $user = 'ipdb';
    122 #  my $pw   = 'ipdbpwd';
     141  my $DSN = "DBI:Pg:".($dbhost ? "host=$dbhost;" : '')."dbname=$dbname";
    123142
    124143# Note that we want to autocommit by default, and we will turn it off locally as necessary.
     
    557576    # have to insert all pool IPs into poolips table as "unallocated".
    558577    $sth = $dbh->prepare("insert into poolips (pool,ip,custid,city,type)".
    559         " values ('$pool', ?, '6750400', '$city', '$type')");
     578        " values ('$pool', ?, '$defcustid', '$city', '$type')");
    560579    my @poolip_list = $pool->hostenum;
    561580    if ($class eq 'all') { # (DSL-ish block - *all* IPs available
     
    614633    eval {
    615634      $msg = "Unable to deallocate $disp_alloctypes{$type} $cidr";
    616       $sth = $dbh->prepare("update poolips set custid='6750400',available='y',".
     635      $sth = $dbh->prepare("update poolips set custid='$defcustid',available='y',".
    617636        "city=(select city from allocations where cidr >>= '$cidr'".
    618637        " order by masklen(cidr) desc limit 1),".
     
    704723      } # end alloctype general case
    705724
    706 ##TEMP
    707 ## Temporary wrapper to "properly" deallocate sIP PPPoE/DSL "netblocks" in 209.91.185.0/24
    708 ## Note that we should really general-case this.
    709 my $staticpool = new NetAddr::IP "209.91.185.0/24";
    710 ##TEMP
    711 if ($cidr->within($staticpool)) {
    712 ##TEMP
    713   # We've already deleted the block, now we have to stuff its IPs into the pool.
    714   my $sth2 = $dbh->prepare("insert into poolips values ('209.91.185.0/24',?,'6750400','Sudbury','di','y','','','')");
    715   $sth2->execute($cidr->addr);
    716   foreach my $ip ($cidr->hostenum) {
    717     $sth2->execute("$ip");
    718   }
    719   $cidr--;
    720   $sth2->execute($cidr->addr);
    721 
    722 ##TEMP
    723 } else {
    724 ##TEMP
    725 
    726       # Now we look for larger-or-equal-sized free blocks in the same master (routed)
    727       # (super)block. If there aren't any, we can't combine blocks anyway.  If there
    728       # are, we check to see if we can combine blocks.
    729       # Execute the statement prepared in the if-else above.
    730 
    731       $sth->execute;
     725      ## Deallocate legacy blocks stashed in the middle of a static IP pool
     726      ## This may be expandable to an even more general case of contained netblock, or other pool types.
     727
     728      # Find out if the block we're deallocating is within a DSL pool
     729      my $sth2 = $dbh->prepare("SELECT cidr,city,type FROM allocations WHERE type LIKE '_p' AND cidr >>= ?");
     730      $sth2->execute("$cidr");
     731      my ($pool,$poolcity,$pooltype) = $sth2->fetchrow_array;
     732
     733      if ($pool || $sth2->rows) {
     734        # We've already deleted the block, now we have to stuff its IPs into the pool.
     735        $pooltype =~ s/p$/i/;   # change type to static IP
     736        $sth2 = $dbh->prepare("INSERT INTO poolips (pool,ip,city,type,custid) values ".
     737                "('$pool',?,'$poolcity','$pooltype','$defcustid')");
     738##fixme:  need to not insert net, gateway, and bcast on "real netblock" pools (DHCPish)
     739        # don't insert .0
     740        $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|;
     741        foreach my $ip ($cidr->hostenum) {
     742          $sth2->execute("$ip");
     743        }
     744        $cidr--;
     745        # don't insert .255
     746        $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.255$|;
     747      } else {  # done returning IPs from a block to a static DSL pool
     748
     749        # Now we look for larger-or-equal-sized free blocks in the same master (routed)
     750        # (super)block. If there aren't any, we can't combine blocks anyway.  If there
     751        # are, we check to see if we can combine blocks.
     752        # Execute the statement prepared in the if-else above.
     753
     754        $sth->execute;
    732755
    733756# NetAddr::IP->compact() attempts to produce the smallest inclusive block
     
    739762#       $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
    740763
    741       my (@together, @combinelist);
    742       my $i=0;
    743       while (my @data = $sth->fetchrow_array) {
    744         my $testIP = new NetAddr::IP $data[0];
    745         @together = $testIP->compact($cidr);
    746         my $num = @together;
    747         if ($num == 1) {
    748           $cidr = $together[0];
    749           $combinelist[$i++] = $testIP;
     764        my (@together, @combinelist);
     765        my $i=0;
     766        while (my @data = $sth->fetchrow_array) {
     767          my $testIP = new NetAddr::IP $data[0];
     768          @together = $testIP->compact($cidr);
     769          my $num = @together;
     770          if ($num == 1) {
     771            $cidr = $together[0];
     772            $combinelist[$i++] = $testIP;
     773          }
    750774        }
    751       }
    752 
    753       # Clear old freeblocks entries - if any.  They should all be within
    754       # the $cidr determined above.
    755       $sth = $dbh->prepare("delete from freeblocks where cidr <<='$cidr'");
    756       $sth->execute;
    757 
    758       # insert "new" freeblocks entry
    759       if ($type eq 'rm') {
    760         $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
     775
     776        # Clear old freeblocks entries - if any.  They should all be within
     777        # the $cidr determined above.
     778        $sth = $dbh->prepare("delete from freeblocks where cidr <<='$cidr'");
     779        $sth->execute;
     780
     781        # insert "new" freeblocks entry
     782        if ($type eq 'rm') {
     783          $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
    761784                " values ('$cidr',".$cidr->masklen.",'<NULL>')");
    762       } else {
    763         # Magic hackery to insert "correct" data for deallocation of
    764         # non-contained blocks allocated from within a container.
    765         $type = 'pr' if $con_type && $con_type eq 'pc';
    766 
    767         $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
     785        } else {
     786          # Magic hackery to insert "correct" data for deallocation of
     787          # non-contained blocks allocated from within a container.
     788          $type = 'pr' if $con_type && $con_type eq 'pc';
     789
     790          $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
    768791                " values ('$cidr',".$cidr->masklen.
    769792                ",(select city from routed where cidr >>= '$cidr'),'".
    770793                (($type =~ /^(.)r$/) ? "$1" : 'y')."')");
    771       }
    772       $sth->execute;
    773 
    774 ##TEMP
    775 }
    776 ##TEMP
     794        }
     795        $sth->execute;
     796
     797      } # done returning IPs to the appropriate place
    777798
    778799      # If we got here, we've succeeded.  Whew!
     
    780801    }; # end eval
    781802    if ($@) {
     803      $msg = $@;
    782804      eval { $dbh->rollback; };
    783805      return ('FAIL', $msg);
     
    806828## IPDB::mailNotify()
    807829# Sends notification mail to recipients regarding an IPDB operation
    808 sub mailNotify ($$$) {
    809   my ($recip,$subj,$message) = @_;
    810   my $mailer = Net::SMTP->new("smtp.example.com", Hello => "ipdb.example.com");
    811 
    812   $mailer->mail('ipdb@example.com');
    813   $mailer->to($recip);
    814   $mailer->data("From: \"IP Database\" <ipdb\@example.com>\n",
     830sub mailNotify {
     831  my $dbh = shift;
     832  my ($action,$subj,$message) = @_;
     833
     834##fixme: need to redesign the breakdown/processing for $action for proper handling of all cases
     835
     836# split action into parts for fiddlement.  nb: there are almost certainly better ways to do this.
     837  my @actionbits = split //, $action;
     838
     839  # want to notify anyone who has specifically requested notify on *this* type ($action as passed),
     840  # on "all static IP types" or "all pool types" (and other last-char-in-type groupings), on eg "all DSL types",
     841  # and "all events with this action"
     842  my @actionsets = ($action);
     843##fixme: ick, eww.  really gotta find a better way to handle this...
     844  push @actionsets, ($actionbits[0].'.'.$actionbits[2],
     845        $actionbits[0].$actionbits[1].'.', $actionbits[0].'a') if $action =~ /^.{3}$/;
     846
     847  my $mailer = Net::SMTP->new($smtphost, Hello => "ipdb.$domain");
     848
     849  # get recip list from db
     850  my $sth = $dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
     851
     852  my %reciplist;
     853  foreach (@actionsets) {
     854    $sth->execute($_);
     855##fixme - need to handle db errors
     856    my ($recipsub) = $sth->fetchrow_array;
     857    next if !$recipsub;
     858    foreach (split(/,/, $recipsub)) {
     859      $reciplist{$_}++;
     860    }
     861  }
     862
     863  return if !%reciplist;
     864
     865  foreach my $recip (keys %reciplist) {
     866    $mailer->mail("ipdb\@$domain");
     867    $mailer->to($recip);
     868    $mailer->data("From: \"$org_name IP Database\" <ipdb\@$domain>\n",
    815869        "To: $recip\n",
    816870        "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
    817871        "Subject: {IPDB} $subj\n",
    818872        "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n",
    819         "Organization: Example Corp\n",
     873        "Organization: $org_name\n",
    820874        "\n$message\n");
     875  }
    821876  $mailer->quit;
    822877}
Note: See TracChangeset for help on using the changeset viewer.