1 | #!/usr/bin/perl
|
---|
2 | # ipdb/cgi-bin/main.cgi
|
---|
3 | ###
|
---|
4 | # SVN revision info
|
---|
5 | # $Date: 2012-11-01 20:28:15 +0000 (Thu, 01 Nov 2012) $
|
---|
6 | # SVN revision $Rev: 538 $
|
---|
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 POSIX qw(ceil);
|
---|
18 | use NetAddr::IP;
|
---|
19 |
|
---|
20 | use Sys::Syslog;
|
---|
21 |
|
---|
22 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
23 | ##uselib##
|
---|
24 |
|
---|
25 | use CustIDCK;
|
---|
26 | use MyIPDB;
|
---|
27 |
|
---|
28 | openlog "IPDB","pid","$IPDB::syslog_facility";
|
---|
29 |
|
---|
30 | ## Environment. Collect some things, process some things, set some things...
|
---|
31 |
|
---|
32 | # Collect the username from HTTP auth. If undefined, we're in
|
---|
33 | # a test environment, or called without a username.
|
---|
34 | my $authuser;
|
---|
35 | if (!defined($ENV{'REMOTE_USER'})) {
|
---|
36 | $authuser = '__temptest';
|
---|
37 | } else {
|
---|
38 | $authuser = $ENV{'REMOTE_USER'};
|
---|
39 | }
|
---|
40 |
|
---|
41 | # anyone got a better name? :P
|
---|
42 | my $thingroot = $ENV{SCRIPT_FILENAME};
|
---|
43 | $thingroot =~ s|cgi-bin/main.cgi||;
|
---|
44 |
|
---|
45 | syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
|
---|
46 |
|
---|
47 | ##fixme there *must* be a better order to do things in so this can go back where it was
|
---|
48 | # CGI fiddling done here so we can declare %webvar so we can alter $webvar{action}
|
---|
49 | # to show the right page on DB errors.
|
---|
50 | # Set up the CGI object...
|
---|
51 | my $q = new CGI::Simple;
|
---|
52 | # ... and get query-string params as well as POST params if necessary
|
---|
53 | $q->parse_query_string;
|
---|
54 |
|
---|
55 | # Convenience; saves changing all references to %webvar
|
---|
56 | ##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
|
---|
57 | my %webvar = $q->Vars;
|
---|
58 |
|
---|
59 | # Why not a global DB handle? (And a global statement handle, as well...)
|
---|
60 | # Use the connectDB function, otherwise we end up confusing ourselves
|
---|
61 | my $ip_dbh;
|
---|
62 | my $sth;
|
---|
63 | my $errstr;
|
---|
64 | ($ip_dbh,$errstr) = connectDB_My;
|
---|
65 | if (!$ip_dbh) {
|
---|
66 | $webvar{action} = "dberr";
|
---|
67 | } else {
|
---|
68 | initIPDBGlobals($ip_dbh);
|
---|
69 | }
|
---|
70 |
|
---|
71 | # Set up some globals
|
---|
72 | $ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
|
---|
73 |
|
---|
74 | my $header = HTML::Template->new(filename => "header.tmpl");
|
---|
75 | my $footer = HTML::Template->new(filename => "footer.tmpl");
|
---|
76 |
|
---|
77 | $header->param(version => $IPDB::VERSION);
|
---|
78 | $header->param(addperm => $IPDBacl{$authuser} =~ /a/);
|
---|
79 | $header->param(webpath => $IPDB::webpath);
|
---|
80 | print "Content-type: text/html\n\n", $header->output;
|
---|
81 |
|
---|
82 |
|
---|
83 | #main()
|
---|
84 | my $aclerr;
|
---|
85 |
|
---|
86 | if(!defined($webvar{action})) {
|
---|
87 | $webvar{action} = "index"; #shuts up the warnings.
|
---|
88 | }
|
---|
89 |
|
---|
90 | my $page;
|
---|
91 | if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
|
---|
92 | $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1);
|
---|
93 | } else {
|
---|
94 | $page = HTML::Template->new(filename => "dunno.tmpl");
|
---|
95 | }
|
---|
96 |
|
---|
97 | if($webvar{action} eq 'index') {
|
---|
98 | showSummary();
|
---|
99 | } elsif ($webvar{action} eq 'addmaster') {
|
---|
100 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
101 | $aclerr = 'addmaster';
|
---|
102 | }
|
---|
103 | } elsif ($webvar{action} eq 'newmaster') {
|
---|
104 |
|
---|
105 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
106 | $aclerr = 'addmaster';
|
---|
107 | } else {
|
---|
108 | my $cidr = new NetAddr::IP $webvar{cidr};
|
---|
109 | $page->param(cidr => "$cidr");
|
---|
110 |
|
---|
111 | my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
|
---|
112 |
|
---|
113 | if ($code eq 'FAIL') {
|
---|
114 | syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
|
---|
115 | $page->param(err => $msg);
|
---|
116 | } else {
|
---|
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 | showPool();
|
---|
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 | elsif ($webvar{action} eq 'nodesearch') {
|
---|
157 | my $nodelist = getNodeList($ip_dbh);
|
---|
158 | $page->param(nodelist => $nodelist);
|
---|
159 | }
|
---|
160 |
|
---|
161 | # DB failure. Can't do much here, really.
|
---|
162 | elsif ($webvar{action} eq 'dberr') {
|
---|
163 | $page->param(errmsg => $errstr);
|
---|
164 | }
|
---|
165 |
|
---|
166 | # Default is an error. It shouldn't be possible to get here unless you're
|
---|
167 | # randomly feeding in values for webvar{action}.
|
---|
168 | else {
|
---|
169 | my $rnd = rand 500;
|
---|
170 | my $boing = sprintf("%.2f", rand 500);
|
---|
171 | my @excuses = (
|
---|
172 | "Aether cloudy. Ask again later about $webvar{action}.",
|
---|
173 | "The gods are unhappy with your sacrificial $webvar{action}.",
|
---|
174 | "Because one of $webvar{action}'s legs are both the same",
|
---|
175 | "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!",
|
---|
176 | "Hey, man, you've had your free $webvar{action}. Next one's gonna... <i>cost</i>....",
|
---|
177 | "I ain't done $webvar{action}",
|
---|
178 | "Oooo, look! A flying $webvar{action}!",
|
---|
179 | "$webvar{action} too evil, avoiding.",
|
---|
180 | "Rocks fall, $webvar{action} dies.",
|
---|
181 | "Bit bucket must be emptied before I can $webvar{action}..."
|
---|
182 | );
|
---|
183 | $page->param(dunno => $excuses[$rnd/50.0]);
|
---|
184 | }
|
---|
185 | ## Finally! Done with that NASTY "case" emulation!
|
---|
186 |
|
---|
187 |
|
---|
188 | # Switch to a different template if we've tripped on an ACL error.
|
---|
189 | # Note that this should only be exercised in development, when
|
---|
190 | # deeplinked, or when being attacked; normal ACL handling should
|
---|
191 | # remove the links a user is not allowed to click on.
|
---|
192 | if ($aclerr) {
|
---|
193 | $page = HTML::Template->new(filename => "aclerror.tmpl");
|
---|
194 | $page->param(ipdbfunc => $aclmsg{$aclerr});
|
---|
195 | }
|
---|
196 |
|
---|
197 | $sth->finish if $sth;
|
---|
198 | # Clean up IPDB globals, DB handle, etc.
|
---|
199 | finish($ip_dbh);
|
---|
200 |
|
---|
201 | ## Do all our printing here so we can generate errors and stick them into the slots in the templates.
|
---|
202 |
|
---|
203 | # can't do this yet, too many blowups
|
---|
204 | #print "Content-type: text/html\n\n", $header->output;
|
---|
205 | $page->param(webpath => $IPDB::webpath);
|
---|
206 | print $page->output;
|
---|
207 |
|
---|
208 | # include the admin tools link in the output?
|
---|
209 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
|
---|
210 | $footer->param(webpath => $IPDB::webpath);
|
---|
211 | print $footer->output;
|
---|
212 |
|
---|
213 | # Just in case something waaaayyy down isn't in place
|
---|
214 | # properly... we exit explicitly.
|
---|
215 | exit 0;
|
---|
216 |
|
---|
217 |
|
---|
218 | # Initial display: Show master blocks with total allocated subnets, total free subnets
|
---|
219 | sub showSummary {
|
---|
220 | my $masterlist = listSummary($ip_dbh);
|
---|
221 | $page->param(masterlist => $masterlist);
|
---|
222 |
|
---|
223 | $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
|
---|
224 | } # showSummary
|
---|
225 |
|
---|
226 |
|
---|
227 | # Display detail on master
|
---|
228 | # Alrighty then! We're showing routed blocks within a single master this time.
|
---|
229 | # We should be able to steal code from showSummary(), and if I'm really smart
|
---|
230 | # I'll figger a way to munge the two together. (Once I've done that, everything
|
---|
231 | # else should follow. YMMV.)
|
---|
232 | sub showMaster {
|
---|
233 |
|
---|
234 | $page->param(master => $webvar{block});
|
---|
235 | $page->param(delmaster => ($IPDBacl{$authuser} =~ /d/));
|
---|
236 |
|
---|
237 | my $rlist = listMaster($ip_dbh, $webvar{block});
|
---|
238 | $page->param(routedlist => $rlist);
|
---|
239 |
|
---|
240 | my $flist = listFree($ip_dbh, $webvar{block});
|
---|
241 | $page->param(unrouted => $flist);
|
---|
242 | } # showMaster
|
---|
243 |
|
---|
244 |
|
---|
245 | # Display details of a routed block
|
---|
246 | # Alrighty then! We're showing allocations within a routed block this time.
|
---|
247 | # We should be able to steal code from showSummary() and showMaster(), and if
|
---|
248 | # I'm really smart I'll figger a way to munge all three together. (Once I've
|
---|
249 | # done that, everything else should follow. YMMV.
|
---|
250 | # This time, we check the database before spewing, because we may
|
---|
251 | # not have anything useful to spew.
|
---|
252 | sub showRBlock {
|
---|
253 |
|
---|
254 | $page->param(master => $webvar{block});
|
---|
255 | $page->param(delrouted => $IPDBacl{$authuser} =~ /d/);
|
---|
256 |
|
---|
257 | my $rcity = getRoutedCity($ip_dbh, $webvar{block});
|
---|
258 | $page->param(rcity => $rcity);
|
---|
259 |
|
---|
260 | my $blist = listRBlock($ip_dbh, $webvar{block});
|
---|
261 | $page->param(blocklist => $blist);
|
---|
262 |
|
---|
263 | my $flist = listFree($ip_dbh, $webvar{block}, 'y');
|
---|
264 | $page->param(unassigned => $flist);
|
---|
265 | } # showRBlock
|
---|
266 |
|
---|
267 |
|
---|
268 | # List the IPs used in a pool
|
---|
269 | sub showPool {
|
---|
270 |
|
---|
271 | my $cidr = new NetAddr::IP $webvar{pool};
|
---|
272 |
|
---|
273 | $page->param(block => $webvar{pool});
|
---|
274 | $page->param(netip => $cidr->addr);
|
---|
275 | $cidr++;
|
---|
276 | $page->param(gate => $cidr->addr);
|
---|
277 | $cidr--; $cidr--;
|
---|
278 | $page->param(bcast => $cidr->addr);
|
---|
279 | $page->param(mask => $cidr->mask);
|
---|
280 |
|
---|
281 | # Snag pool info for heading
|
---|
282 | my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
|
---|
283 |
|
---|
284 | $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
|
---|
285 | $page->param(city => $poolinfo->{city});
|
---|
286 |
|
---|
287 | # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
|
---|
288 | $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
|
---|
289 |
|
---|
290 | # probably have to add an "edit IP allocation" link here somewhere.
|
---|
291 |
|
---|
292 | my $plist = listPool($ip_dbh, $webvar{pool});
|
---|
293 | # technically slightly more efficient to check the ACL in an if () once outside the foreach
|
---|
294 | foreach (@{$plist}) {
|
---|
295 | $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
|
---|
296 | }
|
---|
297 | $page->param(poolips => $plist);
|
---|
298 | } # end showPool
|
---|
299 |
|
---|
300 |
|
---|
301 | # Show "Add new allocation" page. Note that the actual page may
|
---|
302 | # be one of two templates, and the lists come from the database.
|
---|
303 | sub assignBlock {
|
---|
304 |
|
---|
305 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
306 | $aclerr = 'addblock';
|
---|
307 | return;
|
---|
308 | }
|
---|
309 |
|
---|
310 | # hack pthbttt eww
|
---|
311 | $webvar{block} = '' if !$webvar{block};
|
---|
312 |
|
---|
313 | # hmm. TMPL_IF block and TMPL_ELSE block on these instead?
|
---|
314 | $page->param(rowa => 'row'.($webvar{block} eq '' ? 1 : 0));
|
---|
315 | $page->param(rowb => 'row'.($webvar{block} eq '' ? 0 : 1));
|
---|
316 | $page->param(block => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
|
---|
317 | $page->param(iscontained => ($webvar{fbtype} && $webvar{fbtype} ne 'y'));
|
---|
318 |
|
---|
319 | # New special case- block to assign is specified
|
---|
320 | if ($webvar{block} ne '') {
|
---|
321 | my $block = new NetAddr::IP $webvar{block};
|
---|
322 |
|
---|
323 | # Handle contained freeblock allocation.
|
---|
324 | # This is a little dangerous, as it's *theoretically* possible to
|
---|
325 | # get fbtype='n' (aka a non-routed freeblock). However, should
|
---|
326 | # someone manage to get there, they get what they deserve.
|
---|
327 | if ($webvar{fbtype} ne 'y') {
|
---|
328 | # Snag the type of the container block from the database.
|
---|
329 | ## hmm. need a flag for parent class/type, sort of?
|
---|
330 | my $pblock = subParent($ip_dbh, $webvar{block});
|
---|
331 | my $ptype = $pblock->{type};
|
---|
332 | $ptype =~ s/c$/r/;
|
---|
333 | $page->param(fbdisptype => $list_alloctypes{$ptype});
|
---|
334 | $page->param(type => $ptype);
|
---|
335 | } else {
|
---|
336 | # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
|
---|
337 | my $tlist = getTypeList($ip_dbh, 'p');
|
---|
338 | $tlist->[0]->{sel} = 1;
|
---|
339 | $page->param(typelist => $tlist);
|
---|
340 | }
|
---|
341 | } else {
|
---|
342 | my @masterlist;
|
---|
343 | foreach my $master (@masterblocks) {
|
---|
344 | my %row = (master => "$master");
|
---|
345 | push (@masterlist, \%row);
|
---|
346 | }
|
---|
347 | $page->param(masterlist => \@masterlist);
|
---|
348 |
|
---|
349 | my @pops;
|
---|
350 | foreach my $pop (@poplist) {
|
---|
351 | my %row = (pop => $pop);
|
---|
352 | push (@pops, \%row);
|
---|
353 | }
|
---|
354 | $page->param(pops => \@pops);
|
---|
355 |
|
---|
356 | # get all standard alloctypes
|
---|
357 | my $tlist = getTypeList($ip_dbh, 'a');
|
---|
358 | $tlist->[0]->{sel} = 1;
|
---|
359 | $page->param(typelist => $tlist);
|
---|
360 | }
|
---|
361 |
|
---|
362 | my @cities;
|
---|
363 | foreach my $city (@citylist) {
|
---|
364 | my %row = (city => $city);
|
---|
365 | push (@cities, \%row);
|
---|
366 | }
|
---|
367 | $page->param(citylist => \@cities);
|
---|
368 |
|
---|
369 | ## node hack
|
---|
370 | my $nlist = getNodeList($ip_dbh);
|
---|
371 | $page->param(nodelist => $nlist);
|
---|
372 | ## end node hack
|
---|
373 |
|
---|
374 | $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
|
---|
375 |
|
---|
376 | } # assignBlock
|
---|
377 |
|
---|
378 |
|
---|
379 | # Take info on requested IP assignment and see what we can provide.
|
---|
380 | sub confirmAssign {
|
---|
381 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
382 | $aclerr = 'addblock';
|
---|
383 | return;
|
---|
384 | }
|
---|
385 |
|
---|
386 | my $cidr;
|
---|
387 | my $alloc_from;
|
---|
388 |
|
---|
389 | # Going to manually validate some items.
|
---|
390 | # custid and city are automagic.
|
---|
391 | return if !validateInput();
|
---|
392 |
|
---|
393 | # Several different cases here.
|
---|
394 | # Static IP vs netblock
|
---|
395 | # + Different flavours of static IP
|
---|
396 | # + Different flavours of netblock
|
---|
397 |
|
---|
398 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
399 | my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
|
---|
400 | $page->param(staticip => 1);
|
---|
401 | $page->param(poollist => $plist) if $plist;
|
---|
402 | $cidr = "Single static IP";
|
---|
403 | ##fixme: need to handle "no available pools"
|
---|
404 |
|
---|
405 | } else { # end show pool options
|
---|
406 |
|
---|
407 | if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
|
---|
408 | $cidr = new NetAddr::IP $webvar{block};
|
---|
409 | $webvar{maskbits} = $cidr->masklen;
|
---|
410 | } else { # done with direct freeblocks assignment
|
---|
411 |
|
---|
412 | if (!$webvar{maskbits}) {
|
---|
413 | $page->param(err => "Please specify a CIDR mask length.");
|
---|
414 | return;
|
---|
415 | }
|
---|
416 |
|
---|
417 | ##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
|
---|
418 | my $failmsg = "No suitable free block found.<br>\n";
|
---|
419 | if ($webvar{alloctype} eq 'rm') {
|
---|
420 | $failmsg .= "We do not have a free routeable block of that size.<br>\n".
|
---|
421 | "You will have to either route a set of smaller netblocks or a single smaller netblock.";
|
---|
422 | } else {
|
---|
423 | if ($webvar{alloctype} =~ /^.[pc]$/) {
|
---|
424 | $failmsg .= "You will have to route another superblock from one of the<br>\n".
|
---|
425 | "master blocks or chose a smaller block size for the pool.";
|
---|
426 | } else {
|
---|
427 | if (!$webvar{pop}) {
|
---|
428 | $page->param(err => 'Please select a POP to route the block from/through.');
|
---|
429 | return;
|
---|
430 | }
|
---|
431 | $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
|
---|
432 | "from one of the master blocks or chose a smaller blocksize.";
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 | $cidr = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype}, $webvar{city}, $webvar{pop},
|
---|
437 | (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
|
---|
438 | if (!$cidr) {
|
---|
439 | $page->param(err => $failmsg);
|
---|
440 | return;
|
---|
441 | }
|
---|
442 | $cidr = new NetAddr::IP $cidr;
|
---|
443 | } # check for freeblocks assignment or IPDB-controlled assignment
|
---|
444 |
|
---|
445 | $alloc_from = "$cidr";
|
---|
446 |
|
---|
447 | # If the block to be allocated is smaller than the one we found,
|
---|
448 | # figure out the "real" block to be allocated.
|
---|
449 | if ($cidr->masklen() ne $webvar{maskbits}) {
|
---|
450 | my $maskbits = $cidr->masklen();
|
---|
451 | my @subblocks;
|
---|
452 | while ($maskbits++ < $webvar{maskbits}) {
|
---|
453 | @subblocks = $cidr->split($maskbits);
|
---|
454 | }
|
---|
455 | $cidr = $subblocks[0];
|
---|
456 | }
|
---|
457 | } # if ($webvar{alloctype} =~ /^.i$/)
|
---|
458 |
|
---|
459 | ## node hack
|
---|
460 | if ($webvar{node} && $webvar{node} ne '-') {
|
---|
461 | my $nodename = getNodeName($ip_dbh, $webvar{node});
|
---|
462 | $page->param(nodename => $nodename);
|
---|
463 | $page->param(nodeid => $webvar{node});
|
---|
464 | }
|
---|
465 | ## end node hack
|
---|
466 |
|
---|
467 | # Stick in the allocation data
|
---|
468 | $page->param(alloc_type => $webvar{alloctype});
|
---|
469 | $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
|
---|
470 | $page->param(alloc_from => $alloc_from);
|
---|
471 | $page->param(cidr => $cidr);
|
---|
472 | $page->param(city => $q->escapeHTML($webvar{city}));
|
---|
473 | $page->param(custid => $webvar{custid});
|
---|
474 | $page->param(circid => $q->escapeHTML($webvar{circid}));
|
---|
475 | $page->param(desc => $q->escapeHTML($webvar{desc}));
|
---|
476 |
|
---|
477 | ##fixme: find a way to have the displayed copy have <br> substitutions
|
---|
478 | # for newlines, and the <input> value have either encoded or bare newlines.
|
---|
479 | # Also applies to privdata.
|
---|
480 | $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
|
---|
481 |
|
---|
482 | # Check to see if user is allowed to do anything with sensitive data
|
---|
483 | my $privdata = '';
|
---|
484 | $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'))
|
---|
485 | if $IPDBacl{$authuser} =~ /s/;
|
---|
486 |
|
---|
487 | # Yay! This now has it's very own little home.
|
---|
488 | $page->param(billinguser => $webvar{userid})
|
---|
489 | if $webvar{userid};
|
---|
490 |
|
---|
491 | ##fixme: this is only needed iff confirm.tmpl and
|
---|
492 | # confirmRemove.tmpl are merged (quite possible, just
|
---|
493 | # a little tedious)
|
---|
494 | $page->param(action => "insert");
|
---|
495 |
|
---|
496 | } # end confirmAssign
|
---|
497 |
|
---|
498 |
|
---|
499 | # Do the work of actually inserting a block in the database.
|
---|
500 | sub insertAssign {
|
---|
501 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
502 | $aclerr = 'addblock';
|
---|
503 | return;
|
---|
504 | }
|
---|
505 | # Some things are done more than once.
|
---|
506 | return if !validateInput();
|
---|
507 |
|
---|
508 | if (!defined($webvar{privdata})) {
|
---|
509 | $webvar{privdata} = '';
|
---|
510 | }
|
---|
511 | # $code is "success" vs "failure", $msg contains OK for a
|
---|
512 | # successful netblock allocation, the IP allocated for static
|
---|
513 | # IP, or the error message if an error occurred.
|
---|
514 |
|
---|
515 | my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
|
---|
516 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
|
---|
517 | $webvar{circid}, $webvar{privdata}, $webvar{node});
|
---|
518 |
|
---|
519 | if ($code eq 'OK') {
|
---|
520 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
521 | $msg =~ s|/32||;
|
---|
522 | $page->param(staticip => $msg);
|
---|
523 | $page->param(custid => $webvar{custid});
|
---|
524 | $page->param(billinguser => $webvar{billinguser});
|
---|
525 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
526 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
|
---|
527 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
528 | } else {
|
---|
529 | my $netblock = new NetAddr::IP $webvar{fullcidr};
|
---|
530 | $page->param(fullcidr => $webvar{fullcidr});
|
---|
531 | $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
|
---|
532 | $page->param(custid => $webvar{custid});
|
---|
533 | if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
|
---|
534 | $page->param(billinguser => $webvar{billinguser});
|
---|
535 | $page->param(custid => $webvar{custid});
|
---|
536 | $page->param(netaddr => $netblock->addr);
|
---|
537 | $page->param(masklen => $netblock->masklen);
|
---|
538 | }
|
---|
539 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
540 | "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
|
---|
541 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
542 | }
|
---|
543 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
544 | "'$webvar{alloctype}' ($msg)";
|
---|
545 | } else {
|
---|
546 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
547 | "'$webvar{alloctype}' by $authuser failed: '$msg'";
|
---|
548 | $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
|
---|
549 | " failed:<br>\n$msg\n");
|
---|
550 | }
|
---|
551 |
|
---|
552 | } # end insertAssign()
|
---|
553 |
|
---|
554 |
|
---|
555 | # Does some basic checks on common input data to make sure nothing
|
---|
556 | # *really* weird gets in to the database through this script.
|
---|
557 | # Does NOT do complete input validation!!!
|
---|
558 | sub validateInput {
|
---|
559 | if ($webvar{city} eq '-') {
|
---|
560 | $page->param(err => 'Please choose a city');
|
---|
561 | return;
|
---|
562 | }
|
---|
563 |
|
---|
564 | # Alloctype check.
|
---|
565 | chomp $webvar{alloctype};
|
---|
566 | if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
|
---|
567 | # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
|
---|
568 | # managing to call things in such a way as to cause this deserves a cryptic error.
|
---|
569 | $page->param(err => 'Invalid alloctype');
|
---|
570 | return;
|
---|
571 | }
|
---|
572 |
|
---|
573 | # CustID check
|
---|
574 | # We have different handling for customer allocations and "internal" or "our" allocations
|
---|
575 | if ($def_custids{$webvar{alloctype}} eq '') {
|
---|
576 | if (!$webvar{custid}) {
|
---|
577 | $page->param(err => 'Please enter a customer ID.');
|
---|
578 | return;
|
---|
579 | }
|
---|
580 | if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
|
---|
581 | # Force uppercase for now...
|
---|
582 | $webvar{custid} =~ tr/a-z/A-Z/;
|
---|
583 | # Crosscheck with billing.
|
---|
584 | my $status = CustIDCK->custid_exist($webvar{custid});
|
---|
585 | if ($CustIDCK::Error) {
|
---|
586 | $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
|
---|
587 | return;
|
---|
588 | }
|
---|
589 | if (!$status) {
|
---|
590 | $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
|
---|
591 | "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
|
---|
592 | "non-customer assignments.");
|
---|
593 | return;
|
---|
594 | }
|
---|
595 | }
|
---|
596 | # print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
|
---|
597 | } else {
|
---|
598 | # New! Improved! And now Loaded From The Database!!
|
---|
599 | if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
|
---|
600 | $webvar{custid} = $def_custids{$webvar{alloctype}};
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | # Check POP location
|
---|
605 | my $flag;
|
---|
606 | if ($webvar{alloctype} eq 'rm') {
|
---|
607 | $flag = 'for a routed netblock';
|
---|
608 | foreach (@poplist) {
|
---|
609 | if (/^$webvar{city}$/) {
|
---|
610 | $flag = 'n';
|
---|
611 | last;
|
---|
612 | }
|
---|
613 | }
|
---|
614 | } else {
|
---|
615 | $flag = 'n';
|
---|
616 | ##fixme: hook to force-set POP or city on certain alloctypes
|
---|
617 | # if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
|
---|
618 | if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
|
---|
619 | $flag = 'to route the block from/through';
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 | # if the alloctype has a restricted city/POP list as determined above,
|
---|
624 | # and the reqested city/POP does not match that list, complain
|
---|
625 | if ($flag ne 'n') {
|
---|
626 | $page->param(err => "Please choose a valid POP location $flag. Valid ".
|
---|
627 | "POP locations are currently:<br>\n".join (" - ", @poplist));
|
---|
628 | return;
|
---|
629 | }
|
---|
630 |
|
---|
631 | return 'OK';
|
---|
632 | } # end validateInput
|
---|
633 |
|
---|
634 |
|
---|
635 | # Displays details of a specific allocation in a form
|
---|
636 | # Allows update/delete
|
---|
637 | # action=edit
|
---|
638 | sub edit {
|
---|
639 |
|
---|
640 | # snag block info from db
|
---|
641 | my $blockinfo = getBlockData($ip_dbh, $webvar{block});
|
---|
642 |
|
---|
643 | # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
|
---|
644 | $blockinfo->{type} =~ s/\s//;
|
---|
645 |
|
---|
646 | $page->param(block => $webvar{block});
|
---|
647 |
|
---|
648 | $page->param(custid => $blockinfo->{custid});
|
---|
649 | $page->param(city => $blockinfo->{city});
|
---|
650 | $page->param(circid => $blockinfo->{circuitid});
|
---|
651 | $page->param(desc => $blockinfo->{description});
|
---|
652 | $page->param(notes => $blockinfo->{notes});
|
---|
653 |
|
---|
654 | ##fixme The check here should be built from the database
|
---|
655 | # Need to expand to support pool types too
|
---|
656 | if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
|
---|
657 | $page->param(changetype => 1);
|
---|
658 | $page->param(alloctype => [
|
---|
659 | { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
|
---|
660 | { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
|
---|
661 | { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
|
---|
662 | { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
|
---|
663 | { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
|
---|
664 | { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
|
---|
665 | { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
|
---|
666 | ]
|
---|
667 | );
|
---|
668 | } else {
|
---|
669 | $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
|
---|
670 | $page->param(type => $blockinfo->{type});
|
---|
671 | }
|
---|
672 |
|
---|
673 | ## node hack
|
---|
674 | my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $webvar{block});
|
---|
675 | $page->param(havenodeid => $nodeid);
|
---|
676 |
|
---|
677 | if ($blockinfo->{type} eq 'fr' || $blockinfo->{type} eq 'bi') {
|
---|
678 | $page->param(typesupportsnodes => 1);
|
---|
679 | $page->param(nodename => $nodename);
|
---|
680 |
|
---|
681 | ##fixme: this whole hack needs cleanup and generalization for all alloctypes
|
---|
682 | ##fixme: arguably a bug that presence of a nodeid implies it can be changed..
|
---|
683 | # but except for manual database changes, only the two types fr and bi can
|
---|
684 | # (currently) have a nodeid set in the first place.
|
---|
685 | if ($IPDBacl{$authuser} =~ /c/) {
|
---|
686 | my $nlist = getNodeList($ip_dbh);
|
---|
687 | foreach (@{$nlist}) {
|
---|
688 | $$_{selme} = ($$_{node_id} == $nodeid);
|
---|
689 | }
|
---|
690 | $page->param(nodelist => $nlist);
|
---|
691 | }
|
---|
692 | }
|
---|
693 | ## end node hack
|
---|
694 |
|
---|
695 | my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
|
---|
696 | $page->param(lastmod => $lastmod);
|
---|
697 |
|
---|
698 | # not happy with the upside-down logic, but...
|
---|
699 | $page->param(swipable => $blockinfo->{type} !~ /.i/);
|
---|
700 | $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
|
---|
701 |
|
---|
702 | # Check to see if we can display sensitive data
|
---|
703 | $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
|
---|
704 | $page->param(privdata => $blockinfo->{privdata});
|
---|
705 |
|
---|
706 | # ACL trickery - these two template booleans control the presence of all form/input tags
|
---|
707 | $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
|
---|
708 | $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
|
---|
709 |
|
---|
710 | } # edit()
|
---|
711 |
|
---|
712 |
|
---|
713 | # Stuff new info about a block into the db
|
---|
714 | # action=update
|
---|
715 | sub update {
|
---|
716 | if ($IPDBacl{$authuser} !~ /c/) {
|
---|
717 | $aclerr = 'updateblock';
|
---|
718 | return;
|
---|
719 | }
|
---|
720 |
|
---|
721 | # Make sure incoming data is in correct format - custID among other things.
|
---|
722 | return if !validateInput;
|
---|
723 |
|
---|
724 | $webvar{swip} = 'n' if !$webvar{swip};
|
---|
725 |
|
---|
726 | my %updargs = (
|
---|
727 | custid => $webvar{custid},
|
---|
728 | city => $webvar{city},
|
---|
729 | description => $webvar{desc},
|
---|
730 | notes => $webvar{notes},
|
---|
731 | circuitid => $webvar{circid},
|
---|
732 | block => $webvar{block},
|
---|
733 | type => $webvar{alloctype},
|
---|
734 | swip => $webvar{swip},
|
---|
735 | );
|
---|
736 |
|
---|
737 | # Semioptional values
|
---|
738 | $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
|
---|
739 | $updargs{node} = $webvar{node} if $webvar{node};
|
---|
740 |
|
---|
741 | my ($code,$msg) = updateBlock($ip_dbh, %updargs);
|
---|
742 |
|
---|
743 | if ($code eq 'FAIL') {
|
---|
744 | syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
|
---|
745 | $page->param(err => "Could not update block/IP $webvar{block}: $msg");
|
---|
746 | return;
|
---|
747 | }
|
---|
748 |
|
---|
749 | # If we get here, the operation succeeded.
|
---|
750 | syslog "notice", "$authuser updated $webvar{block}";
|
---|
751 | ##fixme: log details of the change? old way is in the .debug stream anyway.
|
---|
752 | ##fixme: need to wedge something in to allow "update:field" notifications
|
---|
753 | ## hmm. how to tell what changed? O_o
|
---|
754 | mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
|
---|
755 | "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
|
---|
756 |
|
---|
757 | ## node hack
|
---|
758 | if ($webvar{node} && $webvar{node} ne '-') {
|
---|
759 | my $nodename = getNodeName($ip_dbh, $webvar{node});
|
---|
760 | $page->param(nodename => $nodename);
|
---|
761 | }
|
---|
762 | ## end node hack
|
---|
763 |
|
---|
764 | # Link back to browse-routed or list-pool page on "Update complete" page.
|
---|
765 | my $cblock;
|
---|
766 | if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
|
---|
767 | $page->param(backpool => 1);
|
---|
768 | $cblock = ipParent($ip_dbh, $webvar{block});
|
---|
769 | } else {
|
---|
770 | $cblock = blockParent($ip_dbh, $webvar{block});
|
---|
771 | }
|
---|
772 | $page->param(backblock => $cblock->{cidr});
|
---|
773 |
|
---|
774 | # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
|
---|
775 | # because otherwise we can't convert \n to <br>. *sigh*
|
---|
776 | $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
|
---|
777 | $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
|
---|
778 | $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : " ");
|
---|
779 | $webvar{privdata} =~ s/\n/<br>\n/;
|
---|
780 |
|
---|
781 | $page->param(cidr => $webvar{block});
|
---|
782 | $page->param(city => $webvar{city});
|
---|
783 | $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
|
---|
784 | $page->param(custid => $webvar{custid});
|
---|
785 | $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
|
---|
786 | $page->param(circid => $webvar{circid});
|
---|
787 | $page->param(desc => $webvar{desc});
|
---|
788 | $page->param(notes => $webvar{notes});
|
---|
789 | $page->param(privdata => $webvar{privdata})
|
---|
790 | if $IPDBacl{$authuser} =~ /s/;
|
---|
791 |
|
---|
792 | } # update()
|
---|
793 |
|
---|
794 |
|
---|
795 | # Delete an allocation.
|
---|
796 | sub remove {
|
---|
797 | if ($IPDBacl{$authuser} !~ /d/) {
|
---|
798 | $aclerr = 'delblock';
|
---|
799 | return;
|
---|
800 | }
|
---|
801 |
|
---|
802 | # Serves'em right for getting here...
|
---|
803 | if (!defined($webvar{block})) {
|
---|
804 | $page->param(err => "Can't delete a block that doesn't exist");
|
---|
805 | return;
|
---|
806 | }
|
---|
807 |
|
---|
808 | my $blockdata;
|
---|
809 |
|
---|
810 | if ($webvar{alloctype} eq 'rm') {
|
---|
811 |
|
---|
812 | $blockdata->{block} = $webvar{block};
|
---|
813 | $blockdata->{city} = getRoutedCity($ip_dbh, $webvar{block});
|
---|
814 | $blockdata->{custid} = "N/A";
|
---|
815 | $blockdata->{type} = $webvar{alloctype};
|
---|
816 | $blockdata->{circuitid} = "N/A";
|
---|
817 | $blockdata->{description} = "N/A";
|
---|
818 | $blockdata->{notes} = "N/A";
|
---|
819 | $blockdata->{privdata} = "N/A";
|
---|
820 |
|
---|
821 | } elsif ($webvar{alloctype} eq 'mm') {
|
---|
822 |
|
---|
823 | $blockdata->{block} = $webvar{block};
|
---|
824 | $blockdata->{city} = "N/A";
|
---|
825 | $blockdata->{custid} = "N/A";
|
---|
826 | $blockdata->{type} = $webvar{alloctype};
|
---|
827 | $blockdata->{circuitid} = "N/A";
|
---|
828 | $blockdata->{description} = "N/A";
|
---|
829 | $blockdata->{notes} = "N/A";
|
---|
830 | $blockdata->{privdata} = "N/A";
|
---|
831 |
|
---|
832 | } else {
|
---|
833 |
|
---|
834 | $blockdata = getBlockData($ip_dbh, $webvar{block})
|
---|
835 |
|
---|
836 | } # end cases for different alloctypes
|
---|
837 |
|
---|
838 | $page->param(block => $blockdata->{block});
|
---|
839 | $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
|
---|
840 | $page->param(type => $blockdata->{type});
|
---|
841 | $page->param(city => $blockdata->{city});
|
---|
842 | $page->param(custid => $blockdata->{custid});
|
---|
843 | $page->param(circid => $blockdata->{circuitid});
|
---|
844 | $page->param(desc => $blockdata->{description});
|
---|
845 | $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
|
---|
846 | $blockdata->{notes} =~ s/\n/<br>\n/;
|
---|
847 | $page->param(notes => $blockdata->{notes});
|
---|
848 | $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
|
---|
849 | $blockdata->{privdata} = ' ' if $blockdata->{privdata} eq '';
|
---|
850 | $blockdata->{privdata} =~ s/\n/<br>\n/;
|
---|
851 | $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
|
---|
852 | $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
|
---|
853 |
|
---|
854 | } # end remove()
|
---|
855 |
|
---|
856 |
|
---|
857 | # Delete an allocation. Return it to the freeblocks table; munge
|
---|
858 | # data as necessary to keep as few records as possible in freeblocks
|
---|
859 | # to prevent weirdness when allocating blocks later.
|
---|
860 | # Remove IPs from pool listing if necessary
|
---|
861 | sub finalDelete {
|
---|
862 | if ($IPDBacl{$authuser} !~ /d/) {
|
---|
863 | $aclerr = 'delblock';
|
---|
864 | return;
|
---|
865 | }
|
---|
866 |
|
---|
867 | # need to retrieve block data before deleting so we can notify on that
|
---|
868 | my $blockinfo = getBlockData($ip_dbh, $webvar{block});
|
---|
869 |
|
---|
870 | my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
|
---|
871 |
|
---|
872 | $page->param(block => $webvar{block});
|
---|
873 | if ($code eq 'OK') {
|
---|
874 | syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block} ".
|
---|
875 | $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
|
---|
876 | mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
|
---|
877 | "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
|
---|
878 | "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
|
---|
879 | "\nDescription: ".$blockinfo->{description}."\n");
|
---|
880 | } else {
|
---|
881 | $page->param(failmsg => $msg);
|
---|
882 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
883 | syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
|
---|
884 | } else {
|
---|
885 | syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
|
---|
886 | $page->param(netblock => 1);
|
---|
887 | }
|
---|
888 | }
|
---|
889 |
|
---|
890 | } # finalDelete
|
---|