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

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

/trunk

Clean up and move SQL for post-update backlink to IPDB.pm. See #34.
Also rename getParent() to subParent() to fit in with ipParent() and
blockParent().

Fix a couple "Use of uninitialized..." log-noise bugs. See #31.

Move some HTML-entity-escaping into the template, and shuffle lines
munging the notes and restricted data on post-update value display
so we can properly munge in <br> for \n. Doesn't seem to be a way
to plug that into HTML::Template. :( See #3.

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