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

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

/trunk

Extend UI to browse into a VRF. See #54.
Also extend breadcrumb segment to show the VRF.

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