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

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

/trunk

Add "Add VRF" page. See #54.

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