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

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

/trunk

Extend UI to browse into a VRF. See #54.
Also extend breadcrumb segment to show the VRF.

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