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

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

/trunk

"add VRF" action etc. See #54.

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