| [4] | 1 | #!/usr/bin/perl | 
|---|
|  | 2 | # ipdb/cgi-bin/main.cgi | 
|---|
| [8] | 3 | ### | 
|---|
|  | 4 | # SVN revision info | 
|---|
|  | 5 | # $Date: 2013-01-25 21:16:04 +0000 (Fri, 25 Jan 2013) $ | 
|---|
|  | 6 | # SVN revision $Rev: 591 $ | 
|---|
|  | 7 | # Last update by $Author: kdeugau $ | 
|---|
|  | 8 | ### | 
|---|
| [417] | 9 | # Copyright (C) 2004-2010 - Kris Deugau | 
|---|
| [4] | 10 |  | 
|---|
|  | 11 | use strict; | 
|---|
|  | 12 | use warnings; | 
|---|
|  | 13 | use CGI::Carp qw(fatalsToBrowser); | 
|---|
| [517] | 14 | use CGI::Simple; | 
|---|
|  | 15 | use HTML::Template; | 
|---|
| [4] | 16 | use DBI; | 
|---|
|  | 17 | use POSIX qw(ceil); | 
|---|
|  | 18 | use NetAddr::IP; | 
|---|
| [582] | 19 | use Frontier::Client; | 
|---|
| [4] | 20 |  | 
|---|
|  | 21 | use Sys::Syslog; | 
|---|
|  | 22 |  | 
|---|
| [417] | 23 | # don't remove!  required for GNU/FHS-ish install from tarball | 
|---|
|  | 24 | ##uselib## | 
|---|
|  | 25 |  | 
|---|
| [515] | 26 | use CustIDCK; | 
|---|
| [417] | 27 | use MyIPDB; | 
|---|
|  | 28 |  | 
|---|
| [431] | 29 | openlog "IPDB","pid","$IPDB::syslog_facility"; | 
|---|
| [4] | 30 |  | 
|---|
| [517] | 31 | ## Environment.  Collect some things, process some things, set some things... | 
|---|
|  | 32 |  | 
|---|
| [233] | 33 | # Collect the username from HTTP auth.  If undefined, we're in | 
|---|
|  | 34 | # a test environment, or called without a username. | 
|---|
| [4] | 35 | my $authuser; | 
|---|
|  | 36 | if (!defined($ENV{'REMOTE_USER'})) { | 
|---|
|  | 37 | $authuser = '__temptest'; | 
|---|
|  | 38 | } else { | 
|---|
|  | 39 | $authuser = $ENV{'REMOTE_USER'}; | 
|---|
|  | 40 | } | 
|---|
|  | 41 |  | 
|---|
| [517] | 42 | # anyone got a better name?  :P | 
|---|
|  | 43 | my $thingroot = $ENV{SCRIPT_FILENAME}; | 
|---|
|  | 44 | $thingroot =~ s|cgi-bin/main.cgi||; | 
|---|
|  | 45 |  | 
|---|
| [402] | 46 | syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}"; | 
|---|
| [4] | 47 |  | 
|---|
| [517] | 48 | ##fixme there *must* be a better order to do things in so this can go back where it was | 
|---|
|  | 49 | # CGI fiddling done here so we can declare %webvar so we can alter $webvar{action} | 
|---|
|  | 50 | # to show the right page on DB errors. | 
|---|
|  | 51 | # Set up the CGI object... | 
|---|
|  | 52 | my $q = new CGI::Simple; | 
|---|
|  | 53 | # ... and get query-string params as well as POST params if necessary | 
|---|
|  | 54 | $q->parse_query_string; | 
|---|
|  | 55 |  | 
|---|
|  | 56 | # Convenience;  saves changing all references to %webvar | 
|---|
|  | 57 | ##fixme:  tweak for handling <select multiple='y' size=3> (list with multiple selection) | 
|---|
|  | 58 | my %webvar = $q->Vars; | 
|---|
|  | 59 |  | 
|---|
| [106] | 60 | # Why not a global DB handle?  (And a global statement handle, as well...) | 
|---|
|  | 61 | # Use the connectDB function, otherwise we end up confusing ourselves | 
|---|
|  | 62 | my $ip_dbh; | 
|---|
|  | 63 | my $errstr; | 
|---|
| [142] | 64 | ($ip_dbh,$errstr) = connectDB_My; | 
|---|
| [106] | 65 | if (!$ip_dbh) { | 
|---|
| [517] | 66 | $webvar{action} = "dberr"; | 
|---|
|  | 67 | } else { | 
|---|
|  | 68 | initIPDBGlobals($ip_dbh); | 
|---|
| [106] | 69 | } | 
|---|
| [4] | 70 |  | 
|---|
| [517] | 71 | # Set up some globals | 
|---|
|  | 72 | $ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates"; | 
|---|
| [233] | 73 |  | 
|---|
| [517] | 74 | my $header = HTML::Template->new(filename => "header.tmpl"); | 
|---|
|  | 75 | my $footer = HTML::Template->new(filename => "footer.tmpl"); | 
|---|
| [233] | 76 |  | 
|---|
| [517] | 77 | $header->param(version => $IPDB::VERSION); | 
|---|
|  | 78 | $header->param(addperm => $IPDBacl{$authuser} =~ /a/); | 
|---|
|  | 79 | $header->param(webpath => $IPDB::webpath); | 
|---|
|  | 80 | print "Content-type: text/html\n\n", $header->output; | 
|---|
| [4] | 81 |  | 
|---|
|  | 82 |  | 
|---|
|  | 83 | #main() | 
|---|
| [517] | 84 | my $aclerr; | 
|---|
| [4] | 85 |  | 
|---|
|  | 86 | if(!defined($webvar{action})) { | 
|---|
| [517] | 87 | $webvar{action} = "index";    #shuts up the warnings. | 
|---|
| [4] | 88 | } | 
|---|
|  | 89 |  | 
|---|
| [517] | 90 | my $page; | 
|---|
|  | 91 | if (-e "$ENV{HTML_TEMPLATE_ROOT}/$webvar{action}.tmpl") { | 
|---|
| [553] | 92 | $page = HTML::Template->new(filename => "$webvar{action}.tmpl", loop_context_vars => 1, global_vars => 1); | 
|---|
| [517] | 93 | } else { | 
|---|
|  | 94 | $page = HTML::Template->new(filename => "dunno.tmpl"); | 
|---|
|  | 95 | } | 
|---|
|  | 96 |  | 
|---|
| [4] | 97 | if($webvar{action} eq 'index') { | 
|---|
|  | 98 | showSummary(); | 
|---|
| [233] | 99 | } elsif ($webvar{action} eq 'addmaster') { | 
|---|
|  | 100 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| [517] | 101 | $aclerr = 'addmaster'; | 
|---|
| [233] | 102 | } | 
|---|
| [582] | 103 |  | 
|---|
|  | 104 | # Retrieve the list of DNS locations if we've got a place to grab them from | 
|---|
|  | 105 | if ($IPDB::rpc_url) { | 
|---|
|  | 106 | # Make an object to represent the XML-RPC server. | 
|---|
|  | 107 | my $server = Frontier::Client->new(url => $IPDB::rpc_url, debug => 0); | 
|---|
|  | 108 | my $result; | 
|---|
|  | 109 |  | 
|---|
|  | 110 | my %rpcargs = ( | 
|---|
|  | 111 | rpcuser => $authuser, | 
|---|
|  | 112 | rpcsystem => 'ipdb', | 
|---|
|  | 113 | group => 1,     # bleh | 
|---|
|  | 114 | defloc => '', | 
|---|
|  | 115 | ); | 
|---|
|  | 116 | $result = $server->call('dnsdb.getLocDropdown', %rpcargs); | 
|---|
|  | 117 | $page->param(loclist => $result); | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
| [4] | 120 | } elsif ($webvar{action} eq 'newmaster') { | 
|---|
|  | 121 |  | 
|---|
| [233] | 122 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| [517] | 123 | $aclerr = 'addmaster'; | 
|---|
| [233] | 124 | } else { | 
|---|
|  | 125 | my $cidr = new NetAddr::IP $webvar{cidr}; | 
|---|
| [517] | 126 | $page->param(cidr => "$cidr"); | 
|---|
| [4] | 127 |  | 
|---|
| [582] | 128 | my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr}, (vrf => $webvar{vrf}, rdns => $webvar{rdns}, | 
|---|
|  | 129 | rwhois => $webvar{rwhois}, defloc => $webvar{loc}, user => $authuser) ); | 
|---|
| [4] | 130 |  | 
|---|
| [371] | 131 | if ($code eq 'FAIL') { | 
|---|
| [320] | 132 | syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'"; | 
|---|
| [517] | 133 | $page->param(err => $msg); | 
|---|
| [233] | 134 | } else { | 
|---|
| [582] | 135 | if ($code eq 'WARN') { | 
|---|
|  | 136 | $msg =~ s/\n\n/<br>\n/g; | 
|---|
|  | 137 | $msg =~ s/:\n/:<br>\n/g; | 
|---|
|  | 138 | $page->param(warn => $msg); | 
|---|
|  | 139 | } | 
|---|
| [233] | 140 | syslog "info", "$authuser added master block $webvar{cidr}"; | 
|---|
|  | 141 | } | 
|---|
| [4] | 142 |  | 
|---|
| [233] | 143 | } # ACL check | 
|---|
|  | 144 |  | 
|---|
| [4] | 145 | } # end add new master | 
|---|
|  | 146 |  | 
|---|
| [567] | 147 | elsif ($webvar{action} eq 'showsubs') { | 
|---|
|  | 148 | showSubs(); | 
|---|
|  | 149 | } | 
|---|
|  | 150 |  | 
|---|
| [4] | 151 | elsif($webvar{action} eq 'listpool') { | 
|---|
| [528] | 152 | showPool(); | 
|---|
| [4] | 153 | } | 
|---|
|  | 154 |  | 
|---|
|  | 155 | # Not modified or added;  just shuffled | 
|---|
|  | 156 | elsif($webvar{action} eq 'assign') { | 
|---|
|  | 157 | assignBlock(); | 
|---|
|  | 158 | } | 
|---|
|  | 159 | elsif($webvar{action} eq 'confirm') { | 
|---|
|  | 160 | confirmAssign(); | 
|---|
|  | 161 | } | 
|---|
|  | 162 | elsif($webvar{action} eq 'insert') { | 
|---|
|  | 163 | insertAssign(); | 
|---|
|  | 164 | } | 
|---|
|  | 165 | elsif($webvar{action} eq 'edit') { | 
|---|
|  | 166 | edit(); | 
|---|
|  | 167 | } | 
|---|
|  | 168 | elsif($webvar{action} eq 'update') { | 
|---|
|  | 169 | update(); | 
|---|
|  | 170 | } | 
|---|
|  | 171 | elsif($webvar{action} eq 'delete') { | 
|---|
|  | 172 | remove(); | 
|---|
|  | 173 | } | 
|---|
|  | 174 | elsif($webvar{action} eq 'finaldelete') { | 
|---|
|  | 175 | finalDelete(); | 
|---|
|  | 176 | } | 
|---|
| [397] | 177 | elsif ($webvar{action} eq 'nodesearch') { | 
|---|
| [519] | 178 | my $nodelist = getNodeList($ip_dbh); | 
|---|
|  | 179 | $page->param(nodelist => $nodelist); | 
|---|
| [517] | 180 | } | 
|---|
| [397] | 181 |  | 
|---|
| [517] | 182 | # DB failure.  Can't do much here, really. | 
|---|
|  | 183 | elsif ($webvar{action} eq 'dberr') { | 
|---|
|  | 184 | $page->param(errmsg => $errstr); | 
|---|
| [397] | 185 | } | 
|---|
|  | 186 |  | 
|---|
| [517] | 187 | # Default is an error.  It shouldn't be possible to get here unless you're | 
|---|
|  | 188 | # randomly feeding in values for webvar{action}. | 
|---|
| [4] | 189 | else { | 
|---|
|  | 190 | my $rnd = rand 500; | 
|---|
|  | 191 | my $boing = sprintf("%.2f", rand 500); | 
|---|
| [517] | 192 | my @excuses = ( | 
|---|
|  | 193 | "Aether cloudy.  Ask again later about $webvar{action}.", | 
|---|
|  | 194 | "The gods are unhappy with your sacrificial $webvar{action}.", | 
|---|
|  | 195 | "Because one of $webvar{action}'s legs are both the same", | 
|---|
|  | 196 | "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!", | 
|---|
|  | 197 | "Hey, man, you've had your free $webvar{action}.  Next one's gonna...  <i>cost</i>....", | 
|---|
|  | 198 | "I ain't done $webvar{action}", | 
|---|
|  | 199 | "Oooo, look!  A flying $webvar{action}!", | 
|---|
|  | 200 | "$webvar{action} too evil, avoiding.", | 
|---|
|  | 201 | "Rocks fall, $webvar{action} dies.", | 
|---|
|  | 202 | "Bit bucket must be emptied before I can $webvar{action}..." | 
|---|
|  | 203 | ); | 
|---|
|  | 204 | $page->param(dunno => $excuses[$rnd/50.0]); | 
|---|
| [4] | 205 | } | 
|---|
| [111] | 206 | ## Finally! Done with that NASTY "case" emulation! | 
|---|
| [4] | 207 |  | 
|---|
|  | 208 |  | 
|---|
| [517] | 209 | # Switch to a different template if we've tripped on an ACL error. | 
|---|
|  | 210 | # Note that this should only be exercised in development, when | 
|---|
|  | 211 | # deeplinked, or when being attacked;  normal ACL handling should | 
|---|
|  | 212 | # remove the links a user is not allowed to click on. | 
|---|
|  | 213 | if ($aclerr) { | 
|---|
|  | 214 | $page = HTML::Template->new(filename => "aclerror.tmpl"); | 
|---|
|  | 215 | $page->param(ipdbfunc => $aclmsg{$aclerr}); | 
|---|
|  | 216 | } | 
|---|
| [4] | 217 |  | 
|---|
| [106] | 218 | # Clean up IPDB globals, DB handle, etc. | 
|---|
| [111] | 219 | finish($ip_dbh); | 
|---|
| [199] | 220 |  | 
|---|
| [517] | 221 | ## Do all our printing here so we can generate errors and stick them into the slots in the templates. | 
|---|
| [199] | 222 |  | 
|---|
| [517] | 223 | # can't do this yet, too many blowups | 
|---|
|  | 224 | #print "Content-type: text/html\n\n", $header->output; | 
|---|
|  | 225 | $page->param(webpath => $IPDB::webpath); | 
|---|
|  | 226 | print $page->output; | 
|---|
|  | 227 |  | 
|---|
|  | 228 | # include the admin tools link in the output? | 
|---|
|  | 229 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/)); | 
|---|
|  | 230 | $footer->param(webpath => $IPDB::webpath); | 
|---|
|  | 231 | print $footer->output; | 
|---|
|  | 232 |  | 
|---|
| [106] | 233 | # Just in case something waaaayyy down isn't in place | 
|---|
|  | 234 | # properly... we exit explicitly. | 
|---|
| [517] | 235 | exit 0; | 
|---|
| [4] | 236 |  | 
|---|
|  | 237 |  | 
|---|
|  | 238 | # Initial display:  Show master blocks with total allocated subnets, total free subnets | 
|---|
| [118] | 239 | sub showSummary { | 
|---|
| [523] | 240 | my $masterlist = listSummary($ip_dbh); | 
|---|
|  | 241 | $page->param(masterlist => $masterlist); | 
|---|
| [106] | 242 |  | 
|---|
| [517] | 243 | $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) ); | 
|---|
| [4] | 244 | } # showSummary | 
|---|
|  | 245 |  | 
|---|
|  | 246 |  | 
|---|
| [566] | 247 | # Display blocks immediately within a given parent | 
|---|
|  | 248 | sub showSubs { | 
|---|
|  | 249 | $page->param(block => $webvar{block}); | 
|---|
|  | 250 | $page->param(mayadd => ($IPDBacl{$authuser} =~ /a/)); | 
|---|
|  | 251 | $page->param(maydel => ($IPDBacl{$authuser} =~ /d/)); | 
|---|
| [4] | 252 |  | 
|---|
| [566] | 253 | my $sublist = listSubs($ip_dbh, block => $webvar{block}, rdepth => $webvar{rdepth}); | 
|---|
|  | 254 | $page->param(deldepth => $webvar{rdepth} - 1); | 
|---|
|  | 255 | $page->param(rdepth => $webvar{rdepth}); | 
|---|
|  | 256 | $page->param(subdepth => $webvar{rdepth} + 1); | 
|---|
|  | 257 | $page->param(sublist => $sublist); | 
|---|
| [4] | 258 |  | 
|---|
| [566] | 259 | my $flist = listFree($ip_dbh, master => $webvar{block}, rdepth => $webvar{rdepth}); | 
|---|
|  | 260 | $page->param(freelist => $flist); | 
|---|
|  | 261 | } # showSubs | 
|---|
| [4] | 262 |  | 
|---|
|  | 263 |  | 
|---|
|  | 264 | # List the IPs used in a pool | 
|---|
| [528] | 265 | sub showPool { | 
|---|
| [4] | 266 |  | 
|---|
|  | 267 | my $cidr = new NetAddr::IP $webvar{pool}; | 
|---|
|  | 268 |  | 
|---|
| [517] | 269 | $page->param(block => $webvar{pool}); | 
|---|
|  | 270 | $page->param(netip => $cidr->addr); | 
|---|
|  | 271 | $cidr++; | 
|---|
|  | 272 | $page->param(gate => $cidr->addr); | 
|---|
|  | 273 | $cidr--;  $cidr--; | 
|---|
|  | 274 | $page->param(bcast => $cidr->addr); | 
|---|
|  | 275 | $page->param(mask => $cidr->mask); | 
|---|
| [157] | 276 |  | 
|---|
| [4] | 277 | # Snag pool info for heading | 
|---|
| [570] | 278 | my $poolinfo = getBlockData($ip_dbh, $webvar{pool}, $webvar{rdepth}); | 
|---|
| [4] | 279 |  | 
|---|
| [528] | 280 | $page->param(disptype => $disp_alloctypes{$poolinfo->{type}}); | 
|---|
|  | 281 | $page->param(city => $poolinfo->{city}); | 
|---|
| [517] | 282 |  | 
|---|
| [157] | 283 | # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy | 
|---|
| [528] | 284 | $page->param(realblock => $poolinfo->{type} =~ /^.d$/); | 
|---|
| [4] | 285 |  | 
|---|
|  | 286 | # probably have to add an "edit IP allocation" link here somewhere. | 
|---|
|  | 287 |  | 
|---|
| [528] | 288 | my $plist = listPool($ip_dbh, $webvar{pool}); | 
|---|
|  | 289 | # technically slightly more efficient to check the ACL in an if () once outside the foreach | 
|---|
|  | 290 | foreach (@{$plist}) { | 
|---|
|  | 291 | $$_{maydel} = $IPDBacl{$authuser} =~ /d/; | 
|---|
| [4] | 292 | } | 
|---|
| [528] | 293 | $page->param(poolips => $plist); | 
|---|
|  | 294 | } # end showPool | 
|---|
| [4] | 295 |  | 
|---|
|  | 296 |  | 
|---|
| [106] | 297 | # Show "Add new allocation" page.  Note that the actual page may | 
|---|
|  | 298 | # be one of two templates, and the lists come from the database. | 
|---|
| [4] | 299 | sub assignBlock { | 
|---|
|  | 300 |  | 
|---|
| [233] | 301 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| [517] | 302 | $aclerr = 'addblock'; | 
|---|
| [233] | 303 | return; | 
|---|
|  | 304 | } | 
|---|
|  | 305 |  | 
|---|
| [517] | 306 | # hack pthbttt eww | 
|---|
|  | 307 | $webvar{block} = '' if !$webvar{block}; | 
|---|
| [21] | 308 |  | 
|---|
| [517] | 309 | # hmm.  TMPL_IF block and TMPL_ELSE block on these instead? | 
|---|
|  | 310 | $page->param(rowa => 'row'.($webvar{block} eq '' ? 1 : 0)); | 
|---|
|  | 311 | $page->param(rowb => 'row'.($webvar{block} eq '' ? 0 : 1)); | 
|---|
| [575] | 312 | $page->param(allocfrom => $webvar{block});    # fb-assign flag, if block is set, we're in fb-assign | 
|---|
| [517] | 313 |  | 
|---|
| [21] | 314 | if ($webvar{block} ne '') { | 
|---|
| [575] | 315 |  | 
|---|
|  | 316 | # Common case, according to reported usage.  Block to assign is specified. | 
|---|
| [21] | 317 | my $block = new NetAddr::IP $webvar{block}; | 
|---|
| [575] | 318 | $page->param(rdepth => $webvar{rdepth}); | 
|---|
| [187] | 319 |  | 
|---|
| [585] | 320 | my $rdns = getBlockRDNS($ip_dbh, $webvar{block}, $webvar{rdepth}, vrf => $webvar{vrf}, user => $authuser); | 
|---|
|  | 321 | $page->param(rdns => $rdns) if $rdns; | 
|---|
|  | 322 |  | 
|---|
| [575] | 323 | $webvar{fbtype} = '' if !$webvar{fbtype}; | 
|---|
|  | 324 | if ($webvar{fbtype} eq 'i') { | 
|---|
|  | 325 | my $ipinfo = getBlockData($ip_dbh, $block); | 
|---|
|  | 326 | $page->param( | 
|---|
|  | 327 | fbip => 1, | 
|---|
|  | 328 | block => $block, | 
|---|
|  | 329 | fbdisptype => $list_alloctypes{$ipinfo->{type}}, | 
|---|
|  | 330 | type => $ipinfo->{type}, | 
|---|
|  | 331 | allocfrom => $ipinfo->{pool}, | 
|---|
|  | 332 | ); | 
|---|
| [187] | 333 | } else { | 
|---|
| [529] | 334 | # get "primary" alloctypes, since these are all that can correctly be assigned if we're in this branch | 
|---|
| [575] | 335 | my $tlist = getTypeList($ip_dbh, 'n'); | 
|---|
| [529] | 336 | $tlist->[0]->{sel} = 1; | 
|---|
| [575] | 337 | $page->param(typelist => $tlist, block => $block); | 
|---|
| [106] | 338 | } | 
|---|
| [575] | 339 |  | 
|---|
| [21] | 340 | } else { | 
|---|
| [575] | 341 |  | 
|---|
|  | 342 | # Uncommon case, according to reported usage.  Block to assign needs to be found based on criteria. | 
|---|
| [541] | 343 | my $mlist = getMasterList($ip_dbh, 'c'); | 
|---|
|  | 344 | $page->param(masterlist => $mlist); | 
|---|
| [517] | 345 |  | 
|---|
|  | 346 | my @pops; | 
|---|
| [92] | 347 | foreach my $pop (@poplist) { | 
|---|
| [517] | 348 | my %row = (pop => $pop); | 
|---|
|  | 349 | push (@pops, \%row); | 
|---|
| [92] | 350 | } | 
|---|
| [517] | 351 | $page->param(pops => \@pops); | 
|---|
|  | 352 |  | 
|---|
| [529] | 353 | # get all standard alloctypes | 
|---|
|  | 354 | my $tlist = getTypeList($ip_dbh, 'a'); | 
|---|
|  | 355 | $tlist->[0]->{sel} = 1; | 
|---|
|  | 356 | $page->param(typelist => $tlist); | 
|---|
| [21] | 357 | } | 
|---|
| [517] | 358 |  | 
|---|
|  | 359 | my @cities; | 
|---|
| [92] | 360 | foreach my $city (@citylist) { | 
|---|
| [517] | 361 | my %row = (city => $city); | 
|---|
|  | 362 | push (@cities, \%row); | 
|---|
| [92] | 363 | } | 
|---|
| [517] | 364 | $page->param(citylist => \@cities); | 
|---|
| [4] | 365 |  | 
|---|
| [397] | 366 | ## node hack | 
|---|
| [530] | 367 | my $nlist = getNodeList($ip_dbh); | 
|---|
|  | 368 | $page->param(nodelist => $nlist); | 
|---|
| [397] | 369 | ## end node hack | 
|---|
|  | 370 |  | 
|---|
| [517] | 371 | $page->param(privdata => $IPDBacl{$authuser} =~ /s/); | 
|---|
| [284] | 372 |  | 
|---|
| [4] | 373 | } # assignBlock | 
|---|
|  | 374 |  | 
|---|
|  | 375 |  | 
|---|
|  | 376 | # Take info on requested IP assignment and see what we can provide. | 
|---|
|  | 377 | sub confirmAssign { | 
|---|
| [233] | 378 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| [517] | 379 | $aclerr = 'addblock'; | 
|---|
| [233] | 380 | return; | 
|---|
|  | 381 | } | 
|---|
| [4] | 382 |  | 
|---|
|  | 383 | my $cidr; | 
|---|
|  | 384 | my $alloc_from; | 
|---|
|  | 385 |  | 
|---|
|  | 386 | # Going to manually validate some items. | 
|---|
|  | 387 | # custid and city are automagic. | 
|---|
| [111] | 388 | return if !validateInput(); | 
|---|
| [4] | 389 |  | 
|---|
|  | 390 | # Several different cases here. | 
|---|
|  | 391 | # Static IP vs netblock | 
|---|
|  | 392 | #  + Different flavours of static IP | 
|---|
|  | 393 | #  + Different flavours of netblock | 
|---|
|  | 394 |  | 
|---|
| [575] | 395 | if ($webvar{alloctype} =~ /^.i$/ && $webvar{fbassign} ne 'y') { | 
|---|
| [532] | 396 | my $plist = getPoolSelect($ip_dbh, $webvar{alloctype}, $webvar{pop}); | 
|---|
| [517] | 397 | $page->param(staticip => 1); | 
|---|
| [532] | 398 | $page->param(poollist => $plist) if $plist; | 
|---|
| [4] | 399 | $cidr = "Single static IP"; | 
|---|
| [517] | 400 | ##fixme:  need to handle "no available pools" | 
|---|
| [4] | 401 |  | 
|---|
|  | 402 | } else { # end show pool options | 
|---|
| [21] | 403 |  | 
|---|
| [533] | 404 | if ($webvar{fbassign} && $webvar{fbassign} eq 'y') { | 
|---|
| [21] | 405 | $cidr = new NetAddr::IP $webvar{block}; | 
|---|
| [575] | 406 | $alloc_from = new NetAddr::IP $webvar{allocfrom}; | 
|---|
| [21] | 407 | $webvar{maskbits} = $cidr->masklen; | 
|---|
|  | 408 | } else { # done with direct freeblocks assignment | 
|---|
|  | 409 |  | 
|---|
|  | 410 | if (!$webvar{maskbits}) { | 
|---|
| [517] | 411 | $page->param(err => "Please specify a CIDR mask length."); | 
|---|
| [111] | 412 | return; | 
|---|
| [21] | 413 | } | 
|---|
| [533] | 414 |  | 
|---|
|  | 415 | ##fixme ick, ew, bleh.  gotta handle the failure message generation better.  push it into findAllocateFrom()? | 
|---|
|  | 416 | my $failmsg = "No suitable free block found.<br>\n"; | 
|---|
| [187] | 417 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| [533] | 418 | $failmsg .= "We do not have a free routeable block of that size.<br>\n". | 
|---|
|  | 419 | "You will have to either route a set of smaller netblocks or a single smaller netblock."; | 
|---|
| [4] | 420 | } else { | 
|---|
| [214] | 421 | if ($webvar{alloctype} =~ /^.[pc]$/) { | 
|---|
| [533] | 422 | $failmsg .= "You will have to route another superblock from one of the<br>\n". | 
|---|
|  | 423 | "master blocks or chose a smaller block size for the pool."; | 
|---|
| [21] | 424 | } else { | 
|---|
| [517] | 425 | if (!$webvar{pop}) { | 
|---|
|  | 426 | $page->param(err => 'Please select a POP to route the block from/through.'); | 
|---|
|  | 427 | return; | 
|---|
|  | 428 | } | 
|---|
| [533] | 429 | $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n". | 
|---|
|  | 430 | "from one of the master blocks or chose a smaller blocksize."; | 
|---|
| [21] | 431 | } | 
|---|
| [4] | 432 | } | 
|---|
| [533] | 433 |  | 
|---|
| [575] | 434 | ## fixme:  add rdepth? | 
|---|
|  | 435 | ($cidr,$webvar{rdepth}) = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype}, $webvar{city}, | 
|---|
|  | 436 | $webvar{pop}, (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) ); | 
|---|
| [533] | 437 | if (!$cidr) { | 
|---|
| [517] | 438 | $page->param(err => $failmsg); | 
|---|
| [111] | 439 | return; | 
|---|
| [21] | 440 | } | 
|---|
| [533] | 441 | $cidr = new NetAddr::IP $cidr; | 
|---|
| [575] | 442 |  | 
|---|
|  | 443 | # this chunk now specific to "guided" allocation;  freeblock-select can now slice-n-dice on its own. | 
|---|
|  | 444 | $alloc_from = "$cidr"; | 
|---|
|  | 445 | # If the block to be allocated is smaller than the one we found, | 
|---|
|  | 446 | # figure out the "real" block to be allocated. | 
|---|
|  | 447 | if ($cidr->masklen() ne $webvar{maskbits}) { | 
|---|
|  | 448 | my $maskbits = $cidr->masklen(); | 
|---|
|  | 449 | my @subblocks; | 
|---|
|  | 450 | while ($maskbits++ < $webvar{maskbits}) { | 
|---|
|  | 451 | @subblocks = $cidr->split($maskbits); | 
|---|
|  | 452 | } | 
|---|
|  | 453 | $cidr = $subblocks[0]; | 
|---|
|  | 454 | } | 
|---|
| [21] | 455 | } # check for freeblocks assignment or IPDB-controlled assignment | 
|---|
| [4] | 456 |  | 
|---|
| [157] | 457 | } # if ($webvar{alloctype} =~ /^.i$/) | 
|---|
| [4] | 458 |  | 
|---|
| [397] | 459 | ## node hack | 
|---|
|  | 460 | if ($webvar{node} && $webvar{node} ne '-') { | 
|---|
| [530] | 461 | my $nodename = getNodeName($ip_dbh, $webvar{node}); | 
|---|
| [517] | 462 | $page->param(nodename => $nodename); | 
|---|
|  | 463 | $page->param(nodeid => $webvar{node}); | 
|---|
| [397] | 464 | } | 
|---|
|  | 465 | ## end node hack | 
|---|
|  | 466 |  | 
|---|
| [4] | 467 | # Stick in the allocation data | 
|---|
| [517] | 468 | $page->param(alloc_type => $webvar{alloctype}); | 
|---|
|  | 469 | $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}})); | 
|---|
|  | 470 | $page->param(alloc_from => $alloc_from); | 
|---|
| [575] | 471 | $page->param(rdepth => $webvar{rdepth}); | 
|---|
| [517] | 472 | $page->param(cidr => $cidr); | 
|---|
| [585] | 473 | $page->param(rdns => $webvar{rdns}); | 
|---|
| [517] | 474 | $page->param(city => $q->escapeHTML($webvar{city})); | 
|---|
|  | 475 | $page->param(custid => $webvar{custid}); | 
|---|
|  | 476 | $page->param(circid => $q->escapeHTML($webvar{circid})); | 
|---|
|  | 477 | $page->param(desc => $q->escapeHTML($webvar{desc})); | 
|---|
| [4] | 478 |  | 
|---|
| [517] | 479 | ##fixme: find a way to have the displayed copy have <br> substitutions | 
|---|
|  | 480 | # for newlines, and the <input> value have either encoded or bare newlines. | 
|---|
|  | 481 | # Also applies to privdata. | 
|---|
|  | 482 | $page->param(notes => $q->escapeHTML($webvar{notes},'y')); | 
|---|
|  | 483 |  | 
|---|
| [284] | 484 | # Check to see if user is allowed to do anything with sensitive data | 
|---|
|  | 485 | my $privdata = ''; | 
|---|
| [517] | 486 | $page->param(privdata => $q->escapeHTML($webvar{privdata},'y')) | 
|---|
|  | 487 | if $IPDBacl{$authuser} =~ /s/; | 
|---|
|  | 488 |  | 
|---|
|  | 489 | # Yay!  This now has it's very own little home. | 
|---|
|  | 490 | $page->param(billinguser => $webvar{userid}) | 
|---|
| [299] | 491 | if $webvar{userid}; | 
|---|
| [284] | 492 |  | 
|---|
| [517] | 493 | ##fixme:  this is only needed iff confirm.tmpl and | 
|---|
|  | 494 | # confirmRemove.tmpl are merged (quite possible, just | 
|---|
|  | 495 | # a little tedious) | 
|---|
|  | 496 | $page->param(action => "insert"); | 
|---|
| [284] | 497 |  | 
|---|
| [4] | 498 | } # end confirmAssign | 
|---|
|  | 499 |  | 
|---|
|  | 500 |  | 
|---|
|  | 501 | # Do the work of actually inserting a block in the database. | 
|---|
|  | 502 | sub insertAssign { | 
|---|
| [233] | 503 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| [517] | 504 | $aclerr = 'addblock'; | 
|---|
| [233] | 505 | return; | 
|---|
|  | 506 | } | 
|---|
| [4] | 507 | # Some things are done more than once. | 
|---|
| [111] | 508 | return if !validateInput(); | 
|---|
| [4] | 509 |  | 
|---|
| [284] | 510 | if (!defined($webvar{privdata})) { | 
|---|
|  | 511 | $webvar{privdata} = ''; | 
|---|
|  | 512 | } | 
|---|
| [575] | 513 |  | 
|---|
|  | 514 | # split up some linked data for static IPs via guided allocation.  needed for breadcrumbs lite. | 
|---|
| [584] | 515 | ($webvar{alloc_from},$webvar{rdepth}) = split /,/, $webvar{alloc_from} if $webvar{alloc_from} =~ /,/; | 
|---|
| [575] | 516 |  | 
|---|
| [106] | 517 | # $code is "success" vs "failure", $msg contains OK for a | 
|---|
|  | 518 | # successful netblock allocation, the IP allocated for static | 
|---|
|  | 519 | # IP, or the error message if an error occurred. | 
|---|
| [517] | 520 |  | 
|---|
| [575] | 521 | my ($code,$msg) = allocateBlock($ip_dbh, cidr => $webvar{fullcidr}, alloc_from => $webvar{alloc_from}, | 
|---|
|  | 522 | rdepth => $webvar{rdepth}, custid => $webvar{custid}, type => $webvar{alloctype}, city => $webvar{city}, | 
|---|
|  | 523 | desc => $webvar{desc}, notes => $webvar{notes}, circid => $webvar{circid}, | 
|---|
| [585] | 524 | privdata => $webvar{privdata}, nodeid => $webvar{node}, rdns => $webvar{rdns}, user => $authuser); | 
|---|
| [4] | 525 |  | 
|---|
| [111] | 526 | if ($code eq 'OK') { | 
|---|
| [106] | 527 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
| [300] | 528 | $msg =~ s|/32||; | 
|---|
| [517] | 529 | $page->param(staticip => $msg); | 
|---|
|  | 530 | $page->param(custid => $webvar{custid}); | 
|---|
| [575] | 531 | $page->param(parent => $webvar{alloc_from}, rdepth => $webvar{rdepth}-1); | 
|---|
| [517] | 532 | $page->param(billinguser => $webvar{billinguser}); | 
|---|
| [416] | 533 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation", | 
|---|
|  | 534 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n". | 
|---|
|  | 535 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n"); | 
|---|
| [106] | 536 | } else { | 
|---|
| [301] | 537 | my $netblock = new NetAddr::IP $webvar{fullcidr}; | 
|---|
| [517] | 538 | $page->param(fullcidr => $webvar{fullcidr}); | 
|---|
|  | 539 | $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}}); | 
|---|
|  | 540 | $page->param(custid => $webvar{custid}); | 
|---|
| [575] | 541 | # breadcrumbs lite!  provide at least a link to the parent of the block we just allocated. | 
|---|
|  | 542 | my $binfo = getBlockData($ip_dbh, $webvar{fullcidr}, $webvar{rdepth}); | 
|---|
|  | 543 | $page->param(parent => $binfo->{parent}, rdepth => $binfo->{rdepth}); | 
|---|
| [517] | 544 | if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) { | 
|---|
|  | 545 | $page->param(billinguser => $webvar{billinguser}); | 
|---|
|  | 546 | $page->param(custid => $webvar{custid}); | 
|---|
|  | 547 | $page->param(netaddr => $netblock->addr); | 
|---|
|  | 548 | $page->param(masklen => $netblock->masklen); | 
|---|
|  | 549 | } | 
|---|
| [416] | 550 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation", | 
|---|
|  | 551 | "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n". | 
|---|
|  | 552 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n"); | 
|---|
| [4] | 553 | } | 
|---|
| [106] | 554 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ". | 
|---|
| [256] | 555 | "'$webvar{alloctype}' ($msg)"; | 
|---|
| [111] | 556 | } else { | 
|---|
|  | 557 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ". | 
|---|
|  | 558 | "'$webvar{alloctype}' by $authuser failed: '$msg'"; | 
|---|
| [517] | 559 | $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'". | 
|---|
| [157] | 560 | " failed:<br>\n$msg\n"); | 
|---|
| [106] | 561 | } | 
|---|
| [4] | 562 |  | 
|---|
|  | 563 | } # end insertAssign() | 
|---|
|  | 564 |  | 
|---|
|  | 565 |  | 
|---|
|  | 566 | # Does some basic checks on common input data to make sure nothing | 
|---|
|  | 567 | # *really* weird gets in to the database through this script. | 
|---|
|  | 568 | # Does NOT do complete input validation!!! | 
|---|
|  | 569 | sub validateInput { | 
|---|
|  | 570 | if ($webvar{city} eq '-') { | 
|---|
| [517] | 571 | $page->param(err => 'Please choose a city'); | 
|---|
| [111] | 572 | return; | 
|---|
| [4] | 573 | } | 
|---|
| [138] | 574 |  | 
|---|
|  | 575 | # Alloctype check. | 
|---|
| [4] | 576 | chomp $webvar{alloctype}; | 
|---|
| [138] | 577 | if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) { | 
|---|
|  | 578 | # Danger! Danger!  alloctype should ALWAYS be set by a dropdown.  Anyone | 
|---|
|  | 579 | # managing to call things in such a way as to cause this deserves a cryptic error. | 
|---|
| [517] | 580 | $page->param(err => 'Invalid alloctype'); | 
|---|
| [138] | 581 | return; | 
|---|
|  | 582 | } | 
|---|
|  | 583 |  | 
|---|
|  | 584 | # CustID check | 
|---|
| [4] | 585 | # We have different handling for customer allocations and "internal" or "our" allocations | 
|---|
| [214] | 586 | if ($def_custids{$webvar{alloctype}} eq '') { | 
|---|
| [4] | 587 | if (!$webvar{custid}) { | 
|---|
| [517] | 588 | $page->param(err => 'Please enter a customer ID.'); | 
|---|
| [111] | 589 | return; | 
|---|
| [4] | 590 | } | 
|---|
| [546] | 591 | # Crosscheck with billing. | 
|---|
|  | 592 | my $status = CustIDCK->custid_exist($webvar{custid}); | 
|---|
|  | 593 | if ($CustIDCK::Error) { | 
|---|
|  | 594 | $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg); | 
|---|
|  | 595 | return; | 
|---|
| [4] | 596 | } | 
|---|
| [546] | 597 | if (!$status) { | 
|---|
|  | 598 | $page->param(err => "Customer ID not valid.  Make sure the Customer ID ". | 
|---|
|  | 599 | "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ". | 
|---|
|  | 600 | "non-customer assignments."); | 
|---|
|  | 601 | return; | 
|---|
|  | 602 | } | 
|---|
| [400] | 603 | #    print "<!-- [ In validateInput().  Insert customer ID cross-check here. ] -->\n"; | 
|---|
| [138] | 604 | } else { | 
|---|
| [167] | 605 | # New!  Improved!  And now Loaded From The Database!! | 
|---|
| [320] | 606 | if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) { | 
|---|
|  | 607 | $webvar{custid} = $def_custids{$webvar{alloctype}}; | 
|---|
|  | 608 | } | 
|---|
| [4] | 609 | } | 
|---|
| [111] | 610 |  | 
|---|
| [571] | 611 | ## hmmm....  is this even useful? | 
|---|
|  | 612 | if (0) { | 
|---|
| [111] | 613 | # Check POP location | 
|---|
|  | 614 | my $flag; | 
|---|
| [187] | 615 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| [111] | 616 | $flag = 'for a routed netblock'; | 
|---|
|  | 617 | foreach (@poplist) { | 
|---|
|  | 618 | if (/^$webvar{city}$/) { | 
|---|
|  | 619 | $flag = 'n'; | 
|---|
|  | 620 | last; | 
|---|
|  | 621 | } | 
|---|
|  | 622 | } | 
|---|
|  | 623 | } else { | 
|---|
|  | 624 | $flag = 'n'; | 
|---|
| [442] | 625 | ##fixme:  hook to force-set POP or city on certain alloctypes | 
|---|
|  | 626 | # if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; } | 
|---|
| [536] | 627 | if ($webvar{pop} && $webvar{pop} =~ /^-$/) { | 
|---|
| [111] | 628 | $flag = 'to route the block from/through'; | 
|---|
|  | 629 | } | 
|---|
|  | 630 | } | 
|---|
| [517] | 631 |  | 
|---|
|  | 632 | # if the alloctype has a restricted city/POP list as determined above, | 
|---|
|  | 633 | # and the reqested city/POP does not match that list, complain | 
|---|
| [111] | 634 | if ($flag ne 'n') { | 
|---|
| [517] | 635 | $page->param(err => "Please choose a valid POP location $flag.  Valid ". | 
|---|
| [111] | 636 | "POP locations are currently:<br>\n".join (" - ", @poplist)); | 
|---|
|  | 637 | return; | 
|---|
|  | 638 | } | 
|---|
| [571] | 639 | } | 
|---|
| [111] | 640 |  | 
|---|
|  | 641 | return 'OK'; | 
|---|
| [4] | 642 | } # end validateInput | 
|---|
|  | 643 |  | 
|---|
|  | 644 |  | 
|---|
|  | 645 | # Displays details of a specific allocation in a form | 
|---|
|  | 646 | # Allows update/delete | 
|---|
|  | 647 | # action=edit | 
|---|
|  | 648 | sub edit { | 
|---|
|  | 649 |  | 
|---|
| [534] | 650 | # snag block info from db | 
|---|
| [576] | 651 | my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{rdepth}); | 
|---|
| [4] | 652 |  | 
|---|
| [534] | 653 | # Clean up extra whitespace on alloc type.  Mainly a legacy-data cleanup. | 
|---|
|  | 654 | $blockinfo->{type} =~ s/\s//; | 
|---|
| [4] | 655 |  | 
|---|
| [586] | 656 | # Get rDNS info;  duplicates a bit of getBlockData but also does the RPC call if possible | 
|---|
|  | 657 | $blockinfo->{rdns} = getBlockRDNS($ip_dbh, $webvar{block}, $webvar{rdepth}, user => $authuser); | 
|---|
|  | 658 |  | 
|---|
| [517] | 659 | $page->param(block => $webvar{block}); | 
|---|
| [586] | 660 | $page->param(rdns     => $blockinfo->{rdns}); | 
|---|
| [576] | 661 | $page->param(rdepth   => $blockinfo->{rdepth}); | 
|---|
| [4] | 662 |  | 
|---|
| [534] | 663 | $page->param(custid   => $blockinfo->{custid}); | 
|---|
|  | 664 | $page->param(city     => $blockinfo->{city}); | 
|---|
|  | 665 | $page->param(circid   => $blockinfo->{circuitid}); | 
|---|
|  | 666 | $page->param(desc     => $blockinfo->{description}); | 
|---|
|  | 667 | $page->param(notes    => $blockinfo->{notes}); | 
|---|
| [4] | 668 |  | 
|---|
| [187] | 669 | ##fixme The check here should be built from the database | 
|---|
| [517] | 670 | # Need to expand to support pool types too | 
|---|
| [534] | 671 | if ($blockinfo->{type} =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) { | 
|---|
| [517] | 672 | $page->param(changetype => 1); | 
|---|
|  | 673 | $page->param(alloctype => [ | 
|---|
| [534] | 674 | { selme => ($blockinfo->{type} eq 'me'), type => "me", disptype => "Dialup netblock" }, | 
|---|
|  | 675 | { selme => ($blockinfo->{type} eq 'de'), type => "de", disptype => "Dynamic DSL netblock" }, | 
|---|
|  | 676 | { selme => ($blockinfo->{type} eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" }, | 
|---|
|  | 677 | { selme => ($blockinfo->{type} eq 'we'), type => "we", disptype => "Dynamic wireless netblock" }, | 
|---|
|  | 678 | { selme => ($blockinfo->{type} eq 'cn'), type => "cn", disptype => "Customer netblock" }, | 
|---|
|  | 679 | { selme => ($blockinfo->{type} eq 'en'), type => "en", disptype => "End-use netblock" }, | 
|---|
|  | 680 | { selme => ($blockinfo->{type} eq 'in'), type => "in", disptype => "Internal netblock" }, | 
|---|
| [517] | 681 | ] | 
|---|
|  | 682 | ); | 
|---|
|  | 683 | } else { | 
|---|
| [534] | 684 | $page->param(disptype => $disp_alloctypes{$blockinfo->{type}}); | 
|---|
|  | 685 | $page->param(type => $blockinfo->{type}); | 
|---|
| [517] | 686 | } | 
|---|
|  | 687 |  | 
|---|
| [397] | 688 | ## node hack | 
|---|
| [530] | 689 | my ($nodeid,$nodename) = getNodeInfo($ip_dbh, $webvar{block}); | 
|---|
| [517] | 690 | $page->param(havenodeid => $nodeid); | 
|---|
|  | 691 |  | 
|---|
| [534] | 692 | if ($blockinfo->{type} eq 'fr' || $blockinfo->{type} eq 'bi') { | 
|---|
| [517] | 693 | $page->param(typesupportsnodes => 1); | 
|---|
|  | 694 | $page->param(nodename => $nodename); | 
|---|
|  | 695 |  | 
|---|
|  | 696 | ##fixme:  this whole hack needs cleanup and generalization for all alloctypes | 
|---|
|  | 697 | ##fixme:  arguably a bug that presence of a nodeid implies it can be changed.. | 
|---|
|  | 698 | #  but except for manual database changes, only the two types fr and bi can | 
|---|
|  | 699 | #  (currently) have a nodeid set in the first place. | 
|---|
|  | 700 | if ($IPDBacl{$authuser} =~ /c/) { | 
|---|
| [530] | 701 | my $nlist = getNodeList($ip_dbh); | 
|---|
|  | 702 | foreach (@{$nlist}) { | 
|---|
|  | 703 | $$_{selme} = ($$_{node_id} == $nodeid); | 
|---|
| [397] | 704 | } | 
|---|
| [530] | 705 | $page->param(nodelist => $nlist); | 
|---|
| [397] | 706 | } | 
|---|
|  | 707 | } | 
|---|
|  | 708 | ## end node hack | 
|---|
| [517] | 709 |  | 
|---|
| [534] | 710 | my ($lastmod,undef) = split /\s+/, $blockinfo->{lastmod}; | 
|---|
| [517] | 711 | $page->param(lastmod => $lastmod); | 
|---|
| [33] | 712 |  | 
|---|
| [517] | 713 | # not happy with the upside-down logic, but... | 
|---|
| [534] | 714 | $page->param(swipable => $blockinfo->{type} !~ /.i/); | 
|---|
|  | 715 | $page->param(swip => $blockinfo->{swip} ne 'n') if $blockinfo->{swip}; | 
|---|
| [320] | 716 |  | 
|---|
| [284] | 717 | # Check to see if we can display sensitive data | 
|---|
| [517] | 718 | $page->param(nocling => $IPDBacl{$authuser} =~ /s/); | 
|---|
| [534] | 719 | $page->param(privdata => $blockinfo->{privdata}); | 
|---|
| [284] | 720 |  | 
|---|
| [517] | 721 | # ACL trickery - these two template booleans control the presence of all form/input tags | 
|---|
|  | 722 | $page->param(maychange => $IPDBacl{$authuser} =~ /c/); | 
|---|
|  | 723 | $page->param(maydel => $IPDBacl{$authuser} =~ /d/); | 
|---|
| [4] | 724 |  | 
|---|
|  | 725 | } # edit() | 
|---|
|  | 726 |  | 
|---|
|  | 727 |  | 
|---|
|  | 728 | # Stuff new info about a block into the db | 
|---|
|  | 729 | # action=update | 
|---|
|  | 730 | sub update { | 
|---|
| [284] | 731 | if ($IPDBacl{$authuser} !~ /c/) { | 
|---|
| [517] | 732 | $aclerr = 'updateblock'; | 
|---|
| [284] | 733 | return; | 
|---|
|  | 734 | } | 
|---|
| [4] | 735 |  | 
|---|
|  | 736 | # Make sure incoming data is in correct format - custID among other things. | 
|---|
| [228] | 737 | return if !validateInput; | 
|---|
| [4] | 738 |  | 
|---|
| [536] | 739 | $webvar{swip} = 'n' if !$webvar{swip}; | 
|---|
|  | 740 |  | 
|---|
| [531] | 741 | my %updargs = ( | 
|---|
|  | 742 | custid          => $webvar{custid}, | 
|---|
|  | 743 | city            => $webvar{city}, | 
|---|
|  | 744 | description     => $webvar{desc}, | 
|---|
|  | 745 | notes           => $webvar{notes}, | 
|---|
|  | 746 | circuitid       => $webvar{circid}, | 
|---|
|  | 747 | block           => $webvar{block}, | 
|---|
|  | 748 | type            => $webvar{alloctype}, | 
|---|
| [536] | 749 | swip            => $webvar{swip}, | 
|---|
| [576] | 750 | rdepth          => $webvar{rdepth}, | 
|---|
| [588] | 751 | rdns            => $webvar{rdns}, | 
|---|
|  | 752 | user            => $authuser, | 
|---|
| [531] | 753 | ); | 
|---|
|  | 754 |  | 
|---|
|  | 755 | # Semioptional values | 
|---|
|  | 756 | $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/; | 
|---|
|  | 757 | $updargs{node} = $webvar{node} if $webvar{node}; | 
|---|
|  | 758 |  | 
|---|
|  | 759 | my ($code,$msg) = updateBlock($ip_dbh, %updargs); | 
|---|
|  | 760 |  | 
|---|
|  | 761 | if ($code eq 'FAIL') { | 
|---|
| [166] | 762 | syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'"; | 
|---|
| [517] | 763 | $page->param(err => "Could not update block/IP $webvar{block}: $msg"); | 
|---|
| [111] | 764 | return; | 
|---|
| [4] | 765 | } | 
|---|
|  | 766 |  | 
|---|
|  | 767 | # If we get here, the operation succeeded. | 
|---|
|  | 768 | syslog "notice", "$authuser updated $webvar{block}"; | 
|---|
| [531] | 769 | ##fixme:  log details of the change?  old way is in the .debug stream anyway. | 
|---|
| [416] | 770 | ##fixme:  need to wedge something in to allow "update:field" notifications | 
|---|
|  | 771 | ## hmm.  how to tell what changed?  O_o | 
|---|
| [426] | 772 | mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}", | 
|---|
| [416] | 773 | "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on'; | 
|---|
| [4] | 774 |  | 
|---|
| [517] | 775 | ## node hack | 
|---|
|  | 776 | if ($webvar{node} && $webvar{node} ne '-') { | 
|---|
| [530] | 777 | my $nodename = getNodeName($ip_dbh, $webvar{node}); | 
|---|
| [517] | 778 | $page->param(nodename => $nodename); | 
|---|
|  | 779 | } | 
|---|
|  | 780 | ## end node hack | 
|---|
|  | 781 |  | 
|---|
| [380] | 782 | # Link back to browse-routed or list-pool page on "Update complete" page. | 
|---|
| [576] | 783 | my $cblock = getBlockData($ip_dbh, $webvar{block}, $webvar{rdepth}); | 
|---|
| [380] | 784 | if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) { | 
|---|
| [517] | 785 | $page->param(backpool => 1); | 
|---|
| [576] | 786 | $page->param(backblock => $cblock->{pool}); | 
|---|
| [380] | 787 | } else { | 
|---|
| [576] | 788 | $page->param(backblock => $cblock->{parent}); | 
|---|
| [380] | 789 | } | 
|---|
| [576] | 790 | $page->param(backdepth => ($webvar{rdepth})); | 
|---|
| [380] | 791 |  | 
|---|
| [536] | 792 | # Do some HTML fiddling here instead of using ESCAPE=HTML in the template, | 
|---|
|  | 793 | # because otherwise we can't convert \n to <br>.  *sigh* | 
|---|
|  | 794 | $webvar{notes} = $q->escapeHTML($webvar{notes});      # escape first... | 
|---|
|  | 795 | $webvar{notes} =~ s/\n/<br>\n/;                       # ... then convert newlines | 
|---|
|  | 796 | $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : " "); | 
|---|
|  | 797 | $webvar{privdata} =~ s/\n/<br>\n/; | 
|---|
|  | 798 |  | 
|---|
| [517] | 799 | $page->param(cidr => $webvar{block}); | 
|---|
| [588] | 800 | $page->param(rdns => $webvar{rdns}); | 
|---|
| [517] | 801 | $page->param(city => $webvar{city}); | 
|---|
|  | 802 | $page->param(disptype => $disp_alloctypes{$webvar{alloctype}}); | 
|---|
|  | 803 | $page->param(custid => $webvar{custid}); | 
|---|
|  | 804 | $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No'); | 
|---|
| [536] | 805 | $page->param(circid => $webvar{circid}); | 
|---|
|  | 806 | $page->param(desc => $webvar{desc}); | 
|---|
|  | 807 | $page->param(notes => $webvar{notes}); | 
|---|
| [517] | 808 | $page->param(privdata => $webvar{privdata}) | 
|---|
|  | 809 | if $IPDBacl{$authuser} =~ /s/; | 
|---|
| [4] | 810 |  | 
|---|
|  | 811 | } # update() | 
|---|
|  | 812 |  | 
|---|
|  | 813 |  | 
|---|
|  | 814 | # Delete an allocation. | 
|---|
| [106] | 815 | sub remove { | 
|---|
| [233] | 816 | if ($IPDBacl{$authuser} !~ /d/) { | 
|---|
| [517] | 817 | $aclerr = 'delblock'; | 
|---|
| [233] | 818 | return; | 
|---|
|  | 819 | } | 
|---|
|  | 820 |  | 
|---|
| [4] | 821 | # Serves'em right for getting here... | 
|---|
|  | 822 | if (!defined($webvar{block})) { | 
|---|
| [517] | 823 | $page->param(err => "Can't delete a block that doesn't exist"); | 
|---|
| [111] | 824 | return; | 
|---|
| [4] | 825 | } | 
|---|
|  | 826 |  | 
|---|
| [538] | 827 | my $blockdata; | 
|---|
| [4] | 828 |  | 
|---|
| [577] | 829 | if ($webvar{rdepth} == 0) {   # $webvar{alloctype} eq 'mm' | 
|---|
| [4] | 830 |  | 
|---|
| [538] | 831 | $blockdata->{block} = $webvar{block}; | 
|---|
|  | 832 | $blockdata->{city} = "N/A"; | 
|---|
|  | 833 | $blockdata->{custid} = "N/A"; | 
|---|
| [577] | 834 | $blockdata->{type} = 'mm'; | 
|---|
| [538] | 835 | $blockdata->{circuitid} = "N/A"; | 
|---|
|  | 836 | $blockdata->{description} = "N/A"; | 
|---|
|  | 837 | $blockdata->{notes} = "N/A"; | 
|---|
|  | 838 | $blockdata->{privdata} = "N/A"; | 
|---|
| [577] | 839 | $blockdata->{rdepth} = 0; | 
|---|
| [517] | 840 |  | 
|---|
| [538] | 841 | } else { | 
|---|
| [4] | 842 |  | 
|---|
| [577] | 843 | $blockdata = getBlockData($ip_dbh, $webvar{block}, $webvar{rdepth}); | 
|---|
| [4] | 844 |  | 
|---|
|  | 845 | } # end cases for different alloctypes | 
|---|
|  | 846 |  | 
|---|
| [538] | 847 | $page->param(block => $blockdata->{block}); | 
|---|
| [589] | 848 |  | 
|---|
|  | 849 | $page->param(rdns => $blockdata->{rdns}); | 
|---|
|  | 850 |  | 
|---|
|  | 851 | # maybe need to apply more magic here? | 
|---|
|  | 852 | # most allocations we *do* want to autodelete the forward as well as reverse;  for a handful we don't. | 
|---|
|  | 853 | # -> all real blocks (nb: pool IPs need extra handling) | 
|---|
|  | 854 | # -> NOC/private-IP (how to ID?) | 
|---|
|  | 855 | # -> anything with a pattern matching $IPDB::domain? | 
|---|
|  | 856 | if ($blockdata->{type} !~ /^.i$/) { | 
|---|
|  | 857 | $page->param(autodel => 1); | 
|---|
|  | 858 | } | 
|---|
|  | 859 |  | 
|---|
| [577] | 860 | $page->param(rdepth => $blockdata->{rdepth}); | 
|---|
| [538] | 861 | $page->param(disptype => $disp_alloctypes{$blockdata->{type}}); | 
|---|
| [577] | 862 | #  $page->param(type => $blockdata->{type}); | 
|---|
| [538] | 863 | $page->param(city => $blockdata->{city}); | 
|---|
|  | 864 | $page->param(custid => $blockdata->{custid}); | 
|---|
|  | 865 | $page->param(circid => $blockdata->{circuitid}); | 
|---|
|  | 866 | $page->param(desc => $blockdata->{description}); | 
|---|
|  | 867 | $blockdata->{notes} = $q->escapeHTML($blockdata->{notes}); | 
|---|
|  | 868 | $blockdata->{notes} =~ s/\n/<br>\n/; | 
|---|
|  | 869 | $page->param(notes => $blockdata->{notes}); | 
|---|
|  | 870 | $blockdata->{privdata} = $q->escapeHTML($blockdata->{privdata}); | 
|---|
| [540] | 871 | $blockdata->{privdata} = ' ' if !$blockdata->{privdata}; | 
|---|
| [538] | 872 | $blockdata->{privdata} =~ s/\n/<br>\n/; | 
|---|
|  | 873 | $page->param(privdata => $blockdata->{privdata}) if $IPDBacl{$authuser} =~ /s/; | 
|---|
|  | 874 | $page->param(delpool => $blockdata->{type} =~ /^.[pd]$/); | 
|---|
| [4] | 875 |  | 
|---|
| [517] | 876 | } # end remove() | 
|---|
| [4] | 877 |  | 
|---|
|  | 878 |  | 
|---|
|  | 879 | # Delete an allocation.  Return it to the freeblocks table;  munge | 
|---|
|  | 880 | # data as necessary to keep as few records as possible in freeblocks | 
|---|
|  | 881 | # to prevent weirdness when allocating blocks later. | 
|---|
|  | 882 | # Remove IPs from pool listing if necessary | 
|---|
|  | 883 | sub finalDelete { | 
|---|
| [233] | 884 | if ($IPDBacl{$authuser} !~ /d/) { | 
|---|
| [517] | 885 | $aclerr = 'delblock'; | 
|---|
| [233] | 886 | return; | 
|---|
|  | 887 | } | 
|---|
| [4] | 888 |  | 
|---|
| [370] | 889 | # need to retrieve block data before deleting so we can notify on that | 
|---|
| [577] | 890 | my $blockinfo = getBlockData($ip_dbh, $webvar{block}, $webvar{rdepth}); | 
|---|
| [370] | 891 |  | 
|---|
| [590] | 892 | my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{rdepth}, $webvar{vrf}, $webvar{delforward}, $authuser); | 
|---|
| [4] | 893 |  | 
|---|
| [517] | 894 | $page->param(block => $webvar{block}); | 
|---|
| [577] | 895 | $page->param(delparent => $blockinfo->{parent}) if $webvar{rdepth}; | 
|---|
|  | 896 | $page->param(prdepth => $webvar{rdepth}); | 
|---|
| [591] | 897 | if ($code =~ /^WARN(POOL|MERGE)/) { | 
|---|
| [577] | 898 | my ($bp,$bd) = split /,/, $msg; | 
|---|
|  | 899 | $page->param(bparent => $bp); | 
|---|
|  | 900 | $page->param(brdepth => $bd); | 
|---|
|  | 901 | $page->param(mergeip => $code eq 'WARNPOOL'); | 
|---|
|  | 902 | } | 
|---|
| [591] | 903 | if ($code eq 'WARN') { | 
|---|
|  | 904 | $msg =~ s/\n/<br>\n/g; | 
|---|
|  | 905 | $page->param(genwarn => $msg); | 
|---|
|  | 906 | } | 
|---|
| [577] | 907 | if ($code eq 'OK' || $code =~ /^WARN/) { | 
|---|
| [528] | 908 | syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block} ". | 
|---|
|  | 909 | $blockinfo->{custid}.", ".$blockinfo->{city}.", desc='".$blockinfo->{description}."'"; | 
|---|
| [416] | 910 | mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}", | 
|---|
|  | 911 | "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n". | 
|---|
| [528] | 912 | "CustID: ".$blockinfo->{custid}."\nCity: ".$blockinfo->{city}. | 
|---|
|  | 913 | "\nDescription: ".$blockinfo->{description}."\n"); | 
|---|
| [106] | 914 | } else { | 
|---|
| [517] | 915 | $page->param(failmsg => $msg); | 
|---|
| [106] | 916 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
|  | 917 | syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'"; | 
|---|
|  | 918 | } else { | 
|---|
|  | 919 | syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'"; | 
|---|
| [517] | 920 | $page->param(netblock => 1); | 
|---|
| [4] | 921 | } | 
|---|
| [106] | 922 | } | 
|---|
| [4] | 923 |  | 
|---|
|  | 924 | } # finalDelete | 
|---|