source: branches/htmlform/cgi-bin/main.cgi@ 510

Last change on this file since 510 was 505, checked in by Kris Deugau, 13 years ago

/branches/htmlform

Clean up final reference to printError(), and remove all references
(commented or otherwise) to CommonWeb.pm. Remove the file itself. See #15.
Tweak block-update template for error handling. See #3.

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