source: branches/stable/cgi-bin/IPDB.pm@ 349

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

/branches/stable

Fix lurking bug-in-waiting where I've reused a DBI statement
handle where I probably shouldn't have.

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