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

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

/trunk

Work in progress, see #5:
Update "add new allocation" templates and calling code for new
table logic and fields.
Also update listpool page to pass around rdepth as necessary

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