source: branches/sql-cleanup/cgi-bin/IPDB.pm@ 196

Last change on this file since 196 was 153, checked in by Kris Deugau, 20 years ago

/branches/sql-cleanup

Another fix related to generalizing pools and static IP types

  • Property svn:keywords set to Date Rev Author
File size: 18.9 KB
RevLine 
[8]1# ipdb/cgi-bin/IPDB.pm
[66]2# Contains functions for IPDB - database access, subnet mangling, block allocation, etc
[8]3###
4# SVN revision info
5# $Date: 2005-02-04 21:32:25 +0000 (Fri, 04 Feb 2005) $
6# SVN revision $Rev: 153 $
7# Last update by $Author: kdeugau $
8###
[66]9# Copyright (C) 2004 - Kris Deugau
[8]10
[4]11package IPDB;
12
13use strict;
14use warnings;
15use Exporter;
[77]16use DBI;
[66]17use Net::SMTP;
[68]18use POSIX;
[4]19use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
20
[77]21$VERSION = 2.0;
[4]22@ISA = qw(Exporter);
[106]23@EXPORT_OK = qw(
24 %disp_alloctypes %list_alloctypes @citylist @poplist @masterblocks
[118]25 %allocated %free %routed %bigfree
[106]26 &initIPDBGlobals &connectDB &finish &checkDBSanity &allocateBlock &deleteBlock
27 &mailNotify
28 );
[4]29
30@EXPORT = (); # Export nothing by default.
[106]31%EXPORT_TAGS = ( ALL => [qw(
32 %disp_alloctypes %list_alloctypes @citylist @poplist @masterblocks
[118]33 %allocated %free %routed %bigfree
[106]34 &initIPDBGlobals &connectDB &finish &checkDBSanity &allocateBlock
35 &deleteBlock &mailNotify
36 )]
37 );
[4]38
[77]39##
40## Global variables
41##
42our %disp_alloctypes;
43our %list_alloctypes;
[96]44our @citylist;
45our @poplist;
[106]46our @masterblocks;
[118]47our %allocated;
48our %free;
49our %routed;
50our %bigfree;
[66]51
[77]52# Let's initialize the globals.
53## IPDB::initIPDBGlobals()
54# Initialize all globals. Takes a database handle, returns a success or error code
55sub initIPDBGlobals {
56 my $dbh = $_[0];
57 my $sth;
58
[106]59 # Initialize alloctypes hashes
[147]60 $sth = $dbh->prepare("select type,listname,dispname,listorder from alloctypes order by listorder");
[77]61 $sth->execute;
62 while (my @data = $sth->fetchrow_array) {
[106]63 $disp_alloctypes{$data[0]} = $data[2];
64 if ($data[3] < 900) {
65 $list_alloctypes{$data[0]} = $data[1];
66 }
[77]67 }
[96]68
69 # City and POP listings
[147]70 $sth = $dbh->prepare("select city,routing from cities order by city");
[96]71 $sth->execute;
72 return (undef,$sth->errstr) if $sth->err;
73 while (my @data = $sth->fetchrow_array) {
[106]74 push @citylist, $data[0];
[96]75 if ($data[1] eq 'y') {
[106]76 push @poplist, $data[0];
[96]77 }
78 }
79
[106]80 # Master block list
[147]81 $sth = $dbh->prepare("select cidr from masterblocks order by cidr");
[106]82 $sth->execute;
83 for (my $i=0; my @data = $sth->fetchrow_array(); $i++) {
84 $masterblocks[$i] = new NetAddr::IP $data[0];
[118]85 $allocated{"$masterblocks[$i]"} = 0;
86 $free{"$masterblocks[$i]"} = 0;
87 $bigfree{"$masterblocks[$i]"} = 128; # Larger number means smaller block.
88 # Set to 128 to prepare for IPv6
89 $routed{"$masterblocks[$i]"} = 0;
[106]90 }
91 return (undef,$sth->errstr) if $sth->err;
92
[77]93 return (1,"OK");
94} # end initIPDBGlobals
95
96
97## IPDB::connectDB()
[4]98# Creates connection to IPDB.
[77]99# Requires the database name, username, and password.
[4]100# Returns a handle to the db.
[77]101# Set up for a PostgreSQL db; could be any transactional DBMS with the
102# right changes.
103# This definition should be sub connectDB($$$) to be technically correct,
104# but this breaks. GRR.
[4]105sub connectDB {
[77]106 my ($dbname,$user,$pass) = @_;
[4]107 my $dbh;
[77]108 my $DSN = "DBI:Pg:dbname=$dbname";
109# my $user = 'ipdb';
110# my $pw = 'ipdbpwd';
[4]111
112# Note that we want to autocommit by default, and we will turn it off locally as necessary.
[77]113# We may not want to print gobbledygook errors; YMMV. Have to ponder that further.
114 $dbh = DBI->connect($DSN, $user, $pass, {
115 AutoCommit => 1,
116 PrintError => 0
117 })
118 or return (undef, $DBI::errstr) if(!$dbh);
[4]119
[77]120# Return here if we can't select. Note that this indicates a
121# problem executing the select.
[147]122 my $sth = $dbh->prepare("select cidr from masterblocks");
[77]123 $sth->execute();
124 return (undef,$DBI::errstr) if ($sth->err);
125
126# See if the select returned anything (or null data). This should
127# succeed if the select executed, but...
128 $sth->fetchrow();
129 return (undef,$DBI::errstr) if ($sth->err);
130
131# If we get here, we should be OK.
132 return ($dbh,"DB connection OK");
[4]133} # end connectDB
134
[77]135
136## IPDB::finish()
137# Cleans up after database handles and so on.
138# Requires a database handle
139sub finish {
140 my $dbh = $_[0];
141 $dbh->disconnect;
142} # end finish
143
144
[106]145## IPDB::checkDBSanity()
[4]146# Quick check to see if the db is responding. A full integrity
147# check will have to be a separate tool to walk the IP allocation trees.
148sub checkDBSanity {
[106]149 my ($dbh) = $_[0];
[4]150
151 if (!$dbh) {
[106]152 print "No database handle, or connection has been closed.";
153 return -1;
[4]154 } else {
155 # it connects, try a stmt.
[147]156 my $sth = $dbh->prepare("select cidr from masterblocks");
[4]157 my $err = $sth->execute();
158
159 if ($sth->fetchrow()) {
160 # all is well.
161 return 1;
162 } else {
[16]163 print "Connected to the database, but could not execute test statement. ".$sth->errstr();
[106]164 return -1;
[4]165 }
166 }
167 # Clean up after ourselves.
[106]168# $dbh->disconnect;
[4]169} # end checkDBSanity
170
[66]171
[77]172## IPDB::allocateBlock()
[66]173# Does all of the magic of actually allocating a netblock
[77]174# Requires database handle, block to allocate, custid, type, city,
175# description, notes, circuit ID, block to allocate from,
176# Returns a success code and optional error message.
177sub allocateBlock {
178 my ($dbh,undef,undef,$custid,$type,$city,$desc,$notes,$circid) = @_;
179
180 my $cidr = new NetAddr::IP $_[1];
181 my $alloc_from = new NetAddr::IP $_[2];
182 my $sth;
[66]183
[79]184 # To contain the error message, if any.
185 my $msg = "Unknown error allocating $cidr as '$type'";
186
[77]187 # Enable transactions and error handling
188 local $dbh->{AutoCommit} = 0; # These need to be local so we don't
189 local $dbh->{RaiseError} = 1; # step on our toes by accident.
[66]190
[153]191 if ($type =~ /^.i$/) {
[79]192 $msg = "Unable to assign static IP $cidr to $custid";
[77]193 eval {
[147]194 # We have to do this in two parts because otherwise we lose
195 # the ability to return the IP assigned. Should that change,
196 # the commented SQL statement below may become usable.
[77]197# update poolips set custid='$custid',city='$city',available='n',
198# description='$desc',notes='$notes',circuitid='$circid'
199# where ip=(select ip from poolips where pool='$alloc_from'
200# and available='y' order by ip limit 1);
[147]201
202 $sth = $dbh->prepare("select ip from poolips where pool='$alloc_from'".
203 " and available='y' order by ip");
204 $sth->execute;
205
[77]206 my @data = $sth->fetchrow_array;
[147]207 $cidr = $data[0]; # $cidr is already declared when we get here!
[77]208
209 $sth = $dbh->prepare("update poolips set custid='$custid',".
[108]210 "city='$city',available='n',description='$desc',notes='$notes',".
[77]211 "circuitid='$circid'".
212 " where ip='$cidr'");
213 $sth->execute;
[79]214 $dbh->commit;
[77]215 };
216 if ($@) {
[107]217 $msg .= ": '".$sth->errstr."'";
[78]218 eval { $dbh->rollback; };
219 return ('FAIL',$msg);
[77]220 } else {
[106]221 return ('OK',"$cidr");
[77]222 }
223
224 } else { # end IP-from-pool allocation
225
226 if ($cidr == $alloc_from) {
227 # Easiest case- insert in one table, delete in the other, and go home. More or less.
228 # insert into allocations values (cidr,custid,type,city,desc) and
229 # delete from freeblocks where cidr='cidr'
230 # For data safety on non-transaction DBs, we delete first.
231
232 eval {
[79]233 $msg = "Unable to allocate $cidr as '$disp_alloctypes{$type}'";
[77]234 if ($type eq 'rr') {
235 $sth = $dbh->prepare("update freeblocks set routed='y',city='$city'".
236 " where cidr='$cidr'");
237 $sth->execute;
[147]238 $sth = $dbh->prepare("insert into routed (cidr,maskbits,city)".
239 " values ('$cidr',".$cidr->masklen.",'$city')");
[77]240 $sth->execute;
241 } else {
242 # common stuff for end-use, dialup, dynDSL, pools, etc, etc.
243 $sth = $dbh->prepare("delete from freeblocks where cidr='$cidr'");
244 $sth->execute;
245
[147]246 $sth = $dbh->prepare("insert into allocations".
247 " (cidr,custid,type,city,description,notes,maskbits,circuitid)".
248 " values ('$cidr','$custid','$type','$city','$desc','$notes',".
[77]249 $cidr->masklen.",'$circid')");
250 $sth->execute;
[78]251
252 # And initialize the pool, if necessary
[149]253 # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
254 # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
[78]255 if ($type =~ /^.p$/) {
[149]256 $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
257 my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"all");
258 die $rmsg if $code eq 'FAIL';
259 } elsif ($type =~ /^.d$/) {
260 $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
261 my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"normal");
262 die $rmsg if $code eq 'FAIL';
[79]263 }
264
[77]265 } # routing vs non-routing netblock
[79]266
[77]267 $dbh->commit;
[78]268 }; # end of eval
[77]269 if ($@) {
[149]270 $msg .= ": ".$@;
[77]271 eval { $dbh->rollback; };
[149]272 return ('FAIL',$msg);
[77]273 } else {
[78]274 return ('OK',"OK");
275 }
[77]276
277 } else { # cidr != alloc_from
278
279 # Hard case. Allocation is smaller than free block.
280 my $wantmaskbits = $cidr->masklen;
281 my $maskbits = $alloc_from->masklen;
282
283 my @newfreeblocks; # Holds free blocks generated from splitting the source freeblock.
284
285 # This determines which blocks will be left "free" after allocation. We take the
286 # block we're allocating from, and split it in half. We see which half the wanted
287 # block is in, and repeat until the wanted block is equal to one of the halves.
288 my $i=0;
289 my $tmp_from = $alloc_from; # So we don't munge $alloc_from
290 while ($maskbits++ < $wantmaskbits) {
291 my @subblocks = $tmp_from->split($maskbits);
292 $newfreeblocks[$i++] = (($cidr->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
293 $tmp_from = ( ($cidr->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
294 } # while
295
296 # Begin SQL transaction block
297 eval {
[79]298 $msg = "Unable to allocate $cidr as '$disp_alloctypes{$type}'";
299
[77]300 # Delete old freeblocks entry
301 $sth = $dbh->prepare("delete from freeblocks where cidr='$alloc_from'");
302 $sth->execute();
303
304 # now we have to do some magic for routing blocks
305 if ($type eq 'rr') {
[79]306
[77]307 # Insert the new freeblocks entries
308 # Note that non-routed blocks are assigned to <NULL>
[147]309 # and use the default value for the routed column ('n')
310 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
311 " values (?, ?, '<NULL>')");
[77]312 foreach my $block (@newfreeblocks) {
313 $sth->execute("$block", $block->masklen);
314 }
[79]315
[77]316 # Insert the entry in the routed table
[147]317 $sth = $dbh->prepare("insert into routed (cidr,maskbits,city)".
318 " values ('$cidr',".$cidr->masklen.",'$city')");
[77]319 $sth->execute;
320 # Insert the (almost) same entry in the freeblocks table
[147]321 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
322 " values ('$cidr',".$cidr->masklen.",'$city','y')");
[77]323 $sth->execute;
324
325 } else { # done with alloctype == rr
326
327 # Insert the new freeblocks entries
[147]328 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
329 " values (?, ?, (select city from routed where cidr >>= '$cidr'),'y')");
[77]330 foreach my $block (@newfreeblocks) {
331 $sth->execute("$block", $block->masklen);
332 }
[79]333
[77]334 # Insert the allocations entry
[147]335 $sth = $dbh->prepare("insert into allocations (cidr,custid,type,city,".
336 "description,notes,maskbits,circuitid)".
337 " values ('$cidr','$custid','$type','$city','$desc','$notes',".
338 $cidr->masklen.",'$circid')");
[77]339 $sth->execute;
[78]340
341 # And initialize the pool, if necessary
[150]342 # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
343 # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
[78]344 if ($type =~ /^.p$/) {
[79]345 $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
[150]346 my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"all");
347 die $rmsg if $code eq 'FAIL';
348 } elsif ($type =~ /^.d$/) {
349 $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
350 my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"normal");
351 die $rmsg if $code eq 'FAIL';
[79]352 }
353
[77]354 } # done with netblock alloctype != rr
[79]355
[77]356 $dbh->commit;
357 }; # end eval
358 if ($@) {
359 eval { $dbh->rollback; };
[78]360 return ('FAIL',$msg);
[77]361 } else {
[78]362 return ('OK',"OK");
[77]363 }
364
365 } # end fullcidr != alloc_from
366
367 } # end static-IP vs netblock allocation
368
369} # end allocateBlock()
370
371
372## IPDB::initPool()
373# Initializes a pool
374# Requires a database handle, the pool CIDR, type, city, and a parameter
375# indicating whether the pool should allow allocation of literally every
376# IP, or if it should reserve network/gateway/broadcast IPs
[78]377# Note that this is NOT done in a transaction, that's why it's a private
378# function and should ONLY EVER get called from allocateBlock()
[77]379sub initPool {
380 my ($dbh,undef,$type,$city,$class) = @_;
381 my $pool = new NetAddr::IP $_[1];
382
[149]383##fixme Need to just replace 2nd char of type with i rather than capturing 1st char of type
384 $type =~ s/[pd]$/i/;
[77]385 my $sth;
[149]386 my $msg;
[77]387
[149]388 # Trap errors so we can pass them back to the caller. Even if the
389 # caller is only ever supposed to be local, and therefore already
390 # trapping errors. >:(
391 local $dbh->{AutoCommit} = 0; # These need to be local so we don't
392 local $dbh->{RaiseError} = 1; # step on our toes by accident.
393
394 eval {
395 # have to insert all pool IPs into poolips table as "unallocated".
396 $sth = $dbh->prepare("insert into poolips (pool,ip,custid,city,type)".
397 " values ('$pool', ?, '6750400', '$city', '$type')");
398 my @poolip_list = $pool->hostenum;
399 if ($class eq 'all') { # (DSL-ish block - *all* IPs available
400 $sth->execute($pool->addr);
401 for (my $i=0; $i<=$#poolip_list; $i++) {
402 $sth->execute($poolip_list[$i]->addr);
403 }
404 $pool--;
405 $sth->execute($pool->addr);
406 } else { # (real netblock)
407 for (my $i=1; $i<=$#poolip_list; $i++) {
408 $sth->execute($poolip_list[$i]->addr);
409 }
[77]410 }
[149]411 };
412 if ($@) {
413 $msg = "'".$sth->errstr."'";
414 eval { $dbh->rollback; };
415 return ('FAIL',$msg);
416 } else {
417 return ('OK',"OK");
[77]418 }
419} # end initPool()
420
421
[93]422## IPDB::deleteBlock()
423# Removes an allocation from the database, including deleting IPs
424# from poolips and recombining entries in freeblocks if possible
425# Also handles "deleting" a static IP allocation, and removal of a master
426# Requires a database handle, the block to delete, and the type of block
427sub deleteBlock {
428 my ($dbh,undef,$type) = @_;
429 my $cidr = new NetAddr::IP $_[1];
430
431 my $sth;
432
433 # To contain the error message, if any.
434 my $msg = "Unknown error deallocating $type $cidr";
435 # Enable transactions and exception-on-errors... but only for this sub
436 local $dbh->{AutoCommit} = 0;
437 local $dbh->{RaiseError} = 1;
438
439 # First case. The "block" is a static IP
440 # Note that we still need some additional code in the odd case
441 # of a netblock-aligned contiguous group of static IPs
442 if ($type =~ /^.i$/) {
443
444 eval {
[149]445 $msg = "Unable to deallocate $disp_alloctypes{$type} $cidr";
[93]446 $sth = $dbh->prepare("update poolips set custid='6750400',available='y',".
[120]447 "city=(select city from allocations where cidr >>= '$cidr'),".
[93]448 "description='',notes='',circuitid='' where ip='$cidr'");
449 $sth->execute;
450 $dbh->commit;
451 };
452 if ($@) {
453 eval { $dbh->rollback; };
454 return ('FAIL',$msg);
455 } else {
456 return ('OK',"OK");
457 }
458
459 } elsif ($type eq 'mm') { # end alloctype =~ /.i/
460
461 $msg = "Unable to delete master block $cidr";
462 eval {
463 $sth = $dbh->prepare("delete from masterblocks where cidr='$cidr'");
464 $sth->execute;
465 $sth = $dbh->prepare("delete from freeblocks where cidr='$cidr'");
466 $sth->execute;
467 $dbh->commit;
468 };
469 if ($@) {
470 eval { $dbh->rollback; };
471 return ('FAIL', $msg);
472 } else {
473 return ('OK',"OK");
474 }
475
476 } else { # end alloctype master block case
477
478 ## This is a big block; but it HAS to be done in a chunk. Any removal
479 ## of a netblock allocation may result in a larger chunk of free
480 ## contiguous IP space - which may in turn be combined into a single
481 ## netblock rather than a number of smaller netblocks.
482
483 eval {
484
485 if ($type eq 'rr') {
486 $msg = "Unable to remove routing allocation $cidr";
487 $sth = $dbh->prepare("delete from routed where cidr='$cidr'");
488 $sth->execute;
489 # Make sure block getting deleted is properly accounted for.
490 $sth = $dbh->prepare("update freeblocks set routed='n',city='<NULL>'".
491 " where cidr='$cidr'");
492 $sth->execute;
493 # Set up query to start compacting free blocks.
[147]494 $sth = $dbh->prepare("select cidr from freeblocks where ".
[93]495 "maskbits<=".$cidr->masklen." and routed='n' order by maskbits desc");
496
497 } else { # end alloctype routing case
498
499 $sth = $dbh->prepare("delete from allocations where cidr='$cidr'");
500 $sth->execute;
501 # Special case - delete pool IPs
[149]502 if ($type =~ /^.[pd]$/) {
[93]503 # We have to delete the IPs from the pool listing.
504 $sth = $dbh->prepare("delete from poolips where pool='$cidr'");
505 $sth->execute;
506 }
507
508 # Set up query for compacting free blocks.
[147]509 $sth = $dbh->prepare("select cidr from freeblocks where cidr <<= ".
[120]510 "(select cidr from routed where cidr >>= '$cidr') ".
[93]511 " and maskbits<=".$cidr->masklen." and routed='y' order by maskbits desc");
512
513 } # end alloctype general case
514
515 # Now we look for larger-or-equal-sized free blocks in the same master (routed)
516 # (super)block. If there aren't any, we can't combine blocks anyway. If there
517 # are, we check to see if we can combine blocks.
518 # Execute the statement prepared in the if-else above.
519
520 $sth->execute;
521
522# NetAddr::IP->compact() attempts to produce the smallest inclusive block
523# from the caller and the passed terms.
524# EG: if you call $cidr->compact($ip1,$ip2,$ip3) when $cidr, $ip1, $ip2,
525# and $ip3 are consecutive /27's starting on .0 (.0-.31, .32-.63,
526# .64-.95, and .96-.128), you will get an array containing a single
527# /25 as element 0 (.0-.127). Order is not important; you could have
528# $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
529
530 my (@together, @combinelist);
531 my $i=0;
532 while (my @data = $sth->fetchrow_array) {
533 my $testIP = new NetAddr::IP $data[0];
534 @together = $testIP->compact($cidr);
535 my $num = @together;
536 if ($num == 1) {
537 $cidr = $together[0];
538 $combinelist[$i++] = $testIP;
539 }
540 }
541
542 # Clear old freeblocks entries - if any. $i==0 if not.
543 if ($i>0) {
544 $sth = $dbh->prepare("delete from freeblocks where cidr=?");
545 foreach my $block (@combinelist) {
546 $sth->execute("$block");
547 }
548 }
549
550 # insert "new" freeblocks entry
551 if ($type eq 'rr') {
[147]552 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
553 " values ('$cidr',".$cidr->masklen.",'<NULL>')");
[93]554 } else {
[147]555 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
556 " values ('$cidr',".$cidr->masklen.
[93]557 ",(select city from routed where cidr >>= '$cidr'),'y')");
558 }
559 $sth->execute;
560
561 # If we got here, we've succeeded. Whew!
562 $dbh->commit;
563 }; # end eval
564 if ($@) {
565 eval { $dbh->rollback; };
566 return ('FAIL', $msg);
567 } else {
568 return ('OK',"OK");
569 }
570
571 } # end alloctype != netblock
572
573} # end deleteBlock()
574
575
[77]576## IPDB::mailNotify()
[66]577# Sends notification mail to recipients regarding an IPDB operation
578sub mailNotify ($$$) {
579 my ($recip,$subj,$message) = @_;
[69]580 my $mailer = Net::SMTP->new("smtp.example.com", Hello => "ipdb.example.com");
[66]581
582 $mailer->mail('ipdb@example.com');
583 $mailer->to($recip);
[69]584 $mailer->data("From: \"IP Database\" <ipdb\@example.com>\n",
[135]585 "To: $recip\n",
[69]586 "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
587 "Subject: {IPDB} $subj\n",
588 "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n",
589 "Organization: Example Corp\n",
590 "\n$message\n");
[66]591 $mailer->quit;
592}
593
[4]594# Indicates module loaded OK. Required by Perl.
5951;
Note: See TracBrowser for help on using the repository browser.