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

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

/trunk

Tweaked another few fixes into place to make a "fresh" install
work "correctly".

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