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

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

/trunk

Expose VRF field, and add a similar VLAN field (see #10).

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