[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: 2013-01-04 22:19:57 +0000 (Fri, 04 Jan 2013) $
|
---|
| 6 | # SVN revision $Rev: 582 $
|
---|
| 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;
|
---|
[573] | 18 | use NetAddr::IP qw(:lower Compact );
|
---|
[582] | 19 | use Frontier::Client;
|
---|
[68] | 20 | use POSIX;
|
---|
[4] | 21 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
|
---|
| 22 |
|
---|
[417] | 23 | $VERSION = 2; ##VERSION##
|
---|
[4] | 24 | @ISA = qw(Exporter);
|
---|
[106] | 25 | @EXPORT_OK = qw(
|
---|
[541] | 26 | %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist
|
---|
| 27 | %IPDBacl %aclmsg
|
---|
[523] | 28 | &initIPDBGlobals &connectDB &finish &checkDBSanity
|
---|
[547] | 29 | &addMaster &touchMaster
|
---|
[561] | 30 | &listSummary &listSubs &listFree &listPool
|
---|
[541] | 31 | &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
|
---|
[536] | 32 | &ipParent &subParent &blockParent &getRoutedCity
|
---|
[531] | 33 | &allocateBlock &updateBlock &deleteBlock &getBlockData
|
---|
[530] | 34 | &getNodeList &getNodeName &getNodeInfo
|
---|
[519] | 35 | &mailNotify
|
---|
[106] | 36 | );
|
---|
[4] | 37 |
|
---|
| 38 | @EXPORT = (); # Export nothing by default.
|
---|
[106] | 39 | %EXPORT_TAGS = ( ALL => [qw(
|
---|
[167] | 40 | %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist
|
---|
[541] | 41 | %IPDBacl %aclmsg
|
---|
[523] | 42 | &initIPDBGlobals &connectDB &finish &checkDBSanity
|
---|
[547] | 43 | &addMaster &touchMaster
|
---|
[561] | 44 | &listSummary &listSubs &listFree &listPool
|
---|
[541] | 45 | &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
|
---|
[536] | 46 | &ipParent &subParent &blockParent &getRoutedCity
|
---|
[531] | 47 | &allocateBlock &updateBlock &deleteBlock &getBlockData
|
---|
[530] | 48 | &getNodeList &getNodeName &getNodeInfo
|
---|
[519] | 49 | &mailNotify
|
---|
[106] | 50 | )]
|
---|
| 51 | );
|
---|
[4] | 52 |
|
---|
[77] | 53 | ##
|
---|
| 54 | ## Global variables
|
---|
| 55 | ##
|
---|
| 56 | our %disp_alloctypes;
|
---|
| 57 | our %list_alloctypes;
|
---|
[167] | 58 | our %def_custids;
|
---|
[96] | 59 | our @citylist;
|
---|
| 60 | our @poplist;
|
---|
[233] | 61 | our %IPDBacl;
|
---|
[66] | 62 |
|
---|
[517] | 63 | # mapping table for functional-area => error message
|
---|
| 64 | our %aclmsg = (
|
---|
| 65 | addmaster => 'add a master block',
|
---|
| 66 | addblock => 'add an allocation',
|
---|
| 67 | updateblock => 'update a block',
|
---|
| 68 | delblock => 'delete an allocation',
|
---|
| 69 | );
|
---|
| 70 |
|
---|
[417] | 71 | our $org_name = 'Example Corp';
|
---|
[416] | 72 | our $smtphost = 'smtp.example.com';
|
---|
| 73 | our $domain = 'example.com';
|
---|
[417] | 74 | our $defcustid = '5554242';
|
---|
| 75 | # mostly for rwhois
|
---|
| 76 | ##fixme: leave these blank by default?
|
---|
[420] | 77 | our $rwhoisDataPath = '/usr/local/rwhoisd/etc/rwhoisd'; # to match ./configure defaults from rwhoisd-1.5.9.6
|
---|
[417] | 78 | our $org_street = '123 4th Street';
|
---|
| 79 | our $org_city = 'Anytown';
|
---|
| 80 | our $org_prov_state = 'ON';
|
---|
| 81 | our $org_pocode = 'H0H 0H0';
|
---|
| 82 | our $org_country = 'CA';
|
---|
| 83 | our $org_phone = '000-555-1234';
|
---|
| 84 | our $org_techhandle = 'ISP-ARIN-HANDLE';
|
---|
[434] | 85 | our $org_email = 'noc@example.com';
|
---|
[437] | 86 | our $hostmaster = 'dns@example.com';
|
---|
[416] | 87 |
|
---|
[417] | 88 | our $syslog_facility = 'local2';
|
---|
| 89 |
|
---|
[582] | 90 | our $rpc_url = '';
|
---|
| 91 |
|
---|
[77] | 92 | # Let's initialize the globals.
|
---|
| 93 | ## IPDB::initIPDBGlobals()
|
---|
| 94 | # Initialize all globals. Takes a database handle, returns a success or error code
|
---|
| 95 | sub initIPDBGlobals {
|
---|
| 96 | my $dbh = $_[0];
|
---|
| 97 | my $sth;
|
---|
| 98 |
|
---|
[106] | 99 | # Initialize alloctypes hashes
|
---|
[167] | 100 | $sth = $dbh->prepare("select type,listname,dispname,listorder,def_custid from alloctypes order by listorder");
|
---|
[77] | 101 | $sth->execute;
|
---|
| 102 | while (my @data = $sth->fetchrow_array) {
|
---|
[106] | 103 | $disp_alloctypes{$data[0]} = $data[2];
|
---|
[167] | 104 | $def_custids{$data[0]} = $data[4];
|
---|
[106] | 105 | if ($data[3] < 900) {
|
---|
| 106 | $list_alloctypes{$data[0]} = $data[1];
|
---|
| 107 | }
|
---|
[77] | 108 | }
|
---|
[96] | 109 |
|
---|
| 110 | # City and POP listings
|
---|
[157] | 111 | $sth = $dbh->prepare("select city,routing from cities order by city");
|
---|
[96] | 112 | $sth->execute;
|
---|
| 113 | return (undef,$sth->errstr) if $sth->err;
|
---|
| 114 | while (my @data = $sth->fetchrow_array) {
|
---|
[106] | 115 | push @citylist, $data[0];
|
---|
[96] | 116 | if ($data[1] eq 'y') {
|
---|
[106] | 117 | push @poplist, $data[0];
|
---|
[96] | 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[233] | 121 | # Load ACL data. Specific username checks are done at a different level.
|
---|
| 122 | $sth = $dbh->prepare("select username,acl from users");
|
---|
| 123 | $sth->execute;
|
---|
[106] | 124 | return (undef,$sth->errstr) if $sth->err;
|
---|
[233] | 125 | while (my @data = $sth->fetchrow_array) {
|
---|
| 126 | $IPDBacl{$data[0]} = $data[1];
|
---|
| 127 | }
|
---|
[106] | 128 |
|
---|
[517] | 129 | ##fixme: initialize HTML::Template env var for template path
|
---|
| 130 | # something like $self->path().'/templates' ?
|
---|
| 131 | # $ENV{HTML_TEMPLATE_ROOT} = 'foo/bar';
|
---|
| 132 |
|
---|
[77] | 133 | return (1,"OK");
|
---|
| 134 | } # end initIPDBGlobals
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | ## IPDB::connectDB()
|
---|
[4] | 138 | # Creates connection to IPDB.
|
---|
[77] | 139 | # Requires the database name, username, and password.
|
---|
[4] | 140 | # Returns a handle to the db.
|
---|
[77] | 141 | # Set up for a PostgreSQL db; could be any transactional DBMS with the
|
---|
| 142 | # right changes.
|
---|
[4] | 143 | sub connectDB {
|
---|
[432] | 144 | my $dbname = shift;
|
---|
| 145 | my $user = shift;
|
---|
| 146 | my $pass = shift;
|
---|
| 147 | my $dbhost = shift;
|
---|
| 148 |
|
---|
[4] | 149 | my $dbh;
|
---|
[432] | 150 | my $DSN = "DBI:Pg:".($dbhost ? "host=$dbhost;" : '')."dbname=$dbname";
|
---|
[4] | 151 |
|
---|
| 152 | # Note that we want to autocommit by default, and we will turn it off locally as necessary.
|
---|
[77] | 153 | # We may not want to print gobbledygook errors; YMMV. Have to ponder that further.
|
---|
| 154 | $dbh = DBI->connect($DSN, $user, $pass, {
|
---|
| 155 | AutoCommit => 1,
|
---|
| 156 | PrintError => 0
|
---|
| 157 | })
|
---|
| 158 | or return (undef, $DBI::errstr) if(!$dbh);
|
---|
[4] | 159 |
|
---|
[77] | 160 | # Return here if we can't select. Note that this indicates a
|
---|
| 161 | # problem executing the select.
|
---|
[183] | 162 | my $sth = $dbh->prepare("select type from alloctypes");
|
---|
[77] | 163 | $sth->execute();
|
---|
| 164 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 165 |
|
---|
| 166 | # See if the select returned anything (or null data). This should
|
---|
| 167 | # succeed if the select executed, but...
|
---|
| 168 | $sth->fetchrow();
|
---|
| 169 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 170 |
|
---|
| 171 | # If we get here, we should be OK.
|
---|
| 172 | return ($dbh,"DB connection OK");
|
---|
[4] | 173 | } # end connectDB
|
---|
| 174 |
|
---|
[77] | 175 |
|
---|
| 176 | ## IPDB::finish()
|
---|
| 177 | # Cleans up after database handles and so on.
|
---|
| 178 | # Requires a database handle
|
---|
| 179 | sub finish {
|
---|
| 180 | my $dbh = $_[0];
|
---|
[517] | 181 | $dbh->disconnect if $dbh;
|
---|
[77] | 182 | } # end finish
|
---|
| 183 |
|
---|
| 184 |
|
---|
[106] | 185 | ## IPDB::checkDBSanity()
|
---|
[4] | 186 | # Quick check to see if the db is responding. A full integrity
|
---|
| 187 | # check will have to be a separate tool to walk the IP allocation trees.
|
---|
| 188 | sub checkDBSanity {
|
---|
[106] | 189 | my ($dbh) = $_[0];
|
---|
[4] | 190 |
|
---|
| 191 | if (!$dbh) {
|
---|
[106] | 192 | print "No database handle, or connection has been closed.";
|
---|
| 193 | return -1;
|
---|
[4] | 194 | } else {
|
---|
| 195 | # it connects, try a stmt.
|
---|
[184] | 196 | my $sth = $dbh->prepare("select type from alloctypes");
|
---|
[4] | 197 | my $err = $sth->execute();
|
---|
| 198 |
|
---|
| 199 | if ($sth->fetchrow()) {
|
---|
| 200 | # all is well.
|
---|
| 201 | return 1;
|
---|
| 202 | } else {
|
---|
[16] | 203 | print "Connected to the database, but could not execute test statement. ".$sth->errstr();
|
---|
[106] | 204 | return -1;
|
---|
[4] | 205 | }
|
---|
| 206 | }
|
---|
| 207 | # Clean up after ourselves.
|
---|
[106] | 208 | # $dbh->disconnect;
|
---|
[4] | 209 | } # end checkDBSanity
|
---|
| 210 |
|
---|
[66] | 211 |
|
---|
[371] | 212 | ## IPDB::addMaster()
|
---|
| 213 | # Does all the magic necessary to sucessfully add a master block
|
---|
| 214 | # Requires database handle, block to add
|
---|
| 215 | # Returns failure code and error message or success code and "message"
|
---|
| 216 | sub addMaster {
|
---|
| 217 | my $dbh = shift;
|
---|
| 218 | my $cidr = new NetAddr::IP shift;
|
---|
[582] | 219 | my %args = @_;
|
---|
[371] | 220 |
|
---|
[582] | 221 | $args{vrf} = '' if !$args{vrf};
|
---|
| 222 | $args{rdns} = '' if !$args{rdns};
|
---|
| 223 | $args{defloc} = '' if !$args{defloc};
|
---|
| 224 | $args{rwhois} = 'n' if !$args{rwhois}; # fail "safe", sort of.
|
---|
| 225 | $args{rwhois} = 'n' if $args{rwhois} ne 'n' and $args{rwhois} ne 'y';
|
---|
| 226 |
|
---|
[371] | 227 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 228 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 229 | local $dbh->{AutoCommit} = 0;
|
---|
| 230 | local $dbh->{RaiseError} = 1;
|
---|
| 231 |
|
---|
| 232 | # Wrap all the SQL in a transaction
|
---|
| 233 | eval {
|
---|
[518] | 234 | my ($mexist) = $dbh->selectrow_array("SELECT cidr FROM masterblocks WHERE cidr <<= ?", undef, ($cidr) );
|
---|
[371] | 235 |
|
---|
[518] | 236 | if (!$mexist) {
|
---|
[371] | 237 | # First case - master is brand-spanking-new.
|
---|
| 238 | ##fixme: rwhois should be globally-flagable somewhere, much like a number of other things
|
---|
| 239 | ## maybe a db table called "config"?
|
---|
[582] | 240 | $dbh->do("INSERT INTO masterblocks (cidr,rwhois,vrf,rdns) VALUES (?,?,?,?)", undef, ($cidr, 'y', $args{vrf}, $args{rdns}) );
|
---|
[371] | 241 |
|
---|
| 242 | # Unrouted blocks aren't associated with a city (yet). We don't rely on this
|
---|
| 243 | # elsewhere though; legacy data may have traps and pitfalls in it to break this.
|
---|
| 244 | # Thus the "routed" flag.
|
---|
[582] | 245 | $dbh->do("INSERT INTO freeblocks (cidr,city,routed,parent,rdepth,vrf) VALUES (?,?,?,?,?,?)", undef,
|
---|
| 246 | ($cidr, '<NULL>', 'm', $cidr, 1, $args{vrf}) );
|
---|
[371] | 247 |
|
---|
| 248 | # If we get here, everything is happy. Commit changes.
|
---|
| 249 | $dbh->commit;
|
---|
| 250 |
|
---|
[518] | 251 | } # done new master does not contain existing master(s)
|
---|
[371] | 252 | else {
|
---|
| 253 |
|
---|
| 254 | # collect the master(s) we're going to absorb, and snag the longest netmask while we're at it.
|
---|
| 255 | my $smallmask = $cidr->masklen;
|
---|
[518] | 256 | my $sth = $dbh->prepare("SELECT cidr FROM masterblocks WHERE cidr <<= ?");
|
---|
| 257 | $sth->execute($cidr);
|
---|
[371] | 258 | my @cmasters;
|
---|
| 259 | while (my @data = $sth->fetchrow_array) {
|
---|
| 260 | my $master = new NetAddr::IP $data[0];
|
---|
| 261 | push @cmasters, $master;
|
---|
| 262 | $smallmask = $master->masklen if $master->masklen > $smallmask;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | # split the new master, and keep only those blocks not part of an existing master
|
---|
| 266 | my @blocklist;
|
---|
| 267 | foreach my $seg ($cidr->split($smallmask)) {
|
---|
| 268 | my $contained = 0;
|
---|
| 269 | foreach my $master (@cmasters) {
|
---|
| 270 | $contained = 1 if $master->contains($seg);
|
---|
| 271 | }
|
---|
| 272 | push @blocklist, $seg if !$contained;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | # collect the unrouted free blocks within the new master
|
---|
[556] | 276 | $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE masklen(cidr) <= ? AND cidr <<= ? AND routed = 'm'");
|
---|
[518] | 277 | $sth->execute($smallmask, $cidr);
|
---|
[371] | 278 | while (my @data = $sth->fetchrow_array) {
|
---|
| 279 | my $freeblock = new NetAddr::IP $data[0];
|
---|
| 280 | push @blocklist, $freeblock;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | # combine the set of free blocks we should have now.
|
---|
| 284 | @blocklist = Compact(@blocklist);
|
---|
| 285 |
|
---|
| 286 | # and now insert the new data. Make sure to delete old masters too.
|
---|
| 287 |
|
---|
| 288 | # freeblocks
|
---|
[518] | 289 | $sth = $dbh->prepare("DELETE FROM freeblocks WHERE cidr <<= ?");
|
---|
[582] | 290 | my $sth2 = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,parent,rdepth,vrf)".
|
---|
| 291 | " VALUES (?,'<NULL>','m',?,1,?)");
|
---|
[371] | 292 | foreach my $newblock (@blocklist) {
|
---|
[518] | 293 | $sth->execute($newblock);
|
---|
[582] | 294 | $sth2->execute($newblock, $cidr, $args{vrf});
|
---|
[371] | 295 | }
|
---|
| 296 |
|
---|
[556] | 297 | # update parent relations at rdepth=1
|
---|
| 298 | $dbh->do("UPDATE allocations SET parent = ? WHERE parent << ? AND rdepth=1", undef, ($cidr, $cidr) );
|
---|
| 299 | $dbh->do("UPDATE freeblocks SET parent = ? WHERE parent << ? AND rdepth=1", undef, ($cidr, $cidr) );
|
---|
| 300 |
|
---|
[371] | 301 | # master
|
---|
[518] | 302 | $dbh->do("DELETE FROM masterblocks WHERE cidr <<= ?", undef, ($cidr) );
|
---|
[582] | 303 | $dbh->do("INSERT INTO masterblocks (cidr,rwhois,vrf,rdns) VALUES (?,?,?,?)", undef, ($cidr, 'y', $args{vrf}, $args{rdns}) );
|
---|
[371] | 304 |
|
---|
| 305 | # *whew* If we got here, we likely suceeded.
|
---|
| 306 | $dbh->commit;
|
---|
| 307 | } # new master contained existing master(s)
|
---|
| 308 | }; # end eval
|
---|
| 309 |
|
---|
| 310 | if ($@) {
|
---|
| 311 | my $msg = $@;
|
---|
| 312 | eval { $dbh->rollback; };
|
---|
| 313 | return ('FAIL',$msg);
|
---|
| 314 | } else {
|
---|
[582] | 315 |
|
---|
| 316 | # Only attempt rDNS if the IPDB side succeeded
|
---|
| 317 | if ($rpc_url) {
|
---|
| 318 | # Make an object to represent the XML-RPC server.
|
---|
| 319 | my $server = Frontier::Client->new(url => $rpc_url, debug => 0);
|
---|
| 320 | my $result;
|
---|
| 321 |
|
---|
| 322 | # Note *not* splitting reverse zones negates any benefit from caching the exported data.
|
---|
| 323 | # IPv6 address space is far too large to split usefully, and in any case (also due to
|
---|
| 324 | # the large address space) doesn't support the iterated template records v4 zones do
|
---|
| 325 | # that causes the bulk of the slowdown that needs the cache anyway.
|
---|
| 326 |
|
---|
| 327 | my @zonelist;
|
---|
| 328 | # allow splitting reverse zones to be disabled, maybe, someday
|
---|
| 329 | #if ($splitrevzones && !$cidr->{isv6}) {
|
---|
| 330 | if (1 && !$cidr->{isv6}) {
|
---|
| 331 | my $splitpoint = ($cidr->masklen <= 16 ? 16 : 24); # hack pthui
|
---|
| 332 | @zonelist = $cidr->split($splitpoint);
|
---|
| 333 | } else {
|
---|
| 334 | @zonelist = ($cidr);
|
---|
| 335 | }
|
---|
| 336 | my @fails;
|
---|
| 337 | ##fixme: remove hardcoding where possible
|
---|
| 338 | foreach my $subzone (@zonelist) {
|
---|
| 339 | my %rpcargs = (
|
---|
| 340 | rpcuser => $args{user},
|
---|
| 341 | rpcsystem => 'ipdb',
|
---|
| 342 | revzone => "$subzone",
|
---|
| 343 | revpatt => $args{rdns},
|
---|
| 344 | defloc => $args{defloc},
|
---|
| 345 | group => 1, # not sure how these two could sanely be exposed, tbh...
|
---|
| 346 | state => 1, # could make them globally configurable maybe
|
---|
| 347 | );
|
---|
| 348 | eval {
|
---|
| 349 | $result = $server->call('dnsdb.addRDNS', %rpcargs);
|
---|
| 350 | };
|
---|
| 351 | if ($@) {
|
---|
| 352 | my $msg = $@;
|
---|
| 353 | $msg =~ s/Fault returned from XML RPC Server, fault code 4: error executing RPC `dnsdb.addRDNS'\.\s//;
|
---|
| 354 | push @fails, ("$subzone" => $msg);
|
---|
| 355 | }
|
---|
| 356 | }
|
---|
| 357 | if (@fails) {
|
---|
| 358 | return ('WARN',"Warning(s) adding $cidr to reverse DNS:\n".join("\n", @fails));
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
[371] | 361 | return ('OK','OK');
|
---|
| 362 | }
|
---|
| 363 | } # end addMaster
|
---|
| 364 |
|
---|
| 365 |
|
---|
[547] | 366 | ## IPDB::touchMaster()
|
---|
| 367 | # Update last-changed timestamp on a master block.
|
---|
| 368 | sub touchMaster {
|
---|
| 369 | my $dbh = shift;
|
---|
| 370 | my $master = shift;
|
---|
| 371 |
|
---|
| 372 | local $dbh->{AutoCommit} = 0;
|
---|
| 373 | local $dbh->{RaiseError} = 1;
|
---|
| 374 |
|
---|
| 375 | eval {
|
---|
| 376 | $dbh->do("UPDATE masterblocks SET mtime=now() WHERE cidr = ?", undef, ($master));
|
---|
| 377 | $dbh->commit;
|
---|
| 378 | };
|
---|
| 379 |
|
---|
| 380 | if ($@) {
|
---|
| 381 | my $msg = $@;
|
---|
| 382 | eval { $dbh->rollback; };
|
---|
| 383 | return ('FAIL',$msg);
|
---|
| 384 | }
|
---|
| 385 | return ('OK','OK');
|
---|
| 386 | } # end touchMaster()
|
---|
| 387 |
|
---|
| 388 |
|
---|
[523] | 389 | ## IPDB::listSummary()
|
---|
| 390 | # Get summary list of all master blocks
|
---|
| 391 | # Returns an arrayref to a list of hashrefs containing the master block, routed count,
|
---|
| 392 | # allocated count, free count, and largest free block masklength
|
---|
| 393 | sub listSummary {
|
---|
| 394 | my $dbh = shift;
|
---|
| 395 |
|
---|
| 396 | my $mlist = $dbh->selectall_arrayref("SELECT cidr AS master FROM masterblocks ORDER BY cidr", { Slice => {} });
|
---|
| 397 |
|
---|
| 398 | foreach (@{$mlist}) {
|
---|
[560] | 399 | my ($rcnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? AND type='rm'",
|
---|
| 400 | undef, ($$_{master}));
|
---|
[523] | 401 | $$_{routed} = $rcnt;
|
---|
[560] | 402 | my ($acnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? AND NOT type='rm'",
|
---|
| 403 | undef, ($$_{master}));
|
---|
[523] | 404 | $$_{allocated} = $acnt;
|
---|
[560] | 405 | my ($fcnt) = $dbh->selectrow_array("SELECT count(*) FROM freeblocks WHERE cidr <<= ?",
|
---|
| 406 | undef, ($$_{master}));
|
---|
[523] | 407 | $$_{free} = $fcnt;
|
---|
[560] | 408 | my ($bigfree) = $dbh->selectrow_array("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
|
---|
| 409 | " AND parent = ? ORDER BY masklen(cidr) LIMIT 1", undef, ($$_{master}, $$_{master}));
|
---|
[523] | 410 | ##fixme: should find a way to do this without having to HTMLize the <>
|
---|
| 411 | $bigfree = "/$bigfree" if $bigfree;
|
---|
[525] | 412 | $bigfree = '<NONE>' if !$bigfree;
|
---|
[523] | 413 | $$_{bigfree} = $bigfree;
|
---|
| 414 | }
|
---|
| 415 | return $mlist;
|
---|
| 416 | } # end listSummary()
|
---|
| 417 |
|
---|
| 418 |
|
---|
[561] | 419 | ## IPDB::listSubs()
|
---|
| 420 | # Get list of subnets within a specified CIDR block, on a specified VRF.
|
---|
| 421 | # Returns an arrayref to a list of hashrefs containing the CIDR block, customer location or
|
---|
| 422 | # city it's routed to, block type, SWIP status, and description
|
---|
| 423 | sub listSubs {
|
---|
| 424 | my $dbh = shift;
|
---|
| 425 | my %args = @_;
|
---|
| 426 |
|
---|
| 427 | # Just In Case
|
---|
| 428 | $args{vrf} = '' if !$args{vrf};
|
---|
| 429 | $args{rdepth} = 1 if !$args{rdepth};
|
---|
| 430 |
|
---|
| 431 | # Snag the allocations for this block
|
---|
| 432 | my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description".
|
---|
| 433 | " FROM allocations WHERE parent = ? AND rdepth = ? ORDER BY cidr");
|
---|
| 434 | $sth->execute($args{block},$args{rdepth});
|
---|
| 435 |
|
---|
| 436 | # hack hack hack
|
---|
| 437 | # set up to flag swip=y records if they don't actually have supporting data in the customers table
|
---|
| 438 | my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?");
|
---|
| 439 |
|
---|
| 440 | my @blocklist;
|
---|
| 441 | while (my ($cidr,$city,$type,$custid,$swip,$desc) = $sth->fetchrow_array()) {
|
---|
| 442 | $custsth->execute($custid);
|
---|
| 443 | my ($ncust) = $custsth->fetchrow_array();
|
---|
| 444 | my %row = (
|
---|
| 445 | block => $cidr,
|
---|
| 446 | city => $city,
|
---|
| 447 | type => $disp_alloctypes{$type},
|
---|
| 448 | custid => $custid,
|
---|
| 449 | swip => ($swip eq 'y' ? 'Yes' : 'No'),
|
---|
| 450 | partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0),
|
---|
| 451 | desc => $desc,
|
---|
| 452 | hassubs => ($type eq 'rm' || $type =~ /.c/ ? 1 : 0),
|
---|
| 453 | );
|
---|
| 454 | # $row{subblock} = ($type =~ /^.r$/); # hmf. wonder why these won't work in the hash declaration...
|
---|
| 455 | $row{listpool} = ($type =~ /^.[pd]$/);
|
---|
| 456 | push (@blocklist, \%row);
|
---|
| 457 | }
|
---|
| 458 | return \@blocklist;
|
---|
| 459 | } # end listSubs()
|
---|
| 460 |
|
---|
| 461 |
|
---|
[524] | 462 | ## IPDB::listMaster()
|
---|
| 463 | # Get list of routed blocks in the requested master
|
---|
| 464 | # Returns an arrayref to a list of hashrefs containing the routed block, POP/city the block is routed to,
|
---|
| 465 | # allocated count, free count, and largest free block masklength
|
---|
| 466 | sub listMaster {
|
---|
| 467 | my $dbh = shift;
|
---|
| 468 | my $master = shift;
|
---|
[523] | 469 |
|
---|
[524] | 470 | my $rlist = $dbh->selectall_arrayref("SELECT cidr AS block,city FROM routed WHERE cidr <<= ? ORDER BY cidr",
|
---|
| 471 | { Slice => {} }, ($master) );
|
---|
[523] | 472 |
|
---|
[524] | 473 | foreach (@{$rlist}) {
|
---|
| 474 | my ($acnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ?", undef, ($$_{block}));
|
---|
| 475 | $$_{nsubs} = $acnt;
|
---|
| 476 | my ($fcnt) = $dbh->selectrow_array("SELECT count(*) FROM freeblocks WHERE cidr <<= ?".
|
---|
| 477 | " AND (routed='y' OR routed='n')", undef, ($$_{block}));
|
---|
| 478 | $$_{nfree} = $fcnt;
|
---|
| 479 | my ($bigfree) = $dbh->selectrow_array("SELECT maskbits FROM freeblocks WHERE cidr <<= ?".
|
---|
| 480 | " AND (routed='y' OR routed='n') ORDER BY maskbits LIMIT 1", undef, ($$_{block}));
|
---|
| 481 | ##fixme: should find a way to do this without having to HTMLize the <>
|
---|
| 482 | $bigfree = "/$bigfree" if $bigfree;
|
---|
[525] | 483 | $bigfree = '<NONE>' if !$bigfree;
|
---|
[524] | 484 | $$_{lfree} = $bigfree;
|
---|
| 485 | }
|
---|
| 486 | return $rlist;
|
---|
| 487 | } # end listMaster()
|
---|
| 488 |
|
---|
| 489 |
|
---|
[527] | 490 | ## IPDB::listRBlock()
|
---|
| 491 | # Gets a list of free blocks in the requested parent/master in both CIDR and range notation
|
---|
| 492 | # Takes a parent/master and an optional flag to look at routed or unrouted blocks, depending
|
---|
| 493 | # on whether the master is a direct master or a routed block
|
---|
| 494 | # Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks
|
---|
| 495 | sub listRBlock {
|
---|
| 496 | my $dbh = shift;
|
---|
| 497 | my $routed = shift;
|
---|
[524] | 498 |
|
---|
[527] | 499 | # Snag the allocations for this block
|
---|
| 500 | my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description".
|
---|
| 501 | " FROM allocations WHERE cidr <<= ? ORDER BY cidr");
|
---|
| 502 | $sth->execute($routed);
|
---|
[524] | 503 |
|
---|
[527] | 504 | # hack hack hack
|
---|
| 505 | # set up to flag swip=y records if they don't actually have supporting data in the customers table
|
---|
| 506 | my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?");
|
---|
| 507 |
|
---|
| 508 | my @blocklist;
|
---|
| 509 | while (my ($cidr,$city,$type,$custid,$swip,$desc) = $sth->fetchrow_array()) {
|
---|
| 510 | $custsth->execute($custid);
|
---|
| 511 | my ($ncust) = $custsth->fetchrow_array();
|
---|
| 512 | my %row = (
|
---|
| 513 | block => $cidr,
|
---|
| 514 | city => $city,
|
---|
| 515 | type => $disp_alloctypes{$type},
|
---|
| 516 | custid => $custid,
|
---|
| 517 | swip => ($swip eq 'y' ? 'Yes' : 'No'),
|
---|
| 518 | partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0),
|
---|
| 519 | desc => $desc
|
---|
| 520 | );
|
---|
| 521 | $row{subblock} = ($type =~ /^.r$/); # hmf. wonder why these won't work in the hash declaration...
|
---|
| 522 | $row{listpool} = ($type =~ /^.[pd]$/);
|
---|
| 523 | push (@blocklist, \%row);
|
---|
| 524 | }
|
---|
| 525 | return \@blocklist;
|
---|
| 526 | } # end listRBlock()
|
---|
| 527 |
|
---|
| 528 |
|
---|
[524] | 529 | ## IPDB::listFree()
|
---|
[562] | 530 | # Gets a list of free blocks in the requested parent/master and VRF instance in both CIDR and range notation
|
---|
| 531 | # Takes a parent/master and an optional VRF specifier that defaults to empty.
|
---|
[524] | 532 | # Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks
|
---|
[527] | 533 | # Returns some extra flags in the hashrefs for routed blocks, since those can have several subtypes
|
---|
[524] | 534 | sub listFree {
|
---|
| 535 | my $dbh = shift;
|
---|
| 536 |
|
---|
[562] | 537 | my %args = @_;
|
---|
| 538 | # Just In Case
|
---|
| 539 | $args{vrf} = '' if !$args{vrf};
|
---|
| 540 | $args{rdepth} = 1 if !$args{rdepth};
|
---|
| 541 |
|
---|
[524] | 542 | # do it this way so we can waste a little less time iterating
|
---|
[579] | 543 | # my $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE parent = ? AND rdepth = ? AND vrf = ? ".
|
---|
| 544 | my $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE parent = ? AND rdepth = ? ".
|
---|
[562] | 545 | "ORDER BY cidr");
|
---|
[579] | 546 | # $sth->execute($args{master}, $args{rdepth}, $args{vrf});
|
---|
| 547 | $sth->execute($args{master}, $args{rdepth});
|
---|
[524] | 548 | my @flist;
|
---|
[562] | 549 | while (my ($cidr) = $sth->fetchrow_array()) {
|
---|
[524] | 550 | $cidr = new NetAddr::IP $cidr;
|
---|
[527] | 551 | my %row = (
|
---|
| 552 | fblock => "$cidr",
|
---|
| 553 | frange => $cidr->range,
|
---|
| 554 | );
|
---|
[524] | 555 | push @flist, \%row;
|
---|
| 556 | }
|
---|
| 557 | return \@flist;
|
---|
[527] | 558 | } # end listFree()
|
---|
[524] | 559 |
|
---|
| 560 |
|
---|
[528] | 561 | ## IPDB::listPool()
|
---|
| 562 | #
|
---|
| 563 | sub listPool {
|
---|
| 564 | my $dbh = shift;
|
---|
| 565 | my $pool = shift;
|
---|
| 566 |
|
---|
[563] | 567 | my $sth = $dbh->prepare("SELECT ip,custid,available,description,type,rdepth".
|
---|
[528] | 568 | " FROM poolips WHERE pool = ? ORDER BY ip");
|
---|
| 569 | $sth->execute($pool);
|
---|
| 570 | my @poolips;
|
---|
[563] | 571 | while (my ($ip,$custid,$available,$desc,$type,$rdepth) = $sth->fetchrow_array) {
|
---|
[528] | 572 | my %row = (
|
---|
| 573 | ip => $ip,
|
---|
| 574 | custid => $custid,
|
---|
| 575 | available => $available,
|
---|
| 576 | desc => $desc,
|
---|
[563] | 577 | delme => $available eq 'n',
|
---|
| 578 | ipdepth => $rdepth,
|
---|
[528] | 579 | );
|
---|
| 580 | push @poolips, \%row;
|
---|
| 581 | }
|
---|
| 582 | return \@poolips;
|
---|
| 583 | } # end listPool()
|
---|
| 584 |
|
---|
| 585 |
|
---|
[541] | 586 | ## IPDB::getMasterList()
|
---|
| 587 | # Get a list of master blocks, optionally including last-modified timestamps
|
---|
| 588 | # Takes an optional flag to indicate whether to include timestamps;
|
---|
| 589 | # 'm' includes ctime, all others (suggest 'c') do not.
|
---|
| 590 | # Returns an arrayref to a list of hashrefs
|
---|
| 591 | sub getMasterList {
|
---|
| 592 | my $dbh = shift;
|
---|
| 593 | my $stampme = shift || 'm'; # optional but should be set by caller for clarity
|
---|
| 594 |
|
---|
| 595 | my $mlist = $dbh->selectall_arrayref("SELECT cidr AS master".($stampme eq 'm' ? ',mtime' : '').
|
---|
| 596 | " FROM masterblocks ORDER BY cidr", { Slice => {} });
|
---|
| 597 | return $mlist;
|
---|
| 598 | } # end getMasterList()
|
---|
| 599 |
|
---|
| 600 |
|
---|
[529] | 601 | ## IPDB::getTypeList()
|
---|
| 602 | # Get an alloctype/description pair list suitable for dropdowns
|
---|
| 603 | # Takes a flag to determine which general groups of types are returned
|
---|
| 604 | # Returns an reference to an array of hashrefs
|
---|
| 605 | sub getTypeList {
|
---|
| 606 | my $dbh = shift;
|
---|
| 607 | my $tgroup = shift || 'a'; # technically optional, like this, but should
|
---|
| 608 | # really be specified in the call for clarity
|
---|
| 609 | my $tlist;
|
---|
[564] | 610 | if ($tgroup eq 'n') {
|
---|
| 611 | # grouping 'p' - all netblock types. These include routed blocks, containers (_c)
|
---|
| 612 | # and contained (_r) types, dynamic-allocation ranges (_e), static IP pools (_d and _p),
|
---|
| 613 | # and the "miscellaneous" cn, in, and en types.
|
---|
| 614 | $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
|
---|
| 615 | "AND type NOT LIKE '_i' ORDER BY listorder", { Slice => {} });
|
---|
| 616 | } elsif ($tgroup eq 'p') {
|
---|
| 617 | # grouping 'p' - primary allocation types. As with 'n' above but without the _r contained types.
|
---|
| 618 | $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
|
---|
[529] | 619 | "AND type NOT LIKE '_i' AND type NOT LIKE '_r' ORDER BY listorder", { Slice => {} });
|
---|
| 620 | } elsif ($tgroup eq 'c') {
|
---|
| 621 | # grouping 'c' - contained types. These include all static IPs and all _r types.
|
---|
| 622 | $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
|
---|
| 623 | " AND (type LIKE '_i' OR type LIKE '_r') ORDER BY listorder", { Slice => {} });
|
---|
| 624 | } else {
|
---|
| 625 | # grouping 'a' - all standard allocation types. This includes everything
|
---|
| 626 | # but mm (present only as a formality). Make this the default.
|
---|
| 627 | $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
|
---|
| 628 | " ORDER BY listorder", { Slice => {} });
|
---|
| 629 | }
|
---|
| 630 | return $tlist;
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 |
|
---|
[532] | 634 | ## IPDB::getPoolSelect()
|
---|
| 635 | # Get a list of pools matching the passed city and type that have 1 or more free IPs
|
---|
[533] | 636 | # Returns an arrayref to a list of hashrefs
|
---|
[532] | 637 | sub getPoolSelect {
|
---|
| 638 | my $dbh = shift;
|
---|
| 639 | my $iptype = shift;
|
---|
| 640 | my $pcity = shift;
|
---|
| 641 |
|
---|
| 642 | my ($ptype) = ($iptype =~ /^(.)i$/);
|
---|
| 643 | return if !$ptype;
|
---|
| 644 | $ptype .= '_';
|
---|
| 645 |
|
---|
| 646 | my $plist = $dbh->selectall_arrayref(
|
---|
[572] | 647 | "SELECT count(*) AS poolfree,p.pool AS poolblock, a.city AS poolcit, a.rdepth AS poolrdepth ".
|
---|
| 648 | "FROM poolips p ".
|
---|
| 649 | "JOIN allocations a ON p.pool=a.cidr ".
|
---|
| 650 | "WHERE p.available='y' AND a.city = ? AND p.type LIKE ? ".
|
---|
| 651 | "GROUP BY p.pool,a.city,a.rdepth",
|
---|
| 652 | { Slice => {} }, ($pcity, $ptype) );
|
---|
[532] | 653 | return $plist;
|
---|
| 654 | } # end getPoolSelect()
|
---|
| 655 |
|
---|
| 656 |
|
---|
[533] | 657 | ## IPDB::findAllocateFrom()
|
---|
| 658 | # Find free block to add a new allocation from. (CIDR block version of pool select above, more or less)
|
---|
| 659 | # Takes
|
---|
| 660 | # - mask length
|
---|
| 661 | # - allocation type
|
---|
| 662 | # - POP city "parent"
|
---|
| 663 | # - optional master-block restriction
|
---|
| 664 | # - optional flag to allow automatic pick-from-private-network-ranges
|
---|
| 665 | # Returns a string with the first CIDR block matching the criteria, if any
|
---|
| 666 | sub findAllocateFrom {
|
---|
| 667 | my $dbh = shift;
|
---|
| 668 | my $maskbits = shift;
|
---|
| 669 | my $type = shift;
|
---|
| 670 | my $city = shift;
|
---|
| 671 | my $pop = shift;
|
---|
| 672 | my %optargs = @_;
|
---|
| 673 |
|
---|
| 674 | my $failmsg = "No suitable free block found\n";
|
---|
| 675 |
|
---|
| 676 | ## Set up the SQL to find out what freeblock we can (probably) use for an allocation.
|
---|
| 677 | ## Very large systems will require development of a reserve system (possibly an extension
|
---|
| 678 | ## of the reserve-for-expansion concept in https://secure.deepnet.cx/trac/ipdb/ticket/24?)
|
---|
| 679 | ## Also populate a value list for the DBI call.
|
---|
| 680 |
|
---|
[572] | 681 | my @vallist = ($maskbits);
|
---|
| 682 | my $sql = "SELECT cidr,rdepth FROM freeblocks WHERE masklen(cidr) <= ?";
|
---|
[533] | 683 |
|
---|
[572] | 684 | # cases, strict rules
|
---|
| 685 | # .c -> container type
|
---|
| 686 | # requires a routing container, fbtype r
|
---|
| 687 | # .d -> DHCP/"normal-routing" static pool
|
---|
| 688 | # requires a routing container, fbtype r
|
---|
| 689 | # .e -> Dynamic-assignment connectivity
|
---|
| 690 | # requires a routing container, fbtype r
|
---|
| 691 | # .i -> error, can't allocate static IPs this way?
|
---|
| 692 | # mm -> error, master block
|
---|
| 693 | # rm -> routed block
|
---|
| 694 | # requires master block, fbtype m
|
---|
| 695 | # .n -> Miscellaneous usage
|
---|
| 696 | # requires a routing container, fbtype r
|
---|
| 697 | # .p -> PPP(oE) static pool
|
---|
| 698 | # requires a routing container, fbtype r
|
---|
| 699 | # .r -> contained type
|
---|
| 700 | # requires a matching container, fbtype $1
|
---|
| 701 | ##fixme: strict-or-not flag
|
---|
| 702 |
|
---|
| 703 | if ($type =~ /^(.)r$/) {
|
---|
| 704 | push @vallist, $1;
|
---|
| 705 | $sql .= " AND routed = ?";
|
---|
| 706 | } elsif ($type eq 'rm') {
|
---|
| 707 | $sql .= " AND routed = 'm'";
|
---|
| 708 | } else {
|
---|
| 709 | $sql .= " AND routed = 'r'";
|
---|
| 710 | }
|
---|
| 711 |
|
---|
[533] | 712 | # for PPP(oE) and container types, the POP city is the one attached to the pool.
|
---|
| 713 | # individual allocations get listed with the customer city site.
|
---|
| 714 | ##fixme: chain cities to align roughly with a full layer-2 node graph
|
---|
| 715 | $city = $pop if $type !~ /^.[pc]$/;
|
---|
[544] | 716 | if ($type ne 'rm' && $city) {
|
---|
[533] | 717 | $sql .= " AND city = ?";
|
---|
| 718 | push @vallist, $city;
|
---|
| 719 | }
|
---|
[544] | 720 | # Allow specifying an arbitrary full block, instead of a master
|
---|
| 721 | if ($optargs{gimme}) {
|
---|
| 722 | $sql .= " AND cidr >>= ?";
|
---|
| 723 | push @vallist, $optargs{gimme};
|
---|
| 724 | }
|
---|
[533] | 725 | # if a specific master was requested, allow the requestor to self->shoot(foot)
|
---|
| 726 | if ($optargs{master} && $optargs{master} ne '-') {
|
---|
| 727 | $sql .= " AND cidr <<= ?" if $optargs{master} ne '-';
|
---|
| 728 | push @vallist, $optargs{master};
|
---|
| 729 | } else {
|
---|
| 730 | # if a specific master was NOT requested, filter out the RFC 1918 private networks
|
---|
| 731 | if (!$optargs{allowpriv}) {
|
---|
| 732 | $sql .= " AND NOT (cidr <<= '192.168.0.0/16' OR cidr <<= '10.0.0.0/8' OR cidr <<= '172.16.0.0/12')";
|
---|
| 733 | }
|
---|
| 734 | }
|
---|
| 735 | # Sorting and limiting, since we don't (currently) care to provide a selection of
|
---|
| 736 | # blocks to carve up. This preserves something resembling optimal usage of the IP
|
---|
| 737 | # space by forcing contiguous allocations and free blocks as much as possible.
|
---|
| 738 | $sql .= " ORDER BY maskbits DESC,cidr LIMIT 1";
|
---|
| 739 |
|
---|
[572] | 740 | my ($fbfound,$fbdepth) = $dbh->selectrow_array($sql, undef, @vallist);
|
---|
| 741 | return $fbfound,$fbdepth;
|
---|
[533] | 742 | } # end findAllocateFrom()
|
---|
| 743 |
|
---|
| 744 |
|
---|
[536] | 745 | ## IPDB::ipParent()
|
---|
| 746 | # Get an IP's parent pool's details
|
---|
| 747 | # Takes a database handle and IP
|
---|
| 748 | # Returns a hashref to the parent pool block, if any
|
---|
| 749 | sub ipParent {
|
---|
| 750 | my $dbh = shift;
|
---|
| 751 | my $block = shift;
|
---|
| 752 |
|
---|
| 753 | my $pinfo = $dbh->selectrow_hashref("SELECT cidr,custid,type,city,description FROM allocations".
|
---|
[565] | 754 | " WHERE cidr >>= ? AND (type LIKE '_p' OR type LIKE '_d')", undef, ($block) );
|
---|
[536] | 755 | return $pinfo;
|
---|
| 756 | } # end ipParent()
|
---|
| 757 |
|
---|
| 758 |
|
---|
| 759 | ## IPDB::subParent()
|
---|
[529] | 760 | # Get a block's parent's details
|
---|
| 761 | # Takes a database handle and CIDR block
|
---|
[536] | 762 | # Returns a hashref to the parent container block, if any
|
---|
| 763 | sub subParent {
|
---|
[529] | 764 | my $dbh = shift;
|
---|
| 765 | my $block = shift;
|
---|
| 766 |
|
---|
| 767 | my $pinfo = $dbh->selectrow_hashref("SELECT cidr,custid,type,city,description FROM allocations".
|
---|
| 768 | " WHERE cidr >>= ?", undef, ($block) );
|
---|
| 769 | return $pinfo;
|
---|
[536] | 770 | } # end subParent()
|
---|
[529] | 771 |
|
---|
| 772 |
|
---|
[536] | 773 | ## IPDB::blockParent()
|
---|
| 774 | # Get a block's parent's details
|
---|
| 775 | # Takes a database handle and CIDR block
|
---|
| 776 | # Returns a hashref to the parent container block, if any
|
---|
| 777 | sub blockParent {
|
---|
| 778 | my $dbh = shift;
|
---|
| 779 | my $block = shift;
|
---|
| 780 |
|
---|
| 781 | my $pinfo = $dbh->selectrow_hashref("SELECT cidr,city FROM routed".
|
---|
| 782 | " WHERE cidr >>= ?", undef, ($block) );
|
---|
| 783 | return $pinfo;
|
---|
| 784 | } # end blockParent()
|
---|
| 785 |
|
---|
| 786 |
|
---|
[527] | 787 | ## IPDB::getRoutedCity()
|
---|
| 788 | # Get the city for a routed block.
|
---|
| 789 | sub getRoutedCity {
|
---|
| 790 | my $dbh = shift;
|
---|
| 791 | my $block = shift;
|
---|
| 792 |
|
---|
| 793 | my ($rcity) = $dbh->selectrow_array("SELECT city FROM routed WHERE cidr = ?", undef, ($block) );
|
---|
| 794 | return $rcity;
|
---|
| 795 | } # end getRoutedCity()
|
---|
| 796 |
|
---|
| 797 |
|
---|
[77] | 798 | ## IPDB::allocateBlock()
|
---|
[66] | 799 | # Does all of the magic of actually allocating a netblock
|
---|
[554] | 800 | # Requires a database handle, and a hash containing the block to allocate, routing depth, custid,
|
---|
| 801 | # type, city, block to allocate from, and optionally a description, notes, circuit ID,
|
---|
| 802 | # and private data
|
---|
[77] | 803 | # Returns a success code and optional error message.
|
---|
| 804 | sub allocateBlock {
|
---|
[554] | 805 | my $dbh = shift;
|
---|
[284] | 806 |
|
---|
[554] | 807 | my %args = @_;
|
---|
| 808 |
|
---|
| 809 | $args{cidr} = new NetAddr::IP $args{cidr};
|
---|
| 810 | $args{alloc_from} = new NetAddr::IP $args{alloc_from};
|
---|
| 811 |
|
---|
| 812 | $args{desc} = '' if !$args{desc};
|
---|
| 813 | $args{notes} = '' if !$args{notes};
|
---|
| 814 | $args{circid} = '' if !$args{circid};
|
---|
| 815 | $args{privdata} = '' if !$args{privdata};
|
---|
| 816 | $args{vrf} = '' if !$args{vrf};
|
---|
| 817 |
|
---|
[77] | 818 | my $sth;
|
---|
[66] | 819 |
|
---|
[349] | 820 | # Snag the "type" of the freeblock (alloc_from) "just in case"
|
---|
[554] | 821 | $sth = $dbh->prepare("select routed from freeblocks where cidr='$args{alloc_from}'");
|
---|
[349] | 822 | $sth->execute;
|
---|
| 823 | my ($alloc_from_type) = $sth->fetchrow_array;
|
---|
| 824 |
|
---|
[79] | 825 | # To contain the error message, if any.
|
---|
[554] | 826 | my $msg = "Unknown error allocating $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
[79] | 827 |
|
---|
[77] | 828 | # Enable transactions and error handling
|
---|
| 829 | local $dbh->{AutoCommit} = 0; # These need to be local so we don't
|
---|
| 830 | local $dbh->{RaiseError} = 1; # step on our toes by accident.
|
---|
[66] | 831 |
|
---|
[554] | 832 | if ($args{type} =~ /^.i$/) {
|
---|
| 833 | $msg = "Unable to assign static IP $args{cidr} to $args{custid}";
|
---|
[77] | 834 | eval {
|
---|
[554] | 835 | if ($args{cidr}) { # IP specified
|
---|
| 836 | my ($isavail) = $dbh->selectrow_array("SELECT available FROM poolips WHERE ip=?", undef, ($args{cidr}) );
|
---|
| 837 | die "IP is not in an IP pool.\n"
|
---|
| 838 | if !$isavail;
|
---|
| 839 | die "IP already allocated. Deallocate and reallocate, or update the entry\n"
|
---|
| 840 | if $isavail eq 'n';
|
---|
| 841 | } else { # IP not specified, take first available
|
---|
| 842 | ($args{cidr}) = $dbh->selectrow_array("SELECT ip FROM poolips WHERE pool=? AND available='y' ORDER BY ip",
|
---|
| 843 | undef, ($args{alloc_from}) );
|
---|
[545] | 844 | }
|
---|
[554] | 845 | $dbh->do("UPDATE poolips SET custid=?,city=?,available='n',description=?,notes=?,circuitid=?,privdata=?,vrf=? ".
|
---|
| 846 | "WHERE ip=?", undef, ($args{custid}, $args{city}, $args{desc}, $args{notes}, $args{circid},
|
---|
| 847 | $args{privdata}, $args{vrf}, $args{cidr}) );
|
---|
[157] | 848 |
|
---|
[397] | 849 | # node hack
|
---|
[554] | 850 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 851 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 852 | }
|
---|
| 853 | # end node hack
|
---|
[545] | 854 |
|
---|
[79] | 855 | $dbh->commit;
|
---|
[77] | 856 | };
|
---|
| 857 | if ($@) {
|
---|
[545] | 858 | $msg .= ": $@";
|
---|
[78] | 859 | eval { $dbh->rollback; };
|
---|
[578] | 860 | return ('FAIL', $msg);
|
---|
[77] | 861 | } else {
|
---|
[578] | 862 | return ('OK', $args{cidr});
|
---|
[77] | 863 | }
|
---|
| 864 |
|
---|
| 865 | } else { # end IP-from-pool allocation
|
---|
| 866 |
|
---|
[554] | 867 | if ($args{cidr} == $args{alloc_from}) {
|
---|
[77] | 868 | # Easiest case- insert in one table, delete in the other, and go home. More or less.
|
---|
| 869 | # insert into allocations values (cidr,custid,type,city,desc) and
|
---|
| 870 | # delete from freeblocks where cidr='cidr'
|
---|
| 871 | # For data safety on non-transaction DBs, we delete first.
|
---|
| 872 |
|
---|
| 873 | eval {
|
---|
[554] | 874 | $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
| 875 |
|
---|
[555] | 876 | # Get old freeblocks parent/depth/routed for new entries... before we delete it.
|
---|
| 877 | my ($fparent) = $dbh->selectrow_array("SELECT parent FROM freeblocks WHERE cidr=? AND rdepth=?",
|
---|
| 878 | undef, ($args{alloc_from}, $args{rdepth}) );
|
---|
| 879 |
|
---|
[554] | 880 | # Munge freeblocks
|
---|
| 881 | if ($args{type} =~ /^(.)[mc]$/) {
|
---|
| 882 | # special case - block is a routed or container/"reserve" block
|
---|
| 883 | my $rtype = $1;
|
---|
| 884 | $dbh->do("UPDATE freeblocks SET routed=?,rdepth=rdepth+1,city=?,parent=? WHERE cidr=? AND rdepth=?",
|
---|
[578] | 885 | undef, ($rtype, $args{city}, $args{cidr}, $args{cidr}, $args{rdepth}) );
|
---|
[77] | 886 | } else {
|
---|
[554] | 887 | # "normal" case
|
---|
| 888 | $dbh->do("DELETE FROM freeblocks WHERE cidr=? AND rdepth=?", undef, ($args{cidr}, $args{rdepth}));
|
---|
| 889 | }
|
---|
[77] | 890 |
|
---|
[554] | 891 | # Insert the allocations entry
|
---|
| 892 | $dbh->do("INSERT INTO allocations ".
|
---|
| 893 | "(cidr,parent,vrf,rdepth,custid,type,city,description,notes,circuitid,privdata)".
|
---|
| 894 | " VALUES (?,?,?,?,?,?,?,?,?,?,?)", undef,
|
---|
| 895 | ($args{cidr}, $fparent, $args{vrf}, $args{rdepth}, $args{custid}, $args{type}, $args{city},
|
---|
| 896 | $args{desc}, $args{notes}, $args{circid}, $args{privdata}) );
|
---|
[79] | 897 |
|
---|
[554] | 898 | # And initialize the pool, if necessary
|
---|
| 899 | # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
|
---|
| 900 | # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
|
---|
| 901 | if ($args{type} =~ /^.p$/) {
|
---|
| 902 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[574] | 903 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $args{rdepth}+1);
|
---|
[554] | 904 | die $rmsg if $code eq 'FAIL';
|
---|
| 905 | } elsif ($args{type} =~ /^.d$/) {
|
---|
| 906 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[574] | 907 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $args{rdepth}+1);
|
---|
[554] | 908 | die $rmsg if $code eq 'FAIL';
|
---|
| 909 | }
|
---|
[79] | 910 |
|
---|
[397] | 911 | # node hack
|
---|
[554] | 912 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 913 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 914 | }
|
---|
| 915 | # end node hack
|
---|
[574] | 916 |
|
---|
[77] | 917 | $dbh->commit;
|
---|
[78] | 918 | }; # end of eval
|
---|
[77] | 919 | if ($@) {
|
---|
[157] | 920 | $msg .= ": ".$@;
|
---|
[77] | 921 | eval { $dbh->rollback; };
|
---|
[157] | 922 | return ('FAIL',$msg);
|
---|
[77] | 923 | } else {
|
---|
[78] | 924 | return ('OK',"OK");
|
---|
| 925 | }
|
---|
[77] | 926 |
|
---|
| 927 | } else { # cidr != alloc_from
|
---|
| 928 |
|
---|
| 929 | # Hard case. Allocation is smaller than free block.
|
---|
[554] | 930 | my $wantmaskbits = $args{cidr}->masklen;
|
---|
| 931 | my $maskbits = $args{alloc_from}->masklen;
|
---|
[77] | 932 |
|
---|
| 933 | my @newfreeblocks; # Holds free blocks generated from splitting the source freeblock.
|
---|
| 934 |
|
---|
| 935 | # This determines which blocks will be left "free" after allocation. We take the
|
---|
| 936 | # block we're allocating from, and split it in half. We see which half the wanted
|
---|
| 937 | # block is in, and repeat until the wanted block is equal to one of the halves.
|
---|
| 938 | my $i=0;
|
---|
[554] | 939 | my $tmp_from = $args{alloc_from}; # So we don't munge $args{alloc_from}
|
---|
[77] | 940 | while ($maskbits++ < $wantmaskbits) {
|
---|
| 941 | my @subblocks = $tmp_from->split($maskbits);
|
---|
[554] | 942 | $newfreeblocks[$i++] = (($args{cidr}->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
|
---|
| 943 | $tmp_from = ( ($args{cidr}->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
|
---|
[77] | 944 | } # while
|
---|
| 945 |
|
---|
| 946 | # Begin SQL transaction block
|
---|
| 947 | eval {
|
---|
[554] | 948 | $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
[79] | 949 |
|
---|
[554] | 950 | # Get old freeblocks parent/depth/routed for new entries
|
---|
| 951 | my ($fparent,$fcity,$wasrouted) = $dbh->selectrow_array("SELECT parent,city,routed FROM freeblocks".
|
---|
| 952 | " WHERE cidr=? AND rdepth=?", undef, ($args{alloc_from}, $args{rdepth}) );
|
---|
| 953 |
|
---|
[77] | 954 | # Delete old freeblocks entry
|
---|
[554] | 955 | $dbh->do("DELETE FROM freeblocks WHERE cidr=? AND rdepth=?", undef, ($args{alloc_from}, $args{rdepth}) );
|
---|
[77] | 956 |
|
---|
[554] | 957 | # Insert new list of smaller free blocks left over
|
---|
| 958 | $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent,rdepth) VALUES (?,?,?,?,?,?)");
|
---|
| 959 | foreach my $block (@newfreeblocks) {
|
---|
| 960 | $sth->execute($block, $fcity, $wasrouted, $args{vrf}, $fparent, $args{rdepth});
|
---|
| 961 | }
|
---|
[79] | 962 |
|
---|
[554] | 963 | # For routed/container types, add a freeblock within the allocated block so we can subdivide it further
|
---|
| 964 | if ($args{type} =~ /(.)[mc]/) { # rm and .c types - containers
|
---|
| 965 | my $rtype = $1;
|
---|
| 966 | $sth->execute($args{cidr}, $args{city}, $rtype, $args{vrf}, $args{cidr}, $args{rdepth}+1);
|
---|
| 967 | }
|
---|
[79] | 968 |
|
---|
[554] | 969 | # Insert the allocations entry
|
---|
| 970 | $dbh->do("INSERT INTO allocations ".
|
---|
| 971 | "(cidr,parent,vrf,rdepth,custid,type,city,description,notes,circuitid,privdata)".
|
---|
| 972 | " VALUES (?,?,?,?,?,?,?,?,?,?,?)", undef,
|
---|
| 973 | ($args{cidr}, $fparent, $args{vrf}, $args{rdepth}, $args{custid}, $args{type}, $args{city},
|
---|
| 974 | $args{desc}, $args{notes}, $args{circid}, $args{privdata}) );
|
---|
[77] | 975 |
|
---|
[554] | 976 | # And initialize the pool, if necessary
|
---|
| 977 | # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
|
---|
| 978 | # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
|
---|
| 979 | if ($args{type} =~ /^.p$/) {
|
---|
| 980 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[574] | 981 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $args{rdepth}+1);
|
---|
[554] | 982 | die $rmsg if $code eq 'FAIL';
|
---|
| 983 | } elsif ($args{type} =~ /^.d$/) {
|
---|
| 984 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[574] | 985 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $args{rdepth}+1);
|
---|
[554] | 986 | die $rmsg if $code eq 'FAIL';
|
---|
| 987 | }
|
---|
[77] | 988 |
|
---|
[397] | 989 | # node hack
|
---|
[554] | 990 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 991 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 992 | }
|
---|
| 993 | # end node hack
|
---|
[554] | 994 |
|
---|
[77] | 995 | $dbh->commit;
|
---|
| 996 | }; # end eval
|
---|
| 997 | if ($@) {
|
---|
[256] | 998 | $msg .= ": ".$@;
|
---|
[77] | 999 | eval { $dbh->rollback; };
|
---|
[78] | 1000 | return ('FAIL',$msg);
|
---|
[77] | 1001 | } else {
|
---|
[78] | 1002 | return ('OK',"OK");
|
---|
[77] | 1003 | }
|
---|
| 1004 |
|
---|
| 1005 | } # end fullcidr != alloc_from
|
---|
| 1006 |
|
---|
| 1007 | } # end static-IP vs netblock allocation
|
---|
| 1008 |
|
---|
| 1009 | } # end allocateBlock()
|
---|
| 1010 |
|
---|
| 1011 |
|
---|
| 1012 | ## IPDB::initPool()
|
---|
| 1013 | # Initializes a pool
|
---|
| 1014 | # Requires a database handle, the pool CIDR, type, city, and a parameter
|
---|
| 1015 | # indicating whether the pool should allow allocation of literally every
|
---|
| 1016 | # IP, or if it should reserve network/gateway/broadcast IPs
|
---|
[78] | 1017 | # Note that this is NOT done in a transaction, that's why it's a private
|
---|
| 1018 | # function and should ONLY EVER get called from allocateBlock()
|
---|
[77] | 1019 | sub initPool {
|
---|
[574] | 1020 | my ($dbh,undef,$type,$city,$class,$rdepth) = @_;
|
---|
[77] | 1021 | my $pool = new NetAddr::IP $_[1];
|
---|
| 1022 |
|
---|
[574] | 1023 | # IPv6 does not lend itself to IP pools as supported
|
---|
| 1024 | return ('FAIL',"Refusing to create IPv6 static IP pool") if $pool->{isv6};
|
---|
| 1025 | # IPv4 pools don't make much sense beyond even /24. Allow up to 4096-host footshooting anyway.
|
---|
| 1026 | # NetAddr::IP won't allow more than a /16 (65k hosts).
|
---|
| 1027 | return ('FAIL',"Refusing to create oversized static IP pool") if $pool->masklen <= 20;
|
---|
| 1028 |
|
---|
[157] | 1029 | ##fixme Need to just replace 2nd char of type with i rather than capturing 1st char of type
|
---|
| 1030 | $type =~ s/[pd]$/i/;
|
---|
[77] | 1031 | my $sth;
|
---|
[157] | 1032 | my $msg;
|
---|
[77] | 1033 |
|
---|
[157] | 1034 | # Trap errors so we can pass them back to the caller. Even if the
|
---|
| 1035 | # caller is only ever supposed to be local, and therefore already
|
---|
| 1036 | # trapping errors. >:(
|
---|
| 1037 | local $dbh->{AutoCommit} = 0; # These need to be local so we don't
|
---|
| 1038 | local $dbh->{RaiseError} = 1; # step on our toes by accident.
|
---|
| 1039 |
|
---|
| 1040 | eval {
|
---|
| 1041 | # have to insert all pool IPs into poolips table as "unallocated".
|
---|
[574] | 1042 | $sth = $dbh->prepare("INSERT INTO poolips (pool,ip,custid,city,type,rdepth)".
|
---|
| 1043 | " VALUES ('$pool', ?, '$defcustid', ?, '$type', $rdepth)");
|
---|
[157] | 1044 | my @poolip_list = $pool->hostenum;
|
---|
| 1045 | if ($class eq 'all') { # (DSL-ish block - *all* IPs available
|
---|
[246] | 1046 | if ($pool->addr !~ /\.0$/) { # .0 causes weirdness.
|
---|
[485] | 1047 | $sth->execute($pool->addr, $city);
|
---|
[246] | 1048 | }
|
---|
[157] | 1049 | for (my $i=0; $i<=$#poolip_list; $i++) {
|
---|
[485] | 1050 | $sth->execute($poolip_list[$i]->addr, $city);
|
---|
[157] | 1051 | }
|
---|
| 1052 | $pool--;
|
---|
[246] | 1053 | if ($pool->addr !~ /\.255$/) { # .255 can cause weirdness.
|
---|
[485] | 1054 | $sth->execute($pool->addr, $city);
|
---|
[246] | 1055 | }
|
---|
[157] | 1056 | } else { # (real netblock)
|
---|
| 1057 | for (my $i=1; $i<=$#poolip_list; $i++) {
|
---|
[485] | 1058 | $sth->execute($poolip_list[$i]->addr, $city);
|
---|
[157] | 1059 | }
|
---|
[77] | 1060 | }
|
---|
[574] | 1061 | $dbh->commit;
|
---|
[157] | 1062 | };
|
---|
| 1063 | if ($@) {
|
---|
[574] | 1064 | $msg = $@;
|
---|
[157] | 1065 | eval { $dbh->rollback; };
|
---|
| 1066 | return ('FAIL',$msg);
|
---|
| 1067 | } else {
|
---|
| 1068 | return ('OK',"OK");
|
---|
[77] | 1069 | }
|
---|
| 1070 | } # end initPool()
|
---|
| 1071 |
|
---|
| 1072 |
|
---|
[531] | 1073 | ## IPDB::updateBlock()
|
---|
| 1074 | # Update an allocation
|
---|
| 1075 | # Takes all allocation fields in a hash
|
---|
| 1076 | sub updateBlock {
|
---|
| 1077 | my $dbh = shift;
|
---|
| 1078 | my %args = @_;
|
---|
| 1079 |
|
---|
| 1080 | return ('FAIL', 'Missing block to update') if !$args{block};
|
---|
| 1081 |
|
---|
| 1082 | # do it all in a transaction
|
---|
| 1083 | local $dbh->{AutoCommit} = 0;
|
---|
| 1084 | local $dbh->{RaiseError} = 1;
|
---|
| 1085 |
|
---|
| 1086 | my @fieldlist;
|
---|
| 1087 | my @vallist;
|
---|
| 1088 | foreach ('custid', 'city', 'description', 'notes', 'circuitid', 'privdata') {
|
---|
| 1089 | if ($args{$_}) {
|
---|
| 1090 | push @fieldlist, $_;
|
---|
| 1091 | push @vallist, $args{$_};
|
---|
| 1092 | }
|
---|
| 1093 | }
|
---|
| 1094 |
|
---|
| 1095 | my $updtable = 'allocations';
|
---|
| 1096 | my $keyfield = 'cidr';
|
---|
[535] | 1097 | if ($args{type} =~ /^(.)i$/) {
|
---|
[531] | 1098 | $updtable = 'poolips';
|
---|
| 1099 | $keyfield = 'ip';
|
---|
| 1100 | } else {
|
---|
| 1101 | ## fixme: there's got to be a better way...
|
---|
| 1102 | if ($args{swip}) {
|
---|
| 1103 | if ($args{swip} eq 'on' || $args{swip} eq '1' || $args{swip} eq 'y') {
|
---|
| 1104 | $args{swip} = 'y';
|
---|
| 1105 | } else {
|
---|
| 1106 | $args{swip} = 'n';
|
---|
| 1107 | }
|
---|
| 1108 | }
|
---|
| 1109 | foreach ('type', 'swip') {
|
---|
| 1110 | if ($args{$_}) {
|
---|
| 1111 | push @fieldlist, $_;
|
---|
| 1112 | push @vallist, $args{$_};
|
---|
| 1113 | }
|
---|
| 1114 | }
|
---|
| 1115 | }
|
---|
| 1116 |
|
---|
| 1117 | return ('FAIL', 'No fields to update') if !@fieldlist;
|
---|
| 1118 |
|
---|
[579] | 1119 | # push @vallist, $args{block}, $args{rdepth}, $args{vrf};
|
---|
| 1120 | push @vallist, $args{block}, $args{rdepth};
|
---|
[531] | 1121 | my $sql = "UPDATE $updtable SET ";
|
---|
| 1122 | $sql .= join " = ?, ", @fieldlist;
|
---|
[579] | 1123 | # $sql .= " = ? WHERE $keyfield = ? AND rdepth = ? AND vrf = ?";
|
---|
| 1124 | $sql .= " = ? WHERE $keyfield = ? AND rdepth = ? ";
|
---|
[531] | 1125 |
|
---|
| 1126 | eval {
|
---|
| 1127 | # do the update
|
---|
| 1128 | $dbh->do($sql, undef, @vallist);
|
---|
| 1129 |
|
---|
| 1130 | if ($args{node}) {
|
---|
| 1131 | # done with delete/insert so we don't have to worry about funkyness updating a node ref that isn't there
|
---|
| 1132 | $dbh->do("DELETE FROM noderef WHERE block = ?", undef, ($args{block}) );
|
---|
| 1133 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{block}, $args{node}) );
|
---|
| 1134 | }
|
---|
| 1135 |
|
---|
| 1136 | $dbh->commit;
|
---|
| 1137 | };
|
---|
| 1138 | if ($@) {
|
---|
| 1139 | my $msg = $@;
|
---|
| 1140 | $dbh->rollback;
|
---|
| 1141 | return ('FAIL', $msg);
|
---|
| 1142 | }
|
---|
| 1143 | return 0;
|
---|
| 1144 | } # end updateBlock()
|
---|
| 1145 |
|
---|
| 1146 |
|
---|
[93] | 1147 | ## IPDB::deleteBlock()
|
---|
| 1148 | # Removes an allocation from the database, including deleting IPs
|
---|
| 1149 | # from poolips and recombining entries in freeblocks if possible
|
---|
| 1150 | # Also handles "deleting" a static IP allocation, and removal of a master
|
---|
[558] | 1151 | # Requires a database handle, the block to delete, the routing depth (if applicable),
|
---|
| 1152 | # and the VRF ID
|
---|
[93] | 1153 | sub deleteBlock {
|
---|
[558] | 1154 | my ($dbh,undef,$rdepth,$vrf) = @_;
|
---|
[93] | 1155 | my $cidr = new NetAddr::IP $_[1];
|
---|
| 1156 |
|
---|
[558] | 1157 | # For possible auto-VRF-ignoring (since public IPs shouldn't usually be present in more than one VRF)
|
---|
| 1158 | # is_rfc1918 requires NetAddr::IP >= 4.059
|
---|
| 1159 | # rather than doing this over and over and over.....
|
---|
| 1160 | my $tmpnum = $cidr->numeric;
|
---|
| 1161 | # 192.168.0.0/16 -> 192.168.255.255 => 3232235520 -> 3232301055
|
---|
| 1162 | # 172.16.0.0/12 -> 172.31.255.255 => 2886729728 -> 2887778303
|
---|
| 1163 | # 10.0.0.0/8 -> 10.255.255.255 => 167772160 -> 184549375
|
---|
| 1164 | my $isprivnet = (3232235520 <= $tmpnum && $tmpnum <= 3232301055) ||
|
---|
| 1165 | (2886729728 <= $tmpnum && $tmpnum <= 2887778303) ||
|
---|
| 1166 | (167772160 <= $tmpnum && $tmpnum <= 184549375);
|
---|
| 1167 |
|
---|
[93] | 1168 | my $sth;
|
---|
| 1169 |
|
---|
[349] | 1170 | # Magic variables used for odd allocation cases.
|
---|
| 1171 | my $container;
|
---|
| 1172 | my $con_type;
|
---|
| 1173 |
|
---|
[558] | 1174 | # Collect info about the block we're going to delete
|
---|
| 1175 | my $binfo = getBlockData($dbh, $cidr, $rdepth, $vrf);
|
---|
| 1176 |
|
---|
| 1177 | # temporarily forced null, until a sane UI for VRF tracking can be found.
|
---|
| 1178 | $vrf = '';# if !$vrf; # as with SQL, the null value is not equal to ''. *sigh*
|
---|
| 1179 |
|
---|
[93] | 1180 | # To contain the error message, if any.
|
---|
[558] | 1181 | my $msg = "Unknown error deallocating $binfo->{type} $cidr";
|
---|
| 1182 | my $goback; # to put the parent in so we can link back where the deallocate started
|
---|
| 1183 |
|
---|
[93] | 1184 | # Enable transactions and exception-on-errors... but only for this sub
|
---|
| 1185 | local $dbh->{AutoCommit} = 0;
|
---|
| 1186 | local $dbh->{RaiseError} = 1;
|
---|
| 1187 |
|
---|
| 1188 | # First case. The "block" is a static IP
|
---|
| 1189 | # Note that we still need some additional code in the odd case
|
---|
| 1190 | # of a netblock-aligned contiguous group of static IPs
|
---|
[558] | 1191 | if ($binfo->{type} =~ /^.i$/) {
|
---|
[93] | 1192 |
|
---|
| 1193 | eval {
|
---|
[558] | 1194 | $msg = "Unable to deallocate $disp_alloctypes{$binfo->{type}} $cidr";
|
---|
| 1195 | my ($pool,$pcust,$pvrf) = $dbh->selectrow_array("SELECT pool,custid,vrf FROM poolips WHERE ip=?", undef, ($cidr) );
|
---|
| 1196 | ##fixme: VRF and rdepth
|
---|
| 1197 | $dbh->do("UPDATE poolips SET custid=?,available='y',".
|
---|
| 1198 | "city=(SELECT city FROM allocations WHERE cidr=?),".
|
---|
| 1199 | "description='',notes='',circuitid='',vrf=? WHERE ip=?", undef, ($pcust, $pool, $pvrf, $cidr) );
|
---|
| 1200 | $goback = $pool;
|
---|
[93] | 1201 | $dbh->commit;
|
---|
| 1202 | };
|
---|
| 1203 | if ($@) {
|
---|
[558] | 1204 | $msg .= ": $@";
|
---|
[93] | 1205 | eval { $dbh->rollback; };
|
---|
| 1206 | return ('FAIL',$msg);
|
---|
| 1207 | } else {
|
---|
| 1208 | return ('OK',"OK");
|
---|
| 1209 | }
|
---|
| 1210 |
|
---|
[558] | 1211 | } elsif ($binfo->{type} eq 'mm') { # end alloctype =~ /.i/
|
---|
[93] | 1212 |
|
---|
[558] | 1213 | ##fixme: VRF limit
|
---|
[93] | 1214 | $msg = "Unable to delete master block $cidr";
|
---|
| 1215 | eval {
|
---|
[558] | 1216 | $dbh->do("DELETE FROM masterblocks WHERE cidr = ?", undef, ($cidr) );
|
---|
| 1217 | $dbh->do("DELETE FROM allocations WHERE cidr <<= ?", undef, ($cidr) );
|
---|
| 1218 | $dbh->do("DELETE FROM freeblocks WHERE cidr <<= ?", undef, ($cidr) );
|
---|
[93] | 1219 | $dbh->commit;
|
---|
| 1220 | };
|
---|
| 1221 | if ($@) {
|
---|
[558] | 1222 | $msg .= ": $@";
|
---|
[93] | 1223 | eval { $dbh->rollback; };
|
---|
| 1224 | return ('FAIL', $msg);
|
---|
| 1225 | } else {
|
---|
| 1226 | return ('OK',"OK");
|
---|
| 1227 | }
|
---|
| 1228 |
|
---|
| 1229 | } else { # end alloctype master block case
|
---|
| 1230 |
|
---|
| 1231 | ## This is a big block; but it HAS to be done in a chunk. Any removal
|
---|
| 1232 | ## of a netblock allocation may result in a larger chunk of free
|
---|
| 1233 | ## contiguous IP space - which may in turn be combined into a single
|
---|
| 1234 | ## netblock rather than a number of smaller netblocks.
|
---|
| 1235 |
|
---|
[558] | 1236 | my $retcode = 'OK';
|
---|
| 1237 |
|
---|
[93] | 1238 | eval {
|
---|
| 1239 |
|
---|
[558] | 1240 | ##fixme: add recursive flag to allow "YES DAMMIT DELETE ALL EVARYTHING!!1!!" without
|
---|
| 1241 | # explicitly deleting any suballocations of the block to be deleted.
|
---|
[93] | 1242 |
|
---|
[558] | 1243 | # find the current parent of the block we're deleting
|
---|
| 1244 | my ($parent) = $dbh->selectrow_array("SELECT parent FROM allocations WHERE cidr=? AND rdepth=?",
|
---|
| 1245 | undef, ($cidr, $rdepth) );
|
---|
[93] | 1246 |
|
---|
[558] | 1247 | # Delete the block
|
---|
| 1248 | $dbh->do("DELETE FROM allocations WHERE cidr=? AND rdepth=?", undef, ($cidr, $rdepth) );
|
---|
[349] | 1249 |
|
---|
[558] | 1250 | ##fixme: we could maybe eliminate a special case if we put masterblocks in the allocations table...?
|
---|
| 1251 | my ($ptype,$pcity);
|
---|
| 1252 | if ($rdepth == 1) {
|
---|
| 1253 | # parent is a master block.
|
---|
| 1254 | $ptype = 'mm';
|
---|
| 1255 | $pcity = '<NULL>';
|
---|
| 1256 | } else {
|
---|
| 1257 | # get that parent's details
|
---|
| 1258 | ($ptype,$pcity) = $dbh->selectrow_array("SELECT type,city FROM allocations ".
|
---|
| 1259 | "WHERE cidr=? AND rdepth=?", undef, ($parent, $rdepth-1) );
|
---|
| 1260 | }
|
---|
[186] | 1261 |
|
---|
[558] | 1262 | # munge the parent type a little
|
---|
| 1263 | $ptype = (split //, $ptype)[0];
|
---|
[93] | 1264 |
|
---|
[558] | 1265 | ##fixme: you can't... CAN NOT.... assign the same public IP to multiple things.
|
---|
| 1266 | # 'Net don't work like that, homey. Restrict VRF-uniqueness to private IPs?
|
---|
| 1267 | # -> $isprivnet flag from start of sub
|
---|
[93] | 1268 |
|
---|
[558] | 1269 | my $fbrdepth = $rdepth;
|
---|
[404] | 1270 |
|
---|
[558] | 1271 | # check to see if any container allocations could be the "true" parent
|
---|
| 1272 | my ($tparent,$trdepth,$trtype,$tcity) = $dbh->selectrow_array("SELECT cidr,rdepth,type,city FROM allocations ".
|
---|
| 1273 | "WHERE (type='rm' OR type LIKE '_c') AND cidr >> ? ".
|
---|
| 1274 | "ORDER BY masklen(cidr) DESC", undef, ($cidr) );
|
---|
[404] | 1275 |
|
---|
[558] | 1276 | my $fparent;
|
---|
| 1277 | if ($tparent && $tparent ne $parent) {
|
---|
| 1278 | # found an alternate parent; reset some parent-info bits
|
---|
| 1279 | $parent = $tparent;
|
---|
| 1280 | $ptype = (split //, $trtype)[0];
|
---|
| 1281 | $pcity = $tcity;
|
---|
| 1282 | ##fixme: hmm. collect $rdepth into $goback here before vanishing?
|
---|
| 1283 | $retcode = 'WARN'; # may be redundant
|
---|
| 1284 | $goback = $tparent;
|
---|
| 1285 | # munge freeblock rdepth and parent to match true parent
|
---|
| 1286 | $dbh->do("UPDATE freeblocks SET rdepth = ?, parent = ?, routed = ? WHERE cidr <<= ? AND rdepth = ?", undef,
|
---|
| 1287 | ($trdepth+1, $parent, $ptype, $cidr, $rdepth) );
|
---|
| 1288 | $rdepth = $trdepth;
|
---|
| 1289 | $fbrdepth = $trdepth+1;
|
---|
| 1290 | }
|
---|
| 1291 |
|
---|
| 1292 | $parent = new NetAddr::IP $parent;
|
---|
| 1293 | $goback = "$parent,$fbrdepth"; # breadcrumb in case of live-parent-is-not-true-parent
|
---|
| 1294 |
|
---|
| 1295 | # Special case - delete pool IPs
|
---|
| 1296 | if ($binfo->{type} =~ /^.[pd]$/) {
|
---|
| 1297 | # We have to delete the IPs from the pool listing.
|
---|
| 1298 | ##fixme: rdepth? vrf?
|
---|
| 1299 | $dbh->do("DELETE FROM poolips WHERE pool = ?", undef, ($cidr) );
|
---|
| 1300 | }
|
---|
| 1301 |
|
---|
| 1302 | # Find out if the block we're deallocating is within a DSL pool (legacy goo)
|
---|
| 1303 | my ($pool,$poolcity,$pooltype,$pooldepth) = $dbh->selectrow_array(
|
---|
| 1304 | "SELECT cidr,city,type,rdepth FROM allocations WHERE type LIKE '_p' AND cidr >>= ?",
|
---|
| 1305 | undef, ($cidr) );
|
---|
| 1306 |
|
---|
| 1307 | # If so, return the block's IPs to the pool, instead of to freeblocks
|
---|
| 1308 | ## NB: not possible to currently cause this even via admin tools, only legacy data.
|
---|
| 1309 | if ($pool) {
|
---|
| 1310 | ## Deallocate legacy blocks stashed in the middle of a static IP pool
|
---|
| 1311 | ## This may be expandable to an even more general case of contained netblock, or other pool types.
|
---|
| 1312 | $retcode = 'WARNPOOL';
|
---|
| 1313 | $goback = "$pool,$pooldepth";
|
---|
[428] | 1314 | # We've already deleted the block, now we have to stuff its IPs into the pool.
|
---|
| 1315 | $pooltype =~ s/p$/i/; # change type to static IP
|
---|
[558] | 1316 | my $sth2 = $dbh->prepare("INSERT INTO poolips (pool,ip,city,type,custid) VALUES ".
|
---|
[428] | 1317 | "('$pool',?,'$poolcity','$pooltype','$defcustid')");
|
---|
[558] | 1318 | # don't insert .0
|
---|
[429] | 1319 | ##fixme: need to not insert net, gateway, and bcast on "real netblock" pools (DHCPish)
|
---|
[428] | 1320 | $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|;
|
---|
| 1321 | foreach my $ip ($cidr->hostenum) {
|
---|
[558] | 1322 | $sth2->execute($ip);
|
---|
[428] | 1323 | }
|
---|
| 1324 | $cidr--;
|
---|
| 1325 | # don't insert .255
|
---|
| 1326 | $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.255$|;
|
---|
| 1327 | } else { # done returning IPs from a block to a static DSL pool
|
---|
[93] | 1328 |
|
---|
[558] | 1329 | # If the block wasn't legacy goo embedded in a static pool, we check the
|
---|
| 1330 | # freeblocks in the identified parent to see if we can combine any of them.
|
---|
[93] | 1331 |
|
---|
[559] | 1332 | # if the block to be deleted is a container, move its freeblock(s) up a level, and reset their parenting info
|
---|
| 1333 | if ($binfo->{type} =~ /^.[mc]/) {
|
---|
| 1334 | # move the freeblocks into the parent
|
---|
| 1335 | # we don't insert a new freeblock because there could be a live reparented sub.
|
---|
| 1336 | $dbh->do("UPDATE freeblocks SET rdepth=rdepth-1,parent=?,routed=?,city=? ".
|
---|
| 1337 | "WHERE parent=? AND rdepth=?", undef,
|
---|
| 1338 | ($parent, $ptype, $pcity, $cidr, $rdepth+1) );
|
---|
| 1339 | } else {
|
---|
| 1340 | # ... otherwise, add the freeblock
|
---|
| 1341 | $dbh->do("INSERT INTO freeblocks (cidr, city, routed, parent, rdepth) VALUES (?,?,?,?,?)", undef,
|
---|
| 1342 | ($cidr, $pcity, $ptype, $parent, $rdepth) );
|
---|
| 1343 | }
|
---|
| 1344 |
|
---|
[558] | 1345 | ##fixme: vrf
|
---|
| 1346 | # set up the query to get the list of blocks to try to merge.
|
---|
| 1347 | $sth = $dbh->prepare("SELECT cidr FROM freeblocks ".
|
---|
| 1348 | "WHERE parent = ? AND routed = ? AND rdepth = ? ".
|
---|
| 1349 | "ORDER BY masklen(cidr) DESC");
|
---|
[428] | 1350 |
|
---|
[558] | 1351 | $sth->execute($parent, $ptype, $fbrdepth);
|
---|
| 1352 |
|
---|
[93] | 1353 | # NetAddr::IP->compact() attempts to produce the smallest inclusive block
|
---|
| 1354 | # from the caller and the passed terms.
|
---|
| 1355 | # EG: if you call $cidr->compact($ip1,$ip2,$ip3) when $cidr, $ip1, $ip2,
|
---|
| 1356 | # and $ip3 are consecutive /27's starting on .0 (.0-.31, .32-.63,
|
---|
| 1357 | # .64-.95, and .96-.128), you will get an array containing a single
|
---|
| 1358 | # /25 as element 0 (.0-.127). Order is not important; you could have
|
---|
| 1359 | # $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
|
---|
| 1360 |
|
---|
[558] | 1361 | my (@rawfb, @combinelist);
|
---|
[428] | 1362 | my $i=0;
|
---|
[558] | 1363 | # for each free block under $parent, push a NetAddr::IP object into one list, and
|
---|
| 1364 | # continuously use NetAddr::IP->compact to automagically merge netblocks as possible.
|
---|
[428] | 1365 | while (my @data = $sth->fetchrow_array) {
|
---|
| 1366 | my $testIP = new NetAddr::IP $data[0];
|
---|
[558] | 1367 | push @rawfb, $testIP;
|
---|
| 1368 | @combinelist = $testIP->compact(@combinelist);
|
---|
[93] | 1369 | }
|
---|
| 1370 |
|
---|
[558] | 1371 | # now that we have the full list of "compacted" freeblocks, go back over
|
---|
| 1372 | # the list of raw freeblocks, and delete the ones that got merged.
|
---|
| 1373 | $sth = $dbh->prepare("DELETE FROM freeblocks WHERE cidr=? AND parent=? AND rdepth=?");
|
---|
| 1374 | foreach my $rawfree (@rawfb) {
|
---|
| 1375 | next if grep { $rawfree == $_ } @combinelist; # skip if the raw block is in the compacted list
|
---|
| 1376 | $sth->execute($rawfree, $parent, $fbrdepth);
|
---|
| 1377 | }
|
---|
[93] | 1378 |
|
---|
[558] | 1379 | # now we walk the new list of compacted blocks, and see which ones we need to insert
|
---|
| 1380 | $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,parent,rdepth) VALUES (?,?,?,?,?)");
|
---|
| 1381 | foreach my $cme (@combinelist) {
|
---|
| 1382 | next if grep { $cme == $_ } @rawfb; # skip if the combined block was in the raw list
|
---|
| 1383 | $sth->execute($cme, $pcity, $ptype, $parent, $fbrdepth);
|
---|
[428] | 1384 | }
|
---|
[93] | 1385 |
|
---|
[428] | 1386 | } # done returning IPs to the appropriate place
|
---|
[404] | 1387 |
|
---|
[93] | 1388 | # If we got here, we've succeeded. Whew!
|
---|
| 1389 | $dbh->commit;
|
---|
| 1390 | }; # end eval
|
---|
| 1391 | if ($@) {
|
---|
[558] | 1392 | $msg .= ": $@";
|
---|
[93] | 1393 | eval { $dbh->rollback; };
|
---|
| 1394 | return ('FAIL', $msg);
|
---|
| 1395 | } else {
|
---|
[558] | 1396 | return ($retcode, $goback);
|
---|
[93] | 1397 | }
|
---|
| 1398 |
|
---|
| 1399 | } # end alloctype != netblock
|
---|
| 1400 |
|
---|
| 1401 | } # end deleteBlock()
|
---|
| 1402 |
|
---|
| 1403 |
|
---|
[370] | 1404 | ## IPDB::getBlockData()
|
---|
[557] | 1405 | # Get CIDR or IP, custid, type, city, circuit ID, description, notes, modification time,
|
---|
| 1406 | # private/restricted data, for a CIDR block or pool IP
|
---|
| 1407 | # Also returns SWIP status flag for CIDR blocks or pool netblock for IPs
|
---|
| 1408 | # Takes the block/IP to look up, routing depth, and VRF identifier
|
---|
| 1409 | # Returns a hashref to the block data
|
---|
[370] | 1410 | sub getBlockData {
|
---|
| 1411 | my $dbh = shift;
|
---|
| 1412 | my $block = shift;
|
---|
[557] | 1413 | my $rdepth = shift;
|
---|
| 1414 | my $vrf = shift || '';
|
---|
[370] | 1415 |
|
---|
[534] | 1416 | my $cidr = new NetAddr::IP $block;
|
---|
| 1417 |
|
---|
[557] | 1418 | # better way to find IP allocations vs /32 "netblocks"
|
---|
| 1419 | my $btype = $dbh->selectrow_array("SELECT type FROM searchme WHERE cidr=?", undef, ($block) );
|
---|
[534] | 1420 |
|
---|
[557] | 1421 | if (defined($rdepth) && $rdepth == 0) {
|
---|
| 1422 | # Only master blocks exist at rdepth 0
|
---|
| 1423 | my $binfo = $dbh->selectrow_hashref("SELECT cidr AS block, 'mm' AS type, 0 AS parent, cidr,".
|
---|
| 1424 | " ctime, mtime, rwhois, vrf".
|
---|
[579] | 1425 | " FROM masterblocks WHERE cidr = ?", undef, ($block) );
|
---|
| 1426 | # " FROM masterblocks WHERE cidr = ? AND vrf = ?", undef, ($block, $vrf) );
|
---|
[557] | 1427 | return $binfo;
|
---|
| 1428 | } elsif ($btype =~ /^.i$/) {
|
---|
| 1429 | my $binfo = $dbh->selectrow_hashref("SELECT ip AS block, custid, type, city, circuitid, description,".
|
---|
| 1430 | " notes, modifystamp AS lastmod, privdata, vrf, pool, rdepth".
|
---|
[579] | 1431 | " FROM poolips WHERE ip = ?", undef, ($block) );
|
---|
| 1432 | # " FROM poolips WHERE ip = ? AND vrf = ?", undef, ($block, $vrf) );
|
---|
[557] | 1433 | return $binfo;
|
---|
| 1434 | } else {
|
---|
| 1435 | my $binfo = $dbh->selectrow_hashref("SELECT cidr AS block, parent, custid, type, city, circuitid, ".
|
---|
| 1436 | "description, notes, modifystamp AS lastmod, privdata, vrf, swip, rdepth".
|
---|
| 1437 | " FROM allocations WHERE cidr = ? AND rdepth = ?", undef, ($block, $rdepth) );
|
---|
| 1438 | # " FROM allocations WHERE cidr = ? AND rdepth = ? AND vrf = ?", undef, ($block, $rdepth, $vrf) );
|
---|
| 1439 | return $binfo;
|
---|
[534] | 1440 | }
|
---|
[370] | 1441 | } # end getBlockData()
|
---|
| 1442 |
|
---|
| 1443 |
|
---|
[519] | 1444 | ## IPDB::getNodeList()
|
---|
| 1445 | # Gets a list of node ID+name pairs as an arrayref to a list of hashrefs
|
---|
| 1446 | sub getNodeList {
|
---|
| 1447 | my $dbh = shift;
|
---|
| 1448 |
|
---|
| 1449 | my $ret = $dbh->selectall_arrayref("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id",
|
---|
| 1450 | { Slice => {} });
|
---|
| 1451 | return $ret;
|
---|
| 1452 | } # end getNodeList()
|
---|
| 1453 |
|
---|
| 1454 |
|
---|
[530] | 1455 | ## IPDB::getNodeName()
|
---|
| 1456 | # Get node name from the ID
|
---|
| 1457 | sub getNodeName {
|
---|
| 1458 | my $dbh = shift;
|
---|
| 1459 | my $nid = shift;
|
---|
| 1460 |
|
---|
| 1461 | my ($nname) = $dbh->selectrow_array("SELECT node_name FROM nodes WHERE node_id = ?", undef, ($nid) );
|
---|
| 1462 | return $nname;
|
---|
| 1463 | } # end getNodeName()
|
---|
| 1464 |
|
---|
| 1465 |
|
---|
| 1466 | ## IPDB::getNodeInfo()
|
---|
| 1467 | # Get node name and ID associated with a block
|
---|
| 1468 | sub getNodeInfo {
|
---|
| 1469 | my $dbh = shift;
|
---|
| 1470 | my $block = shift;
|
---|
| 1471 |
|
---|
| 1472 | my ($nid, $nname) = $dbh->selectrow_array("SELECT nodes.node_id,node_name FROM nodes INNER JOIN noderef".
|
---|
| 1473 | " ON nodes.node_id=noderef.node_id WHERE noderef.block = ?", undef, ($block) );
|
---|
| 1474 | return ($nid, $nname);
|
---|
| 1475 | } # end getNodeInfo()
|
---|
| 1476 |
|
---|
| 1477 |
|
---|
[77] | 1478 | ## IPDB::mailNotify()
|
---|
[66] | 1479 | # Sends notification mail to recipients regarding an IPDB operation
|
---|
[416] | 1480 | sub mailNotify {
|
---|
| 1481 | my $dbh = shift;
|
---|
| 1482 | my ($action,$subj,$message) = @_;
|
---|
[66] | 1483 |
|
---|
[462] | 1484 | return if $smtphost eq 'smtp.example.com'; # do nothing if still using default SMTP host.
|
---|
| 1485 |
|
---|
[422] | 1486 | ##fixme: need to redesign the breakdown/processing for $action for proper handling of all cases
|
---|
| 1487 |
|
---|
[416] | 1488 | # split action into parts for fiddlement. nb: there are almost certainly better ways to do this.
|
---|
[422] | 1489 | my @actionbits = split //, $action;
|
---|
[416] | 1490 |
|
---|
| 1491 | # want to notify anyone who has specifically requested notify on *this* type ($action as passed),
|
---|
| 1492 | # on "all static IP types" or "all pool types" (and other last-char-in-type groupings), on eg "all DSL types",
|
---|
| 1493 | # and "all events with this action"
|
---|
| 1494 | my @actionsets = ($action);
|
---|
| 1495 | ##fixme: ick, eww. really gotta find a better way to handle this...
|
---|
| 1496 | push @actionsets, ($actionbits[0].'.'.$actionbits[2],
|
---|
| 1497 | $actionbits[0].$actionbits[1].'.', $actionbits[0].'a') if $action =~ /^.{3}$/;
|
---|
| 1498 |
|
---|
| 1499 | my $mailer = Net::SMTP->new($smtphost, Hello => "ipdb.$domain");
|
---|
| 1500 |
|
---|
| 1501 | # get recip list from db
|
---|
| 1502 | my $sth = $dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
|
---|
| 1503 |
|
---|
[443] | 1504 | my %reciplist;
|
---|
[416] | 1505 | foreach (@actionsets) {
|
---|
[426] | 1506 | $sth->execute($_);
|
---|
[416] | 1507 | ##fixme - need to handle db errors
|
---|
| 1508 | my ($recipsub) = $sth->fetchrow_array;
|
---|
| 1509 | next if !$recipsub;
|
---|
| 1510 | foreach (split(/,/, $recipsub)) {
|
---|
[443] | 1511 | $reciplist{$_}++;
|
---|
[416] | 1512 | }
|
---|
| 1513 | }
|
---|
| 1514 |
|
---|
[443] | 1515 | return if !%reciplist;
|
---|
[420] | 1516 |
|
---|
[443] | 1517 | foreach my $recip (keys %reciplist) {
|
---|
[416] | 1518 | $mailer->mail("ipdb\@$domain");
|
---|
| 1519 | $mailer->to($recip);
|
---|
[422] | 1520 | $mailer->data("From: \"$org_name IP Database\" <ipdb\@$domain>\n",
|
---|
[135] | 1521 | "To: $recip\n",
|
---|
[69] | 1522 | "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
|
---|
| 1523 | "Subject: {IPDB} $subj\n",
|
---|
| 1524 | "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n",
|
---|
[417] | 1525 | "Organization: $org_name\n",
|
---|
[69] | 1526 | "\n$message\n");
|
---|
[416] | 1527 | }
|
---|
[66] | 1528 | $mailer->quit;
|
---|
| 1529 | }
|
---|
| 1530 |
|
---|
[4] | 1531 | # Indicates module loaded OK. Required by Perl.
|
---|
| 1532 | 1;
|
---|