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

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

/trunk

Wrap validation of backup fields in an ACL check. See #52.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 56.0 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3###
4# SVN revision info
5# $Date: 2016-03-08 21:08:40 +0000 (Tue, 08 Mar 2016) $
6# SVN revision $Rev: 813 $
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 # Bypass if the user isn't authorized for backup data, or if the checkbox is unchecked
967 if ($IPDBacl{$authuser} =~ /s/ && defined($webvar{backupfields})) {
968 for my $bkfield (qw(brand model)) {
969 if (!$webvar{"bk$bkfield"}) {
970 $page->param(err => "Backup $bkfield must be filled in if IP/netblock is flagged for backup");
971 return;
972 }
973 if ($webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9\s_.-]+$/) {
974 $page->param(err => "Invalid characters in backup $bkfield");
975 return;
976 }
977 }
978 for my $bkfield (qw(type src user)) { # no spaces in these!
979 if ($webvar{"bk$bkfield"} && $webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9_.-]+$/) {
980 $page->param(err => "Invalid characters in backup $bkfield");
981 return;
982 }
983 }
984 if ($webvar{bkport}) {
985 $webvar{bkport} =~ s/^\s+//g;
986 $webvar{bkport} =~ s/\s+$//g;
987 if ($webvar{bkport} !~ /^\d+$/) {
988 $page->param(err => "Backup port must be numeric");
989 return;
990 }
991 }
992##fixme: code review: should normalize $webvar{cidr} variants so we can
993# check for non-/32 allocations having the backup IP field filled in here,
994# instead of failing on the allocation or update attempt
995 if ($webvar{bkip}) {
996 $webvar{bkip} =~ s/^\s+//g;
997 $webvar{bkip} =~ s/\s+$//g;
998 if ($webvar{bkip} !~ /^[\da-fA-F:.]+$/) {
999 $page->param(err => "Backup IP must be an IP");
1000 return;
1001 }
1002 }
1003 } # backup
1004
1005 return 'OK';
1006} # end validateInput
1007
1008
1009# Displays details of a specific allocation in a form
1010# Allows update/delete
1011# action=edit
1012sub edit {
1013
1014 # snag block info from db
1015 my $blockinfo = getBlockData($ip_dbh, $webvar{id}, $webvar{basetype});
1016 my $cidr = new NetAddr::IP $blockinfo->{block};
1017 $page->param(id => $webvar{id});
1018 $page->param(basetype => $webvar{basetype});
1019
1020 # Tree navigation
1021 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1022 my @rcrumbs = reverse (@$crumbs);
1023 $utilbar->param(breadcrumb => \@rcrumbs);
1024
1025 # Show link to IP list for pools
1026 $page->param(ispool => 1) if $blockinfo->{type} =~ /^.[dp]$/;
1027
1028 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
1029 $blockinfo->{type} =~ s/\s//;
1030
1031##fixme: The case of "allocation larger than a /24" (or any similar case
1032# where the allocation is larger than the zone(s) in DNS) doesn't work well.
1033# Best solution may just be to add a warning that the entry shown may not be
1034# correct/complete.
1035 if ($blockinfo->{revavail} || $blockinfo->{revpartial}) {
1036 $page->param(showrev => ($blockinfo->{revavail} || $blockinfo->{revpartial}) );
1037 my $cached;
1038 # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
1039 ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser);
1040 $page->param(rdns => $blockinfo->{rdns});
1041 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
1042 $page->param(cached => $cached);
1043
1044 # Limit the per-IP rDNS list based on CIDR length; larger ones just take up too much space.
1045 # Also, don't show on IP pools; the individual IPs will have a space for rDNS
1046 # Don't show on single IPs; these use the "pattern" field
1047 if ($IPDBacl{$authuser} =~ /c/
1048 && $cidr->masklen != $cidr->bits
1049 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
1050 # config flag for "all block types" OR "not-a-pool-or-IP type"
1051 && ($IPDB::revlistalltypes || $blockinfo->{type} !~ /^.[dpi]/)
1052 # safety against trying to retrieve and display more than 1k (10 bits, /22 v4) worth of individual IPs
1053 # ever. If you really need to manage a long list of IPs like that all in one place, you can use the DNS
1054 # management tool. Even a /26 is a bit much, really.
1055 && ($cidr->bits - $cidr->masklen) <= 10
1056 # do we want to allow v6 at all?
1057 #&& ! $cidr->{isv6}
1058 ) {
1059 $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type},
1060 range => $blockinfo->{block}, user => $authuser) );
1061 }
1062 } # rDNS availability check
1063
1064 # backup data
1065 if ($blockinfo->{hasbk}) {
1066 $page->param(hasbackup => $blockinfo->{hasbk});
1067 for my $bkfield (@IPDB::backupfields) {
1068 $page->param("bk$bkfield" => $blockinfo->{"bk$bkfield"});
1069 }
1070 $page->param(bktelnet => 1) if $blockinfo->{bktype} eq 'telnet';
1071 $page->param(bkssh => 1) if $blockinfo->{bktype} eq 'SSH';
1072 }
1073
1074 # consider extending this to show time as well as date
1075 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
1076 $page->param(lastmod => $lastmod);
1077
1078 $page->param(block => $blockinfo->{block});
1079 $page->param(city => $blockinfo->{city});
1080 $page->param(custid => $blockinfo->{custid});
1081
1082##fixme The check here should be built from the database
1083# Need to expand to support pool types too
1084 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
1085 $page->param(changetype => 1);
1086 $page->param(alloctype => [
1087 { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
1088 { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
1089 { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
1090 { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
1091 { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
1092 { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
1093 { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
1094 ]
1095 );
1096 } else {
1097 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
1098 $page->param(type => $blockinfo->{type});
1099 }
1100
1101## node hack
1102 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block});
1103# $page->param(havenodeid => $nodeid);
1104 $page->param(nodename => $nodename);
1105
1106##fixme: this whole hack needs cleanup and generalization for all alloctypes
1107##fixme: arguably a bug that presence of a nodeid implies it can be changed..
1108 if ($IPDBacl{$authuser} =~ /c/) {
1109 my $nlist = getNodeList($ip_dbh);
1110 if ($nodeid) {
1111 foreach (@{$nlist}) {
1112 $$_{selme} = ($$_{node_id} == $nodeid);
1113 }
1114 }
1115 $page->param(nodelist => $nlist);
1116 }
1117## end node hack
1118
1119 $page->param(vrf => $blockinfo->{vrf});
1120 $page->param(vlan => $blockinfo->{vlan});
1121
1122 # Reserved-for-expansion
1123 $page->param(reserve => $blockinfo->{reserve});
1124 $page->param(reserve_id => $blockinfo->{reserve_id});
1125 my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
1126 $page->param(newblock => $newblock);
1127
1128 # not happy with the upside-down logic, but...
1129 $page->param(swipable => $blockinfo->{type} !~ /.i/);
1130 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
1131
1132 $page->param(circid => $blockinfo->{circuitid});
1133 $page->param(desc => $blockinfo->{description});
1134 $page->param(notes => $blockinfo->{notes});
1135
1136 # Check to see if we can display sensitive data
1137 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
1138 $page->param(privdata => $blockinfo->{privdata});
1139
1140 # ACL trickery - these two template booleans control the presence of all form/input tags
1141 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
1142 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
1143
1144 # Need to find internal knobs to twist to actually vary these. (Ab)use "change" flag for now
1145 $page->param(maymerge => ($IPDBacl{$authuser} =~ /m/ && $blockinfo->{type} !~ /^.i$/));
1146
1147 if ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/) {
1148 if ($blockinfo->{type} =~ /^.p$/) {
1149 # PPP pools
1150 $page->param(maysplit => 1) if $cidr->masklen+1 < $cidr->bits;
1151 } elsif ($blockinfo->{type} =~ /.d/) {
1152 # Non-PPP pools
1153 $page->param(maysplit => 1) if $cidr->masklen+2 < $cidr->bits;
1154 } else {
1155 # Standard netblocks. Arguably allowing splitting these down to single IPs
1156 # doesn't make much sense, but forcing users to apply allocation types
1157 # "properly" is worse than herding cats.
1158 $page->param(maysplit => 1) if $cidr->masklen < $cidr->bits;
1159 }
1160 }
1161
1162} # edit()
1163
1164
1165# Stuff new info about a block into the db
1166# action=update
1167sub update {
1168 if ($IPDBacl{$authuser} !~ /c/) {
1169 $aclerr = 'updateblock';
1170 return;
1171 }
1172
1173 # Collect existing block info here, since we need it for the breadcrumb nav
1174 my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1175 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1176 my @rcrumbs = reverse (@$crumbs);
1177 $utilbar->param(breadcrumb => \@rcrumbs);
1178
1179 # Make sure incoming data is in correct format - custID among other things.
1180 return if !validateInput;
1181
1182 $webvar{swip} = 'n' if !$webvar{swip};
1183
1184 my %updargs = (
1185 custid => $webvar{custid},
1186 city => $webvar{city},
1187 description => $webvar{desc},
1188 notes => $webvar{notes},
1189 circuitid => $webvar{circid},
1190 block => $webvar{block},
1191 type => $webvar{alloctype},
1192 swip => $webvar{swip},
1193 rdns => $webvar{rdns},
1194 vrf => $webvar{vrf},
1195 vlan => $webvar{vlan},
1196 user => $authuser,
1197 );
1198
1199 # Check to see if user is allowed to do anything with sensitive data
1200 if ($IPDBacl{$authuser} =~ /s/) {
1201 $updargs{privdata} = $webvar{privdata};
1202 for my $bkfield (@IPDB::backupfields) {
1203 $updargs{"bk$bkfield"} = $webvar{"bk$bkfield"};
1204 }
1205 $updargs{backup} = $webvar{backupfields};
1206 } else {
1207 # If the user doesn't have permissions to monkey with NOC-things, pass
1208 # a flag so we don't treat it as "backup data removed"
1209 $updargs{ignorebk} = 1;
1210 }
1211
1212 # Semioptional values
1213 $updargs{node} = $webvar{node} if $webvar{node};
1214
1215 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
1216 my %iprev;
1217 foreach (keys %webvar) {
1218 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
1219 }
1220
1221 # Merge with reserved freeblock
1222 $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme};
1223
1224 my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
1225
1226 if ($code eq 'FAIL') {
1227 syslog "err", "$authuser could not update block/IP '$binfo->{block}' (id $webvar{block}): '$msg'";
1228 $page->param(err => "Could not update block/IP $binfo->{block}: $msg");
1229 return;
1230 }
1231
1232 # If we get here, the operation succeeded.
1233 syslog "notice", "$authuser updated $binfo->{block}";
1234##fixme: log details of the change? old way is in the .debug stream anyway.
1235##fixme: need to wedge something in to allow "update:field" notifications
1236## hmm. how to tell what changed? O_o
1237mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $binfo->{block}",
1238 "$binfo->{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
1239
1240## node hack
1241 if ($webvar{node} && $webvar{node} ne '-') {
1242 my $nodename = getNodeName($ip_dbh, $webvar{node});
1243 $page->param(nodename => $nodename);
1244 }
1245## end node hack
1246
1247 # Link back to browse-routed or list-pool page on "Update complete" page.
1248 my $pblock = getBlockData($ip_dbh, $binfo->{parent_id});
1249 $page->param(backid => $binfo->{parent_id});
1250 $page->param(backblock => $pblock->{block});
1251 $page->param(backpool => ($webvar{basetype} eq 'i'));
1252
1253 # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
1254 # because otherwise we can't convert \n to <br>. *sigh*
1255 $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
1256 $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
1257 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
1258 $webvar{privdata} =~ s/\n/<br>\n/;
1259
1260 if ($webvar{expandme}) {
1261 # this is fugly but still faster than hitting the DB again with getBlockData()
1262 my $tmp = new NetAddr::IP $binfo->{block};
1263 my $fb = new NetAddr::IP $binfo->{reserve};
1264 my @newblock = $tmp->compact($fb);
1265 $page->param(cidr => $newblock[0]);
1266 } else {
1267 $page->param(cidr => $binfo->{block});
1268 }
1269 $page->param(rdns => $webvar{rdns});
1270 $page->param(city => $webvar{city});
1271 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
1272 $page->param(custid => $webvar{custid});
1273 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
1274 $page->param(circid => $webvar{circid});
1275 $page->param(desc => $webvar{desc});
1276 $page->param(notes => $webvar{notes});
1277 if ($IPDBacl{$authuser} =~ /s/) {
1278 $page->param(nocling => 1);
1279 $page->param(privdata => $webvar{privdata});
1280 if ($webvar{backupfields} && $webvar{backupfields} eq 'on') {
1281 $page->param(hasbackup => 1);
1282 for my $bkfield (@IPDB::backupfields) {
1283 $page->param("bk$bkfield" => $webvar{"bk$bkfield"});
1284 }
1285 }
1286 }
1287
1288} # update()
1289
1290
1291sub prepSplit {
1292 if ($IPDBacl{$authuser} !~ /c/) {
1293 $aclerr = 'splitblock';
1294 return;
1295 }
1296
1297 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1298
1299 # Tree navigation
1300 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1301 my @rcrumbs = reverse (@$crumbs);
1302 $utilbar->param(breadcrumb => \@rcrumbs);
1303
1304 if ($blockinfo->{type} =~ /^.i$/) {
1305 $page->param(err => "Can't split a single IP allocation");
1306 return;
1307 }
1308
1309 # Info about current allocation
1310 $page->param(oldblock => $blockinfo->{block});
1311 $page->param(block => $webvar{block});
1312
1313# Note that there are probably different rules that should be followed to restrict splitting IPv6 blocks;
1314# strictly speaking it will be exceptionally rare to see smaller than a /64 assigned to a customer, since that
1315# breaks auto-addressing schemes.
1316
1317 # Generate possible splits
1318 my $block = new NetAddr::IP $blockinfo->{block};
1319 my $oldmask = $block->masklen;
1320 if ($blockinfo->{type} =~ /^.d$/) {
1321 # Non-PPP pools
1322 $page->param(ispool => 1);
1323 if ($oldmask+2 >= $block->bits) {
1324 $page->param(err => "Can't split a standard netblock pool any further");
1325 return;
1326 }
1327 # Allow splitting down to v4 /30 (which results in one usable IP; dubiously useful)
1328 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-2;
1329 } elsif ($blockinfo->{type} =~ /.p/) {
1330 $page->param(ispool => 1);
1331 # Allow splitting PPP pools down to v4 /31
1332 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-1;
1333 } else {
1334 # Allow splitting all other non-pool netblocks down to single IPs, which...
1335 # arguably should be *aggregated* in a pool. Except where they shouldn't.
1336 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits;
1337 }
1338 # set the split-in-half mask
1339 $page->param(sp2mask => $oldmask+1);
1340
1341 # Generate possible shrink targets
1342 my @keepers = $block->split($block->masklen+1);
1343 $page->param(newblockA => $keepers[0]);
1344 $page->param(newblockB => $keepers[1]);
1345} # prepSplit()
1346
1347
1348sub doSplit {
1349 if ($IPDBacl{$authuser} !~ /c/) {
1350 $aclerr = 'splitblock';
1351 return;
1352 }
1353
1354##fixme: need consistent way to identify "this thing that is this thing" with only the ID
1355# also applies to other locations
1356 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1357
1358 # Tree navigation
1359 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1360 my @rcrumbs = reverse (@$crumbs);
1361 $utilbar->param(breadcrumb => \@rcrumbs);
1362
1363 if ($blockinfo->{type} =~ /^.i$/) {
1364 $page->param(err => "Can't split a single IP allocation");
1365 return;
1366 }
1367
1368 if ($webvar{subact} eq 'split') {
1369 $page->param(issplit => 1);
1370 my $block = new NetAddr::IP $blockinfo->{block};
1371 my $newblocks = splitBlock($ip_dbh, id => $webvar{block}, basetype => 'b', newmask => $webvar{split},
1372 user => $authuser);
1373 if ($newblocks) {
1374 $page->param(newblocks => $newblocks);
1375 } else {
1376 $page->param(err => $IPDB::errstr);
1377 }
1378
1379 } elsif ($webvar{subact} eq 'shrink') {
1380 $page->param(nid => $webvar{block});
1381 $page->param(newblock => $webvar{shrink});
1382 my $newfree = shrinkBlock($ip_dbh, $webvar{block}, $webvar{shrink});
1383 if ($newfree) {
1384 $page->param(newfb => $newfree);
1385 } else {
1386 $page->param(err => $IPDB::errstr);
1387 }
1388
1389 } else {
1390 # Your llama is on fire.
1391 $page->param(err => "Missing form field that shouldn't be missing.");
1392 return;
1393 }
1394
1395 # common bits
1396 $page->param(cidr => $blockinfo->{block});
1397 # and the backlink to the parent container
1398 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id});
1399 $page->param(backid => $blockinfo->{parent_id});
1400 $page->param(backblock => $pinfo->{block});
1401} # doSplit()
1402
1403
1404# Set up for merge
1405sub prepMerge {
1406 if ($IPDBacl{$authuser} !~ /m/) {
1407 $aclerr = 'mergeblock';
1408 return;
1409 }
1410
1411 my $binfo = getBlockData($ip_dbh, $webvar{block});
1412
1413 # Tree navigation
1414 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1415 my @rcrumbs = reverse (@$crumbs);
1416 $utilbar->param(breadcrumb => \@rcrumbs);
1417
1418 $page->param(block => $webvar{block});
1419 $page->param(ispool => $binfo->{type} =~ /.[dp]/);
1420 $page->param(ismaster => $binfo->{type} eq 'mm');
1421 $page->param(oldblock => $binfo->{block});
1422 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1423 $page->param(typelist => getTypeList($ip_dbh, 'n', $binfo->{type})); # down the rabbit hole we go...
1424
1425 # Strings for scope; do this way so we don't have to edit them many places
1426 $page->param(vis_keepall => $merge_display{keepall});
1427 $page->param(vis_mergepeer => $merge_display{mergepeer});
1428 $page->param(vis_clearpeer => $merge_display{clearpeer});
1429 $page->param(vis_clearall => $merge_display{clearall});
1430
1431} # prepMerge()
1432
1433
1434# Show what will be merged, present warnings about data loss
1435sub confMerge {
1436 if ($IPDBacl{$authuser} !~ /m/) {
1437 $aclerr = 'mergeblock';
1438 return;
1439 }
1440
1441 if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) {
1442 $page->param(err => 'New netmask required');
1443 return;
1444 }
1445
1446 $page->param(block => $webvar{block});
1447 my $binfo = getBlockData($ip_dbh, $webvar{block});
1448 my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
1449 my $minfo = getBlockData($ip_dbh, $binfo->{master_id});
1450 my $block = new NetAddr::IP $binfo->{block};
1451
1452 # Tree navigation
1453 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1454 my @rcrumbs = reverse (@$crumbs);
1455 $utilbar->param(breadcrumb => \@rcrumbs);
1456
1457 $page->param(oldblock => $binfo->{block});
1458 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1459 $page->param(ismaster => $binfo->{type} eq 'mm');
1460 $page->param(ispool => $webvar{alloctype} =~ /.[dp]/);
1461 $page->param(isleaf => $webvar{alloctype} =~ /.[enr]/);
1462 $page->param(newtype => $webvar{alloctype});
1463 $page->param(newdisptype => $disp_alloctypes{$webvar{alloctype}});
1464 my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}";
1465 $newblock = $newblock->network;
1466 $page->param(newmask => $webvar{newmask});
1467 $page->param(newblock => "$newblock");
1468
1469 # get list of allocations and freeblocks to be merged
1470 my $malloc_list = listForMerge($ip_dbh, $binfo->{parent_id}, $newblock, 'a');
1471 $page->param(mergealloc => $malloc_list);
1472
1473 $page->param(vis_scope => $merge_display{$webvar{scope}});
1474 $page->param(scope => $webvar{scope});
1475} # confMerge()
1476
1477
1478# Make it so
1479sub doMerge {
1480 if ($IPDBacl{$authuser} !~ /m/) {
1481 $aclerr = 'mergeblock';
1482 return;
1483 }
1484
1485 if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) {
1486 $page->param(err => 'New netmask required');
1487 return;
1488 }
1489
1490 $page->param(block => $webvar{block});
1491 my $binfo = getBlockData($ip_dbh, $webvar{block});
1492 my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
1493 my $block = new NetAddr::IP $binfo->{block};
1494
1495 # Tree navigation
1496 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1497 my @rcrumbs = reverse (@$crumbs);
1498 $utilbar->param(breadcrumb => \@rcrumbs);
1499
1500 $page->param(oldblock => $binfo->{block});
1501 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1502 $page->param(newdisptype => $disp_alloctypes{$webvar{newtype}});
1503 my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}";
1504 $newblock = $newblock->network;
1505 $page->param(newblock => $newblock);
1506 $page->param(vis_scope => $merge_display{$webvar{scope}});
1507
1508 my $mlist = mergeBlocks($ip_dbh, $webvar{block}, %webvar, user => $authuser);
1509
1510 if ($mlist) {
1511 #(newtype => $webvar{newtype}, newmask => $webvar{newmask}));
1512 # Slice off first entry (the new parent - note this may be a new allocation,
1513 # not the same ID that was "merged"!
1514 my $parent = shift @$mlist;
1515 $page->param(backpool => $webvar{newtype} =~ /.[dp]/);
1516 if ($webvar{newtype} =~ /.[enr]/) {
1517 $page->param(backleaf => 1);
1518 $page->param(backid => $binfo->{parent_id});
1519 $page->param(backblock => $pinfo->{block});
1520 } else {
1521 $page->param(backid => $parent->{id});
1522 $page->param(backblock => $parent->{block});
1523 }
1524 $page->param(mergelist => $mlist);
1525 } else {
1526 $page->param(err => "Merge failed: $IPDB::errstr");
1527 }
1528} # doMerge()
1529
1530
1531# Delete an allocation.
1532sub remove {
1533 if ($IPDBacl{$authuser} !~ /d/) {
1534 $aclerr = 'delblock';
1535 return;
1536 }
1537
1538 # Serves'em right for getting here...
1539 if (!defined($webvar{block})) {
1540 $page->param(err => "Can't delete a block that doesn't exist");
1541 return;
1542 }
1543
1544 my $blockdata;
1545 $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1546
1547 # Tree navigation
1548 my $crumbs = getBreadCrumbs($ip_dbh, $blockdata->{parent_id});
1549 my @rcrumbs = reverse (@$crumbs);
1550 $utilbar->param(breadcrumb => \@rcrumbs);
1551
1552 if ($blockdata->{parent_id} == 0) { # $webvar{alloctype} eq 'mm'
1553 $blockdata->{city} = "N/A";
1554 $blockdata->{custid} = "N/A";
1555 $blockdata->{circuitid} = "N/A";
1556 $blockdata->{description} = "N/A";
1557 $blockdata->{notes} = "N/A";
1558 $blockdata->{privdata} = "N/A";
1559 } # end cases for different alloctypes
1560
1561 $page->param(blockid => $webvar{block});
1562 $page->param(basetype => $webvar{basetype});
1563
1564 $page->param(block => $blockdata->{block});
1565 $page->param(rdns => $blockdata->{rdns});
1566
1567 # maybe need to apply more magic here?
1568 # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
1569 # -> all real blocks (nb: pool IPs need extra handling)
1570 # -> NOC/private-IP (how to ID?)
1571 # -> anything with a pattern matching $IPDB::domain?
1572 if ($blockdata->{type} !~ /^.i$/) {
1573 $page->param(autodel => 1);
1574 }
1575
1576 $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
1577 $page->param(city => $blockdata->{city});
1578 $page->param(custid => $blockdata->{custid});
1579 $page->param(circid => $blockdata->{circuitid});
1580 $page->param(desc => $blockdata->{description});
1581 $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
1582 $blockdata->{notes} =~ s/\n/<br>\n/;
1583 $page->param(notes => $blockdata->{notes});
1584 $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
1585 $blockdata->{privdata} = '&nbsp;' if !$blockdata->{privdata};
1586 $blockdata->{privdata} =~ s/\n/<br>\n/;
1587 $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
1588 $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
1589
1590} # end remove()
1591
1592
1593# Delete an allocation. Return it to the freeblocks table; munge
1594# data as necessary to keep as few records as possible in freeblocks
1595# to prevent weirdness when allocating blocks later.
1596# Remove IPs from pool listing if necessary
1597sub finalDelete {
1598 if ($IPDBacl{$authuser} !~ /d/) {
1599 $aclerr = 'delblock';
1600 return;
1601 }
1602
1603 # need to retrieve block data before deleting so we can notify on that
1604 my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1605 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b');
1606
1607 # Tree navigation
1608 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1609 my @rcrumbs = reverse (@$crumbs);
1610 $utilbar->param(breadcrumb => \@rcrumbs);
1611
1612 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser);
1613
1614 $page->param(block => $blockinfo->{block});
1615 $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}}));
1616 $page->param(delparent_id => $blockinfo->{parent_id});
1617 if ($pinfo) {
1618 $page->param(delparent => $pinfo->{block});
1619 $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}}));
1620 }
1621 $page->param(returnpool => ($webvar{basetype} eq 'i') );
1622 if ($code =~ /^WARN(POOL|MERGE)/) {
1623 my ($pid,$pcidr) = split /,/, $msg;
1624 my $real_pinfo = getBlockData($ip_dbh, $pid, 'b');
1625 $page->param(parent_id => $pid);
1626 $page->param(parent => $pcidr);
1627 $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}}));
1628 $page->param(mergeip => $code eq 'WARNPOOL');
1629 }
1630 if ($code eq 'WARN') {
1631 $msg =~ s/\n/<br>\n/g;
1632 $page->param(genwarn => $msg);
1633 }
1634 if ($code eq 'OK' || $code =~ /^WARN/) {
1635 syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock $webvar{block} ".
1636 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
1637 mailNotify($ip_dbh, 'da', "REMOVED: ".$disp_alloctypes{$blockinfo->{type}}." $webvar{block}",
1638 $disp_alloctypes{$blockinfo->{type}}." $webvar{block} deallocated by $authuser\n".
1639 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
1640 "\nDescription: ".$blockinfo->{description}."\n");
1641 } else {
1642 $page->param(failmsg => $msg);
1643 if ($webvar{alloctype} =~ /^.i$/) {
1644 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1645 } else {
1646 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
1647 $page->param(netblock => 1);
1648 }
1649 }
1650
1651} # finalDelete
Note: See TracBrowser for help on using the repository browser.