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