| 1 | #!/usr/bin/perl | 
|---|
| 2 | # ipdb/cgi-bin/main.cgi | 
|---|
| 3 | ## | 
|---|
| 4 | # $Id: main.cgi 951 2024-08-02 15:45:49Z kdeugau $ | 
|---|
| 5 | # Copyright (C) 2004-2018,2022-2024 - Kris Deugau <kdeugau@deepnet.cx> | 
|---|
| 6 | # | 
|---|
| 7 | #    This program is free software: you can redistribute it and/or modify | 
|---|
| 8 | #    it under the terms of the GNU General Public License as published by | 
|---|
| 9 | #    the Free Software Foundation, either version 3 of the License, or | 
|---|
| 10 | #    (at your option) any later version. | 
|---|
| 11 | # | 
|---|
| 12 | #    This program is distributed in the hope that it will be useful, | 
|---|
| 13 | #    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 14 | #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 15 | #    GNU General Public License for more details. | 
|---|
| 16 | # | 
|---|
| 17 | #    You should have received a copy of the GNU General Public License | 
|---|
| 18 | #    along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 19 | ## | 
|---|
| 20 |  | 
|---|
| 21 | use strict; | 
|---|
| 22 | use warnings; | 
|---|
| 23 | use CGI::Carp qw(fatalsToBrowser); | 
|---|
| 24 | use CGI::Simple; | 
|---|
| 25 | use HTML::Template; | 
|---|
| 26 | use DBI; | 
|---|
| 27 | use POSIX qw(ceil); | 
|---|
| 28 | use NetAddr::IP; | 
|---|
| 29 | use Frontier::Client; | 
|---|
| 30 |  | 
|---|
| 31 | use Sys::Syslog; | 
|---|
| 32 |  | 
|---|
| 33 | # don't remove!  required for GNU/FHS-ish install from tarball | 
|---|
| 34 | ##uselib## | 
|---|
| 35 |  | 
|---|
| 36 | # Taint-safe (ish) voodoo to push "the directory the script is in" into @INC. | 
|---|
| 37 | use File::Spec (); | 
|---|
| 38 | use File::Basename (); | 
|---|
| 39 | my $path; | 
|---|
| 40 | BEGIN { | 
|---|
| 41 | $path = File::Basename::dirname(File::Spec->rel2abs($0)); | 
|---|
| 42 | if ($path =~ /(.*)/) { | 
|---|
| 43 | $path = $1; | 
|---|
| 44 | } | 
|---|
| 45 | } | 
|---|
| 46 | use lib $path; | 
|---|
| 47 |  | 
|---|
| 48 | use CustIDCK; | 
|---|
| 49 | use MyIPDB; | 
|---|
| 50 |  | 
|---|
| 51 | openlog "IPDB","pid","$IPDB::syslog_facility"; | 
|---|
| 52 |  | 
|---|
| 53 | ## Environment.  Collect some things, process some things, set some things... | 
|---|
| 54 |  | 
|---|
| 55 | # Collect the username from HTTP auth.  If undefined, we're in | 
|---|
| 56 | # a test environment, or called without a username. | 
|---|
| 57 | my $authuser; | 
|---|
| 58 | if (!defined($ENV{'REMOTE_USER'})) { | 
|---|
| 59 | $authuser = '__temptest'; | 
|---|
| 60 | } else { | 
|---|
| 61 | $authuser = $ENV{'REMOTE_USER'}; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | # anyone got a better name?  :P | 
|---|
| 65 | my $thingroot = $ENV{SCRIPT_FILENAME}; | 
|---|
| 66 | $thingroot =~ s|cgi-bin/main.cgi||; | 
|---|
| 67 |  | 
|---|
| 68 | ##fixme there *must* be a better order to do things in so this can go back where it was | 
|---|
| 69 | # CGI fiddling done here so we can declare %webvar so we can alter $webvar{action} | 
|---|
| 70 | # to show the right page on DB errors. | 
|---|
| 71 | # Set up the CGI object... | 
|---|
| 72 | my $q = new CGI::Simple; | 
|---|
| 73 | # ... and get query-string params as well as POST params if necessary | 
|---|
| 74 | $q->parse_query_string; | 
|---|
| 75 |  | 
|---|
| 76 | # Convenience;  saves changing all references to %webvar | 
|---|
| 77 | ##fixme:  tweak for handling <select multiple='y' size=3> (list with multiple selection) | 
|---|
| 78 | my %webvar = $q->Vars; | 
|---|
| 79 |  | 
|---|
| 80 | # Why not a global DB handle?  (And a global statement handle, as well...) | 
|---|
| 81 | # Use the connectDB function, otherwise we end up confusing ourselves | 
|---|
| 82 | my $ip_dbh; | 
|---|
| 83 | my $errstr; | 
|---|
| 84 | ($ip_dbh,$errstr) = connectDB_My; | 
|---|
| 85 | if (!$ip_dbh) { | 
|---|
| 86 | $webvar{action} = "dberr"; | 
|---|
| 87 | } else { | 
|---|
| 88 | initIPDBGlobals($ip_dbh); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | syslog "debug", "'$authuser active', $ENV{'REMOTE_ADDR'}, '$IPDBacl{$authuser}'"; | 
|---|
| 92 |  | 
|---|
| 93 | # Set up some globals | 
|---|
| 94 | $ENV{HTML_TEMPLATE_ROOT} = $thingroot; | 
|---|
| 95 | my @templatepath = [ "localtemplates", "templates" ]; | 
|---|
| 96 |  | 
|---|
| 97 | my $header = HTML::Template->new(filename => "header.tmpl", path => @templatepath); | 
|---|
| 98 | my $footer = HTML::Template->new(filename => "footer.tmpl", path => @templatepath); | 
|---|
| 99 | my $utilbar = HTML::Template->new(filename => "utilbar.tmpl", loop_context_vars => 1, global_vars => 1, | 
|---|
| 100 | path => @templatepath); | 
|---|
| 101 |  | 
|---|
| 102 | print "Content-type: text/html\n\n"; | 
|---|
| 103 |  | 
|---|
| 104 | $header->param(version => $IPDB::VERSION); | 
|---|
| 105 | $header->param(addperm => $IPDBacl{$authuser} =~ /a/); | 
|---|
| 106 | $header->param(webpath => $IPDB::webpath); | 
|---|
| 107 |  | 
|---|
| 108 | $utilbar->param(webpath => $IPDB::webpath); | 
|---|
| 109 |  | 
|---|
| 110 | print $header->output; | 
|---|
| 111 |  | 
|---|
| 112 | ##fixme:  whine and complain when the user is not present in the ACL hash above | 
|---|
| 113 |  | 
|---|
| 114 | #main() | 
|---|
| 115 | my $aclerr; | 
|---|
| 116 |  | 
|---|
| 117 | if(!defined($webvar{action})) { | 
|---|
| 118 | $webvar{action} = "index";    #shuts up the warnings. | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | my $page; | 
|---|
| 122 | if (-e "$ENV{HTML_TEMPLATE_ROOT}/templates/$webvar{action}.tmpl") { | 
|---|
| 123 | $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1, | 
|---|
| 124 | path => @templatepath); | 
|---|
| 125 | } else { | 
|---|
| 126 | $page = HTML::Template->new(filename => "dunno.tmpl", die_on_bad_params => 0, | 
|---|
| 127 | path => @templatepath); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | if($webvar{action} eq 'index') { | 
|---|
| 131 | showSummary(); | 
|---|
| 132 | } elsif ($webvar{action} eq 'showvrf') { | 
|---|
| 133 | showVRF(); | 
|---|
| 134 |  | 
|---|
| 135 | } elsif ($webvar{action} eq 'addvrf') { | 
|---|
| 136 | if ($IPDBacl{$authuser} !~ /s/) { | 
|---|
| 137 | $aclerr = 'addvrf'; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | # Retrieve the list of DNS locations if we've got a place to grab them from | 
|---|
| 141 | if ($IPDB::rpc_url) { | 
|---|
| 142 | my %rpcargs = ( | 
|---|
| 143 | rpcuser => $authuser, | 
|---|
| 144 | group => 1,     # bleh | 
|---|
| 145 | defloc => '', | 
|---|
| 146 | ); | 
|---|
| 147 | my $result = IPDB::_rpc('getLocDropdown', %rpcargs); | 
|---|
| 148 | $page->param(loclist => $result); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | } elsif ($webvar{action} eq 'newvrf') { | 
|---|
| 152 | if ($IPDBacl{$authuser} !~ /s/) { | 
|---|
| 153 | $aclerr = 'addvrf'; | 
|---|
| 154 | } else { | 
|---|
| 155 | my ($code,$msg) = addVRF($ip_dbh, $webvar{vrf}, comment => $webvar{comment}, location => $webvar{loc}); | 
|---|
| 156 |  | 
|---|
| 157 | if ($code eq 'FAIL') { | 
|---|
| 158 | syslog "err", "Could not add VRF '$webvar{vrf}' to database: '$msg'"; | 
|---|
| 159 | $page->param(err => $msg); | 
|---|
| 160 | $page->param(vrf => $webvar{vrf}); | 
|---|
| 161 | } else { | 
|---|
| 162 | $page->param(vrf => $msg); | 
|---|
| 163 | if ($code eq 'WARN') { | 
|---|
| 164 | $IPDB::errstr =~ s/\n\n/<br>\n/g; | 
|---|
| 165 | $IPDB::errstr =~ s/:\n/:<br>\n/g; | 
|---|
| 166 | $page->param(warn => $IPDB::errstr); | 
|---|
| 167 | } | 
|---|
| 168 | syslog "info", "$authuser added VRF $webvar{vrf}"; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | } # ACL check | 
|---|
| 172 |  | 
|---|
| 173 | } elsif ($webvar{action} eq 'delvrf') { | 
|---|
| 174 | if ($IPDBacl{$authuser} !~ /s/) { | 
|---|
| 175 | $aclerr = 'delvrf'; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | my $vrf = getVRF($ip_dbh, $webvar{vrf}); | 
|---|
| 179 |  | 
|---|
| 180 | $page->param(vrf => $webvar{vrf}); | 
|---|
| 181 | $page->param(vrfcomment => $vrf->{comment}); | 
|---|
| 182 |  | 
|---|
| 183 | } elsif ($webvar{action} eq 'finaldelvrf') { | 
|---|
| 184 | if ($IPDBacl{$authuser} !~ /s/) { | 
|---|
| 185 | $aclerr = 'finaldelvrf'; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | my $vrf = getVRF($ip_dbh, $webvar{vrf}); | 
|---|
| 189 | $page->param(vrf => $webvar{vrf}); | 
|---|
| 190 | $page->param(vrfcomment => $vrf->{comment}); | 
|---|
| 191 |  | 
|---|
| 192 | my ($code,$msg) = deleteVRF($ip_dbh, $webvar{vrf}, $authuser); | 
|---|
| 193 |  | 
|---|
| 194 | if ($code eq 'FAIL') { | 
|---|
| 195 | $page->param(failmsg => $msg); | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | } elsif ($webvar{action} eq 'addmaster') { | 
|---|
| 199 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| 200 | $aclerr = 'addmaster'; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | my $vrf = getVRF($ip_dbh, $webvar{vrf}); | 
|---|
| 204 |  | 
|---|
| 205 | # Retrieve the list of DNS locations if we've got a place to grab them from | 
|---|
| 206 | if ($IPDB::rpc_url) { | 
|---|
| 207 | my %rpcargs = ( | 
|---|
| 208 | rpcuser => $authuser, | 
|---|
| 209 | group => 1,     # bleh | 
|---|
| 210 | defloc => $vrf->{location}, | 
|---|
| 211 | ); | 
|---|
| 212 | my $result = IPDB::_rpc('getLocDropdown', %rpcargs); | 
|---|
| 213 | $page->param(loclist => $result); | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | # we don't have a netblock;  pass 0 for the block ID | 
|---|
| 217 | # Tree navigation | 
|---|
| 218 | my $crumbs = getBreadCrumbs($ip_dbh, 0, $webvar{vrf}); | 
|---|
| 219 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 220 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 221 |  | 
|---|
| 222 | $page->param(vrf => $webvar{vrf}); | 
|---|
| 223 |  | 
|---|
| 224 | } elsif ($webvar{action} eq 'newmaster') { | 
|---|
| 225 |  | 
|---|
| 226 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| 227 | $aclerr = 'addmaster'; | 
|---|
| 228 | } else { | 
|---|
| 229 | my $cidr = new NetAddr::IP $webvar{cidr}; | 
|---|
| 230 | $page->param(cidr => "$cidr"); | 
|---|
| 231 |  | 
|---|
| 232 | my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr}, (vrf => $webvar{vrf}, rdns => $webvar{rdns}, | 
|---|
| 233 | rwhois => $webvar{rwhois}, defloc => $webvar{loc}, user => $authuser) ); | 
|---|
| 234 |  | 
|---|
| 235 | if ($code eq 'FAIL') { | 
|---|
| 236 | syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'"; | 
|---|
| 237 | $page->param(err => $msg); | 
|---|
| 238 | } else { | 
|---|
| 239 | $page->param(parent => $msg); | 
|---|
| 240 | if ($code eq 'WARN') { | 
|---|
| 241 | $IPDB::errstr =~ s/\n\n/<br>\n/g; | 
|---|
| 242 | $IPDB::errstr =~ s/:\n/:<br>\n/g; | 
|---|
| 243 | $page->param(warn => $IPDB::errstr); | 
|---|
| 244 | } | 
|---|
| 245 | syslog "info", "$authuser added master block $webvar{cidr}"; | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | # we don't have a netblock;  pass 0 for the block ID | 
|---|
| 249 | # Tree navigation | 
|---|
| 250 | my $crumbs = getBreadCrumbs($ip_dbh, 0, $webvar{vrf}); | 
|---|
| 251 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 252 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 253 |  | 
|---|
| 254 | } # ACL check | 
|---|
| 255 |  | 
|---|
| 256 | } # end add new master | 
|---|
| 257 |  | 
|---|
| 258 | elsif ($webvar{action} eq 'showsubs') { | 
|---|
| 259 | showSubs(); | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | elsif($webvar{action} eq 'listpool') { | 
|---|
| 263 | showPool(); | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | # Not modified or added;  just shuffled | 
|---|
| 267 | elsif($webvar{action} eq 'assign') { | 
|---|
| 268 | assignBlock(); | 
|---|
| 269 | } | 
|---|
| 270 | elsif($webvar{action} eq 'confirm') { | 
|---|
| 271 | confirmAssign(); | 
|---|
| 272 | } | 
|---|
| 273 | elsif($webvar{action} eq 'insert') { | 
|---|
| 274 | insertAssign(); | 
|---|
| 275 | } | 
|---|
| 276 | elsif($webvar{action} eq 'edit') { | 
|---|
| 277 | edit(); | 
|---|
| 278 | } | 
|---|
| 279 | elsif($webvar{action} eq 'update') { | 
|---|
| 280 | update(); | 
|---|
| 281 | } | 
|---|
| 282 | elsif($webvar{action} eq 'split') { | 
|---|
| 283 | prepSplit(); | 
|---|
| 284 | } | 
|---|
| 285 | elsif($webvar{action} eq 'dosplit') { | 
|---|
| 286 | doSplit(); | 
|---|
| 287 | } | 
|---|
| 288 | elsif($webvar{action} eq 'merge') { | 
|---|
| 289 | prepMerge(); | 
|---|
| 290 | } | 
|---|
| 291 | elsif($webvar{action} eq 'confmerge') { | 
|---|
| 292 | confMerge(); | 
|---|
| 293 | } | 
|---|
| 294 | elsif($webvar{action} eq 'domerge') { | 
|---|
| 295 | doMerge(); | 
|---|
| 296 | } | 
|---|
| 297 | elsif($webvar{action} eq 'delete') { | 
|---|
| 298 | remove(); | 
|---|
| 299 | } | 
|---|
| 300 | elsif($webvar{action} eq 'finaldelete') { | 
|---|
| 301 | finalDelete(); | 
|---|
| 302 | } | 
|---|
| 303 | elsif ($webvar{action} eq 'nodesearch') { | 
|---|
| 304 | my $nodelist = getNodeList($ip_dbh); | 
|---|
| 305 | $page->param(nodelist => $nodelist); | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 | # DB failure.  Can't do much here, really. | 
|---|
| 309 | elsif ($webvar{action} eq 'dberr') { | 
|---|
| 310 | $page->param(errmsg => $errstr); | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | # Default is an error.  It shouldn't be possible to get here unless you're | 
|---|
| 314 | # randomly feeding in values for webvar{action}. | 
|---|
| 315 | else { | 
|---|
| 316 | my $rnd = rand 500; | 
|---|
| 317 | my $boing = sprintf("%.2f", rand 500); | 
|---|
| 318 | my @excuses = ( | 
|---|
| 319 | "Aether cloudy.  Ask again later about $webvar{action}.", | 
|---|
| 320 | "The gods are unhappy with your sacrificial $webvar{action}.", | 
|---|
| 321 | "Because one of $webvar{action}'s legs are both the same", | 
|---|
| 322 | "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!", | 
|---|
| 323 | "Hey, man, you've had your free $webvar{action}.  Next one's gonna...  <i>cost</i>....", | 
|---|
| 324 | "I ain't done $webvar{action}", | 
|---|
| 325 | "Oooo, look!  A flying $webvar{action}!", | 
|---|
| 326 | "$webvar{action} too evil, avoiding.", | 
|---|
| 327 | "Rocks fall, $webvar{action} dies.", | 
|---|
| 328 | "Bit bucket must be emptied before I can $webvar{action}..." | 
|---|
| 329 | ); | 
|---|
| 330 | $page->param(dunno => $excuses[$rnd/50.0]); | 
|---|
| 331 | } | 
|---|
| 332 | ## Finally! Done with that NASTY "case" emulation! | 
|---|
| 333 |  | 
|---|
| 334 |  | 
|---|
| 335 | # Switch to a different template if we've tripped on an ACL error. | 
|---|
| 336 | # Note that this should only be exercised in development, when | 
|---|
| 337 | # deeplinked, or when being attacked;  normal ACL handling should | 
|---|
| 338 | # remove the links a user is not allowed to click on. | 
|---|
| 339 | if ($aclerr) { | 
|---|
| 340 | $page = HTML::Template->new(filename => "aclerror.tmpl", path => @templatepath); | 
|---|
| 341 | $page->param(ipdbfunc => $aclmsg{$aclerr}); | 
|---|
| 342 | } | 
|---|
| 343 |  | 
|---|
| 344 | # Clean up IPDB globals, DB handle, etc. | 
|---|
| 345 | finish($ip_dbh); | 
|---|
| 346 |  | 
|---|
| 347 | ## Do all our printing here so we can generate errors and stick them into the slots in the templates. | 
|---|
| 348 |  | 
|---|
| 349 | # can't do this yet, too many blowups | 
|---|
| 350 | #print "Content-type: text/html\n\n", $header->output; | 
|---|
| 351 | $page->param(webpath => $IPDB::webpath); | 
|---|
| 352 | print $utilbar->output; | 
|---|
| 353 | print $page->output; | 
|---|
| 354 |  | 
|---|
| 355 | # include the admin tools link in the output? | 
|---|
| 356 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/)); | 
|---|
| 357 | $footer->param(webpath => $IPDB::webpath); | 
|---|
| 358 | print $footer->output; | 
|---|
| 359 |  | 
|---|
| 360 | # Just in case something waaaayyy down isn't in place | 
|---|
| 361 | # properly... we exit explicitly. | 
|---|
| 362 | exit 0; | 
|---|
| 363 |  | 
|---|
| 364 |  | 
|---|
| 365 | # Initial display:  Show list of VRFs | 
|---|
| 366 | sub showSummary { | 
|---|
| 367 | my $vrflist = listVRF($ip_dbh); | 
|---|
| 368 |  | 
|---|
| 369 | if ($IPDB::masterswithvrfs == 2) { | 
|---|
| 370 | $page = HTML::Template->new(filename => "index2.tmpl", loop_context_vars => 1, global_vars => 1, | 
|---|
| 371 | path => @templatepath); | 
|---|
| 372 | # alternate layout;  put master blocks on the front summary page instead of "out"/"down" a | 
|---|
| 373 | # layer in the browse tree.  Leaving the manual include of showvrf.tmpl for reference;  not | 
|---|
| 374 | # sure why setting webpath here didn't seem to make it to the output. | 
|---|
| 375 | #    my $vrfinfo = HTML::Template->new(filename => "showvrf.tmpl", path => @templatepath); | 
|---|
| 376 | foreach my $vrf (@$vrflist) { | 
|---|
| 377 | my $masterlist = listSummary($ip_dbh, $vrf->{vrf}); | 
|---|
| 378 | $vrf->{masterlist} = $masterlist; | 
|---|
| 379 | $vrf->{addmaster} = ($IPDBacl{$authuser} =~ /s/); | 
|---|
| 380 | $vrf->{maydel} = ($IPDBacl{$authuser} =~ /s/); | 
|---|
| 381 | $vrf->{sub} = 1; | 
|---|
| 382 | #      $vrfinfo->param(vrf => $vrf->{vrf}); | 
|---|
| 383 | #      $vrfinfo->param(masterlist => $masterlist); | 
|---|
| 384 | #      $vrfinfo->param(addmaster => ($IPDBacl{$authuser} =~ /s/) ); | 
|---|
| 385 | #      $vrfinfo->param(maydel => ($IPDBacl{$authuser} =~ /s/) ); | 
|---|
| 386 | #      $vrfinfo->param(sub => 1); | 
|---|
| 387 | #      $vrfinfo->param(webpath => $IPDB::webpath); | 
|---|
| 388 | #      $vrf->{vrfinfo} = $vrfinfo->output; | 
|---|
| 389 | } | 
|---|
| 390 | } | 
|---|
| 391 |  | 
|---|
| 392 | $page->param(vrflist => $vrflist); | 
|---|
| 393 |  | 
|---|
| 394 | # Only systems/network should be allowed to add VRFs - or maybe higher? | 
|---|
| 395 | $page->param(addvrf => ($IPDBacl{$authuser} =~ /s/) ); | 
|---|
| 396 |  | 
|---|
| 397 | } # showSummary | 
|---|
| 398 |  | 
|---|
| 399 |  | 
|---|
| 400 | # Show IP blocks in a VRF | 
|---|
| 401 | sub showVRF { | 
|---|
| 402 | my $masterlist = listSummary($ip_dbh, $webvar{vrf}); | 
|---|
| 403 | $page->param(vrf => $webvar{vrf}); | 
|---|
| 404 | $page->param(masterlist => $masterlist); | 
|---|
| 405 |  | 
|---|
| 406 | # we don't have a netblock;  pass 0 for the block ID | 
|---|
| 407 | # Tree navigation | 
|---|
| 408 | my $crumbs = getBreadCrumbs($ip_dbh, 0, $webvar{vrf}); | 
|---|
| 409 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 410 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 411 |  | 
|---|
| 412 | $page->param(maydel => ($IPDBacl{$authuser} =~ /s/) ); | 
|---|
| 413 | $page->param(addmaster => ($IPDBacl{$authuser} =~ /s/) ); | 
|---|
| 414 | } # showVRF | 
|---|
| 415 |  | 
|---|
| 416 |  | 
|---|
| 417 | # Display blocks immediately within a given parent | 
|---|
| 418 | sub showSubs { | 
|---|
| 419 | # Which layout? | 
|---|
| 420 | if ($IPDB::sublistlayout == 2) { | 
|---|
| 421 |  | 
|---|
| 422 | # 2-part layout;  mixed containers and end-use allocations and free blocks. | 
|---|
| 423 | # Containers have a second line for the subblock metadata. | 
|---|
| 424 | # We need to load an alternate template for this case. | 
|---|
| 425 | $page = HTML::Template->new(filename => "showsubs2.tmpl", loop_context_vars => 1, global_vars => 1, | 
|---|
| 426 | path => @templatepath); | 
|---|
| 427 |  | 
|---|
| 428 | $page->param(maydel => ($IPDBacl{$authuser} =~ /d/)); | 
|---|
| 429 |  | 
|---|
| 430 | my $sublist = listSubs($ip_dbh, parent => $webvar{parent}); | 
|---|
| 431 | $page->param(sublist => $sublist); | 
|---|
| 432 |  | 
|---|
| 433 | } else { | 
|---|
| 434 |  | 
|---|
| 435 | # 3-part layout;  containers, end-use allocations, and free blocks | 
|---|
| 436 |  | 
|---|
| 437 | my $contlist = listContainers($ip_dbh, parent => $webvar{parent}); | 
|---|
| 438 | $page->param(contlist => $contlist); | 
|---|
| 439 |  | 
|---|
| 440 | my $alloclist = listAllocations($ip_dbh, parent => $webvar{parent}); | 
|---|
| 441 | $page->param(alloclist => $alloclist); | 
|---|
| 442 |  | 
|---|
| 443 | # only show "delete" button if we have no container or usage allocations | 
|---|
| 444 | $page->param(maydel => ($IPDBacl{$authuser} =~ /d/) && !(@$contlist || @$alloclist)); | 
|---|
| 445 |  | 
|---|
| 446 | } | 
|---|
| 447 |  | 
|---|
| 448 | # Common elements | 
|---|
| 449 | my $pinfo = getBlockData($ip_dbh, $webvar{parent}); | 
|---|
| 450 | $page->param(blockpctfree => sprintf("%0.1f", $pinfo->{nfree}/2**(32-$pinfo->{masklen}) * 100 ) ); | 
|---|
| 451 |  | 
|---|
| 452 | # retrieve any notices | 
|---|
| 453 | blocknotices($webvar{parent}); | 
|---|
| 454 |  | 
|---|
| 455 | ##fixme:  do we add a wrapper to not show the edit link for master blocks? | 
|---|
| 456 | #$page->param(editme => 1) unless $pinfo->{type} ne 'mm'; | 
|---|
| 457 |  | 
|---|
| 458 | my $crumbs = getBreadCrumbs($ip_dbh, $pinfo->{parent_id}, $pinfo->{vrf}); | 
|---|
| 459 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 460 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 461 |  | 
|---|
| 462 | $page->param(self_id => $webvar{parent}); | 
|---|
| 463 | $page->param(block => $pinfo->{block}); | 
|---|
| 464 | $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/)); | 
|---|
| 465 |  | 
|---|
| 466 | my $flist = listFree($ip_dbh, parent => $webvar{parent}); | 
|---|
| 467 | $page->param(freelist => $flist); | 
|---|
| 468 | } # showSubs | 
|---|
| 469 |  | 
|---|
| 470 |  | 
|---|
| 471 | # List the IPs used in a pool | 
|---|
| 472 | sub showPool { | 
|---|
| 473 |  | 
|---|
| 474 | my $poolinfo = getBlockData($ip_dbh, $webvar{pool}); | 
|---|
| 475 | my $cidr = new NetAddr::IP $poolinfo->{block}; | 
|---|
| 476 | $page->param(vlan => $poolinfo->{vlan}); | 
|---|
| 477 | $page->param(poolpctfree => sprintf("%0.1f", $poolinfo->{nfree}/2**(32-$poolinfo->{masklen}) * 100 ) ); | 
|---|
| 478 | $page->param(csvlink => $IPDB::enablecsv); | 
|---|
| 479 |  | 
|---|
| 480 | # Tree navigation | 
|---|
| 481 | my $crumbs = getBreadCrumbs($ip_dbh, $poolinfo->{parent_id}); | 
|---|
| 482 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 483 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 484 |  | 
|---|
| 485 | $page->param(block => $cidr); | 
|---|
| 486 | $page->param(netip => $cidr->addr); | 
|---|
| 487 | $cidr++; | 
|---|
| 488 | $page->param(gate => $cidr->addr); | 
|---|
| 489 | $cidr--;  $cidr--; | 
|---|
| 490 | $page->param(bcast => $cidr->addr); | 
|---|
| 491 | $page->param(mask => $cidr->mask); | 
|---|
| 492 |  | 
|---|
| 493 | $page->param(disptype => $disp_alloctypes{$poolinfo->{type}}); | 
|---|
| 494 | $page->param(city => $poolinfo->{city}); | 
|---|
| 495 |  | 
|---|
| 496 | # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy | 
|---|
| 497 | $page->param(realblock => $poolinfo->{type} =~ /^.d$/); | 
|---|
| 498 |  | 
|---|
| 499 | # probably have to add an "edit IP allocation" link here somewhere. | 
|---|
| 500 |  | 
|---|
| 501 | # this will cascade into the IP list below | 
|---|
| 502 | $page->param(maydel => $IPDBacl{$authuser} =~ /d/); | 
|---|
| 503 |  | 
|---|
| 504 | my $plist = listPool($ip_dbh, $webvar{pool}, 1); | 
|---|
| 505 | $page->param(poolips => $plist); | 
|---|
| 506 | # CSV voodoo | 
|---|
| 507 | $page->param(poolid => $webvar{pool}); | 
|---|
| 508 |  | 
|---|
| 509 | # retrieve any notices | 
|---|
| 510 | blocknotices($webvar{pool}); | 
|---|
| 511 | } # end showPool | 
|---|
| 512 |  | 
|---|
| 513 |  | 
|---|
| 514 | # Show "Add new allocation" page.  Note that the actual page may | 
|---|
| 515 | # be one of two templates, and the lists come from the database. | 
|---|
| 516 | sub assignBlock { | 
|---|
| 517 |  | 
|---|
| 518 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| 519 | $aclerr = 'addblock'; | 
|---|
| 520 | return; | 
|---|
| 521 | } | 
|---|
| 522 |  | 
|---|
| 523 | # hack pthbttt eww | 
|---|
| 524 | $webvar{parent} = 0 if !$webvar{parent}; | 
|---|
| 525 | $webvar{block} = '' if !$webvar{block}; | 
|---|
| 526 |  | 
|---|
| 527 | $page->param(allocfrom => $webvar{block});    # fb-assign flag, if block is set, we're in fb-assign | 
|---|
| 528 |  | 
|---|
| 529 | if ($webvar{fbid} || $webvar{fbtype}) { | 
|---|
| 530 |  | 
|---|
| 531 | # Common case, according to reported usage.  Block to assign is specified. | 
|---|
| 532 | my $block = new NetAddr::IP $webvar{block}; | 
|---|
| 533 |  | 
|---|
| 534 | my ($rdns,$cached) = getBlockRDNS($ip_dbh, id => $webvar{parent}, type => $webvar{fbtype}, user => $authuser); | 
|---|
| 535 | $page->param(rdns => $rdns) if $rdns; | 
|---|
| 536 | $page->param(parent => $webvar{parent}); | 
|---|
| 537 | $page->param(fbid => $webvar{fbid}); | 
|---|
| 538 | # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin | 
|---|
| 539 | $page->param(cached => $cached); | 
|---|
| 540 |  | 
|---|
| 541 | my $pinfo = getBlockData($ip_dbh, $webvar{parent}); | 
|---|
| 542 |  | 
|---|
| 543 | # retrieve any notices | 
|---|
| 544 | blocknotices($webvar{parent}); | 
|---|
| 545 |  | 
|---|
| 546 | my @cities; | 
|---|
| 547 | foreach my $city (@citylist) { | 
|---|
| 548 | my %row = (city => $city, sel => ($pinfo->{city} eq $city) ); | 
|---|
| 549 | push (@cities, \%row); | 
|---|
| 550 | } | 
|---|
| 551 | $page->param(preselected => 1); | 
|---|
| 552 | $page->param(citylist => \@cities); | 
|---|
| 553 |  | 
|---|
| 554 | # Tree navigation | 
|---|
| 555 | my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent}); | 
|---|
| 556 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 557 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 558 |  | 
|---|
| 559 | $webvar{fbtype} = '' if !$webvar{fbtype}; | 
|---|
| 560 | if ($webvar{fbtype} eq 'i') { | 
|---|
| 561 | my $ipinfo = getBlockData($ip_dbh, $webvar{block}, 'i'); | 
|---|
| 562 | $page->param( | 
|---|
| 563 | fbip => 1, | 
|---|
| 564 | block => $ipinfo->{block}, | 
|---|
| 565 | fbdisptype => $list_alloctypes{$ipinfo->{type}}, | 
|---|
| 566 | type => $ipinfo->{type}, | 
|---|
| 567 | allocfrom => $pinfo->{block}, | 
|---|
| 568 | ); | 
|---|
| 569 | } else { | 
|---|
| 570 | # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch | 
|---|
| 571 | my $tlist = getTypeList($ip_dbh, 'n'); | 
|---|
| 572 | $tlist->[0]->{sel} = 1; | 
|---|
| 573 | $page->param(typelist => $tlist, block => $block); | 
|---|
| 574 | } | 
|---|
| 575 |  | 
|---|
| 576 | } else { | 
|---|
| 577 |  | 
|---|
| 578 | # Uncommon case, according to reported usage.  Block to assign needs to be found based on criteria. | 
|---|
| 579 | my $mlist = getMasterList($ip_dbh, 'c'); | 
|---|
| 580 | $page->param(masterlist => $mlist); | 
|---|
| 581 |  | 
|---|
| 582 | my @pops; | 
|---|
| 583 | foreach my $pop (@citylist) { | 
|---|
| 584 | my %row = (pop => $pop); | 
|---|
| 585 | push (@pops, \%row); | 
|---|
| 586 | } | 
|---|
| 587 | $page->param(pops => \@pops); | 
|---|
| 588 |  | 
|---|
| 589 | my @cities; | 
|---|
| 590 | foreach my $city (@citylist) { | 
|---|
| 591 | my %row = (city => $city); | 
|---|
| 592 | push (@cities, \%row); | 
|---|
| 593 | } | 
|---|
| 594 | $page->param(citylist => \@cities); | 
|---|
| 595 |  | 
|---|
| 596 | # get all standard alloctypes | 
|---|
| 597 | my $tlist = getTypeList($ip_dbh, 'a'); | 
|---|
| 598 | $tlist->[0]->{sel} = 1; | 
|---|
| 599 | $page->param(typelist => $tlist); | 
|---|
| 600 | } | 
|---|
| 601 |  | 
|---|
| 602 | ## node hack | 
|---|
| 603 | my $nlist = getNodeList($ip_dbh); | 
|---|
| 604 | $page->param(nodelist => $nlist); | 
|---|
| 605 | ## end node hack | 
|---|
| 606 |  | 
|---|
| 607 | $page->param(nocling => $IPDBacl{$authuser} =~ /s/); | 
|---|
| 608 |  | 
|---|
| 609 | } # assignBlock | 
|---|
| 610 |  | 
|---|
| 611 |  | 
|---|
| 612 | # Take info on requested IP assignment and see what we can provide. | 
|---|
| 613 | sub confirmAssign { | 
|---|
| 614 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| 615 | $aclerr = 'addblock'; | 
|---|
| 616 | return; | 
|---|
| 617 | } | 
|---|
| 618 |  | 
|---|
| 619 | my $cidr; | 
|---|
| 620 | my $resv;   # Reserved for expansion. | 
|---|
| 621 | my $alloc_from; | 
|---|
| 622 | my $fbid = $webvar{fbid}; | 
|---|
| 623 | my $p_id = $webvar{parent}; | 
|---|
| 624 |  | 
|---|
| 625 | # Going to manually validate some items. | 
|---|
| 626 | # custid and city are automagic. | 
|---|
| 627 | return if !validateInput(); | 
|---|
| 628 |  | 
|---|
| 629 | # make sure this is defined | 
|---|
| 630 | $webvar{fbassign} = 'n' if !$webvar{fbassign}; | 
|---|
| 631 |  | 
|---|
| 632 | # Several different cases here. | 
|---|
| 633 | # Static IP vs netblock | 
|---|
| 634 | #  + Different flavours of static IP | 
|---|
| 635 | #  + Different flavours of netblock | 
|---|
| 636 |  | 
|---|
| 637 | if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') { | 
|---|
| 638 | if (!$webvar{pop}) { | 
|---|
| 639 | $page->param(err => "Please select a location/POP site to allocate from."); | 
|---|
| 640 | return; | 
|---|
| 641 | } | 
|---|
| 642 | my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop}); | 
|---|
| 643 | $page->param(staticip => 1); | 
|---|
| 644 | $page->param(poollist => $plist) if $plist; | 
|---|
| 645 | $cidr = "Single static IP"; | 
|---|
| 646 | ##fixme:  need to handle "no available pools" | 
|---|
| 647 |  | 
|---|
| 648 | } else { # end show pool options | 
|---|
| 649 |  | 
|---|
| 650 | if ($webvar{fbassign} && $webvar{fbassign} eq 'y') { | 
|---|
| 651 |  | 
|---|
| 652 | # Tree navigation | 
|---|
| 653 | my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent}); | 
|---|
| 654 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 655 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 656 |  | 
|---|
| 657 | $cidr = new NetAddr::IP $webvar{block}; | 
|---|
| 658 | $alloc_from = new NetAddr::IP $webvar{allocfrom}; | 
|---|
| 659 | $webvar{maskbits} = $cidr->masklen; | 
|---|
| 660 | # Some additional checks are needed for reserving free space | 
|---|
| 661 | if ($webvar{reserve}) { | 
|---|
| 662 | if ($cidr == $alloc_from) { | 
|---|
| 663 | # We could still squirm and fiddle to try to find a way to reserve space, but the storage model for | 
|---|
| 664 | # IPDB means that all continguous free space is kept in the smallest number of strict CIDR netblocks | 
|---|
| 665 | # possible.  (In theory.)  If the request and the freeblock are the same, it is theoretically impossible | 
|---|
| 666 | # to reserve an equivalent-sized block either ahead or behind the requested one, because the pair | 
|---|
| 667 | # together would never be a strict CIDR block. | 
|---|
| 668 | $page->param(warning => "Can't reserve space for expansion;  free block and requested allocation are the same."); | 
|---|
| 669 | delete $webvar{reserve}; | 
|---|
| 670 | } else { | 
|---|
| 671 | # Find which new free block will match the reqested block. | 
|---|
| 672 | # Take the requested mask, shift by one | 
|---|
| 673 | my $tmpmask = $webvar{maskbits}; | 
|---|
| 674 | $tmpmask--; | 
|---|
| 675 | # find the subnets with that mask in the selected free block | 
|---|
| 676 | my @pieces = $alloc_from->split($tmpmask); | 
|---|
| 677 | foreach my $slice (@pieces) { | 
|---|
| 678 | if ($slice->contains($cidr)) { | 
|---|
| 679 | # For the subnet that contains the requested block, split that in two, | 
|---|
| 680 | # and flag/cache the one that's not the requested block. | 
|---|
| 681 | my @bits = $slice->split($webvar{maskbits}); | 
|---|
| 682 | if ($bits[0] == $cidr) { | 
|---|
| 683 | $resv = $bits[1]; | 
|---|
| 684 | } else { | 
|---|
| 685 | $resv = $bits[0]; | 
|---|
| 686 | } | 
|---|
| 687 | } | 
|---|
| 688 | } | 
|---|
| 689 | } | 
|---|
| 690 | } # reserve block check | 
|---|
| 691 |  | 
|---|
| 692 | } else { # done with direct freeblocks assignment | 
|---|
| 693 |  | 
|---|
| 694 | if (!$webvar{maskbits}) { | 
|---|
| 695 | $page->param(err => "Please specify a CIDR mask length."); | 
|---|
| 696 | return; | 
|---|
| 697 | } | 
|---|
| 698 |  | 
|---|
| 699 | ##fixme ick, ew, bleh.  gotta handle the failure message generation better.  push it into findAllocateFrom()? | 
|---|
| 700 | my $failmsg = "No suitable free block found.<br>\n"; | 
|---|
| 701 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| 702 | $failmsg .= "We do not have a free routeable block of that size.<br>\n". | 
|---|
| 703 | "You will have to either route a set of smaller netblocks or a single smaller netblock."; | 
|---|
| 704 | } else { | 
|---|
| 705 | if ($webvar{alloctype} =~ /^.[pc]$/) { | 
|---|
| 706 | $failmsg .= "You will have to route another superblock from one of the<br>\n". | 
|---|
| 707 | "master blocks or chose a smaller block size for the pool."; | 
|---|
| 708 | } else { | 
|---|
| 709 | if (!$webvar{pop}) { | 
|---|
| 710 | $page->param(err => 'Please select a POP to route the block from/through.'); | 
|---|
| 711 | return; | 
|---|
| 712 | } | 
|---|
| 713 | $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n". | 
|---|
| 714 | "from one of the master blocks"; | 
|---|
| 715 | if ($webvar{reserve}) { | 
|---|
| 716 | $failmsg .= ', choose a smaller blocksize, or uncheck "Reserve space for expansion".'; | 
|---|
| 717 | } else { | 
|---|
| 718 | $failmsg .= " or chose a smaller blocksize."; | 
|---|
| 719 | } | 
|---|
| 720 | } | 
|---|
| 721 | } | 
|---|
| 722 |  | 
|---|
| 723 | # if requesting extra space "reserved for expansion", we need to find a free | 
|---|
| 724 | # block at least double the size of the request. | 
|---|
| 725 | if ($webvar{reserve}) { | 
|---|
| 726 | $webvar{maskbits}--; | 
|---|
| 727 | } | 
|---|
| 728 |  | 
|---|
| 729 | ($fbid,$cidr,$p_id) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype}, | 
|---|
| 730 | $webvar{city}, $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) ); | 
|---|
| 731 | if (!$cidr) { | 
|---|
| 732 | $page->param(err => $failmsg); | 
|---|
| 733 | return; | 
|---|
| 734 | } | 
|---|
| 735 | $cidr = new NetAddr::IP $cidr; | 
|---|
| 736 |  | 
|---|
| 737 | $alloc_from = "$cidr"; | 
|---|
| 738 |  | 
|---|
| 739 | # when autofinding a block to allocate from, use the first piece of the found | 
|---|
| 740 | # block for the allocation, and the next piece for the "reserved for expansion". | 
|---|
| 741 | if ($webvar{reserve}) { | 
|---|
| 742 | # reset the mask to the real requested one, now that we've got a | 
|---|
| 743 | # block large enough for the request plus reserve | 
|---|
| 744 | $webvar{maskbits}++; | 
|---|
| 745 | ($cidr,$resv) = $cidr->split($webvar{maskbits}); | 
|---|
| 746 | } | 
|---|
| 747 |  | 
|---|
| 748 | # If the block to be allocated is smaller than the one we found, | 
|---|
| 749 | # figure out the "real" block to be allocated. | 
|---|
| 750 | if ($cidr->masklen() ne $webvar{maskbits}) { | 
|---|
| 751 | my $maskbits = $cidr->masklen(); | 
|---|
| 752 | # we reset $cidr each time around the loop to better allow for the huge | 
|---|
| 753 | # address space of IPv6;  many allocation schemes in v6 will break NetAddr::IP's | 
|---|
| 754 | # hard limit of 65536 subnets from a split() | 
|---|
| 755 | while ($maskbits++ < $webvar{maskbits}) { | 
|---|
| 756 | $cidr = ($cidr->split($maskbits))[0]; | 
|---|
| 757 | } | 
|---|
| 758 | } | 
|---|
| 759 | } # check for freeblocks assignment or IPDB-controlled assignment | 
|---|
| 760 |  | 
|---|
| 761 | # Generate the IP list for the new allocation in case someone wants to set per-IP rDNS right away. | 
|---|
| 762 | # We don't do this on the previous page because we don't know how big a block or even what IP range | 
|---|
| 763 | # it's for (if following the "normal" allocation process) | 
|---|
| 764 | if ($IPDBacl{$authuser} =~ /c/ | 
|---|
| 765 | && $cidr->masklen != $cidr->bits | 
|---|
| 766 | && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist | 
|---|
| 767 | # config flag for "all block types" OR "not-a-pool-or-IP type" | 
|---|
| 768 | && ($IPDB::revlistalltypes || $webvar{alloctype} !~ /^.[dpi]/) | 
|---|
| 769 | # safety against trying to retrieve and display more than 1k (10 bits, /22 v4) worth of individual IPs | 
|---|
| 770 | # ever.  If you really need to manage a long list of IPs like that all in one place, you can use the DNS | 
|---|
| 771 | # management tool.  Even a /26 is a bit much, really. | 
|---|
| 772 | && ($cidr->bits - $cidr->masklen) <= 10 | 
|---|
| 773 | # do we want to allow v6 at all? | 
|---|
| 774 | #&& ! $cidr->{isv6} | 
|---|
| 775 | ) { | 
|---|
| 776 | my @list; | 
|---|
| 777 | foreach my $ip (@{$cidr->splitref()}) { | 
|---|
| 778 | my %row; | 
|---|
| 779 | $row{r_ip} = $ip->addr; | 
|---|
| 780 | $row{iphost} = ''; | 
|---|
| 781 | push @list, \%row; | 
|---|
| 782 | } | 
|---|
| 783 | $page->param(r_iplist => \@list); | 
|---|
| 784 | # We don't use this here, because these IPs should already be bare. | 
|---|
| 785 | # ... or should we be paranoid?  Make it a config option? | 
|---|
| 786 | #getRDNSbyIP($ip_dbh, type => $webvar{alloctype}, range => "$cidr", user => $authuser) ); | 
|---|
| 787 | } | 
|---|
| 788 | } # if ($webvar{alloctype} =~ /^.i$/) | 
|---|
| 789 |  | 
|---|
| 790 | ## node hack | 
|---|
| 791 | if ($webvar{node} && $webvar{node} ne '-') { | 
|---|
| 792 | my $nodename = getNodeName($ip_dbh, $webvar{node}); | 
|---|
| 793 | $page->param(nodename => $nodename); | 
|---|
| 794 | $page->param(nodeid => $webvar{node}); | 
|---|
| 795 | } | 
|---|
| 796 | ## end node hack | 
|---|
| 797 |  | 
|---|
| 798 | # flag DNS info if we can't publish the entry remotely | 
|---|
| 799 | my $pinfo = getBlockData($ip_dbh, $webvar{parent}); | 
|---|
| 800 | $page->param(dnslocal => 1) unless ($pinfo->{revpartial} || $pinfo->{revavail}); | 
|---|
| 801 |  | 
|---|
| 802 | # retrieve any notices | 
|---|
| 803 | blocknotices($webvar{parent}); | 
|---|
| 804 |  | 
|---|
| 805 | # reserve for expansion | 
|---|
| 806 | $page->param(reserve => $webvar{reserve}); | 
|---|
| 807 | # probably just preventing a little log noise doing this;  could just set the param | 
|---|
| 808 | # all the time since it won't be shown if the reserve param above isn't set. | 
|---|
| 809 | #  if ($webvar{reserve}) { | 
|---|
| 810 | $page->param(resvblock => $resv); | 
|---|
| 811 | #  } | 
|---|
| 812 |  | 
|---|
| 813 | # Stick in the allocation data | 
|---|
| 814 | $page->param(alloc_type => $webvar{alloctype}); | 
|---|
| 815 | $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}})); | 
|---|
| 816 | $page->param(alloc_from => $alloc_from); | 
|---|
| 817 | $page->param(parent => $p_id); | 
|---|
| 818 | $page->param(fbid => $fbid); | 
|---|
| 819 | $page->param(cidr => $cidr); | 
|---|
| 820 | $page->param(rdns => $webvar{rdns}); | 
|---|
| 821 | $page->param(vrf => $webvar{vrf}); | 
|---|
| 822 | $page->param(vlan => $webvar{vlan}); | 
|---|
| 823 | $page->param(city => $q->escapeHTML($webvar{city})); | 
|---|
| 824 | $page->param(custid => $webvar{custid}); | 
|---|
| 825 | $page->param(circid => $q->escapeHTML($webvar{circid})); | 
|---|
| 826 | $page->param(desc => $q->escapeHTML($webvar{desc})); | 
|---|
| 827 |  | 
|---|
| 828 | ##fixme: find a way to have the displayed copy have <br> substitutions | 
|---|
| 829 | # for newlines, and the <input> value have either encoded or bare newlines. | 
|---|
| 830 | # Also applies to privdata. | 
|---|
| 831 | $page->param(notes => $q->escapeHTML($webvar{notes},'y')); | 
|---|
| 832 |  | 
|---|
| 833 | # Check to see if user is allowed to do anything with sensitive data | 
|---|
| 834 | if ($IPDBacl{$authuser} =~ /s/) { | 
|---|
| 835 | $page->param(nocling => 1); | 
|---|
| 836 | $page->param(privdata => $q->escapeHTML($webvar{privdata},'y')); | 
|---|
| 837 |  | 
|---|
| 838 | $page->param(backupfields => $webvar{backupfields}); | 
|---|
| 839 | $page->param(bkbrand => $webvar{bkbrand}); | 
|---|
| 840 | $page->param(bkmodel => $webvar{bkmodel}); | 
|---|
| 841 | $page->param(bktype  => $webvar{bktype}); | 
|---|
| 842 | $page->param(bksrc   => $webvar{bksrc}); | 
|---|
| 843 | $page->param(bkuser  => $webvar{bkuser}); | 
|---|
| 844 | # these two could use virtually any character | 
|---|
| 845 | $page->param(bkvpass => $q->escapeHTML($webvar{bkvpass})); | 
|---|
| 846 | $page->param(bkepass => $q->escapeHTML($webvar{bkepass})); | 
|---|
| 847 | $page->param(bkport  => $webvar{bkport}); | 
|---|
| 848 | $page->param(bkip    => $webvar{bkip}); | 
|---|
| 849 | } | 
|---|
| 850 |  | 
|---|
| 851 | # Yay!  This now has it's very own little home. | 
|---|
| 852 | $page->param(billinguser => $webvar{userid}) | 
|---|
| 853 | if $webvar{userid}; | 
|---|
| 854 | ##fixme:  figure out some kind of "specials" metatemplate thingy to allow passing arbitrary data along a form chain? | 
|---|
| 855 |  | 
|---|
| 856 | syslog "debug", "billinguser used ($authuser): alloc_from $alloc_from, type $webvar{alloctype}" if $webvar{userid}; | 
|---|
| 857 |  | 
|---|
| 858 | ##fixme:  this is only needed iff confirm.tmpl and | 
|---|
| 859 | # confirmRemove.tmpl are merged (quite possible, just | 
|---|
| 860 | # a little tedious) | 
|---|
| 861 | $page->param(action => "insert"); | 
|---|
| 862 |  | 
|---|
| 863 | } # end confirmAssign | 
|---|
| 864 |  | 
|---|
| 865 |  | 
|---|
| 866 | # Do the work of actually inserting a block in the database. | 
|---|
| 867 | sub insertAssign { | 
|---|
| 868 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| 869 | $aclerr = 'addblock'; | 
|---|
| 870 | return; | 
|---|
| 871 | } | 
|---|
| 872 | # Some things are done more than once. | 
|---|
| 873 | return if !validateInput(); | 
|---|
| 874 |  | 
|---|
| 875 | ##fixme: permission check | 
|---|
| 876 | if (!defined($webvar{privdata})) { | 
|---|
| 877 | $webvar{privdata} = ''; | 
|---|
| 878 | } | 
|---|
| 879 |  | 
|---|
| 880 | # $code is "success" vs "failure", $msg contains OK for a | 
|---|
| 881 | # successful netblock allocation, the IP allocated for static | 
|---|
| 882 | # IP, or the error message if an error occurred. | 
|---|
| 883 |  | 
|---|
| 884 | ##fixme:  consider just passing \%webvar to allocateBlock()? | 
|---|
| 885 | # collect per-IP rDNS fields.  only copy over the ones that actually have something in them. | 
|---|
| 886 | my %iprev; | 
|---|
| 887 | foreach (sort keys %webvar) { | 
|---|
| 888 | # skip all but the host_* fields | 
|---|
| 889 | next unless /^host_[\d.a-fA-F:]+/; | 
|---|
| 890 | # propagate the per-IP rDNS if present | 
|---|
| 891 | $iprev{$_} = $webvar{$_} if $webvar{$_}; | 
|---|
| 892 | # set the rDNS to be cleared if there was a value before (was_host_* field), | 
|---|
| 893 | # but there's no value in the matching visible form field now (host_*) | 
|---|
| 894 | $iprev{$_} = '' if $webvar{"was_$_"} && !$webvar{$_}; | 
|---|
| 895 | } | 
|---|
| 896 |  | 
|---|
| 897 | # Easier to see and cosmetically fiddle the list like this | 
|---|
| 898 | my %insert_args = ( | 
|---|
| 899 | cidr            => $webvar{fullcidr}, | 
|---|
| 900 | fbid            => $webvar{fbid}, | 
|---|
| 901 | reserve         => $webvar{reserve}, | 
|---|
| 902 | parent          => $webvar{parent}, | 
|---|
| 903 | custid          => $webvar{custid}, | 
|---|
| 904 | type            => $webvar{alloctype}, | 
|---|
| 905 | city            => $webvar{city}, | 
|---|
| 906 | desc            => $webvar{desc}, | 
|---|
| 907 | notes           => $webvar{notes}, | 
|---|
| 908 | circid          => $webvar{circid}, | 
|---|
| 909 | privdata        => $webvar{privdata}, | 
|---|
| 910 | nodeid          => $webvar{node}, | 
|---|
| 911 | rdns            => $webvar{rdns}, | 
|---|
| 912 | vrf             => $webvar{vrf}, | 
|---|
| 913 | vlan            => $webvar{vlan}, | 
|---|
| 914 | user            => $authuser, | 
|---|
| 915 | billinguserid   => $webvar{billinguser}, | 
|---|
| 916 | ); | 
|---|
| 917 |  | 
|---|
| 918 | ##fixme: permission check | 
|---|
| 919 | # fill in backup data, if present/allowed | 
|---|
| 920 | if ($webvar{backupfields}) { | 
|---|
| 921 | $insert_args{backup} = 1; | 
|---|
| 922 | for my $bkfield (@IPDB::backupfields) { | 
|---|
| 923 | $insert_args{"bk$bkfield"} = ($webvar{"bk$bkfield"} ? $webvar{"bk$bkfield"} : ''); | 
|---|
| 924 | } | 
|---|
| 925 | } | 
|---|
| 926 |  | 
|---|
| 927 | my $pinfo = getBlockData($ip_dbh, $webvar{parent}); | 
|---|
| 928 |  | 
|---|
| 929 | # retrieve any notices | 
|---|
| 930 | blocknotices($webvar{parent}); | 
|---|
| 931 |  | 
|---|
| 932 | # clean up a minor mess with guided allocation of static IPs | 
|---|
| 933 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
| 934 | $insert_args{alloc_from} = $pinfo->{block}; | 
|---|
| 935 | } | 
|---|
| 936 |  | 
|---|
| 937 | my ($code,$msg) = allocateBlock($ip_dbh, %insert_args, iprev => \%iprev); | 
|---|
| 938 |  | 
|---|
| 939 | if ($code eq 'OK') { | 
|---|
| 940 | # breadcrumbs lite!  provide at least a link to the parent of the block we just allocated. | 
|---|
| 941 | $page->param(parentid => $webvar{parent}); | 
|---|
| 942 | $page->param(parentblock => $pinfo->{block}); | 
|---|
| 943 |  | 
|---|
| 944 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
| 945 | $msg =~ s|/32||; | 
|---|
| 946 | $page->param(staticip => $msg); | 
|---|
| 947 | $page->param(custid => $webvar{custid}); | 
|---|
| 948 | $page->param(billinguser => $webvar{billinguser}); | 
|---|
| 949 | $page->param(billinglink => $IPDB::billinglink); | 
|---|
| 950 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} $msg", | 
|---|
| 951 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n". | 
|---|
| 952 | "Description: $webvar{desc}\n". | 
|---|
| 953 | ($webvar{rdns} ? "DNS name: $webvar{rdns}\n" : ''). | 
|---|
| 954 | "\nAllocated by: $authuser\n"); | 
|---|
| 955 | } else { | 
|---|
| 956 | my $netblock = new NetAddr::IP $webvar{fullcidr}; | 
|---|
| 957 | $page->param(fullcidr => $webvar{fullcidr}); | 
|---|
| 958 | $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}}); | 
|---|
| 959 | $page->param(custid => $webvar{custid}); | 
|---|
| 960 |  | 
|---|
| 961 | # Full breadcrumbs | 
|---|
| 962 | my $crumbs = getBreadCrumbs($ip_dbh, $webvar{parent}); | 
|---|
| 963 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 964 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 965 |  | 
|---|
| 966 | if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) { | 
|---|
| 967 | $page->param(billinguser => $webvar{billinguser}); | 
|---|
| 968 | $page->param(billinglink => $IPDB::billinglink); | 
|---|
| 969 | $page->param(custid => $webvar{custid}); | 
|---|
| 970 | $page->param(netaddr => $netblock->addr); | 
|---|
| 971 | $page->param(masklen => $netblock->masklen); | 
|---|
| 972 | } | 
|---|
| 973 | syslog "debug", "billinguser used ($authuser): allocated $netblock, type $webvar{alloctype}" if $webvar{billinguser}; | 
|---|
| 974 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr}", | 
|---|
| 975 | "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n". | 
|---|
| 976 | "Description: $webvar{desc}\n". | 
|---|
| 977 | ($webvar{rdns} ? "DNS name/pattern: $webvar{rdns}\n" : ''). | 
|---|
| 978 | "\nAllocated by: $authuser\n"); | 
|---|
| 979 | } | 
|---|
| 980 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ". | 
|---|
| 981 | "'$webvar{alloctype}' ($msg)"; | 
|---|
| 982 | } else { | 
|---|
| 983 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ". | 
|---|
| 984 | "'$webvar{alloctype}' by $authuser failed: '$msg'"; | 
|---|
| 985 | $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}' failed:"); | 
|---|
| 986 | $page->param(errmsg => $msg); | 
|---|
| 987 | } | 
|---|
| 988 |  | 
|---|
| 989 | } # end insertAssign() | 
|---|
| 990 |  | 
|---|
| 991 |  | 
|---|
| 992 | # Does some basic checks on common input data to make sure nothing | 
|---|
| 993 | # *really* weird gets in to the database through this script. | 
|---|
| 994 | # Does NOT do complete input validation!!! | 
|---|
| 995 | sub validateInput { | 
|---|
| 996 | if ($webvar{city} eq '-') { | 
|---|
| 997 | $page->param(err => 'Please choose a city'); | 
|---|
| 998 | return; | 
|---|
| 999 | } | 
|---|
| 1000 |  | 
|---|
| 1001 | # Alloctype check. | 
|---|
| 1002 | chomp $webvar{alloctype}; | 
|---|
| 1003 | if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) { | 
|---|
| 1004 | # Danger! Danger!  alloctype should ALWAYS be set by a dropdown.  Anyone | 
|---|
| 1005 | # managing to call things in such a way as to cause this deserves a cryptic error. | 
|---|
| 1006 | $page->param(err => 'Invalid alloctype'); | 
|---|
| 1007 | return; | 
|---|
| 1008 | } | 
|---|
| 1009 |  | 
|---|
| 1010 | # CustID check | 
|---|
| 1011 | # We have different handling for customer allocations and "internal" or "our" allocations | 
|---|
| 1012 | if ($def_custids{$webvar{alloctype}} eq '') { | 
|---|
| 1013 | if (!$webvar{custid}) { | 
|---|
| 1014 | $page->param(err => 'Please enter a customer ID.'); | 
|---|
| 1015 | return; | 
|---|
| 1016 | } | 
|---|
| 1017 | # Crosscheck with billing. | 
|---|
| 1018 | my $status = CustIDCK->custid_exist($webvar{custid}); | 
|---|
| 1019 | if ($CustIDCK::Error) { | 
|---|
| 1020 | $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg); | 
|---|
| 1021 | return; | 
|---|
| 1022 | } | 
|---|
| 1023 | if (!$status) { | 
|---|
| 1024 | $page->param(err => "Customer ID not valid.  Make sure the Customer ID ". | 
|---|
| 1025 | "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ". | 
|---|
| 1026 | "non-customer assignments."); | 
|---|
| 1027 | return; | 
|---|
| 1028 | } | 
|---|
| 1029 | #    print "<!-- [ In validateInput().  Insert customer ID cross-check here. ] -->\n"; | 
|---|
| 1030 | } else { | 
|---|
| 1031 | # New!  Improved!  And now Loaded From The Database!! | 
|---|
| 1032 | if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) { | 
|---|
| 1033 | $webvar{custid} = $def_custids{$webvar{alloctype}}; | 
|---|
| 1034 | } | 
|---|
| 1035 |  | 
|---|
| 1036 | # and custom overrides. | 
|---|
| 1037 | ##fixme:  need to genericize this: | 
|---|
| 1038 | ## check a database table for "flag:field:value" overrides? | 
|---|
| 1039 | #    if ($webvar{somflag}) { | 
|---|
| 1040 | #      $webvar{custid} = 'special1'; | 
|---|
| 1041 | #    } | 
|---|
| 1042 |  | 
|---|
| 1043 | } | 
|---|
| 1044 |  | 
|---|
| 1045 | ## hmmm....  is this even useful? | 
|---|
| 1046 | if (0) { | 
|---|
| 1047 | # Check POP location | 
|---|
| 1048 | my $flag; | 
|---|
| 1049 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| 1050 | $flag = 'for a routed netblock'; | 
|---|
| 1051 | foreach (@poplist) { | 
|---|
| 1052 | if (/^$webvar{city}$/) { | 
|---|
| 1053 | $flag = 'n'; | 
|---|
| 1054 | last; | 
|---|
| 1055 | } | 
|---|
| 1056 | } | 
|---|
| 1057 | } else { | 
|---|
| 1058 | $flag = 'n'; | 
|---|
| 1059 | ##fixme:  hook to force-set POP or city on certain alloctypes | 
|---|
| 1060 | # if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; } | 
|---|
| 1061 | if ($webvar{pop} && $webvar{pop} =~ /^-$/) { | 
|---|
| 1062 | $flag = 'to route the block from/through'; | 
|---|
| 1063 | } | 
|---|
| 1064 | } | 
|---|
| 1065 |  | 
|---|
| 1066 | # if the alloctype has a restricted city/POP list as determined above, | 
|---|
| 1067 | # and the reqested city/POP does not match that list, complain | 
|---|
| 1068 | if ($flag ne 'n') { | 
|---|
| 1069 | $page->param(err => "Please choose a valid POP location $flag.  Valid ". | 
|---|
| 1070 | "POP locations are currently:<br>\n".join (" - ", @poplist)); | 
|---|
| 1071 | return; | 
|---|
| 1072 | } | 
|---|
| 1073 | } | 
|---|
| 1074 |  | 
|---|
| 1075 | # VRF.  Not a full validity check, just a basic sanity check. | 
|---|
| 1076 | if ($webvar{vrf}) { | 
|---|
| 1077 | # Trim leading and trailing whitespace first | 
|---|
| 1078 | $webvar{vrf} =~ s/^\s+//; | 
|---|
| 1079 | $webvar{vrf} =~ s/\s+$//; | 
|---|
| 1080 | if ($webvar{vrf} !~ /^[\w\d_.-]{1,32}$/) { | 
|---|
| 1081 | $page->param(err => "VRF values may only contain alphanumerics, and may not be more than 32 characters"); | 
|---|
| 1082 | return; | 
|---|
| 1083 | } | 
|---|
| 1084 | } | 
|---|
| 1085 |  | 
|---|
| 1086 | # VLAN.  Should we allow/use VLAN names, or just the numeric ID? | 
|---|
| 1087 | if ($webvar{vlan}) { | 
|---|
| 1088 | # Trim leading and trailing whitespace first | 
|---|
| 1089 | $webvar{vlan} =~ s/^\s+//; | 
|---|
| 1090 | $webvar{vlan} =~ s/\s+$//; | 
|---|
| 1091 | # Then any surrounding a comma | 
|---|
| 1092 | $webvar{vlan} =~ s/\s*,\s*/,/g; | 
|---|
| 1093 | # ...  ve make it ze configurable thingy! | 
|---|
| 1094 | if ($IPDB::numeric_vlan) { | 
|---|
| 1095 | if ($webvar{vlan} !~ /^[\d,-]+$/) { | 
|---|
| 1096 | $page->param(err => "VLANs must be numeric"); | 
|---|
| 1097 | return; | 
|---|
| 1098 | } | 
|---|
| 1099 | } else { | 
|---|
| 1100 | if ($webvar{vlan} !~ /^[\w\d_.,-]+$/) { | 
|---|
| 1101 | $page->param(err => "VLANs must be alphanumeric"); | 
|---|
| 1102 | return; | 
|---|
| 1103 | } | 
|---|
| 1104 | } | 
|---|
| 1105 | } | 
|---|
| 1106 |  | 
|---|
| 1107 | # Backup fields.  Minimal sanity checks. | 
|---|
| 1108 | # Bypass if the user isn't authorized for backup data, or if the checkbox is unchecked | 
|---|
| 1109 | if ($IPDBacl{$authuser} =~ /s/ && defined($webvar{backupfields})) { | 
|---|
| 1110 | for my $bkfield (qw(brand model)) { | 
|---|
| 1111 | if (!$webvar{"bk$bkfield"}) { | 
|---|
| 1112 | $page->param(err => "Backup $bkfield must be filled in if IP/netblock is flagged for backup"); | 
|---|
| 1113 | return; | 
|---|
| 1114 | } | 
|---|
| 1115 | if ($webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9\s_.-]+$/) { | 
|---|
| 1116 | $page->param(err => "Invalid characters in backup $bkfield"); | 
|---|
| 1117 | return; | 
|---|
| 1118 | } | 
|---|
| 1119 | } | 
|---|
| 1120 | for my $bkfield (qw(type src user)) {  # no spaces in these! | 
|---|
| 1121 | if ($webvar{"bk$bkfield"} && $webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9_.-]+$/) { | 
|---|
| 1122 | $page->param(err => "Invalid characters in backup $bkfield"); | 
|---|
| 1123 | return; | 
|---|
| 1124 | } | 
|---|
| 1125 | } | 
|---|
| 1126 | if ($webvar{bkport}) { | 
|---|
| 1127 | $webvar{bkport} =~ s/^\s+//g; | 
|---|
| 1128 | $webvar{bkport} =~ s/\s+$//g; | 
|---|
| 1129 | if ($webvar{bkport} !~ /^\d+$/) { | 
|---|
| 1130 | $page->param(err => "Backup port must be numeric"); | 
|---|
| 1131 | return; | 
|---|
| 1132 | } | 
|---|
| 1133 | } | 
|---|
| 1134 | ##fixme:  code review:  should normalize $webvar{cidr} variants so we can | 
|---|
| 1135 | # check for non-/32 allocations having the backup IP field filled in here, | 
|---|
| 1136 | # instead of failing on the allocation or update attempt | 
|---|
| 1137 | if ($webvar{bkip}) { | 
|---|
| 1138 | $webvar{bkip} =~ s/^\s+//g; | 
|---|
| 1139 | $webvar{bkip} =~ s/\s+$//g; | 
|---|
| 1140 | if ($webvar{bkip} !~ /^[\da-fA-F:.]+$/) { | 
|---|
| 1141 | $page->param(err => "Backup IP must be an IP"); | 
|---|
| 1142 | return; | 
|---|
| 1143 | } | 
|---|
| 1144 | } | 
|---|
| 1145 | } # backup | 
|---|
| 1146 |  | 
|---|
| 1147 | return 'OK'; | 
|---|
| 1148 | } # end validateInput | 
|---|
| 1149 |  | 
|---|
| 1150 |  | 
|---|
| 1151 | # Displays details of a specific allocation in a form | 
|---|
| 1152 | # Allows update/delete | 
|---|
| 1153 | # action=edit | 
|---|
| 1154 | sub edit { | 
|---|
| 1155 |  | 
|---|
| 1156 | # snag block info from db | 
|---|
| 1157 | my $blockinfo = getBlockData($ip_dbh, $webvar{id}, $webvar{basetype}); | 
|---|
| 1158 | my $cidr = new NetAddr::IP $blockinfo->{block}; | 
|---|
| 1159 | $page->param(id       => $webvar{id}); | 
|---|
| 1160 | $page->param(basetype => $webvar{basetype}); | 
|---|
| 1161 |  | 
|---|
| 1162 | # Tree navigation | 
|---|
| 1163 | my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id}); | 
|---|
| 1164 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 1165 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 1166 |  | 
|---|
| 1167 | # Show link to IP list for pools | 
|---|
| 1168 | $page->param(ispool => 1) if $blockinfo->{type} =~ /^.[dp]$/; | 
|---|
| 1169 |  | 
|---|
| 1170 | # Clean up extra whitespace on alloc type.  Mainly a legacy-data cleanup. | 
|---|
| 1171 | $blockinfo->{type} =~ s/\s//; | 
|---|
| 1172 |  | 
|---|
| 1173 | ##fixme:  The case of "allocation larger than a /24" (or any similar case | 
|---|
| 1174 | # where the allocation is larger than the zone(s) in DNS) doesn't work well. | 
|---|
| 1175 | # Best solution may just be to add a warning that the entry shown may not be | 
|---|
| 1176 | # correct/complete. | 
|---|
| 1177 | if ($blockinfo->{revavail} || $blockinfo->{revpartial}) { | 
|---|
| 1178 | $page->param(showrev => ($blockinfo->{revavail} || $blockinfo->{revpartial}) ); | 
|---|
| 1179 | $page->param(v6 => $cidr->{isv6}); | 
|---|
| 1180 | $page->param(dnslink => $IPDB::dnsadmin_url); | 
|---|
| 1181 |  | 
|---|
| 1182 | # get the DNSAdmin zone ID(s) for this allocation. | 
|---|
| 1183 | # Multiple zones should be rare, but are NOT impossible! | 
|---|
| 1184 | my $revlist = getRevID($ip_dbh, user => $authuser, cidr => $blockinfo->{block}, | 
|---|
| 1185 | location => $blockinfo->{location}); | 
|---|
| 1186 | $page->param(revlist => $revlist) if $revlist; | 
|---|
| 1187 |  | 
|---|
| 1188 | my $cached; | 
|---|
| 1189 | # Get rDNS info;  duplicates a bit of getBlockData but also does the RPC call if possible | 
|---|
| 1190 | ($blockinfo->{rdns},$cached) = getBlockRDNS($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, user => $authuser); | 
|---|
| 1191 | $page->param(rdns     => $blockinfo->{rdns}); | 
|---|
| 1192 | # visual flag that we're working IPDB-local, not off more authoritative data in dnsadmin | 
|---|
| 1193 | $page->param(cached   => $cached); | 
|---|
| 1194 |  | 
|---|
| 1195 | # Limit the per-IP rDNS list based on CIDR length;  larger ones just take up too much space. | 
|---|
| 1196 | # Also, don't show on IP pools;  the individual IPs will have a space for rDNS | 
|---|
| 1197 | # Don't show on single IPs;  these use the "pattern" field | 
|---|
| 1198 | if ($IPDBacl{$authuser} =~ /c/ | 
|---|
| 1199 | && $cidr->masklen != $cidr->bits | 
|---|
| 1200 | && ($cidr->bits - $cidr->masklen) <= $IPDB::maxrevlist | 
|---|
| 1201 | # config flag for "all block types" OR "not-a-pool-or-IP type" | 
|---|
| 1202 | && ($IPDB::revlistalltypes || $blockinfo->{type} !~ /^.[dpi]/) | 
|---|
| 1203 | # safety against trying to retrieve and display more than 1k (10 bits, /22 v4) worth of individual IPs | 
|---|
| 1204 | # ever.  If you really need to manage a long list of IPs like that all in one place, you can use the DNS | 
|---|
| 1205 | # management tool.  Even a /26 is a bit much, really. | 
|---|
| 1206 | && ($cidr->bits - $cidr->masklen) <= 10 | 
|---|
| 1207 | # do we want to allow v6 at all? | 
|---|
| 1208 | #&& ! $cidr->{isv6} | 
|---|
| 1209 | ) { | 
|---|
| 1210 | $page->param(r_iplist => getRDNSbyIP($ip_dbh, id => $webvar{id}, type => $blockinfo->{type}, | 
|---|
| 1211 | range => $blockinfo->{block}, user => $authuser) ); | 
|---|
| 1212 | } | 
|---|
| 1213 | } # rDNS availability check | 
|---|
| 1214 |  | 
|---|
| 1215 | # backup data | 
|---|
| 1216 | if ($blockinfo->{hasbk}) { | 
|---|
| 1217 | $page->param(hasbackup => $blockinfo->{hasbk}); | 
|---|
| 1218 | for my $bkfield (@IPDB::backupfields) { | 
|---|
| 1219 | $page->param("bk$bkfield" => $blockinfo->{"bk$bkfield"}); | 
|---|
| 1220 | } | 
|---|
| 1221 | $page->param(bktelnet => 1) if $blockinfo->{bktype} eq 'telnet'; | 
|---|
| 1222 | $page->param(bkssh => 1) if $blockinfo->{bktype} eq 'SSH'; | 
|---|
| 1223 | } | 
|---|
| 1224 |  | 
|---|
| 1225 | # consider extending this to show time as well as date | 
|---|
| 1226 | my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod}; | 
|---|
| 1227 | $page->param(lastmod  => $lastmod); | 
|---|
| 1228 |  | 
|---|
| 1229 | $page->param(block    => $blockinfo->{block}); | 
|---|
| 1230 | $page->param(city     => $blockinfo->{city}); | 
|---|
| 1231 | $page->param(custid   => $blockinfo->{custid}); | 
|---|
| 1232 |  | 
|---|
| 1233 | ##fixme The check here should be built from the database | 
|---|
| 1234 | # Need to expand to support pool types too | 
|---|
| 1235 | if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) { | 
|---|
| 1236 | $page->param(changetype => 1); | 
|---|
| 1237 | $page->param(alloctype => [ | 
|---|
| 1238 | { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" }, | 
|---|
| 1239 | { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" }, | 
|---|
| 1240 | { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" }, | 
|---|
| 1241 | { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" }, | 
|---|
| 1242 | { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" }, | 
|---|
| 1243 | { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" }, | 
|---|
| 1244 | { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" }, | 
|---|
| 1245 | ] | 
|---|
| 1246 | ); | 
|---|
| 1247 | } else { | 
|---|
| 1248 | $page->param(disptype => $disp_alloctypes{$blockinfo->{type}}); | 
|---|
| 1249 | $page->param(type => $blockinfo->{type}); | 
|---|
| 1250 | } | 
|---|
| 1251 |  | 
|---|
| 1252 | ## node hack | 
|---|
| 1253 | my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $blockinfo->{block}); | 
|---|
| 1254 | #  $page->param(havenodeid => $nodeid); | 
|---|
| 1255 | $page->param(nodename => $nodename); | 
|---|
| 1256 |  | 
|---|
| 1257 | ##fixme:  this whole hack needs cleanup and generalization for all alloctypes | 
|---|
| 1258 | ##fixme:  arguably a bug that presence of a nodeid implies it can be changed.. | 
|---|
| 1259 | if ($IPDBacl{$authuser} =~ /c/) { | 
|---|
| 1260 | my $nlist = getNodeList($ip_dbh, $nodeid); | 
|---|
| 1261 | $page->param(nodelist => $nlist); | 
|---|
| 1262 | } | 
|---|
| 1263 | ## end node hack | 
|---|
| 1264 |  | 
|---|
| 1265 | #  $page->param(vrf      => $blockinfo->{vrf}); | 
|---|
| 1266 | $page->param(vlan     => $blockinfo->{vlan}); | 
|---|
| 1267 |  | 
|---|
| 1268 | # Reserved-for-expansion | 
|---|
| 1269 | $page->param(reserve  => $blockinfo->{reserve}); | 
|---|
| 1270 | $page->param(reserve_id => $blockinfo->{reserve_id}); | 
|---|
| 1271 | my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network; | 
|---|
| 1272 | $page->param(newblock => $newblock); | 
|---|
| 1273 |  | 
|---|
| 1274 | # not happy with the upside-down logic, but... | 
|---|
| 1275 | $page->param(swipable => $blockinfo->{type} !~ /.i/); | 
|---|
| 1276 | $page->param(swip     => $blockinfo->{swip} ne 'n') if $blockinfo->{swip}; | 
|---|
| 1277 |  | 
|---|
| 1278 | $page->param(circid   => $blockinfo->{circuitid}); | 
|---|
| 1279 | $page->param(desc     => $blockinfo->{description}); | 
|---|
| 1280 | $page->param(notes    => $blockinfo->{notes}); | 
|---|
| 1281 |  | 
|---|
| 1282 | # Check to see if we can display sensitive data | 
|---|
| 1283 | $page->param(nocling  => $IPDBacl{$authuser} =~ /s/); | 
|---|
| 1284 | $page->param(privdata => $blockinfo->{privdata}); | 
|---|
| 1285 |  | 
|---|
| 1286 | # retrieve any notices | 
|---|
| 1287 | blocknotices($webvar{id}); | 
|---|
| 1288 |  | 
|---|
| 1289 | # ACL trickery - these two template booleans control the presence of all form/input tags | 
|---|
| 1290 | $page->param(maychange => $IPDBacl{$authuser} =~ /c/); | 
|---|
| 1291 | $page->param(maydel => $IPDBacl{$authuser} =~ /d/); | 
|---|
| 1292 |  | 
|---|
| 1293 | # Need to find internal knobs to twist to actually vary these.  (Ab)use "change" flag for now | 
|---|
| 1294 | $page->param(maymerge => ($IPDBacl{$authuser} =~ /m/ && $blockinfo->{type} !~ /^.i$/)); | 
|---|
| 1295 |  | 
|---|
| 1296 | if ($IPDBacl{$authuser} =~ /c/ && $blockinfo->{type} !~ /^.i$/) { | 
|---|
| 1297 | if ($blockinfo->{type} =~ /^.p$/) { | 
|---|
| 1298 | # PPP pools | 
|---|
| 1299 | $page->param(maysplit => 1) if $cidr->masklen+1 < $cidr->bits; | 
|---|
| 1300 | } elsif ($blockinfo->{type} =~ /.d/) { | 
|---|
| 1301 | # Non-PPP pools | 
|---|
| 1302 | $page->param(maysplit => 1) if $cidr->masklen+2 < $cidr->bits; | 
|---|
| 1303 | } else { | 
|---|
| 1304 | # Standard netblocks.  Arguably allowing splitting these down to single IPs | 
|---|
| 1305 | # doesn't make much sense, but forcing users to apply allocation types | 
|---|
| 1306 | # "properly" is worse than herding cats. | 
|---|
| 1307 | $page->param(maysplit => 1) if $cidr->masklen < $cidr->bits; | 
|---|
| 1308 | } | 
|---|
| 1309 | } | 
|---|
| 1310 |  | 
|---|
| 1311 | } # edit() | 
|---|
| 1312 |  | 
|---|
| 1313 |  | 
|---|
| 1314 | # Stuff new info about a block into the db | 
|---|
| 1315 | # action=update | 
|---|
| 1316 | sub update { | 
|---|
| 1317 | if ($IPDBacl{$authuser} !~ /c/) { | 
|---|
| 1318 | $aclerr = 'updateblock'; | 
|---|
| 1319 | return; | 
|---|
| 1320 | } | 
|---|
| 1321 |  | 
|---|
| 1322 | # Collect existing block info here, since we need it for the breadcrumb nav | 
|---|
| 1323 | my $binfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype}); | 
|---|
| 1324 | my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id}); | 
|---|
| 1325 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 1326 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 1327 |  | 
|---|
| 1328 | # Make sure incoming data is in correct format - custID among other things. | 
|---|
| 1329 | return if !validateInput; | 
|---|
| 1330 |  | 
|---|
| 1331 | $webvar{swip} = 'n' if !$webvar{swip}; | 
|---|
| 1332 |  | 
|---|
| 1333 | my %updargs = ( | 
|---|
| 1334 | custid          => $webvar{custid}, | 
|---|
| 1335 | city            => $webvar{city}, | 
|---|
| 1336 | description     => $webvar{desc}, | 
|---|
| 1337 | notes           => $webvar{notes}, | 
|---|
| 1338 | circuitid       => $webvar{circid}, | 
|---|
| 1339 | block           => $webvar{block}, | 
|---|
| 1340 | type            => $webvar{alloctype}, | 
|---|
| 1341 | swip            => $webvar{swip}, | 
|---|
| 1342 | rdns            => $webvar{rdns}, | 
|---|
| 1343 | vrf             => $webvar{vrf}, | 
|---|
| 1344 | vlan            => $webvar{vlan}, | 
|---|
| 1345 | user            => $authuser, | 
|---|
| 1346 | ); | 
|---|
| 1347 |  | 
|---|
| 1348 | # Check to see if user is allowed to do anything with sensitive data | 
|---|
| 1349 | if ($IPDBacl{$authuser} =~ /s/) { | 
|---|
| 1350 | $updargs{privdata} = $webvar{privdata}; | 
|---|
| 1351 | for my $bkfield (@IPDB::backupfields) { | 
|---|
| 1352 | $updargs{"bk$bkfield"} = $webvar{"bk$bkfield"}; | 
|---|
| 1353 | } | 
|---|
| 1354 | $updargs{backup} = $webvar{backupfields}; | 
|---|
| 1355 | } else { | 
|---|
| 1356 | # If the user doesn't have permissions to monkey with NOC-things, pass | 
|---|
| 1357 | # a flag so we don't treat it as "backup data removed" | 
|---|
| 1358 | $updargs{ignorebk} = 1; | 
|---|
| 1359 | } | 
|---|
| 1360 |  | 
|---|
| 1361 | # Semioptional values | 
|---|
| 1362 | $updargs{node} = $webvar{node} if $webvar{node}; | 
|---|
| 1363 |  | 
|---|
| 1364 | # collect per-IP rDNS fields.  only copy over the ones that actually have something in them. | 
|---|
| 1365 | my %iprev; | 
|---|
| 1366 | foreach (sort keys %webvar) { | 
|---|
| 1367 | # skip all but the host_* fields | 
|---|
| 1368 | next unless /^host_[\d.a-fA-F:]+/; | 
|---|
| 1369 | # propagate the per-IP rDNS if present | 
|---|
| 1370 | $iprev{$_} = $webvar{$_} if $webvar{$_}; | 
|---|
| 1371 | # set the rDNS to be cleared if there was a value before (was_host_* field), | 
|---|
| 1372 | # but there's no value in the matching visible form field now (host_*) | 
|---|
| 1373 | $iprev{$_} = '' if $webvar{"was_$_"} && !$webvar{$_}; | 
|---|
| 1374 | } | 
|---|
| 1375 |  | 
|---|
| 1376 | # and now IPv6 | 
|---|
| 1377 | ##fixme:  how to remove an entry?  maybe treat empty host as "delete meeeee!"? | 
|---|
| 1378 | if ($webvar{v6list}) { | 
|---|
| 1379 | my @v6lines = split /\n/, $webvar{v6list}; | 
|---|
| 1380 | foreach (@v6lines) { | 
|---|
| 1381 | s/^\s+//; | 
|---|
| 1382 | s/\s+$//; | 
|---|
| 1383 | next if /^$/; | 
|---|
| 1384 | my ($ip,$name) = split /,/; | 
|---|
| 1385 | $iprev{"host_$ip"} = $name; | 
|---|
| 1386 | } | 
|---|
| 1387 | } | 
|---|
| 1388 |  | 
|---|
| 1389 | # Merge with reserved freeblock | 
|---|
| 1390 | $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme}; | 
|---|
| 1391 |  | 
|---|
| 1392 | my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev); | 
|---|
| 1393 |  | 
|---|
| 1394 | if ($code eq 'FAIL') { | 
|---|
| 1395 | syslog "err", "$authuser could not update block/IP $webvar{block} ($binfo->{block}): '$msg'"; | 
|---|
| 1396 | $page->param(err => "Could not update block/IP $binfo->{block}: $msg"); | 
|---|
| 1397 | return; | 
|---|
| 1398 | } | 
|---|
| 1399 |  | 
|---|
| 1400 | # If we get here, the operation succeeded. | 
|---|
| 1401 | syslog "notice", "$authuser updated $webvar{block} ($binfo->{block})"; | 
|---|
| 1402 | ##fixme:  log details of the change?  old way is in the .debug stream anyway. | 
|---|
| 1403 | ##fixme:  need to wedge something in to allow "update:field" notifications | 
|---|
| 1404 | ## hmm.  how to tell what changed?  O_o | 
|---|
| 1405 | mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $binfo->{block}", | 
|---|
| 1406 | "$binfo->{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on'; | 
|---|
| 1407 |  | 
|---|
| 1408 | ## node hack | 
|---|
| 1409 | if ($webvar{node} && $webvar{node} ne '-') { | 
|---|
| 1410 | my $nodename = getNodeName($ip_dbh, $webvar{node}); | 
|---|
| 1411 | $page->param(nodename => $nodename); | 
|---|
| 1412 | } | 
|---|
| 1413 | ## end node hack | 
|---|
| 1414 |  | 
|---|
| 1415 | # Link back to browse-routed or list-pool page on "Update complete" page. | 
|---|
| 1416 | my $pblock = getBlockData($ip_dbh, $binfo->{parent_id}); | 
|---|
| 1417 | $page->param(backid => $binfo->{parent_id}); | 
|---|
| 1418 | $page->param(backblock => $pblock->{block}); | 
|---|
| 1419 | $page->param(backpool => ($webvar{basetype} eq 'i')); | 
|---|
| 1420 |  | 
|---|
| 1421 | # Do some HTML fiddling here instead of using ESCAPE=HTML in the template, | 
|---|
| 1422 | # because otherwise we can't convert \n to <br>.  *sigh* | 
|---|
| 1423 | $webvar{notes} = $q->escapeHTML($webvar{notes});      # escape first... | 
|---|
| 1424 | $webvar{notes} =~ s/\n/<br>\n/;                       # ... then convert newlines | 
|---|
| 1425 | $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : " "); | 
|---|
| 1426 | $webvar{privdata} =~ s/\n/<br>\n/; | 
|---|
| 1427 |  | 
|---|
| 1428 | if ($webvar{expandme}) { | 
|---|
| 1429 | # this is fugly but still faster than hitting the DB again with getBlockData() | 
|---|
| 1430 | my $tmp = new NetAddr::IP $binfo->{block}; | 
|---|
| 1431 | my $fb = new NetAddr::IP $binfo->{reserve}; | 
|---|
| 1432 | my @newblock = $tmp->compact($fb); | 
|---|
| 1433 | $page->param(cidr => $newblock[0]); | 
|---|
| 1434 | } else { | 
|---|
| 1435 | $page->param(cidr => $binfo->{block}); | 
|---|
| 1436 | } | 
|---|
| 1437 | $page->param(rdns => $webvar{rdns}); | 
|---|
| 1438 | $page->param(city => $webvar{city}); | 
|---|
| 1439 | $page->param(disptype => $disp_alloctypes{$webvar{alloctype}}); | 
|---|
| 1440 | $page->param(vlan => $webvar{vlan}); | 
|---|
| 1441 | $page->param(custid => $webvar{custid}); | 
|---|
| 1442 | $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No'); | 
|---|
| 1443 | $page->param(circid => $webvar{circid}); | 
|---|
| 1444 | $page->param(desc => $webvar{desc}); | 
|---|
| 1445 | $page->param(notes => $webvar{notes}); | 
|---|
| 1446 | if ($IPDBacl{$authuser} =~ /s/) { | 
|---|
| 1447 | $page->param(nocling  => 1); | 
|---|
| 1448 | $page->param(privdata => $webvar{privdata}); | 
|---|
| 1449 | if ($webvar{backupfields} && $webvar{backupfields} eq 'on') { | 
|---|
| 1450 | $page->param(hasbackup => 1); | 
|---|
| 1451 | for my $bkfield (@IPDB::backupfields) { | 
|---|
| 1452 | $page->param("bk$bkfield" => $webvar{"bk$bkfield"}); | 
|---|
| 1453 | } | 
|---|
| 1454 | } | 
|---|
| 1455 | } | 
|---|
| 1456 |  | 
|---|
| 1457 | } # update() | 
|---|
| 1458 |  | 
|---|
| 1459 |  | 
|---|
| 1460 | sub prepSplit { | 
|---|
| 1461 | if ($IPDBacl{$authuser} !~ /c/) { | 
|---|
| 1462 | $aclerr = 'splitblock'; | 
|---|
| 1463 | return; | 
|---|
| 1464 | } | 
|---|
| 1465 |  | 
|---|
| 1466 | my $blockinfo = getBlockData($ip_dbh, $webvar{block}); | 
|---|
| 1467 |  | 
|---|
| 1468 | # Tree navigation | 
|---|
| 1469 | my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id}); | 
|---|
| 1470 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 1471 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 1472 |  | 
|---|
| 1473 | if ($blockinfo->{type} =~ /^.i$/) { | 
|---|
| 1474 | $page->param(err => "Can't split a single IP allocation"); | 
|---|
| 1475 | return; | 
|---|
| 1476 | } | 
|---|
| 1477 |  | 
|---|
| 1478 | # Info about current allocation | 
|---|
| 1479 | $page->param(oldblock => $blockinfo->{block}); | 
|---|
| 1480 | $page->param(block => $webvar{block}); | 
|---|
| 1481 |  | 
|---|
| 1482 | # Note that there are probably different rules that should be followed to restrict splitting IPv6 blocks; | 
|---|
| 1483 | # strictly speaking it will be exceptionally rare to see smaller than a /64 assigned to a customer, since that | 
|---|
| 1484 | # breaks auto-addressing schemes. | 
|---|
| 1485 |  | 
|---|
| 1486 | # Generate possible splits | 
|---|
| 1487 | my $block = new NetAddr::IP $blockinfo->{block}; | 
|---|
| 1488 | my $oldmask = $block->masklen; | 
|---|
| 1489 | if ($blockinfo->{type} =~ /^.d$/) { | 
|---|
| 1490 | # Non-PPP pools | 
|---|
| 1491 | $page->param(ispool => 1); | 
|---|
| 1492 | if ($oldmask+2 >= $block->bits) { | 
|---|
| 1493 | $page->param(err => "Can't split a standard netblock pool any further"); | 
|---|
| 1494 | return; | 
|---|
| 1495 | } | 
|---|
| 1496 | # Allow splitting down to v4 /30 (which results in one usable IP;  dubiously useful) | 
|---|
| 1497 | $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-2; | 
|---|
| 1498 | } elsif ($blockinfo->{type} =~ /.p/) { | 
|---|
| 1499 | $page->param(ispool => 1); | 
|---|
| 1500 | # Allow splitting PPP pools down to v4 /31 | 
|---|
| 1501 | $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits-1; | 
|---|
| 1502 | } else { | 
|---|
| 1503 | # Allow splitting all other non-pool netblocks down to single IPs, which... | 
|---|
| 1504 | # arguably should be *aggregated* in a pool.  Except where they shouldn't. | 
|---|
| 1505 | $page->param(sp4mask => $oldmask+2) if $oldmask+2 <= $block->bits; | 
|---|
| 1506 | } | 
|---|
| 1507 | # set the split-in-half mask | 
|---|
| 1508 | $page->param(sp2mask => $oldmask+1); | 
|---|
| 1509 |  | 
|---|
| 1510 | # Generate possible shrink targets | 
|---|
| 1511 | my @keepers = $block->split($block->masklen+1); | 
|---|
| 1512 | $page->param(newblockA => $keepers[0]); | 
|---|
| 1513 | $page->param(newblockB => $keepers[1]); | 
|---|
| 1514 | } # prepSplit() | 
|---|
| 1515 |  | 
|---|
| 1516 |  | 
|---|
| 1517 | sub doSplit { | 
|---|
| 1518 | if ($IPDBacl{$authuser} !~ /c/) { | 
|---|
| 1519 | $aclerr = 'splitblock'; | 
|---|
| 1520 | return; | 
|---|
| 1521 | } | 
|---|
| 1522 |  | 
|---|
| 1523 | ##fixme:  need consistent way to identify "this thing that is this thing" with only the ID | 
|---|
| 1524 | # also applies to other locations | 
|---|
| 1525 | my $blockinfo = getBlockData($ip_dbh, $webvar{block}); | 
|---|
| 1526 |  | 
|---|
| 1527 | # Tree navigation | 
|---|
| 1528 | my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id}); | 
|---|
| 1529 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 1530 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 1531 |  | 
|---|
| 1532 | if ($blockinfo->{type} =~ /^.i$/) { | 
|---|
| 1533 | $page->param(err => "Can't split a single IP allocation"); | 
|---|
| 1534 | return; | 
|---|
| 1535 | } | 
|---|
| 1536 |  | 
|---|
| 1537 | if ($webvar{subact} eq 'split') { | 
|---|
| 1538 | $page->param(issplit => 1); | 
|---|
| 1539 | my $block = new NetAddr::IP $blockinfo->{block}; | 
|---|
| 1540 | my $newblocks = splitBlock($ip_dbh, id => $webvar{block}, basetype => 'b', newmask => $webvar{split}, | 
|---|
| 1541 | user => $authuser); | 
|---|
| 1542 | if ($newblocks) { | 
|---|
| 1543 | $page->param(newblocks => $newblocks); | 
|---|
| 1544 | } else { | 
|---|
| 1545 | $page->param(err => $IPDB::errstr); | 
|---|
| 1546 | } | 
|---|
| 1547 |  | 
|---|
| 1548 | } elsif ($webvar{subact} eq 'shrink') { | 
|---|
| 1549 | $page->param(nid => $webvar{block}); | 
|---|
| 1550 | $page->param(newblock => $webvar{shrink}); | 
|---|
| 1551 | my $newfree = shrinkBlock($ip_dbh, $webvar{block}, $webvar{shrink}); | 
|---|
| 1552 | if ($newfree) { | 
|---|
| 1553 | $page->param(newfb => $newfree); | 
|---|
| 1554 | } else { | 
|---|
| 1555 | $page->param(err => $IPDB::errstr); | 
|---|
| 1556 | } | 
|---|
| 1557 |  | 
|---|
| 1558 | } else { | 
|---|
| 1559 | # Your llama is on fire. | 
|---|
| 1560 | $page->param(err => "Missing form field that shouldn't be missing."); | 
|---|
| 1561 | return; | 
|---|
| 1562 | } | 
|---|
| 1563 |  | 
|---|
| 1564 | # common bits | 
|---|
| 1565 | $page->param(cidr => $blockinfo->{block}); | 
|---|
| 1566 | # and the backlink to the parent container | 
|---|
| 1567 | my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}); | 
|---|
| 1568 | $page->param(backid => $blockinfo->{parent_id}); | 
|---|
| 1569 | $page->param(backblock => $pinfo->{block}); | 
|---|
| 1570 | } # doSplit() | 
|---|
| 1571 |  | 
|---|
| 1572 |  | 
|---|
| 1573 | # Set up for merge | 
|---|
| 1574 | sub prepMerge { | 
|---|
| 1575 | if ($IPDBacl{$authuser} !~ /m/) { | 
|---|
| 1576 | $aclerr = 'mergeblock'; | 
|---|
| 1577 | return; | 
|---|
| 1578 | } | 
|---|
| 1579 |  | 
|---|
| 1580 | my $binfo = getBlockData($ip_dbh, $webvar{block}); | 
|---|
| 1581 |  | 
|---|
| 1582 | # Tree navigation | 
|---|
| 1583 | my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id}); | 
|---|
| 1584 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 1585 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 1586 |  | 
|---|
| 1587 | $page->param(block => $webvar{block}); | 
|---|
| 1588 | $page->param(ispool => $binfo->{type} =~ /.[dp]/); | 
|---|
| 1589 | $page->param(ismaster => $binfo->{type} eq 'mm'); | 
|---|
| 1590 | $page->param(oldblock => $binfo->{block}); | 
|---|
| 1591 | $page->param(oldtype => $disp_alloctypes{$binfo->{type}}); | 
|---|
| 1592 | $page->param(typelist => getTypeList($ip_dbh, 'n', $binfo->{type}));  # down the rabbit hole we go... | 
|---|
| 1593 |  | 
|---|
| 1594 | # Strings for scope;  do this way so we don't have to edit them many places | 
|---|
| 1595 | $page->param(vis_keepall => $merge_display{keepall}); | 
|---|
| 1596 | $page->param(vis_mergepeer => $merge_display{mergepeer}); | 
|---|
| 1597 | $page->param(vis_clearpeer => $merge_display{clearpeer}); | 
|---|
| 1598 | $page->param(vis_clearall => $merge_display{clearall}); | 
|---|
| 1599 |  | 
|---|
| 1600 | } # prepMerge() | 
|---|
| 1601 |  | 
|---|
| 1602 |  | 
|---|
| 1603 | # Show what will be merged, present warnings about data loss | 
|---|
| 1604 | sub confMerge { | 
|---|
| 1605 | if ($IPDBacl{$authuser} !~ /m/) { | 
|---|
| 1606 | $aclerr = 'mergeblock'; | 
|---|
| 1607 | return; | 
|---|
| 1608 | } | 
|---|
| 1609 |  | 
|---|
| 1610 | if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) { | 
|---|
| 1611 | $page->param(err => 'New netmask required'); | 
|---|
| 1612 | return; | 
|---|
| 1613 | } | 
|---|
| 1614 |  | 
|---|
| 1615 | $page->param(block => $webvar{block}); | 
|---|
| 1616 | my $binfo = getBlockData($ip_dbh, $webvar{block}); | 
|---|
| 1617 | my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id}); | 
|---|
| 1618 | my $minfo = getBlockData($ip_dbh, $binfo->{master_id}); | 
|---|
| 1619 | my $block = new NetAddr::IP $binfo->{block}; | 
|---|
| 1620 |  | 
|---|
| 1621 | # Tree navigation | 
|---|
| 1622 | my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id}); | 
|---|
| 1623 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 1624 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 1625 |  | 
|---|
| 1626 | $page->param(oldblock => $binfo->{block}); | 
|---|
| 1627 | $page->param(oldtype => $disp_alloctypes{$binfo->{type}}); | 
|---|
| 1628 | $page->param(ismaster => $binfo->{type} eq 'mm'); | 
|---|
| 1629 | $page->param(ispool => $webvar{alloctype} =~ /.[dp]/); | 
|---|
| 1630 | $page->param(isleaf => $webvar{alloctype} =~ /.[enr]/); | 
|---|
| 1631 | $page->param(newtype => $webvar{alloctype}); | 
|---|
| 1632 | $page->param(newdisptype => $disp_alloctypes{$webvar{alloctype}}); | 
|---|
| 1633 | my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}"; | 
|---|
| 1634 | $newblock = $newblock->network; | 
|---|
| 1635 | $page->param(newmask => $webvar{newmask}); | 
|---|
| 1636 | $page->param(newblock => "$newblock"); | 
|---|
| 1637 |  | 
|---|
| 1638 | # get list of allocations and freeblocks to be merged | 
|---|
| 1639 | my $malloc_list = listForMerge($ip_dbh, $binfo->{parent_id}, $newblock, 'a'); | 
|---|
| 1640 | $page->param(mergealloc => $malloc_list); | 
|---|
| 1641 |  | 
|---|
| 1642 | $page->param(vis_scope => $merge_display{$webvar{scope}}); | 
|---|
| 1643 | $page->param(scope => $webvar{scope}); | 
|---|
| 1644 | } # confMerge() | 
|---|
| 1645 |  | 
|---|
| 1646 |  | 
|---|
| 1647 | # Make it so | 
|---|
| 1648 | sub doMerge { | 
|---|
| 1649 | if ($IPDBacl{$authuser} !~ /m/) { | 
|---|
| 1650 | $aclerr = 'mergeblock'; | 
|---|
| 1651 | return; | 
|---|
| 1652 | } | 
|---|
| 1653 |  | 
|---|
| 1654 | if (!$webvar{newmask} || $webvar{newmask} !~ /^\d+$/) { | 
|---|
| 1655 | $page->param(err => 'New netmask required'); | 
|---|
| 1656 | return; | 
|---|
| 1657 | } | 
|---|
| 1658 |  | 
|---|
| 1659 | $page->param(block => $webvar{block}); | 
|---|
| 1660 | my $binfo = getBlockData($ip_dbh, $webvar{block}); | 
|---|
| 1661 | my $pinfo = getBlockData($ip_dbh, $binfo->{parent_id}); | 
|---|
| 1662 | my $block = new NetAddr::IP $binfo->{block}; | 
|---|
| 1663 |  | 
|---|
| 1664 | # Tree navigation | 
|---|
| 1665 | my $crumbs = getBreadCrumbs($ip_dbh, $binfo->{parent_id}); | 
|---|
| 1666 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 1667 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 1668 |  | 
|---|
| 1669 | $page->param(oldblock => $binfo->{block}); | 
|---|
| 1670 | $page->param(oldtype => $disp_alloctypes{$binfo->{type}}); | 
|---|
| 1671 | $page->param(newdisptype => $disp_alloctypes{$webvar{newtype}}); | 
|---|
| 1672 | my $newblock = new NetAddr::IP $block->addr."/$webvar{newmask}"; | 
|---|
| 1673 | $newblock = $newblock->network; | 
|---|
| 1674 | $page->param(newblock => $newblock); | 
|---|
| 1675 | $page->param(vis_scope => $merge_display{$webvar{scope}}); | 
|---|
| 1676 |  | 
|---|
| 1677 | my $mlist = mergeBlocks($ip_dbh, $webvar{block}, %webvar, user => $authuser); | 
|---|
| 1678 |  | 
|---|
| 1679 | if ($mlist) { | 
|---|
| 1680 | #(newtype => $webvar{newtype}, newmask => $webvar{newmask})); | 
|---|
| 1681 | # Slice off first entry (the new parent - note this may be a new allocation, | 
|---|
| 1682 | # not the same ID that was "merged"! | 
|---|
| 1683 | my $parent = shift @$mlist; | 
|---|
| 1684 | $page->param(backpool => $webvar{newtype} =~ /.[dp]/); | 
|---|
| 1685 | if ($webvar{newtype} =~ /.[enr]/) { | 
|---|
| 1686 | $page->param(backleaf => 1); | 
|---|
| 1687 | $page->param(backid => $binfo->{parent_id}); | 
|---|
| 1688 | $page->param(backblock => $pinfo->{block}); | 
|---|
| 1689 | } else { | 
|---|
| 1690 | $page->param(backid => $parent->{id}); | 
|---|
| 1691 | $page->param(backblock => $parent->{block}); | 
|---|
| 1692 | } | 
|---|
| 1693 | $page->param(mergelist => $mlist); | 
|---|
| 1694 | } else { | 
|---|
| 1695 | $page->param(err => "Merge failed: $IPDB::errstr"); | 
|---|
| 1696 | } | 
|---|
| 1697 | } # doMerge() | 
|---|
| 1698 |  | 
|---|
| 1699 |  | 
|---|
| 1700 | # Delete an allocation. | 
|---|
| 1701 | sub remove { | 
|---|
| 1702 | if ($IPDBacl{$authuser} !~ /d/) { | 
|---|
| 1703 | $aclerr = 'delblock'; | 
|---|
| 1704 | return; | 
|---|
| 1705 | } | 
|---|
| 1706 |  | 
|---|
| 1707 | # Serves'em right for getting here... | 
|---|
| 1708 | if (!defined($webvar{block})) { | 
|---|
| 1709 | $page->param(err => "Can't delete a block that doesn't exist"); | 
|---|
| 1710 | return; | 
|---|
| 1711 | } | 
|---|
| 1712 |  | 
|---|
| 1713 | my $blockdata; | 
|---|
| 1714 | $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype}); | 
|---|
| 1715 |  | 
|---|
| 1716 | # Tree navigation | 
|---|
| 1717 | my $crumbs = getBreadCrumbs($ip_dbh, $blockdata->{parent_id}); | 
|---|
| 1718 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 1719 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 1720 |  | 
|---|
| 1721 | if ($blockdata->{parent_id} == 0) {   # $webvar{alloctype} eq 'mm' | 
|---|
| 1722 | $blockdata->{city} = "N/A"; | 
|---|
| 1723 | $blockdata->{custid} = "N/A"; | 
|---|
| 1724 | $blockdata->{circuitid} = "N/A"; | 
|---|
| 1725 | $blockdata->{description} = "N/A"; | 
|---|
| 1726 | $blockdata->{notes} = "N/A"; | 
|---|
| 1727 | $blockdata->{privdata} = "N/A"; | 
|---|
| 1728 | } # end cases for different alloctypes | 
|---|
| 1729 |  | 
|---|
| 1730 | $page->param(blockid => $webvar{block}); | 
|---|
| 1731 | $page->param(basetype => $webvar{basetype}); | 
|---|
| 1732 |  | 
|---|
| 1733 | $page->param(block => $blockdata->{block}); | 
|---|
| 1734 | $page->param(rdns => $blockdata->{rdns}); | 
|---|
| 1735 |  | 
|---|
| 1736 | # maybe need to apply more magic here? | 
|---|
| 1737 | # most allocations we *do* want to autodelete the forward as well as reverse;  for a handful we don't. | 
|---|
| 1738 | # -> all real blocks (nb: pool IPs need extra handling) | 
|---|
| 1739 | # -> NOC/private-IP (how to ID?) | 
|---|
| 1740 | # -> anything with a pattern matching $IPDB::domain? | 
|---|
| 1741 | if ($blockdata->{type} !~ /^.i$/) { | 
|---|
| 1742 | $page->param(autodel => 1); | 
|---|
| 1743 | } | 
|---|
| 1744 |  | 
|---|
| 1745 | $page->param(disptype => $disp_alloctypes{$blockdata->{type}}); | 
|---|
| 1746 | $page->param(city => $blockdata->{city}); | 
|---|
| 1747 | $page->param(custid => $blockdata->{custid}); | 
|---|
| 1748 | $page->param(circid => $blockdata->{circuitid}); | 
|---|
| 1749 | $page->param(desc => $blockdata->{description}); | 
|---|
| 1750 | $blockdata->{notes} = $q->escapeHTML($blockdata->{notes}); | 
|---|
| 1751 | $blockdata->{notes} =~ s/\n/<br>\n/; | 
|---|
| 1752 | $page->param(notes => $blockdata->{notes}); | 
|---|
| 1753 | $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata}); | 
|---|
| 1754 | $blockdata->{privdata} = ' ' if !$blockdata->{privdata}; | 
|---|
| 1755 | $blockdata->{privdata} =~ s/\n/<br>\n/; | 
|---|
| 1756 | $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/; | 
|---|
| 1757 | $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/); | 
|---|
| 1758 |  | 
|---|
| 1759 | } # end remove() | 
|---|
| 1760 |  | 
|---|
| 1761 |  | 
|---|
| 1762 | # Delete an allocation.  Return it to the freeblocks table;  munge | 
|---|
| 1763 | # data as necessary to keep as few records as possible in freeblocks | 
|---|
| 1764 | # to prevent weirdness when allocating blocks later. | 
|---|
| 1765 | # Remove IPs from pool listing if necessary | 
|---|
| 1766 | sub finalDelete { | 
|---|
| 1767 | if ($IPDBacl{$authuser} !~ /d/) { | 
|---|
| 1768 | $aclerr = 'delblock'; | 
|---|
| 1769 | return; | 
|---|
| 1770 | } | 
|---|
| 1771 |  | 
|---|
| 1772 | # need to retrieve block data before deleting so we can notify on that | 
|---|
| 1773 | my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{basetype}); | 
|---|
| 1774 | my $pinfo = getBlockData($ip_dbh, $blockinfo->{parent_id}, 'b'); | 
|---|
| 1775 |  | 
|---|
| 1776 | # Tree navigation | 
|---|
| 1777 | my $crumbs = getBreadCrumbs($ip_dbh, $blockinfo->{parent_id}); | 
|---|
| 1778 | my @rcrumbs = reverse (@$crumbs); | 
|---|
| 1779 | $utilbar->param(breadcrumb => \@rcrumbs); | 
|---|
| 1780 |  | 
|---|
| 1781 | my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{basetype}, $webvar{delforward}, $authuser); | 
|---|
| 1782 |  | 
|---|
| 1783 | $page->param(block => $blockinfo->{block}); | 
|---|
| 1784 | $page->param(bdisp => $q->escapeHTML($disp_alloctypes{$blockinfo->{type}})); | 
|---|
| 1785 | $page->param(delparent_id => $blockinfo->{parent_id}); | 
|---|
| 1786 | if ($pinfo) { | 
|---|
| 1787 | $page->param(delparent => $pinfo->{block}); | 
|---|
| 1788 | $page->param(pdisp => $q->escapeHTML($disp_alloctypes{$pinfo->{type}})); | 
|---|
| 1789 | } | 
|---|
| 1790 | $page->param(returnpool => ($webvar{basetype} eq 'i') ); | 
|---|
| 1791 | if ($code =~ /^WARN(POOL|MERGE)/) { | 
|---|
| 1792 | my ($pid,$pcidr) = split /,/, $msg; | 
|---|
| 1793 | my $real_pinfo = getBlockData($ip_dbh, $pid, 'b'); | 
|---|
| 1794 | $page->param(parent_id => $pid); | 
|---|
| 1795 | $page->param(parent => $pcidr); | 
|---|
| 1796 | $page->param(real_disp => $q->escapeHTML($disp_alloctypes{$real_pinfo->{type}})); | 
|---|
| 1797 | $page->param(mergeip => $code eq 'WARNPOOL'); | 
|---|
| 1798 | } | 
|---|
| 1799 | if ($code eq 'WARN') { | 
|---|
| 1800 | $msg =~ s/\n/<br>\n/g; | 
|---|
| 1801 | $page->param(genwarn => $msg); | 
|---|
| 1802 | } | 
|---|
| 1803 | if ($code eq 'OK' || $code =~ /^WARN/) { | 
|---|
| 1804 | syslog "notice", "$authuser deallocated '".$blockinfo->{type}."'-type netblock ID $webvar{block} ". | 
|---|
| 1805 | "($blockinfo->{block}), $blockinfo->{custid}, $blockinfo->{city}, desc='$blockinfo->{description}'"; | 
|---|
| 1806 | mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$blockinfo->{type}} $blockinfo->{block}", | 
|---|
| 1807 | # $webvar{block} useful?  do we care about the block ID here? | 
|---|
| 1808 | "$disp_alloctypes{$blockinfo->{type}} $blockinfo->{block} deallocated by $authuser\n". | 
|---|
| 1809 | "CustID: $blockinfo->{custid}\nCity: $blockinfo->{city}\n". | 
|---|
| 1810 | "Description: $blockinfo->{description}\n"); | 
|---|
| 1811 | } else { | 
|---|
| 1812 | $page->param(failmsg => $msg); | 
|---|
| 1813 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
| 1814 | syslog "err", "$authuser could not deallocate static IP $webvar{block} ($blockinfo->{block}): '$msg'"; | 
|---|
| 1815 | } else { | 
|---|
| 1816 | syslog "err", "$authuser could not deallocate netblock $webvar{block} ($blockinfo->{block}): '$msg'"; | 
|---|
| 1817 | $page->param(netblock => 1); | 
|---|
| 1818 | } | 
|---|
| 1819 | } | 
|---|
| 1820 |  | 
|---|
| 1821 | } # finalDelete | 
|---|
| 1822 |  | 
|---|
| 1823 | # Retrive and lightly preprocess any block notices. | 
|---|
| 1824 | sub blocknotices { | 
|---|
| 1825 | my $blockid = shift; | 
|---|
| 1826 | # retrieve any notices | 
|---|
| 1827 | my $nlist = getBlockNotices($ip_dbh, $blockid); | 
|---|
| 1828 | my @notices; | 
|---|
| 1829 | foreach (@$nlist) { | 
|---|
| 1830 | push @notices, $_->{notice} if $_->{notice}; | 
|---|
| 1831 | } | 
|---|
| 1832 | my $blockmsg = join("<br>\n", @notices); | 
|---|
| 1833 | $page->param(blockmsg => $blockmsg); | 
|---|
| 1834 | } | 
|---|