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
Line 
1# ipdb/cgi-bin/IPDB.pm
2# Contains functions for IPDB - database access, subnet mangling, block allocation, etc
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###
9# Copyright (C) 2004-2016 - Kris Deugau
10
11package IPDB;
12
13use strict;
14use warnings;
15use Exporter;
16use DBI;
17use Net::SMTP;
18use NetAddr::IP qw(:lower Compact );
19use Frontier::Client;
20use POSIX;
21use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
22
23$VERSION = 3; ##VERSION##
24@ISA = qw(Exporter);
25@EXPORT_OK = qw(
26 %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist
27 %IPDBacl %merge_display %aclmsg %rpcacl $maxfcgi
28 $errstr
29 &initIPDBGlobals &connectDB &finish &checkDBSanity
30 &addVRF &getVRF &deleteVRF &addMaster &touchMaster
31 &listVRF &listSummary &listSubs &listContainers &listAllocations &listForMerge &listFree &listPool
32 &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
33 &ipParent &subParent &blockParent &getBreadCrumbs &getRoutedCity
34 &allocateBlock &updateBlock &splitBlock &shrinkBlock &mergeBlocks &deleteBlock &getBlockData
35 &getBlockRDNS &getRDNSbyIP &getRevID
36 &getNodeList &getNodeName &getNodeInfo
37 &getBlockNotices
38 &mailNotify
39 );
40
41@EXPORT = (); # Export nothing by default.
42%EXPORT_TAGS = ( ALL => [qw(
43 %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist
44 %IPDBacl %merge_display %aclmsg %rpcacl $maxfcgi
45 $errstr
46 &initIPDBGlobals &connectDB &finish &checkDBSanity
47 &addVRF &getVRF &deleteVRF &addMaster &touchMaster
48 &listVRF &listSummary &listSubs &listContainers &listAllocations &listForMerge &listFree &listPool
49 &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
50 &ipParent &subParent &blockParent &getBreadCrumbs &getRoutedCity
51 &allocateBlock &updateBlock &splitBlock &shrinkBlock &mergeBlocks &deleteBlock &getBlockData
52 &getBlockRDNS &getRDNSbyIP &getRevID
53 &getNodeList &getNodeName &getNodeInfo
54 &getBlockNotices
55 &mailNotify
56 )]
57 );
58
59##
60## Global variables
61##
62our %disp_alloctypes;
63our %list_alloctypes;
64our %def_custids;
65our @citylist;
66our @poplist;
67our %IPDBacl;
68
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
72# Backup fields, since we iterate over the set regularly
73our @backupfields = qw(brand model type src user vpass epass port ip);
74
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
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',
89 mergeblock => 'merge allocations',
90 );
91
92our %rpcacl;
93our $maxfcgi = 3;
94
95# error reporting
96our $errstr = '';
97
98our $org_name = 'Example Corp';
99our $smtphost = 'smtp.example.com';
100our $domain = 'example.com';
101our $defcustid = '5554242';
102our $smtpsender = 'ipdb@example.com';
103# mostly for rwhois
104##fixme: leave these blank by default?
105our $rwhoisDataPath = '/usr/local/rwhoisd/etc/rwhoisd'; # to match ./configure defaults from rwhoisd-1.5.9.6
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';
113our $org_email = 'noc@example.com';
114our $hostmaster = 'dns@example.com';
115
116our $syslog_facility = 'local2';
117
118our $rpc_url = '';
119our $dnsadmin_url; # needs to be modified later
120our $revgroup = 1; # should probably be configurable somewhere
121our $rpccount = 0;
122
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
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
132# UI layout for subblocks/containers
133our $sublistlayout = 1;
134
135# UI layout for VRF/master blocks
136our $masterswithvrfs = 2;
137
138# VLAN validation mode. Set to 0 to allow alphanumeric vlan names instead of using the vlan number.
139our $numeric_vlan = 1;
140
141# Billing system return link
142our $billinglink = 'https://billing.example.com/radius.pl';
143
144# Append the default domain on unqualified DNS names?
145our $append_domain = 1;
146
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',
164# must be provided by caller's caller
165# rpcuser => $args{user},
166 %args,
167 );
168
169 eval {
170 $result = $server->call("dnsdb.$rpcsub", %rpcargs);
171 };
172 if ($@) {
173 $errstr = $@;
174 $errstr =~ s/\s*$//;
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
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);
192 my $ftype = (split //, $pinfo->{type})[0];
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
233 $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id) VALUES (?,?,?,?,?,?)");
234 foreach my $cme (@combinelist) {
235 next if grep { $cme == $_ } @rawfb; # skip if the combined block was in the raw list
236 $sth->execute($cme, $pinfo->{city}, $ftype, $pinfo->{vrf}, $parent, $pinfo->{master_id});
237 }
238
239} # end _compactFree()
240
241
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;
251 my $retall = shift || 0;
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 (
274 ip,type,parent_id,master_id,available,
275 city,description,notes,circuitid,createstamp,modifystamp,privdata,custid,vrf,vlan,rdns
276 )
277 VALUES (?,?,?,?,'n',?,?,?,?,?,?,?,?,?,?,?)
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 }
303 $inssth->execute($newip, $newtype, $poolparent, $master, @oldalloc) if !$poolcounter{"$newip"};
304 $poolcounter{"$newip"}++;
305 }
306 } elsif ($oldtype =~ /.[dp]/) {
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
317 push @retlist, { block => $oldcidr, mdisp => $disp_alloctypes{$oldtype}, mtype => $oldtype }
318 if (($oldparent == $mainparent) || $retall) && $oldid != $poolparent;
319 } # while $asth->fetch
320
321 return \@retlist;
322} # end _toPool()
323
324
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
395## IPDB::_deleteCascade()
396# Internal sub. Deletes an allocation and all subcomponents
397sub _deleteCascade {
398 my $dbh = shift;
399 my $id = shift;
400 my $createfb = shift; # may be null at this point
401
402 my $binfo = getBlockData($dbh, $id);
403
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
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
490 # Insert a new free block if needed
491 if ($createfb) {
492 my $pinfo = getBlockData($dbh, $createfb);
493 my $pt = (split //, $pinfo->{type})[1];
494 $dbh->do("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id) VALUES (?,?,?,?,?,?)", undef,
495 $binfo->{block}, $pinfo->{city}, $pt, $createfb, $pinfo->{vrf}, $binfo->{master_id});
496 }
497
498##todo: and hey! bonus! we can return @dellist, or something (%cidrlist{@dellist})
499
500} # end _deleteCascade()
501
502
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
531##
532## Public subs
533##
534
535
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
542 # Initialize alloctypes hashes
543 $sth = $dbh->prepare("select type,listname,dispname,listorder,def_custid from alloctypes order by listorder");
544 $sth->execute;
545 while (my @data = $sth->fetchrow_array) {
546 $disp_alloctypes{$data[0]} = $data[2];
547 $def_custids{$data[0]} = $data[4];
548 if ($data[3] < 900) {
549 $list_alloctypes{$data[0]} = $data[1];
550 }
551 }
552
553 # City and POP listings
554 $sth = $dbh->prepare("select city,routing from cities order by city");
555 $sth->execute;
556 return (undef,$sth->errstr) if $sth->err;
557 while (my @data = $sth->fetchrow_array) {
558 push @citylist, $data[0];
559 if ($data[1] eq 'y') {
560 push @poplist, $data[0];
561 }
562 }
563
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;
567 return (undef,$sth->errstr) if $sth->err;
568 while (my @data = $sth->fetchrow_array) {
569 $IPDBacl{$data[0]} = $data[1];
570 }
571
572##fixme: initialize HTML::Template env var for template path
573# something like $self->path().'/templates' ?
574# $ENV{HTML_TEMPLATE_ROOT} = 'foo/bar';
575
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
581 return (1,"OK");
582} # end initIPDBGlobals
583
584
585## IPDB::connectDB()
586# Creates connection to IPDB.
587# Requires the database name, username, and password.
588# Returns a handle to the db.
589# Set up for a PostgreSQL db; could be any transactional DBMS with the
590# right changes.
591sub connectDB {
592 my $dbname = shift;
593 my $user = shift;
594 my $pass = shift;
595 my $dbhost = shift;
596
597 my $dbh;
598 my $DSN = "DBI:Pg:".($dbhost ? "host=$dbhost;" : '')."dbname=$dbname";
599
600# Note that we want to autocommit by default, and we will turn it off locally as necessary.
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);
607
608# Return here if we can't select. Note that this indicates a
609# problem executing the select.
610 my $sth = $dbh->prepare("select type from alloctypes");
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");
621} # end connectDB
622
623
624## IPDB::finish()
625# Cleans up after database handles and so on.
626# Requires a database handle
627sub finish {
628 my $dbh = $_[0];
629 $dbh->disconnect if $dbh;
630} # end finish
631
632
633## IPDB::checkDBSanity()
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 {
637 my ($dbh) = $_[0];
638
639 if (!$dbh) {
640 print "No database handle, or connection has been closed.";
641 return -1;
642 } else {
643 # it connects, try a stmt.
644 my $sth = $dbh->prepare("select type from alloctypes");
645 my $err = $sth->execute();
646
647 if ($sth->fetchrow()) {
648 # all is well.
649 return 1;
650 } else {
651 print "Connected to the database, but could not execute test statement. ".$sth->errstr();
652 return -1;
653 }
654 }
655 # Clean up after ourselves.
656# $dbh->disconnect;
657} # end checkDBSanity
658
659
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
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
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
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;
735 # warning! during testing, this somehow generated a "Bad file descriptor" error. O_o
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
741 my %args = @_;
742
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
749 my $mid;
750
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 {
758 # First check - does the master exist in this VRF?
759 my ($mcontained) = $dbh->selectrow_array("SELECT cidr FROM allocations WHERE cidr >>= ? AND type = 'mm' AND vrf = ?",
760 undef, ($cidr, $args{vrf}) );
761 die "Master block $mcontained already exists and entirely contains $cidr\n"
762 if $mcontained;
763
764 # Second check - does the new master contain an existing one or ones?
765 my ($mexist) = $dbh->selectrow_array("SELECT cidr FROM allocations WHERE cidr <<= ? AND type = 'mm' AND vrf = ?",
766 undef, ($cidr, $args{vrf}) );
767
768 if (!$mexist) {
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"?
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')");
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.
779 $dbh->do("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id) VALUES (?,?,?,?,?,?)", undef,
780 ($cidr, '<NULL>', 'm', $mid, $args{vrf}, $mid) );
781
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
785 # If we get here, everything is happy. Commit changes.
786 $dbh->commit;
787
788 } # done new master does not contain existing master(s)
789 else {
790
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
799 # collect the master(s) we're going to absorb, and snag the longest netmask while we're at it.
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.
803 my $smallmask = $cidr->masklen;
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});
806 my @cmasters;
807 my @oldmids;
808 while (my @data = $sth->fetchrow_array) {
809 my $master = new NetAddr::IP $data[0];
810 push @cmasters, $master;
811 push @oldmids, $data[1];
812 $smallmask = $master->masklen if $master->masklen > $smallmask;
813 }
814
815 # update existing DNS refs. do this unconditionally Just In Case
816 $dbh->do("UPDATE dnsavail SET parent_alloc = ? WHERE zone << ?".
817 " AND parent_alloc IN (".join(',', @oldmids).")",
818 undef, $mid, $cidr);
819
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
830##fixme: master_id
831 # collect the unrouted free blocks within the new master
832 $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE masklen(cidr) <= ? AND cidr <<= ? AND routed = 'm' AND master_id IN (".join(',',@oldmids).")");
833 $sth->execute($smallmask, $cidr);
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
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).")");
844
845 # insert new combined freeblocks.
846 my $sth2 = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id)".
847 " VALUES (?,'<NULL>','m',?,?,?)");
848 foreach my $newblock (@blocklist) {
849 $sth->execute($newblock);
850 $sth2->execute($newblock, $mid, $args{vrf}, $mid);
851 }
852
853 # Update immediate allocations, and remove the old parents
854 $sth2 = $dbh->prepare("DELETE FROM allocations WHERE id = ?");
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 = ?");
860 foreach my $old (@oldmids) {
861 $sth2->execute($old);
862 $fbfix->execute($mid, $old);
863 $fbfix2->execute($mid, $old);
864 $allocfix->execute($mid, $old);
865 $allocfix2->execute($mid, $old);
866 }
867
868 # *whew* If we got here, we likely suceeded.
869 $dbh->commit;
870
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 {
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
899 my $dasth = $dbh->prepare("INSERT INTO dnsavail (zone,location,parent_alloc) VALUES (?,?,?)");
900 foreach my $subzone (@zonelist) {
901 my %rpcargs = (
902 rpcuser => $args{user},
903 revzone => "$subzone",
904 revpatt => $args{rdns},
905 defloc => $args{defloc},
906 group => $revgroup, # not sure how these two could sanely be exposed, tbh...
907 state => 1, # could make them globally configurable maybe
908 );
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 }
916 }
917 }
918 if (@fails) {
919 $errstr = "Warning(s) adding $cidr to reverse DNS:\n".join("\n", @fails);
920 return ('WARN',$mid);
921 }
922 }
923 return ('OK',$mid);
924 }
925} # end addMaster
926
927
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 {
938 $dbh->do("UPDATE allocations SET modifystamp=now() WHERE id = ?", undef, ($master));
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
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
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;
967 my $vrf = shift;
968
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);
972
973 foreach (@{$mlist}) {
974 my ($rcnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? AND type='rm' AND master_id = ?",
975 undef, ($$_{master}, $$_{id}));
976 $$_{routed} = $rcnt;
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}));
980 $$_{allocated} = $acnt;
981 my ($fcnt) = $dbh->selectrow_array("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?",
982 undef, ($$_{master}, $$_{id}));
983 $$_{free} = $fcnt;
984 my ($bigfree) = $dbh->selectrow_array("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
985 " AND master_id = ? ORDER BY masklen(cidr) LIMIT 1", undef, ($$_{master}, $$_{id}));
986##fixme: should find a way to do this without having to HTMLize the <>
987 $bigfree = "/$bigfree" if $bigfree;
988 $bigfree = '<NONE>' if !$bigfree;
989 $$_{bigfree} = $bigfree;
990 }
991 return $mlist;
992} # end listSummary()
993
994
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
1007 my $sth = $dbh->prepare("SELECT masklen(cidr),cidr,city,type,custid,swip,description,vrf,id,master_id".
1008 " FROM allocations WHERE parent_id = ? ORDER BY cidr");
1009 $sth->execute($args{parent});
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
1015 # snag some more details
1016 my $substh = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
1017 "AND type ~ '[mc]\$' AND master_id = ? AND NOT cidr = ? ");
1018 my $alsth = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
1019 "AND NOT type='rm' AND NOT type='mm' AND master_id = ? AND NOT id = ?");
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 <<= ?".
1022 " AND master_id = ? ORDER BY masklen(cidr)");
1023
1024 my @blocklist;
1025 while (my ($blockmask,$cidr,$city,$type,$custid,$swip,$desc,$vrf,$id,$mid) = $sth->fetchrow_array()) {
1026 $desc .= " - vrf:$vrf" if $desc && $vrf;
1027 $desc = "vrf:$vrf" if !$desc && $vrf;
1028 $custsth->execute($custid);
1029 my ($ncust) = $custsth->fetchrow_array();
1030 $substh->execute($cidr, $mid, $cidr);
1031 my ($cont) = $substh->fetchrow_array();
1032 $alsth->execute($cidr, $mid, $id);
1033 my ($alloc) = $alsth->fetchrow_array();
1034 $freesth->execute($cidr, $mid);
1035 my ($free) = $freesth->fetchrow_array();
1036 $lfreesth->execute($cidr, $mid);
1037
1038 # make sure we capture the first one separately...
1039 my ($lfree) = $lfreesth->fetchrow_array();
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;
1049 $lfree = "/$lfree" if $lfree;
1050 $lfree = '<NONE>' if !$lfree;
1051
1052 my %row = (
1053 block => $cidr,
1054 subfree => $free,
1055 lfree => $lfree,
1056 city => $city,
1057 type => $disp_alloctypes{$type},
1058 custid => $custid,
1059 desc => $desc,
1060 hassubs => ($type eq 'rm' || $type =~ /.c/ ? 1 : 0),
1061 id => $id,
1062 );
1063# $row{subblock} = ($type =~ /^.r$/); # hmf. wonder why these won't work in the hash declaration...
1064 $row{listpool} = ($type =~ /^.[pd]$/);
1065 # only set this on suitable types (pools, containers)
1066 $row{pctfree} = "$num_ipfree IPs, $pctfree" if $type =~ /^.[cpdm]/;
1067 push (@blocklist, \%row);
1068 }
1069 return \@blocklist;
1070} # end listSubs()
1071
1072
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
1088 my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,vrf,id,master_id".
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 <<= ? ".
1093 "AND NOT type='rm' AND NOT type='mm' AND master_id = ? AND NOT id = ?");
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;
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;
1102 $alsth->execute($cidr, $mid, $id);
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
1137 my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,vrf,id,master_id".
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;
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;
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
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]$/;
1177 my $incsub = shift;
1178 $incsub = 1 if !defined($incsub);
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
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);
1203 return $ret;
1204 }
1205 return;
1206} # end listForMerge()
1207
1208
1209## IPDB::listFree()
1210# Gets a list of free blocks in the requested parent/master and VRF instance in both CIDR and range notation
1211# Takes a parent/master ID and an optional VRF specifier that defaults to empty.
1212# Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks
1213# Returns some extra flags in the hashrefs for routed blocks, since those can have several subtypes
1214sub listFree {
1215 my $dbh = shift;
1216
1217 my %args = @_;
1218 # Just In Case
1219 $args{vrf} = '' if !$args{vrf};
1220
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 ) );
1228# $sth->execute($args{parent}, $args{vrf});
1229 $sth->execute($args{parent});
1230 my @flist;
1231 while (my ($cidr,$id,$resv) = $sth->fetchrow_array()) {
1232 $cidr = new NetAddr::IP $cidr;
1233 my %row = (
1234 fblock => "$cidr",
1235 frange => $cidr->range,
1236 fbid => $id,
1237 fbparent => $args{parent},
1238 resv => $resv,
1239 );
1240 push @flist, \%row;
1241 }
1242 return \@flist;
1243} # end listFree()
1244
1245
1246## IPDB::listPool()
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.
1253sub listPool {
1254 my $dbh = shift;
1255 my $pool = shift;
1256 my $incdesc = shift;
1257 $incdesc = 1 if !defined($incdesc); # extra flag to include description and "deleteme" flag
1258
1259 my $sth = $dbh->prepare("SELECT ip,custid,available,description,type,id".
1260 " FROM poolips WHERE parent_id = ? ORDER BY ip");
1261 $sth->execute($pool);
1262 my @poolips;
1263 while (my ($ip,$custid,$available,$desc,$type,$id) = $sth->fetchrow_array) {
1264 my %row = (
1265 ip => $ip,
1266 custid => $custid,
1267 available => $available,
1268 parent => $pool,
1269 id => $id,
1270 );
1271 if ($incdesc) {
1272 $row{desc} = $desc;
1273 $row{delme} = ($available eq 'n');
1274 }
1275 push @poolips, \%row;
1276 }
1277 return \@poolips;
1278} # end listPool()
1279
1280
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
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 => {} });
1292 return $mlist;
1293} # end getMasterList()
1294
1295
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
1304 my $seltype = shift || '';
1305
1306 my $sql = "SELECT type,listname,type=? AS sel FROM alloctypes WHERE listorder <= 500";
1307 if ($tgroup eq 'n') {
1308 # grouping 'n' - all netblock types. These include routed blocks, containers (_c)
1309 # and contained (_r) types, dynamic-allocation ranges (_e), static IP pools (_d and _p),
1310 # and the "miscellaneous" cn, in, and en types.
1311 # Or in other words, everything but master and static IP types.
1312 $sql .= " AND type NOT LIKE '_i'";
1313 } elsif ($tgroup eq 'p') {
1314 # grouping 'p' - primary allocation types. As with 'n' above but without the _r contained types.
1315 $sql .= " AND type NOT LIKE '_i' AND type NOT LIKE '_r'";
1316 } elsif ($tgroup eq 'c') {
1317 # grouping 'c' - contained types. These include all static IPs and all _r types.
1318 $sql .= " AND (type LIKE '_i' OR type LIKE '_r')";
1319 } elsif ($tgroup eq 'i') {
1320 # grouping 'i' - static IP types.
1321 $sql .= " AND type LIKE '_i'";
1322 } else {
1323 # grouping 'a' - all standard allocation types. This includes everything
1324 # but mm (present only as a formality). Make this the default.
1325 # ... whee! no extra WHERE clauses
1326 }
1327 $sql .= " ORDER BY listorder";
1328 my $tlist = $dbh->selectall_arrayref($sql, { Slice => {} }, $seltype);
1329 return $tlist;
1330}
1331
1332
1333## IPDB::getPoolSelect()
1334# Get a list of pools matching the passed city and type that have 1 or more free IPs
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.
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
1346 my $plist = $dbh->selectall_arrayref( q(
1347 SELECT a.id as poolid,count(*) AS poolfree,a.cidr AS poolblock, a.city AS poolcit
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 ?
1351 GROUP BY a.id,a.cidr,a.city
1352 ORDER BY a.cidr
1353 ),
1354 { Slice => {} }, ($pcity, $ptype) );
1355 return $plist;
1356} # end getPoolSelect()
1357
1358
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
1367# Returns a 3-element list with the free block ID, CIDR, and parent ID matching the criteria, if any
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
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
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
1393 @vallist = ($maskbits);
1394 $sql = "SELECT id,cidr,parent_id FROM freeblocks WHERE masklen(cidr) <= ?";
1395
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
1415##fixme: config or UI flag for "Strict" mode
1416# if ($strictmode) {
1417if (0) {
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 }
1426}
1427
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;
1435 }
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 }
1452 # Keep "reserved" blocks out of automatic assignment.
1453##fixme: needs a UI flag or a config knob
1454 $sql .= " AND reserve_for = 0";
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
1460
1461 my ($fbid,$fbfound,$fbparent) = $dbh->selectrow_array($sql, undef, @vallist);
1462 return $fbid,$fbfound,$fbparent;
1463} # end findAllocateFrom()
1464
1465
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".
1475 " WHERE cidr >>= ? AND (type LIKE '_p' OR type LIKE '_d')", undef, ($block) );
1476 return $pinfo;
1477} # end ipParent()
1478
1479
1480## IPDB::subParent()
1481# Get a block's parent's details
1482# Takes a database handle and CIDR block
1483# Returns a hashref to the parent container block, if any
1484sub subParent {
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;
1491} # end subParent()
1492
1493
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
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;
1513 my $parent = shift || 0;
1514 my $vrf = shift; # if we're not browsing into netblocks yet, caller can pass the VRF for breadcrumbs
1515 my @result;
1516
1517 my $sth = $dbh-> prepare("SELECT cidr,type,id,parent_id,vrf FROM allocations WHERE id=?");
1518
1519 while ($parent != 0) {
1520 $sth->execute($parent);
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) };
1524 $parent = $pid;
1525 }
1526
1527 push @result, { cidr => "vrf:$vrf", link => $vrf, isvrf => 1 }
1528 if $vrf;
1529
1530 return \@result;
1531} # end getBreadCrumbs()
1532
1533
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
1545## IPDB::allocateBlock()
1546# Does all of the magic of actually allocating a netblock
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
1550# Returns a success code and optional error message.
1551sub allocateBlock {
1552 my $dbh = shift;
1553
1554 my %args = @_;
1555
1556 if ($args{cidr} eq 'Single static IP') {
1557 $args{cidr} = '';
1558 } else {
1559 $args{cidr} = new NetAddr::IP $args{cidr};
1560 }
1561
1562 $args{desc} = $args{description} if $args{description};
1563 $args{desc} = '' if !$args{desc};
1564 $args{notes} = '' if !$args{notes};
1565 $args{circid} = '' if !$args{circid};
1566 $args{privdata} = '' if !$args{privdata};
1567##fixme: VRF should trickle down like master_id
1568 $args{vrf} = '' if !$args{vrf};
1569 $args{vlan} = '' if !$args{vlan};
1570 $args{rdns} = '' if !$args{rdns};
1571
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
1576 $args{user} = $args{rpcuser} if !$args{user};
1577
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
1580 if ($args{cidr} && $args{cidr}->{isv6} && $args{rdns} =~ /\%/) {
1581 return ('FAIL','Reverse DNS template patterns are not supported for IPv6 allocations');
1582 }
1583
1584 my $sth;
1585
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;
1591 return ('FAIL',"Failed to allocate $args{cidr}; intended free block was used by another allocation.")
1592 if ($args{type} !~ /.i/ && !$fbparent);
1593##fixme: fail here if !$alloc_from
1594# also consider "lock for allocation" due to multistep allocation process
1595
1596 # To contain the error message, if any.
1597 my $msg = "Unknown error allocating $args{cidr} as '$disp_alloctypes{$args{type}}'";
1598
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.
1602
1603 if ($args{type} =~ /^.i$/) {
1604 $msg = "Unable to assign static IP $args{cidr} to $args{custid}";
1605 eval {
1606##fixme: IP pools across VRFs, need to use the IP ID instead of the CIDR
1607# ... or the VRF itself?
1608 if ($args{cidr}) { # IP specified
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}) );
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
1617 ($args{cidr}) = $dbh->selectrow_array("SELECT ip FROM poolips WHERE parent_id=? AND available='y' ORDER BY ip",
1618 undef, ($args{parent}) );
1619 }
1620
1621 # backup
1622 my $backupid = 0;
1623 if ($args{backup}) {
1624 my $bksql = "INSERT INTO backuplist (";
1625 my @bkvals;
1626 my @bkfields;
1627 for my $bk (@backupfields) {
1628 if ($args{"bk$bk"}) {
1629 push @bkfields, "bk$bk";
1630 push @bkvals, $args{"bk$bk"};
1631 }
1632 }
1633 $bksql .= join(',',@bkfields).") VALUES (".join(',', map {'?'} @bkfields).")";
1634 $dbh->do($bksql, undef, @bkvals);
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 = ?, ".
1640 "circuitid = ?, privdata = ?, rdns = ?, backup_id = ? ".
1641 "WHERE ip = ? AND parent_id = ?", undef,
1642 ($args{custid}, $args{city}, $args{desc}, $args{notes},
1643 $args{circid}, $args{privdata}, $args{rdns}, $backupid,
1644 $args{cidr}, $args{parent}) );
1645
1646# node hack
1647 if ($args{nodeid} && $args{nodeid} ne '') {
1648 $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
1649 }
1650# end node hack
1651
1652 $dbh->commit; # Allocate IP from pool
1653 };
1654 if ($@) {
1655 $msg .= ": $@";
1656 eval { $dbh->rollback; };
1657 return ('FAIL', $msg);
1658 } else {
1659 # Snag the pool info
1660 my $pinfo = getBlockData($dbh, $args{parent});
1661 # Only try to update rDNS when the pool is flagged as "rDNS available"
1662 if ($pinfo->{revavail} || $pinfo->{revpartial}) {
1663 _rpc('addOrUpdateRevRec', cidr => "$args{cidr}", name => $args{rdns}, rpcuser => $args{user},
1664 location => $pinfo->{location});
1665 }
1666 return ('OK', $args{cidr});
1667 }
1668
1669 } else { # end IP-from-pool allocation
1670
1671 if ($args{cidr} == $alloc_from) {
1672 # Easiest case- insert in one table, delete in the other, and go home. More or less.
1673
1674 eval {
1675 $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
1676
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
1699 # Insert the allocations entry
1700 $dbh->do("INSERT INTO allocations ".
1701 "(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns,backup_id)".
1702 " VALUES (?,?,?,(SELECT vrf FROM allocations WHERE id=?),?,?,?,?,?,?,?,?,?,?)", undef,
1703 ($args{cidr}, $fbparent, $fbmaster, $fbmaster, $args{vlan}, $args{custid}, $args{type}, $args{city},
1704 $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}, $backupid) );
1705 my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
1706
1707 # Munge freeblocks
1708 if ($args{type} =~ /^(.)[mc]$/) {
1709 # special case - block is a routed or container/"reserve" block
1710 my $rtype = $1;
1711 $dbh->do("UPDATE freeblocks SET routed = ?,city = ?,parent_id = ? WHERE id = ?",
1712 undef, ($rtype, $args{city}, $bid, $args{fbid}) );
1713 } else {
1714 # "normal" case
1715 $dbh->do("DELETE FROM freeblocks WHERE id = ?", undef, ($args{fbid}) );
1716 }
1717
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}";
1723 my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $bid);
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}";
1727 my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $bid);
1728 die $rmsg if $code eq 'FAIL';
1729 }
1730
1731# node hack
1732 if ($args{nodeid} && $args{nodeid} ne '') {
1733 $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
1734 }
1735# end node hack
1736
1737 $dbh->commit; # Simple block allocation
1738 }; # end of eval
1739 if ($@) {
1740 $msg .= ": ".$@;
1741 eval { $dbh->rollback; };
1742 return ('FAIL',$msg);
1743 }
1744
1745 } else { # cidr != alloc_from
1746
1747 # Hard case. Allocation is smaller than free block.
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});
1752 my $wantmaskbits = $args{cidr}->masklen;
1753 my $maskbits = $alloc_from->masklen;
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;
1761 my $tmp_from = $alloc_from; # So we don't munge $args{alloc_from}
1762 while ($maskbits++ < $wantmaskbits) {
1763 my @subblocks = $tmp_from->split($maskbits);
1764 $newfreeblocks[$i++] = (($args{cidr}->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
1765 $tmp_from = ( ($args{cidr}->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
1766 } # while
1767
1768 # Begin SQL transaction block
1769 eval {
1770 $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
1771
1772 # Delete old freeblocks entry
1773 $dbh->do("DELETE FROM freeblocks WHERE id = ?", undef, ($args{fbid}) );
1774
1775 # Insert the allocations entry
1776 $dbh->do("INSERT INTO allocations ".
1777 "(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns)".
1778 " VALUES (?,?,?,(SELECT vrf FROM allocations WHERE id=?),?,?,?,?,?,?,?,?,?)", undef,
1779 ($args{cidr}, $fbparent, $fbmaster, $fbmaster, $args{vlan}, $args{custid}, $args{type}, $args{city},
1780 $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
1781 my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
1782
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,
1789 ($args{reserve} && $block->masklen == $wantmaskbits ? $bid : 0));
1790 }
1791
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;
1795 $sth->execute($args{cidr}, $args{city}, $rtype, $args{vrf}, $bid, $fbmaster, 0);
1796 }
1797
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}";
1803 my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $bid);
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}";
1807 my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $bid);
1808 die $rmsg if $code eq 'FAIL';
1809 }
1810
1811# node hack
1812 if ($args{nodeid} && $args{nodeid} ne '') {
1813 $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
1814 }
1815# end node hack
1816
1817 $dbh->commit; # Complex block allocation
1818 }; # end eval
1819 if ($@) {
1820 $msg .= ": ".$@;
1821 eval { $dbh->rollback; };
1822 return ('FAIL',$msg);
1823 }
1824
1825 } # end fullcidr != alloc_from
1826
1827 # Snag the parent info
1828 my $pinfo = getBlockData($dbh, $fbparent);
1829 # Only try to update rDNS when the block is flagged as "rDNS available"
1830 if (($pinfo->{revavail} || $pinfo->{revpartial}) && ($args{rdns} || $args{iprev})) {
1831 # the netblock/allocation...
1832 _rpc('addOrUpdateRevRec', cidr => "$args{cidr}", name => $args{rdns}, rpcuser => $args{user},
1833 location => $pinfo->{location});
1834 # ...and the per-IP set, if there is one.
1835 _rpc('updateRevSet', %{$args{iprev}}, rpcuser => $args{user}, location => $pinfo->{location})
1836 if keys (%{$args{iprev}});
1837 }
1838
1839 return ('OK', 'OK');
1840
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
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()
1853sub initPool {
1854 my ($dbh,undef,$type,$city,$class,$parent) = @_;
1855 my $pool = new NetAddr::IP $_[1];
1856
1857 # IPv6 does not lend itself to IP pools as supported
1858 return ('FAIL',"Refusing to create IPv6 static IP pool\n") if $pool->{isv6};
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).
1861 return ('FAIL',"Refusing to create oversized static IP pool\n") if $pool->masklen <= 20;
1862
1863 # Retrieve some odds and ends for defaults on the IPs
1864 my ($pcustid) = $dbh->selectrow_array("SELECT def_custid FROM alloctypes WHERE type=?", undef, ($type) );
1865 my ($vrf,$vlan,$master) = $dbh->selectrow_array("SELECT vrf,vlan,master_id FROM allocations WHERE id = ?",
1866 undef, ($parent) );
1867
1868 $type =~ s/[pd]$/i/;
1869 my $sth;
1870 my $msg;
1871
1872 eval {
1873 # have to insert all pool IPs into poolips table as "unallocated".
1874 $sth = $dbh->prepare("INSERT INTO poolips (ip,custid,city,type,parent_id,master_id,vrf) VALUES (?,?,?,?,?,?,?)");
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
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
1902 # enumerate the hosts in the IP range - everything except the first (net) and last (bcast) IP
1903 my @poolip_list = $pool->hostenum;
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++) {
1908 my $baseip = $poolip_list[$i]->addr;
1909 if ($baseip !~ /\.(?:0|255)$/ && !$foundips{$poolip_list[$i]}) {
1910 $sth->execute($baseip, $pcustid, $city, $type, $parent, $master, $vrf);
1911 }
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.
1916 if ($class eq 'all') { # (DSL-ish block - *all* IPs available
1917 if ($pool->addr !~ /\.0$/) { # .0 causes weirdness.
1918 $sth->execute($pool->addr, $pcustid, $city, $type, $parent, $master, $vrf)
1919 unless $foundips{$pool->addr."/32"};
1920 }
1921 $sth->execute($poolip_list[0]->addr, $pcustid, $city, $type, $parent, $master, $vrf)
1922 unless $foundips{$poolip_list[0]};
1923 $pool--;
1924 if ($pool->addr !~ /\.255$/) { # .255 can cause weirdness.
1925 $sth->execute($pool->addr, $pcustid, $city, $type, $parent, $master, $vrf)
1926 unless $foundips{$pool->addr."/32"};
1927 }
1928 }
1929# don't commit here! the caller may not be done.
1930# $dbh->commit;
1931 };
1932 if ($@) {
1933 $msg = $@;
1934# Don't roll back! It's up to the caller to handle this.
1935# eval { $dbh->rollback; };
1936 return ('FAIL',$msg);
1937 } else {
1938 return ('OK',"OK");
1939 }
1940} # end initPool()
1941
1942
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
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
1956 # do it all in a transaction
1957 local $dbh->{AutoCommit} = 0;
1958 local $dbh->{RaiseError} = 1;
1959
1960 my @fieldlist;
1961 my @vallist;
1962 foreach ('custid', 'city', 'description', 'notes', 'circuitid', 'privdata', 'rdns', 'vrf', 'vlan') {
1963 if ($args{$_}) {
1964 push @fieldlist, $_;
1965 push @vallist, $args{$_};
1966 }
1967 }
1968
1969 my $binfo;
1970 my $updtable = 'allocations';
1971 my $keyfield = 'id';
1972 if ($args{type} =~ /^(.)i$/) {
1973 $updtable = 'poolips';
1974 $binfo = getBlockData($dbh, $args{block}, 'i');
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 }
1980 } else {
1981## fixme: there's got to be a better way...
1982 $binfo = getBlockData($dbh, $args{block});
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
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
2008 eval {
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
2029 # backup
2030 if (!defined($args{ignorebk})) {
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};
2041 }
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";
2058 }
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;
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...
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 }
2092 }
2093 } # $binfo->{hasbk} defined
2094 } # if !args{ignorebk}
2095
2096 # append another SQL fragment
2097 push @vallist, $args{block};
2098 $sql .= " = ? WHERE $keyfield = ?";
2099
2100##fixme: don't do the update on pool IPs if the IP is available and assignIP_on_update is not set
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
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 '--';
2109 }
2110
2111 $dbh->commit;
2112 };
2113 if ($@) {
2114 my $msg = $@;
2115 $dbh->rollback;
2116 return ('FAIL', $msg);
2117 }
2118
2119 # Do RPC rDNS call, if available.
2120 # Snag the parent info
2121 my $pinfo = getBlockData($dbh, $binfo->{parent_id});
2122 # Return early if rDNS flag(s) are not set
2123 return ('OK','OK') unless ($pinfo->{revavail} || $pinfo->{revpartial});
2124
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
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
2150 _rpc('updateRevSet', %fbset, rpcuser => $args{user}, location => $pinfo->{location});
2151
2152 } else {
2153 $binfo->{block} =~ s{/(?:32|128)$}{};
2154 # Only insert a record for IPv4, or actual single v6 IPs
2155 _rpc('addOrUpdateRevRec', cidr => $binfo->{block}, name => $args{rdns}, rpcuser => $args{user},
2156 location => $pinfo->{location})
2157 if !$cidr->{isv6} || ($cidr->{isv6} && $cidr->masklen == 128);
2158
2159 # and the per-IP set, if there is one.
2160 _rpc('updateRevSet', cidr => $binfo->{block}, %{$args{iprev}}, rpcuser => $args{user},
2161 location => $pinfo->{location})
2162 if keys (%{$args{iprev}});
2163
2164 # and fix up the template's CIDR if required
2165 _rpc('resizeTemplate', oldcidr => "$binfo->{block}", newcidr => $newblock->network.'',
2166 rpcuser => $args{user}, location => $pinfo->{location})
2167 if $args{fbmerge};
2168 }
2169
2170##fixme: RPC failures?
2171 return ('OK','OK');
2172} # end updateBlock()
2173
2174
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
2180# Mostly works but may return Strange Things(TM) if used on a master block
2181sub splitBlock {
2182 my $dbh = shift;
2183 my %args = @_;
2184
2185##fixme: set errstr on errors so caller can suitably clue-by-four the user
2186 return if $args{basetype} ne 'b'; # only netblocks allowed!
2187
2188 my $binfo = getBlockData($dbh, $args{id});
2189 return if !$binfo;
2190
2191 return if $args{newmask} !~ /^\d+$/;
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
2200 if ($args{newmask} - $oldmask <= 0) {
2201 $errstr = "Can't split a /$oldmask allocation into /$args{newmask} pieces";
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}) {
2209 if ($args{newmask} - $oldmask > 128) {
2210 $errstr = "Impossible IPv6 mask length /$args{newmask} requested";
2211 return;
2212 }
2213 } else {
2214 if ($args{newmask} - $oldmask > 32) {
2215 $errstr = "Impossible IPv4 mask length /$args{newmask} requested";
2216 return;
2217 }
2218 }
2219
2220 my @newblocks = $block->split($args{newmask});
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
2236 push @ret, {nid => $args{id}, nblock => "$newblocks[0]"};
2237
2238 # prepare
2239 my $idsth = $dbh->prepare("SELECT currval('allocations_id_seq')");
2240 my $allocsth = $dbh->prepare("INSERT INTO allocations (cidr, $fields_sql)".
2241 " VALUES (?".',?'x(scalar(@fieldlist)).")");
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 ) );
2248 my $nbsth = $dbh->prepare("DELETE FROM poolips WHERE parent_id = ? AND ip = ?");
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 <<= ?");
2255
2256 my @clist;
2257 _getChildren($dbh, $args{id}, $binfo->{master_id}, \@clist, $block);
2258
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 }
2267 }
2268
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;
2272
2273 foreach my $pr (@processlist) {
2274 my @nbset = $pr->{cidr}->split($args{newmask});
2275
2276 # update existing block
2277 $dbh->do("UPDATE allocations SET cidr = ? WHERE id = ?", undef, ("$nbset[0]", $pr->{id}) );
2278
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);
2283 }
2284
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
2340##fixme:
2341# 2015/09/09 not sure if the latest rewrite has covered this case complete or not
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
2346 } # for (... @nbset)
2347
2348 if (%fbdel) {
2349 # pretty sure this SELECT result isn't used...
2350 my $delfblist = $dbh->selectall_arrayref(q{
2351 SELECT cidr,parent_id,id FROM freeblocks
2352 WHERE id in (
2353 }.join(',', keys %fbdel).")", {Slice=>{}} );
2354 foreach my $fbd (keys %fbdel) {
2355 $dbh->do("UPDATE freeblocks SET cidr = set_masklen(cidr, ?) WHERE id = ?", undef, $args{newmask}, $fbd);
2356 }
2357 }
2358
2359 } # foreach @processlist
2360
2361 $dbh->commit;
2362 };
2363 if ($@) {
2364 $errstr = "Error splitting $binfo->{block}: $@";
2365 $dbh->rollback;
2366 return;
2367 }
2368
2369 # Only try to update rDNS when the original block is flagged as "rDNS available"
2370 _rpc('splitTemplate', cidr => $binfo->{block}, newmask => $args{newmask}, rpcuser => $args{user},
2371 location => $binfo->{location})
2372 if ($binfo->{revavail} || $binfo->{revpartial});
2373
2374 return \@ret;
2375} # end splitBlock()
2376
2377
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
2390 my $user = shift;
2391
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
2416 my $addfbsth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id) VALUES (?,?,?,?,?,?)");
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 = ?");
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 <<= ?");
2422
2423 my @ret;
2424 my @newfreelist;
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;
2432 push @newfreelist, $newsub;
2433 }
2434 @newfreelist = Compact(@newfreelist);
2435
2436 # set new freeblocks, and clean up any IP pool entries if needed.
2437 foreach my $newfree (@newfreelist) {
2438 my @clist;
2439 # the block we're munging
2440 push @clist, { id => $id, type => $binfo->{type}, cidr => $binfo->{block} };
2441 _getChildren($dbh, $id, $binfo->{master_id}, \@clist, $newfree);
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);
2447 }
2448
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
2458 } # $newfree (@newfreelist)
2459
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
2478 # Only try to update rDNS when the original block is flagged as "rDNS available"
2479 _rpc('resizeTemplate', oldcidr => $binfo->{block}, newcidr => $newblock->network, rpcuser => $user,
2480 location => $binfo->{location})
2481 if ($binfo->{revavail} || $binfo->{revpartial});
2482
2483 return \@ret;
2484} # end shrinkBlock()
2485
2486
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.
2492# Returns a list starting with the new merged block, then the merged allocations with comment
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
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.
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.
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 }
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
2577 local $dbh->{AutoCommit} = 0;
2578 local $dbh->{RaiseError} = 1;
2579
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...
2584 my $updsth = $dbh->prepare("UPDATE allocations SET cidr = ?, type = ? WHERE id = ?");
2585
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
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 (?,?,?,?,?,?)");
2609 my $delsth = $dbh->prepare("DELETE FROM allocations WHERE id = ?");
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
2620 # In case of merging a master block. Somewhat redundant with calls to $fbreparentsth,
2621 # but not *quite* entirely.
2622 my $mfbsth = $dbh->prepare("UPDATE freeblocks SET master_id = ? WHERE master_id = ?");
2623
2624 if ($args{scope} eq 'keepall') {
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')");
2629 # snag the new parent info for the return list
2630 push @retlist, { block => "$newblock", type => $disp_alloctypes{$args{newtype}}, id => $prime };
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
2635 while (my ($peercidr, $peer_id, $peertype, $m_id) = $peersth->fetchrow_array) {
2636 $reparentsth->execute($prime, $binfo->{master_id}, $peer_id);
2637 # Fix up master_id on free blocks if we're merging a master block
2638 $mfbsth->execute($binfo->{master_id}, $m_id) if $peertype eq 'mm';
2639 # capture block for return
2640 push @retlist, { block => $peercidr, mdisp => $disp_alloctypes{$peertype}, mtype => $peertype };
2641 }
2642
2643 } elsif ($args{scope} =~ /^clear/) {
2644 # clearpeer and clearall share a starting point
2645 # snag the new parent info for the return list
2646 push @retlist, { block => "$newblock", type => $disp_alloctypes{$args{newtype}}, id => $prime };
2647 # update the primary allocation info
2648 $updsth->execute($newblock, $args{newtype}, $prime);
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 }
2657 # delete the peers.
2658 while (my ($peercidr, $peer_id, $peertype, $m_id) = $peersth->fetchrow_array) {
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 (?,?,?,?)",
2667 undef, ($peercidr, 'm', $prime, $prime) ) if $binfo->{type} eq 'mm';
2668 # capture block for return
2669 push @retlist, { block => $peercidr, mdisp => $disp_alloctypes{$peertype}, mtype => $peertype };
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
2686 } elsif ($args{scope} eq 'mergepeer') { # should this just be an else?
2687 # Default case. Merge "peer" blocks, but keep all suballocations
2688 # snag the new parent info for the return list
2689 push @retlist, {block => "$newblock", type => $disp_alloctypes{$args{newtype}}, id => $prime};
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
2710 _poolToAllocations($dbh, $peerfull, $pinfo, newparent => $prime) if $peertype =~ /.[dp]/;
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';
2713 # capture block for return
2714 push @retlist, { block => $peercidr, mdisp => $disp_alloctypes{$peertype}, mtype => $peertype };
2715 } # merge peers
2716 # update the primary allocation info. Do this last so we don't stomp extra data-retrieval in the loop above
2717 $updsth->execute($newblock, $args{newtype}, $prime);
2718
2719 } # scope
2720
2721 # Clean up free blocks
2722 _compactFree($dbh, $prime);
2723
2724 } elsif ($args{newtype} =~ /.[dp]/) {
2725 ## Pool
2726 # Snag the new parent info for the return list
2727 push @retlist, { block => "$newblock", type => $disp_alloctypes{$args{newtype}}, id => $prime };
2728
2729 if ($args{scope} eq 'keepall') {
2730 # Convert all mergeable allocations and subs to chunks of pool IP assignments
2731 push @retlist, @{ _toPool($dbh, $prime, $newblock, $args{newtype}, 1) };
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
2742 push @retlist, { block => $peercidr, mdisp => $disp_alloctypes{$peertype}, mtype => $peertype };
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.
2753 push @retlist, @{ _toPool($dbh, $prime, $newblock, $args{newtype}, 1) }
2754 unless $binfo->{type} =~ /.[enr]/;
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
2762 push @retlist, { block => $peercidr, mdisp => $disp_alloctypes{$peertype}, mtype => $peertype };
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.
2772 $updsth->execute($newblock, $args{newtype}, $prime);
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
2779 } elsif ($args{newtype} =~ /.[enr]/) {
2780 ## Leaf
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.
2784 # Update the old allocation
2785 $updsth->execute($newblock, $args{newtype}, $prime);
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
2795 push @retlist, { block => $peercidr, mdisp => $disp_alloctypes{$peertype}, mtype => $peertype };
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);
2808
2809 } # $args{newtype} if()
2810
2811 $dbh->commit;
2812 };
2813 if ($@) {
2814 my $msg = $@;
2815 $errstr = $msg;
2816 $dbh->rollback;
2817 return ('FAIL',$msg);
2818 }
2819
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
2824 # Return early if the block wasn't flagged as rDNS-able
2825 return \@retlist unless $binfo->{revavail} || $binfo->{revpartial};
2826
2827 if ($args{newtype} =~ /.[cm]/) {
2828
2829 if ($args{scope} eq 'keepall') {
2830 # Add new rDNS for new container
2831 _rpc('addOrUpdateRevRec', cidr => $binfo->{block}, name => $binfo->{rdns}, rpcuser => $args{user},
2832 location => $binfo->{location});
2833
2834 } else {
2835 # Resize rDNS template for $prime
2836 _rpc('resizeTemplate', oldcidr => "$binfo->{block}", newcidr => $newblock->network.'',
2837 rpcuser => $args{user}, location => $binfo->{location});
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',
2850 parpatt => $pinfo->{rdns}, location => $binfo->{location});
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',
2856 parpatt => $pinfo->{rdns}, location => $binfo->{location});
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',
2862 parpatt => $pinfo->{rdns}, location => $binfo->{location});
2863
2864 } # scope, second level
2865 } # scope, !keepall
2866
2867 } elsif ($args{newtype} =~ /.[dp]/) {
2868 # Merge to pool
2869
2870 # Resize rDNS template for $prime
2871 _rpc('resizeTemplate', oldcidr => "$binfo->{block}", newcidr => $newblock->network.'',
2872 rpcuser => $args{user}, location => $binfo->{location});
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',
2890 parpatt => $pinfo->{rdns}, location => $binfo->{location});
2891 _rpc('templatesToRecords', templates => \@convlist, rpcuser => $args{user}, location => $binfo->{location});
2892
2893 } # scope eq 'keepall' || 'mergepeer'
2894 else {
2895
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',
2921 delsubs => 'y', parpatt => $pinfo->{rdns}, location => $binfo->{location})
2922 if @fulldellist;
2923 # ... and just the template for container or pool templates in $prime...
2924 _rpc('delRevSet', cidrlist => join(',', @dellist), rpcuser => $args{user}, delforward => 'y',
2925 delsubs => 'n', parpatt => $pinfo->{rdns}, location => $binfo->{location})
2926 if @dellist;
2927 # ... and convert a few to record groups
2928 _rpc('templatesToRecords', templates => \@convlist, rpcuser => $args{user},
2929 location => $binfo->{location})
2930 if @convlist;
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}),
2935 rpcuser => $args{user}, delforward => 'y', delsubs => 'y', parpatt => $pinfo->{rdns},
2936 location => $binfo->{location});
2937 }
2938
2939 } # scope eq 'clearpeer' || 'clearall'
2940
2941 } elsif ($args{newtype} =~ /.[enr]/) {
2942 # Merge to leaf type
2943
2944 # Resize rDNS template for $prime
2945 _rpc('resizeTemplate', oldcidr => "$binfo->{block}", newcidr => $newblock->network.'',
2946 rpcuser => $args{user}, location => $binfo->{location});
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',
2956 parpatt => $pinfo->{rdns}, location => $binfo->{location});
2957
2958 } # type grouping for rDNS calls
2959
2960 return \@retlist;
2961
2962} # end mergeBlocks()
2963
2964
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
2969# Requires a database handle, the block to delete, the routing depth (if applicable),
2970# the VRF ID, and a flag to indicate whether to delete associated forward DNS entries
2971# as well as the reverse entry
2972sub deleteBlock {
2973 my ($dbh,$id,$basetype,$delfwd,$user) = @_;
2974
2975 # reset $basetype so caller can just pass the complete allocation type
2976 if ($basetype =~ /^.?i$/) {
2977 $basetype = 'i';
2978 } else {
2979 $basetype = 'b';
2980 }
2981
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
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
2997 my $sth;
2998
2999 # Magic variables used for odd allocation cases.
3000 my $container;
3001 my $con_type;
3002
3003
3004 # temporarily forced null, until a sane UI for VRF tracking can be found.
3005# $vrf = '';# if !$vrf; # as with SQL, the null value is not equal to ''. *sigh*
3006
3007 # To contain the error message, if any.
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
3011 # Enable transactions and exception-on-errors... but only for this sub
3012 local $dbh->{AutoCommit} = 0;
3013 local $dbh->{RaiseError} = 1;
3014
3015 if ($binfo->{type} =~ /^.i$/) {
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
3019 my $pinfo;
3020
3021 eval {
3022 $msg = "Unable to deallocate $disp_alloctypes{$binfo->{type}} $cidr";
3023 $pinfo = getBlockData($dbh, $binfo->{parent_id}, 'b');
3024 $dbh->do("UPDATE poolips SET custid = ?, available = 'y',".
3025 "city = (SELECT city FROM allocations WHERE id = ?),".
3026 "description = '', notes = '', circuitid = '', vrf = ?, backup_id = 0".
3027 " WHERE id = ?", undef,
3028 ($pinfo->{custid}, $binfo->{parent_id}, $pinfo->{vrf}, $id) );
3029 $dbh->do("DELETE FROM backuplist WHERE backup_id = ?", undef, $binfo->{hasbk})
3030 if $binfo->{hasbk};
3031 $dbh->commit;
3032 };
3033 if ($@) {
3034 $msg .= ": $@";
3035 eval { $dbh->rollback; };
3036 return ('FAIL',$msg);
3037 }
3038
3039##fixme: RPC return code?
3040 _rpc('delByCIDR', cidr => "$cidr", user => $user, delforward => $delfwd, rpcuser => $user,
3041 location => $binfo->{location})
3042 if ($pinfo->{revavail} || $pinfo->{revpartial});
3043
3044 return ('OK',"OK");
3045
3046 } elsif ($binfo->{type} eq 'mm') { # end alloctype =~ /.i/
3047 # Second case. The block is a full master block
3048
3049##fixme: VRF limit
3050 $msg = "Unable to delete master block $cidr";
3051 eval {
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}) );
3054 $dbh->do("DELETE FROM dnsavail WHERE location = ? AND parent_alloc = ?", undef,
3055 ($binfo->{location}, $binfo->{master_id}) );
3056 $dbh->do("DELETE FROM backuplist WHERE backup_id = ?", undef, $binfo->{hasbk})
3057 if $binfo->{hasbk};
3058 $dbh->commit;
3059 };
3060 if ($@) {
3061 $msg .= ": $@";
3062 eval { $dbh->rollback; };
3063 return ('FAIL', $msg);
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);
3074 } else {
3075 @zonelist = ($cidr);
3076 }
3077 my @fails;
3078 foreach my $subzone (@zonelist) {
3079 # We don't wrap this call tighter, since there isn't an inherent allocation to check for rDNS-ability.
3080 if ($rpc_url && !_rpc('delZone', zone => "$subzone", revrec => 'y', rpcuser => $user, delforward => $delfwd) ) {
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');
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
3096 my $retcode = 'OK';
3097 my ($ptype,$pcity,$ppatt,$p_id);
3098
3099 eval {
3100
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.
3103
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
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};
3117
3118 # Delete the block
3119 $dbh->do("DELETE FROM allocations WHERE id = ?", undef, ($id) );
3120
3121 # munge the parent type a little
3122 $ptype = (split //, $ptype)[1];
3123
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
3127
3128 # check to see if any container allocations could be the "true" parent
3129 my ($tparent,$tpar_id,$trtype,$tcity);
3130 $tpar_id = 0;
3131
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.
3134
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
3143
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}) );
3148
3149##fixme?
3150# edge cases not handled, or handled badly:
3151# -> $cidr managed to get to be the entirety of an IP pool
3152
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)");
3170
3171##fixme: need to not insert net, gateway, and bcast on "real netblock" pools (DHCPish)
3172 # don't insert .0
3173 $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|;
3174 $cidr++;
3175 my $bcast = $cidr->broadcast;
3176 while ($cidr != $bcast) {
3177 $sth2->execute($cidr->addr);
3178 $cidr++;
3179 }
3180 # don't insert .255
3181 $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.255$|;
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
3189 }
3190
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
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. :/
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 = ? ".
3205 "ORDER BY masklen(cidr) DESC,parent_id DESC");
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]$/) {
3246 # We have to delete the IPs from the pool listing.
3247##fixme: rdepth? vrf?
3248 $dbh->do("DELETE FROM poolips WHERE parent_id = ?", undef, ($id) );
3249 }
3250
3251 $pinfo = getBlockData($dbh, $p_id);
3252
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.
3255
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.
3260 $dbh->do("UPDATE freeblocks SET parent_id = ?, routed = ?, city = ? WHERE parent_id = ?", undef,
3261 ($p_id, $ptype, $pcity, $id) );
3262 } else {
3263 # ... otherwise, add the freeblock
3264 $dbh->do("INSERT INTO freeblocks (cidr, city, routed, parent_id, master_id) VALUES (?,?,?,?,?)", undef,
3265 ($cidr, $pcity, $ptype, $p_id, $binfo->{master_id}) );
3266 }
3267
3268 # Walk the free blocks in the parent and reduce them to the minimal set of CIDR ranges necessary
3269 _compactFree($dbh, $p_id);
3270
3271 } # done returning IPs to the appropriate place
3272
3273 # If we got here, we've succeeded. Whew!
3274 $dbh->commit;
3275 }; # end eval
3276 if ($@) {
3277 if ($msg) { $msg .= ": $@"; } else { $msg = $@; }
3278 eval { $dbh->rollback; };
3279 return ('FAIL', $msg);
3280 }
3281
3282##fixme: RPC return code?
3283 _rpc('delByCIDR', cidr => "$cidr", rpcuser => $user, delforward => $delfwd, delsubs => 'y',
3284 parpatt => $ppatt, location => $binfo->{location})
3285 if ($binfo->{revavail} || $binfo->{revpartial});
3286
3287 return ($retcode, $goback);
3288
3289 } # end alloctype != netblock
3290
3291} # end deleteBlock()
3292
3293
3294## IPDB::getBlockData()
3295# Get CIDR or IP, custid, type, city, circuit ID, description, notes, modification time,
3296# private/restricted data, and backup fields, for a CIDR block or pool IP
3297# Also returns SWIP status flag for CIDR blocks or pool netblock for IPs
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.
3300# Returns a hashref to the block data
3301sub getBlockData {
3302 my $dbh = shift;
3303 my $id = shift;
3304 my $type = shift || 'b'; # default to netblock for lazy callers
3305
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
3313# catch some errors, someday
3314# if (!$id || $id !~ /^\d+$/) {
3315# $errstr = "Allocation ID must be numeric
3316# }
3317
3318 # Note city, vrf, parent_id and master_id removed due to JOIN uncertainty for block allocations
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);
3321 my $bkfields = q(b.backup_id AS hasbk, b.bkbrand, b.bkmodel, b.bktype, b.bkport, b.bksrc,
3322 b.bkuser, b.bkvpass, b.bkepass, b.bkip);
3323
3324 if ($type eq 'i') {
3325 my $binfo = $dbh->selectrow_hashref(qq(
3326 SELECT a.id, a.ip AS block, a.city, a.vrf, a.parent_id, a.master_id, $commonfields,
3327 d.zone >> a.ip AS revavail, d.location,
3328 $bkfields,
3329 v.location AS vrfloc
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 = ?
3335 ), undef, ($id) );
3336 return $binfo;
3337 } else {
3338 my $binfo = $dbh->selectrow_hashref(qq(
3339 SELECT a.id, masklen(a.cidr), a.cidr AS block, a.city, a.vrf, a.parent_id, a.master_id, a.swip, $commonfields,
3340 f.cidr AS reserve, f.id as reserve_id,
3341 d.zone >>= a.cidr AS revavail, d.zone << a.cidr AS revpartial, d.location,
3342 $bkfields,
3343 v.location AS vrfloc
3344 FROM allocations a
3345 LEFT JOIN freeblocks f ON a.id=f.reserve_for
3346 LEFT JOIN dnsavail d ON a.master_id = d.parent_alloc AND (a.cidr <<= d.zone OR a.cidr >> d.zone)
3347 LEFT JOIN backuplist b ON a.backup_id = b.backup_id
3348 JOIN allocations m ON a.master_id = m.id JOIN vrfs v ON m.vrf = v.vrf
3349 WHERE a.id = ?
3350 ), undef, ($id) );
3351
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
3365 return $binfo;
3366 }
3367} # end getBlockData()
3368
3369
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
3379 $args{type} = 'b' if !$args{type};
3380 my $cached = 1;
3381
3382 # snag entry from database
3383 my ($rdns,$rfrom,$pid,$mid);
3384 if ($args{type} =~ /.i/) {
3385 ($rdns, $rfrom, $pid, $mid) = $dbh->selectrow_array("SELECT rdns,ip,parent_id,master_id FROM poolips WHERE id = ?",
3386 undef, ($args{id}) );
3387 } else {
3388 ($rdns, $rfrom, $pid, $mid) = $dbh->selectrow_array("SELECT rdns,cidr,parent_id,master_id FROM allocations WHERE id = ?",
3389 undef, ($args{id}) );
3390 }
3391
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;
3395
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
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
3413 my ($rpcblock) = ($cidr->masklen <= 24 ? $cidr->split( ($cidr->masklen <= 16 ? 16 : 24) ) : $cidr);
3414 my %rpcargs = (
3415 rpcuser => $args{user},
3416 group => $revgroup, # not sure how this could sanely be exposed, tbh...
3417 cidr => "$rpcblock",
3418 );
3419
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".
3428 " WHERE d.parent_alloc = ? ORDER BY zone DESC", undef, $mid);
3429
3430 $errstr = '';
3431 my $remote_rdns = _rpc('getRevPattern', %rpcargs);
3432 if ($remote_rdns) {
3433 $rdns = $remote_rdns;
3434 $cached = 0;
3435 } else {
3436 if (!$errstr) {
3437 # no error, but no data
3438 $cached = 0;
3439 }
3440 }
3441 }
3442
3443 # hmm. do we care about where it actually came from?
3444 return $rdns, $cached;
3445} # end getBlockRDNS()
3446
3447
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
3461 my $binfo = getBlockData($dbh, $args{id}, $args{type});
3462
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 };
3468##fixme: rpc call?
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
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
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
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
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
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
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
3581## IPDB::mailNotify()
3582# Sends notification mail to recipients regarding an IPDB operation
3583sub mailNotify {
3584 my $dbh = shift;
3585 my ($action,$subj,$message) = @_;
3586
3587 return if $smtphost eq 'smtp.example.com'; # do nothing if still using default SMTP host.
3588
3589##fixme: need to redesign the breakdown/processing for $action for proper handling of all cases
3590
3591# split action into parts for fiddlement. nb: there are almost certainly better ways to do this.
3592 my @actionbits = split //, $action;
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
3607 my %reciplist;
3608 foreach (@actionsets) {
3609 $sth->execute($_);
3610##fixme - need to handle db errors
3611 my ($recipsub) = $sth->fetchrow_array;
3612 next if !$recipsub;
3613 foreach (split(/,/, $recipsub)) {
3614 $reciplist{$_}++;
3615 }
3616 }
3617
3618 return if !%reciplist;
3619
3620 # dodge a bullet with module version "numbers"
3621 my ($ver) = ($IPDB::VERSION =~ /^(\d+(?:\.\d+)?)/);
3622
3623 foreach my $recip (keys %reciplist) {
3624 $mailer->mail($smtpsender);
3625 $mailer->to($recip);
3626 $mailer->data("From: \"$org_name IP Database\" <$smtpsender>\n",
3627 "To: $recip\n",
3628 "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
3629 "Subject: {IPDB} $subj\n",
3630 "X-Mailer: IPDB Notify v".sprintf("%.1d",$ver)."\n",
3631 "Organization: $org_name\n",
3632 "\n$message\n");
3633 }
3634 $mailer->quit;
3635}
3636
3637# Indicates module loaded OK. Required by Perl.
36381;
Note: See TracBrowser for help on using the repository browser.