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

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

/trunk

Code tidy-up; remove irrelevant obsolete history comment

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