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

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

/trunk

Promote VRFs to top-level entities. See #54.

1 of mumble; convert index page to list VRFs instead of master blocks.

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