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