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