Changeset 445 for branches/stable/cgi-bin/IPDB.pm
- Timestamp:
- 07/26/10 17:00:00 (14 years ago)
- Location:
- branches/stable
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable
- Property svn:ignore
-
old new 1 1 local.css 2 *.tar.gz
-
- Property svn:mergeinfo changed
/trunk merged: 415-420,422-443
- Property svn:ignore
-
branches/stable/cgi-bin/IPDB.pm
r394 r445 7 7 # Last update by $Author$ 8 8 ### 9 # Copyright (C) 2004-20 06- Kris Deugau9 # Copyright (C) 2004-2010 - Kris Deugau 10 10 11 11 package IPDB; … … 20 20 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); 21 21 22 $VERSION = 2 .0;22 $VERSION = 2; ##VERSION## 23 23 @ISA = qw(Exporter); 24 24 @EXPORT_OK = qw( … … 53 53 our %IPDBacl; 54 54 55 our $org_name = 'Example Corp'; 56 our $smtphost = 'smtp.example.com'; 57 our $domain = 'example.com'; 58 our $defcustid = '5554242'; 59 # mostly for rwhois 60 ##fixme: leave these blank by default? 61 our $rwhoisDataPath = '/usr/local/rwhoisd/etc/rwhoisd'; # to match ./configure defaults from rwhoisd-1.5.9.6 62 our $org_street = '123 4th Street'; 63 our $org_city = 'Anytown'; 64 our $org_prov_state = 'ON'; 65 our $org_pocode = 'H0H 0H0'; 66 our $org_country = 'CA'; 67 our $org_phone = '000-555-1234'; 68 our $org_techhandle = 'ISP-ARIN-HANDLE'; 69 our $org_email = 'noc@example.com'; 70 our $hostmaster = 'dns@example.com'; 71 72 our $syslog_facility = 'local2'; 73 55 74 # Let's initialize the globals. 56 75 ## IPDB::initIPDBGlobals() … … 113 132 # Set up for a PostgreSQL db; could be any transactional DBMS with the 114 133 # right changes. 115 # This definition should be sub connectDB($$$) to be technically correct,116 # but this breaks. GRR.117 134 sub connectDB { 118 my ($dbname,$user,$pass) = @_; 135 my $dbname = shift; 136 my $user = shift; 137 my $pass = shift; 138 my $dbhost = shift; 139 119 140 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"; 123 142 124 143 # Note that we want to autocommit by default, and we will turn it off locally as necessary. … … 557 576 # have to insert all pool IPs into poolips table as "unallocated". 558 577 $sth = $dbh->prepare("insert into poolips (pool,ip,custid,city,type)". 559 " values ('$pool', ?, ' 6750400', '$city', '$type')");578 " values ('$pool', ?, '$defcustid', '$city', '$type')"); 560 579 my @poolip_list = $pool->hostenum; 561 580 if ($class eq 'all') { # (DSL-ish block - *all* IPs available … … 614 633 eval { 615 634 $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',". 617 636 "city=(select city from allocations where cidr >>= '$cidr'". 618 637 " order by masklen(cidr) desc limit 1),". … … 704 723 } # end alloctype general case 705 724 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; 732 755 733 756 # NetAddr::IP->compact() attempts to produce the smallest inclusive block … … 739 762 # $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27. 740 763 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 } 750 774 } 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)". 761 784 " values ('$cidr',".$cidr->masklen.",'<NULL>')"); 762 763 # Magic hackery to insert "correct" data for deallocation of764 # 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)". 768 791 " values ('$cidr',".$cidr->masklen. 769 792 ",(select city from routed where cidr >>= '$cidr'),'". 770 793 (($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 777 798 778 799 # If we got here, we've succeeded. Whew! … … 780 801 }; # end eval 781 802 if ($@) { 803 $msg = $@; 782 804 eval { $dbh->rollback; }; 783 805 return ('FAIL', $msg); … … 806 828 ## IPDB::mailNotify() 807 829 # 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", 830 sub 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", 815 869 "To: $recip\n", 816 870 "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n", 817 871 "Subject: {IPDB} $subj\n", 818 872 "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n", 819 "Organization: Example Corp\n",873 "Organization: $org_name\n", 820 874 "\n$message\n"); 875 } 821 876 $mailer->quit; 822 877 }
Note:
See TracChangeset
for help on using the changeset viewer.