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