1 | #!/usr/bin/perl
|
---|
2 | # ipdb/cgi-bin/main.cgi
|
---|
3 | ###
|
---|
4 | # SVN revision info
|
---|
5 | # $Date: 2010-08-25 20:12:50 +0000 (Wed, 25 Aug 2010) $
|
---|
6 | # SVN revision $Rev: 477 $
|
---|
7 | # Last update by $Author: kdeugau $
|
---|
8 | ###
|
---|
9 | # Copyright (C) 2004-2010 - Kris Deugau
|
---|
10 |
|
---|
11 | use strict;
|
---|
12 | use warnings;
|
---|
13 | use CGI::Carp qw(fatalsToBrowser);
|
---|
14 | use CGI::Simple;
|
---|
15 | use HTML::Template;
|
---|
16 | use DBI;
|
---|
17 | use CommonWeb qw(:ALL);
|
---|
18 | use CustIDCK;
|
---|
19 | use POSIX qw(ceil);
|
---|
20 | use NetAddr::IP;
|
---|
21 |
|
---|
22 | use Sys::Syslog;
|
---|
23 |
|
---|
24 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
25 | ##uselib##
|
---|
26 |
|
---|
27 | use MyIPDB;
|
---|
28 |
|
---|
29 | openlog "IPDB","pid","$IPDB::syslog_facility";
|
---|
30 |
|
---|
31 | # Collect the username from HTTP auth. If undefined, we're in
|
---|
32 | # a test environment, or called without a username.
|
---|
33 | my $authuser;
|
---|
34 | if (!defined($ENV{'REMOTE_USER'})) {
|
---|
35 | $authuser = '__temptest';
|
---|
36 | } else {
|
---|
37 | $authuser = $ENV{'REMOTE_USER'};
|
---|
38 | }
|
---|
39 |
|
---|
40 | syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
|
---|
41 |
|
---|
42 | # Why not a global DB handle? (And a global statement handle, as well...)
|
---|
43 | # Use the connectDB function, otherwise we end up confusing ourselves
|
---|
44 | my $ip_dbh;
|
---|
45 | my $sth;
|
---|
46 | my $errstr;
|
---|
47 | ($ip_dbh,$errstr) = connectDB_My;
|
---|
48 | if (!$ip_dbh) {
|
---|
49 | exitError("Database error: $errstr\n");
|
---|
50 | }
|
---|
51 | initIPDBGlobals($ip_dbh);
|
---|
52 |
|
---|
53 | # Set up some globals
|
---|
54 | ##fixme - need to autofill this somehow
|
---|
55 | $ENV{HTML_TEMPLATE_ROOT} = '/home/kdeugau/dev/ipdb/trunk/templates';
|
---|
56 |
|
---|
57 | my $header = HTML::Template->new(filename => "header.tmpl");
|
---|
58 | my $footer = HTML::Template->new(filename => "footer.tmpl");
|
---|
59 |
|
---|
60 | $header->param(version => $IPDB::VERSION);
|
---|
61 | $header->param(addperm => $IPDBacl{$authuser} =~ /a/);
|
---|
62 | print "Content-type: text/html\n\n", $header->output;
|
---|
63 |
|
---|
64 | # Set up the CGI object...
|
---|
65 | my $q = new CGI::Simple;
|
---|
66 | # ... and get query-string params as well as POST params if necessary
|
---|
67 | $q->parse_query_string;
|
---|
68 |
|
---|
69 | # Convenience; saves changing all references to %webvar
|
---|
70 | ##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
|
---|
71 | my %webvar = $q->Vars;
|
---|
72 |
|
---|
73 |
|
---|
74 | #main()
|
---|
75 |
|
---|
76 | if(!defined($webvar{action})) {
|
---|
77 | $webvar{action} = "index"; #shuts up the warnings.
|
---|
78 | }
|
---|
79 |
|
---|
80 | my $page = HTML::Template->new(filename => "$webvar{action}.tmpl");
|
---|
81 |
|
---|
82 | if($webvar{action} eq 'index') {
|
---|
83 | showSummary();
|
---|
84 | } elsif ($webvar{action} eq 'addmaster') {
|
---|
85 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
86 | printError("You shouldn't have been able to get here. Access denied.");
|
---|
87 | }
|
---|
88 | } elsif ($webvar{action} eq 'newmaster') {
|
---|
89 |
|
---|
90 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
91 | printError("You shouldn't have been able to get here. Access denied.");
|
---|
92 | } else {
|
---|
93 | my $cidr = new NetAddr::IP $webvar{cidr};
|
---|
94 | $page->param(cidr => "$cidr");
|
---|
95 |
|
---|
96 | my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
|
---|
97 |
|
---|
98 | if ($code eq 'FAIL') {
|
---|
99 | syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
|
---|
100 | $page->param(err => $msg);
|
---|
101 | } else {
|
---|
102 | syslog "info", "$authuser added master block $webvar{cidr}";
|
---|
103 | }
|
---|
104 |
|
---|
105 | } # ACL check
|
---|
106 |
|
---|
107 | } # end add new master
|
---|
108 |
|
---|
109 | elsif($webvar{action} eq 'showmaster') {
|
---|
110 | showMaster();
|
---|
111 | }
|
---|
112 | elsif($webvar{action} eq 'showrouted') {
|
---|
113 | showRBlock();
|
---|
114 | }
|
---|
115 | elsif($webvar{action} eq 'listpool') {
|
---|
116 | listPool();
|
---|
117 | }
|
---|
118 |
|
---|
119 | # Not modified or added; just shuffled
|
---|
120 | elsif($webvar{action} eq 'assign') {
|
---|
121 | assignBlock();
|
---|
122 | }
|
---|
123 | elsif($webvar{action} eq 'confirm') {
|
---|
124 | confirmAssign();
|
---|
125 | }
|
---|
126 | elsif($webvar{action} eq 'insert') {
|
---|
127 | insertAssign();
|
---|
128 | }
|
---|
129 | elsif($webvar{action} eq 'edit') {
|
---|
130 | edit();
|
---|
131 | }
|
---|
132 | elsif($webvar{action} eq 'update') {
|
---|
133 | update();
|
---|
134 | }
|
---|
135 | elsif($webvar{action} eq 'delete') {
|
---|
136 | remove();
|
---|
137 | }
|
---|
138 | elsif($webvar{action} eq 'finaldelete') {
|
---|
139 | finalDelete();
|
---|
140 | }
|
---|
141 | elsif ($webvar{action} eq 'nodesearch') {
|
---|
142 | $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
|
---|
143 | $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
|
---|
144 | my @nodelist;
|
---|
145 | while (my ($nid,$nname) = $sth->fetchrow_array()) {
|
---|
146 | my %row = (nodeid => $nid, nodename => $nname);
|
---|
147 | push @nodelist, \%row;
|
---|
148 | }
|
---|
149 | $page->param(nodelist => \@nodelist);
|
---|
150 | }
|
---|
151 |
|
---|
152 | # Default is an error. It shouldn't be possible to easily get here.
|
---|
153 | # The only way I can think of offhand is to just call main.cgi bare-
|
---|
154 | # which is not in any way guaranteed to provide anything useful.
|
---|
155 | else {
|
---|
156 | my $rnd = rand 500;
|
---|
157 | my $boing = sprintf("%.2f", rand 500);
|
---|
158 | my @excuses = ("Aether cloudy. Ask again later.","The gods are unhappy with your sacrifice.",
|
---|
159 | "Because one of it's legs are both the same", "*wibble*",
|
---|
160 | "Hey! Stop pushing my buttons!", "I ain't done nuttin'", "9",
|
---|
161 | "8", "9", "10", "11", "12", "13", "14", "15", "16", "17");
|
---|
162 | printAndExit("Error $boing: ".$excuses[$rnd/30.0]);
|
---|
163 | }
|
---|
164 | ## Finally! Done with that NASTY "case" emulation!
|
---|
165 |
|
---|
166 |
|
---|
167 |
|
---|
168 | # Clean up IPDB globals, DB handle, etc.
|
---|
169 | finish($ip_dbh);
|
---|
170 |
|
---|
171 | #print qq(<div align=right style="position: absolute; right: 30px;">).
|
---|
172 | # qq(<a href="/ip/cgi-bin/admin.cgi">Admin tools</a></div><br>\n)
|
---|
173 | # if $IPDBacl{$authuser} =~ /A/;
|
---|
174 |
|
---|
175 | ## Do all our printing here so we can generate errors and stick them into the slots in the templates.
|
---|
176 |
|
---|
177 | # can't do this yet, too many blowups
|
---|
178 | #print "Content-type: text/html\n\n", $header->output;
|
---|
179 |
|
---|
180 | print $page->output;
|
---|
181 |
|
---|
182 | # include the admin tools link in the output?
|
---|
183 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
|
---|
184 |
|
---|
185 | print $footer->output;
|
---|
186 |
|
---|
187 | # Just in case something waaaayyy down isn't in place
|
---|
188 | # properly... we exit explicitly.
|
---|
189 | exit;
|
---|
190 |
|
---|
191 |
|
---|
192 |
|
---|
193 | # args are: a reference to an array with the row to be printed and the
|
---|
194 | # class(stylesheet) to use for formatting.
|
---|
195 | # if ommitting the class - call the sub as &printRow(\@array)
|
---|
196 | sub printRow {
|
---|
197 | my ($rowRef,$class) = @_;
|
---|
198 |
|
---|
199 | if (!$class) {
|
---|
200 | print "<tr>\n";
|
---|
201 | } else {
|
---|
202 | print "<tr class=\"$class\">\n";
|
---|
203 | }
|
---|
204 |
|
---|
205 | ELEMENT: foreach my $element (@$rowRef) {
|
---|
206 | if (!defined($element)) {
|
---|
207 | print "<td></td>\n";
|
---|
208 | next ELEMENT;
|
---|
209 | }
|
---|
210 | $element =~ s|\n|</br>|g;
|
---|
211 | print "<td>$element</td>\n";
|
---|
212 | }
|
---|
213 | print "</tr>";
|
---|
214 | } # printRow
|
---|
215 |
|
---|
216 |
|
---|
217 | # Prints table headings. Accepts any number of arguments;
|
---|
218 | # each argument is a table heading.
|
---|
219 | sub startTable {
|
---|
220 | print qq(<center><table width="98%" cellspacing="0" class="center"><tr>);
|
---|
221 |
|
---|
222 | foreach(@_) {
|
---|
223 | print qq(<td class="heading">$_</td>);
|
---|
224 | }
|
---|
225 | print "</tr>\n";
|
---|
226 | } # startTable
|
---|
227 |
|
---|
228 |
|
---|
229 | # Initial display: Show master blocks with total allocated subnets, total free subnets
|
---|
230 | sub showSummary {
|
---|
231 | my %allocated;
|
---|
232 | my %free;
|
---|
233 | my %routed;
|
---|
234 | my %bigfree;
|
---|
235 |
|
---|
236 | # Count the allocations.
|
---|
237 | $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?");
|
---|
238 | foreach my $master (@masterblocks) {
|
---|
239 | $sth->execute("$master");
|
---|
240 | $sth->bind_columns(\$allocated{"$master"});
|
---|
241 | $sth->fetch();
|
---|
242 | }
|
---|
243 |
|
---|
244 | # Count routed blocks
|
---|
245 | $sth = $ip_dbh->prepare("select count(*) from routed where cidr <<= ?");
|
---|
246 | foreach my $master (@masterblocks) {
|
---|
247 | $sth->execute("$master");
|
---|
248 | $sth->bind_columns(\$routed{"$master"});
|
---|
249 | $sth->fetch();
|
---|
250 | }
|
---|
251 |
|
---|
252 | # Count the free blocks.
|
---|
253 | $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
|
---|
254 | "(routed='y' or routed='n')");
|
---|
255 | foreach my $master (@masterblocks) {
|
---|
256 | $sth->execute("$master");
|
---|
257 | $sth->bind_columns(\$free{"$master"});
|
---|
258 | $sth->fetch();
|
---|
259 | }
|
---|
260 |
|
---|
261 | # Find the largest free block in each master
|
---|
262 | $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
|
---|
263 | "(routed='y' or routed='n') order by maskbits limit 1");
|
---|
264 | foreach my $master (@masterblocks) {
|
---|
265 | $sth->execute("$master");
|
---|
266 | $sth->bind_columns(\$bigfree{"$master"});
|
---|
267 | $sth->fetch();
|
---|
268 | }
|
---|
269 |
|
---|
270 | # Assemble the data to stuff into the template.
|
---|
271 | my @masterlist;
|
---|
272 | my $rowclass=0;
|
---|
273 | foreach my $master (@masterblocks) {
|
---|
274 | my %row = (rowclass => "row$rowclass",
|
---|
275 | master => "$master",
|
---|
276 | routed => $routed{"$master"},
|
---|
277 | allocated => $allocated{"$master"},
|
---|
278 | free => $free{"$master"},
|
---|
279 | bigfree => ( ($bigfree{"$master"} eq '') ? ("<NONE>") : ("/".$bigfree{"$master"}) )
|
---|
280 | );
|
---|
281 | push (@masterlist, \%row);
|
---|
282 | $rowclass++; $rowclass = $rowclass % 2;
|
---|
283 | }
|
---|
284 | $page->param(masterlist => \@masterlist);
|
---|
285 |
|
---|
286 | $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
|
---|
287 |
|
---|
288 | } # showSummary
|
---|
289 |
|
---|
290 |
|
---|
291 | # Display detail on master
|
---|
292 | # Alrighty then! We're showing routed blocks within a single master this time.
|
---|
293 | # We should be able to steal code from showSummary(), and if I'm really smart
|
---|
294 | # I'll figger a way to munge the two together. (Once I've done that, everything
|
---|
295 | # else should follow. YMMV.)
|
---|
296 | sub showMaster {
|
---|
297 |
|
---|
298 | $page->param(master => $webvar{block});
|
---|
299 |
|
---|
300 | my %allocated;
|
---|
301 | my %free;
|
---|
302 | my %cities;
|
---|
303 | my %bigfree;
|
---|
304 |
|
---|
305 | my $master = new NetAddr::IP $webvar{block};
|
---|
306 | my @localmasters;
|
---|
307 |
|
---|
308 | # Fetch only the blocks relevant to this master
|
---|
309 | $sth = $ip_dbh->prepare("select cidr,city from routed where cidr <<= '$master' order by cidr");
|
---|
310 | $sth->execute();
|
---|
311 |
|
---|
312 | my $i=0;
|
---|
313 | while (my @data = $sth->fetchrow_array()) {
|
---|
314 | my $cidr = new NetAddr::IP $data[0];
|
---|
315 | $localmasters[$i++] = $cidr;
|
---|
316 | $free{"$cidr"} = 0;
|
---|
317 | $allocated{"$cidr"} = 0;
|
---|
318 | $bigfree{"$cidr"} = 128;
|
---|
319 | # Retain the routing destination
|
---|
320 | $cities{"$cidr"} = $data[1];
|
---|
321 | }
|
---|
322 |
|
---|
323 | # Check if there were actually any blocks routed from this master
|
---|
324 | if ($i > 0) {
|
---|
325 |
|
---|
326 | # Count the allocations
|
---|
327 | $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?");
|
---|
328 | foreach my $master (@localmasters) {
|
---|
329 | $sth->execute("$master");
|
---|
330 | $sth->bind_columns(\$allocated{"$master"});
|
---|
331 | $sth->fetch();
|
---|
332 | }
|
---|
333 |
|
---|
334 | # Count the free blocks.
|
---|
335 | $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
|
---|
336 | "(routed='y' or routed='n')");
|
---|
337 | foreach my $master (@localmasters) {
|
---|
338 | $sth->execute("$master");
|
---|
339 | $sth->bind_columns(\$free{"$master"});
|
---|
340 | $sth->fetch();
|
---|
341 | }
|
---|
342 |
|
---|
343 | # Get the size of the largest free block
|
---|
344 | $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
|
---|
345 | "(routed='y' or routed='n') order by maskbits limit 1");
|
---|
346 | foreach my $master (@localmasters) {
|
---|
347 | $sth->execute("$master");
|
---|
348 | $sth->bind_columns(\$bigfree{"$master"});
|
---|
349 | $sth->fetch();
|
---|
350 | }
|
---|
351 |
|
---|
352 | my @routed;
|
---|
353 | my $rowclass = 0;
|
---|
354 | foreach my $master (@localmasters) {
|
---|
355 | my %row = (
|
---|
356 | rowclass => $rowclass % 2,
|
---|
357 | block => "$master",
|
---|
358 | city => $cities{"$master"},
|
---|
359 | nsubs => $allocated{"$master"},
|
---|
360 | nfree => $free{"$master"},
|
---|
361 | lfree => ( ($bigfree{"$master"} eq 128) ? ("<NONE>") : ("/".$bigfree{"$master"}) )
|
---|
362 | );
|
---|
363 | push @routed, \%row;
|
---|
364 | $rowclass++;
|
---|
365 | }
|
---|
366 | $page->param(routedlist => \@routed);
|
---|
367 |
|
---|
368 | } # end check for existence of routed blocks in master
|
---|
369 |
|
---|
370 | $page->param(delmaster => ($IPDBacl{$authuser} =~ /d/));
|
---|
371 |
|
---|
372 | # Snag the free blocks.
|
---|
373 | my $count = 0;
|
---|
374 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr <<='$master' and ".
|
---|
375 | "routed='n' order by cidr");
|
---|
376 | $sth->execute();
|
---|
377 | my @unrouted;
|
---|
378 | my $rowclass = 0;
|
---|
379 | while (my @data = $sth->fetchrow_array()) {
|
---|
380 | my $cidr = new NetAddr::IP $data[0];
|
---|
381 | my %row = (
|
---|
382 | rowclass => $rowclass % 2,
|
---|
383 | fblock => "$cidr",
|
---|
384 | frange => $cidr->range
|
---|
385 | );
|
---|
386 | push @unrouted, \%row;
|
---|
387 | $rowclass++;
|
---|
388 | }
|
---|
389 | $page->param(unrouted => \@unrouted);
|
---|
390 |
|
---|
391 | } # showMaster
|
---|
392 |
|
---|
393 |
|
---|
394 | # Display details of a routed block
|
---|
395 | # Alrighty then! We're showing allocations within a routed block this time.
|
---|
396 | # We should be able to steal code from showSummary() and showMaster(), and if
|
---|
397 | # I'm really smart I'll figger a way to munge all three together. (Once I've
|
---|
398 | # done that, everything else should follow. YMMV.
|
---|
399 | # This time, we check the database before spewing, because we may
|
---|
400 | # not have anything useful to spew.
|
---|
401 | sub showRBlock {
|
---|
402 |
|
---|
403 | my $master = new NetAddr::IP $webvar{block};
|
---|
404 |
|
---|
405 | $sth = $ip_dbh->prepare("select city from routed where cidr='$master'");
|
---|
406 | $sth->execute;
|
---|
407 | my ($rcity) = $sth->fetchrow_array;
|
---|
408 |
|
---|
409 | $page->param(master => "$master");
|
---|
410 | $page->param(rcity => $rcity);
|
---|
411 |
|
---|
412 | # Snag the allocations for this block
|
---|
413 | $sth = $ip_dbh->prepare("select cidr,city,type,custid,swip,description".
|
---|
414 | " from allocations where cidr <<= '$master' order by cidr");
|
---|
415 | $sth->execute();
|
---|
416 |
|
---|
417 | # hack hack hack
|
---|
418 | # set up to flag swip=y records if they don't actually have supporting data in the customers table
|
---|
419 | my $custsth = $ip_dbh->prepare("select count(*) from customers where custid=?");
|
---|
420 |
|
---|
421 | my $rowclass = 0;
|
---|
422 | my @blocklist;
|
---|
423 | while (my ($cidr,$city,$type,$custid,$swip,$desc) = $sth->fetchrow_array()) {
|
---|
424 | $custsth->execute($custid);
|
---|
425 | my ($ncust) = $custsth->fetchrow_array();
|
---|
426 |
|
---|
427 | my %row = (
|
---|
428 | rowclass => $rowclass++ % 2,
|
---|
429 | block => $cidr,
|
---|
430 | city => $city,
|
---|
431 | type => $disp_alloctypes{$type},
|
---|
432 | custid => $custid,
|
---|
433 | swip => ($swip eq 'y' ? ($ncust == 0 ? 'Yes<small>*</small>' : 'Yes') : 'No'),
|
---|
434 | desc => $desc
|
---|
435 | );
|
---|
436 | $row{subblock} = ($type =~ /^.r$/); # hmf. wonder why these won't work in the hash declaration...
|
---|
437 | $row{listpool} = ($type =~ /^.[pd]$/);
|
---|
438 | push (@blocklist, \%row);
|
---|
439 | }
|
---|
440 | $page->param(blocklist => \@blocklist);
|
---|
441 |
|
---|
442 | $page->param(delrouted => $IPDBacl{$authuser} =~ /d/);
|
---|
443 |
|
---|
444 | # Snag the free blocks. We don't really *need* to be pedantic about avoiding
|
---|
445 | # unrouted free blocks, but it's better to let the database do the work if we can.
|
---|
446 | $rowclass = 0;
|
---|
447 | my @unassigned;
|
---|
448 | $sth = $ip_dbh->prepare("select cidr,routed from freeblocks where cidr <<= '$master'".
|
---|
449 | " order by cidr");
|
---|
450 | $sth->execute();
|
---|
451 | while (my ($cidr_db,$routed) = $sth->fetchrow_array()) {
|
---|
452 | my $cidr = new NetAddr::IP $cidr_db;
|
---|
453 |
|
---|
454 | my %row = (
|
---|
455 | rowclass => $rowclass++ % 2,
|
---|
456 | subblock => ($routed ne 'y' && $routed ne 'n'),
|
---|
457 | fblock => $cidr_db,
|
---|
458 | fbtype => $routed,
|
---|
459 | frange => $cidr->range,
|
---|
460 | );
|
---|
461 | push @unassigned, \%row;
|
---|
462 | }
|
---|
463 | $page->param(unassigned => \@unassigned);
|
---|
464 |
|
---|
465 | } # showRBlock
|
---|
466 |
|
---|
467 |
|
---|
468 | # List the IPs used in a pool
|
---|
469 | sub listPool {
|
---|
470 |
|
---|
471 | my $cidr = new NetAddr::IP $webvar{pool};
|
---|
472 |
|
---|
473 | $page->param(block => $webvar{pool});
|
---|
474 | $page->param(netip => $cidr->addr);
|
---|
475 | $cidr++;
|
---|
476 | $page->param(gate => $cidr->addr);
|
---|
477 | $cidr--; $cidr--;
|
---|
478 | $page->param(bcast => $cidr->addr);
|
---|
479 | $page->param(mask => $cidr->mask);
|
---|
480 |
|
---|
481 | # Snag pool info for heading
|
---|
482 | $sth = $ip_dbh->prepare("select type,city from allocations where cidr=?");
|
---|
483 | $sth->execute($webvar{pool});
|
---|
484 | my ($pooltype, $poolcity) = $sth->fetchrow_array;
|
---|
485 |
|
---|
486 | $page->param(disptype => $disp_alloctypes{$pooltype});
|
---|
487 | $page->param(city => $poolcity);
|
---|
488 |
|
---|
489 | # print qq(<center><div class="heading">Listing pool IPs for $cidr<br>\n).
|
---|
490 | # qq(($disp_alloctypes{$pooltype} in $poolcity)</div></center><br>\n);
|
---|
491 | # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
|
---|
492 | $page->param(realblock => $pooltype =~ /^.d$/);
|
---|
493 |
|
---|
494 | # probably have to add an "edit IP allocation" link here somewhere.
|
---|
495 |
|
---|
496 | $sth = $ip_dbh->prepare("select ip,custid,available,description,type".
|
---|
497 | " from poolips where pool='$webvar{pool}' order by ip");
|
---|
498 | $sth->execute;
|
---|
499 | my @poolips;
|
---|
500 | my $rowclass = 0;
|
---|
501 | while (my ($ip,$custid,$available,$desc,$type) = $sth->fetchrow_array) {
|
---|
502 | my %row = (
|
---|
503 | rowclass => $rowclass++ % 2,
|
---|
504 | ip => $ip,
|
---|
505 | custid => $custid,
|
---|
506 | available => $available,
|
---|
507 | desc => $desc,
|
---|
508 | maydel => $IPDBacl{$authuser} =~ /d/,
|
---|
509 | delme => $available eq 'n'
|
---|
510 | );
|
---|
511 | push @poolips, \%row;
|
---|
512 | }
|
---|
513 | $page->param(poolips => \@poolips);
|
---|
514 |
|
---|
515 | } # end listPool
|
---|
516 |
|
---|
517 |
|
---|
518 | # Show "Add new allocation" page. Note that the actual page may
|
---|
519 | # be one of two templates, and the lists come from the database.
|
---|
520 | sub assignBlock {
|
---|
521 |
|
---|
522 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
523 | printError("You shouldn't have been able to get here. Access denied.");
|
---|
524 | return;
|
---|
525 | }
|
---|
526 |
|
---|
527 | # hack pthbttt eww
|
---|
528 | $webvar{block} = '' if !$webvar{block};
|
---|
529 |
|
---|
530 | # hmm. TMPL_IF block and TMPL_ELSE block on these instead?
|
---|
531 | $page->param(rowa => 'row'.($webvar{block} eq '' ? 1 : 0));
|
---|
532 | $page->param(rowb => 'row'.($webvar{block} eq '' ? 0 : 1));
|
---|
533 | $page->param(block => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
|
---|
534 | $page->param(iscontained => ($webvar{fbtype} && $webvar{fbtype} ne 'y'));
|
---|
535 |
|
---|
536 | # New special case- block to assign is specified
|
---|
537 | if ($webvar{block} ne '') {
|
---|
538 | my $block = new NetAddr::IP $webvar{block};
|
---|
539 |
|
---|
540 | # Handle contained freeblock allocation.
|
---|
541 | # This is a little dangerous, as it's *theoretically* possible to
|
---|
542 | # get fbtype='n' (aka a non-routed freeblock). However, should
|
---|
543 | # someone manage to get there, they get what they deserve.
|
---|
544 | if ($webvar{fbtype} ne 'y') {
|
---|
545 | # Snag the type of the container block from the database.
|
---|
546 | $sth = $ip_dbh->prepare("select type from allocations where cidr >>='$block'");
|
---|
547 | $sth->execute;
|
---|
548 | my @data = $sth->fetchrow_array;
|
---|
549 | $data[0] =~ s/c$/r/; # Munge the type into the correct form
|
---|
550 | $page->param(fbdisptype => $list_alloctypes{$data[0]});
|
---|
551 | $page->param(type => $data[0]);
|
---|
552 | } else {
|
---|
553 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 500 ".
|
---|
554 | "and type not like '_i' and type not like '_r' order by listorder");
|
---|
555 | $sth->execute;
|
---|
556 | my @data = $sth->fetchrow_array;
|
---|
557 | my @typelist;
|
---|
558 | my $selflag = 0;
|
---|
559 | while (my @data = $sth->fetchrow_array) {
|
---|
560 | my %row = (tval => $data[0],
|
---|
561 | type => $data[1],
|
---|
562 | sel => ($selflag == 0 ? ' selected' : '')
|
---|
563 | );
|
---|
564 | push (@typelist, \%row);
|
---|
565 | $selflag++;
|
---|
566 | }
|
---|
567 | $page->param(typelist => \@typelist);
|
---|
568 | }
|
---|
569 | } else {
|
---|
570 | my @masterlist;
|
---|
571 | foreach my $master (@masterblocks) {
|
---|
572 | my %row = (master => "$master");
|
---|
573 | push (@masterlist, \%row);
|
---|
574 | }
|
---|
575 | $page->param(masterlist => \@masterlist);
|
---|
576 |
|
---|
577 | my @pops;
|
---|
578 | foreach my $pop (@poplist) {
|
---|
579 | my %row = (pop => $pop);
|
---|
580 | push (@pops, \%row);
|
---|
581 | }
|
---|
582 | $page->param(pops => \@pops);
|
---|
583 |
|
---|
584 | # could arguably include routing (500) in the list, but ATM it doesn't
|
---|
585 | # make sense, and in any case that shouldn't be structurally possible here.
|
---|
586 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder <= 500 order by listorder");
|
---|
587 | $sth->execute;
|
---|
588 | my @typelist;
|
---|
589 | my $selflag = 0;
|
---|
590 | while (my @data = $sth->fetchrow_array) {
|
---|
591 | my %row = (tval => $data[0],
|
---|
592 | type => $data[1],
|
---|
593 | sel => ($selflag == 0 ? ' selected' : '')
|
---|
594 | );
|
---|
595 | push (@typelist, \%row);
|
---|
596 | $selflag++;
|
---|
597 | }
|
---|
598 | $page->param(typelist => \@typelist);
|
---|
599 | }
|
---|
600 |
|
---|
601 | my @cities;
|
---|
602 | foreach my $city (@citylist) {
|
---|
603 | my %row = (city => $city);
|
---|
604 | push (@cities, \%row);
|
---|
605 | }
|
---|
606 | $page->param(citylist => \@cities);
|
---|
607 |
|
---|
608 | ## node hack
|
---|
609 | $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
|
---|
610 | $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n";
|
---|
611 | my @nodes;
|
---|
612 | while (my ($nid,$nname) = $sth->fetchrow_array()) {
|
---|
613 | my %row = (nid => $nid, nname => $nname);
|
---|
614 | push (@nodes, \%row);
|
---|
615 | }
|
---|
616 | $page->param(nodelist => \@nodes);
|
---|
617 | ## end node hack
|
---|
618 |
|
---|
619 | $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
|
---|
620 |
|
---|
621 | } # assignBlock
|
---|
622 |
|
---|
623 |
|
---|
624 | # Take info on requested IP assignment and see what we can provide.
|
---|
625 | sub confirmAssign {
|
---|
626 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
627 | printError("You shouldn't have been able to get here. Access denied.");
|
---|
628 | return;
|
---|
629 | }
|
---|
630 |
|
---|
631 | my $cidr;
|
---|
632 | my $alloc_from;
|
---|
633 |
|
---|
634 | # Going to manually validate some items.
|
---|
635 | # custid and city are automagic.
|
---|
636 | return if !validateInput();
|
---|
637 |
|
---|
638 | # Several different cases here.
|
---|
639 | # Static IP vs netblock
|
---|
640 | # + Different flavours of static IP
|
---|
641 | # + Different flavours of netblock
|
---|
642 |
|
---|
643 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
644 | my ($base,undef) = split //, $webvar{alloctype}; # split into individual chars
|
---|
645 |
|
---|
646 | # Ewww. But it works.
|
---|
647 | $sth = $ip_dbh->prepare("SELECT (SELECT city FROM allocations WHERE cidr=poolips.pool), ".
|
---|
648 | "poolips.pool, COUNT(*) FROM poolips,allocations WHERE poolips.available='y' AND ".
|
---|
649 | "poolips.pool=allocations.cidr AND allocations.city='$webvar{pop}' AND poolips.type LIKE '".$base."_' ".
|
---|
650 | "GROUP BY pool");
|
---|
651 | $sth->execute;
|
---|
652 | my $optionlist;
|
---|
653 |
|
---|
654 | my @poollist;
|
---|
655 | while (my ($poolcit,$poolblock,$poolfree) = $sth->fetchrow_array) {
|
---|
656 | # city,pool cidr,free IP count
|
---|
657 | if ($poolfree > 0) {
|
---|
658 | my %row = (poolcit => $poolcit, poolblock => $poolblock, poolfree => $poolfree);
|
---|
659 | push (@poollist, \%row);
|
---|
660 | }
|
---|
661 | }
|
---|
662 | $page->param(staticip => 1);
|
---|
663 | $page->param(poollist => \@poollist);
|
---|
664 | $cidr = "Single static IP";
|
---|
665 | ##fixme: need to handle "no available pools"
|
---|
666 |
|
---|
667 | } else { # end show pool options
|
---|
668 |
|
---|
669 | if ($webvar{fbassign} eq 'y') {
|
---|
670 | $cidr = new NetAddr::IP $webvar{block};
|
---|
671 | $webvar{maskbits} = $cidr->masklen;
|
---|
672 | } else { # done with direct freeblocks assignment
|
---|
673 |
|
---|
674 | if (!$webvar{maskbits}) {
|
---|
675 | $page->param(err => "Please specify a CIDR mask length.");
|
---|
676 | return;
|
---|
677 | }
|
---|
678 | my $sql;
|
---|
679 | my $city;
|
---|
680 | my $failmsg;
|
---|
681 | my $extracond = '';
|
---|
682 | if ($webvar{allocfrom} eq '-') {
|
---|
683 | $extracond = ($webvar{allowpriv} eq 'on' ? '' :
|
---|
684 | " and not (cidr <<= '192.168.0.0/16'".
|
---|
685 | " or cidr <<= '10.0.0.0/8'".
|
---|
686 | " or cidr <<= '172.16.0.0/12')");
|
---|
687 | }
|
---|
688 | my $sortorder;
|
---|
689 | if ($webvar{alloctype} eq 'rm') {
|
---|
690 | if ($webvar{allocfrom} ne '-') {
|
---|
691 | $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
|
---|
692 | " and cidr <<= '$webvar{allocfrom}'";
|
---|
693 | $sortorder = "maskbits desc";
|
---|
694 | } else {
|
---|
695 | $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'";
|
---|
696 | $sortorder = "maskbits desc";
|
---|
697 | }
|
---|
698 | $failmsg = "No suitable free block found.<br>\nWe do not have a free".
|
---|
699 | " routeable block of that size.<br>\nYou will have to either route".
|
---|
700 | " a set of smaller netblocks or a single smaller netblock.";
|
---|
701 | } else {
|
---|
702 | ##fixme
|
---|
703 | # This section needs serious Pondering.
|
---|
704 | # Pools of most types get assigned to the POP they're "routed from"
|
---|
705 | # This includes WAN blocks and other netblock "containers"
|
---|
706 | # This does NOT include cable pools.
|
---|
707 | if ($webvar{alloctype} =~ /^.[pc]$/) {
|
---|
708 | $city = $webvar{city};
|
---|
709 | $failmsg = "No suitable free block found.<br>\nYou will have to route another".
|
---|
710 | " superblock from one of the<br>\nmaster blocks or chose a smaller".
|
---|
711 | " block size for the pool.";
|
---|
712 | } else {
|
---|
713 | $city = $webvar{pop};
|
---|
714 | $failmsg = "No suitable free block found.<br>\nYou will have to route another".
|
---|
715 | " superblock to $webvar{pop}<br>\nfrom one of the master blocks or".
|
---|
716 | " chose a smaller blocksize.";
|
---|
717 | }
|
---|
718 | if (defined $webvar{allocfrom} && $webvar{allocfrom} ne '-') {
|
---|
719 | $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
|
---|
720 | " and cidr <<= '$webvar{allocfrom}' and routed='".
|
---|
721 | (($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
|
---|
722 | $sortorder = "maskbits desc,cidr";
|
---|
723 | } else {
|
---|
724 | $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
|
---|
725 | " and routed='".(($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
|
---|
726 | $sortorder = "maskbits desc,cidr";
|
---|
727 | }
|
---|
728 | }
|
---|
729 | $sql = $sql.$extracond." order by ".$sortorder;
|
---|
730 | $sth = $ip_dbh->prepare($sql);
|
---|
731 | $sth->execute;
|
---|
732 | my @data = $sth->fetchrow_array();
|
---|
733 | if ($data[0] eq "") {
|
---|
734 | $page->param(err => $failmsg);
|
---|
735 | return;
|
---|
736 | }
|
---|
737 | $cidr = new NetAddr::IP $data[0];
|
---|
738 | } # check for freeblocks assignment or IPDB-controlled assignment
|
---|
739 |
|
---|
740 | $alloc_from = "$cidr";
|
---|
741 |
|
---|
742 | # If the block to be allocated is smaller than the one we found,
|
---|
743 | # figure out the "real" block to be allocated.
|
---|
744 | if ($cidr->masklen() ne $webvar{maskbits}) {
|
---|
745 | my $maskbits = $cidr->masklen();
|
---|
746 | my @subblocks;
|
---|
747 | while ($maskbits++ < $webvar{maskbits}) {
|
---|
748 | @subblocks = $cidr->split($maskbits);
|
---|
749 | }
|
---|
750 | $cidr = $subblocks[0];
|
---|
751 | }
|
---|
752 | } # if ($webvar{alloctype} =~ /^.i$/)
|
---|
753 |
|
---|
754 | ## node hack
|
---|
755 | if ($webvar{node} && $webvar{node} ne '-') {
|
---|
756 | $sth = $ip_dbh->prepare("SELECT node_name FROM nodes WHERE node_id=?");
|
---|
757 | $sth->execute($webvar{node});
|
---|
758 | my ($nodename) = $sth->fetchrow_array();
|
---|
759 | $page->param(nodename => $nodename);
|
---|
760 | $page->param(nodeid => $webvar{node});
|
---|
761 | }
|
---|
762 | ## end node hack
|
---|
763 |
|
---|
764 | # Stick in the allocation data
|
---|
765 | $page->param(alloc_type => $webvar{alloctype});
|
---|
766 | $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
|
---|
767 | $page->param(alloc_from => $alloc_from);
|
---|
768 | $page->param(cidr => $cidr);
|
---|
769 | $page->param(city => $q->escapeHTML($webvar{city}));
|
---|
770 | $page->param(custid => $webvar{custid});
|
---|
771 | $page->param(circid => $q->escapeHTML($webvar{circid}));
|
---|
772 | $page->param(desc => $q->escapeHTML($webvar{desc}));
|
---|
773 |
|
---|
774 | ##fixme: find a way to have the displayed copy have <br> substitutions
|
---|
775 | # for newlines, and the <input> value have either encoded or bare newlines.
|
---|
776 | # Also applies to privdata.
|
---|
777 | $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
|
---|
778 |
|
---|
779 | # Check to see if user is allowed to do anything with sensitive data
|
---|
780 | my $privdata = '';
|
---|
781 | $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'))
|
---|
782 | if $IPDBacl{$authuser} =~ /s/;
|
---|
783 |
|
---|
784 | # Yay! This now has it's very own little home.
|
---|
785 | $page->param(billinguser => $webvar{userid})
|
---|
786 | if $webvar{userid};
|
---|
787 |
|
---|
788 | ##fixme: this is only needed iff confirm.tmpl/confirm.html and
|
---|
789 | # confirmRemove.html/confirmRemove.tmpl are merged (quite possible, just
|
---|
790 | # a little tedious)
|
---|
791 | $page->param(action => "insert");
|
---|
792 |
|
---|
793 | } # end confirmAssign
|
---|
794 |
|
---|
795 |
|
---|
796 | # Do the work of actually inserting a block in the database.
|
---|
797 | sub insertAssign {
|
---|
798 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
799 | printError("You shouldn't have been able to get here. Access denied.");
|
---|
800 | return;
|
---|
801 | }
|
---|
802 | # Some things are done more than once.
|
---|
803 | return if !validateInput();
|
---|
804 |
|
---|
805 | if (!defined($webvar{privdata})) {
|
---|
806 | $webvar{privdata} = '';
|
---|
807 | }
|
---|
808 | # $code is "success" vs "failure", $msg contains OK for a
|
---|
809 | # successful netblock allocation, the IP allocated for static
|
---|
810 | # IP, or the error message if an error occurred.
|
---|
811 |
|
---|
812 | my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
|
---|
813 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
|
---|
814 | $webvar{circid}, $webvar{privdata}, $webvar{node});
|
---|
815 |
|
---|
816 | if ($code eq 'OK') {
|
---|
817 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
818 | $msg =~ s|/32||;
|
---|
819 | $page->param(staticip => $msg);
|
---|
820 | $page->param(custid => $webvar{custid});
|
---|
821 | $page->param(billinguser => $webvar{billinguser});
|
---|
822 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
823 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
|
---|
824 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
825 | } else {
|
---|
826 | my $netblock = new NetAddr::IP $webvar{fullcidr};
|
---|
827 | $page->param(fullcidr => $webvar{fullcidr});
|
---|
828 | $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
|
---|
829 | $page->param(custid => $webvar{custid});
|
---|
830 | if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
|
---|
831 | $page->param(billinguser => $webvar{billinguser});
|
---|
832 | $page->param(custid => $webvar{custid});
|
---|
833 | $page->param(netaddr => $netblock->addr);
|
---|
834 | $page->param(masklen => $netblock->masklen);
|
---|
835 | }
|
---|
836 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
837 | "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
|
---|
838 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
839 | }
|
---|
840 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
841 | "'$webvar{alloctype}' ($msg)";
|
---|
842 | } else {
|
---|
843 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
844 | "'$webvar{alloctype}' by $authuser failed: '$msg'";
|
---|
845 | $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
|
---|
846 | " failed:<br>\n$msg\n");
|
---|
847 | }
|
---|
848 |
|
---|
849 | } # end insertAssign()
|
---|
850 |
|
---|
851 |
|
---|
852 | # Does some basic checks on common input data to make sure nothing
|
---|
853 | # *really* weird gets in to the database through this script.
|
---|
854 | # Does NOT do complete input validation!!!
|
---|
855 | sub validateInput {
|
---|
856 | if ($webvar{city} eq '-') {
|
---|
857 | printError("Please choose a city.");
|
---|
858 | return;
|
---|
859 | }
|
---|
860 |
|
---|
861 | # Alloctype check.
|
---|
862 | chomp $webvar{alloctype};
|
---|
863 | if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
|
---|
864 | # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
|
---|
865 | # managing to call things in such a way as to cause this deserves a cryptic error.
|
---|
866 | printError("Invalid alloctype");
|
---|
867 | return;
|
---|
868 | }
|
---|
869 |
|
---|
870 | # CustID check
|
---|
871 | # We have different handling for customer allocations and "internal" or "our" allocations
|
---|
872 | if ($def_custids{$webvar{alloctype}} eq '') {
|
---|
873 | if (!$webvar{custid}) {
|
---|
874 | printError("Please enter a customer ID.");
|
---|
875 | return;
|
---|
876 | }
|
---|
877 | if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
|
---|
878 | # Force uppercase for now...
|
---|
879 | $webvar{custid} =~ tr/a-z/A-Z/;
|
---|
880 | # Crosscheck with billing.
|
---|
881 | my $status = CustIDCK->custid_exist($webvar{custid});
|
---|
882 | if ($CustIDCK::Error) {
|
---|
883 | printError("Error verifying customer ID: ".$CustIDCK::ErrMsg);
|
---|
884 | return;
|
---|
885 | }
|
---|
886 | if (!$status) {
|
---|
887 | printError("Customer ID not valid. Make sure the Customer ID ".
|
---|
888 | "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
|
---|
889 | "non-customer assignments.");
|
---|
890 | return;
|
---|
891 | }
|
---|
892 | }
|
---|
893 | # print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
|
---|
894 | } else {
|
---|
895 | # New! Improved! And now Loaded From The Database!!
|
---|
896 | if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
|
---|
897 | $webvar{custid} = $def_custids{$webvar{alloctype}};
|
---|
898 | }
|
---|
899 | }
|
---|
900 |
|
---|
901 | # Check POP location
|
---|
902 | my $flag;
|
---|
903 | if ($webvar{alloctype} eq 'rm') {
|
---|
904 | $flag = 'for a routed netblock';
|
---|
905 | foreach (@poplist) {
|
---|
906 | if (/^$webvar{city}$/) {
|
---|
907 | $flag = 'n';
|
---|
908 | last;
|
---|
909 | }
|
---|
910 | }
|
---|
911 | } else {
|
---|
912 | $flag = 'n';
|
---|
913 | ##fixme: hook to force-set POP or city on certain alloctypes
|
---|
914 | # if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
|
---|
915 | if ($webvar{pop} =~ /^-$/) {
|
---|
916 | $flag = 'to route the block from/through';
|
---|
917 | }
|
---|
918 | }
|
---|
919 | if ($flag ne 'n') {
|
---|
920 | printError("Please choose a valid POP location $flag. Valid ".
|
---|
921 | "POP locations are currently:<br>\n".join (" - ", @poplist));
|
---|
922 | return;
|
---|
923 | }
|
---|
924 |
|
---|
925 | return 'OK';
|
---|
926 | } # end validateInput
|
---|
927 |
|
---|
928 |
|
---|
929 | # Displays details of a specific allocation in a form
|
---|
930 | # Allows update/delete
|
---|
931 | # action=edit
|
---|
932 | sub edit {
|
---|
933 |
|
---|
934 | my $sql;
|
---|
935 |
|
---|
936 | # Two cases: block is a netblock, or block is a static IP from a pool
|
---|
937 | # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
|
---|
938 | ##fixme: allow "SWIP" (publication to rWHOIS) of static IP data
|
---|
939 | if ($webvar{block} =~ /\/32$/) {
|
---|
940 | $sql = "select ip,custid,type,city,circuitid,description,notes,modifystamp,privdata from poolips where ip='$webvar{block}'";
|
---|
941 | } else {
|
---|
942 | $sql = "select cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,swip from allocations where cidr='$webvar{block}'"
|
---|
943 | }
|
---|
944 |
|
---|
945 | # gotta snag block info from db
|
---|
946 | $sth = $ip_dbh->prepare($sql);
|
---|
947 | $sth->execute;
|
---|
948 | my @data = $sth->fetchrow_array;
|
---|
949 |
|
---|
950 | # Clean up extra whitespace on alloc type
|
---|
951 | $data[2] =~ s/\s//;
|
---|
952 |
|
---|
953 | # We can't let the city be changed here; this block is a part of
|
---|
954 | # a larger routed allocation and therefore by definition can't be moved.
|
---|
955 | # block and city are static.
|
---|
956 | ##fixme
|
---|
957 | # Needs thinking. Have to allow changes to city to correct errors, no?
|
---|
958 | # Also have areas where a routed block at a POP serves "many" cities/towns/named crossroads
|
---|
959 |
|
---|
960 | # @data: cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,swip
|
---|
961 |
|
---|
962 | $page->param(block => $webvar{block});
|
---|
963 |
|
---|
964 | $page->param(custid => $data[1]);
|
---|
965 | $page->param(city => $data[3]);
|
---|
966 | $page->param(circid => $data[4]);
|
---|
967 | $page->param(desc => $data[5]);
|
---|
968 | $page->param(notes => $data[6]);
|
---|
969 |
|
---|
970 | ##fixme The check here should be built from the database
|
---|
971 | # Need to expand to support pool types too
|
---|
972 | if ($data[2] =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
|
---|
973 | $page->param(changetype => 1);
|
---|
974 | $page->param(alloctype => [
|
---|
975 | { selme => ($data[2] eq 'me'), type => "me", disptype => "Dialup netblock" },
|
---|
976 | { selme => ($data[2] eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
|
---|
977 | { selme => ($data[2] eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
|
---|
978 | { selme => ($data[2] eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
|
---|
979 | { selme => ($data[2] eq 'cn'), type => "cn", disptype => "Customer netblock" },
|
---|
980 | { selme => ($data[2] eq 'en'), type => "en", disptype => "End-use netblock" },
|
---|
981 | { selme => ($data[2] eq 'in'), type => "in", disptype => "Internal netblock" },
|
---|
982 | ]
|
---|
983 | );
|
---|
984 | } else {
|
---|
985 | $page->param(disptype => $disp_alloctypes{$data[2]});
|
---|
986 | $page->param(type => $data[2]);
|
---|
987 | }
|
---|
988 |
|
---|
989 | ## node hack
|
---|
990 | $sth = $ip_dbh->prepare("SELECT nodes.node_id,node_name FROM nodes INNER JOIN noderef".
|
---|
991 | " ON nodes.node_id=noderef.node_id WHERE noderef.block='$webvar{block}'");
|
---|
992 | $sth->execute;
|
---|
993 | my ($nodeid,$nodename) = $sth->fetchrow_array();
|
---|
994 | $page->param(havenodeid => $nodeid);
|
---|
995 |
|
---|
996 | if ($data[2] eq 'fr' || $data[2] eq 'bi') {
|
---|
997 | $page->param(typesupportsnodes => 1);
|
---|
998 | $page->param(nodename => $nodename);
|
---|
999 |
|
---|
1000 | ##fixme: this whole hack needs cleanup and generalization for all alloctypes
|
---|
1001 | ##fixme: arguably a bug that presence of a nodeid implies it can be changed..
|
---|
1002 | # but except for manual database changes, only the two types fr and bi can
|
---|
1003 | # (currently) have a nodeid set in the first place.
|
---|
1004 | if ($IPDBacl{$authuser} =~ /c/) {
|
---|
1005 | $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
|
---|
1006 | $sth->execute;
|
---|
1007 | my @nodelist;
|
---|
1008 | while (my ($nid,$nname) = $sth->fetchrow_array()) {
|
---|
1009 | my %row = (
|
---|
1010 | selme => ($nodeid == $nid),
|
---|
1011 | nodeid => $nid,
|
---|
1012 | nodename => $nname,
|
---|
1013 | );
|
---|
1014 | push (@nodelist, \%row);
|
---|
1015 | }
|
---|
1016 | $page->param(nodelist => \@nodelist);
|
---|
1017 | }
|
---|
1018 | }
|
---|
1019 | ## end node hack
|
---|
1020 |
|
---|
1021 | my ($lastmod,undef) = split /\s+/, $data[7];
|
---|
1022 | $page->param(lastmod => $lastmod);
|
---|
1023 |
|
---|
1024 | # not happy with the upside-down logic, but...
|
---|
1025 | $page->param(swipable => $data[2] !~ /.i/);
|
---|
1026 | $page->param(swip => $data[10] ne 'n');
|
---|
1027 |
|
---|
1028 | # Check to see if we can display sensitive data
|
---|
1029 | $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
|
---|
1030 | $page->param(privdata => $data[8]);
|
---|
1031 |
|
---|
1032 | # ACL trickery - these two template booleans control the presence of all form/input tags
|
---|
1033 | $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
|
---|
1034 | $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
|
---|
1035 |
|
---|
1036 | } # edit()
|
---|
1037 |
|
---|
1038 |
|
---|
1039 | # Stuff new info about a block into the db
|
---|
1040 | # action=update
|
---|
1041 | sub update {
|
---|
1042 | if ($IPDBacl{$authuser} !~ /c/) {
|
---|
1043 | printError("You shouldn't have been able to get here. Access denied.");
|
---|
1044 | return;
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | # Check to see if we can update restricted data
|
---|
1048 | my $privdata = '';
|
---|
1049 | if ($IPDBacl{$authuser} =~ /s/) {
|
---|
1050 | $privdata = ",privdata='$webvar{privdata}'";
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | # Make sure incoming data is in correct format - custID among other things.
|
---|
1054 | return if !validateInput;
|
---|
1055 |
|
---|
1056 | # SQL transaction wrapper
|
---|
1057 | eval {
|
---|
1058 | # Relatively simple SQL transaction here.
|
---|
1059 | my $sql;
|
---|
1060 | if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
|
---|
1061 | $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',".
|
---|
1062 | "circuitid='$webvar{circid}',description='$webvar{desc}',city='$webvar{city}'".
|
---|
1063 | "$privdata where ip='$webvar{block}'";
|
---|
1064 | } else {
|
---|
1065 | $sql = "update allocations set custid='$webvar{custid}',".
|
---|
1066 | "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',".
|
---|
1067 | "type='$webvar{alloctype}',circuitid='$webvar{circid}'$privdata,".
|
---|
1068 | "swip='".($webvar{swip} eq 'on' ? 'y' : 'n')."' ".
|
---|
1069 | "where cidr='$webvar{block}'";
|
---|
1070 | }
|
---|
1071 | # Log the details of the change.
|
---|
1072 | syslog "debug", $sql;
|
---|
1073 | $sth = $ip_dbh->prepare($sql);
|
---|
1074 | $sth->execute;
|
---|
1075 | ## node hack
|
---|
1076 | if ($webvar{node}) {
|
---|
1077 | $ip_dbh->do("DELETE FROM noderef WHERE block='$webvar{block}'");
|
---|
1078 | $sth = $ip_dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)");
|
---|
1079 | $sth->execute($webvar{block},$webvar{node});
|
---|
1080 | }
|
---|
1081 | ## end node hack
|
---|
1082 | $ip_dbh->commit;
|
---|
1083 | };
|
---|
1084 | if ($@) {
|
---|
1085 | my $msg = $@;
|
---|
1086 | carp "Transaction aborted because $msg";
|
---|
1087 | eval { $ip_dbh->rollback; };
|
---|
1088 | syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
|
---|
1089 | printError("Could not update block/IP $webvar{block}: $msg");
|
---|
1090 | return;
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | # If we get here, the operation succeeded.
|
---|
1094 | syslog "notice", "$authuser updated $webvar{block}";
|
---|
1095 | ##fixme: need to wedge something in to allow "update:field" notifications
|
---|
1096 | ## hmm. how to tell what changed? O_o
|
---|
1097 | mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
|
---|
1098 | "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
|
---|
1099 |
|
---|
1100 | ## node hack
|
---|
1101 | if ($webvar{node} && $webvar{node} ne '-') {
|
---|
1102 | $sth = $ip_dbh->prepare("SELECT node_name FROM nodes WHERE node_id=?");
|
---|
1103 | $sth->execute($webvar{node});
|
---|
1104 | my ($nodename) = $sth->fetchrow_array();
|
---|
1105 | $page->param(nodename => $nodename);
|
---|
1106 | }
|
---|
1107 | ## end node hack
|
---|
1108 |
|
---|
1109 | # Link back to browse-routed or list-pool page on "Update complete" page.
|
---|
1110 | my $cblock; # to contain the CIDR of the container block we're retrieving.
|
---|
1111 | my $sql;
|
---|
1112 | if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
|
---|
1113 | $page->param(backpool => 1);
|
---|
1114 | $sql = "select pool from poolips where ip='$webvar{block}'";
|
---|
1115 | } else {
|
---|
1116 | $sql = "select cidr from routed where cidr >>= '$webvar{block}'";
|
---|
1117 | }
|
---|
1118 | # I define there to be no errors on this operation... so we don't need to check for them.
|
---|
1119 | $sth = $ip_dbh->prepare($sql);
|
---|
1120 | $sth->execute;
|
---|
1121 | $sth->bind_columns(\$cblock);
|
---|
1122 | $sth->fetch();
|
---|
1123 | $sth->finish;
|
---|
1124 | $page->param(backblock => $cblock);
|
---|
1125 |
|
---|
1126 | $page->param(cidr => $webvar{block});
|
---|
1127 | $page->param(city => $webvar{city});
|
---|
1128 | $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
|
---|
1129 | $page->param(custid => $webvar{custid});
|
---|
1130 | $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
|
---|
1131 | $page->param(circid => $q->escapeHTML($webvar{circid}));
|
---|
1132 | $page->param(desc => $q->escapeHTML($webvar{desc}));
|
---|
1133 | $page->param(notes => $q->escapeHTML($webvar{notes}));
|
---|
1134 | $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : " ");
|
---|
1135 | $page->param(privdata => $webvar{privdata})
|
---|
1136 | if $IPDBacl{$authuser} =~ /s/;
|
---|
1137 |
|
---|
1138 | } # update()
|
---|
1139 |
|
---|
1140 |
|
---|
1141 | # Delete an allocation.
|
---|
1142 | sub remove {
|
---|
1143 | if ($IPDBacl{$authuser} !~ /d/) {
|
---|
1144 | printError("You shouldn't have been able to get here. Access denied.");
|
---|
1145 | return;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | # Serves'em right for getting here...
|
---|
1149 | if (!defined($webvar{block})) {
|
---|
1150 | printError("Error 332");
|
---|
1151 | return;
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata);
|
---|
1155 |
|
---|
1156 | if ($webvar{alloctype} eq 'rm') {
|
---|
1157 | $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
|
---|
1158 | $sth->execute();
|
---|
1159 |
|
---|
1160 | # This feels... extreme.
|
---|
1161 | croak $sth->errstr() if($sth->errstr());
|
---|
1162 |
|
---|
1163 | $sth->bind_columns(\$cidr,\$city);
|
---|
1164 | $sth->execute();
|
---|
1165 | $sth->fetch || croak $sth->errstr();
|
---|
1166 | $custid = "N/A";
|
---|
1167 | $alloctype = $webvar{alloctype};
|
---|
1168 | $circid = "N/A";
|
---|
1169 | $desc = "N/A";
|
---|
1170 | $notes = "N/A";
|
---|
1171 | $privdata = "N/A";
|
---|
1172 |
|
---|
1173 | } elsif ($webvar{alloctype} eq 'mm') {
|
---|
1174 |
|
---|
1175 | $cidr = $webvar{block};
|
---|
1176 | $city = "N/A";
|
---|
1177 | $custid = "N/A";
|
---|
1178 | $alloctype = $webvar{alloctype};
|
---|
1179 | $circid = "N/A";
|
---|
1180 | $desc = "N/A";
|
---|
1181 | $notes = "N/A";
|
---|
1182 | $privdata = "N/A";
|
---|
1183 |
|
---|
1184 | } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m
|
---|
1185 |
|
---|
1186 | # Unassigning a static IP
|
---|
1187 | my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata".
|
---|
1188 | " from poolips where ip='$webvar{block}'");
|
---|
1189 | $sth->execute();
|
---|
1190 | # croak $sth->errstr() if($sth->errstr());
|
---|
1191 |
|
---|
1192 | $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid,
|
---|
1193 | \$privdata);
|
---|
1194 | $sth->fetch() || croak $sth->errstr;
|
---|
1195 |
|
---|
1196 | } else { # done with alloctype=~ /^.i$/
|
---|
1197 |
|
---|
1198 | my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata".
|
---|
1199 | " from allocations where cidr='$webvar{block}'");
|
---|
1200 | $sth->execute();
|
---|
1201 | # croak $sth->errstr() if($sth->errstr());
|
---|
1202 |
|
---|
1203 | $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc,
|
---|
1204 | \$notes, \$privdata);
|
---|
1205 | $sth->fetch() || carp $sth->errstr;
|
---|
1206 | } # end cases for different alloctypes
|
---|
1207 |
|
---|
1208 | $page->param(block => $cidr);
|
---|
1209 | $page->param(disptype => $disp_alloctypes{$alloctype});
|
---|
1210 | $page->param(type => $alloctype);
|
---|
1211 | $page->param(city => $city);
|
---|
1212 | $page->param(custid => $custid);
|
---|
1213 | $page->param(circid => $circid);
|
---|
1214 | $page->param(desc => $desc);
|
---|
1215 | $page->param(notes => $notes);
|
---|
1216 | $privdata = ' ' if $privdata eq '';
|
---|
1217 | $page->param(privdata => $privdata) if $IPDBacl{$authuser} =~ /s/;
|
---|
1218 | $page->param(delpool => $alloctype =~ /^.[pd]$/);
|
---|
1219 |
|
---|
1220 | } # end remove()
|
---|
1221 |
|
---|
1222 |
|
---|
1223 | # Delete an allocation. Return it to the freeblocks table; munge
|
---|
1224 | # data as necessary to keep as few records as possible in freeblocks
|
---|
1225 | # to prevent weirdness when allocating blocks later.
|
---|
1226 | # Remove IPs from pool listing if necessary
|
---|
1227 | sub finalDelete {
|
---|
1228 | if ($IPDBacl{$authuser} !~ /d/) {
|
---|
1229 | $page->param(aclerr => 1);
|
---|
1230 | return;
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | # need to retrieve block data before deleting so we can notify on that
|
---|
1234 | my ($cidr,$custid,$type,$city,$description) = getBlockData($ip_dbh, $webvar{block});
|
---|
1235 |
|
---|
1236 | my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
|
---|
1237 |
|
---|
1238 | $page->param(block => $webvar{block});
|
---|
1239 | if ($code eq 'OK') {
|
---|
1240 | syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}".
|
---|
1241 | " $custid, $city, desc='$description'";
|
---|
1242 | mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
|
---|
1243 | "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
|
---|
1244 | "CustID: $custid\nCity: $city\nDescription: $description\n");
|
---|
1245 | } else {
|
---|
1246 | $page->param(failmsg => $msg);
|
---|
1247 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
1248 | syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
|
---|
1249 | } else {
|
---|
1250 | syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
|
---|
1251 | $page->param(netblock => 1);
|
---|
1252 | }
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | } # finalDelete
|
---|
1256 |
|
---|
1257 |
|
---|
1258 | # going, going, gone... this sub to be removed Any Day Real Soon Now(TM)
|
---|
1259 | sub exitError {
|
---|
1260 | my $errStr = $_[0];
|
---|
1261 | printHeader('','');
|
---|
1262 | print qq(<center><p class="regular"> $errStr </p>
|
---|
1263 | <input type="button" value="Back" onclick="history.go(-1)">
|
---|
1264 | </center>
|
---|
1265 | );
|
---|
1266 |
|
---|
1267 | # We print the footer here, so we don't have to do it elsewhere.
|
---|
1268 | # include the admin tools link in the output?
|
---|
1269 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
|
---|
1270 |
|
---|
1271 | print $footer->output;
|
---|
1272 |
|
---|
1273 | exit;
|
---|
1274 | } # errorExit
|
---|
1275 |
|
---|
1276 |
|
---|
1277 | # Just in case we manage to get here.
|
---|
1278 | exit 0;
|
---|