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