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

Last change on this file since 427 was 408, checked in by Kris Deugau, 14 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
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3# Started munging from noc.vianet's old IPDB 04/22/2004
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###
10
11use strict;
12use warnings;
13use CGI::Carp qw(fatalsToBrowser);
14use DBI;
15use CommonWeb qw(:ALL);
16use MyIPDB;
17use CustIDCK;
18use POSIX qw(ceil);
19use NetAddr::IP;
20
21use Sys::Syslog;
22
23openlog "IPDB","pid","local2";
24
25# Collect the username from HTTP auth. If undefined, we're in
26# a test environment, or called without a username.
27my $authuser;
28if (!defined($ENV{'REMOTE_USER'})) {
29 $authuser = '__temptest';
30} else {
31 $authuser = $ENV{'REMOTE_USER'};
32}
33
34syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
35
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;
41($ip_dbh,$errstr) = connectDB_My;
42if (!$ip_dbh) {
43 exitError("Database error: $errstr\n");
44}
45initIPDBGlobals($ip_dbh);
46
47# Headerize! Make sure we replace the $$EXTRA0$$ bit as needed.
48printHeader('', ($IPDBacl{$authuser} =~ /a/ ?
49 '<td align=right><a href="/ip/cgi-bin/main.cgi?action=assign">Add new assignment</a>' : ''
50 ));
51
52
53# Global variables
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();
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 }
73} elsif ($webvar{action} eq 'newmaster') {
74
75 if ($IPDBacl{$authuser} !~ /a/) {
76 printError("You shouldn't have been able to get here. Access denied.");
77 } else {
78
79 my $cidr = new NetAddr::IP $webvar{cidr};
80
81 print "<div type=heading align=center>Adding $cidr as master block....</div>\n";
82
83 my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
84
85 if ($code eq 'FAIL') {
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");
89 } else {
90 print "<div type=heading align=center>Success!</div>\n";
91 syslog "info", "$authuser added master block $webvar{cidr}";
92 }
93
94 } # ACL check
95
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}
130elsif ($webvar{action} eq 'nodesearch') {
131 open HTML, "<../nodesearch.html";
132 my $html = join('',<HTML>);
133 close HTML;
134
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
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}
158## Finally! Done with that NASTY "case" emulation!
159
160
161
162# Clean up IPDB globals, DB handle, etc.
163finish($ip_dbh);
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)
167 if $IPDBacl{$authuser} =~ /A/;
168
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;
174
175
176
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
189ELEMENT: foreach my $element (@$rowRef) {
190 if (!defined($element)) {
191 print "<td></td>\n";
192 next ELEMENT;
193 }
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
214sub showSummary {
215
216 startTable('Master netblock', 'Routed netblocks', 'Allocated netblocks',
217 'Free netblocks', 'Largest free block');
218
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();
230 }
231
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();
238 }
239
240 # Count the free blocks.
241 $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
242 "(routed='y' or routed='n')");
243 foreach my $master (@masterblocks) {
244 $sth->execute("$master");
245 $sth->bind_columns(\$free{"$master"});
246 $sth->fetch();
247 }
248
249 # Find the largest free block in each master
250 $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
251 "(routed='y' or routed='n') order by maskbits limit 1");
252 foreach my $master (@masterblocks) {
253 $sth->execute("$master");
254 $sth->bind_columns(\$bigfree{"$master"});
255 $sth->fetch();
256 }
257
258 # Print the data.
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"},
263 ( ($bigfree{"$master"} eq '') ? ("&lt;NONE&gt;") : ("/".$bigfree{"$master"}) )
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";
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 }
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
289 my %allocated;
290 my %free;
291 my %routed;
292 my %bigfree;
293
294 my $master = new NetAddr::IP $webvar{block};
295 my @localmasters;
296
297 # Fetch only the blocks relevant to this master
298 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr <<= '$master' order by cidr");
299 $sth->execute();
300
301 my $i=0;
302 while (my @data = $sth->fetchrow_array()) {
303 my $cidr = new NetAddr::IP $data[0];
304 $localmasters[$i++] = $cidr;
305 $free{"$cidr"} = 0;
306 $allocated{"$cidr"} = 0;
307 $bigfree{"$cidr"} = 128;
308 # Retain the routing destination
309 $routed{"$cidr"} = $data[1];
310 }
311
312 # Check if there were actually any blocks routed from this master
313 if ($i > 0) {
314 startTable('Routed block','Routed to','Allocated blocks',
315 'Free blocks','Largest free block');
316
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();
323 }
324
325 # Count the free blocks.
326 $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
327 "(routed='y' or routed='n')");
328 foreach my $master (@localmasters) {
329 $sth->execute("$master");
330 $sth->bind_columns(\$free{"$master"});
331 $sth->fetch();
332 }
333
334 # Get the size of the largest free block
335 $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
336 "(routed='y' or routed='n') order by maskbits limit 1");
337 foreach my $master (@localmasters) {
338 $sth->execute("$master");
339 $sth->bind_columns(\$bigfree{"$master"});
340 $sth->fetch();
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).
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 '');
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;
378 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr <<='$master' and ".
379 "routed='n' order by cidr");
380 $sth->execute();
381 while (my @data = $sth->fetchrow_array()) {
382 my $cidr = new NetAddr::IP $data[0];
383 my @row = ("$cidr", $cidr->range);
384 printRow(\@row, 'color1' ) if($count%2==0);
385 printRow(\@row, 'color2' ) if($count%2!=0);
386 $count++;
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
404 $sth = $ip_dbh->prepare("select city from routed where cidr='$master'");
405 $sth->execute;
406 my @data = $sth->fetchrow_array;
407
408 print qq(<center><div class="heading">Summarizing allocated blocks for ).
409 qq($master ($data[0]):</div></center><br>\n);
410
411 startTable('CIDR allocation','Customer Location','Type','CustID','SWIPed?','Description/Name');
412
413 # Snag the allocations for this block
414 $sth = $ip_dbh->prepare("select cidr,city,type,custid,swip,description".
415 " from allocations where cidr <<= '$master' order by cidr");
416 $sth->execute();
417
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
422 my $count=0;
423 while (my @data = $sth->fetchrow_array()) {
424 # cidr,city,type,custid,swip,description, as per the SELECT
425 my $cidr = new NetAddr::IP $data[0];
426
427 # Clean up extra spaces that are borking things.
428# $data[2] =~ s/\s+//g;
429
430 $custsth->execute($data[3]);
431 my ($ncust) = $custsth->fetchrow_array();
432
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>),
436 $data[1], $disp_alloctypes{$data[2]}, $data[3],
437 ($data[4] eq 'y' ? ($ncust == 0 ? 'Yes<small>*</small>' : 'Yes') : 'No'), $data[5]);
438 # If the allocation is a pool, allow listing of the IPs in the pool.
439 if ($data[2] =~ /^.[pd]$/) {
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).
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 '');
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;
474 $sth = $ip_dbh->prepare("select cidr,routed from freeblocks where cidr <<= '$master'".
475 " order by cidr");
476 $sth->execute();
477 while (my @data = $sth->fetchrow_array()) {
478 # cidr,routed
479 my $cidr = new NetAddr::IP $data[0];
480 # Include some HairyPerl(TM) to prefix subblocks with "Sub "
481 my @row = ((($data[1] ne 'y' && $data[1] ne 'n') ? 'Sub ' : '').
482 ($IPDBacl{$authuser} =~ /a/ ? qq(<a href="/ip/cgi-bin/main.cgi?action=assign&block=$cidr&fbtype=$data[1]">$cidr</a>) : $cidr),
483 $cidr->range);
484 printRow(\@row, 'color1') if ($count%2 == 0);
485 printRow(\@row, 'color2') if ($count%2 != 0);
486 $count++;
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
498 my ($pooltype,$poolcity);
499
500 # Snag pool info for heading
501 $sth = $ip_dbh->prepare("select type,city from allocations where cidr='$cidr'");
502 $sth->execute;
503 $sth->bind_columns(\$pooltype, \$poolcity);
504 $sth->fetch() || carp $sth->errstr;
505
506 print qq(<center><div class="heading">Listing pool IPs for $cidr<br>\n).
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>).
512 $cidr->addr."</td></tr>\n";
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".
517 "<tr><td>Netmask:</td><td>".$cidr->mask."</td></tr>\n".
518 "</table></div></div>\n";
519 }
520
521# probably have to add an "edit IP allocation" link here somewhere.
522
523 startTable('IP','Customer ID','Available?','Description','');
524 $sth = $ip_dbh->prepare("select ip,custid,available,description,type".
525 " from poolips where pool='$webvar{pool}' order by ip");
526 $sth->execute;
527 my $count = 0;
528 while (my @data = $sth->fetchrow_array) {
529 # pool,ip,custid,city,ptype,available,notes,description,circuitid
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;';
534 }
535 # Some nice hairy Perl to decide whether to allow unassigning each IP
536 # -> if $data[2] (aka poolips.available) == 'n' then we print the unassign link
537 # else we print a blank space
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],
540 ( (($data[2] eq 'n') && ($IPDBacl{$authuser} =~ /d/)) ?
541 ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[0]&".
542 "alloctype=$data[4]\">Unassign this IP</a>") :
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
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.
556sub assignBlock {
557
558 if ($IPDBacl{$authuser} !~ /a/) {
559 printError("You shouldn't have been able to get here. Access denied.");
560 return;
561 }
562
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|;
574 my $typelist = '';
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";
598 }
599 $html =~ s|\$\$TYPELIST\$\$|$typelist|g;
600 } else {
601 open HTML, "../assign.html"
602 or croak "Could not open assign.html: $!";
603 $html = join('',<HTML>);
604 close HTML;
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;
611 my $pops = '';
612 foreach my $pop (@poplist) {
613 $pops .= "<option>$pop</option>\n";
614 }
615 $html =~ s|\$\$POPLIST\$\$|$pops|g;
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;
625 }
626 my $cities = '';
627 foreach my $city (@citylist) {
628 $cities .= "<option>$city</option>\n";
629 }
630 $html =~ s|\$\$ALLCITIES\$\$|$cities|g;
631
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
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
657 print $html;
658
659} # assignBlock
660
661
662# Take info on requested IP assignment and see what we can provide.
663sub confirmAssign {
664 if ($IPDBacl{$authuser} !~ /a/) {
665 printError("You shouldn't have been able to get here. Access denied.");
666 return;
667 }
668
669 my $cidr;
670 my $alloc_from;
671
672 # Going to manually validate some items.
673 # custid and city are automagic.
674 return if !validateInput();
675
676# Several different cases here.
677# Static IP vs netblock
678# + Different flavours of static IP
679# + Different flavours of netblock
680
681 if ($webvar{alloctype} =~ /^.i$/) {
682 my ($base,undef) = split //, $webvar{alloctype}; # split into individual chars
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.
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]$/) {
689 $city = "(allocations.city='Sudbury' or allocations.city='North Bay' or ".
690 "allocations.city='Toronto')";
691 } else {
692 $city = "allocations.city='$webvar{pop}'";
693 }
694
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");
700 $sth->execute;
701 my $optionlist;
702 while (my @data = $sth->fetchrow_array) {
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 }
707 }
708 $cidr = "Single static IP";
709 $alloc_from = "<select name=alloc_from>".$optionlist."</select>\n";
710
711 } else { # end show pool options
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}) {
719 printError("Please specify a CIDR mask length.");
720 return;
721 }
722 my $sql;
723 my $city;
724 my $failmsg;
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;
733 if ($webvar{alloctype} eq 'rm') {
734 if ($webvar{allocfrom} ne '-') {
735 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
736 " and cidr <<= '$webvar{allocfrom}'";
737 $sortorder = "maskbits desc";
738 } else {
739 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'";
740 $sortorder = "maskbits desc";
741 }
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.";
745 } else {
746##fixme
747# This section needs serious Pondering.
748 # Pools of most types get assigned to the POP they're "routed from"
749 # This includes WAN blocks and other netblock "containers"
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.");
754 return;
755 }
756 $city = $webvar{city};
757 $failmsg = "No suitable free block found.<br>\nYou will have to route another".
758 " superblock from one of the<br>\nmaster blocks in Sudbury or chose a smaller".
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".
763 " superblock to $webvar{pop}<br>\nfrom one of the master blocks in Sudbury or".
764 " chose a smaller blocksize.";
765 }
766 if (defined $webvar{allocfrom} && $webvar{allocfrom} ne '-') {
767 $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
768 " and cidr <<= '$webvar{allocfrom}' and routed='".
769 (($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
770 $sortorder = "maskbits desc,cidr";
771 } else {
772 $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
773 " and routed='".(($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
774 $sortorder = "maskbits desc,cidr";
775 }
776 }
777 $sql = $sql.$extracond." order by ".$sortorder;
778 $sth = $ip_dbh->prepare($sql);
779 $sth->execute;
780 my @data = $sth->fetchrow_array();
781 if ($data[0] eq "") {
782 printError($failmsg);
783 return;
784 }
785 $cidr = new NetAddr::IP $data[0];
786 } # check for freeblocks assignment or IPDB-controlled assignment
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 }
800 } # if ($webvar{alloctype} =~ /^.i$/)
801
802 open HTML, "../confirm.html"
803 or croak "Could not open confirm.html: $!";
804 my $html = join '', <HTML>;
805 close HTML;
806
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
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;
829 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$webvar{alloctype}}|g;
830 $html =~ s|\$\$ALLOC_FROM\$\$|$alloc_from|g;
831 $html =~ s|\$\$CIDR\$\$|$cidr|g;
832 $webvar{city} = desanitize($webvar{city});
833 $html =~ s|\$\$CITY\$\$|$webvar{city}|g;
834 $html =~ s|\$\$CUSTID\$\$|$webvar{custid}|g;
835 $webvar{circid} = desanitize($webvar{circid});
836 $html =~ s|\$\$CIRCID\$\$|$webvar{circid}|g;
837 $webvar{desc} = desanitize($webvar{desc});
838 $html =~ s|\$\$DESC\$\$|$webvar{desc}|g;
839 $webvar{notes} = desanitize($webvar{notes});
840 $html =~ s|\$\$NOTES\$\$|$webvar{notes}|g;
841 $html =~ s|\$\$ACTION\$\$|insert|g;
842
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 }
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};
855 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
856
857 $i = $i % 2;
858 $html =~ s/\$\$BUTTONROWCOLOUR\$\$/color$i/;
859
860 print $html;
861
862} # end confirmAssign
863
864
865# Do the work of actually inserting a block in the database.
866sub insertAssign {
867 if ($IPDBacl{$authuser} !~ /a/) {
868 printError("You shouldn't have been able to get here. Access denied.");
869 return;
870 }
871 # Some things are done more than once.
872 return if !validateInput();
873
874 if (!defined($webvar{privdata})) {
875 $webvar{privdata} = '';
876 }
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},
882 $webvar{circid}, $webvar{privdata}, $webvar{node});
883
884 if ($code eq 'OK') {
885 if ($webvar{alloctype} =~ /^.i$/) {
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}) ?
889 qq(<div><a href="https://billing.example.com/radius.pl?).
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>");
893 # Notify tech@example.com
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");
897 } else {
898 my $netblock = new NetAddr::IP $webvar{fullcidr};
899 print qq(<div class="center"><div class="heading">The block $webvar{fullcidr} was ).
900 "sucessfully added as: $disp_alloctypes{$webvar{alloctype}}</div>".
901 ( ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) ?
902 qq(<div><a href="https://billing.example.com/radius.pl?).
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>");
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");
911 }
912 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
913 "'$webvar{alloctype}' ($msg)";
914 } else {
915 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
916 "'$webvar{alloctype}' by $authuser failed: '$msg'";
917 printError("Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
918 " failed:<br>\n$msg\n");
919 }
920
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 '-') {
929 printError("Please choose a city.");
930 return;
931 }
932
933 # Alloctype check.
934 chomp $webvar{alloctype};
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
943 # We have different handling for customer allocations and "internal" or "our" allocations
944 if ($def_custids{$webvar{alloctype}} eq '') {
945 if (!$webvar{custid}) {
946 printError("Please enter a customer ID.");
947 return;
948 }
949 if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
950 # Force uppercase for now...
951 $webvar{custid} =~ tr/a-z/A-Z/;
952 # Crosscheck with billing.
953 my $status = CustIDCK->custid_exist($webvar{custid});
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 }
964#"Please enter a valid customer ID- this must be a 7- or 10-digit number, or STAFF for
965#static IPs for staff.");
966 }
967# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
968 } else {
969 # New! Improved! And now Loaded From The Database!!
970 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
971 $webvar{custid} = $def_custids{$webvar{alloctype}};
972 }
973 }
974
975 # Check POP location
976 my $flag;
977 if ($webvar{alloctype} eq 'rm') {
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';
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} =~ /^-$/) {
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';
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$/) {
1015 $sql = "select ip,custid,type,city,circuitid,description,notes,modifystamp,privdata,oldcustid from poolips where ip='$webvar{block}'";
1016 } else {
1017 $sql = "select cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,oldcustid,swip from allocations where cidr='$webvar{block}'"
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
1039 if ($IPDBacl{$authuser} =~ /c/) {
1040 $html =~ s/\$\$CUSTID\$\$/<input type=text name=custid value="$data[1]" maxlength=15 class="regular">/;
1041
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
1046# this has now been Requested, so here goes.
1047
1048##fixme The check here should be built from the database
1049 if ($data[2] =~ /^.[ne]$/) {
1050 # Block that can be changed
1051 my $blockoptions = "<select name=alloctype><option".
1052 (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
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".
1056 (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
1057 (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option".
1058 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
1059 "</select>\n";
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 }
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
1092 $html =~ s/\$\$OLDCUSTID\$\$/$data[9]/g;
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;
1097 } else {
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
1109 $html =~ s/\$\$CUSTID\$\$/$data[1]/g;
1110 $html =~ s/\$\$OLDCUSTID\$\$/$data[9]/g;
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;
1116 }
1117 my ($lastmod,undef) = split /\s+/, $data[7];
1118 $html =~ s/\$\$LASTMOD\$\$/$lastmod/g;
1119
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 {
1124 my $tmp = (($data[10] eq 'n') ? '<input type=checkbox name=swip>' :
1125 '<input type=checkbox name=swip checked=yes>');
1126 $html =~ s/\$\$SWIP\$\$/$tmp/;
1127}
1128
1129 # Allows us to "correctly" colour backgrounds in table
1130 my $i=1;
1131
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
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/) {
1147 $updok = qq(<tr class="color).($i%2).qq("><td colspan=2><div class="center">).
1148 qq(<input type="submit" value=" Update this block " class="regular">).
1149 "</div></td></tr></form>\n";
1150 $i++;
1151 }
1152 $html =~ s/\$\$UPDOK\$\$/$updok/g;
1153
1154 my $delok = '';
1155 if ($IPDBacl{$authuser} =~ /d/) {
1156 $delok = qq(<form method="POST" action="main.cgi">
1157 <tr class="color).($i%2).qq("><td colspan=2 class="regular"><div class=center>
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
1166 print $html;
1167
1168} # edit()
1169
1170
1171# Stuff new info about a block into the db
1172# action=update
1173sub update {
1174 if ($IPDBacl{$authuser} !~ /c/) {
1175 printError("You shouldn't have been able to get here. Access denied.");
1176 return;
1177 }
1178
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
1185 # Make sure incoming data is in correct format - custID among other things.
1186 return if !validateInput;
1187
1188 # SQL transaction wrapper
1189 eval {
1190 # Relatively simple SQL transaction here.
1191 my $sql;
1192 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
1193 $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',".
1194 "circuitid='$webvar{circid}',description='$webvar{desc}',city='$webvar{city}'".
1195 "$privdata where ip='$webvar{block}'";
1196 } else {
1197 $sql = "update allocations set custid='$webvar{custid}',".
1198 "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',".
1199 "type='$webvar{alloctype}',circuitid='$webvar{circid}'$privdata,".
1200 "swip='".($webvar{swip} eq 'on' ? 'y' : 'n')."' ".
1201 "where cidr='$webvar{block}'";
1202 }
1203 # Log the details of the change.
1204 syslog "debug", $sql;
1205 $sth = $ip_dbh->prepare($sql);
1206 $sth->execute;
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
1214 $ip_dbh->commit;
1215 };
1216 if ($@) {
1217 my $msg = $@;
1218 carp "Transaction aborted because $msg";
1219 eval { $ip_dbh->rollback; };
1220 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
1221 printError("Could not update block/IP $webvar{block}: $msg");
1222 return;
1223 }
1224
1225 # If we get here, the operation succeeded.
1226 syslog "notice", "$authuser updated $webvar{block}";
1227#mailNotify('nocmgr@example.com',"SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1228# "$webvar{block} had SWIP status changed to \"Yes\" by $authuser");
1229 open (HTML, "../updated.html")
1230 or croak "Could not open updated.html :$!";
1231 my $html = join('', <HTML>);
1232
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
1252my $swiptmp = ($webvar{swip} eq 'on' ? 'Yes' : 'No');
1253 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
1254 $webvar{city} = desanitize($webvar{city});
1255 $html =~ s/\$\$CITY\$\$/$webvar{city}/g;
1256 $html =~ s/\$\$ALLOCTYPE\$\$/$webvar{alloctype}/g;
1257 $html =~ s/\$\$TYPEFULL\$\$/$disp_alloctypes{$webvar{alloctype}}/g;
1258 $html =~ s/\$\$CUSTID\$\$/$webvar{custid}/g;
1259 $html =~ s/\$\$SWIP\$\$/$swiptmp/g;
1260 $webvar{circid} = desanitize($webvar{circid});
1261 $html =~ s/\$\$CIRCID\$\$/$webvar{circid}/g;
1262 $webvar{desc} = desanitize($webvar{desc});
1263 $html =~ s/\$\$DESC\$\$/$webvar{desc}/g;
1264 $webvar{notes} = desanitize($webvar{notes});
1265 $html =~ s/\$\$NOTES\$\$/$webvar{notes}/g;
1266 $html =~ s/\$\$BACKLINK\$\$/$backlink/g;
1267 $html =~ s/\$\$BACKBLOCK\$\$/$cblock/g;
1268
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
1275 print $html;
1276
1277} # update()
1278
1279
1280# Delete an allocation.
1281sub remove {
1282 if ($IPDBacl{$authuser} !~ /d/) {
1283 printError("You shouldn't have been able to get here. Access denied.");
1284 return;
1285 }
1286
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})) {
1295 printError("Error 332");
1296 return;
1297 }
1298
1299 my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata);
1300
1301 if ($webvar{alloctype} eq 'rm') {
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};
1313 $circid = "N/A";
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};
1322 $circid = "N/A";
1323 $desc = "N/A";
1324 $notes = "N/A";
1325 } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m
1326
1327 # Unassigning a static IP
1328 my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata".
1329 " from poolips where ip='$webvar{block}'");
1330 $sth->execute();
1331# croak $sth->errstr() if($sth->errstr());
1332
1333 $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid,
1334 \$privdata);
1335 $sth->fetch() || croak $sth->errstr;
1336
1337 } else { # done with alloctype=~ /^.i$/
1338
1339 my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata".
1340 " from allocations where cidr='$webvar{block}'");
1341 $sth->execute();
1342# croak $sth->errstr() if($sth->errstr());
1343
1344 $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc,
1345 \$notes, \$privdata);
1346 $sth->fetch() || carp $sth->errstr;
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;
1352 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$alloctype}|g;
1353 $html =~ s|\$\$ALLOCTYPE\$\$|$alloctype|g;
1354 $html =~ s|\$\$CITY\$\$|$city|g;
1355 $html =~ s|\$\$CUSTID\$\$|$custid|g;
1356 $html =~ s|\$\$CIRCID\$\$|$circid|g;
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.
1363 if ($alloctype =~ /^.[pd]$/) {
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
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
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 {
1390 if ($IPDBacl{$authuser} !~ /d/) {
1391 printError("You shouldn't have been able to get here. Access denied.");
1392 return;
1393 }
1394
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
1398 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
1399
1400 if ($code eq 'OK') {
1401 print "<div class=heading align=center>Success! $webvar{block} deallocated.</div>\n";
1402 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}".
1403 " $custid, $city, desc='$description'";
1404 # Notify tech@ when a block/IP is deallocated
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");
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");
1418 }
1419 }
1420
1421} # finalDelete
1422
1423
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
1436# Just in case we manage to get here.
1437exit 0;
Note: See TracBrowser for help on using the repository browser.