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

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

/trunk

Work in progress, see #5:
Replace $webvar{action} branches for showmaster and showrouted
with showsubs, calling listSubs()

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