[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-06-11 21:34:46 +0000 (Thu, 11 Jun 2015) $
|
---|
| 6 | # SVN revision $Rev: 744 $
|
---|
| 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
|
---|
[733] | 27 | %IPDBacl %merge_display %aclmsg %rpcacl $maxfcgi
|
---|
[660] | 28 | $errstr
|
---|
[523] | 29 | &initIPDBGlobals &connectDB &finish &checkDBSanity
|
---|
[547] | 30 | &addMaster &touchMaster
|
---|
[720] | 31 | &listSummary &listSubs &listContainers &listAllocations &listForMerge &listFree &listPool
|
---|
[541] | 32 | &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
|
---|
[697] | 33 | &ipParent &subParent &blockParent &getBreadCrumbs &getRoutedCity
|
---|
[728] | 34 | &allocateBlock &updateBlock &splitBlock &shrinkBlock &mergeBlocks &deleteBlock &getBlockData
|
---|
[705] | 35 | &getBlockRDNS &getRDNSbyIP
|
---|
[530] | 36 | &getNodeList &getNodeName &getNodeInfo
|
---|
[519] | 37 | &mailNotify
|
---|
[106] | 38 | );
|
---|
[4] | 39 |
|
---|
| 40 | @EXPORT = (); # Export nothing by default.
|
---|
[106] | 41 | %EXPORT_TAGS = ( ALL => [qw(
|
---|
[167] | 42 | %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist
|
---|
[733] | 43 | %IPDBacl %merge_display %aclmsg %rpcacl $maxfcgi
|
---|
[660] | 44 | $errstr
|
---|
[523] | 45 | &initIPDBGlobals &connectDB &finish &checkDBSanity
|
---|
[547] | 46 | &addMaster &touchMaster
|
---|
[720] | 47 | &listSummary &listSubs &listContainers &listAllocations &listForMerge &listFree &listPool
|
---|
[541] | 48 | &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
|
---|
[697] | 49 | &ipParent &subParent &blockParent &getBreadCrumbs &getRoutedCity
|
---|
[728] | 50 | &allocateBlock &updateBlock &splitBlock &shrinkBlock &mergeBlocks &deleteBlock &getBlockData
|
---|
[705] | 51 | &getBlockRDNS &getRDNSbyIP
|
---|
[530] | 52 | &getNodeList &getNodeName &getNodeInfo
|
---|
[519] | 53 | &mailNotify
|
---|
[106] | 54 | )]
|
---|
| 55 | );
|
---|
[4] | 56 |
|
---|
[77] | 57 | ##
|
---|
| 58 | ## Global variables
|
---|
| 59 | ##
|
---|
| 60 | our %disp_alloctypes;
|
---|
| 61 | our %list_alloctypes;
|
---|
[167] | 62 | our %def_custids;
|
---|
[96] | 63 | our @citylist;
|
---|
| 64 | our @poplist;
|
---|
[233] | 65 | our %IPDBacl;
|
---|
[66] | 66 |
|
---|
[725] | 67 | # Mapping hash for pooltype -> poolip-as-netblock conversions
|
---|
| 68 | my %poolmap = (sd => 'en', cd => 'cn', dp => 'cn', mp => 'cn', wp => 'cn', ld => 'in', ad => 'in', bd => 'in');
|
---|
| 69 |
|
---|
[733] | 70 | # Friendly display strings for merge scopes
|
---|
| 71 | our %merge_display = (
|
---|
| 72 | keepall => "Keep mergeable allocations as suballocations of new block",
|
---|
| 73 | mergepeer => "Keep suballocations of mergeable allocations",
|
---|
| 74 | clearpeer => "Keep only suballocations of the selected block",
|
---|
| 75 | clearall => "Clear all suballocations"
|
---|
| 76 | );
|
---|
| 77 |
|
---|
[517] | 78 | # mapping table for functional-area => error message
|
---|
| 79 | our %aclmsg = (
|
---|
| 80 | addmaster => 'add a master block',
|
---|
| 81 | addblock => 'add an allocation',
|
---|
| 82 | updateblock => 'update a block',
|
---|
| 83 | delblock => 'delete an allocation',
|
---|
| 84 | );
|
---|
| 85 |
|
---|
[660] | 86 | our %rpcacl;
|
---|
[661] | 87 | our $maxfcgi = 3;
|
---|
[660] | 88 |
|
---|
[585] | 89 | # error reporting
|
---|
| 90 | our $errstr = '';
|
---|
| 91 |
|
---|
[417] | 92 | our $org_name = 'Example Corp';
|
---|
[416] | 93 | our $smtphost = 'smtp.example.com';
|
---|
| 94 | our $domain = 'example.com';
|
---|
[417] | 95 | our $defcustid = '5554242';
|
---|
[681] | 96 | our $smtpsender = 'ipdb@example.com';
|
---|
[417] | 97 | # mostly for rwhois
|
---|
| 98 | ##fixme: leave these blank by default?
|
---|
[420] | 99 | our $rwhoisDataPath = '/usr/local/rwhoisd/etc/rwhoisd'; # to match ./configure defaults from rwhoisd-1.5.9.6
|
---|
[417] | 100 | our $org_street = '123 4th Street';
|
---|
| 101 | our $org_city = 'Anytown';
|
---|
| 102 | our $org_prov_state = 'ON';
|
---|
| 103 | our $org_pocode = 'H0H 0H0';
|
---|
| 104 | our $org_country = 'CA';
|
---|
| 105 | our $org_phone = '000-555-1234';
|
---|
| 106 | our $org_techhandle = 'ISP-ARIN-HANDLE';
|
---|
[434] | 107 | our $org_email = 'noc@example.com';
|
---|
[437] | 108 | our $hostmaster = 'dns@example.com';
|
---|
[416] | 109 |
|
---|
[417] | 110 | our $syslog_facility = 'local2';
|
---|
| 111 |
|
---|
[582] | 112 | our $rpc_url = '';
|
---|
[585] | 113 | our $revgroup = 1; # should probably be configurable somewhere
|
---|
| 114 | our $rpccount = 0;
|
---|
[582] | 115 |
|
---|
[674] | 116 | # Largest inverse CIDR mask length to show per-IP rDNS list
|
---|
| 117 | # (eg, NetAddr::IP->bits - NetAddr::IP->masklen)
|
---|
| 118 | our $maxrevlist = 5; # /27
|
---|
| 119 |
|
---|
[682] | 120 | # UI layout for subblocks/containers
|
---|
| 121 | our $sublistlayout = 1;
|
---|
| 122 |
|
---|
[691] | 123 | # VLAN validation mode. Set to 0 to allow alphanumeric vlan names instead of using the vlan number.
|
---|
| 124 | our $numeric_vlan = 1;
|
---|
| 125 |
|
---|
| 126 |
|
---|
[585] | 127 | ##
|
---|
| 128 | ## Internal utility functions
|
---|
| 129 | ##
|
---|
| 130 |
|
---|
| 131 | ## IPDB::_rpc
|
---|
| 132 | # Make an RPC call for DNS changes
|
---|
| 133 | sub _rpc {
|
---|
| 134 | return if !$rpc_url; # Just In Case
|
---|
| 135 | my $rpcsub = shift;
|
---|
| 136 | my %args = @_;
|
---|
| 137 |
|
---|
| 138 | # Make an object to represent the XML-RPC server.
|
---|
| 139 | my $server = Frontier::Client->new(url => $rpc_url, debug => 0);
|
---|
| 140 | my $result;
|
---|
| 141 |
|
---|
| 142 | my %rpcargs = (
|
---|
| 143 | rpcsystem => 'ipdb',
|
---|
[640] | 144 | # must be provided by caller's caller
|
---|
| 145 | # rpcuser => $args{user},
|
---|
| 146 | %args,
|
---|
[585] | 147 | );
|
---|
| 148 |
|
---|
| 149 | eval {
|
---|
[640] | 150 | $result = $server->call("dnsdb.$rpcsub", %rpcargs);
|
---|
[585] | 151 | };
|
---|
| 152 | if ($@) {
|
---|
| 153 | $errstr = $@;
|
---|
[678] | 154 | $errstr =~ s/\s*$//;
|
---|
[585] | 155 | $errstr =~ s/Fault returned from XML RPC Server, fault code 4: error executing RPC `dnsdb.$rpcsub'\.\s//;
|
---|
| 156 | }
|
---|
| 157 | $rpccount++;
|
---|
| 158 |
|
---|
| 159 | return $result if $result;
|
---|
| 160 | } # end _rpc()
|
---|
| 161 |
|
---|
| 162 |
|
---|
[715] | 163 | ## IPDB::_compactFree()
|
---|
| 164 | # Utility sub to compact a set of free block entries down to the minimum possible set of CIDR entries
|
---|
| 165 | # Not to be called outside of an eval{}!
|
---|
| 166 | sub _compactFree {
|
---|
| 167 | my $dbh = shift;
|
---|
| 168 | my $parent = shift;
|
---|
| 169 |
|
---|
| 170 | # Rather than having the caller provide all the details
|
---|
| 171 | my $pinfo = getBlockData($dbh, $parent);
|
---|
[722] | 172 | my $ftype = (split //, $pinfo->{type})[0];
|
---|
[715] | 173 |
|
---|
| 174 | # NetAddr::IP->compact() attempts to produce the smallest inclusive block
|
---|
| 175 | # from the caller and the passed terms.
|
---|
| 176 | # EG: if you call $cidr->compact($ip1,$ip2,$ip3) when $cidr, $ip1, $ip2,
|
---|
| 177 | # and $ip3 are consecutive /27's starting on .0 (.0-.31, .32-.63,
|
---|
| 178 | # .64-.95, and .96-.128), you will get an array containing a single
|
---|
| 179 | # /25 as element 0 (.0-.127). Order is not important; you could have
|
---|
| 180 | # $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
|
---|
| 181 |
|
---|
| 182 | ##fixme: vrf
|
---|
| 183 | ##fixme: simplify since all containers now represent different "layers"/"levels"?
|
---|
| 184 |
|
---|
| 185 | # set up the query to get the list of blocks to try to merge.
|
---|
| 186 | my $sth = $dbh->prepare(q{
|
---|
| 187 | SELECT cidr,id FROM freeblocks
|
---|
| 188 | WHERE parent_id = ?
|
---|
| 189 | ORDER BY masklen(cidr) DESC
|
---|
| 190 | });
|
---|
| 191 | $sth->execute($parent);
|
---|
| 192 |
|
---|
| 193 | my (@rawfb, @combinelist, %rawid);
|
---|
| 194 | my $i=0;
|
---|
| 195 | # for each free block under $parent, push a NetAddr::IP object into one list, and
|
---|
| 196 | # continuously use NetAddr::IP->compact to automagically merge netblocks as possible.
|
---|
| 197 | while (my ($fcidr, $fid) = $sth->fetchrow_array) {
|
---|
| 198 | my $testIP = new NetAddr::IP $fcidr;
|
---|
| 199 | push @rawfb, $testIP;
|
---|
| 200 | $rawid{"$testIP"} = $fid; # $data[0] vs "$testIP" *does* make a difference for v6
|
---|
| 201 | @combinelist = $testIP->compact(@combinelist);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | # now that we have the full list of "compacted" freeblocks, go back over
|
---|
| 205 | # the list of raw freeblocks, and delete the ones that got merged.
|
---|
| 206 | $sth = $dbh->prepare("DELETE FROM freeblocks WHERE id = ?");
|
---|
| 207 | foreach my $rawfree (@rawfb) {
|
---|
| 208 | next if grep { $rawfree == $_ } @combinelist; # skip if the raw block is in the compacted list
|
---|
| 209 | $sth->execute($rawid{$rawfree});
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | # now we walk the new list of compacted blocks, and see which ones we need to insert
|
---|
[723] | 213 | $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id) VALUES (?,?,?,?,?,?)");
|
---|
[715] | 214 | foreach my $cme (@combinelist) {
|
---|
| 215 | next if grep { $cme == $_ } @rawfb; # skip if the combined block was in the raw list
|
---|
[722] | 216 | $sth->execute($cme, $pinfo->{city}, $ftype, $pinfo->{vrf}, $parent, $pinfo->{master_id});
|
---|
[715] | 217 | }
|
---|
| 218 |
|
---|
| 219 | } # end _compactFree()
|
---|
| 220 |
|
---|
| 221 |
|
---|
[736] | 222 | ## IPDB::_toPool()
|
---|
| 223 | # Convert an allocation or allocation tree to entries in an IP pool
|
---|
| 224 | # Assumes an incomplete/empty pool
|
---|
| 225 | # Takes a parent ID for the pool, CIDR range descriptor for the allocation(s) to convert, and the pool type
|
---|
| 226 | sub _toPool {
|
---|
| 227 | my $dbh = shift;
|
---|
| 228 | my $poolparent = shift;
|
---|
| 229 | my $convblock = shift; # May be smaller than the block referenced by $poolparent
|
---|
| 230 | my $pooltype = shift;
|
---|
| 231 |
|
---|
| 232 | # there is probably a way to avoid the temporary $foo here
|
---|
| 233 | my $foo = $dbh->selectall_arrayref("SELECT master_id,parent_id FROM allocations WHERE id = ?", undef, $poolparent);
|
---|
| 234 | my ($master,$mainparent) = @{$foo->[0]};
|
---|
| 235 |
|
---|
| 236 | my @retlist;
|
---|
| 237 |
|
---|
| 238 | my $iptype = $pooltype;
|
---|
| 239 | $iptype =~ s/[pd]$/i/;
|
---|
| 240 | my $poolclass = (split //, $iptype)[0];
|
---|
| 241 |
|
---|
| 242 | my $cidrpool = new NetAddr::IP $convblock;
|
---|
| 243 |
|
---|
| 244 | my $asth = $dbh->prepare(q{
|
---|
| 245 | SELECT id, cidr, type, parent_id, city, description, notes, circuitid,
|
---|
| 246 | createstamp, modifystamp, privdata, custid, vrf, vlan, rdns
|
---|
| 247 | FROM allocations
|
---|
| 248 | WHERE cidr <<= ? AND master_id = ?
|
---|
| 249 | ORDER BY masklen(cidr) DESC
|
---|
| 250 | });
|
---|
| 251 | my $inssth = $dbh->prepare(q{
|
---|
| 252 | INSERT INTO poolips (
|
---|
| 253 | ip,type,parent_id,available,
|
---|
| 254 | city,description,notes,circuitid,createstamp,modifystamp,privdata,custid,vrf,vlan,rdns
|
---|
| 255 | )
|
---|
| 256 | VALUES (?,?,?,'n',?,?,?,?,?,?,?,?,?,?,?)
|
---|
| 257 | });
|
---|
| 258 | my $updsth = $dbh->prepare("UPDATE poolips SET parent_id = ?, type = ? WHERE parent_id = ?");
|
---|
| 259 | my $delsth = $dbh->prepare("DELETE FROM allocations WHERE id = ?");
|
---|
| 260 | my $fbdelsth = $dbh->prepare("DELETE FROM freeblocks WHERE parent_id = ?");
|
---|
| 261 |
|
---|
| 262 | $asth->execute($convblock, $master);
|
---|
| 263 | my %poolcounter;
|
---|
| 264 | while (my ($oldid, $oldcidr, $oldtype, $oldparent, @oldalloc) = $asth->fetchrow_array) {
|
---|
| 265 | if ($oldtype =~ /.[enr]/) {
|
---|
| 266 | # Convert leaf allocations to block of pool IP assignments
|
---|
| 267 | my $tmpcidr = new NetAddr::IP $oldcidr;
|
---|
| 268 | my $newtype = $poolclass.'i';
|
---|
| 269 | # set up the gateway IP in case we need it
|
---|
| 270 | my $gw = $cidrpool+1;
|
---|
| 271 | foreach my $newip ($tmpcidr->split(32)) {
|
---|
| 272 | my $baseip = $newip->addr;
|
---|
| 273 | # skip .0 and .255, they are prefectly legitimate but some systems behave
|
---|
| 274 | # poorly talking to a client using them.
|
---|
| 275 | next if $baseip =~ /\.(?:0|255)$/;
|
---|
| 276 | # skip the network, broadcast, and gateway IPs if we're creating a "normal netblock" pool
|
---|
| 277 | if ($pooltype =~ /d$/) {
|
---|
| 278 | next if $newip->addr eq $cidrpool->network->addr;
|
---|
| 279 | next if $newip->addr eq $cidrpool->broadcast->addr;
|
---|
| 280 | next if $newip->addr eq $gw->addr;
|
---|
| 281 | }
|
---|
| 282 | $inssth->execute($newip, $newtype, $poolparent, @oldalloc) if !$poolcounter{"$newip"};
|
---|
| 283 | $poolcounter{"$newip"}++;
|
---|
| 284 | }
|
---|
| 285 | } elsif ($oldalloc[2] =~ /.[dp]/) {
|
---|
| 286 | # Reparent IPs in an existing pool, and rewrite their type
|
---|
| 287 | $updsth->execute($poolparent, $poolclass.'i', $oldid);
|
---|
| 288 | } else {
|
---|
| 289 | # Containers are mostly "not interesting" in this context since they're
|
---|
| 290 | # equivalent to the pool allocation on .[dp] types. Clean up the lingering free block(s).
|
---|
| 291 | $fbdelsth->execute($oldid);
|
---|
| 292 | }
|
---|
| 293 | # Clean up - remove the converted block unless it is the "primary"
|
---|
| 294 | $delsth->execute($oldid) unless $oldid == $poolparent;
|
---|
| 295 | # Return the converted blocks, but only the immediate peers, not the entire tree
|
---|
| 296 | push @retlist, { block => $oldcidr, mtype => $disp_alloctypes{$oldtype} }
|
---|
| 297 | if $oldparent == $mainparent;
|
---|
| 298 | }
|
---|
| 299 | return \@retlist;
|
---|
| 300 | } # end _toPool()
|
---|
| 301 |
|
---|
| 302 |
|
---|
[725] | 303 | ## IPDB::_poolToAllocations
|
---|
| 304 | # Convert pool IPs into allocations, and free IPs into free blocks
|
---|
| 305 | # Takes a pool ID, original pool CIDR (in case the allocation has been updated before the call here)
|
---|
| 306 | # and hashref to data for the new parent container for the IPs,
|
---|
| 307 | # and an optional hash with the new parent ID and allocation type
|
---|
| 308 | sub _poolToAllocations {
|
---|
| 309 | my $dbh = shift;
|
---|
| 310 | my $oldpool = shift;
|
---|
| 311 | my $parentinfo = shift;
|
---|
| 312 | my %args = @_;
|
---|
| 313 |
|
---|
| 314 | # Default to converting the pool to a container
|
---|
| 315 | $args{newparent} = $oldpool->{id} if !$args{newparent};
|
---|
| 316 |
|
---|
| 317 | my ($containerclass) = ($parentinfo->{type} =~ /(.)./);
|
---|
| 318 |
|
---|
| 319 | # Default type mapping
|
---|
| 320 | $args{newtype} = $poolmap{$oldpool->{type}} if !$args{newtype};
|
---|
| 321 |
|
---|
| 322 | # Convert a bunch of pool IP allocations into "normal" netblock allocations
|
---|
| 323 | my $pool2alloc = $dbh->prepare(q{
|
---|
| 324 | INSERT INTO allocations (
|
---|
| 325 | cidr,type,city, description, notes, circuitid, createstamp, modifystamp,
|
---|
| 326 | privdata, custid, vrf, vlan, rdns, parent_id, master_id
|
---|
| 327 | )
|
---|
| 328 | SELECT
|
---|
| 329 | ip, ? AS type, city, description, notes, circuitid, createstamp, modifystamp,
|
---|
| 330 | privdata, custid, vrf, vlan, rdns, ? AS parent_id, master_id
|
---|
| 331 | FROM poolips
|
---|
| 332 | WHERE parent_id = ? AND available = 'n'
|
---|
| 333 | });
|
---|
| 334 | $pool2alloc->execute($args{newtype}, $args{newparent}, $oldpool->{id});
|
---|
| 335 |
|
---|
| 336 | # Snag the whole list of pool IPs
|
---|
| 337 | my @freeips = @{$dbh->selectall_arrayref("SELECT ip,available FROM poolips WHERE parent_id = ?",
|
---|
| 338 | undef, $oldpool->{id})};
|
---|
| 339 | my @iplist;
|
---|
| 340 | my %usedips;
|
---|
| 341 | # Filter out the ones that were used...
|
---|
| 342 | foreach my $ip (@freeips) {
|
---|
| 343 | $$ip[0] =~ s{/32$}{};
|
---|
| 344 | push @iplist, NetAddr::IP->new($$ip[0]) if $$ip[1] eq 'y';
|
---|
| 345 | $usedips{$$ip[0]}++ if $$ip[1] eq 'n';
|
---|
| 346 | }
|
---|
| 347 | # ... so that we can properly decide whether the net, gw, and bcast IPs need to be added to the free list.
|
---|
| 348 | my $tmpblock = new NetAddr::IP $oldpool->{block};
|
---|
| 349 | push @iplist, NetAddr::IP->new($tmpblock->network->addr)
|
---|
| 350 | if !$usedips{$tmpblock->network->addr} || $tmpblock->network->addr =~ /\.0$/;
|
---|
| 351 | push @iplist, NetAddr::IP->new($tmpblock->broadcast->addr)
|
---|
| 352 | if !$usedips{$tmpblock->broadcast->addr} || $tmpblock->broadcast->addr =~ /\.255$/;
|
---|
| 353 | # only "DHCP"-ish pools have a gw ip removed from the pool
|
---|
| 354 | if ($oldpool->{type} =~ /.d/) {
|
---|
| 355 | $tmpblock++;
|
---|
| 356 | push @iplist, NetAddr::IP->new($tmpblock->addr);
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | # take the list of /32 IPs, and see what CIDR ranges we get back as free, then insert them.
|
---|
| 360 | @iplist = Compact(@iplist);
|
---|
| 361 | my $insfbsth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id) VALUES (?,?,?,?,?,?)");
|
---|
| 362 | foreach (@iplist) {
|
---|
| 363 | $insfbsth->execute($_, $parentinfo->{city}, $containerclass, $parentinfo->{vrf},
|
---|
| 364 | $args{newparent}, $parentinfo->{master_id});
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | # and finally delete the poolips entries
|
---|
| 368 | $dbh->do("DELETE FROM poolips WHERE parent_id = ?", undef, $oldpool->{id});
|
---|
| 369 |
|
---|
| 370 | } # end _poolToAllocations()
|
---|
| 371 |
|
---|
| 372 |
|
---|
[716] | 373 | ## IPDB::_deleteCascade()
|
---|
| 374 | # Internal sub. Deletes an allocation and all subcomponents
|
---|
| 375 | sub _deleteCascade {
|
---|
| 376 | my $dbh = shift;
|
---|
| 377 | my $id = shift;
|
---|
[724] | 378 | my $createfb = shift; # may be null at this point
|
---|
[716] | 379 |
|
---|
| 380 | my $binfo = getBlockData($dbh, $id);
|
---|
| 381 |
|
---|
[724] | 382 | # Decide if we're going to add a free block.
|
---|
| 383 |
|
---|
| 384 | # Caller is normal block delete -> add freeblock under $binfo->{parent_id} -> pass nothing
|
---|
| 385 | # Caller is delete for merge to leaf -> do not add freeblock -> pass 0
|
---|
| 386 | # Caller is normal master delete -> do not add freeblock -> pass nothing
|
---|
| 387 | # Caller is merge master -> add freeblock under alternate parent -> pass parent ID
|
---|
| 388 | if ($binfo->{type} ne 'mm') {
|
---|
| 389 | # Deleting a non-master block
|
---|
| 390 | if (!defined($createfb)) {
|
---|
| 391 | # No createfb flag passed; assuming normal block delete. Add the freeblock
|
---|
| 392 | # under the parent of the block we're deleting.
|
---|
| 393 | $createfb = $binfo->{parent_id};
|
---|
| 394 | #} else {
|
---|
| 395 | # Don't need to actually do anything here. The caller has given us an ID,
|
---|
| 396 | # which is either 0 (causing no free block) or (theoretically) a valid block
|
---|
| 397 | # ID to add the free block under.
|
---|
| 398 | }
|
---|
| 399 | #} else {
|
---|
| 400 | # Deleting a master block
|
---|
| 401 | # Don't need to actually do anything here. If the caller passed a parent ID,
|
---|
| 402 | # that parent will get the new free block. if the caller didn't pass anything,
|
---|
| 403 | # no free block will be added.
|
---|
| 404 | }
|
---|
| 405 |
|
---|
[716] | 406 | ##fixme: special-case master blocks up here and quickly delete based on master_id,
|
---|
| 407 | # instead of wasting time tracing parent relations
|
---|
| 408 |
|
---|
| 409 | # grab all allocations in the master within the CIDR of the block to be deleted
|
---|
| 410 | my %parents;
|
---|
| 411 | my %cidrlist;
|
---|
| 412 | ##fixme: limit by VRF?
|
---|
| 413 | my $sth = $dbh->prepare("SELECT cidr,id,parent_id FROM allocations WHERE cidr <<= ? AND master_id = ?");
|
---|
| 414 | $sth->execute($binfo->{block}, $binfo->{master_id});
|
---|
| 415 | while (my ($cidr, $cid, $pid) = $sth->fetchrow_array) {
|
---|
| 416 | $parents{$cid} = $pid;
|
---|
| 417 | $cidrlist{$cid} = $cidr;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | # Trace the parent relations up the tree until we either hit parent ID 0 (we've found a master block
|
---|
| 421 | # but not the parent we're looking for - arguably this is already an error) or the parent ID matches
|
---|
| 422 | # the passed ID. If the latter, push the whole set into a second flag hash, so we can terminate
|
---|
| 423 | # further tree-tracing early.
|
---|
| 424 | my %found;
|
---|
| 425 | foreach my $cid (keys %parents) {
|
---|
| 426 | my @tmp;
|
---|
| 427 | if ($cid == $id) {
|
---|
| 428 | # "child" is the ID we've been asked to cascade-delete.
|
---|
| 429 | $found{$cid}++;
|
---|
| 430 | } elsif ($found{$cid}) {
|
---|
| 431 | # ID already seen and the chain terminates in our parent.
|
---|
| 432 | } elsif ($parents{$cid} == $id) {
|
---|
| 433 | # Immediate parent is the target parent
|
---|
| 434 | $found{$cid}++;
|
---|
| 435 | } else {
|
---|
| 436 | # Immediate parent isn't the one we're looking for. Walk the chain up until we hit our parent,
|
---|
| 437 | # the nonexistent parent id 0, or undefined (ID is not a child of the target ID at all)
|
---|
| 438 | # There are probably better ways to structure this loop.
|
---|
| 439 | while (1) {
|
---|
| 440 | # cache the ID
|
---|
| 441 | push @tmp, $cid;
|
---|
| 442 | # some very particularly defined loop ending conditions
|
---|
| 443 | if (!defined($parents{$cid}) || $parents{$cid} == $id || $parents{$cid} == 0) {
|
---|
| 444 | last;
|
---|
| 445 | } else {
|
---|
| 446 | # if we haven't found either the desired parent or another limiting condition,
|
---|
| 447 | # reset the ID to the parent next up the tree
|
---|
| 448 | $cid = $parents{$cid};
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
| 451 | # if the current chain of relations ended with our target parent, shuffle the cached IDs into a flag hash
|
---|
| 452 | if (defined($parents{$cid}) && $parents{$cid} == $id) {
|
---|
| 453 | foreach (@tmp) { $found{$_}++; }
|
---|
| 454 | }
|
---|
| 455 | } # else
|
---|
| 456 | } # foreach my $cid
|
---|
| 457 |
|
---|
| 458 | # Use the keys in the flag hash to determine which allocations to actually delete.
|
---|
| 459 | # Delete matching freeblocks and pool IPs; their parents are going away so we want
|
---|
| 460 | # to make sure we don't leave orphaned records lying around loose.
|
---|
| 461 | my @dellist = keys %found;
|
---|
| 462 | push @dellist, $id; # Just In Case the target ID didn't make the list earlier.
|
---|
| 463 | my $b = '?'. (',?' x $#dellist);
|
---|
| 464 | $dbh->do("DELETE FROM allocations WHERE id IN ($b)", undef, (@dellist) );
|
---|
| 465 | $dbh->do("DELETE FROM freeblocks WHERE parent_id IN ($b)", undef, (@dellist) );
|
---|
| 466 | $dbh->do("DELETE FROM poolips WHERE parent_id IN ($b)", undef, (@dellist) );
|
---|
| 467 |
|
---|
[724] | 468 | # Insert a new free block if needed
|
---|
| 469 | if ($createfb) {
|
---|
| 470 | my $pinfo = getBlockData($dbh, $createfb);
|
---|
[716] | 471 | my $pt = (split //, $pinfo->{type})[1];
|
---|
| 472 | $dbh->do("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id) VALUES (?,?,?,?,?,?)", undef,
|
---|
[724] | 473 | $binfo->{block}, $pinfo->{city}, $pt, $createfb, $pinfo->{vrf}, $binfo->{master_id});
|
---|
[716] | 474 | }
|
---|
| 475 |
|
---|
| 476 | ##todo: and hey! bonus! we can return @dellist, or something (%cidrlist{@dellist})
|
---|
[724] | 477 |
|
---|
[716] | 478 | } # end _deleteCascade()
|
---|
| 479 |
|
---|
| 480 |
|
---|
[715] | 481 | ##
|
---|
| 482 | ## Public subs
|
---|
| 483 | ##
|
---|
| 484 |
|
---|
| 485 |
|
---|
[77] | 486 | ## IPDB::initIPDBGlobals()
|
---|
| 487 | # Initialize all globals. Takes a database handle, returns a success or error code
|
---|
| 488 | sub initIPDBGlobals {
|
---|
| 489 | my $dbh = $_[0];
|
---|
| 490 | my $sth;
|
---|
| 491 |
|
---|
[106] | 492 | # Initialize alloctypes hashes
|
---|
[167] | 493 | $sth = $dbh->prepare("select type,listname,dispname,listorder,def_custid from alloctypes order by listorder");
|
---|
[77] | 494 | $sth->execute;
|
---|
| 495 | while (my @data = $sth->fetchrow_array) {
|
---|
[106] | 496 | $disp_alloctypes{$data[0]} = $data[2];
|
---|
[167] | 497 | $def_custids{$data[0]} = $data[4];
|
---|
[106] | 498 | if ($data[3] < 900) {
|
---|
| 499 | $list_alloctypes{$data[0]} = $data[1];
|
---|
| 500 | }
|
---|
[77] | 501 | }
|
---|
[96] | 502 |
|
---|
| 503 | # City and POP listings
|
---|
[157] | 504 | $sth = $dbh->prepare("select city,routing from cities order by city");
|
---|
[96] | 505 | $sth->execute;
|
---|
| 506 | return (undef,$sth->errstr) if $sth->err;
|
---|
| 507 | while (my @data = $sth->fetchrow_array) {
|
---|
[106] | 508 | push @citylist, $data[0];
|
---|
[96] | 509 | if ($data[1] eq 'y') {
|
---|
[106] | 510 | push @poplist, $data[0];
|
---|
[96] | 511 | }
|
---|
| 512 | }
|
---|
| 513 |
|
---|
[233] | 514 | # Load ACL data. Specific username checks are done at a different level.
|
---|
| 515 | $sth = $dbh->prepare("select username,acl from users");
|
---|
| 516 | $sth->execute;
|
---|
[106] | 517 | return (undef,$sth->errstr) if $sth->err;
|
---|
[233] | 518 | while (my @data = $sth->fetchrow_array) {
|
---|
| 519 | $IPDBacl{$data[0]} = $data[1];
|
---|
| 520 | }
|
---|
[106] | 521 |
|
---|
[517] | 522 | ##fixme: initialize HTML::Template env var for template path
|
---|
| 523 | # something like $self->path().'/templates' ?
|
---|
| 524 | # $ENV{HTML_TEMPLATE_ROOT} = 'foo/bar';
|
---|
| 525 |
|
---|
[77] | 526 | return (1,"OK");
|
---|
| 527 | } # end initIPDBGlobals
|
---|
| 528 |
|
---|
| 529 |
|
---|
| 530 | ## IPDB::connectDB()
|
---|
[4] | 531 | # Creates connection to IPDB.
|
---|
[77] | 532 | # Requires the database name, username, and password.
|
---|
[4] | 533 | # Returns a handle to the db.
|
---|
[77] | 534 | # Set up for a PostgreSQL db; could be any transactional DBMS with the
|
---|
| 535 | # right changes.
|
---|
[4] | 536 | sub connectDB {
|
---|
[432] | 537 | my $dbname = shift;
|
---|
| 538 | my $user = shift;
|
---|
| 539 | my $pass = shift;
|
---|
| 540 | my $dbhost = shift;
|
---|
| 541 |
|
---|
[4] | 542 | my $dbh;
|
---|
[432] | 543 | my $DSN = "DBI:Pg:".($dbhost ? "host=$dbhost;" : '')."dbname=$dbname";
|
---|
[4] | 544 |
|
---|
| 545 | # Note that we want to autocommit by default, and we will turn it off locally as necessary.
|
---|
[77] | 546 | # We may not want to print gobbledygook errors; YMMV. Have to ponder that further.
|
---|
| 547 | $dbh = DBI->connect($DSN, $user, $pass, {
|
---|
| 548 | AutoCommit => 1,
|
---|
| 549 | PrintError => 0
|
---|
| 550 | })
|
---|
| 551 | or return (undef, $DBI::errstr) if(!$dbh);
|
---|
[4] | 552 |
|
---|
[77] | 553 | # Return here if we can't select. Note that this indicates a
|
---|
| 554 | # problem executing the select.
|
---|
[183] | 555 | my $sth = $dbh->prepare("select type from alloctypes");
|
---|
[77] | 556 | $sth->execute();
|
---|
| 557 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 558 |
|
---|
| 559 | # See if the select returned anything (or null data). This should
|
---|
| 560 | # succeed if the select executed, but...
|
---|
| 561 | $sth->fetchrow();
|
---|
| 562 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 563 |
|
---|
| 564 | # If we get here, we should be OK.
|
---|
| 565 | return ($dbh,"DB connection OK");
|
---|
[4] | 566 | } # end connectDB
|
---|
| 567 |
|
---|
[77] | 568 |
|
---|
| 569 | ## IPDB::finish()
|
---|
| 570 | # Cleans up after database handles and so on.
|
---|
| 571 | # Requires a database handle
|
---|
| 572 | sub finish {
|
---|
| 573 | my $dbh = $_[0];
|
---|
[517] | 574 | $dbh->disconnect if $dbh;
|
---|
[77] | 575 | } # end finish
|
---|
| 576 |
|
---|
| 577 |
|
---|
[106] | 578 | ## IPDB::checkDBSanity()
|
---|
[4] | 579 | # Quick check to see if the db is responding. A full integrity
|
---|
| 580 | # check will have to be a separate tool to walk the IP allocation trees.
|
---|
| 581 | sub checkDBSanity {
|
---|
[106] | 582 | my ($dbh) = $_[0];
|
---|
[4] | 583 |
|
---|
| 584 | if (!$dbh) {
|
---|
[106] | 585 | print "No database handle, or connection has been closed.";
|
---|
| 586 | return -1;
|
---|
[4] | 587 | } else {
|
---|
| 588 | # it connects, try a stmt.
|
---|
[184] | 589 | my $sth = $dbh->prepare("select type from alloctypes");
|
---|
[4] | 590 | my $err = $sth->execute();
|
---|
| 591 |
|
---|
| 592 | if ($sth->fetchrow()) {
|
---|
| 593 | # all is well.
|
---|
| 594 | return 1;
|
---|
| 595 | } else {
|
---|
[16] | 596 | print "Connected to the database, but could not execute test statement. ".$sth->errstr();
|
---|
[106] | 597 | return -1;
|
---|
[4] | 598 | }
|
---|
| 599 | }
|
---|
| 600 | # Clean up after ourselves.
|
---|
[106] | 601 | # $dbh->disconnect;
|
---|
[4] | 602 | } # end checkDBSanity
|
---|
| 603 |
|
---|
[66] | 604 |
|
---|
[371] | 605 | ## IPDB::addMaster()
|
---|
| 606 | # Does all the magic necessary to sucessfully add a master block
|
---|
| 607 | # Requires database handle, block to add
|
---|
| 608 | # Returns failure code and error message or success code and "message"
|
---|
| 609 | sub addMaster {
|
---|
| 610 | my $dbh = shift;
|
---|
[591] | 611 | # warning! during testing, this somehow generated a "Bad file descriptor" error. O_o
|
---|
[371] | 612 | my $cidr = new NetAddr::IP shift;
|
---|
[582] | 613 | my %args = @_;
|
---|
[371] | 614 |
|
---|
[582] | 615 | $args{vrf} = '' if !$args{vrf};
|
---|
| 616 | $args{rdns} = '' if !$args{rdns};
|
---|
| 617 | $args{defloc} = '' if !$args{defloc};
|
---|
| 618 | $args{rwhois} = 'n' if !$args{rwhois}; # fail "safe", sort of.
|
---|
| 619 | $args{rwhois} = 'n' if $args{rwhois} ne 'n' and $args{rwhois} ne 'y';
|
---|
| 620 |
|
---|
[629] | 621 | my $mid;
|
---|
| 622 |
|
---|
[371] | 623 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 624 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 625 | local $dbh->{AutoCommit} = 0;
|
---|
| 626 | local $dbh->{RaiseError} = 1;
|
---|
| 627 |
|
---|
| 628 | # Wrap all the SQL in a transaction
|
---|
| 629 | eval {
|
---|
[628] | 630 | # First check - does the master exist? Ignore VRFs until we can see a sane UI
|
---|
| 631 | my ($mcontained) = $dbh->selectrow_array("SELECT cidr FROM allocations WHERE cidr >>= ? AND type = 'mm'",
|
---|
| 632 | undef, ($cidr) );
|
---|
| 633 | die "Master block $mcontained already exists and entirely contains $cidr\n"
|
---|
| 634 | if $mcontained;
|
---|
[371] | 635 |
|
---|
[628] | 636 | # Second check - does the new master contain an existing one or ones?
|
---|
| 637 | my ($mexist) = $dbh->selectrow_array("SELECT cidr FROM allocations WHERE cidr <<= ? AND type = 'mm'",
|
---|
| 638 | undef, ($cidr) );
|
---|
| 639 |
|
---|
[518] | 640 | if (!$mexist) {
|
---|
[371] | 641 | # First case - master is brand-spanking-new.
|
---|
| 642 | ##fixme: rwhois should be globally-flagable somewhere, much like a number of other things
|
---|
| 643 | ## maybe a db table called "config"?
|
---|
[628] | 644 | $dbh->do("INSERT INTO allocations (cidr,type,swip,vrf,rdns) VALUES (?,?,?,?,?)", undef,
|
---|
| 645 | ($cidr, 'mm', 'y', $args{vrf}, $args{rdns}) );
|
---|
| 646 | ($mid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
|
---|
[371] | 647 |
|
---|
| 648 | # Unrouted blocks aren't associated with a city (yet). We don't rely on this
|
---|
| 649 | # elsewhere though; legacy data may have traps and pitfalls in it to break this.
|
---|
| 650 | # Thus the "routed" flag.
|
---|
[628] | 651 | $dbh->do("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id) VALUES (?,?,?,?,?,?)", undef,
|
---|
| 652 | ($cidr, '<NULL>', 'm', $mid, $args{vrf}, $mid) );
|
---|
[371] | 653 |
|
---|
[628] | 654 | # master should be its own master, so deletes directly at the master level work
|
---|
| 655 | $dbh->do("UPDATE allocations SET master_id = ? WHERE id = ?", undef, ($mid, $mid) );
|
---|
| 656 |
|
---|
[371] | 657 | # If we get here, everything is happy. Commit changes.
|
---|
| 658 | $dbh->commit;
|
---|
| 659 |
|
---|
[518] | 660 | } # done new master does not contain existing master(s)
|
---|
[371] | 661 | else {
|
---|
| 662 |
|
---|
| 663 | # collect the master(s) we're going to absorb, and snag the longest netmask while we're at it.
|
---|
| 664 | my $smallmask = $cidr->masklen;
|
---|
[628] | 665 | my $sth = $dbh->prepare("SELECT cidr,id FROM allocations WHERE cidr <<= ? AND type='mm' AND parent_id=0");
|
---|
[518] | 666 | $sth->execute($cidr);
|
---|
[371] | 667 | my @cmasters;
|
---|
[628] | 668 | my @oldmids;
|
---|
[371] | 669 | while (my @data = $sth->fetchrow_array) {
|
---|
| 670 | my $master = new NetAddr::IP $data[0];
|
---|
| 671 | push @cmasters, $master;
|
---|
[628] | 672 | push @oldmids, $data[1];
|
---|
[371] | 673 | $smallmask = $master->masklen if $master->masklen > $smallmask;
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 | # split the new master, and keep only those blocks not part of an existing master
|
---|
| 677 | my @blocklist;
|
---|
| 678 | foreach my $seg ($cidr->split($smallmask)) {
|
---|
| 679 | my $contained = 0;
|
---|
| 680 | foreach my $master (@cmasters) {
|
---|
| 681 | $contained = 1 if $master->contains($seg);
|
---|
| 682 | }
|
---|
| 683 | push @blocklist, $seg if !$contained;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
[628] | 686 | ##fixme: master_id
|
---|
[371] | 687 | # collect the unrouted free blocks within the new master
|
---|
[556] | 688 | $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE masklen(cidr) <= ? AND cidr <<= ? AND routed = 'm'");
|
---|
[518] | 689 | $sth->execute($smallmask, $cidr);
|
---|
[371] | 690 | while (my @data = $sth->fetchrow_array) {
|
---|
| 691 | my $freeblock = new NetAddr::IP $data[0];
|
---|
| 692 | push @blocklist, $freeblock;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 | # combine the set of free blocks we should have now.
|
---|
| 696 | @blocklist = Compact(@blocklist);
|
---|
| 697 |
|
---|
[628] | 698 | # master
|
---|
| 699 | $dbh->do("INSERT INTO allocations (cidr,type,swip,vrf,rdns) VALUES (?,?,?,?,?)", undef,
|
---|
| 700 | ($cidr, 'mm', 'y', $args{vrf}, $args{rdns}) );
|
---|
| 701 | ($mid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
|
---|
| 702 |
|
---|
| 703 | # master should be its own master, so deletes directly at the master level work
|
---|
| 704 | $dbh->do("UPDATE allocations SET master_id = ? WHERE id = ?", undef, ($mid, $mid) );
|
---|
| 705 |
|
---|
[371] | 706 | # and now insert the new data. Make sure to delete old masters too.
|
---|
| 707 |
|
---|
| 708 | # freeblocks
|
---|
[628] | 709 | $sth = $dbh->prepare("DELETE FROM freeblocks WHERE cidr <<= ? AND parent_id IN (".join(',', @oldmids).")");
|
---|
| 710 | my $sth2 = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id)".
|
---|
| 711 | " VALUES (?,'<NULL>','m',?,?,?)");
|
---|
[371] | 712 | foreach my $newblock (@blocklist) {
|
---|
[518] | 713 | $sth->execute($newblock);
|
---|
[628] | 714 | $sth2->execute($newblock, $mid, $args{vrf}, $mid);
|
---|
[371] | 715 | }
|
---|
| 716 |
|
---|
[628] | 717 | # Update immediate allocations, and remove the old parents
|
---|
| 718 | $sth = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE parent_id = ?");
|
---|
| 719 | $sth2 = $dbh->prepare("DELETE FROM allocations WHERE id = ?");
|
---|
| 720 | foreach my $old (@oldmids) {
|
---|
| 721 | $sth->execute($mid, $old);
|
---|
| 722 | $sth2->execute($old);
|
---|
| 723 | }
|
---|
[556] | 724 |
|
---|
[371] | 725 | # *whew* If we got here, we likely suceeded.
|
---|
| 726 | $dbh->commit;
|
---|
[628] | 727 |
|
---|
[371] | 728 | } # new master contained existing master(s)
|
---|
| 729 | }; # end eval
|
---|
| 730 |
|
---|
| 731 | if ($@) {
|
---|
| 732 | my $msg = $@;
|
---|
| 733 | eval { $dbh->rollback; };
|
---|
| 734 | return ('FAIL',$msg);
|
---|
| 735 | } else {
|
---|
[582] | 736 |
|
---|
| 737 | # Only attempt rDNS if the IPDB side succeeded
|
---|
| 738 | if ($rpc_url) {
|
---|
| 739 |
|
---|
| 740 | # Note *not* splitting reverse zones negates any benefit from caching the exported data.
|
---|
| 741 | # IPv6 address space is far too large to split usefully, and in any case (also due to
|
---|
| 742 | # the large address space) doesn't support the iterated template records v4 zones do
|
---|
| 743 | # that causes the bulk of the slowdown that needs the cache anyway.
|
---|
| 744 |
|
---|
| 745 | my @zonelist;
|
---|
| 746 | # allow splitting reverse zones to be disabled, maybe, someday
|
---|
| 747 | #if ($splitrevzones && !$cidr->{isv6}) {
|
---|
| 748 | if (1 && !$cidr->{isv6}) {
|
---|
| 749 | my $splitpoint = ($cidr->masklen <= 16 ? 16 : 24); # hack pthui
|
---|
| 750 | @zonelist = $cidr->split($splitpoint);
|
---|
| 751 | } else {
|
---|
| 752 | @zonelist = ($cidr);
|
---|
| 753 | }
|
---|
| 754 | my @fails;
|
---|
| 755 | ##fixme: remove hardcoding where possible
|
---|
| 756 | foreach my $subzone (@zonelist) {
|
---|
| 757 | my %rpcargs = (
|
---|
| 758 | rpcuser => $args{user},
|
---|
| 759 | revzone => "$subzone",
|
---|
| 760 | revpatt => $args{rdns},
|
---|
| 761 | defloc => $args{defloc},
|
---|
[585] | 762 | group => $revgroup, # not sure how these two could sanely be exposed, tbh...
|
---|
[582] | 763 | state => 1, # could make them globally configurable maybe
|
---|
| 764 | );
|
---|
[587] | 765 | if ($rpc_url && !_rpc('addRDNS', %rpcargs)) {
|
---|
[585] | 766 | push @fails, ("$subzone" => $errstr);
|
---|
[582] | 767 | }
|
---|
| 768 | }
|
---|
| 769 | if (@fails) {
|
---|
[628] | 770 | $errstr = "Warning(s) adding $cidr to reverse DNS:\n".join("\n", @fails);
|
---|
| 771 | return ('WARN',$mid);
|
---|
[582] | 772 | }
|
---|
| 773 | }
|
---|
[628] | 774 | return ('OK',$mid);
|
---|
[371] | 775 | }
|
---|
| 776 | } # end addMaster
|
---|
| 777 |
|
---|
| 778 |
|
---|
[547] | 779 | ## IPDB::touchMaster()
|
---|
| 780 | # Update last-changed timestamp on a master block.
|
---|
| 781 | sub touchMaster {
|
---|
| 782 | my $dbh = shift;
|
---|
| 783 | my $master = shift;
|
---|
| 784 |
|
---|
| 785 | local $dbh->{AutoCommit} = 0;
|
---|
| 786 | local $dbh->{RaiseError} = 1;
|
---|
| 787 |
|
---|
| 788 | eval {
|
---|
[652] | 789 | $dbh->do("UPDATE allocations SET modifystamp=now() WHERE id = ?", undef, ($master));
|
---|
[547] | 790 | $dbh->commit;
|
---|
| 791 | };
|
---|
| 792 |
|
---|
| 793 | if ($@) {
|
---|
| 794 | my $msg = $@;
|
---|
| 795 | eval { $dbh->rollback; };
|
---|
| 796 | return ('FAIL',$msg);
|
---|
| 797 | }
|
---|
| 798 | return ('OK','OK');
|
---|
| 799 | } # end touchMaster()
|
---|
| 800 |
|
---|
| 801 |
|
---|
[523] | 802 | ## IPDB::listSummary()
|
---|
| 803 | # Get summary list of all master blocks
|
---|
| 804 | # Returns an arrayref to a list of hashrefs containing the master block, routed count,
|
---|
| 805 | # allocated count, free count, and largest free block masklength
|
---|
| 806 | sub listSummary {
|
---|
| 807 | my $dbh = shift;
|
---|
| 808 |
|
---|
[625] | 809 | my $mlist = $dbh->selectall_arrayref("SELECT cidr AS master,id,vrf FROM allocations ".
|
---|
| 810 | "WHERE type='mm' ORDER BY cidr",
|
---|
| 811 | { Slice => {} });
|
---|
[523] | 812 |
|
---|
| 813 | foreach (@{$mlist}) {
|
---|
[625] | 814 | my ($rcnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? AND type='rm' AND master_id = ?",
|
---|
| 815 | undef, ($$_{master}, $$_{id}));
|
---|
[523] | 816 | $$_{routed} = $rcnt;
|
---|
[625] | 817 | my ($acnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
|
---|
| 818 | "AND NOT type='rm' AND NOT type='mm' AND master_id = ?",
|
---|
| 819 | undef, ($$_{master}, $$_{id}));
|
---|
[523] | 820 | $$_{allocated} = $acnt;
|
---|
[625] | 821 | my ($fcnt) = $dbh->selectrow_array("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?",
|
---|
| 822 | undef, ($$_{master}, $$_{id}));
|
---|
[523] | 823 | $$_{free} = $fcnt;
|
---|
[560] | 824 | my ($bigfree) = $dbh->selectrow_array("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
|
---|
[625] | 825 | " AND master_id = ? ORDER BY masklen(cidr) LIMIT 1", undef, ($$_{master}, $$_{id}));
|
---|
[523] | 826 | ##fixme: should find a way to do this without having to HTMLize the <>
|
---|
| 827 | $bigfree = "/$bigfree" if $bigfree;
|
---|
[525] | 828 | $bigfree = '<NONE>' if !$bigfree;
|
---|
[523] | 829 | $$_{bigfree} = $bigfree;
|
---|
| 830 | }
|
---|
| 831 | return $mlist;
|
---|
| 832 | } # end listSummary()
|
---|
| 833 |
|
---|
| 834 |
|
---|
[561] | 835 | ## IPDB::listSubs()
|
---|
| 836 | # Get list of subnets within a specified CIDR block, on a specified VRF.
|
---|
| 837 | # Returns an arrayref to a list of hashrefs containing the CIDR block, customer location or
|
---|
| 838 | # city it's routed to, block type, SWIP status, and description
|
---|
| 839 | sub listSubs {
|
---|
| 840 | my $dbh = shift;
|
---|
| 841 | my %args = @_;
|
---|
| 842 |
|
---|
| 843 | # Just In Case
|
---|
| 844 | $args{vrf} = '' if !$args{vrf};
|
---|
| 845 |
|
---|
| 846 | # Snag the allocations for this block
|
---|
[691] | 847 | my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,vrf,id,master_id".
|
---|
[627] | 848 | " FROM allocations WHERE parent_id = ? ORDER BY cidr");
|
---|
| 849 | $sth->execute($args{parent});
|
---|
[561] | 850 |
|
---|
| 851 | # hack hack hack
|
---|
| 852 | # set up to flag swip=y records if they don't actually have supporting data in the customers table
|
---|
| 853 | my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?");
|
---|
| 854 |
|
---|
[653] | 855 | # snag some more details
|
---|
[664] | 856 | my $substh = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
|
---|
[663] | 857 | "AND type ~ '[mc]\$' AND master_id = ? AND NOT cidr = ? ");
|
---|
[653] | 858 | my $alsth = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
|
---|
[712] | 859 | "AND NOT type='rm' AND NOT type='mm' AND master_id = ? AND NOT id = ?");
|
---|
[653] | 860 | my $freesth = $dbh->prepare("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?");
|
---|
| 861 | my $lfreesth = $dbh->prepare("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
|
---|
| 862 | " AND master_id = ? ORDER BY masklen(cidr) LIMIT 1");
|
---|
| 863 |
|
---|
[561] | 864 | my @blocklist;
|
---|
[691] | 865 | while (my ($cidr,$city,$type,$custid,$swip,$desc,$vrf,$id,$mid) = $sth->fetchrow_array()) {
|
---|
| 866 | $desc .= " - vrf:$vrf" if $desc && $vrf;
|
---|
| 867 | $desc = "vrf:$vrf" if !$desc && $vrf;
|
---|
[561] | 868 | $custsth->execute($custid);
|
---|
| 869 | my ($ncust) = $custsth->fetchrow_array();
|
---|
[653] | 870 | $substh->execute($cidr, $mid, $cidr);
|
---|
| 871 | my ($cont) = $substh->fetchrow_array();
|
---|
[712] | 872 | $alsth->execute($cidr, $mid, $id);
|
---|
[653] | 873 | my ($alloc) = $alsth->fetchrow_array();
|
---|
| 874 | $freesth->execute($cidr, $mid);
|
---|
| 875 | my ($free) = $freesth->fetchrow_array();
|
---|
| 876 | $lfreesth->execute($cidr, $mid);
|
---|
| 877 | my ($lfree) = $lfreesth->fetchrow_array();
|
---|
| 878 | $lfree = "/$lfree" if $lfree;
|
---|
| 879 | $lfree = '<NONE>' if !$lfree;
|
---|
[561] | 880 | my %row = (
|
---|
| 881 | block => $cidr,
|
---|
[653] | 882 | subcontainers => $cont,
|
---|
| 883 | suballocs => $alloc,
|
---|
| 884 | subfree => $free,
|
---|
| 885 | lfree => $lfree,
|
---|
[561] | 886 | city => $city,
|
---|
| 887 | type => $disp_alloctypes{$type},
|
---|
| 888 | custid => $custid,
|
---|
| 889 | swip => ($swip eq 'y' ? 'Yes' : 'No'),
|
---|
| 890 | partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0),
|
---|
| 891 | desc => $desc,
|
---|
| 892 | hassubs => ($type eq 'rm' || $type =~ /.c/ ? 1 : 0),
|
---|
[627] | 893 | id => $id,
|
---|
[561] | 894 | );
|
---|
| 895 | # $row{subblock} = ($type =~ /^.r$/); # hmf. wonder why these won't work in the hash declaration...
|
---|
| 896 | $row{listpool} = ($type =~ /^.[pd]$/);
|
---|
| 897 | push (@blocklist, \%row);
|
---|
| 898 | }
|
---|
| 899 | return \@blocklist;
|
---|
| 900 | } # end listSubs()
|
---|
| 901 |
|
---|
| 902 |
|
---|
[663] | 903 | ## IPDB::listContainers()
|
---|
| 904 | # List all container-type allocations in a given parent
|
---|
| 905 | # Takes a database handle and a hash:
|
---|
| 906 | # - parent is the ID of the parent block
|
---|
| 907 | # Returns an arrayref to a list of hashrefs with the CIDR block, location, type,
|
---|
| 908 | # description, block ID, and counts for the nmber uf suballocations (all types),
|
---|
| 909 | # free blocks, and the CIDR size of the largest free block
|
---|
| 910 | sub listContainers {
|
---|
| 911 | my $dbh = shift;
|
---|
| 912 | my %args = @_;
|
---|
| 913 |
|
---|
| 914 | # Just In Case
|
---|
| 915 | $args{vrf} = '' if !$args{vrf};
|
---|
| 916 |
|
---|
| 917 | # Snag the allocations for this block
|
---|
[691] | 918 | my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,vrf,id,master_id".
|
---|
[663] | 919 | " FROM allocations WHERE parent_id = ? AND type ~ '[mc]\$' ORDER BY cidr");
|
---|
| 920 | $sth->execute($args{parent});
|
---|
| 921 |
|
---|
| 922 | my $alsth = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
|
---|
[712] | 923 | "AND NOT type='rm' AND NOT type='mm' AND master_id = ? AND NOT id = ?");
|
---|
[663] | 924 | my $freesth = $dbh->prepare("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?");
|
---|
| 925 | my $lfreesth = $dbh->prepare("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
|
---|
| 926 | " AND master_id = ? ORDER BY masklen(cidr) LIMIT 1");
|
---|
| 927 |
|
---|
| 928 | my @blocklist;
|
---|
[691] | 929 | while (my ($cidr,$city,$type,$custid,$swip,$desc,$vrf,$id,$mid) = $sth->fetchrow_array()) {
|
---|
| 930 | $desc .= " - vrf:$vrf" if $desc && $vrf;
|
---|
| 931 | $desc = "vrf:$vrf" if !$desc && $vrf;
|
---|
[712] | 932 | $alsth->execute($cidr, $mid, $id);
|
---|
[663] | 933 | my ($alloc) = $alsth->fetchrow_array();
|
---|
| 934 | $freesth->execute($cidr, $mid);
|
---|
| 935 | my ($free) = $freesth->fetchrow_array();
|
---|
| 936 | $lfreesth->execute($cidr, $mid);
|
---|
| 937 | my ($lfree) = $lfreesth->fetchrow_array();
|
---|
| 938 | $lfree = "/$lfree" if $lfree;
|
---|
| 939 | $lfree = '<NONE>' if !$lfree;
|
---|
| 940 | my %row = (
|
---|
| 941 | block => $cidr,
|
---|
| 942 | suballocs => $alloc,
|
---|
| 943 | subfree => $free,
|
---|
| 944 | lfree => $lfree,
|
---|
| 945 | city => $city,
|
---|
| 946 | type => $disp_alloctypes{$type},
|
---|
| 947 | desc => $desc,
|
---|
| 948 | id => $id,
|
---|
| 949 | );
|
---|
| 950 | push (@blocklist, \%row);
|
---|
| 951 | }
|
---|
| 952 | return \@blocklist;
|
---|
| 953 | } # end listContainers()
|
---|
| 954 |
|
---|
| 955 |
|
---|
| 956 | ## IPDB::listAllocations()
|
---|
| 957 | # List all end-use allocations in a given parent
|
---|
| 958 | # Takes a database handle and a hash:
|
---|
| 959 | # - parent is the ID of the parent block
|
---|
| 960 | # Returns an arrayref to a list of hashrefs with the CIDR block, location, type,
|
---|
| 961 | # custID, SWIP flag, description, block ID, and master ID
|
---|
| 962 | sub listAllocations {
|
---|
| 963 | my $dbh = shift;
|
---|
| 964 | my %args = @_;
|
---|
| 965 |
|
---|
| 966 | # Snag the allocations for this block
|
---|
[691] | 967 | my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,vrf,id,master_id".
|
---|
[663] | 968 | " FROM allocations WHERE parent_id = ? AND type !~ '[mc]\$' ORDER BY cidr");
|
---|
| 969 | $sth->execute($args{parent});
|
---|
| 970 |
|
---|
| 971 | # hack hack hack
|
---|
| 972 | # set up to flag swip=y records if they don't actually have supporting data in the customers table
|
---|
| 973 | my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?");
|
---|
| 974 |
|
---|
| 975 | my @blocklist;
|
---|
[691] | 976 | while (my ($cidr,$city,$type,$custid,$swip,$desc,$vrf,$id,$mid) = $sth->fetchrow_array()) {
|
---|
| 977 | $desc .= " - vrf:$vrf" if $desc && $vrf;
|
---|
| 978 | $desc = "vrf:$vrf" if !$desc && $vrf;
|
---|
[663] | 979 | $custsth->execute($custid);
|
---|
| 980 | my ($ncust) = $custsth->fetchrow_array();
|
---|
| 981 | my %row = (
|
---|
| 982 | block => $cidr,
|
---|
| 983 | city => $city,
|
---|
| 984 | type => $disp_alloctypes{$type},
|
---|
| 985 | custid => $custid,
|
---|
| 986 | swip => ($swip eq 'y' ? 'Yes' : 'No'),
|
---|
| 987 | partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0),
|
---|
| 988 | desc => $desc,
|
---|
| 989 | id => $id,
|
---|
| 990 | );
|
---|
| 991 | # $row{subblock} = ($type =~ /^.r$/); # hmf. wonder why these won't work in the hash declaration...
|
---|
| 992 | $row{listpool} = ($type =~ /^.[pd]$/);
|
---|
| 993 | push (@blocklist, \%row);
|
---|
| 994 | }
|
---|
| 995 | return \@blocklist;
|
---|
| 996 | } # end listAllocations()
|
---|
| 997 |
|
---|
| 998 |
|
---|
[720] | 999 | ## IPDB::listForMerge()
|
---|
| 1000 | # Get a list of blocks targetted in a proposed merge
|
---|
| 1001 | sub listForMerge {
|
---|
| 1002 | my $dbh = shift;
|
---|
| 1003 | my $parent = shift;
|
---|
| 1004 | my $newblock = shift;
|
---|
| 1005 | my $btype = shift || 'a';
|
---|
| 1006 | $btype = 'a' if $btype !~/^[af]$/;
|
---|
[733] | 1007 | my $incsub = shift;
|
---|
| 1008 | $incsub = 1 if !defined($incsub);
|
---|
[720] | 1009 |
|
---|
| 1010 | my $sql;
|
---|
| 1011 | if ($btype eq 'a') {
|
---|
| 1012 | my $ret = $dbh->selectall_arrayref(q(
|
---|
| 1013 | SELECT a.cidr,a.id,t.dispname FROM allocations a
|
---|
| 1014 | JOIN alloctypes t ON a.type=t.type
|
---|
| 1015 | WHERE a.parent_id = ? AND a.cidr <<= ?
|
---|
| 1016 | ORDER BY a.cidr
|
---|
| 1017 | ),
|
---|
| 1018 | { Slice => {} }, $parent, $newblock);
|
---|
| 1019 | return $ret;
|
---|
| 1020 | } else {
|
---|
| 1021 | ##fixme: Not sure about the casting hackery in "SELECT ?::integer AS id", but it works as intended
|
---|
[733] | 1022 | my @dbargs = ($parent, "$newblock");
|
---|
| 1023 | push @dbargs, $parent, $newblock if $incsub;
|
---|
| 1024 | my $ret = $dbh->selectall_arrayref(q{
|
---|
| 1025 | SELECT cidr,id FROM freeblocks
|
---|
| 1026 | WHERE parent_id IN (
|
---|
| 1027 | }.($incsub ? "SELECT id FROM allocations WHERE parent_id = ? AND cidr <<= ? UNION " : '').q{
|
---|
| 1028 | SELECT ?::integer AS id
|
---|
| 1029 | ) AND cidr <<= ?
|
---|
| 1030 | ORDER BY cidr
|
---|
| 1031 | },
|
---|
| 1032 | { Slice => {} }, @dbargs);
|
---|
[720] | 1033 | return $ret;
|
---|
| 1034 | }
|
---|
[733] | 1035 | return;
|
---|
[720] | 1036 | } # end listForMerge()
|
---|
| 1037 |
|
---|
| 1038 |
|
---|
[524] | 1039 | ## IPDB::listFree()
|
---|
[562] | 1040 | # Gets a list of free blocks in the requested parent/master and VRF instance in both CIDR and range notation
|
---|
[630] | 1041 | # Takes a parent/master ID and an optional VRF specifier that defaults to empty.
|
---|
[524] | 1042 | # Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks
|
---|
[527] | 1043 | # Returns some extra flags in the hashrefs for routed blocks, since those can have several subtypes
|
---|
[524] | 1044 | sub listFree {
|
---|
| 1045 | my $dbh = shift;
|
---|
| 1046 |
|
---|
[562] | 1047 | my %args = @_;
|
---|
| 1048 | # Just In Case
|
---|
| 1049 | $args{vrf} = '' if !$args{vrf};
|
---|
| 1050 |
|
---|
[692] | 1051 | my $sth = $dbh->prepare(q(
|
---|
| 1052 | SELECT f.cidr,f.id,allocations.cidr
|
---|
| 1053 | FROM freeblocks f
|
---|
| 1054 | LEFT JOIN allocations ON f.reserve_for = allocations.id
|
---|
| 1055 | WHERE f.parent_id = ?
|
---|
| 1056 | ORDER BY f.cidr
|
---|
| 1057 | ) );
|
---|
[630] | 1058 | # $sth->execute($args{parent}, $args{vrf});
|
---|
| 1059 | $sth->execute($args{parent});
|
---|
[524] | 1060 | my @flist;
|
---|
[692] | 1061 | while (my ($cidr,$id,$resv) = $sth->fetchrow_array()) {
|
---|
[524] | 1062 | $cidr = new NetAddr::IP $cidr;
|
---|
[527] | 1063 | my %row = (
|
---|
| 1064 | fblock => "$cidr",
|
---|
| 1065 | frange => $cidr->range,
|
---|
[630] | 1066 | fbid => $id,
|
---|
| 1067 | fbparent => $args{parent},
|
---|
[692] | 1068 | resv => $resv,
|
---|
[527] | 1069 | );
|
---|
[524] | 1070 | push @flist, \%row;
|
---|
| 1071 | }
|
---|
| 1072 | return \@flist;
|
---|
[527] | 1073 | } # end listFree()
|
---|
[524] | 1074 |
|
---|
| 1075 |
|
---|
[528] | 1076 | ## IPDB::listPool()
|
---|
[740] | 1077 | # List the IPs in an IP pool.
|
---|
| 1078 | # Takes a pool/parent ID
|
---|
| 1079 | # Returns
|
---|
| 1080 | # Returns an arrayref to a list of hashrefs containing the IP, customer ID, availability flag,
|
---|
| 1081 | # description, backreference to the pool/parent, and the IP ID in the pool.
|
---|
| 1082 | # Also includes a "may be deleted" metaflag mainly useful for allowing the return to be passed
|
---|
| 1083 | # directly to HTML::Template for UI display.
|
---|
[528] | 1084 | sub listPool {
|
---|
| 1085 | my $dbh = shift;
|
---|
| 1086 | my $pool = shift;
|
---|
| 1087 |
|
---|
[630] | 1088 | my $sth = $dbh->prepare("SELECT ip,custid,available,description,type,id".
|
---|
| 1089 | " FROM poolips WHERE parent_id = ? ORDER BY ip");
|
---|
[528] | 1090 | $sth->execute($pool);
|
---|
| 1091 | my @poolips;
|
---|
[630] | 1092 | while (my ($ip,$custid,$available,$desc,$type,$id) = $sth->fetchrow_array) {
|
---|
[528] | 1093 | my %row = (
|
---|
| 1094 | ip => $ip,
|
---|
| 1095 | custid => $custid,
|
---|
| 1096 | available => $available,
|
---|
| 1097 | desc => $desc,
|
---|
[563] | 1098 | delme => $available eq 'n',
|
---|
[630] | 1099 | parent => $pool,
|
---|
| 1100 | id => $id,
|
---|
[528] | 1101 | );
|
---|
| 1102 | push @poolips, \%row;
|
---|
| 1103 | }
|
---|
| 1104 | return \@poolips;
|
---|
| 1105 | } # end listPool()
|
---|
| 1106 |
|
---|
| 1107 |
|
---|
[541] | 1108 | ## IPDB::getMasterList()
|
---|
| 1109 | # Get a list of master blocks, optionally including last-modified timestamps
|
---|
| 1110 | # Takes an optional flag to indicate whether to include timestamps;
|
---|
| 1111 | # 'm' includes ctime, all others (suggest 'c') do not.
|
---|
| 1112 | # Returns an arrayref to a list of hashrefs
|
---|
| 1113 | sub getMasterList {
|
---|
| 1114 | my $dbh = shift;
|
---|
| 1115 | my $stampme = shift || 'm'; # optional but should be set by caller for clarity
|
---|
| 1116 |
|
---|
[632] | 1117 | my $mlist = $dbh->selectall_arrayref("SELECT id,vrf,cidr AS master".($stampme eq 'm' ? ',modifystamp AS mtime' : '').
|
---|
| 1118 | " FROM allocations WHERE type='mm' ORDER BY cidr", { Slice => {} });
|
---|
[541] | 1119 | return $mlist;
|
---|
| 1120 | } # end getMasterList()
|
---|
| 1121 |
|
---|
| 1122 |
|
---|
[529] | 1123 | ## IPDB::getTypeList()
|
---|
| 1124 | # Get an alloctype/description pair list suitable for dropdowns
|
---|
| 1125 | # Takes a flag to determine which general groups of types are returned
|
---|
| 1126 | # Returns an reference to an array of hashrefs
|
---|
| 1127 | sub getTypeList {
|
---|
| 1128 | my $dbh = shift;
|
---|
| 1129 | my $tgroup = shift || 'a'; # technically optional, like this, but should
|
---|
| 1130 | # really be specified in the call for clarity
|
---|
[714] | 1131 | my $seltype = shift || '';
|
---|
| 1132 |
|
---|
| 1133 | my $sql = "SELECT type,listname,type=? AS sel FROM alloctypes WHERE listorder <= 500";
|
---|
[564] | 1134 | if ($tgroup eq 'n') {
|
---|
| 1135 | # grouping 'p' - all netblock types. These include routed blocks, containers (_c)
|
---|
| 1136 | # and contained (_r) types, dynamic-allocation ranges (_e), static IP pools (_d and _p),
|
---|
| 1137 | # and the "miscellaneous" cn, in, and en types.
|
---|
[714] | 1138 | $sql .= " AND type NOT LIKE '_i'";
|
---|
[564] | 1139 | } elsif ($tgroup eq 'p') {
|
---|
| 1140 | # grouping 'p' - primary allocation types. As with 'n' above but without the _r contained types.
|
---|
[714] | 1141 | $sql .= " AND type NOT LIKE '_i' AND type NOT LIKE '_r'";
|
---|
[529] | 1142 | } elsif ($tgroup eq 'c') {
|
---|
| 1143 | # grouping 'c' - contained types. These include all static IPs and all _r types.
|
---|
[714] | 1144 | $sql .= " AND (type LIKE '_i' OR type LIKE '_r')";
|
---|
[632] | 1145 | } elsif ($tgroup eq 'i') {
|
---|
| 1146 | # grouping 'i' - static IP types.
|
---|
[714] | 1147 | $sql .= " AND type LIKE '_i'";
|
---|
[529] | 1148 | } else {
|
---|
| 1149 | # grouping 'a' - all standard allocation types. This includes everything
|
---|
| 1150 | # but mm (present only as a formality). Make this the default.
|
---|
[714] | 1151 | # ... whee! no extra WHERE clauses
|
---|
[529] | 1152 | }
|
---|
[714] | 1153 | $sql .= " ORDER BY listorder";
|
---|
| 1154 | my $tlist = $dbh->selectall_arrayref($sql, { Slice => {} }, $seltype);
|
---|
[529] | 1155 | return $tlist;
|
---|
| 1156 | }
|
---|
| 1157 |
|
---|
| 1158 |
|
---|
[532] | 1159 | ## IPDB::getPoolSelect()
|
---|
| 1160 | # Get a list of pools matching the passed city and type that have 1 or more free IPs
|
---|
[740] | 1161 | # Returns an arrayref to a list of hashrefs containing the number of available IPs, the CIDR pool,
|
---|
| 1162 | # and the city it's nominally in.
|
---|
[532] | 1163 | sub getPoolSelect {
|
---|
| 1164 | my $dbh = shift;
|
---|
| 1165 | my $iptype = shift;
|
---|
| 1166 | my $pcity = shift;
|
---|
| 1167 |
|
---|
| 1168 | my ($ptype) = ($iptype =~ /^(.)i$/);
|
---|
| 1169 | return if !$ptype;
|
---|
| 1170 | $ptype .= '_';
|
---|
| 1171 |
|
---|
[663] | 1172 | my $plist = $dbh->selectall_arrayref( q(
|
---|
| 1173 | SELECT count(*) AS poolfree,p.pool AS poolblock, a.city AS poolcit
|
---|
| 1174 | FROM poolips p
|
---|
| 1175 | JOIN allocations a ON p.parent_id=a.id
|
---|
| 1176 | WHERE p.available='y' AND a.city = ? AND p.type LIKE ?
|
---|
| 1177 | GROUP BY p.pool,a.city
|
---|
| 1178 | ),
|
---|
[572] | 1179 | { Slice => {} }, ($pcity, $ptype) );
|
---|
[532] | 1180 | return $plist;
|
---|
| 1181 | } # end getPoolSelect()
|
---|
| 1182 |
|
---|
| 1183 |
|
---|
[533] | 1184 | ## IPDB::findAllocateFrom()
|
---|
| 1185 | # Find free block to add a new allocation from. (CIDR block version of pool select above, more or less)
|
---|
| 1186 | # Takes
|
---|
| 1187 | # - mask length
|
---|
| 1188 | # - allocation type
|
---|
| 1189 | # - POP city "parent"
|
---|
| 1190 | # - optional master-block restriction
|
---|
| 1191 | # - optional flag to allow automatic pick-from-private-network-ranges
|
---|
[740] | 1192 | # Returns a 3-element list with the free block ID, CIDR, and parent ID matching the criteria, if any
|
---|
[533] | 1193 | sub findAllocateFrom {
|
---|
| 1194 | my $dbh = shift;
|
---|
| 1195 | my $maskbits = shift;
|
---|
| 1196 | my $type = shift;
|
---|
| 1197 | my $city = shift;
|
---|
| 1198 | my $pop = shift;
|
---|
| 1199 | my %optargs = @_;
|
---|
| 1200 |
|
---|
| 1201 | my $failmsg = "No suitable free block found\n";
|
---|
| 1202 |
|
---|
[633] | 1203 | my @vallist;
|
---|
| 1204 | my $sql;
|
---|
| 1205 |
|
---|
| 1206 | # Free pool IPs should be easy.
|
---|
| 1207 | if ($type =~ /^.i$/) {
|
---|
| 1208 | # User may get an IP from the wrong VRF. User should not be using admin tools to allocate static IPs.
|
---|
| 1209 | $sql = "SELECT id, ip, parent_id FROM poolips WHERE ip = ?";
|
---|
| 1210 | @vallist = ($optargs{gimme});
|
---|
| 1211 | } else {
|
---|
| 1212 |
|
---|
[533] | 1213 | ## Set up the SQL to find out what freeblock we can (probably) use for an allocation.
|
---|
| 1214 | ## Very large systems will require development of a reserve system (possibly an extension
|
---|
| 1215 | ## of the reserve-for-expansion concept in https://secure.deepnet.cx/trac/ipdb/ticket/24?)
|
---|
| 1216 | ## Also populate a value list for the DBI call.
|
---|
| 1217 |
|
---|
[633] | 1218 | @vallist = ($maskbits);
|
---|
| 1219 | $sql = "SELECT id,cidr,parent_id FROM freeblocks WHERE masklen(cidr) <= ?";
|
---|
[533] | 1220 |
|
---|
[572] | 1221 | # cases, strict rules
|
---|
| 1222 | # .c -> container type
|
---|
| 1223 | # requires a routing container, fbtype r
|
---|
| 1224 | # .d -> DHCP/"normal-routing" static pool
|
---|
| 1225 | # requires a routing container, fbtype r
|
---|
| 1226 | # .e -> Dynamic-assignment connectivity
|
---|
| 1227 | # requires a routing container, fbtype r
|
---|
| 1228 | # .i -> error, can't allocate static IPs this way?
|
---|
| 1229 | # mm -> error, master block
|
---|
| 1230 | # rm -> routed block
|
---|
| 1231 | # requires master block, fbtype m
|
---|
| 1232 | # .n -> Miscellaneous usage
|
---|
| 1233 | # requires a routing container, fbtype r
|
---|
| 1234 | # .p -> PPP(oE) static pool
|
---|
| 1235 | # requires a routing container, fbtype r
|
---|
| 1236 | # .r -> contained type
|
---|
| 1237 | # requires a matching container, fbtype $1
|
---|
| 1238 | ##fixme: strict-or-not flag
|
---|
| 1239 |
|
---|
[633] | 1240 | ##fixme: config or UI flag for "Strict" mode
|
---|
| 1241 | # if ($strictmode) {
|
---|
| 1242 | if (0) {
|
---|
[572] | 1243 | if ($type =~ /^(.)r$/) {
|
---|
| 1244 | push @vallist, $1;
|
---|
| 1245 | $sql .= " AND routed = ?";
|
---|
| 1246 | } elsif ($type eq 'rm') {
|
---|
| 1247 | $sql .= " AND routed = 'm'";
|
---|
| 1248 | } else {
|
---|
| 1249 | $sql .= " AND routed = 'r'";
|
---|
| 1250 | }
|
---|
[633] | 1251 | }
|
---|
[572] | 1252 |
|
---|
[633] | 1253 | # for PPP(oE) and container types, the POP city is the one attached to the pool.
|
---|
| 1254 | # individual allocations get listed with the customer city site.
|
---|
| 1255 | ##fixme: chain cities to align roughly with a full layer-2 node graph
|
---|
| 1256 | $city = $pop if $type !~ /^.[pc]$/;
|
---|
| 1257 | if ($type ne 'rm' && $city) {
|
---|
| 1258 | $sql .= " AND city = ?";
|
---|
| 1259 | push @vallist, $city;
|
---|
[533] | 1260 | }
|
---|
[633] | 1261 | # Allow specifying an arbitrary full block, instead of a master
|
---|
| 1262 | if ($optargs{gimme}) {
|
---|
| 1263 | $sql .= " AND cidr >>= ?";
|
---|
| 1264 | push @vallist, $optargs{gimme};
|
---|
| 1265 | }
|
---|
| 1266 | # if a specific master was requested, allow the requestor to self->shoot(foot)
|
---|
| 1267 | if ($optargs{master} && $optargs{master} ne '-') {
|
---|
| 1268 | $sql .= " AND master_id = ?";
|
---|
| 1269 | # if $optargs{master} ne '-';
|
---|
| 1270 | push @vallist, $optargs{master};
|
---|
| 1271 | } else {
|
---|
| 1272 | # if a specific master was NOT requested, filter out the RFC 1918 private networks
|
---|
| 1273 | if (!$optargs{allowpriv}) {
|
---|
| 1274 | $sql .= " AND NOT (cidr <<= '192.168.0.0/16' OR cidr <<= '10.0.0.0/8' OR cidr <<= '172.16.0.0/12')";
|
---|
| 1275 | }
|
---|
| 1276 | }
|
---|
[693] | 1277 | # Keep "reserved" blocks out of automatic assignment.
|
---|
| 1278 | ##fixme: needs a UI flag or a config knob
|
---|
| 1279 | $sql .= " AND reserve_for = 0";
|
---|
[633] | 1280 | # Sorting and limiting, since we don't (currently) care to provide a selection of
|
---|
| 1281 | # blocks to carve up. This preserves something resembling optimal usage of the IP
|
---|
| 1282 | # space by forcing contiguous allocations and free blocks as much as possible.
|
---|
| 1283 | $sql .= " ORDER BY masklen(cidr) DESC,cidr LIMIT 1";
|
---|
| 1284 | } # done setting up SQL for free CIDR block
|
---|
[533] | 1285 |
|
---|
[633] | 1286 | my ($fbid,$fbfound,$fbparent) = $dbh->selectrow_array($sql, undef, @vallist);
|
---|
| 1287 | return $fbid,$fbfound,$fbparent;
|
---|
[533] | 1288 | } # end findAllocateFrom()
|
---|
| 1289 |
|
---|
| 1290 |
|
---|
[536] | 1291 | ## IPDB::ipParent()
|
---|
| 1292 | # Get an IP's parent pool's details
|
---|
| 1293 | # Takes a database handle and IP
|
---|
| 1294 | # Returns a hashref to the parent pool block, if any
|
---|
| 1295 | sub ipParent {
|
---|
| 1296 | my $dbh = shift;
|
---|
| 1297 | my $block = shift;
|
---|
| 1298 |
|
---|
| 1299 | my $pinfo = $dbh->selectrow_hashref("SELECT cidr,custid,type,city,description FROM allocations".
|
---|
[565] | 1300 | " WHERE cidr >>= ? AND (type LIKE '_p' OR type LIKE '_d')", undef, ($block) );
|
---|
[536] | 1301 | return $pinfo;
|
---|
| 1302 | } # end ipParent()
|
---|
| 1303 |
|
---|
| 1304 |
|
---|
| 1305 | ## IPDB::subParent()
|
---|
[529] | 1306 | # Get a block's parent's details
|
---|
| 1307 | # Takes a database handle and CIDR block
|
---|
[536] | 1308 | # Returns a hashref to the parent container block, if any
|
---|
| 1309 | sub subParent {
|
---|
[529] | 1310 | my $dbh = shift;
|
---|
| 1311 | my $block = shift;
|
---|
| 1312 |
|
---|
| 1313 | my $pinfo = $dbh->selectrow_hashref("SELECT cidr,custid,type,city,description FROM allocations".
|
---|
| 1314 | " WHERE cidr >>= ?", undef, ($block) );
|
---|
| 1315 | return $pinfo;
|
---|
[536] | 1316 | } # end subParent()
|
---|
[529] | 1317 |
|
---|
| 1318 |
|
---|
[536] | 1319 | ## IPDB::blockParent()
|
---|
| 1320 | # Get a block's parent's details
|
---|
| 1321 | # Takes a database handle and CIDR block
|
---|
| 1322 | # Returns a hashref to the parent container block, if any
|
---|
| 1323 | sub blockParent {
|
---|
| 1324 | my $dbh = shift;
|
---|
| 1325 | my $block = shift;
|
---|
| 1326 |
|
---|
| 1327 | my $pinfo = $dbh->selectrow_hashref("SELECT cidr,city FROM routed".
|
---|
| 1328 | " WHERE cidr >>= ?", undef, ($block) );
|
---|
| 1329 | return $pinfo;
|
---|
| 1330 | } # end blockParent()
|
---|
| 1331 |
|
---|
| 1332 |
|
---|
[697] | 1333 | ## IPDB::getBreadCrumbs()
|
---|
| 1334 | # Retrieve the ID and CIDR of a block's parent(s) up to the master block
|
---|
| 1335 | # Returns an arrayref to a list of hashrefs with CIDR and block ID
|
---|
| 1336 | sub getBreadCrumbs {
|
---|
| 1337 | my $dbh = shift;
|
---|
| 1338 | my $parent = shift;
|
---|
| 1339 | my @result;
|
---|
| 1340 |
|
---|
| 1341 | my $sth = $dbh-> prepare("SELECT cidr,type,id,parent_id FROM allocations WHERE id=?");
|
---|
| 1342 |
|
---|
| 1343 | while ($parent != 0) {
|
---|
| 1344 | $sth->execute($parent);
|
---|
| 1345 | my ($cidr,$type,$id,$pid) = $sth->fetchrow_array;
|
---|
| 1346 | push @result, {cidr => $cidr, link => $id, ispool => ($type =~ /^.[dp]$/ ? 1 : 0)};
|
---|
| 1347 | $parent = $pid;
|
---|
| 1348 | }
|
---|
| 1349 |
|
---|
| 1350 | return \@result;
|
---|
| 1351 | } # end getBread()
|
---|
| 1352 |
|
---|
| 1353 |
|
---|
[527] | 1354 | ## IPDB::getRoutedCity()
|
---|
| 1355 | # Get the city for a routed block.
|
---|
| 1356 | sub getRoutedCity {
|
---|
| 1357 | my $dbh = shift;
|
---|
| 1358 | my $block = shift;
|
---|
| 1359 |
|
---|
| 1360 | my ($rcity) = $dbh->selectrow_array("SELECT city FROM routed WHERE cidr = ?", undef, ($block) );
|
---|
| 1361 | return $rcity;
|
---|
| 1362 | } # end getRoutedCity()
|
---|
| 1363 |
|
---|
| 1364 |
|
---|
[77] | 1365 | ## IPDB::allocateBlock()
|
---|
[66] | 1366 | # Does all of the magic of actually allocating a netblock
|
---|
[554] | 1367 | # Requires a database handle, and a hash containing the block to allocate, routing depth, custid,
|
---|
| 1368 | # type, city, block to allocate from, and optionally a description, notes, circuit ID,
|
---|
| 1369 | # and private data
|
---|
[77] | 1370 | # Returns a success code and optional error message.
|
---|
| 1371 | sub allocateBlock {
|
---|
[554] | 1372 | my $dbh = shift;
|
---|
[284] | 1373 |
|
---|
[554] | 1374 | my %args = @_;
|
---|
| 1375 |
|
---|
| 1376 | $args{cidr} = new NetAddr::IP $args{cidr};
|
---|
| 1377 |
|
---|
| 1378 | $args{desc} = '' if !$args{desc};
|
---|
| 1379 | $args{notes} = '' if !$args{notes};
|
---|
| 1380 | $args{circid} = '' if !$args{circid};
|
---|
| 1381 | $args{privdata} = '' if !$args{privdata};
|
---|
| 1382 | $args{vrf} = '' if !$args{vrf};
|
---|
[691] | 1383 | $args{vlan} = '' if !$args{vlan};
|
---|
[585] | 1384 | $args{rdns} = '' if !$args{rdns};
|
---|
[554] | 1385 |
|
---|
[690] | 1386 | # Could arguably allow this for eg /120 allocations, but end users who get a single v4 IP are
|
---|
| 1387 | # usually given a v6 /64, and most v6 addressing schemes need at least half that address space
|
---|
| 1388 | if ($args{cidr}->{isv6} && $args{rdns} =~ /\%/) {
|
---|
| 1389 | return ('FAIL','Reverse DNS template patterns are not supported for IPv6 allocations');
|
---|
| 1390 | }
|
---|
| 1391 |
|
---|
[77] | 1392 | my $sth;
|
---|
[66] | 1393 |
|
---|
[633] | 1394 | # Snag the "type" of the freeblock and its CIDR
|
---|
| 1395 | my ($alloc_from_type, $alloc_from, $fbparent, $fcity, $fbmaster) =
|
---|
| 1396 | $dbh->selectrow_array("SELECT routed,cidr,parent_id,city,master_id FROM freeblocks WHERE id = ?",
|
---|
| 1397 | undef, $args{fbid});
|
---|
| 1398 | $alloc_from = new NetAddr::IP $alloc_from;
|
---|
[692] | 1399 | return ('FAIL',"Failed to allocate $args{cidr}; intended free block was used by another allocation.")
|
---|
[741] | 1400 | if ($args{type} !~ /.i/ && !$fbparent);
|
---|
[692] | 1401 | ##fixme: fail here if !$alloc_from
|
---|
| 1402 | # also consider "lock for allocation" due to multistep allocation process
|
---|
[349] | 1403 |
|
---|
[79] | 1404 | # To contain the error message, if any.
|
---|
[554] | 1405 | my $msg = "Unknown error allocating $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
[79] | 1406 |
|
---|
[77] | 1407 | # Enable transactions and error handling
|
---|
| 1408 | local $dbh->{AutoCommit} = 0; # These need to be local so we don't
|
---|
| 1409 | local $dbh->{RaiseError} = 1; # step on our toes by accident.
|
---|
[66] | 1410 |
|
---|
[554] | 1411 | if ($args{type} =~ /^.i$/) {
|
---|
| 1412 | $msg = "Unable to assign static IP $args{cidr} to $args{custid}";
|
---|
[77] | 1413 | eval {
|
---|
[554] | 1414 | if ($args{cidr}) { # IP specified
|
---|
| 1415 | my ($isavail) = $dbh->selectrow_array("SELECT available FROM poolips WHERE ip=?", undef, ($args{cidr}) );
|
---|
| 1416 | die "IP is not in an IP pool.\n"
|
---|
| 1417 | if !$isavail;
|
---|
| 1418 | die "IP already allocated. Deallocate and reallocate, or update the entry\n"
|
---|
| 1419 | if $isavail eq 'n';
|
---|
| 1420 | } else { # IP not specified, take first available
|
---|
| 1421 | ($args{cidr}) = $dbh->selectrow_array("SELECT ip FROM poolips WHERE pool=? AND available='y' ORDER BY ip",
|
---|
| 1422 | undef, ($args{alloc_from}) );
|
---|
[545] | 1423 | }
|
---|
[633] | 1424 | $dbh->do("UPDATE poolips SET custid = ?, city = ?,available='n', description = ?, notes = ?, ".
|
---|
| 1425 | "circuitid = ?, privdata = ?, vrf = ?, rdns = ? ".
|
---|
| 1426 | "WHERE ip = ? AND parent_id = ?", undef,
|
---|
| 1427 | ($args{custid}, $args{city}, $args{desc}, $args{notes},
|
---|
| 1428 | $args{circid}, $args{privdata}, $args{vrf}, $args{rdns},
|
---|
| 1429 | $args{cidr}, $args{parent}) );
|
---|
[157] | 1430 |
|
---|
[397] | 1431 | # node hack
|
---|
[554] | 1432 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 1433 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 1434 | }
|
---|
| 1435 | # end node hack
|
---|
[545] | 1436 |
|
---|
[691] | 1437 | $dbh->commit; # Allocate IP from pool
|
---|
[77] | 1438 | };
|
---|
| 1439 | if ($@) {
|
---|
[545] | 1440 | $msg .= ": $@";
|
---|
[78] | 1441 | eval { $dbh->rollback; };
|
---|
[578] | 1442 | return ('FAIL', $msg);
|
---|
[77] | 1443 | } else {
|
---|
[585] | 1444 | _rpc('addOrUpdateRevRec', cidr => "$args{cidr}", name => $args{rdns}, rpcuser => $args{user});
|
---|
[578] | 1445 | return ('OK', $args{cidr});
|
---|
[77] | 1446 | }
|
---|
| 1447 |
|
---|
| 1448 | } else { # end IP-from-pool allocation
|
---|
| 1449 |
|
---|
[633] | 1450 | if ($args{cidr} == $alloc_from) {
|
---|
[77] | 1451 | # Easiest case- insert in one table, delete in the other, and go home. More or less.
|
---|
| 1452 |
|
---|
| 1453 | eval {
|
---|
[554] | 1454 | $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
| 1455 |
|
---|
[633] | 1456 | # Insert the allocations entry
|
---|
| 1457 | $dbh->do("INSERT INTO allocations ".
|
---|
[691] | 1458 | "(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns)".
|
---|
| 1459 | " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", undef,
|
---|
| 1460 | ($args{cidr}, $fbparent, $fbmaster, $args{vrf}, $args{vlan}, $args{custid}, $args{type}, $args{city},
|
---|
[633] | 1461 | $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
|
---|
| 1462 | my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
|
---|
[555] | 1463 |
|
---|
[554] | 1464 | # Munge freeblocks
|
---|
| 1465 | if ($args{type} =~ /^(.)[mc]$/) {
|
---|
| 1466 | # special case - block is a routed or container/"reserve" block
|
---|
| 1467 | my $rtype = $1;
|
---|
[633] | 1468 | $dbh->do("UPDATE freeblocks SET routed = ?,city = ?,parent_id = ? WHERE id = ?",
|
---|
| 1469 | undef, ($rtype, $args{city}, $bid, $args{fbid}) );
|
---|
[77] | 1470 | } else {
|
---|
[554] | 1471 | # "normal" case
|
---|
[633] | 1472 | $dbh->do("DELETE FROM freeblocks WHERE id = ?", undef, ($args{fbid}) );
|
---|
[554] | 1473 | }
|
---|
[77] | 1474 |
|
---|
[554] | 1475 | # And initialize the pool, if necessary
|
---|
| 1476 | # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
|
---|
| 1477 | # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
|
---|
| 1478 | if ($args{type} =~ /^.p$/) {
|
---|
| 1479 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[633] | 1480 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $bid);
|
---|
[554] | 1481 | die $rmsg if $code eq 'FAIL';
|
---|
| 1482 | } elsif ($args{type} =~ /^.d$/) {
|
---|
| 1483 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[633] | 1484 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $bid);
|
---|
[554] | 1485 | die $rmsg if $code eq 'FAIL';
|
---|
| 1486 | }
|
---|
[79] | 1487 |
|
---|
[397] | 1488 | # node hack
|
---|
[554] | 1489 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 1490 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 1491 | }
|
---|
| 1492 | # end node hack
|
---|
[574] | 1493 |
|
---|
[691] | 1494 | $dbh->commit; # Simple block allocation
|
---|
[78] | 1495 | }; # end of eval
|
---|
[77] | 1496 | if ($@) {
|
---|
[157] | 1497 | $msg .= ": ".$@;
|
---|
[77] | 1498 | eval { $dbh->rollback; };
|
---|
[157] | 1499 | return ('FAIL',$msg);
|
---|
[78] | 1500 | }
|
---|
[77] | 1501 |
|
---|
| 1502 | } else { # cidr != alloc_from
|
---|
| 1503 |
|
---|
| 1504 | # Hard case. Allocation is smaller than free block.
|
---|
[633] | 1505 |
|
---|
| 1506 | # make sure new allocation is in fact within freeblock. *sigh*
|
---|
| 1507 | return ('FAIL',"Requested allocation $args{cidr} is not within $alloc_from")
|
---|
| 1508 | if !$alloc_from->contains($args{cidr});
|
---|
[554] | 1509 | my $wantmaskbits = $args{cidr}->masklen;
|
---|
[633] | 1510 | my $maskbits = $alloc_from->masklen;
|
---|
[77] | 1511 |
|
---|
| 1512 | my @newfreeblocks; # Holds free blocks generated from splitting the source freeblock.
|
---|
| 1513 |
|
---|
| 1514 | # This determines which blocks will be left "free" after allocation. We take the
|
---|
| 1515 | # block we're allocating from, and split it in half. We see which half the wanted
|
---|
| 1516 | # block is in, and repeat until the wanted block is equal to one of the halves.
|
---|
| 1517 | my $i=0;
|
---|
[633] | 1518 | my $tmp_from = $alloc_from; # So we don't munge $args{alloc_from}
|
---|
[77] | 1519 | while ($maskbits++ < $wantmaskbits) {
|
---|
| 1520 | my @subblocks = $tmp_from->split($maskbits);
|
---|
[554] | 1521 | $newfreeblocks[$i++] = (($args{cidr}->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
|
---|
| 1522 | $tmp_from = ( ($args{cidr}->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
|
---|
[77] | 1523 | } # while
|
---|
| 1524 |
|
---|
| 1525 | # Begin SQL transaction block
|
---|
| 1526 | eval {
|
---|
[554] | 1527 | $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
|
---|
[79] | 1528 |
|
---|
[77] | 1529 | # Delete old freeblocks entry
|
---|
[633] | 1530 | $dbh->do("DELETE FROM freeblocks WHERE id = ?", undef, ($args{fbid}) );
|
---|
[77] | 1531 |
|
---|
[633] | 1532 | # Insert the allocations entry
|
---|
| 1533 | $dbh->do("INSERT INTO allocations ".
|
---|
[691] | 1534 | "(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns)".
|
---|
| 1535 | " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", undef,
|
---|
| 1536 | ($args{cidr}, $fbparent, $fbmaster, $args{vrf}, $args{vlan}, $args{custid}, $args{type}, $args{city},
|
---|
[633] | 1537 | $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
|
---|
| 1538 | my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
|
---|
| 1539 |
|
---|
[692] | 1540 | # Insert new list of smaller free blocks left over. Flag the one that matches the
|
---|
| 1541 | # masklength of the new allocation, if a reserve block was requested.
|
---|
| 1542 | $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id,reserve_for) ".
|
---|
| 1543 | "VALUES (?,?,?,?,?,?,?)");
|
---|
| 1544 | foreach my $block (@newfreeblocks) {
|
---|
| 1545 | $sth->execute($block, $fcity, $alloc_from_type, $args{vrf}, $fbparent, $fbmaster,
|
---|
[721] | 1546 | ($args{reserve} && $block->masklen == $wantmaskbits ? $bid : 0));
|
---|
[692] | 1547 | }
|
---|
| 1548 |
|
---|
[554] | 1549 | # For routed/container types, add a freeblock within the allocated block so we can subdivide it further
|
---|
| 1550 | if ($args{type} =~ /(.)[mc]/) { # rm and .c types - containers
|
---|
| 1551 | my $rtype = $1;
|
---|
[698] | 1552 | $sth->execute($args{cidr}, $args{city}, $rtype, $args{vrf}, $bid, $fbmaster, 0);
|
---|
[554] | 1553 | }
|
---|
[79] | 1554 |
|
---|
[554] | 1555 | # And initialize the pool, if necessary
|
---|
| 1556 | # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
|
---|
| 1557 | # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
|
---|
| 1558 | if ($args{type} =~ /^.p$/) {
|
---|
| 1559 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[633] | 1560 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $bid);
|
---|
[554] | 1561 | die $rmsg if $code eq 'FAIL';
|
---|
| 1562 | } elsif ($args{type} =~ /^.d$/) {
|
---|
| 1563 | $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
|
---|
[633] | 1564 | my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $bid);
|
---|
[554] | 1565 | die $rmsg if $code eq 'FAIL';
|
---|
| 1566 | }
|
---|
[77] | 1567 |
|
---|
[397] | 1568 | # node hack
|
---|
[554] | 1569 | if ($args{nodeid} && $args{nodeid} ne '') {
|
---|
| 1570 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
|
---|
[397] | 1571 | }
|
---|
| 1572 | # end node hack
|
---|
[554] | 1573 |
|
---|
[691] | 1574 | $dbh->commit; # Complex block allocation
|
---|
[77] | 1575 | }; # end eval
|
---|
| 1576 | if ($@) {
|
---|
[256] | 1577 | $msg .= ": ".$@;
|
---|
[77] | 1578 | eval { $dbh->rollback; };
|
---|
[78] | 1579 | return ('FAIL',$msg);
|
---|
[77] | 1580 | }
|
---|
| 1581 |
|
---|
| 1582 | } # end fullcidr != alloc_from
|
---|
| 1583 |
|
---|
[590] | 1584 | # now we do the DNS dance for netblocks, if we have an RPC server to do it with and a pattern to use.
|
---|
| 1585 | _rpc('addOrUpdateRevRec', cidr => "$args{cidr}", name => $args{rdns}, rpcuser => $args{user})
|
---|
| 1586 | if $args{rdns};
|
---|
[585] | 1587 |
|
---|
[677] | 1588 | # and the per-IP set, if there is one.
|
---|
| 1589 | _rpc('updateRevSet', %{$args{iprev}}, rpcuser => $args{user});
|
---|
| 1590 |
|
---|
[585] | 1591 | return ('OK', 'OK');
|
---|
| 1592 |
|
---|
[77] | 1593 | } # end static-IP vs netblock allocation
|
---|
| 1594 |
|
---|
| 1595 | } # end allocateBlock()
|
---|
| 1596 |
|
---|
| 1597 |
|
---|
| 1598 | ## IPDB::initPool()
|
---|
| 1599 | # Initializes a pool
|
---|
| 1600 | # Requires a database handle, the pool CIDR, type, city, and a parameter
|
---|
| 1601 | # indicating whether the pool should allow allocation of literally every
|
---|
| 1602 | # IP, or if it should reserve network/gateway/broadcast IPs
|
---|
[78] | 1603 | # Note that this is NOT done in a transaction, that's why it's a private
|
---|
| 1604 | # function and should ONLY EVER get called from allocateBlock()
|
---|
[77] | 1605 | sub initPool {
|
---|
[633] | 1606 | my ($dbh,undef,$type,$city,$class,$parent) = @_;
|
---|
[77] | 1607 | my $pool = new NetAddr::IP $_[1];
|
---|
| 1608 |
|
---|
[574] | 1609 | # IPv6 does not lend itself to IP pools as supported
|
---|
| 1610 | return ('FAIL',"Refusing to create IPv6 static IP pool") if $pool->{isv6};
|
---|
| 1611 | # IPv4 pools don't make much sense beyond even /24. Allow up to 4096-host footshooting anyway.
|
---|
| 1612 | # NetAddr::IP won't allow more than a /16 (65k hosts).
|
---|
| 1613 | return ('FAIL',"Refusing to create oversized static IP pool") if $pool->masklen <= 20;
|
---|
| 1614 |
|
---|
[691] | 1615 | # Retrieve some odds and ends for defaults on the IPs
|
---|
[633] | 1616 | my ($pcustid) = $dbh->selectrow_array("SELECT def_custid FROM alloctypes WHERE type=?", undef, ($type) );
|
---|
[699] | 1617 | my ($vrf,$vlan,$master) = $dbh->selectrow_array("SELECT vrf,vlan,master_id FROM allocations WHERE id = ?",
|
---|
[691] | 1618 | undef, ($parent) );
|
---|
| 1619 |
|
---|
[157] | 1620 | $type =~ s/[pd]$/i/;
|
---|
[77] | 1621 | my $sth;
|
---|
[157] | 1622 | my $msg;
|
---|
[77] | 1623 |
|
---|
[157] | 1624 | eval {
|
---|
| 1625 | # have to insert all pool IPs into poolips table as "unallocated".
|
---|
[699] | 1626 | $sth = $dbh->prepare("INSERT INTO poolips (ip,custid,city,type,parent_id,master_id) VALUES (?,?,?,?,?,?)");
|
---|
[695] | 1627 |
|
---|
| 1628 | # in case of pool extension by some means, we need to see what IPs were already inserted
|
---|
| 1629 | my $tmp1 = $dbh->selectall_arrayref("SELECT ip FROM poolips WHERE parent_id = ?", undef, $parent);
|
---|
| 1630 | my %foundips;
|
---|
| 1631 | foreach (@{$tmp1}) {
|
---|
| 1632 | $foundips{$_->[0]} = 1;
|
---|
| 1633 | }
|
---|
| 1634 |
|
---|
[696] | 1635 | # Dodge an edge case - pool where IPs have been "stolen" and turned into a netblock assignment.
|
---|
| 1636 | # We can't just "get all the current IPs, and add the missing ones", because some IPs are
|
---|
| 1637 | # legitimately missing (for stretchy values of "legitimately").
|
---|
| 1638 |
|
---|
| 1639 | my $pdata = getBlockData($dbh, $parent);
|
---|
| 1640 | my $pcidr = new NetAddr::IP $pdata->{block};
|
---|
| 1641 |
|
---|
| 1642 | if ($pcidr != $pool) {
|
---|
| 1643 | # enumerate the IPs from the *old* pool, flag them as "found", so we can iterate the entire
|
---|
| 1644 | # requested pool and still make sure we skip the IPs in the old pool - even if they've been
|
---|
| 1645 | # "stolen" by legacy netblocks.
|
---|
| 1646 | my @oldips = $pcidr->hostenum;
|
---|
| 1647 | # decide whether to start excluding existing IPs at the "gateway" or "gateway+1"
|
---|
| 1648 | my $ostart = ($pdata->{type} =~ /^.d$/ ? 1 : 0);
|
---|
| 1649 | for (my $i = $ostart; $i<= $#oldips; $i++) {
|
---|
| 1650 | $foundips{$oldips[$i]} = 1;
|
---|
| 1651 | }
|
---|
| 1652 | }
|
---|
| 1653 |
|
---|
[695] | 1654 | # enumerate the hosts in the IP range - everything except the first (net) and last (bcast) IP
|
---|
[157] | 1655 | my @poolip_list = $pool->hostenum;
|
---|
[695] | 1656 |
|
---|
| 1657 | # always check/add IPs from gw+1 through bcast-1:
|
---|
| 1658 | # (but the set won't be in oooorderrrrr! <pout>)
|
---|
| 1659 | for (my $i=1; $i<=$#poolip_list; $i++) {
|
---|
[696] | 1660 | my $baseip = $poolip_list[$i]->addr;
|
---|
| 1661 | if ($baseip !~ /\.(?:0|255)$/ && !$foundips{$poolip_list[$i]}) {
|
---|
[699] | 1662 | $sth->execute($baseip, $pcustid, $city, $type, $parent, $master);
|
---|
[696] | 1663 | }
|
---|
[695] | 1664 | }
|
---|
| 1665 |
|
---|
| 1666 | # now do the special case - DSL/PPP blocks can use the "net", "gw", and "bcast" IPs.
|
---|
| 1667 | # we exclude .0 and .255 anyway, since while they'll mostly work, they *will* behave badly here and there.
|
---|
[157] | 1668 | if ($class eq 'all') { # (DSL-ish block - *all* IPs available
|
---|
[246] | 1669 | if ($pool->addr !~ /\.0$/) { # .0 causes weirdness.
|
---|
[734] | 1670 | $sth->execute($pool->addr, $pcustid, $city, $type, $parent, $master) unless $foundips{$pool->addr."/32"};
|
---|
[246] | 1671 | }
|
---|
[699] | 1672 | $sth->execute($poolip_list[0]->addr, $pcustid, $city, $type, $parent, $master) unless $foundips{$poolip_list[0]};
|
---|
[157] | 1673 | $pool--;
|
---|
[246] | 1674 | if ($pool->addr !~ /\.255$/) { # .255 can cause weirdness.
|
---|
[734] | 1675 | $sth->execute($pool->addr, $pcustid, $city, $type, $parent, $master) unless $foundips{$pool->addr."/32"};
|
---|
[246] | 1676 | }
|
---|
[77] | 1677 | }
|
---|
[633] | 1678 | # don't commit here! the caller may not be done.
|
---|
| 1679 | # $dbh->commit;
|
---|
[157] | 1680 | };
|
---|
| 1681 | if ($@) {
|
---|
[574] | 1682 | $msg = $@;
|
---|
[633] | 1683 | # Don't roll back! It's up to the caller to handle this.
|
---|
| 1684 | # eval { $dbh->rollback; };
|
---|
[157] | 1685 | return ('FAIL',$msg);
|
---|
| 1686 | } else {
|
---|
| 1687 | return ('OK',"OK");
|
---|
[77] | 1688 | }
|
---|
| 1689 | } # end initPool()
|
---|
| 1690 |
|
---|
| 1691 |
|
---|
[531] | 1692 | ## IPDB::updateBlock()
|
---|
| 1693 | # Update an allocation
|
---|
| 1694 | # Takes all allocation fields in a hash
|
---|
| 1695 | sub updateBlock {
|
---|
| 1696 | my $dbh = shift;
|
---|
| 1697 | my %args = @_;
|
---|
| 1698 |
|
---|
| 1699 | return ('FAIL', 'Missing block to update') if !$args{block};
|
---|
| 1700 |
|
---|
[634] | 1701 | # Spaces don't show up well in lots of places. Make sure they don't get into the DB.
|
---|
| 1702 | $args{custid} =~ s/^\s+//;
|
---|
| 1703 | $args{custid} =~ s/\s+$//;
|
---|
| 1704 |
|
---|
[531] | 1705 | # do it all in a transaction
|
---|
| 1706 | local $dbh->{AutoCommit} = 0;
|
---|
| 1707 | local $dbh->{RaiseError} = 1;
|
---|
| 1708 |
|
---|
| 1709 | my @fieldlist;
|
---|
| 1710 | my @vallist;
|
---|
[691] | 1711 | foreach ('custid', 'city', 'description', 'notes', 'circuitid', 'privdata', 'rdns', 'vrf', 'vlan') {
|
---|
[531] | 1712 | if ($args{$_}) {
|
---|
| 1713 | push @fieldlist, $_;
|
---|
| 1714 | push @vallist, $args{$_};
|
---|
| 1715 | }
|
---|
| 1716 | }
|
---|
| 1717 |
|
---|
[634] | 1718 | my $binfo;
|
---|
[531] | 1719 | my $updtable = 'allocations';
|
---|
[634] | 1720 | my $keyfield = 'id';
|
---|
[535] | 1721 | if ($args{type} =~ /^(.)i$/) {
|
---|
[531] | 1722 | $updtable = 'poolips';
|
---|
[634] | 1723 | $binfo = getBlockData($dbh, $args{block}, 'i');
|
---|
[531] | 1724 | } else {
|
---|
| 1725 | ## fixme: there's got to be a better way...
|
---|
[634] | 1726 | $binfo = getBlockData($dbh, $args{block});
|
---|
[531] | 1727 | if ($args{swip}) {
|
---|
| 1728 | if ($args{swip} eq 'on' || $args{swip} eq '1' || $args{swip} eq 'y') {
|
---|
| 1729 | $args{swip} = 'y';
|
---|
| 1730 | } else {
|
---|
| 1731 | $args{swip} = 'n';
|
---|
| 1732 | }
|
---|
| 1733 | }
|
---|
| 1734 | foreach ('type', 'swip') {
|
---|
| 1735 | if ($args{$_}) {
|
---|
| 1736 | push @fieldlist, $_;
|
---|
| 1737 | push @vallist, $args{$_};
|
---|
| 1738 | }
|
---|
| 1739 | }
|
---|
| 1740 | }
|
---|
| 1741 |
|
---|
| 1742 | return ('FAIL', 'No fields to update') if !@fieldlist;
|
---|
| 1743 |
|
---|
| 1744 | my $sql = "UPDATE $updtable SET ";
|
---|
| 1745 | $sql .= join " = ?, ", @fieldlist;
|
---|
| 1746 |
|
---|
| 1747 | eval {
|
---|
[695] | 1748 | # check for block merge first...
|
---|
| 1749 | if ($args{fbmerge}) {
|
---|
| 1750 | my $cidr = NetAddr::IP->new($binfo->{block});
|
---|
| 1751 | my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
|
---|
| 1752 | # safety net? make sure mergeable block passed in is really one or both of
|
---|
| 1753 | # a) reserved for expansion of the block and
|
---|
| 1754 | # b) confirmed CIDR-combinable
|
---|
| 1755 | # "safety? SELECT foo FROM freeblocks WHERE cidr << ? AND masklen(cidr) = ?, $newblock, ".$cidr->masklen."\n";
|
---|
| 1756 | $dbh->do("DELETE FROM freeblocks WHERE id=?", undef, $args{fbmerge});
|
---|
| 1757 | # ... so we can append the change in the stored CIDR field to extend the allocation.
|
---|
| 1758 | $sql .= " = ?, cidr";
|
---|
| 1759 | push @vallist, $newblock;
|
---|
| 1760 | # if we have an IP pool, call initPool to fill in any missing entries in the pool
|
---|
| 1761 | if ($binfo->{type} =~ /^.p$/) {
|
---|
| 1762 | my ($code,$rmsg) = initPool($dbh, "$newblock", $binfo->{type}, $binfo->{city}, 'all', $args{block});
|
---|
| 1763 | die $rmsg if $code eq 'FAIL';
|
---|
| 1764 | } elsif ($binfo->{type} =~ /^.d$/) {
|
---|
| 1765 | my ($code,$rmsg) = initPool($dbh, "$newblock", $binfo->{type}, $binfo->{city}, 'normal', $args{block});
|
---|
| 1766 | die $rmsg if $code eq 'FAIL';
|
---|
| 1767 | }
|
---|
| 1768 | }
|
---|
| 1769 |
|
---|
| 1770 | # append another SQL fragment
|
---|
| 1771 | push @vallist, $args{block};
|
---|
| 1772 | $sql .= " = ? WHERE $keyfield = ?";
|
---|
| 1773 |
|
---|
[531] | 1774 | # do the update
|
---|
| 1775 | $dbh->do($sql, undef, @vallist);
|
---|
| 1776 |
|
---|
| 1777 | if ($args{node}) {
|
---|
| 1778 | # done with delete/insert so we don't have to worry about funkyness updating a node ref that isn't there
|
---|
[634] | 1779 | $dbh->do("DELETE FROM noderef WHERE block = ?", undef, ($binfo->{block}) );
|
---|
| 1780 | $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($binfo->{block}, $args{node}) )
|
---|
| 1781 | if $args{node} ne '--';
|
---|
[531] | 1782 | }
|
---|
| 1783 |
|
---|
| 1784 | $dbh->commit;
|
---|
| 1785 | };
|
---|
| 1786 | if ($@) {
|
---|
| 1787 | my $msg = $@;
|
---|
| 1788 | $dbh->rollback;
|
---|
| 1789 | return ('FAIL', $msg);
|
---|
| 1790 | }
|
---|
[588] | 1791 |
|
---|
[686] | 1792 | # In case of any container (mainly master block), only update freeblocks so we don't stomp subs
|
---|
| 1793 | # (which would be the wrong thing in pretty much any case except "DELETE ALL EVARYTHING!!1!oneone!")
|
---|
| 1794 | if ($binfo->{type} =~ '.[mc]') {
|
---|
| 1795 | # Not using listFree() as it doesn't return quite all of the blocks wanted.
|
---|
| 1796 | # Retrieve the immediate free blocks
|
---|
| 1797 | my $sth = $dbh->prepare(q(
|
---|
| 1798 | SELECT cidr FROM freeblocks WHERE parent_id = ?
|
---|
| 1799 | UNION
|
---|
| 1800 | SELECT cidr FROM freeblocks f WHERE
|
---|
| 1801 | cidr = (SELECT cidr FROM allocations a WHERE f.cidr = a.cidr)
|
---|
| 1802 | AND master_id = ?
|
---|
| 1803 | ) );
|
---|
| 1804 | $sth->execute($args{block}, $binfo->{master_id});
|
---|
| 1805 | my %fbset;
|
---|
| 1806 | while (my ($fb) = $sth->fetchrow_array) {
|
---|
| 1807 | $fbset{"host_$fb"} = $args{rdns};
|
---|
| 1808 | }
|
---|
| 1809 | # We use this RPC call instead of multiple addOrUpdateRevRec calls, since we don't
|
---|
| 1810 | # know how many records we'll be updating and more than 3-4 is far too slow. This
|
---|
| 1811 | # should be safe to call unconditionally.
|
---|
| 1812 | # Requires dnsadmin >= r678
|
---|
| 1813 | _rpc('updateRevSet', %fbset, rpcuser => $args{user});
|
---|
[677] | 1814 |
|
---|
[686] | 1815 | } else {
|
---|
| 1816 | $binfo->{block} =~ s|/32$||;
|
---|
| 1817 | _rpc('addOrUpdateRevRec', cidr => $binfo->{block}, name => $args{rdns}, rpcuser => $args{user});
|
---|
[677] | 1818 |
|
---|
[686] | 1819 | # and the per-IP set, if there is one.
|
---|
| 1820 | _rpc('updateRevSet', %{$args{iprev}}, rpcuser => $args{user}) if keys (%{$args{iprev}});
|
---|
| 1821 | }
|
---|
| 1822 |
|
---|
[588] | 1823 | return ('OK','OK');
|
---|
[531] | 1824 | } # end updateBlock()
|
---|
| 1825 |
|
---|
| 1826 |
|
---|
[702] | 1827 | ## IPDB::splitBlock()
|
---|
| 1828 | # Splits an existing allocation into two or more smaller allocations based on a passed netmask
|
---|
| 1829 | # Duplicates all other data
|
---|
| 1830 | # Returns an arrayref to a list of hashrefs with ID and CIDR keys for the list of new allocations.
|
---|
| 1831 | # Should probably commit DNS magic to realign DNS data
|
---|
[713] | 1832 | # Mostly works but may return Strange Things(TM) if used on a master block
|
---|
[702] | 1833 | sub splitBlock {
|
---|
| 1834 | my $dbh = shift;
|
---|
[707] | 1835 | my %args = @_;
|
---|
| 1836 | # my $id = shift;
|
---|
| 1837 | # my $basetype = shift;
|
---|
| 1838 | # my $newmask = shift;
|
---|
[702] | 1839 |
|
---|
| 1840 | ##fixme: set errstr on errors so caller can suitably clue-by-four the user
|
---|
[707] | 1841 | return if $args{basetype} ne 'b'; # only netblocks allowed!
|
---|
[702] | 1842 |
|
---|
[707] | 1843 | my $binfo = getBlockData($dbh, $args{id});
|
---|
[702] | 1844 | return if !$binfo;
|
---|
| 1845 |
|
---|
[707] | 1846 | return if $args{newmask} !~ /^\d+$/;
|
---|
[702] | 1847 |
|
---|
| 1848 | my @ret;
|
---|
| 1849 | my $block = new NetAddr::IP $binfo->{block};
|
---|
| 1850 | my $oldmask = $block->masklen;
|
---|
| 1851 |
|
---|
| 1852 | # Fail if the block to split is "too small" - eg, can't split a v4 /32 at all
|
---|
| 1853 | # failure modes:
|
---|
| 1854 | # difference between $oldmask and $newmask is negative or 0
|
---|
[707] | 1855 | if ($args{newmask} - $oldmask <= 0) {
|
---|
| 1856 | $errstr = "Can't split a /$oldmask allocation into /$args{newmask} pieces";
|
---|
[702] | 1857 | return;
|
---|
| 1858 | }
|
---|
| 1859 | # # difference between $oldmask and $newmask is > n, for arbitrary n?
|
---|
| 1860 | # if ($newmask - $oldmask > 42) { # because 42
|
---|
| 1861 | # }
|
---|
| 1862 | # $oldmask > n, for arbitrary n? At least check limits of data type.
|
---|
| 1863 | if ($block->{isv6}) {
|
---|
[707] | 1864 | if ($args{newmask} - $oldmask > 128) {
|
---|
| 1865 | $errstr = "Impossible IPv6 mask length /$args{newmask} requested";
|
---|
[702] | 1866 | return;
|
---|
| 1867 | }
|
---|
| 1868 | } else {
|
---|
[707] | 1869 | if ($args{newmask} - $oldmask > 32) {
|
---|
| 1870 | $errstr = "Impossible IPv4 mask length /$args{newmask} requested";
|
---|
[702] | 1871 | return;
|
---|
| 1872 | }
|
---|
| 1873 | }
|
---|
| 1874 |
|
---|
[707] | 1875 | my @newblocks = $block->split($args{newmask});
|
---|
[702] | 1876 |
|
---|
| 1877 | local $dbh->{AutoCommit} = 0;
|
---|
| 1878 | local $dbh->{RaiseError} = 1;
|
---|
| 1879 |
|
---|
| 1880 | eval {
|
---|
| 1881 | # line up a list of fields and values. Be nice if there was a handy way to do,
|
---|
| 1882 | # direct in SQL, something like
|
---|
| 1883 | # "INSERT INTO foo (f1,f2,f3) VALUES (newf1,(SELECT oldf2,oldf3 FROM foo WHERE baz))"
|
---|
| 1884 | my @fieldlist = qw(type city description notes circuitid privdata custid swip vrf vlan rdns parent_id master_id);
|
---|
| 1885 | my $fields_sql = join(',', @fieldlist);
|
---|
| 1886 | my @vals;
|
---|
| 1887 | foreach (@fieldlist) {
|
---|
| 1888 | push @vals, $binfo->{$_};
|
---|
| 1889 | }
|
---|
| 1890 | # note the first block in the split for return
|
---|
[707] | 1891 | push @ret, {nid => $args{id}, nblock => "$newblocks[0]"};
|
---|
[702] | 1892 |
|
---|
| 1893 | # prepare
|
---|
| 1894 | my $idsth = $dbh->prepare("SELECT currval('allocations_id_seq')");
|
---|
[713] | 1895 | my $allocsth = $dbh->prepare("INSERT INTO allocations (cidr, $fields_sql)".
|
---|
[702] | 1896 | " VALUES (?".',?'x(scalar(@fieldlist)).")");
|
---|
| 1897 | my $nbsth = $dbh->prepare("DELETE FROM poolips WHERE parent_id = ? AND ip = ?");
|
---|
[713] | 1898 | my $upd_psth = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE parent_id = ? AND cidr <<= ?");
|
---|
| 1899 | my $upd_msth = $dbh->prepare("UPDATE allocations SET master_id = ? WHERE master_id = ? AND cidr <<= ?");
|
---|
| 1900 | my $fb_psth = $dbh->prepare("UPDATE freeblocks SET parent_id = ? WHERE parent_id = ? AND cidr <<= ?");
|
---|
| 1901 | my $fb_msth = $dbh->prepare("UPDATE freeblocks SET master_id = ? WHERE master_id = ? AND cidr <<= ?");
|
---|
| 1902 | my $pool_psth = $dbh->prepare("UPDATE poolips SET parent_id = ? WHERE parent_id = ? AND ip << ?");
|
---|
| 1903 | my $pool_msth = $dbh->prepare("UPDATE poolips SET master_id = ? WHERE master_id = ? AND ip <<= ?");
|
---|
[702] | 1904 |
|
---|
| 1905 | # set up update of existing block
|
---|
[707] | 1906 | $dbh->do("UPDATE allocations SET cidr = ? WHERE id = ?", undef, ("$newblocks[0]", $args{id}) );
|
---|
[702] | 1907 |
|
---|
[713] | 1908 | # axe the new bcast IP from the smaller pool at the "base" block, if it's a "normal" pool
|
---|
[702] | 1909 | if ($binfo->{type} =~ /.d/) {
|
---|
| 1910 | $newblocks[0]--;
|
---|
[707] | 1911 | $nbsth->execute($args{id}, $newblocks[0]->addr);
|
---|
[702] | 1912 | }
|
---|
| 1913 |
|
---|
[739] | 1914 | # Holder for freeblocks-to-delete. Should be impossible to have more than one...
|
---|
| 1915 | my %fbdel;
|
---|
| 1916 |
|
---|
[702] | 1917 | # Loop over the new blocks that are not the base block
|
---|
| 1918 | for (my $i = 1; $i <= $#newblocks; $i++) {
|
---|
[713] | 1919 | # add the new allocation
|
---|
| 1920 | $allocsth->execute($newblocks[$i], @vals);
|
---|
[702] | 1921 | # fetch the ID of the entry we just added...
|
---|
| 1922 | $idsth->execute();
|
---|
| 1923 | my ($nid) = $idsth->fetchrow_array();
|
---|
| 1924 | # ... so we can pass back the list of blocks and IDs...
|
---|
| 1925 | push @ret, {nid => $nid, nblock => "$newblocks[$i]"};
|
---|
| 1926 | # axe the net, gw, and bcast IPs as necessary when splitting a "normal" pool
|
---|
| 1927 | if ($binfo->{type} =~ /.d/) {
|
---|
| 1928 | # net
|
---|
[707] | 1929 | $nbsth->execute($args{id}, $newblocks[$i]->addr);
|
---|
[702] | 1930 | $newblocks[$i]++;
|
---|
| 1931 | # gw
|
---|
[707] | 1932 | $nbsth->execute($args{id}, $newblocks[$i]->addr);
|
---|
[702] | 1933 | $newblocks[$i]--;
|
---|
| 1934 | $newblocks[$i]--;
|
---|
| 1935 | # bcast
|
---|
[707] | 1936 | $nbsth->execute($args{id}, $newblocks[$i]->addr);
|
---|
[702] | 1937 | $newblocks[$i]++;
|
---|
[739] | 1938 | } # $binfo->{type} =~ /.d/
|
---|
[702] | 1939 |
|
---|
[739] | 1940 | # Check for free blocks larger than the new mask length, and split those as needed.
|
---|
| 1941 | if ($binfo->{type} =~ /.[cm]/) {
|
---|
| 1942 | # get a "list" of freeblocks bigger than the allocation in the parent. there's only one, right?
|
---|
| 1943 | my $fblist = $dbh->selectall_arrayref("SELECT id FROM freeblocks WHERE cidr >> ? AND parent_id = ? ",
|
---|
| 1944 | {Slice=>{}}, $newblocks[$i], $args{id});
|
---|
| 1945 | if (@$fblist) {
|
---|
| 1946 | # create a new freeblock for the new block we created earlier
|
---|
| 1947 | $dbh->do(q{
|
---|
| 1948 | INSERT INTO freeblocks (cidr, parent_id, master_id, city, routed,vrf)
|
---|
| 1949 | SELECT ? AS cidr, ? AS parent_id, master_id, city, routed, vrf FROM freeblocks
|
---|
| 1950 | WHERE id = ?
|
---|
| 1951 | }, undef, ($newblocks[$i], $nid, $fblist->[0]->{id}) );
|
---|
| 1952 | $fbdel{$fblist->[0]->{id}}++;
|
---|
| 1953 | }
|
---|
| 1954 | } # $binfo->{type} =~ /.[cm]/
|
---|
| 1955 |
|
---|
[713] | 1956 | # Reparent allocations, freeblocks, and pool IPs.
|
---|
| 1957 | $upd_psth->execute($nid, $args{id}, $newblocks[$i]);
|
---|
| 1958 | $fb_psth->execute($nid, $args{id}, $newblocks[$i]);
|
---|
| 1959 | $pool_psth->execute($nid, $args{id}, $newblocks[$i]);
|
---|
| 1960 |
|
---|
| 1961 | # Update master if we've split a master block
|
---|
| 1962 | if ($binfo->{type} eq 'mm') {
|
---|
| 1963 | $upd_msth->execute($nid, $args{id}, $newblocks[$i]);
|
---|
| 1964 | $fb_msth->execute($nid, $args{id}, $newblocks[$i]);
|
---|
| 1965 | $pool_msth->execute($nid, $args{id}, $newblocks[$i]);
|
---|
| 1966 | }
|
---|
| 1967 |
|
---|
[739] | 1968 | ##fixme:
|
---|
| 1969 | # Still missing one edge case - megasplitting a large block such that "many" children also need to be split.
|
---|
| 1970 | # I'm going to call this "unsupported" because I really can't imagine a sane reason for doing this.
|
---|
| 1971 | # Should probably check and error out at least
|
---|
| 1972 |
|
---|
[713] | 1973 | } # for (... @newblocks)
|
---|
| 1974 |
|
---|
[739] | 1975 | $dbh->do("DELETE FROM freeblocks WHERE id IN (".join(',', keys %fbdel).")");
|
---|
| 1976 |
|
---|
[702] | 1977 | $dbh->commit;
|
---|
| 1978 | };
|
---|
| 1979 | if ($@) {
|
---|
| 1980 | $errstr = "Error splitting $binfo->{block}: $@";
|
---|
| 1981 | $dbh->rollback;
|
---|
| 1982 | return;
|
---|
| 1983 | }
|
---|
[707] | 1984 | ##fixme: RPC return code?
|
---|
| 1985 | # particularly useful in this case as there may be arbitrary combinations of pass, warn, fail
|
---|
| 1986 | # results from each of the update and add(s), which could get the user to prod dnsadmin to see what exploded
|
---|
| 1987 | my $foo = _rpc('splitTemplate', cidr => $binfo->{block}, newmask => $args{newmask}, rpcuser => $args{user});
|
---|
[702] | 1988 |
|
---|
| 1989 | return \@ret;
|
---|
| 1990 | } # end splitBlock()
|
---|
| 1991 |
|
---|
| 1992 |
|
---|
[705] | 1993 | ## IPDB::shrinkBlock()
|
---|
| 1994 | # Shrink an allocation to the passed CIDR block
|
---|
| 1995 | # Takes an allocation ID and a new CIDR
|
---|
| 1996 | # Returns an arrayref to a list of hashrefs with the ID and CIDR of the freed block(s)
|
---|
| 1997 | # Refuses to shrink "real netblock" pool types below /30
|
---|
| 1998 | sub shrinkBlock {
|
---|
| 1999 | my $dbh = shift;
|
---|
| 2000 | my $id = shift;
|
---|
| 2001 |
|
---|
| 2002 | # just take the new CIDR spec; this way we can shrink eg .16/28 to .20/30 without extra contortions
|
---|
| 2003 | my $newblock = new NetAddr::IP shift;
|
---|
| 2004 |
|
---|
| 2005 | if (!$newblock) {
|
---|
| 2006 | $errstr = "Can't shrink something that's not a netblock";
|
---|
| 2007 | return;
|
---|
| 2008 | }
|
---|
| 2009 |
|
---|
| 2010 | my $binfo = getBlockData($dbh, $id);
|
---|
| 2011 | my $pinfo = getBlockData($dbh, $binfo->{parent_id});
|
---|
| 2012 |
|
---|
| 2013 | if ($binfo->{type} =~ /.d/ && $newblock->masklen > ($newblock->bits+2) ) {
|
---|
| 2014 | $errstr = "Can't shrink a non-PPP pool smaller than ".($newblock->{isv6} ? '/124' : '/30');
|
---|
| 2015 | return;
|
---|
| 2016 | }
|
---|
| 2017 |
|
---|
| 2018 | my $oldblock = new NetAddr::IP $binfo->{block};
|
---|
| 2019 |
|
---|
| 2020 | # Don't try to shrink the block outside of itself, Bad Things (probably) Happen.
|
---|
| 2021 | if (!$oldblock->contains($newblock)) {
|
---|
| 2022 | $errstr = "Can't shrink an allocation outside of itself";
|
---|
| 2023 | return;
|
---|
| 2024 | }
|
---|
| 2025 |
|
---|
| 2026 | local $dbh->{AutoCommit} = 0;
|
---|
| 2027 | local $dbh->{RaiseError} = 1;
|
---|
| 2028 |
|
---|
[713] | 2029 | my $addfbsth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id) VALUES (?,?,?,?,?,?)");
|
---|
[705] | 2030 | my $idsth = $dbh->prepare("SELECT currval('freeblocks_id_seq')");
|
---|
| 2031 | my $poolsth = $dbh->prepare("DELETE FROM poolips WHERE parent_id = ? AND ip << ?");
|
---|
| 2032 | my $netsth = $dbh->prepare("DELETE FROM poolips WHERE parent_id = ? AND ip = ?");
|
---|
[713] | 2033 | my $allocsth = $dbh->prepare("DELETE FROM allocations WHERE parent_id = ? AND cidr <<= ?");
|
---|
| 2034 | my $delfbsth = $dbh->prepare("DELETE FROM freeblocks WHERE parent_id = ? AND cidr <<= ?");
|
---|
[705] | 2035 |
|
---|
[713] | 2036 | ##fixme: turn this into a public/top-level sub?
|
---|
| 2037 | sub getchildren {
|
---|
| 2038 | my $dbh = shift;
|
---|
| 2039 | my $id = shift;
|
---|
| 2040 | my $master = shift;
|
---|
| 2041 | my $retlist = shift; # better than trying to return complex structures recursively. Ow.
|
---|
| 2042 | my $cidr = shift;
|
---|
| 2043 |
|
---|
| 2044 | if (!$cidr) {
|
---|
| 2045 | my $bd = getBlockData($dbh, $id);
|
---|
| 2046 | $cidr = $bd->{cidr};
|
---|
| 2047 | }
|
---|
| 2048 |
|
---|
| 2049 | my $sth = $dbh->prepare(q(
|
---|
| 2050 | SELECT id,cidr,type FROM allocations
|
---|
| 2051 | WHERE parent_id = ? AND master_id = ? AND cidr <<= ?
|
---|
| 2052 | ) );
|
---|
| 2053 | $sth->execute($id, $master, $cidr);
|
---|
| 2054 | while (my $row = $sth->fetchrow_hashref) {
|
---|
| 2055 | push @$retlist, $row;
|
---|
| 2056 | getchildren($dbh, $row->{id}, $master, $retlist, $cidr);
|
---|
| 2057 | }
|
---|
| 2058 | }
|
---|
| 2059 |
|
---|
[705] | 2060 | my @ret;
|
---|
[743] | 2061 | my @newfreelist;
|
---|
[705] | 2062 | eval {
|
---|
| 2063 | $dbh->do("UPDATE allocations SET cidr = ? WHERE id = ?", undef, $newblock, $id);
|
---|
| 2064 |
|
---|
| 2065 | # find the netblock(s) that are now free
|
---|
| 2066 | my @workingblocks = $oldblock->split($newblock->masklen);
|
---|
| 2067 | foreach my $newsub (@workingblocks) {
|
---|
| 2068 | next if $newsub == $newblock;
|
---|
[743] | 2069 | push @newfreelist, $newsub;
|
---|
[705] | 2070 | }
|
---|
[743] | 2071 | @newfreelist = Compact(@newfreelist);
|
---|
[705] | 2072 |
|
---|
| 2073 | # set new freeblocks, and clean up any IP pool entries if needed.
|
---|
[743] | 2074 | foreach my $newfree (@newfreelist) {
|
---|
[713] | 2075 | my @clist;
|
---|
| 2076 | # the block we're munging
|
---|
| 2077 | push @clist, { id => $id, type => $binfo->{type}, cidr => $binfo->{block} };
|
---|
| 2078 | getchildren($dbh, $id, $binfo->{master_id}, \@clist, $newfree);
|
---|
| 2079 |
|
---|
| 2080 | foreach my $goner (@clist) {
|
---|
| 2081 | $poolsth->execute($goner->{id}, $newfree) if $goner->{type} =~ /.[dp]/;
|
---|
| 2082 | $allocsth->execute($goner->{id}, $newfree);
|
---|
| 2083 | $delfbsth->execute($goner->{id}, $newfree);
|
---|
[705] | 2084 | }
|
---|
| 2085 |
|
---|
[713] | 2086 | # No pinfo means we're shrinking a master block, which means the free space is returned outside of IPDB.
|
---|
| 2087 | if ($pinfo) {
|
---|
| 2088 | $addfbsth->execute($newfree, $pinfo->{city}, 'm', $pinfo->{vrf}, $binfo->{parent_id}, $pinfo->{master_id});
|
---|
| 2089 | $idsth->execute;
|
---|
| 2090 | my ($nid) = $idsth->fetchrow_array();
|
---|
| 2091 | # add to return list
|
---|
| 2092 | push @ret, {fbid => $nid, newfree => "$newfree", fbparent => $binfo->{parent_id} };
|
---|
| 2093 | }
|
---|
| 2094 |
|
---|
[743] | 2095 | } # $newfree (@newfreelist)
|
---|
[713] | 2096 |
|
---|
[705] | 2097 | # additional cleanup on net/gw/bcast IPs in pool
|
---|
| 2098 | if ($binfo->{type} =~ /.d/) {
|
---|
| 2099 | $netsth->execute($id, $newblock->addr);
|
---|
| 2100 | $newblock++;
|
---|
| 2101 | $netsth->execute($id, $newblock->addr);
|
---|
| 2102 | $newblock--;
|
---|
| 2103 | $newblock--;
|
---|
| 2104 | $netsth->execute($id, $newblock->addr);
|
---|
| 2105 | }
|
---|
| 2106 |
|
---|
| 2107 | $dbh->commit;
|
---|
| 2108 | };
|
---|
| 2109 | if ($@) {
|
---|
| 2110 | $errstr = "Error splitting $binfo->{block}: $@";
|
---|
| 2111 | $dbh->rollback;
|
---|
| 2112 | return;
|
---|
| 2113 | }
|
---|
| 2114 |
|
---|
| 2115 | return \@ret;
|
---|
| 2116 | } # end shrinkBlock()
|
---|
| 2117 |
|
---|
| 2118 |
|
---|
[728] | 2119 | ## IPDB::mergeBlocks()
|
---|
| 2120 | # Merges two or more adjacent allocations, optionally including relevant
|
---|
| 2121 | # free space, into one allocation.
|
---|
| 2122 | # Takes a "base" block ID and a hash with a mask length and a scope argument to decide
|
---|
| 2123 | # how much existing allocation data to delete.
|
---|
[733] | 2124 | # Returns a list starting with the new merged block, then the merged allocations with comment
|
---|
[728] | 2125 | ## Merge scope:
|
---|
| 2126 | # Merge to container
|
---|
| 2127 | # keepall
|
---|
| 2128 | # Move all mergeable allocations into the new block
|
---|
| 2129 | # Move all mergeable free blocks into the new block
|
---|
| 2130 | # mergepeer
|
---|
| 2131 | # Move subs of mergeable containers into the updated primary.
|
---|
| 2132 | # Reparent free blocks in mergeable containers to the updated primary.
|
---|
| 2133 | # Convert assigned IPs from pools into subs.
|
---|
| 2134 | # Convert unused IPs from pools into free blocks.
|
---|
| 2135 | # Convert leaf allocations into free blocks.
|
---|
| 2136 | # clearpeer
|
---|
| 2137 | # Keep subs of the original
|
---|
| 2138 | # Convert assigned IPs from pools into subs.
|
---|
| 2139 | # Convert unused IPs from pools into free blocks.
|
---|
| 2140 | # Convert leaf allocations into free blocks.
|
---|
| 2141 | # clearall
|
---|
| 2142 | # Delete all peers, subs and IPs.
|
---|
| 2143 | # Add single free block for new container.
|
---|
| 2144 | # Merge to pool
|
---|
| 2145 | # keepall
|
---|
| 2146 | # Convert all leaf allocations in the merge range to groups of used IPs
|
---|
| 2147 | # mergepeer
|
---|
| 2148 | # Effectively equal to keepall
|
---|
| 2149 | # clearpeer
|
---|
| 2150 | # Only convert IPs from the original allocation to used IPs
|
---|
| 2151 | # clearall
|
---|
| 2152 | # Delete any existing IPs, and reinitialize the new pool entirely
|
---|
| 2153 | # Merge to leaf type
|
---|
| 2154 | # Remove all subs
|
---|
| 2155 | sub mergeBlocks {
|
---|
| 2156 | my $dbh = shift;
|
---|
| 2157 | my $prime = shift; # "base" block ID to use as a starting point
|
---|
| 2158 | if (!$prime) {
|
---|
| 2159 | $errstr = "Missing block ID to base merge on";
|
---|
| 2160 | return;
|
---|
| 2161 | }
|
---|
| 2162 |
|
---|
| 2163 | my %args = @_;
|
---|
| 2164 |
|
---|
| 2165 | # check key arguments.
|
---|
| 2166 | if (!$args{scope} || $args{scope} !~ /^(keepall|mergepeer|clearpeer|clearall)$/) {
|
---|
| 2167 | $errstr = "Bad or missing merge scope";
|
---|
| 2168 | return;
|
---|
| 2169 | }
|
---|
| 2170 | if (!$args{newmask} || $args{newmask} !~ /^\d+$/) {
|
---|
| 2171 | $errstr = "Bad or missing new netmask";
|
---|
| 2172 | return;
|
---|
| 2173 | }
|
---|
| 2174 |
|
---|
| 2175 | # Retrieve info about the base allocation we're munging
|
---|
| 2176 | my $binfo = getBlockData($dbh, $prime);
|
---|
| 2177 | my $block = new NetAddr::IP $binfo->{block};
|
---|
| 2178 | my ($basetype) = ($binfo->{type} =~ /^.(.)$/);
|
---|
| 2179 | $binfo->{id} = $prime; # preserve for later, just in case
|
---|
| 2180 |
|
---|
| 2181 | # proposed block
|
---|
| 2182 | my $newblock = new NetAddr::IP $block->addr."/$args{newmask}";
|
---|
| 2183 | $newblock = $newblock->network;
|
---|
| 2184 | $args{newtype} = $binfo->{type} if !$args{newtype};
|
---|
| 2185 | # if the "primary" block being changed is a master, it must remain one.
|
---|
[733] | 2186 | # Also force the scope, since otherwise things get ugly.
|
---|
| 2187 | if ($binfo->{type} eq 'mm') {
|
---|
| 2188 | $args{newtype} = 'mm';
|
---|
| 2189 | # don't want to make a peer master a sub of the existing one; too many special cases go explodey,
|
---|
| 2190 | # but want to retain all other allocations
|
---|
| 2191 | $args{scope} = 'mergepeer';
|
---|
| 2192 | }
|
---|
[728] | 2193 | my ($newcontainerclass) = ($args{newtype} =~ /^(.).$/);
|
---|
| 2194 |
|
---|
| 2195 | # build an info hash for the "new" allocation we're creating
|
---|
| 2196 | my $pinfo = {
|
---|
| 2197 | id => $prime,
|
---|
| 2198 | block => "$newblock",
|
---|
| 2199 | type => $args{newtype},
|
---|
| 2200 | parent_id =>
|
---|
| 2201 | $binfo->{parent_id},
|
---|
| 2202 | city => $binfo->{city},
|
---|
| 2203 | vrf => $binfo->{vrf},
|
---|
| 2204 | master_id => $binfo->{master_id}
|
---|
| 2205 | };
|
---|
| 2206 |
|
---|
| 2207 | my @retlist;
|
---|
| 2208 |
|
---|
[729] | 2209 | local $dbh->{AutoCommit} = 0;
|
---|
| 2210 | local $dbh->{RaiseError} = 1;
|
---|
| 2211 |
|
---|
[728] | 2212 | # Want to do all of the DB stuff in a transaction, to minimize data changing underfoot
|
---|
| 2213 | eval {
|
---|
| 2214 |
|
---|
| 2215 | # We always update the "prime" block passed in...
|
---|
| 2216 | $dbh->do("UPDATE allocations SET cidr = ?, type = ? WHERE id = ?", undef,
|
---|
| 2217 | ($newblock, $args{newtype}, $prime) )
|
---|
| 2218 | # ... but only on existing container or pool types. Leaf -> container conversions
|
---|
| 2219 | # may need a new parent inserted instead.
|
---|
| 2220 | if $basetype =~ /[cm]/;
|
---|
| 2221 |
|
---|
| 2222 | # For leaf blocks, we may need to create a new parent as the "primary" instead
|
---|
| 2223 | # of updating the existing block
|
---|
| 2224 | my $newparent = $dbh->prepare(q{
|
---|
| 2225 | INSERT INTO allocations (
|
---|
| 2226 | cidr, type, city, description, notes, circuitid, createstamp, modifystamp,
|
---|
| 2227 | privdata, custid, swip, vrf, vlan, rdns, parent_id, master_id
|
---|
| 2228 | )
|
---|
| 2229 | SELECT
|
---|
| 2230 | ? AS cidr, ? AS type, city, description, notes, circuitid, createstamp, modifystamp,
|
---|
| 2231 | privdata, custid, swip, vrf, vlan, rdns, parent_id, master_id
|
---|
| 2232 | FROM allocations
|
---|
| 2233 | WHERE id = ?
|
---|
| 2234 | });
|
---|
| 2235 |
|
---|
| 2236 | # Common actions
|
---|
| 2237 | my $peersth = $dbh->prepare("SELECT cidr,id,type,master_id FROM allocations WHERE parent_id = ? AND cidr <<= ?");
|
---|
| 2238 | $peersth->execute($binfo->{parent_id}, "$newblock");
|
---|
| 2239 | my $reparentsth = $dbh->prepare("UPDATE allocations SET parent_id = ?, master_id = ? WHERE id = ?");
|
---|
| 2240 | my $insfbsth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id) VALUES (?,?,?,?,?,?)");
|
---|
[737] | 2241 | my $delsth = $dbh->prepare("DELETE FROM allocations WHERE id = ?");
|
---|
[728] | 2242 |
|
---|
| 2243 | my $fbreparentsth = $dbh->prepare(q{
|
---|
| 2244 | UPDATE freeblocks
|
---|
| 2245 | SET parent_id = ?, master_id = ?, city = ?, routed = ?, vrf = ?
|
---|
| 2246 | WHERE parent_id = ? AND cidr <<= ?
|
---|
| 2247 | });
|
---|
| 2248 |
|
---|
| 2249 | if ($args{newtype} =~ /.[cm]/) {
|
---|
| 2250 | ## Container
|
---|
| 2251 |
|
---|
[733] | 2252 | # In case of merging a master block. Somewhat redundant with calls to $fbreparentsth,
|
---|
| 2253 | # but not *quite* entirely.
|
---|
[729] | 2254 | my $mfbsth = $dbh->prepare("UPDATE freeblocks SET master_id = ? WHERE master_id = ?");
|
---|
| 2255 |
|
---|
| 2256 | if ($args{scope} eq 'keepall') {
|
---|
[730] | 2257 | # Create a new parent with the same info as the passed "primary".
|
---|
| 2258 | $newparent->execute($newblock, $args{newtype}, $prime);
|
---|
| 2259 | # and now retrieve the new parent ID
|
---|
| 2260 | ($prime) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
|
---|
[733] | 2261 | # snag the new parent info for the return list
|
---|
| 2262 | push @retlist, { block => "$newblock", type => $disp_alloctypes{$args{newtype}}, id => $prime };
|
---|
[730] | 2263 | # Reparent the free blocks in the new block
|
---|
| 2264 | $fbreparentsth->execute($prime, $binfo->{master_id}, $binfo->{city}, $newcontainerclass, $binfo->{vrf},
|
---|
| 2265 | $binfo->{parent_id}, $newblock);
|
---|
| 2266 | # keep existing allocations (including the original primary), just push them down a level
|
---|
[733] | 2267 | while (my ($peercidr, $peer_id, $peertype, $m_id) = $peersth->fetchrow_array) {
|
---|
[730] | 2268 | $reparentsth->execute($prime, $binfo->{master_id}, $peer_id);
|
---|
| 2269 | # Fix up master_id on free blocks if we're merging a master block
|
---|
[733] | 2270 | $mfbsth->execute($binfo->{master_id}, $m_id) if $peertype eq 'mm';
|
---|
| 2271 | # capture block for return
|
---|
| 2272 | push @retlist, { block => $peercidr, mtype => $disp_alloctypes{$peertype} };
|
---|
[730] | 2273 | }
|
---|
| 2274 |
|
---|
[729] | 2275 | } elsif ($args{scope} =~ /^clear/) {
|
---|
[731] | 2276 | # clearpeer and clearall share a starting point
|
---|
[733] | 2277 | # snag the new parent info for the return list
|
---|
| 2278 | push @retlist, { block => "$newblock", type => $disp_alloctypes{$args{newtype}}, id => $prime };
|
---|
[731] | 2279 | # update the primary allocation info
|
---|
| 2280 | $dbh->do("UPDATE allocations SET cidr = ?, type = ? WHERE id = ?", undef, ($newblock, $args{newtype}, $prime) );
|
---|
| 2281 | # Reparent the free blocks in the new block
|
---|
| 2282 | $fbreparentsth->execute($prime, $binfo->{master_id}, $binfo->{city}, $newcontainerclass, $binfo->{vrf},
|
---|
| 2283 | $binfo->{parent_id}, $newblock);
|
---|
| 2284 | # Insert a free block if $prime is a leaf
|
---|
| 2285 | if ($binfo->{type} =~ /.[enr]/) {
|
---|
| 2286 | $insfbsth->execute($binfo->{block}, $binfo->{city}, $newcontainerclass, $binfo->{vrf}, $prime,
|
---|
| 2287 | $binfo->{master_id});
|
---|
| 2288 | }
|
---|
[733] | 2289 | # delete the peers.
|
---|
| 2290 | while (my ($peercidr, $peer_id, $peertype, $m_id) = $peersth->fetchrow_array) {
|
---|
[731] | 2291 | next if $peer_id == $prime;
|
---|
| 2292 | # push existing allocations down a level before deleting,
|
---|
| 2293 | # so that when they're deleted the parent info is correct
|
---|
| 2294 | $reparentsth->execute($prime, $binfo->{master_id}, $peer_id);
|
---|
| 2295 | _deleteCascade($dbh, $peer_id);
|
---|
| 2296 | # insert the freeblock _deleteCascade() (deliberately) didn't when deleting a master block.
|
---|
| 2297 | # aren't special cases fun?
|
---|
| 2298 | $dbh->do("INSERT INTO freeblocks (cidr,routed,parent_id,master_id) values (?,?,?,?)",
|
---|
[733] | 2299 | undef, ($peercidr, 'm', $prime, $prime) ) if $binfo->{type} eq 'mm';
|
---|
| 2300 | # capture block for return
|
---|
| 2301 | push @retlist, { block => $peercidr, mtype => $disp_alloctypes{$peertype} };
|
---|
[731] | 2302 | }
|
---|
| 2303 | if ($args{scope} eq 'clearall') {
|
---|
| 2304 | # delete any subs of $prime as well
|
---|
| 2305 | my $substh = $dbh->prepare("SELECT cidr,id FROM allocations WHERE parent_id = ?");
|
---|
| 2306 | $substh->execute($prime);
|
---|
| 2307 | while (my ($scidr, $s_id) = $substh->fetchrow_array) {
|
---|
| 2308 | _deleteCascade($dbh, $s_id);
|
---|
| 2309 | }
|
---|
| 2310 | } else {
|
---|
| 2311 | # clearpeer
|
---|
| 2312 | if ($basetype =~ /[dp]/) {
|
---|
| 2313 | # Convert active IP pool entries to allocations if the original was an IP pool
|
---|
| 2314 | _poolToAllocations($dbh, $binfo, $pinfo, newtype => $poolmap{$binfo->{type}});
|
---|
| 2315 | }
|
---|
| 2316 | } # clearall or clearpeer
|
---|
| 2317 |
|
---|
[729] | 2318 | } elsif ($args{scope} eq 'mergepeer') { # should this just be an else?
|
---|
[732] | 2319 | # Default case. Merge "peer" blocks, but keep all suballocations
|
---|
[733] | 2320 | # snag the new parent info for the return list
|
---|
| 2321 | push @retlist, {block => "$newblock", type => $disp_alloctypes{$args{newtype}}, id => $prime};
|
---|
[732] | 2322 | my $substh = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE parent_id = ?");
|
---|
| 2323 | my $delsth = $dbh->prepare("DELETE FROM allocations WHERE id = ?");
|
---|
| 2324 | # Reparent freeblocks in parent
|
---|
| 2325 | $fbreparentsth->execute($prime, $binfo->{master_id}, $binfo->{city}, $newcontainerclass, $binfo->{vrf},
|
---|
| 2326 | $binfo->{parent_id}, $newblock);
|
---|
| 2327 | # Loop over "peer" allocations to be merged
|
---|
| 2328 | while (my ($peercidr, $peer_id, $peertype, $m_id) = $peersth->fetchrow_array) {
|
---|
| 2329 | # Snag existing peer data since we may need it
|
---|
| 2330 | my $peerfull = getBlockData($dbh, $peer_id);
|
---|
| 2331 | # Reparent free blocks from existing containers
|
---|
| 2332 | $fbreparentsth->execute($prime, $binfo->{master_id}, $binfo->{city}, $newcontainerclass,
|
---|
| 2333 | $binfo->{vrf}, $peer_id, $newblock);
|
---|
| 2334 | # Reparent any subblocks from existing containers
|
---|
| 2335 | $substh->execute($prime, $peer_id);
|
---|
| 2336 | # Delete the old container
|
---|
| 2337 | $delsth->execute($peer_id) unless $peer_id == $prime;
|
---|
| 2338 | # Add new freeblocks for merged leaf blocks
|
---|
| 2339 | $insfbsth->execute($peercidr, $binfo->{city}, $newcontainerclass, $binfo->{vrf}, $binfo->{id},
|
---|
| 2340 | $binfo->{master_id}) if $peertype =~ /.[enr]/;
|
---|
| 2341 | # Convert pool IPs into allocations or aggregated free blocks
|
---|
[733] | 2342 | _poolToAllocations($dbh, $peerfull, $pinfo, newparent => $prime) if $peertype =~ /.[dp]/;
|
---|
[732] | 2343 | # Fix up master_id on free blocks if we're merging a master block
|
---|
| 2344 | $mfbsth->execute($binfo->{master_id}, $m_id) if $peertype eq 'mm';
|
---|
[733] | 2345 | # capture block for return
|
---|
| 2346 | push @retlist, { block => $peercidr, mtype => $disp_alloctypes{$peertype} };
|
---|
[732] | 2347 | } # merge peers
|
---|
| 2348 | # update the primary allocation info. Do this last so we don't stomp extra data-retrieval in the loop above
|
---|
| 2349 | $dbh->do("UPDATE allocations SET cidr = ?, type = ? WHERE id = ?", undef, ($newblock, $args{newtype}, $prime) );
|
---|
| 2350 |
|
---|
[729] | 2351 | } # scope
|
---|
| 2352 |
|
---|
| 2353 | # Clean up free blocks
|
---|
| 2354 | _compactFree($dbh, $prime);
|
---|
| 2355 |
|
---|
[728] | 2356 | } elsif ($args{newtype} =~ /.[dp]/) {
|
---|
| 2357 | ## Pool
|
---|
[737] | 2358 | # Snag the new parent info for the return list
|
---|
| 2359 | push @retlist, { block => "$newblock", type => $disp_alloctypes{$args{newtype}}, id => $prime };
|
---|
[728] | 2360 |
|
---|
[737] | 2361 | if ($args{scope} eq 'keepall') {
|
---|
| 2362 | # Convert all mergeable allocations and subs to chunks of pool IP assignments
|
---|
| 2363 | push @retlist, @{ _toPool($dbh, $prime, $newblock, $args{newtype}) };
|
---|
| 2364 |
|
---|
| 2365 | } elsif ($args{scope} =~ /^clear/) {
|
---|
| 2366 | # Clear it all out for a fresh (mostly?) empty IP pool
|
---|
| 2367 | while (my ($peercidr, $peer_id, $peertype, $m_id) = $peersth->fetchrow_array) {
|
---|
| 2368 | next if $peer_id == $prime;
|
---|
| 2369 | # Push existing allocations down a level before deleting,
|
---|
| 2370 | # so that when they're deleted the parent info is correct
|
---|
| 2371 | $reparentsth->execute($prime, $binfo->{master_id}, $peer_id);
|
---|
| 2372 | _deleteCascade($dbh, $peer_id, 0);
|
---|
| 2373 | # Capture block for return
|
---|
| 2374 | push @retlist, { block => $peercidr, mtype => $disp_alloctypes{$peertype} };
|
---|
| 2375 | }
|
---|
| 2376 | if ($args{scope} eq 'clearall') {
|
---|
| 2377 | # Delete any subs of $prime as well
|
---|
| 2378 | my $substh = $dbh->prepare("SELECT cidr,id FROM allocations WHERE parent_id = ?");
|
---|
| 2379 | $substh->execute($prime);
|
---|
| 2380 | while (my ($scidr, $s_id) = $substh->fetchrow_array) {
|
---|
| 2381 | _deleteCascade($dbh, $s_id);
|
---|
| 2382 | }
|
---|
| 2383 | } else {
|
---|
| 2384 | # Convert (subs of) self if not a leaf.
|
---|
| 2385 | _toPool($dbh, $prime, $newblock, $args{newtype}) unless $binfo->{type} =~ /.[enr]/;
|
---|
| 2386 | } # scope ne 'clearall'
|
---|
| 2387 |
|
---|
| 2388 | } elsif ($args{scope} eq 'mergepeer') {
|
---|
| 2389 | # Try to match behaviour from (target type == container) by deleting immediate peer leaf allocations
|
---|
| 2390 | while (my ($peercidr, $peer_id, $peertype, $m_id) = $peersth->fetchrow_array) {
|
---|
| 2391 | next if $peer_id == $prime; # don't delete the block we're turning into the pool allocation
|
---|
| 2392 | # Capture block for return
|
---|
| 2393 | push @retlist, { block => $peercidr, mtype => $disp_alloctypes{$peertype} };
|
---|
| 2394 | next unless $peertype =~ /.[enr]/;
|
---|
| 2395 | # Don't need _deleteCascade(), since we'll just be deleting the freshly
|
---|
| 2396 | # added free block a little later anyway
|
---|
| 2397 | $delsth->execute($peer_id);
|
---|
| 2398 | }
|
---|
| 2399 | # Convert self if not a leaf, to match behaviour with a container type as target
|
---|
| 2400 | _toPool($dbh, $prime, $newblock, $args{newtype}) unless $binfo->{type} =~ /.[enr]/;
|
---|
| 2401 | }
|
---|
| 2402 | # Update the primary allocation info.
|
---|
| 2403 | $dbh->do("UPDATE allocations SET cidr = ?, type = ? WHERE id = ?", undef, ($newblock, $args{newtype}, $prime) );
|
---|
| 2404 | # Delete any lingering free blocks
|
---|
| 2405 | $dbh->do("DELETE FROM freeblocks WHERE parent_id = ? AND cidr <<= ?", undef, $binfo->{parent_id}, $newblock);
|
---|
| 2406 | # Fix up the rest of the pool IPs
|
---|
| 2407 | my ($code,$msg) = initPool($dbh, $newblock, $args{newtype}, $binfo->{city},
|
---|
| 2408 | ($args{newtype} =~ /.p/ ? 'all' : 'normal'), $prime);
|
---|
| 2409 |
|
---|
[728] | 2410 | } elsif ($args{newtype} =~ /.[enr]/) {
|
---|
| 2411 | ## Leaf
|
---|
[738] | 2412 | # Merging to a leaf type of any kind is, pretty much be definition, scope == 'clearall'.
|
---|
| 2413 | # keepall, mergepeer, and clearpeer all imply keeping suballocations, where leaf allocations
|
---|
| 2414 | # by definition do not have suballocations.
|
---|
| 2415 | # Snag the new parent info for the return list
|
---|
| 2416 | push @retlist, {block => "$newblock", type => $disp_alloctypes{$args{newtype}}, id => $prime};
|
---|
| 2417 | while (my ($peercidr, $peer_id, $peertype, $m_id) = $peersth->fetchrow_array) {
|
---|
| 2418 | next if $peer_id == $prime;
|
---|
| 2419 | # Push existing allocations down a level before deleting,
|
---|
| 2420 | # so that when they're deleted the parent info is correct
|
---|
| 2421 | $reparentsth->execute($prime, $binfo->{master_id}, $peer_id);
|
---|
| 2422 | _deleteCascade($dbh, $peer_id, 0);
|
---|
| 2423 | # Capture block for return
|
---|
| 2424 | push @retlist, { block => $peercidr, mtype => $disp_alloctypes{$peertype} };
|
---|
| 2425 | }
|
---|
| 2426 | # Delete any subs of $prime as well
|
---|
| 2427 | my $substh = $dbh->prepare("SELECT cidr,id FROM allocations WHERE parent_id = ?");
|
---|
| 2428 | $substh->execute($prime);
|
---|
| 2429 | while (my ($scidr, $s_id) = $substh->fetchrow_array) {
|
---|
| 2430 | _deleteCascade($dbh, $s_id);
|
---|
| 2431 | }
|
---|
| 2432 | # Clean up lingering free blocks and pool IPs
|
---|
| 2433 | $dbh->do("DELETE FROM freeblocks WHERE cidr <<= ? AND (parent_id = ? OR parent_id = ?)", undef,
|
---|
| 2434 | $newblock, $binfo->{parent_id}, $prime);
|
---|
| 2435 | $dbh->do("DELETE FROM poolips WHERE parent_id = ? AND ip <<= ? ", undef,
|
---|
| 2436 | $prime, $newblock);
|
---|
[728] | 2437 |
|
---|
[738] | 2438 | } # $args{newtype} if()
|
---|
[728] | 2439 |
|
---|
| 2440 | $dbh->commit;
|
---|
| 2441 | };
|
---|
| 2442 | if ($@) {
|
---|
| 2443 | my $msg = $@;
|
---|
| 2444 | $errstr = $msg;
|
---|
| 2445 | $dbh->rollback;
|
---|
| 2446 | return ('FAIL',$msg);
|
---|
| 2447 | }
|
---|
| 2448 |
|
---|
[744] | 2449 | # Make the assumption that any change crossing /24 or /16 boundaries will not come out right. Reverse DNS
|
---|
| 2450 | # updates for this operation are already complex enough without handling those edge cases.
|
---|
| 2451 | # ... er, how do we detect this?
|
---|
| 2452 |
|
---|
| 2453 | if ($args{newtype} =~ /.[cm]/) {
|
---|
| 2454 |
|
---|
| 2455 | if ($args{scope} eq 'keepall') {
|
---|
| 2456 | # Add new rDNS for new container
|
---|
| 2457 | _rpc('addOrUpdateRevRec', cidr => $binfo->{block}, name => $binfo->{rdns}, rpcuser => $args{user});
|
---|
| 2458 |
|
---|
| 2459 | } else {
|
---|
| 2460 | # Resize rDNS template for $prime
|
---|
| 2461 | _rpc('resizeTemplate', oldcidr => "$binfo->{block}", newcidr => $newblock->network.'', rpcuser => $args{user});
|
---|
| 2462 |
|
---|
| 2463 | # Assemble a list of blocks to delete...
|
---|
| 2464 | my $cidrlist;
|
---|
| 2465 | foreach my $mblock (@retlist) {
|
---|
| 2466 | $cidrlist .= $mblock->{block}."," unless $mblock->{block} =~ $newblock;
|
---|
| 2467 | }
|
---|
| 2468 |
|
---|
| 2469 | # ... then make slight variant batch delete calls depending on the merge scope
|
---|
| 2470 | if ($args{scope} eq 'mergepeer') {
|
---|
| 2471 | # Delete separate rDNS for other peers
|
---|
| 2472 | $cidrlist =~ s/,$//;
|
---|
| 2473 | _rpc('delRevSet', cidrlist => $cidrlist, rpcuser => $args{user}, delforward => 'y', delsubs => 'n',
|
---|
| 2474 | parpatt => $pinfo->{rdns});
|
---|
| 2475 |
|
---|
| 2476 | } elsif ($args{scope} eq 'clearpeer') {
|
---|
| 2477 | # Delete all rDNS within other peers
|
---|
| 2478 | $cidrlist =~ s/,$//;
|
---|
| 2479 | _rpc('delRevSet', cidrlist => $cidrlist, rpcuser => $args{user}, delforward => 'y', delsubs => 'y',
|
---|
| 2480 | parpatt => $pinfo->{rdns})
|
---|
| 2481 |
|
---|
| 2482 | } elsif ($args{scope} eq 'clearall') {
|
---|
| 2483 | # Delete all other records within the new block
|
---|
| 2484 | $cidrlist .= $binfo->{block};
|
---|
| 2485 | _rpc('delRevSet', cidrlist => $cidrlist, rpcuser => $args{user}, delforward => 'y', delsubs => 'y',
|
---|
| 2486 | parpatt => $pinfo->{rdns});
|
---|
| 2487 |
|
---|
| 2488 | } # scope, second level
|
---|
| 2489 | } # scope, !keepall
|
---|
| 2490 |
|
---|
| 2491 | } # type grouping for rDNS calls
|
---|
| 2492 |
|
---|
| 2493 |
|
---|
[728] | 2494 | return \@retlist;
|
---|
| 2495 |
|
---|
| 2496 | } # end mergeBlocks()
|
---|
| 2497 |
|
---|
| 2498 |
|
---|
[93] | 2499 | ## IPDB::deleteBlock()
|
---|
| 2500 | # Removes an allocation from the database, including deleting IPs
|
---|
| 2501 | # from poolips and recombining entries in freeblocks if possible
|
---|
| 2502 | # Also handles "deleting" a static IP allocation, and removal of a master
|
---|
[558] | 2503 | # Requires a database handle, the block to delete, the routing depth (if applicable),
|
---|
[590] | 2504 | # the VRF ID, and a flag to indicate whether to delete associated forward DNS entries
|
---|
| 2505 | # as well as the reverse entry
|
---|
[93] | 2506 | sub deleteBlock {
|
---|
[638] | 2507 | my ($dbh,$id,$basetype,$delfwd,$user) = @_;
|
---|
[93] | 2508 |
|
---|
[638] | 2509 | # Collect info about the block we're going to delete
|
---|
| 2510 | my $binfo = getBlockData($dbh, $id, $basetype);
|
---|
| 2511 | my $cidr = new NetAddr::IP $binfo->{block};
|
---|
| 2512 |
|
---|
[558] | 2513 | # For possible auto-VRF-ignoring (since public IPs shouldn't usually be present in more than one VRF)
|
---|
| 2514 | # is_rfc1918 requires NetAddr::IP >= 4.059
|
---|
| 2515 | # rather than doing this over and over and over.....
|
---|
| 2516 | my $tmpnum = $cidr->numeric;
|
---|
| 2517 | # 192.168.0.0/16 -> 192.168.255.255 => 3232235520 -> 3232301055
|
---|
| 2518 | # 172.16.0.0/12 -> 172.31.255.255 => 2886729728 -> 2887778303
|
---|
| 2519 | # 10.0.0.0/8 -> 10.255.255.255 => 167772160 -> 184549375
|
---|
| 2520 | my $isprivnet = (3232235520 <= $tmpnum && $tmpnum <= 3232301055) ||
|
---|
| 2521 | (2886729728 <= $tmpnum && $tmpnum <= 2887778303) ||
|
---|
| 2522 | (167772160 <= $tmpnum && $tmpnum <= 184549375);
|
---|
| 2523 |
|
---|
[93] | 2524 | my $sth;
|
---|
| 2525 |
|
---|
[349] | 2526 | # Magic variables used for odd allocation cases.
|
---|
| 2527 | my $container;
|
---|
| 2528 | my $con_type;
|
---|
| 2529 |
|
---|
[558] | 2530 |
|
---|
| 2531 | # temporarily forced null, until a sane UI for VRF tracking can be found.
|
---|
[638] | 2532 | # $vrf = '';# if !$vrf; # as with SQL, the null value is not equal to ''. *sigh*
|
---|
[558] | 2533 |
|
---|
[93] | 2534 | # To contain the error message, if any.
|
---|
[558] | 2535 | my $msg = "Unknown error deallocating $binfo->{type} $cidr";
|
---|
| 2536 | my $goback; # to put the parent in so we can link back where the deallocate started
|
---|
| 2537 |
|
---|
[93] | 2538 | # Enable transactions and exception-on-errors... but only for this sub
|
---|
| 2539 | local $dbh->{AutoCommit} = 0;
|
---|
| 2540 | local $dbh->{RaiseError} = 1;
|
---|
| 2541 |
|
---|
[558] | 2542 | if ($binfo->{type} =~ /^.i$/) {
|
---|
[638] | 2543 | # First case. The "block" is a static IP
|
---|
| 2544 | # Note that we still need some additional code in the odd case
|
---|
| 2545 | # of a netblock-aligned contiguous group of static IPs
|
---|
[93] | 2546 |
|
---|
| 2547 | eval {
|
---|
[558] | 2548 | $msg = "Unable to deallocate $disp_alloctypes{$binfo->{type}} $cidr";
|
---|
[638] | 2549 | my $pinfo = getBlockData($dbh, $binfo->{parent_id}, 'b');
|
---|
[558] | 2550 | ##fixme: VRF and rdepth
|
---|
[638] | 2551 | $dbh->do("UPDATE poolips SET custid = ?, available = 'y',".
|
---|
| 2552 | "city = (SELECT city FROM allocations WHERE id = ?),".
|
---|
| 2553 | "description = '', notes = '', circuitid = '', vrf = ? WHERE id = ?", undef,
|
---|
| 2554 | ($pinfo->{custid}, $binfo->{parent_id}, $pinfo->{vrf}, $id) );
|
---|
[93] | 2555 | $dbh->commit;
|
---|
| 2556 | };
|
---|
| 2557 | if ($@) {
|
---|
[558] | 2558 | $msg .= ": $@";
|
---|
[93] | 2559 | eval { $dbh->rollback; };
|
---|
| 2560 | return ('FAIL',$msg);
|
---|
| 2561 | } else {
|
---|
[590] | 2562 | ##fixme: RPC return code?
|
---|
[638] | 2563 | _rpc('delByCIDR', cidr => "$cidr", user => $user, delforward => $delfwd, rpcuser => $user);
|
---|
[93] | 2564 | return ('OK',"OK");
|
---|
| 2565 | }
|
---|
| 2566 |
|
---|
[558] | 2567 | } elsif ($binfo->{type} eq 'mm') { # end alloctype =~ /.i/
|
---|
[638] | 2568 | # Second case. The block is a full master block
|
---|
[93] | 2569 |
|
---|
[558] | 2570 | ##fixme: VRF limit
|
---|
[93] | 2571 | $msg = "Unable to delete master block $cidr";
|
---|
| 2572 | eval {
|
---|
[638] | 2573 | $dbh->do("DELETE FROM allocations WHERE cidr <<= ? AND master_id = ?", undef, ($cidr, $binfo->{master_id}) );
|
---|
| 2574 | $dbh->do("DELETE FROM freeblocks WHERE cidr <<= ? AND master_id = ?", undef, ($cidr, $binfo->{master_id}) );
|
---|
[93] | 2575 | $dbh->commit;
|
---|
| 2576 | };
|
---|
| 2577 | if ($@) {
|
---|
[558] | 2578 | $msg .= ": $@";
|
---|
[93] | 2579 | eval { $dbh->rollback; };
|
---|
| 2580 | return ('FAIL', $msg);
|
---|
[591] | 2581 | }
|
---|
| 2582 |
|
---|
| 2583 | # Have to handle potentially split reverse zones. Assume they *are* split,
|
---|
| 2584 | # since if we added them here, they would have been added split.
|
---|
| 2585 | # allow splitting reverse zones to be disabled, maybe, someday
|
---|
| 2586 | #if ($splitrevzones && !$cidr->{isv6}) {
|
---|
| 2587 | my @zonelist;
|
---|
| 2588 | if (1 && !$cidr->{isv6}) {
|
---|
| 2589 | my $splitpoint = ($cidr->masklen <= 16 ? 16 : 24); # hack pthui
|
---|
| 2590 | @zonelist = $cidr->split($splitpoint);
|
---|
[93] | 2591 | } else {
|
---|
[591] | 2592 | @zonelist = ($cidr);
|
---|
[93] | 2593 | }
|
---|
[591] | 2594 | my @fails;
|
---|
| 2595 | foreach my $subzone (@zonelist) {
|
---|
[638] | 2596 | if ($rpc_url && !_rpc('delZone', zone => "$subzone", revrec => 'y', rpcuser => $user, delforward => $delfwd) ) {
|
---|
[591] | 2597 | push @fails, ("$subzone" => $errstr);
|
---|
| 2598 | }
|
---|
| 2599 | }
|
---|
| 2600 | if (@fails) {
|
---|
| 2601 | return ('WARN',"Warning(s) deleting $cidr from reverse DNS:\n".join("\n", @fails));
|
---|
| 2602 | }
|
---|
| 2603 | return ('OK','OK');
|
---|
[93] | 2604 |
|
---|
| 2605 | } else { # end alloctype master block case
|
---|
| 2606 |
|
---|
| 2607 | ## This is a big block; but it HAS to be done in a chunk. Any removal
|
---|
| 2608 | ## of a netblock allocation may result in a larger chunk of free
|
---|
| 2609 | ## contiguous IP space - which may in turn be combined into a single
|
---|
| 2610 | ## netblock rather than a number of smaller netblocks.
|
---|
| 2611 |
|
---|
[558] | 2612 | my $retcode = 'OK';
|
---|
[638] | 2613 | my ($ptype,$pcity,$ppatt,$p_id);
|
---|
[558] | 2614 |
|
---|
[93] | 2615 | eval {
|
---|
| 2616 |
|
---|
[558] | 2617 | ##fixme: add recursive flag to allow "YES DAMMIT DELETE ALL EVARYTHING!!1!!" without
|
---|
| 2618 | # explicitly deleting any suballocations of the block to be deleted.
|
---|
[93] | 2619 |
|
---|
[638] | 2620 | # get parent info of the block we're deleting
|
---|
| 2621 | my $pinfo = getBlockData($dbh, $binfo->{parent_id});
|
---|
| 2622 | $ptype = $pinfo->{type};
|
---|
| 2623 | $pcity = $pinfo->{city};
|
---|
| 2624 | $ppatt = $pinfo->{rdns};
|
---|
| 2625 | $p_id = $binfo->{parent_id};
|
---|
[93] | 2626 |
|
---|
[558] | 2627 | # Delete the block
|
---|
[638] | 2628 | $dbh->do("DELETE FROM allocations WHERE id = ?", undef, ($id) );
|
---|
[349] | 2629 |
|
---|
[558] | 2630 | # munge the parent type a little
|
---|
[638] | 2631 | $ptype = (split //, $ptype)[1];
|
---|
[93] | 2632 |
|
---|
[558] | 2633 | ##fixme: you can't... CAN NOT.... assign the same public IP to multiple things.
|
---|
| 2634 | # 'Net don't work like that, homey. Restrict VRF-uniqueness to private IPs?
|
---|
| 2635 | # -> $isprivnet flag from start of sub
|
---|
[93] | 2636 |
|
---|
[558] | 2637 | # check to see if any container allocations could be the "true" parent
|
---|
[638] | 2638 | my ($tparent,$tpar_id,$trtype,$tcity);
|
---|
| 2639 | $tpar_id = 0;
|
---|
[404] | 2640 |
|
---|
[638] | 2641 | ##fixme: this is far simpler in the strict VRF case; we "know" that any allocation
|
---|
| 2642 | # contained by a container is a part of the same allocation tree when the VRF fields are equal.
|
---|
[558] | 2643 |
|
---|
[638] | 2644 | # logic:
|
---|
| 2645 | # For each possible container of $cidr
|
---|
| 2646 | # note the parent id
|
---|
| 2647 | # walk the chain up the parents
|
---|
| 2648 | # if we intersect $cidr's current parent, break
|
---|
| 2649 | # if we've intersected $cidr's current parent
|
---|
| 2650 | # set some variables to track that block
|
---|
| 2651 | # break
|
---|
[558] | 2652 |
|
---|
[638] | 2653 | # Set up part of "is it in the middle of a pool?" check
|
---|
| 2654 | my $wuzpool = $dbh->selectrow_hashref("SELECT cidr,parent_id,type,city,custid,id FROM allocations ".
|
---|
| 2655 | "WHERE (type LIKE '_d' OR type LIKE '_p') AND cidr >> ? AND master_id = ?", { Slice => {} },
|
---|
| 2656 | ($cidr, $binfo->{master_id}) );
|
---|
[558] | 2657 |
|
---|
[638] | 2658 | ##fixme?
|
---|
| 2659 | # edge cases not handled, or handled badly:
|
---|
| 2660 | # -> $cidr managed to get to be the entirety of an IP pool
|
---|
[558] | 2661 |
|
---|
[638] | 2662 | if ($wuzpool && $wuzpool->{id} != $id) {
|
---|
| 2663 | # we have legacy goo to be purified
|
---|
| 2664 | # going to ignore nested pools; not possible to create them via API and no current legacy data includes any.
|
---|
| 2665 |
|
---|
| 2666 | # for convenience
|
---|
| 2667 | my $poolid = $wuzpool->{id};
|
---|
| 2668 | my $pool = $wuzpool->{cidr};
|
---|
| 2669 | my $poolcity = $wuzpool->{city};
|
---|
| 2670 | my $pooltype = $wuzpool->{type};
|
---|
| 2671 | my $poolcustid = $wuzpool->{custid};
|
---|
| 2672 |
|
---|
| 2673 | $retcode = 'WARNPOOL';
|
---|
| 2674 | $goback = "$poolid,$pool";
|
---|
| 2675 | # We've already deleted the block, now we have to stuff its IPs into the pool.
|
---|
| 2676 | $pooltype =~ s/[dp]$/i/; # change type to static IP
|
---|
| 2677 | my $sth2 = $dbh->prepare("INSERT INTO poolips (ip,city,type,custid,parent_id) VALUES ".
|
---|
| 2678 | "(?,'$poolcity','$pooltype','$poolcustid',$poolid)");
|
---|
[655] | 2679 |
|
---|
[429] | 2680 | ##fixme: need to not insert net, gateway, and bcast on "real netblock" pools (DHCPish)
|
---|
[638] | 2681 | # don't insert .0
|
---|
| 2682 | $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|;
|
---|
[655] | 2683 | $cidr++;
|
---|
| 2684 | my $bcast = $cidr->broadcast;
|
---|
| 2685 | while ($cidr != $bcast) {
|
---|
| 2686 | $sth2->execute($cidr->addr);
|
---|
| 2687 | $cidr++;
|
---|
[638] | 2688 | }
|
---|
| 2689 | # don't insert .255
|
---|
| 2690 | $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.255$|;
|
---|
[655] | 2691 |
|
---|
| 2692 | # Weirdness Happens. $cidr goes read-only somewhere (this is a thing?!?),
|
---|
| 2693 | # causing ->split, ->hostenum, and related methods to explode. O_o
|
---|
| 2694 | # foreach my $ip ($cidr->hostenum) {
|
---|
| 2695 | # $sth2->execute($ip);
|
---|
| 2696 | # }
|
---|
| 2697 |
|
---|
[638] | 2698 | }
|
---|
[93] | 2699 |
|
---|
[638] | 2700 | ## important!
|
---|
| 2701 | # ... or IS IT?
|
---|
| 2702 | # we may have undef'ed $wuzpool above, if the allocation tree $cidr is in doesn't intersect the pool we found
|
---|
| 2703 | #if (!$wuzpool) {
|
---|
| 2704 |
|
---|
| 2705 | else {
|
---|
| 2706 |
|
---|
[655] | 2707 | # Edge case: Block is the same size as more than one parent level. Should be rare.
|
---|
| 2708 | # - mainly master + first routing. Sorting on parent_id hides the problem pretty well,
|
---|
| 2709 | # but it's likely still possible to fail in particularly well-mangled databases.
|
---|
| 2710 | # The ultimate fix for this may be to resurrect the "routing depth" atrocity. :/
|
---|
[638] | 2711 | # Get all possible (and probably a number of impossible) containers for $cidr
|
---|
| 2712 | $sth = $dbh->prepare("SELECT cidr,parent_id,type,city,id FROM allocations ".
|
---|
| 2713 | "WHERE (type LIKE '_m' OR type LIKE '_c') AND cidr >>= ? AND master_id = ? ".
|
---|
[655] | 2714 | "ORDER BY masklen(cidr) DESC,parent_id DESC");
|
---|
[638] | 2715 | $sth->execute($cidr, $binfo->{master_id});
|
---|
| 2716 |
|
---|
| 2717 | # Quickly get certain fields (simpler than getBlockData()
|
---|
| 2718 | my $sth2 = $dbh->prepare("SELECT cidr,parent_id,type,city FROM allocations ".
|
---|
| 2719 | "WHERE (type LIKE '_m' OR type LIKE '_c') AND id = ? AND master_id = ?");
|
---|
| 2720 |
|
---|
| 2721 | # For each possible container of $cidr...
|
---|
| 2722 | while (my @data = $sth->fetchrow_array) {
|
---|
| 2723 | my $i = 0;
|
---|
| 2724 | # Save some state and set a start point - parent ID of container we're checking
|
---|
| 2725 | $tparent = $data[0];
|
---|
| 2726 | my $ppid = $data[1];
|
---|
| 2727 | $trtype = $data[2];
|
---|
| 2728 | $tcity = $data[3];
|
---|
| 2729 | $tpar_id = $data[4];
|
---|
| 2730 | last if $data[4] == $binfo->{parent_id}; # Preemptively break if we're already in the right place
|
---|
| 2731 | last if $ppid == $binfo->{parent_id}; # ... or if the parent of the container is the block's parent
|
---|
| 2732 | while (1) {
|
---|
| 2733 | # Retrieve bits on that parent ID
|
---|
| 2734 | $sth2->execute($ppid, $binfo->{master_id});
|
---|
| 2735 | my @container = $sth2->fetchrow_array;
|
---|
| 2736 | $ppid = $container[1];
|
---|
| 2737 | last if $container[1] == 0; # Break if we've hit a master block
|
---|
| 2738 | last if $ppid == $binfo->{parent_id}; # Break if we've reached the block $cidr is currently in
|
---|
| 2739 | }
|
---|
| 2740 | last if $ppid == $binfo->{parent_id};
|
---|
| 2741 | }
|
---|
| 2742 |
|
---|
| 2743 | # found an alternate parent; reset some parent-info bits
|
---|
| 2744 | if ($tpar_id != $binfo->{parent_id}) {
|
---|
| 2745 | $ptype = (split //, $trtype)[1];
|
---|
| 2746 | $pcity = $tcity;
|
---|
| 2747 | $retcode = 'WARNMERGE'; # may be redundant
|
---|
| 2748 | $p_id = $tpar_id;
|
---|
| 2749 | }
|
---|
| 2750 |
|
---|
| 2751 | $goback = "$p_id,$tparent"; # breadcrumb, currently only used in case of live-parent-is-not-true-parent
|
---|
| 2752 |
|
---|
| 2753 | # Special case - delete pool IPs
|
---|
| 2754 | if ($binfo->{type} =~ /^.[pd]$/) {
|
---|
[655] | 2755 | # We have to delete the IPs from the pool listing.
|
---|
| 2756 | ##fixme: rdepth? vrf?
|
---|
[638] | 2757 | $dbh->do("DELETE FROM poolips WHERE parent_id = ?", undef, ($id) );
|
---|
| 2758 | }
|
---|
| 2759 |
|
---|
| 2760 | $pinfo = getBlockData($dbh, $p_id);
|
---|
| 2761 |
|
---|
[558] | 2762 | # If the block wasn't legacy goo embedded in a static pool, we check the
|
---|
| 2763 | # freeblocks in the identified parent to see if we can combine any of them.
|
---|
[93] | 2764 |
|
---|
[559] | 2765 | # if the block to be deleted is a container, move its freeblock(s) up a level, and reset their parenting info
|
---|
| 2766 | if ($binfo->{type} =~ /^.[mc]/) {
|
---|
| 2767 | # move the freeblocks into the parent
|
---|
| 2768 | # we don't insert a new freeblock because there could be a live reparented sub.
|
---|
[638] | 2769 | $dbh->do("UPDATE freeblocks SET parent_id = ?, routed = ?, city = ? WHERE parent_id = ?", undef,
|
---|
| 2770 | ($p_id, $ptype, $pcity, $id) );
|
---|
[559] | 2771 | } else {
|
---|
| 2772 | # ... otherwise, add the freeblock
|
---|
[638] | 2773 | $dbh->do("INSERT INTO freeblocks (cidr, city, routed, parent_id, master_id) VALUES (?,?,?,?,?)", undef,
|
---|
| 2774 | ($cidr, $pcity, $ptype, $p_id, $binfo->{master_id}) );
|
---|
[559] | 2775 | }
|
---|
| 2776 |
|
---|
[715] | 2777 | # Walk the free blocks in the parent and reduce them to the minimal set of CIDR ranges necessary
|
---|
| 2778 | _compactFree($dbh, $p_id);
|
---|
[428] | 2779 |
|
---|
| 2780 | } # done returning IPs to the appropriate place
|
---|
[404] | 2781 |
|
---|
[93] | 2782 | # If we got here, we've succeeded. Whew!
|
---|
| 2783 | $dbh->commit;
|
---|
| 2784 | }; # end eval
|
---|
| 2785 | if ($@) {
|
---|
[558] | 2786 | $msg .= ": $@";
|
---|
[93] | 2787 | eval { $dbh->rollback; };
|
---|
| 2788 | return ('FAIL', $msg);
|
---|
| 2789 | } else {
|
---|
[590] | 2790 | ##fixme: RPC return code?
|
---|
[638] | 2791 | _rpc('delByCIDR', cidr => "$cidr", rpcuser => $user, delforward => $delfwd, delsubs => 'y', parpatt => $ppatt);
|
---|
[558] | 2792 | return ($retcode, $goback);
|
---|
[93] | 2793 | }
|
---|
| 2794 |
|
---|
| 2795 | } # end alloctype != netblock
|
---|
| 2796 |
|
---|
| 2797 | } # end deleteBlock()
|
---|
| 2798 |
|
---|
| 2799 |
|
---|
[370] | 2800 | ## IPDB::getBlockData()
|
---|
[557] | 2801 | # Get CIDR or IP, custid, type, city, circuit ID, description, notes, modification time,
|
---|
| 2802 | # private/restricted data, for a CIDR block or pool IP
|
---|
| 2803 | # Also returns SWIP status flag for CIDR blocks or pool netblock for IPs
|
---|
[636] | 2804 | # Takes the block ID or IP to look up and an optional flag to indicate a pool IP lookup
|
---|
| 2805 | # instead of a netblock.
|
---|
[557] | 2806 | # Returns a hashref to the block data
|
---|
[370] | 2807 | sub getBlockData {
|
---|
| 2808 | my $dbh = shift;
|
---|
[636] | 2809 | my $id = shift;
|
---|
| 2810 | my $type = shift || 'b'; # default to netblock for lazy callers
|
---|
[370] | 2811 |
|
---|
[636] | 2812 | # netblocks are in the allocations table; pool IPs are in the poolips table.
|
---|
| 2813 | # If we try to look up a CIDR in an integer field we should just get back nothing.
|
---|
| 2814 | my ($btype) = $dbh->selectrow_array("SELECT type FROM allocations WHERE id=?", undef, ($id) );
|
---|
[534] | 2815 |
|
---|
[692] | 2816 | # Note city, vrf, parent_id and master_id removed due to JOIN uncertainty for block allocations
|
---|
| 2817 | my $commonfields = q(custid, type, circuitid, description, notes, modifystamp AS lastmod,
|
---|
| 2818 | privdata, vlan, rdns);
|
---|
| 2819 |
|
---|
[636] | 2820 | if ($type eq 'i') {
|
---|
[692] | 2821 | my $binfo = $dbh->selectrow_hashref(qq(
|
---|
[726] | 2822 | SELECT id, ip AS block, city, vrf, parent_id, master_id, $commonfields
|
---|
[692] | 2823 | FROM poolips WHERE id = ?
|
---|
| 2824 | ), undef, ($id) );
|
---|
[557] | 2825 | return $binfo;
|
---|
| 2826 | } else {
|
---|
[692] | 2827 | my $binfo = $dbh->selectrow_hashref(qq(
|
---|
[726] | 2828 | SELECT a.id, a.cidr AS block, a.city, a.vrf, a.parent_id, a.master_id, swip, $commonfields,
|
---|
[692] | 2829 | f.cidr AS reserve, f.id as reserve_id
|
---|
| 2830 | FROM allocations a LEFT JOIN freeblocks f ON a.id=f.reserve_for
|
---|
| 2831 | WHERE a.id = ?
|
---|
| 2832 | ), undef, ($id) );
|
---|
[557] | 2833 | return $binfo;
|
---|
[534] | 2834 | }
|
---|
[370] | 2835 | } # end getBlockData()
|
---|
| 2836 |
|
---|
| 2837 |
|
---|
[585] | 2838 | ## IPDB::getBlockRDNS()
|
---|
| 2839 | # Gets reverse DNS pattern for a block or IP. Note that this will also
|
---|
| 2840 | # retrieve any default pattern following the parent chain up, and check via
|
---|
| 2841 | # RPC (if available) to see what the narrowest pattern for the requested block is
|
---|
| 2842 | # Returns the current pattern for the block or IP.
|
---|
| 2843 | sub getBlockRDNS {
|
---|
| 2844 | my $dbh = shift;
|
---|
| 2845 | my %args = @_;
|
---|
| 2846 |
|
---|
[637] | 2847 | $args{type} = 'b' if !$args{type};
|
---|
[675] | 2848 | my $cached = 1;
|
---|
[585] | 2849 |
|
---|
[637] | 2850 | # snag entry from database
|
---|
| 2851 | my ($rdns,$rfrom,$pid);
|
---|
| 2852 | if ($args{type} =~ /.i/) {
|
---|
| 2853 | ($rdns, $rfrom, $pid) = $dbh->selectrow_array("SELECT rdns,ip,parent_id FROM poolips WHERE id = ?",
|
---|
| 2854 | undef, ($args{id}) );
|
---|
| 2855 | } else {
|
---|
| 2856 | ($rdns, $rfrom, $pid) = $dbh->selectrow_array("SELECT rdns,cidr,parent_id FROM allocations WHERE id = ?",
|
---|
| 2857 | undef, ($args{id}) );
|
---|
| 2858 | }
|
---|
[585] | 2859 |
|
---|
[637] | 2860 | # Can't see a way this could end up empty, for any case I care about. If the caller
|
---|
| 2861 | # doesn't know an allocation ID to request, then they don't know anything else anyway.
|
---|
| 2862 | my $selfblock = $rfrom;
|
---|
[585] | 2863 |
|
---|
[637] | 2864 | my $type;
|
---|
| 2865 | while (!$rdns && $pid) {
|
---|
| 2866 | ($rdns, $rfrom, $pid, $type) = $dbh->selectrow_array(
|
---|
| 2867 | "SELECT rdns,cidr,parent_id,type FROM allocations WHERE id = ?",
|
---|
| 2868 | undef, ($pid) );
|
---|
| 2869 | last if $type eq 'mm'; # break loops in unfortunate legacy data
|
---|
| 2870 | }
|
---|
| 2871 |
|
---|
| 2872 | # use the actual allocation to check against the DNS utility; we don't want
|
---|
| 2873 | # to always go chasing up the chain to the master... which may (usually won't)
|
---|
| 2874 | # be present directly in DNS anyway
|
---|
| 2875 | my $cidr = new NetAddr::IP $selfblock;
|
---|
| 2876 |
|
---|
[585] | 2877 | if ($rpc_url) {
|
---|
| 2878 | # Use the first /16 or /24, rather than dithering over which sub-/14 /16
|
---|
| 2879 | # or sub-/19 /24 to retrieve - it's the least-wrong way to do things.
|
---|
| 2880 |
|
---|
[586] | 2881 | my ($rpcblock) = ($cidr->masklen <= 24 ? $cidr->split( ($cidr->masklen <= 16 ? 16 : 24) ) : $cidr);
|
---|
[585] | 2882 | my %rpcargs = (
|
---|
| 2883 | rpcuser => $args{user},
|
---|
| 2884 | group => $revgroup, # not sure how this could sanely be exposed, tbh...
|
---|
| 2885 | cidr => "$rpcblock",
|
---|
| 2886 | );
|
---|
| 2887 |
|
---|
[637] | 2888 | my $remote_rdns = _rpc('getRevPattern', %rpcargs);
|
---|
| 2889 | $rdns = $remote_rdns if $remote_rdns;
|
---|
[675] | 2890 | $cached = 0;
|
---|
[585] | 2891 | }
|
---|
| 2892 |
|
---|
| 2893 | # hmm. do we care about where it actually came from?
|
---|
[675] | 2894 | return $rdns, $cached;
|
---|
[585] | 2895 | } # end getBlockRDNS()
|
---|
| 2896 |
|
---|
| 2897 |
|
---|
[675] | 2898 | ## IPDB::getRDNSbyIP()
|
---|
| 2899 | # Get individual reverse entries for the IP or CIDR IP range passed. Sort of looking the
|
---|
| 2900 | # opposite direction down the netblock tree compared to getBlockRDNS() above.
|
---|
| 2901 | sub getRDNSbyIP {
|
---|
| 2902 | my $dbh = shift;
|
---|
| 2903 | my %args = @_; # We want to accept a variety of call types
|
---|
| 2904 |
|
---|
| 2905 | # key arguments: allocation ID, type
|
---|
| 2906 | unless ($args{id} || $args{type}) {
|
---|
| 2907 | $errstr = 'Missing allocation ID or type';
|
---|
| 2908 | return;
|
---|
| 2909 | }
|
---|
| 2910 |
|
---|
| 2911 | my @ret = ();
|
---|
| 2912 | # special case: single IP. Check if it's an allocation or in a pool, then do the RPC call for fresh data.
|
---|
| 2913 | if ($args{type} =~ /^.i$/) {
|
---|
| 2914 | my ($ip, $localrev) = $dbh->selectrow_array("SELECT ip, rdns FROM poolips WHERE id = ?", undef, ($args{id}) );
|
---|
| 2915 | push @ret, { 'r_ip' => $ip, 'iphost' => $localrev };
|
---|
| 2916 | } else {
|
---|
| 2917 | if ($rpc_url) {
|
---|
| 2918 | my %rpcargs = (
|
---|
| 2919 | rpcuser => $args{user},
|
---|
| 2920 | group => $revgroup, # not sure how this could sanely be exposed, tbh...
|
---|
| 2921 | cidr => $args{range},
|
---|
| 2922 | );
|
---|
| 2923 |
|
---|
| 2924 | my $remote_rdns = _rpc('getRevSet', %rpcargs);
|
---|
| 2925 | return $remote_rdns;
|
---|
| 2926 | # $rdns = $remote_rdns if $remote_rdns;
|
---|
| 2927 | # $cached = 0;
|
---|
| 2928 | }
|
---|
| 2929 | }
|
---|
| 2930 | return \@ret;
|
---|
| 2931 | } # end getRDNSbyIP()
|
---|
| 2932 |
|
---|
| 2933 |
|
---|
[519] | 2934 | ## IPDB::getNodeList()
|
---|
| 2935 | # Gets a list of node ID+name pairs as an arrayref to a list of hashrefs
|
---|
| 2936 | sub getNodeList {
|
---|
| 2937 | my $dbh = shift;
|
---|
| 2938 |
|
---|
| 2939 | my $ret = $dbh->selectall_arrayref("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id",
|
---|
| 2940 | { Slice => {} });
|
---|
| 2941 | return $ret;
|
---|
| 2942 | } # end getNodeList()
|
---|
| 2943 |
|
---|
| 2944 |
|
---|
[530] | 2945 | ## IPDB::getNodeName()
|
---|
| 2946 | # Get node name from the ID
|
---|
| 2947 | sub getNodeName {
|
---|
| 2948 | my $dbh = shift;
|
---|
| 2949 | my $nid = shift;
|
---|
| 2950 |
|
---|
| 2951 | my ($nname) = $dbh->selectrow_array("SELECT node_name FROM nodes WHERE node_id = ?", undef, ($nid) );
|
---|
| 2952 | return $nname;
|
---|
| 2953 | } # end getNodeName()
|
---|
| 2954 |
|
---|
| 2955 |
|
---|
| 2956 | ## IPDB::getNodeInfo()
|
---|
| 2957 | # Get node name and ID associated with a block
|
---|
| 2958 | sub getNodeInfo {
|
---|
| 2959 | my $dbh = shift;
|
---|
| 2960 | my $block = shift;
|
---|
| 2961 |
|
---|
| 2962 | my ($nid, $nname) = $dbh->selectrow_array("SELECT nodes.node_id,node_name FROM nodes INNER JOIN noderef".
|
---|
| 2963 | " ON nodes.node_id=noderef.node_id WHERE noderef.block = ?", undef, ($block) );
|
---|
| 2964 | return ($nid, $nname);
|
---|
| 2965 | } # end getNodeInfo()
|
---|
| 2966 |
|
---|
| 2967 |
|
---|
[77] | 2968 | ## IPDB::mailNotify()
|
---|
[66] | 2969 | # Sends notification mail to recipients regarding an IPDB operation
|
---|
[416] | 2970 | sub mailNotify {
|
---|
| 2971 | my $dbh = shift;
|
---|
| 2972 | my ($action,$subj,$message) = @_;
|
---|
[66] | 2973 |
|
---|
[462] | 2974 | return if $smtphost eq 'smtp.example.com'; # do nothing if still using default SMTP host.
|
---|
| 2975 |
|
---|
[422] | 2976 | ##fixme: need to redesign the breakdown/processing for $action for proper handling of all cases
|
---|
| 2977 |
|
---|
[416] | 2978 | # split action into parts for fiddlement. nb: there are almost certainly better ways to do this.
|
---|
[422] | 2979 | my @actionbits = split //, $action;
|
---|
[416] | 2980 |
|
---|
| 2981 | # want to notify anyone who has specifically requested notify on *this* type ($action as passed),
|
---|
| 2982 | # on "all static IP types" or "all pool types" (and other last-char-in-type groupings), on eg "all DSL types",
|
---|
| 2983 | # and "all events with this action"
|
---|
| 2984 | my @actionsets = ($action);
|
---|
| 2985 | ##fixme: ick, eww. really gotta find a better way to handle this...
|
---|
| 2986 | push @actionsets, ($actionbits[0].'.'.$actionbits[2],
|
---|
| 2987 | $actionbits[0].$actionbits[1].'.', $actionbits[0].'a') if $action =~ /^.{3}$/;
|
---|
| 2988 |
|
---|
| 2989 | my $mailer = Net::SMTP->new($smtphost, Hello => "ipdb.$domain");
|
---|
| 2990 |
|
---|
| 2991 | # get recip list from db
|
---|
| 2992 | my $sth = $dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
|
---|
| 2993 |
|
---|
[443] | 2994 | my %reciplist;
|
---|
[416] | 2995 | foreach (@actionsets) {
|
---|
[426] | 2996 | $sth->execute($_);
|
---|
[416] | 2997 | ##fixme - need to handle db errors
|
---|
| 2998 | my ($recipsub) = $sth->fetchrow_array;
|
---|
| 2999 | next if !$recipsub;
|
---|
| 3000 | foreach (split(/,/, $recipsub)) {
|
---|
[443] | 3001 | $reciplist{$_}++;
|
---|
[416] | 3002 | }
|
---|
| 3003 | }
|
---|
| 3004 |
|
---|
[443] | 3005 | return if !%reciplist;
|
---|
[420] | 3006 |
|
---|
[443] | 3007 | foreach my $recip (keys %reciplist) {
|
---|
[681] | 3008 | $mailer->mail($smtpsender);
|
---|
[416] | 3009 | $mailer->to($recip);
|
---|
[681] | 3010 | $mailer->data("From: \"$org_name IP Database\" <$smtpsender>\n",
|
---|
[135] | 3011 | "To: $recip\n",
|
---|
[69] | 3012 | "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
|
---|
| 3013 | "Subject: {IPDB} $subj\n",
|
---|
| 3014 | "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n",
|
---|
[417] | 3015 | "Organization: $org_name\n",
|
---|
[69] | 3016 | "\n$message\n");
|
---|
[416] | 3017 | }
|
---|
[66] | 3018 | $mailer->quit;
|
---|
| 3019 | }
|
---|
| 3020 |
|
---|
[4] | 3021 | # Indicates module loaded OK. Required by Perl.
|
---|
| 3022 | 1;
|
---|