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

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

/trunk

Convert main.cgi's sole direct RPC call to use IPDB::_rpc()

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