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

Last change on this file since 771 was 768, checked in by Kris Deugau, 9 years ago

/trunk

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