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

Last change on this file since 627 was 627, checked in by Kris Deugau, 10 years ago

/trunk

Commit 3/mumble for work done intermittently over the past ~year.
See #5, comment 25:

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