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

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

/trunk

Work in progress, see #5:
Replace $webvar{action} branches for showmaster and showrouted
with showsubs, calling listSubs()

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