[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: 2015-02-20 23:15:14 +0000 (Fri, 20 Feb 2015) $
|
---|
| 6 | # SVN revision $Rev: 699 $
|
---|
| 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
|
---|
[661] | 27 | %IPDBacl %aclmsg %rpcacl $maxfcgi
|
---|
[660] | 28 | $errstr
|
---|
[523] | 29 | &initIPDBGlobals &connectDB &finish &checkDBSanity
|
---|
[547] | 30 | &addMaster &touchMaster
|
---|
[663] | 31 | &listSummary &listSubs &listContainers &listAllocations &listFree &listPool
|
---|
[541] | 32 | &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
|
---|
[697] | 33 | &ipParent &subParent &blockParent &getBreadCrumbs &getRoutedCity
|
---|
[675] | 34 | &allocateBlock &updateBlock &deleteBlock &getBlockData &getBlockRDNS &getRDNSbyIP
|
---|
[530] | 35 | &getNodeList &getNodeName &getNodeInfo
|
---|
[519] | 36 | &mailNotify
|
---|
[106] | 37 | );
|
---|
[4] | 38 |
|
---|
| 39 | @EXPORT = (); # Export nothing by default.
|
---|
[106] | 40 | %EXPORT_TAGS = ( ALL => [qw(
|
---|
[167] | 41 | %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist
|
---|
[661] | 42 | %IPDBacl %aclmsg %rpcacl $maxfcgi
|
---|
[660] | 43 | $errstr
|
---|
[523] | 44 | &initIPDBGlobals &connectDB &finish &checkDBSanity
|
---|
[547] | 45 | &addMaster &touchMaster
|
---|
[663] | 46 | &listSummary &listSubs &listContainers &listAllocations &listFree &listPool
|
---|
[541] | 47 | &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
|
---|
[697] | 48 | &ipParent &subParent &blockParent &getBreadCrumbs &getRoutedCity
|
---|
[675] | 49 | &allocateBlock &updateBlock &deleteBlock &getBlockData &getBlockRDNS &getRDNSbyIP
|
---|
[530] | 50 | &getNodeList &getNodeName &getNodeInfo
|
---|
[519] | 51 | &mailNotify
|
---|
[106] | 52 | )]
|
---|
| 53 | );
|
---|
[4] | 54 |
|
---|
[77] | 55 | ##
|
---|
| 56 | ## Global variables
|
---|
| 57 | ##
|
---|
| 58 | our %disp_alloctypes;
|
---|
| 59 | our %list_alloctypes;
|
---|
[167] | 60 | our %def_custids;
|
---|
[96] | 61 | our @citylist;
|
---|
| 62 | our @poplist;
|
---|
[233] | 63 | our %IPDBacl;
|
---|
[66] | 64 |
|
---|
[517] | 65 | # mapping table for functional-area => error message
|
---|
| 66 | our %aclmsg = (
|
---|
| 67 | addmaster => 'add a master block',
|
---|
| 68 | addblock => 'add an allocation',
|
---|
| 69 | updateblock => 'update a block',
|
---|
| 70 | delblock => 'delete an allocation',
|
---|
| 71 | );
|
---|
| 72 |
|
---|
[660] | 73 | our %rpcacl;
|
---|
[661] | 74 | our $maxfcgi = 3;
|
---|
[660] | 75 |
|
---|
[585] | 76 | # error reporting
|
---|
| 77 | our $errstr = '';
|
---|
| 78 |
|
---|
[417] | 79 | our $org_name = 'Example Corp';
|
---|
[416] | 80 | our $smtphost = 'smtp.example.com';
|
---|
| 81 | our $domain = 'example.com';
|
---|
[417] | 82 | our $defcustid = '5554242';
|
---|
[681] | 83 | our $smtpsender = 'ipdb@example.com';
|
---|
[417] | 84 | # mostly for rwhois
|
---|
| 85 | ##fixme: leave these blank by default?
|
---|
[420] | 86 | our $rwhoisDataPath = '/usr/local/rwhoisd/etc/rwhoisd'; # to match ./configure defaults from rwhoisd-1.5.9.6
|
---|
[417] | 87 | our $org_street = '123 4th Street';
|
---|
| 88 | our $org_city = 'Anytown';
|
---|
| 89 | our $org_prov_state = 'ON';
|
---|
| 90 | our $org_pocode = 'H0H 0H0';
|
---|
| 91 | our $org_country = 'CA';
|
---|
| 92 | our $org_phone = '000-555-1234';
|
---|
| 93 | our $org_techhandle = 'ISP-ARIN-HANDLE';
|
---|
[434] | 94 | our $org_email = 'noc@example.com';
|
---|
[437] | 95 | our $hostmaster = 'dns@example.com';
|
---|
[416] | 96 |
|
---|
[417] | 97 | our $syslog_facility = 'local2';
|
---|
| 98 |
|
---|
[582] | 99 | our $rpc_url = '';
|
---|
[585] | 100 | our $revgroup = 1; # should probably be configurable somewhere
|
---|
| 101 | our $rpccount = 0;
|
---|
[582] | 102 |
|
---|
[674] | 103 | # Largest inverse CIDR mask length to show per-IP rDNS list
|
---|
| 104 | # (eg, NetAddr::IP->bits - NetAddr::IP->masklen)
|
---|
| 105 | our $maxrevlist = 5; # /27
|
---|
| 106 |
|
---|
[682] | 107 | # UI layout for subblocks/containers
|
---|
| 108 | our $sublistlayout = 1;
|
---|
| 109 |
|
---|
[691] | 110 | # VLAN validation mode. Set to 0 to allow alphanumeric vlan names instead of using the vlan number.
|
---|
| 111 | our $numeric_vlan = 1;
|
---|
| 112 |
|
---|
| 113 |
|
---|
[585] | 114 | ##
|
---|
| 115 | ## Internal utility functions
|
---|
| 116 | ##
|
---|
| 117 |
|
---|
| 118 | ## IPDB::_rpc
|
---|
| 119 | # Make an RPC call for DNS changes
|
---|
| 120 | sub _rpc {
|
---|
| 121 | return if !$rpc_url; # Just In Case
|
---|
| 122 | my $rpcsub = shift;
|
---|
| 123 | my %args = @_;
|
---|
| 124 |
|
---|
| 125 | # Make an object to represent the XML-RPC server.
|
---|
| 126 | my $server = Frontier::Client->new(url => $rpc_url, debug => 0);
|
---|
| 127 | my $result;
|
---|
| 128 |
|
---|
| 129 | my %rpcargs = (
|
---|
| 130 | rpcsystem => 'ipdb',
|
---|
[640] | 131 | # must be provided by caller's caller
|
---|
| 132 | # rpcuser => $args{user},
|
---|
| 133 | %args,
|
---|
[585] | 134 | );
|
---|
| 135 |
|
---|
| 136 | eval {
|
---|
[640] | 137 | $result = $server->call("dnsdb.$rpcsub", %rpcargs);
|
---|
[585] | 138 | };
|
---|
| 139 | if ($@) {
|
---|
| 140 | $errstr = $@;
|
---|
[678] | 141 | $errstr =~ s/\s*$//;
|
---|
[585] | 142 | $errstr =~ s/Fault returned from XML RPC Server, fault code 4: error executing RPC `dnsdb.$rpcsub'\.\s//;
|
---|
| 143 | }
|
---|
| 144 | $rpccount++;
|
---|
| 145 |
|
---|
| 146 | return $result if $result;
|
---|
| 147 | } # end _rpc()
|
---|
| 148 |
|
---|
| 149 |
|
---|
[77] | 150 | # Let's initialize the globals.
|
---|
| 151 | ## IPDB::initIPDBGlobals()
|
---|
| 152 | # Initialize all globals. Takes a database handle, returns a success or error code
|
---|
| 153 | sub initIPDBGlobals {
|
---|
| 154 | my $dbh = $_[0];
|
---|
| 155 | my $sth;
|
---|
| 156 |
|
---|
[106] | 157 | # Initialize alloctypes hashes
|
---|
[167] | 158 | $sth = $dbh->prepare("select type,listname,dispname,listorder,def_custid from alloctypes order by listorder");
|
---|
[77] | 159 | $sth->execute;
|
---|
| 160 | while (my @data = $sth->fetchrow_array) {
|
---|
[106] | 161 | $disp_alloctypes{$data[0]} = $data[2];
|
---|
[167] | 162 | $def_custids{$data[0]} = $data[4];
|
---|
[106] | 163 | if ($data[3] < 900) {
|
---|
| 164 | $list_alloctypes{$data[0]} = $data[1];
|
---|
| 165 | }
|
---|
[77] | 166 | }
|
---|
[96] | 167 |
|
---|
| 168 | # City and POP listings
|
---|
[157] | 169 | $sth = $dbh->prepare("select city,routing from cities order by city");
|
---|
[96] | 170 | $sth->execute;
|
---|
| 171 | return (undef,$sth->errstr) if $sth->err;
|
---|
| 172 | while (my @data = $sth->fetchrow_array) {
|
---|
[106] | 173 | push @citylist, $data[0];
|
---|
[96] | 174 | if ($data[1] eq 'y') {
|
---|
[106] | 175 | push @poplist, $data[0];
|
---|
[96] | 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[233] | 179 | # Load ACL data. Specific username checks are done at a different level.
|
---|
| 180 | $sth = $dbh->prepare("select username,acl from users");
|
---|
| 181 | $sth->execute;
|
---|
[106] | 182 | return (undef,$sth->errstr) if $sth->err;
|
---|
[233] | 183 | while (my @data = $sth->fetchrow_array) {
|
---|
| 184 | $IPDBacl{$data[0]} = $data[1];
|
---|
| 185 | }
|
---|
[106] | 186 |
|
---|
[517] | 187 | ##fixme: initialize HTML::Template env var for template path
|
---|
| 188 | # something like $self->path().'/templates' ?
|
---|
| 189 | # $ENV{HTML_TEMPLATE_ROOT} = 'foo/bar';
|
---|
| 190 |
|
---|
[77] | 191 | return (1,"OK");
|
---|
| 192 | } # end initIPDBGlobals
|
---|
| 193 |
|
---|
| 194 |
|
---|
| 195 | ## IPDB::connectDB()
|
---|
[4] | 196 | # Creates connection to IPDB.
|
---|
[77] | 197 | # Requires the database name, username, and password.
|
---|
[4] | 198 | # Returns a handle to the db.
|
---|
[77] | 199 | # Set up for a PostgreSQL db; could be any transactional DBMS with the
|
---|
| 200 | # right changes.
|
---|
[4] | 201 | sub connectDB {
|
---|
[432] | 202 | my $dbname = shift;
|
---|
| 203 | my $user = shift;
|
---|
| 204 | my $pass = shift;
|
---|
| 205 | my $dbhost = shift;
|
---|
| 206 |
|
---|
[4] | 207 | my $dbh;
|
---|
[432] | 208 | my $DSN = "DBI:Pg:".($dbhost ? "host=$dbhost;" : '')."dbname=$dbname";
|
---|
[4] | 209 |
|
---|
| 210 | # Note that we want to autocommit by default, and we will turn it off locally as necessary.
|
---|
[77] | 211 | # We may not want to print gobbledygook errors; YMMV. Have to ponder that further.
|
---|
| 212 | $dbh = DBI->connect($DSN, $user, $pass, {
|
---|
| 213 | AutoCommit => 1,
|
---|
| 214 | PrintError => 0
|
---|
| 215 | })
|
---|
| 216 | or return (undef, $DBI::errstr) if(!$dbh);
|
---|
[4] | 217 |
|
---|
[77] | 218 | # Return here if we can't select. Note that this indicates a
|
---|
| 219 | # problem executing the select.
|
---|
[183] | 220 | my $sth = $dbh->prepare("select type from alloctypes");
|
---|
[77] | 221 | $sth->execute();
|
---|
| 222 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 223 |
|
---|
| 224 | # See if the select returned anything (or null data). This should
|
---|
| 225 | # succeed if the select executed, but...
|
---|
| 226 | $sth->fetchrow();
|
---|
| 227 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 228 |
|
---|
| 229 | # If we get here, we should be OK.
|
---|
| 230 | return ($dbh,"DB connection OK");
|
---|
[4] | 231 | } # end connectDB
|
---|
| 232 |
|
---|
[77] | 233 |
|
---|
| 234 | ## IPDB::finish()
|
---|
| 235 | # Cleans up after database handles and so on.
|
---|
| 236 | # Requires a database handle
|
---|
| 237 | sub finish {
|
---|
| 238 | my $dbh = $_[0];
|
---|
[517] | 239 | $dbh->disconnect if $dbh;
|
---|
[77] | 240 | } # end finish
|
---|
| 241 |
|
---|
| 242 |
|
---|
[106] | 243 | ## IPDB::checkDBSanity()
|
---|
[4] | 244 | # Quick check to see if the db is responding. A full integrity
|
---|
| 245 | # check will have to be a separate tool to walk the IP allocation trees.
|
---|
| 246 | sub checkDBSanity {
|
---|
[106] | 247 | my ($dbh) = $_[0];
|
---|
[4] | 248 |
|
---|
| 249 | if (!$dbh) {
|
---|
[106] | 250 | print "No database handle, or connection has been closed.";
|
---|
| 251 | return -1;
|
---|
[4] | 252 | } else {
|
---|
| 253 | # it connects, try a stmt.
|
---|
[184] | 254 | my $sth = $dbh->prepare("select type from alloctypes");
|
---|
[4] | 255 | my $err = $sth->execute();
|
---|
| 256 |
|
---|
| 257 | if ($sth->fetchrow()) {
|
---|
| 258 | # all is well.
|
---|
| 259 | return 1;
|
---|
| 260 | } else {
|
---|
[16] | 261 | print "Connected to the database, but could not execute test statement. ".$sth->errstr();
|
---|
[106] | 262 | return -1;
|
---|
[4] | 263 | }
|
---|
| 264 | }
|
---|
| 265 | # Clean up after ourselves.
|
---|
[106] | 266 | # $dbh->disconnect;
|
---|
[4] | 267 | } # end checkDBSanity
|
---|
| 268 |
|
---|
[66] | 269 |
|
---|
[371] | 270 | ## IPDB::addMaster()
|
---|
| 271 | # Does all the magic necessary to sucessfully add a master block
|
---|
| 272 | # Requires database handle, block to add
|
---|
| 273 | # Returns failure code and error message or success code and "message"
|
---|
| 274 | sub addMaster {
|
---|
| 275 | my $dbh = shift;
|
---|
[591] | 276 | # warning! during testing, this somehow generated a "Bad file descriptor" error. O_o
|
---|
[371] | 277 | my $cidr = new NetAddr::IP shift;
|
---|
[582] | 278 | my %args = @_;
|
---|
[371] | 279 |
|
---|
[582] | 280 | $args{vrf} = '' if !$args{vrf};
|
---|
| 281 | $args{rdns} = '' if !$args{rdns};
|
---|
| 282 | $args{defloc} = '' if !$args{defloc};
|
---|
| 283 | $args{rwhois} = 'n' if !$args{rwhois}; # fail "safe", sort of.
|
---|
| 284 | $args{rwhois} = 'n' if $args{rwhois} ne 'n' and $args{rwhois} ne 'y';
|
---|
| 285 |
|
---|
[629] | 286 | my $mid;
|
---|
| 287 |
|
---|
[371] | 288 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 289 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 290 | local $dbh->{AutoCommit} = 0;
|
---|
| 291 | local $dbh->{RaiseError} = 1;
|
---|
| 292 |
|
---|
| 293 | # Wrap all the SQL in a transaction
|
---|
| 294 | eval {
|
---|
[628] | 295 | # First check - does the master exist? Ignore VRFs until we can see a sane UI
|
---|
| 296 | my ($mcontained) = $dbh->selectrow_array("SELECT cidr FROM allocations WHERE cidr >>= ? AND type = 'mm'",
|
---|
| 297 | undef, ($cidr) );
|
---|
| 298 | die "Master block $mcontained already exists and entirely contains $cidr\n"
|
---|
| 299 | if $mcontained;
|
---|
[371] | 300 |
|
---|
[628] | 301 | # Second check - does the new master contain an existing one or ones?
|
---|
| 302 | my ($mexist) = $dbh->selectrow_array("SELECT cidr FROM allocations WHERE cidr <<= ? AND type = 'mm'",
|
---|
| 303 | undef, ($cidr) );
|
---|
| 304 |
|
---|
[518] | 305 | if (!$mexist) {
|
---|
[371] | 306 | # First case - master is brand-spanking-new.
|
---|
| 307 | ##fixme: rwhois should be globally-flagable somewhere, much like a number of other things
|
---|
| 308 | ## maybe a db table called "config"?
|
---|
[628] | 309 | $dbh->do("INSERT INTO allocations (cidr,type,swip,vrf,rdns) VALUES (?,?,?,?,?)", undef,
|
---|
| 310 | ($cidr, 'mm', 'y', $args{vrf}, $args{rdns}) );
|
---|
| 311 | ($mid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
|
---|
[371] | 312 |
|
---|
| 313 | # Unrouted blocks aren't associated with a city (yet). We don't rely on this
|
---|
| 314 | # elsewhere though; legacy data may have traps and pitfalls in it to break this.
|
---|
| 315 | # Thus the "routed" flag.
|
---|
[628] | 316 | $dbh->do("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id) VALUES (?,?,?,?,?,?)", undef,
|
---|
| 317 | ($cidr, '<NULL>', 'm', $mid, $args{vrf}, $mid) );
|
---|
[371] | 318 |
|
---|
[628] | 319 | # master should be its own master, so deletes directly at the master level work
|
---|
| 320 | $dbh->do("UPDATE allocations SET master_id = ? WHERE id = ?", undef, ($mid, $mid) );
|
---|
| 321 |
|
---|
[371] | 322 | # If we get here, everything is happy. Commit changes.
|
---|
| 323 | $dbh->commit;
|
---|
| 324 |
|
---|
[518] | 325 | } # done new master does not contain existing master(s)
|
---|
[371] | 326 | else {
|
---|
| 327 |
|
---|
| 328 | # collect the master(s) we're going to absorb, and snag the longest netmask while we're at it.
|
---|
| 329 | my $smallmask = $cidr->masklen;
|
---|
[628] | 330 | my $sth = $dbh->prepare("SELECT cidr,id FROM allocations WHERE cidr <<= ? AND type='mm' AND parent_id=0");
|
---|
[518] | 331 | $sth->execute($cidr);
|
---|
[371] | 332 | my @cmasters;
|
---|
[628] | 333 | my @oldmids;
|
---|
[371] | 334 | while (my @data = $sth->fetchrow_array) {
|
---|
| 335 | my $master = new NetAddr::IP $data[0];
|
---|
| 336 | push @cmasters, $master;
|
---|
[628] | 337 | push @oldmids, $data[1];
|
---|
[371] | 338 | $smallmask = $master->masklen if $master->masklen > $smallmask;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | # split the new master, and keep only those blocks not part of an existing master
|
---|
| 342 | my @blocklist;
|
---|
| 343 | foreach my $seg ($cidr->split($smallmask)) {
|
---|
| 344 | my $contained = 0;
|
---|
| 345 | foreach my $master (@cmasters) {
|
---|
| 346 | $contained = 1 if $master->contains($seg);
|
---|
| 347 | }
|
---|
| 348 | push @blocklist, $seg if !$contained;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
[628] | 351 | ##fixme: master_id
|
---|
[371] | 352 | # collect the unrouted free blocks within the new master
|
---|
[556] | 353 | $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE masklen(cidr) <= ? AND cidr <<= ? AND routed = 'm'");
|
---|
[518] | 354 | $sth->execute($smallmask, $cidr);
|
---|
[371] | 355 | while (my @data = $sth->fetchrow_array) {
|
---|
| 356 | my $freeblock = new NetAddr::IP $data[0];
|
---|
| 357 | push @blocklist, $freeblock;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | # combine the set of free blocks we should have now.
|
---|
| 361 | @blocklist = Compact(@blocklist);
|
---|
| 362 |
|
---|
[628] | 363 | # master
|
---|
| 364 | $dbh->do("INSERT INTO allocations (cidr,type,swip,vrf,rdns) VALUES (?,?,?,?,?)", undef,
|
---|
| 365 | ($cidr, 'mm', 'y', $args{vrf}, $args{rdns}) );
|
---|
| 366 | ($mid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
|
---|
| 367 |
|
---|
| 368 | # master should be its own master, so deletes directly at the master level work
|
---|
| 369 | $dbh->do("UPDATE allocations SET master_id = ? WHERE id = ?", undef, ($mid, $mid) );
|
---|
| 370 |
|
---|
[371] | 371 | # and now insert the new data. Make sure to delete old masters too.
|
---|
| 372 |
|
---|
| 373 | # freeblocks
|
---|
[628] | 374 | $sth = $dbh->prepare("DELETE FROM freeblocks WHERE cidr <<= ? AND parent_id IN (".join(',', @oldmids).")");
|
---|
| 375 | my $sth2 = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id)".
|
---|
| 376 | " VALUES (?,'<NULL>','m',?,?,?)");
|
---|
[371] | 377 | foreach my $newblock (@blocklist) {
|
---|
[518] | 378 | $sth->execute($newblock);
|
---|
[628] | 379 | $sth2->execute($newblock, $mid, $args{vrf}, $mid);
|
---|
[371] | 380 | }
|
---|
| 381 |
|
---|
[628] | 382 | # Update immediate allocations, and remove the old parents
|
---|
| 383 | $sth = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE parent_id = ?");
|
---|
| 384 | $sth2 = $dbh->prepare("DELETE FROM allocations WHERE id = ?");
|
---|
| 385 | foreach my $old (@oldmids) {
|
---|
| 386 | $sth->execute($mid, $old);
|
---|
| 387 | $sth2->execute($old);
|
---|
| 388 | }
|
---|
[556] | 389 |
|
---|
[371] | 390 | # *whew* If we got here, we likely suceeded.
|
---|
| 391 | $dbh->commit;
|
---|
[628] | 392 |
|
---|
[371] | 393 | } # new master contained existing master(s)
|
---|
| 394 | }; # end eval
|
---|
| 395 |
|
---|
| 396 | if ($@) {
|
---|
| 397 | my $msg = $@;
|
---|
| 398 | eval { $dbh->rollback; };
|
---|
| 399 | return ('FAIL',$msg);
|
---|
| 400 | } else {
|
---|
[582] | 401 |
|
---|
| 402 | # Only attempt rDNS if the IPDB side succeeded
|
---|
| 403 | if ($rpc_url) {
|
---|
| 404 |
|
---|
| 405 | # Note *not* splitting reverse zones negates any benefit from caching the exported data.
|
---|
| 406 | # IPv6 address space is far too large to split usefully, and in any case (also due to
|
---|
| 407 | # the large address space) doesn't support the iterated template records v4 zones do
|
---|
| 408 | # that causes the bulk of the slowdown that needs the cache anyway.
|
---|
| 409 |
|
---|
| 410 | my @zonelist;
|
---|
| 411 | # allow splitting reverse zones to be disabled, maybe, someday
|
---|
| 412 | #if ($splitrevzones && !$cidr->{isv6}) {
|
---|
| 413 | if (1 && !$cidr->{isv6}) {
|
---|
| 414 | my $splitpoint = ($cidr->masklen <= 16 ? 16 : 24); # hack pthui
|
---|
| 415 | @zonelist = $cidr->split($splitpoint);
|
---|
| 416 | } else {
|
---|
| 417 | @zonelist = ($cidr);
|
---|
| 418 | }
|
---|
| 419 | my @fails;
|
---|
| 420 | ##fixme: remove hardcoding where possible
|
---|
| 421 | foreach my $subzone (@zonelist) {
|
---|
| 422 | my %rpcargs = (
|
---|
| 423 | rpcuser => $args{user},
|
---|
| 424 | revzone => "$subzone",
|
---|
| 425 | revpatt => $args{rdns},
|
---|
| 426 | defloc => $args{defloc},
|
---|
[585] | 427 | group => $revgroup, # not sure how these two could sanely be exposed, tbh...
|
---|
[582] | 428 | state => 1, # could make them globally configurable maybe
|
---|
| 429 | );
|
---|
[587] | 430 | if ($rpc_url && !_rpc('addRDNS', %rpcargs)) {
|
---|
[585] | 431 | push @fails, ("$subzone" => $errstr);
|
---|
[582] | 432 | }
|
---|
| 433 | }
|
---|
| 434 | if (@fails) {
|
---|
[628] | 435 | $errstr = "Warning(s) adding $cidr to reverse DNS:\n".join("\n", @fails);
|
---|
| 436 | return ('WARN',$mid);
|
---|
[582] | 437 | }
|
---|
| 438 | }
|
---|
[628] | 439 | return ('OK',$mid);
|
---|
[371] | 440 | }
|
---|
| 441 | } # end addMaster
|
---|
| 442 |
|
---|
| 443 |
|
---|
[547] | 444 | ## IPDB::touchMaster()
|
---|
| 445 | # Update last-changed timestamp on a master block.
|
---|
| 446 | sub touchMaster {
|
---|
| 447 | my $dbh = shift;
|
---|
| 448 | my $master = shift;
|
---|
| 449 |
|
---|
| 450 | local $dbh->{AutoCommit} = 0;
|
---|
| 451 | local $dbh->{RaiseError} = 1;
|
---|
| 452 |
|
---|
| 453 | eval {
|
---|
[652] | 454 | $dbh->do("UPDATE allocations SET modifystamp=now() WHERE id = ?", undef, ($master));
|
---|
[547] | 455 | $dbh->commit;
|
---|
| 456 | };
|
---|
| 457 |
|
---|
| 458 | if ($@) {
|
---|
| 459 | my $msg = $@;
|
---|
| 460 | eval { $dbh->rollback; };
|
---|
| 461 | return ('FAIL',$msg);
|
---|
| 462 | }
|
---|
| 463 | return ('OK','OK');
|
---|
| 464 | } # end touchMaster()
|
---|
| 465 |
|
---|
| 466 |
|
---|
[523] | 467 | ## IPDB::listSummary()
|
---|
| 468 | # Get summary list of all master blocks
|
---|
| 469 | # Returns an arrayref to a list of hashrefs containing the master block, routed count,
|
---|
| 470 | # allocated count, free count, and largest free block masklength
|
---|
| 471 | sub listSummary {
|
---|
| 472 | my $dbh = shift;
|
---|
| 473 |
|
---|
[625] | 474 | my $mlist = $dbh->selectall_arrayref("SELECT cidr AS master,id,vrf FROM allocations ".
|
---|
| 475 | "WHERE type='mm' ORDER BY cidr",
|
---|
| 476 | { Slice => {} });
|
---|
[523] | 477 |
|
---|
| 478 | foreach (@{$mlist}) {
|
---|
[625] | 479 | my ($rcnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? AND type='rm' AND master_id = ?",
|
---|
| 480 | undef, ($$_{master}, $$_{id}));
|
---|
[523] | 481 | $$_{routed} = $rcnt;
|
---|
[625] | 482 | my ($acnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
|
---|
| 483 | "AND NOT type='rm' AND NOT type='mm' AND master_id = ?",
|
---|
| 484 | undef, ($$_{master}, $$_{id}));
|
---|
[523] | 485 | $$_{allocated} = $acnt;
|
---|
[625] | 486 | my ($fcnt) = $dbh->selectrow_array("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?",
|
---|
| 487 | undef, ($$_{master}, $$_{id}));
|
---|
[523] | 488 | $$_{free} = $fcnt;
|
---|
[560] | 489 | my ($bigfree) = $dbh->selectrow_array("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
|
---|
[625] | 490 | " AND master_id = ? ORDER BY masklen(cidr) LIMIT 1", undef, ($$_{master}, $$_{id}));
|
---|
[523] | 491 | ##fixme: should find a way to do this without having to HTMLize the <>
|
---|
| 492 | $bigfree = "/$bigfree" if $bigfree;
|
---|
[525] | 493 | $bigfree = '<NONE>' if !$bigfree;
|
---|
[523] | 494 | $$_{bigfree} = $bigfree;
|
---|
| 495 | }
|
---|
| 496 | return $mlist;
|
---|
| 497 | } # end listSummary()
|
---|
| 498 |
|
---|
| 499 |
|
---|
[561] | 500 | ## IPDB::listSubs()
|
---|
| 501 | # Get list of subnets within a specified CIDR block, on a specified VRF.
|
---|
| 502 | # Returns an arrayref to a list of hashrefs containing the CIDR block, customer location or
|
---|
| 503 | # city it's routed to, block type, SWIP status, and description
|
---|
| 504 | sub listSubs {
|
---|
| 505 | my $dbh = shift;
|
---|
| 506 | my %args = @_;
|
---|
| 507 |
|
---|
| 508 | # Just In Case
|
---|
| 509 | $args{vrf} = '' if !$args{vrf};
|
---|
| 510 |
|
---|
| 511 | # Snag the allocations for this block
|
---|
[691] | 512 | my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,vrf,id,master_id".
|
---|
[627] | 513 | " FROM allocations WHERE parent_id = ? ORDER BY cidr");
|
---|
| 514 | $sth->execute($args{parent});
|
---|
[561] | 515 |
|
---|
| 516 | # hack hack hack
|
---|
| 517 | # set up to flag swip=y records if they don't actually have supporting data in the customers table
|
---|
| 518 | my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?");
|
---|
| 519 |
|
---|
[653] | 520 | # snag some more details
|
---|
[664] | 521 | my $substh = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
|
---|
[663] | 522 | "AND type ~ '[mc]\$' AND master_id = ? AND NOT cidr = ? ");
|
---|
[653] | 523 | my $alsth = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
|
---|
| 524 | "AND NOT type='rm' AND NOT type='mm' AND master_id = ?");
|
---|
| 525 | my $freesth = $dbh->prepare("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?");
|
---|
| 526 | my $lfreesth = $dbh->prepare("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
|
---|
| 527 | " AND master_id = ? ORDER BY masklen(cidr) LIMIT 1");
|
---|
| 528 |
|
---|
[561] | 529 | my @blocklist;
|
---|
[691] | 530 | while (my ($cidr,$city,$type,$custid,$swip,$desc,$vrf,$id,$mid) = $sth->fetchrow_array()) {
|
---|
| 531 | $desc .= " - vrf:$vrf" if $desc && $vrf;
|
---|
| 532 | $desc = "vrf:$vrf" if !$desc && $vrf;
|
---|
[561] | 533 | $custsth->execute($custid);
|
---|
| 534 | my ($ncust) = $custsth->fetchrow_array();
|
---|
[653] | 535 | $substh->execute($cidr, $mid, $cidr);
|
---|
| 536 | my ($cont) = $substh->fetchrow_array();
|
---|
| 537 | $alsth->execute($cidr, $mid);
|
---|
| 538 | my ($alloc) = $alsth->fetchrow_array();
|
---|
| 539 | $freesth->execute($cidr, $mid);
|
---|
| 540 | my ($free) = $freesth->fetchrow_array();
|
---|
| 541 | $lfreesth->execute($cidr, $mid);
|
---|
| 542 | my ($lfree) = $lfreesth->fetchrow_array();
|
---|
| 543 | $lfree = "/$lfree" if $lfree;
|
---|
| 544 | $lfree = '<NONE>' if !$lfree;
|
---|
[561] | 545 | my %row = (
|
---|
| 546 | block => $cidr,
|
---|
[653] | 547 | subcontainers => $cont,
|
---|
| 548 | suballocs => $alloc,
|
---|
| 549 | subfree => $free,
|
---|
| 550 | lfree => $lfree,
|
---|
[561] | 551 | city => $city,
|
---|
| 552 | type => $disp_alloctypes{$type},
|
---|
| 553 | custid => $custid,
|
---|
| 554 | swip => ($swip eq 'y' ? 'Yes' : 'No'),
|
---|
| 555 | partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0),
|
---|
| 556 | desc => $desc,
|
---|
| 557 | hassubs => ($type eq 'rm' || $type =~ /.c/ ? 1 : 0),
|
---|
[627] | 558 | id => $id,
|
---|
[561] | 559 | );
|
---|
| 560 | # $row{subblock} = ($type =~ /^.r$/); # hmf. wonder why these won't work in the hash declaration...
|
---|
| 561 | $row{listpool} = ($type =~ /^.[pd]$/);
|
---|
| 562 | push (@blocklist, \%row);
|
---|
| 563 | }
|
---|
| 564 | return \@blocklist;
|
---|
| 565 | } # end listSubs()
|
---|
| 566 |
|
---|
| 567 |
|
---|
[663] | 568 | ## IPDB::listContainers()
|
---|
| 569 | # List all container-type allocations in a given parent
|
---|
| 570 | # Takes a database handle and a hash:
|
---|
| 571 | # - parent is the ID of the parent block
|
---|
| 572 | # Returns an arrayref to a list of hashrefs with the CIDR block, location, type,
|
---|
| 573 | # description, block ID, and counts for the nmber uf suballocations (all types),
|
---|
| 574 | # free blocks, and the CIDR size of the largest free block
|
---|
| 575 | sub listContainers {
|
---|
| 576 | my $dbh = shift;
|
---|
| 577 | my %args = @_;
|
---|
| 578 |
|
---|
| 579 | # Just In Case
|
---|
| 580 | $args{vrf} = '' if !$args{vrf};
|
---|
| 581 |
|
---|
| 582 | # Snag the allocations for this block
|
---|
[691] | 583 | my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,vrf,id,master_id".
|
---|
[663] | 584 | " FROM allocations WHERE parent_id = ? AND type ~ '[mc]\$' ORDER BY cidr");
|
---|
| 585 | $sth->execute($args{parent});
|
---|
| 586 |
|
---|
| 587 | my $alsth = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
|
---|
| 588 | "AND NOT type='rm' AND NOT type='mm' AND master_id = ?");
|
---|
| 589 | my $freesth = $dbh->prepare("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?");
|
---|
| 590 | my $lfreesth = $dbh->prepare("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
|
---|
| 591 | " AND master_id = ? ORDER BY masklen(cidr) LIMIT 1");
|
---|
| 592 |
|
---|
| 593 | my @blocklist;
|
---|
[691] | 594 | while (my ($cidr,$city,$type,$custid,$swip,$desc,$vrf,$id,$mid) = $sth->fetchrow_array()) {
|
---|
| 595 | $desc .= " - vrf:$vrf" if $desc && $vrf;
|
---|
| 596 | $desc = "vrf:$vrf" if !$desc && $vrf;
|
---|
[663] | 597 | $alsth->execute($cidr, $mid);
|
---|
| 598 | my ($alloc) = $alsth->fetchrow_array();
|
---|
| 599 | $freesth->execute($cidr, $mid);
|
---|
| 600 | my ($free) = $freesth->fetchrow_array();
|
---|
| 601 | $lfreesth->execute($cidr, $mid);
|
---|
| 602 | my ($lfree) = $lfreesth->fetchrow_array();
|
---|
| 603 | $lfree = "/$lfree" if $lfree;
|
---|
| 604 | $lfree = '<NONE>' if !$lfree;
|
---|
| 605 | my %row = (
|
---|
| 606 | block => $cidr,
|
---|
| 607 | suballocs => $alloc,
|
---|
| 608 | subfree => $free,
|
---|
| 609 | lfree => $lfree,
|
---|
| 610 | city => $city,
|
---|
| 611 | type => $disp_alloctypes{$type},
|
---|
| 612 | desc => $desc,
|
---|
| 613 | id => $id,
|
---|
| 614 | );
|
---|
| 615 | push (@blocklist, \%row);
|
---|
| 616 | }
|
---|
| 617 | return \@blocklist;
|
---|
| 618 | } # end listContainers()
|
---|
| 619 |
|
---|
| 620 |
|
---|
| 621 | ## IPDB::listAllocations()
|
---|
| 622 | # List all end-use allocations in a given parent
|
---|
| 623 | # Takes a database handle and a hash:
|
---|
| 624 | # - parent is the ID of the parent block
|
---|
| 625 | # Returns an arrayref to a list of hashrefs with the CIDR block, location, type,
|
---|
| 626 | # custID, SWIP flag, description, block ID, and master ID
|
---|
| 627 | sub listAllocations {
|
---|
| 628 | my $dbh = shift;
|
---|
| 629 | my %args = @_;
|
---|
| 630 |
|
---|
| 631 | # Snag the allocations for this block
|
---|
[691] | 632 | my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,vrf,id,master_id".
|
---|
[663] | 633 | " FROM allocations WHERE parent_id = ? AND type !~ '[mc]\$' ORDER BY cidr");
|
---|
| 634 | $sth->execute($args{parent});
|
---|
| 635 |
|
---|
| 636 | # hack hack hack
|
---|
| 637 | # set up to flag swip=y records if they don't actually have supporting data in the customers table
|
---|
| 638 | my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?");
|
---|
| 639 |
|
---|
| 640 | my @blocklist;
|
---|
[691] | 641 | while (my ($cidr,$city,$type,$custid,$swip,$desc,$vrf,$id,$mid) = $sth->fetchrow_array()) {
|
---|
| 642 | $desc .= " - vrf:$vrf" if $desc && $vrf;
|
---|
| 643 | $desc = "vrf:$vrf" if !$desc && $vrf;
|
---|
[663] | 644 | $custsth->execute($custid);
|
---|
| 645 | my ($ncust) = $custsth->fetchrow_array();
|
---|
| 646 | my %row = (
|
---|
| 647 | block => $cidr,
|
---|
| 648 | city => $city,
|
---|
| 649 | type => $disp_alloctypes{$type},
|
---|
| 650 | custid => $custid,
|
---|
| 651 | swip => ($swip eq 'y' ? 'Yes' : 'No'),
|
---|
| 652 | partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0),
|
---|
| 653 | desc => $desc,
|
---|
| 654 | id => $id,
|
---|
| 655 | );
|
---|
| 656 | # $row{subblock} = ($type =~ /^.r$/); # hmf. wonder why these won't work in the hash declaration...
|
---|
| 657 | $row{listpool} = ($type =~ /^.[pd]$/);
|
---|
| 658 | push (@blocklist, \%row);
|
---|
| 659 | }
|
---|
| 660 | return \@blocklist;
|
---|
| 661 | } # end listAllocations()
|
---|
| 662 |
|
---|
| 663 |
|
---|
[524] | 664 | ## IPDB::listFree()
|
---|
[562] | 665 | # Gets a list of free blocks in the requested parent/master and VRF instance in both CIDR and range notation
|
---|
[630] | 666 | # Takes a parent/master ID and an optional VRF specifier that defaults to empty.
|
---|
[524] | 667 | # Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks
|
---|
[527] | 668 | # Returns some extra flags in the hashrefs for routed blocks, since those can have several subtypes
|
---|
[524] | 669 | sub listFree {
|
---|
| 670 | my $dbh = shift;
|
---|
| 671 |
|
---|
[562] | 672 | my %args = @_;
|
---|
| 673 | # Just In Case
|
---|
| 674 | $args{vrf} = '' if !$args{vrf};
|
---|
| 675 |
|
---|
[692] | 676 | my $sth = $dbh->prepare(q(
|
---|
| 677 | SELECT f.cidr,f.id,allocations.cidr
|
---|
| 678 | FROM freeblocks f
|
---|
| 679 | LEFT JOIN allocations ON f.reserve_for = allocations.id
|
---|
| 680 | WHERE f.parent_id = ?
|
---|
| 681 | ORDER BY f.cidr
|
---|
| 682 | ) );
|
---|
[630] | 683 | # $sth->execute($args{parent}, $args{vrf});
|
---|
| 684 | $sth->execute($args{parent});
|
---|
[524] | 685 | my @flist;
|
---|
[692] | 686 | while (my ($cidr,$id,$resv) = $sth->fetchrow_array()) {
|
---|
[524] | 687 | $cidr = new NetAddr::IP $cidr;
|
---|
[527] | 688 | my %row = (
|
---|
| 689 | fblock => "$cidr",
|
---|
| 690 | frange => $cidr->range,
|
---|
[630] | 691 | fbid => $id,
|
---|
| 692 | fbparent => $args{parent},
|
---|
[692] | 693 | resv => $resv,
|
---|
[527] | 694 | );
|
---|
[524] | 695 | push @flist, \%row;
|
---|
| 696 | }
|
---|
| 697 | return \@flist;
|
---|
[527] | 698 | } # end listFree()
|
---|
[524] | 699 |
|
---|
| 700 |
|
---|
[528] | 701 | ## IPDB::listPool()
|
---|
| 702 | #
|
---|
| 703 | sub listPool {
|
---|
| 704 | my $dbh = shift;
|
---|
| 705 | my $pool = shift;
|
---|
| 706 |
|
---|
[630] | 707 | my $sth = $dbh->prepare("SELECT ip,custid,available,description,type,id".
|
---|
| 708 | " FROM poolips WHERE parent_id = ? ORDER BY ip");
|
---|
[528] | 709 | $sth->execute($pool);
|
---|
| 710 | my @poolips;
|
---|
[630] | 711 | while (my ($ip,$custid,$available,$desc,$type,$id) = $sth->fetchrow_array) {
|
---|
[528] | 712 | my %row = (
|
---|
| 713 | ip => $ip,
|
---|
| 714 | custid => $custid,
|
---|
| 715 | available => $available,
|
---|
| 716 | desc => $desc,
|
---|
[563] | 717 | delme => $available eq 'n',
|
---|
[630] | 718 | parent => $pool,
|
---|
| 719 | id => $id,
|
---|
[528] | 720 | );
|
---|
| 721 | push @poolips, \%row;
|
---|
| 722 | }
|
---|
| 723 | return \@poolips;
|
---|
| 724 | } # end listPool()
|
---|
| 725 |
|
---|
| 726 |
|
---|
[541] | 727 | ## IPDB::getMasterList()
|
---|
| 728 | # Get a list of master blocks, optionally including last-modified timestamps
|
---|
| 729 | # Takes an optional flag to indicate whether to include timestamps;
|
---|
| 730 | # 'm' includes ctime, all others (suggest 'c') do not.
|
---|
| 731 | # Returns an arrayref to a list of hashrefs
|
---|
| 732 | sub getMasterList {
|
---|
| 733 | my $dbh = shift;
|
---|
| 734 | my $stampme = shift || 'm'; # optional but should be set by caller for clarity
|
---|
| 735 |
|
---|
[632] | 736 | my $mlist = $dbh->selectall_arrayref("SELECT id,vrf,cidr AS master".($stampme eq 'm' ? ',modifystamp AS mtime' : '').
|
---|
| 737 | " FROM allocations WHERE type='mm' ORDER BY cidr", { Slice => {} });
|
---|
[541] | 738 | return $mlist;
|
---|
| 739 | } # end getMasterList()
|
---|
| 740 |
|
---|
| 741 |
|
---|
[529] | 742 | ## IPDB::getTypeList()
|
---|
| 743 | # Get an alloctype/description pair list suitable for dropdowns
|
---|
| 744 | # Takes a flag to determine which general groups of types are returned
|
---|
| 745 | # Returns an reference to an array of hashrefs
|
---|
| 746 | sub getTypeList {
|
---|
| 747 | my $dbh = shift;
|
---|
| 748 | my $tgroup = shift || 'a'; # technically optional, like this, but should
|
---|
| 749 | # really be specified in the call for clarity
|
---|
| 750 | my $tlist;
|
---|
[564] | 751 | if ($tgroup eq 'n') {
|
---|
| 752 | # grouping 'p' - all netblock types. These include routed blocks, containers (_c)
|
---|
| 753 | # and contained (_r) types, dynamic-allocation ranges (_e), static IP pools (_d and _p),
|
---|
| 754 | # and the "miscellaneous" cn, in, and en types.
|
---|
| 755 | $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
|
---|
| 756 | "AND type NOT LIKE '_i' ORDER BY listorder", { Slice => {} });
|
---|
| 757 | } elsif ($tgroup eq 'p') {
|
---|
| 758 | # grouping 'p' - primary allocation types. As with 'n' above but without the _r contained types.
|
---|
| 759 | $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
|
---|
[529] | 760 | "AND type NOT LIKE '_i' AND type NOT LIKE '_r' ORDER BY listorder", { Slice => {} });
|
---|
| 761 | } elsif ($tgroup eq 'c') {
|
---|
| 762 | # grouping 'c' - contained types. These include all static IPs and all _r types.
|
---|
| 763 | $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
|
---|
| 764 | " AND (type LIKE '_i' OR type LIKE '_r') ORDER BY listorder", { Slice => {} });
|
---|
[632] | 765 | } elsif ($tgroup eq 'i') {
|
---|
| 766 | # grouping 'i' - static IP types.
|
---|
| 767 | $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
|
---|
| 768 | " AND type LIKE '_i' ORDER BY listorder", { Slice => {} });
|
---|
[529] | 769 | } else {
|
---|
| 770 | # grouping 'a' - all standard allocation types. This includes everything
|
---|
| 771 | # but mm (present only as a formality). Make this the default.
|
---|
| 772 | $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
|
---|
| 773 | " ORDER BY listorder", { Slice => {} });
|
---|
| 774 | }
|
---|
| 775 | return $tlist;
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 |
|
---|
[532] | 779 | ## IPDB::getPoolSelect()
|
---|
| 780 | # Get a list of pools matching the passed city and type that have 1 or more free IPs
|
---|
[533] | 781 | # Returns an arrayref to a list of hashrefs
|
---|
[532] | 782 | sub getPoolSelect {
|
---|
| 783 | my $dbh = shift;
|
---|
| 784 | my $iptype = shift;
|
---|
| 785 | my $pcity = shift;
|
---|
| 786 |
|
---|
| 787 | my ($ptype) = ($iptype =~ /^(.)i$/);
|
---|
| 788 | return if !$ptype;
|
---|
| 789 | $ptype .= '_';
|
---|
| 790 |
|
---|
[663] | 791 | my $plist = $dbh->selectall_arrayref( q(
|
---|
| 792 | SELECT count(*) AS poolfree,p.pool AS poolblock, a.city AS poolcit
|
---|
| 793 | FROM poolips p
|
---|
| 794 | JOIN allocations a ON p.parent_id=a.id
|
---|
| 795 | WHERE p.available='y' AND a.city = ? AND p.type LIKE ?
|
---|
| 796 | GROUP BY p.pool,a.city
|
---|
| 797 | ),
|
---|
[572] | 798 | { Slice => {} }, ($pcity, $ptype) );
|
---|
[532] | 799 | return $plist;
|
---|
| 800 | } # end getPoolSelect()
|
---|
| 801 |
|
---|
| 802 |
|
---|
[533] | 803 | ## IPDB::findAllocateFrom()
|
---|
| 804 | # Find free block to add a new allocation from. (CIDR block version of pool select above, more or less)
|
---|
| 805 | # Takes
|
---|
| 806 | # - mask length
|
---|
| 807 | # - allocation type
|
---|
| 808 | # - POP city "parent"
|
---|
| 809 | # - optional master-block restriction
|
---|
| 810 | # - optional flag to allow automatic pick-from-private-network-ranges
|
---|
| 811 | # Returns a string with the first CIDR block matching the criteria, if any
|
---|
| 812 | sub findAllocateFrom {
|
---|
| 813 | my $dbh = shift;
|
---|
| 814 | my $maskbits = shift;
|
---|
| 815 | my $type = shift;
|
---|
| 816 | my $city = shift;
|
---|
| 817 | my $pop = shift;
|
---|
| 818 | my %optargs = @_;
|
---|
| 819 |
|
---|
| 820 | my $failmsg = "No suitable free block found\n";
|
---|
| 821 |
|
---|
[633] | 822 | my @vallist;
|
---|
| 823 | my $sql;
|
---|
| 824 |
|
---|
| 825 | # Free pool IPs should be easy.
|
---|
| 826 | if ($type =~ /^.i$/) {
|
---|
| 827 | # User may get an IP from the wrong VRF. User should not be using admin tools to allocate static IPs.
|
---|
| 828 | $sql = "SELECT id, ip, parent_id FROM poolips WHERE ip = ?";
|
---|
| 829 | @vallist = ($optargs{gimme});
|
---|
| 830 | } else {
|
---|
| 831 |
|
---|
[533] | 832 | ## Set up the SQL to find out what freeblock we can (probably) use for an allocation.
|
---|
| 833 | ## Very large systems will require development of a reserve system (possibly an extension
|
---|
| 834 | ## of the reserve-for-expansion concept in https://secure.deepnet.cx/trac/ipdb/ticket/24?)
|
---|
| 835 | ## Also populate a value list for the DBI call.
|
---|
| 836 |
|
---|
[633] | 837 | @vallist = ($maskbits);
|
---|
| 838 | $sql = "SELECT id,cidr,parent_id FROM freeblocks WHERE masklen(cidr) <= ?";
|
---|
[533] | 839 |
|
---|
[572] | 840 | # cases, strict rules
|
---|
| 841 | # .c -> container type
|
---|
| 842 | # requires a routing container, fbtype r
|
---|
| 843 | # .d -> DHCP/"normal-routing" static pool
|
---|
| 844 | # requires a routing container, fbtype r
|
---|
| 845 | # .e -> Dynamic-assignment connectivity
|
---|
| 846 | # requires a routing container, fbtype r
|
---|
| 847 | # .i -> error, can't allocate static IPs this way?
|
---|
| 848 | # mm -> error, master block
|
---|
| 849 | # rm -> routed block
|
---|
| 850 | # requires master block, fbtype m
|
---|
| 851 | # .n -> Miscellaneous usage
|
---|
| 852 | # requires a routing container, fbtype r
|
---|
| 853 | # .p -> PPP(oE) static pool
|
---|
| 854 | # requires a routing container, fbtype r
|
---|
| 855 | # .r -> contained type
|
---|
| 856 | # requires a matching container, fbtype $1
|
---|
| 857 | ##fixme: strict-or-not flag
|
---|
| 858 |
|
---|
[633] | 859 | ##fixme: config or UI flag for "Strict" mode
|
---|
| 860 | # if ($strictmode) {
|
---|
| 861 | if (0) {
|
---|
[572] | 862 | if ($type =~ /^(.)r$/) {
|
---|
| 863 | push @vallist, $1;
|
---|
| 864 | $sql .= " AND routed = ?";
|
---|
| 865 | } elsif ($type eq 'rm') {
|
---|
| 866 | $sql .= " AND routed = 'm'";
|
---|
| 867 | } else {
|
---|
| 868 | $sql .= " AND routed = 'r'";
|
---|
| 869 | }
|
---|
[633] | 870 | }
|
---|
[572] | 871 |
|
---|
[633] | 872 | # for PPP(oE) and container types, the POP city is the one attached to the pool.
|
---|
| 873 | # individual allocations get listed with the customer city site.
|
---|
| 874 | ##fixme: chain cities to align roughly with a full layer-2 node graph
|
---|
| 875 | $city = $pop if $type !~ /^.[pc]$/;
|
---|
| 876 | if ($type ne 'rm' && $city) {
|
---|
| 877 | $sql .= " AND city = ?";
|
---|
| 878 | push @vallist, $city;
|
---|
[533] | 879 | }
|
---|
[633] | 880 | # Allow specifying an arbitrary full block, instead of a master
|
---|
| 881 | if ($optargs{gimme}) {
|
---|
| 882 | $sql .= " AND cidr >>= ?";
|
---|
| 883 | push @vallist, $optargs{gimme};
|
---|
| 884 | }
|
---|
| 885 | # if a specific master was requested, allow the requestor to self->shoot(foot)
|
---|
| 886 | if ($optargs{master} && $optargs{master} ne '-') {
|
---|
| 887 | $sql .= " AND master_id = ?";
|
---|
| 888 | # if $optargs{master} ne '-';
|
---|
| 889 | push @vallist, $optargs{master};
|
---|
| 890 | } else {
|
---|
| 891 | # if a specific master was NOT requested, filter out the RFC 1918 private networks
|
---|
| 892 | if (!$optargs{allowpriv}) {
|
---|
| 893 | $sql .= " AND NOT (cidr <<= '192.168.0.0/16' OR cidr <<= '10.0.0.0/8' OR cidr <<= '172.16.0.0/12')";
|
---|
| 894 | }
|
---|
| 895 | }
|
---|
[693] | 896 | # Keep "reserved" blocks out of automatic assignment.
|
---|
| 897 | ##fixme: needs a UI flag or a config knob
|
---|
| 898 | $sql .= " AND reserve_for = 0";
|
---|
[633] | 899 | # Sorting and limiting, since we don't (currently) care to provide a selection of
|
---|
| 900 | # blocks to carve up. This preserves something resembling optimal usage of the IP
|
---|
| 901 | # space by forcing contiguous allocations and free blocks as much as possible.
|
---|
| 902 | $sql .= " ORDER BY masklen(cidr) DESC,cidr LIMIT 1";
|
---|
| 903 | } # done setting up SQL for free CIDR block
|
---|
[533] | 904 |
|
---|
[633] | 905 | my ($fbid,$fbfound,$fbparent) = $dbh->selectrow_array($sql, undef, @vallist);
|
---|
| 906 | return $fbid,$fbfound,$fbparent;
|
---|
[533] | 907 | } # end findAllocateFrom()
|
---|
| 908 |
|
---|
| 909 |
|
---|
[536] | 910 | ## IPDB::ipParent()
|
---|
| 911 | # Get an IP's parent pool's details
|
---|
| 912 | # Takes a database handle and IP
|
---|
| 913 | # Returns a hashref to the parent pool block, if any
|
---|
| 914 | sub ipParent {
|
---|
| 915 | my $dbh = shift;
|
---|
| 916 | my $block = shift;
|
---|
| 917 |
|
---|
| 918 | my $pinfo = $dbh->selectrow_hashref("SELECT cidr,custid,type,city,description FROM allocations".
|
---|
[565] | 919 | " WHERE cidr >>= ? AND (type LIKE '_p' OR type LIKE '_d')", undef, ($block) );
|
---|
[536] | 920 | return $pinfo;
|
---|
| 921 | } # end ipParent()
|
---|
| 922 |
|
---|
| 923 |
|
---|
| 924 | ## IPDB::subParent()
|
---|
[529] | 925 | # Get a block's parent's details
|
---|
| 926 | # Takes a database handle and CIDR block
|
---|
[536] | 927 | # Returns a hashref to the parent container block, if any
|
---|
| 928 | sub subParent {
|
---|
[529] | 929 | my $dbh = shift;
|
---|
| 930 | my $block = shift;
|
---|
| 931 |
|
---|
| 932 | my $pinfo = $dbh->selectrow_hashref("SELECT cidr,custid,type,city,description FROM allocations".
|
---|
| 933 | " WHERE cidr >>= ?", undef, ($block) );
|
---|
| 934 | return $pinfo;
|
---|
[536] | 935 | } # end subParent()
|
---|
[529] | 936 |
|
---|
| 937 |
|
---|
[536] | 938 | ## IPDB::blockParent()
|
---|
| 939 | # Get a block's parent's details
|
---|
| 940 | # Takes a database handle and CIDR block
|
---|
| 941 | # Returns a hashref to the parent container block, if any
|
---|
| 942 | sub blockParent {
|
---|
| 943 | my $dbh = shift;
|
---|
| 944 | my $block = shift;
|
---|
| 945 |
|
---|
| 946 | my $pinfo = $dbh->selectrow_hashref("SELECT cidr,city FROM routed".
|
---|
| 947 | " WHERE cidr >>= ?", undef, ($block) );
|
---|
| 948 | return $pinfo;
|
---|
| 949 | } # end blockParent()
|
---|
| 950 |
|
---|
| 951 |
|
---|
[697] | 952 | ## IPDB::getBreadCrumbs()
|
---|
| 953 | # Retrieve the ID and CIDR of a block's parent(s) up to the master block
|
---|
| 954 | # Returns an arrayref to a list of hashrefs with CIDR and block ID
|
---|
| 955 | sub getBreadCrumbs {
|
---|
| 956 | my $dbh = shift;
|
---|
| 957 | my $parent = shift;
|
---|
| 958 | my @result;
|
---|
| 959 |
|
---|
| 960 | my $sth = $dbh-> prepare("SELECT cidr,type,id,parent_id FROM allocations WHERE id=?");
|
---|
| 961 |
|
---|
| 962 | while ($parent != 0) {
|
---|
| 963 | $sth->execute($parent);
|
---|
| 964 | my ($cidr,$type,$id,$pid) = $sth->fetchrow_array;
|
---|
| 965 | push @result, {cidr => $cidr, link => $id, ispool => ($type =~ /^.[dp]$/ ? 1 : 0)};
|
---|
| 966 | $parent = $pid;
|
---|
| 967 | }
|
---|
| 968 |
|
---|
| 969 | return \@result;
|
---|
| 970 | } # end getBread()
|
---|
| 971 |
|
---|
| 972 |
|
---|
[527] | 973 | ## IPDB::getRoutedCity()
|
---|
| 974 | # Get the city for a routed block.
|
---|
| 975 | sub getRoutedCity {
|
---|
| 976 | my $dbh = shift;
|
---|
| 977 | my $block = shift;
|
---|
| 978 |
|
---|
| 979 | my ($rcity) = $dbh->selectrow_array("SELECT city FROM routed WHERE cidr = ?", undef, ($block) );
|
---|
| 980 | return $rcity;
|
---|
| 981 | } # end getRoutedCity()
|
---|
| 982 |
|
---|
| 983 |
|
---|
[77] | 984 | ## IPDB::allocateBlock()
|
---|
[66] | 985 | # Does all of the magic of actually allocating a netblock
|
---|
[554] | 986 | # Requires a database handle, and a hash containing the block to allocate, routing depth, custid,
|
---|
| 987 | # type, city, block to allocate from, and optionally a description, notes, circuit ID,
|
---|
| 988 | # and private data
|
---|
[77] | 989 | # Returns a success code and optional error message.
|
---|
| 990 | sub allocateBlock {
|
---|
[554] | 991 | my $dbh = shift;
|
---|
[284] | 992 |
|
---|
[554] | 993 | my %args = @_;
|
---|
| 994 |
|
---|
| 995 | $args{cidr} = new NetAddr::IP $args{cidr};
|
---|
| 996 |
|
---|
| 997 | $args{desc} = '' if !$args{desc};
|
---|
| 998 | $args{notes} = '' if !$args{notes};
|
---|
| 999 | $args{circid} = '' if !$args{circid};
|
---|
| 1000 | $args{privdata} = '' if !$args{privdata};
|
---|
| 1001 | $args{vrf} = '' if !$args{vrf};
|
---|
[691] | 1002 | $args{vlan} = '' if !$args{vlan};
|
---|
[585] | 1003 | $args{rdns} = '' if !$args{rdns};
|
---|
[554] | 1004 |
|
---|
[690] | 1005 | # Could arguably allow this for eg /120 allocations, but end users who get a single v4 IP are
|
---|
| 1006 | # usually given a v6 /64, and most v6 addressing schemes need at least half that address space
|
---|
| 1007 | if ($args{cidr}->{isv6} && $args{rdns} =~ /\%/) {
|
---|
| 1008 | return ('FAIL','Reverse DNS template patterns are not supported for IPv6 allocations');
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
[77] | 1011 | my $sth;
|
---|
[66] | 1012 |
|
---|
[633] | 1013 | # Snag the "type" of the freeblock and its CIDR
|
---|
| 1014 | my ($alloc_from_type, $alloc_from, $fbparent, $fcity, $fbmaster) =
|
---|
| 1015 | $dbh->selectrow_array("SELECT routed,cidr,parent_id,city,master_id FROM freeblocks WHERE id = ?",
|
---|
| 1016 | undef, $args{fbid});
|
---|
| 1017 | $alloc_from = new NetAddr::IP $alloc_from;
|
---|
[692] | 1018 | return ('FAIL',"Failed to allocate $args{cidr}; intended free block was used by another allocation.")
|
---|
| 1019 | if !$fbparent;
|
---|
| 1020 | ##fixme: fail here if !$alloc_from
|
---|
| 1021 | # also consider "lock for allocation" due to multistep allocation process
|
---|
[349] | 1022 |
|
---|
[79] | 1023 | # To contain the error message, if any.
|
---|
[554] | 1024 | my $msg = "Unknown error allocating $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
[79] | 1025 |
|
---|
[77] | 1026 | # Enable transactions and error handling
|
---|
| 1027 | local $dbh->{AutoCommit} = 0; # These need to be local so we don't
|
---|
| 1028 | local $dbh->{RaiseError} = 1; # step on our toes by accident.
|
---|
[66] | 1029 |
|
---|
[554] | 1030 | if ($args{type} =~ /^.i$/) {
|
---|
| 1031 | $msg = "Unable to assign static IP $args{cidr} to $args{custid}";
|
---|
[77] | 1032 | eval {
|
---|
[554] | 1033 | if ($args{cidr}) { # IP specified
|
---|
| 1034 | my ($isavail) = $dbh->selectrow_array("SELECT available FROM poolips WHERE ip=?", undef, ($args{cidr}) );
|
---|
| 1035 | die "IP is not in an IP pool.\n"
|
---|
| 1036 | if !$isavail;
|
---|
| 1037 | die "IP already allocated. Deallocate and reallocate, or update the entry\n"
|
---|
| 1038 | if $isavail eq 'n';
|
---|
| 1039 | } else { # IP not specified, take first available
|
---|
| 1040 | ($args{cidr}) = $dbh->selectrow_array("SELECT ip FROM poolips WHERE pool=? AND available='y' ORDER BY ip",
|
---|
| 1041 | undef, ($args{alloc_from}) );
|
---|
[545] | 1042 | }
|
---|
[633] | 1043 | $dbh->do("UPDATE poolips SET custid = ?, city = ?,available='n', description = ?, notes = ?, ".
|
---|
| 1044 | "circuitid = ?, privdata = ?, vrf = ?, rdns = ? ".
|
---|
| 1045 | "WHERE ip = ? AND parent_id = ?", undef,
|
---|
| 1046 | ($args{custid}, $args{city}, $args{desc}, $args{notes},
|
---|
| 1047 | $args{circid}, $args{privdata}, $args{vrf}, $args{rdns},
|
---|
| 1048 | $args{cidr}, $args{parent}) );
|
---|
[157] | 1049 |
|
---|
[397] | 1050 | # node hack
|
---|
[554] | 1051 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 1052 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 1053 | }
|
---|
| 1054 | # end node hack
|
---|
[545] | 1055 |
|
---|
[691] | 1056 | $dbh->commit; # Allocate IP from pool
|
---|
[77] | 1057 | };
|
---|
| 1058 | if ($@) {
|
---|
[545] | 1059 | $msg .= ": $@";
|
---|
[78] | 1060 | eval { $dbh->rollback; };
|
---|
[578] | 1061 | return ('FAIL', $msg);
|
---|
[77] | 1062 | } else {
|
---|
[585] | 1063 | _rpc('addOrUpdateRevRec', cidr => "$args{cidr}", name => $args{rdns}, rpcuser => $args{user});
|
---|
[578] | 1064 | return ('OK', $args{cidr});
|
---|
[77] | 1065 | }
|
---|
| 1066 |
|
---|
| 1067 | } else { # end IP-from-pool allocation
|
---|
| 1068 |
|
---|
[633] | 1069 | if ($args{cidr} == $alloc_from) {
|
---|
[77] | 1070 | # Easiest case- insert in one table, delete in the other, and go home. More or less.
|
---|
| 1071 | # insert into allocations values (cidr,custid,type,city,desc) and
|
---|
| 1072 | # delete from freeblocks where cidr='cidr'
|
---|
| 1073 | # For data safety on non-transaction DBs, we delete first.
|
---|
| 1074 |
|
---|
| 1075 | eval {
|
---|
[554] | 1076 | $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
| 1077 |
|
---|
[633] | 1078 | # Insert the allocations entry
|
---|
| 1079 | $dbh->do("INSERT INTO allocations ".
|
---|
[691] | 1080 | "(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns)".
|
---|
| 1081 | " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", undef,
|
---|
| 1082 | ($args{cidr}, $fbparent, $fbmaster, $args{vrf}, $args{vlan}, $args{custid}, $args{type}, $args{city},
|
---|
[633] | 1083 | $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
|
---|
| 1084 | my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
|
---|
[555] | 1085 |
|
---|
[554] | 1086 | # Munge freeblocks
|
---|
| 1087 | if ($args{type} =~ /^(.)[mc]$/) {
|
---|
| 1088 | # special case - block is a routed or container/"reserve" block
|
---|
| 1089 | my $rtype = $1;
|
---|
[633] | 1090 | $dbh->do("UPDATE freeblocks SET routed = ?,city = ?,parent_id = ? WHERE id = ?",
|
---|
| 1091 | undef, ($rtype, $args{city}, $bid, $args{fbid}) );
|
---|
[77] | 1092 | } else {
|
---|
[554] | 1093 | # "normal" case
|
---|
[633] | 1094 | $dbh->do("DELETE FROM freeblocks WHERE id = ?", undef, ($args{fbid}) );
|
---|
[554] | 1095 | }
|
---|
[77] | 1096 |
|
---|
[554] | 1097 | # And initialize the pool, if necessary
|
---|
| 1098 | # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
|
---|
| 1099 | # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
|
---|
| 1100 | if ($args{type} =~ /^.p$/) {
|
---|
| 1101 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[633] | 1102 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $bid);
|
---|
[554] | 1103 | die $rmsg if $code eq 'FAIL';
|
---|
| 1104 | } elsif ($args{type} =~ /^.d$/) {
|
---|
| 1105 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[633] | 1106 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $bid);
|
---|
[554] | 1107 | die $rmsg if $code eq 'FAIL';
|
---|
| 1108 | }
|
---|
[79] | 1109 |
|
---|
[397] | 1110 | # node hack
|
---|
[554] | 1111 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 1112 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 1113 | }
|
---|
| 1114 | # end node hack
|
---|
[574] | 1115 |
|
---|
[691] | 1116 | $dbh->commit; # Simple block allocation
|
---|
[78] | 1117 | }; # end of eval
|
---|
[77] | 1118 | if ($@) {
|
---|
[157] | 1119 | $msg .= ": ".$@;
|
---|
[77] | 1120 | eval { $dbh->rollback; };
|
---|
[157] | 1121 | return ('FAIL',$msg);
|
---|
[78] | 1122 | }
|
---|
[77] | 1123 |
|
---|
| 1124 | } else { # cidr != alloc_from
|
---|
| 1125 |
|
---|
| 1126 | # Hard case. Allocation is smaller than free block.
|
---|
[633] | 1127 |
|
---|
| 1128 | # make sure new allocation is in fact within freeblock. *sigh*
|
---|
| 1129 | return ('FAIL',"Requested allocation $args{cidr} is not within $alloc_from")
|
---|
| 1130 | if !$alloc_from->contains($args{cidr});
|
---|
[554] | 1131 | my $wantmaskbits = $args{cidr}->masklen;
|
---|
[633] | 1132 | my $maskbits = $alloc_from->masklen;
|
---|
[77] | 1133 |
|
---|
| 1134 | my @newfreeblocks; # Holds free blocks generated from splitting the source freeblock.
|
---|
| 1135 |
|
---|
| 1136 | # This determines which blocks will be left "free" after allocation. We take the
|
---|
| 1137 | # block we're allocating from, and split it in half. We see which half the wanted
|
---|
| 1138 | # block is in, and repeat until the wanted block is equal to one of the halves.
|
---|
| 1139 | my $i=0;
|
---|
[633] | 1140 | my $tmp_from = $alloc_from; # So we don't munge $args{alloc_from}
|
---|
[77] | 1141 | while ($maskbits++ < $wantmaskbits) {
|
---|
| 1142 | my @subblocks = $tmp_from->split($maskbits);
|
---|
[554] | 1143 | $newfreeblocks[$i++] = (($args{cidr}->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
|
---|
| 1144 | $tmp_from = ( ($args{cidr}->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
|
---|
[77] | 1145 | } # while
|
---|
| 1146 |
|
---|
| 1147 | # Begin SQL transaction block
|
---|
| 1148 | eval {
|
---|
[554] | 1149 | $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
[79] | 1150 |
|
---|
[77] | 1151 | # Delete old freeblocks entry
|
---|
[633] | 1152 | $dbh->do("DELETE FROM freeblocks WHERE id = ?", undef, ($args{fbid}) );
|
---|
[77] | 1153 |
|
---|
[633] | 1154 | # Insert the allocations entry
|
---|
| 1155 | $dbh->do("INSERT INTO allocations ".
|
---|
[691] | 1156 | "(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns)".
|
---|
| 1157 | " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", undef,
|
---|
| 1158 | ($args{cidr}, $fbparent, $fbmaster, $args{vrf}, $args{vlan}, $args{custid}, $args{type}, $args{city},
|
---|
[633] | 1159 | $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
|
---|
| 1160 | my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
|
---|
| 1161 |
|
---|
[692] | 1162 | # Insert new list of smaller free blocks left over. Flag the one that matches the
|
---|
| 1163 | # masklength of the new allocation, if a reserve block was requested.
|
---|
| 1164 | $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id,reserve_for) ".
|
---|
| 1165 | "VALUES (?,?,?,?,?,?,?)");
|
---|
| 1166 | foreach my $block (@newfreeblocks) {
|
---|
| 1167 | $sth->execute($block, $fcity, $alloc_from_type, $args{vrf}, $fbparent, $fbmaster,
|
---|
| 1168 | ($block->masklen == $wantmaskbits ? $bid : 0));
|
---|
| 1169 | }
|
---|
| 1170 |
|
---|
[554] | 1171 | # For routed/container types, add a freeblock within the allocated block so we can subdivide it further
|
---|
| 1172 | if ($args{type} =~ /(.)[mc]/) { # rm and .c types - containers
|
---|
| 1173 | my $rtype = $1;
|
---|
[698] | 1174 | $sth->execute($args{cidr}, $args{city}, $rtype, $args{vrf}, $bid, $fbmaster, 0);
|
---|
[554] | 1175 | }
|
---|
[79] | 1176 |
|
---|
[554] | 1177 | # And initialize the pool, if necessary
|
---|
| 1178 | # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
|
---|
| 1179 | # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
|
---|
| 1180 | if ($args{type} =~ /^.p$/) {
|
---|
| 1181 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[633] | 1182 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $bid);
|
---|
[554] | 1183 | die $rmsg if $code eq 'FAIL';
|
---|
| 1184 | } elsif ($args{type} =~ /^.d$/) {
|
---|
| 1185 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[633] | 1186 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $bid);
|
---|
[554] | 1187 | die $rmsg if $code eq 'FAIL';
|
---|
| 1188 | }
|
---|
[77] | 1189 |
|
---|
[397] | 1190 | # node hack
|
---|
[554] | 1191 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 1192 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 1193 | }
|
---|
| 1194 | # end node hack
|
---|
[554] | 1195 |
|
---|
[691] | 1196 | $dbh->commit; # Complex block allocation
|
---|
[77] | 1197 | }; # end eval
|
---|
| 1198 | if ($@) {
|
---|
[256] | 1199 | $msg .= ": ".$@;
|
---|
[77] | 1200 | eval { $dbh->rollback; };
|
---|
[78] | 1201 | return ('FAIL',$msg);
|
---|
[77] | 1202 | }
|
---|
| 1203 |
|
---|
| 1204 | } # end fullcidr != alloc_from
|
---|
| 1205 |
|
---|
[590] | 1206 | # now we do the DNS dance for netblocks, if we have an RPC server to do it with and a pattern to use.
|
---|
| 1207 | _rpc('addOrUpdateRevRec', cidr => "$args{cidr}", name => $args{rdns}, rpcuser => $args{user})
|
---|
| 1208 | if $args{rdns};
|
---|
[585] | 1209 |
|
---|
[677] | 1210 | # and the per-IP set, if there is one.
|
---|
| 1211 | _rpc('updateRevSet', %{$args{iprev}}, rpcuser => $args{user});
|
---|
| 1212 |
|
---|
[585] | 1213 | return ('OK', 'OK');
|
---|
| 1214 |
|
---|
[77] | 1215 | } # end static-IP vs netblock allocation
|
---|
| 1216 |
|
---|
| 1217 | } # end allocateBlock()
|
---|
| 1218 |
|
---|
| 1219 |
|
---|
| 1220 | ## IPDB::initPool()
|
---|
| 1221 | # Initializes a pool
|
---|
| 1222 | # Requires a database handle, the pool CIDR, type, city, and a parameter
|
---|
| 1223 | # indicating whether the pool should allow allocation of literally every
|
---|
| 1224 | # IP, or if it should reserve network/gateway/broadcast IPs
|
---|
[78] | 1225 | # Note that this is NOT done in a transaction, that's why it's a private
|
---|
| 1226 | # function and should ONLY EVER get called from allocateBlock()
|
---|
[77] | 1227 | sub initPool {
|
---|
[633] | 1228 | my ($dbh,undef,$type,$city,$class,$parent) = @_;
|
---|
[77] | 1229 | my $pool = new NetAddr::IP $_[1];
|
---|
| 1230 |
|
---|
[574] | 1231 | # IPv6 does not lend itself to IP pools as supported
|
---|
| 1232 | return ('FAIL',"Refusing to create IPv6 static IP pool") if $pool->{isv6};
|
---|
| 1233 | # IPv4 pools don't make much sense beyond even /24. Allow up to 4096-host footshooting anyway.
|
---|
| 1234 | # NetAddr::IP won't allow more than a /16 (65k hosts).
|
---|
| 1235 | return ('FAIL',"Refusing to create oversized static IP pool") if $pool->masklen <= 20;
|
---|
| 1236 |
|
---|
[691] | 1237 | # Retrieve some odds and ends for defaults on the IPs
|
---|
[633] | 1238 | my ($pcustid) = $dbh->selectrow_array("SELECT def_custid FROM alloctypes WHERE type=?", undef, ($type) );
|
---|
[699] | 1239 | my ($vrf,$vlan,$master) = $dbh->selectrow_array("SELECT vrf,vlan,master_id FROM allocations WHERE id = ?",
|
---|
[691] | 1240 | undef, ($parent) );
|
---|
| 1241 |
|
---|
[157] | 1242 | $type =~ s/[pd]$/i/;
|
---|
[77] | 1243 | my $sth;
|
---|
[157] | 1244 | my $msg;
|
---|
[77] | 1245 |
|
---|
[157] | 1246 | # Trap errors so we can pass them back to the caller. Even if the
|
---|
| 1247 | # caller is only ever supposed to be local, and therefore already
|
---|
| 1248 | # trapping errors. >:(
|
---|
| 1249 | local $dbh->{AutoCommit} = 0; # These need to be local so we don't
|
---|
| 1250 | local $dbh->{RaiseError} = 1; # step on our toes by accident.
|
---|
| 1251 |
|
---|
| 1252 | eval {
|
---|
| 1253 | # have to insert all pool IPs into poolips table as "unallocated".
|
---|
[699] | 1254 | $sth = $dbh->prepare("INSERT INTO poolips (ip,custid,city,type,parent_id,master_id) VALUES (?,?,?,?,?,?)");
|
---|
[695] | 1255 |
|
---|
| 1256 | # in case of pool extension by some means, we need to see what IPs were already inserted
|
---|
| 1257 | my $tmp1 = $dbh->selectall_arrayref("SELECT ip FROM poolips WHERE parent_id = ?", undef, $parent);
|
---|
| 1258 | my %foundips;
|
---|
| 1259 | foreach (@{$tmp1}) {
|
---|
| 1260 | $foundips{$_->[0]} = 1;
|
---|
| 1261 | }
|
---|
| 1262 |
|
---|
[696] | 1263 | # Dodge an edge case - pool where IPs have been "stolen" and turned into a netblock assignment.
|
---|
| 1264 | # We can't just "get all the current IPs, and add the missing ones", because some IPs are
|
---|
| 1265 | # legitimately missing (for stretchy values of "legitimately").
|
---|
| 1266 |
|
---|
| 1267 | my $pdata = getBlockData($dbh, $parent);
|
---|
| 1268 | my $pcidr = new NetAddr::IP $pdata->{block};
|
---|
| 1269 |
|
---|
| 1270 | if ($pcidr != $pool) {
|
---|
| 1271 | # enumerate the IPs from the *old* pool, flag them as "found", so we can iterate the entire
|
---|
| 1272 | # requested pool and still make sure we skip the IPs in the old pool - even if they've been
|
---|
| 1273 | # "stolen" by legacy netblocks.
|
---|
| 1274 | my @oldips = $pcidr->hostenum;
|
---|
| 1275 | # decide whether to start excluding existing IPs at the "gateway" or "gateway+1"
|
---|
| 1276 | my $ostart = ($pdata->{type} =~ /^.d$/ ? 1 : 0);
|
---|
| 1277 | for (my $i = $ostart; $i<= $#oldips; $i++) {
|
---|
| 1278 | $foundips{$oldips[$i]} = 1;
|
---|
| 1279 | }
|
---|
| 1280 | }
|
---|
| 1281 |
|
---|
[695] | 1282 | # enumerate the hosts in the IP range - everything except the first (net) and last (bcast) IP
|
---|
[157] | 1283 | my @poolip_list = $pool->hostenum;
|
---|
[695] | 1284 |
|
---|
| 1285 | # always check/add IPs from gw+1 through bcast-1:
|
---|
| 1286 | # (but the set won't be in oooorderrrrr! <pout>)
|
---|
| 1287 | for (my $i=1; $i<=$#poolip_list; $i++) {
|
---|
[696] | 1288 | my $baseip = $poolip_list[$i]->addr;
|
---|
| 1289 | if ($baseip !~ /\.(?:0|255)$/ && !$foundips{$poolip_list[$i]}) {
|
---|
[699] | 1290 | $sth->execute($baseip, $pcustid, $city, $type, $parent, $master);
|
---|
[696] | 1291 | }
|
---|
[695] | 1292 | }
|
---|
| 1293 |
|
---|
| 1294 | # now do the special case - DSL/PPP blocks can use the "net", "gw", and "bcast" IPs.
|
---|
| 1295 | # we exclude .0 and .255 anyway, since while they'll mostly work, they *will* behave badly here and there.
|
---|
[157] | 1296 | if ($class eq 'all') { # (DSL-ish block - *all* IPs available
|
---|
[246] | 1297 | if ($pool->addr !~ /\.0$/) { # .0 causes weirdness.
|
---|
[699] | 1298 | $sth->execute($pool->addr, $pcustid, $city, $type, $parent, $master) unless $foundips{$pool->addr};
|
---|
[246] | 1299 | }
|
---|
[699] | 1300 | $sth->execute($poolip_list[0]->addr, $pcustid, $city, $type, $parent, $master) unless $foundips{$poolip_list[0]};
|
---|
[157] | 1301 | $pool--;
|
---|
[246] | 1302 | if ($pool->addr !~ /\.255$/) { # .255 can cause weirdness.
|
---|
[699] | 1303 | $sth->execute($pool->addr, $pcustid, $city, $type, $parent, $master) unless $foundips{$pool->addr};
|
---|
[246] | 1304 | }
|
---|
[77] | 1305 | }
|
---|
[633] | 1306 | # don't commit here! the caller may not be done.
|
---|
| 1307 | # $dbh->commit;
|
---|
[157] | 1308 | };
|
---|
| 1309 | if ($@) {
|
---|
[574] | 1310 | $msg = $@;
|
---|
[633] | 1311 | # Don't roll back! It's up to the caller to handle this.
|
---|
| 1312 | # eval { $dbh->rollback; };
|
---|
[157] | 1313 | return ('FAIL',$msg);
|
---|
| 1314 | } else {
|
---|
| 1315 | return ('OK',"OK");
|
---|
[77] | 1316 | }
|
---|
| 1317 | } # end initPool()
|
---|
| 1318 |
|
---|
| 1319 |
|
---|
[531] | 1320 | ## IPDB::updateBlock()
|
---|
| 1321 | # Update an allocation
|
---|
| 1322 | # Takes all allocation fields in a hash
|
---|
| 1323 | sub updateBlock {
|
---|
| 1324 | my $dbh = shift;
|
---|
| 1325 | my %args = @_;
|
---|
| 1326 |
|
---|
| 1327 | return ('FAIL', 'Missing block to update') if !$args{block};
|
---|
| 1328 |
|
---|
[634] | 1329 | # Spaces don't show up well in lots of places. Make sure they don't get into the DB.
|
---|
| 1330 | $args{custid} =~ s/^\s+//;
|
---|
| 1331 | $args{custid} =~ s/\s+$//;
|
---|
| 1332 |
|
---|
[531] | 1333 | # do it all in a transaction
|
---|
| 1334 | local $dbh->{AutoCommit} = 0;
|
---|
| 1335 | local $dbh->{RaiseError} = 1;
|
---|
| 1336 |
|
---|
| 1337 | my @fieldlist;
|
---|
| 1338 | my @vallist;
|
---|
[691] | 1339 | foreach ('custid', 'city', 'description', 'notes', 'circuitid', 'privdata', 'rdns', 'vrf', 'vlan') {
|
---|
[531] | 1340 | if ($args{$_}) {
|
---|
| 1341 | push @fieldlist, $_;
|
---|
| 1342 | push @vallist, $args{$_};
|
---|
| 1343 | }
|
---|
| 1344 | }
|
---|
| 1345 |
|
---|
[634] | 1346 | my $binfo;
|
---|
[531] | 1347 | my $updtable = 'allocations';
|
---|
[634] | 1348 | my $keyfield = 'id';
|
---|
[535] | 1349 | if ($args{type} =~ /^(.)i$/) {
|
---|
[531] | 1350 | $updtable = 'poolips';
|
---|
[634] | 1351 | $binfo = getBlockData($dbh, $args{block}, 'i');
|
---|
[531] | 1352 | } else {
|
---|
| 1353 | ## fixme: there's got to be a better way...
|
---|
[634] | 1354 | $binfo = getBlockData($dbh, $args{block});
|
---|
[531] | 1355 | if ($args{swip}) {
|
---|
| 1356 | if ($args{swip} eq 'on' || $args{swip} eq '1' || $args{swip} eq 'y') {
|
---|
| 1357 | $args{swip} = 'y';
|
---|
| 1358 | } else {
|
---|
| 1359 | $args{swip} = 'n';
|
---|
| 1360 | }
|
---|
| 1361 | }
|
---|
| 1362 | foreach ('type', 'swip') {
|
---|
| 1363 | if ($args{$_}) {
|
---|
| 1364 | push @fieldlist, $_;
|
---|
| 1365 | push @vallist, $args{$_};
|
---|
| 1366 | }
|
---|
| 1367 | }
|
---|
| 1368 | }
|
---|
| 1369 |
|
---|
| 1370 | return ('FAIL', 'No fields to update') if !@fieldlist;
|
---|
| 1371 |
|
---|
| 1372 | my $sql = "UPDATE $updtable SET ";
|
---|
| 1373 | $sql .= join " = ?, ", @fieldlist;
|
---|
| 1374 |
|
---|
| 1375 | eval {
|
---|
[695] | 1376 | # check for block merge first...
|
---|
| 1377 | if ($args{fbmerge}) {
|
---|
| 1378 | my $cidr = NetAddr::IP->new($binfo->{block});
|
---|
| 1379 | my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
|
---|
| 1380 | # safety net? make sure mergeable block passed in is really one or both of
|
---|
| 1381 | # a) reserved for expansion of the block and
|
---|
| 1382 | # b) confirmed CIDR-combinable
|
---|
| 1383 | # "safety? SELECT foo FROM freeblocks WHERE cidr << ? AND masklen(cidr) = ?, $newblock, ".$cidr->masklen."\n";
|
---|
| 1384 | $dbh->do("DELETE FROM freeblocks WHERE id=?", undef, $args{fbmerge});
|
---|
| 1385 | # ... so we can append the change in the stored CIDR field to extend the allocation.
|
---|
| 1386 | $sql .= " = ?, cidr";
|
---|
| 1387 | push @vallist, $newblock;
|
---|
| 1388 | # if we have an IP pool, call initPool to fill in any missing entries in the pool
|
---|
| 1389 | if ($binfo->{type} =~ /^.p$/) {
|
---|
| 1390 | my ($code,$rmsg) = initPool($dbh, "$newblock", $binfo->{type}, $binfo->{city}, 'all', $args{block});
|
---|
| 1391 | die $rmsg if $code eq 'FAIL';
|
---|
| 1392 | } elsif ($binfo->{type} =~ /^.d$/) {
|
---|
| 1393 | my ($code,$rmsg) = initPool($dbh, "$newblock", $binfo->{type}, $binfo->{city}, 'normal', $args{block});
|
---|
| 1394 | die $rmsg if $code eq 'FAIL';
|
---|
| 1395 | }
|
---|
| 1396 | }
|
---|
| 1397 |
|
---|
| 1398 | # append another SQL fragment
|
---|
| 1399 | push @vallist, $args{block};
|
---|
| 1400 | $sql .= " = ? WHERE $keyfield = ?";
|
---|
| 1401 |
|
---|
[531] | 1402 | # do the update
|
---|
| 1403 | $dbh->do($sql, undef, @vallist);
|
---|
| 1404 |
|
---|
| 1405 | if ($args{node}) {
|
---|
| 1406 | # done with delete/insert so we don't have to worry about funkyness updating a node ref that isn't there
|
---|
[634] | 1407 | $dbh->do("DELETE FROM noderef WHERE block = ?", undef, ($binfo->{block}) );
|
---|
| 1408 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($binfo->{block}, $args{node}) )
|
---|
| 1409 | if $args{node} ne '--';
|
---|
[531] | 1410 | }
|
---|
| 1411 |
|
---|
| 1412 | $dbh->commit;
|
---|
| 1413 | };
|
---|
| 1414 | if ($@) {
|
---|
| 1415 | my $msg = $@;
|
---|
| 1416 | $dbh->rollback;
|
---|
| 1417 | return ('FAIL', $msg);
|
---|
| 1418 | }
|
---|
[588] | 1419 |
|
---|
[686] | 1420 | # In case of any container (mainly master block), only update freeblocks so we don't stomp subs
|
---|
| 1421 | # (which would be the wrong thing in pretty much any case except "DELETE ALL EVARYTHING!!1!oneone!")
|
---|
| 1422 | if ($binfo->{type} =~ '.[mc]') {
|
---|
| 1423 | # Not using listFree() as it doesn't return quite all of the blocks wanted.
|
---|
| 1424 | # Retrieve the immediate free blocks
|
---|
| 1425 | my $sth = $dbh->prepare(q(
|
---|
| 1426 | SELECT cidr FROM freeblocks WHERE parent_id = ?
|
---|
| 1427 | UNION
|
---|
| 1428 | SELECT cidr FROM freeblocks f WHERE
|
---|
| 1429 | cidr = (SELECT cidr FROM allocations a WHERE f.cidr = a.cidr)
|
---|
| 1430 | AND master_id = ?
|
---|
| 1431 | ) );
|
---|
| 1432 | $sth->execute($args{block}, $binfo->{master_id});
|
---|
| 1433 | my %fbset;
|
---|
| 1434 | while (my ($fb) = $sth->fetchrow_array) {
|
---|
| 1435 | $fbset{"host_$fb"} = $args{rdns};
|
---|
| 1436 | }
|
---|
| 1437 | # We use this RPC call instead of multiple addOrUpdateRevRec calls, since we don't
|
---|
| 1438 | # know how many records we'll be updating and more than 3-4 is far too slow. This
|
---|
| 1439 | # should be safe to call unconditionally.
|
---|
| 1440 | # Requires dnsadmin >= r678
|
---|
| 1441 | _rpc('updateRevSet', %fbset, rpcuser => $args{user});
|
---|
[677] | 1442 |
|
---|
[686] | 1443 | } else {
|
---|
| 1444 | $binfo->{block} =~ s|/32$||;
|
---|
| 1445 | _rpc('addOrUpdateRevRec', cidr => $binfo->{block}, name => $args{rdns}, rpcuser => $args{user});
|
---|
[677] | 1446 |
|
---|
[686] | 1447 | # and the per-IP set, if there is one.
|
---|
| 1448 | _rpc('updateRevSet', %{$args{iprev}}, rpcuser => $args{user}) if keys (%{$args{iprev}});
|
---|
| 1449 | }
|
---|
| 1450 |
|
---|
[588] | 1451 | return ('OK','OK');
|
---|
[531] | 1452 | } # end updateBlock()
|
---|
| 1453 |
|
---|
| 1454 |
|
---|
[93] | 1455 | ## IPDB::deleteBlock()
|
---|
| 1456 | # Removes an allocation from the database, including deleting IPs
|
---|
| 1457 | # from poolips and recombining entries in freeblocks if possible
|
---|
| 1458 | # Also handles "deleting" a static IP allocation, and removal of a master
|
---|
[558] | 1459 | # Requires a database handle, the block to delete, the routing depth (if applicable),
|
---|
[590] | 1460 | # the VRF ID, and a flag to indicate whether to delete associated forward DNS entries
|
---|
| 1461 | # as well as the reverse entry
|
---|
[93] | 1462 | sub deleteBlock {
|
---|
[638] | 1463 | my ($dbh,$id,$basetype,$delfwd,$user) = @_;
|
---|
[93] | 1464 |
|
---|
[638] | 1465 | # Collect info about the block we're going to delete
|
---|
| 1466 | my $binfo = getBlockData($dbh, $id, $basetype);
|
---|
| 1467 | my $cidr = new NetAddr::IP $binfo->{block};
|
---|
| 1468 |
|
---|
[558] | 1469 | # For possible auto-VRF-ignoring (since public IPs shouldn't usually be present in more than one VRF)
|
---|
| 1470 | # is_rfc1918 requires NetAddr::IP >= 4.059
|
---|
| 1471 | # rather than doing this over and over and over.....
|
---|
| 1472 | my $tmpnum = $cidr->numeric;
|
---|
| 1473 | # 192.168.0.0/16 -> 192.168.255.255 => 3232235520 -> 3232301055
|
---|
| 1474 | # 172.16.0.0/12 -> 172.31.255.255 => 2886729728 -> 2887778303
|
---|
| 1475 | # 10.0.0.0/8 -> 10.255.255.255 => 167772160 -> 184549375
|
---|
| 1476 | my $isprivnet = (3232235520 <= $tmpnum && $tmpnum <= 3232301055) ||
|
---|
| 1477 | (2886729728 <= $tmpnum && $tmpnum <= 2887778303) ||
|
---|
| 1478 | (167772160 <= $tmpnum && $tmpnum <= 184549375);
|
---|
| 1479 |
|
---|
[93] | 1480 | my $sth;
|
---|
| 1481 |
|
---|
[349] | 1482 | # Magic variables used for odd allocation cases.
|
---|
| 1483 | my $container;
|
---|
| 1484 | my $con_type;
|
---|
| 1485 |
|
---|
[558] | 1486 |
|
---|
| 1487 | # temporarily forced null, until a sane UI for VRF tracking can be found.
|
---|
[638] | 1488 | # $vrf = '';# if !$vrf; # as with SQL, the null value is not equal to ''. *sigh*
|
---|
[558] | 1489 |
|
---|
[93] | 1490 | # To contain the error message, if any.
|
---|
[558] | 1491 | my $msg = "Unknown error deallocating $binfo->{type} $cidr";
|
---|
| 1492 | my $goback; # to put the parent in so we can link back where the deallocate started
|
---|
| 1493 |
|
---|
[93] | 1494 | # Enable transactions and exception-on-errors... but only for this sub
|
---|
| 1495 | local $dbh->{AutoCommit} = 0;
|
---|
| 1496 | local $dbh->{RaiseError} = 1;
|
---|
| 1497 |
|
---|
[558] | 1498 | if ($binfo->{type} =~ /^.i$/) {
|
---|
[638] | 1499 | # First case. The "block" is a static IP
|
---|
| 1500 | # Note that we still need some additional code in the odd case
|
---|
| 1501 | # of a netblock-aligned contiguous group of static IPs
|
---|
[93] | 1502 |
|
---|
| 1503 | eval {
|
---|
[558] | 1504 | $msg = "Unable to deallocate $disp_alloctypes{$binfo->{type}} $cidr";
|
---|
[638] | 1505 | my $pinfo = getBlockData($dbh, $binfo->{parent_id}, 'b');
|
---|
[558] | 1506 | ##fixme: VRF and rdepth
|
---|
[638] | 1507 | $dbh->do("UPDATE poolips SET custid = ?, available = 'y',".
|
---|
| 1508 | "city = (SELECT city FROM allocations WHERE id = ?),".
|
---|
| 1509 | "description = '', notes = '', circuitid = '', vrf = ? WHERE id = ?", undef,
|
---|
| 1510 | ($pinfo->{custid}, $binfo->{parent_id}, $pinfo->{vrf}, $id) );
|
---|
[93] | 1511 | $dbh->commit;
|
---|
| 1512 | };
|
---|
| 1513 | if ($@) {
|
---|
[558] | 1514 | $msg .= ": $@";
|
---|
[93] | 1515 | eval { $dbh->rollback; };
|
---|
| 1516 | return ('FAIL',$msg);
|
---|
| 1517 | } else {
|
---|
[590] | 1518 | ##fixme: RPC return code?
|
---|
[638] | 1519 | _rpc('delByCIDR', cidr => "$cidr", user => $user, delforward => $delfwd, rpcuser => $user);
|
---|
[93] | 1520 | return ('OK',"OK");
|
---|
| 1521 | }
|
---|
| 1522 |
|
---|
[558] | 1523 | } elsif ($binfo->{type} eq 'mm') { # end alloctype =~ /.i/
|
---|
[638] | 1524 | # Second case. The block is a full master block
|
---|
[93] | 1525 |
|
---|
[558] | 1526 | ##fixme: VRF limit
|
---|
[93] | 1527 | $msg = "Unable to delete master block $cidr";
|
---|
| 1528 | eval {
|
---|
[638] | 1529 | $dbh->do("DELETE FROM allocations WHERE cidr <<= ? AND master_id = ?", undef, ($cidr, $binfo->{master_id}) );
|
---|
| 1530 | $dbh->do("DELETE FROM freeblocks WHERE cidr <<= ? AND master_id = ?", undef, ($cidr, $binfo->{master_id}) );
|
---|
[93] | 1531 | $dbh->commit;
|
---|
| 1532 | };
|
---|
| 1533 | if ($@) {
|
---|
[558] | 1534 | $msg .= ": $@";
|
---|
[93] | 1535 | eval { $dbh->rollback; };
|
---|
| 1536 | return ('FAIL', $msg);
|
---|
[591] | 1537 | }
|
---|
| 1538 |
|
---|
| 1539 | # Have to handle potentially split reverse zones. Assume they *are* split,
|
---|
| 1540 | # since if we added them here, they would have been added split.
|
---|
| 1541 | # allow splitting reverse zones to be disabled, maybe, someday
|
---|
| 1542 | #if ($splitrevzones && !$cidr->{isv6}) {
|
---|
| 1543 | my @zonelist;
|
---|
| 1544 | if (1 && !$cidr->{isv6}) {
|
---|
| 1545 | my $splitpoint = ($cidr->masklen <= 16 ? 16 : 24); # hack pthui
|
---|
| 1546 | @zonelist = $cidr->split($splitpoint);
|
---|
[93] | 1547 | } else {
|
---|
[591] | 1548 | @zonelist = ($cidr);
|
---|
[93] | 1549 | }
|
---|
[591] | 1550 | my @fails;
|
---|
| 1551 | foreach my $subzone (@zonelist) {
|
---|
[638] | 1552 | if ($rpc_url && !_rpc('delZone', zone => "$subzone", revrec => 'y', rpcuser => $user, delforward => $delfwd) ) {
|
---|
[591] | 1553 | push @fails, ("$subzone" => $errstr);
|
---|
| 1554 | }
|
---|
| 1555 | }
|
---|
| 1556 | if (@fails) {
|
---|
| 1557 | return ('WARN',"Warning(s) deleting $cidr from reverse DNS:\n".join("\n", @fails));
|
---|
| 1558 | }
|
---|
| 1559 | return ('OK','OK');
|
---|
[93] | 1560 |
|
---|
| 1561 | } else { # end alloctype master block case
|
---|
| 1562 |
|
---|
| 1563 | ## This is a big block; but it HAS to be done in a chunk. Any removal
|
---|
| 1564 | ## of a netblock allocation may result in a larger chunk of free
|
---|
| 1565 | ## contiguous IP space - which may in turn be combined into a single
|
---|
| 1566 | ## netblock rather than a number of smaller netblocks.
|
---|
| 1567 |
|
---|
[558] | 1568 | my $retcode = 'OK';
|
---|
[638] | 1569 | my ($ptype,$pcity,$ppatt,$p_id);
|
---|
[558] | 1570 |
|
---|
[93] | 1571 | eval {
|
---|
| 1572 |
|
---|
[558] | 1573 | ##fixme: add recursive flag to allow "YES DAMMIT DELETE ALL EVARYTHING!!1!!" without
|
---|
| 1574 | # explicitly deleting any suballocations of the block to be deleted.
|
---|
[93] | 1575 |
|
---|
[638] | 1576 | # get parent info of the block we're deleting
|
---|
| 1577 | my $pinfo = getBlockData($dbh, $binfo->{parent_id});
|
---|
| 1578 | $ptype = $pinfo->{type};
|
---|
| 1579 | $pcity = $pinfo->{city};
|
---|
| 1580 | $ppatt = $pinfo->{rdns};
|
---|
| 1581 | $p_id = $binfo->{parent_id};
|
---|
[93] | 1582 |
|
---|
[558] | 1583 | # Delete the block
|
---|
[638] | 1584 | $dbh->do("DELETE FROM allocations WHERE id = ?", undef, ($id) );
|
---|
[349] | 1585 |
|
---|
[558] | 1586 | # munge the parent type a little
|
---|
[638] | 1587 | $ptype = (split //, $ptype)[1];
|
---|
[93] | 1588 |
|
---|
[558] | 1589 | ##fixme: you can't... CAN NOT.... assign the same public IP to multiple things.
|
---|
| 1590 | # 'Net don't work like that, homey. Restrict VRF-uniqueness to private IPs?
|
---|
| 1591 | # -> $isprivnet flag from start of sub
|
---|
[93] | 1592 |
|
---|
[558] | 1593 | # check to see if any container allocations could be the "true" parent
|
---|
[638] | 1594 | my ($tparent,$tpar_id,$trtype,$tcity);
|
---|
| 1595 | $tpar_id = 0;
|
---|
[404] | 1596 |
|
---|
[638] | 1597 | ##fixme: this is far simpler in the strict VRF case; we "know" that any allocation
|
---|
| 1598 | # contained by a container is a part of the same allocation tree when the VRF fields are equal.
|
---|
[558] | 1599 |
|
---|
[638] | 1600 | # logic:
|
---|
| 1601 | # For each possible container of $cidr
|
---|
| 1602 | # note the parent id
|
---|
| 1603 | # walk the chain up the parents
|
---|
| 1604 | # if we intersect $cidr's current parent, break
|
---|
| 1605 | # if we've intersected $cidr's current parent
|
---|
| 1606 | # set some variables to track that block
|
---|
| 1607 | # break
|
---|
[558] | 1608 |
|
---|
[638] | 1609 | # Set up part of "is it in the middle of a pool?" check
|
---|
| 1610 | my $wuzpool = $dbh->selectrow_hashref("SELECT cidr,parent_id,type,city,custid,id FROM allocations ".
|
---|
| 1611 | "WHERE (type LIKE '_d' OR type LIKE '_p') AND cidr >> ? AND master_id = ?", { Slice => {} },
|
---|
| 1612 | ($cidr, $binfo->{master_id}) );
|
---|
[558] | 1613 |
|
---|
[638] | 1614 | ##fixme?
|
---|
| 1615 | # edge cases not handled, or handled badly:
|
---|
| 1616 | # -> $cidr managed to get to be the entirety of an IP pool
|
---|
[558] | 1617 |
|
---|
[638] | 1618 | if ($wuzpool && $wuzpool->{id} != $id) {
|
---|
| 1619 | # we have legacy goo to be purified
|
---|
| 1620 | # going to ignore nested pools; not possible to create them via API and no current legacy data includes any.
|
---|
| 1621 |
|
---|
| 1622 | # for convenience
|
---|
| 1623 | my $poolid = $wuzpool->{id};
|
---|
| 1624 | my $pool = $wuzpool->{cidr};
|
---|
| 1625 | my $poolcity = $wuzpool->{city};
|
---|
| 1626 | my $pooltype = $wuzpool->{type};
|
---|
| 1627 | my $poolcustid = $wuzpool->{custid};
|
---|
| 1628 |
|
---|
| 1629 | $retcode = 'WARNPOOL';
|
---|
| 1630 | $goback = "$poolid,$pool";
|
---|
| 1631 | # We've already deleted the block, now we have to stuff its IPs into the pool.
|
---|
| 1632 | $pooltype =~ s/[dp]$/i/; # change type to static IP
|
---|
| 1633 | my $sth2 = $dbh->prepare("INSERT INTO poolips (ip,city,type,custid,parent_id) VALUES ".
|
---|
| 1634 | "(?,'$poolcity','$pooltype','$poolcustid',$poolid)");
|
---|
[655] | 1635 |
|
---|
[429] | 1636 | ##fixme: need to not insert net, gateway, and bcast on "real netblock" pools (DHCPish)
|
---|
[638] | 1637 | # don't insert .0
|
---|
| 1638 | $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|;
|
---|
[655] | 1639 | $cidr++;
|
---|
| 1640 | my $bcast = $cidr->broadcast;
|
---|
| 1641 | while ($cidr != $bcast) {
|
---|
| 1642 | $sth2->execute($cidr->addr);
|
---|
| 1643 | $cidr++;
|
---|
[638] | 1644 | }
|
---|
| 1645 | # don't insert .255
|
---|
| 1646 | $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.255$|;
|
---|
[655] | 1647 |
|
---|
| 1648 | # Weirdness Happens. $cidr goes read-only somewhere (this is a thing?!?),
|
---|
| 1649 | # causing ->split, ->hostenum, and related methods to explode. O_o
|
---|
| 1650 | # foreach my $ip ($cidr->hostenum) {
|
---|
| 1651 | # $sth2->execute($ip);
|
---|
| 1652 | # }
|
---|
| 1653 |
|
---|
[638] | 1654 | }
|
---|
[93] | 1655 |
|
---|
[638] | 1656 | ## important!
|
---|
| 1657 | # ... or IS IT?
|
---|
| 1658 | # we may have undef'ed $wuzpool above, if the allocation tree $cidr is in doesn't intersect the pool we found
|
---|
| 1659 | #if (!$wuzpool) {
|
---|
| 1660 |
|
---|
| 1661 | else {
|
---|
| 1662 |
|
---|
[655] | 1663 | # Edge case: Block is the same size as more than one parent level. Should be rare.
|
---|
| 1664 | # - mainly master + first routing. Sorting on parent_id hides the problem pretty well,
|
---|
| 1665 | # but it's likely still possible to fail in particularly well-mangled databases.
|
---|
| 1666 | # The ultimate fix for this may be to resurrect the "routing depth" atrocity. :/
|
---|
[638] | 1667 | # Get all possible (and probably a number of impossible) containers for $cidr
|
---|
| 1668 | $sth = $dbh->prepare("SELECT cidr,parent_id,type,city,id FROM allocations ".
|
---|
| 1669 | "WHERE (type LIKE '_m' OR type LIKE '_c') AND cidr >>= ? AND master_id = ? ".
|
---|
[655] | 1670 | "ORDER BY masklen(cidr) DESC,parent_id DESC");
|
---|
[638] | 1671 | $sth->execute($cidr, $binfo->{master_id});
|
---|
| 1672 |
|
---|
| 1673 | # Quickly get certain fields (simpler than getBlockData()
|
---|
| 1674 | my $sth2 = $dbh->prepare("SELECT cidr,parent_id,type,city FROM allocations ".
|
---|
| 1675 | "WHERE (type LIKE '_m' OR type LIKE '_c') AND id = ? AND master_id = ?");
|
---|
| 1676 |
|
---|
| 1677 | # For each possible container of $cidr...
|
---|
| 1678 | while (my @data = $sth->fetchrow_array) {
|
---|
| 1679 | my $i = 0;
|
---|
| 1680 | # Save some state and set a start point - parent ID of container we're checking
|
---|
| 1681 | $tparent = $data[0];
|
---|
| 1682 | my $ppid = $data[1];
|
---|
| 1683 | $trtype = $data[2];
|
---|
| 1684 | $tcity = $data[3];
|
---|
| 1685 | $tpar_id = $data[4];
|
---|
| 1686 | last if $data[4] == $binfo->{parent_id}; # Preemptively break if we're already in the right place
|
---|
| 1687 | last if $ppid == $binfo->{parent_id}; # ... or if the parent of the container is the block's parent
|
---|
| 1688 | while (1) {
|
---|
| 1689 | # Retrieve bits on that parent ID
|
---|
| 1690 | $sth2->execute($ppid, $binfo->{master_id});
|
---|
| 1691 | my @container = $sth2->fetchrow_array;
|
---|
| 1692 | $ppid = $container[1];
|
---|
| 1693 | last if $container[1] == 0; # Break if we've hit a master block
|
---|
| 1694 | last if $ppid == $binfo->{parent_id}; # Break if we've reached the block $cidr is currently in
|
---|
| 1695 | }
|
---|
| 1696 | last if $ppid == $binfo->{parent_id};
|
---|
| 1697 | }
|
---|
| 1698 |
|
---|
| 1699 | # found an alternate parent; reset some parent-info bits
|
---|
| 1700 | if ($tpar_id != $binfo->{parent_id}) {
|
---|
| 1701 | $ptype = (split //, $trtype)[1];
|
---|
| 1702 | $pcity = $tcity;
|
---|
| 1703 | $retcode = 'WARNMERGE'; # may be redundant
|
---|
| 1704 | $p_id = $tpar_id;
|
---|
| 1705 | }
|
---|
| 1706 |
|
---|
| 1707 | $goback = "$p_id,$tparent"; # breadcrumb, currently only used in case of live-parent-is-not-true-parent
|
---|
| 1708 |
|
---|
| 1709 | # Special case - delete pool IPs
|
---|
| 1710 | if ($binfo->{type} =~ /^.[pd]$/) {
|
---|
[655] | 1711 | # We have to delete the IPs from the pool listing.
|
---|
| 1712 | ##fixme: rdepth? vrf?
|
---|
[638] | 1713 | $dbh->do("DELETE FROM poolips WHERE parent_id = ?", undef, ($id) );
|
---|
| 1714 | }
|
---|
| 1715 |
|
---|
| 1716 | $pinfo = getBlockData($dbh, $p_id);
|
---|
| 1717 |
|
---|
[558] | 1718 | # If the block wasn't legacy goo embedded in a static pool, we check the
|
---|
| 1719 | # freeblocks in the identified parent to see if we can combine any of them.
|
---|
[93] | 1720 |
|
---|
[559] | 1721 | # if the block to be deleted is a container, move its freeblock(s) up a level, and reset their parenting info
|
---|
| 1722 | if ($binfo->{type} =~ /^.[mc]/) {
|
---|
| 1723 | # move the freeblocks into the parent
|
---|
| 1724 | # we don't insert a new freeblock because there could be a live reparented sub.
|
---|
[638] | 1725 | $dbh->do("UPDATE freeblocks SET parent_id = ?, routed = ?, city = ? WHERE parent_id = ?", undef,
|
---|
| 1726 | ($p_id, $ptype, $pcity, $id) );
|
---|
[559] | 1727 | } else {
|
---|
| 1728 | # ... otherwise, add the freeblock
|
---|
[638] | 1729 | $dbh->do("INSERT INTO freeblocks (cidr, city, routed, parent_id, master_id) VALUES (?,?,?,?,?)", undef,
|
---|
| 1730 | ($cidr, $pcity, $ptype, $p_id, $binfo->{master_id}) );
|
---|
[559] | 1731 | }
|
---|
| 1732 |
|
---|
[558] | 1733 | ##fixme: vrf
|
---|
[638] | 1734 | ##fixme: simplify since all containers now represent different "layers"/"levels"?
|
---|
[558] | 1735 | # set up the query to get the list of blocks to try to merge.
|
---|
[638] | 1736 | $sth = $dbh->prepare("SELECT cidr,id FROM freeblocks ".
|
---|
| 1737 | "WHERE parent_id = ? ".
|
---|
[558] | 1738 | "ORDER BY masklen(cidr) DESC");
|
---|
[428] | 1739 |
|
---|
[638] | 1740 | $sth->execute($p_id);
|
---|
[558] | 1741 |
|
---|
[93] | 1742 | # NetAddr::IP->compact() attempts to produce the smallest inclusive block
|
---|
| 1743 | # from the caller and the passed terms.
|
---|
| 1744 | # EG: if you call $cidr->compact($ip1,$ip2,$ip3) when $cidr, $ip1, $ip2,
|
---|
| 1745 | # and $ip3 are consecutive /27's starting on .0 (.0-.31, .32-.63,
|
---|
| 1746 | # .64-.95, and .96-.128), you will get an array containing a single
|
---|
| 1747 | # /25 as element 0 (.0-.127). Order is not important; you could have
|
---|
| 1748 | # $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
|
---|
| 1749 |
|
---|
[638] | 1750 | my (@rawfb, @combinelist, %rawid);
|
---|
[428] | 1751 | my $i=0;
|
---|
[558] | 1752 | # for each free block under $parent, push a NetAddr::IP object into one list, and
|
---|
| 1753 | # continuously use NetAddr::IP->compact to automagically merge netblocks as possible.
|
---|
[428] | 1754 | while (my @data = $sth->fetchrow_array) {
|
---|
| 1755 | my $testIP = new NetAddr::IP $data[0];
|
---|
[558] | 1756 | push @rawfb, $testIP;
|
---|
[671] | 1757 | $rawid{"$testIP"} = $data[1]; # $data[0] vs "$testIP" *does* make a difference for v6
|
---|
[558] | 1758 | @combinelist = $testIP->compact(@combinelist);
|
---|
[93] | 1759 | }
|
---|
| 1760 |
|
---|
[558] | 1761 | # now that we have the full list of "compacted" freeblocks, go back over
|
---|
| 1762 | # the list of raw freeblocks, and delete the ones that got merged.
|
---|
[638] | 1763 | $sth = $dbh->prepare("DELETE FROM freeblocks WHERE id = ?");
|
---|
[558] | 1764 | foreach my $rawfree (@rawfb) {
|
---|
| 1765 | next if grep { $rawfree == $_ } @combinelist; # skip if the raw block is in the compacted list
|
---|
[638] | 1766 | $sth->execute($rawid{$rawfree});
|
---|
[558] | 1767 | }
|
---|
[93] | 1768 |
|
---|
[558] | 1769 | # now we walk the new list of compacted blocks, and see which ones we need to insert
|
---|
[638] | 1770 | $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,parent_id,master_id) VALUES (?,?,?,?,?)");
|
---|
[558] | 1771 | foreach my $cme (@combinelist) {
|
---|
| 1772 | next if grep { $cme == $_ } @rawfb; # skip if the combined block was in the raw list
|
---|
[638] | 1773 | $sth->execute($cme, $pcity, $ptype, $p_id, $binfo->{master_id});
|
---|
[428] | 1774 | }
|
---|
[93] | 1775 |
|
---|
[428] | 1776 | } # done returning IPs to the appropriate place
|
---|
[404] | 1777 |
|
---|
[93] | 1778 | # If we got here, we've succeeded. Whew!
|
---|
| 1779 | $dbh->commit;
|
---|
| 1780 | }; # end eval
|
---|
| 1781 | if ($@) {
|
---|
[558] | 1782 | $msg .= ": $@";
|
---|
[93] | 1783 | eval { $dbh->rollback; };
|
---|
| 1784 | return ('FAIL', $msg);
|
---|
| 1785 | } else {
|
---|
[590] | 1786 | ##fixme: RPC return code?
|
---|
[638] | 1787 | _rpc('delByCIDR', cidr => "$cidr", rpcuser => $user, delforward => $delfwd, delsubs => 'y', parpatt => $ppatt);
|
---|
[558] | 1788 | return ($retcode, $goback);
|
---|
[93] | 1789 | }
|
---|
| 1790 |
|
---|
| 1791 | } # end alloctype != netblock
|
---|
| 1792 |
|
---|
| 1793 | } # end deleteBlock()
|
---|
| 1794 |
|
---|
| 1795 |
|
---|
[370] | 1796 | ## IPDB::getBlockData()
|
---|
[557] | 1797 | # Get CIDR or IP, custid, type, city, circuit ID, description, notes, modification time,
|
---|
| 1798 | # private/restricted data, for a CIDR block or pool IP
|
---|
| 1799 | # Also returns SWIP status flag for CIDR blocks or pool netblock for IPs
|
---|
[636] | 1800 | # Takes the block ID or IP to look up and an optional flag to indicate a pool IP lookup
|
---|
| 1801 | # instead of a netblock.
|
---|
[557] | 1802 | # Returns a hashref to the block data
|
---|
[370] | 1803 | sub getBlockData {
|
---|
| 1804 | my $dbh = shift;
|
---|
[636] | 1805 | my $id = shift;
|
---|
| 1806 | my $type = shift || 'b'; # default to netblock for lazy callers
|
---|
[370] | 1807 |
|
---|
[636] | 1808 | # netblocks are in the allocations table; pool IPs are in the poolips table.
|
---|
| 1809 | # If we try to look up a CIDR in an integer field we should just get back nothing.
|
---|
| 1810 | my ($btype) = $dbh->selectrow_array("SELECT type FROM allocations WHERE id=?", undef, ($id) );
|
---|
[534] | 1811 |
|
---|
[692] | 1812 | # Note city, vrf, parent_id and master_id removed due to JOIN uncertainty for block allocations
|
---|
| 1813 | my $commonfields = q(custid, type, circuitid, description, notes, modifystamp AS lastmod,
|
---|
| 1814 | privdata, vlan, rdns);
|
---|
| 1815 |
|
---|
[636] | 1816 | if ($type eq 'i') {
|
---|
[692] | 1817 | my $binfo = $dbh->selectrow_hashref(qq(
|
---|
| 1818 | SELECT ip AS block, city, vrf, parent_id, master_id, $commonfields
|
---|
| 1819 | FROM poolips WHERE id = ?
|
---|
| 1820 | ), undef, ($id) );
|
---|
[557] | 1821 | return $binfo;
|
---|
| 1822 | } else {
|
---|
[692] | 1823 | my $binfo = $dbh->selectrow_hashref(qq(
|
---|
| 1824 | SELECT a.cidr AS block, a.city, a.vrf, a.parent_id, a.master_id, swip, $commonfields,
|
---|
| 1825 | f.cidr AS reserve, f.id as reserve_id
|
---|
| 1826 | FROM allocations a LEFT JOIN freeblocks f ON a.id=f.reserve_for
|
---|
| 1827 | WHERE a.id = ?
|
---|
| 1828 | ), undef, ($id) );
|
---|
[557] | 1829 | return $binfo;
|
---|
[534] | 1830 | }
|
---|
[370] | 1831 | } # end getBlockData()
|
---|
| 1832 |
|
---|
| 1833 |
|
---|
[585] | 1834 | ## IPDB::getBlockRDNS()
|
---|
| 1835 | # Gets reverse DNS pattern for a block or IP. Note that this will also
|
---|
| 1836 | # retrieve any default pattern following the parent chain up, and check via
|
---|
| 1837 | # RPC (if available) to see what the narrowest pattern for the requested block is
|
---|
| 1838 | # Returns the current pattern for the block or IP.
|
---|
| 1839 | sub getBlockRDNS {
|
---|
| 1840 | my $dbh = shift;
|
---|
| 1841 | my %args = @_;
|
---|
| 1842 |
|
---|
[637] | 1843 | $args{type} = 'b' if !$args{type};
|
---|
[675] | 1844 | my $cached = 1;
|
---|
[585] | 1845 |
|
---|
[637] | 1846 | # snag entry from database
|
---|
| 1847 | my ($rdns,$rfrom,$pid);
|
---|
| 1848 | if ($args{type} =~ /.i/) {
|
---|
| 1849 | ($rdns, $rfrom, $pid) = $dbh->selectrow_array("SELECT rdns,ip,parent_id FROM poolips WHERE id = ?",
|
---|
| 1850 | undef, ($args{id}) );
|
---|
| 1851 | } else {
|
---|
| 1852 | ($rdns, $rfrom, $pid) = $dbh->selectrow_array("SELECT rdns,cidr,parent_id FROM allocations WHERE id = ?",
|
---|
| 1853 | undef, ($args{id}) );
|
---|
| 1854 | }
|
---|
[585] | 1855 |
|
---|
[637] | 1856 | # Can't see a way this could end up empty, for any case I care about. If the caller
|
---|
| 1857 | # doesn't know an allocation ID to request, then they don't know anything else anyway.
|
---|
| 1858 | my $selfblock = $rfrom;
|
---|
[585] | 1859 |
|
---|
[637] | 1860 | my $type;
|
---|
| 1861 | while (!$rdns && $pid) {
|
---|
| 1862 | ($rdns, $rfrom, $pid, $type) = $dbh->selectrow_array(
|
---|
| 1863 | "SELECT rdns,cidr,parent_id,type FROM allocations WHERE id = ?",
|
---|
| 1864 | undef, ($pid) );
|
---|
| 1865 | last if $type eq 'mm'; # break loops in unfortunate legacy data
|
---|
| 1866 | }
|
---|
| 1867 |
|
---|
| 1868 | # use the actual allocation to check against the DNS utility; we don't want
|
---|
| 1869 | # to always go chasing up the chain to the master... which may (usually won't)
|
---|
| 1870 | # be present directly in DNS anyway
|
---|
| 1871 | my $cidr = new NetAddr::IP $selfblock;
|
---|
| 1872 |
|
---|
[585] | 1873 | if ($rpc_url) {
|
---|
| 1874 | # Use the first /16 or /24, rather than dithering over which sub-/14 /16
|
---|
| 1875 | # or sub-/19 /24 to retrieve - it's the least-wrong way to do things.
|
---|
| 1876 |
|
---|
[586] | 1877 | my ($rpcblock) = ($cidr->masklen <= 24 ? $cidr->split( ($cidr->masklen <= 16 ? 16 : 24) ) : $cidr);
|
---|
[585] | 1878 | my %rpcargs = (
|
---|
| 1879 | rpcuser => $args{user},
|
---|
| 1880 | group => $revgroup, # not sure how this could sanely be exposed, tbh...
|
---|
| 1881 | cidr => "$rpcblock",
|
---|
| 1882 | );
|
---|
| 1883 |
|
---|
[637] | 1884 | my $remote_rdns = _rpc('getRevPattern', %rpcargs);
|
---|
| 1885 | $rdns = $remote_rdns if $remote_rdns;
|
---|
[675] | 1886 | $cached = 0;
|
---|
[585] | 1887 | }
|
---|
| 1888 |
|
---|
| 1889 | # hmm. do we care about where it actually came from?
|
---|
[675] | 1890 | return $rdns, $cached;
|
---|
[585] | 1891 | } # end getBlockRDNS()
|
---|
| 1892 |
|
---|
| 1893 |
|
---|
[675] | 1894 | ## IPDB::getRDNSbyIP()
|
---|
| 1895 | # Get individual reverse entries for the IP or CIDR IP range passed. Sort of looking the
|
---|
| 1896 | # opposite direction down the netblock tree compared to getBlockRDNS() above.
|
---|
| 1897 | sub getRDNSbyIP {
|
---|
| 1898 | my $dbh = shift;
|
---|
| 1899 | my %args = @_; # We want to accept a variety of call types
|
---|
| 1900 |
|
---|
| 1901 | # key arguments: allocation ID, type
|
---|
| 1902 | unless ($args{id} || $args{type}) {
|
---|
| 1903 | $errstr = 'Missing allocation ID or type';
|
---|
| 1904 | return;
|
---|
| 1905 | }
|
---|
| 1906 |
|
---|
| 1907 | my @ret = ();
|
---|
| 1908 | # special case: single IP. Check if it's an allocation or in a pool, then do the RPC call for fresh data.
|
---|
| 1909 | if ($args{type} =~ /^.i$/) {
|
---|
| 1910 | my ($ip, $localrev) = $dbh->selectrow_array("SELECT ip, rdns FROM poolips WHERE id = ?", undef, ($args{id}) );
|
---|
| 1911 | push @ret, { 'r_ip' => $ip, 'iphost' => $localrev };
|
---|
| 1912 | } else {
|
---|
| 1913 | if ($rpc_url) {
|
---|
| 1914 | my %rpcargs = (
|
---|
| 1915 | rpcuser => $args{user},
|
---|
| 1916 | group => $revgroup, # not sure how this could sanely be exposed, tbh...
|
---|
| 1917 | cidr => $args{range},
|
---|
| 1918 | );
|
---|
| 1919 |
|
---|
| 1920 | my $remote_rdns = _rpc('getRevSet', %rpcargs);
|
---|
| 1921 | return $remote_rdns;
|
---|
| 1922 | # $rdns = $remote_rdns if $remote_rdns;
|
---|
| 1923 | # $cached = 0;
|
---|
| 1924 | }
|
---|
| 1925 | }
|
---|
| 1926 | return \@ret;
|
---|
| 1927 | } # end getRDNSbyIP()
|
---|
| 1928 |
|
---|
| 1929 |
|
---|
[519] | 1930 | ## IPDB::getNodeList()
|
---|
| 1931 | # Gets a list of node ID+name pairs as an arrayref to a list of hashrefs
|
---|
| 1932 | sub getNodeList {
|
---|
| 1933 | my $dbh = shift;
|
---|
| 1934 |
|
---|
| 1935 | my $ret = $dbh->selectall_arrayref("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id",
|
---|
| 1936 | { Slice => {} });
|
---|
| 1937 | return $ret;
|
---|
| 1938 | } # end getNodeList()
|
---|
| 1939 |
|
---|
| 1940 |
|
---|
[530] | 1941 | ## IPDB::getNodeName()
|
---|
| 1942 | # Get node name from the ID
|
---|
| 1943 | sub getNodeName {
|
---|
| 1944 | my $dbh = shift;
|
---|
| 1945 | my $nid = shift;
|
---|
| 1946 |
|
---|
| 1947 | my ($nname) = $dbh->selectrow_array("SELECT node_name FROM nodes WHERE node_id = ?", undef, ($nid) );
|
---|
| 1948 | return $nname;
|
---|
| 1949 | } # end getNodeName()
|
---|
| 1950 |
|
---|
| 1951 |
|
---|
| 1952 | ## IPDB::getNodeInfo()
|
---|
| 1953 | # Get node name and ID associated with a block
|
---|
| 1954 | sub getNodeInfo {
|
---|
| 1955 | my $dbh = shift;
|
---|
| 1956 | my $block = shift;
|
---|
| 1957 |
|
---|
| 1958 | my ($nid, $nname) = $dbh->selectrow_array("SELECT nodes.node_id,node_name FROM nodes INNER JOIN noderef".
|
---|
| 1959 | " ON nodes.node_id=noderef.node_id WHERE noderef.block = ?", undef, ($block) );
|
---|
| 1960 | return ($nid, $nname);
|
---|
| 1961 | } # end getNodeInfo()
|
---|
| 1962 |
|
---|
| 1963 |
|
---|
[77] | 1964 | ## IPDB::mailNotify()
|
---|
[66] | 1965 | # Sends notification mail to recipients regarding an IPDB operation
|
---|
[416] | 1966 | sub mailNotify {
|
---|
| 1967 | my $dbh = shift;
|
---|
| 1968 | my ($action,$subj,$message) = @_;
|
---|
[66] | 1969 |
|
---|
[462] | 1970 | return if $smtphost eq 'smtp.example.com'; # do nothing if still using default SMTP host.
|
---|
| 1971 |
|
---|
[422] | 1972 | ##fixme: need to redesign the breakdown/processing for $action for proper handling of all cases
|
---|
| 1973 |
|
---|
[416] | 1974 | # split action into parts for fiddlement. nb: there are almost certainly better ways to do this.
|
---|
[422] | 1975 | my @actionbits = split //, $action;
|
---|
[416] | 1976 |
|
---|
| 1977 | # want to notify anyone who has specifically requested notify on *this* type ($action as passed),
|
---|
| 1978 | # on "all static IP types" or "all pool types" (and other last-char-in-type groupings), on eg "all DSL types",
|
---|
| 1979 | # and "all events with this action"
|
---|
| 1980 | my @actionsets = ($action);
|
---|
| 1981 | ##fixme: ick, eww. really gotta find a better way to handle this...
|
---|
| 1982 | push @actionsets, ($actionbits[0].'.'.$actionbits[2],
|
---|
| 1983 | $actionbits[0].$actionbits[1].'.', $actionbits[0].'a') if $action =~ /^.{3}$/;
|
---|
| 1984 |
|
---|
| 1985 | my $mailer = Net::SMTP->new($smtphost, Hello => "ipdb.$domain");
|
---|
| 1986 |
|
---|
| 1987 | # get recip list from db
|
---|
| 1988 | my $sth = $dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
|
---|
| 1989 |
|
---|
[443] | 1990 | my %reciplist;
|
---|
[416] | 1991 | foreach (@actionsets) {
|
---|
[426] | 1992 | $sth->execute($_);
|
---|
[416] | 1993 | ##fixme - need to handle db errors
|
---|
| 1994 | my ($recipsub) = $sth->fetchrow_array;
|
---|
| 1995 | next if !$recipsub;
|
---|
| 1996 | foreach (split(/,/, $recipsub)) {
|
---|
[443] | 1997 | $reciplist{$_}++;
|
---|
[416] | 1998 | }
|
---|
| 1999 | }
|
---|
| 2000 |
|
---|
[443] | 2001 | return if !%reciplist;
|
---|
[420] | 2002 |
|
---|
[443] | 2003 | foreach my $recip (keys %reciplist) {
|
---|
[681] | 2004 | $mailer->mail($smtpsender);
|
---|
[416] | 2005 | $mailer->to($recip);
|
---|
[681] | 2006 | $mailer->data("From: \"$org_name IP Database\" <$smtpsender>\n",
|
---|
[135] | 2007 | "To: $recip\n",
|
---|
[69] | 2008 | "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
|
---|
| 2009 | "Subject: {IPDB} $subj\n",
|
---|
| 2010 | "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n",
|
---|
[417] | 2011 | "Organization: $org_name\n",
|
---|
[69] | 2012 | "\n$message\n");
|
---|
[416] | 2013 | }
|
---|
[66] | 2014 | $mailer->quit;
|
---|
| 2015 | }
|
---|
| 2016 |
|
---|
[4] | 2017 | # Indicates module loaded OK. Required by Perl.
|
---|
| 2018 | 1;
|
---|