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

Last change on this file since 158 was 158, checked in by Kris Deugau, 20 years ago

/branches/stable

Merge all changes from /trunk r141-144 and r146

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 43.3 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/main.cgi
3# Started munging from noc.vianet's old IPDB 04/22/2004
4###
5# SVN revision info
6# $Date: 2005-02-09 20:53:56 +0000 (Wed, 09 Feb 2005) $
7# SVN revision $Rev: 158 $
8# Last update by $Author: kdeugau $
9###
10
11use strict;
12use warnings;
13use CGI::Carp qw(fatalsToBrowser);
14use DBI;
15use CommonWeb qw(:ALL);
16use MyIPDB;
17use CustIDCK;
18use POSIX qw(ceil);
19use NetAddr::IP;
20
21use Sys::Syslog;
22
23openlog "IPDB","pid","local2";
24
25# Collect the username from HTTP auth. If undefined, we're in a test environment.
26my $authuser;
27if (!defined($ENV{'REMOTE_USER'})) {
28 $authuser = '__temptest';
29} else {
30 $authuser = $ENV{'REMOTE_USER'};
31}
32
33syslog "debug", "$authuser active";
34
35# Why not a global DB handle? (And a global statement handle, as well...)
36# Use the connectDB function, otherwise we end up confusing ourselves
37my $ip_dbh;
38my $sth;
39my $errstr;
40($ip_dbh,$errstr) = connectDB_My;
41if (!$ip_dbh) {
42 printAndExit("Failed to connect to database: $errstr\n");
43}
44checkDBSanity($ip_dbh);
45initIPDBGlobals($ip_dbh);
46
47#prototypes
48sub viewBy($$); # feed it the category and query
49sub queryResults($$$); # args is the sql, the page# and the rowCount
50# Needs rewrite/rename
51sub countRows($); # returns first element of first row of passed SQL
52 # Only usage passes "select count(*) ..."
53
54# Global variables
55my $RESULTS_PER_PAGE = 50;
56my %webvar = parse_post();
57cleanInput(\%webvar);
58
59# Stuff that gets loaded from the database
60#my @citylist;
61#my @poplist;
62#my %allocated; # Count for allocated blocks in a master block
63#my %free; # Count for free blocks (routed and unrouted) in a master block
64#my %bigfree; # Tracking largest free block in a master block
65#my %routed; # Number of routed blocks in a master block
66
67# Slurp up the master block list - we need this several places
68# While we're at it, initialize the related hashes.
69#$sth = $ip_dbh->prepare("select * from masterblocks order by cidr");
70#$sth->execute;
71#for (my $i=0; my @data = $sth->fetchrow_array(); $i++) {
72# $masterblocks[$i] = new NetAddr::IP $data[0];
73# $allocated{"$masterblocks[$i]"} = 0;
74# $free{"$masterblocks[$i]"} = 0;
75# $bigfree{"$masterblocks[$i]"} = 128; # Larger number means smaller block.
76# # Set to 128 to prepare for IPv6
77# $routed{"$masterblocks[$i]"} = 0;
78#}
79
80
81
82
83#main()
84
85if(!defined($webvar{action})) {
86 $webvar{action} = "<NULL>"; #shuts up the warnings.
87}
88
89if($webvar{action} eq 'index') {
90 showSummary();
91} elsif ($webvar{action} eq 'newmaster') {
92 printHeader('');
93
94 my $cidr = new NetAddr::IP $webvar{cidr};
95
96 print "<div type=heading align=center>Adding $cidr as master block....</div>\n";
97
98 # Allow transactions, and raise an exception on errors so we can catch it later.
99 # Use local to make sure these get "reset" properly on exiting this block
100 local $ip_dbh->{AutoCommit} = 0;
101 local $ip_dbh->{RaiseError} = 1;
102
103 # Wrap the SQL in a transaction
104 eval {
105 $sth = $ip_dbh->prepare("insert into masterblocks values ('$webvar{cidr}')");
106 $sth->execute;
107
108# Unrouted blocks aren't associated with a city (yet). We don't rely on this
109# elsewhere though; legacy data may have traps and pitfalls in it to break this.
110# Thus the "routed" flag.
111
112 $sth = $ip_dbh->prepare("insert into freeblocks values ('$webvar{cidr}',".
113 $cidr->masklen.",'<NULL>','n')");
114 $sth->execute;
115
116 # If we get here, everything is happy. Commit changes.
117 $ip_dbh->commit;
118 }; # end eval
119
120 if ($@) {
121 carp "Transaction aborted because $@";
122 eval { $ip_dbh->rollback; };
123 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$@'";
124 printError("Could not add master block $webvar{cidr} to database: $@");
125 } else {
126 print "<div type=heading align=center>Success!</div>\n";
127 syslog "info", "$authuser added master block $webvar{cidr}";
128 }
129
130} # end add new master
131
132elsif($webvar{action} eq 'showmaster') {
133 showMaster();
134}
135elsif($webvar{action} eq 'showrouted') {
136 showRBlock();
137}
138elsif($webvar{action} eq 'listpool') {
139 listPool();
140}
141elsif($webvar{action} eq 'search') {
142 printHeader('');
143 if (!$webvar{input}) {
144 # No search term. Display everything.
145 viewBy('all', '');
146 } else {
147 # Search term entered. Display matches.
148 # We should really sanitize $webvar{input}, no?
149 viewBy($webvar{searchfor}, $webvar{input});
150 }
151}
152
153# Not modified or added; just shuffled
154elsif($webvar{action} eq 'assign') {
155 assignBlock();
156}
157elsif($webvar{action} eq 'confirm') {
158 confirmAssign();
159}
160elsif($webvar{action} eq 'insert') {
161 insertAssign();
162}
163elsif($webvar{action} eq 'edit') {
164 edit();
165}
166elsif($webvar{action} eq 'update') {
167 update();
168}
169elsif($webvar{action} eq 'delete') {
170 remove();
171}
172elsif($webvar{action} eq 'finaldelete') {
173 finalDelete();
174}
175
176# Default is an error. It shouldn't be possible to easily get here.
177# The only way I can think of offhand is to just call main.cgi bare-
178# which is not in any way guaranteed to provide anything useful.
179else {
180 printHeader('');
181 my $rnd = rand 500;
182 my $boing = sprintf("%.2f", rand 500);
183 my @excuses = ("Aether cloudy. Ask again later.","The gods are unhappy with your sacrifice.",
184 "Because one of it's legs are both the same", "*wibble*",
185 "Hey! Stop pushing my buttons!", "I ain't done nuttin'", "9",
186 "8", "9", "10", "11", "12", "13", "14", "15", "16", "17");
187 printAndExit("Error $boing: ".$excuses[$rnd/30.0]);
188}
189## Finally! Done with that NASTY "case" emulation!
190
191
192
193# Clean up IPDB globals, DB handle, etc.
194finish($ip_dbh);
195# We print the footer here, so we don't have to do it elsewhere.
196printFooter;
197# Just in case something waaaayyy down isn't in place
198# properly... we exit explicitly.
199exit;
200
201
202
203sub viewBy($$) {
204 my ($category,$query) = @_;
205
206 # Local variables
207 my $sql;
208
209#print "<pre>\n";
210
211#print "start querysub: query '$query'\n";
212# this may happen with more than one subcategory. Unlikely, but possible.
213
214 # Calculate start point for LIMIT clause
215 my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
216
217# Possible cases:
218# 1) Partial IP/subnet. Treated as "first-three-octets-match" in old IPDB,
219# I should be able to handle it similarly here.
220# 2a) CIDR subnet. Treated more or less as such in old IPDB.
221# 2b) CIDR netmask. Not sure how it's treated.
222# 3) Customer ID. Not handled in old IPDB
223# 4) Description.
224# 5) Invalid data which might be interpretable as an IP or something, but
225# which probably shouldn't be for reasons of sanity.
226
227 if ($category eq 'all') {
228
229 print qq(<div class="heading">Showing all netblock and static-IP allocations</div><br>\n);
230 $sql = "select * from searchme";
231 my $count = countRows("select count(*) from ($sql) foo");
232 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
233 queryResults($sql, $webvar{page}, $count);
234
235 } elsif ($category eq 'cust') {
236
237 print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
238
239 # Query for a customer ID. Note that we can't restrict to "numeric-only"
240 # as we have non-numeric custIDs in the legacy data. :/
241 $sql = "select * from searchme where custid ilike '%$query%'";
242 my $count = countRows("select count(*) from ($sql) foo");
243 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
244 queryResults($sql, $webvar{page}, $count);
245
246 } elsif ($category eq 'desc') {
247
248 print qq(<div class="heading">Searching for descriptions containing '$query'</div><br>\n);
249 # Query based on description (includes "name" from old DB).
250 $sql = "select * from searchme where description ilike '%$query%'";
251 my $count = countRows("select count(*) from ($sql) foo");
252 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
253 queryResults($sql, $webvar{page}, $count);
254
255 } elsif ($category =~ /ipblock/) {
256
257 # Query is for a partial IP, a CIDR block in some form, or a flat IP.
258 print qq(<div class="heading">Searching for IP-based matches on '$query'</div><br>\n);
259
260 $query =~ s/\s+//g;
261 if ($query =~ /\//) {
262 # 209.91.179/26 should show all /26 subnets in 209.91.179
263 my ($net,$maskbits) = split /\//, $query;
264 if ($query =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
265 # /0->/9 are silly to worry about right now. I don't think
266 # we'll be getting a class A anytime soon. <g>
267 $sql = "select * from searchme where cidr='$query'";
268 queryResults($sql, $webvar{page}, 1);
269 } else {
270 print "Finding all blocks with netmask /$maskbits, leading octet(s) $net<br>\n";
271 # Partial match; beginning of subnet and maskbits are provided
272 $sql = "select * from searchme where text(cidr) like '$net%' and ".
273 "text(cidr) like '%$maskbits'";
274 my $count = countRows("select count(*) from ($sql) foo");
275 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
276 queryResults($sql, $webvar{page}, $count);
277 }
278 } elsif ($query =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
279 # Specific IP address match
280 print "4-octet pattern found; finding netblock containing IP $query<br>\n";
281 my ($net,$ip) = ($query =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/);
282 my $sfor = new NetAddr::IP $query;
283 $sth = $ip_dbh->prepare("select * from searchme where text(cidr) like '$net%'");
284 $sth->execute;
285 while (my @data = $sth->fetchrow_array()) {
286 my $cidr = new NetAddr::IP $data[0];
287 if ($cidr->contains($sfor)) {
288 queryResults("select * from searchme where cidr='$cidr'", $webvar{page}, 1);
289 }
290 }
291 } elsif ($query =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.?$/) {
292 print "Finding matches where the first three octets are $query<br>\n";
293 $sql = "select * from searchme where text(cidr) like '$query%'";
294 my $count = countRows("select count(*) from ($sql) foo");
295 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
296 queryResults($sql, $webvar{page}, $count);
297 } else {
298 # This shouldn't happen, but if it does, whoever gets it deserves what they get...
299 printError("Invalid query.");
300 }
301 } else {
302 # This shouldn't happen, but if it does, whoever gets it deserves what they get...
303 printError("Invalid searchfor.");
304 }
305} # viewBy
306
307
308# args are: a reference to an array with the row to be printed and the
309# class(stylesheet) to use for formatting.
310# if ommitting the class - call the sub as &printRow(\@array)
311sub printRow {
312 my ($rowRef,$class) = @_;
313
314 if (!$class) {
315 print "<tr>\n";
316 } else {
317 print "<tr class=\"$class\">\n";
318 }
319
320ELEMENT: foreach my $element (@$rowRef) {
321 if (!defined($element)) {
322 print "<td></td>\n";
323 next ELEMENT;
324 }
325 $element =~ s|\n|</br>|g;
326 print "<td>$element</td>\n";
327 }
328 print "</tr>";
329} # printRow
330
331
332# Display certain types of search query. Note that this can't be
333# cleanly reused much of anywhere else as the data isn't neatly tabulated.
334# This is tied to the search sub tightly enough I may just gut it and provide
335# more appropriate tables directly as needed.
336sub queryResults($$$) {
337 my ($sql, $pageNo, $rowCount) = @_;
338 my $offset = 0;
339 $offset = $1 if($sql =~ m/.*limit\s+(.*),.*/);
340
341 my $sth = $ip_dbh->prepare($sql);
342 $sth->execute();
343
344 startTable('Allocation','CustID','Type','City','Description/Name');
345 my $count = 0;
346
347 while (my @data = $sth->fetchrow_array) {
348 # cidr,custid,type,city,description,notes
349 # Fix up types from pools (which are single-char)
350 # Fixing the database would be... painful. :(
351 if ($data[2] =~ /^[cdsmw]$/) {
352 $data[2] .= 'i';
353 }
354 my @row = (qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
355 $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]);
356 # Allow listing of pool if desired/required.
357 if ($data[2] =~ /^[cdsmw]p$/) {
358 $row[0] .= ' &nbsp; <a href="/ip/cgi-bin/main.cgi?action=listpool'.
359 "&pool=$data[0]\">List IPs</a>";
360 }
361 printRow(\@row, 'color1', 1) if ($count%2==0);
362 printRow(\@row, 'color2', 1) if ($count%2!=0);
363 $count++;
364 }
365
366 # Have to think on this call, it's primarily to clean up unfetched rows from a select.
367 # In this context it's probably a good idea.
368 $sth->finish();
369
370 my $upper = $offset+$count;
371 print "<tr><td colspan=10 bgcolor=white class=regular>Records found: $rowCount<br><i>Displaying: $offset - $upper</i></td></tr>\n";
372 print "</table></center>\n";
373
374 # print the page thing..
375 if ($rowCount > $RESULTS_PER_PAGE) {
376 my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
377 print qq(<div class="center"> Page: );
378 for (my $i = 1; $i <= $pages; $i++) {
379 if ($i == $pageNo) {
380 print "<b>$i&nbsp;</b>\n";
381 } else {
382 print qq(<a href="/ip/cgi-bin/main.cgi?page=$i&input=$webvar{input}&action=search&searchfor=$webvar{searchfor}">$i</a>&nbsp;\n);
383 }
384 }
385 print "</div>";
386 }
387} # queryResults
388
389
390# Prints table headings. Accepts any number of arguments;
391# each argument is a table heading.
392sub startTable {
393 print qq(<center><table width="98%" cellspacing="0" class="center"><tr>);
394
395 foreach(@_) {
396 print qq(<td class="heading">$_</td>);
397 }
398 print "</tr>\n";
399} # startTable
400
401
402# Return first element of passed SQL query
403sub countRows($) {
404 my $sth = $ip_dbh->prepare($_[0]);
405 $sth->execute();
406 my @a = $sth->fetchrow_array();
407 $sth->finish();
408 return $a[0];
409}
410
411
412# Initial display: Show master blocks with total allocated subnets, total free subnets
413sub showSummary {
414 # this is horrible-ugly-bad and will Go Away real soon now(TM)
415 print "Content-type: text/html\n\n";
416
417 startTable('Master netblock', 'Routed netblocks', 'Allocated netblocks',
418 'Free netblocks', 'Largest free block');
419
420 my %allocated;
421 my %free;
422 my %routed;
423 my %bigfree;
424
425 # Count the allocations.
426 $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?");
427 foreach my $master (@masterblocks) {
428 $sth->execute("$master");
429 $sth->bind_columns(\$allocated{"$master"});
430 $sth->fetch();
431 }
432
433 # Count routed blocks
434 $sth = $ip_dbh->prepare("select count(*) from routed where cidr <<= ?");
435 foreach my $master (@masterblocks) {
436 $sth->execute("$master");
437 $sth->bind_columns(\$routed{"$master"});
438 $sth->fetch();
439 }
440
441 # Count the free blocks.
442 $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ?");
443 foreach my $master (@masterblocks) {
444 $sth->execute("$master");
445 $sth->bind_columns(\$free{"$master"});
446 $sth->fetch();
447 }
448
449 # Find the largest free block in each master
450 $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? order by maskbits limit 1");
451 foreach my $master (@masterblocks) {
452 $sth->execute("$master");
453 $sth->bind_columns(\$bigfree{"$master"});
454 $sth->fetch();
455 }
456
457 # Print the data.
458 my $count=0;
459 foreach my $master (@masterblocks) {
460 my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=showmaster&block=$master\">$master</a>",
461 $routed{"$master"}, $allocated{"$master"}, $free{"$master"},
462 ( ($bigfree{"$master"} eq 128) ? ("&lt;NONE&gt;") : ("/".$bigfree{"$master"}) )
463 );
464
465 printRow(\@row, 'color1' ) if($count%2==0);
466 printRow(\@row, 'color2' ) if($count%2!=0);
467 $count++;
468 }
469 print "</table>\n";
470 print qq(<a href="/ip/addmaster.shtml">Add new master block</a><br><br>\n);
471 print "Note: Free blocks noted here include both routed and unrouted blocks.\n";
472
473} # showSummary
474
475
476# Display detail on master
477# Alrighty then! We're showing routed blocks within a single master this time.
478# We should be able to steal code from showSummary(), and if I'm really smart
479# I'll figger a way to munge the two together. (Once I've done that, everything
480# else should follow. YMMV.)
481sub showMaster {
482 printHeader('');
483
484 print qq(<center><div class="heading">Summarizing routed blocks for ).
485 qq($webvar{block}:</div></center><br>\n);
486
487 my %allocated;
488 my %free;
489 my %routed;
490 my %bigfree;
491
492 my $master = new NetAddr::IP $webvar{block};
493 my @localmasters;
494
495 # Fetch only the blocks relevant to this master
496 $sth = $ip_dbh->prepare("select * from routed where cidr <<= '$master' order by cidr");
497 $sth->execute();
498
499 my $i=0;
500 while (my @data = $sth->fetchrow_array()) {
501 my $cidr = new NetAddr::IP $data[0];
502 $localmasters[$i++] = $cidr;
503 $free{"$cidr"} = 0;
504 $allocated{"$cidr"} = 0;
505 $bigfree{"$cidr"} = 128;
506 # Retain the routing destination
507 $routed{"$cidr"} = $data[2];
508 }
509
510 # Check if there were actually any blocks routed from this master
511 if ($i > 0) {
512 startTable('Routed block','Routed to','Allocated blocks',
513 'Free blocks','Largest free block');
514
515 # Count the allocations
516 $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?");
517 foreach my $master (@localmasters) {
518 $sth->execute("$master");
519 $sth->bind_columns(\$allocated{"$master"});
520 $sth->fetch();
521 }
522
523 # Count the free blocks.
524 $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ?");
525 foreach my $master (@localmasters) {
526 $sth->execute("$master");
527 $sth->bind_columns(\$free{"$master"});
528 $sth->fetch();
529 }
530
531 # Get the size of the largest free block
532 $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? order by maskbits limit 1");
533 foreach my $master (@localmasters) {
534 $sth->execute("$master");
535 $sth->bind_columns(\$bigfree{"$master"});
536 $sth->fetch();
537 }
538
539 # Print the data.
540 my $count=0;
541 foreach my $master (@localmasters) {
542 my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=showrouted&block=$master\">$master</a>",
543 $routed{"$master"}, $allocated{"$master"},
544 $free{"$master"},
545 ( ($bigfree{"$master"} eq 128) ? ("&lt;NONE&gt;") : ("/".$bigfree{"$master"}) )
546 );
547 printRow(\@row, 'color1' ) if($count%2==0);
548 printRow(\@row, 'color2' ) if($count%2!=0);
549 $count++;
550 }
551 } else {
552 # If a master block has no routed blocks, then by definition it has no
553 # allocations, and can be deleted.
554 print qq(<hr width="60%"><center><div class="heading">No allocations in ).
555 qq($master.</div>\n).
556 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n).
557 qq(<input type=hidden name=action value="delete">\n).
558 qq(<input type=hidden name=block value="$master">\n).
559 qq(<input type=hidden name=alloctype value="mm">\n).
560 qq(<input type=submit value=" Remove this master ">\n).
561 qq(</form></center>\n);
562
563 } # end check for existence of routed blocks in master
564
565 print qq(</table>\n<hr width="60%">\n).
566 qq(<center><div class="heading">Unrouted blocks in $master:</div></center><br>\n);
567
568 startTable('Netblock','Range');
569
570 # Snag the free blocks.
571 my $count = 0;
572 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr <<='$master' and ".
573 "routed='n' order by cidr");
574 $sth->execute();
575 while (my @data = $sth->fetchrow_array()) {
576 my $cidr = new NetAddr::IP $data[0];
577 my @row = ("$cidr", $cidr->range);
578 printRow(\@row, 'color1' ) if($count%2==0);
579 printRow(\@row, 'color2' ) if($count%2!=0);
580 $count++;
581 }
582
583 print "</table>\n";
584} # showMaster
585
586
587# Display details of a routed block
588# Alrighty then! We're showing allocations within a routed block this time.
589# We should be able to steal code from showSummary() and showMaster(), and if
590# I'm really smart I'll figger a way to munge all three together. (Once I've
591# done that, everything else should follow. YMMV.
592# This time, we check the database before spewing, because we may
593# not have anything useful to spew.
594sub showRBlock {
595 printHeader('');
596
597 my $master = new NetAddr::IP $webvar{block};
598
599 $sth = $ip_dbh->prepare("select * from routed where cidr='$master'");
600 $sth->execute;
601 my @data = $sth->fetchrow_array;
602
603 print qq(<center><div class="heading">Summarizing allocated blocks for ).
604 qq($master ($data[2]):</div></center><br>\n);
605
606 startTable('CIDR allocation','Customer Location','Type','CustID','Description/Name');
607
608 # Snag the allocations for this block
609 $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$master' order by cidr");
610 $sth->execute();
611
612 my $count=0;
613 while (my @data = $sth->fetchrow_array()) {
614 # cidr,custid,type,city,description,notes,maskbits,circuitid
615 my $cidr = new NetAddr::IP $data[0];
616
617 # Clean up extra spaces that are borking things.
618 $data[2] =~ s/\s+//g;
619
620 my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=edit&block=$data[0]\">$data[0]</a>",
621 $data[3], $disp_alloctypes{$data[2]}, $data[1], $data[4]);
622 # If the allocation is a pool, allow listing of the IPs in the pool.
623 if ($data[2] =~ /^[cdsmw]p$/) {
624 $row[0] .= ' &nbsp; <a href="/ip/cgi-bin/main.cgi?action=listpool'.
625 "&pool=$data[0]\">List IPs</a>";
626 }
627
628 printRow(\@row, 'color1') if ($count%2 == 0);
629 printRow(\@row, 'color2') if ($count%2 != 0);
630 $count++;
631 }
632
633 print "</table>\n";
634
635 # If the routed block has no allocations, by definition it only has
636 # one free block, and therefore may be deleted.
637 if ($count == 0) {
638 print qq(<hr width="60%"><center><div class="heading">No allocations in ).
639 qq($master.</div></center>\n).
640 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n).
641 qq(<input type=hidden name=action value="delete">\n).
642 qq(<input type=hidden name=block value="$master">\n).
643 qq(<input type=hidden name=alloctype value="rr">\n).
644 qq(<input type=submit value=" Remove this block ">\n).
645 qq(</form>\n);
646 }
647
648 print qq(<hr width="60%">\n<center><div class="heading">Free blocks within routed ).
649 qq(submaster $master</div></center>\n);
650
651 startTable('CIDR block','Range');
652
653 # Snag the free blocks. We don't really *need* to be pedantic about avoiding
654 # unrouted free blocks, but it's better to let the database do the work if we can.
655 $count = 0;
656 $sth = $ip_dbh->prepare("select * from freeblocks where routed='y' and cidr <<= '$master' order by cidr");
657 $sth->execute();
658 while (my @data = $sth->fetchrow_array()) {
659 # cidr,maskbits,city
660 my $cidr = new NetAddr::IP $data[0];
661 my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=assign&block=$cidr\">$cidr</a>",
662 $cidr->range);
663 printRow(\@row, 'color1') if ($count%2 == 0);
664 printRow(\@row, 'color2') if ($count%2 != 0);
665 $count++;
666 }
667
668 print "</table>\n";
669} # showRBlock
670
671
672# List the IPs used in a pool
673sub listPool {
674 printHeader('');
675
676 my $cidr = new NetAddr::IP $webvar{pool};
677
678 # Snag pool info for heading
679 $sth = $ip_dbh->prepare("select * from allocations where cidr='$cidr'");
680 $sth->execute;
681 my @data = $sth->fetchrow_array;
682 my $type = $data[2]; # We'll need this later.
683
684 print qq(<center><div class="heading">Listing pool IPs for $cidr<br>\n).
685 qq(($disp_alloctypes{$type} in $data[3])</div></center><br>\n);
686 print qq(<div class="indent"><b>Reserved IPs:</b><br>\n);
687 print qq(<div class="indent"><table><tr class=color1><td>Network IP:</td><td>).
688 $cidr->addr."</td></tr>\n";
689 $cidr++;
690 print "<tr class=color2><td>Gateway:</td><td>".$cidr->addr."</td></tr>\n";
691 $cidr--; $cidr--;
692 print "<tr class=color1><td>Broadcast:</td><td>".$cidr->addr."</td></tr>\n".
693 "<tr><td>Netmask:</td><td>".$cidr->mask."</td></tr>\n".
694 "</table></div></div>\n";
695
696# probably have to add an "edit IP allocation" link here somewhere.
697
698 startTable('IP','Customer ID','Available?','Description','');
699 $sth = $ip_dbh->prepare("select * from poolips where pool='$webvar{pool}' order by ip");
700 $sth->execute;
701 my $count = 0;
702 while (my @data = $sth->fetchrow_array) {
703 # pool,ip,custid,city,ptype,available,notes,description,circuitid
704 # If desc is null, make it not null. <g>
705 if ($data[7] eq '') {
706 $data[7] = '&nbsp;';
707 }
708 # Some nice hairy Perl to decide whether to allow unassigning each IP
709 # -> if $data[5] (aka poolips.available) == 'n' then we print the unassign link
710 # else we print a blank space
711 my @row = ( qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[1]">$data[1]</a>),
712 $data[2],$data[5],$data[7],
713 ( ($data[5] eq 'n') ?
714 ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[1]&".
715 "alloctype=$data[4]i\">Unassign this IP</a>") :
716 ("&nbsp;") )
717 );
718 printRow(\@row, 'color1') if($count%2==0);
719 printRow(\@row, 'color2') if($count%2!=0);
720 $count++;
721 }
722 print "</table>\n";
723
724} # end listPool
725
726
727# Show "Add new allocation" page. Note that the actual page may
728# be one of two templates, and the lists come from the database.
729sub assignBlock {
730 printHeader('');
731
732 my $html;
733
734 # New special case- block to assign is specified
735 if ($webvar{block} ne '') {
736 open HTML, "../fb-assign.html"
737 or croak "Could not open fb-assign.html: $!";
738 $html = join('',<HTML>);
739 close HTML;
740 my $block = new NetAddr::IP $webvar{block};
741 $html =~ s|\$\$BLOCK\$\$|$block|g;
742 $html =~ s|\$\$MASKBITS\$\$|$block->masklen|;
743 my $typelist = '';
744 $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 500 and type not like '_i' order by listorder");
745 $sth->execute;
746 my @data = $sth->fetchrow_array;
747 $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
748 while (my @data = $sth->fetchrow_array) {
749 $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
750 }
751 $html =~ s|\$\$TYPELIST\$\$|$typelist|g;
752 } else {
753 open HTML, "../assign.html"
754 or croak "Could not open assign.html: $!";
755 $html = join('',<HTML>);
756 close HTML;
757 my $masterlist = "<select name=allocfrom><option selected>-</option>\n";
758 foreach my $master (@masterblocks) {
759 $masterlist .= "<option>$master</option>\n";
760 }
761 $masterlist .= "</select>\n";
762 $html =~ s|\$\$MASTERLIST\$\$|$masterlist|g;
763 my $pops = '';
764 foreach my $pop (@poplist) {
765 $pops .= "<option>$pop</option>\n";
766 }
767 $html =~ s|\$\$POPLIST\$\$|$pops|g;
768 my $typelist = '';
769 $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder");
770 $sth->execute;
771 my @data = $sth->fetchrow_array;
772 $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
773 while (my @data = $sth->fetchrow_array) {
774 $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
775 }
776 $html =~ s|\$\$TYPELIST\$\$|$typelist|g;
777 }
778 my $cities = '';
779 foreach my $city (@citylist) {
780 $cities .= "<option>$city</option>\n";
781 }
782 $html =~ s|\$\$ALLCITIES\$\$|$cities|g;
783
784 print $html;
785
786} # assignBlock
787
788
789# Take info on requested IP assignment and see what we can provide.
790sub confirmAssign {
791 printHeader('');
792
793 my $cidr;
794 my $alloc_from;
795
796 # Going to manually validate some items.
797 # custid and city are automagic.
798 return if !validateInput();
799
800# Several different cases here.
801# Static IP vs netblock
802# + Different flavours of static IP
803# + Different flavours of netblock
804
805 if ($webvar{alloctype} =~ /^[cdsmw]i$/) {
806 my ($base,undef) = split //, $webvar{alloctype}; # split into individual chars
807 my $sql;
808 # Check for pools in Subury or North Bay if DSL or server pool. Anywhere else is
809 # invalid and shouldn't be in the db in the first place.
810 # ... aside from #^%#$%#@#^%^^!!!! legacy data. GRRR.
811 # Note that we want to retain the requested city to relate to customer info.
812 if ($base =~ /^[ds]$/) {
813 $sql = "select * from poolips where available='y' and".
814 " ptype='$base' and (city='Sudbury' or city='North Bay')";
815 } else {
816 $sql = "select * from poolips where available='y' and".
817 " ptype='$base' and city='$webvar{pop}'";
818 }
819
820 # Now that we know where we're looking, we can list the pools with free IPs.
821 $sth = $ip_dbh->prepare($sql);
822 $sth->execute;
823 my %ipcount;
824 my $optionlist;
825 while (my @data = $sth->fetchrow_array) {
826 $ipcount{$data[0]}++;
827 }
828 $sth = $ip_dbh->prepare("select city from allocations where cidr=?");
829 foreach my $key (keys %ipcount) {
830 $sth->execute($key);
831 my @data = $sth->fetchrow_array;
832 $optionlist .= "<option value='$key'>$key [$ipcount{$key} free IP(s)] in $data[0]</option>\n";
833 }
834 $cidr = "Single static IP";
835 $alloc_from = "<select name=alloc_from>".$optionlist."</select>\n";
836
837 } else { # end show pool options
838
839 if ($webvar{fbassign} eq 'y') {
840 $cidr = new NetAddr::IP $webvar{block};
841 $webvar{maskbits} = $cidr->masklen;
842 } else { # done with direct freeblocks assignment
843
844 if (!$webvar{maskbits}) {
845 printError("Please specify a CIDR mask length.");
846 return;
847 }
848 my $sql;
849 my $city;
850 my $failmsg;
851 if ($webvar{alloctype} eq 'rr') {
852 if ($webvar{allocfrom} ne '-') {
853 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
854 " and cidr <<= '$webvar{allocfrom}' order by maskbits desc";
855 } else {
856 $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
857 " order by maskbits desc";
858 }
859 $failmsg = "No suitable free block found.<br>\nWe do not have a free".
860 " routeable block of that size.<br>\nYou will have to either route".
861 " a set of smaller netblocks or a single smaller netblock.";
862 } else {
863##fixme
864# This section needs serious Pondering.
865 if ($webvar{alloctype} =~ /^[cdsmw]p$/) {
866 if (($webvar{city} !~ /^(Sudbury|North Bay)$/) && ($webvar{alloctype} eq 'dp')) {
867 printError("You must chose Sudbury or North Bay for DSL pools.");
868 return;
869 }
870 $city = $webvar{city};
871 $failmsg = "No suitable free block found.<br>\nYou will have to route another".
872 " superblock from one of the<br>\nmaster blocks in Sudbury or chose a smaller".
873 " block size for the pool.";
874 } else {
875 $city = $webvar{pop};
876 $failmsg = "No suitable free block found.<br>\nYou will have to route another".
877 " superblock to $webvar{pop}<br>\nfrom one of the master blocks in Sudbury or".
878 " chose a smaller blocksize.";
879 }
880 if ($webvar{allocfrom} ne '-') {
881 $sql = "select * from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
882 " and cidr <<= '$webvar{allocfrom}' and routed='y' order by cidr,maskbits desc";
883 } else {
884 $sql = "select * from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
885 " and routed='y' order by cidr,maskbits desc";
886 }
887 }
888 $sth = $ip_dbh->prepare($sql);
889 $sth->execute;
890 my @data = $sth->fetchrow_array();
891 if ($data[0] eq "") {
892 printError($failmsg);
893 return;
894 }
895 $cidr = new NetAddr::IP $data[0];
896 } # check for freeblocks assignment or IPDB-controlled assignment
897
898 $alloc_from = qq($cidr<input type=hidden name=alloc_from value="$cidr">);
899
900 # If the block to be allocated is smaller than the one we found,
901 # figure out the "real" block to be allocated.
902 if ($cidr->masklen() ne $webvar{maskbits}) {
903 my $maskbits = $cidr->masklen();
904 my @subblocks;
905 while ($maskbits++ < $webvar{maskbits}) {
906 @subblocks = $cidr->split($maskbits);
907 }
908 $cidr = $subblocks[0];
909 }
910 } # if ($webvar{alloctype} =~ /^[cdsmw]i$/)
911
912 open HTML, "../confirm.html"
913 or croak "Could not open confirm.html: $!";
914 my $html = join '', <HTML>;
915 close HTML;
916
917### gotta fix this in final
918 # Stick in customer info as necessary - if it's blank, it just ends
919 # up as blank lines ignored in the rendering of the page
920 my $custbits;
921 $html =~ s|\$\$CUSTBITS\$\$|$custbits|g;
922###
923
924 # Stick in the allocation data
925 $html =~ s|\$\$ALLOC_TYPE\$\$|$webvar{alloctype}|g;
926 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$webvar{alloctype}}|g;
927 $html =~ s|\$\$ALLOC_FROM\$\$|$alloc_from|g;
928 $html =~ s|\$\$CIDR\$\$|$cidr|g;
929 $webvar{city} = desanitize($webvar{city});
930 $html =~ s|\$\$CITY\$\$|$webvar{city}|g;
931 $html =~ s|\$\$CUSTID\$\$|$webvar{custid}|g;
932 $webvar{circid} = desanitize($webvar{circid});
933 $html =~ s|\$\$CIRCID\$\$|$webvar{circid}|g;
934 $webvar{desc} = desanitize($webvar{desc});
935 $html =~ s|\$\$DESC\$\$|$webvar{desc}|g;
936 $webvar{notes} = desanitize($webvar{notes});
937 $html =~ s|\$\$NOTES\$\$|$webvar{notes}|g;
938 $html =~ s|\$\$ACTION\$\$|insert|g;
939
940 print $html;
941
942} # end confirmAssign
943
944
945# Do the work of actually inserting a block in the database.
946sub insertAssign {
947 # Some things are done more than once.
948 printHeader('');
949 return if !validateInput();
950
951 # $code is "success" vs "failure", $msg contains OK for a
952 # successful netblock allocation, the IP allocated for static
953 # IP, or the error message if an error occurred.
954 my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
955 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
956 $webvar{circid});
957
958 if ($code eq 'OK') {
959 if ($webvar{alloctype} =~ /^.i$/) {
960 print qq(<div class="center"><div class="heading">The IP $msg has been allocated to customer $webvar{custid}</div></div>);
961 # Notify tech@example.com
962 mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation",
963 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
964 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
965 } else {
966 print qq(<div class="center"><div class="heading">The block $webvar{fullcidr} was ).
967 "sucessfully added as type '$webvar{alloctype}' ".
968 "($disp_alloctypes{$webvar{alloctype}})</div></div>";
969 }
970 syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
971 "'$webvar{alloctype}'";
972 } else {
973 syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
974 "'$webvar{alloctype}' by $authuser failed: '$msg'";
975 printError("Allocation of $webvar{fullcidr} as $disp_alloctypes{$webvar{alloctype}}".
976 " failed: $msg\n");
977 }
978
979} # end insertAssign()
980
981
982# Does some basic checks on common input data to make sure nothing
983# *really* weird gets in to the database through this script.
984# Does NOT do complete input validation!!!
985sub validateInput {
986 if ($webvar{city} eq '-') {
987 printError("Please choose a city.");
988 return;
989 }
990
991 # Alloctype check.
992 chomp $webvar{alloctype};
993 if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
994 # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
995 # managing to call things in such a way as to cause this deserves a cryptic error.
996 printError("Invalid alloctype");
997 return;
998 }
999
1000 # CustID check
1001 # We have different handling for customer allocations and "internal" or "our" allocations
1002 if ($webvar{alloctype} =~ /^(cn|.i)$/) {
1003 if (!$webvar{custid}) {
1004 printError("Please enter a customer ID.");
1005 return;
1006 }
1007 if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
1008 # Force uppercase for now...
1009 $webvar{custid} =~ tr/a-z/A-Z/;
1010 # Crosscheck with ... er... something.
1011 my $status = CustIDCK->custid_exist($webvar{custid});
1012 if ($CustIDCK::Error) {
1013 printError("Error verifying customer ID: ".$CustIDCK::ErrMsg);
1014 return;
1015 }
1016 if (!$status) {
1017 printError("Customer ID not valid. Make sure the Customer ID ".
1018 "is correct.<br>\nUse STAFF for staff static IPs, and 6750400 for any other ".
1019 "non-customer assignments.");
1020 return;
1021 }
1022#"Please enter a valid customer ID- this must be a 7- or 10-digit number, or STAFF for
1023#static IPs for staff.");
1024 }
1025# print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
1026 } else {
1027 # All non-customer allocations MUST be entered with "our" customer ID.
1028 # I have Defined this as 6750400 for consistency.
1029 # STAFF is also acceptable.
1030 if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
1031 $webvar{custid} = "6750400";
1032 }
1033 }
1034
1035 # Check POP location
1036 my $flag;
1037 if ($webvar{alloctype} eq 'rr') {
1038 $flag = 'for a routed netblock';
1039 foreach (@poplist) {
1040 if (/^$webvar{city}$/) {
1041 $flag = 'n';
1042 last;
1043 }
1044 }
1045 } else {
1046 $flag = 'n';
1047 if ($webvar{pop} =~ /^-$/) {
1048 $flag = 'to route the block from/through';
1049 }
1050 }
1051 if ($flag ne 'n') {
1052 printError("Please choose a valid POP location $flag. Valid ".
1053 "POP locations are currently:<br>\n".join (" - ", @poplist));
1054 return;
1055 }
1056
1057 return 'OK';
1058} # end validateInput
1059
1060
1061# Displays details of a specific allocation in a form
1062# Allows update/delete
1063# action=edit
1064sub edit {
1065 printHeader('');
1066
1067 my $sql;
1068
1069 # Two cases: block is a netblock, or block is a static IP from a pool
1070 # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
1071 if ($webvar{block} =~ /\/32$/) {
1072 $sql = "select ip,custid,ptype,city,circuitid,description,notes from poolips where ip='$webvar{block}'";
1073 } else {
1074 $sql = "select cidr,custid,type,city,circuitid,description,notes from allocations where cidr='$webvar{block}'"
1075 }
1076
1077 # gotta snag block info from db
1078 $sth = $ip_dbh->prepare($sql);
1079 $sth->execute;
1080 my @data = $sth->fetchrow_array;
1081
1082 # Clean up extra whitespace on alloc type
1083 $data[2] =~ s/\s//;
1084
1085 # Postfix "i" on pool IP types
1086 if ($data[2] =~ /^[cdsmw]$/) {
1087 $data[2] .= "i";
1088 }
1089
1090 open (HTML, "../editDisplay.html")
1091 or croak "Could not open editDisplay.html :$!";
1092 my $html = join('', <HTML>);
1093
1094 # We can't let the city be changed here; this block is a part of
1095 # a larger routed allocation and therefore by definition can't be moved.
1096 # block and city are static.
1097##fixme
1098# Needs thinking. Have to allow changes to city to correct errors, no?
1099 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
1100 $html =~ s/\$\$CITY\$\$/$data[3]/g;
1101
1102# Screw it. Changing allocation types gets very ugly VERY quickly- especially
1103# with the much longer list of allocation types.
1104# We'll just show what type of block it is.
1105
1106# this has now been Requested, so here goes.
1107
1108 if ($data[2] =~ /^d[nyc]|cn|ee|in$/) {
1109 # Block that can be changed
1110 my $blockoptions = "<select name=alloctype><option".
1111 (($data[2] eq 'dn') ? ' selected' : '') ." value='dn'>Dialup netblock</option>\n<option".
1112 (($data[2] eq 'dy') ? ' selected' : '') ." value='dy'>Dynamic DSL netblock</option>\n<option".
1113 (($data[2] eq 'dc') ? ' selected' : '') ." value='dc'>Dynamic cable netblock</option>\n<option".
1114 (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
1115 (($data[2] eq 'ee') ? ' selected' : '') ." value='ee'>End-use netblock</option>\n<option".
1116 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
1117 "</select>\n";
1118 $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g;
1119 } else {
1120 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g;
1121 }
1122
1123 # These can be modified, although CustID changes may get ignored.
1124 $html =~ s/\$\$CUSTID\$\$/$data[1]/g;
1125 $html =~ s/\$\$TYPE\$\$/$data[2]/g;
1126 $html =~ s/\$\$CIRCID\$\$/$data[4]/g;
1127 $html =~ s/\$\$DESC\$\$/$data[5]/g;
1128 $html =~ s/\$\$NOTES\$\$/$data[6]/g;
1129
1130 print $html;
1131
1132} # edit()
1133
1134
1135# Stuff new info about a block into the db
1136# action=update
1137sub update {
1138 printHeader('');
1139
1140 # Make sure incoming data is in correct format - custID among other things.
1141 validateInput;
1142
1143 # SQL transaction wrapper
1144 eval {
1145 # Relatively simple SQL transaction here.
1146 my $sql;
1147 if (my $pooltype = ($webvar{alloctype} =~ /^([cdsmw])i$/) ) {
1148 $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',".
1149 "circuitid='$webvar{circid}',description='$webvar{desc}' ".
1150 "where ip='$webvar{block}'";
1151 } else {
1152 $sql = "update allocations set custid='$webvar{custid}',".
1153 "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',".
1154 "type='$webvar{alloctype}',circuitid='$webvar{circid}' where cidr='$webvar{block}'";
1155 }
1156syslog "debug", $sql;
1157 $sth = $ip_dbh->prepare($sql);
1158 $sth->execute;
1159 $ip_dbh->commit;
1160 };
1161 if ($@) {
1162 carp "Transaction aborted because $@";
1163 eval { $ip_dbh->rollback; };
1164 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$@'";
1165 printError("Could not update block/IP $webvar{block}: $@");
1166 return;
1167 }
1168
1169 # If we get here, the operation succeeded.
1170 syslog "notice", "$authuser updated $webvar{block}";
1171 open (HTML, "../updated.html")
1172 or croak "Could not open updated.html :$!";
1173 my $html = join('', <HTML>);
1174
1175 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
1176 $webvar{city} = desanitize($webvar{city});
1177 $html =~ s/\$\$CITY\$\$/$webvar{city}/g;
1178 $html =~ s/\$\$ALLOCTYPE\$\$/$webvar{alloctype}/g;
1179 $html =~ s/\$\$TYPEFULL\$\$/$disp_alloctypes{$webvar{alloctype}}/g;
1180 $html =~ s/\$\$CUSTID\$\$/$webvar{custid}/g;
1181 $webvar{circid} = desanitize($webvar{circid});
1182 $html =~ s/\$\$CIRCID\$\$/$webvar{circid}/g;
1183 $webvar{desc} = desanitize($webvar{desc});
1184 $html =~ s/\$\$DESC\$\$/$webvar{desc}/g;
1185 $webvar{notes} = desanitize($webvar{notes});
1186 $html =~ s/\$\$NOTES\$\$/$webvar{notes}/g;
1187
1188 print $html;
1189
1190} # update()
1191
1192
1193# Delete an allocation.
1194sub remove {
1195 printHeader('');
1196 #show confirm screen.
1197 open HTML, "../confirmRemove.html"
1198 or croak "Could not open confirmRemove.html :$!";
1199 my $html = join('', <HTML>);
1200 close HTML;
1201
1202 # Serves'em right for getting here...
1203 if (!defined($webvar{block})) {
1204 printError("Error 332");
1205 return;
1206 }
1207
1208 my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype);
1209
1210 if ($webvar{alloctype} eq 'rr') {
1211 $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
1212 $sth->execute();
1213
1214# This feels... extreme.
1215 croak $sth->errstr() if($sth->errstr());
1216
1217 $sth->bind_columns(\$cidr,\$city);
1218 $sth->execute();
1219 $sth->fetch || croak $sth->errstr();
1220 $custid = "N/A";
1221 $alloctype = $webvar{alloctype};
1222 $circid = "N/A";
1223 $desc = "N/A";
1224 $notes = "N/A";
1225
1226 } elsif ($webvar{alloctype} eq 'mm') {
1227 $cidr = $webvar{block};
1228 $city = "N/A";
1229 $custid = "N/A";
1230 $alloctype = $webvar{alloctype};
1231 $circid = "N/A";
1232 $desc = "N/A";
1233 $notes = "N/A";
1234 } elsif ($webvar{alloctype} =~ /^[cdsmw]i$/) { # done with alloctype=rr
1235
1236 # Unassigning a static IP
1237 my $sth = $ip_dbh->prepare("select ip,custid,city,ptype,notes,circuitid from poolips".
1238 " where ip='$webvar{block}'");
1239 $sth->execute();
1240# croak $sth->errstr() if($sth->errstr());
1241
1242 $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid);
1243 $sth->fetch() || croak $sth->errstr;
1244
1245 $alloctype .="i";
1246
1247 } else { # done with alloctype=[cdsmw]i
1248
1249 my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes from ".
1250 "allocations where cidr='$webvar{block}'");
1251 $sth->execute();
1252# croak $sth->errstr() if($sth->errstr());
1253
1254 $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc, \$notes);
1255 $sth->fetch() || carp $sth->errstr;
1256 } # end cases for different alloctypes
1257
1258 # Munge everything into HTML
1259 $html =~ s|Please confirm|Please confirm <b>removal</b> of|;
1260 $html =~ s|\$\$BLOCK\$\$|$cidr|g;
1261 $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$alloctype}|g;
1262 $html =~ s|\$\$ALLOCTYPE\$\$|$alloctype|g;
1263 $html =~ s|\$\$CITY\$\$|$city|g;
1264 $html =~ s|\$\$CUSTID\$\$|$custid|g;
1265 $html =~ s|\$\$CIRCID\$\$|$circid|g;
1266 $html =~ s|\$\$DESC\$\$|$desc|g;
1267 $html =~ s|\$\$NOTES\$\$|$notes|g;
1268
1269 $html =~ s|\$\$ACTION\$\$|finaldelete|g;
1270
1271 # Set the warning text.
1272 if ($alloctype =~ /^[cdsmw]p$/) {
1273 $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>|;
1274 } else {
1275 $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.</div></td></tr>|;
1276 }
1277
1278 print $html;
1279} # end edit()
1280
1281
1282# Delete an allocation. Return it to the freeblocks table; munge
1283# data as necessary to keep as few records as possible in freeblocks
1284# to prevent weirdness when allocating blocks later.
1285# Remove IPs from pool listing if necessary
1286sub finalDelete {
1287 printHeader('');
1288
1289 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
1290
1291 if ($code eq 'OK') {
1292 print "<div class=heading align=center>Success! $webvar{block} deallocated.</div>\n";
1293 syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}";
1294 } else {
1295 if ($webvar{alloctype} =~ /^.i$/) {
1296 syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
1297 printError("Could not deallocate static IP $webvar{block}: $msg");
1298 } else {
1299 syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
1300 printError("Could not deallocate netblock $webvar{block}: $msg");
1301 }
1302 }
1303
1304} # finalDelete
1305
1306
1307# Just in case we manage to get here.
1308exit 0;
Note: See TracBrowser for help on using the repository browser.