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