[8] | 1 | # ipdb/cgi-bin/IPDB.pm
|
---|
[71] | 2 | # Contains functions for IPDB - database access, subnet mangling, block allocation, etc
|
---|
[8] | 3 | ###
|
---|
| 4 | # SVN revision info
|
---|
| 5 | # $Date: 2012-11-12 22:31:34 +0000 (Mon, 12 Nov 2012) $
|
---|
| 6 | # SVN revision $Rev: 550 $
|
---|
| 7 | # Last update by $Author: kdeugau $
|
---|
| 8 | ###
|
---|
[445] | 9 | # Copyright (C) 2004-2010 - Kris Deugau
|
---|
[8] | 10 |
|
---|
[4] | 11 | package IPDB;
|
---|
| 12 |
|
---|
| 13 | use strict;
|
---|
| 14 | use warnings;
|
---|
| 15 | use Exporter;
|
---|
[125] | 16 | use DBI;
|
---|
[71] | 17 | use Net::SMTP;
|
---|
[365] | 18 | use NetAddr::IP qw( Compact );
|
---|
[71] | 19 | use POSIX;
|
---|
[4] | 20 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
|
---|
| 21 |
|
---|
[445] | 22 | $VERSION = 2; ##VERSION##
|
---|
[4] | 23 | @ISA = qw(Exporter);
|
---|
[125] | 24 | @EXPORT_OK = qw(
|
---|
[168] | 25 | %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist @masterblocks
|
---|
[242] | 26 | %allocated %free %routed %bigfree %IPDBacl
|
---|
[365] | 27 | &initIPDBGlobals &connectDB &finish &checkDBSanity &allocateBlock &addMaster
|
---|
| 28 | &deleteBlock &getBlockData &mailNotify
|
---|
[125] | 29 | );
|
---|
[4] | 30 |
|
---|
| 31 | @EXPORT = (); # Export nothing by default.
|
---|
[125] | 32 | %EXPORT_TAGS = ( ALL => [qw(
|
---|
[168] | 33 | %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist
|
---|
[242] | 34 | @masterblocks %allocated %free %routed %bigfree %IPDBacl
|
---|
[125] | 35 | &initIPDBGlobals &connectDB &finish &checkDBSanity &allocateBlock
|
---|
[365] | 36 | &addMaster &deleteBlock &getBlockData &mailNotify
|
---|
[125] | 37 | )]
|
---|
| 38 | );
|
---|
[4] | 39 |
|
---|
[125] | 40 | ##
|
---|
| 41 | ## Global variables
|
---|
| 42 | ##
|
---|
| 43 | our %disp_alloctypes;
|
---|
| 44 | our %list_alloctypes;
|
---|
[168] | 45 | our %def_custids;
|
---|
[125] | 46 | our @citylist;
|
---|
| 47 | our @poplist;
|
---|
| 48 | our @masterblocks;
|
---|
| 49 | our %allocated;
|
---|
| 50 | our %free;
|
---|
| 51 | our %routed;
|
---|
| 52 | our %bigfree;
|
---|
[242] | 53 | our %IPDBacl;
|
---|
[71] | 54 |
|
---|
[445] | 55 | our $org_name = 'Example Corp';
|
---|
| 56 | our $smtphost = 'smtp.example.com';
|
---|
| 57 | our $domain = 'example.com';
|
---|
| 58 | our $defcustid = '5554242';
|
---|
| 59 | # mostly for rwhois
|
---|
| 60 | ##fixme: leave these blank by default?
|
---|
| 61 | our $rwhoisDataPath = '/usr/local/rwhoisd/etc/rwhoisd'; # to match ./configure defaults from rwhoisd-1.5.9.6
|
---|
| 62 | our $org_street = '123 4th Street';
|
---|
| 63 | our $org_city = 'Anytown';
|
---|
| 64 | our $org_prov_state = 'ON';
|
---|
| 65 | our $org_pocode = 'H0H 0H0';
|
---|
| 66 | our $org_country = 'CA';
|
---|
| 67 | our $org_phone = '000-555-1234';
|
---|
| 68 | our $org_techhandle = 'ISP-ARIN-HANDLE';
|
---|
| 69 | our $org_email = 'noc@example.com';
|
---|
| 70 | our $hostmaster = 'dns@example.com';
|
---|
| 71 |
|
---|
| 72 | our $syslog_facility = 'local2';
|
---|
| 73 |
|
---|
[508] | 74 | # Allow allocations to come from private IP ranges by default?
|
---|
| 75 | # Set to 'y' or 'on' to enable.
|
---|
| 76 | our $allowprivrange = '';
|
---|
| 77 |
|
---|
[125] | 78 | # Let's initialize the globals.
|
---|
| 79 | ## IPDB::initIPDBGlobals()
|
---|
| 80 | # Initialize all globals. Takes a database handle, returns a success or error code
|
---|
| 81 | sub initIPDBGlobals {
|
---|
| 82 | my $dbh = $_[0];
|
---|
| 83 | my $sth;
|
---|
| 84 |
|
---|
| 85 | # Initialize alloctypes hashes
|
---|
[168] | 86 | $sth = $dbh->prepare("select type,listname,dispname,listorder,def_custid from alloctypes order by listorder");
|
---|
[125] | 87 | $sth->execute;
|
---|
| 88 | while (my @data = $sth->fetchrow_array) {
|
---|
| 89 | $disp_alloctypes{$data[0]} = $data[2];
|
---|
[168] | 90 | $def_custids{$data[0]} = $data[4];
|
---|
[125] | 91 | if ($data[3] < 900) {
|
---|
| 92 | $list_alloctypes{$data[0]} = $data[1];
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | # City and POP listings
|
---|
[159] | 97 | $sth = $dbh->prepare("select city,routing from cities order by city");
|
---|
[125] | 98 | $sth->execute;
|
---|
| 99 | return (undef,$sth->errstr) if $sth->err;
|
---|
| 100 | while (my @data = $sth->fetchrow_array) {
|
---|
| 101 | push @citylist, $data[0];
|
---|
| 102 | if ($data[1] eq 'y') {
|
---|
| 103 | push @poplist, $data[0];
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | # Master block list
|
---|
[159] | 108 | $sth = $dbh->prepare("select cidr from masterblocks order by cidr");
|
---|
[125] | 109 | $sth->execute;
|
---|
[242] | 110 | return (undef,$sth->errstr) if $sth->err;
|
---|
[125] | 111 | for (my $i=0; my @data = $sth->fetchrow_array(); $i++) {
|
---|
| 112 | $masterblocks[$i] = new NetAddr::IP $data[0];
|
---|
| 113 | $allocated{"$masterblocks[$i]"} = 0;
|
---|
| 114 | $free{"$masterblocks[$i]"} = 0;
|
---|
| 115 | $bigfree{"$masterblocks[$i]"} = 128; # Larger number means smaller block.
|
---|
| 116 | # Set to 128 to prepare for IPv6
|
---|
| 117 | $routed{"$masterblocks[$i]"} = 0;
|
---|
| 118 | }
|
---|
[242] | 119 |
|
---|
| 120 | # Load ACL data. Specific username checks are done at a different level.
|
---|
| 121 | $sth = $dbh->prepare("select username,acl from users");
|
---|
| 122 | $sth->execute;
|
---|
[125] | 123 | return (undef,$sth->errstr) if $sth->err;
|
---|
[242] | 124 | while (my @data = $sth->fetchrow_array) {
|
---|
| 125 | $IPDBacl{$data[0]} = $data[1];
|
---|
| 126 | }
|
---|
[125] | 127 |
|
---|
| 128 | return (1,"OK");
|
---|
| 129 | } # end initIPDBGlobals
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | ## IPDB::connectDB()
|
---|
[4] | 133 | # Creates connection to IPDB.
|
---|
[125] | 134 | # Requires the database name, username, and password.
|
---|
[4] | 135 | # Returns a handle to the db.
|
---|
[125] | 136 | # Set up for a PostgreSQL db; could be any transactional DBMS with the
|
---|
| 137 | # right changes.
|
---|
[4] | 138 | sub connectDB {
|
---|
[445] | 139 | my $dbname = shift;
|
---|
| 140 | my $user = shift;
|
---|
| 141 | my $pass = shift;
|
---|
| 142 | my $dbhost = shift;
|
---|
| 143 |
|
---|
[4] | 144 | my $dbh;
|
---|
[445] | 145 | my $DSN = "DBI:Pg:".($dbhost ? "host=$dbhost;" : '')."dbname=$dbname";
|
---|
[4] | 146 |
|
---|
| 147 | # Note that we want to autocommit by default, and we will turn it off locally as necessary.
|
---|
[125] | 148 | # We may not want to print gobbledygook errors; YMMV. Have to ponder that further.
|
---|
| 149 | $dbh = DBI->connect($DSN, $user, $pass, {
|
---|
| 150 | AutoCommit => 1,
|
---|
| 151 | PrintError => 0
|
---|
| 152 | })
|
---|
| 153 | or return (undef, $DBI::errstr) if(!$dbh);
|
---|
[4] | 154 |
|
---|
[125] | 155 | # Return here if we can't select. Note that this indicates a
|
---|
| 156 | # problem executing the select.
|
---|
[185] | 157 | my $sth = $dbh->prepare("select type from alloctypes");
|
---|
[125] | 158 | $sth->execute();
|
---|
| 159 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 160 |
|
---|
| 161 | # See if the select returned anything (or null data). This should
|
---|
| 162 | # succeed if the select executed, but...
|
---|
| 163 | $sth->fetchrow();
|
---|
| 164 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 165 |
|
---|
| 166 | # If we get here, we should be OK.
|
---|
| 167 | return ($dbh,"DB connection OK");
|
---|
[4] | 168 | } # end connectDB
|
---|
| 169 |
|
---|
[125] | 170 |
|
---|
| 171 | ## IPDB::finish()
|
---|
| 172 | # Cleans up after database handles and so on.
|
---|
| 173 | # Requires a database handle
|
---|
| 174 | sub finish {
|
---|
| 175 | my $dbh = $_[0];
|
---|
| 176 | $dbh->disconnect;
|
---|
| 177 | } # end finish
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | ## IPDB::checkDBSanity()
|
---|
[4] | 181 | # Quick check to see if the db is responding. A full integrity
|
---|
| 182 | # check will have to be a separate tool to walk the IP allocation trees.
|
---|
| 183 | sub checkDBSanity {
|
---|
[125] | 184 | my ($dbh) = $_[0];
|
---|
[4] | 185 |
|
---|
| 186 | if (!$dbh) {
|
---|
[125] | 187 | print "No database handle, or connection has been closed.";
|
---|
| 188 | return -1;
|
---|
[4] | 189 | } else {
|
---|
| 190 | # it connects, try a stmt.
|
---|
[185] | 191 | my $sth = $dbh->prepare("select type from alloctypes");
|
---|
[4] | 192 | my $err = $sth->execute();
|
---|
| 193 |
|
---|
| 194 | if ($sth->fetchrow()) {
|
---|
| 195 | # all is well.
|
---|
| 196 | return 1;
|
---|
| 197 | } else {
|
---|
[16] | 198 | print "Connected to the database, but could not execute test statement. ".$sth->errstr();
|
---|
[125] | 199 | return -1;
|
---|
[4] | 200 | }
|
---|
| 201 | }
|
---|
| 202 | # Clean up after ourselves.
|
---|
[125] | 203 | # $dbh->disconnect;
|
---|
[4] | 204 | } # end checkDBSanity
|
---|
| 205 |
|
---|
[71] | 206 |
|
---|
[365] | 207 | ## IPDB::addMaster()
|
---|
| 208 | # Does all the magic necessary to sucessfully add a master block
|
---|
| 209 | # Requires database handle, block to add
|
---|
| 210 | # Returns failure code and error message or success code and "message"
|
---|
| 211 | sub addMaster {
|
---|
| 212 | my $dbh = shift;
|
---|
| 213 | my $cidr = new NetAddr::IP shift;
|
---|
| 214 |
|
---|
| 215 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 216 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 217 | local $dbh->{AutoCommit} = 0;
|
---|
| 218 | local $dbh->{RaiseError} = 1;
|
---|
| 219 |
|
---|
| 220 | # Wrap all the SQL in a transaction
|
---|
| 221 | eval {
|
---|
| 222 | my $sth = $dbh->prepare("select count(*) from masterblocks where cidr <<= '$cidr'");
|
---|
| 223 | $sth->execute;
|
---|
| 224 | my @data = $sth->fetchrow_array;
|
---|
| 225 |
|
---|
| 226 | if ($data[0] eq 0) {
|
---|
| 227 | # First case - master is brand-spanking-new.
|
---|
| 228 | ##fixme: rwhois should be globally-flagable somewhere, much like a number of other things
|
---|
| 229 | ## maybe a db table called "config"?
|
---|
| 230 | $sth = $dbh->prepare("insert into masterblocks (cidr,rwhois) values ('$cidr','y')");
|
---|
| 231 | $sth->execute;
|
---|
| 232 |
|
---|
| 233 | # Unrouted blocks aren't associated with a city (yet). We don't rely on this
|
---|
| 234 | # elsewhere though; legacy data may have traps and pitfalls in it to break this.
|
---|
| 235 | # Thus the "routed" flag.
|
---|
| 236 |
|
---|
| 237 | $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
|
---|
| 238 | " values ('$cidr',".$cidr->masklen.",'<NULL>','n')");
|
---|
| 239 | $sth->execute;
|
---|
| 240 |
|
---|
| 241 | # If we get here, everything is happy. Commit changes.
|
---|
| 242 | $dbh->commit;
|
---|
| 243 |
|
---|
| 244 | } # new master does not contain existing master(s)
|
---|
| 245 | else {
|
---|
| 246 |
|
---|
| 247 | # collect the master(s) we're going to absorb, and snag the longest netmask while we're at it.
|
---|
| 248 | my $smallmask = $cidr->masklen;
|
---|
| 249 | $sth = $dbh->prepare("select cidr as mask from masterblocks where cidr <<= '$cidr'");
|
---|
| 250 | $sth->execute;
|
---|
| 251 | my @cmasters;
|
---|
| 252 | while (my @data = $sth->fetchrow_array) {
|
---|
| 253 | my $master = new NetAddr::IP $data[0];
|
---|
| 254 | push @cmasters, $master;
|
---|
| 255 | $smallmask = $master->masklen if $master->masklen > $smallmask;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | # split the new master, and keep only those blocks not part of an existing master
|
---|
| 259 | my @blocklist;
|
---|
| 260 | foreach my $seg ($cidr->split($smallmask)) {
|
---|
| 261 | my $contained = 0;
|
---|
| 262 | foreach my $master (@cmasters) {
|
---|
| 263 | $contained = 1 if $master->contains($seg);
|
---|
| 264 | }
|
---|
| 265 | push @blocklist, $seg if !$contained;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | # collect the unrouted free blocks within the new master
|
---|
| 269 | $sth = $dbh->prepare("select cidr from freeblocks where ".
|
---|
| 270 | "maskbits>=$smallmask and cidr <<= '$cidr' and routed='n'");
|
---|
| 271 | $sth->execute;
|
---|
| 272 | while (my @data = $sth->fetchrow_array) {
|
---|
| 273 | my $freeblock = new NetAddr::IP $data[0];
|
---|
| 274 | push @blocklist, $freeblock;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | # combine the set of free blocks we should have now.
|
---|
| 278 | @blocklist = Compact(@blocklist);
|
---|
| 279 |
|
---|
| 280 | # and now insert the new data. Make sure to delete old masters too.
|
---|
| 281 |
|
---|
| 282 | # freeblocks
|
---|
| 283 | $sth = $dbh->prepare("delete from freeblocks where cidr <<= ?");
|
---|
| 284 | my $sth2 = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed) values (?,?,'<NULL>','n')");
|
---|
| 285 | foreach my $newblock (@blocklist) {
|
---|
| 286 | $sth->execute("$newblock");
|
---|
| 287 | $sth2->execute("$newblock", $newblock->masklen);
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | # master
|
---|
| 291 | $sth = $dbh->prepare("delete from masterblocks where cidr <<= '$cidr'");
|
---|
| 292 | $sth->execute;
|
---|
| 293 | $sth = $dbh->prepare("insert into masterblocks (cidr,rwhois) values ('$cidr','y')");
|
---|
| 294 | $sth->execute;
|
---|
| 295 |
|
---|
| 296 | # *whew* If we got here, we likely suceeded.
|
---|
| 297 | $dbh->commit;
|
---|
| 298 | } # new master contained existing master(s)
|
---|
| 299 | }; # end eval
|
---|
| 300 |
|
---|
| 301 | if ($@) {
|
---|
| 302 | my $msg = $@;
|
---|
| 303 | eval { $dbh->rollback; };
|
---|
| 304 | return ('FAIL',$msg);
|
---|
| 305 | } else {
|
---|
| 306 | return ('OK','OK');
|
---|
| 307 | }
|
---|
| 308 | } # end addMaster
|
---|
| 309 |
|
---|
| 310 |
|
---|
[125] | 311 | ## IPDB::allocateBlock()
|
---|
[71] | 312 | # Does all of the magic of actually allocating a netblock
|
---|
[125] | 313 | # Requires database handle, block to allocate, custid, type, city,
|
---|
[286] | 314 | # description, notes, circuit ID, block to allocate from, private data
|
---|
[125] | 315 | # Returns a success code and optional error message.
|
---|
| 316 | sub allocateBlock {
|
---|
[394] | 317 | my ($dbh,undef,undef,$custid,$type,$city,$desc,$notes,$circid,$privdata,$nodeid) = @_;
|
---|
[508] | 318 | $privdata = '' if !defined($privdata);
|
---|
[286] | 319 |
|
---|
[125] | 320 | my $cidr = new NetAddr::IP $_[1];
|
---|
| 321 | my $alloc_from = new NetAddr::IP $_[2];
|
---|
| 322 | my $sth;
|
---|
[71] | 323 |
|
---|
[336] | 324 | # Snag the "type" of the freeblock (alloc_from) "just in case"
|
---|
| 325 | $sth = $dbh->prepare("select routed from freeblocks where cidr='$alloc_from'");
|
---|
| 326 | $sth->execute;
|
---|
| 327 | my ($alloc_from_type) = $sth->fetchrow_array;
|
---|
| 328 |
|
---|
[125] | 329 | # To contain the error message, if any.
|
---|
| 330 | my $msg = "Unknown error allocating $cidr as '$type'";
|
---|
[71] | 331 |
|
---|
[125] | 332 | # Enable transactions and error handling
|
---|
| 333 | local $dbh->{AutoCommit} = 0; # These need to be local so we don't
|
---|
| 334 | local $dbh->{RaiseError} = 1; # step on our toes by accident.
|
---|
| 335 |
|
---|
[159] | 336 | if ($type =~ /^.i$/) {
|
---|
[125] | 337 | $msg = "Unable to assign static IP $cidr to $custid";
|
---|
| 338 | eval {
|
---|
[159] | 339 | # We have to do this in two parts because otherwise we lose
|
---|
| 340 | # the ability to return the IP assigned. Should that change,
|
---|
| 341 | # the commented SQL statement below may become usable.
|
---|
[125] | 342 | # update poolips set custid='$custid',city='$city',available='n',
|
---|
| 343 | # description='$desc',notes='$notes',circuitid='$circid'
|
---|
| 344 | # where ip=(select ip from poolips where pool='$alloc_from'
|
---|
| 345 | # and available='y' order by ip limit 1);
|
---|
[159] | 346 |
|
---|
[550] | 347 | # If no specific IP was requested, pick the next available one from the pool.
|
---|
| 348 | if (!$cidr) {
|
---|
| 349 | $sth = $dbh->prepare("select ip from poolips where pool='$alloc_from'".
|
---|
| 350 | " and available='y' order by ip");
|
---|
| 351 | $sth->execute;
|
---|
[159] | 352 |
|
---|
[550] | 353 | my @data = $sth->fetchrow_array;
|
---|
| 354 | $cidr = $data[0]; # $cidr is already declared when we get here!
|
---|
| 355 | }
|
---|
[125] | 356 |
|
---|
| 357 | $sth = $dbh->prepare("update poolips set custid='$custid',".
|
---|
| 358 | "city='$city',available='n',description='$desc',notes='$notes',".
|
---|
[286] | 359 | "circuitid='$circid',privdata='$privdata'".
|
---|
[125] | 360 | " where ip='$cidr'");
|
---|
| 361 | $sth->execute;
|
---|
[394] | 362 | # node hack
|
---|
| 363 | if ($nodeid && $nodeid ne '') {
|
---|
| 364 | $sth = $dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)");
|
---|
| 365 | $sth->execute("$cidr",$nodeid);
|
---|
| 366 | }
|
---|
| 367 | # end node hack
|
---|
[125] | 368 | $dbh->commit;
|
---|
| 369 | };
|
---|
| 370 | if ($@) {
|
---|
| 371 | $msg .= ": '".$sth->errstr."'";
|
---|
| 372 | eval { $dbh->rollback; };
|
---|
| 373 | return ('FAIL',$msg);
|
---|
| 374 | } else {
|
---|
| 375 | return ('OK',"$cidr");
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | } else { # end IP-from-pool allocation
|
---|
| 379 |
|
---|
[549] | 380 | my $errcode = 'OK';
|
---|
[125] | 381 | if ($cidr == $alloc_from) {
|
---|
| 382 | # Easiest case- insert in one table, delete in the other, and go home. More or less.
|
---|
| 383 | # insert into allocations values (cidr,custid,type,city,desc) and
|
---|
| 384 | # delete from freeblocks where cidr='cidr'
|
---|
| 385 | # For data safety on non-transaction DBs, we delete first.
|
---|
| 386 |
|
---|
| 387 | eval {
|
---|
| 388 | $msg = "Unable to allocate $cidr as '$disp_alloctypes{$type}'";
|
---|
[192] | 389 | if ($type eq 'rm') {
|
---|
[125] | 390 | $sth = $dbh->prepare("update freeblocks set routed='y',city='$city'".
|
---|
| 391 | " where cidr='$cidr'");
|
---|
| 392 | $sth->execute;
|
---|
[159] | 393 | $sth = $dbh->prepare("insert into routed (cidr,maskbits,city)".
|
---|
| 394 | " values ('$cidr',".$cidr->masklen.",'$city')");
|
---|
[125] | 395 | $sth->execute;
|
---|
| 396 | } else {
|
---|
| 397 | # common stuff for end-use, dialup, dynDSL, pools, etc, etc.
|
---|
| 398 |
|
---|
[192] | 399 | # special case - block is a container/"reserve" block
|
---|
| 400 | if ($type =~ /^(.)c$/) {
|
---|
| 401 | $sth = $dbh->prepare("update freeblocks set routed='$1' where cidr='$cidr'");
|
---|
| 402 | $sth->execute;
|
---|
| 403 | } else {
|
---|
| 404 | # "normal" case
|
---|
| 405 | $sth = $dbh->prepare("delete from freeblocks where cidr='$cidr'");
|
---|
| 406 | $sth->execute;
|
---|
| 407 | }
|
---|
[159] | 408 | $sth = $dbh->prepare("insert into allocations".
|
---|
[286] | 409 | " (cidr,custid,type,city,description,notes,maskbits,circuitid,privdata)".
|
---|
[508] | 410 | " values ('$cidr','$custid','$type','$city',?,?,".
|
---|
| 411 | $cidr->masklen.",?,?)");
|
---|
| 412 | $sth->execute($desc,$notes,$circid,$privdata);
|
---|
[125] | 413 |
|
---|
| 414 | # And initialize the pool, if necessary
|
---|
[159] | 415 | # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
|
---|
| 416 | # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
|
---|
[125] | 417 | if ($type =~ /^.p$/) {
|
---|
[159] | 418 | $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
|
---|
| 419 | my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"all");
|
---|
| 420 | die $rmsg if $code eq 'FAIL';
|
---|
[549] | 421 | $msg = $rmsg;
|
---|
| 422 | $errcode = $code;
|
---|
[159] | 423 | } elsif ($type =~ /^.d$/) {
|
---|
| 424 | $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
|
---|
| 425 | my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"normal");
|
---|
| 426 | die $rmsg if $code eq 'FAIL';
|
---|
[549] | 427 | $msg = $rmsg;
|
---|
| 428 | $errcode = $code;
|
---|
[125] | 429 | }
|
---|
| 430 |
|
---|
| 431 | } # routing vs non-routing netblock
|
---|
| 432 |
|
---|
[394] | 433 | # node hack
|
---|
| 434 | if ($nodeid && $nodeid ne '') {
|
---|
| 435 | $sth = $dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)");
|
---|
| 436 | $sth->execute("$cidr",$nodeid);
|
---|
| 437 | }
|
---|
| 438 | # end node hack
|
---|
[125] | 439 | $dbh->commit;
|
---|
| 440 | }; # end of eval
|
---|
| 441 | if ($@) {
|
---|
[159] | 442 | $msg .= ": ".$@;
|
---|
[125] | 443 | eval { $dbh->rollback; };
|
---|
[159] | 444 | return ('FAIL',$msg);
|
---|
[125] | 445 | } else {
|
---|
[549] | 446 | return ($errcode,($type =~ /^.[pd]$/ ? $msg : "OK"));
|
---|
[125] | 447 | }
|
---|
| 448 |
|
---|
| 449 | } else { # cidr != alloc_from
|
---|
| 450 |
|
---|
| 451 | # Hard case. Allocation is smaller than free block.
|
---|
| 452 | my $wantmaskbits = $cidr->masklen;
|
---|
| 453 | my $maskbits = $alloc_from->masklen;
|
---|
| 454 |
|
---|
| 455 | my @newfreeblocks; # Holds free blocks generated from splitting the source freeblock.
|
---|
| 456 |
|
---|
| 457 | # This determines which blocks will be left "free" after allocation. We take the
|
---|
| 458 | # block we're allocating from, and split it in half. We see which half the wanted
|
---|
| 459 | # block is in, and repeat until the wanted block is equal to one of the halves.
|
---|
| 460 | my $i=0;
|
---|
| 461 | my $tmp_from = $alloc_from; # So we don't munge $alloc_from
|
---|
| 462 | while ($maskbits++ < $wantmaskbits) {
|
---|
| 463 | my @subblocks = $tmp_from->split($maskbits);
|
---|
| 464 | $newfreeblocks[$i++] = (($cidr->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
|
---|
| 465 | $tmp_from = ( ($cidr->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
|
---|
| 466 | } # while
|
---|
| 467 |
|
---|
| 468 | # Begin SQL transaction block
|
---|
| 469 | eval {
|
---|
| 470 | $msg = "Unable to allocate $cidr as '$disp_alloctypes{$type}'";
|
---|
| 471 |
|
---|
| 472 | # Delete old freeblocks entry
|
---|
| 473 | $sth = $dbh->prepare("delete from freeblocks where cidr='$alloc_from'");
|
---|
| 474 | $sth->execute();
|
---|
| 475 |
|
---|
| 476 | # now we have to do some magic for routing blocks
|
---|
[192] | 477 | if ($type eq 'rm') {
|
---|
[125] | 478 |
|
---|
| 479 | # Insert the new freeblocks entries
|
---|
| 480 | # Note that non-routed blocks are assigned to <NULL>
|
---|
[159] | 481 | # and use the default value for the routed column ('n')
|
---|
| 482 | $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
|
---|
| 483 | " values (?, ?, '<NULL>')");
|
---|
[125] | 484 | foreach my $block (@newfreeblocks) {
|
---|
| 485 | $sth->execute("$block", $block->masklen);
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | # Insert the entry in the routed table
|
---|
[159] | 489 | $sth = $dbh->prepare("insert into routed (cidr,maskbits,city)".
|
---|
| 490 | " values ('$cidr',".$cidr->masklen.",'$city')");
|
---|
[125] | 491 | $sth->execute;
|
---|
| 492 | # Insert the (almost) same entry in the freeblocks table
|
---|
[159] | 493 | $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
|
---|
| 494 | " values ('$cidr',".$cidr->masklen.",'$city','y')");
|
---|
[125] | 495 | $sth->execute;
|
---|
| 496 |
|
---|
[192] | 497 | } else { # done with alloctype == rm
|
---|
[125] | 498 |
|
---|
| 499 | # Insert the new freeblocks entries
|
---|
[336] | 500 | # Along with some more HairyPerl(TM):
|
---|
| 501 | # if $alloc_type_from is p
|
---|
| 502 | # OR
|
---|
| 503 | # $type matches /^(.)r$/
|
---|
| 504 | # inserted value for routed column should match.
|
---|
| 505 | # This solves the case of inserting an arbitrary block into a
|
---|
| 506 | # "Reserve-for-routed-DSL" block. Which you really shouldn't
|
---|
| 507 | # do in the first place, but anyway...
|
---|
[159] | 508 | $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
|
---|
[192] | 509 | " values (?, ?, (select city from routed where cidr >>= '$cidr'),'".
|
---|
[336] | 510 | ( ( ($alloc_from_type =~ /^(p)$/) || ($type =~ /^(.)r$/) ) ? "$1" : 'y')."')");
|
---|
[125] | 511 | foreach my $block (@newfreeblocks) {
|
---|
| 512 | $sth->execute("$block", $block->masklen);
|
---|
| 513 | }
|
---|
[192] | 514 | # Special-case for reserve/"container" blocks - generate
|
---|
| 515 | # the "extra" freeblocks entry for the container
|
---|
| 516 | if ($type =~ /^(.)c$/) {
|
---|
| 517 | $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
|
---|
| 518 | " values ('$cidr',".$cidr->masklen.",'$city','$1')");
|
---|
| 519 | $sth->execute;
|
---|
| 520 | }
|
---|
[125] | 521 | # Insert the allocations entry
|
---|
[159] | 522 | $sth = $dbh->prepare("insert into allocations (cidr,custid,type,city,".
|
---|
[286] | 523 | "description,notes,maskbits,circuitid,privdata)".
|
---|
[508] | 524 | " values ('$cidr','$custid','$type','$city',?,?,".
|
---|
| 525 | $cidr->masklen.",?,?)");
|
---|
| 526 | $sth->execute($desc,$notes,$circid,$privdata);
|
---|
[125] | 527 |
|
---|
| 528 | # And initialize the pool, if necessary
|
---|
[159] | 529 | # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
|
---|
| 530 | # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
|
---|
[125] | 531 | if ($type =~ /^.p$/) {
|
---|
| 532 | $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
|
---|
[159] | 533 | my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"all");
|
---|
| 534 | die $rmsg if $code eq 'FAIL';
|
---|
| 535 | } elsif ($type =~ /^.d$/) {
|
---|
| 536 | $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
|
---|
| 537 | my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"normal");
|
---|
| 538 | die $rmsg if $code eq 'FAIL';
|
---|
[125] | 539 | }
|
---|
| 540 |
|
---|
[192] | 541 | } # done with netblock alloctype != rm
|
---|
[125] | 542 |
|
---|
[394] | 543 | # node hack
|
---|
| 544 | if ($nodeid && $nodeid ne '') {
|
---|
| 545 | $sth = $dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)");
|
---|
| 546 | $sth->execute("$cidr",$nodeid);
|
---|
| 547 | }
|
---|
| 548 | # end node hack
|
---|
[125] | 549 | $dbh->commit;
|
---|
| 550 | }; # end eval
|
---|
| 551 | if ($@) {
|
---|
[250] | 552 | $msg .= ": ".$@;
|
---|
[125] | 553 | eval { $dbh->rollback; };
|
---|
| 554 | return ('FAIL',$msg);
|
---|
| 555 | } else {
|
---|
[549] | 556 | return ($errcode,($type =~ /^.[pd]$/ ? $msg : "OK"));
|
---|
[125] | 557 | }
|
---|
| 558 |
|
---|
| 559 | } # end fullcidr != alloc_from
|
---|
| 560 |
|
---|
| 561 | } # end static-IP vs netblock allocation
|
---|
| 562 |
|
---|
| 563 | } # end allocateBlock()
|
---|
| 564 |
|
---|
| 565 |
|
---|
| 566 | ## IPDB::initPool()
|
---|
| 567 | # Initializes a pool
|
---|
| 568 | # Requires a database handle, the pool CIDR, type, city, and a parameter
|
---|
| 569 | # indicating whether the pool should allow allocation of literally every
|
---|
| 570 | # IP, or if it should reserve network/gateway/broadcast IPs
|
---|
| 571 | # Note that this is NOT done in a transaction, that's why it's a private
|
---|
| 572 | # function and should ONLY EVER get called from allocateBlock()
|
---|
| 573 | sub initPool {
|
---|
| 574 | my ($dbh,undef,$type,$city,$class) = @_;
|
---|
| 575 | my $pool = new NetAddr::IP $_[1];
|
---|
| 576 |
|
---|
[549] | 577 | return ('WARN','Refusing to melt server with IPv6 IP pool') if $pool->bits == 128;
|
---|
| 578 |
|
---|
[159] | 579 | ##fixme Need to just replace 2nd char of type with i rather than capturing 1st char of type
|
---|
| 580 | $type =~ s/[pd]$/i/;
|
---|
[125] | 581 | my $sth;
|
---|
[159] | 582 | my $msg;
|
---|
[125] | 583 |
|
---|
[159] | 584 | # Trap errors so we can pass them back to the caller. Even if the
|
---|
| 585 | # caller is only ever supposed to be local, and therefore already
|
---|
| 586 | # trapping errors. >:(
|
---|
| 587 | local $dbh->{AutoCommit} = 0; # These need to be local so we don't
|
---|
| 588 | local $dbh->{RaiseError} = 1; # step on our toes by accident.
|
---|
| 589 |
|
---|
| 590 | eval {
|
---|
| 591 | # have to insert all pool IPs into poolips table as "unallocated".
|
---|
| 592 | $sth = $dbh->prepare("insert into poolips (pool,ip,custid,city,type)".
|
---|
[445] | 593 | " values ('$pool', ?, '$defcustid', '$city', '$type')");
|
---|
[159] | 594 | my @poolip_list = $pool->hostenum;
|
---|
| 595 | if ($class eq 'all') { # (DSL-ish block - *all* IPs available
|
---|
[248] | 596 | if ($pool->addr !~ /\.0$/) { # .0 causes weirdness.
|
---|
| 597 | $sth->execute($pool->addr);
|
---|
| 598 | }
|
---|
[159] | 599 | for (my $i=0; $i<=$#poolip_list; $i++) {
|
---|
| 600 | $sth->execute($poolip_list[$i]->addr);
|
---|
| 601 | }
|
---|
| 602 | $pool--;
|
---|
[248] | 603 | if ($pool->addr !~ /\.255$/) { # .255 can cause weirdness.
|
---|
| 604 | $sth->execute($pool->addr);
|
---|
| 605 | }
|
---|
[159] | 606 | } else { # (real netblock)
|
---|
| 607 | for (my $i=1; $i<=$#poolip_list; $i++) {
|
---|
| 608 | $sth->execute($poolip_list[$i]->addr);
|
---|
| 609 | }
|
---|
[125] | 610 | }
|
---|
[159] | 611 | };
|
---|
| 612 | if ($@) {
|
---|
| 613 | $msg = "'".$sth->errstr."'";
|
---|
| 614 | eval { $dbh->rollback; };
|
---|
| 615 | return ('FAIL',$msg);
|
---|
| 616 | } else {
|
---|
| 617 | return ('OK',"OK");
|
---|
[125] | 618 | }
|
---|
| 619 | } # end initPool()
|
---|
| 620 |
|
---|
| 621 |
|
---|
| 622 | ## IPDB::deleteBlock()
|
---|
| 623 | # Removes an allocation from the database, including deleting IPs
|
---|
| 624 | # from poolips and recombining entries in freeblocks if possible
|
---|
| 625 | # Also handles "deleting" a static IP allocation, and removal of a master
|
---|
| 626 | # Requires a database handle, the block to delete, and the type of block
|
---|
| 627 | sub deleteBlock {
|
---|
| 628 | my ($dbh,undef,$type) = @_;
|
---|
| 629 | my $cidr = new NetAddr::IP $_[1];
|
---|
| 630 |
|
---|
| 631 | my $sth;
|
---|
| 632 |
|
---|
[336] | 633 | # Magic variables used for odd allocation cases.
|
---|
| 634 | my $container;
|
---|
| 635 | my $con_type;
|
---|
| 636 |
|
---|
[125] | 637 | # To contain the error message, if any.
|
---|
| 638 | my $msg = "Unknown error deallocating $type $cidr";
|
---|
| 639 | # Enable transactions and exception-on-errors... but only for this sub
|
---|
| 640 | local $dbh->{AutoCommit} = 0;
|
---|
| 641 | local $dbh->{RaiseError} = 1;
|
---|
| 642 |
|
---|
| 643 | # First case. The "block" is a static IP
|
---|
| 644 | # Note that we still need some additional code in the odd case
|
---|
| 645 | # of a netblock-aligned contiguous group of static IPs
|
---|
| 646 | if ($type =~ /^.i$/) {
|
---|
| 647 |
|
---|
| 648 | eval {
|
---|
[159] | 649 | $msg = "Unable to deallocate $disp_alloctypes{$type} $cidr";
|
---|
[445] | 650 | $sth = $dbh->prepare("update poolips set custid='$defcustid',available='y',".
|
---|
[354] | 651 | "city=(select city from allocations where cidr >>= '$cidr'".
|
---|
| 652 | " order by masklen(cidr) desc limit 1),".
|
---|
[125] | 653 | "description='',notes='',circuitid='' where ip='$cidr'");
|
---|
| 654 | $sth->execute;
|
---|
| 655 | $dbh->commit;
|
---|
| 656 | };
|
---|
| 657 | if ($@) {
|
---|
| 658 | eval { $dbh->rollback; };
|
---|
| 659 | return ('FAIL',$msg);
|
---|
| 660 | } else {
|
---|
| 661 | return ('OK',"OK");
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 | } elsif ($type eq 'mm') { # end alloctype =~ /.i/
|
---|
| 665 |
|
---|
| 666 | $msg = "Unable to delete master block $cidr";
|
---|
| 667 | eval {
|
---|
| 668 | $sth = $dbh->prepare("delete from masterblocks where cidr='$cidr'");
|
---|
| 669 | $sth->execute;
|
---|
[365] | 670 | $sth = $dbh->prepare("delete from freeblocks where cidr <<= '$cidr'");
|
---|
[125] | 671 | $sth->execute;
|
---|
| 672 | $dbh->commit;
|
---|
| 673 | };
|
---|
| 674 | if ($@) {
|
---|
| 675 | eval { $dbh->rollback; };
|
---|
| 676 | return ('FAIL', $msg);
|
---|
| 677 | } else {
|
---|
| 678 | return ('OK',"OK");
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | } else { # end alloctype master block case
|
---|
| 682 |
|
---|
| 683 | ## This is a big block; but it HAS to be done in a chunk. Any removal
|
---|
| 684 | ## of a netblock allocation may result in a larger chunk of free
|
---|
| 685 | ## contiguous IP space - which may in turn be combined into a single
|
---|
| 686 | ## netblock rather than a number of smaller netblocks.
|
---|
| 687 |
|
---|
| 688 | eval {
|
---|
| 689 |
|
---|
[192] | 690 | if ($type eq 'rm') {
|
---|
[125] | 691 | $msg = "Unable to remove routing allocation $cidr";
|
---|
| 692 | $sth = $dbh->prepare("delete from routed where cidr='$cidr'");
|
---|
| 693 | $sth->execute;
|
---|
| 694 | # Make sure block getting deleted is properly accounted for.
|
---|
| 695 | $sth = $dbh->prepare("update freeblocks set routed='n',city='<NULL>'".
|
---|
| 696 | " where cidr='$cidr'");
|
---|
| 697 | $sth->execute;
|
---|
| 698 | # Set up query to start compacting free blocks.
|
---|
[159] | 699 | $sth = $dbh->prepare("select cidr from freeblocks where ".
|
---|
[125] | 700 | "maskbits<=".$cidr->masklen." and routed='n' order by maskbits desc");
|
---|
| 701 |
|
---|
| 702 | } else { # end alloctype routing case
|
---|
| 703 |
|
---|
[336] | 704 | # Magic. We need to get information about the containing block (if any)
|
---|
| 705 | # so as to make sure that the freeblocks we insert get the correct "type".
|
---|
| 706 | $sth = $dbh->prepare("select cidr,type from allocations where cidr >> '$cidr'");
|
---|
| 707 | $sth->execute;
|
---|
| 708 | ($container, $con_type) = $sth->fetchrow_array;
|
---|
| 709 |
|
---|
[192] | 710 | # Delete all allocations within the block being deleted. This is
|
---|
| 711 | # deliberate and correct, and removes the need to special-case
|
---|
| 712 | # removal of "container" blocks.
|
---|
| 713 | $sth = $dbh->prepare("delete from allocations where cidr <<='$cidr'");
|
---|
[125] | 714 | $sth->execute;
|
---|
[192] | 715 |
|
---|
[125] | 716 | # Special case - delete pool IPs
|
---|
[159] | 717 | if ($type =~ /^.[pd]$/) {
|
---|
[125] | 718 | # We have to delete the IPs from the pool listing.
|
---|
| 719 | $sth = $dbh->prepare("delete from poolips where pool='$cidr'");
|
---|
| 720 | $sth->execute;
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 | # Set up query for compacting free blocks.
|
---|
[336] | 724 | if ($con_type && $con_type eq 'pc') {
|
---|
| 725 | # Clean up after "bad" allocations (blocks that are not formally
|
---|
| 726 | # contained which have nevertheless been allocated from a container block)
|
---|
| 727 | # We want to make certain that the freeblocks are properly "labelled"
|
---|
[362] | 728 | $sth = $dbh->prepare("select cidr from freeblocks where cidr <<= '$container' order by maskbits desc");
|
---|
[336] | 729 | } else {
|
---|
| 730 | # Standard deallocation.
|
---|
| 731 | $sth = $dbh->prepare("select cidr from freeblocks where cidr <<= ".
|
---|
[125] | 732 | "(select cidr from routed where cidr >>= '$cidr') ".
|
---|
[192] | 733 | " and maskbits<=".$cidr->masklen.
|
---|
[304] | 734 | " and routed='".(($type =~ /^(.)r$/) ? "$1" : 'y').
|
---|
[192] | 735 | "' order by maskbits desc");
|
---|
[336] | 736 | }
|
---|
[125] | 737 |
|
---|
| 738 | } # end alloctype general case
|
---|
| 739 |
|
---|
[445] | 740 | ## Deallocate legacy blocks stashed in the middle of a static IP pool
|
---|
| 741 | ## This may be expandable to an even more general case of contained netblock, or other pool types.
|
---|
[126] | 742 |
|
---|
[445] | 743 | # Find out if the block we're deallocating is within a DSL pool
|
---|
| 744 | my $sth2 = $dbh->prepare("SELECT cidr,city,type FROM allocations WHERE type LIKE '_p' AND cidr >>= ?");
|
---|
| 745 | $sth2->execute("$cidr");
|
---|
| 746 | my ($pool,$poolcity,$pooltype) = $sth2->fetchrow_array;
|
---|
[126] | 747 |
|
---|
[445] | 748 | if ($pool || $sth2->rows) {
|
---|
| 749 | # We've already deleted the block, now we have to stuff its IPs into the pool.
|
---|
| 750 | $pooltype =~ s/p$/i/; # change type to static IP
|
---|
| 751 | $sth2 = $dbh->prepare("INSERT INTO poolips (pool,ip,city,type,custid) values ".
|
---|
| 752 | "('$pool',?,'$poolcity','$pooltype','$defcustid')");
|
---|
| 753 | ##fixme: need to not insert net, gateway, and bcast on "real netblock" pools (DHCPish)
|
---|
| 754 | # don't insert .0
|
---|
| 755 | $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|;
|
---|
| 756 | foreach my $ip ($cidr->hostenum) {
|
---|
| 757 | $sth2->execute("$ip");
|
---|
| 758 | }
|
---|
| 759 | $cidr--;
|
---|
| 760 | # don't insert .255
|
---|
| 761 | $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.255$|;
|
---|
| 762 | } else { # done returning IPs from a block to a static DSL pool
|
---|
[125] | 763 |
|
---|
[445] | 764 | # Now we look for larger-or-equal-sized free blocks in the same master (routed)
|
---|
| 765 | # (super)block. If there aren't any, we can't combine blocks anyway. If there
|
---|
| 766 | # are, we check to see if we can combine blocks.
|
---|
| 767 | # Execute the statement prepared in the if-else above.
|
---|
[125] | 768 |
|
---|
[445] | 769 | $sth->execute;
|
---|
| 770 |
|
---|
[125] | 771 | # NetAddr::IP->compact() attempts to produce the smallest inclusive block
|
---|
| 772 | # from the caller and the passed terms.
|
---|
| 773 | # EG: if you call $cidr->compact($ip1,$ip2,$ip3) when $cidr, $ip1, $ip2,
|
---|
| 774 | # and $ip3 are consecutive /27's starting on .0 (.0-.31, .32-.63,
|
---|
| 775 | # .64-.95, and .96-.128), you will get an array containing a single
|
---|
| 776 | # /25 as element 0 (.0-.127). Order is not important; you could have
|
---|
| 777 | # $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
|
---|
| 778 |
|
---|
[445] | 779 | my (@together, @combinelist);
|
---|
| 780 | my $i=0;
|
---|
| 781 | while (my @data = $sth->fetchrow_array) {
|
---|
| 782 | my $testIP = new NetAddr::IP $data[0];
|
---|
| 783 | @together = $testIP->compact($cidr);
|
---|
| 784 | my $num = @together;
|
---|
| 785 | if ($num == 1) {
|
---|
| 786 | $cidr = $together[0];
|
---|
| 787 | $combinelist[$i++] = $testIP;
|
---|
| 788 | }
|
---|
[125] | 789 | }
|
---|
| 790 |
|
---|
[445] | 791 | # Clear old freeblocks entries - if any. They should all be within
|
---|
| 792 | # the $cidr determined above.
|
---|
| 793 | $sth = $dbh->prepare("delete from freeblocks where cidr <<='$cidr'");
|
---|
| 794 | $sth->execute;
|
---|
[125] | 795 |
|
---|
[445] | 796 | # insert "new" freeblocks entry
|
---|
| 797 | if ($type eq 'rm') {
|
---|
| 798 | $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
|
---|
[159] | 799 | " values ('$cidr',".$cidr->masklen.",'<NULL>')");
|
---|
[445] | 800 | } else {
|
---|
| 801 | # Magic hackery to insert "correct" data for deallocation of
|
---|
| 802 | # non-contained blocks allocated from within a container.
|
---|
| 803 | $type = 'pr' if $con_type && $con_type eq 'pc';
|
---|
[336] | 804 |
|
---|
[445] | 805 | $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
|
---|
[159] | 806 | " values ('$cidr',".$cidr->masklen.
|
---|
[192] | 807 | ",(select city from routed where cidr >>= '$cidr'),'".
|
---|
| 808 | (($type =~ /^(.)r$/) ? "$1" : 'y')."')");
|
---|
[445] | 809 | }
|
---|
| 810 | $sth->execute;
|
---|
[125] | 811 |
|
---|
[445] | 812 | } # done returning IPs to the appropriate place
|
---|
[126] | 813 |
|
---|
[125] | 814 | # If we got here, we've succeeded. Whew!
|
---|
| 815 | $dbh->commit;
|
---|
| 816 | }; # end eval
|
---|
| 817 | if ($@) {
|
---|
[445] | 818 | $msg = $@;
|
---|
[125] | 819 | eval { $dbh->rollback; };
|
---|
| 820 | return ('FAIL', $msg);
|
---|
| 821 | } else {
|
---|
| 822 | return ('OK',"OK");
|
---|
| 823 | }
|
---|
| 824 |
|
---|
| 825 | } # end alloctype != netblock
|
---|
| 826 |
|
---|
| 827 | } # end deleteBlock()
|
---|
| 828 |
|
---|
| 829 |
|
---|
[356] | 830 | ## IPDB::getBlockData()
|
---|
| 831 | # Return custid, type, city, and description for a block
|
---|
| 832 | sub getBlockData {
|
---|
| 833 | my $dbh = shift;
|
---|
| 834 | my $block = shift;
|
---|
| 835 |
|
---|
| 836 | my $sth = $dbh->prepare("select cidr,custid,type,city,description from searchme".
|
---|
| 837 | " where cidr='$block'");
|
---|
| 838 | $sth->execute();
|
---|
| 839 | return $sth->fetchrow_array();
|
---|
| 840 | } # end getBlockData()
|
---|
| 841 |
|
---|
| 842 |
|
---|
[125] | 843 | ## IPDB::mailNotify()
|
---|
[71] | 844 | # Sends notification mail to recipients regarding an IPDB operation
|
---|
[445] | 845 | sub mailNotify {
|
---|
| 846 | my $dbh = shift;
|
---|
| 847 | my ($action,$subj,$message) = @_;
|
---|
[71] | 848 |
|
---|
[509] | 849 | return if $domain eq 'example.com'; # no point being obnoxious if we're still stuck with defaults.
|
---|
| 850 |
|
---|
[445] | 851 | ##fixme: need to redesign the breakdown/processing for $action for proper handling of all cases
|
---|
| 852 |
|
---|
| 853 | # split action into parts for fiddlement. nb: there are almost certainly better ways to do this.
|
---|
| 854 | my @actionbits = split //, $action;
|
---|
| 855 |
|
---|
| 856 | # want to notify anyone who has specifically requested notify on *this* type ($action as passed),
|
---|
| 857 | # on "all static IP types" or "all pool types" (and other last-char-in-type groupings), on eg "all DSL types",
|
---|
| 858 | # and "all events with this action"
|
---|
| 859 | my @actionsets = ($action);
|
---|
| 860 | ##fixme: ick, eww. really gotta find a better way to handle this...
|
---|
| 861 | push @actionsets, ($actionbits[0].'.'.$actionbits[2],
|
---|
| 862 | $actionbits[0].$actionbits[1].'.', $actionbits[0].'a') if $action =~ /^.{3}$/;
|
---|
| 863 |
|
---|
| 864 | my $mailer = Net::SMTP->new($smtphost, Hello => "ipdb.$domain");
|
---|
| 865 |
|
---|
| 866 | # get recip list from db
|
---|
| 867 | my $sth = $dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
|
---|
| 868 |
|
---|
| 869 | my %reciplist;
|
---|
| 870 | foreach (@actionsets) {
|
---|
| 871 | $sth->execute($_);
|
---|
| 872 | ##fixme - need to handle db errors
|
---|
| 873 | my ($recipsub) = $sth->fetchrow_array;
|
---|
| 874 | next if !$recipsub;
|
---|
| 875 | foreach (split(/,/, $recipsub)) {
|
---|
| 876 | $reciplist{$_}++;
|
---|
| 877 | }
|
---|
| 878 | }
|
---|
| 879 |
|
---|
| 880 | return if !%reciplist;
|
---|
| 881 |
|
---|
| 882 | foreach my $recip (keys %reciplist) {
|
---|
| 883 | $mailer->mail("ipdb\@$domain");
|
---|
| 884 | $mailer->to($recip);
|
---|
| 885 | $mailer->data("From: \"$org_name IP Database\" <ipdb\@$domain>\n",
|
---|
[131] | 886 | "To: $recip\n",
|
---|
[71] | 887 | "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
|
---|
| 888 | "Subject: {IPDB} $subj\n",
|
---|
| 889 | "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n",
|
---|
[445] | 890 | "Organization: $org_name\n",
|
---|
[71] | 891 | "\n$message\n");
|
---|
[445] | 892 | }
|
---|
[71] | 893 | $mailer->quit;
|
---|
| 894 | }
|
---|
| 895 |
|
---|
[4] | 896 | # Indicates module loaded OK. Required by Perl.
|
---|
| 897 | 1;
|
---|