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

Last change on this file since 926 was 926, checked in by kdeugau, 5 months ago

/trunk

Commit inexplicably missing custID whitespace removal in allocateBlock()

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