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