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