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