source: trunk/cgi-bin/IPDB.pm@ 186

Last change on this file since 186 was 186, checked in by Kris Deugau, 19 years ago

/trunk

First round of changes for "clean" handling of subblock allocations.
Note that the code will likely malfunction in a number of corner
and not-so-corner cases. Fixing those will require rethinking of
the allocation types.

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