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