source: branches/dns/cgi-bin/IPDB.pm@ 275

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

/branches/dns

Remove unused/unusable (?) parameter $domain from initPool()

  • Property svn:keywords set to Date Rev Author
File size: 21.5 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-08-10 17:45:18 +0000 (Wed, 10 Aug 2005) $
6# SVN revision $Rev: 275 $
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 %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,
187# rDNS for the block/IP
188# Returns a success code and optional error message.
189sub allocateBlock {
190 my ($dbh,undef,undef,$custid,$type,$city,$desc,$notes,$circid,$rdns) = @_;
191
192 my $cidr = new NetAddr::IP $_[1];
193 my $alloc_from = new NetAddr::IP $_[2];
194 my $sth;
195
196 # To contain the error message, if any.
197 my $msg = "Unknown error allocating $cidr as '$type'";
198
199 # Enable transactions and error handling
200 local $dbh->{AutoCommit} = 0; # These need to be local so we don't
201 local $dbh->{RaiseError} = 1; # step on our toes by accident.
202
203 if ($type =~ /^.i$/) {
204 $msg = "Unable to assign static IP $cidr to $custid";
205 eval {
206 # We have to do this in two parts because otherwise we lose
207 # the ability to return the IP assigned. Should that change,
208 # the commented SQL statement below may become usable.
209# update poolips set custid='$custid',city='$city',available='n',
210# description='$desc',notes='$notes',circuitid='$circid'
211# where ip=(select ip from poolips where pool='$alloc_from'
212# and available='y' order by ip limit 1);
213
214 $sth = $dbh->prepare("select ip from poolips where pool='$alloc_from'".
215 " and available='y' order by ip");
216 $sth->execute;
217
218 my @data = $sth->fetchrow_array;
219 $cidr = $data[0]; # $cidr is already declared when we get here!
220
221 $sth = $dbh->prepare("update poolips set custid='$custid',".
222 "city='$city',available='n',description='$desc',notes='$notes',".
223 "circuitid='$circid'".
224 " where ip='$cidr'");
225 $sth->execute;
226
227 # rDNS change is an update as the pool should have set "unassigned..."
228 # rDNS on all IPs already.
229 $msg = "Unable to update rDNS assigning $cidr to $custid";
230 $sth = $dbh->prepare("update dns set hostname='$rdns',auto='n' where ip='$cidr'");
231 $sth->execute;
232
233 $dbh->commit;
234 };
235 if ($@) {
236 $msg .= ": '".$sth->errstr."'";
237 eval { $dbh->rollback; };
238 return ('FAIL',$msg);
239 } else {
240 return ('OK',"$cidr");
241 }
242
243 } else { # end IP-from-pool allocation
244
245 if ($cidr == $alloc_from) {
246 # Easiest case- insert in one table, delete in the other, and go home. More or less.
247 # insert into allocations values (cidr,custid,type,city,desc) and
248 # delete from freeblocks where cidr='cidr'
249 # For data safety on non-transaction DBs, we delete first.
250
251 eval {
252 $msg = "Unable to allocate $cidr as '$disp_alloctypes{$type}'";
253 if ($type eq 'rm') {
254 $sth = $dbh->prepare("update freeblocks set routed='y',city='$city'".
255 " where cidr='$cidr'");
256 $sth->execute;
257 $sth = $dbh->prepare("insert into routed (cidr,maskbits,city)".
258 " values ('$cidr',".$cidr->masklen.",'$city')");
259 $sth->execute;
260 } else {
261 # common stuff for end-use, dialup, dynDSL, pools, etc, etc.
262
263 # special case - block is a container/"reserve" block
264 if ($type =~ /^(.)c$/) {
265 $sth = $dbh->prepare("update freeblocks set routed='$1' where cidr='$cidr'");
266 $sth->execute;
267 } else {
268 # "normal" case
269 $sth = $dbh->prepare("delete from freeblocks where cidr='$cidr'");
270 $sth->execute;
271 }
272 $sth = $dbh->prepare("insert into allocations".
273 " (cidr,custid,type,city,description,notes,maskbits,circuitid)".
274 " values ('$cidr','$custid','$type','$city','$desc','$notes',".
275 $cidr->masklen.",'$circid')");
276 $sth->execute;
277
278 # And initialize the pool, if necessary
279 # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
280 # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
281 if ($type =~ /^.p$/) {
282 $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
283 my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"all");
284 die $rmsg if $code eq 'FAIL';
285 } elsif ($type =~ /^.d$/) {
286 $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
287 my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"normal");
288 die $rmsg if $code eq 'FAIL';
289 }
290
291 } # routing vs non-routing netblock
292
293 $dbh->commit;
294 }; # end of eval
295 if ($@) {
296 $msg .= ": ".$@;
297 eval { $dbh->rollback; };
298 return ('FAIL',$msg);
299 } else {
300 return ('OK',"OK");
301 }
302
303 } else { # cidr != alloc_from
304
305 # Hard case. Allocation is smaller than free block.
306 my $wantmaskbits = $cidr->masklen;
307 my $maskbits = $alloc_from->masklen;
308
309 my @newfreeblocks; # Holds free blocks generated from splitting the source freeblock.
310
311 # This determines which blocks will be left "free" after allocation. We take the
312 # block we're allocating from, and split it in half. We see which half the wanted
313 # block is in, and repeat until the wanted block is equal to one of the halves.
314 my $i=0;
315 my $tmp_from = $alloc_from; # So we don't munge $alloc_from
316 while ($maskbits++ < $wantmaskbits) {
317 my @subblocks = $tmp_from->split($maskbits);
318 $newfreeblocks[$i++] = (($cidr->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
319 $tmp_from = ( ($cidr->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
320 } # while
321
322 # Begin SQL transaction block
323 eval {
324 $msg = "Unable to allocate $cidr as '$disp_alloctypes{$type}'";
325
326 # Delete old freeblocks entry
327 $sth = $dbh->prepare("delete from freeblocks where cidr='$alloc_from'");
328 $sth->execute();
329
330 # now we have to do some magic for routing blocks
331 if ($type eq 'rm') {
332
333 # Insert the new freeblocks entries
334 # Note that non-routed blocks are assigned to <NULL>
335 # and use the default value for the routed column ('n')
336 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
337 " values (?, ?, '<NULL>')");
338 foreach my $block (@newfreeblocks) {
339 $sth->execute("$block", $block->masklen);
340 }
341
342 # Insert the entry in the routed table
343 $sth = $dbh->prepare("insert into routed (cidr,maskbits,city)".
344 " values ('$cidr',".$cidr->masklen.",'$city')");
345 $sth->execute;
346 # Insert the (almost) same entry in the freeblocks table
347 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
348 " values ('$cidr',".$cidr->masklen.",'$city','y')");
349 $sth->execute;
350
351 } else { # done with alloctype == rm
352
353 # Insert the new freeblocks entries
354 # Along with some more HairyPerl(TM) in case we're inserting a
355 # subblock (.r) allocation
356 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
357 " values (?, ?, (select city from routed where cidr >>= '$cidr'),'".
358 (($type =~ /^(.)r$/) ? "$1" : 'y')."')");
359 foreach my $block (@newfreeblocks) {
360 $sth->execute("$block", $block->masklen);
361 }
362 # Special-case for reserve/"container" blocks - generate
363 # the "extra" freeblocks entry for the container
364 if ($type =~ /^(.)c$/) {
365 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
366 " values ('$cidr',".$cidr->masklen.",'$city','$1')");
367 $sth->execute;
368 }
369 # Insert the allocations entry
370 $sth = $dbh->prepare("insert into allocations (cidr,custid,type,city,".
371 "description,notes,maskbits,circuitid)".
372 " values ('$cidr','$custid','$type','$city','$desc','$notes',".
373 $cidr->masklen.",'$circid')");
374 $sth->execute;
375
376 # And initialize the pool, if necessary
377 # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
378 # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
379 if ($type =~ /^.p$/) {
380 $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
381 my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"all");
382 die $rmsg if $code eq 'FAIL';
383 } elsif ($type =~ /^.d$/) {
384 $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
385 my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"normal");
386 die $rmsg if $code eq 'FAIL';
387 }
388
389 } # done with netblock alloctype != rm
390
391 $dbh->commit;
392 }; # end eval
393 if ($@) {
394 $msg .= ": ".$@;
395 eval { $dbh->rollback; };
396 return ('FAIL',$msg);
397 } else {
398 return ('OK',"OK");
399 }
400
401 } # end fullcidr != alloc_from
402
403 } # end static-IP vs netblock allocation
404
405} # end allocateBlock()
406
407
408## IPDB::initPool()
409# Initializes a pool
410# Requires a database handle, the pool CIDR, type, city, and a parameter
411# indicating whether the pool should allow allocation of literally every
412# IP, or if it should reserve network/gateway/broadcast IPs
413# Note that this is NOT done in a transaction, that's why it's a private
414# function and should ONLY EVER get called from allocateBlock()
415# We also do the rDNS for the pool here to save some sanity.
416sub initPool {
417 my ($dbh,undef,$type,$city,$class,$rdns) = @_;
418 my $pool = new NetAddr::IP $_[1];
419
420##fixme Need to just replace 2nd char of type with i rather than capturing 1st char of type
421 $type =~ s/[pd]$/i/;
422 my $sth;
423 my $msg;
424
425 # Trap errors so we can pass them back to the caller. Even if the
426 # caller is only ever supposed to be local, and therefore already
427 # trapping errors. >:(
428 local $dbh->{AutoCommit} = 0; # These need to be local so we don't
429 local $dbh->{RaiseError} = 1; # step on our toes by accident.
430
431 eval {
432 # have to insert all pool IPs into poolips table as "unallocated".
433 $sth = $dbh->prepare("insert into poolips (pool,ip,custid,city,type)".
434 " values ('$pool', ?, '6750400', '$city', '$type')");
435 my $sth2 = $dbh->prepare("insert into dns (ip,hostname) values (?, ?)");
436 my @poolip_list = $pool->hostenum;
437 if ($class eq 'all') { # (DSL-ish block - *all* IPs available
438 if ($pool->addr !~ /\.0$/) { # .0 causes weirdness.
439 $sth->execute($pool->addr);
440 # Insert rDNS entry
441 $sth2->execute($pool->addr,subst_rDNS($rdns,$pool->addr));
442 }
443 for (my $i=0; $i<=$#poolip_list; $i++) {
444 $sth->execute($poolip_list[$i]->addr);
445 # Insert rDNS entry
446 $sth2->execute($poolip_list[$i]->addr,subst_rDNS($rdns,$poolip_list[$i]->addr));
447 }
448 $pool--;
449 if ($pool->addr !~ /\.255$/) { # .255 can cause weirdness.
450 $sth->execute($pool->addr);
451 # Insert rDNS entry
452 $sth2->execute($pool->addr,subst_rDNS($rdns,$pool->addr));
453 }
454 } else { # (real netblock)
455 for (my $i=1; $i<=$#poolip_list; $i++) {
456 $sth->execute($poolip_list[$i]->addr);
457 # Insert rDNS entry
458 $sth2->execute($poolip_list[$i]->addr,
459 subst_rDNS($rdns,$poolip_list[$i]->addr));
460 }
461 }
462 };
463 if ($@) {
464 $msg = "'".$sth->errstr."'";
465 eval { $dbh->rollback; };
466 return ('FAIL',$msg);
467 } else {
468 return ('OK',"OK");
469 }
470} # end initPool()
471
472
473## IPDB::deleteBlock()
474# Removes an allocation from the database, including deleting IPs
475# from poolips and recombining entries in freeblocks if possible
476# Also handles "deleting" a static IP allocation, and removal of a master
477# Requires a database handle, the block to delete, and the type of block
478sub deleteBlock {
479 my ($dbh,undef,$type) = @_;
480 my $cidr = new NetAddr::IP $_[1];
481
482 my $sth;
483
484 # To contain the error message, if any.
485 my $msg = "Unknown error deallocating $type $cidr";
486 # Enable transactions and exception-on-errors... but only for this sub
487 local $dbh->{AutoCommit} = 0;
488 local $dbh->{RaiseError} = 1;
489
490 # First case. The "block" is a static IP
491 # Note that we still need some additional code in the odd case
492 # of a netblock-aligned contiguous group of static IPs
493 if ($type =~ /^.i$/) {
494
495 eval {
496 $msg = "Unable to deallocate $disp_alloctypes{$type} $cidr";
497 $sth = $dbh->prepare("update poolips set custid='6750400',available='y',".
498 "city=(select city from allocations where cidr >>= '$cidr'),".
499 "description='',notes='',circuitid='' where ip='$cidr'");
500 $sth->execute;
501 $dbh->commit;
502 };
503 if ($@) {
504 eval { $dbh->rollback; };
505 return ('FAIL',$msg);
506 } else {
507 return ('OK',"OK");
508 }
509
510 } elsif ($type eq 'mm') { # end alloctype =~ /.i/
511
512 $msg = "Unable to delete master block $cidr";
513 eval {
514 $sth = $dbh->prepare("delete from masterblocks where cidr='$cidr'");
515 $sth->execute;
516 $sth = $dbh->prepare("delete from freeblocks where cidr='$cidr'");
517 $sth->execute;
518 $dbh->commit;
519 };
520 if ($@) {
521 eval { $dbh->rollback; };
522 return ('FAIL', $msg);
523 } else {
524 return ('OK',"OK");
525 }
526
527 } else { # end alloctype master block case
528
529 ## This is a big block; but it HAS to be done in a chunk. Any removal
530 ## of a netblock allocation may result in a larger chunk of free
531 ## contiguous IP space - which may in turn be combined into a single
532 ## netblock rather than a number of smaller netblocks.
533
534 eval {
535
536 if ($type eq 'rm') {
537 $msg = "Unable to remove routing allocation $cidr";
538 $sth = $dbh->prepare("delete from routed where cidr='$cidr'");
539 $sth->execute;
540 # Make sure block getting deleted is properly accounted for.
541 $sth = $dbh->prepare("update freeblocks set routed='n',city='<NULL>'".
542 " where cidr='$cidr'");
543 $sth->execute;
544 # Set up query to start compacting free blocks.
545 $sth = $dbh->prepare("select cidr from freeblocks where ".
546 "maskbits<=".$cidr->masklen." and routed='n' order by maskbits desc");
547
548 } else { # end alloctype routing case
549
550 # Delete all allocations within the block being deleted. This is
551 # deliberate and correct, and removes the need to special-case
552 # removal of "container" blocks.
553 $sth = $dbh->prepare("delete from allocations where cidr <<='$cidr'");
554 $sth->execute;
555
556 # Special case - delete pool IPs
557 if ($type =~ /^.[pd]$/) {
558 # We have to delete the IPs from the pool listing.
559 $sth = $dbh->prepare("delete from poolips where pool='$cidr'");
560 $sth->execute;
561 }
562
563 # Set up query for compacting free blocks.
564 $sth = $dbh->prepare("select cidr from freeblocks where cidr <<= ".
565 "(select cidr from routed where cidr >>= '$cidr') ".
566 " and maskbits<=".$cidr->masklen.
567 " and routed='".(($type =~ /^(.)r$/) ? '$1' : 'y').
568 "' order by maskbits desc");
569
570 } # end alloctype general case
571
572 # Now we look for larger-or-equal-sized free blocks in the same master (routed)
573 # (super)block. If there aren't any, we can't combine blocks anyway. If there
574 # are, we check to see if we can combine blocks.
575 # Execute the statement prepared in the if-else above.
576
577 $sth->execute;
578
579# NetAddr::IP->compact() attempts to produce the smallest inclusive block
580# from the caller and the passed terms.
581# EG: if you call $cidr->compact($ip1,$ip2,$ip3) when $cidr, $ip1, $ip2,
582# and $ip3 are consecutive /27's starting on .0 (.0-.31, .32-.63,
583# .64-.95, and .96-.128), you will get an array containing a single
584# /25 as element 0 (.0-.127). Order is not important; you could have
585# $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
586
587 my (@together, @combinelist);
588 my $i=0;
589 while (my @data = $sth->fetchrow_array) {
590 my $testIP = new NetAddr::IP $data[0];
591 @together = $testIP->compact($cidr);
592 my $num = @together;
593 if ($num == 1) {
594 $cidr = $together[0];
595 $combinelist[$i++] = $testIP;
596 }
597 }
598
599 # Clear old freeblocks entries - if any. They should all be within
600 # the $cidr determined above.
601 $sth = $dbh->prepare("delete from freeblocks where cidr <<='$cidr'");
602 $sth->execute;
603
604 # insert "new" freeblocks entry
605 if ($type eq 'rm') {
606 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
607 " values ('$cidr',".$cidr->masklen.",'<NULL>')");
608 } else {
609 $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
610 " values ('$cidr',".$cidr->masklen.
611 ",(select city from routed where cidr >>= '$cidr'),'".
612 (($type =~ /^(.)r$/) ? "$1" : 'y')."')");
613 }
614 $sth->execute;
615
616 # If we got here, we've succeeded. Whew!
617 $dbh->commit;
618 }; # end eval
619 if ($@) {
620 eval { $dbh->rollback; };
621 return ('FAIL', $msg);
622 } else {
623 return ('OK',"OK");
624 }
625
626 } # end alloctype != netblock
627
628} # end deleteBlock()
629
630
631## IPDB::mailNotify()
632# Sends notification mail to recipients regarding an IPDB operation
633sub mailNotify ($$$) {
634 my ($recip,$subj,$message) = @_;
635 my $mailer = Net::SMTP->new("smtp.example.com", Hello => "ipdb.example.com");
636
637 $mailer->mail('ipdb@example.com');
638 $mailer->to($recip);
639 $mailer->data("From: \"IP Database\" <ipdb\@example.com>\n",
640 "To: $recip\n",
641 "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
642 "Subject: {IPDB} $subj\n",
643 "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n",
644 "Organization: Example Corp\n",
645 "\n$message\n");
646 $mailer->quit;
647}
648
649
650## IPDB::subst_rDNS()
651# Substitute tokens in a template string for rDNS entries
652sub subst_rDNS {
653 my ($template,$ip) = $@;
654
655 my ($o1,$o2,$o3,$o4) = ($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)/);
656 $template =~ s/\%o1\%/$o1/;
657 $template =~ s/\%o2\%/$o2/;
658 $template =~ s/\%o3\%/$o3/;
659 $template =~ s/\%o4\%/$o4/;
660 $template =~ s/\%d\%/$o1$-o2-$o3-$o4/;
661
662 return $template;
663} # end subst_rDNS()
664
665# Indicates module loaded OK. Required by Perl.
6661;
Note: See TracBrowser for help on using the repository browser.