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