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

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

/trunk

Clean up and merge SQL for block-edit page into getBlockData.
Fortunately, the enhancement does not affect previous uses of
that sub. See #34.
Also tweak the template with a whitespace nitpick and to escape
HTML-funky characters in the circuit ID, description, notes, or
restricted data. Still need to confirm these can be reversed
on submission. See The Ticket That Won't Die, #3.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 29.1 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3###
4# SVN revision info
5# $Date: 2012-10-31 20:51:18 +0000 (Wed, 31 Oct 2012) $
6# SVN revision $Rev: 534 $
7# Last update by $Author: kdeugau $
8###
9# Copyright (C) 2004-2010 - Kris Deugau
10
11use strict;
12use warnings;
13use CGI::Carp qw(fatalsToBrowser);
14use CGI::Simple;
15use HTML::Template;
16use DBI;
17use POSIX qw(ceil);
18use NetAddr::IP;
19
20use Sys::Syslog;
21
22# don't remove! required for GNU/FHS-ish install from tarball
23##uselib##
24
25use CustIDCK;
26use MyIPDB;
27
28openlog "IPDB","pid","$IPDB::syslog_facility";
29
30## Environment. Collect some things, process some things, set some things...
31
32# Collect the username from HTTP auth. If undefined, we're in
33# a test environment, or called without a username.
34my $authuser;
35if (!defined($ENV{'REMOTE_USER'})) {
36 $authuser = '__temptest';
37} else {
38 $authuser = $ENV{'REMOTE_USER'};
39}
40
41# anyone got a better name? :P
42my $thingroot = $ENV{SCRIPT_FILENAME};
43$thingroot =~ s|cgi-bin/main.cgi||;
44
45syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
46
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
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;
64($ip_dbh,$errstr) = connectDB_My;
65if (!$ip_dbh) {
66 $webvar{action} = "dberr";
67} else {
68 initIPDBGlobals($ip_dbh);
69}
70
71# Set up some globals
72$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
73
74my $header = HTML::Template->new(filename => "header.tmpl");
75my $footer = HTML::Template->new(filename => "footer.tmpl");
76
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;
81
82
83#main()
84my $aclerr;
85
86if(!defined($webvar{action})) {
87 $webvar{action} = "index"; #shuts up the warnings.
88}
89
90my $page;
91if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
92 $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1);
93} else {
94 $page = HTML::Template->new(filename => "dunno.tmpl");
95}
96
97if($webvar{action} eq 'index') {
98 showSummary();
99} elsif ($webvar{action} eq 'addmaster') {
100 if ($IPDBacl{$authuser} !~ /a/) {
101 $aclerr = 'addmaster';
102 }
103} elsif ($webvar{action} eq 'newmaster') {
104
105 if ($IPDBacl{$authuser} !~ /a/) {
106 $aclerr = 'addmaster';
107 } else {
108 my $cidr = new NetAddr::IP $webvar{cidr};
109 $page->param(cidr => "$cidr");
110
111 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
112
113 if ($code eq 'FAIL') {
114 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
115 $page->param(err => $msg);
116 } else {
117 syslog "info", "$authuser added master block $webvar{cidr}";
118 }
119
120 } # ACL check
121
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') {
131 showPool();
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}
156elsif ($webvar{action} eq 'nodesearch') {
157 my $nodelist = getNodeList($ip_dbh);
158 $page->param(nodelist => $nodelist);
159}
160
161# DB failure. Can't do much here, really.
162elsif ($webvar{action} eq 'dberr') {
163 $page->param(errmsg => $errstr);
164}
165
166# Default is an error. It shouldn't be possible to get here unless you're
167# randomly feeding in values for webvar{action}.
168else {
169 my $rnd = rand 500;
170 my $boing = sprintf("%.2f", rand 500);
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]);
184}
185## Finally! Done with that NASTY "case" emulation!
186
187
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}
196
197$sth->finish if $sth;
198# Clean up IPDB globals, DB handle, etc.
199finish($ip_dbh);
200
201## Do all our printing here so we can generate errors and stick them into the slots in the templates.
202
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
213# Just in case something waaaayyy down isn't in place
214# properly... we exit explicitly.
215exit 0;
216
217
218# Initial display: Show master blocks with total allocated subnets, total free subnets
219sub showSummary {
220 my $masterlist = listSummary($ip_dbh);
221 $page->param(masterlist => $masterlist);
222
223 $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
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
234 $page->param(master => $webvar{block});
235 $page->param(delmaster => ($IPDBacl{$authuser} =~ /d/));
236
237 my $rlist = listMaster($ip_dbh, $webvar{block});
238 $page->param(routedlist => $rlist);
239
240 my $flist = listFree($ip_dbh, $webvar{block});
241 $page->param(unrouted => $flist);
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
254 $page->param(master => $webvar{block});
255 $page->param(delrouted => $IPDBacl{$authuser} =~ /d/);
256
257 my $rcity = getRoutedCity($ip_dbh, $webvar{block});
258 $page->param(rcity => $rcity);
259
260 my $blist = listRBlock($ip_dbh, $webvar{block});
261 $page->param(blocklist => $blist);
262
263 my $flist = listFree($ip_dbh, $webvar{block}, 'y');
264 $page->param(unassigned => $flist);
265} # showRBlock
266
267
268# List the IPs used in a pool
269sub showPool {
270
271 my $cidr = new NetAddr::IP $webvar{pool};
272
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);
280
281 # Snag pool info for heading
282 my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
283
284 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
285 $page->param(city => $poolinfo->{city});
286
287 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
288 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
289
290# probably have to add an "edit IP allocation" link here somewhere.
291
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/;
296 }
297 $page->param(poolips => $plist);
298} # end showPool
299
300
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.
303sub assignBlock {
304
305 if ($IPDBacl{$authuser} !~ /a/) {
306 $aclerr = 'addblock';
307 return;
308 }
309
310 # hack pthbttt eww
311 $webvar{block} = '' if !$webvar{block};
312
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
319 # New special case- block to assign is specified
320 if ($webvar{block} ne '') {
321 my $block = new NetAddr::IP $webvar{block};
322
323 # Handle contained freeblock allocation.
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') {
328 # Snag the type of the container block from the database.
329## hmm. need a flag for parent class/type, sort of?
330 my $pblock = getParent($ip_dbh, $webvar{block});
331 my $ptype = $pblock->{type};
332 $ptype =~ s/c$/r/;
333 $page->param(fbdisptype => $list_alloctypes{$ptype});
334 $page->param(type => $ptype);
335 } else {
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);
340 }
341 } else {
342 my @masterlist;
343 foreach my $master (@masterblocks) {
344 my %row = (master => "$master");
345 push (@masterlist, \%row);
346 }
347 $page->param(masterlist => \@masterlist);
348
349 my @pops;
350 foreach my $pop (@poplist) {
351 my %row = (pop => $pop);
352 push (@pops, \%row);
353 }
354 $page->param(pops => \@pops);
355
356 # get all standard alloctypes
357 my $tlist = getTypeList($ip_dbh, 'a');
358 $tlist->[0]->{sel} = 1;
359 $page->param(typelist => $tlist);
360 }
361
362 my @cities;
363 foreach my $city (@citylist) {
364 my %row = (city => $city);
365 push (@cities, \%row);
366 }
367 $page->param(citylist => \@cities);
368
369## node hack
370 my $nlist = getNodeList($ip_dbh);
371 $page->param(nodelist => $nlist);
372## end node hack
373
374 $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
375
376} # assignBlock
377
378
379# Take info on requested IP assignment and see what we can provide.
380sub confirmAssign {
381 if ($IPDBacl{$authuser} !~ /a/) {
382 $aclerr = 'addblock';
383 return;
384 }
385
386 my $cidr;
387 my $alloc_from;
388
389 # Going to manually validate some items.
390 # custid and city are automagic.
391 return if !validateInput();
392
393# Several different cases here.
394# Static IP vs netblock
395# + Different flavours of static IP
396# + Different flavours of netblock
397
398 if ($webvar{alloctype} =~ /^.i$/) {
399 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
400 $page->param(staticip => 1);
401 $page->param(poollist => $plist) if $plist;
402 $cidr = "Single static IP";
403##fixme: need to handle "no available pools"
404
405 } else { # end show pool options
406
407 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
408 $cidr = new NetAddr::IP $webvar{block};
409 $webvar{maskbits} = $cidr->masklen;
410 } else { # done with direct freeblocks assignment
411
412 if (!$webvar{maskbits}) {
413 $page->param(err => "Please specify a CIDR mask length.");
414 return;
415 }
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";
419 if ($webvar{alloctype} eq 'rm') {
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.";
422 } else {
423 if ($webvar{alloctype} =~ /^.[pc]$/) {
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.";
426 } else {
427 if (!$webvar{pop}) {
428 $page->param(err => 'Please select a POP to route the block from/through.');
429 return;
430 }
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.";
433 }
434 }
435
436 $cidr = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype}, $webvar{city}, $webvar{pop},
437 (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
438 if (!$cidr) {
439 $page->param(err => $failmsg);
440 return;
441 }
442 $cidr = new NetAddr::IP $cidr;
443 } # check for freeblocks assignment or IPDB-controlled assignment
444
445 $alloc_from = "$cidr";
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 }
457 } # if ($webvar{alloctype} =~ /^.i$/)
458
459## node hack
460 if ($webvar{node} && $webvar{node} ne '-') {
461 my $nodename = getNodeName($ip_dbh, $webvar{node});
462 $page->param(nodename => $nodename);
463 $page->param(nodeid => $webvar{node});
464 }
465## end node hack
466
467 # Stick in the allocation data
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}));
476
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
482 # Check to see if user is allowed to do anything with sensitive data
483 my $privdata = '';
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})
489 if $webvar{userid};
490
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");
495
496} # end confirmAssign
497
498
499# Do the work of actually inserting a block in the database.
500sub insertAssign {
501 if ($IPDBacl{$authuser} !~ /a/) {
502 $aclerr = 'addblock';
503 return;
504 }
505 # Some things are done more than once.
506 return if !validateInput();
507
508 if (!defined($webvar{privdata})) {
509 $webvar{privdata} = '';
510 }
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.
514
515 my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
516 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
517 $webvar{circid}, $webvar{privdata}, $webvar{node});
518
519 if ($code eq 'OK') {
520 if ($webvar{alloctype} =~ /^.i$/) {
521 $msg =~ s|/32||;
522 $page->param(staticip => $msg);
523 $page->param(custid => $webvar{custid});
524 $page->param(billinguser => $webvar{billinguser});
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");
528 } else {
529 my $netblock = new NetAddr::IP $webvar{fullcidr};
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 }
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");
542 }
543 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
544 "'$webvar{alloctype}' ($msg)";
545 } else {
546 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
547 "'$webvar{alloctype}' by $authuser failed: '$msg'";
548 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
549 " failed:<br>\n$msg\n");
550 }
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 '-') {
560 $page->param(err => 'Please choose a city');
561 return;
562 }
563
564 # Alloctype check.
565 chomp $webvar{alloctype};
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.
569 $page->param(err => 'Invalid alloctype');
570 return;
571 }
572
573 # CustID check
574 # We have different handling for customer allocations and "internal" or "our" allocations
575 if ($def_custids{$webvar{alloctype}} eq '') {
576 if (!$webvar{custid}) {
577 $page->param(err => 'Please enter a customer ID.');
578 return;
579 }
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) {
586 $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
587 return;
588 }
589 if (!$status) {
590 $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
591 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
592 "non-customer assignments.");
593 return;
594 }
595 }
596# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
597 } else {
598 # New! Improved! And now Loaded From The Database!!
599 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
600 $webvar{custid} = $def_custids{$webvar{alloctype}};
601 }
602 }
603
604 # Check POP location
605 my $flag;
606 if ($webvar{alloctype} eq 'rm') {
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';
616##fixme: hook to force-set POP or city on certain alloctypes
617# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
618 if ($webvar{pop} =~ /^-$/) {
619 $flag = 'to route the block from/through';
620 }
621 }
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
625 if ($flag ne 'n') {
626 $page->param(err => "Please choose a valid POP location $flag. Valid ".
627 "POP locations are currently:<br>\n".join (" - ", @poplist));
628 return;
629 }
630
631 return 'OK';
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
640 # snag block info from db
641 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
642
643 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
644 $blockinfo->{type} =~ s/\s//;
645
646 $page->param(block => $webvar{block});
647
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});
653
654##fixme The check here should be built from the database
655# Need to expand to support pool types too
656 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
657 $page->param(changetype => 1);
658 $page->param(alloctype => [
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" },
666 ]
667 );
668 } else {
669 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
670 $page->param(type => $blockinfo->{type});
671 }
672
673## node hack
674 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $webvar{block});
675 $page->param(havenodeid => $nodeid);
676
677 if ($blockinfo->{type} eq 'fr' || $blockinfo->{type} eq 'bi') {
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/) {
686 my $nlist = getNodeList($ip_dbh);
687 foreach (@{$nlist}) {
688 $$_{selme} = ($$_{node_id} == $nodeid);
689 }
690 $page->param(nodelist => $nlist);
691 }
692 }
693## end node hack
694
695 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
696 $page->param(lastmod => $lastmod);
697
698 # not happy with the upside-down logic, but...
699 $page->param(swipable => $blockinfo->{type} !~ /.i/);
700 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
701
702 # Check to see if we can display sensitive data
703 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
704 $page->param(privdata => $blockinfo->{privdata});
705
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/);
709
710} # edit()
711
712
713# Stuff new info about a block into the db
714# action=update
715sub update {
716 if ($IPDBacl{$authuser} !~ /c/) {
717 $aclerr = 'updateblock';
718 return;
719 }
720
721 # Make sure incoming data is in correct format - custID among other things.
722 return if !validateInput;
723
724 my %updargs = (
725 custid => $webvar{custid},
726 city => $webvar{city},
727 description => $webvar{desc},
728 notes => $webvar{notes},
729 circuitid => $webvar{circid},
730 block => $webvar{block},
731 type => $webvar{alloctype},
732 );
733
734 # Semioptional values
735 $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
736 $updargs{node} = $webvar{node} if $webvar{node};
737
738 my ($code,$msg) = updateBlock($ip_dbh, %updargs);
739
740 if ($code eq 'FAIL') {
741 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
742 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
743 return;
744 }
745
746 # If we get here, the operation succeeded.
747 syslog "notice", "$authuser updated $webvar{block}";
748##fixme: log details of the change? old way is in the .debug stream anyway.
749##fixme: need to wedge something in to allow "update:field" notifications
750## hmm. how to tell what changed? O_o
751mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
752 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
753
754## node hack
755 if ($webvar{node} && $webvar{node} ne '-') {
756 my $nodename = getNodeName($ip_dbh, $webvar{node});
757 $page->param(nodename => $nodename);
758 }
759## end node hack
760
761 # Link back to browse-routed or list-pool page on "Update complete" page.
762 my $cblock; # to contain the CIDR of the container block we're retrieving.
763 my $sql;
764 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
765 $page->param(backpool => 1);
766 $sql = "select pool from poolips where ip='$webvar{block}'";
767 } else {
768 $sql = "select cidr from routed where cidr >>= '$webvar{block}'";
769 }
770 # I define there to be no errors on this operation... so we don't need to check for them.
771 $sth = $ip_dbh->prepare($sql);
772 $sth->execute;
773 $sth->bind_columns(\$cblock);
774 $sth->fetch();
775 $sth->finish;
776 $page->param(backblock => $cblock);
777
778 $page->param(cidr => $webvar{block});
779 $page->param(city => $webvar{city});
780 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
781 $page->param(custid => $webvar{custid});
782 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
783 $page->param(circid => $q->escapeHTML($webvar{circid}));
784 $page->param(desc => $q->escapeHTML($webvar{desc}));
785 $page->param(notes => $q->escapeHTML($webvar{notes}));
786 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
787 $page->param(privdata => $webvar{privdata})
788 if $IPDBacl{$authuser} =~ /s/;
789
790} # update()
791
792
793# Delete an allocation.
794sub remove {
795 if ($IPDBacl{$authuser} !~ /d/) {
796 $aclerr = 'delblock';
797 return;
798 }
799
800 # Serves'em right for getting here...
801 if (!defined($webvar{block})) {
802 $page->param(err => "Can't delete a block that doesn't exist");
803 return;
804 }
805
806 my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata);
807
808 if ($webvar{alloctype} eq 'rm') {
809 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
810 $sth->execute();
811
812# This feels... extreme.
813 croak $sth->errstr() if($sth->errstr());
814
815 $sth->bind_columns(\$cidr,\$city);
816 $sth->execute();
817 $sth->fetch || croak $sth->errstr();
818 $custid = "N/A";
819 $alloctype = $webvar{alloctype};
820 $circid = "N/A";
821 $desc = "N/A";
822 $notes = "N/A";
823 $privdata = "N/A";
824
825 } elsif ($webvar{alloctype} eq 'mm') {
826
827 $cidr = $webvar{block};
828 $city = "N/A";
829 $custid = "N/A";
830 $alloctype = $webvar{alloctype};
831 $circid = "N/A";
832 $desc = "N/A";
833 $notes = "N/A";
834 $privdata = "N/A";
835
836 } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m
837
838 # Unassigning a static IP
839 my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata".
840 " from poolips where ip='$webvar{block}'");
841 $sth->execute();
842# croak $sth->errstr() if($sth->errstr());
843
844 $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid,
845 \$privdata);
846 $sth->fetch() || croak $sth->errstr;
847
848 } else { # done with alloctype=~ /^.i$/
849
850 my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata".
851 " from allocations where cidr='$webvar{block}'");
852 $sth->execute();
853# croak $sth->errstr() if($sth->errstr());
854
855 $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc,
856 \$notes, \$privdata);
857 $sth->fetch() || carp $sth->errstr;
858 } # end cases for different alloctypes
859
860 $page->param(block => $cidr);
861 $page->param(disptype => $disp_alloctypes{$alloctype});
862 $page->param(type => $alloctype);
863 $page->param(city => $city);
864 $page->param(custid => $custid);
865 $page->param(circid => $circid);
866 $page->param(desc => $desc);
867 $page->param(notes => $notes);
868 $privdata = '&nbsp;' if $privdata eq '';
869 $page->param(privdata => $privdata) if $IPDBacl{$authuser} =~ /s/;
870 $page->param(delpool => $alloctype =~ /^.[pd]$/);
871
872} # end remove()
873
874
875# Delete an allocation. Return it to the freeblocks table; munge
876# data as necessary to keep as few records as possible in freeblocks
877# to prevent weirdness when allocating blocks later.
878# Remove IPs from pool listing if necessary
879sub finalDelete {
880 if ($IPDBacl{$authuser} !~ /d/) {
881 $aclerr = 'delblock';
882 return;
883 }
884
885 # need to retrieve block data before deleting so we can notify on that
886 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
887
888 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
889
890 $page->param(block => $webvar{block});
891 if ($code eq 'OK') {
892 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block} ".
893 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
894 mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
895 "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
896 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
897 "\nDescription: ".$blockinfo->{description}."\n");
898 } else {
899 $page->param(failmsg => $msg);
900 if ($webvar{alloctype} =~ /^.i$/) {
901 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
902 } else {
903 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
904 $page->param(netblock => 1);
905 }
906 }
907
908} # finalDelete
Note: See TracBrowser for help on using the repository browser.