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