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

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

/trunk

Fix bug generating the links in the index page with integrated master blocks

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