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

Last change on this file since 478 was 478, checked in by Kris Deugau, 14 years ago

/branches/htmlfrom

Set a global variable so we can move the site to any level of
web directory. See #17 - note this is an internal autoconfiguration
that shouldn't need to be exposed.
Make code/template structure for setting row colours in tables more
consistent - also tweak search.cgi so it spits out live colour
classes. See #3.

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