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

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

/trunk

Move SQL for node search page into IPDB.pm. See #34.
Also tweak some handling for the lingering global statement handle
variable, since it may be undefined instead of a statement handle.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 39.9 KB
RevLine 
[4]1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
[8]3###
4# SVN revision info
5# $Date: 2012-10-19 20:15:59 +0000 (Fri, 19 Oct 2012) $
6# SVN revision $Rev: 519 $
7# Last update by $Author: kdeugau $
8###
[417]9# Copyright (C) 2004-2010 - Kris Deugau
[4]10
11use strict;
12use warnings;
13use CGI::Carp qw(fatalsToBrowser);
[517]14use CGI::Simple;
15use HTML::Template;
[4]16use DBI;
17use POSIX qw(ceil);
18use NetAddr::IP;
19
20use Sys::Syslog;
21
[417]22# don't remove! required for GNU/FHS-ish install from tarball
23##uselib##
24
[515]25use CustIDCK;
[417]26use MyIPDB;
27
[431]28openlog "IPDB","pid","$IPDB::syslog_facility";
[4]29
[517]30## Environment. Collect some things, process some things, set some things...
31
[233]32# Collect the username from HTTP auth. If undefined, we're in
33# a test environment, or called without a username.
[4]34my $authuser;
35if (!defined($ENV{'REMOTE_USER'})) {
36 $authuser = '__temptest';
37} else {
38 $authuser = $ENV{'REMOTE_USER'};
39}
40
[517]41# anyone got a better name? :P
42my $thingroot = $ENV{SCRIPT_FILENAME};
43$thingroot =~ s|cgi-bin/main.cgi||;
44
[402]45syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
[4]46
[517]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
[106]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;
[142]64($ip_dbh,$errstr) = connectDB_My;
[106]65if (!$ip_dbh) {
[517]66 $webvar{action} = "dberr";
67} else {
68 initIPDBGlobals($ip_dbh);
[106]69}
[4]70
[517]71# Set up some globals
72$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
[233]73
[517]74my $header = HTML::Template->new(filename => "header.tmpl");
75my $footer = HTML::Template->new(filename => "footer.tmpl");
[233]76
[517]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;
[4]81
82
83#main()
[517]84my $aclerr;
[4]85
86if(!defined($webvar{action})) {
[517]87 $webvar{action} = "index"; #shuts up the warnings.
[4]88}
89
[517]90my $page;
91if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
92 $page = HTML::Template->new(filename => "$webvar{action}.tmpl");
93} else {
94 $page = HTML::Template->new(filename => "dunno.tmpl");
95}
96
[4]97if($webvar{action} eq 'index') {
98 showSummary();
[233]99} elsif ($webvar{action} eq 'addmaster') {
100 if ($IPDBacl{$authuser} !~ /a/) {
[517]101 $aclerr = 'addmaster';
[233]102 }
[4]103} elsif ($webvar{action} eq 'newmaster') {
104
[233]105 if ($IPDBacl{$authuser} !~ /a/) {
[517]106 $aclerr = 'addmaster';
[233]107 } else {
108 my $cidr = new NetAddr::IP $webvar{cidr};
[517]109 $page->param(cidr => "$cidr");
[4]110
[371]111 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
[4]112
[371]113 if ($code eq 'FAIL') {
[320]114 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
[517]115 $page->param(err => $msg);
[233]116 } else {
117 syslog "info", "$authuser added master block $webvar{cidr}";
118 }
[4]119
[233]120 } # ACL check
121
[4]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 listPool();
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}
[397]156elsif ($webvar{action} eq 'nodesearch') {
[519]157 my $nodelist = getNodeList($ip_dbh);
158 $page->param(nodelist => $nodelist);
[517]159}
[397]160
[517]161# DB failure. Can't do much here, really.
162elsif ($webvar{action} eq 'dberr') {
163 $page->param(errmsg => $errstr);
[397]164}
165
[517]166# Default is an error. It shouldn't be possible to get here unless you're
167# randomly feeding in values for webvar{action}.
[4]168else {
169 my $rnd = rand 500;
170 my $boing = sprintf("%.2f", rand 500);
[517]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]);
[4]184}
[111]185## Finally! Done with that NASTY "case" emulation!
[4]186
187
[517]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}
[4]196
[519]197$sth->finish if $sth;
[106]198# Clean up IPDB globals, DB handle, etc.
[111]199finish($ip_dbh);
[199]200
[517]201## Do all our printing here so we can generate errors and stick them into the slots in the templates.
[199]202
[517]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
[106]213# Just in case something waaaayyy down isn't in place
214# properly... we exit explicitly.
[517]215exit 0;
[4]216
217
218# Initial display: Show master blocks with total allocated subnets, total free subnets
[118]219sub showSummary {
[106]220 my %allocated;
221 my %free;
222 my %routed;
223 my %bigfree;
224
[118]225 # Count the allocations.
226 $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?");
227 foreach my $master (@masterblocks) {
228 $sth->execute("$master");
229 $sth->bind_columns(\$allocated{"$master"});
230 $sth->fetch();
[4]231 }
232
[118]233 # Count routed blocks
234 $sth = $ip_dbh->prepare("select count(*) from routed where cidr <<= ?");
235 foreach my $master (@masterblocks) {
236 $sth->execute("$master");
237 $sth->bind_columns(\$routed{"$master"});
238 $sth->fetch();
[4]239 }
240
[118]241 # Count the free blocks.
[231]242 $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
243 "(routed='y' or routed='n')");
[118]244 foreach my $master (@masterblocks) {
245 $sth->execute("$master");
246 $sth->bind_columns(\$free{"$master"});
247 $sth->fetch();
[4]248 }
249
[118]250 # Find the largest free block in each master
[231]251 $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
252 "(routed='y' or routed='n') order by maskbits limit 1");
[118]253 foreach my $master (@masterblocks) {
254 $sth->execute("$master");
255 $sth->bind_columns(\$bigfree{"$master"});
256 $sth->fetch();
257 }
258
[517]259 # Assemble the data to stuff into the template.
260 my @masterlist;
261 my $rowclass=0;
[4]262 foreach my $master (@masterblocks) {
[517]263 my %row = (
264 rowclass => $rowclass++ % 2,
265 master => "$master",
266 routed => $routed{"$master"},
267 allocated => $allocated{"$master"},
268 free => $free{"$master"},
269 bigfree => ( ($bigfree{"$master"} eq '') ? ("&lt;NONE&gt;") : ("/".$bigfree{"$master"}) )
[4]270 );
[517]271 push (@masterlist, \%row);
[4]272 }
[517]273 $page->param(masterlist => \@masterlist);
[4]274
[517]275 $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
276
[4]277} # showSummary
278
279
280# Display detail on master
281# Alrighty then! We're showing routed blocks within a single master this time.
282# We should be able to steal code from showSummary(), and if I'm really smart
283# I'll figger a way to munge the two together. (Once I've done that, everything
284# else should follow. YMMV.)
285sub showMaster {
286
[517]287 $page->param(master => $webvar{block});
[4]288
[106]289 my %allocated;
290 my %free;
[517]291 my %cities;
[106]292 my %bigfree;
293
[4]294 my $master = new NetAddr::IP $webvar{block};
295 my @localmasters;
296
[118]297 # Fetch only the blocks relevant to this master
[157]298 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr <<= '$master' order by cidr");
[4]299 $sth->execute();
300
301 my $i=0;
302 while (my @data = $sth->fetchrow_array()) {
303 my $cidr = new NetAddr::IP $data[0];
[118]304 $localmasters[$i++] = $cidr;
305 $free{"$cidr"} = 0;
306 $allocated{"$cidr"} = 0;
307 $bigfree{"$cidr"} = 128;
[4]308 # Retain the routing destination
[517]309 $cities{"$cidr"} = $data[1];
[4]310 }
311
[118]312 # Check if there were actually any blocks routed from this master
[4]313 if ($i > 0) {
314
[118]315 # Count the allocations
316 $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?");
317 foreach my $master (@localmasters) {
318 $sth->execute("$master");
319 $sth->bind_columns(\$allocated{"$master"});
320 $sth->fetch();
[4]321 }
322
[118]323 # Count the free blocks.
[231]324 $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
325 "(routed='y' or routed='n')");
[118]326 foreach my $master (@localmasters) {
327 $sth->execute("$master");
328 $sth->bind_columns(\$free{"$master"});
329 $sth->fetch();
[4]330 }
331
[118]332 # Get the size of the largest free block
[231]333 $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
334 "(routed='y' or routed='n') order by maskbits limit 1");
[118]335 foreach my $master (@localmasters) {
336 $sth->execute("$master");
337 $sth->bind_columns(\$bigfree{"$master"});
338 $sth->fetch();
[4]339 }
340
[517]341 my @routed;
342 my $rowclass = 0;
[4]343 foreach my $master (@localmasters) {
[517]344 my %row = (
345 rowclass => $rowclass++ % 2,
346 block => "$master",
347 city => $cities{"$master"},
348 nsubs => $allocated{"$master"},
349 nfree => $free{"$master"},
350 lfree => ( ($bigfree{"$master"} eq 128) ? ("&lt;NONE&gt;") : ("/".$bigfree{"$master"}) )
351 );
352 push @routed, \%row;
[4]353 }
[517]354 $page->param(routedlist => \@routed);
[4]355
356 } # end check for existence of routed blocks in master
357
[517]358 $page->param(delmaster => ($IPDBacl{$authuser} =~ /d/));
[4]359
360 # Snag the free blocks.
361 my $count = 0;
[118]362 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr <<='$master' and ".
363 "routed='n' order by cidr");
[4]364 $sth->execute();
[517]365 my @unrouted;
366 my $rowclass = 0;
[4]367 while (my @data = $sth->fetchrow_array()) {
368 my $cidr = new NetAddr::IP $data[0];
[517]369 my %row = (
370 rowclass => $rowclass++ % 2,
371 fblock => "$cidr",
372 frange => $cidr->range
373 );
374 push @unrouted, \%row;
[4]375 }
[517]376 $page->param(unrouted => \@unrouted);
[4]377
378} # showMaster
379
380
381# Display details of a routed block
382# Alrighty then! We're showing allocations within a routed block this time.
383# We should be able to steal code from showSummary() and showMaster(), and if
384# I'm really smart I'll figger a way to munge all three together. (Once I've
385# done that, everything else should follow. YMMV.
386# This time, we check the database before spewing, because we may
387# not have anything useful to spew.
388sub showRBlock {
389
390 my $master = new NetAddr::IP $webvar{block};
391
[157]392 $sth = $ip_dbh->prepare("select city from routed where cidr='$master'");
[4]393 $sth->execute;
[517]394 my ($rcity) = $sth->fetchrow_array;
[4]395
[517]396 $page->param(master => "$master");
397 $page->param(rcity => $rcity);
[4]398
[118]399 # Snag the allocations for this block
[320]400 $sth = $ip_dbh->prepare("select cidr,city,type,custid,swip,description".
[157]401 " from allocations where cidr <<= '$master' order by cidr");
[4]402 $sth->execute();
403
[380]404 # hack hack hack
405 # set up to flag swip=y records if they don't actually have supporting data in the customers table
406 my $custsth = $ip_dbh->prepare("select count(*) from customers where custid=?");
407
[517]408 my $rowclass = 0;
409 my @blocklist;
410 while (my ($cidr,$city,$type,$custid,$swip,$desc) = $sth->fetchrow_array()) {
411 $custsth->execute($custid);
[380]412 my ($ncust) = $custsth->fetchrow_array();
413
[517]414 my %row = (
415 rowclass => $rowclass++ % 2,
416 block => $cidr,
417 city => $city,
418 type => $disp_alloctypes{$type},
419 custid => $custid,
420 swip => ($swip eq 'y' ? ($ncust == 0 ? 'Yes<small>*</small>' : 'Yes') : 'No'),
421 desc => $desc
422 );
423 $row{subblock} = ($type =~ /^.r$/); # hmf. wonder why these won't work in the hash declaration...
424 $row{listpool} = ($type =~ /^.[pd]$/);
425 push (@blocklist, \%row);
[4]426 }
[517]427 $page->param(blocklist => \@blocklist);
[4]428
[517]429 $page->param(delrouted => $IPDBacl{$authuser} =~ /d/);
[4]430
431 # Snag the free blocks. We don't really *need* to be pedantic about avoiding
432 # unrouted free blocks, but it's better to let the database do the work if we can.
[517]433 $rowclass = 0;
434 my @unassigned;
[186]435 $sth = $ip_dbh->prepare("select cidr,routed from freeblocks where cidr <<= '$master'".
436 " order by cidr");
[4]437 $sth->execute();
[517]438 while (my ($cidr_db,$routed) = $sth->fetchrow_array()) {
439 my $cidr = new NetAddr::IP $cidr_db;
440
441 my %row = (
442 rowclass => $rowclass++ % 2,
443 subblock => ($routed ne 'y' && $routed ne 'n'),
444 fblock => $cidr_db,
445 fbtype => $routed,
446 frange => $cidr->range,
447 );
448 push @unassigned, \%row;
[4]449 }
[517]450 $page->param(unassigned => \@unassigned);
[4]451
452} # showRBlock
453
454
455# List the IPs used in a pool
456sub listPool {
457
458 my $cidr = new NetAddr::IP $webvar{pool};
459
[517]460 $page->param(block => $webvar{pool});
461 $page->param(netip => $cidr->addr);
462 $cidr++;
463 $page->param(gate => $cidr->addr);
464 $cidr--; $cidr--;
465 $page->param(bcast => $cidr->addr);
466 $page->param(mask => $cidr->mask);
[157]467
[4]468 # Snag pool info for heading
[517]469 $sth = $ip_dbh->prepare("select type,city from allocations where cidr=?");
470 $sth->execute($webvar{pool});
471 my ($pooltype, $poolcity) = $sth->fetchrow_array;
[4]472
[517]473 $page->param(disptype => $disp_alloctypes{$pooltype});
474 $page->param(city => $poolcity);
475
[157]476 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
[517]477 $page->param(realblock => $pooltype =~ /^.d$/);
[4]478
479# probably have to add an "edit IP allocation" link here somewhere.
480
[157]481 $sth = $ip_dbh->prepare("select ip,custid,available,description,type".
482 " from poolips where pool='$webvar{pool}' order by ip");
[4]483 $sth->execute;
[517]484 my @poolips;
485 my $rowclass = 0;
486 while (my ($ip,$custid,$available,$desc,$type) = $sth->fetchrow_array) {
487 my %row = (
488 rowclass => $rowclass++ % 2,
489 ip => $ip,
490 custid => $custid,
491 available => $available,
492 desc => $desc,
493 maydel => $IPDBacl{$authuser} =~ /d/,
494 delme => $available eq 'n'
[4]495 );
[517]496 push @poolips, \%row;
[4]497 }
[517]498 $page->param(poolips => \@poolips);
[4]499
500} # end listPool
501
502
[106]503# Show "Add new allocation" page. Note that the actual page may
504# be one of two templates, and the lists come from the database.
[4]505sub assignBlock {
506
[233]507 if ($IPDBacl{$authuser} !~ /a/) {
[517]508 $aclerr = 'addblock';
[233]509 return;
510 }
511
[517]512 # hack pthbttt eww
513 $webvar{block} = '' if !$webvar{block};
[21]514
[517]515# hmm. TMPL_IF block and TMPL_ELSE block on these instead?
516 $page->param(rowa => 'row'.($webvar{block} eq '' ? 1 : 0));
517 $page->param(rowb => 'row'.($webvar{block} eq '' ? 0 : 1));
518 $page->param(block => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
519 $page->param(iscontained => ($webvar{fbtype} && $webvar{fbtype} ne 'y'));
520
[21]521 # New special case- block to assign is specified
522 if ($webvar{block} ne '') {
523 my $block = new NetAddr::IP $webvar{block};
[187]524
[517]525 # Handle contained freeblock allocation.
[187]526 # This is a little dangerous, as it's *theoretically* possible to
527 # get fbtype='n' (aka a non-routed freeblock). However, should
528 # someone manage to get there, they get what they deserve.
529 if ($webvar{fbtype} ne 'y') {
[517]530 # Snag the type of the container block from the database.
[187]531 $sth = $ip_dbh->prepare("select type from allocations where cidr >>='$block'");
532 $sth->execute;
533 my @data = $sth->fetchrow_array;
534 $data[0] =~ s/c$/r/; # Munge the type into the correct form
[517]535 $page->param(fbdisptype => $list_alloctypes{$data[0]});
536 $page->param(type => $data[0]);
[187]537 } else {
538 $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 500 ".
[191]539 "and type not like '_i' and type not like '_r' order by listorder");
[187]540 $sth->execute;
[517]541 my @typelist;
542 my $selflag = 0;
[187]543 while (my @data = $sth->fetchrow_array) {
[517]544 my %row = (tval => $data[0],
545 type => $data[1],
546 sel => ($selflag == 0 ? ' selected' : '')
547 );
548 push (@typelist, \%row);
549 $selflag++;
[187]550 }
[517]551 $page->param(typelist => \@typelist);
[106]552 }
[21]553 } else {
[517]554 my @masterlist;
[31]555 foreach my $master (@masterblocks) {
[517]556 my %row = (master => "$master");
557 push (@masterlist, \%row);
[31]558 }
[517]559 $page->param(masterlist => \@masterlist);
560
561 my @pops;
[92]562 foreach my $pop (@poplist) {
[517]563 my %row = (pop => $pop);
564 push (@pops, \%row);
[92]565 }
[517]566 $page->param(pops => \@pops);
567
568 # could arguably include routing (500) in the list, but ATM it doesn't
569 # make sense, and in any case that shouldn't be structurally possible here.
570 $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder <= 500 order by listorder");
[106]571 $sth->execute;
[517]572 my @typelist;
573 my $selflag = 0;
[106]574 while (my @data = $sth->fetchrow_array) {
[517]575 my %row = (tval => $data[0],
576 type => $data[1],
577 sel => ($selflag == 0 ? ' selected' : '')
578 );
579 push (@typelist, \%row);
580 $selflag++;
[106]581 }
[517]582 $page->param(typelist => \@typelist);
[21]583 }
[517]584
585 my @cities;
[92]586 foreach my $city (@citylist) {
[517]587 my %row = (city => $city);
588 push (@cities, \%row);
[92]589 }
[517]590 $page->param(citylist => \@cities);
[4]591
[397]592## node hack
593 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
594 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
[517]595 my @nodes;
[397]596 while (my ($nid,$nname) = $sth->fetchrow_array()) {
[517]597 my %row = (nid => $nid, nname => $nname);
598 push (@nodes, \%row);
[397]599 }
[517]600 $page->param(nodelist => \@nodes);
[397]601## end node hack
602
[517]603 $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
[284]604
[4]605} # assignBlock
606
607
608# Take info on requested IP assignment and see what we can provide.
609sub confirmAssign {
[233]610 if ($IPDBacl{$authuser} !~ /a/) {
[517]611 $aclerr = 'addblock';
[233]612 return;
613 }
[4]614
615 my $cidr;
616 my $alloc_from;
617
618 # Going to manually validate some items.
619 # custid and city are automagic.
[111]620 return if !validateInput();
[4]621
622# Several different cases here.
623# Static IP vs netblock
624# + Different flavours of static IP
625# + Different flavours of netblock
626
[157]627 if ($webvar{alloctype} =~ /^.i$/) {
[4]628 my ($base,undef) = split //, $webvar{alloctype}; # split into individual chars
629
[214]630# Ewww. But it works.
631 $sth = $ip_dbh->prepare("SELECT (SELECT city FROM allocations WHERE cidr=poolips.pool), ".
632 "poolips.pool, COUNT(*) FROM poolips,allocations WHERE poolips.available='y' AND ".
[442]633 "poolips.pool=allocations.cidr AND allocations.city='$webvar{pop}' AND poolips.type LIKE '".$base."_' ".
[214]634 "GROUP BY pool");
[4]635 $sth->execute;
636 my $optionlist;
[517]637
638 my @poollist;
639 while (my ($poolcit,$poolblock,$poolfree) = $sth->fetchrow_array) {
[214]640 # city,pool cidr,free IP count
[517]641 if ($poolfree > 0) {
642 my %row = (poolcit => $poolcit, poolblock => $poolblock, poolfree => $poolfree);
643 push (@poollist, \%row);
[214]644 }
[4]645 }
[517]646 $page->param(staticip => 1);
647 $page->param(poollist => \@poollist);
[4]648 $cidr = "Single static IP";
[517]649##fixme: need to handle "no available pools"
[4]650
651 } else { # end show pool options
[21]652
653 if ($webvar{fbassign} eq 'y') {
654 $cidr = new NetAddr::IP $webvar{block};
655 $webvar{maskbits} = $cidr->masklen;
656 } else { # done with direct freeblocks assignment
657
658 if (!$webvar{maskbits}) {
[517]659 $page->param(err => "Please specify a CIDR mask length.");
[111]660 return;
[21]661 }
662 my $sql;
663 my $city;
664 my $failmsg;
[320]665 my $extracond = '';
666 if ($webvar{allocfrom} eq '-') {
667 $extracond = ($webvar{allowpriv} eq 'on' ? '' :
668 " and not (cidr <<= '192.168.0.0/16'".
669 " or cidr <<= '10.0.0.0/8'".
670 " or cidr <<= '172.16.0.0/12')");
671 }
672 my $sortorder;
[187]673 if ($webvar{alloctype} eq 'rm') {
[31]674 if ($webvar{allocfrom} ne '-') {
675 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
[320]676 " and cidr <<= '$webvar{allocfrom}'";
677 $sortorder = "maskbits desc";
[31]678 } else {
[320]679 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'";
680 $sortorder = "maskbits desc";
[31]681 }
[21]682 $failmsg = "No suitable free block found.<br>\nWe do not have a free".
683 " routeable block of that size.<br>\nYou will have to either route".
684 " a set of smaller netblocks or a single smaller netblock.";
[4]685 } else {
[118]686##fixme
687# This section needs serious Pondering.
[214]688 # Pools of most types get assigned to the POP they're "routed from"
[187]689 # This includes WAN blocks and other netblock "containers"
[214]690 # This does NOT include cable pools.
691 if ($webvar{alloctype} =~ /^.[pc]$/) {
[37]692 $city = $webvar{city};
[21]693 $failmsg = "No suitable free block found.<br>\nYou will have to route another".
[442]694 " superblock from one of the<br>\nmaster blocks or chose a smaller".
[21]695 " block size for the pool.";
696 } else {
[517]697 if (!$webvar{pop}) {
698 $page->param(err => 'Please select a POP to route the block from/through.');
699 return;
700 }
[21]701 $city = $webvar{pop};
702 $failmsg = "No suitable free block found.<br>\nYou will have to route another".
[442]703 " superblock to $webvar{pop}<br>\nfrom one of the master blocks or".
[21]704 " chose a smaller blocksize.";
705 }
[300]706 if (defined $webvar{allocfrom} && $webvar{allocfrom} ne '-') {
[157]707 $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
[186]708 " and cidr <<= '$webvar{allocfrom}' and routed='".
[320]709 (($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
710 $sortorder = "maskbits desc,cidr";
[31]711 } else {
[157]712 $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
[320]713 " and routed='".(($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
714 $sortorder = "maskbits desc,cidr";
[31]715 }
[4]716 }
[320]717 $sql = $sql.$extracond." order by ".$sortorder;
[21]718 $sth = $ip_dbh->prepare($sql);
719 $sth->execute;
720 my @data = $sth->fetchrow_array();
721 if ($data[0] eq "") {
[517]722 $page->param(err => $failmsg);
[111]723 return;
[21]724 }
725 $cidr = new NetAddr::IP $data[0];
726 } # check for freeblocks assignment or IPDB-controlled assignment
[4]727
[517]728 $alloc_from = "$cidr";
[4]729
730 # If the block to be allocated is smaller than the one we found,
731 # figure out the "real" block to be allocated.
732 if ($cidr->masklen() ne $webvar{maskbits}) {
733 my $maskbits = $cidr->masklen();
734 my @subblocks;
735 while ($maskbits++ < $webvar{maskbits}) {
736 @subblocks = $cidr->split($maskbits);
737 }
738 $cidr = $subblocks[0];
739 }
[157]740 } # if ($webvar{alloctype} =~ /^.i$/)
[4]741
[397]742## node hack
743 if ($webvar{node} && $webvar{node} ne '-') {
744 $sth = $ip_dbh->prepare("SELECT node_name FROM nodes WHERE node_id=?");
745 $sth->execute($webvar{node});
746 my ($nodename) = $sth->fetchrow_array();
[517]747 $page->param(nodename => $nodename);
748 $page->param(nodeid => $webvar{node});
[397]749 }
750## end node hack
751
[4]752 # Stick in the allocation data
[517]753 $page->param(alloc_type => $webvar{alloctype});
754 $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
755 $page->param(alloc_from => $alloc_from);
756 $page->param(cidr => $cidr);
757 $page->param(city => $q->escapeHTML($webvar{city}));
758 $page->param(custid => $webvar{custid});
759 $page->param(circid => $q->escapeHTML($webvar{circid}));
760 $page->param(desc => $q->escapeHTML($webvar{desc}));
[4]761
[517]762##fixme: find a way to have the displayed copy have <br> substitutions
763# for newlines, and the <input> value have either encoded or bare newlines.
764# Also applies to privdata.
765 $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
766
[284]767 # Check to see if user is allowed to do anything with sensitive data
768 my $privdata = '';
[517]769 $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'))
770 if $IPDBacl{$authuser} =~ /s/;
771
772 # Yay! This now has it's very own little home.
773 $page->param(billinguser => $webvar{userid})
[299]774 if $webvar{userid};
[284]775
[517]776##fixme: this is only needed iff confirm.tmpl and
777# confirmRemove.tmpl are merged (quite possible, just
778# a little tedious)
779 $page->param(action => "insert");
[284]780
[4]781} # end confirmAssign
782
783
784# Do the work of actually inserting a block in the database.
785sub insertAssign {
[233]786 if ($IPDBacl{$authuser} !~ /a/) {
[517]787 $aclerr = 'addblock';
[233]788 return;
789 }
[4]790 # Some things are done more than once.
[111]791 return if !validateInput();
[4]792
[284]793 if (!defined($webvar{privdata})) {
794 $webvar{privdata} = '';
795 }
[106]796 # $code is "success" vs "failure", $msg contains OK for a
797 # successful netblock allocation, the IP allocated for static
798 # IP, or the error message if an error occurred.
[517]799
[106]800 my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
801 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
[397]802 $webvar{circid}, $webvar{privdata}, $webvar{node});
[4]803
[111]804 if ($code eq 'OK') {
[106]805 if ($webvar{alloctype} =~ /^.i$/) {
[300]806 $msg =~ s|/32||;
[517]807 $page->param(staticip => $msg);
808 $page->param(custid => $webvar{custid});
809 $page->param(billinguser => $webvar{billinguser});
[416]810 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
811 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
812 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[106]813 } else {
[301]814 my $netblock = new NetAddr::IP $webvar{fullcidr};
[517]815 $page->param(fullcidr => $webvar{fullcidr});
816 $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
817 $page->param(custid => $webvar{custid});
818 if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
819 $page->param(billinguser => $webvar{billinguser});
820 $page->param(custid => $webvar{custid});
821 $page->param(netaddr => $netblock->addr);
822 $page->param(masklen => $netblock->masklen);
823 }
[416]824 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
825 "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
826 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[4]827 }
[106]828 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
[256]829 "'$webvar{alloctype}' ($msg)";
[111]830 } else {
831 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
832 "'$webvar{alloctype}' by $authuser failed: '$msg'";
[517]833 $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
[157]834 " failed:<br>\n$msg\n");
[106]835 }
[4]836
837} # end insertAssign()
838
839
840# Does some basic checks on common input data to make sure nothing
841# *really* weird gets in to the database through this script.
842# Does NOT do complete input validation!!!
843sub validateInput {
844 if ($webvar{city} eq '-') {
[517]845 $page->param(err => 'Please choose a city');
[111]846 return;
[4]847 }
[138]848
849 # Alloctype check.
[4]850 chomp $webvar{alloctype};
[138]851 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
852 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
853 # managing to call things in such a way as to cause this deserves a cryptic error.
[517]854 $page->param(err => 'Invalid alloctype');
[138]855 return;
856 }
857
858 # CustID check
[4]859 # We have different handling for customer allocations and "internal" or "our" allocations
[214]860 if ($def_custids{$webvar{alloctype}} eq '') {
[4]861 if (!$webvar{custid}) {
[517]862 $page->param(err => 'Please enter a customer ID.');
[111]863 return;
[4]864 }
[400]865 if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
866 # Force uppercase for now...
867 $webvar{custid} =~ tr/a-z/A-Z/;
868 # Crosscheck with billing.
869 my $status = CustIDCK->custid_exist($webvar{custid});
870 if ($CustIDCK::Error) {
[517]871 $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
[400]872 return;
873 }
874 if (!$status) {
[517]875 $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
[420]876 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
[400]877 "non-customer assignments.");
878 return;
879 }
[4]880 }
[400]881# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
[138]882 } else {
[167]883 # New! Improved! And now Loaded From The Database!!
[320]884 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
885 $webvar{custid} = $def_custids{$webvar{alloctype}};
886 }
[4]887 }
[111]888
889 # Check POP location
890 my $flag;
[187]891 if ($webvar{alloctype} eq 'rm') {
[111]892 $flag = 'for a routed netblock';
893 foreach (@poplist) {
894 if (/^$webvar{city}$/) {
895 $flag = 'n';
896 last;
897 }
898 }
899 } else {
900 $flag = 'n';
[442]901##fixme: hook to force-set POP or city on certain alloctypes
902# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
[400]903 if ($webvar{pop} =~ /^-$/) {
[111]904 $flag = 'to route the block from/through';
905 }
906 }
[517]907
908 # if the alloctype has a restricted city/POP list as determined above,
909 # and the reqested city/POP does not match that list, complain
[111]910 if ($flag ne 'n') {
[517]911 $page->param(err => "Please choose a valid POP location $flag. Valid ".
[111]912 "POP locations are currently:<br>\n".join (" - ", @poplist));
913 return;
914 }
915
916 return 'OK';
[4]917} # end validateInput
918
919
920# Displays details of a specific allocation in a form
921# Allows update/delete
922# action=edit
923sub edit {
924
925 my $sql;
926
927 # Two cases: block is a netblock, or block is a static IP from a pool
928 # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
[517]929##fixme: allow "SWIP" (publication to rWHOIS) of static IP data
[4]930 if ($webvar{block} =~ /\/32$/) {
[455]931 $sql = "select ip,custid,type,city,circuitid,description,notes,modifystamp,privdata from poolips where ip='$webvar{block}'";
[4]932 } else {
[455]933 $sql = "select cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,swip from allocations where cidr='$webvar{block}'"
[4]934 }
935
936 # gotta snag block info from db
937 $sth = $ip_dbh->prepare($sql);
938 $sth->execute;
939 my @data = $sth->fetchrow_array;
940
941 # Clean up extra whitespace on alloc type
942 $data[2] =~ s/\s//;
943
944 # We can't let the city be changed here; this block is a part of
945 # a larger routed allocation and therefore by definition can't be moved.
946 # block and city are static.
947##fixme
948# Needs thinking. Have to allow changes to city to correct errors, no?
[517]949# Also have areas where a routed block at a POP serves "many" cities/towns/named crossroads
[4]950
[517]951# @data: cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,swip
[233]952
[517]953 $page->param(block => $webvar{block});
[4]954
[517]955 $page->param(custid => $data[1]);
956 $page->param(city => $data[3]);
957 $page->param(circid => $data[4]);
958 $page->param(desc => $data[5]);
959 $page->param(notes => $data[6]);
[4]960
[187]961##fixme The check here should be built from the database
[517]962# Need to expand to support pool types too
963 if ($data[2] =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
964 $page->param(changetype => 1);
965 $page->param(alloctype => [
966 { selme => ($data[2] eq 'me'), type => "me", disptype => "Dialup netblock" },
967 { selme => ($data[2] eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
968 { selme => ($data[2] eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
969 { selme => ($data[2] eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
970 { selme => ($data[2] eq 'cn'), type => "cn", disptype => "Customer netblock" },
971 { selme => ($data[2] eq 'en'), type => "en", disptype => "End-use netblock" },
972 { selme => ($data[2] eq 'in'), type => "in", disptype => "Internal netblock" },
973 ]
974 );
975 } else {
976 $page->param(disptype => $disp_alloctypes{$data[2]});
977 $page->param(type => $data[2]);
978 }
979
[397]980## node hack
[517]981 $sth = $ip_dbh->prepare("SELECT nodes.node_id,node_name FROM nodes INNER JOIN noderef".
982 " ON nodes.node_id=noderef.node_id WHERE noderef.block='$webvar{block}'");
[397]983 $sth->execute;
[517]984 my ($nodeid,$nodename) = $sth->fetchrow_array();
985 $page->param(havenodeid => $nodeid);
986
987 if ($data[2] eq 'fr' || $data[2] eq 'bi') {
988 $page->param(typesupportsnodes => 1);
989 $page->param(nodename => $nodename);
990
991##fixme: this whole hack needs cleanup and generalization for all alloctypes
992##fixme: arguably a bug that presence of a nodeid implies it can be changed..
993# but except for manual database changes, only the two types fr and bi can
994# (currently) have a nodeid set in the first place.
995 if ($IPDBacl{$authuser} =~ /c/) {
[397]996 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
[517]997 $sth->execute;
998 my @nodelist;
[397]999 while (my ($nid,$nname) = $sth->fetchrow_array()) {
[517]1000 my %row = (
1001 selme => ($nodeid == $nid),
1002 nodeid => $nid,
1003 nodename => $nname,
1004 );
1005 push (@nodelist, \%row);
[397]1006 }
[517]1007 $page->param(nodelist => \@nodelist);
[397]1008 }
1009 }
1010## end node hack
[517]1011
[255]1012 my ($lastmod,undef) = split /\s+/, $data[7];
[517]1013 $page->param(lastmod => $lastmod);
[33]1014
[517]1015 # not happy with the upside-down logic, but...
1016 $page->param(swipable => $data[2] !~ /.i/);
1017 $page->param(swip => $data[10] ne 'n');
[320]1018
[284]1019 # Check to see if we can display sensitive data
[517]1020 $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
1021 $page->param(privdata => $data[8]);
[284]1022
[517]1023 # ACL trickery - these two template booleans control the presence of all form/input tags
1024 $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
1025 $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
[4]1026
1027} # edit()
1028
1029
1030# Stuff new info about a block into the db
1031# action=update
1032sub update {
[284]1033 if ($IPDBacl{$authuser} !~ /c/) {
[517]1034 $aclerr = 'updateblock';
[284]1035 return;
1036 }
[4]1037
[284]1038 # Check to see if we can update restricted data
1039 my $privdata = '';
1040 if ($IPDBacl{$authuser} =~ /s/) {
1041 $privdata = ",privdata='$webvar{privdata}'";
1042 }
1043
[4]1044 # Make sure incoming data is in correct format - custID among other things.
[228]1045 return if !validateInput;
[4]1046
1047 # SQL transaction wrapper
1048 eval {
1049 # Relatively simple SQL transaction here.
1050 my $sql;
[157]1051 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
[517]1052 $sql = "UPDATE poolips SET custid='$webvar{custid}',".
1053 "city=?,description=?,notes=?,".
1054 "circuitid='$webvar{circid}',".
[284]1055 "$privdata where ip='$webvar{block}'";
[4]1056 } else {
[517]1057 $sql = "UPDATE allocations SET custid='$webvar{custid}',".
1058 "city=?,description=?,notes=?,".
1059 "circuitid='$webvar{circid}'$privdata,".
1060 "type='$webvar{alloctype}',".
[320]1061 "swip='".($webvar{swip} eq 'on' ? 'y' : 'n')."' ".
[284]1062 "where cidr='$webvar{block}'";
[4]1063 }
[157]1064 # Log the details of the change.
1065 syslog "debug", $sql;
[4]1066 $sth = $ip_dbh->prepare($sql);
[517]1067 $sth->execute($webvar{city}, $webvar{desc}, $webvar{notes});
[397]1068## node hack
1069 if ($webvar{node}) {
[517]1070 # done with delete/insert so we don't have to worry about funkyness updating a node ref that isn't there
[397]1071 $ip_dbh->do("DELETE FROM noderef WHERE block='$webvar{block}'");
1072 $sth = $ip_dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)");
1073 $sth->execute($webvar{block},$webvar{node});
1074 }
1075## end node hack
[4]1076 $ip_dbh->commit;
1077 };
1078 if ($@) {
[166]1079 my $msg = $@;
[4]1080 eval { $ip_dbh->rollback; };
[166]1081 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
[517]1082 $page->param(err => "Could not update block/IP $webvar{block}: $msg");
[111]1083 return;
[4]1084 }
1085
1086 # If we get here, the operation succeeded.
1087 syslog "notice", "$authuser updated $webvar{block}";
[416]1088##fixme: need to wedge something in to allow "update:field" notifications
1089## hmm. how to tell what changed? O_o
[426]1090mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
[416]1091 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
[4]1092
[517]1093## node hack
1094 if ($webvar{node} && $webvar{node} ne '-') {
1095 $sth = $ip_dbh->prepare("SELECT node_name FROM nodes WHERE node_id=?");
1096 $sth->execute($webvar{node});
1097 my ($nodename) = $sth->fetchrow_array();
1098 $page->param(nodename => $nodename);
1099 }
1100## end node hack
1101
[380]1102 # Link back to browse-routed or list-pool page on "Update complete" page.
1103 my $cblock; # to contain the CIDR of the container block we're retrieving.
1104 my $sql;
1105 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
[517]1106 $page->param(backpool => 1);
[380]1107 $sql = "select pool from poolips where ip='$webvar{block}'";
1108 } else {
1109 $sql = "select cidr from routed where cidr >>= '$webvar{block}'";
1110 }
1111 # I define there to be no errors on this operation... so we don't need to check for them.
1112 $sth = $ip_dbh->prepare($sql);
1113 $sth->execute;
1114 $sth->bind_columns(\$cblock);
1115 $sth->fetch();
1116 $sth->finish;
[517]1117 $page->param(backblock => $cblock);
[380]1118
[517]1119 $page->param(cidr => $webvar{block});
1120 $page->param(city => $webvar{city});
1121 $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
1122 $page->param(custid => $webvar{custid});
1123 $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
1124 $page->param(circid => $q->escapeHTML($webvar{circid}));
1125 $page->param(desc => $q->escapeHTML($webvar{desc}));
1126 $page->param(notes => $q->escapeHTML($webvar{notes}));
1127 $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : "&nbsp;");
1128 $page->param(privdata => $webvar{privdata})
1129 if $IPDBacl{$authuser} =~ /s/;
[4]1130
1131} # update()
1132
1133
1134# Delete an allocation.
[106]1135sub remove {
[233]1136 if ($IPDBacl{$authuser} !~ /d/) {
[517]1137 $aclerr = 'delblock';
[233]1138 return;
1139 }
1140
[4]1141 # Serves'em right for getting here...
1142 if (!defined($webvar{block})) {
[517]1143 $page->param(err => "Can't delete a block that doesn't exist");
[111]1144 return;
[4]1145 }
1146
[284]1147 my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata);
[4]1148
[187]1149 if ($webvar{alloctype} eq 'rm') {
[4]1150 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
1151 $sth->execute();
1152
1153# This feels... extreme.
1154 croak $sth->errstr() if($sth->errstr());
1155
1156 $sth->bind_columns(\$cidr,\$city);
1157 $sth->execute();
1158 $sth->fetch || croak $sth->errstr();
1159 $custid = "N/A";
1160 $alloctype = $webvar{alloctype};
[74]1161 $circid = "N/A";
[4]1162 $desc = "N/A";
1163 $notes = "N/A";
[517]1164 $privdata = "N/A";
[4]1165
1166 } elsif ($webvar{alloctype} eq 'mm') {
[517]1167
[4]1168 $cidr = $webvar{block};
1169 $city = "N/A";
1170 $custid = "N/A";
1171 $alloctype = $webvar{alloctype};
[74]1172 $circid = "N/A";
[4]1173 $desc = "N/A";
1174 $notes = "N/A";
[517]1175 $privdata = "N/A";
1176
[189]1177 } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m
[4]1178
1179 # Unassigning a static IP
[284]1180 my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata".
1181 " from poolips where ip='$webvar{block}'");
[4]1182 $sth->execute();
1183# croak $sth->errstr() if($sth->errstr());
1184
[284]1185 $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid,
1186 \$privdata);
[4]1187 $sth->fetch() || croak $sth->errstr;
1188
[157]1189 } else { # done with alloctype=~ /^.i$/
[4]1190
[284]1191 my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata".
1192 " from allocations where cidr='$webvar{block}'");
[4]1193 $sth->execute();
1194# croak $sth->errstr() if($sth->errstr());
1195
[284]1196 $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc,
1197 \$notes, \$privdata);
[74]1198 $sth->fetch() || carp $sth->errstr;
[4]1199 } # end cases for different alloctypes
1200
[517]1201 $page->param(block => $cidr);
1202 $page->param(disptype => $disp_alloctypes{$alloctype});
1203 $page->param(type => $alloctype);
1204 $page->param(city => $city);
1205 $page->param(custid => $custid);
1206 $page->param(circid => $circid);
1207 $page->param(desc => $desc);
1208 $page->param(notes => $notes);
1209 $privdata = '&nbsp;' if $privdata eq '';
1210 $page->param(privdata => $privdata) if $IPDBacl{$authuser} =~ /s/;
1211 $page->param(delpool => $alloctype =~ /^.[pd]$/);
[4]1212
[517]1213} # end remove()
[4]1214
1215
1216# Delete an allocation. Return it to the freeblocks table; munge
1217# data as necessary to keep as few records as possible in freeblocks
1218# to prevent weirdness when allocating blocks later.
1219# Remove IPs from pool listing if necessary
1220sub finalDelete {
[233]1221 if ($IPDBacl{$authuser} !~ /d/) {
[517]1222 $aclerr = 'delblock';
[233]1223 return;
1224 }
[4]1225
[370]1226 # need to retrieve block data before deleting so we can notify on that
1227 my ($cidr,$custid,$type,$city,$description) = getBlockData($ip_dbh, $webvar{block});
1228
[106]1229 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
[4]1230
[517]1231 $page->param(block => $webvar{block});
[106]1232 if ($code eq 'OK') {
[370]1233 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}".
1234 " $custid, $city, desc='$description'";
[416]1235 mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1236 "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
1237 "CustID: $custid\nCity: $city\nDescription: $description\n");
[106]1238 } else {
[517]1239 $page->param(failmsg => $msg);
[106]1240 if ($webvar{alloctype} =~ /^.i$/) {
1241 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1242 } else {
1243 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
[517]1244 $page->param(netblock => 1);
[4]1245 }
[106]1246 }
[4]1247
1248} # finalDelete
Note: See TracBrowser for help on using the repository browser.