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

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

/trunk

Begin committing "merge/extend blocks". See #8.

This commit adds the first merge page to the UI.

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