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 |
|
---|
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 POSIX qw(ceil);
|
---|
18 | use NetAddr::IP;
|
---|
19 |
|
---|
20 | use Sys::Syslog;
|
---|
21 |
|
---|
22 | openlog "IPDB","pid","local2";
|
---|
23 |
|
---|
24 | # Collect the username from HTTP auth. If undefined, we're in a test environment.
|
---|
25 | my $authuser;
|
---|
26 | if (!defined($ENV{'REMOTE_USER'})) {
|
---|
27 | $authuser = '__temptest';
|
---|
28 | } else {
|
---|
29 | $authuser = $ENV{'REMOTE_USER'};
|
---|
30 | }
|
---|
31 |
|
---|
32 | syslog "debug", "$authuser active";
|
---|
33 |
|
---|
34 | checkDBSanity();
|
---|
35 |
|
---|
36 | #prototypes
|
---|
37 | sub viewBy($$); # feed it the category and query
|
---|
38 | sub queryResults($$$); # args is the sql, the page# and the rowCount
|
---|
39 | # Needs rewrite/rename
|
---|
40 | sub countRows($); # returns first element of first row of passed SQL
|
---|
41 | # Only usage passes "select count(*) ..."
|
---|
42 |
|
---|
43 | my $RESULTS_PER_PAGE = 50;
|
---|
44 | my %webvar = parse_post();
|
---|
45 | cleanInput(\%webvar);
|
---|
46 |
|
---|
47 | my %full_alloc_types = (
|
---|
48 | "ci","Static cable IP",
|
---|
49 | "di","Static DSL 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
|
---|
69 | my @masterblocks;
|
---|
70 | my %allocated; # Count for allocated blocks in a master block
|
---|
71 | my %free; # Count for free blocks (routed and unrouted) in a master block
|
---|
72 | my %bigfree; # Tracking largest free block in a master block
|
---|
73 | my %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
|
---|
78 | my $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.
|
---|
82 | my $sth = $ip_dbh->prepare("select * from masterblocks order by cidr");
|
---|
83 | $sth->execute;
|
---|
84 | for (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 |
|
---|
98 | if(!defined($webvar{action})) {
|
---|
99 | $webvar{action} = "<NULL>"; #shuts up the warnings.
|
---|
100 | }
|
---|
101 |
|
---|
102 | if($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 |
|
---|
145 | elsif($webvar{action} eq 'showmaster') {
|
---|
146 | showMaster();
|
---|
147 | }
|
---|
148 | elsif($webvar{action} eq 'showrouted') {
|
---|
149 | showRBlock();
|
---|
150 | }
|
---|
151 | elsif($webvar{action} eq 'listpool') {
|
---|
152 | listPool();
|
---|
153 | }
|
---|
154 | elsif($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
|
---|
168 | elsif($webvar{action} eq 'assign') {
|
---|
169 | assignBlock();
|
---|
170 | }
|
---|
171 | elsif($webvar{action} eq 'confirm') {
|
---|
172 | confirmAssign();
|
---|
173 | }
|
---|
174 | elsif($webvar{action} eq 'insert') {
|
---|
175 | insertAssign();
|
---|
176 | }
|
---|
177 | elsif($webvar{action} eq 'edit') {
|
---|
178 | edit();
|
---|
179 | }
|
---|
180 | elsif($webvar{action} eq 'update') {
|
---|
181 | update();
|
---|
182 | }
|
---|
183 | elsif($webvar{action} eq 'delete') {
|
---|
184 | remove();
|
---|
185 | }
|
---|
186 | elsif($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.
|
---|
193 | else {
|
---|
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...
|
---|
210 | exit 0;
|
---|
211 |
|
---|
212 |
|
---|
213 | sub 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)
|
---|
321 | sub 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.
|
---|
343 | sub 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] =~ /^[cdsmw]$/) {
|
---|
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] =~ /^[cdsmw]p$/) {
|
---|
365 | $row[0] .= ' <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 </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> \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.
|
---|
399 | sub 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
|
---|
410 | sub 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
|
---|
420 | sub 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) ? ("<NONE>") : ("/".$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.)
|
---|
499 | sub 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) ? ("<NONE>") : ("/".$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.
|
---|
624 | sub 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] =~ /^[cdsmw]p$/) {
|
---|
654 | $row[0] .= ' <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
|
---|
706 | sub 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,circuitid
|
---|
737 | # If desc is null, make it not null. <g>
|
---|
738 | if ($data[7] eq '') {
|
---|
739 | $data[7] = ' ';
|
---|
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 | (" ") )
|
---|
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.
|
---|
762 | sub 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.
|
---|
796 | sub 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} =~ /^[cdsmw]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.
|
---|
828 | my $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} =~ /^[cdsmw]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} =~ /^[cdsmw]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 | $html =~ s|\$\$CIRCID\$\$|$webvar{circid}|g;
|
---|
936 | $webvar{desc} = desanitize($webvar{desc});
|
---|
937 | $webvar{notes} = desanitize($webvar{notes});
|
---|
938 | $html =~ s|\$\$DESC\$\$|$webvar{desc}|g;
|
---|
939 | $html =~ s|\$\$NOTES\$\$|$webvar{notes}|g;
|
---|
940 | $html =~ s|\$\$ACTION\$\$|insert|g;
|
---|
941 |
|
---|
942 | print $html;
|
---|
943 |
|
---|
944 | printFooter;
|
---|
945 | } # end confirmAssign
|
---|
946 |
|
---|
947 |
|
---|
948 | # Do the work of actually inserting a block in the database.
|
---|
949 | sub insertAssign {
|
---|
950 | # Some things are done more than once.
|
---|
951 | printHeader('');
|
---|
952 | validateInput();
|
---|
953 |
|
---|
954 | # Set some things that may be needed
|
---|
955 | # Don't set $cidr here as it may not even be a valid IP address.
|
---|
956 | my $alloc_from = new NetAddr::IP $webvar{alloc_from};
|
---|
957 |
|
---|
958 | # dynDSL (dy), sIP DSL(dp), and server pools (sp) are nominally allocated to Sudbury
|
---|
959 | # no matter what else happens.
|
---|
960 | # if ($webvar{alloctype} =~ /^([sd]p|dy)$/) { $webvar{city} = "Sudbury"; }
|
---|
961 | # OOPS. forgot about North Bay DSL.
|
---|
962 | #### Gotta make this cleaner and more accurate
|
---|
963 | # if ($webvar{alloctype} eq "sp") { $webvar{city} = "Sudbury"; }
|
---|
964 |
|
---|
965 | # Same ordering as confirmation page
|
---|
966 |
|
---|
967 | if ($webvar{alloctype} =~ /^[cdsmw]i$/) {
|
---|
968 | my ($base,$tmp) = split //, $webvar{alloctype}; # split into individual chars
|
---|
969 |
|
---|
970 | # We'll just have to put up with the oddities caused by SQL (un)sort order
|
---|
971 | $sth = $ip_dbh->prepare("select * from poolips where pool='$webvar{alloc_from}'".
|
---|
972 | " and available='y' order by ip");
|
---|
973 | $sth->execute;
|
---|
974 |
|
---|
975 | my @data = $sth->fetchrow_array;
|
---|
976 | my $cidr = $data[1];
|
---|
977 |
|
---|
978 | $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}',".
|
---|
979 | "city='$webvar{city}',available='n',description='$webvar{desc}',".
|
---|
980 | "circuitid='$webvar{circid}'".
|
---|
981 | " where ip='$cidr'");
|
---|
982 | $sth->execute;
|
---|
983 | if ($sth->err) {
|
---|
984 | syslog "err", "Allocation of $cidr to $webvar{custid} by $authuser failed: ".
|
---|
985 | "'".$sth->errstr."'";
|
---|
986 | printAndExit("Allocation of $cidr to $webvar{custid} failed: '".$sth->errstr."'");
|
---|
987 | }
|
---|
988 | print qq(<div class="center"><div class="heading">The IP $cidr has been allocated to customer $webvar{custid}</div></div>);
|
---|
989 | syslog "notice", "$authuser allocated $cidr to $webvar{custid}";
|
---|
990 | # Notify tech@example.com
|
---|
991 | mailNotify('tech@example.com','$full_alloc_types{$webvar{alloctype}} allocation',
|
---|
992 | "$full_alloc_types{$webvar{alloctype}} $cidr allocated to customer $webvar{custid}");
|
---|
993 |
|
---|
994 | } else { # end IP-from-pool allocation
|
---|
995 |
|
---|
996 | # Set $cidr here as it may not be a valid IP address elsewhere.
|
---|
997 | my $cidr = new NetAddr::IP $webvar{fullcidr};
|
---|
998 |
|
---|
999 | # Allow transactions, and make errors much easier to catch.
|
---|
1000 | # Much as I would like to error-track specifically on each ->execute,
|
---|
1001 | # that's a LOT of code, and some SQL blocks MUST be atomic at a
|
---|
1002 | # multi-statement level. :/
|
---|
1003 | local $ip_dbh->{AutoCommit} = 0; # These need to be local so we don't
|
---|
1004 | local $ip_dbh->{RaiseError} = 1; # step on our toes by accident.
|
---|
1005 |
|
---|
1006 | if ($webvar{fullcidr} eq $webvar{alloc_from}) {
|
---|
1007 | # Easiest case- insert in one table, delete in the other, and go home. More or less.
|
---|
1008 | # insert into allocations values (cidr,custid,type,city,desc) and
|
---|
1009 | # delete from freeblocks where cidr='cidr'
|
---|
1010 | # For data safety on non-transaction DBs, we delete first.
|
---|
1011 |
|
---|
1012 | eval {
|
---|
1013 | if ($webvar{alloctype} eq 'rr') {
|
---|
1014 | $sth = $ip_dbh->prepare("update freeblocks set routed='y',city='$webvar{city}'".
|
---|
1015 | " where cidr='$webvar{fullcidr}'");
|
---|
1016 | $sth->execute;
|
---|
1017 | $sth = $ip_dbh->prepare("insert into routed values ('$webvar{fullcidr}',".
|
---|
1018 | $cidr->masklen.",'$webvar{city}')");
|
---|
1019 | $sth->execute;
|
---|
1020 | } else {
|
---|
1021 | # common stuff for end-use, dialup, dynDSL, pools, etc, etc.
|
---|
1022 |
|
---|
1023 | # city has to be reset for DSL/server pools; nominally to Sudbury.
|
---|
1024 | ## Gotta rethink this; DSL pools can be in North Bay as well. :/
|
---|
1025 | #if ($webvar{alloctype} =~ /^[sd]p$/) { $webvar{city} = 'Sudbury'; }
|
---|
1026 |
|
---|
1027 | $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$webvar{fullcidr}'");
|
---|
1028 | $sth->execute;
|
---|
1029 |
|
---|
1030 | $sth = $ip_dbh->prepare("insert into allocations values ('$webvar{fullcidr}',".
|
---|
1031 | "'$webvar{custid}','$webvar{alloctype}','$webvar{city}','$webvar{desc}',".
|
---|
1032 | "'$webvar{notes}',".$cidr->masklen.",'$webvar{circid}')");
|
---|
1033 | $sth->execute;
|
---|
1034 | } # routing vs non-routing netblock
|
---|
1035 | $ip_dbh->commit;
|
---|
1036 | }; # end of eval
|
---|
1037 | if ($@) {
|
---|
1038 | carp "Transaction aborted because $@";
|
---|
1039 | eval { $ip_dbh->rollback; };
|
---|
1040 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
1041 | "'$webvar{alloctype}' by $authuser failed: '$@'";
|
---|
1042 | printAndExit("Allocation of $cidr as $full_alloc_types{$webvar{alloctype}} failed.\n");
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | # If we get here, the DB transaction has succeeded.
|
---|
1046 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as '$webvar{alloctype}'";
|
---|
1047 |
|
---|
1048 | # How to log SQL without munging too many error-checking wrappers in?
|
---|
1049 | # syslog "info", "
|
---|
1050 | # We don't. GRRR.
|
---|
1051 |
|
---|
1052 | } else { # webvar{fullcidr} != webvar{alloc_from}
|
---|
1053 | # Hard case. Allocation is smaller than free block.
|
---|
1054 | my $wantmaskbits = $cidr->masklen;
|
---|
1055 | my $maskbits = $alloc_from->masklen;
|
---|
1056 |
|
---|
1057 | my @newfreeblocks; # Holds free blocks generated from splitting the source freeblock.
|
---|
1058 |
|
---|
1059 | my $i=0;
|
---|
1060 | while ($maskbits++ < $wantmaskbits) {
|
---|
1061 | my @subblocks = $alloc_from->split($maskbits);
|
---|
1062 | $newfreeblocks[$i++] = $subblocks[1];
|
---|
1063 | } # while
|
---|
1064 |
|
---|
1065 | # Begin SQL transaction block
|
---|
1066 | eval {
|
---|
1067 | # Delete old freeblocks entry
|
---|
1068 | $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$webvar{alloc_from}'");
|
---|
1069 | $sth->execute();
|
---|
1070 |
|
---|
1071 | # now we have to do some magic for routing blocks
|
---|
1072 | if ($webvar{alloctype} eq 'rr') {
|
---|
1073 | # Insert the new freeblocks entries
|
---|
1074 | # Note that non-routed blocks are assigned to <NULL>
|
---|
1075 | $sth = $ip_dbh->prepare("insert into freeblocks values (?, ?, '<NULL>','n')");
|
---|
1076 | foreach my $block (@newfreeblocks) {
|
---|
1077 | $sth->execute("$block", $block->masklen);
|
---|
1078 | }
|
---|
1079 | # Insert the entry in the routed table
|
---|
1080 | $sth = $ip_dbh->prepare("insert into routed values ('$cidr',".
|
---|
1081 | $cidr->masklen.",'$webvar{city}')");
|
---|
1082 | $sth->execute;
|
---|
1083 | # Insert the (almost) same entry in the freeblocks table
|
---|
1084 | $sth = $ip_dbh->prepare("insert into freeblocks values ('$cidr',".
|
---|
1085 | $cidr->masklen.",'$webvar{city}','y')");
|
---|
1086 | $sth->execute;
|
---|
1087 |
|
---|
1088 | } else { # done with alloctype == rr
|
---|
1089 |
|
---|
1090 | # Insert the new freeblocks entries
|
---|
1091 | $sth = $ip_dbh->prepare("insert into freeblocks values (?, ?, ?,'y')");
|
---|
1092 | foreach my $block (@newfreeblocks) {
|
---|
1093 | $sth->execute("$block", $block->masklen, $webvar{city});
|
---|
1094 | }
|
---|
1095 | # Insert the allocations entry
|
---|
1096 | $sth = $ip_dbh->prepare("insert into allocations values ('$webvar{fullcidr}',".
|
---|
1097 | "'$webvar{custid}','$webvar{alloctype}','$webvar{city}',".
|
---|
1098 | "'$webvar{desc}','$webvar{notes}',".$cidr->masklen.",'$webvar{circid}')");
|
---|
1099 | $sth->execute;
|
---|
1100 | } # done with netblock alloctype != rr
|
---|
1101 | $ip_dbh->commit;
|
---|
1102 | }; # end eval
|
---|
1103 | if ($@) {
|
---|
1104 | carp "Transaction aborted because $@";
|
---|
1105 | eval { $ip_dbh->rollback; };
|
---|
1106 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
1107 | "'$webvar{alloctype}' by $authuser failed: '$@'";
|
---|
1108 | printAndExit("Allocation of $cidr as $full_alloc_types{$webvar{alloctype}} failed.\n");
|
---|
1109 | }
|
---|
1110 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as '$webvar{alloctype}'";
|
---|
1111 |
|
---|
1112 | } # end fullcidr != alloc_from
|
---|
1113 |
|
---|
1114 | # Begin SQL transaction block
|
---|
1115 | eval {
|
---|
1116 | # special extra handling for pools.
|
---|
1117 | # Note that this must be done for ANY pool allocation!
|
---|
1118 | if ( my ($pooltype) = ($webvar{alloctype} =~ /^([cdsmw])p$/) ) {
|
---|
1119 | # have to insert all pool IPs into poolips table as "unallocated".
|
---|
1120 | $sth = $ip_dbh->prepare("insert into poolips values ('$webvar{fullcidr}',".
|
---|
1121 | " ?, '6750400', '$webvar{city}', '$pooltype', 'y', '', '', '')");
|
---|
1122 | my @poolip_list = $cidr->hostenum;
|
---|
1123 | for (my $i=1; $i<=$#poolip_list; $i++) {
|
---|
1124 | $sth->execute($poolip_list[$i]->addr);
|
---|
1125 | }
|
---|
1126 | } # end pool special
|
---|
1127 | $ip_dbh->commit;
|
---|
1128 | }; # end eval
|
---|
1129 | if ($@) {
|
---|
1130 | carp "Transaction aborted because $@";
|
---|
1131 | eval { $ip_dbh->rollback; };
|
---|
1132 | syslog "err", "Initialization of pool '$webvar{fullcidr}' by $authuser failed: '$@'";
|
---|
1133 | printAndExit("$full_alloc_types{$webvar{alloctype}} $webvar{fullcidr} not completely initialized.");
|
---|
1134 | }
|
---|
1135 | syslog "notice", "$full_alloc_types{$webvar{alloctype}} '$webvar{fullcidr}' successfully initialized by $authuser";
|
---|
1136 |
|
---|
1137 | 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>);
|
---|
1138 |
|
---|
1139 | } # end static-IP vs netblock allocation
|
---|
1140 |
|
---|
1141 | printFooter();
|
---|
1142 | } # end insertAssign()
|
---|
1143 |
|
---|
1144 |
|
---|
1145 | # Does some basic checks on common input data to make sure nothing
|
---|
1146 | # *really* weird gets in to the database through this script.
|
---|
1147 | # Does NOT do complete input validation!!!
|
---|
1148 | sub validateInput {
|
---|
1149 | if ($webvar{city} eq '-') {
|
---|
1150 | printAndExit("Please choose a city.");
|
---|
1151 | }
|
---|
1152 | chomp $webvar{alloctype};
|
---|
1153 | # We have different handling for customer allocations and "internal" or "our" allocations
|
---|
1154 | if ($webvar{alloctype} =~ /^(ci|di|cn|mi)$/) {
|
---|
1155 | if (!$webvar{custid}) {
|
---|
1156 | printAndExit("Please enter a customer ID.");
|
---|
1157 | }
|
---|
1158 | if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
|
---|
1159 | printAndExit("Please enter a valid customer ID- this must be a 7- or 10-digit number, or STAFF for static IPs for staff.");
|
---|
1160 | }
|
---|
1161 | print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
|
---|
1162 | } elsif ($webvar{alloctype} =~ /^([cdsmw]p|si|dn|dy|dc|ee|rr|ii)$/){
|
---|
1163 | # All non-customer allocations MUST be entered with "our" customer ID.
|
---|
1164 | # I have Defined this as 6750400 for consistency.
|
---|
1165 | $webvar{custid} = "6750400";
|
---|
1166 | if ($webvar{alloctype} eq 'rr') {
|
---|
1167 | if ($webvar{city} !~ /^(?:Huntsville|North Bay|Ottawa|Pembroke|Sault Ste. Marie|Sudbury|Timmins|Thunder Bay|Toronto)$/) {
|
---|
1168 | printAndExit("Please choose a valid POP location for a routed netblock. Valid ".
|
---|
1169 | "POP locations are currently:<br>\n Elliot Lake - Huntsville - North Bay -".
|
---|
1170 | " Ottawa -".
|
---|
1171 | " Pembroke - Sault Ste. Marie - Sudbury - Timmins - Thunder Bay - Toronto");
|
---|
1172 | }
|
---|
1173 | }
|
---|
1174 | } else {
|
---|
1175 | # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
|
---|
1176 | # managing to call things in such a way as to cause this deserves a cryptic error.
|
---|
1177 | printAndExit("Invalid alloctype");
|
---|
1178 | }
|
---|
1179 | return 0;
|
---|
1180 | } # end validateInput
|
---|
1181 |
|
---|
1182 |
|
---|
1183 | # Displays details of a specific allocation in a form
|
---|
1184 | # Allows update/delete
|
---|
1185 | # action=edit
|
---|
1186 | sub edit {
|
---|
1187 | printHeader('');
|
---|
1188 |
|
---|
1189 | my $sql;
|
---|
1190 |
|
---|
1191 | # Two cases: block is a netblock, or block is a static IP from a pool
|
---|
1192 | # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
|
---|
1193 | if ($webvar{block} =~ /\/32$/) {
|
---|
1194 | $sql = "select ip,custid,ptype,city,circuitid,description,notes from poolips where ip='$webvar{block}'";
|
---|
1195 | } else {
|
---|
1196 | $sql = "select cidr,custid,type,city,circuitid,description,notes from allocations where cidr='$webvar{block}'"
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | # gotta snag block info from db
|
---|
1200 | $sth = $ip_dbh->prepare($sql);
|
---|
1201 | $sth->execute;
|
---|
1202 | my @data = $sth->fetchrow_array;
|
---|
1203 |
|
---|
1204 | # Clean up extra whitespace on alloc type
|
---|
1205 | $data[2] =~ s/\s//;
|
---|
1206 |
|
---|
1207 | # Postfix "i" on pool IP types
|
---|
1208 | if ($data[2] =~ /^[cdsmw]$/) {
|
---|
1209 | $data[2] .= "i";
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | open (HTML, "../editDisplay.html")
|
---|
1213 | or croak "Could not open editDisplay.html :$!";
|
---|
1214 | my $html = join('', <HTML>);
|
---|
1215 |
|
---|
1216 | # We can't let the city be changed here; this block is a part of
|
---|
1217 | # a larger routed allocation and therefore by definition can't be moved.
|
---|
1218 | # block and city are static.
|
---|
1219 | ##fixme
|
---|
1220 | # Needs thinking. Have to allow changes to city to correct errors, no?
|
---|
1221 | $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
|
---|
1222 | $html =~ s/\$\$CITY\$\$/$data[3]/g;
|
---|
1223 |
|
---|
1224 | # Screw it. Changing allocation types gets very ugly VERY quickly- especially
|
---|
1225 | # with the much longer list of allocation types.
|
---|
1226 | # We'll just show what type of block it is.
|
---|
1227 |
|
---|
1228 | # this has now been Requested, so here goes.
|
---|
1229 |
|
---|
1230 | if ($data[2] =~ /^d[nyc]|cn|ee|ii$/) {
|
---|
1231 | # Block that can be changed
|
---|
1232 | my $blockoptions = "<select name=alloctype><option".
|
---|
1233 | (($data[2] eq 'dn') ? ' selected' : '') ." value='dn'>Dialup netblock</option>\n<option".
|
---|
1234 | (($data[2] eq 'dy') ? ' selected' : '') ." value='dy'>Dynamic DSL netblock</option>\n<option".
|
---|
1235 | (($data[2] eq 'dc') ? ' selected' : '') ." value='dc'>Dynamic cable netblock</option>\n<option".
|
---|
1236 | (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
|
---|
1237 | (($data[2] eq 'ee') ? ' selected' : '') ." value='ee'>End-use netblock</option>\n<option".
|
---|
1238 | (($data[2] eq 'ii') ? ' selected' : '') ." value='ii'>Internal netblock</option>\n".
|
---|
1239 | "</select>\n";
|
---|
1240 | $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g;
|
---|
1241 | } else {
|
---|
1242 | $html =~ s/\$\$TYPESELECT\$\$/$full_alloc_types{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g;
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | # These can be modified, although CustID changes may get ignored.
|
---|
1246 | $html =~ s/\$\$CUSTID\$\$/$data[1]/g;
|
---|
1247 | $html =~ s/\$\$TYPE\$\$/$data[2]/g;
|
---|
1248 | $html =~ s/\$\$CIRCID\$\$/$data[4]/g;
|
---|
1249 | $html =~ s/\$\$DESC\$\$/$data[5]/g;
|
---|
1250 | $html =~ s/\$\$NOTES\$\$/$data[6]/g;
|
---|
1251 |
|
---|
1252 | print $html;
|
---|
1253 |
|
---|
1254 | printFooter();
|
---|
1255 | } # edit()
|
---|
1256 |
|
---|
1257 |
|
---|
1258 | # Stuff new info about a block into the db
|
---|
1259 | # action=update
|
---|
1260 | sub update {
|
---|
1261 | printHeader('');
|
---|
1262 |
|
---|
1263 | # Make sure incoming data is in correct format - custID among other things.
|
---|
1264 | validateInput;
|
---|
1265 |
|
---|
1266 | # SQL transaction wrapper
|
---|
1267 | eval {
|
---|
1268 | # Relatively simple SQL transaction here.
|
---|
1269 | my $sql;
|
---|
1270 | if (my $pooltype = ($webvar{alloctype} =~ /^([cdsmw])i$/) ) {
|
---|
1271 | # Note the hack ( available='n' ) to work around "update" additions
|
---|
1272 | # to static IP space. Eww.
|
---|
1273 | $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',".
|
---|
1274 | "circuitid='$webvar{circid}',description='$webvar{desc}',available='n' ".
|
---|
1275 | "where ip='$webvar{block}'";
|
---|
1276 | } else {
|
---|
1277 | $sql = "update allocations set custid='$webvar{custid}',".
|
---|
1278 | "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',".
|
---|
1279 | "type='$webvar{alloctype}',circuitid='$webvar{circid}' where cidr='$webvar{block}'";
|
---|
1280 | }
|
---|
1281 | syslog "debug", $sql;
|
---|
1282 | $sth = $ip_dbh->prepare($sql);
|
---|
1283 | $sth->execute;
|
---|
1284 | $ip_dbh->commit;
|
---|
1285 | };
|
---|
1286 | if ($@) {
|
---|
1287 | carp "Transaction aborted because $@";
|
---|
1288 | eval { $ip_dbh->rollback; };
|
---|
1289 | syslog "err", "$authuser could not update block/IP '$webvar{block}': '$@'";
|
---|
1290 | printAndExit("Could not update block/IP $webvar{block}: $@");
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | # If we get here, the operation succeeded.
|
---|
1294 | syslog "notice", "$authuser updated $webvar{block}";
|
---|
1295 | open (HTML, "../updated.html")
|
---|
1296 | or croak "Could not open updated.html :$!";
|
---|
1297 | my $html = join('', <HTML>);
|
---|
1298 |
|
---|
1299 | $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
|
---|
1300 | $html =~ s/\$\$CITY\$\$/$webvar{city}/g;
|
---|
1301 | $html =~ s/\$\$ALLOCTYPE\$\$/$webvar{alloctype}/g;
|
---|
1302 | $html =~ s/\$\$TYPEFULL\$\$/$full_alloc_types{$webvar{alloctype}}/g;
|
---|
1303 | $html =~ s/\$\$CUSTID\$\$/$webvar{custid}/g;
|
---|
1304 | $html =~ s/\$\$CIRCID\$\$/$webvar{circid}/g;
|
---|
1305 | $html =~ s/\$\$DESC\$\$/$webvar{desc}/g;
|
---|
1306 | $html =~ s/\$\$NOTES\$\$/$webvar{notes}/g;
|
---|
1307 |
|
---|
1308 | print $html;
|
---|
1309 |
|
---|
1310 | printFooter;
|
---|
1311 | } # update()
|
---|
1312 |
|
---|
1313 |
|
---|
1314 | # Delete an allocation.
|
---|
1315 | sub remove
|
---|
1316 | {
|
---|
1317 | printHeader('');
|
---|
1318 | #show confirm screen.
|
---|
1319 | open HTML, "../confirmRemove.html"
|
---|
1320 | or croak "Could not open confirmRemove.html :$!";
|
---|
1321 | my $html = join('', <HTML>);
|
---|
1322 | close HTML;
|
---|
1323 |
|
---|
1324 | # Serves'em right for getting here...
|
---|
1325 | if (!defined($webvar{block})) {
|
---|
1326 | printAndExit("Error 332");
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype);
|
---|
1330 |
|
---|
1331 | if ($webvar{alloctype} eq 'rr') {
|
---|
1332 | $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
|
---|
1333 | $sth->execute();
|
---|
1334 |
|
---|
1335 | # This feels... extreme.
|
---|
1336 | croak $sth->errstr() if($sth->errstr());
|
---|
1337 |
|
---|
1338 | $sth->bind_columns(\$cidr,\$city);
|
---|
1339 | $sth->execute();
|
---|
1340 | $sth->fetch || croak $sth->errstr();
|
---|
1341 | $custid = "N/A";
|
---|
1342 | $alloctype = $webvar{alloctype};
|
---|
1343 | $circid = "N/A";
|
---|
1344 | $desc = "N/A";
|
---|
1345 | $notes = "N/A";
|
---|
1346 |
|
---|
1347 | } elsif ($webvar{alloctype} eq 'mm') {
|
---|
1348 | $cidr = $webvar{block};
|
---|
1349 | $city = "N/A";
|
---|
1350 | $custid = "N/A";
|
---|
1351 | $alloctype = $webvar{alloctype};
|
---|
1352 | $circid = "N/A";
|
---|
1353 | $desc = "N/A";
|
---|
1354 | $notes = "N/A";
|
---|
1355 | } elsif ($webvar{alloctype} =~ /^[cdsmw]i$/) { # done with alloctype=rr
|
---|
1356 |
|
---|
1357 | # Unassigning a static IP
|
---|
1358 | my $sth = $ip_dbh->prepare("select ip,custid,city,ptype,notes,circuitid from poolips".
|
---|
1359 | " where ip='$webvar{block}'");
|
---|
1360 | $sth->execute();
|
---|
1361 | # croak $sth->errstr() if($sth->errstr());
|
---|
1362 |
|
---|
1363 | $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid);
|
---|
1364 | $sth->fetch() || croak $sth->errstr;
|
---|
1365 |
|
---|
1366 | $alloctype .="i";
|
---|
1367 |
|
---|
1368 | } else { # done with alloctype=[cdsmw]i
|
---|
1369 |
|
---|
1370 | my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes from ".
|
---|
1371 | "allocations where cidr='$webvar{block}'");
|
---|
1372 | $sth->execute();
|
---|
1373 | # croak $sth->errstr() if($sth->errstr());
|
---|
1374 |
|
---|
1375 | $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc, \$notes);
|
---|
1376 | $sth->fetch() || carp $sth->errstr;
|
---|
1377 | } # end cases for different alloctypes
|
---|
1378 |
|
---|
1379 | # Munge everything into HTML
|
---|
1380 | $html =~ s|Please confirm|Please confirm <b>removal</b> of|;
|
---|
1381 | $html =~ s|\$\$BLOCK\$\$|$cidr|g;
|
---|
1382 | $html =~ s|\$\$TYPEFULL\$\$|$full_alloc_types{$alloctype}|g;
|
---|
1383 | $html =~ s|\$\$ALLOCTYPE\$\$|$alloctype|g;
|
---|
1384 | $html =~ s|\$\$CITY\$\$|$city|g;
|
---|
1385 | $html =~ s|\$\$CUSTID\$\$|$custid|g;
|
---|
1386 | $html =~ s|\$\$CIRCID\$\$|$circid|g;
|
---|
1387 | $html =~ s|\$\$DESC\$\$|$desc|g;
|
---|
1388 | $html =~ s|\$\$NOTES\$\$|$notes|g;
|
---|
1389 |
|
---|
1390 | $html =~ s|\$\$ACTION\$\$|finaldelete|g;
|
---|
1391 |
|
---|
1392 | # Set the warning text.
|
---|
1393 | if ($alloctype =~ /^[cdsmw]p$/) {
|
---|
1394 | $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>|;
|
---|
1395 | } else {
|
---|
1396 | $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.</div></td></tr>|;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | print $html;
|
---|
1400 | printFooter;
|
---|
1401 | } # end edit()
|
---|
1402 |
|
---|
1403 |
|
---|
1404 | # Delete an allocation. Return it to the freeblocks table; munge
|
---|
1405 | # data as necessary to keep as few records as possible in freeblocks
|
---|
1406 | # to prevent weirdness when allocating blocks later.
|
---|
1407 | # Remove IPs from pool listing if necessary
|
---|
1408 | sub finalDelete {
|
---|
1409 | printHeader('');
|
---|
1410 |
|
---|
1411 | # Enable transactions and exception-on-errors... but only for this sub
|
---|
1412 | local $ip_dbh->{AutoCommit} = 0;
|
---|
1413 | local $ip_dbh->{RaiseError} = 1;
|
---|
1414 |
|
---|
1415 | if ($webvar{alloctype} =~ /^[cdsmw]i$/) {
|
---|
1416 |
|
---|
1417 | eval {
|
---|
1418 | $sth = $ip_dbh->prepare("select * from poolips where ip='$webvar{block}'");
|
---|
1419 | $sth->execute;
|
---|
1420 | my @data = $sth->fetchrow_array;
|
---|
1421 | $sth = $ip_dbh->prepare("select city from allocations where cidr='$data[0]'");
|
---|
1422 | $sth->execute;
|
---|
1423 | @data = $sth->fetchrow_array;
|
---|
1424 | $sth = $ip_dbh->prepare("update poolips set custid='6750400', available='y',".
|
---|
1425 | " city='$data[0]', description='' where ip='$webvar{block}'");
|
---|
1426 | $sth->execute;
|
---|
1427 | $ip_dbh->commit;
|
---|
1428 | };
|
---|
1429 | if ($@) {
|
---|
1430 | carp "Transaction aborted because $@";
|
---|
1431 | eval { $ip_dbh->rollback; };
|
---|
1432 | syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$@'";
|
---|
1433 | printAndExit("Could not deallocate static IP $webvar{block}: $@");
|
---|
1434 | }
|
---|
1435 | print "<div class=heading align=center>Success! $webvar{block} deallocated.</div>\n";
|
---|
1436 | syslog "notice", "$authuser deallocated static IP $webvar{block}";
|
---|
1437 |
|
---|
1438 | } elsif ($webvar{alloctype} eq 'mm') { # end alloctype = [cdsmw]i
|
---|
1439 |
|
---|
1440 | eval {
|
---|
1441 | $sth = $ip_dbh->prepare("delete from masterblocks where cidr='$webvar{block}'");
|
---|
1442 | $sth->execute;
|
---|
1443 | $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$webvar{block}'");
|
---|
1444 | $sth->execute;
|
---|
1445 | $ip_dbh->commit;
|
---|
1446 | };
|
---|
1447 | if ($@) {
|
---|
1448 | carp "Transaction aborted because $@";
|
---|
1449 | eval { $ip_dbh->rollback; };
|
---|
1450 | syslog "err", "$authuser could not remove master block '$webvar{block}': '$@'";
|
---|
1451 | printAndExit("Could not remove master block $webvar{block}: $@");
|
---|
1452 | }
|
---|
1453 | print "<div class=heading align=center>Success! Master $webvar{block} removed.</div>\n";
|
---|
1454 | syslog "notice", "$authuser removed master block $webvar{block}";
|
---|
1455 |
|
---|
1456 | } else { # end alloctype master block case
|
---|
1457 |
|
---|
1458 | ## This is a big block; but it HAS to be done in a chunk. Any removal
|
---|
1459 | ## of a netblock allocation may result in a larger chunk of free
|
---|
1460 | ## contiguous IP space - which may in turn be combined into a single
|
---|
1461 | ## netblock rather than a number of smaller netblocks.
|
---|
1462 |
|
---|
1463 | eval {
|
---|
1464 |
|
---|
1465 | my $cidr = new NetAddr::IP $webvar{block};
|
---|
1466 | if ($webvar{alloctype} eq 'rr') {
|
---|
1467 |
|
---|
1468 | $sth = $ip_dbh->prepare("delete from routed where cidr='$webvar{block}'");
|
---|
1469 | $sth->execute;
|
---|
1470 | # Make sure block getting deleted is properly accounted for.
|
---|
1471 | $sth = $ip_dbh->prepare("update freeblocks set routed='n',city='<NULL>'".
|
---|
1472 | " where cidr='$webvar{block}'");
|
---|
1473 | $sth->execute;
|
---|
1474 | # Set up query to start compacting free blocks.
|
---|
1475 | $sth = $ip_dbh->prepare("select * from freeblocks where ".
|
---|
1476 | "maskbits<=".$cidr->masklen." and routed='n' order by maskbits desc");
|
---|
1477 |
|
---|
1478 | } else { # end alloctype routing case
|
---|
1479 |
|
---|
1480 | $sth = $ip_dbh->prepare("delete from allocations where cidr='$webvar{block}'");
|
---|
1481 | $sth->execute;
|
---|
1482 | # Special case - delete pool IPs
|
---|
1483 | if ($webvar{alloctype} =~ /^[cdsmw]p$/) {
|
---|
1484 | # We have to delete the IPs from the pool listing.
|
---|
1485 | $sth = $ip_dbh->prepare("delete from poolips where pool='$webvar{block}'");
|
---|
1486 | $sth->execute;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | # Set up query for compacting free blocks.
|
---|
1490 | $sth = $ip_dbh->prepare("select * from freeblocks where cidr << ".
|
---|
1491 | "(select cidr from routed where cidr >> '$cidr') ".
|
---|
1492 | " and maskbits<=".$cidr->masklen." and routed='y' order by maskbits desc");
|
---|
1493 |
|
---|
1494 | } # end alloctype general case
|
---|
1495 |
|
---|
1496 | # Now we look for larger-or-equal-sized free blocks in the same master (routed)
|
---|
1497 | # (super)block. If there aren't any, we can't combine blocks anyway. If there
|
---|
1498 | # are, we check to see if we can combine blocks.
|
---|
1499 | # Execute the statement prepared in the if-else above.
|
---|
1500 |
|
---|
1501 | $sth->execute;
|
---|
1502 |
|
---|
1503 | # NetAddr::IP->compact() attempts to produce the smallest inclusive block
|
---|
1504 | # from the caller and the passed terms.
|
---|
1505 | # EG: if you call $cidr->compact($ip1,$ip2,$ip3) when $cidr, $ip1, $ip2,
|
---|
1506 | # and $ip3 are consecutive /27's starting on .0 (.0-.31, .32-.63,
|
---|
1507 | # .64-.95, and .96-.128), you will get an array containing a single
|
---|
1508 | # /25 as element 0 (.0-.127). Order is not important; you could have
|
---|
1509 | # $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
|
---|
1510 |
|
---|
1511 | my (@together, @combinelist);
|
---|
1512 | my $i=0;
|
---|
1513 | while (my @data = $sth->fetchrow_array) {
|
---|
1514 | my $testIP = new NetAddr::IP $data[0];
|
---|
1515 | @together = $testIP->compact($cidr);
|
---|
1516 | my $num = @together;
|
---|
1517 | if ($num == 1) {
|
---|
1518 | $cidr = $together[0];
|
---|
1519 | $combinelist[$i++] = $testIP;
|
---|
1520 | }
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | # Clear old freeblocks entries - if any. $i==0 if not.
|
---|
1524 | if ($i>0) {
|
---|
1525 | $sth = $ip_dbh->prepare("delete from freeblocks where cidr=?");
|
---|
1526 | foreach my $block (@combinelist) {
|
---|
1527 | $sth->execute("$block");
|
---|
1528 | }
|
---|
1529 | }
|
---|
1530 |
|
---|
1531 | # insert "new" freeblocks entry
|
---|
1532 | if ($webvar{alloctype} eq 'rr') {
|
---|
1533 | $sth = $ip_dbh->prepare("insert into freeblocks values ('$cidr',".$cidr->masklen.
|
---|
1534 | ",'<NULL>','n')");
|
---|
1535 | } else {
|
---|
1536 | $sth = $ip_dbh->prepare("insert into freeblocks values ('$cidr',".$cidr->masklen.
|
---|
1537 | ",(select city from routed where cidr >>= '$cidr'),'y')");
|
---|
1538 | }
|
---|
1539 | $sth->execute;
|
---|
1540 |
|
---|
1541 | # If we got here, we've succeeded. Whew!
|
---|
1542 | $ip_dbh->commit;
|
---|
1543 | }; # end eval
|
---|
1544 | if ($@) {
|
---|
1545 | carp "Transaction aborted because $@";
|
---|
1546 | eval { $ip_dbh->rollback; };
|
---|
1547 | syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$@'";
|
---|
1548 | printAndExit("Could not deallocate netblock $webvar{block}: $@");
|
---|
1549 | }
|
---|
1550 | print "<div class=heading align=center>Success! $webvar{block} deleted.</div>\n";
|
---|
1551 | syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}";
|
---|
1552 |
|
---|
1553 | } # end alloctype != netblock
|
---|
1554 |
|
---|
1555 | printFooter;
|
---|
1556 | } # finalDelete
|
---|
1557 |
|
---|
1558 |
|
---|
1559 | # Just in case we manage to get here.
|
---|
1560 | exit 0;
|
---|