1 | #!/usr/bin/perl
|
---|
2 | # ipdb/cgi-bin/main.cgi
|
---|
3 | ###
|
---|
4 | # SVN revision info
|
---|
5 | # $Date: 2015-10-08 21:49:27 +0000 (Thu, 08 Oct 2015) $
|
---|
6 | # SVN revision $Rev: 788 $
|
---|
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 | ##fixme: whine and complain when the user is not present in the ACL hash above
|
---|
89 |
|
---|
90 | #main()
|
---|
91 | my $aclerr;
|
---|
92 |
|
---|
93 | if(!defined($webvar{action})) {
|
---|
94 | $webvar{action} = "index"; #shuts up the warnings.
|
---|
95 | }
|
---|
96 |
|
---|
97 | my $page;
|
---|
98 | if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") {
|
---|
99 | $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1);
|
---|
100 | } else {
|
---|
101 | $page = HTML::Template->new(filename => "dunno.tmpl");
|
---|
102 | }
|
---|
103 |
|
---|
104 | if($webvar{action} eq 'index') {
|
---|
105 | showSummary();
|
---|
106 | } elsif ($webvar{action} eq 'addmaster') {
|
---|
107 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
108 | $aclerr = 'addmaster';
|
---|
109 | }
|
---|
110 |
|
---|
111 | # Retrieve the list of DNS locations if we've got a place to grab them from
|
---|
112 | if ($IPDB::rpc_url) {
|
---|
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 'merge') {
|
---|
181 | prepMerge();
|
---|
182 | }
|
---|
183 | elsif($webvar{action} eq 'confmerge') {
|
---|
184 | confMerge();
|
---|
185 | }
|
---|
186 | elsif($webvar{action} eq 'domerge') {
|
---|
187 | doMerge();
|
---|
188 | }
|
---|
189 | elsif($webvar{action} eq 'delete') {
|
---|
190 | remove();
|
---|
191 | }
|
---|
192 | elsif($webvar{action} eq 'finaldelete') {
|
---|
193 | finalDelete();
|
---|
194 | }
|
---|
195 | elsif ($webvar{action} eq 'nodesearch') {
|
---|
196 | my $nodelist = getNodeList($ip_dbh);
|
---|
197 | $page->param(nodelist => $nodelist);
|
---|
198 | }
|
---|
199 |
|
---|
200 | # DB failure. Can't do much here, really.
|
---|
201 | elsif ($webvar{action} eq 'dberr') {
|
---|
202 | $page->param(errmsg => $errstr);
|
---|
203 | }
|
---|
204 |
|
---|
205 | # Default is an error. It shouldn't be possible to get here unless you're
|
---|
206 | # randomly feeding in values for webvar{action}.
|
---|
207 | else {
|
---|
208 | my $rnd = rand 500;
|
---|
209 | my $boing = sprintf("%.2f", rand 500);
|
---|
210 | my @excuses = (
|
---|
211 | "Aether cloudy. Ask again later about $webvar{action}.",
|
---|
212 | "The gods are unhappy with your sacrificial $webvar{action}.",
|
---|
213 | "Because one of $webvar{action}'s legs are both the same",
|
---|
214 | "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!",
|
---|
215 | "Hey, man, you've had your free $webvar{action}. Next one's gonna... <i>cost</i>....",
|
---|
216 | "I ain't done $webvar{action}",
|
---|
217 | "Oooo, look! A flying $webvar{action}!",
|
---|
218 | "$webvar{action} too evil, avoiding.",
|
---|
219 | "Rocks fall, $webvar{action} dies.",
|
---|
220 | "Bit bucket must be emptied before I can $webvar{action}..."
|
---|
221 | );
|
---|
222 | $page->param(dunno => $excuses[$rnd/50.0]);
|
---|
223 | }
|
---|
224 | ## Finally! Done with that NASTY "case" emulation!
|
---|
225 |
|
---|
226 |
|
---|
227 | # Switch to a different template if we've tripped on an ACL error.
|
---|
228 | # Note that this should only be exercised in development, when
|
---|
229 | # deeplinked, or when being attacked; normal ACL handling should
|
---|
230 | # remove the links a user is not allowed to click on.
|
---|
231 | if ($aclerr) {
|
---|
232 | $page = HTML::Template->new(filename => "aclerror.tmpl");
|
---|
233 | $page->param(ipdbfunc => $aclmsg{$aclerr});
|
---|
234 | }
|
---|
235 |
|
---|
236 | # Clean up IPDB globals, DB handle, etc.
|
---|
237 | finish($ip_dbh);
|
---|
238 |
|
---|
239 | ## Do all our printing here so we can generate errors and stick them into the slots in the templates.
|
---|
240 |
|
---|
241 | # can't do this yet, too many blowups
|
---|
242 | #print "Content-type: text/html\n\n", $header->output;
|
---|
243 | $page->param(webpath => $IPDB::webpath);
|
---|
244 | print $utilbar->output;
|
---|
245 | print $page->output;
|
---|
246 |
|
---|
247 | # include the admin tools link in the output?
|
---|
248 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
|
---|
249 | $footer->param(webpath => $IPDB::webpath);
|
---|
250 | print $footer->output;
|
---|
251 |
|
---|
252 | # Just in case something waaaayyy down isn't in place
|
---|
253 | # properly... we exit explicitly.
|
---|
254 | exit 0;
|
---|
255 |
|
---|
256 |
|
---|
257 | # Initial display: Show master blocks with total allocated subnets, total free subnets
|
---|
258 | sub showSummary {
|
---|
259 | my $masterlist = listSummary($ip_dbh);
|
---|
260 | $page->param(masterlist => $masterlist);
|
---|
261 |
|
---|
262 | $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
|
---|
263 | } # showSummary
|
---|
264 |
|
---|
265 |
|
---|
266 | # Display blocks immediately within a given parent
|
---|
267 | sub showSubs {
|
---|
268 | # Which layout?
|
---|
269 | if ($IPDB::sublistlayout == 2) {
|
---|
270 |
|
---|
271 | # 2-part layout; mixed containers and end-use allocations and free blocks.
|
---|
272 | # Containers have a second line for the subblock metadata.
|
---|
273 | # We need to load an alternate template for this case.
|
---|
274 | $page = HTML::Template->new(filename => "showsubs2.tmpl", loop_context_vars => 1, global_vars => 1);
|
---|
275 |
|
---|
276 | $page->param(maydel => ($IPDBacl{$authuser} =~ /d/));
|
---|
277 |
|
---|
278 | my $sublist = listSubs($ip_dbh, parent => $webvar{parent});
|
---|
279 | $page->param(sublist => $sublist);
|
---|
280 |
|
---|
281 | } else {
|
---|
282 |
|
---|
283 | # 3-part layout; containers, end-use allocations, and free blocks
|
---|
284 |
|
---|
285 | my $contlist = listContainers($ip_dbh, parent => $webvar{parent});
|
---|
286 | $page->param(contlist => $contlist);
|
---|
287 |
|
---|
288 | my $alloclist = listAllocations($ip_dbh, parent => $webvar{parent});
|
---|
289 | $page->param(alloclist => $alloclist);
|
---|
290 |
|
---|
291 | # only show "delete" button if we have no container or usage allocations
|
---|
292 | $page->param(maydel => ($IPDBacl{$authuser} =~ /d/) && !(@$contlist || @$alloclist));
|
---|
293 |
|
---|
294 | }
|
---|
295 |
|
---|
296 | # Common elements
|
---|
297 | my $pinfo = getBlockData($ip_dbh, $webvar{parent});
|
---|
298 |
|
---|
299 | ##fixme: do we add a wrapper to not show the edit link for master blocks?
|
---|
300 | #$page->param(editme => 1) unless $pinfo->{type} ne 'mm';
|
---|
301 |
|
---|
302 | my $crumbs = getBreadCrumbs($ip_dbh, $pinfo->{parent_id});
|
---|
303 | my @rcrumbs = reverse (@$crumbs);
|
---|
304 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
305 |
|
---|
306 | $page->param(self_id => $webvar{parent});
|
---|
307 | $page->param(block => $pinfo->{block});
|
---|
308 | $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/));
|
---|
309 |
|
---|
310 | my $flist = listFree($ip_dbh, parent => $webvar{parent});
|
---|
311 | $page->param(freelist => $flist);
|
---|
312 | } # showSubs
|
---|
313 |
|
---|
314 |
|
---|
315 | # List the IPs used in a pool
|
---|
316 | sub showPool {
|
---|
317 |
|
---|
318 | my $poolinfo = getBlockData($ip_dbh, $webvar{pool});
|
---|
319 | my $cidr = new NetAddr::IP $poolinfo->{block};
|
---|
320 | $page->param(vlan => $poolinfo->{vlan});
|
---|
321 |
|
---|
322 | # Tree navigation
|
---|
323 | my $crumbs = getBreadCrumbs($ip_dbh, $poolinfo->{parent_id});
|
---|
324 | my @rcrumbs = reverse (@$crumbs);
|
---|
325 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
326 |
|
---|
327 | $page->param(block => $cidr);
|
---|
328 | $page->param(netip => $cidr->addr);
|
---|
329 | $cidr++;
|
---|
330 | $page->param(gate => $cidr->addr);
|
---|
331 | $cidr--; $cidr--;
|
---|
332 | $page->param(bcast => $cidr->addr);
|
---|
333 | $page->param(mask => $cidr->mask);
|
---|
334 |
|
---|
335 | $page->param(disptype => $disp_alloctypes{$poolinfo->{type}});
|
---|
336 | $page->param(city => $poolinfo->{city});
|
---|
337 |
|
---|
338 | # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
|
---|
339 | $page->param(realblock => $poolinfo->{type} =~ /^.d$/);
|
---|
340 |
|
---|
341 | # probably have to add an "edit IP allocation" link here somewhere.
|
---|
342 |
|
---|
343 | my $plist = listPool($ip_dbh, $webvar{pool});
|
---|
344 | # technically slightly more efficient to check the ACL in an if () once outside the foreach
|
---|
345 | foreach (@{$plist}) {
|
---|
346 | $$_{maydel} = $IPDBacl{$authuser} =~ /d/;
|
---|
347 | }
|
---|
348 | $page->param(poolips => $plist);
|
---|
349 | } # end showPool
|
---|
350 |
|
---|
351 |
|
---|
352 | # Show "Add new allocation" page. Note that the actual page may
|
---|
353 | # be one of two templates, and the lists come from the database.
|
---|
354 | sub assignBlock {
|
---|
355 |
|
---|
356 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
357 | $aclerr = 'addblock';
|
---|
358 | return;
|
---|
359 | }
|
---|
360 |
|
---|
361 | # hack pthbttt eww
|
---|
362 | $webvar{parent} = 0 if !$webvar{parent};
|
---|
363 | $webvar{block} = '' if !$webvar{block};
|
---|
364 |
|
---|
365 | $page->param(allocfrom => $webvar{block}); # fb-assign flag, if block is set, we're in fb-assign
|
---|
366 |
|
---|
367 | if ($webvar{fbid} || $webvar{fbtype}) {
|
---|
368 |
|
---|
369 | # Common case, according to reported usage. Block to assign is specified.
|
---|
370 | my $block = new NetAddr::IP $webvar{block};
|
---|
371 |
|
---|
372 | my ($rdns,$cached) = getBlockRDNS($ip_dbh, id => $webvar{parent}, type => $webvar{fbtype}, user => $authuser);
|
---|
373 | $page->param(rdns => $rdns) if $rdns;
|
---|
374 | $page->param(parent => $webvar{parent});
|
---|
375 | $page->param(fbid => $webvar{fbid});
|
---|
376 | # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
|
---|
377 | $page->param(cached => $cached);
|
---|
378 |
|
---|
379 | my $pinfo = getBlockData($ip_dbh, $webvar{parent});
|
---|
380 | # seems reasonable that a new allocation would share a VRF with its parent
|
---|
381 | $page->param(pvrf => $pinfo->{vrf});
|
---|
382 |
|
---|
383 | # Tree navigation
|
---|
384 | my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
|
---|
385 | my @rcrumbs = reverse (@$crumbs);
|
---|
386 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
387 |
|
---|
388 | $webvar{fbtype} = '' if !$webvar{fbtype};
|
---|
389 | if ($webvar{fbtype} eq 'i') {
|
---|
390 | my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i');
|
---|
391 | $page->param(
|
---|
392 | fbip => 1,
|
---|
393 | block => $ipinfo->{block},
|
---|
394 | fbdisptype => $list_alloctypes{$ipinfo->{type}},
|
---|
395 | type => $ipinfo->{type},
|
---|
396 | allocfrom => $pinfo->{block},
|
---|
397 | );
|
---|
398 | } else {
|
---|
399 | # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch
|
---|
400 | my $tlist = getTypeList($ip_dbh, 'n');
|
---|
401 | $tlist->[0]->{sel} = 1;
|
---|
402 | $page->param(typelist => $tlist, block => $block);
|
---|
403 | }
|
---|
404 |
|
---|
405 | } else {
|
---|
406 |
|
---|
407 | # Uncommon case, according to reported usage. Block to assign needs to be found based on criteria.
|
---|
408 | my $mlist = getMasterList($ip_dbh, 'c');
|
---|
409 | $page->param(masterlist => $mlist);
|
---|
410 |
|
---|
411 | my @pops;
|
---|
412 | foreach my $pop (@citylist) {
|
---|
413 | my %row = (pop => $pop);
|
---|
414 | push (@pops, \%row);
|
---|
415 | }
|
---|
416 | $page->param(pops => \@pops);
|
---|
417 |
|
---|
418 | # get all standard alloctypes
|
---|
419 | my $tlist = getTypeList($ip_dbh, 'a');
|
---|
420 | $tlist->[0]->{sel} = 1;
|
---|
421 | $page->param(typelist => $tlist);
|
---|
422 | }
|
---|
423 |
|
---|
424 | my @cities;
|
---|
425 | foreach my $city (@citylist) {
|
---|
426 | my %row = (city => $city);
|
---|
427 | push (@cities, \%row);
|
---|
428 | }
|
---|
429 | $page->param(citylist => \@cities);
|
---|
430 |
|
---|
431 | ## node hack
|
---|
432 | my $nlist = getNodeList($ip_dbh);
|
---|
433 | $page->param(nodelist => $nlist);
|
---|
434 | ## end node hack
|
---|
435 |
|
---|
436 | $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
|
---|
437 |
|
---|
438 | } # assignBlock
|
---|
439 |
|
---|
440 |
|
---|
441 | # Take info on requested IP assignment and see what we can provide.
|
---|
442 | sub confirmAssign {
|
---|
443 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
444 | $aclerr = 'addblock';
|
---|
445 | return;
|
---|
446 | }
|
---|
447 |
|
---|
448 | my $cidr;
|
---|
449 | my $resv; # Reserved for expansion.
|
---|
450 | my $alloc_from;
|
---|
451 | my $fbid = $webvar{fbid};
|
---|
452 | my $p_id = $webvar{parent};
|
---|
453 |
|
---|
454 | # Going to manually validate some items.
|
---|
455 | # custid and city are automagic.
|
---|
456 | return if !validateInput();
|
---|
457 |
|
---|
458 | # make sure this is defined
|
---|
459 | $webvar{fbassign} = 'n' if !$webvar{fbassign};
|
---|
460 |
|
---|
461 | # Several different cases here.
|
---|
462 | # Static IP vs netblock
|
---|
463 | # + Different flavours of static IP
|
---|
464 | # + Different flavours of netblock
|
---|
465 |
|
---|
466 | if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') {
|
---|
467 | if (!$webvar{pop}) {
|
---|
468 | $page->param(err => "Please select a location/POP site to allocate from.");
|
---|
469 | return;
|
---|
470 | }
|
---|
471 | my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop});
|
---|
472 | $page->param(staticip => 1);
|
---|
473 | $page->param(poollist => $plist) if $plist;
|
---|
474 | $cidr = "Single static IP";
|
---|
475 | ##fixme: need to handle "no available pools"
|
---|
476 |
|
---|
477 | } else { # end show pool options
|
---|
478 |
|
---|
479 | if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
|
---|
480 |
|
---|
481 | # Tree navigation
|
---|
482 | my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
|
---|
483 | my @rcrumbs = reverse (@$crumbs);
|
---|
484 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
485 |
|
---|
486 | $cidr = new NetAddr::IP $webvar{block};
|
---|
487 | $alloc_from = new NetAddr::IP $webvar{allocfrom};
|
---|
488 | $webvar{maskbits} = $cidr->masklen;
|
---|
489 | # Some additional checks are needed for reserving free space
|
---|
490 | if ($webvar{reserve}) {
|
---|
491 | if ($cidr == $alloc_from) {
|
---|
492 | # We could still squirm and fiddle to try to find a way to reserve space, but the storage model for
|
---|
493 | # IPDB means that all continguous free space is kept in the smallest number of strict CIDR netblocks
|
---|
494 | # possible. (In theory.) If the request and the freeblock are the same, it is theoretically impossible
|
---|
495 | # to reserve an equivalent-sized block either ahead or behind the requested one, because the pair
|
---|
496 | # together would never be a strict CIDR block.
|
---|
497 | $page->param(warning => "Can't reserve space for expansion; free block and requested allocation are the same.");
|
---|
498 | delete $webvar{reserve};
|
---|
499 | } else {
|
---|
500 | # Find which new free block will match the reqested block.
|
---|
501 | # Take the requested mask, shift by one
|
---|
502 | my $tmpmask = $webvar{maskbits};
|
---|
503 | $tmpmask--;
|
---|
504 | # find the subnets with that mask in the selected free block
|
---|
505 | my @pieces = $alloc_from->split($tmpmask);
|
---|
506 | foreach my $slice (@pieces) {
|
---|
507 | if ($slice->contains($cidr)) {
|
---|
508 | # For the subnet that contains the requested block, split that in two,
|
---|
509 | # and flag/cache the one that's not the requested block.
|
---|
510 | my @bits = $slice->split($webvar{maskbits});
|
---|
511 | if ($bits[0] == $cidr) {
|
---|
512 | $resv = $bits[1];
|
---|
513 | } else {
|
---|
514 | $resv = $bits[0];
|
---|
515 | }
|
---|
516 | }
|
---|
517 | }
|
---|
518 | }
|
---|
519 | } # reserve block check
|
---|
520 |
|
---|
521 | } else { # done with direct freeblocks assignment
|
---|
522 |
|
---|
523 | if (!$webvar{maskbits}) {
|
---|
524 | $page->param(err => "Please specify a CIDR mask length.");
|
---|
525 | return;
|
---|
526 | }
|
---|
527 |
|
---|
528 | ##fixme ick, ew, bleh. gotta handle the failure message generation better. push it into findAllocateFrom()?
|
---|
529 | my $failmsg = "No suitable free block found.<br>\n";
|
---|
530 | if ($webvar{alloctype} eq 'rm') {
|
---|
531 | $failmsg .= "We do not have a free routeable block of that size.<br>\n".
|
---|
532 | "You will have to either route a set of smaller netblocks or a single smaller netblock.";
|
---|
533 | } else {
|
---|
534 | if ($webvar{alloctype} =~ /^.[pc]$/) {
|
---|
535 | $failmsg .= "You will have to route another superblock from one of the<br>\n".
|
---|
536 | "master blocks or chose a smaller block size for the pool.";
|
---|
537 | } else {
|
---|
538 | if (!$webvar{pop}) {
|
---|
539 | $page->param(err => 'Please select a POP to route the block from/through.');
|
---|
540 | return;
|
---|
541 | }
|
---|
542 | $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
|
---|
543 | "from one of the master blocks";
|
---|
544 | if ($webvar{reserve}) {
|
---|
545 | $failmsg .= ', choose a smaller blocksize, or uncheck "Reserve space for expansion".';
|
---|
546 | } else {
|
---|
547 | $failmsg .= " or chose a smaller blocksize.";
|
---|
548 | }
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | # if requesting extra space "reserved for expansion", we need to find a free
|
---|
553 | # block at least double the size of the request.
|
---|
554 | if ($webvar{reserve}) {
|
---|
555 | $webvar{maskbits}--;
|
---|
556 | }
|
---|
557 |
|
---|
558 | ($fbid,$cidr,$p_id) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype},
|
---|
559 | $webvar{city}, $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
|
---|
560 | if (!$cidr) {
|
---|
561 | $page->param(err => $failmsg);
|
---|
562 | return;
|
---|
563 | }
|
---|
564 | $cidr = new NetAddr::IP $cidr;
|
---|
565 |
|
---|
566 | $alloc_from = "$cidr";
|
---|
567 |
|
---|
568 | # when autofinding a block to allocate from, use the first piece of the found
|
---|
569 | # block for the allocation, and the next piece for the "reserved for expansion".
|
---|
570 | if ($webvar{reserve}) {
|
---|
571 | # reset the mask to the real requested one, now that we've got a
|
---|
572 | # block large enough for the request plus reserve
|
---|
573 | $webvar{maskbits}++;
|
---|
574 | ($cidr,$resv) = $cidr->split($webvar{maskbits});
|
---|
575 | }
|
---|
576 |
|
---|
577 | # If the block to be allocated is smaller than the one we found,
|
---|
578 | # figure out the "real" block to be allocated.
|
---|
579 | if ($cidr->masklen() ne $webvar{maskbits}) {
|
---|
580 | my $maskbits = $cidr->masklen();
|
---|
581 | my @subblocks;
|
---|
582 | while ($maskbits++ < $webvar{maskbits}) {
|
---|
583 | @subblocks = $cidr->split($maskbits);
|
---|
584 | }
|
---|
585 | $cidr = $subblocks[0];
|
---|
586 | }
|
---|
587 | } # check for freeblocks assignment or IPDB-controlled assignment
|
---|
588 |
|
---|
589 | # Generate the IP list for the new allocation in case someone wants to set per-IP rDNS right away.
|
---|
590 | # We don't do this on the previous page because we don't know how big a block or even what IP range
|
---|
591 | # it's for (if following the "normal" allocation process)
|
---|
592 | if ($IPDBacl{$authuser} =~ /c/
|
---|
593 | && $cidr->masklen != $cidr->bits
|
---|
594 | && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
|
---|
595 | # config flag for "all block types" OR "not-a-pool-or-IP type"
|
---|
596 | && ($IPDB::revlistalltypes || $webvar{alloctype} !~ /^.[dpi]/)
|
---|
597 | # safety against trying to retrieve and display more than 1k (10 bits, /22 v4) worth of individual IPs
|
---|
598 | # ever. If you really need to manage a long list of IPs like that all in one place, you can use the DNS
|
---|
599 | # management tool. Even a /26 is a bit much, really.
|
---|
600 | && ($cidr->bits - $cidr->masklen) <= 10
|
---|
601 | # do we want to allow v6 at all?
|
---|
602 | #&& ! $cidr->{isv6}
|
---|
603 | ) {
|
---|
604 | my @list;
|
---|
605 | foreach my $ip (@{$cidr->splitref()}) {
|
---|
606 | my %row;
|
---|
607 | $row{r_ip} = $ip->addr;
|
---|
608 | $row{iphost} = '';
|
---|
609 | push @list, \%row;
|
---|
610 | }
|
---|
611 | $page->param(r_iplist => \@list);
|
---|
612 | # We don't use this here, because these IPs should already be bare.
|
---|
613 | # ... or should we be paranoid? Make it a config option?
|
---|
614 | #getRDNSbyIP($ip_dbh, type => $webvar{alloctype}, range => "$cidr", user => $authuser) );
|
---|
615 | }
|
---|
616 | } # if ($webvar{alloctype} =~ /^.i$/)
|
---|
617 |
|
---|
618 | ## node hack
|
---|
619 | if ($webvar{node} && $webvar{node} ne '-') {
|
---|
620 | my $nodename = getNodeName($ip_dbh, $webvar{node});
|
---|
621 | $page->param(nodename => $nodename);
|
---|
622 | $page->param(nodeid => $webvar{node});
|
---|
623 | }
|
---|
624 | ## end node hack
|
---|
625 |
|
---|
626 | # flag DNS info if we can't publish the entry remotely
|
---|
627 | my $pinfo = getBlockData($ip_dbh, $webvar{parent});
|
---|
628 | $page->param(dnslocal => 1) unless ($pinfo->{revpartial} || $pinfo->{revavail});
|
---|
629 |
|
---|
630 | # reserve for expansion
|
---|
631 | $page->param(reserve => $webvar{reserve});
|
---|
632 | # probably just preventing a little log noise doing this; could just set the param
|
---|
633 | # all the time since it won't be shown if the reserve param above isn't set.
|
---|
634 | # if ($webvar{reserve}) {
|
---|
635 | $page->param(resvblock => $resv);
|
---|
636 | # }
|
---|
637 |
|
---|
638 | # Stick in the allocation data
|
---|
639 | $page->param(alloc_type => $webvar{alloctype});
|
---|
640 | $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}}));
|
---|
641 | $page->param(alloc_from => $alloc_from);
|
---|
642 | $page->param(parent => $p_id);
|
---|
643 | $page->param(fbid => $fbid);
|
---|
644 | $page->param(cidr => $cidr);
|
---|
645 | $page->param(rdns => $webvar{rdns});
|
---|
646 | $page->param(vrf => $webvar{vrf});
|
---|
647 | $page->param(vlan => $webvar{vlan});
|
---|
648 | $page->param(city => $q->escapeHTML($webvar{city}));
|
---|
649 | $page->param(custid => $webvar{custid});
|
---|
650 | $page->param(circid => $q->escapeHTML($webvar{circid}));
|
---|
651 | $page->param(desc => $q->escapeHTML($webvar{desc}));
|
---|
652 |
|
---|
653 | ##fixme: find a way to have the displayed copy have <br> substitutions
|
---|
654 | # for newlines, and the <input> value have either encoded or bare newlines.
|
---|
655 | # Also applies to privdata.
|
---|
656 | $page->param(notes => $q->escapeHTML($webvar{notes},'y'));
|
---|
657 |
|
---|
658 | # Check to see if user is allowed to do anything with sensitive data
|
---|
659 | if ($IPDBacl{$authuser} =~ /s/) {
|
---|
660 | $page->param(nocling => 1);
|
---|
661 | $page->param(privdata => $q->escapeHTML($webvar{privdata},'y'));
|
---|
662 |
|
---|
663 | $page->param(backupfields => $webvar{backupfields});
|
---|
664 | $page->param(bkbrand => $webvar{bkbrand});
|
---|
665 | $page->param(bkmodel => $webvar{bkmodel});
|
---|
666 | $page->param(bktype => $webvar{bktype});
|
---|
667 | $page->param(bksrc => $webvar{bksrc});
|
---|
668 | $page->param(bkuser => $webvar{bkuser});
|
---|
669 | # these two could use virtually any character
|
---|
670 | $page->param(bkvpass => $q->escapeHTML($webvar{bkvpass}));
|
---|
671 | $page->param(bkepass => $q->escapeHTML($webvar{bkepass}));
|
---|
672 | $page->param(bkport => $webvar{bkport});
|
---|
673 | }
|
---|
674 |
|
---|
675 | # Yay! This now has it's very own little home.
|
---|
676 | $page->param(billinguser => $webvar{userid})
|
---|
677 | if $webvar{userid};
|
---|
678 |
|
---|
679 | ##fixme: this is only needed iff confirm.tmpl and
|
---|
680 | # confirmRemove.tmpl are merged (quite possible, just
|
---|
681 | # a little tedious)
|
---|
682 | $page->param(action => "insert");
|
---|
683 |
|
---|
684 | } # end confirmAssign
|
---|
685 |
|
---|
686 |
|
---|
687 | # Do the work of actually inserting a block in the database.
|
---|
688 | sub insertAssign {
|
---|
689 | if ($IPDBacl{$authuser} !~ /a/) {
|
---|
690 | $aclerr = 'addblock';
|
---|
691 | return;
|
---|
692 | }
|
---|
693 | # Some things are done more than once.
|
---|
694 | return if !validateInput();
|
---|
695 |
|
---|
696 | ##fixme: permission check
|
---|
697 | if (!defined($webvar{privdata})) {
|
---|
698 | $webvar{privdata} = '';
|
---|
699 | }
|
---|
700 |
|
---|
701 | # $code is "success" vs "failure", $msg contains OK for a
|
---|
702 | # successful netblock allocation, the IP allocated for static
|
---|
703 | # IP, or the error message if an error occurred.
|
---|
704 |
|
---|
705 | ##fixme: consider just passing \%webvar to allocateBlock()?
|
---|
706 | # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
|
---|
707 | my %iprev;
|
---|
708 | foreach (keys %webvar) {
|
---|
709 | $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
|
---|
710 | }
|
---|
711 |
|
---|
712 | # Easier to see and cosmetically fiddle the list like this
|
---|
713 | my %insert_args = (
|
---|
714 | cidr => $webvar{fullcidr},
|
---|
715 | fbid => $webvar{fbid},
|
---|
716 | reserve => $webvar{reserve},
|
---|
717 | parent => $webvar{parent},
|
---|
718 | custid => $webvar{custid},
|
---|
719 | type => $webvar{alloctype},
|
---|
720 | city => $webvar{city},
|
---|
721 | desc => $webvar{desc},
|
---|
722 | notes => $webvar{notes},
|
---|
723 | circid => $webvar{circid},
|
---|
724 | privdata => $webvar{privdata},
|
---|
725 | nodeid => $webvar{node},
|
---|
726 | rdns => $webvar{rdns},
|
---|
727 | vrf => $webvar{vrf},
|
---|
728 | vlan => $webvar{vlan},
|
---|
729 | user => $authuser,
|
---|
730 | );
|
---|
731 |
|
---|
732 | ##fixme: permission check
|
---|
733 | # fill in backup data, if present/allowed
|
---|
734 | if ($webvar{backupfields}) {
|
---|
735 | $insert_args{backup} = 1;
|
---|
736 | $insert_args{bkbrand} = $webvar{bkbrand};
|
---|
737 | $insert_args{bkmodel} = $webvar{bkmodel};
|
---|
738 | for my $bkfield (qw(type src user vpass epass port)) {
|
---|
739 | $insert_args{"bk$bkfield"} = ($webvar{"bk$bkfield"} ? $webvar{"bk$bkfield"} : '');
|
---|
740 | }
|
---|
741 | }
|
---|
742 |
|
---|
743 | my $pinfo = getBlockData($ip_dbh, $webvar{parent});
|
---|
744 |
|
---|
745 | # clean up a minor mess with guided allocation of static IPs
|
---|
746 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
747 | $insert_args{alloc_from} = $pinfo->{block};
|
---|
748 | }
|
---|
749 |
|
---|
750 | my ($code,$msg) = allocateBlock($ip_dbh, %insert_args, iprev => \%iprev);
|
---|
751 |
|
---|
752 | if ($code eq 'OK') {
|
---|
753 | # breadcrumbs lite! provide at least a link to the parent of the block we just allocated.
|
---|
754 | $page->param(parentid => $webvar{parent});
|
---|
755 | $page->param(parentblock => $pinfo->{block});
|
---|
756 |
|
---|
757 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
758 | $msg =~ s|/32||;
|
---|
759 | $page->param(staticip => $msg);
|
---|
760 | $page->param(custid => $webvar{custid});
|
---|
761 | $page->param(billinguser => $webvar{billinguser});
|
---|
762 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
763 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
|
---|
764 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
765 | } else {
|
---|
766 | my $netblock = new NetAddr::IP $webvar{fullcidr};
|
---|
767 | $page->param(fullcidr => $webvar{fullcidr});
|
---|
768 | $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}});
|
---|
769 | $page->param(custid => $webvar{custid});
|
---|
770 |
|
---|
771 | # Full breadcrumbs
|
---|
772 | my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent});
|
---|
773 | my @rcrumbs = reverse (@$crumbs);
|
---|
774 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
775 |
|
---|
776 | if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) {
|
---|
777 | $page->param(billinguser => $webvar{billinguser});
|
---|
778 | $page->param(custid => $webvar{custid});
|
---|
779 | $page->param(netaddr => $netblock->addr);
|
---|
780 | $page->param(masklen => $netblock->masklen);
|
---|
781 | }
|
---|
782 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
783 | "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
|
---|
784 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
785 | }
|
---|
786 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
787 | "'$webvar{alloctype}' ($msg)";
|
---|
788 | } else {
|
---|
789 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
790 | "'$webvar{alloctype}' by $authuser failed: '$msg'";
|
---|
791 | $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}' failed:");
|
---|
792 | $page->param(errmsg => $msg);
|
---|
793 | }
|
---|
794 |
|
---|
795 | } # end insertAssign()
|
---|
796 |
|
---|
797 |
|
---|
798 | # Does some basic checks on common input data to make sure nothing
|
---|
799 | # *really* weird gets in to the database through this script.
|
---|
800 | # Does NOT do complete input validation!!!
|
---|
801 | sub validateInput {
|
---|
802 | if ($webvar{city} eq '-') {
|
---|
803 | $page->param(err => 'Please choose a city');
|
---|
804 | return;
|
---|
805 | }
|
---|
806 |
|
---|
807 | # Alloctype check.
|
---|
808 | chomp $webvar{alloctype};
|
---|
809 | if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) {
|
---|
810 | # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
|
---|
811 | # managing to call things in such a way as to cause this deserves a cryptic error.
|
---|
812 | $page->param(err => 'Invalid alloctype');
|
---|
813 | return;
|
---|
814 | }
|
---|
815 |
|
---|
816 | # CustID check
|
---|
817 | # We have different handling for customer allocations and "internal" or "our" allocations
|
---|
818 | if ($def_custids{$webvar{alloctype}} eq '') {
|
---|
819 | if (!$webvar{custid}) {
|
---|
820 | $page->param(err => 'Please enter a customer ID.');
|
---|
821 | return;
|
---|
822 | }
|
---|
823 | # Crosscheck with billing.
|
---|
824 | my $status = CustIDCK->custid_exist($webvar{custid});
|
---|
825 | if ($CustIDCK::Error) {
|
---|
826 | $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
|
---|
827 | return;
|
---|
828 | }
|
---|
829 | if (!$status) {
|
---|
830 | $page->param(err => "Customer ID not valid. Make sure the Customer ID ".
|
---|
831 | "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
|
---|
832 | "non-customer assignments.");
|
---|
833 | return;
|
---|
834 | }
|
---|
835 | # print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
|
---|
836 | } else {
|
---|
837 | # New! Improved! And now Loaded From The Database!!
|
---|
838 | if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
|
---|
839 | $webvar{custid} = $def_custids{$webvar{alloctype}};
|
---|
840 | }
|
---|
841 | }
|
---|
842 |
|
---|
843 | ## hmmm.... is this even useful?
|
---|
844 | if (0) {
|
---|
845 | # Check POP location
|
---|
846 | my $flag;
|
---|
847 | if ($webvar{alloctype} eq 'rm') {
|
---|
848 | $flag = 'for a routed netblock';
|
---|
849 | foreach (@poplist) {
|
---|
850 | if (/^$webvar{city}$/) {
|
---|
851 | $flag = 'n';
|
---|
852 | last;
|
---|
853 | }
|
---|
854 | }
|
---|
855 | } else {
|
---|
856 | $flag = 'n';
|
---|
857 | ##fixme: hook to force-set POP or city on certain alloctypes
|
---|
858 | # if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
|
---|
859 | if ($webvar{pop} && $webvar{pop} =~ /^-$/) {
|
---|
860 | $flag = 'to route the block from/through';
|
---|
861 | }
|
---|
862 | }
|
---|
863 |
|
---|
864 | # if the alloctype has a restricted city/POP list as determined above,
|
---|
865 | # and the reqested city/POP does not match that list, complain
|
---|
866 | if ($flag ne 'n') {
|
---|
867 | $page->param(err => "Please choose a valid POP location $flag. Valid ".
|
---|
868 | "POP locations are currently:<br>\n".join (" - ", @poplist));
|
---|
869 | return;
|
---|
870 | }
|
---|
871 | }
|
---|
872 |
|
---|
873 | # VRF. Not a full validity check, just a basic sanity check.
|
---|
874 | if ($webvar{vrf}) {
|
---|
875 | # Trim leading and trailing whitespace first
|
---|
876 | $webvar{vrf} =~ s/^\s+//;
|
---|
877 | $webvar{vrf} =~ s/\s+$//;
|
---|
878 | if ($webvar{vrf} !~ /^[\w\d_.-]{1,32}$/) {
|
---|
879 | $page->param(err => "VRF values may only contain alphanumerics, and may not be more than 32 characters");
|
---|
880 | return;
|
---|
881 | }
|
---|
882 | }
|
---|
883 |
|
---|
884 | # VLAN. Should we allow/use VLAN names, or just the numeric ID?
|
---|
885 | if ($webvar{vlan}) {
|
---|
886 | # Trim leading and trailing whitespace first
|
---|
887 | $webvar{vlan} =~ s/^\s+//;
|
---|
888 | $webvar{vlan} =~ s/\s+$//;
|
---|
889 | # ... ve make it ze configurable thingy!
|
---|
890 | if ($IPDB::numeric_vlan) {
|
---|
891 | if ($webvar{vlan} !~ /^\d+$/) {
|
---|
892 | $page->param(err => "VLANs must be numeric");
|
---|
893 | return;
|
---|
894 | }
|
---|
895 | } else {
|
---|
896 | if ($webvar{vlan} !~ /^[\w\d_.-]+$/) {
|
---|
897 | $page->param(err => "VLANs must be alphanumeric");
|
---|
898 | return;
|
---|
899 | }
|
---|
900 | }
|
---|
901 | }
|
---|
902 |
|
---|
903 | # Backup fields. Minimal sanity checks.
|
---|
904 | for my $bkfield (qw(brand model)) {
|
---|
905 | if ($webvar{"bk$bkfield"} && $webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9\s_.-]+$/) {
|
---|
906 | $page->param(err => "Invalid characters in backup $bkfield");
|
---|
907 | return;
|
---|
908 | }
|
---|
909 | }
|
---|
910 | for my $bkfield (qw(type src user)) { # no spaces in these!
|
---|
911 | if ($webvar{"bk$bkfield"} && $webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9_.-]+$/) {
|
---|
912 | $page->param(err => "Invalid characters in backup $bkfield");
|
---|
913 | return;
|
---|
914 | }
|
---|
915 | }
|
---|
916 | if ($webvar{bkport}) {
|
---|
917 | $webvar{bkport} =~ s/^\s+//g;
|
---|
918 | $webvar{bkport} =~ s/\s+$//g;
|
---|
919 | if ($webvar{bkport} !~ /^\d+$/) {
|
---|
920 | $page->param(err => "Backup port must be numeric");
|
---|
921 | return;
|
---|
922 | }
|
---|
923 | }
|
---|
924 |
|
---|
925 | return 'OK';
|
---|
926 | } # end validateInput
|
---|
927 |
|
---|
928 |
|
---|
929 | # Displays details of a specific allocation in a form
|
---|
930 | # Allows update/delete
|
---|
931 | # action=edit
|
---|
932 | sub edit {
|
---|
933 |
|
---|
934 | # snag block info from db
|
---|
935 | my $blockinfo = getBlockData($ip_dbh, $webvar{id}, $webvar{basetype});
|
---|
936 | my $cidr = new NetAddr::IP $blockinfo->{block};
|
---|
937 | $page->param(id => $webvar{id});
|
---|
938 | $page->param(basetype => $webvar{basetype});
|
---|
939 |
|
---|
940 | # Tree navigation
|
---|
941 | my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
|
---|
942 | my @rcrumbs = reverse (@$crumbs);
|
---|
943 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
944 |
|
---|
945 | # Show link to IP list for pools
|
---|
946 | $page->param(ispool => 1) if $blockinfo->{type} =~ /^.[dp]$/;
|
---|
947 |
|
---|
948 | # Clean up extra whitespace on alloc type. Mainly a legacy-data cleanup.
|
---|
949 | $blockinfo->{type} =~ s/\s//;
|
---|
950 |
|
---|
951 | ##fixme: The case of "allocation larger than a /24" (or any similar case
|
---|
952 | # where the allocation is larger than the zone(s) in DNS) doesn't work well.
|
---|
953 | # Best solution may just be to add a warning that the entry shown may not be
|
---|
954 | # correct/complete.
|
---|
955 | if ($blockinfo->{revavail} || $blockinfo->{revpartial}) {
|
---|
956 | $page->param(showrev => ($blockinfo->{revavail} || $blockinfo->{revpartial}) );
|
---|
957 | my $cached;
|
---|
958 | # Get rDNS info; duplicates a bit of getBlockData but also does the RPC call if possible
|
---|
959 | ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser);
|
---|
960 | $page->param(rdns => $blockinfo->{rdns});
|
---|
961 | # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin
|
---|
962 | $page->param(cached => $cached);
|
---|
963 |
|
---|
964 | # Limit the per-IP rDNS list based on CIDR length; larger ones just take up too much space.
|
---|
965 | # Also, don't show on IP pools; the individual IPs will have a space for rDNS
|
---|
966 | # Don't show on single IPs; these use the "pattern" field
|
---|
967 | if ($IPDBacl{$authuser} =~ /c/
|
---|
968 | && $cidr->masklen != $cidr->bits
|
---|
969 | && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist
|
---|
970 | # config flag for "all block types" OR "not-a-pool-or-IP type"
|
---|
971 | && ($IPDB::revlistalltypes || $blockinfo->{type} !~ /^.[dpi]/)
|
---|
972 | # safety against trying to retrieve and display more than 1k (10 bits, /22 v4) worth of individual IPs
|
---|
973 | # ever. If you really need to manage a long list of IPs like that all in one place, you can use the DNS
|
---|
974 | # management tool. Even a /26 is a bit much, really.
|
---|
975 | && ($cidr->bits - $cidr->masklen) <= 10
|
---|
976 | # do we want to allow v6 at all?
|
---|
977 | #&& ! $cidr->{isv6}
|
---|
978 | ) {
|
---|
979 | $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type},
|
---|
980 | range => $blockinfo->{block}, user => $authuser) );
|
---|
981 | }
|
---|
982 | } # rDNS availability check
|
---|
983 |
|
---|
984 | # backup data
|
---|
985 | if ($blockinfo->{hasbk}) {
|
---|
986 | $page->param(hasbackup => $blockinfo->{hasbk});
|
---|
987 | for my $bkfield (qw(brand model src user vpass epass port)) {
|
---|
988 | $page->param("bk$bkfield" => $blockinfo->{"bk$bkfield"});
|
---|
989 | }
|
---|
990 | $page->param(bktelnet => 1) if $blockinfo->{bktype} eq 'telnet';
|
---|
991 | $page->param(bkssh => 1) if $blockinfo->{bktype} eq 'SSH';
|
---|
992 | }
|
---|
993 |
|
---|
994 | # consider extending this to show time as well as date
|
---|
995 | my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod};
|
---|
996 | $page->param(lastmod => $lastmod);
|
---|
997 |
|
---|
998 | $page->param(block => $blockinfo->{block});
|
---|
999 | $page->param(city => $blockinfo->{city});
|
---|
1000 | $page->param(custid => $blockinfo->{custid});
|
---|
1001 |
|
---|
1002 | ##fixme The check here should be built from the database
|
---|
1003 | # Need to expand to support pool types too
|
---|
1004 | if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
|
---|
1005 | $page->param(changetype => 1);
|
---|
1006 | $page->param(alloctype => [
|
---|
1007 | { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" },
|
---|
1008 | { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" },
|
---|
1009 | { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" },
|
---|
1010 | { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" },
|
---|
1011 | { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" },
|
---|
1012 | { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" },
|
---|
1013 | { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" },
|
---|
1014 | ]
|
---|
1015 | );
|
---|
1016 | } else {
|
---|
1017 | $page->param(disptype => $disp_alloctypes{$blockinfo->{type}});
|
---|
1018 | $page->param(type => $blockinfo->{type});
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | ## node hack
|
---|
1022 | my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block});
|
---|
1023 | # $page->param(havenodeid => $nodeid);
|
---|
1024 | $page->param(nodename => $nodename);
|
---|
1025 |
|
---|
1026 | ##fixme: this whole hack needs cleanup and generalization for all alloctypes
|
---|
1027 | ##fixme: arguably a bug that presence of a nodeid implies it can be changed..
|
---|
1028 | if ($IPDBacl{$authuser} =~ /c/) {
|
---|
1029 | my $nlist = getNodeList($ip_dbh);
|
---|
1030 | if ($nodeid) {
|
---|
1031 | foreach (@{$nlist}) {
|
---|
1032 | $$_{selme} = ($$_{node_id} == $nodeid);
|
---|
1033 | }
|
---|
1034 | }
|
---|
1035 | $page->param(nodelist => $nlist);
|
---|
1036 | }
|
---|
1037 | ## end node hack
|
---|
1038 |
|
---|
1039 | $page->param(vrf => $blockinfo->{vrf});
|
---|
1040 | $page->param(vlan => $blockinfo->{vlan});
|
---|
1041 |
|
---|
1042 | # Reserved-for-expansion
|
---|
1043 | $page->param(reserve => $blockinfo->{reserve});
|
---|
1044 | $page->param(reserve_id => $blockinfo->{reserve_id});
|
---|
1045 | my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
|
---|
1046 | $page->param(newblock => $newblock);
|
---|
1047 |
|
---|
1048 | # not happy with the upside-down logic, but...
|
---|
1049 | $page->param(swipable => $blockinfo->{type} !~ /.i/);
|
---|
1050 | $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip};
|
---|
1051 |
|
---|
1052 | $page->param(circid => $blockinfo->{circuitid});
|
---|
1053 | $page->param(desc => $blockinfo->{description});
|
---|
1054 | $page->param(notes => $blockinfo->{notes});
|
---|
1055 |
|
---|
1056 | # Check to see if we can display sensitive data
|
---|
1057 | $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
|
---|
1058 | $page->param(privdata => $blockinfo->{privdata});
|
---|
1059 |
|
---|
1060 | # ACL trickery - these two template booleans control the presence of all form/input tags
|
---|
1061 | $page->param(maychange => $IPDBacl{$authuser} =~ /c/);
|
---|
1062 | $page->param(maydel => $IPDBacl{$authuser} =~ /d/);
|
---|
1063 |
|
---|
1064 | # Need to find internal knobs to twist to actually vary these. (Ab)use "change" flag for now
|
---|
1065 | $page->param(maymerge => ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/));
|
---|
1066 | if ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/) {
|
---|
1067 | if ($blockinfo->{type} =~ /^.p$/) {
|
---|
1068 | # PPP pools
|
---|
1069 | $page->param(maysplit => 1) if $cidr->masklen+1 < $cidr->bits;
|
---|
1070 | } elsif ($blockinfo->{type} =~ /.d/) {
|
---|
1071 | # Non-PPP pools
|
---|
1072 | $page->param(maysplit => 1) if $cidr->masklen+2 < $cidr->bits;
|
---|
1073 | } else {
|
---|
1074 | # Standard netblocks. Arguably allowing splitting these down to single IPs
|
---|
1075 | # doesn't make much sense, but forcing users to apply allocation types
|
---|
1076 | # "properly" is worse than herding cats.
|
---|
1077 | $page->param(maysplit => 1) if $cidr->masklen < $cidr->bits;
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | } # edit()
|
---|
1082 |
|
---|
1083 |
|
---|
1084 | # Stuff new info about a block into the db
|
---|
1085 | # action=update
|
---|
1086 | sub update {
|
---|
1087 | if ($IPDBacl{$authuser} !~ /c/) {
|
---|
1088 | $aclerr = 'updateblock';
|
---|
1089 | return;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | # Collect existing block info here, since we need it for the breadcrumb nav
|
---|
1093 | my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
|
---|
1094 | my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
|
---|
1095 | my @rcrumbs = reverse (@$crumbs);
|
---|
1096 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
1097 |
|
---|
1098 | # Make sure incoming data is in correct format - custID among other things.
|
---|
1099 | return if !validateInput;
|
---|
1100 |
|
---|
1101 | $webvar{swip} = 'n' if !$webvar{swip};
|
---|
1102 |
|
---|
1103 | my %updargs = (
|
---|
1104 | custid => $webvar{custid},
|
---|
1105 | city => $webvar{city},
|
---|
1106 | description => $webvar{desc},
|
---|
1107 | notes => $webvar{notes},
|
---|
1108 | circuitid => $webvar{circid},
|
---|
1109 | block => $webvar{block},
|
---|
1110 | type => $webvar{alloctype},
|
---|
1111 | swip => $webvar{swip},
|
---|
1112 | rdns => $webvar{rdns},
|
---|
1113 | vrf => $webvar{vrf},
|
---|
1114 | vlan => $webvar{vlan},
|
---|
1115 | user => $authuser,
|
---|
1116 | );
|
---|
1117 |
|
---|
1118 | # Check to see if user is allowed to do anything with sensitive data
|
---|
1119 | if ($IPDBacl{$authuser} =~ /s/) {
|
---|
1120 | $updargs{privdata} = $webvar{privdata};
|
---|
1121 | for my $bkfield (qw(brand model type src user vpass epass port)) {
|
---|
1122 | $updargs{"bk$bkfield"} = $webvar{"bk$bkfield"};
|
---|
1123 | }
|
---|
1124 | $updargs{backupfields} = $webvar{backupfields};
|
---|
1125 | } else {
|
---|
1126 | # If the user doesn't have permissions to monkey with NOC-things, pass
|
---|
1127 | # a flag so we don't treat it as "backup data removed"
|
---|
1128 | $updargs{ignorebk} = 1;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | # Semioptional values
|
---|
1132 | $updargs{node} = $webvar{node} if $webvar{node};
|
---|
1133 |
|
---|
1134 | # collect per-IP rDNS fields. only copy over the ones that actually have something in them.
|
---|
1135 | my %iprev;
|
---|
1136 | foreach (keys %webvar) {
|
---|
1137 | $iprev{$_} = $webvar{$_} if /host_[\d.a-fA-F:]+/ && $webvar{$_};
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | # Merge with reserved freeblock
|
---|
1141 | $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme};
|
---|
1142 |
|
---|
1143 | my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
|
---|
1144 |
|
---|
1145 | if ($code eq 'FAIL') {
|
---|
1146 | syslog "err", "$authuser could not update block/IP '$binfo->{block}' (id $webvar{block}): '$msg'";
|
---|
1147 | $page->param(err => "Could not update block/IP $binfo->{block}: $msg");
|
---|
1148 | return;
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | # If we get here, the operation succeeded.
|
---|
1152 | syslog "notice", "$authuser updated $binfo->{block}";
|
---|
1153 | ##fixme: log details of the change? old way is in the .debug stream anyway.
|
---|
1154 | ##fixme: need to wedge something in to allow "update:field" notifications
|
---|
1155 | ## hmm. how to tell what changed? O_o
|
---|
1156 | mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $binfo->{block}",
|
---|
1157 | "$binfo->{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
|
---|
1158 |
|
---|
1159 | ## node hack
|
---|
1160 | if ($webvar{node} && $webvar{node} ne '-') {
|
---|
1161 | my $nodename = getNodeName($ip_dbh, $webvar{node});
|
---|
1162 | $page->param(nodename => $nodename);
|
---|
1163 | }
|
---|
1164 | ## end node hack
|
---|
1165 |
|
---|
1166 | # Link back to browse-routed or list-pool page on "Update complete" page.
|
---|
1167 | my $pblock = getBlockData($ip_dbh, $binfo->{parent_id});
|
---|
1168 | $page->param(backid => $binfo->{parent_id});
|
---|
1169 | $page->param(backblock => $pblock->{block});
|
---|
1170 | $page->param(backpool => ($webvar{basetype} eq 'i'));
|
---|
1171 |
|
---|
1172 | # Do some HTML fiddling here instead of using ESCAPE=HTML in the template,
|
---|
1173 | # because otherwise we can't convert \n to <br>. *sigh*
|
---|
1174 | $webvar{notes} = $q->escapeHTML($webvar{notes}); # escape first...
|
---|
1175 | $webvar{notes} =~ s/\n/<br>\n/; # ... then convert newlines
|
---|
1176 | $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : " ");
|
---|
1177 | $webvar{privdata} =~ s/\n/<br>\n/;
|
---|
1178 |
|
---|
1179 | if ($webvar{expandme}) {
|
---|
1180 | # this is fugly but still faster than hitting the DB again with getBlockData()
|
---|
1181 | my $tmp = new NetAddr::IP $binfo->{block};
|
---|
1182 | my $fb = new NetAddr::IP $binfo->{reserve};
|
---|
1183 | my @newblock = $tmp->compact($fb);
|
---|
1184 | $page->param(cidr => $newblock[0]);
|
---|
1185 | } else {
|
---|
1186 | $page->param(cidr => $binfo->{block});
|
---|
1187 | }
|
---|
1188 | $page->param(rdns => $webvar{rdns});
|
---|
1189 | $page->param(city => $webvar{city});
|
---|
1190 | $page->param(disptype => $disp_alloctypes{$webvar{alloctype}});
|
---|
1191 | $page->param(custid => $webvar{custid});
|
---|
1192 | $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No');
|
---|
1193 | $page->param(circid => $webvar{circid});
|
---|
1194 | $page->param(desc => $webvar{desc});
|
---|
1195 | $page->param(notes => $webvar{notes});
|
---|
1196 | if ($IPDBacl{$authuser} =~ /s/) {
|
---|
1197 | $page->param(nocling => 1);
|
---|
1198 | $page->param(privdata => $webvar{privdata});
|
---|
1199 | if ($binfo->{hasbk}) {
|
---|
1200 | $page->param(hasbackup => $binfo->{hasbk});
|
---|
1201 | for my $bkfield (qw(brand model type src user vpass epass port)) {
|
---|
1202 | $page->param("bk$bkfield" => $binfo->{"bk$bkfield"});
|
---|
1203 | }
|
---|
1204 | }
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | } # update()
|
---|
1208 |
|
---|
1209 |
|
---|
1210 | sub prepSplit {
|
---|
1211 | if ($IPDBacl{$authuser} !~ /c/) {
|
---|
1212 | $aclerr = 'splitblock';
|
---|
1213 | return;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | my $blockinfo = getBlockData($ip_dbh, $webvar{block});
|
---|
1217 |
|
---|
1218 | # Tree navigation
|
---|
1219 | my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
|
---|
1220 | my @rcrumbs = reverse (@$crumbs);
|
---|
1221 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
1222 |
|
---|
1223 | if ($blockinfo->{type} =~ /^.i$/) {
|
---|
1224 | $page->param(err => "Can't split a single IP allocation");
|
---|
1225 | return;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | # Info about current allocation
|
---|
1229 | $page->param(oldblock => $blockinfo->{block});
|
---|
1230 | $page->param(block => $webvar{block});
|
---|
1231 |
|
---|
1232 | # Note that there are probably different rules that should be followed to restrict splitting IPv6 blocks;
|
---|
1233 | # strictly speaking it will be exceptionally rare to see smaller than a /64 assigned to a customer, since that
|
---|
1234 | # breaks auto-addressing schemes.
|
---|
1235 |
|
---|
1236 | # Generate possible splits
|
---|
1237 | my $block = new NetAddr::IP $blockinfo->{block};
|
---|
1238 | my $oldmask = $block->masklen;
|
---|
1239 | if ($blockinfo->{type} =~ /^.d$/) {
|
---|
1240 | # Non-PPP pools
|
---|
1241 | $page->param(ispool => 1);
|
---|
1242 | if ($oldmask+2 >= $block->bits) {
|
---|
1243 | $page->param(err => "Can't split a standard netblock pool any further");
|
---|
1244 | return;
|
---|
1245 | }
|
---|
1246 | # Allow splitting down to v4 /30 (which results in one usable IP; dubiously useful)
|
---|
1247 | $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-2;
|
---|
1248 | } elsif ($blockinfo->{type} =~ /.p/) {
|
---|
1249 | $page->param(ispool => 1);
|
---|
1250 | # Allow splitting PPP pools down to v4 /31
|
---|
1251 | $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-1;
|
---|
1252 | } else {
|
---|
1253 | # Allow splitting all other non-pool netblocks down to single IPs, which...
|
---|
1254 | # arguably should be *aggregated* in a pool. Except where they shouldn't.
|
---|
1255 | $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits;
|
---|
1256 | }
|
---|
1257 | # set the split-in-half mask
|
---|
1258 | $page->param(sp2mask => $oldmask+1);
|
---|
1259 |
|
---|
1260 | # Generate possible shrink targets
|
---|
1261 | my @keepers = $block->split($block->masklen+1);
|
---|
1262 | $page->param(newblockA => $keepers[0]);
|
---|
1263 | $page->param(newblockB => $keepers[1]);
|
---|
1264 | } # prepSplit()
|
---|
1265 |
|
---|
1266 |
|
---|
1267 | sub doSplit {
|
---|
1268 | if ($IPDBacl{$authuser} !~ /c/) {
|
---|
1269 | $aclerr = 'splitblock';
|
---|
1270 | return;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | ##fixme: need consistent way to identify "this thing that is this thing" with only the ID
|
---|
1274 | # also applies to other locations
|
---|
1275 | my $blockinfo = getBlockData($ip_dbh, $webvar{block});
|
---|
1276 |
|
---|
1277 | # Tree navigation
|
---|
1278 | my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
|
---|
1279 | my @rcrumbs = reverse (@$crumbs);
|
---|
1280 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
1281 |
|
---|
1282 | if ($blockinfo->{type} =~ /^.i$/) {
|
---|
1283 | $page->param(err => "Can't split a single IP allocation");
|
---|
1284 | return;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | if ($webvar{subact} eq 'split') {
|
---|
1288 | $page->param(issplit => 1);
|
---|
1289 | my $block = new NetAddr::IP $blockinfo->{block};
|
---|
1290 | my $newblocks = splitBlock($ip_dbh, id => $webvar{block}, basetype => 'b', newmask => $webvar{split},
|
---|
1291 | user => $authuser);
|
---|
1292 | if ($newblocks) {
|
---|
1293 | $page->param(newblocks => $newblocks);
|
---|
1294 | } else {
|
---|
1295 | $page->param(err => $IPDB::errstr);
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | } elsif ($webvar{subact} eq 'shrink') {
|
---|
1299 | $page->param(nid => $webvar{block});
|
---|
1300 | $page->param(newblock => $webvar{shrink});
|
---|
1301 | my $newfree = shrinkBlock($ip_dbh, $webvar{block}, $webvar{shrink});
|
---|
1302 | if ($newfree) {
|
---|
1303 | $page->param(newfb => $newfree);
|
---|
1304 | } else {
|
---|
1305 | $page->param(err => $IPDB::errstr);
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | } else {
|
---|
1309 | # Your llama is on fire.
|
---|
1310 | $page->param(err => "Missing form field that shouldn't be missing.");
|
---|
1311 | return;
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | # common bits
|
---|
1315 | $page->param(cidr => $blockinfo->{block});
|
---|
1316 | # and the backlink to the parent container
|
---|
1317 | my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id});
|
---|
1318 | $page->param(backid => $blockinfo->{parent_id});
|
---|
1319 | $page->param(backblock => $pinfo->{block});
|
---|
1320 | } # doSplit()
|
---|
1321 |
|
---|
1322 |
|
---|
1323 | # Set up for merge
|
---|
1324 | sub prepMerge {
|
---|
1325 | my $binfo = getBlockData($ip_dbh, $webvar{block});
|
---|
1326 |
|
---|
1327 | # Tree navigation
|
---|
1328 | my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
|
---|
1329 | my @rcrumbs = reverse (@$crumbs);
|
---|
1330 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
1331 |
|
---|
1332 | $page->param(block => $webvar{block});
|
---|
1333 | $page->param(ispool => $binfo->{type} =~ /.[dp]/);
|
---|
1334 | $page->param(ismaster => $binfo->{type} eq 'mm');
|
---|
1335 | $page->param(oldblock => $binfo->{block});
|
---|
1336 | $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
|
---|
1337 | $page->param(typelist => getTypeList($ip_dbh, 'n', $binfo->{type})); # down the rabbit hole we go...
|
---|
1338 |
|
---|
1339 | # Strings for scope; do this way so we don't have to edit them many places
|
---|
1340 | $page->param(vis_keepall => $merge_display{keepall});
|
---|
1341 | $page->param(vis_mergepeer => $merge_display{mergepeer});
|
---|
1342 | $page->param(vis_clearpeer => $merge_display{clearpeer});
|
---|
1343 | $page->param(vis_clearall => $merge_display{clearall});
|
---|
1344 |
|
---|
1345 | } # prepMerge()
|
---|
1346 |
|
---|
1347 |
|
---|
1348 | # Show what will be merged, present warnings about data loss
|
---|
1349 | sub confMerge {
|
---|
1350 | if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) {
|
---|
1351 | $page->param(err => 'New netmask required');
|
---|
1352 | return;
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | $page->param(block => $webvar{block});
|
---|
1356 | my $binfo = getBlockData($ip_dbh, $webvar{block});
|
---|
1357 | my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
|
---|
1358 | my $minfo = getBlockData($ip_dbh, $binfo->{master_id});
|
---|
1359 | my $block = new NetAddr::IP $binfo->{block};
|
---|
1360 |
|
---|
1361 | # Tree navigation
|
---|
1362 | my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
|
---|
1363 | my @rcrumbs = reverse (@$crumbs);
|
---|
1364 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
1365 |
|
---|
1366 | $page->param(oldblock => $binfo->{block});
|
---|
1367 | $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
|
---|
1368 | $page->param(ismaster => $binfo->{type} eq 'mm');
|
---|
1369 | $page->param(ispool => $webvar{alloctype} =~ /.[dp]/);
|
---|
1370 | $page->param(isleaf => $webvar{alloctype} =~ /.[enr]/);
|
---|
1371 | $page->param(newtype => $webvar{alloctype});
|
---|
1372 | $page->param(newdisptype => $disp_alloctypes{$webvar{alloctype}});
|
---|
1373 | my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}";
|
---|
1374 | $newblock = $newblock->network;
|
---|
1375 | $page->param(newmask => $webvar{newmask});
|
---|
1376 | $page->param(newblock => "$newblock");
|
---|
1377 |
|
---|
1378 | # get list of allocations and freeblocks to be merged
|
---|
1379 | my $malloc_list = listForMerge($ip_dbh, $binfo->{parent_id}, $newblock, 'a');
|
---|
1380 | $page->param(mergealloc => $malloc_list);
|
---|
1381 |
|
---|
1382 | $page->param(vis_scope => $merge_display{$webvar{scope}});
|
---|
1383 | $page->param(scope => $webvar{scope});
|
---|
1384 | } # confMerge()
|
---|
1385 |
|
---|
1386 |
|
---|
1387 | # Make it so
|
---|
1388 | sub doMerge {
|
---|
1389 | if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) {
|
---|
1390 | $page->param(err => 'New netmask required');
|
---|
1391 | return;
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 | $page->param(block => $webvar{block});
|
---|
1395 | my $binfo = getBlockData($ip_dbh, $webvar{block});
|
---|
1396 | my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id});
|
---|
1397 | my $block = new NetAddr::IP $binfo->{block};
|
---|
1398 |
|
---|
1399 | # Tree navigation
|
---|
1400 | my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id});
|
---|
1401 | my @rcrumbs = reverse (@$crumbs);
|
---|
1402 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
1403 |
|
---|
1404 | $page->param(oldblock => $binfo->{block});
|
---|
1405 | $page->param(oldtype => $disp_alloctypes{$binfo->{type}});
|
---|
1406 | $page->param(newdisptype => $disp_alloctypes{$webvar{newtype}});
|
---|
1407 | my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}";
|
---|
1408 | $newblock = $newblock->network;
|
---|
1409 | $page->param(newblock => $newblock);
|
---|
1410 | $page->param(vis_scope => $merge_display{$webvar{scope}});
|
---|
1411 |
|
---|
1412 | my $mlist = mergeBlocks($ip_dbh, $webvar{block}, %webvar, user => $authuser);
|
---|
1413 |
|
---|
1414 | if ($mlist) {
|
---|
1415 | #(newtype => $webvar{newtype}, newmask => $webvar{newmask}));
|
---|
1416 | # Slice off first entry (the new parent - note this may be a new allocation,
|
---|
1417 | # not the same ID that was "merged"!
|
---|
1418 | my $parent = shift @$mlist;
|
---|
1419 | $page->param(backpool => $webvar{newtype} =~ /.[dp]/);
|
---|
1420 | if ($webvar{newtype} =~ /.[enr]/) {
|
---|
1421 | $page->param(backleaf => 1);
|
---|
1422 | $page->param(backid => $binfo->{parent_id});
|
---|
1423 | $page->param(backblock => $pinfo->{block});
|
---|
1424 | } else {
|
---|
1425 | $page->param(backid => $parent->{id});
|
---|
1426 | $page->param(backblock => $parent->{block});
|
---|
1427 | }
|
---|
1428 | $page->param(mergelist => $mlist);
|
---|
1429 | } else {
|
---|
1430 | $page->param(err => "Merge failed: $IPDB::errstr");
|
---|
1431 | }
|
---|
1432 | } # doMerge()
|
---|
1433 |
|
---|
1434 |
|
---|
1435 | # Delete an allocation.
|
---|
1436 | sub remove {
|
---|
1437 | if ($IPDBacl{$authuser} !~ /d/) {
|
---|
1438 | $aclerr = 'delblock';
|
---|
1439 | return;
|
---|
1440 | }
|
---|
1441 |
|
---|
1442 | # Serves'em right for getting here...
|
---|
1443 | if (!defined($webvar{block})) {
|
---|
1444 | $page->param(err => "Can't delete a block that doesn't exist");
|
---|
1445 | return;
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | my $blockdata;
|
---|
1449 | $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
|
---|
1450 |
|
---|
1451 | # Tree navigation
|
---|
1452 | my $crumbs = getBreadCrumbs($ip_dbh, $blockdata->{parent_id});
|
---|
1453 | my @rcrumbs = reverse (@$crumbs);
|
---|
1454 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
1455 |
|
---|
1456 | if ($blockdata->{parent_id} == 0) { # $webvar{alloctype} eq 'mm'
|
---|
1457 | $blockdata->{city} = "N/A";
|
---|
1458 | $blockdata->{custid} = "N/A";
|
---|
1459 | $blockdata->{circuitid} = "N/A";
|
---|
1460 | $blockdata->{description} = "N/A";
|
---|
1461 | $blockdata->{notes} = "N/A";
|
---|
1462 | $blockdata->{privdata} = "N/A";
|
---|
1463 | } # end cases for different alloctypes
|
---|
1464 |
|
---|
1465 | $page->param(blockid => $webvar{block});
|
---|
1466 | $page->param(basetype => $webvar{basetype});
|
---|
1467 |
|
---|
1468 | $page->param(block => $blockdata->{block});
|
---|
1469 | $page->param(rdns => $blockdata->{rdns});
|
---|
1470 |
|
---|
1471 | # maybe need to apply more magic here?
|
---|
1472 | # most allocations we *do* want to autodelete the forward as well as reverse; for a handful we don't.
|
---|
1473 | # -> all real blocks (nb: pool IPs need extra handling)
|
---|
1474 | # -> NOC/private-IP (how to ID?)
|
---|
1475 | # -> anything with a pattern matching $IPDB::domain?
|
---|
1476 | if ($blockdata->{type} !~ /^.i$/) {
|
---|
1477 | $page->param(autodel => 1);
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | $page->param(disptype => $disp_alloctypes{$blockdata->{type}});
|
---|
1481 | $page->param(city => $blockdata->{city});
|
---|
1482 | $page->param(custid => $blockdata->{custid});
|
---|
1483 | $page->param(circid => $blockdata->{circuitid});
|
---|
1484 | $page->param(desc => $blockdata->{description});
|
---|
1485 | $blockdata->{notes} = $q->escapeHTML($blockdata->{notes});
|
---|
1486 | $blockdata->{notes} =~ s/\n/<br>\n/;
|
---|
1487 | $page->param(notes => $blockdata->{notes});
|
---|
1488 | $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata});
|
---|
1489 | $blockdata->{privdata} = ' ' if !$blockdata->{privdata};
|
---|
1490 | $blockdata->{privdata} =~ s/\n/<br>\n/;
|
---|
1491 | $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/;
|
---|
1492 | $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/);
|
---|
1493 |
|
---|
1494 | } # end remove()
|
---|
1495 |
|
---|
1496 |
|
---|
1497 | # Delete an allocation. Return it to the freeblocks table; munge
|
---|
1498 | # data as necessary to keep as few records as possible in freeblocks
|
---|
1499 | # to prevent weirdness when allocating blocks later.
|
---|
1500 | # Remove IPs from pool listing if necessary
|
---|
1501 | sub finalDelete {
|
---|
1502 | if ($IPDBacl{$authuser} !~ /d/) {
|
---|
1503 | $aclerr = 'delblock';
|
---|
1504 | return;
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | # need to retrieve block data before deleting so we can notify on that
|
---|
1508 | my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype});
|
---|
1509 | my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b');
|
---|
1510 |
|
---|
1511 | # Tree navigation
|
---|
1512 | my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id});
|
---|
1513 | my @rcrumbs = reverse (@$crumbs);
|
---|
1514 | $utilbar->param(breadcrumb => \@rcrumbs);
|
---|
1515 |
|
---|
1516 | my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser);
|
---|
1517 |
|
---|
1518 | $page->param(block => $blockinfo->{block});
|
---|
1519 | $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}}));
|
---|
1520 | $page->param(delparent_id => $blockinfo->{parent_id});
|
---|
1521 | if ($pinfo) {
|
---|
1522 | $page->param(delparent => $pinfo->{block});
|
---|
1523 | $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}}));
|
---|
1524 | }
|
---|
1525 | $page->param(returnpool => ($webvar{basetype} eq 'i') );
|
---|
1526 | if ($code =~ /^WARN(POOL|MERGE)/) {
|
---|
1527 | my ($pid,$pcidr) = split /,/, $msg;
|
---|
1528 | my $real_pinfo = getBlockData($ip_dbh, $pid, 'b');
|
---|
1529 | $page->param(parent_id => $pid);
|
---|
1530 | $page->param(parent => $pcidr);
|
---|
1531 | $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}}));
|
---|
1532 | $page->param(mergeip => $code eq 'WARNPOOL');
|
---|
1533 | }
|
---|
1534 | if ($code eq 'WARN') {
|
---|
1535 | $msg =~ s/\n/<br>\n/g;
|
---|
1536 | $page->param(genwarn => $msg);
|
---|
1537 | }
|
---|
1538 | if ($code eq 'OK' || $code =~ /^WARN/) {
|
---|
1539 | syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock $webvar{block} ".
|
---|
1540 | $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'";
|
---|
1541 | mailNotify($ip_dbh, 'da', "REMOVED: ".$disp_alloctypes{$blockinfo->{type}}." $webvar{block}",
|
---|
1542 | $disp_alloctypes{$blockinfo->{type}}." $webvar{block} deallocated by $authuser\n".
|
---|
1543 | "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}.
|
---|
1544 | "\nDescription: ".$blockinfo->{description}."\n");
|
---|
1545 | } else {
|
---|
1546 | $page->param(failmsg => $msg);
|
---|
1547 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
1548 | syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
|
---|
1549 | } else {
|
---|
1550 | syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
|
---|
1551 | $page->param(netblock => 1);
|
---|
1552 | }
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | } # finalDelete
|
---|