source: branches/stable/cgi-bin/main.cgi@ 596

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

/branches/stable

Hand-backport static IP assign-by-browse. As with assign-routed-by-browse
in r595, this could not be merged from /trunk directly due to intermingled
changes for the new database structure and will cause merge conflicts
later.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 28.9 KB
RevLine 
[4]1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
[8]3###
4# SVN revision info
5# $Date: 2013-05-15 22:03:32 +0000 (Wed, 15 May 2013) $
6# SVN revision $Rev: 596 $
7# Last update by $Author: kdeugau $
8###
[507]9# Copyright (C) 2004-2011 - Kris Deugau
[4]10
11use strict;
12use warnings;
13use CGI::Carp qw(fatalsToBrowser);
[593]14use CGI::Simple;
15use HTML::Template;
[4]16use DBI;
17use POSIX qw(ceil);
18use NetAddr::IP;
19
20use Sys::Syslog;
21
[445]22# don't remove! required for GNU/FHS-ish install from tarball
23##uselib##
[4]24
[592]25use CustIDCK;
[445]26use MyIPDB;
27
28openlog "IPDB","pid","$IPDB::syslog_facility";
29
[593]30## Environment. Collect some things, process some things, set some things...
31
[242]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
[593]41# anyone got a better name? :P
42my $thingroot = $ENV{SCRIPT_FILENAME};
43$thingroot =~ s|cgi-bin/main.cgi||;
44
[383]45syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
[4]46
[593]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
[125]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;
[158]63($ip_dbh,$errstr) = connectDB_My;
[125]64if (!$ip_dbh) {
[593]65 $webvar{action} = "dberr";
66} else {
67 initIPDBGlobals($ip_dbh);
[125]68}
[4]69
[593]70# Set up some globals
71$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
[242]72
[593]73my $header = HTML::Template->new(filename => "header.tmpl");
74my $footer = HTML::Template->new(filename => "footer.tmpl");
[242]75
[593]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()
[593]83my $aclerr;
[4]84
85if(!defined($webvar{action})) {
[593]86 $webvar{action} = "index"; #shuts up the warnings.
[4]87}
88
[593]89my $page;
90if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
[594]91 $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1);
[593]92} else {
93 $page = HTML::Template->new(filename => "dunno.tmpl");
94}
95
[4]96if($webvar{action} eq 'index') {
97 showSummary();
[242]98} elsif ($webvar{action} eq 'addmaster') {
99 if ($IPDBacl{$authuser} !~ /a/) {
[593]100 $aclerr = 'addmaster';
[242]101 }
[4]102} elsif ($webvar{action} eq 'newmaster') {
103
[242]104 if ($IPDBacl{$authuser} !~ /a/) {
[593]105 $aclerr = 'addmaster';
[242]106 } else {
107 my $cidr = new NetAddr::IP $webvar{cidr};
[593]108 $page->param(cidr => "$cidr");
[4]109
[366]110 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
[4]111
[366]112 if ($code eq 'FAIL') {
[265]113 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
[593]114 $page->param(err => $msg);
[242]115 } else {
116 syslog "info", "$authuser added master block $webvar{cidr}";
117 }
[4]118
[242]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') {
[594]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}
[395]155elsif ($webvar{action} eq 'nodesearch') {
[594]156 my $nodelist = getNodeList($ip_dbh);
157 $page->param(nodelist => $nodelist);
[593]158}
[395]159
[593]160# DB failure. Can't do much here, really.
161elsif ($webvar{action} eq 'dberr') {
162 $page->param(errmsg => $errstr);
[395]163}
164
[593]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);
[593]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}
[125]184## Finally! Done with that NASTY "case" emulation!
[4]185
186
[593]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
[125]196# Clean up IPDB globals, DB handle, etc.
197finish($ip_dbh);
[200]198
[593]199## Do all our printing here so we can generate errors and stick them into the slots in the templates.
[200]200
[593]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
[125]211# Just in case something waaaayyy down isn't in place
212# properly... we exit explicitly.
[593]213exit 0;
[4]214
215
216# Initial display: Show master blocks with total allocated subnets, total free subnets
[125]217sub showSummary {
[594]218 my $masterlist = listSummary($ip_dbh);
219 $page->param(masterlist => $masterlist);
[125]220
[593]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
[593]232 $page->param(master => $webvar{block});
233 $page->param(delmaster => ($IPDBacl{$authuser} =~ /d/));
[4]234
[594]235 my $rlist = listMaster($ip_dbh, $webvar{block});
236 $page->param(routedlist => $rlist);
[4]237
[594]238 my $flist = listFree($ip_dbh, $webvar{block});
239 $page->param(unrouted => $flist);
[595]240 $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
[4]241} # showMaster
242
243
244# Display details of a routed block
245# Alrighty then! We're showing allocations within a routed block this time.
246# We should be able to steal code from showSummary() and showMaster(), and if
247# I'm really smart I'll figger a way to munge all three together. (Once I've
248# done that, everything else should follow. YMMV.
249# This time, we check the database before spewing, because we may
250# not have anything useful to spew.
251sub showRBlock {
252
[594]253 $page->param(master => $webvar{block});
254 $page->param(delrouted => $IPDBacl{$authuser} =~ /d/);
[4]255
[594]256 my $rcity = getRoutedCity($ip_dbh, $webvar{block});
[593]257 $page->param(rcity => $rcity);
[4]258
[594]259 my $blist = listRBlock($ip_dbh, $webvar{block});
260 $page->param(blocklist => $blist);
[4]261
[594]262 my $flist = listFree($ip_dbh, $webvar{block}, 'y');
263 $page->param(unassigned => $flist);
[4]264} # showRBlock
265
266
267# List the IPs used in a pool
[594]268sub showPool {
[4]269
270 my $cidr = new NetAddr::IP $webvar{pool};
271
[593]272 $page->param(block => $webvar{pool});
273 $page->param(netip => $cidr->addr);
274 $cidr++;
275 $page->param(gate => $cidr->addr);
276 $cidr--; $cidr--;
277 $page->param(bcast => $cidr->addr);
278 $page->param(mask => $cidr->mask);
[159]279
[4]280 # Snag pool info for heading
[594]281 my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
[4]282
[594]283 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
284 $page->param(city => $poolinfo->{city});
[593]285
[159]286 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
[594]287 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
[4]288
289# probably have to add an "edit IP allocation" link here somewhere.
290
[594]291 my $plist = listPool($ip_dbh, $webvar{pool});
292 # technically slightly more efficient to check the ACL in an if () once outside the foreach
293 foreach (@{$plist}) {
294 $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
[4]295 }
[594]296 $page->param(poolips => $plist);
297} # end showPool
[4]298
299
[125]300# Show "Add new allocation" page. Note that the actual page may
301# be one of two templates, and the lists come from the database.
[4]302sub assignBlock {
303
[242]304 if ($IPDBacl{$authuser} !~ /a/) {
[593]305 $aclerr = 'addblock';
[242]306 return;
307 }
308
[593]309 # hack pthbttt eww
310 $webvar{block} = '' if !$webvar{block};
[24]311
[593]312# hmm. TMPL_IF block and TMPL_ELSE block on these instead?
313 $page->param(rowa => 'row'.($webvar{block} eq '' ? 1 : 0));
314 $page->param(rowb => 'row'.($webvar{block} eq '' ? 0 : 1));
[595]315 $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
[593]316
[595]317 if ($webvar{fbtype}) {
318
319 # Common case, according to reported usage. Block to assign is specified.
[24]320 my $block = new NetAddr::IP $webvar{block};
[550]321
[595]322 $webvar{fbtype} = '' if !$webvar{fbtype};
323 if ($webvar{fbtype} eq 'i') {
324 my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i');
[596]325##fixme: a reversion! kill it!
326my ($iptype,$pool) = $ip_dbh->selectrow_array("SELECT type,pool FROM poolips WHERE ip = ?", undef, ($block));
[595]327 $page->param(
328 fbip => 1,
329 block => $ipinfo->{block},
330 fbdisptype => $list_alloctypes{$ipinfo->{type}},
331 type => $ipinfo->{type},
[596]332 allocfrom => $pool,
[595]333 );
334 } elsif ($webvar{fbtype} eq 'n') {
335 $page->param(
336 fbrouted => 1,
337 fbdisptype => $list_alloctypes{'rm'},
338 type => 'rm',
339 block => $block,
340 );
[593]341 } else {
[594]342 # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
[595]343 my $tlist = getTypeList($ip_dbh, 'n');
[594]344 $tlist->[0]->{sel} = 1;
[595]345 $page->param(typelist => $tlist, block => $block);
[98]346 }
[595]347
[24]348 } else {
[595]349
350 # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
[594]351 my $mlist = getMasterList($ip_dbh, 'c');
352 $page->param(masterlist => $mlist);
[593]353
354 my @pops;
[595]355 foreach my $pop (@citylist) {
[593]356 my %row = (pop => $pop);
357 push (@pops, \%row);
[91]358 }
[593]359 $page->param(pops => \@pops);
360
[594]361 # get all standard alloctypes
362 my $tlist = getTypeList($ip_dbh, 'a');
363 $tlist->[0]->{sel} = 1;
364 $page->param(typelist => $tlist);
[24]365 }
[593]366 my @cities;
[91]367 foreach my $city (@citylist) {
[593]368 my %row = (city => $city);
369 push (@cities, \%row);
[91]370 }
[593]371 $page->param(citylist => \@cities);
[4]372
[394]373## node hack
[594]374 my $nlist = getNodeList($ip_dbh);
375 $page->param(nodelist => $nlist);
[394]376## end node hack
377
[593]378 $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
[286]379
[4]380} # assignBlock
381
382
383# Take info on requested IP assignment and see what we can provide.
384sub confirmAssign {
[242]385 if ($IPDBacl{$authuser} !~ /a/) {
[593]386 $aclerr = 'addblock';
[242]387 return;
388 }
[4]389
390 my $cidr;
391 my $alloc_from;
392
393 # Going to manually validate some items.
394 # custid and city are automagic.
[125]395 return if !validateInput();
[4]396
397# Several different cases here.
398# Static IP vs netblock
399# + Different flavours of static IP
400# + Different flavours of netblock
401
[550]402 if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
[594]403 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
[593]404 $page->param(staticip => 1);
[594]405 $page->param(poollist => $plist) if $plist;
[4]406 $cidr = "Single static IP";
[593]407##fixme: need to handle "no available pools"
[4]408
409 } else { # end show pool options
[24]410
[594]411 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
[595]412 $cidr = new NetAddr::IP $webvar{block};
[550]413 $alloc_from = new NetAddr::IP $webvar{allocfrom};
[24]414 $webvar{maskbits} = $cidr->masklen;
415 } else { # done with direct freeblocks assignment
416
417 if (!$webvar{maskbits}) {
[593]418 $page->param(err => "Please specify a CIDR mask length.");
[125]419 return;
[24]420 }
[594]421
422##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
423 my $failmsg = "No suitable free block found.<br>\n";
[192]424 if ($webvar{alloctype} eq 'rm') {
[594]425 $failmsg .= "We do not have a free routeable block of that size.<br>\n".
426 "You will have to either route a set of smaller netblocks or a single smaller netblock.";
[4]427 } else {
[211]428 if ($webvar{alloctype} =~ /^.[pc]$/) {
[594]429 $failmsg .= "You will have to route another superblock from one of the<br>\n".
430 "master blocks or chose a smaller block size for the pool.";
[24]431 } else {
[593]432 if (!$webvar{pop}) {
433 $page->param(err => 'Please select a POP to route the block from/through.');
434 return;
435 }
[594]436 $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
437 "from one of the master blocks or chose a smaller blocksize.";
[24]438 }
[4]439 }
[594]440
441 $cidr = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype}, $webvar{city}, $webvar{pop},
442 (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
443 if (!$cidr) {
[593]444 $page->param(err => $failmsg);
[125]445 return;
[24]446 }
[594]447 $cidr = new NetAddr::IP $cidr;
[550]448
449# this chunk now specific to "guided" allocation; freeblock-select can now slice-n-dice on its own.
[595]450 $alloc_from = "$cidr";
[550]451 # If the block to be allocated is smaller than the one we found,
452 # figure out the "real" block to be allocated.
453 if ($cidr->masklen() ne $webvar{maskbits}) {
454 my $maskbits = $cidr->masklen();
455 my @subblocks;
456 while ($maskbits++ < $webvar{maskbits}) {
457 @subblocks = $cidr->split($maskbits);
458 }
459 $cidr = $subblocks[0];
460 }
[24]461 } # check for freeblocks assignment or IPDB-controlled assignment
[4]462
[159]463 } # if ($webvar{alloctype} =~ /^.i$/)
[4]464
[394]465## node hack
466 if ($webvar{node} && $webvar{node} ne '-') {
[594]467 my $nodename = getNodeName($ip_dbh, $webvar{node});
[593]468 $page->param(nodename => $nodename);
469 $page->param(nodeid => $webvar{node});
[394]470 }
471## end node hack
472
[4]473 # Stick in the allocation data
[593]474 $page->param(alloc_type => $webvar{alloctype});
475 $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
476 $page->param(alloc_from => $alloc_from);
477 $page->param(cidr => $cidr);
478 $page->param(city => $q->escapeHTML($webvar{city}));
479 $page->param(custid => $webvar{custid});
480 $page->param(circid => $q->escapeHTML($webvar{circid}));
481 $page->param(desc => $q->escapeHTML($webvar{desc}));
[4]482
[593]483##fixme: find a way to have the displayed copy have <br> substitutions
484# for newlines, and the <input> value have either encoded or bare newlines.
485# Also applies to privdata.
486 $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
487
[286]488 # Check to see if user is allowed to do anything with sensitive data
489 my $privdata = '';
[593]490 $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'))
491 if $IPDBacl{$authuser} =~ /s/;
492
493 # Yay! This now has it's very own little home.
494 $page->param(billinguser => $webvar{userid})
[315]495 if $webvar{userid};
[286]496
[593]497##fixme: this is only needed iff confirm.tmpl and
498# confirmRemove.tmpl are merged (quite possible, just
499# a little tedious)
500 $page->param(action => "insert");
[286]501
[4]502} # end confirmAssign
503
504
505# Do the work of actually inserting a block in the database.
506sub insertAssign {
[242]507 if ($IPDBacl{$authuser} !~ /a/) {
[593]508 $aclerr = 'addblock';
[242]509 return;
510 }
[4]511 # Some things are done more than once.
[125]512 return if !validateInput();
[4]513
[286]514 if (!defined($webvar{privdata})) {
515 $webvar{privdata} = '';
516 }
[125]517 # $code is "success" vs "failure", $msg contains OK for a
518 # successful netblock allocation, the IP allocated for static
519 # IP, or the error message if an error occurred.
[593]520
[125]521 my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
522 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
[394]523 $webvar{circid}, $webvar{privdata}, $webvar{node});
[4]524
[549]525 if ($code eq 'OK' || $code eq 'WARN') {
[125]526 if ($webvar{alloctype} =~ /^.i$/) {
[315]527 $msg =~ s|/32||;
[593]528 $page->param(staticip => $msg);
529 $page->param(custid => $webvar{custid});
530 $page->param(billinguser => $webvar{billinguser});
[445]531 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
532 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
533 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[125]534 } else {
[315]535 my $netblock = new NetAddr::IP $webvar{fullcidr};
[593]536 $page->param(fullcidr => $webvar{fullcidr});
537 $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
538 $page->param(custid => $webvar{custid});
539 if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
540 $page->param(billinguser => $webvar{billinguser});
541 $page->param(custid => $webvar{custid});
542 $page->param(netaddr => $netblock->addr);
543 $page->param(masklen => $netblock->masklen);
[549]544 }
[445]545 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
546 "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
547 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[122]548 }
[125]549 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
[251]550 "'$webvar{alloctype}' ($msg)";
[125]551 } else {
552 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
553 "'$webvar{alloctype}' by $authuser failed: '$msg'";
[593]554 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
[159]555 " failed:<br>\n$msg\n");
[125]556 }
[122]557
[4]558} # end insertAssign()
559
560
561# Does some basic checks on common input data to make sure nothing
562# *really* weird gets in to the database through this script.
563# Does NOT do complete input validation!!!
564sub validateInput {
565 if ($webvar{city} eq '-') {
[593]566 $page->param(err => 'Please choose a city');
[125]567 return;
[4]568 }
[140]569
570 # Alloctype check.
[4]571 chomp $webvar{alloctype};
[140]572 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
573 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
574 # managing to call things in such a way as to cause this deserves a cryptic error.
[593]575 $page->param(err => 'Invalid alloctype');
[140]576 return;
577 }
578
579 # CustID check
[4]580 # We have different handling for customer allocations and "internal" or "our" allocations
[209]581 if ($def_custids{$webvar{alloctype}} eq '') {
[4]582 if (!$webvar{custid}) {
[593]583 $page->param(err => 'Please enter a customer ID.');
[125]584 return;
[4]585 }
[594]586 # Crosscheck with billing.
587 my $status = CustIDCK->custid_exist($webvar{custid});
588 if ($CustIDCK::Error) {
589 $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
590 return;
[4]591 }
[594]592 if (!$status) {
593 $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
594 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
595 "non-customer assignments.");
596 return;
597 }
[56]598# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
[140]599 } else {
[168]600 # New! Improved! And now Loaded From The Database!!
[56]601 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
[168]602 $webvar{custid} = $def_custids{$webvar{alloctype}};
[56]603 }
[4]604 }
[125]605
606 # Check POP location
607 my $flag;
[192]608 if ($webvar{alloctype} eq 'rm') {
[125]609 $flag = 'for a routed netblock';
610 foreach (@poplist) {
611 if (/^$webvar{city}$/) {
612 $flag = 'n';
613 last;
614 }
615 }
616 } else {
617 $flag = 'n';
[445]618##fixme: hook to force-set POP or city on certain alloctypes
619# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
[594]620 if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
[125]621 $flag = 'to route the block from/through';
622 }
623 }
[593]624
625 # if the alloctype has a restricted city/POP list as determined above,
626 # and the reqested city/POP does not match that list, complain
[125]627 if ($flag ne 'n') {
[593]628 $page->param(err => "Please choose a valid POP location $flag. Valid ".
[125]629 "POP locations are currently:<br>\n".join (" - ", @poplist));
630 return;
631 }
632
633 return 'OK';
[4]634} # end validateInput
635
636
637# Displays details of a specific allocation in a form
638# Allows update/delete
639# action=edit
640sub edit {
641
[594]642 # snag block info from db
643 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
[4]644
[594]645 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
646 $blockinfo->{type} =~ s/\s//;
[4]647
[593]648 $page->param(block => $webvar{block});
[4]649
[594]650 $page->param(custid => $blockinfo->{custid});
651 $page->param(city => $blockinfo->{city});
652 $page->param(circid => $blockinfo->{circuitid});
653 $page->param(desc => $blockinfo->{description});
654 $page->param(notes => $blockinfo->{notes});
[4]655
[192]656##fixme The check here should be built from the database
[593]657# Need to expand to support pool types too
[594]658 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
[593]659 $page->param(changetype => 1);
660 $page->param(alloctype => [
[594]661 { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
662 { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
663 { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
664 { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
665 { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
666 { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
667 { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
[593]668 ]
669 );
670 } else {
[594]671 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
672 $page->param(type => $blockinfo->{type});
[593]673 }
674
[394]675## node hack
[594]676 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $webvar{block});
[593]677 $page->param(havenodeid => $nodeid);
678
[594]679 if ($blockinfo->{type} eq 'fr' || $blockinfo->{type} eq 'bi') {
[593]680 $page->param(typesupportsnodes => 1);
681 $page->param(nodename => $nodename);
682
683##fixme: this whole hack needs cleanup and generalization for all alloctypes
684##fixme: arguably a bug that presence of a nodeid implies it can be changed..
685# but except for manual database changes, only the two types fr and bi can
686# (currently) have a nodeid set in the first place.
687 if ($IPDBacl{$authuser} =~ /c/) {
[594]688 my $nlist = getNodeList($ip_dbh);
689 foreach (@{$nlist}) {
690 $$_{selme} = ($$_{node_id} == $nodeid);
[394]691 }
[594]692 $page->param(nodelist => $nlist);
[394]693 }
694 }
695## end node hack
[593]696
[594]697 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
[593]698 $page->param(lastmod => $lastmod);
[35]699
[593]700 # not happy with the upside-down logic, but...
[594]701 $page->param(swipable => $blockinfo->{type} !~ /.i/);
702 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
[244]703
[286]704 # Check to see if we can display sensitive data
[593]705 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
[594]706 $page->param(privdata => $blockinfo->{privdata});
[286]707
[593]708 # ACL trickery - these two template booleans control the presence of all form/input tags
709 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
710 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
[4]711
712} # edit()
713
714
715# Stuff new info about a block into the db
716# action=update
717sub update {
[286]718 if ($IPDBacl{$authuser} !~ /c/) {
[593]719 $aclerr = 'updateblock';
[286]720 return;
721 }
[4]722
723 # Make sure incoming data is in correct format - custID among other things.
[220]724 return if !validateInput;
[4]725
[594]726 $webvar{swip} = 'n' if !$webvar{swip};
727
728 my %updargs = (
729 custid => $webvar{custid},
730 city => $webvar{city},
731 description => $webvar{desc},
732 notes => $webvar{notes},
733 circuitid => $webvar{circid},
734 block => $webvar{block},
735 type => $webvar{alloctype},
736 swip => $webvar{swip},
737 );
738
739 # Semioptional values
740 $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
741 $updargs{node} = $webvar{node} if $webvar{node};
742
743 my ($code,$msg) = updateBlock($ip_dbh, %updargs);
744
745 if ($code eq 'FAIL') {
[163]746 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
[593]747 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
[125]748 return;
[4]749 }
750
751 # If we get here, the operation succeeded.
752 syslog "notice", "$authuser updated $webvar{block}";
[594]753##fixme: log details of the change? old way is in the .debug stream anyway.
[445]754##fixme: need to wedge something in to allow "update:field" notifications
755## hmm. how to tell what changed? O_o
756mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
757 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
[4]758
[593]759## node hack
760 if ($webvar{node} && $webvar{node} ne '-') {
[594]761 my $nodename = getNodeName($ip_dbh, $webvar{node});
[593]762 $page->param(nodename => $nodename);
763 }
764## end node hack
765
[378]766 # Link back to browse-routed or list-pool page on "Update complete" page.
[594]767 my $cblock;
[378]768 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
[593]769 $page->param(backpool => 1);
[594]770 $cblock = ipParent($ip_dbh, $webvar{block});
[378]771 } else {
[594]772 $cblock = blockParent($ip_dbh, $webvar{block});
[378]773 }
[594]774 $page->param(backblock => $cblock->{cidr});
[378]775
[594]776 # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
777 # because otherwise we can't convert \n to <br>. *sigh*
778 $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
779 $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
780 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
781 $webvar{privdata} =~ s/\n/<br>\n/;
782
[593]783 $page->param(cidr => $webvar{block});
784 $page->param(city => $webvar{city});
785 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
786 $page->param(custid => $webvar{custid});
787 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
[594]788 $page->param(circid => $webvar{circid});
789 $page->param(desc => $webvar{desc});
790 $page->param(notes => $webvar{notes});
[593]791 $page->param(privdata => $webvar{privdata})
792 if $IPDBacl{$authuser} =~ /s/;
[4]793
794} # update()
795
796
797# Delete an allocation.
[125]798sub remove {
[242]799 if ($IPDBacl{$authuser} !~ /d/) {
[593]800 $aclerr = 'delblock';
[242]801 return;
802 }
803
[4]804 # Serves'em right for getting here...
805 if (!defined($webvar{block})) {
[593]806 $page->param(err => "Can't delete a block that doesn't exist");
[125]807 return;
[4]808 }
809
[594]810 my $blockdata;
[4]811
[192]812 if ($webvar{alloctype} eq 'rm') {
[4]813
[594]814 $blockdata->{block} = $webvar{block};
815 $blockdata->{city} = getRoutedCity($ip_dbh, $webvar{block});
816 $blockdata->{custid} = "N/A";
817 $blockdata->{type} = $webvar{alloctype};
818 $blockdata->{circuitid} = "N/A";
819 $blockdata->{description} = "N/A";
820 $blockdata->{notes} = "N/A";
821 $blockdata->{privdata} = "N/A";
[4]822
823 } elsif ($webvar{alloctype} eq 'mm') {
[593]824
[594]825 $blockdata->{block} = $webvar{block};
826 $blockdata->{city} = "N/A";
827 $blockdata->{custid} = "N/A";
828 $blockdata->{type} = $webvar{alloctype};
829 $blockdata->{circuitid} = "N/A";
830 $blockdata->{description} = "N/A";
831 $blockdata->{notes} = "N/A";
832 $blockdata->{privdata} = "N/A";
[593]833
[594]834 } else {
[4]835
[594]836 $blockdata = getBlockData($ip_dbh, $webvar{block})
[4]837
838 } # end cases for different alloctypes
839
[594]840 $page->param(block => $blockdata->{block});
841 $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
842 $page->param(type => $blockdata->{type});
843 $page->param(city => $blockdata->{city});
844 $page->param(custid => $blockdata->{custid});
845 $page->param(circid => $blockdata->{circuitid});
846 $page->param(desc => $blockdata->{description});
847 $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
848 $blockdata->{notes} =~ s/\n/<br>\n/;
849 $page->param(notes => $blockdata->{notes});
850 $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
851 $blockdata->{privdata} = '&nbsp;' if !$blockdata->{privdata};
852 $blockdata->{privdata} =~ s/\n/<br>\n/;
853 $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
854 $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
[4]855
[593]856} # end remove()
[4]857
858
859# Delete an allocation. Return it to the freeblocks table; munge
860# data as necessary to keep as few records as possible in freeblocks
861# to prevent weirdness when allocating blocks later.
862# Remove IPs from pool listing if necessary
863sub finalDelete {
[242]864 if ($IPDBacl{$authuser} !~ /d/) {
[593]865 $aclerr = 'delblock';
[242]866 return;
867 }
[4]868
[356]869 # need to retrieve block data before deleting so we can notify on that
[594]870 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
[356]871
[125]872 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
[4]873
[593]874 $page->param(block => $webvar{block});
[125]875 if ($code eq 'OK') {
[594]876 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block} ".
877 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
[445]878 mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
879 "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
[594]880 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
881 "\nDescription: ".$blockinfo->{description}."\n");
[125]882 } else {
[593]883 $page->param(failmsg => $msg);
[125]884 if ($webvar{alloctype} =~ /^.i$/) {
885 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
886 } else {
887 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
[593]888 $page->param(netblock => 1);
[4]889 }
[102]890 }
891
[4]892} # finalDelete
Note: See TracBrowser for help on using the repository browser.