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

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

/trunk

Patch up a few glitches in the "automatic" assignment process

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