1 | #!/usr/bin/perl
|
---|
2 | # ipdb/cgi-bin/main.cgi
|
---|
3 | ###
|
---|
4 | # SVN revision info
|
---|
5 | # $Date: 2015-02-23 23:16:11 +0000 (Mon, 23 Feb 2015) $
|
---|
6 | # SVN revision $Rev: 702 $
|
---|
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 | use Frontier::Client;
|
---|
20 |
|
---|
21 | use Sys::Syslog;
|
---|
22 |
|
---|
23 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
24 | ##uselib##
|
---|
25 |
|
---|
26 | use CustIDCK;
|
---|
27 | use MyIPDB;
|
---|
28 |
|
---|
29 | openlog "IPDB","pid","$IPDB::syslog_facility";
|
---|
30 |
|
---|
31 | ## Environment. Collect some things, process some things, set some things...
|
---|
32 |
|
---|
33 | # Collect the username from HTTP auth. If undefined, we're in
|
---|
34 | # a test environment, or called without a username.
|
---|
35 | my $authuser;
|
---|
36 | if (!defined($ENV{'REMOTE_USER'})) {
|
---|
37 | $authuser = '__temptest';
|
---|
38 | } else {
|
---|
39 | $authuser = $ENV{'REMOTE_USER'};
|
---|
40 | }
|
---|
41 |
|
---|
42 | # anyone got a better name? :P
|
---|
43 | my $thingroot = $ENV{SCRIPT_FILENAME};
|
---|
44 | $thingroot =~ s|cgi-bin/main.cgi||;
|
---|
45 |
|
---|
46 | syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
|
---|
47 |
|
---|
48 | ##fixme there *must* be a better order to do things in so this can go back where it was
|
---|
49 | # CGI fiddling done here so we can declare %webvar so we can alter $webvar{action}
|
---|
50 | # to show the right page on DB errors.
|
---|
51 | # Set up the CGI object...
|
---|
52 | my $q = new CGI::Simple;
|
---|
53 | # ... and get query-string params as well as POST params if necessary
|
---|
54 | $q->parse_query_string;
|
---|
55 |
|
---|
56 | # Convenience; saves changing all references to %webvar
|
---|
57 | ##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
|
---|
58 | my %webvar = $q->Vars;
|
---|
59 |
|
---|
60 | # Why not a global DB handle? (And a global statement handle, as well...)
|
---|
61 | # Use the connectDB function, otherwise we end up confusing ourselves
|
---|
62 | my $ip_dbh;
|
---|
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 | my $utilbar = HTML::Template->new(filename => "utilbar.tmpl", loop_context_vars => 1, global_vars => 1);
|
---|
77 |
|
---|
78 | print "Content-type: text/html\n\n";
|
---|
79 |
|
---|
80 | $header->param(version => $IPDB::VERSION);
|
---|
81 | $header->param(addperm => $IPDBacl{$authuser} =~ /a/);
|
---|
82 | $header->param(webpath => $IPDB::webpath);
|
---|
83 |
|
---|
84 | $utilbar->param(webpath => $IPDB::webpath);
|
---|
85 |
|
---|
86 | print $header->output;
|
---|
87 |
|
---|
88 |
|
---|
89 | #main()
|
---|
90 | my $aclerr;
|
---|
91 |
|
---|
92 | if(!defined($webvar{action})) {
|
---|
93 | $webvar{action} = "index"; #shuts up the warnings.
|
---|
94 | }
|
---|
95 |
|
---|
96 | my $page;
|
---|
97 | if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
|
---|
98 | $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1);
|
---|
99 | } else {
|
---|
100 | $page = HTML::Template->new(filename => "dunno.tmpl");
|
---|
101 | }
|
---|
102 |
|
---|
103 | if($webvar{action} eq 'index') {
|
---|
104 | showSummary();
|
---|
105 | } elsif ($webvar{action} eq 'addmaster') {
|
---|
106 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
107 | $aclerr = 'addmaster';
|
---|
108 | }
|
---|
109 |
|
---|
110 | # Retrieve the list of DNS locations if we've got a place to grab them from
|
---|
111 | if ($IPDB::rpc_url) {
|
---|
112 |
|
---|
113 | my %rpcargs = (
|
---|
114 | rpcuser => $authuser,
|
---|
115 | group => 1, # bleh
|
---|
116 | defloc => '',
|
---|
117 | );
|
---|
118 | my $result = IPDB::_rpc('getLocDropdown', %rpcargs);
|
---|
119 | $page->param(loclist => $result);
|
---|
120 | }
|
---|
121 |
|
---|
122 | } elsif ($webvar{action} eq 'newmaster') {
|
---|
123 |
|
---|
124 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
125 | $aclerr = 'addmaster';
|
---|
126 | } else {
|
---|
127 | my $cidr = new NetAddr::IP $webvar{cidr};
|
---|
128 | $page->param(cidr => "$cidr");
|
---|
129 |
|
---|
130 | my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr}, (vrf => $webvar{vrf}, rdns => $webvar{rdns},
|
---|
131 | rwhois => $webvar{rwhois}, defloc => $webvar{loc}, user => $authuser) );
|
---|
132 |
|
---|
133 | if ($code eq 'FAIL') {
|
---|
134 | syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
|
---|
135 | $page->param(err => $msg);
|
---|
136 | } else {
|
---|
137 | $page->param(parent => $msg);
|
---|
138 | if ($code eq 'WARN') {
|
---|
139 | $IPDB::errstr =~ s/\n\n/<br>\n/g;
|
---|
140 | $IPDB::errstr =~ s/:\n/:<br>\n/g;
|
---|
141 | $page->param(warn => $IPDB::errstr);
|
---|
142 | }
|
---|
143 | syslog "info", "$authuser added master block $webvar{cidr}";
|
---|
144 | }
|
---|
145 |
|
---|
146 | } # ACL check
|
---|
147 |
|
---|
148 | } # end add new master
|
---|
149 |
|
---|
150 | elsif ($webvar{action} eq 'showsubs') {
|
---|
151 | showSubs();
|
---|
152 | }
|
---|
153 |
|
---|
154 | elsif($webvar{action} eq 'listpool') {
|
---|
155 | showPool();
|
---|
156 | }
|
---|
157 |
|
---|
158 | # Not modified or added; just shuffled
|
---|
159 | elsif($webvar{action} eq 'assign') {
|
---|
160 | assignBlock();
|
---|
161 | }
|
---|
162 | elsif($webvar{action} eq 'confirm') {
|
---|
163 | confirmAssign();
|
---|
164 | }
|
---|
165 | elsif($webvar{action} eq 'insert') {
|
---|
166 | insertAssign();
|
---|
167 | }
|
---|
168 | elsif($webvar{action} eq 'edit') {
|
---|
169 | edit();
|
---|
170 | }
|
---|
171 | elsif($webvar{action} eq 'update') {
|
---|
172 | update();
|
---|
173 | }
|
---|
174 | elsif($webvar{action} eq 'split') {
|
---|
175 | prepSplit();
|
---|
176 | }
|
---|
177 | elsif($webvar{action} eq 'dosplit') {
|
---|
178 | doSplit();
|
---|
179 | }
|
---|
180 | elsif($webvar{action} eq 'delete') {
|
---|
181 | remove();
|
---|
182 | }
|
---|
183 | elsif($webvar{action} eq 'finaldelete') {
|
---|
184 | finalDelete();
|
---|
185 | }
|
---|
186 | elsif ($webvar{action} eq 'nodesearch') {
|
---|
187 | my $nodelist = getNodeList($ip_dbh);
|
---|
188 | $page->param(nodelist => $nodelist);
|
---|
189 | }
|
---|
190 |
|
---|
191 | # DB failure. Can't do much here, really.
|
---|
192 | elsif ($webvar{action} eq 'dberr') {
|
---|
193 | $page->param(errmsg => $errstr);
|
---|
194 | }
|
---|
195 |
|
---|
196 | # Default is an error. It shouldn't be possible to get here unless you're
|
---|
197 | # randomly feeding in values for webvar{action}.
|
---|
198 | else {
|
---|
199 | my $rnd = rand 500;
|
---|
200 | my $boing = sprintf("%.2f", rand 500);
|
---|
201 | my @excuses = (
|
---|
202 | "Aether cloudy. Ask again later about $webvar{action}.",
|
---|
203 | "The gods are unhappy with your sacrificial $webvar{action}.",
|
---|
204 | "Because one of $webvar{action}'s legs are both the same",
|
---|
205 | "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!",
|
---|
206 | "Hey, man, you've had your free $webvar{action}. Next one's gonna... <i>cost</i>....",
|
---|
207 | "I ain't done $webvar{action}",
|
---|
208 | "Oooo, look! A flying $webvar{action}!",
|
---|
209 | "$webvar{action} too evil, avoiding.",
|
---|
210 | "Rocks fall, $webvar{action} dies.",
|
---|
211 | "Bit bucket must be emptied before I can $webvar{action}..."
|
---|
212 | );
|
---|
213 | $page->param(dunno => $excuses[$rnd/50.0]);
|
---|
214 | }
|
---|
215 | ## Finally! Done with that NASTY "case" emulation!
|
---|
216 |
|
---|
217 |
|
---|
218 | # Switch to a different template if we've tripped on an ACL error.
|
---|
219 | # Note that this should only be exercised in development, when
|
---|
220 | # deeplinked, or when being attacked; normal ACL handling should
|
---|
221 | # remove the links a user is not allowed to click on.
|
---|
222 | if ($aclerr) {
|
---|
223 | $page = HTML::Template->new(filename => "aclerror.tmpl");
|
---|
224 | $page->param(ipdbfunc => $aclmsg{$aclerr});
|
---|
225 | }
|
---|
226 |
|
---|
227 | # Clean up IPDB globals, DB handle, etc.
|
---|
228 | finish($ip_dbh);
|
---|
229 |
|
---|
230 | ## Do all our printing here so we can generate errors and stick them into the slots in the templates.
|
---|
231 |
|
---|
232 | # can't do this yet, too many blowups
|
---|
233 | #print "Content-type: text/html\n\n", $header->output;
|
---|
234 | $page->param(webpath => $IPDB::webpath);
|
---|
235 | print $utilbar->output;
|
---|
236 | print $page->output;
|
---|
237 |
|
---|
238 | # include the admin tools link in the output?
|
---|
239 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
|
---|
240 | $footer->param(webpath => $IPDB::webpath);
|
---|
241 | print $footer->output;
|
---|
242 |
|
---|
243 | # Just in case something waaaayyy down isn't in place
|
---|
244 | # properly... we exit explicitly.
|
---|
245 | exit 0;
|
---|
246 |
|
---|
247 |
|
---|
248 | # Initial display: Show master blocks with total allocated subnets, total free subnets
|
---|
249 | sub showSummary {
|
---|
250 | my $masterlist = listSummary($ip_dbh);
|
---|
251 | $page->param(masterlist => $masterlist);
|
---|
252 |
|
---|
253 | $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
|
---|
254 | } # showSummary
|
---|
255 |
|
---|
256 |
|
---|
257 | # Display blocks immediately within a given parent
|
---|
258 | sub showSubs {
|
---|
259 | # Which layout?
|
---|
260 | if ($IPDB::sublistlayout == 2) {
|
---|
261 |
|
---|
262 | # 2-part layout; mixed containers and end-use allocations and free blocks.
|
---|
263 | # Containers have a second line for the subblock metadata.
|
---|
264 | # We need to load an alternate template for this case.
|
---|
265 | $page = HTML::Template->new(filename => "showsubs2.tmpl", loop_context_vars => 1, global_vars => 1);
|
---|
266 |
|
---|
267 | $page->param(maydel => ($IPDBacl{$authuser} =~ /d/));
|
---|
268 |
|
---|
269 | my $sublist = listSubs($ip_dbh, parent => $webvar{parent});
|
---|
270 | $page->param(sublist => $sublist);
|
---|
271 |
|
---|
272 | } else {
|
---|
273 |
|
---|
274 | # 3-part layout; containers, end-use allocations, and free blocks
|
---|
275 |
|
---|
276 | my $contlist = listContainers($ip_dbh, parent => $webvar{parent});
|
---|
277 | $page->param(contlist => $contlist);
|
---|
278 |
|
---|
279 | my $alloclist = listAllocations($ip_dbh, parent => $webvar{parent});
|
---|
280 | $page->param(alloclist => $alloclist);
|
---|
281 |
|
---|
282 | # only show "delete" button if we have no container or usage allocations
|
---|
283 | $page->param(maydel => ($IPDBacl{$authuser} =~ /d/) && !(@$contlist || @$alloclist));
|
---|
284 |
|
---|
285 | }
|
---|
286 |
|
---|
287 | # Common elements
|
---|
288 | my $pinfo = getBlockData($ip_dbh, $webvar{parent});
|
---|
289 |
|
---|
290 | ##fixme: do we add a wrapper to not show the edit link for master blocks?
|
---|
291 | #$page->param(editme => 1) unless $pinfo->{type} ne 'mm';
|
---|
292 |
|
---|
293 | my $crumbs = getBreadCrumbs($ip_dbh, $pinfo->{parent_id});
|
---|
294 | my @rcrumbs = reverse (@$crumbs);
|
---|
295 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
296 |
|
---|
297 | $page->param(self_id => $webvar{parent});
|
---|
298 | $page->param(block => $pinfo->{block});
|
---|
299 | $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
|
---|
300 |
|
---|
301 | my $flist = listFree($ip_dbh, parent => $webvar{parent});
|
---|
302 | $page->param(freelist => $flist);
|
---|
303 | } # showSubs
|
---|
304 |
|
---|
305 |
|
---|
306 | # List the IPs used in a pool
|
---|
307 | sub showPool {
|
---|
308 |
|
---|
309 | my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
|
---|
310 | my $cidr = new NetAddr::IP $poolinfo->{block};
|
---|
311 |
|
---|
312 | # Tree navigation
|
---|
313 | my $crumbs = getBreadCrumbs($ip_dbh, $poolinfo->{parent_id});
|
---|
314 | my @rcrumbs = reverse (@$crumbs);
|
---|
315 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
316 |
|
---|
317 | $page->param(block => $cidr);
|
---|
318 | $page->param(netip => $cidr->addr);
|
---|
319 | $cidr++;
|
---|
320 | $page->param(gate => $cidr->addr);
|
---|
321 | $cidr--; $cidr--;
|
---|
322 | $page->param(bcast => $cidr->addr);
|
---|
323 | $page->param(mask => $cidr->mask);
|
---|
324 |
|
---|
325 | $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
|
---|
326 | $page->param(city => $poolinfo->{city});
|
---|
327 |
|
---|
328 | # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
|
---|
329 | $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
|
---|
330 |
|
---|
331 | # probably have to add an "edit IP allocation" link here somewhere.
|
---|
332 |
|
---|
333 | my $plist = listPool($ip_dbh, $webvar{pool});
|
---|
334 | # technically slightly more efficient to check the ACL in an if () once outside the foreach
|
---|
335 | foreach (@{$plist}) {
|
---|
336 | $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
|
---|
337 | }
|
---|
338 | $page->param(poolips => $plist);
|
---|
339 | } # end showPool
|
---|
340 |
|
---|
341 |
|
---|
342 | # Show "Add new allocation" page. Note that the actual page may
|
---|
343 | # be one of two templates, and the lists come from the database.
|
---|
344 | sub assignBlock {
|
---|
345 |
|
---|
346 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
347 | $aclerr = 'addblock';
|
---|
348 | return;
|
---|
349 | }
|
---|
350 |
|
---|
351 | # hack pthbttt eww
|
---|
352 | $webvar{parent} = 0 if !$webvar{parent};
|
---|
353 | $webvar{block} = '' if !$webvar{block};
|
---|
354 |
|
---|
355 | $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
|
---|
356 |
|
---|
357 | if ($webvar{fbid} || $webvar{fbtype}) {
|
---|
358 |
|
---|
359 | # Common case, according to reported usage. Block to assign is specified.
|
---|
360 | my $block = new NetAddr::IP $webvar{block};
|
---|
361 |
|
---|
362 | my ($rdns,$cached) = getBlockRDNS($ip_dbh, id => $webvar{parent}, type => $webvar{fbtype}, user => $authuser);
|
---|
363 | $page->param(rdns => $rdns) if $rdns;
|
---|
364 | $page->param(parent => $webvar{parent});
|
---|
365 | $page->param(fbid => $webvar{fbid});
|
---|
366 | # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
|
---|
367 | $page->param(cached => $cached);
|
---|
368 |
|
---|
369 | my $pinfo = getBlockData($ip_dbh, $webvar{parent});
|
---|
370 | # seems reasonable that a new allocation would share a VRF with its parent
|
---|
371 | $page->param(pvrf => $pinfo->{vrf});
|
---|
372 |
|
---|
373 | # Tree navigation
|
---|
374 | my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
|
---|
375 | my @rcrumbs = reverse (@$crumbs);
|
---|
376 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
377 |
|
---|
378 | $webvar{fbtype} = '' if !$webvar{fbtype};
|
---|
379 | if ($webvar{fbtype} eq 'i') {
|
---|
380 | my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i');
|
---|
381 | $page->param(
|
---|
382 | fbip => 1,
|
---|
383 | block => $ipinfo->{block},
|
---|
384 | fbdisptype => $list_alloctypes{$ipinfo->{type}},
|
---|
385 | type => $ipinfo->{type},
|
---|
386 | allocfrom => $pinfo->{block},
|
---|
387 | );
|
---|
388 | } else {
|
---|
389 | # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
|
---|
390 | my $tlist = getTypeList($ip_dbh, 'n');
|
---|
391 | $tlist->[0]->{sel} = 1;
|
---|
392 | $page->param(typelist => $tlist, block => $block);
|
---|
393 | }
|
---|
394 |
|
---|
395 | } else {
|
---|
396 |
|
---|
397 | # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
|
---|
398 | my $mlist = getMasterList($ip_dbh, 'c');
|
---|
399 | $page->param(masterlist => $mlist);
|
---|
400 |
|
---|
401 | my @pops;
|
---|
402 | foreach my $pop (@citylist) {
|
---|
403 | my %row = (pop => $pop);
|
---|
404 | push (@pops, \%row);
|
---|
405 | }
|
---|
406 | $page->param(pops => \@pops);
|
---|
407 |
|
---|
408 | # get all standard alloctypes
|
---|
409 | my $tlist = getTypeList($ip_dbh, 'a');
|
---|
410 | $tlist->[0]->{sel} = 1;
|
---|
411 | $page->param(typelist => $tlist);
|
---|
412 | }
|
---|
413 |
|
---|
414 | my @cities;
|
---|
415 | foreach my $city (@citylist) {
|
---|
416 | my %row = (city => $city);
|
---|
417 | push (@cities, \%row);
|
---|
418 | }
|
---|
419 | $page->param(citylist => \@cities);
|
---|
420 |
|
---|
421 | ## node hack
|
---|
422 | my $nlist = getNodeList($ip_dbh);
|
---|
423 | $page->param(nodelist => $nlist);
|
---|
424 | ## end node hack
|
---|
425 |
|
---|
426 | $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
|
---|
427 |
|
---|
428 | } # assignBlock
|
---|
429 |
|
---|
430 |
|
---|
431 | # Take info on requested IP assignment and see what we can provide.
|
---|
432 | sub confirmAssign {
|
---|
433 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
434 | $aclerr = 'addblock';
|
---|
435 | return;
|
---|
436 | }
|
---|
437 |
|
---|
438 | my $cidr;
|
---|
439 | my $resv; # Reserved for expansion.
|
---|
440 | my $alloc_from;
|
---|
441 | my $fbid = $webvar{fbid};
|
---|
442 | my $p_id = $webvar{parent};
|
---|
443 |
|
---|
444 | # Going to manually validate some items.
|
---|
445 | # custid and city are automagic.
|
---|
446 | return if !validateInput();
|
---|
447 |
|
---|
448 | # make sure this is defined
|
---|
449 | $webvar{fbassign} = 'n' if !$webvar{fbassign};
|
---|
450 |
|
---|
451 | # Several different cases here.
|
---|
452 | # Static IP vs netblock
|
---|
453 | # + Different flavours of static IP
|
---|
454 | # + Different flavours of netblock
|
---|
455 |
|
---|
456 | if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
|
---|
457 | if (!$webvar{pop}) {
|
---|
458 | $page->param(err => "Please select a location/POP site to allocate from.");
|
---|
459 | return;
|
---|
460 | }
|
---|
461 | my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
|
---|
462 | $page->param(staticip => 1);
|
---|
463 | $page->param(poollist => $plist) if $plist;
|
---|
464 | $cidr = "Single static IP";
|
---|
465 | ##fixme: need to handle "no available pools"
|
---|
466 |
|
---|
467 | } else { # end show pool options
|
---|
468 |
|
---|
469 | if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
|
---|
470 |
|
---|
471 | # Tree navigation
|
---|
472 | my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
|
---|
473 | my @rcrumbs = reverse (@$crumbs);
|
---|
474 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
475 |
|
---|
476 | $cidr = new NetAddr::IP $webvar{block};
|
---|
477 | $alloc_from = new NetAddr::IP $webvar{allocfrom};
|
---|
478 | $webvar{maskbits} = $cidr->masklen;
|
---|
479 | # Some additional checks are needed for reserving free space
|
---|
480 | if ($webvar{reserve}) {
|
---|
481 | if ($cidr == $alloc_from) {
|
---|
482 | # We could still squirm and fiddle to try to find a way to reserve space, but the storage model for
|
---|
483 | # IPDB means that all continguous free space is kept in the smallest number of strict CIDR netblocks
|
---|
484 | # possible. (In theory.) If the request and the freeblock are the same, it is theoretically impossible
|
---|
485 | # to reserve an equivalent-sized block either ahead or behind the requested one, because the pair
|
---|
486 | # together would never be a strict CIDR block.
|
---|
487 | $page->param(warning => "Can't reserve space for expansion; free block and requested allocation are the same.");
|
---|
488 | delete $webvar{reserve};
|
---|
489 | } else {
|
---|
490 | # Find which new free block will match the reqested block.
|
---|
491 | # Take the requested mask, shift by one
|
---|
492 | my $tmpmask = $webvar{maskbits};
|
---|
493 | $tmpmask--;
|
---|
494 | # find the subnets with that mask in the selected free block
|
---|
495 | my @pieces = $alloc_from->split($tmpmask);
|
---|
496 | foreach my $slice (@pieces) {
|
---|
497 | if ($slice->contains($cidr)) {
|
---|
498 | # For the subnet that contains the requested block, split that in two,
|
---|
499 | # and flag/cache the one that's not the requested block.
|
---|
500 | my @bits = $slice->split($webvar{maskbits});
|
---|
501 | if ($bits[0] == $cidr) {
|
---|
502 | $resv = $bits[1];
|
---|
503 | } else {
|
---|
504 | $resv = $bits[0];
|
---|
505 | }
|
---|
506 | }
|
---|
507 | }
|
---|
508 | }
|
---|
509 | } # reserve block check
|
---|
510 |
|
---|
511 | } else { # done with direct freeblocks assignment
|
---|
512 |
|
---|
513 | if (!$webvar{maskbits}) {
|
---|
514 | $page->param(err => "Please specify a CIDR mask length.");
|
---|
515 | return;
|
---|
516 | }
|
---|
517 |
|
---|
518 | ##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
|
---|
519 | my $failmsg = "No suitable free block found.<br>\n";
|
---|
520 | if ($webvar{alloctype} eq 'rm') {
|
---|
521 | $failmsg .= "We do not have a free routeable block of that size.<br>\n".
|
---|
522 | "You will have to either route a set of smaller netblocks or a single smaller netblock.";
|
---|
523 | } else {
|
---|
524 | if ($webvar{alloctype} =~ /^.[pc]$/) {
|
---|
525 | $failmsg .= "You will have to route another superblock from one of the<br>\n".
|
---|
526 | "master blocks or chose a smaller block size for the pool.";
|
---|
527 | } else {
|
---|
528 | if (!$webvar{pop}) {
|
---|
529 | $page->param(err => 'Please select a POP to route the block from/through.');
|
---|
530 | return;
|
---|
531 | }
|
---|
532 | $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
|
---|
533 | "from one of the master blocks";
|
---|
534 | if ($webvar{reserve}) {
|
---|
535 | $failmsg .= ', choose a smaller blocksize, or uncheck "Reserve space for expansion".';
|
---|
536 | } else {
|
---|
537 | $failmsg .= " or chose a smaller blocksize.";
|
---|
538 | }
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | # if requesting extra space "reserved for expansion", we need to find a free
|
---|
543 | # block at least double the size of the request.
|
---|
544 | if ($webvar{reserve}) {
|
---|
545 | $webvar{maskbits}--;
|
---|
546 | }
|
---|
547 |
|
---|
548 | ($fbid,$cidr,$p_id) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype},
|
---|
549 | $webvar{city}, $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
|
---|
550 | if (!$cidr) {
|
---|
551 | $page->param(err => $failmsg);
|
---|
552 | return;
|
---|
553 | }
|
---|
554 | $cidr = new NetAddr::IP $cidr;
|
---|
555 |
|
---|
556 | $alloc_from = "$cidr";
|
---|
557 |
|
---|
558 | # when autofinding a block to allocate from, use the first piece of the found
|
---|
559 | # block for the allocation, and the next piece for the "reserved for expansion".
|
---|
560 | if ($webvar{reserve}) {
|
---|
561 | # reset the mask to the real requested one, now that we've got a
|
---|
562 | # block large enough for the request plus reserve
|
---|
563 | $webvar{maskbits}++;
|
---|
564 | ($cidr,$resv) = $cidr->split($webvar{maskbits});
|
---|
565 | }
|
---|
566 |
|
---|
567 | # If the block to be allocated is smaller than the one we found,
|
---|
568 | # figure out the "real" block to be allocated.
|
---|
569 | if ($cidr->masklen() ne $webvar{maskbits}) {
|
---|
570 | my $maskbits = $cidr->masklen();
|
---|
571 | my @subblocks;
|
---|
572 | while ($maskbits++ < $webvar{maskbits}) {
|
---|
573 | @subblocks = $cidr->split($maskbits);
|
---|
574 | }
|
---|
575 | $cidr = $subblocks[0];
|
---|
576 | }
|
---|
577 | } # check for freeblocks assignment or IPDB-controlled assignment
|
---|
578 |
|
---|
579 | # Generate the IP list for the new allocation in case someone wants to set per-IP rDNS right away.
|
---|
580 | # We don't do this on the previous page because we don't know how big a block or even what IP range
|
---|
581 | # it's for (if following the "normal" allocation process)
|
---|
582 | if ($IPDBacl{$authuser} =~ /c/
|
---|
583 | && $cidr->masklen != $cidr->bits
|
---|
584 | && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
|
---|
585 | && $webvar{alloctype} !~ /^.[dpi]/
|
---|
586 | # do we want to allow v6 at all?
|
---|
587 | #&& ! $cidr->{isv6}
|
---|
588 | ) {
|
---|
589 | my @list;
|
---|
590 | foreach my $ip (@{$cidr->splitref()}) {
|
---|
591 | my %row;
|
---|
592 | $row{r_ip} = $ip->addr;
|
---|
593 | $row{iphost} = '';
|
---|
594 | push @list, \%row;
|
---|
595 | }
|
---|
596 | $page->param(r_iplist => \@list);
|
---|
597 | # We don't use this here, because these IPs should already be bare.
|
---|
598 | # ... or should we be paranoid? Make it a config option?
|
---|
599 | #getRDNSbyIP($ip_dbh, type => $webvar{alloctype}, range => "$cidr", user => $authuser) );
|
---|
600 | }
|
---|
601 | } # if ($webvar{alloctype} =~ /^.i$/)
|
---|
602 |
|
---|
603 | ## node hack
|
---|
604 | if ($webvar{node} && $webvar{node} ne '-') {
|
---|
605 | my $nodename = getNodeName($ip_dbh, $webvar{node});
|
---|
606 | $page->param(nodename => $nodename);
|
---|
607 | $page->param(nodeid => $webvar{node});
|
---|
608 | }
|
---|
609 | ## end node hack
|
---|
610 |
|
---|
611 | # reserve for expansion
|
---|
612 | $page->param(reserve => $webvar{reserve});
|
---|
613 | # probably just preventing a little log noise doing this; could just set the param
|
---|
614 | # all the time since it won't be shown if the reserve param above isn't set.
|
---|
615 | # if ($webvar{reserve}) {
|
---|
616 | $page->param(resvblock => $resv);
|
---|
617 | # }
|
---|
618 |
|
---|
619 | # Stick in the allocation data
|
---|
620 | $page->param(alloc_type => $webvar{alloctype});
|
---|
621 | $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
|
---|
622 | $page->param(alloc_from => $alloc_from);
|
---|
623 | $page->param(parent => $p_id);
|
---|
624 | $page->param(fbid => $fbid);
|
---|
625 | $page->param(cidr => $cidr);
|
---|
626 | $page->param(rdns => $webvar{rdns});
|
---|
627 | $page->param(vrf => $webvar{vrf});
|
---|
628 | $page->param(vlan => $webvar{vlan});
|
---|
629 | $page->param(city => $q->escapeHTML($webvar{city}));
|
---|
630 | $page->param(custid => $webvar{custid});
|
---|
631 | $page->param(circid => $q->escapeHTML($webvar{circid}));
|
---|
632 | $page->param(desc => $q->escapeHTML($webvar{desc}));
|
---|
633 |
|
---|
634 | ##fixme: find a way to have the displayed copy have <br> substitutions
|
---|
635 | # for newlines, and the <input> value have either encoded or bare newlines.
|
---|
636 | # Also applies to privdata.
|
---|
637 | $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
|
---|
638 |
|
---|
639 | # Check to see if user is allowed to do anything with sensitive data
|
---|
640 | my $privdata = '';
|
---|
641 | $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'))
|
---|
642 | if $IPDBacl{$authuser} =~ /s/;
|
---|
643 |
|
---|
644 | # Yay! This now has it's very own little home.
|
---|
645 | $page->param(billinguser => $webvar{userid})
|
---|
646 | if $webvar{userid};
|
---|
647 |
|
---|
648 | ##fixme: this is only needed iff confirm.tmpl and
|
---|
649 | # confirmRemove.tmpl are merged (quite possible, just
|
---|
650 | # a little tedious)
|
---|
651 | $page->param(action => "insert");
|
---|
652 |
|
---|
653 | } # end confirmAssign
|
---|
654 |
|
---|
655 |
|
---|
656 | # Do the work of actually inserting a block in the database.
|
---|
657 | sub insertAssign {
|
---|
658 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
659 | $aclerr = 'addblock';
|
---|
660 | return;
|
---|
661 | }
|
---|
662 | # Some things are done more than once.
|
---|
663 | return if !validateInput();
|
---|
664 |
|
---|
665 | if (!defined($webvar{privdata})) {
|
---|
666 | $webvar{privdata} = '';
|
---|
667 | }
|
---|
668 |
|
---|
669 | # split up some linked data for static IPs via guided allocation. needed for breadcrumbs lite.
|
---|
670 | ($webvar{alloc_from},$webvar{rdepth}) = split /,/, $webvar{alloc_from} if $webvar{alloc_from} =~ /,/;
|
---|
671 |
|
---|
672 | # $code is "success" vs "failure", $msg contains OK for a
|
---|
673 | # successful netblock allocation, the IP allocated for static
|
---|
674 | # IP, or the error message if an error occurred.
|
---|
675 |
|
---|
676 | ##fixme: consider just passing \%webvar to allocateBlock()?
|
---|
677 | # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
|
---|
678 | my %iprev;
|
---|
679 | foreach (keys %webvar) {
|
---|
680 | $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
|
---|
681 | }
|
---|
682 |
|
---|
683 | # Easier to see and cosmetically fiddle the list like this
|
---|
684 | my %insert_args = (
|
---|
685 | cidr => $webvar{fullcidr},
|
---|
686 | fbid => $webvar{fbid},
|
---|
687 | reserve => $webvar{reserve},
|
---|
688 | parent => $webvar{parent},
|
---|
689 | custid => $webvar{custid},
|
---|
690 | type => $webvar{alloctype},
|
---|
691 | city => $webvar{city},
|
---|
692 | desc => $webvar{desc},
|
---|
693 | notes => $webvar{notes},
|
---|
694 | circid => $webvar{circid},
|
---|
695 | privdata => $webvar{privdata},
|
---|
696 | nodeid => $webvar{node},
|
---|
697 | rdns => $webvar{rdns},
|
---|
698 | vrf => $webvar{vrf},
|
---|
699 | vlan => $webvar{vlan},
|
---|
700 | user => $authuser,
|
---|
701 | );
|
---|
702 |
|
---|
703 | my ($code,$msg) = allocateBlock($ip_dbh, %insert_args, iprev => \%iprev);
|
---|
704 |
|
---|
705 | if ($code eq 'OK') {
|
---|
706 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
707 | $msg =~ s|/32||;
|
---|
708 | $page->param(staticip => $msg);
|
---|
709 | $page->param(custid => $webvar{custid});
|
---|
710 | $page->param(parent => $webvar{parent}, pool => $webvar{alloc_from});
|
---|
711 | $page->param(billinguser => $webvar{billinguser});
|
---|
712 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
713 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
|
---|
714 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
715 | } else {
|
---|
716 | my $netblock = new NetAddr::IP $webvar{fullcidr};
|
---|
717 | $page->param(fullcidr => $webvar{fullcidr});
|
---|
718 | $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
|
---|
719 | $page->param(custid => $webvar{custid});
|
---|
720 |
|
---|
721 | # breadcrumbs lite! provide at least a link to the parent of the block we just allocated.
|
---|
722 | my $binfo = getBlockData($ip_dbh, $webvar{parent});
|
---|
723 | $page->param(parentid => $webvar{parent});
|
---|
724 | $page->param(parentblock => $binfo->{block});
|
---|
725 |
|
---|
726 | # Full breadcrumbs
|
---|
727 | my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
|
---|
728 | my @rcrumbs = reverse (@$crumbs);
|
---|
729 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
730 |
|
---|
731 | if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
|
---|
732 | $page->param(billinguser => $webvar{billinguser});
|
---|
733 | $page->param(custid => $webvar{custid});
|
---|
734 | $page->param(netaddr => $netblock->addr);
|
---|
735 | $page->param(masklen => $netblock->masklen);
|
---|
736 | }
|
---|
737 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
738 | "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
|
---|
739 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
740 | }
|
---|
741 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
742 | "'$webvar{alloctype}' ($msg)";
|
---|
743 | } else {
|
---|
744 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
745 | "'$webvar{alloctype}' by $authuser failed: '$msg'";
|
---|
746 | $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
|
---|
747 | " failed:<br>\n$msg\n");
|
---|
748 | }
|
---|
749 |
|
---|
750 | } # end insertAssign()
|
---|
751 |
|
---|
752 |
|
---|
753 | # Does some basic checks on common input data to make sure nothing
|
---|
754 | # *really* weird gets in to the database through this script.
|
---|
755 | # Does NOT do complete input validation!!!
|
---|
756 | sub validateInput {
|
---|
757 | if ($webvar{city} eq '-') {
|
---|
758 | $page->param(err => 'Please choose a city');
|
---|
759 | return;
|
---|
760 | }
|
---|
761 |
|
---|
762 | # Alloctype check.
|
---|
763 | chomp $webvar{alloctype};
|
---|
764 | if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
|
---|
765 | # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
|
---|
766 | # managing to call things in such a way as to cause this deserves a cryptic error.
|
---|
767 | $page->param(err => 'Invalid alloctype');
|
---|
768 | return;
|
---|
769 | }
|
---|
770 |
|
---|
771 | # CustID check
|
---|
772 | # We have different handling for customer allocations and "internal" or "our" allocations
|
---|
773 | if ($def_custids{$webvar{alloctype}} eq '') {
|
---|
774 | if (!$webvar{custid}) {
|
---|
775 | $page->param(err => 'Please enter a customer ID.');
|
---|
776 | return;
|
---|
777 | }
|
---|
778 | # Crosscheck with billing.
|
---|
779 | my $status = CustIDCK->custid_exist($webvar{custid});
|
---|
780 | if ($CustIDCK::Error) {
|
---|
781 | $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
|
---|
782 | return;
|
---|
783 | }
|
---|
784 | if (!$status) {
|
---|
785 | $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
|
---|
786 | "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
|
---|
787 | "non-customer assignments.");
|
---|
788 | return;
|
---|
789 | }
|
---|
790 | # print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
|
---|
791 | } else {
|
---|
792 | # New! Improved! And now Loaded From The Database!!
|
---|
793 | if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
|
---|
794 | $webvar{custid} = $def_custids{$webvar{alloctype}};
|
---|
795 | }
|
---|
796 | }
|
---|
797 |
|
---|
798 | ## hmmm.... is this even useful?
|
---|
799 | if (0) {
|
---|
800 | # Check POP location
|
---|
801 | my $flag;
|
---|
802 | if ($webvar{alloctype} eq 'rm') {
|
---|
803 | $flag = 'for a routed netblock';
|
---|
804 | foreach (@poplist) {
|
---|
805 | if (/^$webvar{city}$/) {
|
---|
806 | $flag = 'n';
|
---|
807 | last;
|
---|
808 | }
|
---|
809 | }
|
---|
810 | } else {
|
---|
811 | $flag = 'n';
|
---|
812 | ##fixme: hook to force-set POP or city on certain alloctypes
|
---|
813 | # if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
|
---|
814 | if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
|
---|
815 | $flag = 'to route the block from/through';
|
---|
816 | }
|
---|
817 | }
|
---|
818 |
|
---|
819 | # if the alloctype has a restricted city/POP list as determined above,
|
---|
820 | # and the reqested city/POP does not match that list, complain
|
---|
821 | if ($flag ne 'n') {
|
---|
822 | $page->param(err => "Please choose a valid POP location $flag. Valid ".
|
---|
823 | "POP locations are currently:<br>\n".join (" - ", @poplist));
|
---|
824 | return;
|
---|
825 | }
|
---|
826 | }
|
---|
827 |
|
---|
828 | # VRF. Not a full validity check, just a basic sanity check.
|
---|
829 | if ($webvar{vrf}) {
|
---|
830 | # Trim leading and trailing whitespace first
|
---|
831 | $webvar{vrf} =~ s/^\s+//;
|
---|
832 | $webvar{vrf} =~ s/\s+$//;
|
---|
833 | if ($webvar{vrf} !~ /^[\w\d_.-]{1,32}$/) {
|
---|
834 | $page->param(err => "VRF values may only contain alphanumerics, and may not be more than 32 characters");
|
---|
835 | return;
|
---|
836 | }
|
---|
837 | }
|
---|
838 |
|
---|
839 | # VLAN. Should we allow/use VLAN names, or just the numeric ID?
|
---|
840 | if ($webvar{vlan}) {
|
---|
841 | # Trim leading and trailing whitespace first
|
---|
842 | $webvar{vlan} =~ s/^\s+//;
|
---|
843 | $webvar{vlan} =~ s/\s+$//;
|
---|
844 | # ... ve make it ze configurable thingy!
|
---|
845 | if ($IPDB::numeric_vlan) {
|
---|
846 | if ($webvar{vlan} !~ /^\d+$/) {
|
---|
847 | $page->param(err => "VLANs must be numeric");
|
---|
848 | return;
|
---|
849 | }
|
---|
850 | } else {
|
---|
851 | if ($webvar{vlan} !~ /^[\w\d_.-]+$/) {
|
---|
852 | $page->param(err => "VLANs must be alphanumeric");
|
---|
853 | return;
|
---|
854 | }
|
---|
855 | }
|
---|
856 | }
|
---|
857 |
|
---|
858 | return 'OK';
|
---|
859 | } # end validateInput
|
---|
860 |
|
---|
861 |
|
---|
862 | # Displays details of a specific allocation in a form
|
---|
863 | # Allows update/delete
|
---|
864 | # action=edit
|
---|
865 | sub edit {
|
---|
866 |
|
---|
867 | # snag block info from db
|
---|
868 | my $blockinfo = getBlockData($ip_dbh, $webvar{id}, $webvar{basetype});
|
---|
869 | $page->param(id => $webvar{id});
|
---|
870 | $page->param(basetype => $webvar{basetype});
|
---|
871 |
|
---|
872 | # Tree navigation
|
---|
873 | my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
|
---|
874 | my @rcrumbs = reverse (@$crumbs);
|
---|
875 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
876 |
|
---|
877 | # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
|
---|
878 | $blockinfo->{type} =~ s/\s//;
|
---|
879 |
|
---|
880 | my $cached;
|
---|
881 | # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
|
---|
882 | ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser);
|
---|
883 | # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
|
---|
884 | $page->param(cached => $cached);
|
---|
885 |
|
---|
886 | my $cidr = new NetAddr::IP $blockinfo->{block};
|
---|
887 | # Limit the per-IP rDNS list based on CIDR length; larger ones just take up too much space.
|
---|
888 | # Also, don't show on IP pools; the individual IPs will have a space for rDNS
|
---|
889 | # Don't show on single IPs; these use the "pattern" field
|
---|
890 | if ($IPDBacl{$authuser} =~ /c/
|
---|
891 | && $cidr->masklen != $cidr->bits
|
---|
892 | && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
|
---|
893 | && $blockinfo->{type} !~ /^.[dpi]/
|
---|
894 | # do we want to allow v6 at all?
|
---|
895 | #&& ! $cidr->{isv6}
|
---|
896 | ) {
|
---|
897 | $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type},
|
---|
898 | range => $blockinfo->{block}, user => $authuser) );
|
---|
899 | }
|
---|
900 |
|
---|
901 | # consider extending this to show time as well as date
|
---|
902 | my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
|
---|
903 | $page->param(lastmod => $lastmod);
|
---|
904 | # $page->param(lastmod => $blockinfo->{lastmod});
|
---|
905 |
|
---|
906 | $page->param(block => $blockinfo->{block});
|
---|
907 | $page->param(city => $blockinfo->{city});
|
---|
908 | $page->param(custid => $blockinfo->{custid});
|
---|
909 |
|
---|
910 | ##fixme The check here should be built from the database
|
---|
911 | # Need to expand to support pool types too
|
---|
912 | if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
|
---|
913 | $page->param(changetype => 1);
|
---|
914 | $page->param(alloctype => [
|
---|
915 | { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
|
---|
916 | { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
|
---|
917 | { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
|
---|
918 | { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
|
---|
919 | { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
|
---|
920 | { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
|
---|
921 | { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
|
---|
922 | ]
|
---|
923 | );
|
---|
924 | } else {
|
---|
925 | $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
|
---|
926 | $page->param(type => $blockinfo->{type});
|
---|
927 | }
|
---|
928 |
|
---|
929 | ## node hack
|
---|
930 | my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block});
|
---|
931 | # $page->param(havenodeid => $nodeid);
|
---|
932 | $page->param(nodename => $nodename);
|
---|
933 |
|
---|
934 | ##fixme: this whole hack needs cleanup and generalization for all alloctypes
|
---|
935 | ##fixme: arguably a bug that presence of a nodeid implies it can be changed..
|
---|
936 | if ($IPDBacl{$authuser} =~ /c/) {
|
---|
937 | my $nlist = getNodeList($ip_dbh);
|
---|
938 | if ($nodeid) {
|
---|
939 | foreach (@{$nlist}) {
|
---|
940 | $$_{selme} = ($$_{node_id} == $nodeid);
|
---|
941 | }
|
---|
942 | }
|
---|
943 | $page->param(nodelist => $nlist);
|
---|
944 | }
|
---|
945 | ## end node hack
|
---|
946 |
|
---|
947 | $page->param(rdns => $blockinfo->{rdns});
|
---|
948 | $page->param(vrf => $blockinfo->{vrf});
|
---|
949 | $page->param(vlan => $blockinfo->{vlan});
|
---|
950 |
|
---|
951 | # Reserved-for-expansion
|
---|
952 | $page->param(reserve => $blockinfo->{reserve});
|
---|
953 | $page->param(reserve_id => $blockinfo->{reserve_id});
|
---|
954 | my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
|
---|
955 | $page->param(newblock => $newblock);
|
---|
956 |
|
---|
957 | # not happy with the upside-down logic, but...
|
---|
958 | $page->param(swipable => $blockinfo->{type} !~ /.i/);
|
---|
959 | $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
|
---|
960 |
|
---|
961 | $page->param(circid => $blockinfo->{circuitid});
|
---|
962 | $page->param(desc => $blockinfo->{description});
|
---|
963 | $page->param(notes => $blockinfo->{notes});
|
---|
964 |
|
---|
965 | # Check to see if we can display sensitive data
|
---|
966 | $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
|
---|
967 | $page->param(privdata => $blockinfo->{privdata});
|
---|
968 |
|
---|
969 | # ACL trickery - these two template booleans control the presence of all form/input tags
|
---|
970 | $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
|
---|
971 | $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
|
---|
972 |
|
---|
973 | # Need to find internal knobs to twist to actually vary these. (Ab)use "change" flag for now
|
---|
974 | $page->param(maymerge => ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/));
|
---|
975 | if ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/) {
|
---|
976 | if ($blockinfo->{type} =~ /^.p$/) {
|
---|
977 | # PPP pools
|
---|
978 | $page->param(maysplit => 1) if $cidr->masklen+1 < $cidr->bits;
|
---|
979 | } elsif ($blockinfo->{type} =~ /.d/) {
|
---|
980 | # Non-PPP pools
|
---|
981 | $page->param(maysplit => 1) if $cidr->masklen+2 < $cidr->bits;
|
---|
982 | } else {
|
---|
983 | # Standard netblocks. Arguably allowing splitting these down to single IPs
|
---|
984 | # doesn't make much sense, but forcing users to apply allocation types
|
---|
985 | # "properly" is worse than herding cats.
|
---|
986 | $page->param(maysplit => 1) if $cidr->masklen < $cidr->bits;
|
---|
987 | }
|
---|
988 | }
|
---|
989 |
|
---|
990 | } # edit()
|
---|
991 |
|
---|
992 |
|
---|
993 | # Stuff new info about a block into the db
|
---|
994 | # action=update
|
---|
995 | sub update {
|
---|
996 | if ($IPDBacl{$authuser} !~ /c/) {
|
---|
997 | $aclerr = 'updateblock';
|
---|
998 | return;
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | # Make sure incoming data is in correct format - custID among other things.
|
---|
1002 | return if !validateInput;
|
---|
1003 |
|
---|
1004 | $webvar{swip} = 'n' if !$webvar{swip};
|
---|
1005 |
|
---|
1006 | my %updargs = (
|
---|
1007 | custid => $webvar{custid},
|
---|
1008 | city => $webvar{city},
|
---|
1009 | description => $webvar{desc},
|
---|
1010 | notes => $webvar{notes},
|
---|
1011 | circuitid => $webvar{circid},
|
---|
1012 | block => $webvar{block},
|
---|
1013 | type => $webvar{alloctype},
|
---|
1014 | swip => $webvar{swip},
|
---|
1015 | rdns => $webvar{rdns},
|
---|
1016 | vrf => $webvar{vrf},
|
---|
1017 | vlan => $webvar{vlan},
|
---|
1018 | user => $authuser,
|
---|
1019 | );
|
---|
1020 |
|
---|
1021 | # Semioptional values
|
---|
1022 | $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
|
---|
1023 | $updargs{node} = $webvar{node} if $webvar{node};
|
---|
1024 |
|
---|
1025 | # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
|
---|
1026 | my %iprev;
|
---|
1027 | foreach (keys %webvar) {
|
---|
1028 | $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | # Merge with reserved freeblock
|
---|
1032 | $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme};
|
---|
1033 |
|
---|
1034 | my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
|
---|
1035 |
|
---|
1036 | if ($code eq 'FAIL') {
|
---|
1037 | syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
|
---|
1038 | $page->param(err => "Could not update block/IP $webvar{block}: $msg");
|
---|
1039 | return;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | # If we get here, the operation succeeded.
|
---|
1043 | syslog "notice", "$authuser updated $webvar{block}";
|
---|
1044 | ##fixme: log details of the change? old way is in the .debug stream anyway.
|
---|
1045 | ##fixme: need to wedge something in to allow "update:field" notifications
|
---|
1046 | ## hmm. how to tell what changed? O_o
|
---|
1047 | mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
|
---|
1048 | "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
|
---|
1049 |
|
---|
1050 | ## node hack
|
---|
1051 | if ($webvar{node} && $webvar{node} ne '-') {
|
---|
1052 | my $nodename = getNodeName($ip_dbh, $webvar{node});
|
---|
1053 | $page->param(nodename => $nodename);
|
---|
1054 | }
|
---|
1055 | ## end node hack
|
---|
1056 |
|
---|
1057 | # Link back to browse-routed or list-pool page on "Update complete" page.
|
---|
1058 | my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
|
---|
1059 | my $pblock = getBlockData($ip_dbh, $binfo->{parent_id});
|
---|
1060 | $page->param(backid => $binfo->{parent_id});
|
---|
1061 | $page->param(backblock => $pblock->{block});
|
---|
1062 | $page->param(backpool => ($webvar{basetype} eq 'i'));
|
---|
1063 |
|
---|
1064 | # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
|
---|
1065 | # because otherwise we can't convert \n to <br>. *sigh*
|
---|
1066 | $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
|
---|
1067 | $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
|
---|
1068 | $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : " ");
|
---|
1069 | $webvar{privdata} =~ s/\n/<br>\n/;
|
---|
1070 |
|
---|
1071 | $page->param(cidr => $binfo->{block});
|
---|
1072 | $page->param(rdns => $webvar{rdns});
|
---|
1073 | $page->param(city => $webvar{city});
|
---|
1074 | $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
|
---|
1075 | $page->param(custid => $webvar{custid});
|
---|
1076 | $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
|
---|
1077 | $page->param(circid => $webvar{circid});
|
---|
1078 | $page->param(desc => $webvar{desc});
|
---|
1079 | $page->param(notes => $webvar{notes});
|
---|
1080 | $page->param(privdata => $webvar{privdata})
|
---|
1081 | if $IPDBacl{$authuser} =~ /s/;
|
---|
1082 |
|
---|
1083 | } # update()
|
---|
1084 |
|
---|
1085 |
|
---|
1086 | sub prepSplit {
|
---|
1087 | if ($IPDBacl{$authuser} !~ /c/) {
|
---|
1088 | $aclerr = 'splitblock';
|
---|
1089 | return;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | my $blockinfo = getBlockData($ip_dbh, $webvar{block});
|
---|
1093 |
|
---|
1094 | if ($blockinfo->{type} =~ /^.i$/) {
|
---|
1095 | $page->param(err => "Can't split a single IP allocation");
|
---|
1096 | return;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | # Info about current allocation
|
---|
1100 | $page->param(oldblock => $blockinfo->{block});
|
---|
1101 | $page->param(block => $webvar{block});
|
---|
1102 |
|
---|
1103 | # Note that there are probably different rules that should be followed to restrict splitting IPv6 blocks;
|
---|
1104 | # strictly speaking it will be exceptionally rare to see smaller than a /64 assigned to a customer, since that
|
---|
1105 | # breaks auto-addressing schemes.
|
---|
1106 |
|
---|
1107 | # Generate possible splits
|
---|
1108 | my $block = new NetAddr::IP $blockinfo->{block};
|
---|
1109 | my $oldmask = $block->masklen;
|
---|
1110 | if ($blockinfo->{type} =~ /^.d$/) {
|
---|
1111 | # Non-PPP pools
|
---|
1112 | if ($oldmask+2 >= $block->bits) {
|
---|
1113 | $page->param(err => "Can't split a standard netblock pool any further");
|
---|
1114 | return;
|
---|
1115 | }
|
---|
1116 | # Allow splitting down to v4 /30 (which results in one usable IP; dubiously useful)
|
---|
1117 | $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-2;
|
---|
1118 | } elsif ($blockinfo->{type} =~ /.p/) {
|
---|
1119 | # Allow splitting PPP pools down to v4 /31
|
---|
1120 | $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-1;
|
---|
1121 | } else {
|
---|
1122 | # Allow splitting all other non-pool netblocks down to single IPs, which...
|
---|
1123 | # arguably should be *aggregated* in a pool. Except where they shouldn't.
|
---|
1124 | $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits;
|
---|
1125 | }
|
---|
1126 | # set the split-in-half mask
|
---|
1127 | $page->param(sp2mask => $oldmask+1);
|
---|
1128 |
|
---|
1129 | # Generate possible shrink targets
|
---|
1130 | my @keepers = $block->split($block->masklen+1);
|
---|
1131 | $page->param(newblockA => $keepers[0]);
|
---|
1132 | $page->param(newblockB => $keepers[1]);
|
---|
1133 | } # prepSplit()
|
---|
1134 |
|
---|
1135 |
|
---|
1136 | sub doSplit {
|
---|
1137 | if ($IPDBacl{$authuser} !~ /c/) {
|
---|
1138 | $aclerr = 'splitblock';
|
---|
1139 | return;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | ##fixme: need consistent way to identify "this thing that is this thing" with only the ID
|
---|
1143 | # also applies to other locations
|
---|
1144 | my $blockinfo = getBlockData($ip_dbh, $webvar{block});
|
---|
1145 |
|
---|
1146 | if ($blockinfo->{type} =~ /^.i$/) {
|
---|
1147 | $page->param(err => "Can't split a single IP allocation");
|
---|
1148 | return;
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | if ($webvar{subact} eq 'split') {
|
---|
1152 | $page->param(issplit => 1);
|
---|
1153 | my $binfo = getBlockData($ip_dbh, $webvar{block});
|
---|
1154 | $page->param(cidr => $binfo->{block});
|
---|
1155 | my $block = new NetAddr::IP $binfo->{block};
|
---|
1156 | my $newblocks = splitBlock($ip_dbh, $webvar{block}, 'b', $webvar{split});
|
---|
1157 | if ($newblocks) {
|
---|
1158 | $page->param(newblocks => $newblocks);
|
---|
1159 | # and the backlink to the parent container
|
---|
1160 | my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
|
---|
1161 | $page->param(backid => $binfo->{parent_id});
|
---|
1162 | $page->param(backblock => $pinfo->{block});
|
---|
1163 | } else {
|
---|
1164 | $page->param(err => $IPDB::errstr);
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | } else {
|
---|
1168 | # Shrink
|
---|
1169 | }
|
---|
1170 | } # doSplit()
|
---|
1171 |
|
---|
1172 |
|
---|
1173 | # Delete an allocation.
|
---|
1174 | sub remove {
|
---|
1175 | if ($IPDBacl{$authuser} !~ /d/) {
|
---|
1176 | $aclerr = 'delblock';
|
---|
1177 | return;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | # Serves'em right for getting here...
|
---|
1181 | if (!defined($webvar{block})) {
|
---|
1182 | $page->param(err => "Can't delete a block that doesn't exist");
|
---|
1183 | return;
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | my $blockdata;
|
---|
1187 | $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
|
---|
1188 |
|
---|
1189 | if ($blockdata->{parent_id} == 0) { # $webvar{alloctype} eq 'mm'
|
---|
1190 | $blockdata->{city} = "N/A";
|
---|
1191 | $blockdata->{custid} = "N/A";
|
---|
1192 | $blockdata->{circuitid} = "N/A";
|
---|
1193 | $blockdata->{description} = "N/A";
|
---|
1194 | $blockdata->{notes} = "N/A";
|
---|
1195 | $blockdata->{privdata} = "N/A";
|
---|
1196 | } # end cases for different alloctypes
|
---|
1197 |
|
---|
1198 | $page->param(blockid => $webvar{block});
|
---|
1199 | $page->param(basetype => $webvar{basetype});
|
---|
1200 |
|
---|
1201 | $page->param(block => $blockdata->{block});
|
---|
1202 | $page->param(rdns => $blockdata->{rdns});
|
---|
1203 |
|
---|
1204 | # maybe need to apply more magic here?
|
---|
1205 | # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
|
---|
1206 | # -> all real blocks (nb: pool IPs need extra handling)
|
---|
1207 | # -> NOC/private-IP (how to ID?)
|
---|
1208 | # -> anything with a pattern matching $IPDB::domain?
|
---|
1209 | if ($blockdata->{type} !~ /^.i$/) {
|
---|
1210 | $page->param(autodel => 1);
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
|
---|
1214 | $page->param(city => $blockdata->{city});
|
---|
1215 | $page->param(custid => $blockdata->{custid});
|
---|
1216 | $page->param(circid => $blockdata->{circuitid});
|
---|
1217 | $page->param(desc => $blockdata->{description});
|
---|
1218 | $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
|
---|
1219 | $blockdata->{notes} =~ s/\n/<br>\n/;
|
---|
1220 | $page->param(notes => $blockdata->{notes});
|
---|
1221 | $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
|
---|
1222 | $blockdata->{privdata} = ' ' if !$blockdata->{privdata};
|
---|
1223 | $blockdata->{privdata} =~ s/\n/<br>\n/;
|
---|
1224 | $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
|
---|
1225 | $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
|
---|
1226 |
|
---|
1227 | } # end remove()
|
---|
1228 |
|
---|
1229 |
|
---|
1230 | # Delete an allocation. Return it to the freeblocks table; munge
|
---|
1231 | # data as necessary to keep as few records as possible in freeblocks
|
---|
1232 | # to prevent weirdness when allocating blocks later.
|
---|
1233 | # Remove IPs from pool listing if necessary
|
---|
1234 | sub finalDelete {
|
---|
1235 | if ($IPDBacl{$authuser} !~ /d/) {
|
---|
1236 | $aclerr = 'delblock';
|
---|
1237 | return;
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | # need to retrieve block data before deleting so we can notify on that
|
---|
1241 | my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
|
---|
1242 | my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b');
|
---|
1243 |
|
---|
1244 | my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser);
|
---|
1245 |
|
---|
1246 | $page->param(block => $blockinfo->{block});
|
---|
1247 | $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}}));
|
---|
1248 | $page->param(delparent_id => $blockinfo->{parent_id});# if $webvar{rdepth};
|
---|
1249 | if ($pinfo) {
|
---|
1250 | $page->param(delparent => $pinfo->{block});
|
---|
1251 | $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}}));
|
---|
1252 | }
|
---|
1253 | $page->param(returnpool => ($webvar{basetype} eq 'i') );
|
---|
1254 | if ($code =~ /^WARN(POOL|MERGE)/) {
|
---|
1255 | my ($pid,$pcidr) = split /,/, $msg;
|
---|
1256 | my $real_pinfo = getBlockData($ip_dbh, $pid, 'b');
|
---|
1257 | $page->param(parent_id => $pid);
|
---|
1258 | $page->param(parent => $pcidr);
|
---|
1259 | $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}}));
|
---|
1260 | $page->param(mergeip => $code eq 'WARNPOOL');
|
---|
1261 | }
|
---|
1262 | if ($code eq 'WARN') {
|
---|
1263 | $msg =~ s/\n/<br>\n/g;
|
---|
1264 | $page->param(genwarn => $msg);
|
---|
1265 | }
|
---|
1266 | if ($code eq 'OK' || $code =~ /^WARN/) {
|
---|
1267 | syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock $webvar{block} ".
|
---|
1268 | $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
|
---|
1269 | mailNotify($ip_dbh, 'da', "REMOVED: ".$disp_alloctypes{$blockinfo->{type}}." $webvar{block}",
|
---|
1270 | $disp_alloctypes{$blockinfo->{type}}." $webvar{block} deallocated by $authuser\n".
|
---|
1271 | "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
|
---|
1272 | "\nDescription: ".$blockinfo->{description}."\n");
|
---|
1273 | } else {
|
---|
1274 | $page->param(failmsg => $msg);
|
---|
1275 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
1276 | syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
|
---|
1277 | } else {
|
---|
1278 | syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
|
---|
1279 | $page->param(netblock => 1);
|
---|
1280 | }
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | } # finalDelete
|
---|