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

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

/trunk

Clean up some error handling code relating to DB transactions

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