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

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

/trunk

Remove some ViaNet-isms (reference to city names with primary POPs).
See #26.
Update alloctypes.html to reflect current preseeded alloctypes

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 48.3 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3###
4# SVN revision info
5# $Date: 2010-07-21 20:08:10 +0000 (Wed, 21 Jul 2010) $
6# SVN revision $Rev: 442 $
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
688# Ewww. But it works.
689 $sth = $ip_dbh->prepare("SELECT (SELECT city FROM allocations WHERE cidr=poolips.pool), ".
690 "poolips.pool, COUNT(*) FROM poolips,allocations WHERE poolips.available='y' AND ".
691 "poolips.pool=allocations.cidr AND allocations.city='$webvar{pop}' AND poolips.type LIKE '".$base."_' ".
692 "GROUP BY pool");
693 $sth->execute;
694 my $optionlist;
695 while (my @data = $sth->fetchrow_array) {
696 # city,pool cidr,free IP count
697 if ($data[2] > 0) {
698 $optionlist .= "<option value='$data[1]'>$data[1] [$data[2] free IP(s)] in $data[0]</option>\n";
699 }
700 }
701 $cidr = "Single static IP";
702 $alloc_from = "<select name=alloc_from>".$optionlist."</select>\n";
703
704 } else { # end show pool options
705
706 if ($webvar{fbassign} eq 'y') {
707 $cidr = new NetAddr::IP $webvar{block};
708 $webvar{maskbits} = $cidr->masklen;
709 } else { # done with direct freeblocks assignment
710
711 if (!$webvar{maskbits}) {
712 printError("Please specify a CIDR mask length.");
713 return;
714 }
715 my $sql;
716 my $city;
717 my $failmsg;
718 my $extracond = '';
719 if ($webvar{allocfrom} eq '-') {
720 $extracond = ($webvar{allowpriv} eq 'on' ? '' :
721 " and not (cidr <<= '192.168.0.0/16'".
722 " or cidr <<= '10.0.0.0/8'".
723 " or cidr <<= '172.16.0.0/12')");
724 }
725 my $sortorder;
726 if ($webvar{alloctype} eq 'rm') {
727 if ($webvar{allocfrom} ne '-') {
728 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
729 " and cidr <<= '$webvar{allocfrom}'";
730 $sortorder = "maskbits desc";
731 } else {
732 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'";
733 $sortorder = "maskbits desc";
734 }
735 $failmsg = "No suitable free block found.<br>\nWe do not have a free".
736 " routeable block of that size.<br>\nYou will have to either route".
737 " a set of smaller netblocks or a single smaller netblock.";
738 } else {
739##fixme
740# This section needs serious Pondering.
741 # Pools of most types get assigned to the POP they're "routed from"
742 # This includes WAN blocks and other netblock "containers"
743 # This does NOT include cable pools.
744 if ($webvar{alloctype} =~ /^.[pc]$/) {
745 $city = $webvar{city};
746 $failmsg = "No suitable free block found.<br>\nYou will have to route another".
747 " superblock from one of the<br>\nmaster blocks or chose a smaller".
748 " block size for the pool.";
749 } else {
750 $city = $webvar{pop};
751 $failmsg = "No suitable free block found.<br>\nYou will have to route another".
752 " superblock to $webvar{pop}<br>\nfrom one of the master blocks or".
753 " chose a smaller blocksize.";
754 }
755 if (defined $webvar{allocfrom} && $webvar{allocfrom} ne '-') {
756 $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
757 " and cidr <<= '$webvar{allocfrom}' and routed='".
758 (($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
759 $sortorder = "maskbits desc,cidr";
760 } else {
761 $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
762 " and routed='".(($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
763 $sortorder = "maskbits desc,cidr";
764 }
765 }
766 $sql = $sql.$extracond." order by ".$sortorder;
767 $sth = $ip_dbh->prepare($sql);
768 $sth->execute;
769 my @data = $sth->fetchrow_array();
770 if ($data[0] eq "") {
771 printError($failmsg);
772 return;
773 }
774 $cidr = new NetAddr::IP $data[0];
775 } # check for freeblocks assignment or IPDB-controlled assignment
776
777 $alloc_from = qq($cidr<input type=hidden name=alloc_from value="$cidr">);
778
779 # If the block to be allocated is smaller than the one we found,
780 # figure out the "real" block to be allocated.
781 if ($cidr->masklen() ne $webvar{maskbits}) {
782 my $maskbits = $cidr->masklen();
783 my @subblocks;
784 while ($maskbits++ < $webvar{maskbits}) {
785 @subblocks = $cidr->split($maskbits);
786 }
787 $cidr = $subblocks[0];
788 }
789 } # if ($webvar{alloctype} =~ /^.i$/)
790
791 open HTML, "../confirm.html"
792 or croak "Could not open confirm.html: $!";
793 my $html = join '', <HTML>;
794 close HTML;
795
796## node hack
797 if ($webvar{node} && $webvar{node} ne '-') {
798 $sth = $ip_dbh->prepare("SELECT node_name FROM nodes WHERE node_id=?");
799 $sth->execute($webvar{node});
800 my ($nodename) = $sth->fetchrow_array();
801 $html =~ s/\$\$NODENAME\$\$/$nodename/;
802 $html =~ s/\$\$NODEID\$\$/$webvar{node}/;
803 } else {
804 $html =~ s/\$\$NODENAME\$\$//;
805 $html =~ s/\$\$NODEID\$\$//;
806 }
807## end node hack
808
809### gotta fix this in final
810 # Stick in customer info as necessary - if it's blank, it just ends
811 # up as blank lines ignored in the rendering of the page
812 my $custbits;
813 $html =~ s|\$\$CUSTBITS\$\$|$custbits|g;
814###
815
816 # Stick in the allocation data
817 $html =~ s|\$\$ALLOC_TYPE\$\$|$webvar{alloctype}|g;
818 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$webvar{alloctype}}|g;
819 $html =~ s|\$\$ALLOC_FROM\$\$|$alloc_from|g;
820 $html =~ s|\$\$CIDR\$\$|$cidr|g;
821 $webvar{city} = desanitize($webvar{city});
822 $html =~ s|\$\$CITY\$\$|$webvar{city}|g;
823 $html =~ s|\$\$CUSTID\$\$|$webvar{custid}|g;
824 $webvar{circid} = desanitize($webvar{circid});
825 $html =~ s|\$\$CIRCID\$\$|$webvar{circid}|g;
826 $webvar{desc} = desanitize($webvar{desc});
827 $html =~ s|\$\$DESC\$\$|$webvar{desc}|g;
828 $webvar{notes} = desanitize($webvar{notes});
829 $html =~ s|\$\$NOTES\$\$|$webvar{notes}|g;
830 $html =~ s|\$\$ACTION\$\$|insert|g;
831
832 my $i=1;
833 # Check to see if user is allowed to do anything with sensitive data
834 my $privdata = '';
835 if ($IPDBacl{$authuser} =~ /s/) {
836 $privdata = qq(<tr class="color).($i%2).qq("><td>Restricted data:</td>).
837 qq(<td class=regular>$webvar{privdata}).
838 qq(<input type=hidden name=privdata value="$webvar{privdata}"></td></tr>\n);
839 $i++;
840 }
841# We're going to abuse $$PRIVDATA$$ to stuff in some stuff for billing.
842 $privdata .= "<input type=hidden name=billinguser value=$webvar{userid}>\n"
843 if $webvar{userid};
844 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
845
846 $i = $i % 2;
847 $html =~ s/\$\$BUTTONROWCOLOUR\$\$/color$i/;
848
849 print $html;
850
851} # end confirmAssign
852
853
854# Do the work of actually inserting a block in the database.
855sub insertAssign {
856 if ($IPDBacl{$authuser} !~ /a/) {
857 printError("You shouldn't have been able to get here. Access denied.");
858 return;
859 }
860 # Some things are done more than once.
861 return if !validateInput();
862
863 if (!defined($webvar{privdata})) {
864 $webvar{privdata} = '';
865 }
866 # $code is "success" vs "failure", $msg contains OK for a
867 # successful netblock allocation, the IP allocated for static
868 # IP, or the error message if an error occurred.
869 my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
870 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
871 $webvar{circid}, $webvar{privdata}, $webvar{node});
872
873 if ($code eq 'OK') {
874 if ($webvar{alloctype} =~ /^.i$/) {
875 $msg =~ s|/32||;
876 print qq(<div class="center"><div class="heading">The IP $msg has been allocated to customer $webvar{custid}</div>).
877 ( ($webvar{alloctype} eq 'di' && $webvar{billinguser}) ?
878 qq(<div><a href="https://billing.example.com/radius.pl?).
879 "action=new_radius_user&custid=$webvar{custid}&userid=$webvar{billinguser}".
880 qq(&ipdb=1&ip=$msg">Add this IP to RADIUS user table</a></div>)
881 : "</div>");
882 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
883 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
884 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
885 } else {
886 my $netblock = new NetAddr::IP $webvar{fullcidr};
887 print qq(<div class="center"><div class="heading">The block $webvar{fullcidr} was ).
888 "sucessfully added as: $disp_alloctypes{$webvar{alloctype}}</div>".
889 ( ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) ?
890 qq(<div><a href="https://billing.example.com/radius.pl?).
891 "action=new_radius_user&custid=$webvar{custid}&userid=$webvar{billinguser}".
892 "&route_subnet=".$netblock->addr."&subnet_slash=".$netblock->masklen.
893 "&include_routed_subnet=1&ipdb=1".
894 qq(">Add this netblock to RADIUS user table</a></div>)
895 : "</div>");
896 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
897 "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
898 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
899 }
900 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
901 "'$webvar{alloctype}' ($msg)";
902 } else {
903 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
904 "'$webvar{alloctype}' by $authuser failed: '$msg'";
905 printError("Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
906 " failed:<br>\n$msg\n");
907 }
908
909} # end insertAssign()
910
911
912# Does some basic checks on common input data to make sure nothing
913# *really* weird gets in to the database through this script.
914# Does NOT do complete input validation!!!
915sub validateInput {
916 if ($webvar{city} eq '-') {
917 printError("Please choose a city.");
918 return;
919 }
920
921 # Alloctype check.
922 chomp $webvar{alloctype};
923 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
924 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
925 # managing to call things in such a way as to cause this deserves a cryptic error.
926 printError("Invalid alloctype");
927 return;
928 }
929
930 # CustID check
931 # We have different handling for customer allocations and "internal" or "our" allocations
932 if ($def_custids{$webvar{alloctype}} eq '') {
933 if (!$webvar{custid}) {
934 printError("Please enter a customer ID.");
935 return;
936 }
937 if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
938 # Force uppercase for now...
939 $webvar{custid} =~ tr/a-z/A-Z/;
940 # Crosscheck with billing.
941 my $status = CustIDCK->custid_exist($webvar{custid});
942 if ($CustIDCK::Error) {
943 printError("Error verifying customer ID: ".$CustIDCK::ErrMsg);
944 return;
945 }
946 if (!$status) {
947 printError("Customer ID not valid. Make sure the Customer ID ".
948 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
949 "non-customer assignments.");
950 return;
951 }
952 }
953# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
954 } else {
955 # New! Improved! And now Loaded From The Database!!
956 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
957 $webvar{custid} = $def_custids{$webvar{alloctype}};
958 }
959 }
960
961 # Check POP location
962 my $flag;
963 if ($webvar{alloctype} eq 'rm') {
964 $flag = 'for a routed netblock';
965 foreach (@poplist) {
966 if (/^$webvar{city}$/) {
967 $flag = 'n';
968 last;
969 }
970 }
971 } else {
972 $flag = 'n';
973##fixme: hook to force-set POP or city on certain alloctypes
974# if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
975 if ($webvar{pop} =~ /^-$/) {
976 $flag = 'to route the block from/through';
977 }
978 }
979 if ($flag ne 'n') {
980 printError("Please choose a valid POP location $flag. Valid ".
981 "POP locations are currently:<br>\n".join (" - ", @poplist));
982 return;
983 }
984
985 return 'OK';
986} # end validateInput
987
988
989# Displays details of a specific allocation in a form
990# Allows update/delete
991# action=edit
992sub edit {
993
994 my $sql;
995
996 # Two cases: block is a netblock, or block is a static IP from a pool
997 # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
998 if ($webvar{block} =~ /\/32$/) {
999 $sql = "select ip,custid,type,city,circuitid,description,notes,modifystamp,privdata,oldcustid from poolips where ip='$webvar{block}'";
1000 } else {
1001 $sql = "select cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,oldcustid,swip from allocations where cidr='$webvar{block}'"
1002 }
1003
1004 # gotta snag block info from db
1005 $sth = $ip_dbh->prepare($sql);
1006 $sth->execute;
1007 my @data = $sth->fetchrow_array;
1008
1009 # Clean up extra whitespace on alloc type
1010 $data[2] =~ s/\s//;
1011
1012 open (HTML, "../editDisplay.html")
1013 or croak "Could not open editDisplay.html :$!";
1014 my $html = join('', <HTML>);
1015
1016 # We can't let the city be changed here; this block is a part of
1017 # a larger routed allocation and therefore by definition can't be moved.
1018 # block and city are static.
1019##fixme
1020# Needs thinking. Have to allow changes to city to correct errors, no?
1021 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
1022
1023 if ($IPDBacl{$authuser} =~ /c/) {
1024 $html =~ s/\$\$CUSTID\$\$/<input type=text name=custid value="$data[1]" maxlength=15 class="regular">/;
1025
1026# Screw it. Changing allocation types gets very ugly VERY quickly- especially
1027# with the much longer list of allocation types.
1028# We'll just show what type of block it is.
1029
1030# this has now been Requested, so here goes.
1031
1032##fixme The check here should be built from the database
1033 if ($data[2] =~ /^.[ne]$/) {
1034 # Block that can be changed
1035 my $blockoptions = "<select name=alloctype><option".
1036 (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
1037 (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".
1038 (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option".
1039 (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic wireless netblock</option>\n<option".
1040 (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
1041 (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option".
1042 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
1043 "</select>\n";
1044 $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g;
1045 } else {
1046 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g;
1047 }
1048## node hack
1049 $sth = $ip_dbh->prepare("SELECT node_id FROM noderef WHERE block='$webvar{block}'");
1050 $sth->execute;
1051 my ($nodeid) = $sth->fetchrow_array();
1052 if ($nodeid) {
1053 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
1054 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1055 my $nodes = "<select name=node>\n";
1056 while (my ($nid,$nname) = $sth->fetchrow_array()) {
1057 $nodes .= "<option".($nodeid == $nid ? ' selected' : '')." value='$nid'>$nname</option>\n";
1058 }
1059 $nodes .= "</select>\n";
1060 $html =~ s/\$\$NODE\$\$/$nodes/;
1061 } else {
1062 if ($data[2] eq 'fr' || $data[2] eq 'bi') {
1063 $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
1064 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1065 my $nodes = "<select name=node>\n<option value=>--</option>\n";
1066 while (my ($nid,$nname) = $sth->fetchrow_array()) {
1067 $nodes .= "<option value='$nid'>$nname</option>\n";
1068 }
1069 $nodes .= "</select>\n";
1070 $html =~ s/\$\$NODE\$\$/$nodes/;
1071 } else {
1072 $html =~ s|\$\$NODE\$\$|N/A|;
1073 }
1074 }
1075## end node hack
1076 $html =~ s/\$\$OLDCUSTID\$\$/$data[9]/g;
1077 $html =~ s/\$\$CITY\$\$/<input type=text name=city value="$data[3]">/g;
1078 $html =~ s/\$\$CIRCID\$\$/<input type="text" name="circid" value="$data[4]" maxlength=64 size=64 class="regular">/g;
1079 $html =~ s/\$\$DESC\$\$/<input type="text" name="desc" value="$data[5]" maxlength=64 size=64 class="regular">/g;
1080 $html =~ s|\$\$NOTES\$\$|<textarea rows="8" cols="64" name="notes" class="regular">$data[6]</textarea>|g;
1081 } else {
1082## node hack
1083 if ($data[2] eq 'fr' || $data[2] eq 'bi') {
1084 $sth = $ip_dbh->prepare("SELECT node_name FROM nodes INNER JOIN noderef".
1085 " ON nodes.node_id=noderef.node_id WHERE noderef.block='$webvar{block}'");
1086 $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
1087 my ($node) = $sth->fetchrow_array;
1088 $html =~ s/\$\$NODE\$\$/$node/;
1089 } else {
1090 $html =~ s|\$\$NODE\$\$|N/A|;
1091 }
1092## end node hack
1093 $html =~ s/\$\$CUSTID\$\$/$data[1]/g;
1094 $html =~ s/\$\$OLDCUSTID\$\$/$data[9]/g;
1095 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}/g;
1096 $html =~ s/\$\$CITY\$\$/$data[3]/g;
1097 $html =~ s/\$\$CIRCID\$\$/$data[4]/g;
1098 $html =~ s/\$\$DESC\$\$/$data[5]/g;
1099 $html =~ s/\$\$NOTES\$\$/$data[6]/g;
1100 }
1101 my ($lastmod,undef) = split /\s+/, $data[7];
1102 $html =~ s/\$\$LASTMOD\$\$/$lastmod/g;
1103
1104## Hack time! SWIP isn't going to stay, so I'm not going to integrate it with ACLs.
1105if ($data[2] =~ /.i/) {
1106 $html =~ s/\$\$SWIP\$\$/N\/A/;
1107} else {
1108 my $tmp = (($data[10] eq 'n') ? '<input type=checkbox name=swip>' :
1109 '<input type=checkbox name=swip checked=yes>');
1110 $html =~ s/\$\$SWIP\$\$/$tmp/;
1111}
1112
1113 # Allows us to "correctly" colour backgrounds in table
1114 my $i=1;
1115
1116 # Check to see if we can display sensitive data
1117 my $privdata = '';
1118 if ($IPDBacl{$authuser} =~ /s/) {
1119 $privdata = qq(<tr class="color).($i%2).qq("><td class=heading>Restricted data:</td>).
1120 qq(<td class=regular><textarea rows="3" cols="64" name="privdata" class="regular">).
1121 qq($data[8]</textarea></td></tr>\n);
1122 $i++;
1123 }
1124 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1125
1126 # More ACL trickery - we can live with forms that don't submit,
1127 # but we can't leave the extra table rows there, and we *really*
1128 # can't leave the submit buttons there.
1129 my $updok = '';
1130 if ($IPDBacl{$authuser} =~ /c/) {
1131 $updok = qq(<tr class="color).($i%2).qq("><td colspan=2><div class="center">).
1132 qq(<input type="submit" value=" Update this block " class="regular">).
1133 "</div></td></tr></form>\n";
1134 $i++;
1135 }
1136 $html =~ s/\$\$UPDOK\$\$/$updok/g;
1137
1138 my $delok = '';
1139 if ($IPDBacl{$authuser} =~ /d/) {
1140 $delok = qq(<form method="POST" action="main.cgi">
1141 <tr class="color).($i%2).qq("><td colspan=2 class="regular"><div class=center>
1142 <input type="hidden" name="action" value="delete">
1143 <input type="hidden" name="block" value="$webvar{block}">
1144 <input type="hidden" name="alloctype" value="$data[2]">
1145 <input type=submit value=" Delete this block ">
1146 </div></td></tr>);
1147 }
1148 $html =~ s/\$\$DELOK\$\$/$delok/;
1149
1150 print $html;
1151
1152} # edit()
1153
1154
1155# Stuff new info about a block into the db
1156# action=update
1157sub update {
1158 if ($IPDBacl{$authuser} !~ /c/) {
1159 printError("You shouldn't have been able to get here. Access denied.");
1160 return;
1161 }
1162
1163 # Check to see if we can update restricted data
1164 my $privdata = '';
1165 if ($IPDBacl{$authuser} =~ /s/) {
1166 $privdata = ",privdata='$webvar{privdata}'";
1167 }
1168
1169 # Make sure incoming data is in correct format - custID among other things.
1170 return if !validateInput;
1171
1172 # SQL transaction wrapper
1173 eval {
1174 # Relatively simple SQL transaction here.
1175 my $sql;
1176 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
1177 $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',".
1178 "circuitid='$webvar{circid}',description='$webvar{desc}',city='$webvar{city}'".
1179 "$privdata where ip='$webvar{block}'";
1180 } else {
1181 $sql = "update allocations set custid='$webvar{custid}',".
1182 "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',".
1183 "type='$webvar{alloctype}',circuitid='$webvar{circid}'$privdata,".
1184 "swip='".($webvar{swip} eq 'on' ? 'y' : 'n')."' ".
1185 "where cidr='$webvar{block}'";
1186 }
1187 # Log the details of the change.
1188 syslog "debug", $sql;
1189 $sth = $ip_dbh->prepare($sql);
1190 $sth->execute;
1191## node hack
1192 if ($webvar{node}) {
1193 $ip_dbh->do("DELETE FROM noderef WHERE block='$webvar{block}'");
1194 $sth = $ip_dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)");
1195 $sth->execute($webvar{block},$webvar{node});
1196 }
1197## end node hack
1198 $ip_dbh->commit;
1199 };
1200 if ($@) {
1201 my $msg = $@;
1202 carp "Transaction aborted because $msg";
1203 eval { $ip_dbh->rollback; };
1204 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
1205 printError("Could not update block/IP $webvar{block}: $msg");
1206 return;
1207 }
1208
1209 # If we get here, the operation succeeded.
1210 syslog "notice", "$authuser updated $webvar{block}";
1211##fixme: need to wedge something in to allow "update:field" notifications
1212## hmm. how to tell what changed? O_o
1213mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1214 "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
1215 open (HTML, "../updated.html")
1216 or croak "Could not open updated.html :$!";
1217 my $html = join('', <HTML>);
1218
1219 # Link back to browse-routed or list-pool page on "Update complete" page.
1220 my $backlink = "/ip/cgi-bin/main.cgi?action=";
1221 my $cblock; # to contain the CIDR of the container block we're retrieving.
1222 my $sql;
1223 if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
1224 $sql = "select pool from poolips where ip='$webvar{block}'";
1225 $backlink .= "listpool&pool=";
1226 } else {
1227 $sql = "select cidr from routed where cidr >>= '$webvar{block}'";
1228 $backlink .= "showrouted&block=";
1229 }
1230 # I define there to be no errors on this operation... so we don't need to check for them.
1231 $sth = $ip_dbh->prepare($sql);
1232 $sth->execute;
1233 $sth->bind_columns(\$cblock);
1234 $sth->fetch();
1235 $sth->finish;
1236 $backlink .= $cblock;
1237
1238my $swiptmp = ($webvar{swip} eq 'on' ? 'Yes' : 'No');
1239 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
1240 $webvar{city} = desanitize($webvar{city});
1241 $html =~ s/\$\$CITY\$\$/$webvar{city}/g;
1242 $html =~ s/\$\$ALLOCTYPE\$\$/$webvar{alloctype}/g;
1243 $html =~ s/\$\$TYPEFULL\$\$/$disp_alloctypes{$webvar{alloctype}}/g;
1244 $html =~ s/\$\$CUSTID\$\$/$webvar{custid}/g;
1245 $html =~ s/\$\$SWIP\$\$/$swiptmp/g;
1246 $webvar{circid} = desanitize($webvar{circid});
1247 $html =~ s/\$\$CIRCID\$\$/$webvar{circid}/g;
1248 $webvar{desc} = desanitize($webvar{desc});
1249 $html =~ s/\$\$DESC\$\$/$webvar{desc}/g;
1250 $webvar{notes} = desanitize($webvar{notes});
1251 $html =~ s/\$\$NOTES\$\$/$webvar{notes}/g;
1252 $html =~ s/\$\$BACKLINK\$\$/$backlink/g;
1253 $html =~ s/\$\$BACKBLOCK\$\$/$cblock/g;
1254
1255 if ($IPDBacl{$authuser} =~ /s/) {
1256 $privdata = qq(<tr class="color2"><td valign="top">Restricted data:</td>).
1257 qq(<td class="regular">).desanitize($webvar{privdata}).qq(</td></tr>\n);
1258 }
1259 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1260
1261 print $html;
1262
1263} # update()
1264
1265
1266# Delete an allocation.
1267sub remove {
1268 if ($IPDBacl{$authuser} !~ /d/) {
1269 printError("You shouldn't have been able to get here. Access denied.");
1270 return;
1271 }
1272
1273 #show confirm screen.
1274 open HTML, "../confirmRemove.html"
1275 or croak "Could not open confirmRemove.html :$!";
1276 my $html = join('', <HTML>);
1277 close HTML;
1278
1279 # Serves'em right for getting here...
1280 if (!defined($webvar{block})) {
1281 printError("Error 332");
1282 return;
1283 }
1284
1285 my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata);
1286
1287 if ($webvar{alloctype} eq 'rm') {
1288 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
1289 $sth->execute();
1290
1291# This feels... extreme.
1292 croak $sth->errstr() if($sth->errstr());
1293
1294 $sth->bind_columns(\$cidr,\$city);
1295 $sth->execute();
1296 $sth->fetch || croak $sth->errstr();
1297 $custid = "N/A";
1298 $alloctype = $webvar{alloctype};
1299 $circid = "N/A";
1300 $desc = "N/A";
1301 $notes = "N/A";
1302
1303 } elsif ($webvar{alloctype} eq 'mm') {
1304 $cidr = $webvar{block};
1305 $city = "N/A";
1306 $custid = "N/A";
1307 $alloctype = $webvar{alloctype};
1308 $circid = "N/A";
1309 $desc = "N/A";
1310 $notes = "N/A";
1311 } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m
1312
1313 # Unassigning a static IP
1314 my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata".
1315 " from poolips where ip='$webvar{block}'");
1316 $sth->execute();
1317# croak $sth->errstr() if($sth->errstr());
1318
1319 $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid,
1320 \$privdata);
1321 $sth->fetch() || croak $sth->errstr;
1322
1323 } else { # done with alloctype=~ /^.i$/
1324
1325 my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata".
1326 " from allocations where cidr='$webvar{block}'");
1327 $sth->execute();
1328# croak $sth->errstr() if($sth->errstr());
1329
1330 $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc,
1331 \$notes, \$privdata);
1332 $sth->fetch() || carp $sth->errstr;
1333 } # end cases for different alloctypes
1334
1335 # Munge everything into HTML
1336 $html =~ s|Please confirm|Please confirm <b>removal</b> of|;
1337 $html =~ s|\$\$BLOCK\$\$|$cidr|g;
1338 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$alloctype}|g;
1339 $html =~ s|\$\$ALLOCTYPE\$\$|$alloctype|g;
1340 $html =~ s|\$\$CITY\$\$|$city|g;
1341 $html =~ s|\$\$CUSTID\$\$|$custid|g;
1342 $html =~ s|\$\$CIRCID\$\$|$circid|g;
1343 $html =~ s|\$\$DESC\$\$|$desc|g;
1344 $html =~ s|\$\$NOTES\$\$|$notes|g;
1345
1346 $html =~ s|\$\$ACTION\$\$|finaldelete|g;
1347
1348 # Set the warning text.
1349 if ($alloctype =~ /^.[pd]$/) {
1350 $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>|;
1351 } else {
1352 $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.</div></td></tr>|;
1353 }
1354
1355 my $i = 1;
1356 # Check to see if user is allowed to do anything with sensitive data
1357 if ($IPDBacl{$authuser} =~ /s/) {
1358 $privdata = qq(<tr class="color).($i%2).qq("><td>Restricted data:</td>).
1359 qq(<td class=regular>$privdata</td></tr>\n);
1360 $i++;
1361 }
1362 $html =~ s/\$\$PRIVDATA\$\$/$privdata/g;
1363
1364 $i = ++$i % 2;
1365 $html =~ s/\$\$BUTTONROWCOLOUR\$\$/color$i/;
1366
1367 print $html;
1368} # end edit()
1369
1370
1371# Delete an allocation. Return it to the freeblocks table; munge
1372# data as necessary to keep as few records as possible in freeblocks
1373# to prevent weirdness when allocating blocks later.
1374# Remove IPs from pool listing if necessary
1375sub finalDelete {
1376 if ($IPDBacl{$authuser} !~ /d/) {
1377 printError("You shouldn't have been able to get here. Access denied.");
1378 return;
1379 }
1380
1381 # need to retrieve block data before deleting so we can notify on that
1382 my ($cidr,$custid,$type,$city,$description) = getBlockData($ip_dbh, $webvar{block});
1383
1384 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
1385
1386 if ($code eq 'OK') {
1387 print "<div class=heading align=center>Success! $webvar{block} deallocated.</div>\n";
1388 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}".
1389 " $custid, $city, desc='$description'";
1390 mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
1391 "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
1392 "CustID: $custid\nCity: $city\nDescription: $description\n");
1393 } else {
1394 if ($webvar{alloctype} =~ /^.i$/) {
1395 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1396 printError("Could not deallocate static IP $webvar{block}: $msg");
1397 } else {
1398 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
1399 printError("Could not deallocate netblock $webvar{block}: $msg");
1400 }
1401 }
1402
1403} # finalDelete
1404
1405
1406sub exitError {
1407 my $errStr = $_[0];
1408 printHeader('','');
1409 print qq(<center><p class="regular"> $errStr </p>
1410<input type="button" value="Back" onclick="history.go(-1)">
1411</center>
1412);
1413 printFooter();
1414 exit;
1415} # errorExit
1416
1417
1418# Just in case we manage to get here.
1419exit 0;
Note: See TracBrowser for help on using the repository browser.