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