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

Last change on this file since 560 was 553, checked in by Kris Deugau, 12 years ago

/trunk

Fix some webpath glitches

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