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

Last change on this file since 402 was 402, checked in by Kris Deugau, 15 years ago

/trunk

Merge miscellaneous microdifferences that have crept in. See #13.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 48.6 KB
RevLine 
[4]1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3# Started munging from noc.vianet's old IPDB 04/22/2004
[8]4###
5# SVN revision info
6# $Date: 2010-05-11 21:33:34 +0000 (Tue, 11 May 2010) $
7# SVN revision $Rev: 402 $
8# Last update by $Author: kdeugau $
9###
[4]10
11use strict;
12use warnings;
13use CGI::Carp qw(fatalsToBrowser);
14use DBI;
15use CommonWeb qw(:ALL);
[142]16use MyIPDB;
[400]17use CustIDCK;
[4]18use POSIX qw(ceil);
19use NetAddr::IP;
20
21use Sys::Syslog;
22
23openlog "IPDB","pid","local2";
24
[233]25# Collect the username from HTTP auth. If undefined, we're in
26# a test environment, or called without a username.
[4]27my $authuser;
28if (!defined($ENV{'REMOTE_USER'})) {
29 $authuser = '__temptest';
30} else {
31 $authuser = $ENV{'REMOTE_USER'};
32}
33
[402]34syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
[4]35
[106]36# Why not a global DB handle? (And a global statement handle, as well...)
37# Use the connectDB function, otherwise we end up confusing ourselves
38my $ip_dbh;
39my $sth;
40my $errstr;
[142]41($ip_dbh,$errstr) = connectDB_My;
[106]42if (!$ip_dbh) {
[233]43 exitError("Database error: $errstr\n");
[106]44}
45initIPDBGlobals($ip_dbh);
[4]46
[233]47# Headerize! Make sure we replace the $$EXTRA0$$ bit as needed.
48printHeader('', ($IPDBacl{$authuser} =~ /a/ ?
[234]49 '<td align=right><a href="/ip/cgi-bin/main.cgi?action=assign">Add new assignment</a>' : ''
[233]50 ));
51
52
[106]53# Global variables
[4]54my %webvar = parse_post();
55cleanInput(\%webvar);
56
57
58#main()
59
60if(!defined($webvar{action})) {
61 $webvar{action} = "<NULL>"; #shuts up the warnings.
62}
63
64if($webvar{action} eq 'index') {
65 showSummary();
[233]66} elsif ($webvar{action} eq 'addmaster') {
67 if ($IPDBacl{$authuser} !~ /a/) {
68 printError("You shouldn't have been able to get here. Access denied.");
69 } else {
70 open HTML, "<../addmaster.html";
71 print while <HTML>;
72 }
[4]73} elsif ($webvar{action} eq 'newmaster') {
74
[233]75 if ($IPDBacl{$authuser} !~ /a/) {
76 printError("You shouldn't have been able to get here. Access denied.");
77 } else {
[4]78
[233]79 my $cidr = new NetAddr::IP $webvar{cidr};
[4]80
[233]81 print "<div type=heading align=center>Adding $cidr as master block....</div>\n";
[4]82
[371]83 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
[4]84
[371]85 if ($code eq 'FAIL') {
[320]86 carp "Transaction aborted because $msg";
87 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
88 printError("Could not add master block $webvar{cidr} to database: $msg");
[233]89 } else {
90 print "<div type=heading align=center>Success!</div>\n";
91 syslog "info", "$authuser added master block $webvar{cidr}";
92 }
[4]93
[233]94 } # ACL check
95
[4]96} # end add new master
97
98elsif($webvar{action} eq 'showmaster') {
99 showMaster();
100}
101elsif($webvar{action} eq 'showrouted') {
102 showRBlock();
103}
104elsif($webvar{action} eq 'listpool') {
105 listPool();
106}
107
108# Not modified or added; just shuffled
109elsif($webvar{action} eq 'assign') {
110 assignBlock();
111}
112elsif($webvar{action} eq 'confirm') {
113 confirmAssign();
114}
115elsif($webvar{action} eq 'insert') {
116 insertAssign();
117}
118elsif($webvar{action} eq 'edit') {
119 edit();
120}
121elsif($webvar{action} eq 'update') {
122 update();
123}
124elsif($webvar{action} eq 'delete') {
125 remove();
126}
127elsif($webvar{action} eq 'finaldelete') {
128 finalDelete();
129}
[397]130elsif ($webvar{action} eq 'nodesearch') {
131 open HTML, "<../nodesearch.html";
132 my $html = join('',<HTML>);
133 close HTML;
[4]134
[397]135 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
136 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
137 my $nodes = '';
138 while (my ($nid,$nname) = $sth->fetchrow_array()) {
139 $nodes .= "<option value='$nid'>$nname</option>\n";
140 }
141 $html =~ s/\$\$NODELIST\$\$/$nodes/;
142
143 print $html;
144}
145
[4]146# Default is an error. It shouldn't be possible to easily get here.
147# The only way I can think of offhand is to just call main.cgi bare-
148# which is not in any way guaranteed to provide anything useful.
149else {
150 my $rnd = rand 500;
151 my $boing = sprintf("%.2f", rand 500);
152 my @excuses = ("Aether cloudy. Ask again later.","The gods are unhappy with your sacrifice.",
153 "Because one of it's legs are both the same", "*wibble*",
154 "Hey! Stop pushing my buttons!", "I ain't done nuttin'", "9",
155 "8", "9", "10", "11", "12", "13", "14", "15", "16", "17");
156 printAndExit("Error $boing: ".$excuses[$rnd/30.0]);
157}
[111]158## Finally! Done with that NASTY "case" emulation!
[4]159
160
161
[106]162# Clean up IPDB globals, DB handle, etc.
[111]163finish($ip_dbh);
[199]164
165print qq(<div align=right style="position: absolute; right: 30px;">).
166 qq(<a href="/ip/cgi-bin/admin.cgi">Admin tools</a></div><br>\n)
[233]167 if $IPDBacl{$authuser} =~ /A/;
[199]168
[111]169# We print the footer here, so we don't have to do it elsewhere.
170printFooter;
[106]171# Just in case something waaaayyy down isn't in place
172# properly... we exit explicitly.
173exit;
[4]174
175
[111]176
[4]177# args are: a reference to an array with the row to be printed and the
178# class(stylesheet) to use for formatting.
179# if ommitting the class - call the sub as &printRow(\@array)
180sub printRow {
181 my ($rowRef,$class) = @_;
182
183 if (!$class) {
184 print "<tr>\n";
185 } else {
186 print "<tr class=\"$class\">\n";
187 }
188
[135]189ELEMENT: foreach my $element (@$rowRef) {
190 if (!defined($element)) {
191 print "<td></td>\n";
192 next ELEMENT;
193 }
[4]194 $element =~ s|\n|</br>|g;
195 print "<td>$element</td>\n";
196 }
197 print "</tr>";
198} # printRow
199
200
201# Prints table headings. Accepts any number of arguments;
202# each argument is a table heading.
203sub startTable {
204 print qq(<center><table width="98%" cellspacing="0" class="center"><tr>);
205
206 foreach(@_) {
207 print qq(<td class="heading">$_</td>);
208 }
209 print "</tr>\n";
210} # startTable
211
212
213# Initial display: Show master blocks with total allocated subnets, total free subnets
[118]214sub showSummary {
[4]215
216 startTable('Master netblock', 'Routed netblocks', 'Allocated netblocks',
217 'Free netblocks', 'Largest free block');
218
[106]219 my %allocated;
220 my %free;
221 my %routed;
222 my %bigfree;
223
[118]224 # Count the allocations.
225 $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?");
226 foreach my $master (@masterblocks) {
227 $sth->execute("$master");
228 $sth->bind_columns(\$allocated{"$master"});
229 $sth->fetch();
[4]230 }
231
[118]232 # Count routed blocks
233 $sth = $ip_dbh->prepare("select count(*) from routed where cidr <<= ?");
234 foreach my $master (@masterblocks) {
235 $sth->execute("$master");
236 $sth->bind_columns(\$routed{"$master"});
237 $sth->fetch();
[4]238 }
239
[118]240 # Count the free blocks.
[231]241 $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
242 "(routed='y' or routed='n')");
[118]243 foreach my $master (@masterblocks) {
244 $sth->execute("$master");
245 $sth->bind_columns(\$free{"$master"});
246 $sth->fetch();
[4]247 }
248
[118]249 # Find the largest free block in each master
[231]250 $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
251 "(routed='y' or routed='n') order by maskbits limit 1");
[118]252 foreach my $master (@masterblocks) {
253 $sth->execute("$master");
254 $sth->bind_columns(\$bigfree{"$master"});
255 $sth->fetch();
256 }
257
258 # Print the data.
[4]259 my $count=0;
260 foreach my $master (@masterblocks) {
261 my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=showmaster&block=$master\">$master</a>",
262 $routed{"$master"}, $allocated{"$master"}, $free{"$master"},
[161]263 ( ($bigfree{"$master"} eq '') ? ("&lt;NONE&gt;") : ("/".$bigfree{"$master"}) )
[4]264 );
265
266 printRow(\@row, 'color1' ) if($count%2==0);
267 printRow(\@row, 'color2' ) if($count%2!=0);
268 $count++;
269 }
270 print "</table>\n";
[233]271 if ($IPDBacl{$authuser} =~ /a/) {
272 print qq(<a href="/ip/cgi-bin/main.cgi?action=addmaster">Add new master block</a><br><br>\n);
273 }
[4]274 print "Note: Free blocks noted here include both routed and unrouted blocks.\n";
275
276} # showSummary
277
278
279# Display detail on master
280# Alrighty then! We're showing routed blocks within a single master this time.
281# We should be able to steal code from showSummary(), and if I'm really smart
282# I'll figger a way to munge the two together. (Once I've done that, everything
283# else should follow. YMMV.)
284sub showMaster {
285
286 print qq(<center><div class="heading">Summarizing routed blocks for ).
287 qq($webvar{block}:</div></center><br>\n);
288
[106]289 my %allocated;
290 my %free;
291 my %routed;
292 my %bigfree;
293
[4]294 my $master = new NetAddr::IP $webvar{block};
295 my @localmasters;
296
[118]297 # Fetch only the blocks relevant to this master
[157]298 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr <<= '$master' order by cidr");
[4]299 $sth->execute();
300
301 my $i=0;
302 while (my @data = $sth->fetchrow_array()) {
303 my $cidr = new NetAddr::IP $data[0];
[118]304 $localmasters[$i++] = $cidr;
305 $free{"$cidr"} = 0;
306 $allocated{"$cidr"} = 0;
307 $bigfree{"$cidr"} = 128;
[4]308 # Retain the routing destination
[157]309 $routed{"$cidr"} = $data[1];
[4]310 }
311
[118]312 # Check if there were actually any blocks routed from this master
[4]313 if ($i > 0) {
314 startTable('Routed block','Routed to','Allocated blocks',
315 'Free blocks','Largest free block');
316
[118]317 # Count the allocations
318 $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?");
319 foreach my $master (@localmasters) {
320 $sth->execute("$master");
321 $sth->bind_columns(\$allocated{"$master"});
322 $sth->fetch();
[4]323 }
324
[118]325 # Count the free blocks.
[231]326 $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
327 "(routed='y' or routed='n')");
[118]328 foreach my $master (@localmasters) {
329 $sth->execute("$master");
330 $sth->bind_columns(\$free{"$master"});
331 $sth->fetch();
[4]332 }
333
[118]334 # Get the size of the largest free block
[231]335 $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
336 "(routed='y' or routed='n') order by maskbits limit 1");
[118]337 foreach my $master (@localmasters) {
338 $sth->execute("$master");
339 $sth->bind_columns(\$bigfree{"$master"});
340 $sth->fetch();
[4]341 }
342
343 # Print the data.
344 my $count=0;
345 foreach my $master (@localmasters) {
346 my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=showrouted&block=$master\">$master</a>",
347 $routed{"$master"}, $allocated{"$master"},
348 $free{"$master"},
349 ( ($bigfree{"$master"} eq 128) ? ("&lt;NONE&gt;") : ("/".$bigfree{"$master"}) )
350 );
351 printRow(\@row, 'color1' ) if($count%2==0);
352 printRow(\@row, 'color2' ) if($count%2!=0);
353 $count++;
354 }
355 } else {
356 # If a master block has no routed blocks, then by definition it has no
357 # allocations, and can be deleted.
358 print qq(<hr width="60%"><center><div class="heading">No allocations in ).
359 qq($master.</div>\n).
[233]360 ($IPDBacl{$authuser} =~ /d/ ?
361 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n).
362 qq(<input type=hidden name=action value="delete">\n).
363 qq(<input type=hidden name=block value="$master">\n).
364 qq(<input type=hidden name=alloctype value="mm">\n).
365 qq(<input type=submit value=" Remove this master ">\n).
366 qq(</form></center>\n) :
367 '');
[4]368
369 } # end check for existence of routed blocks in master
370
371 print qq(</table>\n<hr width="60%">\n).
372 qq(<center><div class="heading">Unrouted blocks in $master:</div></center><br>\n);
373
374 startTable('Netblock','Range');
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();
381 while (my @data = $sth->fetchrow_array()) {
382 my $cidr = new NetAddr::IP $data[0];
[118]383 my @row = ("$cidr", $cidr->range);
384 printRow(\@row, 'color1' ) if($count%2==0);
385 printRow(\@row, 'color2' ) if($count%2!=0);
386 $count++;
[4]387 }
388
389 print "</table>\n";
390} # showMaster
391
392
393# Display details of a routed block
394# Alrighty then! We're showing allocations within a routed block this time.
395# We should be able to steal code from showSummary() and showMaster(), and if
396# I'm really smart I'll figger a way to munge all three together. (Once I've
397# done that, everything else should follow. YMMV.
398# This time, we check the database before spewing, because we may
399# not have anything useful to spew.
400sub showRBlock {
401
402 my $master = new NetAddr::IP $webvar{block};
403
[157]404 $sth = $ip_dbh->prepare("select city from routed where cidr='$master'");
[4]405 $sth->execute;
406 my @data = $sth->fetchrow_array;
407
408 print qq(<center><div class="heading">Summarizing allocated blocks for ).
[157]409 qq($master ($data[0]):</div></center><br>\n);
[4]410
[320]411 startTable('CIDR allocation','Customer Location','Type','CustID','SWIPed?','Description/Name');
[118]412
413 # Snag the allocations for this block
[320]414 $sth = $ip_dbh->prepare("select cidr,city,type,custid,swip,description".
[157]415 " from allocations where cidr <<= '$master' order by cidr");
[4]416 $sth->execute();
417
[380]418 # hack hack hack
419 # set up to flag swip=y records if they don't actually have supporting data in the customers table
420 my $custsth = $ip_dbh->prepare("select count(*) from customers where custid=?");
421
[4]422 my $count=0;
423 while (my @data = $sth->fetchrow_array()) {
[320]424 # cidr,city,type,custid,swip,description, as per the SELECT
[4]425 my $cidr = new NetAddr::IP $data[0];
426
427 # Clean up extra spaces that are borking things.
[157]428# $data[2] =~ s/\s+//g;
[4]429
[380]430 $custsth->execute($data[3]);
431 my ($ncust) = $custsth->fetchrow_array();
432
[187]433 # Prefix subblocks with "Sub "
434 my @row = ( (($data[2] =~ /^.r$/) ? 'Sub ' : '').
[186]435 qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
[320]436 $data[1], $disp_alloctypes{$data[2]}, $data[3],
[380]437 ($data[4] eq 'y' ? ($ncust == 0 ? 'Yes<small>*</small>' : 'Yes') : 'No'), $data[5]);
[4]438 # If the allocation is a pool, allow listing of the IPs in the pool.
[157]439 if ($data[2] =~ /^.[pd]$/) {
[4]440 $row[0] .= ' &nbsp; <a href="/ip/cgi-bin/main.cgi?action=listpool'.
441 "&pool=$data[0]\">List IPs</a>";
442 }
443
444 printRow(\@row, 'color1') if ($count%2 == 0);
445 printRow(\@row, 'color2') if ($count%2 != 0);
446 $count++;
447 }
448
449 print "</table>\n";
450
451 # If the routed block has no allocations, by definition it only has
452 # one free block, and therefore may be deleted.
453 if ($count == 0) {
454 print qq(<hr width="60%"><center><div class="heading">No allocations in ).
455 qq($master.</div></center>\n).
[233]456 ($IPDBacl{$authuser} =~ /d/ ?
457 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n).
458 qq(<input type=hidden name=action value="delete">\n).
459 qq(<input type=hidden name=block value="$master">\n).
460 qq(<input type=hidden name=alloctype value="rm">\n).
461 qq(<input type=submit value=" Remove this block ">\n).
462 qq(</form>\n) :
463 '');
[4]464 }
465
466 print qq(<hr width="60%">\n<center><div class="heading">Free blocks within routed ).
467 qq(submaster $master</div></center>\n);
468
469 startTable('CIDR block','Range');
470
471 # Snag the free blocks. We don't really *need* to be pedantic about avoiding
472 # unrouted free blocks, but it's better to let the database do the work if we can.
473 $count = 0;
[186]474 $sth = $ip_dbh->prepare("select cidr,routed from freeblocks where cidr <<= '$master'".
475 " order by cidr");
[4]476 $sth->execute();
477 while (my @data = $sth->fetchrow_array()) {
[186]478 # cidr,routed
[4]479 my $cidr = new NetAddr::IP $data[0];
[187]480 # Include some HairyPerl(TM) to prefix subblocks with "Sub "
481 my @row = ((($data[1] ne 'y' && $data[1] ne 'n') ? 'Sub ' : '').
[233]482 ($IPDBacl{$authuser} =~ /a/ ? qq(<a href="/ip/cgi-bin/main.cgi?action=assign&block=$cidr&fbtype=$data[1]">$cidr</a>) : $cidr),
[21]483 $cidr->range);
[118]484 printRow(\@row, 'color1') if ($count%2 == 0);
485 printRow(\@row, 'color2') if ($count%2 != 0);
486 $count++;
[4]487 }
488
489 print "</table>\n";
490} # showRBlock
491
492
493# List the IPs used in a pool
494sub listPool {
495
496 my $cidr = new NetAddr::IP $webvar{pool};
497
[157]498 my ($pooltype,$poolcity);
499
[4]500 # Snag pool info for heading
[157]501 $sth = $ip_dbh->prepare("select type,city from allocations where cidr='$cidr'");
[4]502 $sth->execute;
[157]503 $sth->bind_columns(\$pooltype, \$poolcity);
504 $sth->fetch() || carp $sth->errstr;
[4]505
506 print qq(<center><div class="heading">Listing pool IPs for $cidr<br>\n).
[157]507 qq(($disp_alloctypes{$pooltype} in $poolcity)</div></center><br>\n);
508 # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
509 if ($pooltype =~ /^.d$/) {
510 print qq(<div class="indent"><b>Reserved IPs:</b><br>\n);
511 print qq(<div class="indent"><table><tr class=color1><td>Network IP:</td><td>).
[4]512 $cidr->addr."</td></tr>\n";
[157]513 $cidr++;
514 print "<tr class=color2><td>Gateway:</td><td>".$cidr->addr."</td></tr>\n";
515 $cidr--; $cidr--;
516 print "<tr class=color1><td>Broadcast:</td><td>".$cidr->addr."</td></tr>\n".
[4]517 "<tr><td>Netmask:</td><td>".$cidr->mask."</td></tr>\n".
518 "</table></div></div>\n";
[157]519 }
[4]520
521# probably have to add an "edit IP allocation" link here somewhere.
522
523 startTable('IP','Customer ID','Available?','Description','');
[157]524 $sth = $ip_dbh->prepare("select ip,custid,available,description,type".
525 " from poolips where pool='$webvar{pool}' order by ip");
[4]526 $sth->execute;
527 my $count = 0;
528 while (my @data = $sth->fetchrow_array) {
[74]529 # pool,ip,custid,city,ptype,available,notes,description,circuitid
[157]530 # ip,custid,available,description,type
531 # If desc is "null", make it not null. <g>
532 if ($data[3] eq '') {
533 $data[3] = '&nbsp;';
[4]534 }
535 # Some nice hairy Perl to decide whether to allow unassigning each IP
[157]536 # -> if $data[2] (aka poolips.available) == 'n' then we print the unassign link
[4]537 # else we print a blank space
[157]538 my @row = ( qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
539 $data[1],$data[2],$data[3],
[233]540 ( (($data[2] eq 'n') && ($IPDBacl{$authuser} =~ /d/)) ?
[157]541 ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[0]&".
542 "alloctype=$data[4]\">Unassign this IP</a>") :
[4]543 ("&nbsp;") )
544 );
545 printRow(\@row, 'color1') if($count%2==0);
546 printRow(\@row, 'color2') if($count%2!=0);
547 $count++;
548 }
549 print "</table>\n";
550
551} # end listPool
552
553
[106]554# Show "Add new allocation" page. Note that the actual page may
555# be one of two templates, and the lists come from the database.
[4]556sub assignBlock {
557
[233]558 if ($IPDBacl{$authuser} !~ /a/) {
559 printError("You shouldn't have been able to get here. Access denied.");
560 return;
561 }
562
[21]563 my $html;
564
565 # New special case- block to assign is specified
566 if ($webvar{block} ne '') {
567 open HTML, "../fb-assign.html"
568 or croak "Could not open fb-assign.html: $!";
569 $html = join('',<HTML>);
570 close HTML;
571 my $block = new NetAddr::IP $webvar{block};
572 $html =~ s|\$\$BLOCK\$\$|$block|g;
573 $html =~ s|\$\$MASKBITS\$\$|$block->masklen|;
[106]574 my $typelist = '';
[187]575
576 # This is a little dangerous, as it's *theoretically* possible to
577 # get fbtype='n' (aka a non-routed freeblock). However, should
578 # someone manage to get there, they get what they deserve.
579 if ($webvar{fbtype} ne 'y') {
580 # Snag the type of the block from the database. We have no
581 # convenient way to pass this in from the calling location. :/
582 $sth = $ip_dbh->prepare("select type from allocations where cidr >>='$block'");
583 $sth->execute;
584 my @data = $sth->fetchrow_array;
585 $data[0] =~ s/c$/r/; # Munge the type into the correct form
[191]586 $typelist = "$list_alloctypes{$data[0]}<input type=hidden name=alloctype value=$data[0]>\n";
[187]587 } else {
588 $typelist .= qq(<select name="alloctype">\n);
589 $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 500 ".
[191]590 "and type not like '_i' and type not like '_r' order by listorder");
[187]591 $sth->execute;
592 my @data = $sth->fetchrow_array;
593 $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
594 while (my @data = $sth->fetchrow_array) {
595 $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
596 }
597 $typelist .= "</select>\n";
[106]598 }
599 $html =~ s|\$\$TYPELIST\$\$|$typelist|g;
[21]600 } else {
601 open HTML, "../assign.html"
[4]602 or croak "Could not open assign.html: $!";
[21]603 $html = join('',<HTML>);
[92]604 close HTML;
[31]605 my $masterlist = "<select name=allocfrom><option selected>-</option>\n";
606 foreach my $master (@masterblocks) {
607 $masterlist .= "<option>$master</option>\n";
608 }
609 $masterlist .= "</select>\n";
610 $html =~ s|\$\$MASTERLIST\$\$|$masterlist|g;
[92]611 my $pops = '';
612 foreach my $pop (@poplist) {
613 $pops .= "<option>$pop</option>\n";
614 }
615 $html =~ s|\$\$POPLIST\$\$|$pops|g;
[106]616 my $typelist = '';
617 $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder");
618 $sth->execute;
619 my @data = $sth->fetchrow_array;
620 $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
621 while (my @data = $sth->fetchrow_array) {
622 $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
623 }
624 $html =~ s|\$\$TYPELIST\$\$|$typelist|g;
[21]625 }
[92]626 my $cities = '';
627 foreach my $city (@citylist) {
628 $cities .= "<option>$city</option>\n";
629 }
630 $html =~ s|\$\$ALLCITIES\$\$|$cities|g;
[4]631
[397]632## node hack
633 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
634 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
635 my $nodes = '';
636 while (my ($nid,$nname) = $sth->fetchrow_array()) {
637 $nodes .= "<option value='$nid'>$nname</option>\n";
638 }
639 $html =~ s/\$\$NODELIST\$\$/$nodes/;
640## end node hack
641
[284]642 my $i = 0;
643 $i++ if $webvar{fbtype} eq 'y';
644 # Check to see if user is allowed to do anything with sensitive data
645 my $privdata = '';
646 if ($IPDBacl{$authuser} =~ /s/) {
647 $privdata = qq(<tr class="color).($i%2).qq("><td>Restricted data:</td>).
648 qq(<td class=regular><textarea rows="3" cols="64" name="privdata" class="regular">).
649 qq(</textarea></td></tr>\n);
650 $i++;
651 }
652 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
653
654 $i = $i % 2;
655 $html =~ s/\$\$BUTTONROWCOLOUR\$\$/color$i/;
656
[4]657 print $html;
658
659} # assignBlock
660
661
662# Take info on requested IP assignment and see what we can provide.
663sub confirmAssign {
[233]664 if ($IPDBacl{$authuser} !~ /a/) {
665 printError("You shouldn't have been able to get here. Access denied.");
666 return;
667 }
[4]668
669 my $cidr;
670 my $alloc_from;
671
672 # Going to manually validate some items.
673 # custid and city are automagic.
[111]674 return if !validateInput();
[4]675
676# Several different cases here.
677# Static IP vs netblock
678# + Different flavours of static IP
679# + Different flavours of netblock
680
[157]681 if ($webvar{alloctype} =~ /^.i$/) {
[4]682 my ($base,undef) = split //, $webvar{alloctype}; # split into individual chars
[214]683 my ($sql,$city);
684 # Check for pools in Subury, North Bay, or Toronto if DSL or server pool.
685 # Anywhere else is invalid and shouldn't be in the db in the first place.
[4]686 # ... aside from #^%#$%#@#^%^^!!!! legacy data. GRRR.
687 # Note that we want to retain the requested city to relate to customer info.
688 if ($base =~ /^[ds]$/) {
[214]689 $city = "(allocations.city='Sudbury' or allocations.city='North Bay' or ".
690 "allocations.city='Toronto')";
[4]691 } else {
[214]692 $city = "allocations.city='$webvar{pop}'";
[4]693 }
694
[214]695# Ewww. But it works.
696 $sth = $ip_dbh->prepare("SELECT (SELECT city FROM allocations WHERE cidr=poolips.pool), ".
697 "poolips.pool, COUNT(*) FROM poolips,allocations WHERE poolips.available='y' AND ".
698 "poolips.pool=allocations.cidr AND $city AND poolips.type LIKE '".$base."_' ".
699 "GROUP BY pool");
[4]700 $sth->execute;
701 my $optionlist;
702 while (my @data = $sth->fetchrow_array) {
[214]703 # city,pool cidr,free IP count
704 if ($data[2] > 0) {
705 $optionlist .= "<option value='$data[1]'>$data[1] [$data[2] free IP(s)] in $data[0]</option>\n";
706 }
[4]707 }
708 $cidr = "Single static IP";
709 $alloc_from = "<select name=alloc_from>".$optionlist."</select>\n";
710
711 } else { # end show pool options
[21]712
713 if ($webvar{fbassign} eq 'y') {
714 $cidr = new NetAddr::IP $webvar{block};
715 $webvar{maskbits} = $cidr->masklen;
716 } else { # done with direct freeblocks assignment
717
718 if (!$webvar{maskbits}) {
[111]719 printError("Please specify a CIDR mask length.");
720 return;
[21]721 }
722 my $sql;
723 my $city;
724 my $failmsg;
[320]725 my $extracond = '';
726 if ($webvar{allocfrom} eq '-') {
727 $extracond = ($webvar{allowpriv} eq 'on' ? '' :
728 " and not (cidr <<= '192.168.0.0/16'".
729 " or cidr <<= '10.0.0.0/8'".
730 " or cidr <<= '172.16.0.0/12')");
731 }
732 my $sortorder;
[187]733 if ($webvar{alloctype} eq 'rm') {
[31]734 if ($webvar{allocfrom} ne '-') {
735 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
[320]736 " and cidr <<= '$webvar{allocfrom}'";
737 $sortorder = "maskbits desc";
[31]738 } else {
[320]739 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'";
740 $sortorder = "maskbits desc";
[31]741 }
[21]742 $failmsg = "No suitable free block found.<br>\nWe do not have a free".
743 " routeable block of that size.<br>\nYou will have to either route".
744 " a set of smaller netblocks or a single smaller netblock.";
[4]745 } else {
[118]746##fixme
747# This section needs serious Pondering.
[214]748 # Pools of most types get assigned to the POP they're "routed from"
[187]749 # This includes WAN blocks and other netblock "containers"
[214]750 # This does NOT include cable pools.
751 if ($webvar{alloctype} =~ /^.[pc]$/) {
752 if (($webvar{city} !~ /^(Sudbury|North Bay|Toronto)$/) && ($webvar{alloctype} eq 'dp')) {
753 printError("You must chose Sudbury, North Bay, or Toronto for DSL pools.");
[111]754 return;
755 }
[37]756 $city = $webvar{city};
[21]757 $failmsg = "No suitable free block found.<br>\nYou will have to route another".
[118]758 " superblock from one of the<br>\nmaster blocks in Sudbury or chose a smaller".
[21]759 " block size for the pool.";
760 } else {
761 $city = $webvar{pop};
762 $failmsg = "No suitable free block found.<br>\nYou will have to route another".
[106]763 " superblock to $webvar{pop}<br>\nfrom one of the master blocks in Sudbury or".
[21]764 " chose a smaller blocksize.";
765 }
[300]766 if (defined $webvar{allocfrom} && $webvar{allocfrom} ne '-') {
[157]767 $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
[186]768 " and cidr <<= '$webvar{allocfrom}' and routed='".
[320]769 (($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
770 $sortorder = "maskbits desc,cidr";
[31]771 } else {
[157]772 $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
[320]773 " and routed='".(($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
774 $sortorder = "maskbits desc,cidr";
[31]775 }
[4]776 }
[320]777 $sql = $sql.$extracond." order by ".$sortorder;
[21]778 $sth = $ip_dbh->prepare($sql);
779 $sth->execute;
780 my @data = $sth->fetchrow_array();
781 if ($data[0] eq "") {
[111]782 printError($failmsg);
783 return;
[21]784 }
785 $cidr = new NetAddr::IP $data[0];
786 } # check for freeblocks assignment or IPDB-controlled assignment
[4]787
788 $alloc_from = qq($cidr<input type=hidden name=alloc_from value="$cidr">);
789
790 # If the block to be allocated is smaller than the one we found,
791 # figure out the "real" block to be allocated.
792 if ($cidr->masklen() ne $webvar{maskbits}) {
793 my $maskbits = $cidr->masklen();
794 my @subblocks;
795 while ($maskbits++ < $webvar{maskbits}) {
796 @subblocks = $cidr->split($maskbits);
797 }
798 $cidr = $subblocks[0];
799 }
[157]800 } # if ($webvar{alloctype} =~ /^.i$/)
[4]801
802 open HTML, "../confirm.html"
803 or croak "Could not open confirm.html: $!";
804 my $html = join '', <HTML>;
805 close HTML;
806
[397]807## node hack
808 if ($webvar{node} && $webvar{node} ne '-') {
809 $sth = $ip_dbh->prepare("SELECT node_name FROM nodes WHERE node_id=?");
810 $sth->execute($webvar{node});
811 my ($nodename) = $sth->fetchrow_array();
812 $html =~ s/\$\$NODENAME\$\$/$nodename/;
813 $html =~ s/\$\$NODEID\$\$/$webvar{node}/;
814 } else {
815 $html =~ s/\$\$NODENAME\$\$//;
816 $html =~ s/\$\$NODEID\$\$//;
817 }
818## end node hack
819
[4]820### gotta fix this in final
821 # Stick in customer info as necessary - if it's blank, it just ends
822 # up as blank lines ignored in the rendering of the page
823 my $custbits;
824 $html =~ s|\$\$CUSTBITS\$\$|$custbits|g;
825###
826
827 # Stick in the allocation data
828 $html =~ s|\$\$ALLOC_TYPE\$\$|$webvar{alloctype}|g;
[106]829 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$webvar{alloctype}}|g;
[4]830 $html =~ s|\$\$ALLOC_FROM\$\$|$alloc_from|g;
831 $html =~ s|\$\$CIDR\$\$|$cidr|g;
[87]832 $webvar{city} = desanitize($webvar{city});
[4]833 $html =~ s|\$\$CITY\$\$|$webvar{city}|g;
834 $html =~ s|\$\$CUSTID\$\$|$webvar{custid}|g;
[87]835 $webvar{circid} = desanitize($webvar{circid});
[74]836 $html =~ s|\$\$CIRCID\$\$|$webvar{circid}|g;
[4]837 $webvar{desc} = desanitize($webvar{desc});
[92]838 $html =~ s|\$\$DESC\$\$|$webvar{desc}|g;
[4]839 $webvar{notes} = desanitize($webvar{notes});
840 $html =~ s|\$\$NOTES\$\$|$webvar{notes}|g;
841 $html =~ s|\$\$ACTION\$\$|insert|g;
842
[284]843 my $i=1;
844 # Check to see if user is allowed to do anything with sensitive data
845 my $privdata = '';
846 if ($IPDBacl{$authuser} =~ /s/) {
847 $privdata = qq(<tr class="color).($i%2).qq("><td>Restricted data:</td>).
848 qq(<td class=regular>$webvar{privdata}).
849 qq(<input type=hidden name=privdata value="$webvar{privdata}"></td></tr>\n);
850 $i++;
851 }
[299]852# We're going to abuse $$PRIVDATA$$ to stuff in some stuff for billing.
853 $privdata .= "<input type=hidden name=billinguser value=$webvar{userid}>\n"
854 if $webvar{userid};
[284]855 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
856
857 $i = $i % 2;
858 $html =~ s/\$\$BUTTONROWCOLOUR\$\$/color$i/;
859
[4]860 print $html;
861
862} # end confirmAssign
863
864
865# Do the work of actually inserting a block in the database.
866sub insertAssign {
[233]867 if ($IPDBacl{$authuser} !~ /a/) {
868 printError("You shouldn't have been able to get here. Access denied.");
869 return;
870 }
[4]871 # Some things are done more than once.
[111]872 return if !validateInput();
[4]873
[284]874 if (!defined($webvar{privdata})) {
875 $webvar{privdata} = '';
876 }
[106]877 # $code is "success" vs "failure", $msg contains OK for a
878 # successful netblock allocation, the IP allocated for static
879 # IP, or the error message if an error occurred.
880 my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
881 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
[397]882 $webvar{circid}, $webvar{privdata}, $webvar{node});
[4]883
[111]884 if ($code eq 'OK') {
[106]885 if ($webvar{alloctype} =~ /^.i$/) {
[300]886 $msg =~ s|/32||;
[297]887 print qq(<div class="center"><div class="heading">The IP $msg has been allocated to customer $webvar{custid}</div>).
[312]888 ( ($webvar{alloctype} eq 'di' && $webvar{billinguser}) ?
[380]889 qq(<div><a href="https://billing.example.com/radius.pl?).
[314]890 "action=new_radius_user&custid=$webvar{custid}&userid=$webvar{billinguser}".
[302]891 qq(&ipdb=1&ip=$msg">Add this IP to RADIUS user table</a></div>)
[297]892 : "</div>");
[106]893 # Notify tech@example.com
[247]894# mailNotify('tech@example.com',"ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
895# "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
896# "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[106]897 } else {
[301]898 my $netblock = new NetAddr::IP $webvar{fullcidr};
[106]899 print qq(<div class="center"><div class="heading">The block $webvar{fullcidr} was ).
[301]900 "sucessfully added as: $disp_alloctypes{$webvar{alloctype}}</div>".
[313]901 ( ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) ?
[380]902 qq(<div><a href="https://billing.example.com/radius.pl?).
[314]903 "action=new_radius_user&custid=$webvar{custid}&userid=$webvar{billinguser}".
[301]904 "&route_subnet=".$netblock->addr."&subnet_slash=".$netblock->masklen.
[302]905 "&include_routed_subnet=1&ipdb=1".
[301]906 qq(">Add this netblock to RADIUS user table</a></div>)
907 : "</div>");
[4]908 }
[106]909 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
[256]910 "'$webvar{alloctype}' ($msg)";
[111]911 } else {
912 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
913 "'$webvar{alloctype}' by $authuser failed: '$msg'";
[189]914 printError("Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
[157]915 " failed:<br>\n$msg\n");
[106]916 }
[4]917
918} # end insertAssign()
919
920
921# Does some basic checks on common input data to make sure nothing
922# *really* weird gets in to the database through this script.
923# Does NOT do complete input validation!!!
924sub validateInput {
925 if ($webvar{city} eq '-') {
[111]926 printError("Please choose a city.");
927 return;
[4]928 }
[138]929
930 # Alloctype check.
[4]931 chomp $webvar{alloctype};
[138]932 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
933 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
934 # managing to call things in such a way as to cause this deserves a cryptic error.
935 printError("Invalid alloctype");
936 return;
937 }
938
939 # CustID check
[4]940 # We have different handling for customer allocations and "internal" or "our" allocations
[214]941 if ($def_custids{$webvar{alloctype}} eq '') {
[4]942 if (!$webvar{custid}) {
[111]943 printError("Please enter a customer ID.");
944 return;
[4]945 }
[400]946 if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
947 # Force uppercase for now...
948 $webvar{custid} =~ tr/a-z/A-Z/;
949 # Crosscheck with billing.
950 my $status = CustIDCK->custid_exist($webvar{custid});
951 if ($CustIDCK::Error) {
952 printError("Error verifying customer ID: ".$CustIDCK::ErrMsg);
953 return;
954 }
955 if (!$status) {
956 printError("Customer ID not valid. Make sure the Customer ID ".
957 "is correct.<br>\nUse STAFF for staff static IPs, and 6750400 for any other ".
958 "non-customer assignments.");
959 return;
960 }
961#"Please enter a valid customer ID- this must be a 7- or 10-digit number, or STAFF for
962#static IPs for staff.");
[4]963 }
[400]964# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
[138]965 } else {
[167]966 # New! Improved! And now Loaded From The Database!!
[320]967 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
968 $webvar{custid} = $def_custids{$webvar{alloctype}};
969 }
[4]970 }
[111]971
972 # Check POP location
973 my $flag;
[187]974 if ($webvar{alloctype} eq 'rm') {
[111]975 $flag = 'for a routed netblock';
976 foreach (@poplist) {
977 if (/^$webvar{city}$/) {
978 $flag = 'n';
979 last;
980 }
981 }
982 } else {
983 $flag = 'n';
[400]984 if ($webvar{alloctype} =~ /[wp][cr]|[ds][pi]/) {
[300]985 # Set this forcibly rather than messing around elsewhere. Yes, this *is* a hack. PTHBTT!!
[400]986 $webvar{pop} = 'Sudbury';
987 }
988 if ($webvar{pop} =~ /^-$/) {
[111]989 $flag = 'to route the block from/through';
990 }
991 }
992 if ($flag ne 'n') {
993 printError("Please choose a valid POP location $flag. Valid ".
994 "POP locations are currently:<br>\n".join (" - ", @poplist));
995 return;
996 }
997
998 return 'OK';
[4]999} # end validateInput
1000
1001
1002# Displays details of a specific allocation in a form
1003# Allows update/delete
1004# action=edit
1005sub edit {
1006
1007 my $sql;
1008
1009 # Two cases: block is a netblock, or block is a static IP from a pool
1010 # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
1011 if ($webvar{block} =~ /\/32$/) {
[320]1012 $sql = "select ip,custid,type,city,circuitid,description,notes,modifystamp,privdata,oldcustid from poolips where ip='$webvar{block}'";
[4]1013 } else {
[320]1014 $sql = "select cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,oldcustid,swip from allocations where cidr='$webvar{block}'"
[4]1015 }
1016
1017 # gotta snag block info from db
1018 $sth = $ip_dbh->prepare($sql);
1019 $sth->execute;
1020 my @data = $sth->fetchrow_array;
1021
1022 # Clean up extra whitespace on alloc type
1023 $data[2] =~ s/\s//;
1024
1025 open (HTML, "../editDisplay.html")
1026 or croak "Could not open editDisplay.html :$!";
1027 my $html = join('', <HTML>);
1028
1029 # We can't let the city be changed here; this block is a part of
1030 # a larger routed allocation and therefore by definition can't be moved.
1031 # block and city are static.
1032##fixme
1033# Needs thinking. Have to allow changes to city to correct errors, no?
1034 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
1035
[233]1036 if ($IPDBacl{$authuser} =~ /c/) {
1037 $html =~ s/\$\$CUSTID\$\$/<input type=text name=custid value="$data[1]" maxlength=15 class="regular">/;
1038
[4]1039# Screw it. Changing allocation types gets very ugly VERY quickly- especially
1040# with the much longer list of allocation types.
1041# We'll just show what type of block it is.
1042
[33]1043# this has now been Requested, so here goes.
[4]1044
[187]1045##fixme The check here should be built from the database
[233]1046 if ($data[2] =~ /^.[ne]$/) {
1047 # Block that can be changed
1048 my $blockoptions = "<select name=alloctype><option".
[187]1049 (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
[228]1050 (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".
1051 (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option".
1052 (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic wireless netblock</option>\n<option".
[33]1053 (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
[187]1054 (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option".
[135]1055 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
[33]1056 "</select>\n";
[233]1057 $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g;
1058 } else {
1059 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g;
1060 }
[397]1061## node hack
1062 $sth = $ip_dbh->prepare("SELECT node_id FROM noderef WHERE block='$webvar{block}'");
1063 $sth->execute;
1064 my ($nodeid) = $sth->fetchrow_array();
1065 if ($nodeid) {
1066 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
1067 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1068 my $nodes = "<select name=node>\n";
1069 while (my ($nid,$nname) = $sth->fetchrow_array()) {
1070 $nodes .= "<option".($nodeid == $nid ? ' selected' : '')." value='$nid'>$nname</option>\n";
1071 }
1072 $nodes .= "</select>\n";
1073 $html =~ s/\$\$NODE\$\$/$nodes/;
1074 } else {
1075 if ($data[2] eq 'fr' || $data[2] eq 'bi') {
1076 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
1077 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1078 my $nodes = "<select name=node>\n<option value=>--</option>\n";
1079 while (my ($nid,$nname) = $sth->fetchrow_array()) {
1080 $nodes .= "<option value='$nid'>$nname</option>\n";
1081 }
1082 $nodes .= "</select>\n";
1083 $html =~ s/\$\$NODE\$\$/$nodes/;
1084 } else {
1085 $html =~ s|\$\$NODE\$\$|N/A|;
1086 }
1087 }
1088## end node hack
[320]1089 $html =~ s/\$\$OLDCUSTID\$\$/$data[9]/g;
[233]1090 $html =~ s/\$\$CITY\$\$/<input type=text name=city value="$data[3]">/g;
1091 $html =~ s/\$\$CIRCID\$\$/<input type="text" name="circid" value="$data[4]" maxlength=64 size=64 class="regular">/g;
1092 $html =~ s/\$\$DESC\$\$/<input type="text" name="desc" value="$data[5]" maxlength=64 size=64 class="regular">/g;
1093 $html =~ s|\$\$NOTES\$\$|<textarea rows="8" cols="64" name="notes" class="regular">$data[6]</textarea>|g;
[33]1094 } else {
[397]1095## node hack
1096 if ($data[2] eq 'fr' || $data[2] eq 'bi') {
1097 $sth = $ip_dbh->prepare("SELECT node_name FROM nodes INNER JOIN noderef".
1098 " ON nodes.node_id=noderef.node_id WHERE noderef.block='$webvar{block}'");
1099 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1100 my ($node) = $sth->fetchrow_array;
1101 $html =~ s/\$\$NODE\$\$/$node/;
1102 } else {
1103 $html =~ s|\$\$NODE\$\$|N/A|;
1104 }
1105## end node hack
[233]1106 $html =~ s/\$\$CUSTID\$\$/$data[1]/g;
[320]1107 $html =~ s/\$\$OLDCUSTID\$\$/$data[9]/g;
[233]1108 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}/g;
1109 $html =~ s/\$\$CITY\$\$/$data[3]/g;
1110 $html =~ s/\$\$CIRCID\$\$/$data[4]/g;
1111 $html =~ s/\$\$DESC\$\$/$data[5]/g;
1112 $html =~ s/\$\$NOTES\$\$/$data[6]/g;
[33]1113 }
[255]1114 my ($lastmod,undef) = split /\s+/, $data[7];
1115 $html =~ s/\$\$LASTMOD\$\$/$lastmod/g;
[33]1116
[320]1117## Hack time! SWIP isn't going to stay, so I'm not going to integrate it with ACLs.
1118if ($data[2] =~ /.i/) {
1119 $html =~ s/\$\$SWIP\$\$/N\/A/;
1120} else {
1121 my $tmp = (($data[10] eq 'n') ? '<input type=checkbox name=swip>' :
1122 '<input type=checkbox name=swip checked=yes>');
1123 $html =~ s/\$\$SWIP\$\$/$tmp/;
1124}
1125
[255]1126 # Allows us to "correctly" colour backgrounds in table
1127 my $i=1;
1128
[284]1129 # Check to see if we can display sensitive data
1130 my $privdata = '';
1131 if ($IPDBacl{$authuser} =~ /s/) {
1132 $privdata = qq(<tr class="color).($i%2).qq("><td class=heading>Restricted data:</td>).
1133 qq(<td class=regular><textarea rows="3" cols="64" name="privdata" class="regular">).
1134 qq($data[8]</textarea></td></tr>\n);
1135 $i++;
1136 }
1137 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1138
[233]1139 # More ACL trickery - we can live with forms that don't submit,
1140 # but we can't leave the extra table rows there, and we *really*
1141 # can't leave the submit buttons there.
1142 my $updok = '';
1143 if ($IPDBacl{$authuser} =~ /c/) {
[284]1144 $updok = qq(<tr class="color).($i%2).qq("><td colspan=2><div class="center">).
[233]1145 qq(<input type="submit" value=" Update this block " class="regular">).
1146 "</div></td></tr></form>\n";
[255]1147 $i++;
[233]1148 }
1149 $html =~ s/\$\$UPDOK\$\$/$updok/g;
[4]1150
[233]1151 my $delok = '';
1152 if ($IPDBacl{$authuser} =~ /d/) {
1153 $delok = qq(<form method="POST" action="main.cgi">
[284]1154 <tr class="color).($i%2).qq("><td colspan=2 class="regular"><div class=center>
[233]1155 <input type="hidden" name="action" value="delete">
1156 <input type="hidden" name="block" value="$webvar{block}">
1157 <input type="hidden" name="alloctype" value="$data[2]">
1158 <input type=submit value=" Delete this block ">
1159 </div></td></tr>);
1160 }
1161 $html =~ s/\$\$DELOK\$\$/$delok/;
1162
[4]1163 print $html;
1164
1165} # edit()
1166
1167
1168# Stuff new info about a block into the db
1169# action=update
1170sub update {
[284]1171 if ($IPDBacl{$authuser} !~ /c/) {
1172 printError("You shouldn't have been able to get here. Access denied.");
1173 return;
1174 }
[4]1175
[284]1176 # Check to see if we can update restricted data
1177 my $privdata = '';
1178 if ($IPDBacl{$authuser} =~ /s/) {
1179 $privdata = ",privdata='$webvar{privdata}'";
1180 }
1181
[4]1182 # Make sure incoming data is in correct format - custID among other things.
[228]1183 return if !validateInput;
[4]1184
1185 # SQL transaction wrapper
1186 eval {
1187 # Relatively simple SQL transaction here.
1188 my $sql;
[157]1189 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
[74]1190 $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',".
[284]1191 "circuitid='$webvar{circid}',description='$webvar{desc}',city='$webvar{city}'".
1192 "$privdata where ip='$webvar{block}'";
[4]1193 } else {
1194 $sql = "update allocations set custid='$webvar{custid}',".
[33]1195 "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',".
[320]1196 "type='$webvar{alloctype}',circuitid='$webvar{circid}'$privdata,".
1197 "swip='".($webvar{swip} eq 'on' ? 'y' : 'n')."' ".
[284]1198 "where cidr='$webvar{block}'";
[4]1199 }
[157]1200 # Log the details of the change.
1201 syslog "debug", $sql;
[4]1202 $sth = $ip_dbh->prepare($sql);
1203 $sth->execute;
[397]1204## node hack
1205 if ($webvar{node}) {
1206 $ip_dbh->do("DELETE FROM noderef WHERE block='$webvar{block}'");
1207 $sth = $ip_dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)");
1208 $sth->execute($webvar{block},$webvar{node});
1209 }
1210## end node hack
[4]1211 $ip_dbh->commit;
1212 };
1213 if ($@) {
[166]1214 my $msg = $@;
1215 carp "Transaction aborted because $msg";
[4]1216 eval { $ip_dbh->rollback; };
[166]1217 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
1218 printError("Could not update block/IP $webvar{block}: $msg");
[111]1219 return;
[4]1220 }
1221
1222 # If we get here, the operation succeeded.
1223 syslog "notice", "$authuser updated $webvar{block}";
1224 open (HTML, "../updated.html")
[111]1225 or croak "Could not open updated.html :$!";
[4]1226 my $html = join('', <HTML>);
1227
[380]1228 # Link back to browse-routed or list-pool page on "Update complete" page.
1229 my $backlink = "/ip/cgi-bin/main.cgi?action=";
1230 my $cblock; # to contain the CIDR of the container block we're retrieving.
1231 my $sql;
1232 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
1233 $sql = "select pool from poolips where ip='$webvar{block}'";
1234 $backlink .= "listpool&pool=";
1235 } else {
1236 $sql = "select cidr from routed where cidr >>= '$webvar{block}'";
1237 $backlink .= "showrouted&block=";
1238 }
1239 # I define there to be no errors on this operation... so we don't need to check for them.
1240 $sth = $ip_dbh->prepare($sql);
1241 $sth->execute;
1242 $sth->bind_columns(\$cblock);
1243 $sth->fetch();
1244 $sth->finish;
1245 $backlink .= $cblock;
1246
[320]1247my $swiptmp = ($webvar{swip} eq 'on' ? 'Yes' : 'No');
[4]1248 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
[92]1249 $webvar{city} = desanitize($webvar{city});
[4]1250 $html =~ s/\$\$CITY\$\$/$webvar{city}/g;
1251 $html =~ s/\$\$ALLOCTYPE\$\$/$webvar{alloctype}/g;
[106]1252 $html =~ s/\$\$TYPEFULL\$\$/$disp_alloctypes{$webvar{alloctype}}/g;
[4]1253 $html =~ s/\$\$CUSTID\$\$/$webvar{custid}/g;
[320]1254 $html =~ s/\$\$SWIP\$\$/$swiptmp/g;
[92]1255 $webvar{circid} = desanitize($webvar{circid});
[74]1256 $html =~ s/\$\$CIRCID\$\$/$webvar{circid}/g;
[92]1257 $webvar{desc} = desanitize($webvar{desc});
[4]1258 $html =~ s/\$\$DESC\$\$/$webvar{desc}/g;
[92]1259 $webvar{notes} = desanitize($webvar{notes});
[4]1260 $html =~ s/\$\$NOTES\$\$/$webvar{notes}/g;
[380]1261 $html =~ s/\$\$BACKLINK\$\$/$backlink/g;
1262 $html =~ s/\$\$BACKBLOCK\$\$/$cblock/g;
[4]1263
[284]1264 if ($IPDBacl{$authuser} =~ /s/) {
1265 $privdata = qq(<tr class="color2"><td valign="top">Restricted data:</td>).
1266 qq(<td class="regular">).desanitize($webvar{privdata}).qq(</td></tr>\n);
1267 }
1268 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1269
[4]1270 print $html;
1271
1272} # update()
1273
1274
1275# Delete an allocation.
[106]1276sub remove {
[233]1277 if ($IPDBacl{$authuser} !~ /d/) {
1278 printError("You shouldn't have been able to get here. Access denied.");
1279 return;
1280 }
1281
[4]1282 #show confirm screen.
1283 open HTML, "../confirmRemove.html"
1284 or croak "Could not open confirmRemove.html :$!";
1285 my $html = join('', <HTML>);
1286 close HTML;
1287
1288 # Serves'em right for getting here...
1289 if (!defined($webvar{block})) {
[111]1290 printError("Error 332");
1291 return;
[4]1292 }
1293
[284]1294 my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata);
[4]1295
[187]1296 if ($webvar{alloctype} eq 'rm') {
[4]1297 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
1298 $sth->execute();
1299
1300# This feels... extreme.
1301 croak $sth->errstr() if($sth->errstr());
1302
1303 $sth->bind_columns(\$cidr,\$city);
1304 $sth->execute();
1305 $sth->fetch || croak $sth->errstr();
1306 $custid = "N/A";
1307 $alloctype = $webvar{alloctype};
[74]1308 $circid = "N/A";
[4]1309 $desc = "N/A";
1310 $notes = "N/A";
1311
1312 } elsif ($webvar{alloctype} eq 'mm') {
1313 $cidr = $webvar{block};
1314 $city = "N/A";
1315 $custid = "N/A";
1316 $alloctype = $webvar{alloctype};
[74]1317 $circid = "N/A";
[4]1318 $desc = "N/A";
1319 $notes = "N/A";
[189]1320 } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m
[4]1321
1322 # Unassigning a static IP
[284]1323 my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata".
1324 " from poolips where ip='$webvar{block}'");
[4]1325 $sth->execute();
1326# croak $sth->errstr() if($sth->errstr());
1327
[284]1328 $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid,
1329 \$privdata);
[4]1330 $sth->fetch() || croak $sth->errstr;
1331
[157]1332 } else { # done with alloctype=~ /^.i$/
[4]1333
[284]1334 my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata".
1335 " from allocations where cidr='$webvar{block}'");
[4]1336 $sth->execute();
1337# croak $sth->errstr() if($sth->errstr());
1338
[284]1339 $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc,
1340 \$notes, \$privdata);
[74]1341 $sth->fetch() || carp $sth->errstr;
[4]1342 } # end cases for different alloctypes
1343
1344 # Munge everything into HTML
1345 $html =~ s|Please confirm|Please confirm <b>removal</b> of|;
1346 $html =~ s|\$\$BLOCK\$\$|$cidr|g;
[106]1347 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$alloctype}|g;
[4]1348 $html =~ s|\$\$ALLOCTYPE\$\$|$alloctype|g;
1349 $html =~ s|\$\$CITY\$\$|$city|g;
1350 $html =~ s|\$\$CUSTID\$\$|$custid|g;
[74]1351 $html =~ s|\$\$CIRCID\$\$|$circid|g;
[4]1352 $html =~ s|\$\$DESC\$\$|$desc|g;
1353 $html =~ s|\$\$NOTES\$\$|$notes|g;
1354
1355 $html =~ s|\$\$ACTION\$\$|finaldelete|g;
1356
1357 # Set the warning text.
[157]1358 if ($alloctype =~ /^.[pd]$/) {
[4]1359 $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.<br>Any IPs allocated from this pool will also be removed!</div></td></tr>|;
1360 } else {
1361 $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.</div></td></tr>|;
1362 }
1363
[284]1364 my $i = 1;
1365 # Check to see if user is allowed to do anything with sensitive data
1366 if ($IPDBacl{$authuser} =~ /s/) {
1367 $privdata = qq(<tr class="color).($i%2).qq("><td>Restricted data:</td>).
1368 qq(<td class=regular>$privdata</td></tr>\n);
1369 $i++;
1370 }
1371 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1372
1373 $i = ++$i % 2;
1374 $html =~ s/\$\$BUTTONROWCOLOUR\$\$/color$i/;
1375
[4]1376 print $html;
1377} # end edit()
1378
1379
1380# Delete an allocation. Return it to the freeblocks table; munge
1381# data as necessary to keep as few records as possible in freeblocks
1382# to prevent weirdness when allocating blocks later.
1383# Remove IPs from pool listing if necessary
1384sub finalDelete {
[233]1385 if ($IPDBacl{$authuser} !~ /d/) {
1386 printError("You shouldn't have been able to get here. Access denied.");
1387 return;
1388 }
[4]1389
[370]1390 # need to retrieve block data before deleting so we can notify on that
1391 my ($cidr,$custid,$type,$city,$description) = getBlockData($ip_dbh, $webvar{block});
1392
[106]1393 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
[4]1394
[106]1395 if ($code eq 'OK') {
[4]1396 print "<div class=heading align=center>Success! $webvar{block} deallocated.</div>\n";
[370]1397 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}".
1398 " $custid, $city, desc='$description'";
[214]1399 # Notify tech@ when a block/IP is deallocated
[247]1400# mailNotify('tech@example.com',"REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
[370]1401# "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
1402# "CustID: $custid\nCity: $city\nDescription: $description\n");
[106]1403 } else {
1404 if ($webvar{alloctype} =~ /^.i$/) {
1405 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
[111]1406 printError("Could not deallocate static IP $webvar{block}: $msg");
[106]1407 } else {
1408 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
[111]1409 printError("Could not deallocate netblock $webvar{block}: $msg");
[4]1410 }
[106]1411 }
[4]1412
1413} # finalDelete
1414
1415
[233]1416sub exitError {
1417 my $errStr = $_[0];
1418 printHeader('','');
1419 print qq(<center><p class="regular"> $errStr </p>
1420<input type="button" value="Back" onclick="history.go(-1)">
1421</center>
1422);
1423 printFooter();
1424 exit;
1425} # errorExit
1426
1427
[4]1428# Just in case we manage to get here.
1429exit 0;
Note: See TracBrowser for help on using the repository browser.