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