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

Last change on this file since 793 was 793, checked in by Kris Deugau, 8 years ago

/trunk

Refine the template loading process to allow completely reskinning the
UI by replacing templates as convenient, to match the CSS override allowed
by referencing not-provided local.css. See #17, sort of.

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