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

Last change on this file since 623 was 623, checked in by Kris Deugau, 10 years ago

/trunk

Convert main.cgi's sole direct RPC call to use IPDB::_rpc()

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 30.3 KB
RevLine 
[4]1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
[8]3###
4# SVN revision info
5# $Date: 2014-10-08 16:54:56 +0000 (Wed, 08 Oct 2014) $
6# SVN revision $Rev: 623 $
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
72$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
[233]73
[517]74my $header = HTML::Template->new(filename => "header.tmpl");
75my $footer = HTML::Template->new(filename => "footer.tmpl");
[233]76
[517]77$header->param(version => $IPDB::VERSION);
78$header->param(addperm => $IPDBacl{$authuser} =~ /a/);
79$header->param(webpath => $IPDB::webpath);
80print "Content-type: text/html\n\n", $header->output;
[4]81
82
83#main()
[517]84my $aclerr;
[4]85
86if(!defined($webvar{action})) {
[517]87 $webvar{action} = "index"; #shuts up the warnings.
[4]88}
89
[517]90my $page;
91if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
[553]92 $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1);
[517]93} else {
94 $page = HTML::Template->new(filename => "dunno.tmpl");
95}
96
[4]97if($webvar{action} eq 'index') {
98 showSummary();
[233]99} elsif ($webvar{action} eq 'addmaster') {
100 if ($IPDBacl{$authuser} !~ /a/) {
[517]101 $aclerr = 'addmaster';
[233]102 }
[582]103
104 # Retrieve the list of DNS locations if we've got a place to grab them from
105 if ($IPDB::rpc_url) {
106
107 my %rpcargs = (
108 rpcuser => $authuser,
109 group => 1, # bleh
110 defloc => '',
111 );
[623]112 my $result = IPDB::_rpc('getLocDropdown', %rpcargs);
[582]113 $page->param(loclist => $result);
114 }
115
[4]116} elsif ($webvar{action} eq 'newmaster') {
117
[233]118 if ($IPDBacl{$authuser} !~ /a/) {
[517]119 $aclerr = 'addmaster';
[233]120 } else {
121 my $cidr = new NetAddr::IP $webvar{cidr};
[517]122 $page->param(cidr => "$cidr");
[4]123
[582]124 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr}, (vrf => $webvar{vrf}, rdns => $webvar{rdns},
125 rwhois => $webvar{rwhois}, defloc => $webvar{loc}, user => $authuser) );
[4]126
[371]127 if ($code eq 'FAIL') {
[320]128 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
[517]129 $page->param(err => $msg);
[233]130 } else {
[582]131 if ($code eq 'WARN') {
132 $msg =~ s/\n\n/<br>\n/g;
133 $msg =~ s/:\n/:<br>\n/g;
134 $page->param(warn => $msg);
135 }
[233]136 syslog "info", "$authuser added master block $webvar{cidr}";
137 }
[4]138
[233]139 } # ACL check
140
[4]141} # end add new master
142
[567]143elsif ($webvar{action} eq 'showsubs') {
144 showSubs();
145}
146
[4]147elsif($webvar{action} eq 'listpool') {
[528]148 showPool();
[4]149}
150
151# Not modified or added; just shuffled
152elsif($webvar{action} eq 'assign') {
153 assignBlock();
154}
155elsif($webvar{action} eq 'confirm') {
156 confirmAssign();
157}
158elsif($webvar{action} eq 'insert') {
159 insertAssign();
160}
161elsif($webvar{action} eq 'edit') {
162 edit();
163}
164elsif($webvar{action} eq 'update') {
165 update();
166}
167elsif($webvar{action} eq 'delete') {
168 remove();
169}
170elsif($webvar{action} eq 'finaldelete') {
171 finalDelete();
172}
[397]173elsif ($webvar{action} eq 'nodesearch') {
[519]174 my $nodelist = getNodeList($ip_dbh);
175 $page->param(nodelist => $nodelist);
[517]176}
[397]177
[517]178# DB failure. Can't do much here, really.
179elsif ($webvar{action} eq 'dberr') {
180 $page->param(errmsg => $errstr);
[397]181}
182
[517]183# Default is an error. It shouldn't be possible to get here unless you're
184# randomly feeding in values for webvar{action}.
[4]185else {
186 my $rnd = rand 500;
187 my $boing = sprintf("%.2f", rand 500);
[517]188 my @excuses = (
189 "Aether cloudy. Ask again later about $webvar{action}.",
190 "The gods are unhappy with your sacrificial $webvar{action}.",
191 "Because one of $webvar{action}'s legs are both the same",
192 "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!",
193 "Hey, man, you've had your free $webvar{action}. Next one's gonna... <i>cost</i>....",
194 "I ain't done $webvar{action}",
195 "Oooo, look! A flying $webvar{action}!",
196 "$webvar{action} too evil, avoiding.",
197 "Rocks fall, $webvar{action} dies.",
198 "Bit bucket must be emptied before I can $webvar{action}..."
199 );
200 $page->param(dunno => $excuses[$rnd/50.0]);
[4]201}
[111]202## Finally! Done with that NASTY "case" emulation!
[4]203
204
[517]205# Switch to a different template if we've tripped on an ACL error.
206# Note that this should only be exercised in development, when
207# deeplinked, or when being attacked; normal ACL handling should
208# remove the links a user is not allowed to click on.
209if ($aclerr) {
210 $page = HTML::Template->new(filename => "aclerror.tmpl");
211 $page->param(ipdbfunc => $aclmsg{$aclerr});
212}
[4]213
[106]214# Clean up IPDB globals, DB handle, etc.
[111]215finish($ip_dbh);
[199]216
[517]217## Do all our printing here so we can generate errors and stick them into the slots in the templates.
[199]218
[517]219# can't do this yet, too many blowups
220#print "Content-type: text/html\n\n", $header->output;
221$page->param(webpath => $IPDB::webpath);
222print $page->output;
223
224# include the admin tools link in the output?
225$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
226$footer->param(webpath => $IPDB::webpath);
227print $footer->output;
228
[106]229# Just in case something waaaayyy down isn't in place
230# properly... we exit explicitly.
[517]231exit 0;
[4]232
233
234# Initial display: Show master blocks with total allocated subnets, total free subnets
[118]235sub showSummary {
[523]236 my $masterlist = listSummary($ip_dbh);
237 $page->param(masterlist => $masterlist);
[106]238
[517]239 $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
[4]240} # showSummary
241
242
[566]243# Display blocks immediately within a given parent
244sub showSubs {
245 $page->param(block => $webvar{block});
246 $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
247 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/));
[4]248
[566]249 my $sublist = listSubs($ip_dbh, block => $webvar{block}, rdepth => $webvar{rdepth});
250 $page->param(deldepth => $webvar{rdepth} - 1);
251 $page->param(rdepth => $webvar{rdepth});
252 $page->param(subdepth => $webvar{rdepth} + 1);
253 $page->param(sublist => $sublist);
[4]254
[566]255 my $flist = listFree($ip_dbh, master => $webvar{block}, rdepth => $webvar{rdepth});
256 $page->param(freelist => $flist);
257} # showSubs
[4]258
259
260# List the IPs used in a pool
[528]261sub showPool {
[4]262
263 my $cidr = new NetAddr::IP $webvar{pool};
264
[517]265 $page->param(block => $webvar{pool});
266 $page->param(netip => $cidr->addr);
267 $cidr++;
268 $page->param(gate => $cidr->addr);
269 $cidr--; $cidr--;
270 $page->param(bcast => $cidr->addr);
271 $page->param(mask => $cidr->mask);
[157]272
[4]273 # Snag pool info for heading
[570]274 my $poolinfo = getBlockData($ip_dbh, $webvar{pool}, $webvar{rdepth});
[4]275
[528]276 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
277 $page->param(city => $poolinfo->{city});
[517]278
[157]279 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
[528]280 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
[4]281
282# probably have to add an "edit IP allocation" link here somewhere.
283
[528]284 my $plist = listPool($ip_dbh, $webvar{pool});
285 # technically slightly more efficient to check the ACL in an if () once outside the foreach
286 foreach (@{$plist}) {
287 $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
[4]288 }
[528]289 $page->param(poolips => $plist);
290} # end showPool
[4]291
292
[106]293# Show "Add new allocation" page. Note that the actual page may
294# be one of two templates, and the lists come from the database.
[4]295sub assignBlock {
296
[233]297 if ($IPDBacl{$authuser} !~ /a/) {
[517]298 $aclerr = 'addblock';
[233]299 return;
300 }
301
[517]302 # hack pthbttt eww
303 $webvar{block} = '' if !$webvar{block};
[21]304
[517]305# hmm. TMPL_IF block and TMPL_ELSE block on these instead?
306 $page->param(rowa => 'row'.($webvar{block} eq '' ? 1 : 0));
307 $page->param(rowb => 'row'.($webvar{block} eq '' ? 0 : 1));
[575]308 $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
[517]309
[21]310 if ($webvar{block} ne '') {
[575]311
312 # Common case, according to reported usage. Block to assign is specified.
[21]313 my $block = new NetAddr::IP $webvar{block};
[575]314 $page->param(rdepth => $webvar{rdepth});
[187]315
[585]316 my $rdns = getBlockRDNS($ip_dbh, $webvar{block}, $webvar{rdepth}, vrf => $webvar{vrf}, user => $authuser);
317 $page->param(rdns => $rdns) if $rdns;
318
[575]319 $webvar{fbtype} = '' if !$webvar{fbtype};
320 if ($webvar{fbtype} eq 'i') {
321 my $ipinfo = getBlockData($ip_dbh, $block);
322 $page->param(
323 fbip => 1,
324 block => $block,
325 fbdisptype => $list_alloctypes{$ipinfo->{type}},
326 type => $ipinfo->{type},
327 allocfrom => $ipinfo->{pool},
328 );
[187]329 } else {
[529]330 # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
[575]331 my $tlist = getTypeList($ip_dbh, 'n');
[529]332 $tlist->[0]->{sel} = 1;
[575]333 $page->param(typelist => $tlist, block => $block);
[106]334 }
[575]335
[21]336 } else {
[575]337
338 # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
[541]339 my $mlist = getMasterList($ip_dbh, 'c');
340 $page->param(masterlist => $mlist);
[517]341
342 my @pops;
[92]343 foreach my $pop (@poplist) {
[517]344 my %row = (pop => $pop);
345 push (@pops, \%row);
[92]346 }
[517]347 $page->param(pops => \@pops);
348
[529]349 # get all standard alloctypes
350 my $tlist = getTypeList($ip_dbh, 'a');
351 $tlist->[0]->{sel} = 1;
352 $page->param(typelist => $tlist);
[21]353 }
[517]354
355 my @cities;
[92]356 foreach my $city (@citylist) {
[517]357 my %row = (city => $city);
358 push (@cities, \%row);
[92]359 }
[517]360 $page->param(citylist => \@cities);
[4]361
[397]362## node hack
[530]363 my $nlist = getNodeList($ip_dbh);
364 $page->param(nodelist => $nlist);
[397]365## end node hack
366
[517]367 $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
[284]368
[4]369} # assignBlock
370
371
372# Take info on requested IP assignment and see what we can provide.
373sub confirmAssign {
[233]374 if ($IPDBacl{$authuser} !~ /a/) {
[517]375 $aclerr = 'addblock';
[233]376 return;
377 }
[4]378
379 my $cidr;
380 my $alloc_from;
381
382 # Going to manually validate some items.
383 # custid and city are automagic.
[111]384 return if !validateInput();
[4]385
386# Several different cases here.
387# Static IP vs netblock
388# + Different flavours of static IP
389# + Different flavours of netblock
390
[575]391 if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
[532]392 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
[517]393 $page->param(staticip => 1);
[532]394 $page->param(poollist => $plist) if $plist;
[4]395 $cidr = "Single static IP";
[517]396##fixme: need to handle "no available pools"
[4]397
398 } else { # end show pool options
[21]399
[533]400 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
[21]401 $cidr = new NetAddr::IP $webvar{block};
[575]402 $alloc_from = new NetAddr::IP $webvar{allocfrom};
[21]403 $webvar{maskbits} = $cidr->masklen;
404 } else { # done with direct freeblocks assignment
405
406 if (!$webvar{maskbits}) {
[517]407 $page->param(err => "Please specify a CIDR mask length.");
[111]408 return;
[21]409 }
[533]410
411##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
412 my $failmsg = "No suitable free block found.<br>\n";
[187]413 if ($webvar{alloctype} eq 'rm') {
[533]414 $failmsg .= "We do not have a free routeable block of that size.<br>\n".
415 "You will have to either route a set of smaller netblocks or a single smaller netblock.";
[4]416 } else {
[214]417 if ($webvar{alloctype} =~ /^.[pc]$/) {
[533]418 $failmsg .= "You will have to route another superblock from one of the<br>\n".
419 "master blocks or chose a smaller block size for the pool.";
[21]420 } else {
[517]421 if (!$webvar{pop}) {
422 $page->param(err => 'Please select a POP to route the block from/through.');
423 return;
424 }
[533]425 $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
426 "from one of the master blocks or chose a smaller blocksize.";
[21]427 }
[4]428 }
[533]429
[575]430## fixme: add rdepth?
431 ($cidr,$webvar{rdepth}) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype}, $webvar{city},
432 $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
[533]433 if (!$cidr) {
[517]434 $page->param(err => $failmsg);
[111]435 return;
[21]436 }
[533]437 $cidr = new NetAddr::IP $cidr;
[575]438
439# this chunk now specific to "guided" allocation; freeblock-select can now slice-n-dice on its own.
440 $alloc_from = "$cidr";
441 # If the block to be allocated is smaller than the one we found,
442 # figure out the "real" block to be allocated.
443 if ($cidr->masklen() ne $webvar{maskbits}) {
444 my $maskbits = $cidr->masklen();
445 my @subblocks;
446 while ($maskbits++ < $webvar{maskbits}) {
447 @subblocks = $cidr->split($maskbits);
448 }
449 $cidr = $subblocks[0];
450 }
[21]451 } # check for freeblocks assignment or IPDB-controlled assignment
[4]452
[157]453 } # if ($webvar{alloctype} =~ /^.i$/)
[4]454
[397]455## node hack
456 if ($webvar{node} && $webvar{node} ne '-') {
[530]457 my $nodename = getNodeName($ip_dbh, $webvar{node});
[517]458 $page->param(nodename => $nodename);
459 $page->param(nodeid => $webvar{node});
[397]460 }
461## end node hack
462
[4]463 # Stick in the allocation data
[517]464 $page->param(alloc_type => $webvar{alloctype});
465 $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
466 $page->param(alloc_from => $alloc_from);
[575]467 $page->param(rdepth => $webvar{rdepth});
[517]468 $page->param(cidr => $cidr);
[585]469 $page->param(rdns => $webvar{rdns});
[517]470 $page->param(city => $q->escapeHTML($webvar{city}));
471 $page->param(custid => $webvar{custid});
472 $page->param(circid => $q->escapeHTML($webvar{circid}));
473 $page->param(desc => $q->escapeHTML($webvar{desc}));
[4]474
[517]475##fixme: find a way to have the displayed copy have <br> substitutions
476# for newlines, and the <input> value have either encoded or bare newlines.
477# Also applies to privdata.
478 $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
479
[284]480 # Check to see if user is allowed to do anything with sensitive data
481 my $privdata = '';
[517]482 $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'))
483 if $IPDBacl{$authuser} =~ /s/;
484
485 # Yay! This now has it's very own little home.
486 $page->param(billinguser => $webvar{userid})
[299]487 if $webvar{userid};
[284]488
[517]489##fixme: this is only needed iff confirm.tmpl and
490# confirmRemove.tmpl are merged (quite possible, just
491# a little tedious)
492 $page->param(action => "insert");
[284]493
[4]494} # end confirmAssign
495
496
497# Do the work of actually inserting a block in the database.
498sub insertAssign {
[233]499 if ($IPDBacl{$authuser} !~ /a/) {
[517]500 $aclerr = 'addblock';
[233]501 return;
502 }
[4]503 # Some things are done more than once.
[111]504 return if !validateInput();
[4]505
[284]506 if (!defined($webvar{privdata})) {
507 $webvar{privdata} = '';
508 }
[575]509
510 # split up some linked data for static IPs via guided allocation. needed for breadcrumbs lite.
[584]511 ($webvar{alloc_from},$webvar{rdepth}) = split /,/, $webvar{alloc_from} if $webvar{alloc_from} =~ /,/;
[575]512
[106]513 # $code is "success" vs "failure", $msg contains OK for a
514 # successful netblock allocation, the IP allocated for static
515 # IP, or the error message if an error occurred.
[517]516
[575]517 my ($code,$msg) = allocateBlock($ip_dbh, cidr => $webvar{fullcidr}, alloc_from => $webvar{alloc_from},
518 rdepth => $webvar{rdepth}, custid => $webvar{custid}, type => $webvar{alloctype}, city => $webvar{city},
519 desc => $webvar{desc}, notes => $webvar{notes}, circid => $webvar{circid},
[585]520 privdata => $webvar{privdata}, nodeid => $webvar{node}, rdns => $webvar{rdns}, user => $authuser);
[4]521
[111]522 if ($code eq 'OK') {
[106]523 if ($webvar{alloctype} =~ /^.i$/) {
[300]524 $msg =~ s|/32||;
[517]525 $page->param(staticip => $msg);
526 $page->param(custid => $webvar{custid});
[575]527 $page->param(parent => $webvar{alloc_from}, rdepth => $webvar{rdepth}-1);
[517]528 $page->param(billinguser => $webvar{billinguser});
[416]529 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
530 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
531 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[106]532 } else {
[301]533 my $netblock = new NetAddr::IP $webvar{fullcidr};
[517]534 $page->param(fullcidr => $webvar{fullcidr});
535 $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
536 $page->param(custid => $webvar{custid});
[575]537 # breadcrumbs lite! provide at least a link to the parent of the block we just allocated.
538 my $binfo = getBlockData($ip_dbh, $webvar{fullcidr}, $webvar{rdepth});
539 $page->param(parent => $binfo->{parent}, rdepth => $binfo->{rdepth});
[517]540 if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
541 $page->param(billinguser => $webvar{billinguser});
542 $page->param(custid => $webvar{custid});
543 $page->param(netaddr => $netblock->addr);
544 $page->param(masklen => $netblock->masklen);
545 }
[416]546 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
547 "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
548 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[4]549 }
[106]550 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
[256]551 "'$webvar{alloctype}' ($msg)";
[111]552 } else {
553 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
554 "'$webvar{alloctype}' by $authuser failed: '$msg'";
[517]555 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
[157]556 " failed:<br>\n$msg\n");
[106]557 }
[4]558
559} # end insertAssign()
560
561
562# Does some basic checks on common input data to make sure nothing
563# *really* weird gets in to the database through this script.
564# Does NOT do complete input validation!!!
565sub validateInput {
566 if ($webvar{city} eq '-') {
[517]567 $page->param(err => 'Please choose a city');
[111]568 return;
[4]569 }
[138]570
571 # Alloctype check.
[4]572 chomp $webvar{alloctype};
[138]573 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
574 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
575 # managing to call things in such a way as to cause this deserves a cryptic error.
[517]576 $page->param(err => 'Invalid alloctype');
[138]577 return;
578 }
579
580 # CustID check
[4]581 # We have different handling for customer allocations and "internal" or "our" allocations
[214]582 if ($def_custids{$webvar{alloctype}} eq '') {
[4]583 if (!$webvar{custid}) {
[517]584 $page->param(err => 'Please enter a customer ID.');
[111]585 return;
[4]586 }
[546]587 # Crosscheck with billing.
588 my $status = CustIDCK->custid_exist($webvar{custid});
589 if ($CustIDCK::Error) {
590 $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
591 return;
[4]592 }
[546]593 if (!$status) {
594 $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
595 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
596 "non-customer assignments.");
597 return;
598 }
[400]599# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
[138]600 } else {
[167]601 # New! Improved! And now Loaded From The Database!!
[320]602 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
603 $webvar{custid} = $def_custids{$webvar{alloctype}};
604 }
[4]605 }
[111]606
[571]607## hmmm.... is this even useful?
608if (0) {
[111]609 # Check POP location
610 my $flag;
[187]611 if ($webvar{alloctype} eq 'rm') {
[111]612 $flag = 'for a routed netblock';
613 foreach (@poplist) {
614 if (/^$webvar{city}$/) {
615 $flag = 'n';
616 last;
617 }
618 }
619 } else {
620 $flag = 'n';
[442]621##fixme: hook to force-set POP or city on certain alloctypes
622# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
[536]623 if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
[111]624 $flag = 'to route the block from/through';
625 }
626 }
[517]627
628 # if the alloctype has a restricted city/POP list as determined above,
629 # and the reqested city/POP does not match that list, complain
[111]630 if ($flag ne 'n') {
[517]631 $page->param(err => "Please choose a valid POP location $flag. Valid ".
[111]632 "POP locations are currently:<br>\n".join (" - ", @poplist));
633 return;
634 }
[571]635}
[111]636
637 return 'OK';
[4]638} # end validateInput
639
640
641# Displays details of a specific allocation in a form
642# Allows update/delete
643# action=edit
644sub edit {
645
[534]646 # snag block info from db
[576]647 my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{rdepth});
[4]648
[534]649 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
650 $blockinfo->{type} =~ s/\s//;
[4]651
[586]652 # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
653 $blockinfo->{rdns} = getBlockRDNS($ip_dbh, $webvar{block}, $webvar{rdepth}, user => $authuser);
654
[517]655 $page->param(block => $webvar{block});
[586]656 $page->param(rdns => $blockinfo->{rdns});
[576]657 $page->param(rdepth => $blockinfo->{rdepth});
[4]658
[534]659 $page->param(custid => $blockinfo->{custid});
660 $page->param(city => $blockinfo->{city});
661 $page->param(circid => $blockinfo->{circuitid});
662 $page->param(desc => $blockinfo->{description});
663 $page->param(notes => $blockinfo->{notes});
[4]664
[187]665##fixme The check here should be built from the database
[517]666# Need to expand to support pool types too
[534]667 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
[517]668 $page->param(changetype => 1);
669 $page->param(alloctype => [
[534]670 { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
671 { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
672 { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
673 { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
674 { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
675 { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
676 { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
[517]677 ]
678 );
679 } else {
[534]680 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
681 $page->param(type => $blockinfo->{type});
[517]682 }
683
[397]684## node hack
[530]685 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $webvar{block});
[517]686 $page->param(havenodeid => $nodeid);
687
[534]688 if ($blockinfo->{type} eq 'fr' || $blockinfo->{type} eq 'bi') {
[517]689 $page->param(typesupportsnodes => 1);
690 $page->param(nodename => $nodename);
691
692##fixme: this whole hack needs cleanup and generalization for all alloctypes
693##fixme: arguably a bug that presence of a nodeid implies it can be changed..
694# but except for manual database changes, only the two types fr and bi can
695# (currently) have a nodeid set in the first place.
696 if ($IPDBacl{$authuser} =~ /c/) {
[530]697 my $nlist = getNodeList($ip_dbh);
698 foreach (@{$nlist}) {
699 $$_{selme} = ($$_{node_id} == $nodeid);
[397]700 }
[530]701 $page->param(nodelist => $nlist);
[397]702 }
703 }
704## end node hack
[517]705
[534]706 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
[517]707 $page->param(lastmod => $lastmod);
[33]708
[517]709 # not happy with the upside-down logic, but...
[534]710 $page->param(swipable => $blockinfo->{type} !~ /.i/);
711 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
[320]712
[284]713 # Check to see if we can display sensitive data
[517]714 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
[534]715 $page->param(privdata => $blockinfo->{privdata});
[284]716
[517]717 # ACL trickery - these two template booleans control the presence of all form/input tags
718 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
719 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
[4]720
721} # edit()
722
723
724# Stuff new info about a block into the db
725# action=update
726sub update {
[284]727 if ($IPDBacl{$authuser} !~ /c/) {
[517]728 $aclerr = 'updateblock';
[284]729 return;
730 }
[4]731
732 # Make sure incoming data is in correct format - custID among other things.
[228]733 return if !validateInput;
[4]734
[536]735 $webvar{swip} = 'n' if !$webvar{swip};
736
[531]737 my %updargs = (
738 custid => $webvar{custid},
739 city => $webvar{city},
740 description => $webvar{desc},
741 notes => $webvar{notes},
742 circuitid => $webvar{circid},
743 block => $webvar{block},
744 type => $webvar{alloctype},
[536]745 swip => $webvar{swip},
[576]746 rdepth => $webvar{rdepth},
[588]747 rdns => $webvar{rdns},
748 user => $authuser,
[531]749 );
750
751 # Semioptional values
752 $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
753 $updargs{node} = $webvar{node} if $webvar{node};
754
755 my ($code,$msg) = updateBlock($ip_dbh, %updargs);
756
757 if ($code eq 'FAIL') {
[166]758 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
[517]759 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
[111]760 return;
[4]761 }
762
763 # If we get here, the operation succeeded.
764 syslog "notice", "$authuser updated $webvar{block}";
[531]765##fixme: log details of the change? old way is in the .debug stream anyway.
[416]766##fixme: need to wedge something in to allow "update:field" notifications
767## hmm. how to tell what changed? O_o
[426]768mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
[416]769 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
[4]770
[517]771## node hack
772 if ($webvar{node} && $webvar{node} ne '-') {
[530]773 my $nodename = getNodeName($ip_dbh, $webvar{node});
[517]774 $page->param(nodename => $nodename);
775 }
776## end node hack
777
[380]778 # Link back to browse-routed or list-pool page on "Update complete" page.
[576]779 my $cblock = getBlockData($ip_dbh, $webvar{block}, $webvar{rdepth});
[380]780 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
[517]781 $page->param(backpool => 1);
[576]782 $page->param(backblock => $cblock->{pool});
[380]783 } else {
[576]784 $page->param(backblock => $cblock->{parent});
[380]785 }
[576]786 $page->param(backdepth => ($webvar{rdepth}));
[380]787
[536]788 # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
789 # because otherwise we can't convert \n to <br>. *sigh*
790 $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
791 $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
792 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
793 $webvar{privdata} =~ s/\n/<br>\n/;
794
[517]795 $page->param(cidr => $webvar{block});
[588]796 $page->param(rdns => $webvar{rdns});
[517]797 $page->param(city => $webvar{city});
798 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
799 $page->param(custid => $webvar{custid});
800 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
[536]801 $page->param(circid => $webvar{circid});
802 $page->param(desc => $webvar{desc});
803 $page->param(notes => $webvar{notes});
[517]804 $page->param(privdata => $webvar{privdata})
805 if $IPDBacl{$authuser} =~ /s/;
[4]806
807} # update()
808
809
810# Delete an allocation.
[106]811sub remove {
[233]812 if ($IPDBacl{$authuser} !~ /d/) {
[517]813 $aclerr = 'delblock';
[233]814 return;
815 }
816
[4]817 # Serves'em right for getting here...
818 if (!defined($webvar{block})) {
[517]819 $page->param(err => "Can't delete a block that doesn't exist");
[111]820 return;
[4]821 }
822
[538]823 my $blockdata;
[4]824
[577]825 if ($webvar{rdepth} == 0) { # $webvar{alloctype} eq 'mm'
[4]826
[538]827 $blockdata->{block} = $webvar{block};
828 $blockdata->{city} = "N/A";
829 $blockdata->{custid} = "N/A";
[577]830 $blockdata->{type} = 'mm';
[538]831 $blockdata->{circuitid} = "N/A";
832 $blockdata->{description} = "N/A";
833 $blockdata->{notes} = "N/A";
834 $blockdata->{privdata} = "N/A";
[577]835 $blockdata->{rdepth} = 0;
[517]836
[538]837 } else {
[4]838
[577]839 $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{rdepth});
[4]840
841 } # end cases for different alloctypes
842
[538]843 $page->param(block => $blockdata->{block});
[589]844
845 $page->param(rdns => $blockdata->{rdns});
846
847 # maybe need to apply more magic here?
848 # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
849 # -> all real blocks (nb: pool IPs need extra handling)
850 # -> NOC/private-IP (how to ID?)
851 # -> anything with a pattern matching $IPDB::domain?
852 if ($blockdata->{type} !~ /^.i$/) {
853 $page->param(autodel => 1);
854 }
855
[577]856 $page->param(rdepth => $blockdata->{rdepth});
[538]857 $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
[577]858# $page->param(type => $blockdata->{type});
[538]859 $page->param(city => $blockdata->{city});
860 $page->param(custid => $blockdata->{custid});
861 $page->param(circid => $blockdata->{circuitid});
862 $page->param(desc => $blockdata->{description});
863 $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
864 $blockdata->{notes} =~ s/\n/<br>\n/;
865 $page->param(notes => $blockdata->{notes});
866 $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
[540]867 $blockdata->{privdata} = '&nbsp;' if !$blockdata->{privdata};
[538]868 $blockdata->{privdata} =~ s/\n/<br>\n/;
869 $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
870 $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
[4]871
[517]872} # end remove()
[4]873
874
875# Delete an allocation. Return it to the freeblocks table; munge
876# data as necessary to keep as few records as possible in freeblocks
877# to prevent weirdness when allocating blocks later.
878# Remove IPs from pool listing if necessary
879sub finalDelete {
[233]880 if ($IPDBacl{$authuser} !~ /d/) {
[517]881 $aclerr = 'delblock';
[233]882 return;
883 }
[4]884
[370]885 # need to retrieve block data before deleting so we can notify on that
[577]886 my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{rdepth});
[370]887
[590]888 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{rdepth}, $webvar{vrf}, $webvar{delforward}, $authuser);
[4]889
[517]890 $page->param(block => $webvar{block});
[577]891 $page->param(delparent => $blockinfo->{parent}) if $webvar{rdepth};
892 $page->param(prdepth => $webvar{rdepth});
[591]893 if ($code =~ /^WARN(POOL|MERGE)/) {
[577]894 my ($bp,$bd) = split /,/, $msg;
895 $page->param(bparent => $bp);
896 $page->param(brdepth => $bd);
897 $page->param(mergeip => $code eq 'WARNPOOL');
898 }
[591]899 if ($code eq 'WARN') {
900 $msg =~ s/\n/<br>\n/g;
901 $page->param(genwarn => $msg);
902 }
[577]903 if ($code eq 'OK' || $code =~ /^WARN/) {
[528]904 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block} ".
905 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
[416]906 mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
907 "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
[528]908 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
909 "\nDescription: ".$blockinfo->{description}."\n");
[106]910 } else {
[517]911 $page->param(failmsg => $msg);
[106]912 if ($webvar{alloctype} =~ /^.i$/) {
913 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
914 } else {
915 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
[517]916 $page->param(netblock => 1);
[4]917 }
[106]918 }
[4]919
920} # finalDelete
Note: See TracBrowser for help on using the repository browser.