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

Last change on this file since 533 was 533, checked in by Kris Deugau, 12 years ago

/trunk

Clean up and move SQL for finding a free block to allocate from
into IPDB.pm. See #34.
Also fix a minor "Use of uninitialized value...".

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 29.8 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3###
4# SVN revision info
5# $Date: 2012-10-31 19:47:53 +0000 (Wed, 31 Oct 2012) $
6# SVN revision $Rev: 533 $
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;
19
20use Sys::Syslog;
21
22# don't remove! required for GNU/FHS-ish install from tarball
23##uselib##
24
25use CustIDCK;
26use MyIPDB;
27
28openlog "IPDB","pid","$IPDB::syslog_facility";
29
30## Environment. Collect some things, process some things, set some things...
31
32# Collect the username from HTTP auth. If undefined, we're in
33# a test environment, or called without a username.
34my $authuser;
35if (!defined($ENV{'REMOTE_USER'})) {
36 $authuser = '__temptest';
37} else {
38 $authuser = $ENV{'REMOTE_USER'};
39}
40
41# anyone got a better name? :P
42my $thingroot = $ENV{SCRIPT_FILENAME};
43$thingroot =~ s|cgi-bin/main.cgi||;
44
45syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
46
47##fixme there *must* be a better order to do things in so this can go back where it was
48# CGI fiddling done here so we can declare %webvar so we can alter $webvar{action}
49# to show the right page on DB errors.
50# Set up the CGI object...
51my $q = new CGI::Simple;
52# ... and get query-string params as well as POST params if necessary
53$q->parse_query_string;
54
55# Convenience; saves changing all references to %webvar
56##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
57my %webvar = $q->Vars;
58
59# Why not a global DB handle? (And a global statement handle, as well...)
60# Use the connectDB function, otherwise we end up confusing ourselves
61my $ip_dbh;
62my $sth;
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);
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} elsif ($webvar{action} eq 'newmaster') {
104
105 if ($IPDBacl{$authuser} !~ /a/) {
106 $aclerr = 'addmaster';
107 } else {
108 my $cidr = new NetAddr::IP $webvar{cidr};
109 $page->param(cidr => "$cidr");
110
111 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
112
113 if ($code eq 'FAIL') {
114 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
115 $page->param(err => $msg);
116 } else {
117 syslog "info", "$authuser added master block $webvar{cidr}";
118 }
119
120 } # ACL check
121
122} # end add new master
123
124elsif($webvar{action} eq 'showmaster') {
125 showMaster();
126}
127elsif($webvar{action} eq 'showrouted') {
128 showRBlock();
129}
130elsif($webvar{action} eq 'listpool') {
131 showPool();
132}
133
134# Not modified or added; just shuffled
135elsif($webvar{action} eq 'assign') {
136 assignBlock();
137}
138elsif($webvar{action} eq 'confirm') {
139 confirmAssign();
140}
141elsif($webvar{action} eq 'insert') {
142 insertAssign();
143}
144elsif($webvar{action} eq 'edit') {
145 edit();
146}
147elsif($webvar{action} eq 'update') {
148 update();
149}
150elsif($webvar{action} eq 'delete') {
151 remove();
152}
153elsif($webvar{action} eq 'finaldelete') {
154 finalDelete();
155}
156elsif ($webvar{action} eq 'nodesearch') {
157 my $nodelist = getNodeList($ip_dbh);
158 $page->param(nodelist => $nodelist);
159}
160
161# DB failure. Can't do much here, really.
162elsif ($webvar{action} eq 'dberr') {
163 $page->param(errmsg => $errstr);
164}
165
166# Default is an error. It shouldn't be possible to get here unless you're
167# randomly feeding in values for webvar{action}.
168else {
169 my $rnd = rand 500;
170 my $boing = sprintf("%.2f", rand 500);
171 my @excuses = (
172 "Aether cloudy. Ask again later about $webvar{action}.",
173 "The gods are unhappy with your sacrificial $webvar{action}.",
174 "Because one of $webvar{action}'s legs are both the same",
175 "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!",
176 "Hey, man, you've had your free $webvar{action}. Next one's gonna... <i>cost</i>....",
177 "I ain't done $webvar{action}",
178 "Oooo, look! A flying $webvar{action}!",
179 "$webvar{action} too evil, avoiding.",
180 "Rocks fall, $webvar{action} dies.",
181 "Bit bucket must be emptied before I can $webvar{action}..."
182 );
183 $page->param(dunno => $excuses[$rnd/50.0]);
184}
185## Finally! Done with that NASTY "case" emulation!
186
187
188# Switch to a different template if we've tripped on an ACL error.
189# Note that this should only be exercised in development, when
190# deeplinked, or when being attacked; normal ACL handling should
191# remove the links a user is not allowed to click on.
192if ($aclerr) {
193 $page = HTML::Template->new(filename => "aclerror.tmpl");
194 $page->param(ipdbfunc => $aclmsg{$aclerr});
195}
196
197$sth->finish if $sth;
198# Clean up IPDB globals, DB handle, etc.
199finish($ip_dbh);
200
201## Do all our printing here so we can generate errors and stick them into the slots in the templates.
202
203# can't do this yet, too many blowups
204#print "Content-type: text/html\n\n", $header->output;
205$page->param(webpath => $IPDB::webpath);
206print $page->output;
207
208# include the admin tools link in the output?
209$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
210$footer->param(webpath => $IPDB::webpath);
211print $footer->output;
212
213# Just in case something waaaayyy down isn't in place
214# properly... we exit explicitly.
215exit 0;
216
217
218# Initial display: Show master blocks with total allocated subnets, total free subnets
219sub showSummary {
220 my $masterlist = listSummary($ip_dbh);
221 $page->param(masterlist => $masterlist);
222
223 $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
224} # showSummary
225
226
227# Display detail on master
228# Alrighty then! We're showing routed blocks within a single master this time.
229# We should be able to steal code from showSummary(), and if I'm really smart
230# I'll figger a way to munge the two together. (Once I've done that, everything
231# else should follow. YMMV.)
232sub showMaster {
233
234 $page->param(master => $webvar{block});
235 $page->param(delmaster => ($IPDBacl{$authuser} =~ /d/));
236
237 my $rlist = listMaster($ip_dbh, $webvar{block});
238 $page->param(routedlist => $rlist);
239
240 my $flist = listFree($ip_dbh, $webvar{block});
241 $page->param(unrouted => $flist);
242} # showMaster
243
244
245# Display details of a routed block
246# Alrighty then! We're showing allocations within a routed block this time.
247# We should be able to steal code from showSummary() and showMaster(), and if
248# I'm really smart I'll figger a way to munge all three together. (Once I've
249# done that, everything else should follow. YMMV.
250# This time, we check the database before spewing, because we may
251# not have anything useful to spew.
252sub showRBlock {
253
254 $page->param(master => $webvar{block});
255 $page->param(delrouted => $IPDBacl{$authuser} =~ /d/);
256
257 my $rcity = getRoutedCity($ip_dbh, $webvar{block});
258 $page->param(rcity => $rcity);
259
260 my $blist = listRBlock($ip_dbh, $webvar{block});
261 $page->param(blocklist => $blist);
262
263 my $flist = listFree($ip_dbh, $webvar{block}, 'y');
264 $page->param(unassigned => $flist);
265} # showRBlock
266
267
268# List the IPs used in a pool
269sub showPool {
270
271 my $cidr = new NetAddr::IP $webvar{pool};
272
273 $page->param(block => $webvar{pool});
274 $page->param(netip => $cidr->addr);
275 $cidr++;
276 $page->param(gate => $cidr->addr);
277 $cidr--; $cidr--;
278 $page->param(bcast => $cidr->addr);
279 $page->param(mask => $cidr->mask);
280
281 # Snag pool info for heading
282 my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
283
284 $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
285 $page->param(city => $poolinfo->{city});
286
287 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
288 $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
289
290# probably have to add an "edit IP allocation" link here somewhere.
291
292 my $plist = listPool($ip_dbh, $webvar{pool});
293 # technically slightly more efficient to check the ACL in an if () once outside the foreach
294 foreach (@{$plist}) {
295 $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
296 }
297 $page->param(poolips => $plist);
298} # end showPool
299
300
301# Show "Add new allocation" page. Note that the actual page may
302# be one of two templates, and the lists come from the database.
303sub assignBlock {
304
305 if ($IPDBacl{$authuser} !~ /a/) {
306 $aclerr = 'addblock';
307 return;
308 }
309
310 # hack pthbttt eww
311 $webvar{block} = '' if !$webvar{block};
312
313# hmm. TMPL_IF block and TMPL_ELSE block on these instead?
314 $page->param(rowa => 'row'.($webvar{block} eq '' ? 1 : 0));
315 $page->param(rowb => 'row'.($webvar{block} eq '' ? 0 : 1));
316 $page->param(block => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
317 $page->param(iscontained => ($webvar{fbtype} && $webvar{fbtype} ne 'y'));
318
319 # New special case- block to assign is specified
320 if ($webvar{block} ne '') {
321 my $block = new NetAddr::IP $webvar{block};
322
323 # Handle contained freeblock allocation.
324 # This is a little dangerous, as it's *theoretically* possible to
325 # get fbtype='n' (aka a non-routed freeblock). However, should
326 # someone manage to get there, they get what they deserve.
327 if ($webvar{fbtype} ne 'y') {
328 # Snag the type of the container block from the database.
329## hmm. need a flag for parent class/type, sort of?
330 my $pblock = getParent($ip_dbh, $webvar{block});
331 my $ptype = $pblock->{type};
332 $ptype =~ s/c$/r/;
333 $page->param(fbdisptype => $list_alloctypes{$ptype});
334 $page->param(type => $ptype);
335 } else {
336 # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
337 my $tlist = getTypeList($ip_dbh, 'p');
338 $tlist->[0]->{sel} = 1;
339 $page->param(typelist => $tlist);
340 }
341 } else {
342 my @masterlist;
343 foreach my $master (@masterblocks) {
344 my %row = (master => "$master");
345 push (@masterlist, \%row);
346 }
347 $page->param(masterlist => \@masterlist);
348
349 my @pops;
350 foreach my $pop (@poplist) {
351 my %row = (pop => $pop);
352 push (@pops, \%row);
353 }
354 $page->param(pops => \@pops);
355
356 # get all standard alloctypes
357 my $tlist = getTypeList($ip_dbh, 'a');
358 $tlist->[0]->{sel} = 1;
359 $page->param(typelist => $tlist);
360 }
361
362 my @cities;
363 foreach my $city (@citylist) {
364 my %row = (city => $city);
365 push (@cities, \%row);
366 }
367 $page->param(citylist => \@cities);
368
369## node hack
370 my $nlist = getNodeList($ip_dbh);
371 $page->param(nodelist => $nlist);
372## end node hack
373
374 $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
375
376} # assignBlock
377
378
379# Take info on requested IP assignment and see what we can provide.
380sub confirmAssign {
381 if ($IPDBacl{$authuser} !~ /a/) {
382 $aclerr = 'addblock';
383 return;
384 }
385
386 my $cidr;
387 my $alloc_from;
388
389 # Going to manually validate some items.
390 # custid and city are automagic.
391 return if !validateInput();
392
393# Several different cases here.
394# Static IP vs netblock
395# + Different flavours of static IP
396# + Different flavours of netblock
397
398 if ($webvar{alloctype} =~ /^.i$/) {
399 my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
400 $page->param(staticip => 1);
401 $page->param(poollist => $plist) if $plist;
402 $cidr = "Single static IP";
403##fixme: need to handle "no available pools"
404
405 } else { # end show pool options
406
407 if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
408 $cidr = new NetAddr::IP $webvar{block};
409 $webvar{maskbits} = $cidr->masklen;
410 } else { # done with direct freeblocks assignment
411
412 if (!$webvar{maskbits}) {
413 $page->param(err => "Please specify a CIDR mask length.");
414 return;
415 }
416
417##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
418 my $failmsg = "No suitable free block found.<br>\n";
419 if ($webvar{alloctype} eq 'rm') {
420 $failmsg .= "We do not have a free routeable block of that size.<br>\n".
421 "You will have to either route a set of smaller netblocks or a single smaller netblock.";
422 } else {
423 if ($webvar{alloctype} =~ /^.[pc]$/) {
424 $failmsg .= "You will have to route another superblock from one of the<br>\n".
425 "master blocks or chose a smaller block size for the pool.";
426 } else {
427 if (!$webvar{pop}) {
428 $page->param(err => 'Please select a POP to route the block from/through.');
429 return;
430 }
431 $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
432 "from one of the master blocks or chose a smaller blocksize.";
433 }
434 }
435
436 $cidr = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype}, $webvar{city}, $webvar{pop},
437 (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
438 if (!$cidr) {
439 $page->param(err => $failmsg);
440 return;
441 }
442 $cidr = new NetAddr::IP $cidr;
443 } # check for freeblocks assignment or IPDB-controlled assignment
444
445 $alloc_from = "$cidr";
446
447 # If the block to be allocated is smaller than the one we found,
448 # figure out the "real" block to be allocated.
449 if ($cidr->masklen() ne $webvar{maskbits}) {
450 my $maskbits = $cidr->masklen();
451 my @subblocks;
452 while ($maskbits++ < $webvar{maskbits}) {
453 @subblocks = $cidr->split($maskbits);
454 }
455 $cidr = $subblocks[0];
456 }
457 } # if ($webvar{alloctype} =~ /^.i$/)
458
459## node hack
460 if ($webvar{node} && $webvar{node} ne '-') {
461 my $nodename = getNodeName($ip_dbh, $webvar{node});
462 $page->param(nodename => $nodename);
463 $page->param(nodeid => $webvar{node});
464 }
465## end node hack
466
467 # Stick in the allocation data
468 $page->param(alloc_type => $webvar{alloctype});
469 $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
470 $page->param(alloc_from => $alloc_from);
471 $page->param(cidr => $cidr);
472 $page->param(city => $q->escapeHTML($webvar{city}));
473 $page->param(custid => $webvar{custid});
474 $page->param(circid => $q->escapeHTML($webvar{circid}));
475 $page->param(desc => $q->escapeHTML($webvar{desc}));
476
477##fixme: find a way to have the displayed copy have <br> substitutions
478# for newlines, and the <input> value have either encoded or bare newlines.
479# Also applies to privdata.
480 $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
481
482 # Check to see if user is allowed to do anything with sensitive data
483 my $privdata = '';
484 $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'))
485 if $IPDBacl{$authuser} =~ /s/;
486
487 # Yay! This now has it's very own little home.
488 $page->param(billinguser => $webvar{userid})
489 if $webvar{userid};
490
491##fixme: this is only needed iff confirm.tmpl and
492# confirmRemove.tmpl are merged (quite possible, just
493# a little tedious)
494 $page->param(action => "insert");
495
496} # end confirmAssign
497
498
499# Do the work of actually inserting a block in the database.
500sub insertAssign {
501 if ($IPDBacl{$authuser} !~ /a/) {
502 $aclerr = 'addblock';
503 return;
504 }
505 # Some things are done more than once.
506 return if !validateInput();
507
508 if (!defined($webvar{privdata})) {
509 $webvar{privdata} = '';
510 }
511 # $code is "success" vs "failure", $msg contains OK for a
512 # successful netblock allocation, the IP allocated for static
513 # IP, or the error message if an error occurred.
514
515 my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
516 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
517 $webvar{circid}, $webvar{privdata}, $webvar{node});
518
519 if ($code eq 'OK') {
520 if ($webvar{alloctype} =~ /^.i$/) {
521 $msg =~ s|/32||;
522 $page->param(staticip => $msg);
523 $page->param(custid => $webvar{custid});
524 $page->param(billinguser => $webvar{billinguser});
525 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
526 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
527 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
528 } else {
529 my $netblock = new NetAddr::IP $webvar{fullcidr};
530 $page->param(fullcidr => $webvar{fullcidr});
531 $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
532 $page->param(custid => $webvar{custid});
533 if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
534 $page->param(billinguser => $webvar{billinguser});
535 $page->param(custid => $webvar{custid});
536 $page->param(netaddr => $netblock->addr);
537 $page->param(masklen => $netblock->masklen);
538 }
539 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
540 "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
541 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
542 }
543 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
544 "'$webvar{alloctype}' ($msg)";
545 } else {
546 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
547 "'$webvar{alloctype}' by $authuser failed: '$msg'";
548 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
549 " failed:<br>\n$msg\n");
550 }
551
552} # end insertAssign()
553
554
555# Does some basic checks on common input data to make sure nothing
556# *really* weird gets in to the database through this script.
557# Does NOT do complete input validation!!!
558sub validateInput {
559 if ($webvar{city} eq '-') {
560 $page->param(err => 'Please choose a city');
561 return;
562 }
563
564 # Alloctype check.
565 chomp $webvar{alloctype};
566 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
567 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
568 # managing to call things in such a way as to cause this deserves a cryptic error.
569 $page->param(err => 'Invalid alloctype');
570 return;
571 }
572
573 # CustID check
574 # We have different handling for customer allocations and "internal" or "our" allocations
575 if ($def_custids{$webvar{alloctype}} eq '') {
576 if (!$webvar{custid}) {
577 $page->param(err => 'Please enter a customer ID.');
578 return;
579 }
580 if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
581 # Force uppercase for now...
582 $webvar{custid} =~ tr/a-z/A-Z/;
583 # Crosscheck with billing.
584 my $status = CustIDCK->custid_exist($webvar{custid});
585 if ($CustIDCK::Error) {
586 $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
587 return;
588 }
589 if (!$status) {
590 $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
591 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
592 "non-customer assignments.");
593 return;
594 }
595 }
596# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
597 } else {
598 # New! Improved! And now Loaded From The Database!!
599 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
600 $webvar{custid} = $def_custids{$webvar{alloctype}};
601 }
602 }
603
604 # Check POP location
605 my $flag;
606 if ($webvar{alloctype} eq 'rm') {
607 $flag = 'for a routed netblock';
608 foreach (@poplist) {
609 if (/^$webvar{city}$/) {
610 $flag = 'n';
611 last;
612 }
613 }
614 } else {
615 $flag = 'n';
616##fixme: hook to force-set POP or city on certain alloctypes
617# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
618 if ($webvar{pop} =~ /^-$/) {
619 $flag = 'to route the block from/through';
620 }
621 }
622
623 # if the alloctype has a restricted city/POP list as determined above,
624 # and the reqested city/POP does not match that list, complain
625 if ($flag ne 'n') {
626 $page->param(err => "Please choose a valid POP location $flag. Valid ".
627 "POP locations are currently:<br>\n".join (" - ", @poplist));
628 return;
629 }
630
631 return 'OK';
632} # end validateInput
633
634
635# Displays details of a specific allocation in a form
636# Allows update/delete
637# action=edit
638sub edit {
639
640 my $sql;
641
642 # Two cases: block is a netblock, or block is a static IP from a pool
643 # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
644##fixme: allow "SWIP" (publication to rWHOIS) of static IP data
645 if ($webvar{block} =~ /\/32$/) {
646 $sql = "select ip,custid,type,city,circuitid,description,notes,modifystamp,privdata from poolips where ip='$webvar{block}'";
647 } else {
648 $sql = "select cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,swip from allocations where cidr='$webvar{block}'"
649 }
650
651 # gotta snag block info from db
652 $sth = $ip_dbh->prepare($sql);
653 $sth->execute;
654 my @data = $sth->fetchrow_array;
655
656 # Clean up extra whitespace on alloc type
657 $data[2] =~ s/\s//;
658
659 # We can't let the city be changed here; this block is a part of
660 # a larger routed allocation and therefore by definition can't be moved.
661 # block and city are static.
662##fixme
663# Needs thinking. Have to allow changes to city to correct errors, no?
664# Also have areas where a routed block at a POP serves "many" cities/towns/named crossroads
665
666# @data: cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,swip
667
668 $page->param(block => $webvar{block});
669
670 $page->param(custid => $data[1]);
671 $page->param(city => $data[3]);
672 $page->param(circid => $data[4]);
673 $page->param(desc => $data[5]);
674 $page->param(notes => $data[6]);
675
676##fixme The check here should be built from the database
677# Need to expand to support pool types too
678 if ($data[2] =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
679 $page->param(changetype => 1);
680 $page->param(alloctype => [
681 { selme => ($data[2] eq 'me'), type => "me", disptype => "Dialup netblock" },
682 { selme => ($data[2] eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
683 { selme => ($data[2] eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
684 { selme => ($data[2] eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
685 { selme => ($data[2] eq 'cn'), type => "cn", disptype => "Customer netblock" },
686 { selme => ($data[2] eq 'en'), type => "en", disptype => "End-use netblock" },
687 { selme => ($data[2] eq 'in'), type => "in", disptype => "Internal netblock" },
688 ]
689 );
690 } else {
691 $page->param(disptype => $disp_alloctypes{$data[2]});
692 $page->param(type => $data[2]);
693 }
694
695## node hack
696 my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $webvar{block});
697 $page->param(havenodeid => $nodeid);
698
699 if ($data[2] eq 'fr' || $data[2] eq 'bi') {
700 $page->param(typesupportsnodes => 1);
701 $page->param(nodename => $nodename);
702
703##fixme: this whole hack needs cleanup and generalization for all alloctypes
704##fixme: arguably a bug that presence of a nodeid implies it can be changed..
705# but except for manual database changes, only the two types fr and bi can
706# (currently) have a nodeid set in the first place.
707 if ($IPDBacl{$authuser} =~ /c/) {
708 my $nlist = getNodeList($ip_dbh);
709 foreach (@{$nlist}) {
710 $$_{selme} = ($$_{node_id} == $nodeid);
711 }
712 $page->param(nodelist => $nlist);
713 }
714 }
715## end node hack
716
717 my ($lastmod,undef) = split /\s+/, $data[7];
718 $page->param(lastmod => $lastmod);
719
720 # not happy with the upside-down logic, but...
721 $page->param(swipable => $data[2] !~ /.i/);
722 $page->param(swip => $data[10] ne 'n');
723
724 # Check to see if we can display sensitive data
725 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
726 $page->param(privdata => $data[8]);
727
728 # ACL trickery - these two template booleans control the presence of all form/input tags
729 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
730 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
731
732} # edit()
733
734
735# Stuff new info about a block into the db
736# action=update
737sub update {
738 if ($IPDBacl{$authuser} !~ /c/) {
739 $aclerr = 'updateblock';
740 return;
741 }
742
743 # Make sure incoming data is in correct format - custID among other things.
744 return if !validateInput;
745
746 my %updargs = (
747 custid => $webvar{custid},
748 city => $webvar{city},
749 description => $webvar{desc},
750 notes => $webvar{notes},
751 circuitid => $webvar{circid},
752 block => $webvar{block},
753 type => $webvar{alloctype},
754 );
755
756 # Semioptional values
757 $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
758 $updargs{node} = $webvar{node} if $webvar{node};
759
760 my ($code,$msg) = updateBlock($ip_dbh, %updargs);
761
762 if ($code eq 'FAIL') {
763 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
764 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
765 return;
766 }
767
768 # If we get here, the operation succeeded.
769 syslog "notice", "$authuser updated $webvar{block}";
770##fixme: log details of the change? old way is in the .debug stream anyway.
771##fixme: need to wedge something in to allow "update:field" notifications
772## hmm. how to tell what changed? O_o
773mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
774 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
775
776## node hack
777 if ($webvar{node} && $webvar{node} ne '-') {
778 my $nodename = getNodeName($ip_dbh, $webvar{node});
779 $page->param(nodename => $nodename);
780 }
781## end node hack
782
783 # Link back to browse-routed or list-pool page on "Update complete" page.
784 my $cblock; # to contain the CIDR of the container block we're retrieving.
785 my $sql;
786 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
787 $page->param(backpool => 1);
788 $sql = "select pool from poolips where ip='$webvar{block}'";
789 } else {
790 $sql = "select cidr from routed where cidr >>= '$webvar{block}'";
791 }
792 # I define there to be no errors on this operation... so we don't need to check for them.
793 $sth = $ip_dbh->prepare($sql);
794 $sth->execute;
795 $sth->bind_columns(\$cblock);
796 $sth->fetch();
797 $sth->finish;
798 $page->param(backblock => $cblock);
799
800 $page->param(cidr => $webvar{block});
801 $page->param(city => $webvar{city});
802 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
803 $page->param(custid => $webvar{custid});
804 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
805 $page->param(circid => $q->escapeHTML($webvar{circid}));
806 $page->param(desc => $q->escapeHTML($webvar{desc}));
807 $page->param(notes => $q->escapeHTML($webvar{notes}));
808 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
809 $page->param(privdata => $webvar{privdata})
810 if $IPDBacl{$authuser} =~ /s/;
811
812} # update()
813
814
815# Delete an allocation.
816sub remove {
817 if ($IPDBacl{$authuser} !~ /d/) {
818 $aclerr = 'delblock';
819 return;
820 }
821
822 # Serves'em right for getting here...
823 if (!defined($webvar{block})) {
824 $page->param(err => "Can't delete a block that doesn't exist");
825 return;
826 }
827
828 my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata);
829
830 if ($webvar{alloctype} eq 'rm') {
831 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
832 $sth->execute();
833
834# This feels... extreme.
835 croak $sth->errstr() if($sth->errstr());
836
837 $sth->bind_columns(\$cidr,\$city);
838 $sth->execute();
839 $sth->fetch || croak $sth->errstr();
840 $custid = "N/A";
841 $alloctype = $webvar{alloctype};
842 $circid = "N/A";
843 $desc = "N/A";
844 $notes = "N/A";
845 $privdata = "N/A";
846
847 } elsif ($webvar{alloctype} eq 'mm') {
848
849 $cidr = $webvar{block};
850 $city = "N/A";
851 $custid = "N/A";
852 $alloctype = $webvar{alloctype};
853 $circid = "N/A";
854 $desc = "N/A";
855 $notes = "N/A";
856 $privdata = "N/A";
857
858 } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m
859
860 # Unassigning a static IP
861 my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata".
862 " from poolips where ip='$webvar{block}'");
863 $sth->execute();
864# croak $sth->errstr() if($sth->errstr());
865
866 $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid,
867 \$privdata);
868 $sth->fetch() || croak $sth->errstr;
869
870 } else { # done with alloctype=~ /^.i$/
871
872 my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata".
873 " from allocations where cidr='$webvar{block}'");
874 $sth->execute();
875# croak $sth->errstr() if($sth->errstr());
876
877 $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc,
878 \$notes, \$privdata);
879 $sth->fetch() || carp $sth->errstr;
880 } # end cases for different alloctypes
881
882 $page->param(block => $cidr);
883 $page->param(disptype => $disp_alloctypes{$alloctype});
884 $page->param(type => $alloctype);
885 $page->param(city => $city);
886 $page->param(custid => $custid);
887 $page->param(circid => $circid);
888 $page->param(desc => $desc);
889 $page->param(notes => $notes);
890 $privdata = '&nbsp;' if $privdata eq '';
891 $page->param(privdata => $privdata) if $IPDBacl{$authuser} =~ /s/;
892 $page->param(delpool => $alloctype =~ /^.[pd]$/);
893
894} # end remove()
895
896
897# Delete an allocation. Return it to the freeblocks table; munge
898# data as necessary to keep as few records as possible in freeblocks
899# to prevent weirdness when allocating blocks later.
900# Remove IPs from pool listing if necessary
901sub finalDelete {
902 if ($IPDBacl{$authuser} !~ /d/) {
903 $aclerr = 'delblock';
904 return;
905 }
906
907 # need to retrieve block data before deleting so we can notify on that
908 my $blockinfo = getBlockData($ip_dbh, $webvar{block});
909
910 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
911
912 $page->param(block => $webvar{block});
913 if ($code eq 'OK') {
914 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block} ".
915 $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
916 mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
917 "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
918 "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
919 "\nDescription: ".$blockinfo->{description}."\n");
920 } else {
921 $page->param(failmsg => $msg);
922 if ($webvar{alloctype} =~ /^.i$/) {
923 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
924 } else {
925 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
926 $page->param(netblock => 1);
927 }
928 }
929
930} # finalDelete
Note: See TracBrowser for help on using the repository browser.