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

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

/trunk

Promote VRFs to top-level entities. See #54.

1 of mumble; convert index page to list VRFs instead of master blocks.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 54.2 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3###
4# SVN revision info
5# $Date: 2016-03-07 23:05:24 +0000 (Mon, 07 Mar 2016) $
6# SVN revision $Rev: 806 $
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 list of VRFs
262sub showSummary {
263 my $vrflist = listVRF($ip_dbh);
264 $page->param(vrflist => $vrflist);
265
266 # Only systems/network should be allowed to add VRFs - or maybe higher?
267 $page->param(addvrf => ($IPDBacl{$authuser} =~ /s/) );
268} # showSummary
269
270
271# Display blocks immediately within a given parent
272sub showSubs {
273 # Which layout?
274 if ($IPDB::sublistlayout == 2) {
275
276 # 2-part layout; mixed containers and end-use allocations and free blocks.
277 # Containers have a second line for the subblock metadata.
278 # We need to load an alternate template for this case.
279 $page = HTML::Template->new(filename => "showsubs2.tmpl", loop_context_vars => 1, global_vars => 1,
280 path => @templatepath);
281
282 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/));
283
284 my $sublist = listSubs($ip_dbh, parent => $webvar{parent});
285 $page->param(sublist => $sublist);
286
287 } else {
288
289 # 3-part layout; containers, end-use allocations, and free blocks
290
291 my $contlist = listContainers($ip_dbh, parent => $webvar{parent});
292 $page->param(contlist => $contlist);
293
294 my $alloclist = listAllocations($ip_dbh, parent => $webvar{parent});
295 $page->param(alloclist => $alloclist);
296
297 # only show "delete" button if we have no container or usage allocations
298 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/) && !(@$contlist || @$alloclist));
299
300 }
301
302 # Common elements
303 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
304
305##fixme: do we add a wrapper to not show the edit link for master blocks?
306#$page->param(editme => 1) unless $pinfo->{type} ne 'mm';
307
308 my $crumbs = getBreadCrumbs($ip_dbh, $pinfo->{parent_id});
309 my @rcrumbs = reverse (@$crumbs);
310 $utilbar->param(breadcrumb => \@rcrumbs);
311
312 $page->param(self_id => $webvar{parent});
313 $page->param(block => $pinfo->{block});
314 $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
315
316 my $flist = listFree($ip_dbh, parent => $webvar{parent});
317 $page->param(freelist => $flist);
318} # showSubs
319
320
321# List the IPs used in a pool
322sub showPool {
323
324 my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
325 my $cidr = new NetAddr::IP $poolinfo->{block};
326 $page->param(vlan => $poolinfo->{vlan});
327
328 # Tree navigation
329 my $crumbs = getBreadCrumbs($ip_dbh, $poolinfo->{parent_id});
330 my @rcrumbs = reverse (@$crumbs);
331 $utilbar->param(breadcrumb => \@rcrumbs);
332
333 $page->param(block => $cidr);
334 $page->param(netip => $cidr->addr);
335 $cidr++;
336 $page->param(gate => $cidr->addr);
337 $cidr--; $cidr--;
338 $page->param(bcast => $cidr->addr);
339 $page->param(mask => $cidr->mask);
340
341 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
342 $page->param(city => $poolinfo->{city});
343
344 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
345 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
346
347# probably have to add an "edit IP allocation" link here somewhere.
348
349 my $plist = listPool($ip_dbh, $webvar{pool});
350 # technically slightly more efficient to check the ACL in an if () once outside the foreach
351 foreach (@{$plist}) {
352 $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
353 }
354 $page->param(poolips => $plist);
355} # end showPool
356
357
358# Show "Add new allocation" page. Note that the actual page may
359# be one of two templates, and the lists come from the database.
360sub assignBlock {
361
362 if ($IPDBacl{$authuser} !~ /a/) {
363 $aclerr = 'addblock';
364 return;
365 }
366
367 # hack pthbttt eww
368 $webvar{parent} = 0 if !$webvar{parent};
369 $webvar{block} = '' if !$webvar{block};
370
371 $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
372
373 if ($webvar{fbid} || $webvar{fbtype}) {
374
375 # Common case, according to reported usage. Block to assign is specified.
376 my $block = new NetAddr::IP $webvar{block};
377
378 my ($rdns,$cached) = getBlockRDNS($ip_dbh, id => $webvar{parent}, type => $webvar{fbtype}, user => $authuser);
379 $page->param(rdns => $rdns) if $rdns;
380 $page->param(parent => $webvar{parent});
381 $page->param(fbid => $webvar{fbid});
382 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
383 $page->param(cached => $cached);
384
385 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
386 # seems reasonable that a new allocation would share a VRF with its parent
387 $page->param(pvrf => $pinfo->{vrf});
388
389 # Tree navigation
390 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
391 my @rcrumbs = reverse (@$crumbs);
392 $utilbar->param(breadcrumb => \@rcrumbs);
393
394 $webvar{fbtype} = '' if !$webvar{fbtype};
395 if ($webvar{fbtype} eq 'i') {
396 my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i');
397 $page->param(
398 fbip => 1,
399 block => $ipinfo->{block},
400 fbdisptype => $list_alloctypes{$ipinfo->{type}},
401 type => $ipinfo->{type},
402 allocfrom => $pinfo->{block},
403 );
404 } else {
405 # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
406 my $tlist = getTypeList($ip_dbh, 'n');
407 $tlist->[0]->{sel} = 1;
408 $page->param(typelist => $tlist, block => $block);
409 }
410
411 } else {
412
413 # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
414 my $mlist = getMasterList($ip_dbh, 'c');
415 $page->param(masterlist => $mlist);
416
417 my @pops;
418 foreach my $pop (@citylist) {
419 my %row = (pop => $pop);
420 push (@pops, \%row);
421 }
422 $page->param(pops => \@pops);
423
424 # get all standard alloctypes
425 my $tlist = getTypeList($ip_dbh, 'a');
426 $tlist->[0]->{sel} = 1;
427 $page->param(typelist => $tlist);
428 }
429
430 my @cities;
431 foreach my $city (@citylist) {
432 my %row = (city => $city);
433 push (@cities, \%row);
434 }
435 $page->param(citylist => \@cities);
436
437## node hack
438 my $nlist = getNodeList($ip_dbh);
439 $page->param(nodelist => $nlist);
440## end node hack
441
442 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
443
444} # assignBlock
445
446
447# Take info on requested IP assignment and see what we can provide.
448sub confirmAssign {
449 if ($IPDBacl{$authuser} !~ /a/) {
450 $aclerr = 'addblock';
451 return;
452 }
453
454 my $cidr;
455 my $resv; # Reserved for expansion.
456 my $alloc_from;
457 my $fbid = $webvar{fbid};
458 my $p_id = $webvar{parent};
459
460 # Going to manually validate some items.
461 # custid and city are automagic.
462 return if !validateInput();
463
464 # make sure this is defined
465 $webvar{fbassign} = 'n' if !$webvar{fbassign};
466
467# Several different cases here.
468# Static IP vs netblock
469# + Different flavours of static IP
470# + Different flavours of netblock
471
472 if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
473 if (!$webvar{pop}) {
474 $page->param(err => "Please select a location/POP site to allocate from.");
475 return;
476 }
477 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
478 $page->param(staticip => 1);
479 $page->param(poollist => $plist) if $plist;
480 $cidr = "Single static IP";
481##fixme: need to handle "no available pools"
482
483 } else { # end show pool options
484
485 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
486
487 # Tree navigation
488 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
489 my @rcrumbs = reverse (@$crumbs);
490 $utilbar->param(breadcrumb => \@rcrumbs);
491
492 $cidr = new NetAddr::IP $webvar{block};
493 $alloc_from = new NetAddr::IP $webvar{allocfrom};
494 $webvar{maskbits} = $cidr->masklen;
495 # Some additional checks are needed for reserving free space
496 if ($webvar{reserve}) {
497 if ($cidr == $alloc_from) {
498# We could still squirm and fiddle to try to find a way to reserve space, but the storage model for
499# IPDB means that all continguous free space is kept in the smallest number of strict CIDR netblocks
500# possible. (In theory.) If the request and the freeblock are the same, it is theoretically impossible
501# to reserve an equivalent-sized block either ahead or behind the requested one, because the pair
502# together would never be a strict CIDR block.
503 $page->param(warning => "Can't reserve space for expansion; free block and requested allocation are the same.");
504 delete $webvar{reserve};
505 } else {
506 # Find which new free block will match the reqested block.
507 # Take the requested mask, shift by one
508 my $tmpmask = $webvar{maskbits};
509 $tmpmask--;
510 # find the subnets with that mask in the selected free block
511 my @pieces = $alloc_from->split($tmpmask);
512 foreach my $slice (@pieces) {
513 if ($slice->contains($cidr)) {
514 # For the subnet that contains the requested block, split that in two,
515 # and flag/cache the one that's not the requested block.
516 my @bits = $slice->split($webvar{maskbits});
517 if ($bits[0] == $cidr) {
518 $resv = $bits[1];
519 } else {
520 $resv = $bits[0];
521 }
522 }
523 }
524 }
525 } # reserve block check
526
527 } else { # done with direct freeblocks assignment
528
529 if (!$webvar{maskbits}) {
530 $page->param(err => "Please specify a CIDR mask length.");
531 return;
532 }
533
534##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
535 my $failmsg = "No suitable free block found.<br>\n";
536 if ($webvar{alloctype} eq 'rm') {
537 $failmsg .= "We do not have a free routeable block of that size.<br>\n".
538 "You will have to either route a set of smaller netblocks or a single smaller netblock.";
539 } else {
540 if ($webvar{alloctype} =~ /^.[pc]$/) {
541 $failmsg .= "You will have to route another superblock from one of the<br>\n".
542 "master blocks or chose a smaller block size for the pool.";
543 } else {
544 if (!$webvar{pop}) {
545 $page->param(err => 'Please select a POP to route the block from/through.');
546 return;
547 }
548 $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
549 "from one of the master blocks";
550 if ($webvar{reserve}) {
551 $failmsg .= ', choose a smaller blocksize, or uncheck "Reserve space for expansion".';
552 } else {
553 $failmsg .= " or chose a smaller blocksize.";
554 }
555 }
556 }
557
558 # if requesting extra space "reserved for expansion", we need to find a free
559 # block at least double the size of the request.
560 if ($webvar{reserve}) {
561 $webvar{maskbits}--;
562 }
563
564 ($fbid,$cidr,$p_id) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype},
565 $webvar{city}, $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
566 if (!$cidr) {
567 $page->param(err => $failmsg);
568 return;
569 }
570 $cidr = new NetAddr::IP $cidr;
571
572 $alloc_from = "$cidr";
573
574 # when autofinding a block to allocate from, use the first piece of the found
575 # block for the allocation, and the next piece for the "reserved for expansion".
576 if ($webvar{reserve}) {
577 # reset the mask to the real requested one, now that we've got a
578 # block large enough for the request plus reserve
579 $webvar{maskbits}++;
580 ($cidr,$resv) = $cidr->split($webvar{maskbits});
581 }
582
583 # If the block to be allocated is smaller than the one we found,
584 # figure out the "real" block to be allocated.
585 if ($cidr->masklen() ne $webvar{maskbits}) {
586 my $maskbits = $cidr->masklen();
587 my @subblocks;
588 while ($maskbits++ < $webvar{maskbits}) {
589 @subblocks = $cidr->split($maskbits);
590 }
591 $cidr = $subblocks[0];
592 }
593 } # check for freeblocks assignment or IPDB-controlled assignment
594
595 # Generate the IP list for the new allocation in case someone wants to set per-IP rDNS right away.
596 # We don't do this on the previous page because we don't know how big a block or even what IP range
597 # it's for (if following the "normal" allocation process)
598 if ($IPDBacl{$authuser} =~ /c/
599 && $cidr->masklen != $cidr->bits
600 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
601 # config flag for "all block types" OR "not-a-pool-or-IP type"
602 && ($IPDB::revlistalltypes || $webvar{alloctype} !~ /^.[dpi]/)
603 # safety against trying to retrieve and display more than 1k (10 bits, /22 v4) worth of individual IPs
604 # ever. If you really need to manage a long list of IPs like that all in one place, you can use the DNS
605 # management tool. Even a /26 is a bit much, really.
606 && ($cidr->bits - $cidr->masklen) <= 10
607 # do we want to allow v6 at all?
608 #&& ! $cidr->{isv6}
609 ) {
610 my @list;
611 foreach my $ip (@{$cidr->splitref()}) {
612 my %row;
613 $row{r_ip} = $ip->addr;
614 $row{iphost} = '';
615 push @list, \%row;
616 }
617 $page->param(r_iplist => \@list);
618 # We don't use this here, because these IPs should already be bare.
619 # ... or should we be paranoid? Make it a config option?
620 #getRDNSbyIP($ip_dbh, type => $webvar{alloctype}, range => "$cidr", user => $authuser) );
621 }
622 } # if ($webvar{alloctype} =~ /^.i$/)
623
624## node hack
625 if ($webvar{node} && $webvar{node} ne '-') {
626 my $nodename = getNodeName($ip_dbh, $webvar{node});
627 $page->param(nodename => $nodename);
628 $page->param(nodeid => $webvar{node});
629 }
630## end node hack
631
632 # flag DNS info if we can't publish the entry remotely
633 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
634 $page->param(dnslocal => 1) unless ($pinfo->{revpartial} || $pinfo->{revavail});
635
636 # reserve for expansion
637 $page->param(reserve => $webvar{reserve});
638 # probably just preventing a little log noise doing this; could just set the param
639 # all the time since it won't be shown if the reserve param above isn't set.
640# if ($webvar{reserve}) {
641 $page->param(resvblock => $resv);
642# }
643
644 # Stick in the allocation data
645 $page->param(alloc_type => $webvar{alloctype});
646 $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
647 $page->param(alloc_from => $alloc_from);
648 $page->param(parent => $p_id);
649 $page->param(fbid => $fbid);
650 $page->param(cidr => $cidr);
651 $page->param(rdns => $webvar{rdns});
652 $page->param(vrf => $webvar{vrf});
653 $page->param(vlan => $webvar{vlan});
654 $page->param(city => $q->escapeHTML($webvar{city}));
655 $page->param(custid => $webvar{custid});
656 $page->param(circid => $q->escapeHTML($webvar{circid}));
657 $page->param(desc => $q->escapeHTML($webvar{desc}));
658
659##fixme: find a way to have the displayed copy have <br> substitutions
660# for newlines, and the <input> value have either encoded or bare newlines.
661# Also applies to privdata.
662 $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
663
664 # Check to see if user is allowed to do anything with sensitive data
665 if ($IPDBacl{$authuser} =~ /s/) {
666 $page->param(nocling => 1);
667 $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'));
668
669 $page->param(backupfields => $webvar{backupfields});
670 $page->param(bkbrand => $webvar{bkbrand});
671 $page->param(bkmodel => $webvar{bkmodel});
672 $page->param(bktype => $webvar{bktype});
673 $page->param(bksrc => $webvar{bksrc});
674 $page->param(bkuser => $webvar{bkuser});
675 # these two could use virtually any character
676 $page->param(bkvpass => $q->escapeHTML($webvar{bkvpass}));
677 $page->param(bkepass => $q->escapeHTML($webvar{bkepass}));
678 $page->param(bkport => $webvar{bkport});
679 $page->param(bkip => $webvar{bkip});
680 }
681
682 # Yay! This now has it's very own little home.
683 $page->param(billinguser => $webvar{userid})
684 if $webvar{userid};
685
686##fixme: this is only needed iff confirm.tmpl and
687# confirmRemove.tmpl are merged (quite possible, just
688# a little tedious)
689 $page->param(action => "insert");
690
691} # end confirmAssign
692
693
694# Do the work of actually inserting a block in the database.
695sub insertAssign {
696 if ($IPDBacl{$authuser} !~ /a/) {
697 $aclerr = 'addblock';
698 return;
699 }
700 # Some things are done more than once.
701 return if !validateInput();
702
703##fixme: permission check
704 if (!defined($webvar{privdata})) {
705 $webvar{privdata} = '';
706 }
707
708 # $code is "success" vs "failure", $msg contains OK for a
709 # successful netblock allocation, the IP allocated for static
710 # IP, or the error message if an error occurred.
711
712##fixme: consider just passing \%webvar to allocateBlock()?
713 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
714 my %iprev;
715 foreach (keys %webvar) {
716 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
717 }
718
719 # Easier to see and cosmetically fiddle the list like this
720 my %insert_args = (
721 cidr => $webvar{fullcidr},
722 fbid => $webvar{fbid},
723 reserve => $webvar{reserve},
724 parent => $webvar{parent},
725 custid => $webvar{custid},
726 type => $webvar{alloctype},
727 city => $webvar{city},
728 desc => $webvar{desc},
729 notes => $webvar{notes},
730 circid => $webvar{circid},
731 privdata => $webvar{privdata},
732 nodeid => $webvar{node},
733 rdns => $webvar{rdns},
734 vrf => $webvar{vrf},
735 vlan => $webvar{vlan},
736 user => $authuser,
737 );
738
739##fixme: permission check
740 # fill in backup data, if present/allowed
741 if ($webvar{backupfields}) {
742 $insert_args{backup} = 1;
743 for my $bkfield (@IPDB::backupfields) {
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"}) {
911 $page->param(err => "Backup $bkfield must be filled in if IP/netblock is flagged for backup");
912 return;
913 }
914 if ($webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9\s_.-]+$/) {
915 $page->param(err => "Invalid characters in backup $bkfield");
916 return;
917 }
918 }
919 for my $bkfield (qw(type src user)) { # no spaces in these!
920 if ($webvar{"bk$bkfield"} && $webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9_.-]+$/) {
921 $page->param(err => "Invalid characters in backup $bkfield");
922 return;
923 }
924 }
925 if ($webvar{bkport}) {
926 $webvar{bkport} =~ s/^\s+//g;
927 $webvar{bkport} =~ s/\s+$//g;
928 if ($webvar{bkport} !~ /^\d+$/) {
929 $page->param(err => "Backup port must be numeric");
930 return;
931 }
932 }
933##fixme: code review: should normalize $webvar{cidr} variants so we can
934# check for non-/32 allocations having the backup IP field filled in here,
935# instead of failing on the allocation or update attempt
936 if ($webvar{bkip}) {
937 $webvar{bkip} =~ s/^\s+//g;
938 $webvar{bkip} =~ s/\s+$//g;
939 if ($webvar{bkip} !~ /^[\da-fA-F:.]+$/) {
940 $page->param(err => "Backup IP must be an IP");
941 return;
942 }
943 }
944
945 return 'OK';
946} # end validateInput
947
948
949# Displays details of a specific allocation in a form
950# Allows update/delete
951# action=edit
952sub edit {
953
954 # snag block info from db
955 my $blockinfo = getBlockData($ip_dbh, $webvar{id}, $webvar{basetype});
956 my $cidr = new NetAddr::IP $blockinfo->{block};
957 $page->param(id => $webvar{id});
958 $page->param(basetype => $webvar{basetype});
959
960 # Tree navigation
961 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
962 my @rcrumbs = reverse (@$crumbs);
963 $utilbar->param(breadcrumb => \@rcrumbs);
964
965 # Show link to IP list for pools
966 $page->param(ispool => 1) if $blockinfo->{type} =~ /^.[dp]$/;
967
968 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
969 $blockinfo->{type} =~ s/\s//;
970
971##fixme: The case of "allocation larger than a /24" (or any similar case
972# where the allocation is larger than the zone(s) in DNS) doesn't work well.
973# Best solution may just be to add a warning that the entry shown may not be
974# correct/complete.
975 if ($blockinfo->{revavail} || $blockinfo->{revpartial}) {
976 $page->param(showrev => ($blockinfo->{revavail} || $blockinfo->{revpartial}) );
977 my $cached;
978 # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
979 ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser);
980 $page->param(rdns => $blockinfo->{rdns});
981 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
982 $page->param(cached => $cached);
983
984 # Limit the per-IP rDNS list based on CIDR length; larger ones just take up too much space.
985 # Also, don't show on IP pools; the individual IPs will have a space for rDNS
986 # Don't show on single IPs; these use the "pattern" field
987 if ($IPDBacl{$authuser} =~ /c/
988 && $cidr->masklen != $cidr->bits
989 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
990 # config flag for "all block types" OR "not-a-pool-or-IP type"
991 && ($IPDB::revlistalltypes || $blockinfo->{type} !~ /^.[dpi]/)
992 # safety against trying to retrieve and display more than 1k (10 bits, /22 v4) worth of individual IPs
993 # ever. If you really need to manage a long list of IPs like that all in one place, you can use the DNS
994 # management tool. Even a /26 is a bit much, really.
995 && ($cidr->bits - $cidr->masklen) <= 10
996 # do we want to allow v6 at all?
997 #&& ! $cidr->{isv6}
998 ) {
999 $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type},
1000 range => $blockinfo->{block}, user => $authuser) );
1001 }
1002 } # rDNS availability check
1003
1004 # backup data
1005 if ($blockinfo->{hasbk}) {
1006 $page->param(hasbackup => $blockinfo->{hasbk});
1007 for my $bkfield (@IPDB::backupfields) {
1008 $page->param("bk$bkfield" => $blockinfo->{"bk$bkfield"});
1009 }
1010 $page->param(bktelnet => 1) if $blockinfo->{bktype} eq 'telnet';
1011 $page->param(bkssh => 1) if $blockinfo->{bktype} eq 'SSH';
1012 }
1013
1014 # consider extending this to show time as well as date
1015 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
1016 $page->param(lastmod => $lastmod);
1017
1018 $page->param(block => $blockinfo->{block});
1019 $page->param(city => $blockinfo->{city});
1020 $page->param(custid => $blockinfo->{custid});
1021
1022##fixme The check here should be built from the database
1023# Need to expand to support pool types too
1024 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
1025 $page->param(changetype => 1);
1026 $page->param(alloctype => [
1027 { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
1028 { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
1029 { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
1030 { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
1031 { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
1032 { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
1033 { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
1034 ]
1035 );
1036 } else {
1037 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
1038 $page->param(type => $blockinfo->{type});
1039 }
1040
1041## node hack
1042 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block});
1043# $page->param(havenodeid => $nodeid);
1044 $page->param(nodename => $nodename);
1045
1046##fixme: this whole hack needs cleanup and generalization for all alloctypes
1047##fixme: arguably a bug that presence of a nodeid implies it can be changed..
1048 if ($IPDBacl{$authuser} =~ /c/) {
1049 my $nlist = getNodeList($ip_dbh);
1050 if ($nodeid) {
1051 foreach (@{$nlist}) {
1052 $$_{selme} = ($$_{node_id} == $nodeid);
1053 }
1054 }
1055 $page->param(nodelist => $nlist);
1056 }
1057## end node hack
1058
1059 $page->param(vrf => $blockinfo->{vrf});
1060 $page->param(vlan => $blockinfo->{vlan});
1061
1062 # Reserved-for-expansion
1063 $page->param(reserve => $blockinfo->{reserve});
1064 $page->param(reserve_id => $blockinfo->{reserve_id});
1065 my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
1066 $page->param(newblock => $newblock);
1067
1068 # not happy with the upside-down logic, but...
1069 $page->param(swipable => $blockinfo->{type} !~ /.i/);
1070 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
1071
1072 $page->param(circid => $blockinfo->{circuitid});
1073 $page->param(desc => $blockinfo->{description});
1074 $page->param(notes => $blockinfo->{notes});
1075
1076 # Check to see if we can display sensitive data
1077 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
1078 $page->param(privdata => $blockinfo->{privdata});
1079
1080 # ACL trickery - these two template booleans control the presence of all form/input tags
1081 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
1082 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
1083
1084 # Need to find internal knobs to twist to actually vary these. (Ab)use "change" flag for now
1085 $page->param(maymerge => ($IPDBacl{$authuser} =~ /m/ && $blockinfo->{type} !~ /^.i$/));
1086
1087 if ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/) {
1088 if ($blockinfo->{type} =~ /^.p$/) {
1089 # PPP pools
1090 $page->param(maysplit => 1) if $cidr->masklen+1 < $cidr->bits;
1091 } elsif ($blockinfo->{type} =~ /.d/) {
1092 # Non-PPP pools
1093 $page->param(maysplit => 1) if $cidr->masklen+2 < $cidr->bits;
1094 } else {
1095 # Standard netblocks. Arguably allowing splitting these down to single IPs
1096 # doesn't make much sense, but forcing users to apply allocation types
1097 # "properly" is worse than herding cats.
1098 $page->param(maysplit => 1) if $cidr->masklen < $cidr->bits;
1099 }
1100 }
1101
1102} # edit()
1103
1104
1105# Stuff new info about a block into the db
1106# action=update
1107sub update {
1108 if ($IPDBacl{$authuser} !~ /c/) {
1109 $aclerr = 'updateblock';
1110 return;
1111 }
1112
1113 # Collect existing block info here, since we need it for the breadcrumb nav
1114 my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1115 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1116 my @rcrumbs = reverse (@$crumbs);
1117 $utilbar->param(breadcrumb => \@rcrumbs);
1118
1119 # Make sure incoming data is in correct format - custID among other things.
1120 return if !validateInput;
1121
1122 $webvar{swip} = 'n' if !$webvar{swip};
1123
1124 my %updargs = (
1125 custid => $webvar{custid},
1126 city => $webvar{city},
1127 description => $webvar{desc},
1128 notes => $webvar{notes},
1129 circuitid => $webvar{circid},
1130 block => $webvar{block},
1131 type => $webvar{alloctype},
1132 swip => $webvar{swip},
1133 rdns => $webvar{rdns},
1134 vrf => $webvar{vrf},
1135 vlan => $webvar{vlan},
1136 user => $authuser,
1137 );
1138
1139 # Check to see if user is allowed to do anything with sensitive data
1140 if ($IPDBacl{$authuser} =~ /s/) {
1141 $updargs{privdata} = $webvar{privdata};
1142 for my $bkfield (@IPDB::backupfields) {
1143 $updargs{"bk$bkfield"} = $webvar{"bk$bkfield"};
1144 }
1145 $updargs{backup} = $webvar{backupfields};
1146 } else {
1147 # If the user doesn't have permissions to monkey with NOC-things, pass
1148 # a flag so we don't treat it as "backup data removed"
1149 $updargs{ignorebk} = 1;
1150 }
1151
1152 # Semioptional values
1153 $updargs{node} = $webvar{node} if $webvar{node};
1154
1155 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
1156 my %iprev;
1157 foreach (keys %webvar) {
1158 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
1159 }
1160
1161 # Merge with reserved freeblock
1162 $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme};
1163
1164 my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
1165
1166 if ($code eq 'FAIL') {
1167 syslog "err", "$authuser could not update block/IP '$binfo->{block}' (id $webvar{block}): '$msg'";
1168 $page->param(err => "Could not update block/IP $binfo->{block}: $msg");
1169 return;
1170 }
1171
1172 # If we get here, the operation succeeded.
1173 syslog "notice", "$authuser updated $binfo->{block}";
1174##fixme: log details of the change? old way is in the .debug stream anyway.
1175##fixme: need to wedge something in to allow "update:field" notifications
1176## hmm. how to tell what changed? O_o
1177mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $binfo->{block}",
1178 "$binfo->{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
1179
1180## node hack
1181 if ($webvar{node} && $webvar{node} ne '-') {
1182 my $nodename = getNodeName($ip_dbh, $webvar{node});
1183 $page->param(nodename => $nodename);
1184 }
1185## end node hack
1186
1187 # Link back to browse-routed or list-pool page on "Update complete" page.
1188 my $pblock = getBlockData($ip_dbh, $binfo->{parent_id});
1189 $page->param(backid => $binfo->{parent_id});
1190 $page->param(backblock => $pblock->{block});
1191 $page->param(backpool => ($webvar{basetype} eq 'i'));
1192
1193 # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
1194 # because otherwise we can't convert \n to <br>. *sigh*
1195 $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
1196 $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
1197 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
1198 $webvar{privdata} =~ s/\n/<br>\n/;
1199
1200 if ($webvar{expandme}) {
1201 # this is fugly but still faster than hitting the DB again with getBlockData()
1202 my $tmp = new NetAddr::IP $binfo->{block};
1203 my $fb = new NetAddr::IP $binfo->{reserve};
1204 my @newblock = $tmp->compact($fb);
1205 $page->param(cidr => $newblock[0]);
1206 } else {
1207 $page->param(cidr => $binfo->{block});
1208 }
1209 $page->param(rdns => $webvar{rdns});
1210 $page->param(city => $webvar{city});
1211 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
1212 $page->param(custid => $webvar{custid});
1213 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
1214 $page->param(circid => $webvar{circid});
1215 $page->param(desc => $webvar{desc});
1216 $page->param(notes => $webvar{notes});
1217 if ($IPDBacl{$authuser} =~ /s/) {
1218 $page->param(nocling => 1);
1219 $page->param(privdata => $webvar{privdata});
1220 if ($webvar{backupfields} && $webvar{backupfields} eq 'on') {
1221 $page->param(hasbackup => 1);
1222 for my $bkfield (@IPDB::backupfields) {
1223 $page->param("bk$bkfield" => $webvar{"bk$bkfield"});
1224 }
1225 }
1226 }
1227
1228} # update()
1229
1230
1231sub prepSplit {
1232 if ($IPDBacl{$authuser} !~ /c/) {
1233 $aclerr = 'splitblock';
1234 return;
1235 }
1236
1237 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1238
1239 # Tree navigation
1240 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1241 my @rcrumbs = reverse (@$crumbs);
1242 $utilbar->param(breadcrumb => \@rcrumbs);
1243
1244 if ($blockinfo->{type} =~ /^.i$/) {
1245 $page->param(err => "Can't split a single IP allocation");
1246 return;
1247 }
1248
1249 # Info about current allocation
1250 $page->param(oldblock => $blockinfo->{block});
1251 $page->param(block => $webvar{block});
1252
1253# Note that there are probably different rules that should be followed to restrict splitting IPv6 blocks;
1254# strictly speaking it will be exceptionally rare to see smaller than a /64 assigned to a customer, since that
1255# breaks auto-addressing schemes.
1256
1257 # Generate possible splits
1258 my $block = new NetAddr::IP $blockinfo->{block};
1259 my $oldmask = $block->masklen;
1260 if ($blockinfo->{type} =~ /^.d$/) {
1261 # Non-PPP pools
1262 $page->param(ispool => 1);
1263 if ($oldmask+2 >= $block->bits) {
1264 $page->param(err => "Can't split a standard netblock pool any further");
1265 return;
1266 }
1267 # Allow splitting down to v4 /30 (which results in one usable IP; dubiously useful)
1268 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-2;
1269 } elsif ($blockinfo->{type} =~ /.p/) {
1270 $page->param(ispool => 1);
1271 # Allow splitting PPP pools down to v4 /31
1272 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-1;
1273 } else {
1274 # Allow splitting all other non-pool netblocks down to single IPs, which...
1275 # arguably should be *aggregated* in a pool. Except where they shouldn't.
1276 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits;
1277 }
1278 # set the split-in-half mask
1279 $page->param(sp2mask => $oldmask+1);
1280
1281 # Generate possible shrink targets
1282 my @keepers = $block->split($block->masklen+1);
1283 $page->param(newblockA => $keepers[0]);
1284 $page->param(newblockB => $keepers[1]);
1285} # prepSplit()
1286
1287
1288sub doSplit {
1289 if ($IPDBacl{$authuser} !~ /c/) {
1290 $aclerr = 'splitblock';
1291 return;
1292 }
1293
1294##fixme: need consistent way to identify "this thing that is this thing" with only the ID
1295# also applies to other locations
1296 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1297
1298 # Tree navigation
1299 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1300 my @rcrumbs = reverse (@$crumbs);
1301 $utilbar->param(breadcrumb => \@rcrumbs);
1302
1303 if ($blockinfo->{type} =~ /^.i$/) {
1304 $page->param(err => "Can't split a single IP allocation");
1305 return;
1306 }
1307
1308 if ($webvar{subact} eq 'split') {
1309 $page->param(issplit => 1);
1310 my $block = new NetAddr::IP $blockinfo->{block};
1311 my $newblocks = splitBlock($ip_dbh, id => $webvar{block}, basetype => 'b', newmask => $webvar{split},
1312 user => $authuser);
1313 if ($newblocks) {
1314 $page->param(newblocks => $newblocks);
1315 } else {
1316 $page->param(err => $IPDB::errstr);
1317 }
1318
1319 } elsif ($webvar{subact} eq 'shrink') {
1320 $page->param(nid => $webvar{block});
1321 $page->param(newblock => $webvar{shrink});
1322 my $newfree = shrinkBlock($ip_dbh, $webvar{block}, $webvar{shrink});
1323 if ($newfree) {
1324 $page->param(newfb => $newfree);
1325 } else {
1326 $page->param(err => $IPDB::errstr);
1327 }
1328
1329 } else {
1330 # Your llama is on fire.
1331 $page->param(err => "Missing form field that shouldn't be missing.");
1332 return;
1333 }
1334
1335 # common bits
1336 $page->param(cidr => $blockinfo->{block});
1337 # and the backlink to the parent container
1338 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id});
1339 $page->param(backid => $blockinfo->{parent_id});
1340 $page->param(backblock => $pinfo->{block});
1341} # doSplit()
1342
1343
1344# Set up for merge
1345sub prepMerge {
1346 if ($IPDBacl{$authuser} !~ /m/) {
1347 $aclerr = 'mergeblock';
1348 return;
1349 }
1350
1351 my $binfo = getBlockData($ip_dbh, $webvar{block});
1352
1353 # Tree navigation
1354 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1355 my @rcrumbs = reverse (@$crumbs);
1356 $utilbar->param(breadcrumb => \@rcrumbs);
1357
1358 $page->param(block => $webvar{block});
1359 $page->param(ispool => $binfo->{type} =~ /.[dp]/);
1360 $page->param(ismaster => $binfo->{type} eq 'mm');
1361 $page->param(oldblock => $binfo->{block});
1362 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1363 $page->param(typelist => getTypeList($ip_dbh, 'n', $binfo->{type})); # down the rabbit hole we go...
1364
1365 # Strings for scope; do this way so we don't have to edit them many places
1366 $page->param(vis_keepall => $merge_display{keepall});
1367 $page->param(vis_mergepeer => $merge_display{mergepeer});
1368 $page->param(vis_clearpeer => $merge_display{clearpeer});
1369 $page->param(vis_clearall => $merge_display{clearall});
1370
1371} # prepMerge()
1372
1373
1374# Show what will be merged, present warnings about data loss
1375sub confMerge {
1376 if ($IPDBacl{$authuser} !~ /m/) {
1377 $aclerr = 'mergeblock';
1378 return;
1379 }
1380
1381 if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) {
1382 $page->param(err => 'New netmask required');
1383 return;
1384 }
1385
1386 $page->param(block => $webvar{block});
1387 my $binfo = getBlockData($ip_dbh, $webvar{block});
1388 my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
1389 my $minfo = getBlockData($ip_dbh, $binfo->{master_id});
1390 my $block = new NetAddr::IP $binfo->{block};
1391
1392 # Tree navigation
1393 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1394 my @rcrumbs = reverse (@$crumbs);
1395 $utilbar->param(breadcrumb => \@rcrumbs);
1396
1397 $page->param(oldblock => $binfo->{block});
1398 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1399 $page->param(ismaster => $binfo->{type} eq 'mm');
1400 $page->param(ispool => $webvar{alloctype} =~ /.[dp]/);
1401 $page->param(isleaf => $webvar{alloctype} =~ /.[enr]/);
1402 $page->param(newtype => $webvar{alloctype});
1403 $page->param(newdisptype => $disp_alloctypes{$webvar{alloctype}});
1404 my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}";
1405 $newblock = $newblock->network;
1406 $page->param(newmask => $webvar{newmask});
1407 $page->param(newblock => "$newblock");
1408
1409 # get list of allocations and freeblocks to be merged
1410 my $malloc_list = listForMerge($ip_dbh, $binfo->{parent_id}, $newblock, 'a');
1411 $page->param(mergealloc => $malloc_list);
1412
1413 $page->param(vis_scope => $merge_display{$webvar{scope}});
1414 $page->param(scope => $webvar{scope});
1415} # confMerge()
1416
1417
1418# Make it so
1419sub doMerge {
1420 if ($IPDBacl{$authuser} !~ /m/) {
1421 $aclerr = 'mergeblock';
1422 return;
1423 }
1424
1425 if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) {
1426 $page->param(err => 'New netmask required');
1427 return;
1428 }
1429
1430 $page->param(block => $webvar{block});
1431 my $binfo = getBlockData($ip_dbh, $webvar{block});
1432 my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
1433 my $block = new NetAddr::IP $binfo->{block};
1434
1435 # Tree navigation
1436 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1437 my @rcrumbs = reverse (@$crumbs);
1438 $utilbar->param(breadcrumb => \@rcrumbs);
1439
1440 $page->param(oldblock => $binfo->{block});
1441 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1442 $page->param(newdisptype => $disp_alloctypes{$webvar{newtype}});
1443 my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}";
1444 $newblock = $newblock->network;
1445 $page->param(newblock => $newblock);
1446 $page->param(vis_scope => $merge_display{$webvar{scope}});
1447
1448 my $mlist = mergeBlocks($ip_dbh, $webvar{block}, %webvar, user => $authuser);
1449
1450 if ($mlist) {
1451 #(newtype => $webvar{newtype}, newmask => $webvar{newmask}));
1452 # Slice off first entry (the new parent - note this may be a new allocation,
1453 # not the same ID that was "merged"!
1454 my $parent = shift @$mlist;
1455 $page->param(backpool => $webvar{newtype} =~ /.[dp]/);
1456 if ($webvar{newtype} =~ /.[enr]/) {
1457 $page->param(backleaf => 1);
1458 $page->param(backid => $binfo->{parent_id});
1459 $page->param(backblock => $pinfo->{block});
1460 } else {
1461 $page->param(backid => $parent->{id});
1462 $page->param(backblock => $parent->{block});
1463 }
1464 $page->param(mergelist => $mlist);
1465 } else {
1466 $page->param(err => "Merge failed: $IPDB::errstr");
1467 }
1468} # doMerge()
1469
1470
1471# Delete an allocation.
1472sub remove {
1473 if ($IPDBacl{$authuser} !~ /d/) {
1474 $aclerr = 'delblock';
1475 return;
1476 }
1477
1478 # Serves'em right for getting here...
1479 if (!defined($webvar{block})) {
1480 $page->param(err => "Can't delete a block that doesn't exist");
1481 return;
1482 }
1483
1484 my $blockdata;
1485 $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1486
1487 # Tree navigation
1488 my $crumbs = getBreadCrumbs($ip_dbh, $blockdata->{parent_id});
1489 my @rcrumbs = reverse (@$crumbs);
1490 $utilbar->param(breadcrumb => \@rcrumbs);
1491
1492 if ($blockdata->{parent_id} == 0) { # $webvar{alloctype} eq 'mm'
1493 $blockdata->{city} = "N/A";
1494 $blockdata->{custid} = "N/A";
1495 $blockdata->{circuitid} = "N/A";
1496 $blockdata->{description} = "N/A";
1497 $blockdata->{notes} = "N/A";
1498 $blockdata->{privdata} = "N/A";
1499 } # end cases for different alloctypes
1500
1501 $page->param(blockid => $webvar{block});
1502 $page->param(basetype => $webvar{basetype});
1503
1504 $page->param(block => $blockdata->{block});
1505 $page->param(rdns => $blockdata->{rdns});
1506
1507 # maybe need to apply more magic here?
1508 # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
1509 # -> all real blocks (nb: pool IPs need extra handling)
1510 # -> NOC/private-IP (how to ID?)
1511 # -> anything with a pattern matching $IPDB::domain?
1512 if ($blockdata->{type} !~ /^.i$/) {
1513 $page->param(autodel => 1);
1514 }
1515
1516 $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
1517 $page->param(city => $blockdata->{city});
1518 $page->param(custid => $blockdata->{custid});
1519 $page->param(circid => $blockdata->{circuitid});
1520 $page->param(desc => $blockdata->{description});
1521 $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
1522 $blockdata->{notes} =~ s/\n/<br>\n/;
1523 $page->param(notes => $blockdata->{notes});
1524 $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
1525 $blockdata->{privdata} = '&nbsp;' if !$blockdata->{privdata};
1526 $blockdata->{privdata} =~ s/\n/<br>\n/;
1527 $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
1528 $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
1529
1530} # end remove()
1531
1532
1533# Delete an allocation. Return it to the freeblocks table; munge
1534# data as necessary to keep as few records as possible in freeblocks
1535# to prevent weirdness when allocating blocks later.
1536# Remove IPs from pool listing if necessary
1537sub finalDelete {
1538 if ($IPDBacl{$authuser} !~ /d/) {
1539 $aclerr = 'delblock';
1540 return;
1541 }
1542
1543 # need to retrieve block data before deleting so we can notify on that
1544 my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1545 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b');
1546
1547 # Tree navigation
1548 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1549 my @rcrumbs = reverse (@$crumbs);
1550 $utilbar->param(breadcrumb => \@rcrumbs);
1551
1552 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser);
1553
1554 $page->param(block => $blockinfo->{block});
1555 $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}}));
1556 $page->param(delparent_id => $blockinfo->{parent_id});
1557 if ($pinfo) {
1558 $page->param(delparent => $pinfo->{block});
1559 $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}}));
1560 }
1561 $page->param(returnpool => ($webvar{basetype} eq 'i') );
1562 if ($code =~ /^WARN(POOL|MERGE)/) {
1563 my ($pid,$pcidr) = split /,/, $msg;
1564 my $real_pinfo = getBlockData($ip_dbh, $pid, 'b');
1565 $page->param(parent_id => $pid);
1566 $page->param(parent => $pcidr);
1567 $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}}));
1568 $page->param(mergeip => $code eq 'WARNPOOL');
1569 }
1570 if ($code eq 'WARN') {
1571 $msg =~ s/\n/<br>\n/g;
1572 $page->param(genwarn => $msg);
1573 }
1574 if ($code eq 'OK' || $code =~ /^WARN/) {
1575 syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock $webvar{block} ".
1576 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
1577 mailNotify($ip_dbh, 'da', "REMOVED: ".$disp_alloctypes{$blockinfo->{type}}." $webvar{block}",
1578 $disp_alloctypes{$blockinfo->{type}}." $webvar{block} deallocated by $authuser\n".
1579 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
1580 "\nDescription: ".$blockinfo->{description}."\n");
1581 } else {
1582 $page->param(failmsg => $msg);
1583 if ($webvar{alloctype} =~ /^.i$/) {
1584 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1585 } else {
1586 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
1587 $page->param(netblock => 1);
1588 }
1589 }
1590
1591} # finalDelete
Note: See TracBrowser for help on using the repository browser.