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

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

/trunk

Missed main.cgi update for IP pool page in r630. See #5.

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