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