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

Last change on this file since 765 was 765, checked in by Kris Deugau, 9 years ago

/trunk

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