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