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

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

/branches/stable

Fix "change allocation type" glitch due to change of "Internal
netblock" typecode from ii to in

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