source: trunk/cgi-bin/IPDB.pm@ 919

Last change on this file since 919 was 915, checked in by Kris Deugau, 7 years ago

/trunk

Start adding a generalized in-your-face infonotice space to attach warnings
to a netblock. See #17 and #23, sort of.

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