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

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

/trunk

Merge changes from /branches/sql-cleanup into mainline

  • Merge was clean
  • Property svn:keywords set to Date Rev Author
File size: 18.9 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-02-08 20:56:33 +0000 (Tue, 08 Feb 2005) $
6# SVN revision $Rev: 157 $
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 @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 @citylist @poplist @masterblocks
33 %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 @citylist;
45our @poplist;
46our @masterblocks;
47our %allocated;
48our %free;
49our %routed;
50our %bigfree;
51
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
59 # Initialize alloctypes hashes
60 $sth = $dbh->prepare("select type,listname,dispname,listorder from alloctypes order by listorder");
61 $sth->execute;
62 while (my @data = $sth->fetchrow_array) {
63 $disp_alloctypes{$data[0]} = $data[2];
64 if ($data[3] < 900) {
65 $list_alloctypes{$data[0]} = $data[1];
66 }
67 }
68
69 # City and POP listings
70 $sth = $dbh->prepare("select city,routing from cities order by city");
71 $sth->execute;
72 return (undef,$sth->errstr) if $sth->err;
73 while (my @data = $sth->fetchrow_array) {
74 push @citylist, $data[0];
75 if ($data[1] eq 'y') {
76 push @poplist, $data[0];
77 }
78 }
79
80 # Master block list
81 $sth = $dbh->prepare("select cidr from masterblocks order by cidr");
82 $sth->execute;
83 for (my $i=0; my @data = $sth->fetchrow_array(); $i++) {
84 $masterblocks[$i] = new NetAddr::IP $data[0];
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;
90 }
91 return (undef,$sth->errstr) if $sth->err;
92
93 return (1,"OK");
94} # end initIPDBGlobals
95
96
97## IPDB::connectDB()
98# Creates connection to IPDB.
99# Requires the database name, username, and password.
100# Returns a handle to the db.
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.
105sub connectDB {
106 my ($dbname,$user,$pass) = @_;
107 my $dbh;
108 my $DSN = "DBI:Pg:dbname=$dbname";
109# my $user = 'ipdb';
110# my $pw = 'ipdbpwd';
111
112# Note that we want to autocommit by default, and we will turn it off locally as necessary.
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);
119
120# Return here if we can't select. Note that this indicates a
121# problem executing the select.
122 my $sth = $dbh->prepare("select cidr from masterblocks");
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");
133} # end connectDB
134
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
145## IPDB::checkDBSanity()
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 {
149 my ($dbh) = $_[0];
150
151 if (!$dbh) {
152 print "No database handle, or connection has been closed.";
153 return -1;
154 } else {
155 # it connects, try a stmt.
156 my $sth = $dbh->prepare("select cidr from masterblocks");
157 my $err = $sth->execute();
158
159 if ($sth->fetchrow()) {
160 # all is well.
161 return 1;
162 } else {
163 print "Connected to the database, but could not execute test statement. ".$sth->errstr();
164 return -1;
165 }
166 }
167 # Clean up after ourselves.
168# $dbh->disconnect;
169} # end checkDBSanity
170
171
172## IPDB::allocateBlock()
173# Does all of the magic of actually allocating a netblock
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;
183
184 # To contain the error message, if any.
185 my $msg = "Unknown error allocating $cidr as '$type'";
186
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.
190
191 if ($type =~ /^.i$/) {
192 $msg = "Unable to assign static IP $cidr to $custid";
193 eval {
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.
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);
201
202 $sth = $dbh->prepare("select ip from poolips where pool='$alloc_from'".
203 " and available='y' order by ip");
204 $sth->execute;
205
206 my @data = $sth->fetchrow_array;
207 $cidr = $data[0]; # $cidr is already declared when we get here!
208
209 $sth = $dbh->prepare("update poolips set custid='$custid',".
210 "city='$city',available='n',description='$desc',notes='$notes',".
211 "circuitid='$circid'".
212 " where ip='$cidr'");
213 $sth->execute;
214 $dbh->commit;
215 };
216 if ($@) {
217 $msg .= ": '".$sth->errstr."'";
218 eval { $dbh->rollback; };
219 return ('FAIL',$msg);
220 } else {
221 return ('OK',"$cidr");
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 {
233 $msg = "Unable to allocate $cidr as '$disp_alloctypes{$type}'";
234 if ($type eq 'rr') {
235 $sth = $dbh->prepare("update freeblocks set routed='y',city='$city'".
236 " where cidr='$cidr'");
237 $sth->execute;
238 $sth = $dbh->prepare("insert into routed (cidr,maskbits,city)".
239 " values ('$cidr',".$cidr->masklen.",'$city')");
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
246 $sth = $dbh->prepare("insert into allocations".
247 " (cidr,custid,type,city,description,notes,maskbits,circuitid)".
248 " values ('$cidr','$custid','$type','$city','$desc','$notes',".
249 $cidr->masklen.",'$circid')");
250 $sth->execute;
251
252 # And initialize the pool, if necessary
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.
255 if ($type =~ /^.p$/) {
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';
263 }
264
265 } # routing vs non-routing netblock
266
267 $dbh->commit;
268 }; # end of eval
269 if ($@) {
270 $msg .= ": ".$@;
271 eval { $dbh->rollback; };
272 return ('FAIL',$msg);
273 } else {
274 return ('OK',"OK");
275 }
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 {
298 $msg = "Unable to allocate $cidr as '$disp_alloctypes{$type}'";
299
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') {
306
307 # Insert the new freeblocks entries
308 # Note that non-routed blocks are assigned to <NULL>
309 # and use the default value for the routed column ('n')
310 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
311 " values (?, ?, '<NULL>')");
312 foreach my $block (@newfreeblocks) {
313 $sth->execute("$block", $block->masklen);
314 }
315
316 # Insert the entry in the routed table
317 $sth = $dbh->prepare("insert into routed (cidr,maskbits,city)".
318 " values ('$cidr',".$cidr->masklen.",'$city')");
319 $sth->execute;
320 # Insert the (almost) same entry in the freeblocks table
321 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
322 " values ('$cidr',".$cidr->masklen.",'$city','y')");
323 $sth->execute;
324
325 } else { # done with alloctype == rr
326
327 # Insert the new freeblocks entries
328 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
329 " values (?, ?, (select city from routed where cidr >>= '$cidr'),'y')");
330 foreach my $block (@newfreeblocks) {
331 $sth->execute("$block", $block->masklen);
332 }
333
334 # Insert the allocations entry
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')");
339 $sth->execute;
340
341 # And initialize the pool, if necessary
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.
344 if ($type =~ /^.p$/) {
345 $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
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';
352 }
353
354 } # done with netblock alloctype != rr
355
356 $dbh->commit;
357 }; # end eval
358 if ($@) {
359 eval { $dbh->rollback; };
360 return ('FAIL',$msg);
361 } else {
362 return ('OK',"OK");
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
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()
379sub initPool {
380 my ($dbh,undef,$type,$city,$class) = @_;
381 my $pool = new NetAddr::IP $_[1];
382
383##fixme Need to just replace 2nd char of type with i rather than capturing 1st char of type
384 $type =~ s/[pd]$/i/;
385 my $sth;
386 my $msg;
387
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 }
410 }
411 };
412 if ($@) {
413 $msg = "'".$sth->errstr."'";
414 eval { $dbh->rollback; };
415 return ('FAIL',$msg);
416 } else {
417 return ('OK',"OK");
418 }
419} # end initPool()
420
421
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 {
445 $msg = "Unable to deallocate $disp_alloctypes{$type} $cidr";
446 $sth = $dbh->prepare("update poolips set custid='6750400',available='y',".
447 "city=(select city from allocations where cidr >>= '$cidr'),".
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.
494 $sth = $dbh->prepare("select cidr from freeblocks where ".
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
502 if ($type =~ /^.[pd]$/) {
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.
509 $sth = $dbh->prepare("select cidr from freeblocks where cidr <<= ".
510 "(select cidr from routed where cidr >>= '$cidr') ".
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') {
552 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
553 " values ('$cidr',".$cidr->masklen.",'<NULL>')");
554 } else {
555 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
556 " values ('$cidr',".$cidr->masklen.
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
576## IPDB::mailNotify()
577# Sends notification mail to recipients regarding an IPDB operation
578sub mailNotify ($$$) {
579 my ($recip,$subj,$message) = @_;
580 my $mailer = Net::SMTP->new("smtp.example.com", Hello => "ipdb.example.com");
581
582 $mailer->mail('ipdb@example.com');
583 $mailer->to($recip);
584 $mailer->data("From: \"IP Database\" <ipdb\@example.com>\n",
585 "To: $recip\n",
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");
591 $mailer->quit;
592}
593
594# Indicates module loaded OK. Required by Perl.
5951;
Note: See TracBrowser for help on using the repository browser.