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

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

/trunk

Add breadcrumb navigation to "update complete" page

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 44.3 KB
RevLine 
[4]1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
[8]3###
4# SVN revision info
5# $Date: 2015-02-26 18:15:15 +0000 (Thu, 26 Feb 2015) $
6# SVN revision $Rev: 706 $
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
[705]877 # Show link to IP list for pools
878 $page->param(ispool => 1) if $blockinfo->{type} =~ /^.[dp]$/;
879
[534]880 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
881 $blockinfo->{type} =~ s/\s//;
[4]882
[675]883 my $cached;
[586]884 # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
[675]885 ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser);
886 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
[691]887 $page->param(cached => $cached);
[586]888
[675]889 my $cidr = new NetAddr::IP $blockinfo->{block};
890 # Limit the per-IP rDNS list based on CIDR length; larger ones just take up too much space.
891 # Also, don't show on IP pools; the individual IPs will have a space for rDNS
892 # Don't show on single IPs; these use the "pattern" field
893 if ($IPDBacl{$authuser} =~ /c/
894 && $cidr->masklen != $cidr->bits
895 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
[683]896 && $blockinfo->{type} !~ /^.[dpi]/
[675]897 # do we want to allow v6 at all?
898 #&& ! $cidr->{isv6}
899 ) {
900 $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type},
901 range => $blockinfo->{block}, user => $authuser) );
902 }
903
[691]904 # consider extending this to show time as well as date
905 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
906 $page->param(lastmod => $lastmod);
907# $page->param(lastmod => $blockinfo->{lastmod});
[4]908
[691]909 $page->param(block => $blockinfo->{block});
910 $page->param(city => $blockinfo->{city});
911 $page->param(custid => $blockinfo->{custid});
[4]912
[187]913##fixme The check here should be built from the database
[517]914# Need to expand to support pool types too
[534]915 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
[517]916 $page->param(changetype => 1);
917 $page->param(alloctype => [
[534]918 { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
919 { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
920 { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
921 { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
922 { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
923 { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
924 { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
[517]925 ]
926 );
927 } else {
[534]928 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
929 $page->param(type => $blockinfo->{type});
[517]930 }
931
[397]932## node hack
[634]933 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block});
934# $page->param(havenodeid => $nodeid);
935 $page->param(nodename => $nodename);
[517]936
937##fixme: this whole hack needs cleanup and generalization for all alloctypes
938##fixme: arguably a bug that presence of a nodeid implies it can be changed..
[634]939 if ($IPDBacl{$authuser} =~ /c/) {
940 my $nlist = getNodeList($ip_dbh);
941 if ($nodeid) {
[530]942 foreach (@{$nlist}) {
[634]943 $$_{selme} = ($$_{node_id} == $nodeid);
[397]944 }
945 }
[634]946 $page->param(nodelist => $nlist);
[397]947 }
948## end node hack
[517]949
[691]950 $page->param(rdns => $blockinfo->{rdns});
951 $page->param(vrf => $blockinfo->{vrf});
952 $page->param(vlan => $blockinfo->{vlan});
[33]953
[695]954 # Reserved-for-expansion
955 $page->param(reserve => $blockinfo->{reserve});
956 $page->param(reserve_id => $blockinfo->{reserve_id});
957 my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
958 $page->param(newblock => $newblock);
959
[517]960 # not happy with the upside-down logic, but...
[534]961 $page->param(swipable => $blockinfo->{type} !~ /.i/);
[691]962 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
[320]963
[691]964 $page->param(circid => $blockinfo->{circuitid});
965 $page->param(desc => $blockinfo->{description});
966 $page->param(notes => $blockinfo->{notes});
967
[284]968 # Check to see if we can display sensitive data
[691]969 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
[534]970 $page->param(privdata => $blockinfo->{privdata});
[284]971
[517]972 # ACL trickery - these two template booleans control the presence of all form/input tags
973 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
974 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
[4]975
[702]976 # Need to find internal knobs to twist to actually vary these. (Ab)use "change" flag for now
977 $page->param(maymerge => ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/));
978 if ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/) {
979 if ($blockinfo->{type} =~ /^.p$/) {
980 # PPP pools
981 $page->param(maysplit => 1) if $cidr->masklen+1 < $cidr->bits;
982 } elsif ($blockinfo->{type} =~ /.d/) {
983 # Non-PPP pools
984 $page->param(maysplit => 1) if $cidr->masklen+2 < $cidr->bits;
985 } else {
986 # Standard netblocks. Arguably allowing splitting these down to single IPs
987 # doesn't make much sense, but forcing users to apply allocation types
988 # "properly" is worse than herding cats.
989 $page->param(maysplit => 1) if $cidr->masklen < $cidr->bits;
990 }
991 }
992
[4]993} # edit()
994
995
996# Stuff new info about a block into the db
997# action=update
998sub update {
[284]999 if ($IPDBacl{$authuser} !~ /c/) {
[517]1000 $aclerr = 'updateblock';
[284]1001 return;
1002 }
[4]1003
[706]1004 # Collect existing block info here, since we need it for the breadcrumb nav
1005 my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1006 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1007 my @rcrumbs = reverse (@$crumbs);
1008 $utilbar->param(breadcrumb => \@rcrumbs);
1009
[4]1010 # Make sure incoming data is in correct format - custID among other things.
[228]1011 return if !validateInput;
[4]1012
[536]1013 $webvar{swip} = 'n' if !$webvar{swip};
1014
[531]1015 my %updargs = (
1016 custid => $webvar{custid},
1017 city => $webvar{city},
1018 description => $webvar{desc},
1019 notes => $webvar{notes},
1020 circuitid => $webvar{circid},
1021 block => $webvar{block},
1022 type => $webvar{alloctype},
[536]1023 swip => $webvar{swip},
[588]1024 rdns => $webvar{rdns},
[691]1025 vrf => $webvar{vrf},
1026 vlan => $webvar{vlan},
[588]1027 user => $authuser,
[531]1028 );
1029
1030 # Semioptional values
1031 $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
1032 $updargs{node} = $webvar{node} if $webvar{node};
1033
[677]1034 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
1035 my %iprev;
1036 foreach (keys %webvar) {
1037 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
1038 }
[531]1039
[695]1040 # Merge with reserved freeblock
1041 $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme};
1042
[677]1043 my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
1044
[531]1045 if ($code eq 'FAIL') {
[166]1046 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
[517]1047 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
[111]1048 return;
[4]1049 }
1050
1051 # If we get here, the operation succeeded.
1052 syslog "notice", "$authuser updated $webvar{block}";
[531]1053##fixme: log details of the change? old way is in the .debug stream anyway.
[416]1054##fixme: need to wedge something in to allow "update:field" notifications
1055## hmm. how to tell what changed? O_o
[426]1056mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
[416]1057 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
[4]1058
[517]1059## node hack
1060 if ($webvar{node} && $webvar{node} ne '-') {
[530]1061 my $nodename = getNodeName($ip_dbh, $webvar{node});
[517]1062 $page->param(nodename => $nodename);
1063 }
1064## end node hack
1065
[380]1066 # Link back to browse-routed or list-pool page on "Update complete" page.
[634]1067 my $pblock = getBlockData($ip_dbh, $binfo->{parent_id});
1068 $page->param(backid => $binfo->{parent_id});
1069 $page->param(backblock => $pblock->{block});
1070 $page->param(backpool => ($webvar{basetype} eq 'i'));
[380]1071
[536]1072 # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
1073 # because otherwise we can't convert \n to <br>. *sigh*
1074 $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
1075 $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
1076 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
1077 $webvar{privdata} =~ s/\n/<br>\n/;
1078
[634]1079 $page->param(cidr => $binfo->{block});
[588]1080 $page->param(rdns => $webvar{rdns});
[517]1081 $page->param(city => $webvar{city});
1082 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
1083 $page->param(custid => $webvar{custid});
1084 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
[536]1085 $page->param(circid => $webvar{circid});
1086 $page->param(desc => $webvar{desc});
1087 $page->param(notes => $webvar{notes});
[517]1088 $page->param(privdata => $webvar{privdata})
1089 if $IPDBacl{$authuser} =~ /s/;
[4]1090
1091} # update()
1092
1093
[702]1094sub prepSplit {
1095 if ($IPDBacl{$authuser} !~ /c/) {
1096 $aclerr = 'splitblock';
1097 return;
1098 }
1099
1100 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1101
[705]1102 # Tree navigation
1103 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1104 my @rcrumbs = reverse (@$crumbs);
1105 $utilbar->param(breadcrumb => \@rcrumbs);
1106
[702]1107 if ($blockinfo->{type} =~ /^.i$/) {
1108 $page->param(err => "Can't split a single IP allocation");
1109 return;
1110 }
1111
1112 # Info about current allocation
1113 $page->param(oldblock => $blockinfo->{block});
1114 $page->param(block => $webvar{block});
1115
1116# Note that there are probably different rules that should be followed to restrict splitting IPv6 blocks;
1117# strictly speaking it will be exceptionally rare to see smaller than a /64 assigned to a customer, since that
1118# breaks auto-addressing schemes.
1119
1120 # Generate possible splits
1121 my $block = new NetAddr::IP $blockinfo->{block};
1122 my $oldmask = $block->masklen;
1123 if ($blockinfo->{type} =~ /^.d$/) {
1124 # Non-PPP pools
[705]1125 $page->param(ispool => 1);
[702]1126 if ($oldmask+2 >= $block->bits) {
1127 $page->param(err => "Can't split a standard netblock pool any further");
1128 return;
1129 }
1130 # Allow splitting down to v4 /30 (which results in one usable IP; dubiously useful)
1131 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-2;
1132 } elsif ($blockinfo->{type} =~ /.p/) {
[705]1133 $page->param(ispool => 1);
[702]1134 # Allow splitting PPP pools down to v4 /31
1135 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-1;
1136 } else {
1137 # Allow splitting all other non-pool netblocks down to single IPs, which...
1138 # arguably should be *aggregated* in a pool. Except where they shouldn't.
1139 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits;
1140 }
1141 # set the split-in-half mask
1142 $page->param(sp2mask => $oldmask+1);
1143
1144 # Generate possible shrink targets
1145 my @keepers = $block->split($block->masklen+1);
1146 $page->param(newblockA => $keepers[0]);
1147 $page->param(newblockB => $keepers[1]);
1148} # prepSplit()
1149
1150
1151sub doSplit {
1152 if ($IPDBacl{$authuser} !~ /c/) {
1153 $aclerr = 'splitblock';
1154 return;
1155 }
1156
1157##fixme: need consistent way to identify "this thing that is this thing" with only the ID
1158# also applies to other locations
1159 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1160
[705]1161 # Tree navigation
1162 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1163 my @rcrumbs = reverse (@$crumbs);
1164 $utilbar->param(breadcrumb => \@rcrumbs);
1165
[702]1166 if ($blockinfo->{type} =~ /^.i$/) {
1167 $page->param(err => "Can't split a single IP allocation");
1168 return;
1169 }
1170
1171 if ($webvar{subact} eq 'split') {
1172 $page->param(issplit => 1);
[705]1173 my $block = new NetAddr::IP $blockinfo->{block};
[702]1174 my $newblocks = splitBlock($ip_dbh, $webvar{block}, 'b', $webvar{split});
1175 if ($newblocks) {
1176 $page->param(newblocks => $newblocks);
1177 } else {
1178 $page->param(err => $IPDB::errstr);
1179 }
1180
[705]1181 } elsif ($webvar{subact} eq 'shrink') {
1182 $page->param(nid => $webvar{block});
1183 $page->param(newblock => $webvar{shrink});
1184 my $newfree = shrinkBlock($ip_dbh, $webvar{block}, $webvar{shrink});
1185 if ($newfree) {
1186 $page->param(newfb => $newfree);
1187 } else {
1188 $page->param(err => $IPDB::errstr);
1189 }
1190
[702]1191 } else {
[705]1192 # Your llama is on fire.
1193 $page->param(err => "Missing form field that shouldn't be missing.");
1194 return;
[702]1195 }
[705]1196
1197 # common bits
1198 $page->param(cidr => $blockinfo->{block});
1199 # and the backlink to the parent container
1200 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id});
1201 $page->param(backid => $blockinfo->{parent_id});
1202 $page->param(backblock => $pinfo->{block});
[702]1203} # doSplit()
1204
1205
[4]1206# Delete an allocation.
[106]1207sub remove {
[233]1208 if ($IPDBacl{$authuser} !~ /d/) {
[517]1209 $aclerr = 'delblock';
[233]1210 return;
1211 }
1212
[4]1213 # Serves'em right for getting here...
1214 if (!defined($webvar{block})) {
[517]1215 $page->param(err => "Can't delete a block that doesn't exist");
[111]1216 return;
[4]1217 }
1218
[538]1219 my $blockdata;
[638]1220 $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
[4]1221
[638]1222 if ($blockdata->{parent_id} == 0) { # $webvar{alloctype} eq 'mm'
[538]1223 $blockdata->{city} = "N/A";
1224 $blockdata->{custid} = "N/A";
1225 $blockdata->{circuitid} = "N/A";
1226 $blockdata->{description} = "N/A";
1227 $blockdata->{notes} = "N/A";
1228 $blockdata->{privdata} = "N/A";
[638]1229 } # end cases for different alloctypes
[517]1230
[638]1231 $page->param(blockid => $webvar{block});
1232 $page->param(basetype => $webvar{basetype});
[4]1233
[538]1234 $page->param(block => $blockdata->{block});
[589]1235 $page->param(rdns => $blockdata->{rdns});
1236
1237 # maybe need to apply more magic here?
1238 # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
1239 # -> all real blocks (nb: pool IPs need extra handling)
1240 # -> NOC/private-IP (how to ID?)
1241 # -> anything with a pattern matching $IPDB::domain?
1242 if ($blockdata->{type} !~ /^.i$/) {
1243 $page->param(autodel => 1);
1244 }
1245
[538]1246 $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
1247 $page->param(city => $blockdata->{city});
1248 $page->param(custid => $blockdata->{custid});
1249 $page->param(circid => $blockdata->{circuitid});
1250 $page->param(desc => $blockdata->{description});
1251 $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
1252 $blockdata->{notes} =~ s/\n/<br>\n/;
1253 $page->param(notes => $blockdata->{notes});
1254 $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
[540]1255 $blockdata->{privdata} = '&nbsp;' if !$blockdata->{privdata};
[538]1256 $blockdata->{privdata} =~ s/\n/<br>\n/;
1257 $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
1258 $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
[4]1259
[517]1260} # end remove()
[4]1261
1262
1263# Delete an allocation. Return it to the freeblocks table; munge
1264# data as necessary to keep as few records as possible in freeblocks
1265# to prevent weirdness when allocating blocks later.
1266# Remove IPs from pool listing if necessary
1267sub finalDelete {
[233]1268 if ($IPDBacl{$authuser} !~ /d/) {
[517]1269 $aclerr = 'delblock';
[233]1270 return;
1271 }
[4]1272
[370]1273 # need to retrieve block data before deleting so we can notify on that
[638]1274 my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1275 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b');
[370]1276
[638]1277 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser);
[4]1278
[638]1279 $page->param(block => $blockinfo->{block});
[651]1280 $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}}));
[638]1281 $page->param(delparent_id => $blockinfo->{parent_id});# if $webvar{rdepth};
[651]1282 if ($pinfo) {
1283 $page->param(delparent => $pinfo->{block});
1284 $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}}));
1285 }
[638]1286 $page->param(returnpool => ($webvar{basetype} eq 'i') );
[591]1287 if ($code =~ /^WARN(POOL|MERGE)/) {
[638]1288 my ($pid,$pcidr) = split /,/, $msg;
[654]1289 my $real_pinfo = getBlockData($ip_dbh, $pid, 'b');
[638]1290 $page->param(parent_id => $pid);
1291 $page->param(parent => $pcidr);
[654]1292 $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}}));
[577]1293 $page->param(mergeip => $code eq 'WARNPOOL');
1294 }
[591]1295 if ($code eq 'WARN') {
1296 $msg =~ s/\n/<br>\n/g;
1297 $page->param(genwarn => $msg);
1298 }
[577]1299 if ($code eq 'OK' || $code =~ /^WARN/) {
[638]1300 syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock $webvar{block} ".
[528]1301 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
[638]1302 mailNotify($ip_dbh, 'da', "REMOVED: ".$disp_alloctypes{$blockinfo->{type}}." $webvar{block}",
1303 $disp_alloctypes{$blockinfo->{type}}." $webvar{block} deallocated by $authuser\n".
[528]1304 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
1305 "\nDescription: ".$blockinfo->{description}."\n");
[106]1306 } else {
[517]1307 $page->param(failmsg => $msg);
[106]1308 if ($webvar{alloctype} =~ /^.i$/) {
1309 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1310 } else {
1311 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
[517]1312 $page->param(netblock => 1);
[4]1313 }
[106]1314 }
[4]1315
1316} # finalDelete
Note: See TracBrowser for help on using the repository browser.