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

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

/trunk

Make sure all syslog-using scripts pick up the global logging
facility instead of hardcoding local2. See #17.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 49.2 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3###
4# SVN revision info
5# $Date: 2010-07-16 19:56:36 +0000 (Fri, 16 Jul 2010) $
6# SVN revision $Rev: 431 $
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#"Please enter a valid customer ID- this must be a 7- or 10-digit number, or STAFF for
968#static IPs for staff.");
969 }
970# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
971 } else {
972 # New! Improved! And now Loaded From The Database!!
973 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
974 $webvar{custid} = $def_custids{$webvar{alloctype}};
975 }
976 }
977
978 # Check POP location
979 my $flag;
980 if ($webvar{alloctype} eq 'rm') {
981 $flag = 'for a routed netblock';
982 foreach (@poplist) {
983 if (/^$webvar{city}$/) {
984 $flag = 'n';
985 last;
986 }
987 }
988 } else {
989 $flag = 'n';
990 if ($webvar{alloctype} =~ /[wp][cr]|[ds][pi]/) {
991 # Set this forcibly rather than messing around elsewhere. Yes, this *is* a hack. PTHBTT!!
992 $webvar{pop} = 'Sudbury';
993 }
994 if ($webvar{pop} =~ /^-$/) {
995 $flag = 'to route the block from/through';
996 }
997 }
998 if ($flag ne 'n') {
999 printError("Please choose a valid POP location $flag. Valid ".
1000 "POP locations are currently:<br>\n".join (" - ", @poplist));
1001 return;
1002 }
1003
1004 return 'OK';
1005} # end validateInput
1006
1007
1008# Displays details of a specific allocation in a form
1009# Allows update/delete
1010# action=edit
1011sub edit {
1012
1013 my $sql;
1014
1015 # Two cases: block is a netblock, or block is a static IP from a pool
1016 # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
1017 if ($webvar{block} =~ /\/32$/) {
1018 $sql = "select ip,custid,type,city,circuitid,description,notes,modifystamp,privdata,oldcustid from poolips where ip='$webvar{block}'";
1019 } else {
1020 $sql = "select cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,oldcustid,swip from allocations where cidr='$webvar{block}'"
1021 }
1022
1023 # gotta snag block info from db
1024 $sth = $ip_dbh->prepare($sql);
1025 $sth->execute;
1026 my @data = $sth->fetchrow_array;
1027
1028 # Clean up extra whitespace on alloc type
1029 $data[2] =~ s/\s//;
1030
1031 open (HTML, "../editDisplay.html")
1032 or croak "Could not open editDisplay.html :$!";
1033 my $html = join('', <HTML>);
1034
1035 # We can't let the city be changed here; this block is a part of
1036 # a larger routed allocation and therefore by definition can't be moved.
1037 # block and city are static.
1038##fixme
1039# Needs thinking. Have to allow changes to city to correct errors, no?
1040 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
1041
1042 if ($IPDBacl{$authuser} =~ /c/) {
1043 $html =~ s/\$\$CUSTID\$\$/<input type=text name=custid value="$data[1]" maxlength=15 class="regular">/;
1044
1045# Screw it. Changing allocation types gets very ugly VERY quickly- especially
1046# with the much longer list of allocation types.
1047# We'll just show what type of block it is.
1048
1049# this has now been Requested, so here goes.
1050
1051##fixme The check here should be built from the database
1052 if ($data[2] =~ /^.[ne]$/) {
1053 # Block that can be changed
1054 my $blockoptions = "<select name=alloctype><option".
1055 (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
1056 (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".
1057 (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option".
1058 (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic wireless netblock</option>\n<option".
1059 (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
1060 (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option".
1061 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
1062 "</select>\n";
1063 $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g;
1064 } else {
1065 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g;
1066 }
1067## node hack
1068 $sth = $ip_dbh->prepare("SELECT node_id FROM noderef WHERE block='$webvar{block}'");
1069 $sth->execute;
1070 my ($nodeid) = $sth->fetchrow_array();
1071 if ($nodeid) {
1072 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
1073 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1074 my $nodes = "<select name=node>\n";
1075 while (my ($nid,$nname) = $sth->fetchrow_array()) {
1076 $nodes .= "<option".($nodeid == $nid ? ' selected' : '')." value='$nid'>$nname</option>\n";
1077 }
1078 $nodes .= "</select>\n";
1079 $html =~ s/\$\$NODE\$\$/$nodes/;
1080 } else {
1081 if ($data[2] eq 'fr' || $data[2] eq 'bi') {
1082 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
1083 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1084 my $nodes = "<select name=node>\n<option value=>--</option>\n";
1085 while (my ($nid,$nname) = $sth->fetchrow_array()) {
1086 $nodes .= "<option value='$nid'>$nname</option>\n";
1087 }
1088 $nodes .= "</select>\n";
1089 $html =~ s/\$\$NODE\$\$/$nodes/;
1090 } else {
1091 $html =~ s|\$\$NODE\$\$|N/A|;
1092 }
1093 }
1094## end node hack
1095 $html =~ s/\$\$OLDCUSTID\$\$/$data[9]/g;
1096 $html =~ s/\$\$CITY\$\$/<input type=text name=city value="$data[3]">/g;
1097 $html =~ s/\$\$CIRCID\$\$/<input type="text" name="circid" value="$data[4]" maxlength=64 size=64 class="regular">/g;
1098 $html =~ s/\$\$DESC\$\$/<input type="text" name="desc" value="$data[5]" maxlength=64 size=64 class="regular">/g;
1099 $html =~ s|\$\$NOTES\$\$|<textarea rows="8" cols="64" name="notes" class="regular">$data[6]</textarea>|g;
1100 } else {
1101## node hack
1102 if ($data[2] eq 'fr' || $data[2] eq 'bi') {
1103 $sth = $ip_dbh->prepare("SELECT node_name FROM nodes INNER JOIN noderef".
1104 " ON nodes.node_id=noderef.node_id WHERE noderef.block='$webvar{block}'");
1105 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1106 my ($node) = $sth->fetchrow_array;
1107 $html =~ s/\$\$NODE\$\$/$node/;
1108 } else {
1109 $html =~ s|\$\$NODE\$\$|N/A|;
1110 }
1111## end node hack
1112 $html =~ s/\$\$CUSTID\$\$/$data[1]/g;
1113 $html =~ s/\$\$OLDCUSTID\$\$/$data[9]/g;
1114 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}/g;
1115 $html =~ s/\$\$CITY\$\$/$data[3]/g;
1116 $html =~ s/\$\$CIRCID\$\$/$data[4]/g;
1117 $html =~ s/\$\$DESC\$\$/$data[5]/g;
1118 $html =~ s/\$\$NOTES\$\$/$data[6]/g;
1119 }
1120 my ($lastmod,undef) = split /\s+/, $data[7];
1121 $html =~ s/\$\$LASTMOD\$\$/$lastmod/g;
1122
1123## Hack time! SWIP isn't going to stay, so I'm not going to integrate it with ACLs.
1124if ($data[2] =~ /.i/) {
1125 $html =~ s/\$\$SWIP\$\$/N\/A/;
1126} else {
1127 my $tmp = (($data[10] eq 'n') ? '<input type=checkbox name=swip>' :
1128 '<input type=checkbox name=swip checked=yes>');
1129 $html =~ s/\$\$SWIP\$\$/$tmp/;
1130}
1131
1132 # Allows us to "correctly" colour backgrounds in table
1133 my $i=1;
1134
1135 # Check to see if we can display sensitive data
1136 my $privdata = '';
1137 if ($IPDBacl{$authuser} =~ /s/) {
1138 $privdata = qq(<tr class="color).($i%2).qq("><td class=heading>Restricted data:</td>).
1139 qq(<td class=regular><textarea rows="3" cols="64" name="privdata" class="regular">).
1140 qq($data[8]</textarea></td></tr>\n);
1141 $i++;
1142 }
1143 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1144
1145 # More ACL trickery - we can live with forms that don't submit,
1146 # but we can't leave the extra table rows there, and we *really*
1147 # can't leave the submit buttons there.
1148 my $updok = '';
1149 if ($IPDBacl{$authuser} =~ /c/) {
1150 $updok = qq(<tr class="color).($i%2).qq("><td colspan=2><div class="center">).
1151 qq(<input type="submit" value=" Update this block " class="regular">).
1152 "</div></td></tr></form>\n";
1153 $i++;
1154 }
1155 $html =~ s/\$\$UPDOK\$\$/$updok/g;
1156
1157 my $delok = '';
1158 if ($IPDBacl{$authuser} =~ /d/) {
1159 $delok = qq(<form method="POST" action="main.cgi">
1160 <tr class="color).($i%2).qq("><td colspan=2 class="regular"><div class=center>
1161 <input type="hidden" name="action" value="delete">
1162 <input type="hidden" name="block" value="$webvar{block}">
1163 <input type="hidden" name="alloctype" value="$data[2]">
1164 <input type=submit value=" Delete this block ">
1165 </div></td></tr>);
1166 }
1167 $html =~ s/\$\$DELOK\$\$/$delok/;
1168
1169 print $html;
1170
1171} # edit()
1172
1173
1174# Stuff new info about a block into the db
1175# action=update
1176sub update {
1177 if ($IPDBacl{$authuser} !~ /c/) {
1178 printError("You shouldn't have been able to get here. Access denied.");
1179 return;
1180 }
1181
1182 # Check to see if we can update restricted data
1183 my $privdata = '';
1184 if ($IPDBacl{$authuser} =~ /s/) {
1185 $privdata = ",privdata='$webvar{privdata}'";
1186 }
1187
1188 # Make sure incoming data is in correct format - custID among other things.
1189 return if !validateInput;
1190
1191 # SQL transaction wrapper
1192 eval {
1193 # Relatively simple SQL transaction here.
1194 my $sql;
1195 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
1196 $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',".
1197 "circuitid='$webvar{circid}',description='$webvar{desc}',city='$webvar{city}'".
1198 "$privdata where ip='$webvar{block}'";
1199 } else {
1200 $sql = "update allocations set custid='$webvar{custid}',".
1201 "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',".
1202 "type='$webvar{alloctype}',circuitid='$webvar{circid}'$privdata,".
1203 "swip='".($webvar{swip} eq 'on' ? 'y' : 'n')."' ".
1204 "where cidr='$webvar{block}'";
1205 }
1206 # Log the details of the change.
1207 syslog "debug", $sql;
1208 $sth = $ip_dbh->prepare($sql);
1209 $sth->execute;
1210## node hack
1211 if ($webvar{node}) {
1212 $ip_dbh->do("DELETE FROM noderef WHERE block='$webvar{block}'");
1213 $sth = $ip_dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)");
1214 $sth->execute($webvar{block},$webvar{node});
1215 }
1216## end node hack
1217 $ip_dbh->commit;
1218 };
1219 if ($@) {
1220 my $msg = $@;
1221 carp "Transaction aborted because $msg";
1222 eval { $ip_dbh->rollback; };
1223 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
1224 printError("Could not update block/IP $webvar{block}: $msg");
1225 return;
1226 }
1227
1228 # If we get here, the operation succeeded.
1229 syslog "notice", "$authuser updated $webvar{block}";
1230##fixme: need to wedge something in to allow "update:field" notifications
1231## hmm. how to tell what changed? O_o
1232mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1233 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
1234 open (HTML, "../updated.html")
1235 or croak "Could not open updated.html :$!";
1236 my $html = join('', <HTML>);
1237
1238 # Link back to browse-routed or list-pool page on "Update complete" page.
1239 my $backlink = "/ip/cgi-bin/main.cgi?action=";
1240 my $cblock; # to contain the CIDR of the container block we're retrieving.
1241 my $sql;
1242 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
1243 $sql = "select pool from poolips where ip='$webvar{block}'";
1244 $backlink .= "listpool&pool=";
1245 } else {
1246 $sql = "select cidr from routed where cidr >>= '$webvar{block}'";
1247 $backlink .= "showrouted&block=";
1248 }
1249 # I define there to be no errors on this operation... so we don't need to check for them.
1250 $sth = $ip_dbh->prepare($sql);
1251 $sth->execute;
1252 $sth->bind_columns(\$cblock);
1253 $sth->fetch();
1254 $sth->finish;
1255 $backlink .= $cblock;
1256
1257my $swiptmp = ($webvar{swip} eq 'on' ? 'Yes' : 'No');
1258 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
1259 $webvar{city} = desanitize($webvar{city});
1260 $html =~ s/\$\$CITY\$\$/$webvar{city}/g;
1261 $html =~ s/\$\$ALLOCTYPE\$\$/$webvar{alloctype}/g;
1262 $html =~ s/\$\$TYPEFULL\$\$/$disp_alloctypes{$webvar{alloctype}}/g;
1263 $html =~ s/\$\$CUSTID\$\$/$webvar{custid}/g;
1264 $html =~ s/\$\$SWIP\$\$/$swiptmp/g;
1265 $webvar{circid} = desanitize($webvar{circid});
1266 $html =~ s/\$\$CIRCID\$\$/$webvar{circid}/g;
1267 $webvar{desc} = desanitize($webvar{desc});
1268 $html =~ s/\$\$DESC\$\$/$webvar{desc}/g;
1269 $webvar{notes} = desanitize($webvar{notes});
1270 $html =~ s/\$\$NOTES\$\$/$webvar{notes}/g;
1271 $html =~ s/\$\$BACKLINK\$\$/$backlink/g;
1272 $html =~ s/\$\$BACKBLOCK\$\$/$cblock/g;
1273
1274 if ($IPDBacl{$authuser} =~ /s/) {
1275 $privdata = qq(<tr class="color2"><td valign="top">Restricted data:</td>).
1276 qq(<td class="regular">).desanitize($webvar{privdata}).qq(</td></tr>\n);
1277 }
1278 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1279
1280 print $html;
1281
1282} # update()
1283
1284
1285# Delete an allocation.
1286sub remove {
1287 if ($IPDBacl{$authuser} !~ /d/) {
1288 printError("You shouldn't have been able to get here. Access denied.");
1289 return;
1290 }
1291
1292 #show confirm screen.
1293 open HTML, "../confirmRemove.html"
1294 or croak "Could not open confirmRemove.html :$!";
1295 my $html = join('', <HTML>);
1296 close HTML;
1297
1298 # Serves'em right for getting here...
1299 if (!defined($webvar{block})) {
1300 printError("Error 332");
1301 return;
1302 }
1303
1304 my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata);
1305
1306 if ($webvar{alloctype} eq 'rm') {
1307 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
1308 $sth->execute();
1309
1310# This feels... extreme.
1311 croak $sth->errstr() if($sth->errstr());
1312
1313 $sth->bind_columns(\$cidr,\$city);
1314 $sth->execute();
1315 $sth->fetch || croak $sth->errstr();
1316 $custid = "N/A";
1317 $alloctype = $webvar{alloctype};
1318 $circid = "N/A";
1319 $desc = "N/A";
1320 $notes = "N/A";
1321
1322 } elsif ($webvar{alloctype} eq 'mm') {
1323 $cidr = $webvar{block};
1324 $city = "N/A";
1325 $custid = "N/A";
1326 $alloctype = $webvar{alloctype};
1327 $circid = "N/A";
1328 $desc = "N/A";
1329 $notes = "N/A";
1330 } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m
1331
1332 # Unassigning a static IP
1333 my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata".
1334 " from poolips where ip='$webvar{block}'");
1335 $sth->execute();
1336# croak $sth->errstr() if($sth->errstr());
1337
1338 $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid,
1339 \$privdata);
1340 $sth->fetch() || croak $sth->errstr;
1341
1342 } else { # done with alloctype=~ /^.i$/
1343
1344 my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata".
1345 " from allocations where cidr='$webvar{block}'");
1346 $sth->execute();
1347# croak $sth->errstr() if($sth->errstr());
1348
1349 $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc,
1350 \$notes, \$privdata);
1351 $sth->fetch() || carp $sth->errstr;
1352 } # end cases for different alloctypes
1353
1354 # Munge everything into HTML
1355 $html =~ s|Please confirm|Please confirm <b>removal</b> of|;
1356 $html =~ s|\$\$BLOCK\$\$|$cidr|g;
1357 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$alloctype}|g;
1358 $html =~ s|\$\$ALLOCTYPE\$\$|$alloctype|g;
1359 $html =~ s|\$\$CITY\$\$|$city|g;
1360 $html =~ s|\$\$CUSTID\$\$|$custid|g;
1361 $html =~ s|\$\$CIRCID\$\$|$circid|g;
1362 $html =~ s|\$\$DESC\$\$|$desc|g;
1363 $html =~ s|\$\$NOTES\$\$|$notes|g;
1364
1365 $html =~ s|\$\$ACTION\$\$|finaldelete|g;
1366
1367 # Set the warning text.
1368 if ($alloctype =~ /^.[pd]$/) {
1369 $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>|;
1370 } else {
1371 $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.</div></td></tr>|;
1372 }
1373
1374 my $i = 1;
1375 # Check to see if user is allowed to do anything with sensitive data
1376 if ($IPDBacl{$authuser} =~ /s/) {
1377 $privdata = qq(<tr class="color).($i%2).qq("><td>Restricted data:</td>).
1378 qq(<td class=regular>$privdata</td></tr>\n);
1379 $i++;
1380 }
1381 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1382
1383 $i = ++$i % 2;
1384 $html =~ s/\$\$BUTTONROWCOLOUR\$\$/color$i/;
1385
1386 print $html;
1387} # end edit()
1388
1389
1390# Delete an allocation. Return it to the freeblocks table; munge
1391# data as necessary to keep as few records as possible in freeblocks
1392# to prevent weirdness when allocating blocks later.
1393# Remove IPs from pool listing if necessary
1394sub finalDelete {
1395 if ($IPDBacl{$authuser} !~ /d/) {
1396 printError("You shouldn't have been able to get here. Access denied.");
1397 return;
1398 }
1399
1400 # need to retrieve block data before deleting so we can notify on that
1401 my ($cidr,$custid,$type,$city,$description) = getBlockData($ip_dbh, $webvar{block});
1402
1403 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
1404
1405 if ($code eq 'OK') {
1406 print "<div class=heading align=center>Success! $webvar{block} deallocated.</div>\n";
1407 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}".
1408 " $custid, $city, desc='$description'";
1409 mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1410 "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
1411 "CustID: $custid\nCity: $city\nDescription: $description\n");
1412 } else {
1413 if ($webvar{alloctype} =~ /^.i$/) {
1414 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1415 printError("Could not deallocate static IP $webvar{block}: $msg");
1416 } else {
1417 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
1418 printError("Could not deallocate netblock $webvar{block}: $msg");
1419 }
1420 }
1421
1422} # finalDelete
1423
1424
1425sub exitError {
1426 my $errStr = $_[0];
1427 printHeader('','');
1428 print qq(<center><p class="regular"> $errStr </p>
1429<input type="button" value="Back" onclick="history.go(-1)">
1430</center>
1431);
1432 printFooter();
1433 exit;
1434} # errorExit
1435
1436
1437# Just in case we manage to get here.
1438exit 0;
Note: See TracBrowser for help on using the repository browser.