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