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

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

/trunk

Fixes for "Allocation complete" screen:

  • Refine and tune order and source of information for breadcrumbs correctness on guided allocation of a static IP
  • Remove a little stale code lingering from "rdepth"
  • Tweak template parameters and CSS/layout for error reporting
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 49.7 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3###
4# SVN revision info
5# $Date: 2015-08-24 22:10:42 +0000 (Mon, 24 Aug 2015) $
6# SVN revision $Rev: 766 $
7# Last update by $Author: kdeugau $
8###
9# Copyright (C) 2004-2010 - Kris Deugau
10
11use strict;
12use warnings;
13use CGI::Carp qw(fatalsToBrowser);
14use CGI::Simple;
15use HTML::Template;
16use DBI;
17use POSIX qw(ceil);
18use NetAddr::IP;
19use Frontier::Client;
20
21use Sys::Syslog;
22
23# don't remove! required for GNU/FHS-ish install from tarball
24##uselib##
25
26use CustIDCK;
27use MyIPDB;
28
29openlog "IPDB","pid","$IPDB::syslog_facility";
30
31## Environment. Collect some things, process some things, set some things...
32
33# Collect the username from HTTP auth. If undefined, we're in
34# a test environment, or called without a username.
35my $authuser;
36if (!defined($ENV{'REMOTE_USER'})) {
37 $authuser = '__temptest';
38} else {
39 $authuser = $ENV{'REMOTE_USER'};
40}
41
42# anyone got a better name? :P
43my $thingroot = $ENV{SCRIPT_FILENAME};
44$thingroot =~ s|cgi-bin/main.cgi||;
45
46syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
47
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
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;
64($ip_dbh,$errstr) = connectDB_My;
65if (!$ip_dbh) {
66 $webvar{action} = "dberr";
67} else {
68 initIPDBGlobals($ip_dbh);
69}
70
71# Set up some globals
72$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
73
74my $header = HTML::Template->new(filename => "header.tmpl");
75my $footer = HTML::Template->new(filename => "footer.tmpl");
76my $utilbar = HTML::Template->new(filename => "utilbar.tmpl", loop_context_vars => 1, global_vars => 1);
77
78print "Content-type: text/html\n\n";
79
80$header->param(version => $IPDB::VERSION);
81$header->param(addperm => $IPDBacl{$authuser} =~ /a/);
82$header->param(webpath => $IPDB::webpath);
83
84$utilbar->param(webpath => $IPDB::webpath);
85
86print $header->output;
87
88##fixme: whine and complain when the user is not present in the ACL hash above
89
90#main()
91my $aclerr;
92
93if(!defined($webvar{action})) {
94 $webvar{action} = "index"; #shuts up the warnings.
95}
96
97my $page;
98if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
99 $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1);
100} else {
101 $page = HTML::Template->new(filename => "dunno.tmpl");
102}
103
104if($webvar{action} eq 'index') {
105 showSummary();
106} elsif ($webvar{action} eq 'addmaster') {
107 if ($IPDBacl{$authuser} !~ /a/) {
108 $aclerr = 'addmaster';
109 }
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 );
118 my $result = IPDB::_rpc('getLocDropdown', %rpcargs);
119 $page->param(loclist => $result);
120 }
121
122} elsif ($webvar{action} eq 'newmaster') {
123
124 if ($IPDBacl{$authuser} !~ /a/) {
125 $aclerr = 'addmaster';
126 } else {
127 my $cidr = new NetAddr::IP $webvar{cidr};
128 $page->param(cidr => "$cidr");
129
130 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr}, (vrf => $webvar{vrf}, rdns => $webvar{rdns},
131 rwhois => $webvar{rwhois}, defloc => $webvar{loc}, user => $authuser) );
132
133 if ($code eq 'FAIL') {
134 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
135 $page->param(err => $msg);
136 } else {
137 $page->param(parent => $msg);
138 if ($code eq 'WARN') {
139 $IPDB::errstr =~ s/\n\n/<br>\n/g;
140 $IPDB::errstr =~ s/:\n/:<br>\n/g;
141 $page->param(warn => $IPDB::errstr);
142 }
143 syslog "info", "$authuser added master block $webvar{cidr}";
144 }
145
146 } # ACL check
147
148} # end add new master
149
150elsif ($webvar{action} eq 'showsubs') {
151 showSubs();
152}
153
154elsif($webvar{action} eq 'listpool') {
155 showPool();
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}
174elsif($webvar{action} eq 'split') {
175 prepSplit();
176}
177elsif($webvar{action} eq 'dosplit') {
178 doSplit();
179}
180elsif($webvar{action} eq 'merge') {
181 prepMerge();
182}
183elsif($webvar{action} eq 'confmerge') {
184 confMerge();
185}
186elsif($webvar{action} eq 'domerge') {
187 doMerge();
188}
189elsif($webvar{action} eq 'delete') {
190 remove();
191}
192elsif($webvar{action} eq 'finaldelete') {
193 finalDelete();
194}
195elsif ($webvar{action} eq 'nodesearch') {
196 my $nodelist = getNodeList($ip_dbh);
197 $page->param(nodelist => $nodelist);
198}
199
200# DB failure. Can't do much here, really.
201elsif ($webvar{action} eq 'dberr') {
202 $page->param(errmsg => $errstr);
203}
204
205# Default is an error. It shouldn't be possible to get here unless you're
206# randomly feeding in values for webvar{action}.
207else {
208 my $rnd = rand 500;
209 my $boing = sprintf("%.2f", rand 500);
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]);
223}
224## Finally! Done with that NASTY "case" emulation!
225
226
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}
235
236# Clean up IPDB globals, DB handle, etc.
237finish($ip_dbh);
238
239## Do all our printing here so we can generate errors and stick them into the slots in the templates.
240
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);
244print $utilbar->output;
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
252# Just in case something waaaayyy down isn't in place
253# properly... we exit explicitly.
254exit 0;
255
256
257# Initial display: Show master blocks with total allocated subnets, total free subnets
258sub showSummary {
259 my $masterlist = listSummary($ip_dbh);
260 $page->param(masterlist => $masterlist);
261
262 $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
263} # showSummary
264
265
266# Display blocks immediately within a given parent
267sub showSubs {
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
283 # 3-part layout; containers, end-use allocations, and free blocks
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
297 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
298
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
302 my $crumbs = getBreadCrumbs($ip_dbh, $pinfo->{parent_id});
303 my @rcrumbs = reverse (@$crumbs);
304 $utilbar->param(breadcrumb => \@rcrumbs);
305
306 $page->param(self_id => $webvar{parent});
307 $page->param(block => $pinfo->{block});
308 $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
309
310 my $flist = listFree($ip_dbh, parent => $webvar{parent});
311 $page->param(freelist => $flist);
312} # showSubs
313
314
315# List the IPs used in a pool
316sub showPool {
317
318 my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
319 my $cidr = new NetAddr::IP $poolinfo->{block};
320
321 # Tree navigation
322 my $crumbs = getBreadCrumbs($ip_dbh, $poolinfo->{parent_id});
323 my @rcrumbs = reverse (@$crumbs);
324 $utilbar->param(breadcrumb => \@rcrumbs);
325
326 $page->param(block => $cidr);
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);
333
334 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
335 $page->param(city => $poolinfo->{city});
336
337 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
338 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
339
340# probably have to add an "edit IP allocation" link here somewhere.
341
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/;
346 }
347 $page->param(poolips => $plist);
348} # end showPool
349
350
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.
353sub assignBlock {
354
355 if ($IPDBacl{$authuser} !~ /a/) {
356 $aclerr = 'addblock';
357 return;
358 }
359
360 # hack pthbttt eww
361 $webvar{parent} = 0 if !$webvar{parent};
362 $webvar{block} = '' if !$webvar{block};
363
364 $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
365
366 if ($webvar{fbid} || $webvar{fbtype}) {
367
368 # Common case, according to reported usage. Block to assign is specified.
369 my $block = new NetAddr::IP $webvar{block};
370
371 my ($rdns,$cached) = getBlockRDNS($ip_dbh, id => $webvar{parent}, type => $webvar{fbtype}, user => $authuser);
372 $page->param(rdns => $rdns) if $rdns;
373 $page->param(parent => $webvar{parent});
374 $page->param(fbid => $webvar{fbid});
375 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
376 $page->param(cached => $cached);
377
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
382 # Tree navigation
383 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
384 my @rcrumbs = reverse (@$crumbs);
385 $utilbar->param(breadcrumb => \@rcrumbs);
386
387 $webvar{fbtype} = '' if !$webvar{fbtype};
388 if ($webvar{fbtype} eq 'i') {
389 my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i');
390 $page->param(
391 fbip => 1,
392 block => $ipinfo->{block},
393 fbdisptype => $list_alloctypes{$ipinfo->{type}},
394 type => $ipinfo->{type},
395 allocfrom => $pinfo->{block},
396 );
397 } else {
398 # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
399 my $tlist = getTypeList($ip_dbh, 'n');
400 $tlist->[0]->{sel} = 1;
401 $page->param(typelist => $tlist, block => $block);
402 }
403
404 } else {
405
406 # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
407 my $mlist = getMasterList($ip_dbh, 'c');
408 $page->param(masterlist => $mlist);
409
410 my @pops;
411 foreach my $pop (@citylist) {
412 my %row = (pop => $pop);
413 push (@pops, \%row);
414 }
415 $page->param(pops => \@pops);
416
417 # get all standard alloctypes
418 my $tlist = getTypeList($ip_dbh, 'a');
419 $tlist->[0]->{sel} = 1;
420 $page->param(typelist => $tlist);
421 }
422
423 my @cities;
424 foreach my $city (@citylist) {
425 my %row = (city => $city);
426 push (@cities, \%row);
427 }
428 $page->param(citylist => \@cities);
429
430## node hack
431 my $nlist = getNodeList($ip_dbh);
432 $page->param(nodelist => $nlist);
433## end node hack
434
435 $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
436
437} # assignBlock
438
439
440# Take info on requested IP assignment and see what we can provide.
441sub confirmAssign {
442 if ($IPDBacl{$authuser} !~ /a/) {
443 $aclerr = 'addblock';
444 return;
445 }
446
447 my $cidr;
448 my $resv; # Reserved for expansion.
449 my $alloc_from;
450 my $fbid = $webvar{fbid};
451 my $p_id = $webvar{parent};
452
453 # Going to manually validate some items.
454 # custid and city are automagic.
455 return if !validateInput();
456
457 # make sure this is defined
458 $webvar{fbassign} = 'n' if !$webvar{fbassign};
459
460# Several different cases here.
461# Static IP vs netblock
462# + Different flavours of static IP
463# + Different flavours of netblock
464
465 if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
466 if (!$webvar{pop}) {
467 $page->param(err => "Please select a location/POP site to allocate from.");
468 return;
469 }
470 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
471 $page->param(staticip => 1);
472 $page->param(poollist => $plist) if $plist;
473 $cidr = "Single static IP";
474##fixme: need to handle "no available pools"
475
476 } else { # end show pool options
477
478 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
479
480 # Tree navigation
481 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
482 my @rcrumbs = reverse (@$crumbs);
483 $utilbar->param(breadcrumb => \@rcrumbs);
484
485 $cidr = new NetAddr::IP $webvar{block};
486 $alloc_from = new NetAddr::IP $webvar{allocfrom};
487 $webvar{maskbits} = $cidr->masklen;
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
520 } else { # done with direct freeblocks assignment
521
522 if (!$webvar{maskbits}) {
523 $page->param(err => "Please specify a CIDR mask length.");
524 return;
525 }
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";
529 if ($webvar{alloctype} eq 'rm') {
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.";
532 } else {
533 if ($webvar{alloctype} =~ /^.[pc]$/) {
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.";
536 } else {
537 if (!$webvar{pop}) {
538 $page->param(err => 'Please select a POP to route the block from/through.');
539 return;
540 }
541 $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
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 }
548 }
549 }
550
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
557 ($fbid,$cidr,$p_id) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype},
558 $webvar{city}, $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
559 if (!$cidr) {
560 $page->param(err => $failmsg);
561 return;
562 }
563 $cidr = new NetAddr::IP $cidr;
564
565 $alloc_from = "$cidr";
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
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 }
586 } # check for freeblocks assignment or IPDB-controlled assignment
587
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 }
610 } # if ($webvar{alloctype} =~ /^.i$/)
611
612## node hack
613 if ($webvar{node} && $webvar{node} ne '-') {
614 my $nodename = getNodeName($ip_dbh, $webvar{node});
615 $page->param(nodename => $nodename);
616 $page->param(nodeid => $webvar{node});
617 }
618## end node hack
619
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
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
632 # Stick in the allocation data
633 $page->param(alloc_type => $webvar{alloctype});
634 $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
635 $page->param(alloc_from => $alloc_from);
636 $page->param(parent => $p_id);
637 $page->param(fbid => $fbid);
638 $page->param(cidr => $cidr);
639 $page->param(rdns => $webvar{rdns});
640 $page->param(vrf => $webvar{vrf});
641 $page->param(vlan => $webvar{vlan});
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}));
646
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
652 # Check to see if user is allowed to do anything with sensitive data
653 my $privdata = '';
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})
659 if $webvar{userid};
660
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");
665
666} # end confirmAssign
667
668
669# Do the work of actually inserting a block in the database.
670sub insertAssign {
671 if ($IPDBacl{$authuser} !~ /a/) {
672 $aclerr = 'addblock';
673 return;
674 }
675 # Some things are done more than once.
676 return if !validateInput();
677
678 if (!defined($webvar{privdata})) {
679 $webvar{privdata} = '';
680 }
681
682 # $code is "success" vs "failure", $msg contains OK for a
683 # successful netblock allocation, the IP allocated for static
684 # IP, or the error message if an error occurred.
685
686##fixme: consider just passing \%webvar to allocateBlock()?
687 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
688 my %iprev;
689 foreach (keys %webvar) {
690 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
691 }
692
693 # Easier to see and cosmetically fiddle the list like this
694 my %insert_args = (
695 cidr => $webvar{fullcidr},
696 fbid => $webvar{fbid},
697 reserve => $webvar{reserve},
698 parent => $webvar{parent},
699 custid => $webvar{custid},
700 type => $webvar{alloctype},
701 city => $webvar{city},
702 desc => $webvar{desc},
703 notes => $webvar{notes},
704 circid => $webvar{circid},
705 privdata => $webvar{privdata},
706 nodeid => $webvar{node},
707 rdns => $webvar{rdns},
708 vrf => $webvar{vrf},
709 vlan => $webvar{vlan},
710 user => $authuser,
711 );
712
713 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
714
715 # clean up a minor mess with guided allocation of static IPs
716 if ($webvar{alloctype} =~ /^.i$/) {
717 $insert_args{alloc_from} = $pinfo->{block};
718 }
719
720 my ($code,$msg) = allocateBlock($ip_dbh, %insert_args, iprev => \%iprev);
721
722 if ($code eq 'OK') {
723 # breadcrumbs lite! provide at least a link to the parent of the block we just allocated.
724 $page->param(parentid => $webvar{parent});
725 $page->param(parentblock => $pinfo->{block});
726
727 if ($webvar{alloctype} =~ /^.i$/) {
728 $msg =~ s|/32||;
729 $page->param(staticip => $msg);
730 $page->param(custid => $webvar{custid});
731 $page->param(billinguser => $webvar{billinguser});
732 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
733 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
734 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
735 } else {
736 my $netblock = new NetAddr::IP $webvar{fullcidr};
737 $page->param(fullcidr => $webvar{fullcidr});
738 $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
739 $page->param(custid => $webvar{custid});
740
741 # Full breadcrumbs
742 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
743 my @rcrumbs = reverse (@$crumbs);
744 $utilbar->param(breadcrumb => \@rcrumbs);
745
746 if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
747 $page->param(billinguser => $webvar{billinguser});
748 $page->param(custid => $webvar{custid});
749 $page->param(netaddr => $netblock->addr);
750 $page->param(masklen => $netblock->masklen);
751 }
752 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
753 "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
754 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
755 }
756 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
757 "'$webvar{alloctype}' ($msg)";
758 } else {
759 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
760 "'$webvar{alloctype}' by $authuser failed: '$msg'";
761 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}' failed:");
762 $page->param(errmsg => $msg);
763 }
764
765} # end insertAssign()
766
767
768# Does some basic checks on common input data to make sure nothing
769# *really* weird gets in to the database through this script.
770# Does NOT do complete input validation!!!
771sub validateInput {
772 if ($webvar{city} eq '-') {
773 $page->param(err => 'Please choose a city');
774 return;
775 }
776
777 # Alloctype check.
778 chomp $webvar{alloctype};
779 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
780 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
781 # managing to call things in such a way as to cause this deserves a cryptic error.
782 $page->param(err => 'Invalid alloctype');
783 return;
784 }
785
786 # CustID check
787 # We have different handling for customer allocations and "internal" or "our" allocations
788 if ($def_custids{$webvar{alloctype}} eq '') {
789 if (!$webvar{custid}) {
790 $page->param(err => 'Please enter a customer ID.');
791 return;
792 }
793 # Crosscheck with billing.
794 my $status = CustIDCK->custid_exist($webvar{custid});
795 if ($CustIDCK::Error) {
796 $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
797 return;
798 }
799 if (!$status) {
800 $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
801 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
802 "non-customer assignments.");
803 return;
804 }
805# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
806 } else {
807 # New! Improved! And now Loaded From The Database!!
808 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
809 $webvar{custid} = $def_custids{$webvar{alloctype}};
810 }
811 }
812
813## hmmm.... is this even useful?
814if (0) {
815 # Check POP location
816 my $flag;
817 if ($webvar{alloctype} eq 'rm') {
818 $flag = 'for a routed netblock';
819 foreach (@poplist) {
820 if (/^$webvar{city}$/) {
821 $flag = 'n';
822 last;
823 }
824 }
825 } else {
826 $flag = 'n';
827##fixme: hook to force-set POP or city on certain alloctypes
828# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
829 if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
830 $flag = 'to route the block from/through';
831 }
832 }
833
834 # if the alloctype has a restricted city/POP list as determined above,
835 # and the reqested city/POP does not match that list, complain
836 if ($flag ne 'n') {
837 $page->param(err => "Please choose a valid POP location $flag. Valid ".
838 "POP locations are currently:<br>\n".join (" - ", @poplist));
839 return;
840 }
841}
842
843 # VRF. Not a full validity check, just a basic sanity check.
844 if ($webvar{vrf}) {
845 # Trim leading and trailing whitespace first
846 $webvar{vrf} =~ s/^\s+//;
847 $webvar{vrf} =~ s/\s+$//;
848 if ($webvar{vrf} !~ /^[\w\d_.-]{1,32}$/) {
849 $page->param(err => "VRF values may only contain alphanumerics, and may not be more than 32 characters");
850 return;
851 }
852 }
853
854 # VLAN. Should we allow/use VLAN names, or just the numeric ID?
855 if ($webvar{vlan}) {
856 # Trim leading and trailing whitespace first
857 $webvar{vlan} =~ s/^\s+//;
858 $webvar{vlan} =~ s/\s+$//;
859 # ... ve make it ze configurable thingy!
860 if ($IPDB::numeric_vlan) {
861 if ($webvar{vlan} !~ /^\d+$/) {
862 $page->param(err => "VLANs must be numeric");
863 return;
864 }
865 } else {
866 if ($webvar{vlan} !~ /^[\w\d_.-]+$/) {
867 $page->param(err => "VLANs must be alphanumeric");
868 return;
869 }
870 }
871 }
872
873 return 'OK';
874} # end validateInput
875
876
877# Displays details of a specific allocation in a form
878# Allows update/delete
879# action=edit
880sub edit {
881
882 # snag block info from db
883 my $blockinfo = getBlockData($ip_dbh, $webvar{id}, $webvar{basetype});
884 my $cidr = new NetAddr::IP $blockinfo->{block};
885 $page->param(id => $webvar{id});
886 $page->param(basetype => $webvar{basetype});
887
888 # Tree navigation
889 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
890 my @rcrumbs = reverse (@$crumbs);
891 $utilbar->param(breadcrumb => \@rcrumbs);
892
893 # Show link to IP list for pools
894 $page->param(ispool => 1) if $blockinfo->{type} =~ /^.[dp]$/;
895
896 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
897 $blockinfo->{type} =~ s/\s//;
898
899##fixme: The case of "allocation larger than a /24" (or any similar case
900# where the allocation is larger than the zone(s) in DNS) doesn't work well.
901# Best solution may just be to add a warning that the entry shown may not be
902# correct/complete.
903 if ($blockinfo->{revavail} || $blockinfo->{revpartial}) {
904 $page->param(showrev => ($blockinfo->{revavail} || $blockinfo->{revpartial}) );
905 my $cached;
906 # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
907 ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser);
908 $page->param(rdns => $blockinfo->{rdns});
909 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
910 $page->param(cached => $cached);
911
912 # Limit the per-IP rDNS list based on CIDR length; larger ones just take up too much space.
913 # Also, don't show on IP pools; the individual IPs will have a space for rDNS
914 # Don't show on single IPs; these use the "pattern" field
915 if ($IPDBacl{$authuser} =~ /c/
916 && $cidr->masklen != $cidr->bits
917 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
918 && $blockinfo->{type} !~ /^.[dpi]/
919 # do we want to allow v6 at all?
920 #&& ! $cidr->{isv6}
921 ) {
922 $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type},
923 range => $blockinfo->{block}, user => $authuser) );
924 }
925 } # rDNS availability check
926
927 # consider extending this to show time as well as date
928 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
929 $page->param(lastmod => $lastmod);
930
931 $page->param(block => $blockinfo->{block});
932 $page->param(city => $blockinfo->{city});
933 $page->param(custid => $blockinfo->{custid});
934
935##fixme The check here should be built from the database
936# Need to expand to support pool types too
937 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
938 $page->param(changetype => 1);
939 $page->param(alloctype => [
940 { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
941 { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
942 { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
943 { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
944 { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
945 { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
946 { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
947 ]
948 );
949 } else {
950 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
951 $page->param(type => $blockinfo->{type});
952 }
953
954## node hack
955 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block});
956# $page->param(havenodeid => $nodeid);
957 $page->param(nodename => $nodename);
958
959##fixme: this whole hack needs cleanup and generalization for all alloctypes
960##fixme: arguably a bug that presence of a nodeid implies it can be changed..
961 if ($IPDBacl{$authuser} =~ /c/) {
962 my $nlist = getNodeList($ip_dbh);
963 if ($nodeid) {
964 foreach (@{$nlist}) {
965 $$_{selme} = ($$_{node_id} == $nodeid);
966 }
967 }
968 $page->param(nodelist => $nlist);
969 }
970## end node hack
971
972 $page->param(vrf => $blockinfo->{vrf});
973 $page->param(vlan => $blockinfo->{vlan});
974
975 # Reserved-for-expansion
976 $page->param(reserve => $blockinfo->{reserve});
977 $page->param(reserve_id => $blockinfo->{reserve_id});
978 my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
979 $page->param(newblock => $newblock);
980
981 # not happy with the upside-down logic, but...
982 $page->param(swipable => $blockinfo->{type} !~ /.i/);
983 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
984
985 $page->param(circid => $blockinfo->{circuitid});
986 $page->param(desc => $blockinfo->{description});
987 $page->param(notes => $blockinfo->{notes});
988
989 # Check to see if we can display sensitive data
990 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
991 $page->param(privdata => $blockinfo->{privdata});
992
993 # ACL trickery - these two template booleans control the presence of all form/input tags
994 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
995 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
996
997 # Need to find internal knobs to twist to actually vary these. (Ab)use "change" flag for now
998 $page->param(maymerge => ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/));
999 if ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/) {
1000 if ($blockinfo->{type} =~ /^.p$/) {
1001 # PPP pools
1002 $page->param(maysplit => 1) if $cidr->masklen+1 < $cidr->bits;
1003 } elsif ($blockinfo->{type} =~ /.d/) {
1004 # Non-PPP pools
1005 $page->param(maysplit => 1) if $cidr->masklen+2 < $cidr->bits;
1006 } else {
1007 # Standard netblocks. Arguably allowing splitting these down to single IPs
1008 # doesn't make much sense, but forcing users to apply allocation types
1009 # "properly" is worse than herding cats.
1010 $page->param(maysplit => 1) if $cidr->masklen < $cidr->bits;
1011 }
1012 }
1013
1014} # edit()
1015
1016
1017# Stuff new info about a block into the db
1018# action=update
1019sub update {
1020 if ($IPDBacl{$authuser} !~ /c/) {
1021 $aclerr = 'updateblock';
1022 return;
1023 }
1024
1025 # Collect existing block info here, since we need it for the breadcrumb nav
1026 my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1027 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1028 my @rcrumbs = reverse (@$crumbs);
1029 $utilbar->param(breadcrumb => \@rcrumbs);
1030
1031 # Make sure incoming data is in correct format - custID among other things.
1032 return if !validateInput;
1033
1034 $webvar{swip} = 'n' if !$webvar{swip};
1035
1036 my %updargs = (
1037 custid => $webvar{custid},
1038 city => $webvar{city},
1039 description => $webvar{desc},
1040 notes => $webvar{notes},
1041 circuitid => $webvar{circid},
1042 block => $webvar{block},
1043 type => $webvar{alloctype},
1044 swip => $webvar{swip},
1045 rdns => $webvar{rdns},
1046 vrf => $webvar{vrf},
1047 vlan => $webvar{vlan},
1048 user => $authuser,
1049 );
1050
1051 # Semioptional values
1052 $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
1053 $updargs{node} = $webvar{node} if $webvar{node};
1054
1055 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
1056 my %iprev;
1057 foreach (keys %webvar) {
1058 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
1059 }
1060
1061 # Merge with reserved freeblock
1062 $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme};
1063
1064 my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
1065
1066 if ($code eq 'FAIL') {
1067 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
1068 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
1069 return;
1070 }
1071
1072 # If we get here, the operation succeeded.
1073 syslog "notice", "$authuser updated $webvar{block}";
1074##fixme: log details of the change? old way is in the .debug stream anyway.
1075##fixme: need to wedge something in to allow "update:field" notifications
1076## hmm. how to tell what changed? O_o
1077mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1078 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
1079
1080## node hack
1081 if ($webvar{node} && $webvar{node} ne '-') {
1082 my $nodename = getNodeName($ip_dbh, $webvar{node});
1083 $page->param(nodename => $nodename);
1084 }
1085## end node hack
1086
1087 # Link back to browse-routed or list-pool page on "Update complete" page.
1088 my $pblock = getBlockData($ip_dbh, $binfo->{parent_id});
1089 $page->param(backid => $binfo->{parent_id});
1090 $page->param(backblock => $pblock->{block});
1091 $page->param(backpool => ($webvar{basetype} eq 'i'));
1092
1093 # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
1094 # because otherwise we can't convert \n to <br>. *sigh*
1095 $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
1096 $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
1097 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
1098 $webvar{privdata} =~ s/\n/<br>\n/;
1099
1100 if ($webvar{expandme}) {
1101 # this is fugly but still faster than hitting the DB again with getBlockData()
1102 my $tmp = new NetAddr::IP $binfo->{block};
1103 my $fb = new NetAddr::IP $binfo->{reserve};
1104 my @newblock = $tmp->compact($fb);
1105 $page->param(cidr => $newblock[0]);
1106 } else {
1107 $page->param(cidr => $binfo->{block});
1108 }
1109 $page->param(rdns => $webvar{rdns});
1110 $page->param(city => $webvar{city});
1111 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
1112 $page->param(custid => $webvar{custid});
1113 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
1114 $page->param(circid => $webvar{circid});
1115 $page->param(desc => $webvar{desc});
1116 $page->param(notes => $webvar{notes});
1117 $page->param(privdata => $webvar{privdata})
1118 if $IPDBacl{$authuser} =~ /s/;
1119
1120} # update()
1121
1122
1123sub prepSplit {
1124 if ($IPDBacl{$authuser} !~ /c/) {
1125 $aclerr = 'splitblock';
1126 return;
1127 }
1128
1129 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1130
1131 # Tree navigation
1132 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1133 my @rcrumbs = reverse (@$crumbs);
1134 $utilbar->param(breadcrumb => \@rcrumbs);
1135
1136 if ($blockinfo->{type} =~ /^.i$/) {
1137 $page->param(err => "Can't split a single IP allocation");
1138 return;
1139 }
1140
1141 # Info about current allocation
1142 $page->param(oldblock => $blockinfo->{block});
1143 $page->param(block => $webvar{block});
1144
1145# Note that there are probably different rules that should be followed to restrict splitting IPv6 blocks;
1146# strictly speaking it will be exceptionally rare to see smaller than a /64 assigned to a customer, since that
1147# breaks auto-addressing schemes.
1148
1149 # Generate possible splits
1150 my $block = new NetAddr::IP $blockinfo->{block};
1151 my $oldmask = $block->masklen;
1152 if ($blockinfo->{type} =~ /^.d$/) {
1153 # Non-PPP pools
1154 $page->param(ispool => 1);
1155 if ($oldmask+2 >= $block->bits) {
1156 $page->param(err => "Can't split a standard netblock pool any further");
1157 return;
1158 }
1159 # Allow splitting down to v4 /30 (which results in one usable IP; dubiously useful)
1160 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-2;
1161 } elsif ($blockinfo->{type} =~ /.p/) {
1162 $page->param(ispool => 1);
1163 # Allow splitting PPP pools down to v4 /31
1164 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-1;
1165 } else {
1166 # Allow splitting all other non-pool netblocks down to single IPs, which...
1167 # arguably should be *aggregated* in a pool. Except where they shouldn't.
1168 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits;
1169 }
1170 # set the split-in-half mask
1171 $page->param(sp2mask => $oldmask+1);
1172
1173 # Generate possible shrink targets
1174 my @keepers = $block->split($block->masklen+1);
1175 $page->param(newblockA => $keepers[0]);
1176 $page->param(newblockB => $keepers[1]);
1177} # prepSplit()
1178
1179
1180sub doSplit {
1181 if ($IPDBacl{$authuser} !~ /c/) {
1182 $aclerr = 'splitblock';
1183 return;
1184 }
1185
1186##fixme: need consistent way to identify "this thing that is this thing" with only the ID
1187# also applies to other locations
1188 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1189
1190 # Tree navigation
1191 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1192 my @rcrumbs = reverse (@$crumbs);
1193 $utilbar->param(breadcrumb => \@rcrumbs);
1194
1195 if ($blockinfo->{type} =~ /^.i$/) {
1196 $page->param(err => "Can't split a single IP allocation");
1197 return;
1198 }
1199
1200 if ($webvar{subact} eq 'split') {
1201 $page->param(issplit => 1);
1202 my $block = new NetAddr::IP $blockinfo->{block};
1203 my $newblocks = splitBlock($ip_dbh, id => $webvar{block}, basetype => 'b', newmask => $webvar{split},
1204 user => $authuser);
1205 if ($newblocks) {
1206 $page->param(newblocks => $newblocks);
1207 } else {
1208 $page->param(err => $IPDB::errstr);
1209 }
1210
1211 } elsif ($webvar{subact} eq 'shrink') {
1212 $page->param(nid => $webvar{block});
1213 $page->param(newblock => $webvar{shrink});
1214 my $newfree = shrinkBlock($ip_dbh, $webvar{block}, $webvar{shrink});
1215 if ($newfree) {
1216 $page->param(newfb => $newfree);
1217 } else {
1218 $page->param(err => $IPDB::errstr);
1219 }
1220
1221 } else {
1222 # Your llama is on fire.
1223 $page->param(err => "Missing form field that shouldn't be missing.");
1224 return;
1225 }
1226
1227 # common bits
1228 $page->param(cidr => $blockinfo->{block});
1229 # and the backlink to the parent container
1230 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id});
1231 $page->param(backid => $blockinfo->{parent_id});
1232 $page->param(backblock => $pinfo->{block});
1233} # doSplit()
1234
1235
1236# Set up for merge
1237sub prepMerge {
1238 my $binfo = getBlockData($ip_dbh, $webvar{block});
1239
1240 # Tree navigation
1241 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1242 my @rcrumbs = reverse (@$crumbs);
1243 $utilbar->param(breadcrumb => \@rcrumbs);
1244
1245 $page->param(block => $webvar{block});
1246 $page->param(ispool => $binfo->{type} =~ /.[dp]/);
1247 $page->param(ismaster => $binfo->{type} eq 'mm');
1248 $page->param(oldblock => $binfo->{block});
1249 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1250 $page->param(typelist => getTypeList($ip_dbh, 'n', $binfo->{type})); # down the rabbit hole we go...
1251
1252 # Strings for scope; do this way so we don't have to edit them many places
1253 $page->param(vis_keepall => $merge_display{keepall});
1254 $page->param(vis_mergepeer => $merge_display{mergepeer});
1255 $page->param(vis_clearpeer => $merge_display{clearpeer});
1256 $page->param(vis_clearall => $merge_display{clearall});
1257
1258} # prepMerge()
1259
1260
1261# Show what will be merged, present warnings about data loss
1262sub confMerge {
1263 if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) {
1264 $page->param(err => 'New netmask required');
1265 return;
1266 }
1267
1268 $page->param(block => $webvar{block});
1269 my $binfo = getBlockData($ip_dbh, $webvar{block});
1270 my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
1271 my $minfo = getBlockData($ip_dbh, $binfo->{master_id});
1272 my $block = new NetAddr::IP $binfo->{block};
1273
1274 # Tree navigation
1275 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1276 my @rcrumbs = reverse (@$crumbs);
1277 $utilbar->param(breadcrumb => \@rcrumbs);
1278
1279 $page->param(oldblock => $binfo->{block});
1280 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1281 $page->param(ismaster => $binfo->{type} eq 'mm');
1282 $page->param(ispool => $webvar{alloctype} =~ /.[dp]/);
1283 $page->param(isleaf => $webvar{alloctype} =~ /.[enr]/);
1284 $page->param(newtype => $webvar{alloctype});
1285 $page->param(newdisptype => $disp_alloctypes{$webvar{alloctype}});
1286 my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}";
1287 $newblock = $newblock->network;
1288 $page->param(newmask => $webvar{newmask});
1289 $page->param(newblock => "$newblock");
1290
1291 # get list of allocations and freeblocks to be merged
1292 my $malloc_list = listForMerge($ip_dbh, $binfo->{parent_id}, $newblock, 'a');
1293 $page->param(mergealloc => $malloc_list);
1294
1295 $page->param(vis_scope => $merge_display{$webvar{scope}});
1296 $page->param(scope => $webvar{scope});
1297} # confMerge()
1298
1299
1300# Make it so
1301sub doMerge {
1302 if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) {
1303 $page->param(err => 'New netmask required');
1304 return;
1305 }
1306
1307 $page->param(block => $webvar{block});
1308 my $binfo = getBlockData($ip_dbh, $webvar{block});
1309 my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
1310 my $block = new NetAddr::IP $binfo->{block};
1311
1312 # Tree navigation
1313 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1314 my @rcrumbs = reverse (@$crumbs);
1315 $utilbar->param(breadcrumb => \@rcrumbs);
1316
1317 $page->param(oldblock => $binfo->{block});
1318 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1319 $page->param(newdisptype => $disp_alloctypes{$webvar{newtype}});
1320 my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}";
1321 $newblock = $newblock->network;
1322 $page->param(newblock => $newblock);
1323 $page->param(vis_scope => $merge_display{$webvar{scope}});
1324
1325 my $mlist = mergeBlocks($ip_dbh, $webvar{block}, %webvar, user => $authuser);
1326
1327 if ($mlist) {
1328 #(newtype => $webvar{newtype}, newmask => $webvar{newmask}));
1329 # Slice off first entry (the new parent - note this may be a new allocation,
1330 # not the same ID that was "merged"!
1331 my $parent = shift @$mlist;
1332 $page->param(backpool => $webvar{newtype} =~ /.[dp]/);
1333 if ($webvar{newtype} =~ /.[enr]/) {
1334 $page->param(backleaf => 1);
1335 $page->param(backid => $binfo->{parent_id});
1336 $page->param(backblock => $pinfo->{block});
1337 } else {
1338 $page->param(backid => $parent->{id});
1339 $page->param(backblock => $parent->{block});
1340 }
1341 $page->param(mergelist => $mlist);
1342 } else {
1343 $page->param(err => "Merge failed: $IPDB::errstr");
1344 }
1345} # doMerge()
1346
1347
1348# Delete an allocation.
1349sub remove {
1350 if ($IPDBacl{$authuser} !~ /d/) {
1351 $aclerr = 'delblock';
1352 return;
1353 }
1354
1355 # Serves'em right for getting here...
1356 if (!defined($webvar{block})) {
1357 $page->param(err => "Can't delete a block that doesn't exist");
1358 return;
1359 }
1360
1361 my $blockdata;
1362 $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1363
1364 # Tree navigation
1365 my $crumbs = getBreadCrumbs($ip_dbh, $blockdata->{parent_id});
1366 my @rcrumbs = reverse (@$crumbs);
1367 $utilbar->param(breadcrumb => \@rcrumbs);
1368
1369 if ($blockdata->{parent_id} == 0) { # $webvar{alloctype} eq 'mm'
1370 $blockdata->{city} = "N/A";
1371 $blockdata->{custid} = "N/A";
1372 $blockdata->{circuitid} = "N/A";
1373 $blockdata->{description} = "N/A";
1374 $blockdata->{notes} = "N/A";
1375 $blockdata->{privdata} = "N/A";
1376 } # end cases for different alloctypes
1377
1378 $page->param(blockid => $webvar{block});
1379 $page->param(basetype => $webvar{basetype});
1380
1381 $page->param(block => $blockdata->{block});
1382 $page->param(rdns => $blockdata->{rdns});
1383
1384 # maybe need to apply more magic here?
1385 # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
1386 # -> all real blocks (nb: pool IPs need extra handling)
1387 # -> NOC/private-IP (how to ID?)
1388 # -> anything with a pattern matching $IPDB::domain?
1389 if ($blockdata->{type} !~ /^.i$/) {
1390 $page->param(autodel => 1);
1391 }
1392
1393 $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
1394 $page->param(city => $blockdata->{city});
1395 $page->param(custid => $blockdata->{custid});
1396 $page->param(circid => $blockdata->{circuitid});
1397 $page->param(desc => $blockdata->{description});
1398 $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
1399 $blockdata->{notes} =~ s/\n/<br>\n/;
1400 $page->param(notes => $blockdata->{notes});
1401 $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
1402 $blockdata->{privdata} = '&nbsp;' if !$blockdata->{privdata};
1403 $blockdata->{privdata} =~ s/\n/<br>\n/;
1404 $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
1405 $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
1406
1407} # end remove()
1408
1409
1410# Delete an allocation. Return it to the freeblocks table; munge
1411# data as necessary to keep as few records as possible in freeblocks
1412# to prevent weirdness when allocating blocks later.
1413# Remove IPs from pool listing if necessary
1414sub finalDelete {
1415 if ($IPDBacl{$authuser} !~ /d/) {
1416 $aclerr = 'delblock';
1417 return;
1418 }
1419
1420 # need to retrieve block data before deleting so we can notify on that
1421 my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1422 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b');
1423
1424 # Tree navigation
1425 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1426 my @rcrumbs = reverse (@$crumbs);
1427 $utilbar->param(breadcrumb => \@rcrumbs);
1428
1429 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser);
1430
1431 $page->param(block => $blockinfo->{block});
1432 $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}}));
1433 $page->param(delparent_id => $blockinfo->{parent_id});
1434 if ($pinfo) {
1435 $page->param(delparent => $pinfo->{block});
1436 $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}}));
1437 }
1438 $page->param(returnpool => ($webvar{basetype} eq 'i') );
1439 if ($code =~ /^WARN(POOL|MERGE)/) {
1440 my ($pid,$pcidr) = split /,/, $msg;
1441 my $real_pinfo = getBlockData($ip_dbh, $pid, 'b');
1442 $page->param(parent_id => $pid);
1443 $page->param(parent => $pcidr);
1444 $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}}));
1445 $page->param(mergeip => $code eq 'WARNPOOL');
1446 }
1447 if ($code eq 'WARN') {
1448 $msg =~ s/\n/<br>\n/g;
1449 $page->param(genwarn => $msg);
1450 }
1451 if ($code eq 'OK' || $code =~ /^WARN/) {
1452 syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock $webvar{block} ".
1453 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
1454 mailNotify($ip_dbh, 'da', "REMOVED: ".$disp_alloctypes{$blockinfo->{type}}." $webvar{block}",
1455 $disp_alloctypes{$blockinfo->{type}}." $webvar{block} deallocated by $authuser\n".
1456 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
1457 "\nDescription: ".$blockinfo->{description}."\n");
1458 } else {
1459 $page->param(failmsg => $msg);
1460 if ($webvar{alloctype} =~ /^.i$/) {
1461 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1462 } else {
1463 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
1464 $page->param(netblock => 1);
1465 }
1466 }
1467
1468} # finalDelete
Note: See TracBrowser for help on using the repository browser.