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

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

/trunk

Collect and pass through per-IP rDNS. Still need to cache these locally.
See #1.

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