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

Last change on this file since 596 was 596, checked in by Kris Deugau, 11 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
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
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###
9# Copyright (C) 2004-2011 - 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 $errstr;
63($ip_dbh,$errstr) = connectDB_My;
64if (!$ip_dbh) {
65 $webvar{action} = "dberr";
66} else {
67 initIPDBGlobals($ip_dbh);
68}
69
70# Set up some globals
71$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
72
73my $header = HTML::Template->new(filename => "header.tmpl");
74my $footer = HTML::Template->new(filename => "footer.tmpl");
75
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;
80
81
82#main()
83my $aclerr;
84
85if(!defined($webvar{action})) {
86 $webvar{action} = "index"; #shuts up the warnings.
87}
88
89my $page;
90if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
91 $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1);
92} else {
93 $page = HTML::Template->new(filename => "dunno.tmpl");
94}
95
96if($webvar{action} eq 'index') {
97 showSummary();
98} elsif ($webvar{action} eq 'addmaster') {
99 if ($IPDBacl{$authuser} !~ /a/) {
100 $aclerr = 'addmaster';
101 }
102} elsif ($webvar{action} eq 'newmaster') {
103
104 if ($IPDBacl{$authuser} !~ /a/) {
105 $aclerr = 'addmaster';
106 } else {
107 my $cidr = new NetAddr::IP $webvar{cidr};
108 $page->param(cidr => "$cidr");
109
110 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
111
112 if ($code eq 'FAIL') {
113 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
114 $page->param(err => $msg);
115 } else {
116 syslog "info", "$authuser added master block $webvar{cidr}";
117 }
118
119 } # ACL check
120
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') {
130 showPool();
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}
155elsif ($webvar{action} eq 'nodesearch') {
156 my $nodelist = getNodeList($ip_dbh);
157 $page->param(nodelist => $nodelist);
158}
159
160# DB failure. Can't do much here, really.
161elsif ($webvar{action} eq 'dberr') {
162 $page->param(errmsg => $errstr);
163}
164
165# Default is an error. It shouldn't be possible to get here unless you're
166# randomly feeding in values for webvar{action}.
167else {
168 my $rnd = rand 500;
169 my $boing = sprintf("%.2f", rand 500);
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]);
183}
184## Finally! Done with that NASTY "case" emulation!
185
186
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}
195
196# Clean up IPDB globals, DB handle, etc.
197finish($ip_dbh);
198
199## Do all our printing here so we can generate errors and stick them into the slots in the templates.
200
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
211# Just in case something waaaayyy down isn't in place
212# properly... we exit explicitly.
213exit 0;
214
215
216# Initial display: Show master blocks with total allocated subnets, total free subnets
217sub showSummary {
218 my $masterlist = listSummary($ip_dbh);
219 $page->param(masterlist => $masterlist);
220
221 $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
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
232 $page->param(master => $webvar{block});
233 $page->param(delmaster => ($IPDBacl{$authuser} =~ /d/));
234
235 my $rlist = listMaster($ip_dbh, $webvar{block});
236 $page->param(routedlist => $rlist);
237
238 my $flist = listFree($ip_dbh, $webvar{block});
239 $page->param(unrouted => $flist);
240 $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
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
253 $page->param(master => $webvar{block});
254 $page->param(delrouted => $IPDBacl{$authuser} =~ /d/);
255
256 my $rcity = getRoutedCity($ip_dbh, $webvar{block});
257 $page->param(rcity => $rcity);
258
259 my $blist = listRBlock($ip_dbh, $webvar{block});
260 $page->param(blocklist => $blist);
261
262 my $flist = listFree($ip_dbh, $webvar{block}, 'y');
263 $page->param(unassigned => $flist);
264} # showRBlock
265
266
267# List the IPs used in a pool
268sub showPool {
269
270 my $cidr = new NetAddr::IP $webvar{pool};
271
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);
279
280 # Snag pool info for heading
281 my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
282
283 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
284 $page->param(city => $poolinfo->{city});
285
286 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
287 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
288
289# probably have to add an "edit IP allocation" link here somewhere.
290
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/;
295 }
296 $page->param(poolips => $plist);
297} # end showPool
298
299
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.
302sub assignBlock {
303
304 if ($IPDBacl{$authuser} !~ /a/) {
305 $aclerr = 'addblock';
306 return;
307 }
308
309 # hack pthbttt eww
310 $webvar{block} = '' if !$webvar{block};
311
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));
315 $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
316
317 if ($webvar{fbtype}) {
318
319 # Common case, according to reported usage. Block to assign is specified.
320 my $block = new NetAddr::IP $webvar{block};
321
322 $webvar{fbtype} = '' if !$webvar{fbtype};
323 if ($webvar{fbtype} eq 'i') {
324 my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i');
325##fixme: a reversion! kill it!
326my ($iptype,$pool) = $ip_dbh->selectrow_array("SELECT type,pool FROM poolips WHERE ip = ?", undef, ($block));
327 $page->param(
328 fbip => 1,
329 block => $ipinfo->{block},
330 fbdisptype => $list_alloctypes{$ipinfo->{type}},
331 type => $ipinfo->{type},
332 allocfrom => $pool,
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 );
341 } else {
342 # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
343 my $tlist = getTypeList($ip_dbh, 'n');
344 $tlist->[0]->{sel} = 1;
345 $page->param(typelist => $tlist, block => $block);
346 }
347
348 } else {
349
350 # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
351 my $mlist = getMasterList($ip_dbh, 'c');
352 $page->param(masterlist => $mlist);
353
354 my @pops;
355 foreach my $pop (@citylist) {
356 my %row = (pop => $pop);
357 push (@pops, \%row);
358 }
359 $page->param(pops => \@pops);
360
361 # get all standard alloctypes
362 my $tlist = getTypeList($ip_dbh, 'a');
363 $tlist->[0]->{sel} = 1;
364 $page->param(typelist => $tlist);
365 }
366 my @cities;
367 foreach my $city (@citylist) {
368 my %row = (city => $city);
369 push (@cities, \%row);
370 }
371 $page->param(citylist => \@cities);
372
373## node hack
374 my $nlist = getNodeList($ip_dbh);
375 $page->param(nodelist => $nlist);
376## end node hack
377
378 $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
379
380} # assignBlock
381
382
383# Take info on requested IP assignment and see what we can provide.
384sub confirmAssign {
385 if ($IPDBacl{$authuser} !~ /a/) {
386 $aclerr = 'addblock';
387 return;
388 }
389
390 my $cidr;
391 my $alloc_from;
392
393 # Going to manually validate some items.
394 # custid and city are automagic.
395 return if !validateInput();
396
397# Several different cases here.
398# Static IP vs netblock
399# + Different flavours of static IP
400# + Different flavours of netblock
401
402 if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
403 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
404 $page->param(staticip => 1);
405 $page->param(poollist => $plist) if $plist;
406 $cidr = "Single static IP";
407##fixme: need to handle "no available pools"
408
409 } else { # end show pool options
410
411 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
412 $cidr = new NetAddr::IP $webvar{block};
413 $alloc_from = new NetAddr::IP $webvar{allocfrom};
414 $webvar{maskbits} = $cidr->masklen;
415 } else { # done with direct freeblocks assignment
416
417 if (!$webvar{maskbits}) {
418 $page->param(err => "Please specify a CIDR mask length.");
419 return;
420 }
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";
424 if ($webvar{alloctype} eq 'rm') {
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.";
427 } else {
428 if ($webvar{alloctype} =~ /^.[pc]$/) {
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.";
431 } else {
432 if (!$webvar{pop}) {
433 $page->param(err => 'Please select a POP to route the block from/through.');
434 return;
435 }
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.";
438 }
439 }
440
441 $cidr = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype}, $webvar{city}, $webvar{pop},
442 (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
443 if (!$cidr) {
444 $page->param(err => $failmsg);
445 return;
446 }
447 $cidr = new NetAddr::IP $cidr;
448
449# this chunk now specific to "guided" allocation; freeblock-select can now slice-n-dice on its own.
450 $alloc_from = "$cidr";
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 }
461 } # check for freeblocks assignment or IPDB-controlled assignment
462
463 } # if ($webvar{alloctype} =~ /^.i$/)
464
465## node hack
466 if ($webvar{node} && $webvar{node} ne '-') {
467 my $nodename = getNodeName($ip_dbh, $webvar{node});
468 $page->param(nodename => $nodename);
469 $page->param(nodeid => $webvar{node});
470 }
471## end node hack
472
473 # Stick in the allocation data
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}));
482
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
488 # Check to see if user is allowed to do anything with sensitive data
489 my $privdata = '';
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})
495 if $webvar{userid};
496
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");
501
502} # end confirmAssign
503
504
505# Do the work of actually inserting a block in the database.
506sub insertAssign {
507 if ($IPDBacl{$authuser} !~ /a/) {
508 $aclerr = 'addblock';
509 return;
510 }
511 # Some things are done more than once.
512 return if !validateInput();
513
514 if (!defined($webvar{privdata})) {
515 $webvar{privdata} = '';
516 }
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.
520
521 my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
522 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
523 $webvar{circid}, $webvar{privdata}, $webvar{node});
524
525 if ($code eq 'OK' || $code eq 'WARN') {
526 if ($webvar{alloctype} =~ /^.i$/) {
527 $msg =~ s|/32||;
528 $page->param(staticip => $msg);
529 $page->param(custid => $webvar{custid});
530 $page->param(billinguser => $webvar{billinguser});
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");
534 } else {
535 my $netblock = new NetAddr::IP $webvar{fullcidr};
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);
544 }
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");
548 }
549 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
550 "'$webvar{alloctype}' ($msg)";
551 } else {
552 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
553 "'$webvar{alloctype}' by $authuser failed: '$msg'";
554 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
555 " failed:<br>\n$msg\n");
556 }
557
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 '-') {
566 $page->param(err => 'Please choose a city');
567 return;
568 }
569
570 # Alloctype check.
571 chomp $webvar{alloctype};
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.
575 $page->param(err => 'Invalid alloctype');
576 return;
577 }
578
579 # CustID check
580 # We have different handling for customer allocations and "internal" or "our" allocations
581 if ($def_custids{$webvar{alloctype}} eq '') {
582 if (!$webvar{custid}) {
583 $page->param(err => 'Please enter a customer ID.');
584 return;
585 }
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;
591 }
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 }
598# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
599 } else {
600 # New! Improved! And now Loaded From The Database!!
601 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
602 $webvar{custid} = $def_custids{$webvar{alloctype}};
603 }
604 }
605
606 # Check POP location
607 my $flag;
608 if ($webvar{alloctype} eq 'rm') {
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';
618##fixme: hook to force-set POP or city on certain alloctypes
619# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
620 if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
621 $flag = 'to route the block from/through';
622 }
623 }
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
627 if ($flag ne 'n') {
628 $page->param(err => "Please choose a valid POP location $flag. Valid ".
629 "POP locations are currently:<br>\n".join (" - ", @poplist));
630 return;
631 }
632
633 return 'OK';
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
642 # snag block info from db
643 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
644
645 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
646 $blockinfo->{type} =~ s/\s//;
647
648 $page->param(block => $webvar{block});
649
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});
655
656##fixme The check here should be built from the database
657# Need to expand to support pool types too
658 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
659 $page->param(changetype => 1);
660 $page->param(alloctype => [
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" },
668 ]
669 );
670 } else {
671 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
672 $page->param(type => $blockinfo->{type});
673 }
674
675## node hack
676 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $webvar{block});
677 $page->param(havenodeid => $nodeid);
678
679 if ($blockinfo->{type} eq 'fr' || $blockinfo->{type} eq 'bi') {
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/) {
688 my $nlist = getNodeList($ip_dbh);
689 foreach (@{$nlist}) {
690 $$_{selme} = ($$_{node_id} == $nodeid);
691 }
692 $page->param(nodelist => $nlist);
693 }
694 }
695## end node hack
696
697 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
698 $page->param(lastmod => $lastmod);
699
700 # not happy with the upside-down logic, but...
701 $page->param(swipable => $blockinfo->{type} !~ /.i/);
702 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
703
704 # Check to see if we can display sensitive data
705 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
706 $page->param(privdata => $blockinfo->{privdata});
707
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/);
711
712} # edit()
713
714
715# Stuff new info about a block into the db
716# action=update
717sub update {
718 if ($IPDBacl{$authuser} !~ /c/) {
719 $aclerr = 'updateblock';
720 return;
721 }
722
723 # Make sure incoming data is in correct format - custID among other things.
724 return if !validateInput;
725
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') {
746 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
747 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
748 return;
749 }
750
751 # If we get here, the operation succeeded.
752 syslog "notice", "$authuser updated $webvar{block}";
753##fixme: log details of the change? old way is in the .debug stream anyway.
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';
758
759## node hack
760 if ($webvar{node} && $webvar{node} ne '-') {
761 my $nodename = getNodeName($ip_dbh, $webvar{node});
762 $page->param(nodename => $nodename);
763 }
764## end node hack
765
766 # Link back to browse-routed or list-pool page on "Update complete" page.
767 my $cblock;
768 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
769 $page->param(backpool => 1);
770 $cblock = ipParent($ip_dbh, $webvar{block});
771 } else {
772 $cblock = blockParent($ip_dbh, $webvar{block});
773 }
774 $page->param(backblock => $cblock->{cidr});
775
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
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');
788 $page->param(circid => $webvar{circid});
789 $page->param(desc => $webvar{desc});
790 $page->param(notes => $webvar{notes});
791 $page->param(privdata => $webvar{privdata})
792 if $IPDBacl{$authuser} =~ /s/;
793
794} # update()
795
796
797# Delete an allocation.
798sub remove {
799 if ($IPDBacl{$authuser} !~ /d/) {
800 $aclerr = 'delblock';
801 return;
802 }
803
804 # Serves'em right for getting here...
805 if (!defined($webvar{block})) {
806 $page->param(err => "Can't delete a block that doesn't exist");
807 return;
808 }
809
810 my $blockdata;
811
812 if ($webvar{alloctype} eq 'rm') {
813
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";
822
823 } elsif ($webvar{alloctype} eq 'mm') {
824
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";
833
834 } else {
835
836 $blockdata = getBlockData($ip_dbh, $webvar{block})
837
838 } # end cases for different alloctypes
839
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]$/);
855
856} # end remove()
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 {
864 if ($IPDBacl{$authuser} !~ /d/) {
865 $aclerr = 'delblock';
866 return;
867 }
868
869 # need to retrieve block data before deleting so we can notify on that
870 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
871
872 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
873
874 $page->param(block => $webvar{block});
875 if ($code eq 'OK') {
876 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block} ".
877 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
878 mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
879 "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
880 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
881 "\nDescription: ".$blockinfo->{description}."\n");
882 } else {
883 $page->param(failmsg => $msg);
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'";
888 $page->param(netblock => 1);
889 }
890 }
891
892} # finalDelete
Note: See TracBrowser for help on using the repository browser.