[8] | 1 | # ipdb/cgi-bin/IPDB.pm
|
---|
[66] | 2 | # Contains functions for IPDB - database access, subnet mangling, block allocation, etc
|
---|
[8] | 3 | ###
|
---|
| 4 | # SVN revision info
|
---|
| 5 | # $Date: 2013-01-25 21:16:04 +0000 (Fri, 25 Jan 2013) $
|
---|
| 6 | # SVN revision $Rev: 591 $
|
---|
| 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 |
|
---|
| 426 | my $mlist = $dbh->selectall_arrayref("SELECT cidr AS master FROM masterblocks ORDER BY cidr", { Slice => {} });
|
---|
| 427 |
|
---|
| 428 | foreach (@{$mlist}) {
|
---|
[560] | 429 | my ($rcnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? AND type='rm'",
|
---|
| 430 | undef, ($$_{master}));
|
---|
[523] | 431 | $$_{routed} = $rcnt;
|
---|
[560] | 432 | my ($acnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? AND NOT type='rm'",
|
---|
| 433 | undef, ($$_{master}));
|
---|
[523] | 434 | $$_{allocated} = $acnt;
|
---|
[560] | 435 | my ($fcnt) = $dbh->selectrow_array("SELECT count(*) FROM freeblocks WHERE cidr <<= ?",
|
---|
| 436 | undef, ($$_{master}));
|
---|
[523] | 437 | $$_{free} = $fcnt;
|
---|
[560] | 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}));
|
---|
[523] | 440 | ##fixme: should find a way to do this without having to HTMLize the <>
|
---|
| 441 | $bigfree = "/$bigfree" if $bigfree;
|
---|
[525] | 442 | $bigfree = '<NONE>' if !$bigfree;
|
---|
[523] | 443 | $$_{bigfree} = $bigfree;
|
---|
| 444 | }
|
---|
| 445 | return $mlist;
|
---|
| 446 | } # end listSummary()
|
---|
| 447 |
|
---|
| 448 |
|
---|
[561] | 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 |
|
---|
[524] | 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;
|
---|
[523] | 499 |
|
---|
[524] | 500 | my $rlist = $dbh->selectall_arrayref("SELECT cidr AS block,city FROM routed WHERE cidr <<= ? ORDER BY cidr",
|
---|
| 501 | { Slice => {} }, ($master) );
|
---|
[523] | 502 |
|
---|
[524] | 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;
|
---|
[525] | 513 | $bigfree = '<NONE>' if !$bigfree;
|
---|
[524] | 514 | $$_{lfree} = $bigfree;
|
---|
| 515 | }
|
---|
| 516 | return $rlist;
|
---|
| 517 | } # end listMaster()
|
---|
| 518 |
|
---|
| 519 |
|
---|
[527] | 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;
|
---|
[524] | 528 |
|
---|
[527] | 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);
|
---|
[524] | 533 |
|
---|
[527] | 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 |
|
---|
[524] | 559 | ## IPDB::listFree()
|
---|
[562] | 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.
|
---|
[524] | 562 | # Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks
|
---|
[527] | 563 | # Returns some extra flags in the hashrefs for routed blocks, since those can have several subtypes
|
---|
[524] | 564 | sub listFree {
|
---|
| 565 | my $dbh = shift;
|
---|
| 566 |
|
---|
[562] | 567 | my %args = @_;
|
---|
| 568 | # Just In Case
|
---|
| 569 | $args{vrf} = '' if !$args{vrf};
|
---|
| 570 | $args{rdepth} = 1 if !$args{rdepth};
|
---|
| 571 |
|
---|
[524] | 572 | # do it this way so we can waste a little less time iterating
|
---|
[579] | 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 = ? ".
|
---|
[562] | 575 | "ORDER BY cidr");
|
---|
[579] | 576 | # $sth->execute($args{master}, $args{rdepth}, $args{vrf});
|
---|
| 577 | $sth->execute($args{master}, $args{rdepth});
|
---|
[524] | 578 | my @flist;
|
---|
[562] | 579 | while (my ($cidr) = $sth->fetchrow_array()) {
|
---|
[524] | 580 | $cidr = new NetAddr::IP $cidr;
|
---|
[527] | 581 | my %row = (
|
---|
| 582 | fblock => "$cidr",
|
---|
| 583 | frange => $cidr->range,
|
---|
| 584 | );
|
---|
[524] | 585 | push @flist, \%row;
|
---|
| 586 | }
|
---|
| 587 | return \@flist;
|
---|
[527] | 588 | } # end listFree()
|
---|
[524] | 589 |
|
---|
| 590 |
|
---|
[528] | 591 | ## IPDB::listPool()
|
---|
| 592 | #
|
---|
| 593 | sub listPool {
|
---|
| 594 | my $dbh = shift;
|
---|
| 595 | my $pool = shift;
|
---|
| 596 |
|
---|
[563] | 597 | my $sth = $dbh->prepare("SELECT ip,custid,available,description,type,rdepth".
|
---|
[528] | 598 | " FROM poolips WHERE pool = ? ORDER BY ip");
|
---|
| 599 | $sth->execute($pool);
|
---|
| 600 | my @poolips;
|
---|
[563] | 601 | while (my ($ip,$custid,$available,$desc,$type,$rdepth) = $sth->fetchrow_array) {
|
---|
[528] | 602 | my %row = (
|
---|
| 603 | ip => $ip,
|
---|
| 604 | custid => $custid,
|
---|
| 605 | available => $available,
|
---|
| 606 | desc => $desc,
|
---|
[563] | 607 | delme => $available eq 'n',
|
---|
| 608 | ipdepth => $rdepth,
|
---|
[528] | 609 | );
|
---|
| 610 | push @poolips, \%row;
|
---|
| 611 | }
|
---|
| 612 | return \@poolips;
|
---|
| 613 | } # end listPool()
|
---|
| 614 |
|
---|
| 615 |
|
---|
[541] | 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 |
|
---|
[529] | 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;
|
---|
[564] | 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 ".
|
---|
[529] | 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 |
|
---|
[532] | 664 | ## IPDB::getPoolSelect()
|
---|
| 665 | # Get a list of pools matching the passed city and type that have 1 or more free IPs
|
---|
[533] | 666 | # Returns an arrayref to a list of hashrefs
|
---|
[532] | 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(
|
---|
[572] | 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) );
|
---|
[532] | 683 | return $plist;
|
---|
| 684 | } # end getPoolSelect()
|
---|
| 685 |
|
---|
| 686 |
|
---|
[533] | 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 |
|
---|
[572] | 711 | my @vallist = ($maskbits);
|
---|
| 712 | my $sql = "SELECT cidr,rdepth FROM freeblocks WHERE masklen(cidr) <= ?";
|
---|
[533] | 713 |
|
---|
[572] | 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 |
|
---|
[533] | 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]$/;
|
---|
[544] | 746 | if ($type ne 'rm' && $city) {
|
---|
[533] | 747 | $sql .= " AND city = ?";
|
---|
| 748 | push @vallist, $city;
|
---|
| 749 | }
|
---|
[544] | 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 | }
|
---|
[533] | 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 |
|
---|
[572] | 770 | my ($fbfound,$fbdepth) = $dbh->selectrow_array($sql, undef, @vallist);
|
---|
| 771 | return $fbfound,$fbdepth;
|
---|
[533] | 772 | } # end findAllocateFrom()
|
---|
| 773 |
|
---|
| 774 |
|
---|
[536] | 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".
|
---|
[565] | 784 | " WHERE cidr >>= ? AND (type LIKE '_p' OR type LIKE '_d')", undef, ($block) );
|
---|
[536] | 785 | return $pinfo;
|
---|
| 786 | } # end ipParent()
|
---|
| 787 |
|
---|
| 788 |
|
---|
| 789 | ## IPDB::subParent()
|
---|
[529] | 790 | # Get a block's parent's details
|
---|
| 791 | # Takes a database handle and CIDR block
|
---|
[536] | 792 | # Returns a hashref to the parent container block, if any
|
---|
| 793 | sub subParent {
|
---|
[529] | 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;
|
---|
[536] | 800 | } # end subParent()
|
---|
[529] | 801 |
|
---|
| 802 |
|
---|
[536] | 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 |
|
---|
[527] | 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 |
|
---|
[77] | 828 | ## IPDB::allocateBlock()
|
---|
[66] | 829 | # Does all of the magic of actually allocating a netblock
|
---|
[554] | 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
|
---|
[77] | 833 | # Returns a success code and optional error message.
|
---|
| 834 | sub allocateBlock {
|
---|
[554] | 835 | my $dbh = shift;
|
---|
[284] | 836 |
|
---|
[554] | 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};
|
---|
[585] | 847 | $args{rdns} = '' if !$args{rdns};
|
---|
[554] | 848 |
|
---|
[77] | 849 | my $sth;
|
---|
[66] | 850 |
|
---|
[349] | 851 | # Snag the "type" of the freeblock (alloc_from) "just in case"
|
---|
[554] | 852 | $sth = $dbh->prepare("select routed from freeblocks where cidr='$args{alloc_from}'");
|
---|
[349] | 853 | $sth->execute;
|
---|
| 854 | my ($alloc_from_type) = $sth->fetchrow_array;
|
---|
| 855 |
|
---|
[79] | 856 | # To contain the error message, if any.
|
---|
[554] | 857 | my $msg = "Unknown error allocating $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
[79] | 858 |
|
---|
[77] | 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.
|
---|
[66] | 862 |
|
---|
[554] | 863 | if ($args{type} =~ /^.i$/) {
|
---|
| 864 | $msg = "Unable to assign static IP $args{cidr} to $args{custid}";
|
---|
[77] | 865 | eval {
|
---|
[554] | 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}) );
|
---|
[545] | 875 | }
|
---|
[585] | 876 | $dbh->do("UPDATE poolips SET custid=?,city=?,available='n',description=?,notes=?,circuitid=?,privdata=?,vrf=?,rdns=? ".
|
---|
[554] | 877 | "WHERE ip=?", undef, ($args{custid}, $args{city}, $args{desc}, $args{notes}, $args{circid},
|
---|
[585] | 878 | $args{privdata}, $args{vrf}, $args{rdns}, $args{cidr}) );
|
---|
[157] | 879 |
|
---|
[397] | 880 | # node hack
|
---|
[554] | 881 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 882 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 883 | }
|
---|
| 884 | # end node hack
|
---|
[545] | 885 |
|
---|
[79] | 886 | $dbh->commit;
|
---|
[77] | 887 | };
|
---|
| 888 | if ($@) {
|
---|
[545] | 889 | $msg .= ": $@";
|
---|
[78] | 890 | eval { $dbh->rollback; };
|
---|
[578] | 891 | return ('FAIL', $msg);
|
---|
[77] | 892 | } else {
|
---|
[585] | 893 | _rpc('addOrUpdateRevRec', cidr => "$args{cidr}", name => $args{rdns}, rpcuser => $args{user});
|
---|
[578] | 894 | return ('OK', $args{cidr});
|
---|
[77] | 895 | }
|
---|
| 896 |
|
---|
| 897 | } else { # end IP-from-pool allocation
|
---|
| 898 |
|
---|
[554] | 899 | if ($args{cidr} == $args{alloc_from}) {
|
---|
[77] | 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 {
|
---|
[554] | 906 | $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
| 907 |
|
---|
[555] | 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 |
|
---|
[554] | 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=?",
|
---|
[578] | 917 | undef, ($rtype, $args{city}, $args{cidr}, $args{cidr}, $args{rdepth}) );
|
---|
[77] | 918 | } else {
|
---|
[554] | 919 | # "normal" case
|
---|
| 920 | $dbh->do("DELETE FROM freeblocks WHERE cidr=? AND rdepth=?", undef, ($args{cidr}, $args{rdepth}));
|
---|
| 921 | }
|
---|
[77] | 922 |
|
---|
[554] | 923 | # Insert the allocations entry
|
---|
| 924 | $dbh->do("INSERT INTO allocations ".
|
---|
[585] | 925 | "(cidr,parent,vrf,rdepth,custid,type,city,description,notes,circuitid,privdata,rdns)".
|
---|
| 926 | " VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", undef,
|
---|
[554] | 927 | ($args{cidr}, $fparent, $args{vrf}, $args{rdepth}, $args{custid}, $args{type}, $args{city},
|
---|
[585] | 928 | $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
|
---|
[79] | 929 |
|
---|
[554] | 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}";
|
---|
[574] | 935 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $args{rdepth}+1);
|
---|
[554] | 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}";
|
---|
[574] | 939 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $args{rdepth}+1);
|
---|
[554] | 940 | die $rmsg if $code eq 'FAIL';
|
---|
| 941 | }
|
---|
[79] | 942 |
|
---|
[397] | 943 | # node hack
|
---|
[554] | 944 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 945 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 946 | }
|
---|
| 947 | # end node hack
|
---|
[574] | 948 |
|
---|
[77] | 949 | $dbh->commit;
|
---|
[78] | 950 | }; # end of eval
|
---|
[77] | 951 | if ($@) {
|
---|
[157] | 952 | $msg .= ": ".$@;
|
---|
[77] | 953 | eval { $dbh->rollback; };
|
---|
[157] | 954 | return ('FAIL',$msg);
|
---|
[78] | 955 | }
|
---|
[77] | 956 |
|
---|
| 957 | } else { # cidr != alloc_from
|
---|
| 958 |
|
---|
| 959 | # Hard case. Allocation is smaller than free block.
|
---|
[554] | 960 | my $wantmaskbits = $args{cidr}->masklen;
|
---|
| 961 | my $maskbits = $args{alloc_from}->masklen;
|
---|
[77] | 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;
|
---|
[554] | 969 | my $tmp_from = $args{alloc_from}; # So we don't munge $args{alloc_from}
|
---|
[77] | 970 | while ($maskbits++ < $wantmaskbits) {
|
---|
| 971 | my @subblocks = $tmp_from->split($maskbits);
|
---|
[554] | 972 | $newfreeblocks[$i++] = (($args{cidr}->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
|
---|
| 973 | $tmp_from = ( ($args{cidr}->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
|
---|
[77] | 974 | } # while
|
---|
| 975 |
|
---|
| 976 | # Begin SQL transaction block
|
---|
| 977 | eval {
|
---|
[554] | 978 | $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
[79] | 979 |
|
---|
[554] | 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 |
|
---|
[77] | 984 | # Delete old freeblocks entry
|
---|
[554] | 985 | $dbh->do("DELETE FROM freeblocks WHERE cidr=? AND rdepth=?", undef, ($args{alloc_from}, $args{rdepth}) );
|
---|
[77] | 986 |
|
---|
[554] | 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 | }
|
---|
[79] | 992 |
|
---|
[554] | 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 | }
|
---|
[79] | 998 |
|
---|
[554] | 999 | # Insert the allocations entry
|
---|
| 1000 | $dbh->do("INSERT INTO allocations ".
|
---|
[585] | 1001 | "(cidr,parent,vrf,rdepth,custid,type,city,description,notes,circuitid,privdata,rdns)".
|
---|
| 1002 | " VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", undef,
|
---|
[554] | 1003 | ($args{cidr}, $fparent, $args{vrf}, $args{rdepth}, $args{custid}, $args{type}, $args{city},
|
---|
[585] | 1004 | $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
|
---|
[77] | 1005 |
|
---|
[554] | 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}";
|
---|
[574] | 1011 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $args{rdepth}+1);
|
---|
[554] | 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}";
|
---|
[574] | 1015 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $args{rdepth}+1);
|
---|
[554] | 1016 | die $rmsg if $code eq 'FAIL';
|
---|
| 1017 | }
|
---|
[77] | 1018 |
|
---|
[397] | 1019 | # node hack
|
---|
[554] | 1020 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 1021 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 1022 | }
|
---|
| 1023 | # end node hack
|
---|
[554] | 1024 |
|
---|
[77] | 1025 | $dbh->commit;
|
---|
| 1026 | }; # end eval
|
---|
| 1027 | if ($@) {
|
---|
[256] | 1028 | $msg .= ": ".$@;
|
---|
[77] | 1029 | eval { $dbh->rollback; };
|
---|
[78] | 1030 | return ('FAIL',$msg);
|
---|
[77] | 1031 | }
|
---|
| 1032 |
|
---|
| 1033 | } # end fullcidr != alloc_from
|
---|
| 1034 |
|
---|
[590] | 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};
|
---|
[585] | 1038 |
|
---|
| 1039 | return ('OK', 'OK');
|
---|
| 1040 |
|
---|
[77] | 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
|
---|
[78] | 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()
|
---|
[77] | 1053 | sub initPool {
|
---|
[574] | 1054 | my ($dbh,undef,$type,$city,$class,$rdepth) = @_;
|
---|
[77] | 1055 | my $pool = new NetAddr::IP $_[1];
|
---|
| 1056 |
|
---|
[574] | 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 |
|
---|
[157] | 1063 | ##fixme Need to just replace 2nd char of type with i rather than capturing 1st char of type
|
---|
| 1064 | $type =~ s/[pd]$/i/;
|
---|
[77] | 1065 | my $sth;
|
---|
[157] | 1066 | my $msg;
|
---|
[77] | 1067 |
|
---|
[157] | 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".
|
---|
[574] | 1076 | $sth = $dbh->prepare("INSERT INTO poolips (pool,ip,custid,city,type,rdepth)".
|
---|
| 1077 | " VALUES ('$pool', ?, '$defcustid', ?, '$type', $rdepth)");
|
---|
[157] | 1078 | my @poolip_list = $pool->hostenum;
|
---|
| 1079 | if ($class eq 'all') { # (DSL-ish block - *all* IPs available
|
---|
[246] | 1080 | if ($pool->addr !~ /\.0$/) { # .0 causes weirdness.
|
---|
[485] | 1081 | $sth->execute($pool->addr, $city);
|
---|
[246] | 1082 | }
|
---|
[157] | 1083 | for (my $i=0; $i<=$#poolip_list; $i++) {
|
---|
[485] | 1084 | $sth->execute($poolip_list[$i]->addr, $city);
|
---|
[157] | 1085 | }
|
---|
| 1086 | $pool--;
|
---|
[246] | 1087 | if ($pool->addr !~ /\.255$/) { # .255 can cause weirdness.
|
---|
[485] | 1088 | $sth->execute($pool->addr, $city);
|
---|
[246] | 1089 | }
|
---|
[157] | 1090 | } else { # (real netblock)
|
---|
| 1091 | for (my $i=1; $i<=$#poolip_list; $i++) {
|
---|
[485] | 1092 | $sth->execute($poolip_list[$i]->addr, $city);
|
---|
[157] | 1093 | }
|
---|
[77] | 1094 | }
|
---|
[574] | 1095 | $dbh->commit;
|
---|
[157] | 1096 | };
|
---|
| 1097 | if ($@) {
|
---|
[574] | 1098 | $msg = $@;
|
---|
[157] | 1099 | eval { $dbh->rollback; };
|
---|
| 1100 | return ('FAIL',$msg);
|
---|
| 1101 | } else {
|
---|
| 1102 | return ('OK',"OK");
|
---|
[77] | 1103 | }
|
---|
| 1104 | } # end initPool()
|
---|
| 1105 |
|
---|
| 1106 |
|
---|
[531] | 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;
|
---|
[588] | 1122 | foreach ('custid', 'city', 'description', 'notes', 'circuitid', 'privdata', 'rdns') {
|
---|
[531] | 1123 | if ($args{$_}) {
|
---|
| 1124 | push @fieldlist, $_;
|
---|
| 1125 | push @vallist, $args{$_};
|
---|
| 1126 | }
|
---|
| 1127 | }
|
---|
| 1128 |
|
---|
| 1129 | my $updtable = 'allocations';
|
---|
| 1130 | my $keyfield = 'cidr';
|
---|
[535] | 1131 | if ($args{type} =~ /^(.)i$/) {
|
---|
[531] | 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 |
|
---|
[579] | 1153 | # push @vallist, $args{block}, $args{rdepth}, $args{vrf};
|
---|
| 1154 | push @vallist, $args{block}, $args{rdepth};
|
---|
[531] | 1155 | my $sql = "UPDATE $updtable SET ";
|
---|
| 1156 | $sql .= join " = ?, ", @fieldlist;
|
---|
[579] | 1157 | # $sql .= " = ? WHERE $keyfield = ? AND rdepth = ? AND vrf = ?";
|
---|
| 1158 | $sql .= " = ? WHERE $keyfield = ? AND rdepth = ? ";
|
---|
[531] | 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 | }
|
---|
[588] | 1177 |
|
---|
| 1178 | _rpc('addOrUpdateRevRec', cidr => $args{block}, name => $args{rdns}, rpcuser => $args{user});
|
---|
| 1179 | return ('OK','OK');
|
---|
[531] | 1180 | } # end updateBlock()
|
---|
| 1181 |
|
---|
| 1182 |
|
---|
[93] | 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
|
---|
[558] | 1187 | # Requires a database handle, the block to delete, the routing depth (if applicable),
|
---|
[590] | 1188 | # the VRF ID, and a flag to indicate whether to delete associated forward DNS entries
|
---|
| 1189 | # as well as the reverse entry
|
---|
[93] | 1190 | sub deleteBlock {
|
---|
[590] | 1191 | my ($dbh,undef,$rdepth,$vrf,$delfwd,$user) = @_;
|
---|
[93] | 1192 | my $cidr = new NetAddr::IP $_[1];
|
---|
| 1193 |
|
---|
[558] | 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 |
|
---|
[93] | 1205 | my $sth;
|
---|
| 1206 |
|
---|
[349] | 1207 | # Magic variables used for odd allocation cases.
|
---|
| 1208 | my $container;
|
---|
| 1209 | my $con_type;
|
---|
| 1210 |
|
---|
[558] | 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 |
|
---|
[93] | 1217 | # To contain the error message, if any.
|
---|
[558] | 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 |
|
---|
[93] | 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
|
---|
[558] | 1228 | if ($binfo->{type} =~ /^.i$/) {
|
---|
[93] | 1229 |
|
---|
| 1230 | eval {
|
---|
[558] | 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;
|
---|
[93] | 1238 | $dbh->commit;
|
---|
| 1239 | };
|
---|
| 1240 | if ($@) {
|
---|
[558] | 1241 | $msg .= ": $@";
|
---|
[93] | 1242 | eval { $dbh->rollback; };
|
---|
| 1243 | return ('FAIL',$msg);
|
---|
| 1244 | } else {
|
---|
[590] | 1245 | ##fixme: RPC return code?
|
---|
| 1246 | _rpc('delByCIDR', cidr => "$cidr", user => $user, delforward => $delfwd);
|
---|
[93] | 1247 | return ('OK',"OK");
|
---|
| 1248 | }
|
---|
| 1249 |
|
---|
[558] | 1250 | } elsif ($binfo->{type} eq 'mm') { # end alloctype =~ /.i/
|
---|
[93] | 1251 |
|
---|
[558] | 1252 | ##fixme: VRF limit
|
---|
[93] | 1253 | $msg = "Unable to delete master block $cidr";
|
---|
| 1254 | eval {
|
---|
[558] | 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) );
|
---|
[93] | 1258 | $dbh->commit;
|
---|
| 1259 | };
|
---|
| 1260 | if ($@) {
|
---|
[558] | 1261 | $msg .= ": $@";
|
---|
[93] | 1262 | eval { $dbh->rollback; };
|
---|
| 1263 | return ('FAIL', $msg);
|
---|
[591] | 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);
|
---|
[93] | 1274 | } else {
|
---|
[591] | 1275 | @zonelist = ($cidr);
|
---|
[93] | 1276 | }
|
---|
[591] | 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');
|
---|
[93] | 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 |
|
---|
[558] | 1295 | my $retcode = 'OK';
|
---|
[591] | 1296 | my ($ptype,$pcity,$ppatt);
|
---|
[558] | 1297 |
|
---|
[93] | 1298 | eval {
|
---|
| 1299 |
|
---|
[558] | 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.
|
---|
[93] | 1302 |
|
---|
[558] | 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) );
|
---|
[93] | 1306 |
|
---|
[558] | 1307 | # Delete the block
|
---|
| 1308 | $dbh->do("DELETE FROM allocations WHERE cidr=? AND rdepth=?", undef, ($cidr, $rdepth) );
|
---|
[349] | 1309 |
|
---|
[558] | 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>';
|
---|
[591] | 1315 | $ppatt = $dbh->selectrow_array("SELECT rdns FROM masterblocks WHERE cidr=?", undef, ($parent) );
|
---|
[558] | 1316 | } else {
|
---|
| 1317 | # get that parent's details
|
---|
[591] | 1318 | ($ptype,$pcity,$ppatt) = $dbh->selectrow_array("SELECT type,city,rdns FROM allocations ".
|
---|
[558] | 1319 | "WHERE cidr=? AND rdepth=?", undef, ($parent, $rdepth-1) );
|
---|
| 1320 | }
|
---|
[186] | 1321 |
|
---|
[558] | 1322 | # munge the parent type a little
|
---|
| 1323 | $ptype = (split //, $ptype)[0];
|
---|
[93] | 1324 |
|
---|
[558] | 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
|
---|
[93] | 1328 |
|
---|
[558] | 1329 | my $fbrdepth = $rdepth;
|
---|
[404] | 1330 |
|
---|
[558] | 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) );
|
---|
[404] | 1335 |
|
---|
[558] | 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?
|
---|
[591] | 1343 | $retcode = 'WARNMERGE'; # may be redundant
|
---|
[558] | 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";
|
---|
[428] | 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
|
---|
[558] | 1376 | my $sth2 = $dbh->prepare("INSERT INTO poolips (pool,ip,city,type,custid) VALUES ".
|
---|
[428] | 1377 | "('$pool',?,'$poolcity','$pooltype','$defcustid')");
|
---|
[558] | 1378 | # don't insert .0
|
---|
[429] | 1379 | ##fixme: need to not insert net, gateway, and bcast on "real netblock" pools (DHCPish)
|
---|
[428] | 1380 | $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|;
|
---|
| 1381 | foreach my $ip ($cidr->hostenum) {
|
---|
[558] | 1382 | $sth2->execute($ip);
|
---|
[428] | 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
|
---|
[93] | 1388 |
|
---|
[558] | 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.
|
---|
[93] | 1391 |
|
---|
[559] | 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 |
|
---|
[558] | 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");
|
---|
[428] | 1410 |
|
---|
[558] | 1411 | $sth->execute($parent, $ptype, $fbrdepth);
|
---|
| 1412 |
|
---|
[93] | 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 |
|
---|
[558] | 1421 | my (@rawfb, @combinelist);
|
---|
[428] | 1422 | my $i=0;
|
---|
[558] | 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.
|
---|
[428] | 1425 | while (my @data = $sth->fetchrow_array) {
|
---|
| 1426 | my $testIP = new NetAddr::IP $data[0];
|
---|
[558] | 1427 | push @rawfb, $testIP;
|
---|
| 1428 | @combinelist = $testIP->compact(@combinelist);
|
---|
[93] | 1429 | }
|
---|
| 1430 |
|
---|
[558] | 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 | }
|
---|
[93] | 1438 |
|
---|
[558] | 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);
|
---|
[428] | 1444 | }
|
---|
[93] | 1445 |
|
---|
[428] | 1446 | } # done returning IPs to the appropriate place
|
---|
[404] | 1447 |
|
---|
[93] | 1448 | # If we got here, we've succeeded. Whew!
|
---|
| 1449 | $dbh->commit;
|
---|
| 1450 | }; # end eval
|
---|
| 1451 | if ($@) {
|
---|
[558] | 1452 | $msg .= ": $@";
|
---|
[93] | 1453 | eval { $dbh->rollback; };
|
---|
| 1454 | return ('FAIL', $msg);
|
---|
| 1455 | } else {
|
---|
[590] | 1456 | ##fixme: RPC return code?
|
---|
[591] | 1457 | _rpc('delByCIDR', cidr => "$cidr", user => $user, delforward => $delfwd, delsubs => 'y', parpatt => $ppatt);
|
---|
[558] | 1458 | return ($retcode, $goback);
|
---|
[93] | 1459 | }
|
---|
| 1460 |
|
---|
| 1461 | } # end alloctype != netblock
|
---|
| 1462 |
|
---|
| 1463 | } # end deleteBlock()
|
---|
| 1464 |
|
---|
| 1465 |
|
---|
[370] | 1466 | ## IPDB::getBlockData()
|
---|
[557] | 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
|
---|
[370] | 1472 | sub getBlockData {
|
---|
| 1473 | my $dbh = shift;
|
---|
| 1474 | my $block = shift;
|
---|
[557] | 1475 | my $rdepth = shift;
|
---|
| 1476 | my $vrf = shift || '';
|
---|
[370] | 1477 |
|
---|
[534] | 1478 | my $cidr = new NetAddr::IP $block;
|
---|
| 1479 |
|
---|
[557] | 1480 | # better way to find IP allocations vs /32 "netblocks"
|
---|
| 1481 | my $btype = $dbh->selectrow_array("SELECT type FROM searchme WHERE cidr=?", undef, ($block) );
|
---|
[534] | 1482 |
|
---|
[557] | 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".
|
---|
[579] | 1487 | " FROM masterblocks WHERE cidr = ?", undef, ($block) );
|
---|
| 1488 | # " FROM masterblocks WHERE cidr = ? AND vrf = ?", undef, ($block, $vrf) );
|
---|
[557] | 1489 | return $binfo;
|
---|
| 1490 | } elsif ($btype =~ /^.i$/) {
|
---|
| 1491 | my $binfo = $dbh->selectrow_hashref("SELECT ip AS block, custid, type, city, circuitid, description,".
|
---|
[586] | 1492 | " notes, modifystamp AS lastmod, privdata, vrf, pool, rdepth, rdns".
|
---|
[579] | 1493 | " FROM poolips WHERE ip = ?", undef, ($block) );
|
---|
| 1494 | # " FROM poolips WHERE ip = ? AND vrf = ?", undef, ($block, $vrf) );
|
---|
[557] | 1495 | return $binfo;
|
---|
| 1496 | } else {
|
---|
| 1497 | my $binfo = $dbh->selectrow_hashref("SELECT cidr AS block, parent, custid, type, city, circuitid, ".
|
---|
[586] | 1498 | "description, notes, modifystamp AS lastmod, privdata, vrf, swip, rdepth, rdns".
|
---|
[557] | 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;
|
---|
[534] | 1502 | }
|
---|
[370] | 1503 | } # end getBlockData()
|
---|
| 1504 |
|
---|
| 1505 |
|
---|
[585] | 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 |
|
---|
[586] | 1528 | my ($rpcblock) = ($cidr->masklen <= 24 ? $cidr->split( ($cidr->masklen <= 16 ? 16 : 24) ) : $cidr);
|
---|
| 1529 |
|
---|
[585] | 1530 | my %rpcargs = (
|
---|
| 1531 | rpcuser => $args{user},
|
---|
| 1532 | group => $revgroup, # not sure how this could sanely be exposed, tbh...
|
---|
| 1533 | cidr => "$rpcblock",
|
---|
| 1534 | );
|
---|
| 1535 |
|
---|
[586] | 1536 | $rdns = _rpc('getRevPattern', %rpcargs);
|
---|
[585] | 1537 | }
|
---|
| 1538 |
|
---|
| 1539 | # hmm. do we care about where it actually came from?
|
---|
| 1540 | return $rdns;
|
---|
| 1541 | } # end getBlockRDNS()
|
---|
| 1542 |
|
---|
| 1543 |
|
---|
[519] | 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 |
|
---|
[530] | 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 |
|
---|
[77] | 1578 | ## IPDB::mailNotify()
|
---|
[66] | 1579 | # Sends notification mail to recipients regarding an IPDB operation
|
---|
[416] | 1580 | sub mailNotify {
|
---|
| 1581 | my $dbh = shift;
|
---|
| 1582 | my ($action,$subj,$message) = @_;
|
---|
[66] | 1583 |
|
---|
[462] | 1584 | return if $smtphost eq 'smtp.example.com'; # do nothing if still using default SMTP host.
|
---|
| 1585 |
|
---|
[422] | 1586 | ##fixme: need to redesign the breakdown/processing for $action for proper handling of all cases
|
---|
| 1587 |
|
---|
[416] | 1588 | # split action into parts for fiddlement. nb: there are almost certainly better ways to do this.
|
---|
[422] | 1589 | my @actionbits = split //, $action;
|
---|
[416] | 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 |
|
---|
[443] | 1604 | my %reciplist;
|
---|
[416] | 1605 | foreach (@actionsets) {
|
---|
[426] | 1606 | $sth->execute($_);
|
---|
[416] | 1607 | ##fixme - need to handle db errors
|
---|
| 1608 | my ($recipsub) = $sth->fetchrow_array;
|
---|
| 1609 | next if !$recipsub;
|
---|
| 1610 | foreach (split(/,/, $recipsub)) {
|
---|
[443] | 1611 | $reciplist{$_}++;
|
---|
[416] | 1612 | }
|
---|
| 1613 | }
|
---|
| 1614 |
|
---|
[443] | 1615 | return if !%reciplist;
|
---|
[420] | 1616 |
|
---|
[443] | 1617 | foreach my $recip (keys %reciplist) {
|
---|
[416] | 1618 | $mailer->mail("ipdb\@$domain");
|
---|
| 1619 | $mailer->to($recip);
|
---|
[422] | 1620 | $mailer->data("From: \"$org_name IP Database\" <ipdb\@$domain>\n",
|
---|
[135] | 1621 | "To: $recip\n",
|
---|
[69] | 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",
|
---|
[417] | 1625 | "Organization: $org_name\n",
|
---|
[69] | 1626 | "\n$message\n");
|
---|
[416] | 1627 | }
|
---|
[66] | 1628 | $mailer->quit;
|
---|
| 1629 | }
|
---|
| 1630 |
|
---|
[4] | 1631 | # Indicates module loaded OK. Required by Perl.
|
---|
| 1632 | 1;
|
---|