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

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

/branches/htmlform

Fix up main.cgi

  • it got contaiminated with the template location for admin.cgi
  • tweak a fixme comment

See #3.

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