source: trunk/cgi-bin/main.cgi@ 704

Last change on this file since 704 was 702, checked in by Kris Deugau, 10 years ago

/trunk

Add "split block" feature. See #7. May still need a little tweaking
("List IPs" link for pools, refiddle rDNS template records?)

Also stubbed out "shrink block" (branch for "split block"), and added
a placeholder for "merge blocks".

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 43.2 KB
RevLine 
[4]1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
[8]3###
4# SVN revision info
5# $Date: 2015-02-23 23:16:11 +0000 (Mon, 23 Feb 2015) $
6# SVN revision $Rev: 702 $
7# Last update by $Author: kdeugau $
8###
[417]9# Copyright (C) 2004-2010 - Kris Deugau
[4]10
11use strict;
12use warnings;
13use CGI::Carp qw(fatalsToBrowser);
[517]14use CGI::Simple;
15use HTML::Template;
[4]16use DBI;
17use POSIX qw(ceil);
18use NetAddr::IP;
[582]19use Frontier::Client;
[4]20
21use Sys::Syslog;
22
[417]23# don't remove! required for GNU/FHS-ish install from tarball
24##uselib##
25
[515]26use CustIDCK;
[417]27use MyIPDB;
28
[431]29openlog "IPDB","pid","$IPDB::syslog_facility";
[4]30
[517]31## Environment. Collect some things, process some things, set some things...
32
[233]33# Collect the username from HTTP auth. If undefined, we're in
34# a test environment, or called without a username.
[4]35my $authuser;
36if (!defined($ENV{'REMOTE_USER'})) {
37 $authuser = '__temptest';
38} else {
39 $authuser = $ENV{'REMOTE_USER'};
40}
41
[517]42# anyone got a better name? :P
43my $thingroot = $ENV{SCRIPT_FILENAME};
44$thingroot =~ s|cgi-bin/main.cgi||;
45
[402]46syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
[4]47
[517]48##fixme there *must* be a better order to do things in so this can go back where it was
49# CGI fiddling done here so we can declare %webvar so we can alter $webvar{action}
50# to show the right page on DB errors.
51# Set up the CGI object...
52my $q = new CGI::Simple;
53# ... and get query-string params as well as POST params if necessary
54$q->parse_query_string;
55
56# Convenience; saves changing all references to %webvar
57##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
58my %webvar = $q->Vars;
59
[106]60# Why not a global DB handle? (And a global statement handle, as well...)
61# Use the connectDB function, otherwise we end up confusing ourselves
62my $ip_dbh;
63my $errstr;
[142]64($ip_dbh,$errstr) = connectDB_My;
[106]65if (!$ip_dbh) {
[517]66 $webvar{action} = "dberr";
67} else {
68 initIPDBGlobals($ip_dbh);
[106]69}
[4]70
[517]71# Set up some globals
72$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
[233]73
[517]74my $header = HTML::Template->new(filename => "header.tmpl");
75my $footer = HTML::Template->new(filename => "footer.tmpl");
[697]76my $utilbar = HTML::Template->new(filename => "utilbar.tmpl", loop_context_vars => 1, global_vars => 1);
[233]77
[697]78print "Content-type: text/html\n\n";
79
[517]80$header->param(version => $IPDB::VERSION);
81$header->param(addperm => $IPDBacl{$authuser} =~ /a/);
82$header->param(webpath => $IPDB::webpath);
[4]83
[697]84$utilbar->param(webpath => $IPDB::webpath);
[4]85
[697]86print $header->output;
87
88
[4]89#main()
[517]90my $aclerr;
[4]91
92if(!defined($webvar{action})) {
[517]93 $webvar{action} = "index"; #shuts up the warnings.
[4]94}
95
[517]96my $page;
97if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
[553]98 $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1);
[517]99} else {
100 $page = HTML::Template->new(filename => "dunno.tmpl");
101}
102
[4]103if($webvar{action} eq 'index') {
104 showSummary();
[233]105} elsif ($webvar{action} eq 'addmaster') {
106 if ($IPDBacl{$authuser} !~ /a/) {
[517]107 $aclerr = 'addmaster';
[233]108 }
[582]109
110 # Retrieve the list of DNS locations if we've got a place to grab them from
111 if ($IPDB::rpc_url) {
112
113 my %rpcargs = (
114 rpcuser => $authuser,
115 group => 1, # bleh
116 defloc => '',
117 );
[623]118 my $result = IPDB::_rpc('getLocDropdown', %rpcargs);
[582]119 $page->param(loclist => $result);
120 }
121
[4]122} elsif ($webvar{action} eq 'newmaster') {
123
[233]124 if ($IPDBacl{$authuser} !~ /a/) {
[517]125 $aclerr = 'addmaster';
[233]126 } else {
127 my $cidr = new NetAddr::IP $webvar{cidr};
[517]128 $page->param(cidr => "$cidr");
[4]129
[582]130 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr}, (vrf => $webvar{vrf}, rdns => $webvar{rdns},
131 rwhois => $webvar{rwhois}, defloc => $webvar{loc}, user => $authuser) );
[4]132
[371]133 if ($code eq 'FAIL') {
[320]134 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
[517]135 $page->param(err => $msg);
[233]136 } else {
[624]137 $page->param(parent => $msg);
[582]138 if ($code eq 'WARN') {
[624]139 $IPDB::errstr =~ s/\n\n/<br>\n/g;
140 $IPDB::errstr =~ s/:\n/:<br>\n/g;
141 $page->param(warn => $IPDB::errstr);
[582]142 }
[233]143 syslog "info", "$authuser added master block $webvar{cidr}";
144 }
[4]145
[233]146 } # ACL check
147
[4]148} # end add new master
149
[567]150elsif ($webvar{action} eq 'showsubs') {
151 showSubs();
152}
153
[4]154elsif($webvar{action} eq 'listpool') {
[528]155 showPool();
[4]156}
157
158# Not modified or added; just shuffled
159elsif($webvar{action} eq 'assign') {
160 assignBlock();
161}
162elsif($webvar{action} eq 'confirm') {
163 confirmAssign();
164}
165elsif($webvar{action} eq 'insert') {
166 insertAssign();
167}
168elsif($webvar{action} eq 'edit') {
169 edit();
170}
171elsif($webvar{action} eq 'update') {
172 update();
173}
[702]174elsif($webvar{action} eq 'split') {
175 prepSplit();
176}
177elsif($webvar{action} eq 'dosplit') {
178 doSplit();
179}
[4]180elsif($webvar{action} eq 'delete') {
181 remove();
182}
183elsif($webvar{action} eq 'finaldelete') {
184 finalDelete();
185}
[397]186elsif ($webvar{action} eq 'nodesearch') {
[519]187 my $nodelist = getNodeList($ip_dbh);
188 $page->param(nodelist => $nodelist);
[517]189}
[397]190
[517]191# DB failure. Can't do much here, really.
192elsif ($webvar{action} eq 'dberr') {
193 $page->param(errmsg => $errstr);
[397]194}
195
[517]196# Default is an error. It shouldn't be possible to get here unless you're
197# randomly feeding in values for webvar{action}.
[4]198else {
199 my $rnd = rand 500;
200 my $boing = sprintf("%.2f", rand 500);
[517]201 my @excuses = (
202 "Aether cloudy. Ask again later about $webvar{action}.",
203 "The gods are unhappy with your sacrificial $webvar{action}.",
204 "Because one of $webvar{action}'s legs are both the same",
205 "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!",
206 "Hey, man, you've had your free $webvar{action}. Next one's gonna... <i>cost</i>....",
207 "I ain't done $webvar{action}",
208 "Oooo, look! A flying $webvar{action}!",
209 "$webvar{action} too evil, avoiding.",
210 "Rocks fall, $webvar{action} dies.",
211 "Bit bucket must be emptied before I can $webvar{action}..."
212 );
213 $page->param(dunno => $excuses[$rnd/50.0]);
[4]214}
[111]215## Finally! Done with that NASTY "case" emulation!
[4]216
217
[517]218# Switch to a different template if we've tripped on an ACL error.
219# Note that this should only be exercised in development, when
220# deeplinked, or when being attacked; normal ACL handling should
221# remove the links a user is not allowed to click on.
222if ($aclerr) {
223 $page = HTML::Template->new(filename => "aclerror.tmpl");
224 $page->param(ipdbfunc => $aclmsg{$aclerr});
225}
[4]226
[106]227# Clean up IPDB globals, DB handle, etc.
[111]228finish($ip_dbh);
[199]229
[517]230## Do all our printing here so we can generate errors and stick them into the slots in the templates.
[199]231
[517]232# can't do this yet, too many blowups
233#print "Content-type: text/html\n\n", $header->output;
234$page->param(webpath => $IPDB::webpath);
[697]235print $utilbar->output;
[517]236print $page->output;
237
238# include the admin tools link in the output?
239$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
240$footer->param(webpath => $IPDB::webpath);
241print $footer->output;
242
[106]243# Just in case something waaaayyy down isn't in place
244# properly... we exit explicitly.
[517]245exit 0;
[4]246
247
248# Initial display: Show master blocks with total allocated subnets, total free subnets
[118]249sub showSummary {
[523]250 my $masterlist = listSummary($ip_dbh);
251 $page->param(masterlist => $masterlist);
[106]252
[517]253 $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
[4]254} # showSummary
255
256
[566]257# Display blocks immediately within a given parent
258sub showSubs {
[682]259 # Which layout?
260 if ($IPDB::sublistlayout == 2) {
261
262 # 2-part layout; mixed containers and end-use allocations and free blocks.
263 # Containers have a second line for the subblock metadata.
264 # We need to load an alternate template for this case.
265 $page = HTML::Template->new(filename => "showsubs2.tmpl", loop_context_vars => 1, global_vars => 1);
266
267 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/));
268
269 my $sublist = listSubs($ip_dbh, parent => $webvar{parent});
270 $page->param(sublist => $sublist);
271
272 } else {
273
[685]274 # 3-part layout; containers, end-use allocations, and free blocks
[682]275
276 my $contlist = listContainers($ip_dbh, parent => $webvar{parent});
277 $page->param(contlist => $contlist);
278
279 my $alloclist = listAllocations($ip_dbh, parent => $webvar{parent});
280 $page->param(alloclist => $alloclist);
281
282 # only show "delete" button if we have no container or usage allocations
283 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/) && !(@$contlist || @$alloclist));
284
285 }
286
287 # Common elements
[627]288 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
289
[685]290##fixme: do we add a wrapper to not show the edit link for master blocks?
291#$page->param(editme => 1) unless $pinfo->{type} ne 'mm';
292
[697]293 my $crumbs = getBreadCrumbs($ip_dbh, $pinfo->{parent_id});
294 my @rcrumbs = reverse (@$crumbs);
295 $utilbar->param(breadcrumb => \@rcrumbs);
296
[685]297 $page->param(self_id => $webvar{parent});
[627]298 $page->param(block => $pinfo->{block});
[566]299 $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
[4]300
[627]301 my $flist = listFree($ip_dbh, parent => $webvar{parent});
[566]302 $page->param(freelist => $flist);
303} # showSubs
[4]304
305
306# List the IPs used in a pool
[528]307sub showPool {
[4]308
[631]309 my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
310 my $cidr = new NetAddr::IP $poolinfo->{block};
[4]311
[697]312 # Tree navigation
313 my $crumbs = getBreadCrumbs($ip_dbh, $poolinfo->{parent_id});
314 my @rcrumbs = reverse (@$crumbs);
315 $utilbar->param(breadcrumb => \@rcrumbs);
316
[631]317 $page->param(block => $cidr);
[517]318 $page->param(netip => $cidr->addr);
319 $cidr++;
320 $page->param(gate => $cidr->addr);
321 $cidr--; $cidr--;
322 $page->param(bcast => $cidr->addr);
323 $page->param(mask => $cidr->mask);
[157]324
[528]325 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
326 $page->param(city => $poolinfo->{city});
[517]327
[157]328 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
[528]329 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
[4]330
331# probably have to add an "edit IP allocation" link here somewhere.
332
[528]333 my $plist = listPool($ip_dbh, $webvar{pool});
334 # technically slightly more efficient to check the ACL in an if () once outside the foreach
335 foreach (@{$plist}) {
336 $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
[4]337 }
[528]338 $page->param(poolips => $plist);
339} # end showPool
[4]340
341
[106]342# Show "Add new allocation" page. Note that the actual page may
343# be one of two templates, and the lists come from the database.
[4]344sub assignBlock {
345
[233]346 if ($IPDBacl{$authuser} !~ /a/) {
[517]347 $aclerr = 'addblock';
[233]348 return;
349 }
350
[517]351 # hack pthbttt eww
[633]352 $webvar{parent} = 0 if !$webvar{parent};
[517]353 $webvar{block} = '' if !$webvar{block};
[21]354
[575]355 $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
[517]356
[633]357 if ($webvar{fbid} || $webvar{fbtype}) {
[575]358
359 # Common case, according to reported usage. Block to assign is specified.
[21]360 my $block = new NetAddr::IP $webvar{block};
[187]361
[675]362 my ($rdns,$cached) = getBlockRDNS($ip_dbh, id => $webvar{parent}, type => $webvar{fbtype}, user => $authuser);
[585]363 $page->param(rdns => $rdns) if $rdns;
[633]364 $page->param(parent => $webvar{parent});
365 $page->param(fbid => $webvar{fbid});
[675]366 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
367 $page->param(cached => $cached);
[585]368
[691]369 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
370 # seems reasonable that a new allocation would share a VRF with its parent
371 $page->param(pvrf => $pinfo->{vrf});
372
[697]373 # Tree navigation
374 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
375 my @rcrumbs = reverse (@$crumbs);
376 $utilbar->param(breadcrumb => \@rcrumbs);
377
[575]378 $webvar{fbtype} = '' if !$webvar{fbtype};
379 if ($webvar{fbtype} eq 'i') {
[633]380 my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i');
[575]381 $page->param(
382 fbip => 1,
[633]383 block => $ipinfo->{block},
[575]384 fbdisptype => $list_alloctypes{$ipinfo->{type}},
385 type => $ipinfo->{type},
[633]386 allocfrom => $pinfo->{block},
[575]387 );
[187]388 } else {
[529]389 # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
[575]390 my $tlist = getTypeList($ip_dbh, 'n');
[529]391 $tlist->[0]->{sel} = 1;
[575]392 $page->param(typelist => $tlist, block => $block);
[106]393 }
[575]394
[21]395 } else {
[575]396
397 # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
[541]398 my $mlist = getMasterList($ip_dbh, 'c');
399 $page->param(masterlist => $mlist);
[517]400
401 my @pops;
[633]402 foreach my $pop (@citylist) {
[517]403 my %row = (pop => $pop);
404 push (@pops, \%row);
[92]405 }
[517]406 $page->param(pops => \@pops);
407
[529]408 # get all standard alloctypes
409 my $tlist = getTypeList($ip_dbh, 'a');
410 $tlist->[0]->{sel} = 1;
411 $page->param(typelist => $tlist);
[21]412 }
[517]413
414 my @cities;
[92]415 foreach my $city (@citylist) {
[517]416 my %row = (city => $city);
417 push (@cities, \%row);
[92]418 }
[517]419 $page->param(citylist => \@cities);
[4]420
[397]421## node hack
[530]422 my $nlist = getNodeList($ip_dbh);
423 $page->param(nodelist => $nlist);
[397]424## end node hack
425
[517]426 $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
[284]427
[4]428} # assignBlock
429
430
431# Take info on requested IP assignment and see what we can provide.
432sub confirmAssign {
[233]433 if ($IPDBacl{$authuser} !~ /a/) {
[517]434 $aclerr = 'addblock';
[233]435 return;
436 }
[4]437
438 my $cidr;
[692]439 my $resv; # Reserved for expansion.
[4]440 my $alloc_from;
[633]441 my $fbid = $webvar{fbid};
442 my $p_id = $webvar{parent};
[4]443
444 # Going to manually validate some items.
445 # custid and city are automagic.
[111]446 return if !validateInput();
[4]447
[665]448 # make sure this is defined
449 $webvar{fbassign} = 'n' if !$webvar{fbassign};
450
[4]451# Several different cases here.
452# Static IP vs netblock
453# + Different flavours of static IP
454# + Different flavours of netblock
455
[575]456 if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
[665]457 if (!$webvar{pop}) {
458 $page->param(err => "Please select a location/POP site to allocate from.");
459 return;
460 }
[532]461 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
[517]462 $page->param(staticip => 1);
[532]463 $page->param(poollist => $plist) if $plist;
[4]464 $cidr = "Single static IP";
[517]465##fixme: need to handle "no available pools"
[4]466
467 } else { # end show pool options
[21]468
[533]469 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
[697]470
471 # Tree navigation
472 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
473 my @rcrumbs = reverse (@$crumbs);
474 $utilbar->param(breadcrumb => \@rcrumbs);
475
[21]476 $cidr = new NetAddr::IP $webvar{block};
[575]477 $alloc_from = new NetAddr::IP $webvar{allocfrom};
[21]478 $webvar{maskbits} = $cidr->masklen;
[692]479 # Some additional checks are needed for reserving free space
480 if ($webvar{reserve}) {
481 if ($cidr == $alloc_from) {
482# We could still squirm and fiddle to try to find a way to reserve space, but the storage model for
483# IPDB means that all continguous free space is kept in the smallest number of strict CIDR netblocks
484# possible. (In theory.) If the request and the freeblock are the same, it is theoretically impossible
485# to reserve an equivalent-sized block either ahead or behind the requested one, because the pair
486# together would never be a strict CIDR block.
487 $page->param(warning => "Can't reserve space for expansion; free block and requested allocation are the same.");
488 delete $webvar{reserve};
489 } else {
490 # Find which new free block will match the reqested block.
491 # Take the requested mask, shift by one
492 my $tmpmask = $webvar{maskbits};
493 $tmpmask--;
494 # find the subnets with that mask in the selected free block
495 my @pieces = $alloc_from->split($tmpmask);
496 foreach my $slice (@pieces) {
497 if ($slice->contains($cidr)) {
498 # For the subnet that contains the requested block, split that in two,
499 # and flag/cache the one that's not the requested block.
500 my @bits = $slice->split($webvar{maskbits});
501 if ($bits[0] == $cidr) {
502 $resv = $bits[1];
503 } else {
504 $resv = $bits[0];
505 }
506 }
507 }
508 }
509 } # reserve block check
510
[21]511 } else { # done with direct freeblocks assignment
512
513 if (!$webvar{maskbits}) {
[517]514 $page->param(err => "Please specify a CIDR mask length.");
[111]515 return;
[21]516 }
[533]517
518##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
519 my $failmsg = "No suitable free block found.<br>\n";
[187]520 if ($webvar{alloctype} eq 'rm') {
[533]521 $failmsg .= "We do not have a free routeable block of that size.<br>\n".
522 "You will have to either route a set of smaller netblocks or a single smaller netblock.";
[4]523 } else {
[214]524 if ($webvar{alloctype} =~ /^.[pc]$/) {
[533]525 $failmsg .= "You will have to route another superblock from one of the<br>\n".
526 "master blocks or chose a smaller block size for the pool.";
[21]527 } else {
[517]528 if (!$webvar{pop}) {
529 $page->param(err => 'Please select a POP to route the block from/through.');
530 return;
531 }
[533]532 $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
[692]533 "from one of the master blocks";
534 if ($webvar{reserve}) {
535 $failmsg .= ', choose a smaller blocksize, or uncheck "Reserve space for expansion".';
536 } else {
537 $failmsg .= " or chose a smaller blocksize.";
538 }
[21]539 }
[4]540 }
[533]541
[692]542 # if requesting extra space "reserved for expansion", we need to find a free
543 # block at least double the size of the request.
544 if ($webvar{reserve}) {
545 $webvar{maskbits}--;
546 }
547
[633]548 ($fbid,$cidr,$p_id) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype},
549 $webvar{city}, $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
[533]550 if (!$cidr) {
[517]551 $page->param(err => $failmsg);
[111]552 return;
[21]553 }
[533]554 $cidr = new NetAddr::IP $cidr;
[575]555
556 $alloc_from = "$cidr";
[692]557
558 # when autofinding a block to allocate from, use the first piece of the found
559 # block for the allocation, and the next piece for the "reserved for expansion".
560 if ($webvar{reserve}) {
561 # reset the mask to the real requested one, now that we've got a
562 # block large enough for the request plus reserve
563 $webvar{maskbits}++;
564 ($cidr,$resv) = $cidr->split($webvar{maskbits});
565 }
566
[575]567 # If the block to be allocated is smaller than the one we found,
568 # figure out the "real" block to be allocated.
569 if ($cidr->masklen() ne $webvar{maskbits}) {
570 my $maskbits = $cidr->masklen();
571 my @subblocks;
572 while ($maskbits++ < $webvar{maskbits}) {
573 @subblocks = $cidr->split($maskbits);
574 }
575 $cidr = $subblocks[0];
576 }
[21]577 } # check for freeblocks assignment or IPDB-controlled assignment
[4]578
[674]579 # Generate the IP list for the new allocation in case someone wants to set per-IP rDNS right away.
580 # We don't do this on the previous page because we don't know how big a block or even what IP range
581 # it's for (if following the "normal" allocation process)
582 if ($IPDBacl{$authuser} =~ /c/
583 && $cidr->masklen != $cidr->bits
584 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
585 && $webvar{alloctype} !~ /^.[dpi]/
586 # do we want to allow v6 at all?
587 #&& ! $cidr->{isv6}
588 ) {
589 my @list;
590 foreach my $ip (@{$cidr->splitref()}) {
591 my %row;
592 $row{r_ip} = $ip->addr;
593 $row{iphost} = '';
594 push @list, \%row;
595 }
596 $page->param(r_iplist => \@list);
597 # We don't use this here, because these IPs should already be bare.
598 # ... or should we be paranoid? Make it a config option?
599 #getRDNSbyIP($ip_dbh, type => $webvar{alloctype}, range => "$cidr", user => $authuser) );
600 }
[157]601 } # if ($webvar{alloctype} =~ /^.i$/)
[4]602
[397]603## node hack
604 if ($webvar{node} && $webvar{node} ne '-') {
[530]605 my $nodename = getNodeName($ip_dbh, $webvar{node});
[517]606 $page->param(nodename => $nodename);
607 $page->param(nodeid => $webvar{node});
[397]608 }
609## end node hack
610
[692]611 # reserve for expansion
612 $page->param(reserve => $webvar{reserve});
613 # probably just preventing a little log noise doing this; could just set the param
614 # all the time since it won't be shown if the reserve param above isn't set.
615# if ($webvar{reserve}) {
616 $page->param(resvblock => $resv);
617# }
618
[4]619 # Stick in the allocation data
[517]620 $page->param(alloc_type => $webvar{alloctype});
621 $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
622 $page->param(alloc_from => $alloc_from);
[633]623 $page->param(parent => $p_id);
624 $page->param(fbid => $fbid);
[517]625 $page->param(cidr => $cidr);
[585]626 $page->param(rdns => $webvar{rdns});
[691]627 $page->param(vrf => $webvar{vrf});
628 $page->param(vlan => $webvar{vlan});
[517]629 $page->param(city => $q->escapeHTML($webvar{city}));
630 $page->param(custid => $webvar{custid});
631 $page->param(circid => $q->escapeHTML($webvar{circid}));
632 $page->param(desc => $q->escapeHTML($webvar{desc}));
[4]633
[517]634##fixme: find a way to have the displayed copy have <br> substitutions
635# for newlines, and the <input> value have either encoded or bare newlines.
636# Also applies to privdata.
637 $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
638
[284]639 # Check to see if user is allowed to do anything with sensitive data
640 my $privdata = '';
[517]641 $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'))
642 if $IPDBacl{$authuser} =~ /s/;
643
644 # Yay! This now has it's very own little home.
645 $page->param(billinguser => $webvar{userid})
[299]646 if $webvar{userid};
[284]647
[517]648##fixme: this is only needed iff confirm.tmpl and
649# confirmRemove.tmpl are merged (quite possible, just
650# a little tedious)
651 $page->param(action => "insert");
[284]652
[4]653} # end confirmAssign
654
655
656# Do the work of actually inserting a block in the database.
657sub insertAssign {
[233]658 if ($IPDBacl{$authuser} !~ /a/) {
[517]659 $aclerr = 'addblock';
[233]660 return;
661 }
[4]662 # Some things are done more than once.
[111]663 return if !validateInput();
[4]664
[284]665 if (!defined($webvar{privdata})) {
666 $webvar{privdata} = '';
667 }
[575]668
669 # split up some linked data for static IPs via guided allocation. needed for breadcrumbs lite.
[584]670 ($webvar{alloc_from},$webvar{rdepth}) = split /,/, $webvar{alloc_from} if $webvar{alloc_from} =~ /,/;
[575]671
[106]672 # $code is "success" vs "failure", $msg contains OK for a
673 # successful netblock allocation, the IP allocated for static
674 # IP, or the error message if an error occurred.
[517]675
[677]676##fixme: consider just passing \%webvar to allocateBlock()?
677 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
678 my %iprev;
679 foreach (keys %webvar) {
680 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
681 }
682
[691]683 # Easier to see and cosmetically fiddle the list like this
684 my %insert_args = (
685 cidr => $webvar{fullcidr},
686 fbid => $webvar{fbid},
[692]687 reserve => $webvar{reserve},
[691]688 parent => $webvar{parent},
689 custid => $webvar{custid},
690 type => $webvar{alloctype},
691 city => $webvar{city},
692 desc => $webvar{desc},
693 notes => $webvar{notes},
694 circid => $webvar{circid},
695 privdata => $webvar{privdata},
696 nodeid => $webvar{node},
697 rdns => $webvar{rdns},
698 vrf => $webvar{vrf},
699 vlan => $webvar{vlan},
700 user => $authuser,
701 );
[4]702
[691]703 my ($code,$msg) = allocateBlock($ip_dbh, %insert_args, iprev => \%iprev);
704
[111]705 if ($code eq 'OK') {
[106]706 if ($webvar{alloctype} =~ /^.i$/) {
[300]707 $msg =~ s|/32||;
[517]708 $page->param(staticip => $msg);
709 $page->param(custid => $webvar{custid});
[633]710 $page->param(parent => $webvar{parent}, pool => $webvar{alloc_from});
[517]711 $page->param(billinguser => $webvar{billinguser});
[416]712 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
713 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
714 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[106]715 } else {
[301]716 my $netblock = new NetAddr::IP $webvar{fullcidr};
[517]717 $page->param(fullcidr => $webvar{fullcidr});
718 $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
719 $page->param(custid => $webvar{custid});
[697]720
[575]721 # breadcrumbs lite! provide at least a link to the parent of the block we just allocated.
[633]722 my $binfo = getBlockData($ip_dbh, $webvar{parent});
723 $page->param(parentid => $webvar{parent});
724 $page->param(parentblock => $binfo->{block});
[697]725
726 # Full breadcrumbs
727 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
728 my @rcrumbs = reverse (@$crumbs);
729 $utilbar->param(breadcrumb => \@rcrumbs);
730
[517]731 if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
732 $page->param(billinguser => $webvar{billinguser});
733 $page->param(custid => $webvar{custid});
734 $page->param(netaddr => $netblock->addr);
735 $page->param(masklen => $netblock->masklen);
736 }
[416]737 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
738 "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
739 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[4]740 }
[106]741 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
[256]742 "'$webvar{alloctype}' ($msg)";
[111]743 } else {
744 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
745 "'$webvar{alloctype}' by $authuser failed: '$msg'";
[517]746 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
[157]747 " failed:<br>\n$msg\n");
[106]748 }
[4]749
750} # end insertAssign()
751
752
753# Does some basic checks on common input data to make sure nothing
754# *really* weird gets in to the database through this script.
755# Does NOT do complete input validation!!!
756sub validateInput {
757 if ($webvar{city} eq '-') {
[517]758 $page->param(err => 'Please choose a city');
[111]759 return;
[4]760 }
[138]761
762 # Alloctype check.
[4]763 chomp $webvar{alloctype};
[138]764 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
765 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
766 # managing to call things in such a way as to cause this deserves a cryptic error.
[517]767 $page->param(err => 'Invalid alloctype');
[138]768 return;
769 }
770
771 # CustID check
[4]772 # We have different handling for customer allocations and "internal" or "our" allocations
[214]773 if ($def_custids{$webvar{alloctype}} eq '') {
[4]774 if (!$webvar{custid}) {
[517]775 $page->param(err => 'Please enter a customer ID.');
[111]776 return;
[4]777 }
[546]778 # Crosscheck with billing.
779 my $status = CustIDCK->custid_exist($webvar{custid});
780 if ($CustIDCK::Error) {
781 $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
782 return;
[4]783 }
[546]784 if (!$status) {
785 $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
786 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
787 "non-customer assignments.");
788 return;
789 }
[400]790# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
[138]791 } else {
[167]792 # New! Improved! And now Loaded From The Database!!
[320]793 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
794 $webvar{custid} = $def_custids{$webvar{alloctype}};
795 }
[4]796 }
[111]797
[571]798## hmmm.... is this even useful?
799if (0) {
[111]800 # Check POP location
801 my $flag;
[187]802 if ($webvar{alloctype} eq 'rm') {
[111]803 $flag = 'for a routed netblock';
804 foreach (@poplist) {
805 if (/^$webvar{city}$/) {
806 $flag = 'n';
807 last;
808 }
809 }
810 } else {
811 $flag = 'n';
[442]812##fixme: hook to force-set POP or city on certain alloctypes
813# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
[536]814 if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
[111]815 $flag = 'to route the block from/through';
816 }
817 }
[517]818
819 # if the alloctype has a restricted city/POP list as determined above,
820 # and the reqested city/POP does not match that list, complain
[111]821 if ($flag ne 'n') {
[517]822 $page->param(err => "Please choose a valid POP location $flag. Valid ".
[111]823 "POP locations are currently:<br>\n".join (" - ", @poplist));
824 return;
825 }
[571]826}
[111]827
[691]828 # VRF. Not a full validity check, just a basic sanity check.
829 if ($webvar{vrf}) {
830 # Trim leading and trailing whitespace first
831 $webvar{vrf} =~ s/^\s+//;
832 $webvar{vrf} =~ s/\s+$//;
833 if ($webvar{vrf} !~ /^[\w\d_.-]{1,32}$/) {
834 $page->param(err => "VRF values may only contain alphanumerics, and may not be more than 32 characters");
835 return;
836 }
837 }
838
839 # VLAN. Should we allow/use VLAN names, or just the numeric ID?
840 if ($webvar{vlan}) {
841 # Trim leading and trailing whitespace first
842 $webvar{vlan} =~ s/^\s+//;
843 $webvar{vlan} =~ s/\s+$//;
844 # ... ve make it ze configurable thingy!
845 if ($IPDB::numeric_vlan) {
846 if ($webvar{vlan} !~ /^\d+$/) {
847 $page->param(err => "VLANs must be numeric");
848 return;
849 }
850 } else {
851 if ($webvar{vlan} !~ /^[\w\d_.-]+$/) {
852 $page->param(err => "VLANs must be alphanumeric");
853 return;
854 }
855 }
856 }
857
[111]858 return 'OK';
[4]859} # end validateInput
860
861
862# Displays details of a specific allocation in a form
863# Allows update/delete
864# action=edit
865sub edit {
866
[534]867 # snag block info from db
[634]868 my $blockinfo = getBlockData($ip_dbh, $webvar{id}, $webvar{basetype});
[691]869 $page->param(id => $webvar{id});
[634]870 $page->param(basetype => $webvar{basetype});
[4]871
[697]872 # Tree navigation
873 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
874 my @rcrumbs = reverse (@$crumbs);
875 $utilbar->param(breadcrumb => \@rcrumbs);
876
[534]877 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
878 $blockinfo->{type} =~ s/\s//;
[4]879
[675]880 my $cached;
[586]881 # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
[675]882 ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser);
883 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
[691]884 $page->param(cached => $cached);
[586]885
[675]886 my $cidr = new NetAddr::IP $blockinfo->{block};
887 # Limit the per-IP rDNS list based on CIDR length; larger ones just take up too much space.
888 # Also, don't show on IP pools; the individual IPs will have a space for rDNS
889 # Don't show on single IPs; these use the "pattern" field
890 if ($IPDBacl{$authuser} =~ /c/
891 && $cidr->masklen != $cidr->bits
892 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
[683]893 && $blockinfo->{type} !~ /^.[dpi]/
[675]894 # do we want to allow v6 at all?
895 #&& ! $cidr->{isv6}
896 ) {
897 $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type},
898 range => $blockinfo->{block}, user => $authuser) );
899 }
900
[691]901 # consider extending this to show time as well as date
902 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
903 $page->param(lastmod => $lastmod);
904# $page->param(lastmod => $blockinfo->{lastmod});
[4]905
[691]906 $page->param(block => $blockinfo->{block});
907 $page->param(city => $blockinfo->{city});
908 $page->param(custid => $blockinfo->{custid});
[4]909
[187]910##fixme The check here should be built from the database
[517]911# Need to expand to support pool types too
[534]912 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
[517]913 $page->param(changetype => 1);
914 $page->param(alloctype => [
[534]915 { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
916 { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
917 { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
918 { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
919 { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
920 { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
921 { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
[517]922 ]
923 );
924 } else {
[534]925 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
926 $page->param(type => $blockinfo->{type});
[517]927 }
928
[397]929## node hack
[634]930 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block});
931# $page->param(havenodeid => $nodeid);
932 $page->param(nodename => $nodename);
[517]933
934##fixme: this whole hack needs cleanup and generalization for all alloctypes
935##fixme: arguably a bug that presence of a nodeid implies it can be changed..
[634]936 if ($IPDBacl{$authuser} =~ /c/) {
937 my $nlist = getNodeList($ip_dbh);
938 if ($nodeid) {
[530]939 foreach (@{$nlist}) {
[634]940 $$_{selme} = ($$_{node_id} == $nodeid);
[397]941 }
942 }
[634]943 $page->param(nodelist => $nlist);
[397]944 }
945## end node hack
[517]946
[691]947 $page->param(rdns => $blockinfo->{rdns});
948 $page->param(vrf => $blockinfo->{vrf});
949 $page->param(vlan => $blockinfo->{vlan});
[33]950
[695]951 # Reserved-for-expansion
952 $page->param(reserve => $blockinfo->{reserve});
953 $page->param(reserve_id => $blockinfo->{reserve_id});
954 my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
955 $page->param(newblock => $newblock);
956
[517]957 # not happy with the upside-down logic, but...
[534]958 $page->param(swipable => $blockinfo->{type} !~ /.i/);
[691]959 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
[320]960
[691]961 $page->param(circid => $blockinfo->{circuitid});
962 $page->param(desc => $blockinfo->{description});
963 $page->param(notes => $blockinfo->{notes});
964
[284]965 # Check to see if we can display sensitive data
[691]966 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
[534]967 $page->param(privdata => $blockinfo->{privdata});
[284]968
[517]969 # ACL trickery - these two template booleans control the presence of all form/input tags
970 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
971 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
[4]972
[702]973 # Need to find internal knobs to twist to actually vary these. (Ab)use "change" flag for now
974 $page->param(maymerge => ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/));
975 if ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/) {
976 if ($blockinfo->{type} =~ /^.p$/) {
977 # PPP pools
978 $page->param(maysplit => 1) if $cidr->masklen+1 < $cidr->bits;
979 } elsif ($blockinfo->{type} =~ /.d/) {
980 # Non-PPP pools
981 $page->param(maysplit => 1) if $cidr->masklen+2 < $cidr->bits;
982 } else {
983 # Standard netblocks. Arguably allowing splitting these down to single IPs
984 # doesn't make much sense, but forcing users to apply allocation types
985 # "properly" is worse than herding cats.
986 $page->param(maysplit => 1) if $cidr->masklen < $cidr->bits;
987 }
988 }
989
[4]990} # edit()
991
992
993# Stuff new info about a block into the db
994# action=update
995sub update {
[284]996 if ($IPDBacl{$authuser} !~ /c/) {
[517]997 $aclerr = 'updateblock';
[284]998 return;
999 }
[4]1000
1001 # Make sure incoming data is in correct format - custID among other things.
[228]1002 return if !validateInput;
[4]1003
[536]1004 $webvar{swip} = 'n' if !$webvar{swip};
1005
[531]1006 my %updargs = (
1007 custid => $webvar{custid},
1008 city => $webvar{city},
1009 description => $webvar{desc},
1010 notes => $webvar{notes},
1011 circuitid => $webvar{circid},
1012 block => $webvar{block},
1013 type => $webvar{alloctype},
[536]1014 swip => $webvar{swip},
[588]1015 rdns => $webvar{rdns},
[691]1016 vrf => $webvar{vrf},
1017 vlan => $webvar{vlan},
[588]1018 user => $authuser,
[531]1019 );
1020
1021 # Semioptional values
1022 $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
1023 $updargs{node} = $webvar{node} if $webvar{node};
1024
[677]1025 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
1026 my %iprev;
1027 foreach (keys %webvar) {
1028 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
1029 }
[531]1030
[695]1031 # Merge with reserved freeblock
1032 $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme};
1033
[677]1034 my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
1035
[531]1036 if ($code eq 'FAIL') {
[166]1037 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
[517]1038 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
[111]1039 return;
[4]1040 }
1041
1042 # If we get here, the operation succeeded.
1043 syslog "notice", "$authuser updated $webvar{block}";
[531]1044##fixme: log details of the change? old way is in the .debug stream anyway.
[416]1045##fixme: need to wedge something in to allow "update:field" notifications
1046## hmm. how to tell what changed? O_o
[426]1047mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
[416]1048 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
[4]1049
[517]1050## node hack
1051 if ($webvar{node} && $webvar{node} ne '-') {
[530]1052 my $nodename = getNodeName($ip_dbh, $webvar{node});
[517]1053 $page->param(nodename => $nodename);
1054 }
1055## end node hack
1056
[380]1057 # Link back to browse-routed or list-pool page on "Update complete" page.
[634]1058 my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1059 my $pblock = getBlockData($ip_dbh, $binfo->{parent_id});
1060 $page->param(backid => $binfo->{parent_id});
1061 $page->param(backblock => $pblock->{block});
1062 $page->param(backpool => ($webvar{basetype} eq 'i'));
[380]1063
[536]1064 # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
1065 # because otherwise we can't convert \n to <br>. *sigh*
1066 $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
1067 $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
1068 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
1069 $webvar{privdata} =~ s/\n/<br>\n/;
1070
[634]1071 $page->param(cidr => $binfo->{block});
[588]1072 $page->param(rdns => $webvar{rdns});
[517]1073 $page->param(city => $webvar{city});
1074 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
1075 $page->param(custid => $webvar{custid});
1076 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
[536]1077 $page->param(circid => $webvar{circid});
1078 $page->param(desc => $webvar{desc});
1079 $page->param(notes => $webvar{notes});
[517]1080 $page->param(privdata => $webvar{privdata})
1081 if $IPDBacl{$authuser} =~ /s/;
[4]1082
1083} # update()
1084
1085
[702]1086sub prepSplit {
1087 if ($IPDBacl{$authuser} !~ /c/) {
1088 $aclerr = 'splitblock';
1089 return;
1090 }
1091
1092 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1093
1094 if ($blockinfo->{type} =~ /^.i$/) {
1095 $page->param(err => "Can't split a single IP allocation");
1096 return;
1097 }
1098
1099 # Info about current allocation
1100 $page->param(oldblock => $blockinfo->{block});
1101 $page->param(block => $webvar{block});
1102
1103# Note that there are probably different rules that should be followed to restrict splitting IPv6 blocks;
1104# strictly speaking it will be exceptionally rare to see smaller than a /64 assigned to a customer, since that
1105# breaks auto-addressing schemes.
1106
1107 # Generate possible splits
1108 my $block = new NetAddr::IP $blockinfo->{block};
1109 my $oldmask = $block->masklen;
1110 if ($blockinfo->{type} =~ /^.d$/) {
1111 # Non-PPP pools
1112 if ($oldmask+2 >= $block->bits) {
1113 $page->param(err => "Can't split a standard netblock pool any further");
1114 return;
1115 }
1116 # Allow splitting down to v4 /30 (which results in one usable IP; dubiously useful)
1117 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-2;
1118 } elsif ($blockinfo->{type} =~ /.p/) {
1119 # Allow splitting PPP pools down to v4 /31
1120 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-1;
1121 } else {
1122 # Allow splitting all other non-pool netblocks down to single IPs, which...
1123 # arguably should be *aggregated* in a pool. Except where they shouldn't.
1124 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits;
1125 }
1126 # set the split-in-half mask
1127 $page->param(sp2mask => $oldmask+1);
1128
1129 # Generate possible shrink targets
1130 my @keepers = $block->split($block->masklen+1);
1131 $page->param(newblockA => $keepers[0]);
1132 $page->param(newblockB => $keepers[1]);
1133} # prepSplit()
1134
1135
1136sub doSplit {
1137 if ($IPDBacl{$authuser} !~ /c/) {
1138 $aclerr = 'splitblock';
1139 return;
1140 }
1141
1142##fixme: need consistent way to identify "this thing that is this thing" with only the ID
1143# also applies to other locations
1144 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1145
1146 if ($blockinfo->{type} =~ /^.i$/) {
1147 $page->param(err => "Can't split a single IP allocation");
1148 return;
1149 }
1150
1151 if ($webvar{subact} eq 'split') {
1152 $page->param(issplit => 1);
1153 my $binfo = getBlockData($ip_dbh, $webvar{block});
1154 $page->param(cidr => $binfo->{block});
1155 my $block = new NetAddr::IP $binfo->{block};
1156 my $newblocks = splitBlock($ip_dbh, $webvar{block}, 'b', $webvar{split});
1157 if ($newblocks) {
1158 $page->param(newblocks => $newblocks);
1159 # and the backlink to the parent container
1160 my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
1161 $page->param(backid => $binfo->{parent_id});
1162 $page->param(backblock => $pinfo->{block});
1163 } else {
1164 $page->param(err => $IPDB::errstr);
1165 }
1166
1167 } else {
1168 # Shrink
1169 }
1170} # doSplit()
1171
1172
[4]1173# Delete an allocation.
[106]1174sub remove {
[233]1175 if ($IPDBacl{$authuser} !~ /d/) {
[517]1176 $aclerr = 'delblock';
[233]1177 return;
1178 }
1179
[4]1180 # Serves'em right for getting here...
1181 if (!defined($webvar{block})) {
[517]1182 $page->param(err => "Can't delete a block that doesn't exist");
[111]1183 return;
[4]1184 }
1185
[538]1186 my $blockdata;
[638]1187 $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
[4]1188
[638]1189 if ($blockdata->{parent_id} == 0) { # $webvar{alloctype} eq 'mm'
[538]1190 $blockdata->{city} = "N/A";
1191 $blockdata->{custid} = "N/A";
1192 $blockdata->{circuitid} = "N/A";
1193 $blockdata->{description} = "N/A";
1194 $blockdata->{notes} = "N/A";
1195 $blockdata->{privdata} = "N/A";
[638]1196 } # end cases for different alloctypes
[517]1197
[638]1198 $page->param(blockid => $webvar{block});
1199 $page->param(basetype => $webvar{basetype});
[4]1200
[538]1201 $page->param(block => $blockdata->{block});
[589]1202 $page->param(rdns => $blockdata->{rdns});
1203
1204 # maybe need to apply more magic here?
1205 # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
1206 # -> all real blocks (nb: pool IPs need extra handling)
1207 # -> NOC/private-IP (how to ID?)
1208 # -> anything with a pattern matching $IPDB::domain?
1209 if ($blockdata->{type} !~ /^.i$/) {
1210 $page->param(autodel => 1);
1211 }
1212
[538]1213 $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
1214 $page->param(city => $blockdata->{city});
1215 $page->param(custid => $blockdata->{custid});
1216 $page->param(circid => $blockdata->{circuitid});
1217 $page->param(desc => $blockdata->{description});
1218 $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
1219 $blockdata->{notes} =~ s/\n/<br>\n/;
1220 $page->param(notes => $blockdata->{notes});
1221 $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
[540]1222 $blockdata->{privdata} = '&nbsp;' if !$blockdata->{privdata};
[538]1223 $blockdata->{privdata} =~ s/\n/<br>\n/;
1224 $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
1225 $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
[4]1226
[517]1227} # end remove()
[4]1228
1229
1230# Delete an allocation. Return it to the freeblocks table; munge
1231# data as necessary to keep as few records as possible in freeblocks
1232# to prevent weirdness when allocating blocks later.
1233# Remove IPs from pool listing if necessary
1234sub finalDelete {
[233]1235 if ($IPDBacl{$authuser} !~ /d/) {
[517]1236 $aclerr = 'delblock';
[233]1237 return;
1238 }
[4]1239
[370]1240 # need to retrieve block data before deleting so we can notify on that
[638]1241 my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1242 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b');
[370]1243
[638]1244 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser);
[4]1245
[638]1246 $page->param(block => $blockinfo->{block});
[651]1247 $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}}));
[638]1248 $page->param(delparent_id => $blockinfo->{parent_id});# if $webvar{rdepth};
[651]1249 if ($pinfo) {
1250 $page->param(delparent => $pinfo->{block});
1251 $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}}));
1252 }
[638]1253 $page->param(returnpool => ($webvar{basetype} eq 'i') );
[591]1254 if ($code =~ /^WARN(POOL|MERGE)/) {
[638]1255 my ($pid,$pcidr) = split /,/, $msg;
[654]1256 my $real_pinfo = getBlockData($ip_dbh, $pid, 'b');
[638]1257 $page->param(parent_id => $pid);
1258 $page->param(parent => $pcidr);
[654]1259 $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}}));
[577]1260 $page->param(mergeip => $code eq 'WARNPOOL');
1261 }
[591]1262 if ($code eq 'WARN') {
1263 $msg =~ s/\n/<br>\n/g;
1264 $page->param(genwarn => $msg);
1265 }
[577]1266 if ($code eq 'OK' || $code =~ /^WARN/) {
[638]1267 syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock $webvar{block} ".
[528]1268 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
[638]1269 mailNotify($ip_dbh, 'da', "REMOVED: ".$disp_alloctypes{$blockinfo->{type}}." $webvar{block}",
1270 $disp_alloctypes{$blockinfo->{type}}." $webvar{block} deallocated by $authuser\n".
[528]1271 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
1272 "\nDescription: ".$blockinfo->{description}."\n");
[106]1273 } else {
[517]1274 $page->param(failmsg => $msg);
[106]1275 if ($webvar{alloctype} =~ /^.i$/) {
1276 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1277 } else {
1278 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
[517]1279 $page->param(netblock => 1);
[4]1280 }
[106]1281 }
[4]1282
1283} # finalDelete
Note: See TracBrowser for help on using the repository browser.