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

Last change on this file since 690 was 687, checked in by Kris Deugau, 9 years ago

/trunk

CSS and layout cleanup, sort of. Convert tables with alternating row
colours to pure CSS (mainly to make addition of future fields, and future
*optional* or restricted-access fields, much easier). Play a little loose
with table structure to let headings and in-page "titles" continue to show
the main background colour instead of the "first line of the table" colour.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 33.9 KB
RevLine 
[4]1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
[8]3###
4# SVN revision info
5# $Date: 2015-01-29 22:02:06 +0000 (Thu, 29 Jan 2015) $
6# SVN revision $Rev: 687 $
7# Last update by $Author: kdeugau $
8###
[417]9# Copyright (C) 2004-2010 - Kris Deugau
[4]10
11use strict;
12use warnings;
13use CGI::Carp qw(fatalsToBrowser);
[517]14use CGI::Simple;
15use HTML::Template;
[4]16use DBI;
17use POSIX qw(ceil);
18use NetAddr::IP;
[582]19use Frontier::Client;
[4]20
21use Sys::Syslog;
22
[417]23# don't remove! required for GNU/FHS-ish install from tarball
24##uselib##
25
[515]26use CustIDCK;
[417]27use MyIPDB;
28
[431]29openlog "IPDB","pid","$IPDB::syslog_facility";
[4]30
[517]31## Environment. Collect some things, process some things, set some things...
32
[233]33# Collect the username from HTTP auth. If undefined, we're in
34# a test environment, or called without a username.
[4]35my $authuser;
36if (!defined($ENV{'REMOTE_USER'})) {
37 $authuser = '__temptest';
38} else {
39 $authuser = $ENV{'REMOTE_USER'};
40}
41
[517]42# anyone got a better name? :P
43my $thingroot = $ENV{SCRIPT_FILENAME};
44$thingroot =~ s|cgi-bin/main.cgi||;
45
[402]46syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
[4]47
[517]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
[106]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;
[142]64($ip_dbh,$errstr) = connectDB_My;
[106]65if (!$ip_dbh) {
[517]66 $webvar{action} = "dberr";
67} else {
68 initIPDBGlobals($ip_dbh);
[106]69}
[4]70
[517]71# Set up some globals
72$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
[233]73
[517]74my $header = HTML::Template->new(filename => "header.tmpl");
75my $footer = HTML::Template->new(filename => "footer.tmpl");
[233]76
[517]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;
[4]81
82
83#main()
[517]84my $aclerr;
[4]85
86if(!defined($webvar{action})) {
[517]87 $webvar{action} = "index"; #shuts up the warnings.
[4]88}
89
[517]90my $page;
91if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
[553]92 $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1);
[517]93} else {
94 $page = HTML::Template->new(filename => "dunno.tmpl");
95}
96
[4]97if($webvar{action} eq 'index') {
98 showSummary();
[233]99} elsif ($webvar{action} eq 'addmaster') {
100 if ($IPDBacl{$authuser} !~ /a/) {
[517]101 $aclerr = 'addmaster';
[233]102 }
[582]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 );
[623]112 my $result = IPDB::_rpc('getLocDropdown', %rpcargs);
[582]113 $page->param(loclist => $result);
114 }
115
[4]116} elsif ($webvar{action} eq 'newmaster') {
117
[233]118 if ($IPDBacl{$authuser} !~ /a/) {
[517]119 $aclerr = 'addmaster';
[233]120 } else {
121 my $cidr = new NetAddr::IP $webvar{cidr};
[517]122 $page->param(cidr => "$cidr");
[4]123
[582]124 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr}, (vrf => $webvar{vrf}, rdns => $webvar{rdns},
125 rwhois => $webvar{rwhois}, defloc => $webvar{loc}, user => $authuser) );
[4]126
[371]127 if ($code eq 'FAIL') {
[320]128 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
[517]129 $page->param(err => $msg);
[233]130 } else {
[624]131 $page->param(parent => $msg);
[582]132 if ($code eq 'WARN') {
[624]133 $IPDB::errstr =~ s/\n\n/<br>\n/g;
134 $IPDB::errstr =~ s/:\n/:<br>\n/g;
135 $page->param(warn => $IPDB::errstr);
[582]136 }
[233]137 syslog "info", "$authuser added master block $webvar{cidr}";
138 }
[4]139
[233]140 } # ACL check
141
[4]142} # end add new master
143
[567]144elsif ($webvar{action} eq 'showsubs') {
145 showSubs();
146}
147
[4]148elsif($webvar{action} eq 'listpool') {
[528]149 showPool();
[4]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}
[397]174elsif ($webvar{action} eq 'nodesearch') {
[519]175 my $nodelist = getNodeList($ip_dbh);
176 $page->param(nodelist => $nodelist);
[517]177}
[397]178
[517]179# DB failure. Can't do much here, really.
180elsif ($webvar{action} eq 'dberr') {
181 $page->param(errmsg => $errstr);
[397]182}
183
[517]184# Default is an error. It shouldn't be possible to get here unless you're
185# randomly feeding in values for webvar{action}.
[4]186else {
187 my $rnd = rand 500;
188 my $boing = sprintf("%.2f", rand 500);
[517]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]);
[4]202}
[111]203## Finally! Done with that NASTY "case" emulation!
[4]204
205
[517]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}
[4]214
[106]215# Clean up IPDB globals, DB handle, etc.
[111]216finish($ip_dbh);
[199]217
[517]218## Do all our printing here so we can generate errors and stick them into the slots in the templates.
[199]219
[517]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
[106]230# Just in case something waaaayyy down isn't in place
231# properly... we exit explicitly.
[517]232exit 0;
[4]233
234
235# Initial display: Show master blocks with total allocated subnets, total free subnets
[118]236sub showSummary {
[523]237 my $masterlist = listSummary($ip_dbh);
238 $page->param(masterlist => $masterlist);
[106]239
[517]240 $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
[4]241} # showSummary
242
243
[566]244# Display blocks immediately within a given parent
245sub showSubs {
[682]246 # Which layout?
247 if ($IPDB::sublistlayout == 2) {
248
249 # 2-part layout; mixed containers and end-use allocations and free blocks.
250 # Containers have a second line for the subblock metadata.
251 # We need to load an alternate template for this case.
252 $page = HTML::Template->new(filename => "showsubs2.tmpl", loop_context_vars => 1, global_vars => 1);
253
254 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/));
255
256 my $sublist = listSubs($ip_dbh, parent => $webvar{parent});
257 $page->param(sublist => $sublist);
258
259 } else {
260
[685]261 # 3-part layout; containers, end-use allocations, and free blocks
[682]262
263 my $contlist = listContainers($ip_dbh, parent => $webvar{parent});
264 $page->param(contlist => $contlist);
265
266 my $alloclist = listAllocations($ip_dbh, parent => $webvar{parent});
267 $page->param(alloclist => $alloclist);
268
269 # only show "delete" button if we have no container or usage allocations
270 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/) && !(@$contlist || @$alloclist));
271
272 }
273
274 # Common elements
[627]275 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
276
[685]277##fixme: do we add a wrapper to not show the edit link for master blocks?
278#$page->param(editme => 1) unless $pinfo->{type} ne 'mm';
279
280 $page->param(self_id => $webvar{parent});
[627]281 $page->param(block => $pinfo->{block});
[566]282 $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
[4]283
[627]284 my $flist = listFree($ip_dbh, parent => $webvar{parent});
[566]285 $page->param(freelist => $flist);
286} # showSubs
[4]287
288
289# List the IPs used in a pool
[528]290sub showPool {
[4]291
[631]292 my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
293 my $cidr = new NetAddr::IP $poolinfo->{block};
[4]294
[631]295 $page->param(block => $cidr);
[517]296 $page->param(netip => $cidr->addr);
297 $cidr++;
298 $page->param(gate => $cidr->addr);
299 $cidr--; $cidr--;
300 $page->param(bcast => $cidr->addr);
301 $page->param(mask => $cidr->mask);
[157]302
[528]303 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
304 $page->param(city => $poolinfo->{city});
[517]305
[157]306 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
[528]307 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
[4]308
309# probably have to add an "edit IP allocation" link here somewhere.
310
[528]311 my $plist = listPool($ip_dbh, $webvar{pool});
312 # technically slightly more efficient to check the ACL in an if () once outside the foreach
313 foreach (@{$plist}) {
314 $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
[4]315 }
[528]316 $page->param(poolips => $plist);
317} # end showPool
[4]318
319
[106]320# Show "Add new allocation" page. Note that the actual page may
321# be one of two templates, and the lists come from the database.
[4]322sub assignBlock {
323
[233]324 if ($IPDBacl{$authuser} !~ /a/) {
[517]325 $aclerr = 'addblock';
[233]326 return;
327 }
328
[517]329 # hack pthbttt eww
[633]330 $webvar{parent} = 0 if !$webvar{parent};
[517]331 $webvar{block} = '' if !$webvar{block};
[21]332
[575]333 $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
[517]334
[633]335 if ($webvar{fbid} || $webvar{fbtype}) {
[575]336
337 # Common case, according to reported usage. Block to assign is specified.
[21]338 my $block = new NetAddr::IP $webvar{block};
[187]339
[675]340 my ($rdns,$cached) = getBlockRDNS($ip_dbh, id => $webvar{parent}, type => $webvar{fbtype}, user => $authuser);
[585]341 $page->param(rdns => $rdns) if $rdns;
[633]342 $page->param(parent => $webvar{parent});
343 $page->param(fbid => $webvar{fbid});
[675]344 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
345 $page->param(cached => $cached);
[585]346
[575]347 $webvar{fbtype} = '' if !$webvar{fbtype};
348 if ($webvar{fbtype} eq 'i') {
[633]349 my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i');
350 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
[575]351 $page->param(
352 fbip => 1,
[633]353 block => $ipinfo->{block},
[575]354 fbdisptype => $list_alloctypes{$ipinfo->{type}},
355 type => $ipinfo->{type},
[633]356 allocfrom => $pinfo->{block},
[575]357 );
[187]358 } else {
[529]359 # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
[575]360 my $tlist = getTypeList($ip_dbh, 'n');
[529]361 $tlist->[0]->{sel} = 1;
[575]362 $page->param(typelist => $tlist, block => $block);
[106]363 }
[575]364
[21]365 } else {
[575]366
367 # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
[541]368 my $mlist = getMasterList($ip_dbh, 'c');
369 $page->param(masterlist => $mlist);
[517]370
371 my @pops;
[633]372 foreach my $pop (@citylist) {
[517]373 my %row = (pop => $pop);
374 push (@pops, \%row);
[92]375 }
[517]376 $page->param(pops => \@pops);
377
[529]378 # get all standard alloctypes
379 my $tlist = getTypeList($ip_dbh, 'a');
380 $tlist->[0]->{sel} = 1;
381 $page->param(typelist => $tlist);
[21]382 }
[517]383
384 my @cities;
[92]385 foreach my $city (@citylist) {
[517]386 my %row = (city => $city);
387 push (@cities, \%row);
[92]388 }
[517]389 $page->param(citylist => \@cities);
[4]390
[397]391## node hack
[530]392 my $nlist = getNodeList($ip_dbh);
393 $page->param(nodelist => $nlist);
[397]394## end node hack
395
[517]396 $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
[284]397
[4]398} # assignBlock
399
400
401# Take info on requested IP assignment and see what we can provide.
402sub confirmAssign {
[233]403 if ($IPDBacl{$authuser} !~ /a/) {
[517]404 $aclerr = 'addblock';
[233]405 return;
406 }
[4]407
408 my $cidr;
409 my $alloc_from;
[633]410 my $fbid = $webvar{fbid};
411 my $p_id = $webvar{parent};
[4]412
413 # Going to manually validate some items.
414 # custid and city are automagic.
[111]415 return if !validateInput();
[4]416
[665]417 # make sure this is defined
418 $webvar{fbassign} = 'n' if !$webvar{fbassign};
419
[4]420# Several different cases here.
421# Static IP vs netblock
422# + Different flavours of static IP
423# + Different flavours of netblock
424
[575]425 if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
[665]426 if (!$webvar{pop}) {
427 $page->param(err => "Please select a location/POP site to allocate from.");
428 return;
429 }
[532]430 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
[517]431 $page->param(staticip => 1);
[532]432 $page->param(poollist => $plist) if $plist;
[4]433 $cidr = "Single static IP";
[517]434##fixme: need to handle "no available pools"
[4]435
436 } else { # end show pool options
[21]437
[533]438 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
[21]439 $cidr = new NetAddr::IP $webvar{block};
[575]440 $alloc_from = new NetAddr::IP $webvar{allocfrom};
[21]441 $webvar{maskbits} = $cidr->masklen;
442 } else { # done with direct freeblocks assignment
443
444 if (!$webvar{maskbits}) {
[517]445 $page->param(err => "Please specify a CIDR mask length.");
[111]446 return;
[21]447 }
[533]448
449##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
450 my $failmsg = "No suitable free block found.<br>\n";
[187]451 if ($webvar{alloctype} eq 'rm') {
[533]452 $failmsg .= "We do not have a free routeable block of that size.<br>\n".
453 "You will have to either route a set of smaller netblocks or a single smaller netblock.";
[4]454 } else {
[214]455 if ($webvar{alloctype} =~ /^.[pc]$/) {
[533]456 $failmsg .= "You will have to route another superblock from one of the<br>\n".
457 "master blocks or chose a smaller block size for the pool.";
[21]458 } else {
[517]459 if (!$webvar{pop}) {
460 $page->param(err => 'Please select a POP to route the block from/through.');
461 return;
462 }
[533]463 $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
464 "from one of the master blocks or chose a smaller blocksize.";
[21]465 }
[4]466 }
[533]467
[575]468## fixme: add rdepth?
[633]469 ($fbid,$cidr,$p_id) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype},
470 $webvar{city}, $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
[533]471 if (!$cidr) {
[517]472 $page->param(err => $failmsg);
[111]473 return;
[21]474 }
[533]475 $cidr = new NetAddr::IP $cidr;
[575]476
477# this chunk now specific to "guided" allocation; freeblock-select can now slice-n-dice on its own.
478 $alloc_from = "$cidr";
479 # If the block to be allocated is smaller than the one we found,
480 # figure out the "real" block to be allocated.
481 if ($cidr->masklen() ne $webvar{maskbits}) {
482 my $maskbits = $cidr->masklen();
483 my @subblocks;
484 while ($maskbits++ < $webvar{maskbits}) {
485 @subblocks = $cidr->split($maskbits);
486 }
487 $cidr = $subblocks[0];
488 }
[21]489 } # check for freeblocks assignment or IPDB-controlled assignment
[4]490
[674]491 # Generate the IP list for the new allocation in case someone wants to set per-IP rDNS right away.
492 # We don't do this on the previous page because we don't know how big a block or even what IP range
493 # it's for (if following the "normal" allocation process)
494 if ($IPDBacl{$authuser} =~ /c/
495 && $cidr->masklen != $cidr->bits
496 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
497 && $webvar{alloctype} !~ /^.[dpi]/
498 # do we want to allow v6 at all?
499 #&& ! $cidr->{isv6}
500 ) {
501 my @list;
502 foreach my $ip (@{$cidr->splitref()}) {
503 my %row;
504 $row{r_ip} = $ip->addr;
505 $row{iphost} = '';
506 push @list, \%row;
507 }
508 $page->param(r_iplist => \@list);
509 # We don't use this here, because these IPs should already be bare.
510 # ... or should we be paranoid? Make it a config option?
511 #getRDNSbyIP($ip_dbh, type => $webvar{alloctype}, range => "$cidr", user => $authuser) );
512 }
[157]513 } # if ($webvar{alloctype} =~ /^.i$/)
[4]514
[397]515## node hack
516 if ($webvar{node} && $webvar{node} ne '-') {
[530]517 my $nodename = getNodeName($ip_dbh, $webvar{node});
[517]518 $page->param(nodename => $nodename);
519 $page->param(nodeid => $webvar{node});
[397]520 }
521## end node hack
522
[4]523 # Stick in the allocation data
[517]524 $page->param(alloc_type => $webvar{alloctype});
525 $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
526 $page->param(alloc_from => $alloc_from);
[633]527 $page->param(parent => $p_id);
528 $page->param(fbid => $fbid);
[517]529 $page->param(cidr => $cidr);
[585]530 $page->param(rdns => $webvar{rdns});
[517]531 $page->param(city => $q->escapeHTML($webvar{city}));
532 $page->param(custid => $webvar{custid});
533 $page->param(circid => $q->escapeHTML($webvar{circid}));
534 $page->param(desc => $q->escapeHTML($webvar{desc}));
[4]535
[517]536##fixme: find a way to have the displayed copy have <br> substitutions
537# for newlines, and the <input> value have either encoded or bare newlines.
538# Also applies to privdata.
539 $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
540
[284]541 # Check to see if user is allowed to do anything with sensitive data
542 my $privdata = '';
[517]543 $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'))
544 if $IPDBacl{$authuser} =~ /s/;
545
546 # Yay! This now has it's very own little home.
547 $page->param(billinguser => $webvar{userid})
[299]548 if $webvar{userid};
[284]549
[517]550##fixme: this is only needed iff confirm.tmpl and
551# confirmRemove.tmpl are merged (quite possible, just
552# a little tedious)
553 $page->param(action => "insert");
[284]554
[4]555} # end confirmAssign
556
557
558# Do the work of actually inserting a block in the database.
559sub insertAssign {
[233]560 if ($IPDBacl{$authuser} !~ /a/) {
[517]561 $aclerr = 'addblock';
[233]562 return;
563 }
[4]564 # Some things are done more than once.
[111]565 return if !validateInput();
[4]566
[284]567 if (!defined($webvar{privdata})) {
568 $webvar{privdata} = '';
569 }
[575]570
571 # split up some linked data for static IPs via guided allocation. needed for breadcrumbs lite.
[584]572 ($webvar{alloc_from},$webvar{rdepth}) = split /,/, $webvar{alloc_from} if $webvar{alloc_from} =~ /,/;
[575]573
[106]574 # $code is "success" vs "failure", $msg contains OK for a
575 # successful netblock allocation, the IP allocated for static
576 # IP, or the error message if an error occurred.
[517]577
[677]578##fixme: consider just passing \%webvar to allocateBlock()?
579 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
580 my %iprev;
581 foreach (keys %webvar) {
582 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
583 }
584
[633]585 my ($code,$msg) = allocateBlock($ip_dbh, cidr => $webvar{fullcidr}, fbid => $webvar{fbid},
586 parent => $webvar{parent}, custid => $webvar{custid}, type => $webvar{alloctype}, city => $webvar{city},
[575]587 desc => $webvar{desc}, notes => $webvar{notes}, circid => $webvar{circid},
[677]588 privdata => $webvar{privdata}, nodeid => $webvar{node}, rdns => $webvar{rdns}, user => $authuser,
589 iprev => \%iprev);
[4]590
[111]591 if ($code eq 'OK') {
[106]592 if ($webvar{alloctype} =~ /^.i$/) {
[300]593 $msg =~ s|/32||;
[517]594 $page->param(staticip => $msg);
595 $page->param(custid => $webvar{custid});
[633]596 $page->param(parent => $webvar{parent}, pool => $webvar{alloc_from});
[517]597 $page->param(billinguser => $webvar{billinguser});
[416]598 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
599 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
600 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[106]601 } else {
[301]602 my $netblock = new NetAddr::IP $webvar{fullcidr};
[517]603 $page->param(fullcidr => $webvar{fullcidr});
604 $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
605 $page->param(custid => $webvar{custid});
[575]606 # breadcrumbs lite! provide at least a link to the parent of the block we just allocated.
[633]607 my $binfo = getBlockData($ip_dbh, $webvar{parent});
608 $page->param(parentid => $webvar{parent});
609 $page->param(parentblock => $binfo->{block});
[517]610 if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
611 $page->param(billinguser => $webvar{billinguser});
612 $page->param(custid => $webvar{custid});
613 $page->param(netaddr => $netblock->addr);
614 $page->param(masklen => $netblock->masklen);
615 }
[416]616 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
617 "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
618 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[4]619 }
[106]620 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
[256]621 "'$webvar{alloctype}' ($msg)";
[111]622 } else {
623 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
624 "'$webvar{alloctype}' by $authuser failed: '$msg'";
[517]625 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
[157]626 " failed:<br>\n$msg\n");
[106]627 }
[4]628
629} # end insertAssign()
630
631
632# Does some basic checks on common input data to make sure nothing
633# *really* weird gets in to the database through this script.
634# Does NOT do complete input validation!!!
635sub validateInput {
636 if ($webvar{city} eq '-') {
[517]637 $page->param(err => 'Please choose a city');
[111]638 return;
[4]639 }
[138]640
641 # Alloctype check.
[4]642 chomp $webvar{alloctype};
[138]643 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
644 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
645 # managing to call things in such a way as to cause this deserves a cryptic error.
[517]646 $page->param(err => 'Invalid alloctype');
[138]647 return;
648 }
649
650 # CustID check
[4]651 # We have different handling for customer allocations and "internal" or "our" allocations
[214]652 if ($def_custids{$webvar{alloctype}} eq '') {
[4]653 if (!$webvar{custid}) {
[517]654 $page->param(err => 'Please enter a customer ID.');
[111]655 return;
[4]656 }
[546]657 # Crosscheck with billing.
658 my $status = CustIDCK->custid_exist($webvar{custid});
659 if ($CustIDCK::Error) {
660 $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
661 return;
[4]662 }
[546]663 if (!$status) {
664 $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
665 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
666 "non-customer assignments.");
667 return;
668 }
[400]669# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
[138]670 } else {
[167]671 # New! Improved! And now Loaded From The Database!!
[320]672 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
673 $webvar{custid} = $def_custids{$webvar{alloctype}};
674 }
[4]675 }
[111]676
[571]677## hmmm.... is this even useful?
678if (0) {
[111]679 # Check POP location
680 my $flag;
[187]681 if ($webvar{alloctype} eq 'rm') {
[111]682 $flag = 'for a routed netblock';
683 foreach (@poplist) {
684 if (/^$webvar{city}$/) {
685 $flag = 'n';
686 last;
687 }
688 }
689 } else {
690 $flag = 'n';
[442]691##fixme: hook to force-set POP or city on certain alloctypes
692# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
[536]693 if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
[111]694 $flag = 'to route the block from/through';
695 }
696 }
[517]697
698 # if the alloctype has a restricted city/POP list as determined above,
699 # and the reqested city/POP does not match that list, complain
[111]700 if ($flag ne 'n') {
[517]701 $page->param(err => "Please choose a valid POP location $flag. Valid ".
[111]702 "POP locations are currently:<br>\n".join (" - ", @poplist));
703 return;
704 }
[571]705}
[111]706
707 return 'OK';
[4]708} # end validateInput
709
710
711# Displays details of a specific allocation in a form
712# Allows update/delete
713# action=edit
714sub edit {
715
[534]716 # snag block info from db
[634]717 my $blockinfo = getBlockData($ip_dbh, $webvar{id}, $webvar{basetype});
718 $page->param(id => $webvar{id});
719 $page->param(basetype => $webvar{basetype});
[4]720
[534]721 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
722 $blockinfo->{type} =~ s/\s//;
[4]723
[675]724 my $cached;
[586]725 # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
[675]726 ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser);
727 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
728 $page->param(cached => $cached);
[586]729
[675]730 my $cidr = new NetAddr::IP $blockinfo->{block};
731 # Limit the per-IP rDNS list based on CIDR length; larger ones just take up too much space.
732 # Also, don't show on IP pools; the individual IPs will have a space for rDNS
733 # Don't show on single IPs; these use the "pattern" field
734 if ($IPDBacl{$authuser} =~ /c/
735 && $cidr->masklen != $cidr->bits
736 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
[683]737 && $blockinfo->{type} !~ /^.[dpi]/
[675]738 # do we want to allow v6 at all?
739 #&& ! $cidr->{isv6}
740 ) {
741 $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type},
742 range => $blockinfo->{block}, user => $authuser) );
743 }
744
[634]745 $page->param(block => $blockinfo->{block});
[586]746 $page->param(rdns => $blockinfo->{rdns});
[4]747
[534]748 $page->param(custid => $blockinfo->{custid});
749 $page->param(city => $blockinfo->{city});
750 $page->param(circid => $blockinfo->{circuitid});
751 $page->param(desc => $blockinfo->{description});
752 $page->param(notes => $blockinfo->{notes});
[4]753
[187]754##fixme The check here should be built from the database
[517]755# Need to expand to support pool types too
[534]756 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
[517]757 $page->param(changetype => 1);
758 $page->param(alloctype => [
[534]759 { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
760 { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
761 { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
762 { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
763 { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
764 { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
765 { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
[517]766 ]
767 );
768 } else {
[534]769 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
770 $page->param(type => $blockinfo->{type});
[517]771 }
772
[397]773## node hack
[634]774 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block});
775# $page->param(havenodeid => $nodeid);
776 $page->param(nodename => $nodename);
[517]777
778##fixme: this whole hack needs cleanup and generalization for all alloctypes
779##fixme: arguably a bug that presence of a nodeid implies it can be changed..
[634]780 if ($IPDBacl{$authuser} =~ /c/) {
781 my $nlist = getNodeList($ip_dbh);
782 if ($nodeid) {
[530]783 foreach (@{$nlist}) {
[634]784 $$_{selme} = ($$_{node_id} == $nodeid);
[397]785 }
786 }
[634]787 $page->param(nodelist => $nlist);
[397]788 }
789## end node hack
[517]790
[534]791 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
[517]792 $page->param(lastmod => $lastmod);
[33]793
[517]794 # not happy with the upside-down logic, but...
[534]795 $page->param(swipable => $blockinfo->{type} !~ /.i/);
796 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
[320]797
[284]798 # Check to see if we can display sensitive data
[517]799 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
[534]800 $page->param(privdata => $blockinfo->{privdata});
[284]801
[517]802 # ACL trickery - these two template booleans control the presence of all form/input tags
803 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
804 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
[4]805
806} # edit()
807
808
809# Stuff new info about a block into the db
810# action=update
811sub update {
[284]812 if ($IPDBacl{$authuser} !~ /c/) {
[517]813 $aclerr = 'updateblock';
[284]814 return;
815 }
[4]816
817 # Make sure incoming data is in correct format - custID among other things.
[228]818 return if !validateInput;
[4]819
[536]820 $webvar{swip} = 'n' if !$webvar{swip};
821
[531]822 my %updargs = (
823 custid => $webvar{custid},
824 city => $webvar{city},
825 description => $webvar{desc},
826 notes => $webvar{notes},
827 circuitid => $webvar{circid},
828 block => $webvar{block},
829 type => $webvar{alloctype},
[536]830 swip => $webvar{swip},
[588]831 rdns => $webvar{rdns},
832 user => $authuser,
[531]833 );
834
835 # Semioptional values
836 $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
837 $updargs{node} = $webvar{node} if $webvar{node};
838
[677]839 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
840 my %iprev;
841 foreach (keys %webvar) {
842 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
843 }
[531]844
[677]845 my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
846
[531]847 if ($code eq 'FAIL') {
[166]848 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
[517]849 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
[111]850 return;
[4]851 }
852
853 # If we get here, the operation succeeded.
854 syslog "notice", "$authuser updated $webvar{block}";
[531]855##fixme: log details of the change? old way is in the .debug stream anyway.
[416]856##fixme: need to wedge something in to allow "update:field" notifications
857## hmm. how to tell what changed? O_o
[426]858mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
[416]859 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
[4]860
[517]861## node hack
862 if ($webvar{node} && $webvar{node} ne '-') {
[530]863 my $nodename = getNodeName($ip_dbh, $webvar{node});
[517]864 $page->param(nodename => $nodename);
865 }
866## end node hack
867
[380]868 # Link back to browse-routed or list-pool page on "Update complete" page.
[634]869 my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
870 my $pblock = getBlockData($ip_dbh, $binfo->{parent_id});
871 $page->param(backid => $binfo->{parent_id});
872 $page->param(backblock => $pblock->{block});
873 $page->param(backpool => ($webvar{basetype} eq 'i'));
[380]874
[536]875 # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
876 # because otherwise we can't convert \n to <br>. *sigh*
877 $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
878 $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
879 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
880 $webvar{privdata} =~ s/\n/<br>\n/;
881
[634]882 $page->param(cidr => $binfo->{block});
[588]883 $page->param(rdns => $webvar{rdns});
[517]884 $page->param(city => $webvar{city});
885 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
886 $page->param(custid => $webvar{custid});
887 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
[536]888 $page->param(circid => $webvar{circid});
889 $page->param(desc => $webvar{desc});
890 $page->param(notes => $webvar{notes});
[517]891 $page->param(privdata => $webvar{privdata})
892 if $IPDBacl{$authuser} =~ /s/;
[4]893
894} # update()
895
896
897# Delete an allocation.
[106]898sub remove {
[233]899 if ($IPDBacl{$authuser} !~ /d/) {
[517]900 $aclerr = 'delblock';
[233]901 return;
902 }
903
[4]904 # Serves'em right for getting here...
905 if (!defined($webvar{block})) {
[517]906 $page->param(err => "Can't delete a block that doesn't exist");
[111]907 return;
[4]908 }
909
[538]910 my $blockdata;
[638]911 $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
[4]912
[638]913 if ($blockdata->{parent_id} == 0) { # $webvar{alloctype} eq 'mm'
[538]914 $blockdata->{city} = "N/A";
915 $blockdata->{custid} = "N/A";
916 $blockdata->{circuitid} = "N/A";
917 $blockdata->{description} = "N/A";
918 $blockdata->{notes} = "N/A";
919 $blockdata->{privdata} = "N/A";
[638]920 } # end cases for different alloctypes
[517]921
[638]922 $page->param(blockid => $webvar{block});
923 $page->param(basetype => $webvar{basetype});
[4]924
[538]925 $page->param(block => $blockdata->{block});
[589]926 $page->param(rdns => $blockdata->{rdns});
927
928 # maybe need to apply more magic here?
929 # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
930 # -> all real blocks (nb: pool IPs need extra handling)
931 # -> NOC/private-IP (how to ID?)
932 # -> anything with a pattern matching $IPDB::domain?
933 if ($blockdata->{type} !~ /^.i$/) {
934 $page->param(autodel => 1);
935 }
936
[538]937 $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
938 $page->param(city => $blockdata->{city});
939 $page->param(custid => $blockdata->{custid});
940 $page->param(circid => $blockdata->{circuitid});
941 $page->param(desc => $blockdata->{description});
942 $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
943 $blockdata->{notes} =~ s/\n/<br>\n/;
944 $page->param(notes => $blockdata->{notes});
945 $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
[540]946 $blockdata->{privdata} = '&nbsp;' if !$blockdata->{privdata};
[538]947 $blockdata->{privdata} =~ s/\n/<br>\n/;
948 $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
949 $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
[4]950
[517]951} # end remove()
[4]952
953
954# Delete an allocation. Return it to the freeblocks table; munge
955# data as necessary to keep as few records as possible in freeblocks
956# to prevent weirdness when allocating blocks later.
957# Remove IPs from pool listing if necessary
958sub finalDelete {
[233]959 if ($IPDBacl{$authuser} !~ /d/) {
[517]960 $aclerr = 'delblock';
[233]961 return;
962 }
[4]963
[370]964 # need to retrieve block data before deleting so we can notify on that
[638]965 my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
966 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b');
[370]967
[638]968 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser);
[4]969
[638]970 $page->param(block => $blockinfo->{block});
[651]971 $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}}));
[638]972 $page->param(delparent_id => $blockinfo->{parent_id});# if $webvar{rdepth};
[651]973 if ($pinfo) {
974 $page->param(delparent => $pinfo->{block});
975 $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}}));
976 }
[638]977 $page->param(returnpool => ($webvar{basetype} eq 'i') );
[591]978 if ($code =~ /^WARN(POOL|MERGE)/) {
[638]979 my ($pid,$pcidr) = split /,/, $msg;
[654]980 my $real_pinfo = getBlockData($ip_dbh, $pid, 'b');
[638]981 $page->param(parent_id => $pid);
982 $page->param(parent => $pcidr);
[654]983 $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}}));
[577]984 $page->param(mergeip => $code eq 'WARNPOOL');
985 }
[591]986 if ($code eq 'WARN') {
987 $msg =~ s/\n/<br>\n/g;
988 $page->param(genwarn => $msg);
989 }
[577]990 if ($code eq 'OK' || $code =~ /^WARN/) {
[638]991 syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock $webvar{block} ".
[528]992 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
[638]993 mailNotify($ip_dbh, 'da', "REMOVED: ".$disp_alloctypes{$blockinfo->{type}}." $webvar{block}",
994 $disp_alloctypes{$blockinfo->{type}}." $webvar{block} deallocated by $authuser\n".
[528]995 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
996 "\nDescription: ".$blockinfo->{description}."\n");
[106]997 } else {
[517]998 $page->param(failmsg => $msg);
[106]999 if ($webvar{alloctype} =~ /^.i$/) {
1000 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1001 } else {
1002 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
[517]1003 $page->param(netblock => 1);
[4]1004 }
[106]1005 }
[4]1006
1007} # finalDelete
Note: See TracBrowser for help on using the repository browser.