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