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

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

/trunk

Add "breadcrumb" navigation trace to most pages.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 39.5 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3###
4# SVN revision info
5# $Date: 2015-02-18 22:16:21 +0000 (Wed, 18 Feb 2015) $
6# SVN revision $Rev: 697 $
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");
76my $utilbar = HTML::Template->new(filename => "utilbar.tmpl", loop_context_vars => 1, global_vars => 1);
77
78print "Content-type: text/html\n\n";
79
80$header->param(version => $IPDB::VERSION);
81$header->param(addperm => $IPDBacl{$authuser} =~ /a/);
82$header->param(webpath => $IPDB::webpath);
83
84$utilbar->param(webpath => $IPDB::webpath);
85
86print $header->output;
87
88
89#main()
90my $aclerr;
91
92if(!defined($webvar{action})) {
93 $webvar{action} = "index"; #shuts up the warnings.
94}
95
96my $page;
97if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
98 $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1);
99} else {
100 $page = HTML::Template->new(filename => "dunno.tmpl");
101}
102
103if($webvar{action} eq 'index') {
104 showSummary();
105} elsif ($webvar{action} eq 'addmaster') {
106 if ($IPDBacl{$authuser} !~ /a/) {
107 $aclerr = 'addmaster';
108 }
109
110 # Retrieve the list of DNS locations if we've got a place to grab them from
111 if ($IPDB::rpc_url) {
112
113 my %rpcargs = (
114 rpcuser => $authuser,
115 group => 1, # bleh
116 defloc => '',
117 );
118 my $result = IPDB::_rpc('getLocDropdown', %rpcargs);
119 $page->param(loclist => $result);
120 }
121
122} elsif ($webvar{action} eq 'newmaster') {
123
124 if ($IPDBacl{$authuser} !~ /a/) {
125 $aclerr = 'addmaster';
126 } else {
127 my $cidr = new NetAddr::IP $webvar{cidr};
128 $page->param(cidr => "$cidr");
129
130 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr}, (vrf => $webvar{vrf}, rdns => $webvar{rdns},
131 rwhois => $webvar{rwhois}, defloc => $webvar{loc}, user => $authuser) );
132
133 if ($code eq 'FAIL') {
134 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
135 $page->param(err => $msg);
136 } else {
137 $page->param(parent => $msg);
138 if ($code eq 'WARN') {
139 $IPDB::errstr =~ s/\n\n/<br>\n/g;
140 $IPDB::errstr =~ s/:\n/:<br>\n/g;
141 $page->param(warn => $IPDB::errstr);
142 }
143 syslog "info", "$authuser added master block $webvar{cidr}";
144 }
145
146 } # ACL check
147
148} # end add new master
149
150elsif ($webvar{action} eq 'showsubs') {
151 showSubs();
152}
153
154elsif($webvar{action} eq 'listpool') {
155 showPool();
156}
157
158# Not modified or added; just shuffled
159elsif($webvar{action} eq 'assign') {
160 assignBlock();
161}
162elsif($webvar{action} eq 'confirm') {
163 confirmAssign();
164}
165elsif($webvar{action} eq 'insert') {
166 insertAssign();
167}
168elsif($webvar{action} eq 'edit') {
169 edit();
170}
171elsif($webvar{action} eq 'update') {
172 update();
173}
174elsif($webvar{action} eq 'delete') {
175 remove();
176}
177elsif($webvar{action} eq 'finaldelete') {
178 finalDelete();
179}
180elsif ($webvar{action} eq 'nodesearch') {
181 my $nodelist = getNodeList($ip_dbh);
182 $page->param(nodelist => $nodelist);
183}
184
185# DB failure. Can't do much here, really.
186elsif ($webvar{action} eq 'dberr') {
187 $page->param(errmsg => $errstr);
188}
189
190# Default is an error. It shouldn't be possible to get here unless you're
191# randomly feeding in values for webvar{action}.
192else {
193 my $rnd = rand 500;
194 my $boing = sprintf("%.2f", rand 500);
195 my @excuses = (
196 "Aether cloudy. Ask again later about $webvar{action}.",
197 "The gods are unhappy with your sacrificial $webvar{action}.",
198 "Because one of $webvar{action}'s legs are both the same",
199 "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!",
200 "Hey, man, you've had your free $webvar{action}. Next one's gonna... <i>cost</i>....",
201 "I ain't done $webvar{action}",
202 "Oooo, look! A flying $webvar{action}!",
203 "$webvar{action} too evil, avoiding.",
204 "Rocks fall, $webvar{action} dies.",
205 "Bit bucket must be emptied before I can $webvar{action}..."
206 );
207 $page->param(dunno => $excuses[$rnd/50.0]);
208}
209## Finally! Done with that NASTY "case" emulation!
210
211
212# Switch to a different template if we've tripped on an ACL error.
213# Note that this should only be exercised in development, when
214# deeplinked, or when being attacked; normal ACL handling should
215# remove the links a user is not allowed to click on.
216if ($aclerr) {
217 $page = HTML::Template->new(filename => "aclerror.tmpl");
218 $page->param(ipdbfunc => $aclmsg{$aclerr});
219}
220
221# Clean up IPDB globals, DB handle, etc.
222finish($ip_dbh);
223
224## Do all our printing here so we can generate errors and stick them into the slots in the templates.
225
226# can't do this yet, too many blowups
227#print "Content-type: text/html\n\n", $header->output;
228$page->param(webpath => $IPDB::webpath);
229print $utilbar->output;
230print $page->output;
231
232# include the admin tools link in the output?
233$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
234$footer->param(webpath => $IPDB::webpath);
235print $footer->output;
236
237# Just in case something waaaayyy down isn't in place
238# properly... we exit explicitly.
239exit 0;
240
241
242# Initial display: Show master blocks with total allocated subnets, total free subnets
243sub showSummary {
244 my $masterlist = listSummary($ip_dbh);
245 $page->param(masterlist => $masterlist);
246
247 $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
248} # showSummary
249
250
251# Display blocks immediately within a given parent
252sub showSubs {
253 # Which layout?
254 if ($IPDB::sublistlayout == 2) {
255
256 # 2-part layout; mixed containers and end-use allocations and free blocks.
257 # Containers have a second line for the subblock metadata.
258 # We need to load an alternate template for this case.
259 $page = HTML::Template->new(filename => "showsubs2.tmpl", loop_context_vars => 1, global_vars => 1);
260
261 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/));
262
263 my $sublist = listSubs($ip_dbh, parent => $webvar{parent});
264 $page->param(sublist => $sublist);
265
266 } else {
267
268 # 3-part layout; containers, end-use allocations, and free blocks
269
270 my $contlist = listContainers($ip_dbh, parent => $webvar{parent});
271 $page->param(contlist => $contlist);
272
273 my $alloclist = listAllocations($ip_dbh, parent => $webvar{parent});
274 $page->param(alloclist => $alloclist);
275
276 # only show "delete" button if we have no container or usage allocations
277 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/) && !(@$contlist || @$alloclist));
278
279 }
280
281 # Common elements
282 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
283
284##fixme: do we add a wrapper to not show the edit link for master blocks?
285#$page->param(editme => 1) unless $pinfo->{type} ne 'mm';
286
287 my $crumbs = getBreadCrumbs($ip_dbh, $pinfo->{parent_id});
288 my @rcrumbs = reverse (@$crumbs);
289 $utilbar->param(breadcrumb => \@rcrumbs);
290
291 $page->param(self_id => $webvar{parent});
292 $page->param(block => $pinfo->{block});
293 $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
294
295 my $flist = listFree($ip_dbh, parent => $webvar{parent});
296 $page->param(freelist => $flist);
297} # showSubs
298
299
300# List the IPs used in a pool
301sub showPool {
302
303 my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
304 my $cidr = new NetAddr::IP $poolinfo->{block};
305
306 # Tree navigation
307 my $crumbs = getBreadCrumbs($ip_dbh, $poolinfo->{parent_id});
308 my @rcrumbs = reverse (@$crumbs);
309 $utilbar->param(breadcrumb => \@rcrumbs);
310
311 $page->param(block => $cidr);
312 $page->param(netip => $cidr->addr);
313 $cidr++;
314 $page->param(gate => $cidr->addr);
315 $cidr--; $cidr--;
316 $page->param(bcast => $cidr->addr);
317 $page->param(mask => $cidr->mask);
318
319 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
320 $page->param(city => $poolinfo->{city});
321
322 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
323 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
324
325# probably have to add an "edit IP allocation" link here somewhere.
326
327 my $plist = listPool($ip_dbh, $webvar{pool});
328 # technically slightly more efficient to check the ACL in an if () once outside the foreach
329 foreach (@{$plist}) {
330 $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
331 }
332 $page->param(poolips => $plist);
333} # end showPool
334
335
336# Show "Add new allocation" page. Note that the actual page may
337# be one of two templates, and the lists come from the database.
338sub assignBlock {
339
340 if ($IPDBacl{$authuser} !~ /a/) {
341 $aclerr = 'addblock';
342 return;
343 }
344
345 # hack pthbttt eww
346 $webvar{parent} = 0 if !$webvar{parent};
347 $webvar{block} = '' if !$webvar{block};
348
349 $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
350
351 if ($webvar{fbid} || $webvar{fbtype}) {
352
353 # Common case, according to reported usage. Block to assign is specified.
354 my $block = new NetAddr::IP $webvar{block};
355
356 my ($rdns,$cached) = getBlockRDNS($ip_dbh, id => $webvar{parent}, type => $webvar{fbtype}, user => $authuser);
357 $page->param(rdns => $rdns) if $rdns;
358 $page->param(parent => $webvar{parent});
359 $page->param(fbid => $webvar{fbid});
360 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
361 $page->param(cached => $cached);
362
363 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
364 # seems reasonable that a new allocation would share a VRF with its parent
365 $page->param(pvrf => $pinfo->{vrf});
366
367 # Tree navigation
368 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
369 my @rcrumbs = reverse (@$crumbs);
370 $utilbar->param(breadcrumb => \@rcrumbs);
371
372 $webvar{fbtype} = '' if !$webvar{fbtype};
373 if ($webvar{fbtype} eq 'i') {
374 my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i');
375 $page->param(
376 fbip => 1,
377 block => $ipinfo->{block},
378 fbdisptype => $list_alloctypes{$ipinfo->{type}},
379 type => $ipinfo->{type},
380 allocfrom => $pinfo->{block},
381 );
382 } else {
383 # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
384 my $tlist = getTypeList($ip_dbh, 'n');
385 $tlist->[0]->{sel} = 1;
386 $page->param(typelist => $tlist, block => $block);
387 }
388
389 } else {
390
391 # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
392 my $mlist = getMasterList($ip_dbh, 'c');
393 $page->param(masterlist => $mlist);
394
395 my @pops;
396 foreach my $pop (@citylist) {
397 my %row = (pop => $pop);
398 push (@pops, \%row);
399 }
400 $page->param(pops => \@pops);
401
402 # get all standard alloctypes
403 my $tlist = getTypeList($ip_dbh, 'a');
404 $tlist->[0]->{sel} = 1;
405 $page->param(typelist => $tlist);
406 }
407
408 my @cities;
409 foreach my $city (@citylist) {
410 my %row = (city => $city);
411 push (@cities, \%row);
412 }
413 $page->param(citylist => \@cities);
414
415## node hack
416 my $nlist = getNodeList($ip_dbh);
417 $page->param(nodelist => $nlist);
418## end node hack
419
420 $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
421
422} # assignBlock
423
424
425# Take info on requested IP assignment and see what we can provide.
426sub confirmAssign {
427 if ($IPDBacl{$authuser} !~ /a/) {
428 $aclerr = 'addblock';
429 return;
430 }
431
432 my $cidr;
433 my $resv; # Reserved for expansion.
434 my $alloc_from;
435 my $fbid = $webvar{fbid};
436 my $p_id = $webvar{parent};
437
438 # Going to manually validate some items.
439 # custid and city are automagic.
440 return if !validateInput();
441
442 # make sure this is defined
443 $webvar{fbassign} = 'n' if !$webvar{fbassign};
444
445# Several different cases here.
446# Static IP vs netblock
447# + Different flavours of static IP
448# + Different flavours of netblock
449
450 if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
451 if (!$webvar{pop}) {
452 $page->param(err => "Please select a location/POP site to allocate from.");
453 return;
454 }
455 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
456 $page->param(staticip => 1);
457 $page->param(poollist => $plist) if $plist;
458 $cidr = "Single static IP";
459##fixme: need to handle "no available pools"
460
461 } else { # end show pool options
462
463 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
464
465 # Tree navigation
466 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
467 my @rcrumbs = reverse (@$crumbs);
468 $utilbar->param(breadcrumb => \@rcrumbs);
469
470 $cidr = new NetAddr::IP $webvar{block};
471 $alloc_from = new NetAddr::IP $webvar{allocfrom};
472 $webvar{maskbits} = $cidr->masklen;
473 # Some additional checks are needed for reserving free space
474 if ($webvar{reserve}) {
475 if ($cidr == $alloc_from) {
476# We could still squirm and fiddle to try to find a way to reserve space, but the storage model for
477# IPDB means that all continguous free space is kept in the smallest number of strict CIDR netblocks
478# possible. (In theory.) If the request and the freeblock are the same, it is theoretically impossible
479# to reserve an equivalent-sized block either ahead or behind the requested one, because the pair
480# together would never be a strict CIDR block.
481 $page->param(warning => "Can't reserve space for expansion; free block and requested allocation are the same.");
482 delete $webvar{reserve};
483 } else {
484 # Find which new free block will match the reqested block.
485 # Take the requested mask, shift by one
486 my $tmpmask = $webvar{maskbits};
487 $tmpmask--;
488 # find the subnets with that mask in the selected free block
489 my @pieces = $alloc_from->split($tmpmask);
490 foreach my $slice (@pieces) {
491 if ($slice->contains($cidr)) {
492 # For the subnet that contains the requested block, split that in two,
493 # and flag/cache the one that's not the requested block.
494 my @bits = $slice->split($webvar{maskbits});
495 if ($bits[0] == $cidr) {
496 $resv = $bits[1];
497 } else {
498 $resv = $bits[0];
499 }
500 }
501 }
502 }
503 } # reserve block check
504
505 } else { # done with direct freeblocks assignment
506
507 if (!$webvar{maskbits}) {
508 $page->param(err => "Please specify a CIDR mask length.");
509 return;
510 }
511
512##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
513 my $failmsg = "No suitable free block found.<br>\n";
514 if ($webvar{alloctype} eq 'rm') {
515 $failmsg .= "We do not have a free routeable block of that size.<br>\n".
516 "You will have to either route a set of smaller netblocks or a single smaller netblock.";
517 } else {
518 if ($webvar{alloctype} =~ /^.[pc]$/) {
519 $failmsg .= "You will have to route another superblock from one of the<br>\n".
520 "master blocks or chose a smaller block size for the pool.";
521 } else {
522 if (!$webvar{pop}) {
523 $page->param(err => 'Please select a POP to route the block from/through.');
524 return;
525 }
526 $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
527 "from one of the master blocks";
528 if ($webvar{reserve}) {
529 $failmsg .= ', choose a smaller blocksize, or uncheck "Reserve space for expansion".';
530 } else {
531 $failmsg .= " or chose a smaller blocksize.";
532 }
533 }
534 }
535
536 # if requesting extra space "reserved for expansion", we need to find a free
537 # block at least double the size of the request.
538 if ($webvar{reserve}) {
539 $webvar{maskbits}--;
540 }
541
542 ($fbid,$cidr,$p_id) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype},
543 $webvar{city}, $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
544 if (!$cidr) {
545 $page->param(err => $failmsg);
546 return;
547 }
548 $cidr = new NetAddr::IP $cidr;
549
550 $alloc_from = "$cidr";
551
552 # when autofinding a block to allocate from, use the first piece of the found
553 # block for the allocation, and the next piece for the "reserved for expansion".
554 if ($webvar{reserve}) {
555 # reset the mask to the real requested one, now that we've got a
556 # block large enough for the request plus reserve
557 $webvar{maskbits}++;
558 ($cidr,$resv) = $cidr->split($webvar{maskbits});
559 }
560
561 # If the block to be allocated is smaller than the one we found,
562 # figure out the "real" block to be allocated.
563 if ($cidr->masklen() ne $webvar{maskbits}) {
564 my $maskbits = $cidr->masklen();
565 my @subblocks;
566 while ($maskbits++ < $webvar{maskbits}) {
567 @subblocks = $cidr->split($maskbits);
568 }
569 $cidr = $subblocks[0];
570 }
571 } # check for freeblocks assignment or IPDB-controlled assignment
572
573 # Generate the IP list for the new allocation in case someone wants to set per-IP rDNS right away.
574 # We don't do this on the previous page because we don't know how big a block or even what IP range
575 # it's for (if following the "normal" allocation process)
576 if ($IPDBacl{$authuser} =~ /c/
577 && $cidr->masklen != $cidr->bits
578 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
579 && $webvar{alloctype} !~ /^.[dpi]/
580 # do we want to allow v6 at all?
581 #&& ! $cidr->{isv6}
582 ) {
583 my @list;
584 foreach my $ip (@{$cidr->splitref()}) {
585 my %row;
586 $row{r_ip} = $ip->addr;
587 $row{iphost} = '';
588 push @list, \%row;
589 }
590 $page->param(r_iplist => \@list);
591 # We don't use this here, because these IPs should already be bare.
592 # ... or should we be paranoid? Make it a config option?
593 #getRDNSbyIP($ip_dbh, type => $webvar{alloctype}, range => "$cidr", user => $authuser) );
594 }
595 } # if ($webvar{alloctype} =~ /^.i$/)
596
597## node hack
598 if ($webvar{node} && $webvar{node} ne '-') {
599 my $nodename = getNodeName($ip_dbh, $webvar{node});
600 $page->param(nodename => $nodename);
601 $page->param(nodeid => $webvar{node});
602 }
603## end node hack
604
605 # reserve for expansion
606 $page->param(reserve => $webvar{reserve});
607 # probably just preventing a little log noise doing this; could just set the param
608 # all the time since it won't be shown if the reserve param above isn't set.
609# if ($webvar{reserve}) {
610 $page->param(resvblock => $resv);
611# }
612
613 # Stick in the allocation data
614 $page->param(alloc_type => $webvar{alloctype});
615 $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
616 $page->param(alloc_from => $alloc_from);
617 $page->param(parent => $p_id);
618 $page->param(fbid => $fbid);
619 $page->param(cidr => $cidr);
620 $page->param(rdns => $webvar{rdns});
621 $page->param(vrf => $webvar{vrf});
622 $page->param(vlan => $webvar{vlan});
623 $page->param(city => $q->escapeHTML($webvar{city}));
624 $page->param(custid => $webvar{custid});
625 $page->param(circid => $q->escapeHTML($webvar{circid}));
626 $page->param(desc => $q->escapeHTML($webvar{desc}));
627
628##fixme: find a way to have the displayed copy have <br> substitutions
629# for newlines, and the <input> value have either encoded or bare newlines.
630# Also applies to privdata.
631 $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
632
633 # Check to see if user is allowed to do anything with sensitive data
634 my $privdata = '';
635 $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'))
636 if $IPDBacl{$authuser} =~ /s/;
637
638 # Yay! This now has it's very own little home.
639 $page->param(billinguser => $webvar{userid})
640 if $webvar{userid};
641
642##fixme: this is only needed iff confirm.tmpl and
643# confirmRemove.tmpl are merged (quite possible, just
644# a little tedious)
645 $page->param(action => "insert");
646
647} # end confirmAssign
648
649
650# Do the work of actually inserting a block in the database.
651sub insertAssign {
652 if ($IPDBacl{$authuser} !~ /a/) {
653 $aclerr = 'addblock';
654 return;
655 }
656 # Some things are done more than once.
657 return if !validateInput();
658
659 if (!defined($webvar{privdata})) {
660 $webvar{privdata} = '';
661 }
662
663 # split up some linked data for static IPs via guided allocation. needed for breadcrumbs lite.
664 ($webvar{alloc_from},$webvar{rdepth}) = split /,/, $webvar{alloc_from} if $webvar{alloc_from} =~ /,/;
665
666 # $code is "success" vs "failure", $msg contains OK for a
667 # successful netblock allocation, the IP allocated for static
668 # IP, or the error message if an error occurred.
669
670##fixme: consider just passing \%webvar to allocateBlock()?
671 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
672 my %iprev;
673 foreach (keys %webvar) {
674 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
675 }
676
677 # Easier to see and cosmetically fiddle the list like this
678 my %insert_args = (
679 cidr => $webvar{fullcidr},
680 fbid => $webvar{fbid},
681 reserve => $webvar{reserve},
682 parent => $webvar{parent},
683 custid => $webvar{custid},
684 type => $webvar{alloctype},
685 city => $webvar{city},
686 desc => $webvar{desc},
687 notes => $webvar{notes},
688 circid => $webvar{circid},
689 privdata => $webvar{privdata},
690 nodeid => $webvar{node},
691 rdns => $webvar{rdns},
692 vrf => $webvar{vrf},
693 vlan => $webvar{vlan},
694 user => $authuser,
695 );
696
697 my ($code,$msg) = allocateBlock($ip_dbh, %insert_args, iprev => \%iprev);
698
699 if ($code eq 'OK') {
700 if ($webvar{alloctype} =~ /^.i$/) {
701 $msg =~ s|/32||;
702 $page->param(staticip => $msg);
703 $page->param(custid => $webvar{custid});
704 $page->param(parent => $webvar{parent}, pool => $webvar{alloc_from});
705 $page->param(billinguser => $webvar{billinguser});
706 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
707 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
708 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
709 } else {
710 my $netblock = new NetAddr::IP $webvar{fullcidr};
711 $page->param(fullcidr => $webvar{fullcidr});
712 $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
713 $page->param(custid => $webvar{custid});
714
715 # breadcrumbs lite! provide at least a link to the parent of the block we just allocated.
716 my $binfo = getBlockData($ip_dbh, $webvar{parent});
717 $page->param(parentid => $webvar{parent});
718 $page->param(parentblock => $binfo->{block});
719
720 # Full breadcrumbs
721 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
722 my @rcrumbs = reverse (@$crumbs);
723 $utilbar->param(breadcrumb => \@rcrumbs);
724
725 if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
726 $page->param(billinguser => $webvar{billinguser});
727 $page->param(custid => $webvar{custid});
728 $page->param(netaddr => $netblock->addr);
729 $page->param(masklen => $netblock->masklen);
730 }
731 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
732 "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
733 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
734 }
735 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
736 "'$webvar{alloctype}' ($msg)";
737 } else {
738 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
739 "'$webvar{alloctype}' by $authuser failed: '$msg'";
740 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
741 " failed:<br>\n$msg\n");
742 }
743
744} # end insertAssign()
745
746
747# Does some basic checks on common input data to make sure nothing
748# *really* weird gets in to the database through this script.
749# Does NOT do complete input validation!!!
750sub validateInput {
751 if ($webvar{city} eq '-') {
752 $page->param(err => 'Please choose a city');
753 return;
754 }
755
756 # Alloctype check.
757 chomp $webvar{alloctype};
758 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
759 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
760 # managing to call things in such a way as to cause this deserves a cryptic error.
761 $page->param(err => 'Invalid alloctype');
762 return;
763 }
764
765 # CustID check
766 # We have different handling for customer allocations and "internal" or "our" allocations
767 if ($def_custids{$webvar{alloctype}} eq '') {
768 if (!$webvar{custid}) {
769 $page->param(err => 'Please enter a customer ID.');
770 return;
771 }
772 # Crosscheck with billing.
773 my $status = CustIDCK->custid_exist($webvar{custid});
774 if ($CustIDCK::Error) {
775 $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
776 return;
777 }
778 if (!$status) {
779 $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
780 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
781 "non-customer assignments.");
782 return;
783 }
784# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
785 } else {
786 # New! Improved! And now Loaded From The Database!!
787 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
788 $webvar{custid} = $def_custids{$webvar{alloctype}};
789 }
790 }
791
792## hmmm.... is this even useful?
793if (0) {
794 # Check POP location
795 my $flag;
796 if ($webvar{alloctype} eq 'rm') {
797 $flag = 'for a routed netblock';
798 foreach (@poplist) {
799 if (/^$webvar{city}$/) {
800 $flag = 'n';
801 last;
802 }
803 }
804 } else {
805 $flag = 'n';
806##fixme: hook to force-set POP or city on certain alloctypes
807# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
808 if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
809 $flag = 'to route the block from/through';
810 }
811 }
812
813 # if the alloctype has a restricted city/POP list as determined above,
814 # and the reqested city/POP does not match that list, complain
815 if ($flag ne 'n') {
816 $page->param(err => "Please choose a valid POP location $flag. Valid ".
817 "POP locations are currently:<br>\n".join (" - ", @poplist));
818 return;
819 }
820}
821
822 # VRF. Not a full validity check, just a basic sanity check.
823 if ($webvar{vrf}) {
824 # Trim leading and trailing whitespace first
825 $webvar{vrf} =~ s/^\s+//;
826 $webvar{vrf} =~ s/\s+$//;
827 if ($webvar{vrf} !~ /^[\w\d_.-]{1,32}$/) {
828 $page->param(err => "VRF values may only contain alphanumerics, and may not be more than 32 characters");
829 return;
830 }
831 }
832
833 # VLAN. Should we allow/use VLAN names, or just the numeric ID?
834 if ($webvar{vlan}) {
835 # Trim leading and trailing whitespace first
836 $webvar{vlan} =~ s/^\s+//;
837 $webvar{vlan} =~ s/\s+$//;
838 # ... ve make it ze configurable thingy!
839 if ($IPDB::numeric_vlan) {
840 if ($webvar{vlan} !~ /^\d+$/) {
841 $page->param(err => "VLANs must be numeric");
842 return;
843 }
844 } else {
845 if ($webvar{vlan} !~ /^[\w\d_.-]+$/) {
846 $page->param(err => "VLANs must be alphanumeric");
847 return;
848 }
849 }
850 }
851
852 return 'OK';
853} # end validateInput
854
855
856# Displays details of a specific allocation in a form
857# Allows update/delete
858# action=edit
859sub edit {
860
861 # snag block info from db
862 my $blockinfo = getBlockData($ip_dbh, $webvar{id}, $webvar{basetype});
863 $page->param(id => $webvar{id});
864 $page->param(basetype => $webvar{basetype});
865
866 # Tree navigation
867 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
868 my @rcrumbs = reverse (@$crumbs);
869 $utilbar->param(breadcrumb => \@rcrumbs);
870
871 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
872 $blockinfo->{type} =~ s/\s//;
873
874 my $cached;
875 # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
876 ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser);
877 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
878 $page->param(cached => $cached);
879
880 my $cidr = new NetAddr::IP $blockinfo->{block};
881 # Limit the per-IP rDNS list based on CIDR length; larger ones just take up too much space.
882 # Also, don't show on IP pools; the individual IPs will have a space for rDNS
883 # Don't show on single IPs; these use the "pattern" field
884 if ($IPDBacl{$authuser} =~ /c/
885 && $cidr->masklen != $cidr->bits
886 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
887 && $blockinfo->{type} !~ /^.[dpi]/
888 # do we want to allow v6 at all?
889 #&& ! $cidr->{isv6}
890 ) {
891 $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type},
892 range => $blockinfo->{block}, user => $authuser) );
893 }
894
895 # consider extending this to show time as well as date
896 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
897 $page->param(lastmod => $lastmod);
898# $page->param(lastmod => $blockinfo->{lastmod});
899
900 $page->param(block => $blockinfo->{block});
901 $page->param(city => $blockinfo->{city});
902 $page->param(custid => $blockinfo->{custid});
903
904##fixme The check here should be built from the database
905# Need to expand to support pool types too
906 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
907 $page->param(changetype => 1);
908 $page->param(alloctype => [
909 { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
910 { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
911 { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
912 { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
913 { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
914 { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
915 { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
916 ]
917 );
918 } else {
919 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
920 $page->param(type => $blockinfo->{type});
921 }
922
923## node hack
924 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block});
925# $page->param(havenodeid => $nodeid);
926 $page->param(nodename => $nodename);
927
928##fixme: this whole hack needs cleanup and generalization for all alloctypes
929##fixme: arguably a bug that presence of a nodeid implies it can be changed..
930 if ($IPDBacl{$authuser} =~ /c/) {
931 my $nlist = getNodeList($ip_dbh);
932 if ($nodeid) {
933 foreach (@{$nlist}) {
934 $$_{selme} = ($$_{node_id} == $nodeid);
935 }
936 }
937 $page->param(nodelist => $nlist);
938 }
939## end node hack
940
941 $page->param(rdns => $blockinfo->{rdns});
942 $page->param(vrf => $blockinfo->{vrf});
943 $page->param(vlan => $blockinfo->{vlan});
944
945 # Reserved-for-expansion
946 $page->param(reserve => $blockinfo->{reserve});
947 $page->param(reserve_id => $blockinfo->{reserve_id});
948 my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
949 $page->param(newblock => $newblock);
950
951 # not happy with the upside-down logic, but...
952 $page->param(swipable => $blockinfo->{type} !~ /.i/);
953 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
954
955 $page->param(circid => $blockinfo->{circuitid});
956 $page->param(desc => $blockinfo->{description});
957 $page->param(notes => $blockinfo->{notes});
958
959 # Check to see if we can display sensitive data
960 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
961 $page->param(privdata => $blockinfo->{privdata});
962
963 # ACL trickery - these two template booleans control the presence of all form/input tags
964 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
965 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
966
967} # edit()
968
969
970# Stuff new info about a block into the db
971# action=update
972sub update {
973 if ($IPDBacl{$authuser} !~ /c/) {
974 $aclerr = 'updateblock';
975 return;
976 }
977
978 # Make sure incoming data is in correct format - custID among other things.
979 return if !validateInput;
980
981 $webvar{swip} = 'n' if !$webvar{swip};
982
983 my %updargs = (
984 custid => $webvar{custid},
985 city => $webvar{city},
986 description => $webvar{desc},
987 notes => $webvar{notes},
988 circuitid => $webvar{circid},
989 block => $webvar{block},
990 type => $webvar{alloctype},
991 swip => $webvar{swip},
992 rdns => $webvar{rdns},
993 vrf => $webvar{vrf},
994 vlan => $webvar{vlan},
995 user => $authuser,
996 );
997
998 # Semioptional values
999 $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
1000 $updargs{node} = $webvar{node} if $webvar{node};
1001
1002 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
1003 my %iprev;
1004 foreach (keys %webvar) {
1005 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
1006 }
1007
1008 # Merge with reserved freeblock
1009 $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme};
1010
1011 my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
1012
1013 if ($code eq 'FAIL') {
1014 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
1015 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
1016 return;
1017 }
1018
1019 # If we get here, the operation succeeded.
1020 syslog "notice", "$authuser updated $webvar{block}";
1021##fixme: log details of the change? old way is in the .debug stream anyway.
1022##fixme: need to wedge something in to allow "update:field" notifications
1023## hmm. how to tell what changed? O_o
1024mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1025 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
1026
1027## node hack
1028 if ($webvar{node} && $webvar{node} ne '-') {
1029 my $nodename = getNodeName($ip_dbh, $webvar{node});
1030 $page->param(nodename => $nodename);
1031 }
1032## end node hack
1033
1034 # Link back to browse-routed or list-pool page on "Update complete" page.
1035 my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1036 my $pblock = getBlockData($ip_dbh, $binfo->{parent_id});
1037 $page->param(backid => $binfo->{parent_id});
1038 $page->param(backblock => $pblock->{block});
1039 $page->param(backpool => ($webvar{basetype} eq 'i'));
1040
1041 # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
1042 # because otherwise we can't convert \n to <br>. *sigh*
1043 $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
1044 $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
1045 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
1046 $webvar{privdata} =~ s/\n/<br>\n/;
1047
1048 $page->param(cidr => $binfo->{block});
1049 $page->param(rdns => $webvar{rdns});
1050 $page->param(city => $webvar{city});
1051 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
1052 $page->param(custid => $webvar{custid});
1053 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
1054 $page->param(circid => $webvar{circid});
1055 $page->param(desc => $webvar{desc});
1056 $page->param(notes => $webvar{notes});
1057 $page->param(privdata => $webvar{privdata})
1058 if $IPDBacl{$authuser} =~ /s/;
1059
1060} # update()
1061
1062
1063# Delete an allocation.
1064sub remove {
1065 if ($IPDBacl{$authuser} !~ /d/) {
1066 $aclerr = 'delblock';
1067 return;
1068 }
1069
1070 # Serves'em right for getting here...
1071 if (!defined($webvar{block})) {
1072 $page->param(err => "Can't delete a block that doesn't exist");
1073 return;
1074 }
1075
1076 my $blockdata;
1077 $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1078
1079 if ($blockdata->{parent_id} == 0) { # $webvar{alloctype} eq 'mm'
1080 $blockdata->{city} = "N/A";
1081 $blockdata->{custid} = "N/A";
1082 $blockdata->{circuitid} = "N/A";
1083 $blockdata->{description} = "N/A";
1084 $blockdata->{notes} = "N/A";
1085 $blockdata->{privdata} = "N/A";
1086 } # end cases for different alloctypes
1087
1088 $page->param(blockid => $webvar{block});
1089 $page->param(basetype => $webvar{basetype});
1090
1091 $page->param(block => $blockdata->{block});
1092 $page->param(rdns => $blockdata->{rdns});
1093
1094 # maybe need to apply more magic here?
1095 # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
1096 # -> all real blocks (nb: pool IPs need extra handling)
1097 # -> NOC/private-IP (how to ID?)
1098 # -> anything with a pattern matching $IPDB::domain?
1099 if ($blockdata->{type} !~ /^.i$/) {
1100 $page->param(autodel => 1);
1101 }
1102
1103 $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
1104 $page->param(city => $blockdata->{city});
1105 $page->param(custid => $blockdata->{custid});
1106 $page->param(circid => $blockdata->{circuitid});
1107 $page->param(desc => $blockdata->{description});
1108 $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
1109 $blockdata->{notes} =~ s/\n/<br>\n/;
1110 $page->param(notes => $blockdata->{notes});
1111 $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
1112 $blockdata->{privdata} = '&nbsp;' if !$blockdata->{privdata};
1113 $blockdata->{privdata} =~ s/\n/<br>\n/;
1114 $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
1115 $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
1116
1117} # end remove()
1118
1119
1120# Delete an allocation. Return it to the freeblocks table; munge
1121# data as necessary to keep as few records as possible in freeblocks
1122# to prevent weirdness when allocating blocks later.
1123# Remove IPs from pool listing if necessary
1124sub finalDelete {
1125 if ($IPDBacl{$authuser} !~ /d/) {
1126 $aclerr = 'delblock';
1127 return;
1128 }
1129
1130 # need to retrieve block data before deleting so we can notify on that
1131 my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1132 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b');
1133
1134 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser);
1135
1136 $page->param(block => $blockinfo->{block});
1137 $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}}));
1138 $page->param(delparent_id => $blockinfo->{parent_id});# if $webvar{rdepth};
1139 if ($pinfo) {
1140 $page->param(delparent => $pinfo->{block});
1141 $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}}));
1142 }
1143 $page->param(returnpool => ($webvar{basetype} eq 'i') );
1144 if ($code =~ /^WARN(POOL|MERGE)/) {
1145 my ($pid,$pcidr) = split /,/, $msg;
1146 my $real_pinfo = getBlockData($ip_dbh, $pid, 'b');
1147 $page->param(parent_id => $pid);
1148 $page->param(parent => $pcidr);
1149 $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}}));
1150 $page->param(mergeip => $code eq 'WARNPOOL');
1151 }
1152 if ($code eq 'WARN') {
1153 $msg =~ s/\n/<br>\n/g;
1154 $page->param(genwarn => $msg);
1155 }
1156 if ($code eq 'OK' || $code =~ /^WARN/) {
1157 syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock $webvar{block} ".
1158 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
1159 mailNotify($ip_dbh, 'da', "REMOVED: ".$disp_alloctypes{$blockinfo->{type}}." $webvar{block}",
1160 $disp_alloctypes{$blockinfo->{type}}." $webvar{block} deallocated by $authuser\n".
1161 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
1162 "\nDescription: ".$blockinfo->{description}."\n");
1163 } else {
1164 $page->param(failmsg => $msg);
1165 if ($webvar{alloctype} =~ /^.i$/) {
1166 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1167 } else {
1168 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
1169 $page->param(netblock => 1);
1170 }
1171 }
1172
1173} # finalDelete
Note: See TracBrowser for help on using the repository browser.