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

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

/trunk

Pass VRF in to add master page, and include it in the database checks. See #54.
Add missing breadcrumb links on add master entry and confirmation pages.

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