source: branches/sql-cleanup/cgi-bin/main.cgi@ 154

Last change on this file since 154 was 154, checked in by Kris Deugau, 19 years ago

/branches/sql-cleanup

main.cgi:

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