source: branches/stable/cgi-bin/main.cgi@ 428

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

/branches/stable

Comment out mailNotify calls to prepare to accept merge of redesigned
notifications. See #2, #13.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 49.3 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-12 22:09:30 +0000 (Wed, 12 May 2010) $
7# SVN revision $Rev: 408 $
8# Last update by $Author: kdeugau $
9###
[4]10
11use strict;
12use warnings;
13use CGI::Carp qw(fatalsToBrowser);
14use DBI;
15use CommonWeb qw(:ALL);
[158]16use MyIPDB;
[56]17use CustIDCK;
[4]18use POSIX qw(ceil);
19use NetAddr::IP;
20
21use Sys::Syslog;
22
23openlog "IPDB","pid","local2";
24
[242]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
[383]34syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
[4]35
[125]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;
[158]41($ip_dbh,$errstr) = connectDB_My;
[125]42if (!$ip_dbh) {
[242]43 exitError("Database error: $errstr\n");
[125]44}
45initIPDBGlobals($ip_dbh);
[4]46
[242]47# Headerize! Make sure we replace the $$EXTRA0$$ bit as needed.
48printHeader('', ($IPDBacl{$authuser} =~ /a/ ?
[286]49 '<td align=right><a href="/ip/cgi-bin/main.cgi?action=assign">Add new assignment</a>' : ''
[242]50 ));
51
52
[99]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();
[242]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
[242]75 if ($IPDBacl{$authuser} !~ /a/) {
76 printError("You shouldn't have been able to get here. Access denied.");
77 } else {
[4]78
[242]79 my $cidr = new NetAddr::IP $webvar{cidr};
[4]80
[242]81 print "<div type=heading align=center>Adding $cidr as master block....</div>\n";
[4]82
[366]83 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
[4]84
[366]85 if ($code eq 'FAIL') {
[265]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");
[242]89 } else {
90 print "<div type=heading align=center>Success!</div>\n";
91 syslog "info", "$authuser added master block $webvar{cidr}";
92 }
[4]93
[242]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}
[395]130elsif ($webvar{action} eq 'nodesearch') {
131 open HTML, "<../nodesearch.html";
132 my $html = join('',<HTML>);
133 close HTML;
[4]134
[395]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}
[125]158## Finally! Done with that NASTY "case" emulation!
[4]159
160
161
[125]162# Clean up IPDB globals, DB handle, etc.
163finish($ip_dbh);
[200]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)
[242]167 if $IPDBacl{$authuser} =~ /A/;
[200]168
[125]169# We print the footer here, so we don't have to do it elsewhere.
170printFooter;
171# Just in case something waaaayyy down isn't in place
172# properly... we exit explicitly.
173exit;
[4]174
175
[125]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
[134]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
[125]214sub showSummary {
[4]215
216 startTable('Master netblock', 'Routed netblocks', 'Allocated netblocks',
217 'Free netblocks', 'Largest free block');
218
[125]219 my %allocated;
220 my %free;
221 my %routed;
222 my %bigfree;
223
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
[125]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
[125]240 # Count the free blocks.
[194]241 $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
242 "(routed='y' or routed='n')");
[125]243 foreach my $master (@masterblocks) {
244 $sth->execute("$master");
245 $sth->bind_columns(\$free{"$master"});
246 $sth->fetch();
[4]247 }
248
[125]249 # Find the largest free block in each master
[194]250 $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
251 "(routed='y' or routed='n') order by maskbits limit 1");
[125]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"},
[160]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";
[242]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
[125]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
[125]297 # Fetch only the blocks relevant to this master
[159]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];
[125]304 $localmasters[$i++] = $cidr;
305 $free{"$cidr"} = 0;
306 $allocated{"$cidr"} = 0;
307 $bigfree{"$cidr"} = 128;
[4]308 # Retain the routing destination
[159]309 $routed{"$cidr"} = $data[1];
[4]310 }
311
[125]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
[125]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
[125]325 # Count the free blocks.
[194]326 $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
327 "(routed='y' or routed='n')");
[125]328 foreach my $master (@localmasters) {
329 $sth->execute("$master");
330 $sth->bind_columns(\$free{"$master"});
331 $sth->fetch();
[4]332 }
333
[125]334 # Get the size of the largest free block
[194]335 $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
336 "(routed='y' or routed='n') order by maskbits limit 1");
[125]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).
[242]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;
[125]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];
[125]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
[159]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 ).
[159]409 qq($master ($data[0]):</div></center><br>\n);
[4]410
[244]411 startTable('CIDR allocation','Customer Location','Type','CustID','SWIPed?','Description/Name');
[125]412
413 # Snag the allocations for this block
[244]414 $sth = $ip_dbh->prepare("select cidr,city,type,custid,swip,description".
[159]415 " from allocations where cidr <<= '$master' order by cidr");
[4]416 $sth->execute();
417
[376]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()) {
[244]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.
[159]428# $data[2] =~ s/\s+//g;
[4]429
[376]430 $custsth->execute($data[3]);
431 my ($ncust) = $custsth->fetchrow_array();
432
[192]433 # Prefix subblocks with "Sub "
434 my @row = ( (($data[2] =~ /^.r$/) ? 'Sub ' : '').
435 qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
[244]436 $data[1], $disp_alloctypes{$data[2]}, $data[3],
[376]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.
[159]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).
[242]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;
[192]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()) {
[192]478 # cidr,routed
[4]479 my $cidr = new NetAddr::IP $data[0];
[192]480 # Include some HairyPerl(TM) to prefix subblocks with "Sub "
481 my @row = ((($data[1] ne 'y' && $data[1] ne 'n') ? 'Sub ' : '').
[242]482 ($IPDBacl{$authuser} =~ /a/ ? qq(<a href="/ip/cgi-bin/main.cgi?action=assign&block=$cidr&fbtype=$data[1]">$cidr</a>) : $cidr),
[24]483 $cidr->range);
[125]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
[159]498 my ($pooltype,$poolcity);
499
[4]500 # Snag pool info for heading
[159]501 $sth = $ip_dbh->prepare("select type,city from allocations where cidr='$cidr'");
[4]502 $sth->execute;
[159]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).
[159]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";
[159]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";
[159]519 }
[4]520
521# probably have to add an "edit IP allocation" link here somewhere.
522
523 startTable('IP','Customer ID','Available?','Description','');
[159]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) {
[75]529 # pool,ip,custid,city,ptype,available,notes,description,circuitid
[159]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
[159]536 # -> if $data[2] (aka poolips.available) == 'n' then we print the unassign link
[4]537 # else we print a blank space
[159]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],
[242]540 ( (($data[2] eq 'n') && ($IPDBacl{$authuser} =~ /d/)) ?
[159]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
[125]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
[242]558 if ($IPDBacl{$authuser} !~ /a/) {
559 printError("You shouldn't have been able to get here. Access denied.");
560 return;
561 }
562
[24]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|;
[98]574 my $typelist = '';
[192]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
586 $typelist = "$list_alloctypes{$data[0]}<input type=hidden name=alloctype value=$data[0]>\n";
587 } else {
588 $typelist .= qq(<select name="alloctype">\n);
589 $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 500 ".
590 "and type not like '_i' and type not like '_r' order by listorder");
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";
[98]598 }
599 $html =~ s|\$\$TYPELIST\$\$|$typelist|g;
[24]600 } else {
601 open HTML, "../assign.html"
[4]602 or croak "Could not open assign.html: $!";
[24]603 $html = join('',<HTML>);
[91]604 close HTML;
[32]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;
[91]611 my $pops = '';
612 foreach my $pop (@poplist) {
613 $pops .= "<option>$pop</option>\n";
614 }
615 $html =~ s|\$\$POPLIST\$\$|$pops|g;
[98]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;
[24]625 }
[91]626 my $cities = '';
627 foreach my $city (@citylist) {
628 $cities .= "<option>$city</option>\n";
629 }
630 $html =~ s|\$\$ALLCITIES\$\$|$cities|g;
[4]631
[394]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
[286]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 {
[242]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.
[125]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
[159]681 if ($webvar{alloctype} =~ /^.i$/) {
[4]682 my ($base,undef) = split //, $webvar{alloctype}; # split into individual chars
[211]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]$/) {
[211]689 $city = "(allocations.city='Sudbury' or allocations.city='North Bay' or ".
690 "allocations.city='Toronto')";
[4]691 } else {
[211]692 $city = "allocations.city='$webvar{pop}'";
[4]693 }
694
[211]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) {
[211]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
[24]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}) {
[125]719 printError("Please specify a CIDR mask length.");
720 return;
[24]721 }
722 my $sql;
723 my $city;
724 my $failmsg;
[267]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;
[192]733 if ($webvar{alloctype} eq 'rm') {
[32]734 if ($webvar{allocfrom} ne '-') {
735 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
[267]736 " and cidr <<= '$webvar{allocfrom}'";
737 $sortorder = "maskbits desc";
[32]738 } else {
[267]739 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'";
740 $sortorder = "maskbits desc";
[32]741 }
[24]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 {
[125]746##fixme
747# This section needs serious Pondering.
[211]748 # Pools of most types get assigned to the POP they're "routed from"
[192]749 # This includes WAN blocks and other netblock "containers"
[211]750 # This does NOT include cable pools.
751 if ($webvar{alloctype} =~ /^.[pc]$/) {
[206]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.");
[125]754 return;
755 }
[39]756 $city = $webvar{city};
[24]757 $failmsg = "No suitable free block found.<br>\nYou will have to route another".
[125]758 " superblock from one of the<br>\nmaster blocks in Sudbury or chose a smaller".
[24]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".
[101]763 " superblock to $webvar{pop}<br>\nfrom one of the master blocks in Sudbury or".
[24]764 " chose a smaller blocksize.";
765 }
[315]766 if (defined $webvar{allocfrom} && $webvar{allocfrom} ne '-') {
[159]767 $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
[192]768 " and cidr <<= '$webvar{allocfrom}' and routed='".
[267]769 (($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
770 $sortorder = "maskbits desc,cidr";
[32]771 } else {
[159]772 $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
[267]773 " and routed='".(($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
774 $sortorder = "maskbits desc,cidr";
[32]775 }
[4]776 }
[267]777 $sql = $sql.$extracond." order by ".$sortorder;
[24]778 $sth = $ip_dbh->prepare($sql);
779 $sth->execute;
780 my @data = $sth->fetchrow_array();
781 if ($data[0] eq "") {
[125]782 printError($failmsg);
783 return;
[24]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 }
[159]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
[394]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;
[98]829 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$webvar{alloctype}}|g;
[4]830 $html =~ s|\$\$ALLOC_FROM\$\$|$alloc_from|g;
831 $html =~ s|\$\$CIDR\$\$|$cidr|g;
[82]832 $webvar{city} = desanitize($webvar{city});
[4]833 $html =~ s|\$\$CITY\$\$|$webvar{city}|g;
834 $html =~ s|\$\$CUSTID\$\$|$webvar{custid}|g;
[83]835 $webvar{circid} = desanitize($webvar{circid});
[75]836 $html =~ s|\$\$CIRCID\$\$|$webvar{circid}|g;
[4]837 $webvar{desc} = desanitize($webvar{desc});
[89]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
[286]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 }
[315]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};
[286]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 {
[242]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.
[125]872 return if !validateInput();
[4]873
[286]874 if (!defined($webvar{privdata})) {
875 $webvar{privdata} = '';
876 }
[125]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},
[394]882 $webvar{circid}, $webvar{privdata}, $webvar{node});
[4]883
[125]884 if ($code eq 'OK') {
885 if ($webvar{alloctype} =~ /^.i$/) {
[315]886 $msg =~ s|/32||;
887 print qq(<div class="center"><div class="heading">The IP $msg has been allocated to customer $webvar{custid}</div>).
888 ( ($webvar{alloctype} eq 'di' && $webvar{billinguser}) ?
[322]889 qq(<div><a href="https://billing.example.com/radius.pl?).
[315]890 "action=new_radius_user&custid=$webvar{custid}&userid=$webvar{billinguser}".
891 qq(&ipdb=1&ip=$msg">Add this IP to RADIUS user table</a></div>)
892 : "</div>");
[122]893 # Notify tech@example.com
[408]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");
[125]897 } else {
[315]898 my $netblock = new NetAddr::IP $webvar{fullcidr};
[125]899 print qq(<div class="center"><div class="heading">The block $webvar{fullcidr} was ).
[315]900 "sucessfully added as: $disp_alloctypes{$webvar{alloctype}}</div>".
901 ( ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) ?
[322]902 qq(<div><a href="https://billing.example.com/radius.pl?).
[315]903 "action=new_radius_user&custid=$webvar{custid}&userid=$webvar{billinguser}".
904 "&route_subnet=".$netblock->addr."&subnet_slash=".$netblock->masklen.
905 "&include_routed_subnet=1&ipdb=1".
906 qq(">Add this netblock to RADIUS user table</a></div>)
907 : "</div>");
[408]908# mailNotify('nocmgr@example.com',"ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
909# "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
910# "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
[122]911 }
[125]912 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
[251]913 "'$webvar{alloctype}' ($msg)";
[125]914 } else {
915 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
916 "'$webvar{alloctype}' by $authuser failed: '$msg'";
[192]917 printError("Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
[159]918 " failed:<br>\n$msg\n");
[125]919 }
[122]920
[4]921} # end insertAssign()
922
923
924# Does some basic checks on common input data to make sure nothing
925# *really* weird gets in to the database through this script.
926# Does NOT do complete input validation!!!
927sub validateInput {
928 if ($webvar{city} eq '-') {
[125]929 printError("Please choose a city.");
930 return;
[4]931 }
[140]932
933 # Alloctype check.
[4]934 chomp $webvar{alloctype};
[140]935 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
936 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
937 # managing to call things in such a way as to cause this deserves a cryptic error.
938 printError("Invalid alloctype");
939 return;
940 }
941
942 # CustID check
[4]943 # We have different handling for customer allocations and "internal" or "our" allocations
[209]944 if ($def_custids{$webvar{alloctype}} eq '') {
[4]945 if (!$webvar{custid}) {
[125]946 printError("Please enter a customer ID.");
947 return;
[4]948 }
[116]949 if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
[56]950 # Force uppercase for now...
951 $webvar{custid} =~ tr/a-z/A-Z/;
[338]952 # Crosscheck with billing.
[56]953 my $status = CustIDCK->custid_exist($webvar{custid});
[125]954 if ($CustIDCK::Error) {
955 printError("Error verifying customer ID: ".$CustIDCK::ErrMsg);
956 return;
957 }
958 if (!$status) {
959 printError("Customer ID not valid. Make sure the Customer ID ".
960 "is correct.<br>\nUse STAFF for staff static IPs, and 6750400 for any other ".
961 "non-customer assignments.");
962 return;
963 }
[56]964#"Please enter a valid customer ID- this must be a 7- or 10-digit number, or STAFF for
965#static IPs for staff.");
[4]966 }
[56]967# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
[140]968 } else {
[168]969 # New! Improved! And now Loaded From The Database!!
[56]970 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
[168]971 $webvar{custid} = $def_custids{$webvar{alloctype}};
[56]972 }
[4]973 }
[125]974
975 # Check POP location
976 my $flag;
[192]977 if ($webvar{alloctype} eq 'rm') {
[125]978 $flag = 'for a routed netblock';
979 foreach (@poplist) {
980 if (/^$webvar{city}$/) {
981 $flag = 'n';
982 last;
983 }
984 }
985 } else {
986 $flag = 'n';
[315]987 if ($webvar{alloctype} =~ /[wp][cr]|[ds][pi]/) {
988 # Set this forcibly rather than messing around elsewhere. Yes, this *is* a hack. PTHBTT!!
989 $webvar{pop} = 'Sudbury';
990 }
991 if ($webvar{pop} =~ /^-$/) {
[125]992 $flag = 'to route the block from/through';
993 }
994 }
995 if ($flag ne 'n') {
996 printError("Please choose a valid POP location $flag. Valid ".
997 "POP locations are currently:<br>\n".join (" - ", @poplist));
998 return;
999 }
1000
1001 return 'OK';
[4]1002} # end validateInput
1003
1004
1005# Displays details of a specific allocation in a form
1006# Allows update/delete
1007# action=edit
1008sub edit {
1009
1010 my $sql;
1011
1012 # Two cases: block is a netblock, or block is a static IP from a pool
1013 # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
1014 if ($webvar{block} =~ /\/32$/) {
[318]1015 $sql = "select ip,custid,type,city,circuitid,description,notes,modifystamp,privdata,oldcustid from poolips where ip='$webvar{block}'";
[4]1016 } else {
[318]1017 $sql = "select cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,oldcustid,swip from allocations where cidr='$webvar{block}'"
[4]1018 }
1019
1020 # gotta snag block info from db
1021 $sth = $ip_dbh->prepare($sql);
1022 $sth->execute;
1023 my @data = $sth->fetchrow_array;
1024
1025 # Clean up extra whitespace on alloc type
1026 $data[2] =~ s/\s//;
1027
1028 open (HTML, "../editDisplay.html")
1029 or croak "Could not open editDisplay.html :$!";
1030 my $html = join('', <HTML>);
1031
1032 # We can't let the city be changed here; this block is a part of
1033 # a larger routed allocation and therefore by definition can't be moved.
1034 # block and city are static.
1035##fixme
1036# Needs thinking. Have to allow changes to city to correct errors, no?
1037 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
1038
[242]1039 if ($IPDBacl{$authuser} =~ /c/) {
1040 $html =~ s/\$\$CUSTID\$\$/<input type=text name=custid value="$data[1]" maxlength=15 class="regular">/;
1041
[4]1042# Screw it. Changing allocation types gets very ugly VERY quickly- especially
1043# with the much longer list of allocation types.
1044# We'll just show what type of block it is.
1045
[35]1046# this has now been Requested, so here goes.
[4]1047
[192]1048##fixme The check here should be built from the database
[242]1049 if ($data[2] =~ /^.[ne]$/) {
1050 # Block that can be changed
1051 my $blockoptions = "<select name=alloctype><option".
[192]1052 (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
[224]1053 (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".
1054 (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option".
1055 (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic wireless netblock</option>\n<option".
[35]1056 (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
[192]1057 (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option".
[133]1058 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
[35]1059 "</select>\n";
[242]1060 $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g;
1061 } else {
1062 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g;
1063 }
[394]1064## node hack
1065 $sth = $ip_dbh->prepare("SELECT node_id FROM noderef WHERE block='$webvar{block}'");
1066 $sth->execute;
1067 my ($nodeid) = $sth->fetchrow_array();
1068 if ($nodeid) {
1069 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
1070 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1071 my $nodes = "<select name=node>\n";
1072 while (my ($nid,$nname) = $sth->fetchrow_array()) {
1073 $nodes .= "<option".($nodeid == $nid ? ' selected' : '')." value='$nid'>$nname</option>\n";
1074 }
1075 $nodes .= "</select>\n";
1076 $html =~ s/\$\$NODE\$\$/$nodes/;
1077 } else {
1078 if ($data[2] eq 'fr' || $data[2] eq 'bi') {
1079 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
1080 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1081 my $nodes = "<select name=node>\n<option value=>--</option>\n";
1082 while (my ($nid,$nname) = $sth->fetchrow_array()) {
1083 $nodes .= "<option value='$nid'>$nname</option>\n";
1084 }
1085 $nodes .= "</select>\n";
1086 $html =~ s/\$\$NODE\$\$/$nodes/;
1087 } else {
1088 $html =~ s|\$\$NODE\$\$|N/A|;
1089 }
1090 }
1091## end node hack
[318]1092 $html =~ s/\$\$OLDCUSTID\$\$/$data[9]/g;
[242]1093 $html =~ s/\$\$CITY\$\$/<input type=text name=city value="$data[3]">/g;
1094 $html =~ s/\$\$CIRCID\$\$/<input type="text" name="circid" value="$data[4]" maxlength=64 size=64 class="regular">/g;
1095 $html =~ s/\$\$DESC\$\$/<input type="text" name="desc" value="$data[5]" maxlength=64 size=64 class="regular">/g;
1096 $html =~ s|\$\$NOTES\$\$|<textarea rows="8" cols="64" name="notes" class="regular">$data[6]</textarea>|g;
[35]1097 } else {
[394]1098## node hack
1099 if ($data[2] eq 'fr' || $data[2] eq 'bi') {
1100 $sth = $ip_dbh->prepare("SELECT node_name FROM nodes INNER JOIN noderef".
1101 " ON nodes.node_id=noderef.node_id WHERE noderef.block='$webvar{block}'");
1102 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1103 my ($node) = $sth->fetchrow_array;
1104 $html =~ s/\$\$NODE\$\$/$node/;
1105 } else {
1106 $html =~ s|\$\$NODE\$\$|N/A|;
1107 }
1108## end node hack
[242]1109 $html =~ s/\$\$CUSTID\$\$/$data[1]/g;
[318]1110 $html =~ s/\$\$OLDCUSTID\$\$/$data[9]/g;
[242]1111 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}/g;
1112 $html =~ s/\$\$CITY\$\$/$data[3]/g;
1113 $html =~ s/\$\$CIRCID\$\$/$data[4]/g;
1114 $html =~ s/\$\$DESC\$\$/$data[5]/g;
1115 $html =~ s/\$\$NOTES\$\$/$data[6]/g;
[35]1116 }
[245]1117 my ($lastmod,undef) = split /\s+/, $data[7];
1118 $html =~ s/\$\$LASTMOD\$\$/$lastmod/g;
[35]1119
[244]1120## Hack time! SWIP isn't going to stay, so I'm not going to integrate it with ACLs.
1121if ($data[2] =~ /.i/) {
1122 $html =~ s/\$\$SWIP\$\$/N\/A/;
1123} else {
[318]1124 my $tmp = (($data[10] eq 'n') ? '<input type=checkbox name=swip>' :
[244]1125 '<input type=checkbox name=swip checked=yes>');
1126 $html =~ s/\$\$SWIP\$\$/$tmp/;
1127}
1128
[245]1129 # Allows us to "correctly" colour backgrounds in table
1130 my $i=1;
1131
[286]1132 # Check to see if we can display sensitive data
1133 my $privdata = '';
1134 if ($IPDBacl{$authuser} =~ /s/) {
1135 $privdata = qq(<tr class="color).($i%2).qq("><td class=heading>Restricted data:</td>).
1136 qq(<td class=regular><textarea rows="3" cols="64" name="privdata" class="regular">).
1137 qq($data[8]</textarea></td></tr>\n);
1138 $i++;
1139 }
1140 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1141
[242]1142 # More ACL trickery - we can live with forms that don't submit,
1143 # but we can't leave the extra table rows there, and we *really*
1144 # can't leave the submit buttons there.
1145 my $updok = '';
1146 if ($IPDBacl{$authuser} =~ /c/) {
[286]1147 $updok = qq(<tr class="color).($i%2).qq("><td colspan=2><div class="center">).
[242]1148 qq(<input type="submit" value=" Update this block " class="regular">).
1149 "</div></td></tr></form>\n";
[245]1150 $i++;
[242]1151 }
1152 $html =~ s/\$\$UPDOK\$\$/$updok/g;
[4]1153
[242]1154 my $delok = '';
1155 if ($IPDBacl{$authuser} =~ /d/) {
1156 $delok = qq(<form method="POST" action="main.cgi">
[286]1157 <tr class="color).($i%2).qq("><td colspan=2 class="regular"><div class=center>
[242]1158 <input type="hidden" name="action" value="delete">
1159 <input type="hidden" name="block" value="$webvar{block}">
1160 <input type="hidden" name="alloctype" value="$data[2]">
1161 <input type=submit value=" Delete this block ">
1162 </div></td></tr>);
1163 }
1164 $html =~ s/\$\$DELOK\$\$/$delok/;
1165
[4]1166 print $html;
1167
1168} # edit()
1169
1170
1171# Stuff new info about a block into the db
1172# action=update
1173sub update {
[286]1174 if ($IPDBacl{$authuser} !~ /c/) {
1175 printError("You shouldn't have been able to get here. Access denied.");
1176 return;
1177 }
[4]1178
[286]1179 # Check to see if we can update restricted data
1180 my $privdata = '';
1181 if ($IPDBacl{$authuser} =~ /s/) {
1182 $privdata = ",privdata='$webvar{privdata}'";
1183 }
1184
[4]1185 # Make sure incoming data is in correct format - custID among other things.
[220]1186 return if !validateInput;
[4]1187
1188 # SQL transaction wrapper
1189 eval {
1190 # Relatively simple SQL transaction here.
1191 my $sql;
[159]1192 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
[75]1193 $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',".
[286]1194 "circuitid='$webvar{circid}',description='$webvar{desc}',city='$webvar{city}'".
1195 "$privdata where ip='$webvar{block}'";
[4]1196 } else {
1197 $sql = "update allocations set custid='$webvar{custid}',".
[35]1198 "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',".
[296]1199 "type='$webvar{alloctype}',circuitid='$webvar{circid}'$privdata,".
[244]1200 "swip='".($webvar{swip} eq 'on' ? 'y' : 'n')."' ".
[286]1201 "where cidr='$webvar{block}'";
[4]1202 }
[159]1203 # Log the details of the change.
1204 syslog "debug", $sql;
[4]1205 $sth = $ip_dbh->prepare($sql);
1206 $sth->execute;
[394]1207## node hack
1208 if ($webvar{node}) {
1209 $ip_dbh->do("DELETE FROM noderef WHERE block='$webvar{block}'");
1210 $sth = $ip_dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)");
1211 $sth->execute($webvar{block},$webvar{node});
1212 }
1213## end node hack
[4]1214 $ip_dbh->commit;
1215 };
1216 if ($@) {
[163]1217 my $msg = $@;
1218 carp "Transaction aborted because $msg";
[4]1219 eval { $ip_dbh->rollback; };
[163]1220 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
1221 printError("Could not update block/IP $webvar{block}: $msg");
[125]1222 return;
[4]1223 }
1224
1225 # If we get here, the operation succeeded.
1226 syslog "notice", "$authuser updated $webvar{block}";
[408]1227#mailNotify('nocmgr@example.com',"SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1228# "$webvar{block} had SWIP status changed to \"Yes\" by $authuser");
[4]1229 open (HTML, "../updated.html")
[125]1230 or croak "Could not open updated.html :$!";
[4]1231 my $html = join('', <HTML>);
1232
[378]1233 # Link back to browse-routed or list-pool page on "Update complete" page.
1234 my $backlink = "/ip/cgi-bin/main.cgi?action=";
1235 my $cblock; # to contain the CIDR of the container block we're retrieving.
1236 my $sql;
1237 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
1238 $sql = "select pool from poolips where ip='$webvar{block}'";
1239 $backlink .= "listpool&pool=";
1240 } else {
1241 $sql = "select cidr from routed where cidr >>= '$webvar{block}'";
1242 $backlink .= "showrouted&block=";
1243 }
1244 # I define there to be no errors on this operation... so we don't need to check for them.
1245 $sth = $ip_dbh->prepare($sql);
1246 $sth->execute;
1247 $sth->bind_columns(\$cblock);
1248 $sth->fetch();
1249 $sth->finish;
1250 $backlink .= $cblock;
1251
[244]1252my $swiptmp = ($webvar{swip} eq 'on' ? 'Yes' : 'No');
[4]1253 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
[89]1254 $webvar{city} = desanitize($webvar{city});
[4]1255 $html =~ s/\$\$CITY\$\$/$webvar{city}/g;
1256 $html =~ s/\$\$ALLOCTYPE\$\$/$webvar{alloctype}/g;
[98]1257 $html =~ s/\$\$TYPEFULL\$\$/$disp_alloctypes{$webvar{alloctype}}/g;
[4]1258 $html =~ s/\$\$CUSTID\$\$/$webvar{custid}/g;
[244]1259 $html =~ s/\$\$SWIP\$\$/$swiptmp/g;
[89]1260 $webvar{circid} = desanitize($webvar{circid});
[75]1261 $html =~ s/\$\$CIRCID\$\$/$webvar{circid}/g;
[89]1262 $webvar{desc} = desanitize($webvar{desc});
[4]1263 $html =~ s/\$\$DESC\$\$/$webvar{desc}/g;
[89]1264 $webvar{notes} = desanitize($webvar{notes});
[4]1265 $html =~ s/\$\$NOTES\$\$/$webvar{notes}/g;
[378]1266 $html =~ s/\$\$BACKLINK\$\$/$backlink/g;
1267 $html =~ s/\$\$BACKBLOCK\$\$/$cblock/g;
[4]1268
[286]1269 if ($IPDBacl{$authuser} =~ /s/) {
1270 $privdata = qq(<tr class="color2"><td valign="top">Restricted data:</td>).
1271 qq(<td class="regular">).desanitize($webvar{privdata}).qq(</td></tr>\n);
1272 }
1273 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1274
[4]1275 print $html;
1276
1277} # update()
1278
1279
1280# Delete an allocation.
[125]1281sub remove {
[242]1282 if ($IPDBacl{$authuser} !~ /d/) {
1283 printError("You shouldn't have been able to get here. Access denied.");
1284 return;
1285 }
1286
[4]1287 #show confirm screen.
1288 open HTML, "../confirmRemove.html"
1289 or croak "Could not open confirmRemove.html :$!";
1290 my $html = join('', <HTML>);
1291 close HTML;
1292
1293 # Serves'em right for getting here...
1294 if (!defined($webvar{block})) {
[125]1295 printError("Error 332");
1296 return;
[4]1297 }
1298
[286]1299 my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata);
[4]1300
[192]1301 if ($webvar{alloctype} eq 'rm') {
[4]1302 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
1303 $sth->execute();
1304
1305# This feels... extreme.
1306 croak $sth->errstr() if($sth->errstr());
1307
1308 $sth->bind_columns(\$cidr,\$city);
1309 $sth->execute();
1310 $sth->fetch || croak $sth->errstr();
1311 $custid = "N/A";
1312 $alloctype = $webvar{alloctype};
[75]1313 $circid = "N/A";
[4]1314 $desc = "N/A";
1315 $notes = "N/A";
1316
1317 } elsif ($webvar{alloctype} eq 'mm') {
1318 $cidr = $webvar{block};
1319 $city = "N/A";
1320 $custid = "N/A";
1321 $alloctype = $webvar{alloctype};
[75]1322 $circid = "N/A";
[4]1323 $desc = "N/A";
1324 $notes = "N/A";
[192]1325 } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m
[4]1326
1327 # Unassigning a static IP
[286]1328 my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata".
1329 " from poolips where ip='$webvar{block}'");
[4]1330 $sth->execute();
1331# croak $sth->errstr() if($sth->errstr());
1332
[286]1333 $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid,
1334 \$privdata);
[4]1335 $sth->fetch() || croak $sth->errstr;
1336
[159]1337 } else { # done with alloctype=~ /^.i$/
[4]1338
[286]1339 my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata".
1340 " from allocations where cidr='$webvar{block}'");
[4]1341 $sth->execute();
1342# croak $sth->errstr() if($sth->errstr());
1343
[286]1344 $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc,
1345 \$notes, \$privdata);
[75]1346 $sth->fetch() || carp $sth->errstr;
[4]1347 } # end cases for different alloctypes
1348
1349 # Munge everything into HTML
1350 $html =~ s|Please confirm|Please confirm <b>removal</b> of|;
1351 $html =~ s|\$\$BLOCK\$\$|$cidr|g;
[98]1352 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$alloctype}|g;
[4]1353 $html =~ s|\$\$ALLOCTYPE\$\$|$alloctype|g;
1354 $html =~ s|\$\$CITY\$\$|$city|g;
1355 $html =~ s|\$\$CUSTID\$\$|$custid|g;
[75]1356 $html =~ s|\$\$CIRCID\$\$|$circid|g;
[4]1357 $html =~ s|\$\$DESC\$\$|$desc|g;
1358 $html =~ s|\$\$NOTES\$\$|$notes|g;
1359
1360 $html =~ s|\$\$ACTION\$\$|finaldelete|g;
1361
1362 # Set the warning text.
[159]1363 if ($alloctype =~ /^.[pd]$/) {
[4]1364 $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>|;
1365 } else {
1366 $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.</div></td></tr>|;
1367 }
1368
[286]1369 my $i = 1;
1370 # Check to see if user is allowed to do anything with sensitive data
1371 if ($IPDBacl{$authuser} =~ /s/) {
1372 $privdata = qq(<tr class="color).($i%2).qq("><td>Restricted data:</td>).
1373 qq(<td class=regular>$privdata</td></tr>\n);
1374 $i++;
1375 }
1376 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1377
1378 $i = ++$i % 2;
1379 $html =~ s/\$\$BUTTONROWCOLOUR\$\$/color$i/;
1380
[4]1381 print $html;
1382} # end edit()
1383
1384
1385# Delete an allocation. Return it to the freeblocks table; munge
1386# data as necessary to keep as few records as possible in freeblocks
1387# to prevent weirdness when allocating blocks later.
1388# Remove IPs from pool listing if necessary
1389sub finalDelete {
[242]1390 if ($IPDBacl{$authuser} !~ /d/) {
1391 printError("You shouldn't have been able to get here. Access denied.");
1392 return;
1393 }
[4]1394
[356]1395 # need to retrieve block data before deleting so we can notify on that
1396 my ($cidr,$custid,$type,$city,$description) = getBlockData($ip_dbh, $webvar{block});
1397
[125]1398 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
[4]1399
[125]1400 if ($code eq 'OK') {
[4]1401 print "<div class=heading align=center>Success! $webvar{block} deallocated.</div>\n";
[356]1402 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}".
1403 " $custid, $city, desc='$description'";
[205]1404 # Notify tech@ when a block/IP is deallocated
[408]1405# mailNotify('tech@example.com',"REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1406# "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
1407# "CustID: $custid\nCity: $city\nDescription: $description\n");
1408# mailNotify('nocmgr@example.com',"REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1409# "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
1410# "CustID: $custid\nCity: $city\nDescription: $description\n");
[125]1411 } else {
1412 if ($webvar{alloctype} =~ /^.i$/) {
1413 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1414 printError("Could not deallocate static IP $webvar{block}: $msg");
1415 } else {
1416 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
1417 printError("Could not deallocate netblock $webvar{block}: $msg");
[4]1418 }
[102]1419 }
1420
[4]1421} # finalDelete
1422
1423
[242]1424sub exitError {
1425 my $errStr = $_[0];
1426 printHeader('','');
1427 print qq(<center><p class="regular"> $errStr </p>
1428<input type="button" value="Back" onclick="history.go(-1)">
1429</center>
1430);
1431 printFooter();
1432 exit;
1433} # errorExit
1434
1435
[4]1436# Just in case we manage to get here.
1437exit 0;
Note: See TracBrowser for help on using the repository browser.