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

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

/trunk

Wrap connectDB() function in a "module" so we don't get headaches
tracking password changes (if and when). This change *may* get
merged back to IPDB.pm, but it may not so as to allow the HTTP
username to be the user authenticated at the database level.

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