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

Last change on this file since 810 was 810, checked in by Kris Deugau, 8 years ago

/trunk

Add "Add VRF" page. See #54.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 55.1 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3###
4# SVN revision info
5# $Date: 2016-03-08 18:38:18 +0000 (Tue, 08 Mar 2016) $
6# SVN revision $Rev: 810 $
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;
73my @templatepath = [ "localtemplates", "templates" ];
74
75my $header = HTML::Template->new(filename => "header.tmpl", path => @templatepath);
76my $footer = HTML::Template->new(filename => "footer.tmpl", path => @templatepath);
77my $utilbar = HTML::Template->new(filename => "utilbar.tmpl", loop_context_vars => 1, global_vars => 1,
78 path => @templatepath);
79
80print "Content-type: text/html\n\n";
81
82$header->param(version => $IPDB::VERSION);
83$header->param(addperm => $IPDBacl{$authuser} =~ /a/);
84$header->param(webpath => $IPDB::webpath);
85
86$utilbar->param(webpath => $IPDB::webpath);
87
88print $header->output;
89
90##fixme: whine and complain when the user is not present in the ACL hash above
91
92#main()
93my $aclerr;
94
95if(!defined($webvar{action})) {
96 $webvar{action} = "index"; #shuts up the warnings.
97}
98
99my $page;
100if (-e "$ENV{HTML_TEMPLATE_ROOT}/templates/$webvar{action}.tmpl") {
101 $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1,
102 path => @templatepath);
103} else {
104 $page = HTML::Template->new(filename => "dunno.tmpl", die_on_bad_params => 0,
105 path => @templatepath);
106}
107
108if($webvar{action} eq 'index') {
109 showSummary();
110} elsif ($webvar{action} eq 'showvrf') {
111 showVRF();
112
113} elsif ($webvar{action} eq 'addvrf') {
114 if ($IPDBacl{$authuser} !~ /s/) {
115 $aclerr = 'addvrf';
116 }
117
118 # Retrieve the list of DNS locations if we've got a place to grab them from
119 if ($IPDB::rpc_url) {
120 my %rpcargs = (
121 rpcuser => $authuser,
122 group => 1, # bleh
123 defloc => '',
124 );
125 my $result = IPDB::_rpc('getLocDropdown', %rpcargs);
126 $page->param(loclist => $result);
127 }
128
129} elsif ($webvar{action} eq 'addmaster') {
130 if ($IPDBacl{$authuser} !~ /a/) {
131 $aclerr = 'addmaster';
132 }
133
134 # Retrieve the list of DNS locations if we've got a place to grab them from
135 if ($IPDB::rpc_url) {
136 my %rpcargs = (
137 rpcuser => $authuser,
138 group => 1, # bleh
139 defloc => '',
140 );
141 my $result = IPDB::_rpc('getLocDropdown', %rpcargs);
142 $page->param(loclist => $result);
143 }
144
145} elsif ($webvar{action} eq 'newmaster') {
146
147 if ($IPDBacl{$authuser} !~ /a/) {
148 $aclerr = 'addmaster';
149 } else {
150 my $cidr = new NetAddr::IP $webvar{cidr};
151 $page->param(cidr => "$cidr");
152
153 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr}, (vrf => $webvar{vrf}, rdns => $webvar{rdns},
154 rwhois => $webvar{rwhois}, defloc => $webvar{loc}, user => $authuser) );
155
156 if ($code eq 'FAIL') {
157 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
158 $page->param(err => $msg);
159 } else {
160 $page->param(parent => $msg);
161 if ($code eq 'WARN') {
162 $IPDB::errstr =~ s/\n\n/<br>\n/g;
163 $IPDB::errstr =~ s/:\n/:<br>\n/g;
164 $page->param(warn => $IPDB::errstr);
165 }
166 syslog "info", "$authuser added master block $webvar{cidr}";
167 }
168
169 } # ACL check
170
171} # end add new master
172
173elsif ($webvar{action} eq 'showsubs') {
174 showSubs();
175}
176
177elsif($webvar{action} eq 'listpool') {
178 showPool();
179}
180
181# Not modified or added; just shuffled
182elsif($webvar{action} eq 'assign') {
183 assignBlock();
184}
185elsif($webvar{action} eq 'confirm') {
186 confirmAssign();
187}
188elsif($webvar{action} eq 'insert') {
189 insertAssign();
190}
191elsif($webvar{action} eq 'edit') {
192 edit();
193}
194elsif($webvar{action} eq 'update') {
195 update();
196}
197elsif($webvar{action} eq 'split') {
198 prepSplit();
199}
200elsif($webvar{action} eq 'dosplit') {
201 doSplit();
202}
203elsif($webvar{action} eq 'merge') {
204 prepMerge();
205}
206elsif($webvar{action} eq 'confmerge') {
207 confMerge();
208}
209elsif($webvar{action} eq 'domerge') {
210 doMerge();
211}
212elsif($webvar{action} eq 'delete') {
213 remove();
214}
215elsif($webvar{action} eq 'finaldelete') {
216 finalDelete();
217}
218elsif ($webvar{action} eq 'nodesearch') {
219 my $nodelist = getNodeList($ip_dbh);
220 $page->param(nodelist => $nodelist);
221}
222
223# DB failure. Can't do much here, really.
224elsif ($webvar{action} eq 'dberr') {
225 $page->param(errmsg => $errstr);
226}
227
228# Default is an error. It shouldn't be possible to get here unless you're
229# randomly feeding in values for webvar{action}.
230else {
231 my $rnd = rand 500;
232 my $boing = sprintf("%.2f", rand 500);
233 my @excuses = (
234 "Aether cloudy. Ask again later about $webvar{action}.",
235 "The gods are unhappy with your sacrificial $webvar{action}.",
236 "Because one of $webvar{action}'s legs are both the same",
237 "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!",
238 "Hey, man, you've had your free $webvar{action}. Next one's gonna... <i>cost</i>....",
239 "I ain't done $webvar{action}",
240 "Oooo, look! A flying $webvar{action}!",
241 "$webvar{action} too evil, avoiding.",
242 "Rocks fall, $webvar{action} dies.",
243 "Bit bucket must be emptied before I can $webvar{action}..."
244 );
245 $page->param(dunno => $excuses[$rnd/50.0]);
246}
247## Finally! Done with that NASTY "case" emulation!
248
249
250# Switch to a different template if we've tripped on an ACL error.
251# Note that this should only be exercised in development, when
252# deeplinked, or when being attacked; normal ACL handling should
253# remove the links a user is not allowed to click on.
254if ($aclerr) {
255 $page = HTML::Template->new(filename => "aclerror.tmpl", path => @templatepath);
256 $page->param(ipdbfunc => $aclmsg{$aclerr});
257}
258
259# Clean up IPDB globals, DB handle, etc.
260finish($ip_dbh);
261
262## Do all our printing here so we can generate errors and stick them into the slots in the templates.
263
264# can't do this yet, too many blowups
265#print "Content-type: text/html\n\n", $header->output;
266$page->param(webpath => $IPDB::webpath);
267print $utilbar->output;
268print $page->output;
269
270# include the admin tools link in the output?
271$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
272$footer->param(webpath => $IPDB::webpath);
273print $footer->output;
274
275# Just in case something waaaayyy down isn't in place
276# properly... we exit explicitly.
277exit 0;
278
279
280# Initial display: Show list of VRFs
281sub showSummary {
282 my $vrflist = listVRF($ip_dbh);
283 $page->param(vrflist => $vrflist);
284
285 # Only systems/network should be allowed to add VRFs - or maybe higher?
286 $page->param(addvrf => ($IPDBacl{$authuser} =~ /s/) );
287} # showSummary
288
289
290# Show IP blocks in a VRF
291sub showVRF {
292 my $masterlist = listSummary($ip_dbh, $webvar{vrf});
293 $page->param(vrf => $webvar{vrf});
294 $page->param(masterlist => $masterlist);
295
296 # we don't have a netblock; pass 0 for the block ID
297 # Tree navigation
298 my $crumbs = getBreadCrumbs($ip_dbh, 0, $webvar{vrf});
299 my @rcrumbs = reverse (@$crumbs);
300 $utilbar->param(breadcrumb => \@rcrumbs);
301
302 $page->param(addmaster => ($IPDBacl{$authuser} =~ /s/) );
303} # showVRF
304
305
306# Display blocks immediately within a given parent
307sub showSubs {
308 # Which layout?
309 if ($IPDB::sublistlayout == 2) {
310
311 # 2-part layout; mixed containers and end-use allocations and free blocks.
312 # Containers have a second line for the subblock metadata.
313 # We need to load an alternate template for this case.
314 $page = HTML::Template->new(filename => "showsubs2.tmpl", loop_context_vars => 1, global_vars => 1,
315 path => @templatepath);
316
317 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/));
318
319 my $sublist = listSubs($ip_dbh, parent => $webvar{parent});
320 $page->param(sublist => $sublist);
321
322 } else {
323
324 # 3-part layout; containers, end-use allocations, and free blocks
325
326 my $contlist = listContainers($ip_dbh, parent => $webvar{parent});
327 $page->param(contlist => $contlist);
328
329 my $alloclist = listAllocations($ip_dbh, parent => $webvar{parent});
330 $page->param(alloclist => $alloclist);
331
332 # only show "delete" button if we have no container or usage allocations
333 $page->param(maydel => ($IPDBacl{$authuser} =~ /d/) && !(@$contlist || @$alloclist));
334
335 }
336
337 # Common elements
338 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
339
340##fixme: do we add a wrapper to not show the edit link for master blocks?
341#$page->param(editme => 1) unless $pinfo->{type} ne 'mm';
342
343 my $crumbs = getBreadCrumbs($ip_dbh, $pinfo->{parent_id}, $pinfo->{vrf});
344 my @rcrumbs = reverse (@$crumbs);
345 $utilbar->param(breadcrumb => \@rcrumbs);
346
347 $page->param(self_id => $webvar{parent});
348 $page->param(block => $pinfo->{block});
349 $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
350
351 my $flist = listFree($ip_dbh, parent => $webvar{parent});
352 $page->param(freelist => $flist);
353} # showSubs
354
355
356# List the IPs used in a pool
357sub showPool {
358
359 my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
360 my $cidr = new NetAddr::IP $poolinfo->{block};
361 $page->param(vlan => $poolinfo->{vlan});
362
363 # Tree navigation
364 my $crumbs = getBreadCrumbs($ip_dbh, $poolinfo->{parent_id});
365 my @rcrumbs = reverse (@$crumbs);
366 $utilbar->param(breadcrumb => \@rcrumbs);
367
368 $page->param(block => $cidr);
369 $page->param(netip => $cidr->addr);
370 $cidr++;
371 $page->param(gate => $cidr->addr);
372 $cidr--; $cidr--;
373 $page->param(bcast => $cidr->addr);
374 $page->param(mask => $cidr->mask);
375
376 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
377 $page->param(city => $poolinfo->{city});
378
379 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
380 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
381
382# probably have to add an "edit IP allocation" link here somewhere.
383
384 my $plist = listPool($ip_dbh, $webvar{pool});
385 # technically slightly more efficient to check the ACL in an if () once outside the foreach
386 foreach (@{$plist}) {
387 $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
388 }
389 $page->param(poolips => $plist);
390} # end showPool
391
392
393# Show "Add new allocation" page. Note that the actual page may
394# be one of two templates, and the lists come from the database.
395sub assignBlock {
396
397 if ($IPDBacl{$authuser} !~ /a/) {
398 $aclerr = 'addblock';
399 return;
400 }
401
402 # hack pthbttt eww
403 $webvar{parent} = 0 if !$webvar{parent};
404 $webvar{block} = '' if !$webvar{block};
405
406 $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
407
408 if ($webvar{fbid} || $webvar{fbtype}) {
409
410 # Common case, according to reported usage. Block to assign is specified.
411 my $block = new NetAddr::IP $webvar{block};
412
413 my ($rdns,$cached) = getBlockRDNS($ip_dbh, id => $webvar{parent}, type => $webvar{fbtype}, user => $authuser);
414 $page->param(rdns => $rdns) if $rdns;
415 $page->param(parent => $webvar{parent});
416 $page->param(fbid => $webvar{fbid});
417 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
418 $page->param(cached => $cached);
419
420 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
421 # seems reasonable that a new allocation would share a VRF with its parent
422 $page->param(pvrf => $pinfo->{vrf});
423
424 # Tree navigation
425 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
426 my @rcrumbs = reverse (@$crumbs);
427 $utilbar->param(breadcrumb => \@rcrumbs);
428
429 $webvar{fbtype} = '' if !$webvar{fbtype};
430 if ($webvar{fbtype} eq 'i') {
431 my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i');
432 $page->param(
433 fbip => 1,
434 block => $ipinfo->{block},
435 fbdisptype => $list_alloctypes{$ipinfo->{type}},
436 type => $ipinfo->{type},
437 allocfrom => $pinfo->{block},
438 );
439 } else {
440 # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
441 my $tlist = getTypeList($ip_dbh, 'n');
442 $tlist->[0]->{sel} = 1;
443 $page->param(typelist => $tlist, block => $block);
444 }
445
446 } else {
447
448 # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
449 my $mlist = getMasterList($ip_dbh, 'c');
450 $page->param(masterlist => $mlist);
451
452 my @pops;
453 foreach my $pop (@citylist) {
454 my %row = (pop => $pop);
455 push (@pops, \%row);
456 }
457 $page->param(pops => \@pops);
458
459 # get all standard alloctypes
460 my $tlist = getTypeList($ip_dbh, 'a');
461 $tlist->[0]->{sel} = 1;
462 $page->param(typelist => $tlist);
463 }
464
465 my @cities;
466 foreach my $city (@citylist) {
467 my %row = (city => $city);
468 push (@cities, \%row);
469 }
470 $page->param(citylist => \@cities);
471
472## node hack
473 my $nlist = getNodeList($ip_dbh);
474 $page->param(nodelist => $nlist);
475## end node hack
476
477 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
478
479} # assignBlock
480
481
482# Take info on requested IP assignment and see what we can provide.
483sub confirmAssign {
484 if ($IPDBacl{$authuser} !~ /a/) {
485 $aclerr = 'addblock';
486 return;
487 }
488
489 my $cidr;
490 my $resv; # Reserved for expansion.
491 my $alloc_from;
492 my $fbid = $webvar{fbid};
493 my $p_id = $webvar{parent};
494
495 # Going to manually validate some items.
496 # custid and city are automagic.
497 return if !validateInput();
498
499 # make sure this is defined
500 $webvar{fbassign} = 'n' if !$webvar{fbassign};
501
502# Several different cases here.
503# Static IP vs netblock
504# + Different flavours of static IP
505# + Different flavours of netblock
506
507 if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
508 if (!$webvar{pop}) {
509 $page->param(err => "Please select a location/POP site to allocate from.");
510 return;
511 }
512 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
513 $page->param(staticip => 1);
514 $page->param(poollist => $plist) if $plist;
515 $cidr = "Single static IP";
516##fixme: need to handle "no available pools"
517
518 } else { # end show pool options
519
520 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
521
522 # Tree navigation
523 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
524 my @rcrumbs = reverse (@$crumbs);
525 $utilbar->param(breadcrumb => \@rcrumbs);
526
527 $cidr = new NetAddr::IP $webvar{block};
528 $alloc_from = new NetAddr::IP $webvar{allocfrom};
529 $webvar{maskbits} = $cidr->masklen;
530 # Some additional checks are needed for reserving free space
531 if ($webvar{reserve}) {
532 if ($cidr == $alloc_from) {
533# We could still squirm and fiddle to try to find a way to reserve space, but the storage model for
534# IPDB means that all continguous free space is kept in the smallest number of strict CIDR netblocks
535# possible. (In theory.) If the request and the freeblock are the same, it is theoretically impossible
536# to reserve an equivalent-sized block either ahead or behind the requested one, because the pair
537# together would never be a strict CIDR block.
538 $page->param(warning => "Can't reserve space for expansion; free block and requested allocation are the same.");
539 delete $webvar{reserve};
540 } else {
541 # Find which new free block will match the reqested block.
542 # Take the requested mask, shift by one
543 my $tmpmask = $webvar{maskbits};
544 $tmpmask--;
545 # find the subnets with that mask in the selected free block
546 my @pieces = $alloc_from->split($tmpmask);
547 foreach my $slice (@pieces) {
548 if ($slice->contains($cidr)) {
549 # For the subnet that contains the requested block, split that in two,
550 # and flag/cache the one that's not the requested block.
551 my @bits = $slice->split($webvar{maskbits});
552 if ($bits[0] == $cidr) {
553 $resv = $bits[1];
554 } else {
555 $resv = $bits[0];
556 }
557 }
558 }
559 }
560 } # reserve block check
561
562 } else { # done with direct freeblocks assignment
563
564 if (!$webvar{maskbits}) {
565 $page->param(err => "Please specify a CIDR mask length.");
566 return;
567 }
568
569##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
570 my $failmsg = "No suitable free block found.<br>\n";
571 if ($webvar{alloctype} eq 'rm') {
572 $failmsg .= "We do not have a free routeable block of that size.<br>\n".
573 "You will have to either route a set of smaller netblocks or a single smaller netblock.";
574 } else {
575 if ($webvar{alloctype} =~ /^.[pc]$/) {
576 $failmsg .= "You will have to route another superblock from one of the<br>\n".
577 "master blocks or chose a smaller block size for the pool.";
578 } else {
579 if (!$webvar{pop}) {
580 $page->param(err => 'Please select a POP to route the block from/through.');
581 return;
582 }
583 $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
584 "from one of the master blocks";
585 if ($webvar{reserve}) {
586 $failmsg .= ', choose a smaller blocksize, or uncheck "Reserve space for expansion".';
587 } else {
588 $failmsg .= " or chose a smaller blocksize.";
589 }
590 }
591 }
592
593 # if requesting extra space "reserved for expansion", we need to find a free
594 # block at least double the size of the request.
595 if ($webvar{reserve}) {
596 $webvar{maskbits}--;
597 }
598
599 ($fbid,$cidr,$p_id) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype},
600 $webvar{city}, $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
601 if (!$cidr) {
602 $page->param(err => $failmsg);
603 return;
604 }
605 $cidr = new NetAddr::IP $cidr;
606
607 $alloc_from = "$cidr";
608
609 # when autofinding a block to allocate from, use the first piece of the found
610 # block for the allocation, and the next piece for the "reserved for expansion".
611 if ($webvar{reserve}) {
612 # reset the mask to the real requested one, now that we've got a
613 # block large enough for the request plus reserve
614 $webvar{maskbits}++;
615 ($cidr,$resv) = $cidr->split($webvar{maskbits});
616 }
617
618 # If the block to be allocated is smaller than the one we found,
619 # figure out the "real" block to be allocated.
620 if ($cidr->masklen() ne $webvar{maskbits}) {
621 my $maskbits = $cidr->masklen();
622 my @subblocks;
623 while ($maskbits++ < $webvar{maskbits}) {
624 @subblocks = $cidr->split($maskbits);
625 }
626 $cidr = $subblocks[0];
627 }
628 } # check for freeblocks assignment or IPDB-controlled assignment
629
630 # Generate the IP list for the new allocation in case someone wants to set per-IP rDNS right away.
631 # We don't do this on the previous page because we don't know how big a block or even what IP range
632 # it's for (if following the "normal" allocation process)
633 if ($IPDBacl{$authuser} =~ /c/
634 && $cidr->masklen != $cidr->bits
635 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
636 # config flag for "all block types" OR "not-a-pool-or-IP type"
637 && ($IPDB::revlistalltypes || $webvar{alloctype} !~ /^.[dpi]/)
638 # safety against trying to retrieve and display more than 1k (10 bits, /22 v4) worth of individual IPs
639 # ever. If you really need to manage a long list of IPs like that all in one place, you can use the DNS
640 # management tool. Even a /26 is a bit much, really.
641 && ($cidr->bits - $cidr->masklen) <= 10
642 # do we want to allow v6 at all?
643 #&& ! $cidr->{isv6}
644 ) {
645 my @list;
646 foreach my $ip (@{$cidr->splitref()}) {
647 my %row;
648 $row{r_ip} = $ip->addr;
649 $row{iphost} = '';
650 push @list, \%row;
651 }
652 $page->param(r_iplist => \@list);
653 # We don't use this here, because these IPs should already be bare.
654 # ... or should we be paranoid? Make it a config option?
655 #getRDNSbyIP($ip_dbh, type => $webvar{alloctype}, range => "$cidr", user => $authuser) );
656 }
657 } # if ($webvar{alloctype} =~ /^.i$/)
658
659## node hack
660 if ($webvar{node} && $webvar{node} ne '-') {
661 my $nodename = getNodeName($ip_dbh, $webvar{node});
662 $page->param(nodename => $nodename);
663 $page->param(nodeid => $webvar{node});
664 }
665## end node hack
666
667 # flag DNS info if we can't publish the entry remotely
668 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
669 $page->param(dnslocal => 1) unless ($pinfo->{revpartial} || $pinfo->{revavail});
670
671 # reserve for expansion
672 $page->param(reserve => $webvar{reserve});
673 # probably just preventing a little log noise doing this; could just set the param
674 # all the time since it won't be shown if the reserve param above isn't set.
675# if ($webvar{reserve}) {
676 $page->param(resvblock => $resv);
677# }
678
679 # Stick in the allocation data
680 $page->param(alloc_type => $webvar{alloctype});
681 $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
682 $page->param(alloc_from => $alloc_from);
683 $page->param(parent => $p_id);
684 $page->param(fbid => $fbid);
685 $page->param(cidr => $cidr);
686 $page->param(rdns => $webvar{rdns});
687 $page->param(vrf => $webvar{vrf});
688 $page->param(vlan => $webvar{vlan});
689 $page->param(city => $q->escapeHTML($webvar{city}));
690 $page->param(custid => $webvar{custid});
691 $page->param(circid => $q->escapeHTML($webvar{circid}));
692 $page->param(desc => $q->escapeHTML($webvar{desc}));
693
694##fixme: find a way to have the displayed copy have <br> substitutions
695# for newlines, and the <input> value have either encoded or bare newlines.
696# Also applies to privdata.
697 $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
698
699 # Check to see if user is allowed to do anything with sensitive data
700 if ($IPDBacl{$authuser} =~ /s/) {
701 $page->param(nocling => 1);
702 $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'));
703
704 $page->param(backupfields => $webvar{backupfields});
705 $page->param(bkbrand => $webvar{bkbrand});
706 $page->param(bkmodel => $webvar{bkmodel});
707 $page->param(bktype => $webvar{bktype});
708 $page->param(bksrc => $webvar{bksrc});
709 $page->param(bkuser => $webvar{bkuser});
710 # these two could use virtually any character
711 $page->param(bkvpass => $q->escapeHTML($webvar{bkvpass}));
712 $page->param(bkepass => $q->escapeHTML($webvar{bkepass}));
713 $page->param(bkport => $webvar{bkport});
714 $page->param(bkip => $webvar{bkip});
715 }
716
717 # Yay! This now has it's very own little home.
718 $page->param(billinguser => $webvar{userid})
719 if $webvar{userid};
720
721##fixme: this is only needed iff confirm.tmpl and
722# confirmRemove.tmpl are merged (quite possible, just
723# a little tedious)
724 $page->param(action => "insert");
725
726} # end confirmAssign
727
728
729# Do the work of actually inserting a block in the database.
730sub insertAssign {
731 if ($IPDBacl{$authuser} !~ /a/) {
732 $aclerr = 'addblock';
733 return;
734 }
735 # Some things are done more than once.
736 return if !validateInput();
737
738##fixme: permission check
739 if (!defined($webvar{privdata})) {
740 $webvar{privdata} = '';
741 }
742
743 # $code is "success" vs "failure", $msg contains OK for a
744 # successful netblock allocation, the IP allocated for static
745 # IP, or the error message if an error occurred.
746
747##fixme: consider just passing \%webvar to allocateBlock()?
748 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
749 my %iprev;
750 foreach (keys %webvar) {
751 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
752 }
753
754 # Easier to see and cosmetically fiddle the list like this
755 my %insert_args = (
756 cidr => $webvar{fullcidr},
757 fbid => $webvar{fbid},
758 reserve => $webvar{reserve},
759 parent => $webvar{parent},
760 custid => $webvar{custid},
761 type => $webvar{alloctype},
762 city => $webvar{city},
763 desc => $webvar{desc},
764 notes => $webvar{notes},
765 circid => $webvar{circid},
766 privdata => $webvar{privdata},
767 nodeid => $webvar{node},
768 rdns => $webvar{rdns},
769 vrf => $webvar{vrf},
770 vlan => $webvar{vlan},
771 user => $authuser,
772 );
773
774##fixme: permission check
775 # fill in backup data, if present/allowed
776 if ($webvar{backupfields}) {
777 $insert_args{backup} = 1;
778 for my $bkfield (@IPDB::backupfields) {
779 $insert_args{"bk$bkfield"} = ($webvar{"bk$bkfield"} ? $webvar{"bk$bkfield"} : '');
780 }
781 }
782
783 my $pinfo = getBlockData($ip_dbh, $webvar{parent});
784
785 # clean up a minor mess with guided allocation of static IPs
786 if ($webvar{alloctype} =~ /^.i$/) {
787 $insert_args{alloc_from} = $pinfo->{block};
788 }
789
790 my ($code,$msg) = allocateBlock($ip_dbh, %insert_args, iprev => \%iprev);
791
792 if ($code eq 'OK') {
793 # breadcrumbs lite! provide at least a link to the parent of the block we just allocated.
794 $page->param(parentid => $webvar{parent});
795 $page->param(parentblock => $pinfo->{block});
796
797 if ($webvar{alloctype} =~ /^.i$/) {
798 $msg =~ s|/32||;
799 $page->param(staticip => $msg);
800 $page->param(custid => $webvar{custid});
801 $page->param(billinguser => $webvar{billinguser});
802 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
803 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
804 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
805 } else {
806 my $netblock = new NetAddr::IP $webvar{fullcidr};
807 $page->param(fullcidr => $webvar{fullcidr});
808 $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
809 $page->param(custid => $webvar{custid});
810
811 # Full breadcrumbs
812 my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
813 my @rcrumbs = reverse (@$crumbs);
814 $utilbar->param(breadcrumb => \@rcrumbs);
815
816 if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
817 $page->param(billinguser => $webvar{billinguser});
818 $page->param(custid => $webvar{custid});
819 $page->param(netaddr => $netblock->addr);
820 $page->param(masklen => $netblock->masklen);
821 }
822 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
823 "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
824 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
825 }
826 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
827 "'$webvar{alloctype}' ($msg)";
828 } else {
829 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
830 "'$webvar{alloctype}' by $authuser failed: '$msg'";
831 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}' failed:");
832 $page->param(errmsg => $msg);
833 }
834
835} # end insertAssign()
836
837
838# Does some basic checks on common input data to make sure nothing
839# *really* weird gets in to the database through this script.
840# Does NOT do complete input validation!!!
841sub validateInput {
842 if ($webvar{city} eq '-') {
843 $page->param(err => 'Please choose a city');
844 return;
845 }
846
847 # Alloctype check.
848 chomp $webvar{alloctype};
849 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
850 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
851 # managing to call things in such a way as to cause this deserves a cryptic error.
852 $page->param(err => 'Invalid alloctype');
853 return;
854 }
855
856 # CustID check
857 # We have different handling for customer allocations and "internal" or "our" allocations
858 if ($def_custids{$webvar{alloctype}} eq '') {
859 if (!$webvar{custid}) {
860 $page->param(err => 'Please enter a customer ID.');
861 return;
862 }
863 # Crosscheck with billing.
864 my $status = CustIDCK->custid_exist($webvar{custid});
865 if ($CustIDCK::Error) {
866 $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
867 return;
868 }
869 if (!$status) {
870 $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
871 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
872 "non-customer assignments.");
873 return;
874 }
875# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
876 } else {
877 # New! Improved! And now Loaded From The Database!!
878 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
879 $webvar{custid} = $def_custids{$webvar{alloctype}};
880 }
881 }
882
883## hmmm.... is this even useful?
884if (0) {
885 # Check POP location
886 my $flag;
887 if ($webvar{alloctype} eq 'rm') {
888 $flag = 'for a routed netblock';
889 foreach (@poplist) {
890 if (/^$webvar{city}$/) {
891 $flag = 'n';
892 last;
893 }
894 }
895 } else {
896 $flag = 'n';
897##fixme: hook to force-set POP or city on certain alloctypes
898# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
899 if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
900 $flag = 'to route the block from/through';
901 }
902 }
903
904 # if the alloctype has a restricted city/POP list as determined above,
905 # and the reqested city/POP does not match that list, complain
906 if ($flag ne 'n') {
907 $page->param(err => "Please choose a valid POP location $flag. Valid ".
908 "POP locations are currently:<br>\n".join (" - ", @poplist));
909 return;
910 }
911}
912
913 # VRF. Not a full validity check, just a basic sanity check.
914 if ($webvar{vrf}) {
915 # Trim leading and trailing whitespace first
916 $webvar{vrf} =~ s/^\s+//;
917 $webvar{vrf} =~ s/\s+$//;
918 if ($webvar{vrf} !~ /^[\w\d_.-]{1,32}$/) {
919 $page->param(err => "VRF values may only contain alphanumerics, and may not be more than 32 characters");
920 return;
921 }
922 }
923
924 # VLAN. Should we allow/use VLAN names, or just the numeric ID?
925 if ($webvar{vlan}) {
926 # Trim leading and trailing whitespace first
927 $webvar{vlan} =~ s/^\s+//;
928 $webvar{vlan} =~ s/\s+$//;
929 # ... ve make it ze configurable thingy!
930 if ($IPDB::numeric_vlan) {
931 if ($webvar{vlan} !~ /^\d+$/) {
932 $page->param(err => "VLANs must be numeric");
933 return;
934 }
935 } else {
936 if ($webvar{vlan} !~ /^[\w\d_.-]+$/) {
937 $page->param(err => "VLANs must be alphanumeric");
938 return;
939 }
940 }
941 }
942
943 # Backup fields. Minimal sanity checks.
944 for my $bkfield (qw(brand model)) {
945 if (!$webvar{"bk$bkfield"}) {
946 $page->param(err => "Backup $bkfield must be filled in if IP/netblock is flagged for backup");
947 return;
948 }
949 if ($webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9\s_.-]+$/) {
950 $page->param(err => "Invalid characters in backup $bkfield");
951 return;
952 }
953 }
954 for my $bkfield (qw(type src user)) { # no spaces in these!
955 if ($webvar{"bk$bkfield"} && $webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9_.-]+$/) {
956 $page->param(err => "Invalid characters in backup $bkfield");
957 return;
958 }
959 }
960 if ($webvar{bkport}) {
961 $webvar{bkport} =~ s/^\s+//g;
962 $webvar{bkport} =~ s/\s+$//g;
963 if ($webvar{bkport} !~ /^\d+$/) {
964 $page->param(err => "Backup port must be numeric");
965 return;
966 }
967 }
968##fixme: code review: should normalize $webvar{cidr} variants so we can
969# check for non-/32 allocations having the backup IP field filled in here,
970# instead of failing on the allocation or update attempt
971 if ($webvar{bkip}) {
972 $webvar{bkip} =~ s/^\s+//g;
973 $webvar{bkip} =~ s/\s+$//g;
974 if ($webvar{bkip} !~ /^[\da-fA-F:.]+$/) {
975 $page->param(err => "Backup IP must be an IP");
976 return;
977 }
978 }
979
980 return 'OK';
981} # end validateInput
982
983
984# Displays details of a specific allocation in a form
985# Allows update/delete
986# action=edit
987sub edit {
988
989 # snag block info from db
990 my $blockinfo = getBlockData($ip_dbh, $webvar{id}, $webvar{basetype});
991 my $cidr = new NetAddr::IP $blockinfo->{block};
992 $page->param(id => $webvar{id});
993 $page->param(basetype => $webvar{basetype});
994
995 # Tree navigation
996 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
997 my @rcrumbs = reverse (@$crumbs);
998 $utilbar->param(breadcrumb => \@rcrumbs);
999
1000 # Show link to IP list for pools
1001 $page->param(ispool => 1) if $blockinfo->{type} =~ /^.[dp]$/;
1002
1003 # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
1004 $blockinfo->{type} =~ s/\s//;
1005
1006##fixme: The case of "allocation larger than a /24" (or any similar case
1007# where the allocation is larger than the zone(s) in DNS) doesn't work well.
1008# Best solution may just be to add a warning that the entry shown may not be
1009# correct/complete.
1010 if ($blockinfo->{revavail} || $blockinfo->{revpartial}) {
1011 $page->param(showrev => ($blockinfo->{revavail} || $blockinfo->{revpartial}) );
1012 my $cached;
1013 # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
1014 ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser);
1015 $page->param(rdns => $blockinfo->{rdns});
1016 # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
1017 $page->param(cached => $cached);
1018
1019 # Limit the per-IP rDNS list based on CIDR length; larger ones just take up too much space.
1020 # Also, don't show on IP pools; the individual IPs will have a space for rDNS
1021 # Don't show on single IPs; these use the "pattern" field
1022 if ($IPDBacl{$authuser} =~ /c/
1023 && $cidr->masklen != $cidr->bits
1024 && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
1025 # config flag for "all block types" OR "not-a-pool-or-IP type"
1026 && ($IPDB::revlistalltypes || $blockinfo->{type} !~ /^.[dpi]/)
1027 # safety against trying to retrieve and display more than 1k (10 bits, /22 v4) worth of individual IPs
1028 # ever. If you really need to manage a long list of IPs like that all in one place, you can use the DNS
1029 # management tool. Even a /26 is a bit much, really.
1030 && ($cidr->bits - $cidr->masklen) <= 10
1031 # do we want to allow v6 at all?
1032 #&& ! $cidr->{isv6}
1033 ) {
1034 $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type},
1035 range => $blockinfo->{block}, user => $authuser) );
1036 }
1037 } # rDNS availability check
1038
1039 # backup data
1040 if ($blockinfo->{hasbk}) {
1041 $page->param(hasbackup => $blockinfo->{hasbk});
1042 for my $bkfield (@IPDB::backupfields) {
1043 $page->param("bk$bkfield" => $blockinfo->{"bk$bkfield"});
1044 }
1045 $page->param(bktelnet => 1) if $blockinfo->{bktype} eq 'telnet';
1046 $page->param(bkssh => 1) if $blockinfo->{bktype} eq 'SSH';
1047 }
1048
1049 # consider extending this to show time as well as date
1050 my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
1051 $page->param(lastmod => $lastmod);
1052
1053 $page->param(block => $blockinfo->{block});
1054 $page->param(city => $blockinfo->{city});
1055 $page->param(custid => $blockinfo->{custid});
1056
1057##fixme The check here should be built from the database
1058# Need to expand to support pool types too
1059 if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
1060 $page->param(changetype => 1);
1061 $page->param(alloctype => [
1062 { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
1063 { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
1064 { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
1065 { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
1066 { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
1067 { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
1068 { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
1069 ]
1070 );
1071 } else {
1072 $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
1073 $page->param(type => $blockinfo->{type});
1074 }
1075
1076## node hack
1077 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block});
1078# $page->param(havenodeid => $nodeid);
1079 $page->param(nodename => $nodename);
1080
1081##fixme: this whole hack needs cleanup and generalization for all alloctypes
1082##fixme: arguably a bug that presence of a nodeid implies it can be changed..
1083 if ($IPDBacl{$authuser} =~ /c/) {
1084 my $nlist = getNodeList($ip_dbh);
1085 if ($nodeid) {
1086 foreach (@{$nlist}) {
1087 $$_{selme} = ($$_{node_id} == $nodeid);
1088 }
1089 }
1090 $page->param(nodelist => $nlist);
1091 }
1092## end node hack
1093
1094 $page->param(vrf => $blockinfo->{vrf});
1095 $page->param(vlan => $blockinfo->{vlan});
1096
1097 # Reserved-for-expansion
1098 $page->param(reserve => $blockinfo->{reserve});
1099 $page->param(reserve_id => $blockinfo->{reserve_id});
1100 my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
1101 $page->param(newblock => $newblock);
1102
1103 # not happy with the upside-down logic, but...
1104 $page->param(swipable => $blockinfo->{type} !~ /.i/);
1105 $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
1106
1107 $page->param(circid => $blockinfo->{circuitid});
1108 $page->param(desc => $blockinfo->{description});
1109 $page->param(notes => $blockinfo->{notes});
1110
1111 # Check to see if we can display sensitive data
1112 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
1113 $page->param(privdata => $blockinfo->{privdata});
1114
1115 # ACL trickery - these two template booleans control the presence of all form/input tags
1116 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
1117 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
1118
1119 # Need to find internal knobs to twist to actually vary these. (Ab)use "change" flag for now
1120 $page->param(maymerge => ($IPDBacl{$authuser} =~ /m/ && $blockinfo->{type} !~ /^.i$/));
1121
1122 if ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/) {
1123 if ($blockinfo->{type} =~ /^.p$/) {
1124 # PPP pools
1125 $page->param(maysplit => 1) if $cidr->masklen+1 < $cidr->bits;
1126 } elsif ($blockinfo->{type} =~ /.d/) {
1127 # Non-PPP pools
1128 $page->param(maysplit => 1) if $cidr->masklen+2 < $cidr->bits;
1129 } else {
1130 # Standard netblocks. Arguably allowing splitting these down to single IPs
1131 # doesn't make much sense, but forcing users to apply allocation types
1132 # "properly" is worse than herding cats.
1133 $page->param(maysplit => 1) if $cidr->masklen < $cidr->bits;
1134 }
1135 }
1136
1137} # edit()
1138
1139
1140# Stuff new info about a block into the db
1141# action=update
1142sub update {
1143 if ($IPDBacl{$authuser} !~ /c/) {
1144 $aclerr = 'updateblock';
1145 return;
1146 }
1147
1148 # Collect existing block info here, since we need it for the breadcrumb nav
1149 my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1150 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1151 my @rcrumbs = reverse (@$crumbs);
1152 $utilbar->param(breadcrumb => \@rcrumbs);
1153
1154 # Make sure incoming data is in correct format - custID among other things.
1155 return if !validateInput;
1156
1157 $webvar{swip} = 'n' if !$webvar{swip};
1158
1159 my %updargs = (
1160 custid => $webvar{custid},
1161 city => $webvar{city},
1162 description => $webvar{desc},
1163 notes => $webvar{notes},
1164 circuitid => $webvar{circid},
1165 block => $webvar{block},
1166 type => $webvar{alloctype},
1167 swip => $webvar{swip},
1168 rdns => $webvar{rdns},
1169 vrf => $webvar{vrf},
1170 vlan => $webvar{vlan},
1171 user => $authuser,
1172 );
1173
1174 # Check to see if user is allowed to do anything with sensitive data
1175 if ($IPDBacl{$authuser} =~ /s/) {
1176 $updargs{privdata} = $webvar{privdata};
1177 for my $bkfield (@IPDB::backupfields) {
1178 $updargs{"bk$bkfield"} = $webvar{"bk$bkfield"};
1179 }
1180 $updargs{backup} = $webvar{backupfields};
1181 } else {
1182 # If the user doesn't have permissions to monkey with NOC-things, pass
1183 # a flag so we don't treat it as "backup data removed"
1184 $updargs{ignorebk} = 1;
1185 }
1186
1187 # Semioptional values
1188 $updargs{node} = $webvar{node} if $webvar{node};
1189
1190 # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
1191 my %iprev;
1192 foreach (keys %webvar) {
1193 $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
1194 }
1195
1196 # Merge with reserved freeblock
1197 $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme};
1198
1199 my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
1200
1201 if ($code eq 'FAIL') {
1202 syslog "err", "$authuser could not update block/IP '$binfo->{block}' (id $webvar{block}): '$msg'";
1203 $page->param(err => "Could not update block/IP $binfo->{block}: $msg");
1204 return;
1205 }
1206
1207 # If we get here, the operation succeeded.
1208 syslog "notice", "$authuser updated $binfo->{block}";
1209##fixme: log details of the change? old way is in the .debug stream anyway.
1210##fixme: need to wedge something in to allow "update:field" notifications
1211## hmm. how to tell what changed? O_o
1212mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $binfo->{block}",
1213 "$binfo->{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
1214
1215## node hack
1216 if ($webvar{node} && $webvar{node} ne '-') {
1217 my $nodename = getNodeName($ip_dbh, $webvar{node});
1218 $page->param(nodename => $nodename);
1219 }
1220## end node hack
1221
1222 # Link back to browse-routed or list-pool page on "Update complete" page.
1223 my $pblock = getBlockData($ip_dbh, $binfo->{parent_id});
1224 $page->param(backid => $binfo->{parent_id});
1225 $page->param(backblock => $pblock->{block});
1226 $page->param(backpool => ($webvar{basetype} eq 'i'));
1227
1228 # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
1229 # because otherwise we can't convert \n to <br>. *sigh*
1230 $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
1231 $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
1232 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
1233 $webvar{privdata} =~ s/\n/<br>\n/;
1234
1235 if ($webvar{expandme}) {
1236 # this is fugly but still faster than hitting the DB again with getBlockData()
1237 my $tmp = new NetAddr::IP $binfo->{block};
1238 my $fb = new NetAddr::IP $binfo->{reserve};
1239 my @newblock = $tmp->compact($fb);
1240 $page->param(cidr => $newblock[0]);
1241 } else {
1242 $page->param(cidr => $binfo->{block});
1243 }
1244 $page->param(rdns => $webvar{rdns});
1245 $page->param(city => $webvar{city});
1246 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
1247 $page->param(custid => $webvar{custid});
1248 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
1249 $page->param(circid => $webvar{circid});
1250 $page->param(desc => $webvar{desc});
1251 $page->param(notes => $webvar{notes});
1252 if ($IPDBacl{$authuser} =~ /s/) {
1253 $page->param(nocling => 1);
1254 $page->param(privdata => $webvar{privdata});
1255 if ($webvar{backupfields} && $webvar{backupfields} eq 'on') {
1256 $page->param(hasbackup => 1);
1257 for my $bkfield (@IPDB::backupfields) {
1258 $page->param("bk$bkfield" => $webvar{"bk$bkfield"});
1259 }
1260 }
1261 }
1262
1263} # update()
1264
1265
1266sub prepSplit {
1267 if ($IPDBacl{$authuser} !~ /c/) {
1268 $aclerr = 'splitblock';
1269 return;
1270 }
1271
1272 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1273
1274 # Tree navigation
1275 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1276 my @rcrumbs = reverse (@$crumbs);
1277 $utilbar->param(breadcrumb => \@rcrumbs);
1278
1279 if ($blockinfo->{type} =~ /^.i$/) {
1280 $page->param(err => "Can't split a single IP allocation");
1281 return;
1282 }
1283
1284 # Info about current allocation
1285 $page->param(oldblock => $blockinfo->{block});
1286 $page->param(block => $webvar{block});
1287
1288# Note that there are probably different rules that should be followed to restrict splitting IPv6 blocks;
1289# strictly speaking it will be exceptionally rare to see smaller than a /64 assigned to a customer, since that
1290# breaks auto-addressing schemes.
1291
1292 # Generate possible splits
1293 my $block = new NetAddr::IP $blockinfo->{block};
1294 my $oldmask = $block->masklen;
1295 if ($blockinfo->{type} =~ /^.d$/) {
1296 # Non-PPP pools
1297 $page->param(ispool => 1);
1298 if ($oldmask+2 >= $block->bits) {
1299 $page->param(err => "Can't split a standard netblock pool any further");
1300 return;
1301 }
1302 # Allow splitting down to v4 /30 (which results in one usable IP; dubiously useful)
1303 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-2;
1304 } elsif ($blockinfo->{type} =~ /.p/) {
1305 $page->param(ispool => 1);
1306 # Allow splitting PPP pools down to v4 /31
1307 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-1;
1308 } else {
1309 # Allow splitting all other non-pool netblocks down to single IPs, which...
1310 # arguably should be *aggregated* in a pool. Except where they shouldn't.
1311 $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits;
1312 }
1313 # set the split-in-half mask
1314 $page->param(sp2mask => $oldmask+1);
1315
1316 # Generate possible shrink targets
1317 my @keepers = $block->split($block->masklen+1);
1318 $page->param(newblockA => $keepers[0]);
1319 $page->param(newblockB => $keepers[1]);
1320} # prepSplit()
1321
1322
1323sub doSplit {
1324 if ($IPDBacl{$authuser} !~ /c/) {
1325 $aclerr = 'splitblock';
1326 return;
1327 }
1328
1329##fixme: need consistent way to identify "this thing that is this thing" with only the ID
1330# also applies to other locations
1331 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
1332
1333 # Tree navigation
1334 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1335 my @rcrumbs = reverse (@$crumbs);
1336 $utilbar->param(breadcrumb => \@rcrumbs);
1337
1338 if ($blockinfo->{type} =~ /^.i$/) {
1339 $page->param(err => "Can't split a single IP allocation");
1340 return;
1341 }
1342
1343 if ($webvar{subact} eq 'split') {
1344 $page->param(issplit => 1);
1345 my $block = new NetAddr::IP $blockinfo->{block};
1346 my $newblocks = splitBlock($ip_dbh, id => $webvar{block}, basetype => 'b', newmask => $webvar{split},
1347 user => $authuser);
1348 if ($newblocks) {
1349 $page->param(newblocks => $newblocks);
1350 } else {
1351 $page->param(err => $IPDB::errstr);
1352 }
1353
1354 } elsif ($webvar{subact} eq 'shrink') {
1355 $page->param(nid => $webvar{block});
1356 $page->param(newblock => $webvar{shrink});
1357 my $newfree = shrinkBlock($ip_dbh, $webvar{block}, $webvar{shrink});
1358 if ($newfree) {
1359 $page->param(newfb => $newfree);
1360 } else {
1361 $page->param(err => $IPDB::errstr);
1362 }
1363
1364 } else {
1365 # Your llama is on fire.
1366 $page->param(err => "Missing form field that shouldn't be missing.");
1367 return;
1368 }
1369
1370 # common bits
1371 $page->param(cidr => $blockinfo->{block});
1372 # and the backlink to the parent container
1373 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id});
1374 $page->param(backid => $blockinfo->{parent_id});
1375 $page->param(backblock => $pinfo->{block});
1376} # doSplit()
1377
1378
1379# Set up for merge
1380sub prepMerge {
1381 if ($IPDBacl{$authuser} !~ /m/) {
1382 $aclerr = 'mergeblock';
1383 return;
1384 }
1385
1386 my $binfo = getBlockData($ip_dbh, $webvar{block});
1387
1388 # Tree navigation
1389 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1390 my @rcrumbs = reverse (@$crumbs);
1391 $utilbar->param(breadcrumb => \@rcrumbs);
1392
1393 $page->param(block => $webvar{block});
1394 $page->param(ispool => $binfo->{type} =~ /.[dp]/);
1395 $page->param(ismaster => $binfo->{type} eq 'mm');
1396 $page->param(oldblock => $binfo->{block});
1397 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1398 $page->param(typelist => getTypeList($ip_dbh, 'n', $binfo->{type})); # down the rabbit hole we go...
1399
1400 # Strings for scope; do this way so we don't have to edit them many places
1401 $page->param(vis_keepall => $merge_display{keepall});
1402 $page->param(vis_mergepeer => $merge_display{mergepeer});
1403 $page->param(vis_clearpeer => $merge_display{clearpeer});
1404 $page->param(vis_clearall => $merge_display{clearall});
1405
1406} # prepMerge()
1407
1408
1409# Show what will be merged, present warnings about data loss
1410sub confMerge {
1411 if ($IPDBacl{$authuser} !~ /m/) {
1412 $aclerr = 'mergeblock';
1413 return;
1414 }
1415
1416 if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) {
1417 $page->param(err => 'New netmask required');
1418 return;
1419 }
1420
1421 $page->param(block => $webvar{block});
1422 my $binfo = getBlockData($ip_dbh, $webvar{block});
1423 my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
1424 my $minfo = getBlockData($ip_dbh, $binfo->{master_id});
1425 my $block = new NetAddr::IP $binfo->{block};
1426
1427 # Tree navigation
1428 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1429 my @rcrumbs = reverse (@$crumbs);
1430 $utilbar->param(breadcrumb => \@rcrumbs);
1431
1432 $page->param(oldblock => $binfo->{block});
1433 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1434 $page->param(ismaster => $binfo->{type} eq 'mm');
1435 $page->param(ispool => $webvar{alloctype} =~ /.[dp]/);
1436 $page->param(isleaf => $webvar{alloctype} =~ /.[enr]/);
1437 $page->param(newtype => $webvar{alloctype});
1438 $page->param(newdisptype => $disp_alloctypes{$webvar{alloctype}});
1439 my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}";
1440 $newblock = $newblock->network;
1441 $page->param(newmask => $webvar{newmask});
1442 $page->param(newblock => "$newblock");
1443
1444 # get list of allocations and freeblocks to be merged
1445 my $malloc_list = listForMerge($ip_dbh, $binfo->{parent_id}, $newblock, 'a');
1446 $page->param(mergealloc => $malloc_list);
1447
1448 $page->param(vis_scope => $merge_display{$webvar{scope}});
1449 $page->param(scope => $webvar{scope});
1450} # confMerge()
1451
1452
1453# Make it so
1454sub doMerge {
1455 if ($IPDBacl{$authuser} !~ /m/) {
1456 $aclerr = 'mergeblock';
1457 return;
1458 }
1459
1460 if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) {
1461 $page->param(err => 'New netmask required');
1462 return;
1463 }
1464
1465 $page->param(block => $webvar{block});
1466 my $binfo = getBlockData($ip_dbh, $webvar{block});
1467 my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
1468 my $block = new NetAddr::IP $binfo->{block};
1469
1470 # Tree navigation
1471 my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
1472 my @rcrumbs = reverse (@$crumbs);
1473 $utilbar->param(breadcrumb => \@rcrumbs);
1474
1475 $page->param(oldblock => $binfo->{block});
1476 $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
1477 $page->param(newdisptype => $disp_alloctypes{$webvar{newtype}});
1478 my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}";
1479 $newblock = $newblock->network;
1480 $page->param(newblock => $newblock);
1481 $page->param(vis_scope => $merge_display{$webvar{scope}});
1482
1483 my $mlist = mergeBlocks($ip_dbh, $webvar{block}, %webvar, user => $authuser);
1484
1485 if ($mlist) {
1486 #(newtype => $webvar{newtype}, newmask => $webvar{newmask}));
1487 # Slice off first entry (the new parent - note this may be a new allocation,
1488 # not the same ID that was "merged"!
1489 my $parent = shift @$mlist;
1490 $page->param(backpool => $webvar{newtype} =~ /.[dp]/);
1491 if ($webvar{newtype} =~ /.[enr]/) {
1492 $page->param(backleaf => 1);
1493 $page->param(backid => $binfo->{parent_id});
1494 $page->param(backblock => $pinfo->{block});
1495 } else {
1496 $page->param(backid => $parent->{id});
1497 $page->param(backblock => $parent->{block});
1498 }
1499 $page->param(mergelist => $mlist);
1500 } else {
1501 $page->param(err => "Merge failed: $IPDB::errstr");
1502 }
1503} # doMerge()
1504
1505
1506# Delete an allocation.
1507sub remove {
1508 if ($IPDBacl{$authuser} !~ /d/) {
1509 $aclerr = 'delblock';
1510 return;
1511 }
1512
1513 # Serves'em right for getting here...
1514 if (!defined($webvar{block})) {
1515 $page->param(err => "Can't delete a block that doesn't exist");
1516 return;
1517 }
1518
1519 my $blockdata;
1520 $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1521
1522 # Tree navigation
1523 my $crumbs = getBreadCrumbs($ip_dbh, $blockdata->{parent_id});
1524 my @rcrumbs = reverse (@$crumbs);
1525 $utilbar->param(breadcrumb => \@rcrumbs);
1526
1527 if ($blockdata->{parent_id} == 0) { # $webvar{alloctype} eq 'mm'
1528 $blockdata->{city} = "N/A";
1529 $blockdata->{custid} = "N/A";
1530 $blockdata->{circuitid} = "N/A";
1531 $blockdata->{description} = "N/A";
1532 $blockdata->{notes} = "N/A";
1533 $blockdata->{privdata} = "N/A";
1534 } # end cases for different alloctypes
1535
1536 $page->param(blockid => $webvar{block});
1537 $page->param(basetype => $webvar{basetype});
1538
1539 $page->param(block => $blockdata->{block});
1540 $page->param(rdns => $blockdata->{rdns});
1541
1542 # maybe need to apply more magic here?
1543 # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
1544 # -> all real blocks (nb: pool IPs need extra handling)
1545 # -> NOC/private-IP (how to ID?)
1546 # -> anything with a pattern matching $IPDB::domain?
1547 if ($blockdata->{type} !~ /^.i$/) {
1548 $page->param(autodel => 1);
1549 }
1550
1551 $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
1552 $page->param(city => $blockdata->{city});
1553 $page->param(custid => $blockdata->{custid});
1554 $page->param(circid => $blockdata->{circuitid});
1555 $page->param(desc => $blockdata->{description});
1556 $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
1557 $blockdata->{notes} =~ s/\n/<br>\n/;
1558 $page->param(notes => $blockdata->{notes});
1559 $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
1560 $blockdata->{privdata} = '&nbsp;' if !$blockdata->{privdata};
1561 $blockdata->{privdata} =~ s/\n/<br>\n/;
1562 $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
1563 $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
1564
1565} # end remove()
1566
1567
1568# Delete an allocation. Return it to the freeblocks table; munge
1569# data as necessary to keep as few records as possible in freeblocks
1570# to prevent weirdness when allocating blocks later.
1571# Remove IPs from pool listing if necessary
1572sub finalDelete {
1573 if ($IPDBacl{$authuser} !~ /d/) {
1574 $aclerr = 'delblock';
1575 return;
1576 }
1577
1578 # need to retrieve block data before deleting so we can notify on that
1579 my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
1580 my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b');
1581
1582 # Tree navigation
1583 my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
1584 my @rcrumbs = reverse (@$crumbs);
1585 $utilbar->param(breadcrumb => \@rcrumbs);
1586
1587 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser);
1588
1589 $page->param(block => $blockinfo->{block});
1590 $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}}));
1591 $page->param(delparent_id => $blockinfo->{parent_id});
1592 if ($pinfo) {
1593 $page->param(delparent => $pinfo->{block});
1594 $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}}));
1595 }
1596 $page->param(returnpool => ($webvar{basetype} eq 'i') );
1597 if ($code =~ /^WARN(POOL|MERGE)/) {
1598 my ($pid,$pcidr) = split /,/, $msg;
1599 my $real_pinfo = getBlockData($ip_dbh, $pid, 'b');
1600 $page->param(parent_id => $pid);
1601 $page->param(parent => $pcidr);
1602 $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}}));
1603 $page->param(mergeip => $code eq 'WARNPOOL');
1604 }
1605 if ($code eq 'WARN') {
1606 $msg =~ s/\n/<br>\n/g;
1607 $page->param(genwarn => $msg);
1608 }
1609 if ($code eq 'OK' || $code =~ /^WARN/) {
1610 syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock $webvar{block} ".
1611 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
1612 mailNotify($ip_dbh, 'da', "REMOVED: ".$disp_alloctypes{$blockinfo->{type}}." $webvar{block}",
1613 $disp_alloctypes{$blockinfo->{type}}." $webvar{block} deallocated by $authuser\n".
1614 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
1615 "\nDescription: ".$blockinfo->{description}."\n");
1616 } else {
1617 $page->param(failmsg => $msg);
1618 if ($webvar{alloctype} =~ /^.i$/) {
1619 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1620 } else {
1621 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
1622 $page->param(netblock => 1);
1623 }
1624 }
1625
1626} # finalDelete
Note: See TracBrowser for help on using the repository browser.