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

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

/trunk

Refine the template loading process to allow completely reskinning the
UI by replacing templates as convenient, to match the CSS override allowed
by referencing not-provided local.css. See #17, sort of.

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