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