| [4] | 1 | #!/usr/bin/perl | 
|---|
|  | 2 | # ipdb/cgi-bin/main.cgi | 
|---|
| [8] | 3 | ### | 
|---|
|  | 4 | # SVN revision info | 
|---|
|  | 5 | # $Date: 2012-10-18 20:53:10 +0000 (Thu, 18 Oct 2012) $ | 
|---|
|  | 6 | # SVN revision $Rev: 517 $ | 
|---|
|  | 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; | 
|---|
|  | 19 |  | 
|---|
|  | 20 | use Sys::Syslog; | 
|---|
|  | 21 |  | 
|---|
| [417] | 22 | # don't remove!  required for GNU/FHS-ish install from tarball | 
|---|
|  | 23 | ##uselib## | 
|---|
|  | 24 |  | 
|---|
| [515] | 25 | use CustIDCK; | 
|---|
| [417] | 26 | use MyIPDB; | 
|---|
|  | 27 |  | 
|---|
| [431] | 28 | openlog "IPDB","pid","$IPDB::syslog_facility"; | 
|---|
| [4] | 29 |  | 
|---|
| [517] | 30 | ## Environment.  Collect some things, process some things, set some things... | 
|---|
|  | 31 |  | 
|---|
| [233] | 32 | # Collect the username from HTTP auth.  If undefined, we're in | 
|---|
|  | 33 | # a test environment, or called without a username. | 
|---|
| [4] | 34 | my $authuser; | 
|---|
|  | 35 | if (!defined($ENV{'REMOTE_USER'})) { | 
|---|
|  | 36 | $authuser = '__temptest'; | 
|---|
|  | 37 | } else { | 
|---|
|  | 38 | $authuser = $ENV{'REMOTE_USER'}; | 
|---|
|  | 39 | } | 
|---|
|  | 40 |  | 
|---|
| [517] | 41 | # anyone got a better name?  :P | 
|---|
|  | 42 | my $thingroot = $ENV{SCRIPT_FILENAME}; | 
|---|
|  | 43 | $thingroot =~ s|cgi-bin/main.cgi||; | 
|---|
|  | 44 |  | 
|---|
| [402] | 45 | syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}"; | 
|---|
| [4] | 46 |  | 
|---|
| [517] | 47 | ##fixme there *must* be a better order to do things in so this can go back where it was | 
|---|
|  | 48 | # CGI fiddling done here so we can declare %webvar so we can alter $webvar{action} | 
|---|
|  | 49 | # to show the right page on DB errors. | 
|---|
|  | 50 | # Set up the CGI object... | 
|---|
|  | 51 | my $q = new CGI::Simple; | 
|---|
|  | 52 | # ... and get query-string params as well as POST params if necessary | 
|---|
|  | 53 | $q->parse_query_string; | 
|---|
|  | 54 |  | 
|---|
|  | 55 | # Convenience;  saves changing all references to %webvar | 
|---|
|  | 56 | ##fixme:  tweak for handling <select multiple='y' size=3> (list with multiple selection) | 
|---|
|  | 57 | my %webvar = $q->Vars; | 
|---|
|  | 58 |  | 
|---|
| [106] | 59 | # Why not a global DB handle?  (And a global statement handle, as well...) | 
|---|
|  | 60 | # Use the connectDB function, otherwise we end up confusing ourselves | 
|---|
|  | 61 | my $ip_dbh; | 
|---|
|  | 62 | my $sth; | 
|---|
|  | 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") { | 
|---|
|  | 92 | $page = HTML::Template->new(filename => "$webvar{action}.tmpl"); | 
|---|
|  | 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 | } | 
|---|
| [4] | 103 | } elsif ($webvar{action} eq 'newmaster') { | 
|---|
|  | 104 |  | 
|---|
| [233] | 105 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| [517] | 106 | $aclerr = 'addmaster'; | 
|---|
| [233] | 107 | } else { | 
|---|
|  | 108 | my $cidr = new NetAddr::IP $webvar{cidr}; | 
|---|
| [517] | 109 | $page->param(cidr => "$cidr"); | 
|---|
| [4] | 110 |  | 
|---|
| [371] | 111 | my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr}); | 
|---|
| [4] | 112 |  | 
|---|
| [371] | 113 | if ($code eq 'FAIL') { | 
|---|
| [320] | 114 | syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'"; | 
|---|
| [517] | 115 | $page->param(err => $msg); | 
|---|
| [233] | 116 | } else { | 
|---|
|  | 117 | syslog "info", "$authuser added master block $webvar{cidr}"; | 
|---|
|  | 118 | } | 
|---|
| [4] | 119 |  | 
|---|
| [233] | 120 | } # ACL check | 
|---|
|  | 121 |  | 
|---|
| [4] | 122 | } # end add new master | 
|---|
|  | 123 |  | 
|---|
|  | 124 | elsif($webvar{action} eq 'showmaster') { | 
|---|
|  | 125 | showMaster(); | 
|---|
|  | 126 | } | 
|---|
|  | 127 | elsif($webvar{action} eq 'showrouted') { | 
|---|
|  | 128 | showRBlock(); | 
|---|
|  | 129 | } | 
|---|
|  | 130 | elsif($webvar{action} eq 'listpool') { | 
|---|
|  | 131 | listPool(); | 
|---|
|  | 132 | } | 
|---|
|  | 133 |  | 
|---|
|  | 134 | # Not modified or added;  just shuffled | 
|---|
|  | 135 | elsif($webvar{action} eq 'assign') { | 
|---|
|  | 136 | assignBlock(); | 
|---|
|  | 137 | } | 
|---|
|  | 138 | elsif($webvar{action} eq 'confirm') { | 
|---|
|  | 139 | confirmAssign(); | 
|---|
|  | 140 | } | 
|---|
|  | 141 | elsif($webvar{action} eq 'insert') { | 
|---|
|  | 142 | insertAssign(); | 
|---|
|  | 143 | } | 
|---|
|  | 144 | elsif($webvar{action} eq 'edit') { | 
|---|
|  | 145 | edit(); | 
|---|
|  | 146 | } | 
|---|
|  | 147 | elsif($webvar{action} eq 'update') { | 
|---|
|  | 148 | update(); | 
|---|
|  | 149 | } | 
|---|
|  | 150 | elsif($webvar{action} eq 'delete') { | 
|---|
|  | 151 | remove(); | 
|---|
|  | 152 | } | 
|---|
|  | 153 | elsif($webvar{action} eq 'finaldelete') { | 
|---|
|  | 154 | finalDelete(); | 
|---|
|  | 155 | } | 
|---|
| [397] | 156 | elsif ($webvar{action} eq 'nodesearch') { | 
|---|
|  | 157 | $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id"); | 
|---|
| [517] | 158 | $sth->execute() or $page->param(errmsg => $sth->errstr); | 
|---|
|  | 159 | my @nodelist; | 
|---|
| [397] | 160 | while (my ($nid,$nname) = $sth->fetchrow_array()) { | 
|---|
| [517] | 161 | my %row = (nodeid => $nid, nodename => $nname); | 
|---|
|  | 162 | push @nodelist, \%row; | 
|---|
| [397] | 163 | } | 
|---|
| [517] | 164 | $page->param(nodelist => \@nodelist); | 
|---|
|  | 165 | } | 
|---|
| [397] | 166 |  | 
|---|
| [517] | 167 | # DB failure.  Can't do much here, really. | 
|---|
|  | 168 | elsif ($webvar{action} eq 'dberr') { | 
|---|
|  | 169 | $page->param(errmsg => $errstr); | 
|---|
| [397] | 170 | } | 
|---|
|  | 171 |  | 
|---|
| [517] | 172 | # Default is an error.  It shouldn't be possible to get here unless you're | 
|---|
|  | 173 | # randomly feeding in values for webvar{action}. | 
|---|
| [4] | 174 | else { | 
|---|
|  | 175 | my $rnd = rand 500; | 
|---|
|  | 176 | my $boing = sprintf("%.2f", rand 500); | 
|---|
| [517] | 177 | my @excuses = ( | 
|---|
|  | 178 | "Aether cloudy.  Ask again later about $webvar{action}.", | 
|---|
|  | 179 | "The gods are unhappy with your sacrificial $webvar{action}.", | 
|---|
|  | 180 | "Because one of $webvar{action}'s legs are both the same", | 
|---|
|  | 181 | "<b>wibble</b><br>Can't $webvar{action}, the grue will get me!<br>Can't $webvar{action}, the grue will get me!", | 
|---|
|  | 182 | "Hey, man, you've had your free $webvar{action}.  Next one's gonna...  <i>cost</i>....", | 
|---|
|  | 183 | "I ain't done $webvar{action}", | 
|---|
|  | 184 | "Oooo, look!  A flying $webvar{action}!", | 
|---|
|  | 185 | "$webvar{action} too evil, avoiding.", | 
|---|
|  | 186 | "Rocks fall, $webvar{action} dies.", | 
|---|
|  | 187 | "Bit bucket must be emptied before I can $webvar{action}..." | 
|---|
|  | 188 | ); | 
|---|
|  | 189 | $page->param(dunno => $excuses[$rnd/50.0]); | 
|---|
| [4] | 190 | } | 
|---|
| [111] | 191 | ## Finally! Done with that NASTY "case" emulation! | 
|---|
| [4] | 192 |  | 
|---|
|  | 193 |  | 
|---|
| [517] | 194 | # Switch to a different template if we've tripped on an ACL error. | 
|---|
|  | 195 | # Note that this should only be exercised in development, when | 
|---|
|  | 196 | # deeplinked, or when being attacked;  normal ACL handling should | 
|---|
|  | 197 | # remove the links a user is not allowed to click on. | 
|---|
|  | 198 | if ($aclerr) { | 
|---|
|  | 199 | $page = HTML::Template->new(filename => "aclerror.tmpl"); | 
|---|
|  | 200 | $page->param(ipdbfunc => $aclmsg{$aclerr}); | 
|---|
|  | 201 | } | 
|---|
| [4] | 202 |  | 
|---|
| [517] | 203 |  | 
|---|
| [106] | 204 | # Clean up IPDB globals, DB handle, etc. | 
|---|
| [111] | 205 | finish($ip_dbh); | 
|---|
| [199] | 206 |  | 
|---|
| [517] | 207 | ## Do all our printing here so we can generate errors and stick them into the slots in the templates. | 
|---|
| [199] | 208 |  | 
|---|
| [517] | 209 | # can't do this yet, too many blowups | 
|---|
|  | 210 | #print "Content-type: text/html\n\n", $header->output; | 
|---|
|  | 211 | $page->param(webpath => $IPDB::webpath); | 
|---|
|  | 212 | print $page->output; | 
|---|
|  | 213 |  | 
|---|
|  | 214 | # include the admin tools link in the output? | 
|---|
|  | 215 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/)); | 
|---|
|  | 216 | $footer->param(webpath => $IPDB::webpath); | 
|---|
|  | 217 | print $footer->output; | 
|---|
|  | 218 |  | 
|---|
| [106] | 219 | # Just in case something waaaayyy down isn't in place | 
|---|
|  | 220 | # properly... we exit explicitly. | 
|---|
| [517] | 221 | exit 0; | 
|---|
| [4] | 222 |  | 
|---|
|  | 223 |  | 
|---|
|  | 224 | # Initial display:  Show master blocks with total allocated subnets, total free subnets | 
|---|
| [118] | 225 | sub showSummary { | 
|---|
| [106] | 226 | my %allocated; | 
|---|
|  | 227 | my %free; | 
|---|
|  | 228 | my %routed; | 
|---|
|  | 229 | my %bigfree; | 
|---|
|  | 230 |  | 
|---|
| [118] | 231 | # Count the allocations. | 
|---|
|  | 232 | $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?"); | 
|---|
|  | 233 | foreach my $master (@masterblocks) { | 
|---|
|  | 234 | $sth->execute("$master"); | 
|---|
|  | 235 | $sth->bind_columns(\$allocated{"$master"}); | 
|---|
|  | 236 | $sth->fetch(); | 
|---|
| [4] | 237 | } | 
|---|
|  | 238 |  | 
|---|
| [118] | 239 | # Count routed blocks | 
|---|
|  | 240 | $sth = $ip_dbh->prepare("select count(*) from routed where cidr <<= ?"); | 
|---|
|  | 241 | foreach my $master (@masterblocks) { | 
|---|
|  | 242 | $sth->execute("$master"); | 
|---|
|  | 243 | $sth->bind_columns(\$routed{"$master"}); | 
|---|
|  | 244 | $sth->fetch(); | 
|---|
| [4] | 245 | } | 
|---|
|  | 246 |  | 
|---|
| [118] | 247 | # Count the free blocks. | 
|---|
| [231] | 248 | $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ". | 
|---|
|  | 249 | "(routed='y' or routed='n')"); | 
|---|
| [118] | 250 | foreach my $master (@masterblocks) { | 
|---|
|  | 251 | $sth->execute("$master"); | 
|---|
|  | 252 | $sth->bind_columns(\$free{"$master"}); | 
|---|
|  | 253 | $sth->fetch(); | 
|---|
| [4] | 254 | } | 
|---|
|  | 255 |  | 
|---|
| [118] | 256 | # Find the largest free block in each master | 
|---|
| [231] | 257 | $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ". | 
|---|
|  | 258 | "(routed='y' or routed='n') order by maskbits limit 1"); | 
|---|
| [118] | 259 | foreach my $master (@masterblocks) { | 
|---|
|  | 260 | $sth->execute("$master"); | 
|---|
|  | 261 | $sth->bind_columns(\$bigfree{"$master"}); | 
|---|
|  | 262 | $sth->fetch(); | 
|---|
|  | 263 | } | 
|---|
|  | 264 |  | 
|---|
| [517] | 265 | # Assemble the data to stuff into the template. | 
|---|
|  | 266 | my @masterlist; | 
|---|
|  | 267 | my $rowclass=0; | 
|---|
| [4] | 268 | foreach my $master (@masterblocks) { | 
|---|
| [517] | 269 | my %row = ( | 
|---|
|  | 270 | rowclass => $rowclass++ % 2, | 
|---|
|  | 271 | master => "$master", | 
|---|
|  | 272 | routed => $routed{"$master"}, | 
|---|
|  | 273 | allocated => $allocated{"$master"}, | 
|---|
|  | 274 | free => $free{"$master"}, | 
|---|
|  | 275 | bigfree => ( ($bigfree{"$master"} eq '') ? ("<NONE>") : ("/".$bigfree{"$master"}) ) | 
|---|
| [4] | 276 | ); | 
|---|
| [517] | 277 | push (@masterlist, \%row); | 
|---|
| [4] | 278 | } | 
|---|
| [517] | 279 | $page->param(masterlist => \@masterlist); | 
|---|
| [4] | 280 |  | 
|---|
| [517] | 281 | $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) ); | 
|---|
|  | 282 |  | 
|---|
| [4] | 283 | } # showSummary | 
|---|
|  | 284 |  | 
|---|
|  | 285 |  | 
|---|
|  | 286 | # Display detail on master | 
|---|
|  | 287 | # Alrighty then!  We're showing routed blocks within a single master this time. | 
|---|
|  | 288 | # We should be able to steal code from showSummary(), and if I'm really smart | 
|---|
|  | 289 | # I'll figger a way to munge the two together.  (Once I've done that, everything | 
|---|
|  | 290 | # else should follow.  YMMV.) | 
|---|
|  | 291 | sub showMaster { | 
|---|
|  | 292 |  | 
|---|
| [517] | 293 | $page->param(master => $webvar{block}); | 
|---|
| [4] | 294 |  | 
|---|
| [106] | 295 | my %allocated; | 
|---|
|  | 296 | my %free; | 
|---|
| [517] | 297 | my %cities; | 
|---|
| [106] | 298 | my %bigfree; | 
|---|
|  | 299 |  | 
|---|
| [4] | 300 | my $master = new NetAddr::IP $webvar{block}; | 
|---|
|  | 301 | my @localmasters; | 
|---|
|  | 302 |  | 
|---|
| [118] | 303 | # Fetch only the blocks relevant to this master | 
|---|
| [157] | 304 | $sth = $ip_dbh->prepare("select cidr,city from routed where cidr <<= '$master' order by cidr"); | 
|---|
| [4] | 305 | $sth->execute(); | 
|---|
|  | 306 |  | 
|---|
|  | 307 | my $i=0; | 
|---|
|  | 308 | while (my @data = $sth->fetchrow_array()) { | 
|---|
|  | 309 | my $cidr = new NetAddr::IP $data[0]; | 
|---|
| [118] | 310 | $localmasters[$i++] = $cidr; | 
|---|
|  | 311 | $free{"$cidr"} = 0; | 
|---|
|  | 312 | $allocated{"$cidr"} = 0; | 
|---|
|  | 313 | $bigfree{"$cidr"} = 128; | 
|---|
| [4] | 314 | # Retain the routing destination | 
|---|
| [517] | 315 | $cities{"$cidr"} = $data[1]; | 
|---|
| [4] | 316 | } | 
|---|
|  | 317 |  | 
|---|
| [118] | 318 | # Check if there were actually any blocks routed from this master | 
|---|
| [4] | 319 | if ($i > 0) { | 
|---|
|  | 320 |  | 
|---|
| [118] | 321 | # Count the allocations | 
|---|
|  | 322 | $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?"); | 
|---|
|  | 323 | foreach my $master (@localmasters) { | 
|---|
|  | 324 | $sth->execute("$master"); | 
|---|
|  | 325 | $sth->bind_columns(\$allocated{"$master"}); | 
|---|
|  | 326 | $sth->fetch(); | 
|---|
| [4] | 327 | } | 
|---|
|  | 328 |  | 
|---|
| [118] | 329 | # Count the free blocks. | 
|---|
| [231] | 330 | $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ". | 
|---|
|  | 331 | "(routed='y' or routed='n')"); | 
|---|
| [118] | 332 | foreach my $master (@localmasters) { | 
|---|
|  | 333 | $sth->execute("$master"); | 
|---|
|  | 334 | $sth->bind_columns(\$free{"$master"}); | 
|---|
|  | 335 | $sth->fetch(); | 
|---|
| [4] | 336 | } | 
|---|
|  | 337 |  | 
|---|
| [118] | 338 | # Get the size of the largest free block | 
|---|
| [231] | 339 | $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ". | 
|---|
|  | 340 | "(routed='y' or routed='n') order by maskbits limit 1"); | 
|---|
| [118] | 341 | foreach my $master (@localmasters) { | 
|---|
|  | 342 | $sth->execute("$master"); | 
|---|
|  | 343 | $sth->bind_columns(\$bigfree{"$master"}); | 
|---|
|  | 344 | $sth->fetch(); | 
|---|
| [4] | 345 | } | 
|---|
|  | 346 |  | 
|---|
| [517] | 347 | my @routed; | 
|---|
|  | 348 | my $rowclass = 0; | 
|---|
| [4] | 349 | foreach my $master (@localmasters) { | 
|---|
| [517] | 350 | my %row = ( | 
|---|
|  | 351 | rowclass => $rowclass++ % 2, | 
|---|
|  | 352 | block => "$master", | 
|---|
|  | 353 | city => $cities{"$master"}, | 
|---|
|  | 354 | nsubs => $allocated{"$master"}, | 
|---|
|  | 355 | nfree => $free{"$master"}, | 
|---|
|  | 356 | lfree => ( ($bigfree{"$master"} eq 128) ? ("<NONE>") : ("/".$bigfree{"$master"}) ) | 
|---|
|  | 357 | ); | 
|---|
|  | 358 | push @routed, \%row; | 
|---|
| [4] | 359 | } | 
|---|
| [517] | 360 | $page->param(routedlist => \@routed); | 
|---|
| [4] | 361 |  | 
|---|
|  | 362 | } # end check for existence of routed blocks in master | 
|---|
|  | 363 |  | 
|---|
| [517] | 364 | $page->param(delmaster => ($IPDBacl{$authuser} =~ /d/)); | 
|---|
| [4] | 365 |  | 
|---|
|  | 366 | # Snag the free blocks. | 
|---|
|  | 367 | my $count = 0; | 
|---|
| [118] | 368 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr <<='$master' and ". | 
|---|
|  | 369 | "routed='n' order by cidr"); | 
|---|
| [4] | 370 | $sth->execute(); | 
|---|
| [517] | 371 | my @unrouted; | 
|---|
|  | 372 | my $rowclass = 0; | 
|---|
| [4] | 373 | while (my @data = $sth->fetchrow_array()) { | 
|---|
|  | 374 | my $cidr = new NetAddr::IP $data[0]; | 
|---|
| [517] | 375 | my %row = ( | 
|---|
|  | 376 | rowclass => $rowclass++ % 2, | 
|---|
|  | 377 | fblock => "$cidr", | 
|---|
|  | 378 | frange => $cidr->range | 
|---|
|  | 379 | ); | 
|---|
|  | 380 | push @unrouted, \%row; | 
|---|
| [4] | 381 | } | 
|---|
| [517] | 382 | $page->param(unrouted => \@unrouted); | 
|---|
| [4] | 383 |  | 
|---|
|  | 384 | } # showMaster | 
|---|
|  | 385 |  | 
|---|
|  | 386 |  | 
|---|
|  | 387 | # Display details of a routed block | 
|---|
|  | 388 | # Alrighty then!  We're showing allocations within a routed block this time. | 
|---|
|  | 389 | # We should be able to steal code from showSummary() and showMaster(), and if | 
|---|
|  | 390 | # I'm really smart I'll figger a way to munge all three together.  (Once I've | 
|---|
|  | 391 | # done that, everything else should follow.  YMMV. | 
|---|
|  | 392 | # This time, we check the database before spewing, because we may | 
|---|
|  | 393 | # not have anything useful to spew. | 
|---|
|  | 394 | sub showRBlock { | 
|---|
|  | 395 |  | 
|---|
|  | 396 | my $master = new NetAddr::IP $webvar{block}; | 
|---|
|  | 397 |  | 
|---|
| [157] | 398 | $sth = $ip_dbh->prepare("select city from routed where cidr='$master'"); | 
|---|
| [4] | 399 | $sth->execute; | 
|---|
| [517] | 400 | my ($rcity) = $sth->fetchrow_array; | 
|---|
| [4] | 401 |  | 
|---|
| [517] | 402 | $page->param(master => "$master"); | 
|---|
|  | 403 | $page->param(rcity => $rcity); | 
|---|
| [4] | 404 |  | 
|---|
| [118] | 405 | # Snag the allocations for this block | 
|---|
| [320] | 406 | $sth = $ip_dbh->prepare("select cidr,city,type,custid,swip,description". | 
|---|
| [157] | 407 | " from allocations where cidr <<= '$master' order by cidr"); | 
|---|
| [4] | 408 | $sth->execute(); | 
|---|
|  | 409 |  | 
|---|
| [380] | 410 | # hack hack hack | 
|---|
|  | 411 | # set up to flag swip=y records if they don't actually have supporting data in the customers table | 
|---|
|  | 412 | my $custsth = $ip_dbh->prepare("select count(*) from customers where custid=?"); | 
|---|
|  | 413 |  | 
|---|
| [517] | 414 | my $rowclass = 0; | 
|---|
|  | 415 | my @blocklist; | 
|---|
|  | 416 | while (my ($cidr,$city,$type,$custid,$swip,$desc) = $sth->fetchrow_array()) { | 
|---|
|  | 417 | $custsth->execute($custid); | 
|---|
| [380] | 418 | my ($ncust) = $custsth->fetchrow_array(); | 
|---|
|  | 419 |  | 
|---|
| [517] | 420 | my %row = ( | 
|---|
|  | 421 | rowclass => $rowclass++ % 2, | 
|---|
|  | 422 | block => $cidr, | 
|---|
|  | 423 | city => $city, | 
|---|
|  | 424 | type => $disp_alloctypes{$type}, | 
|---|
|  | 425 | custid => $custid, | 
|---|
|  | 426 | swip => ($swip eq 'y' ? ($ncust == 0 ? 'Yes<small>*</small>' : 'Yes') : 'No'), | 
|---|
|  | 427 | desc => $desc | 
|---|
|  | 428 | ); | 
|---|
|  | 429 | $row{subblock} = ($type =~ /^.r$/);         # hmf.  wonder why these won't work in the hash declaration... | 
|---|
|  | 430 | $row{listpool} = ($type =~ /^.[pd]$/); | 
|---|
|  | 431 | push (@blocklist, \%row); | 
|---|
| [4] | 432 | } | 
|---|
| [517] | 433 | $page->param(blocklist => \@blocklist); | 
|---|
| [4] | 434 |  | 
|---|
| [517] | 435 | $page->param(delrouted => $IPDBacl{$authuser} =~ /d/); | 
|---|
| [4] | 436 |  | 
|---|
|  | 437 | # Snag the free blocks.  We don't really *need* to be pedantic about avoiding | 
|---|
|  | 438 | # unrouted free blocks, but it's better to let the database do the work if we can. | 
|---|
| [517] | 439 | $rowclass = 0; | 
|---|
|  | 440 | my @unassigned; | 
|---|
| [186] | 441 | $sth = $ip_dbh->prepare("select cidr,routed from freeblocks where cidr <<= '$master'". | 
|---|
|  | 442 | " order by cidr"); | 
|---|
| [4] | 443 | $sth->execute(); | 
|---|
| [517] | 444 | while (my ($cidr_db,$routed) = $sth->fetchrow_array()) { | 
|---|
|  | 445 | my $cidr = new NetAddr::IP $cidr_db; | 
|---|
|  | 446 |  | 
|---|
|  | 447 | my %row = ( | 
|---|
|  | 448 | rowclass => $rowclass++ % 2, | 
|---|
|  | 449 | subblock => ($routed ne 'y' && $routed ne 'n'), | 
|---|
|  | 450 | fblock => $cidr_db, | 
|---|
|  | 451 | fbtype => $routed, | 
|---|
|  | 452 | frange => $cidr->range, | 
|---|
|  | 453 | ); | 
|---|
|  | 454 | push @unassigned, \%row; | 
|---|
| [4] | 455 | } | 
|---|
| [517] | 456 | $page->param(unassigned => \@unassigned); | 
|---|
| [4] | 457 |  | 
|---|
|  | 458 | } # showRBlock | 
|---|
|  | 459 |  | 
|---|
|  | 460 |  | 
|---|
|  | 461 | # List the IPs used in a pool | 
|---|
|  | 462 | sub listPool { | 
|---|
|  | 463 |  | 
|---|
|  | 464 | my $cidr = new NetAddr::IP $webvar{pool}; | 
|---|
|  | 465 |  | 
|---|
| [517] | 466 | $page->param(block => $webvar{pool}); | 
|---|
|  | 467 | $page->param(netip => $cidr->addr); | 
|---|
|  | 468 | $cidr++; | 
|---|
|  | 469 | $page->param(gate => $cidr->addr); | 
|---|
|  | 470 | $cidr--;  $cidr--; | 
|---|
|  | 471 | $page->param(bcast => $cidr->addr); | 
|---|
|  | 472 | $page->param(mask => $cidr->mask); | 
|---|
| [157] | 473 |  | 
|---|
| [4] | 474 | # Snag pool info for heading | 
|---|
| [517] | 475 | $sth = $ip_dbh->prepare("select type,city from allocations where cidr=?"); | 
|---|
|  | 476 | $sth->execute($webvar{pool}); | 
|---|
|  | 477 | my ($pooltype, $poolcity) = $sth->fetchrow_array; | 
|---|
| [4] | 478 |  | 
|---|
| [517] | 479 | $page->param(disptype => $disp_alloctypes{$pooltype}); | 
|---|
|  | 480 | $page->param(city => $poolcity); | 
|---|
|  | 481 |  | 
|---|
| [157] | 482 | # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy | 
|---|
| [517] | 483 | $page->param(realblock => $pooltype =~ /^.d$/); | 
|---|
| [4] | 484 |  | 
|---|
|  | 485 | # probably have to add an "edit IP allocation" link here somewhere. | 
|---|
|  | 486 |  | 
|---|
| [157] | 487 | $sth = $ip_dbh->prepare("select ip,custid,available,description,type". | 
|---|
|  | 488 | " from poolips where pool='$webvar{pool}' order by ip"); | 
|---|
| [4] | 489 | $sth->execute; | 
|---|
| [517] | 490 | my @poolips; | 
|---|
|  | 491 | my $rowclass = 0; | 
|---|
|  | 492 | while (my ($ip,$custid,$available,$desc,$type) = $sth->fetchrow_array) { | 
|---|
|  | 493 | my %row = ( | 
|---|
|  | 494 | rowclass => $rowclass++ % 2, | 
|---|
|  | 495 | ip => $ip, | 
|---|
|  | 496 | custid => $custid, | 
|---|
|  | 497 | available => $available, | 
|---|
|  | 498 | desc => $desc, | 
|---|
|  | 499 | maydel => $IPDBacl{$authuser} =~ /d/, | 
|---|
|  | 500 | delme => $available eq 'n' | 
|---|
| [4] | 501 | ); | 
|---|
| [517] | 502 | push @poolips, \%row; | 
|---|
| [4] | 503 | } | 
|---|
| [517] | 504 | $page->param(poolips => \@poolips); | 
|---|
| [4] | 505 |  | 
|---|
|  | 506 | } # end listPool | 
|---|
|  | 507 |  | 
|---|
|  | 508 |  | 
|---|
| [106] | 509 | # Show "Add new allocation" page.  Note that the actual page may | 
|---|
|  | 510 | # be one of two templates, and the lists come from the database. | 
|---|
| [4] | 511 | sub assignBlock { | 
|---|
|  | 512 |  | 
|---|
| [233] | 513 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| [517] | 514 | $aclerr = 'addblock'; | 
|---|
| [233] | 515 | return; | 
|---|
|  | 516 | } | 
|---|
|  | 517 |  | 
|---|
| [517] | 518 | # hack pthbttt eww | 
|---|
|  | 519 | $webvar{block} = '' if !$webvar{block}; | 
|---|
| [21] | 520 |  | 
|---|
| [517] | 521 | # hmm.  TMPL_IF block and TMPL_ELSE block on these instead? | 
|---|
|  | 522 | $page->param(rowa => 'row'.($webvar{block} eq '' ? 1 : 0)); | 
|---|
|  | 523 | $page->param(rowb => 'row'.($webvar{block} eq '' ? 0 : 1)); | 
|---|
|  | 524 | $page->param(block => $webvar{block});        # fb-assign flag, if block is set, we're in fb-assign | 
|---|
|  | 525 | $page->param(iscontained => ($webvar{fbtype} && $webvar{fbtype} ne 'y')); | 
|---|
|  | 526 |  | 
|---|
| [21] | 527 | # New special case- block to assign is specified | 
|---|
|  | 528 | if ($webvar{block} ne '') { | 
|---|
|  | 529 | my $block = new NetAddr::IP $webvar{block}; | 
|---|
| [187] | 530 |  | 
|---|
| [517] | 531 | # Handle contained freeblock allocation. | 
|---|
| [187] | 532 | # This is a little dangerous, as it's *theoretically* possible to | 
|---|
|  | 533 | # get fbtype='n' (aka a non-routed freeblock).  However, should | 
|---|
|  | 534 | # someone manage to get there, they get what they deserve. | 
|---|
|  | 535 | if ($webvar{fbtype} ne 'y') { | 
|---|
| [517] | 536 | # Snag the type of the container block from the database. | 
|---|
| [187] | 537 | $sth = $ip_dbh->prepare("select type from allocations where cidr >>='$block'"); | 
|---|
|  | 538 | $sth->execute; | 
|---|
|  | 539 | my @data = $sth->fetchrow_array; | 
|---|
|  | 540 | $data[0] =~ s/c$/r/;      # Munge the type into the correct form | 
|---|
| [517] | 541 | $page->param(fbdisptype => $list_alloctypes{$data[0]}); | 
|---|
|  | 542 | $page->param(type => $data[0]); | 
|---|
| [187] | 543 | } else { | 
|---|
|  | 544 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 500 ". | 
|---|
| [191] | 545 | "and type not like '_i' and type not like '_r' order by listorder"); | 
|---|
| [187] | 546 | $sth->execute; | 
|---|
| [517] | 547 | my @typelist; | 
|---|
|  | 548 | my $selflag = 0; | 
|---|
| [187] | 549 | while (my @data = $sth->fetchrow_array) { | 
|---|
| [517] | 550 | my %row = (tval => $data[0], | 
|---|
|  | 551 | type => $data[1], | 
|---|
|  | 552 | sel => ($selflag == 0 ? ' selected' : '') | 
|---|
|  | 553 | ); | 
|---|
|  | 554 | push (@typelist, \%row); | 
|---|
|  | 555 | $selflag++; | 
|---|
| [187] | 556 | } | 
|---|
| [517] | 557 | $page->param(typelist => \@typelist); | 
|---|
| [106] | 558 | } | 
|---|
| [21] | 559 | } else { | 
|---|
| [517] | 560 | my @masterlist; | 
|---|
| [31] | 561 | foreach my $master (@masterblocks) { | 
|---|
| [517] | 562 | my %row = (master => "$master"); | 
|---|
|  | 563 | push (@masterlist, \%row); | 
|---|
| [31] | 564 | } | 
|---|
| [517] | 565 | $page->param(masterlist => \@masterlist); | 
|---|
|  | 566 |  | 
|---|
|  | 567 | my @pops; | 
|---|
| [92] | 568 | foreach my $pop (@poplist) { | 
|---|
| [517] | 569 | my %row = (pop => $pop); | 
|---|
|  | 570 | push (@pops, \%row); | 
|---|
| [92] | 571 | } | 
|---|
| [517] | 572 | $page->param(pops => \@pops); | 
|---|
|  | 573 |  | 
|---|
|  | 574 | # could arguably include routing (500) in the list, but ATM it doesn't | 
|---|
|  | 575 | # make sense, and in any case that shouldn't be structurally possible here. | 
|---|
|  | 576 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder <= 500 order by listorder"); | 
|---|
| [106] | 577 | $sth->execute; | 
|---|
| [517] | 578 | my @typelist; | 
|---|
|  | 579 | my $selflag = 0; | 
|---|
| [106] | 580 | while (my @data = $sth->fetchrow_array) { | 
|---|
| [517] | 581 | my %row = (tval => $data[0], | 
|---|
|  | 582 | type => $data[1], | 
|---|
|  | 583 | sel => ($selflag == 0 ? ' selected' : '') | 
|---|
|  | 584 | ); | 
|---|
|  | 585 | push (@typelist, \%row); | 
|---|
|  | 586 | $selflag++; | 
|---|
| [106] | 587 | } | 
|---|
| [517] | 588 | $page->param(typelist => \@typelist); | 
|---|
| [21] | 589 | } | 
|---|
| [517] | 590 |  | 
|---|
|  | 591 | my @cities; | 
|---|
| [92] | 592 | foreach my $city (@citylist) { | 
|---|
| [517] | 593 | my %row = (city => $city); | 
|---|
|  | 594 | push (@cities, \%row); | 
|---|
| [92] | 595 | } | 
|---|
| [517] | 596 | $page->param(citylist => \@cities); | 
|---|
| [4] | 597 |  | 
|---|
| [397] | 598 | ## node hack | 
|---|
|  | 599 | $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id"); | 
|---|
|  | 600 | $sth->execute() or print "DEBUG: failed retrieval from nodes: ".$sth->errstr,"<br>\n"; | 
|---|
| [517] | 601 | my @nodes; | 
|---|
| [397] | 602 | while (my ($nid,$nname) = $sth->fetchrow_array()) { | 
|---|
| [517] | 603 | my %row = (nid => $nid, nname => $nname); | 
|---|
|  | 604 | push (@nodes, \%row); | 
|---|
| [397] | 605 | } | 
|---|
| [517] | 606 | $page->param(nodelist => \@nodes); | 
|---|
| [397] | 607 | ## end node hack | 
|---|
|  | 608 |  | 
|---|
| [517] | 609 | $page->param(privdata => $IPDBacl{$authuser} =~ /s/); | 
|---|
| [284] | 610 |  | 
|---|
| [4] | 611 | } # assignBlock | 
|---|
|  | 612 |  | 
|---|
|  | 613 |  | 
|---|
|  | 614 | # Take info on requested IP assignment and see what we can provide. | 
|---|
|  | 615 | sub confirmAssign { | 
|---|
| [233] | 616 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| [517] | 617 | $aclerr = 'addblock'; | 
|---|
| [233] | 618 | return; | 
|---|
|  | 619 | } | 
|---|
| [4] | 620 |  | 
|---|
|  | 621 | my $cidr; | 
|---|
|  | 622 | my $alloc_from; | 
|---|
|  | 623 |  | 
|---|
|  | 624 | # Going to manually validate some items. | 
|---|
|  | 625 | # custid and city are automagic. | 
|---|
| [111] | 626 | return if !validateInput(); | 
|---|
| [4] | 627 |  | 
|---|
|  | 628 | # Several different cases here. | 
|---|
|  | 629 | # Static IP vs netblock | 
|---|
|  | 630 | #  + Different flavours of static IP | 
|---|
|  | 631 | #  + Different flavours of netblock | 
|---|
|  | 632 |  | 
|---|
| [157] | 633 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
| [4] | 634 | my ($base,undef) = split //, $webvar{alloctype};    # split into individual chars | 
|---|
|  | 635 |  | 
|---|
| [214] | 636 | # Ewww.  But it works. | 
|---|
|  | 637 | $sth = $ip_dbh->prepare("SELECT (SELECT city FROM allocations WHERE cidr=poolips.pool), ". | 
|---|
|  | 638 | "poolips.pool, COUNT(*) FROM poolips,allocations WHERE poolips.available='y' AND ". | 
|---|
| [442] | 639 | "poolips.pool=allocations.cidr AND allocations.city='$webvar{pop}' AND poolips.type LIKE '".$base."_' ". | 
|---|
| [214] | 640 | "GROUP BY pool"); | 
|---|
| [4] | 641 | $sth->execute; | 
|---|
|  | 642 | my $optionlist; | 
|---|
| [517] | 643 |  | 
|---|
|  | 644 | my @poollist; | 
|---|
|  | 645 | while (my ($poolcit,$poolblock,$poolfree) = $sth->fetchrow_array) { | 
|---|
| [214] | 646 | # city,pool cidr,free IP count | 
|---|
| [517] | 647 | if ($poolfree > 0) { | 
|---|
|  | 648 | my %row = (poolcit => $poolcit, poolblock => $poolblock, poolfree => $poolfree); | 
|---|
|  | 649 | push (@poollist, \%row); | 
|---|
| [214] | 650 | } | 
|---|
| [4] | 651 | } | 
|---|
| [517] | 652 | $page->param(staticip => 1); | 
|---|
|  | 653 | $page->param(poollist => \@poollist); | 
|---|
| [4] | 654 | $cidr = "Single static IP"; | 
|---|
| [517] | 655 | ##fixme:  need to handle "no available pools" | 
|---|
| [4] | 656 |  | 
|---|
|  | 657 | } else { # end show pool options | 
|---|
| [21] | 658 |  | 
|---|
|  | 659 | if ($webvar{fbassign} eq 'y') { | 
|---|
|  | 660 | $cidr = new NetAddr::IP $webvar{block}; | 
|---|
|  | 661 | $webvar{maskbits} = $cidr->masklen; | 
|---|
|  | 662 | } else { # done with direct freeblocks assignment | 
|---|
|  | 663 |  | 
|---|
|  | 664 | if (!$webvar{maskbits}) { | 
|---|
| [517] | 665 | $page->param(err => "Please specify a CIDR mask length."); | 
|---|
| [111] | 666 | return; | 
|---|
| [21] | 667 | } | 
|---|
|  | 668 | my $sql; | 
|---|
|  | 669 | my $city; | 
|---|
|  | 670 | my $failmsg; | 
|---|
| [320] | 671 | my $extracond = ''; | 
|---|
|  | 672 | if ($webvar{allocfrom} eq '-') { | 
|---|
|  | 673 | $extracond = ($webvar{allowpriv} eq 'on' ? '' : | 
|---|
|  | 674 | " and not (cidr <<= '192.168.0.0/16'". | 
|---|
|  | 675 | " or cidr <<= '10.0.0.0/8'". | 
|---|
|  | 676 | " or cidr <<= '172.16.0.0/12')"); | 
|---|
|  | 677 | } | 
|---|
|  | 678 | my $sortorder; | 
|---|
| [187] | 679 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| [31] | 680 | if ($webvar{allocfrom} ne '-') { | 
|---|
|  | 681 | $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'". | 
|---|
| [320] | 682 | " and cidr <<= '$webvar{allocfrom}'"; | 
|---|
|  | 683 | $sortorder = "maskbits desc"; | 
|---|
| [31] | 684 | } else { | 
|---|
| [320] | 685 | $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'"; | 
|---|
|  | 686 | $sortorder = "maskbits desc"; | 
|---|
| [31] | 687 | } | 
|---|
| [21] | 688 | $failmsg = "No suitable free block found.<br>\nWe do not have a free". | 
|---|
|  | 689 | " routeable block of that size.<br>\nYou will have to either route". | 
|---|
|  | 690 | " a set of smaller netblocks or a single smaller netblock."; | 
|---|
| [4] | 691 | } else { | 
|---|
| [118] | 692 | ##fixme | 
|---|
|  | 693 | # This section needs serious Pondering. | 
|---|
| [214] | 694 | # Pools of most types get assigned to the POP they're "routed from" | 
|---|
| [187] | 695 | # This includes WAN blocks and other netblock "containers" | 
|---|
| [214] | 696 | # This does NOT include cable pools. | 
|---|
|  | 697 | if ($webvar{alloctype} =~ /^.[pc]$/) { | 
|---|
| [37] | 698 | $city = $webvar{city}; | 
|---|
| [21] | 699 | $failmsg = "No suitable free block found.<br>\nYou will have to route another". | 
|---|
| [442] | 700 | " superblock from one of the<br>\nmaster blocks or chose a smaller". | 
|---|
| [21] | 701 | " block size for the pool."; | 
|---|
|  | 702 | } else { | 
|---|
| [517] | 703 | if (!$webvar{pop}) { | 
|---|
|  | 704 | $page->param(err => 'Please select a POP to route the block from/through.'); | 
|---|
|  | 705 | return; | 
|---|
|  | 706 | } | 
|---|
| [21] | 707 | $city = $webvar{pop}; | 
|---|
|  | 708 | $failmsg = "No suitable free block found.<br>\nYou will have to route another". | 
|---|
| [442] | 709 | " superblock to $webvar{pop}<br>\nfrom one of the master blocks or". | 
|---|
| [21] | 710 | " chose a smaller blocksize."; | 
|---|
|  | 711 | } | 
|---|
| [300] | 712 | if (defined $webvar{allocfrom} && $webvar{allocfrom} ne '-') { | 
|---|
| [157] | 713 | $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}". | 
|---|
| [186] | 714 | " and cidr <<= '$webvar{allocfrom}' and routed='". | 
|---|
| [320] | 715 | (($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'"; | 
|---|
|  | 716 | $sortorder = "maskbits desc,cidr"; | 
|---|
| [31] | 717 | } else { | 
|---|
| [157] | 718 | $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}". | 
|---|
| [320] | 719 | " and routed='".(($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'"; | 
|---|
|  | 720 | $sortorder = "maskbits desc,cidr"; | 
|---|
| [31] | 721 | } | 
|---|
| [4] | 722 | } | 
|---|
| [320] | 723 | $sql = $sql.$extracond." order by ".$sortorder; | 
|---|
| [21] | 724 | $sth = $ip_dbh->prepare($sql); | 
|---|
|  | 725 | $sth->execute; | 
|---|
|  | 726 | my @data = $sth->fetchrow_array(); | 
|---|
|  | 727 | if ($data[0] eq "") { | 
|---|
| [517] | 728 | $page->param(err => $failmsg); | 
|---|
| [111] | 729 | return; | 
|---|
| [21] | 730 | } | 
|---|
|  | 731 | $cidr = new NetAddr::IP $data[0]; | 
|---|
|  | 732 | } # check for freeblocks assignment or IPDB-controlled assignment | 
|---|
| [4] | 733 |  | 
|---|
| [517] | 734 | $alloc_from = "$cidr"; | 
|---|
| [4] | 735 |  | 
|---|
|  | 736 | # If the block to be allocated is smaller than the one we found, | 
|---|
|  | 737 | # figure out the "real" block to be allocated. | 
|---|
|  | 738 | if ($cidr->masklen() ne $webvar{maskbits}) { | 
|---|
|  | 739 | my $maskbits = $cidr->masklen(); | 
|---|
|  | 740 | my @subblocks; | 
|---|
|  | 741 | while ($maskbits++ < $webvar{maskbits}) { | 
|---|
|  | 742 | @subblocks = $cidr->split($maskbits); | 
|---|
|  | 743 | } | 
|---|
|  | 744 | $cidr = $subblocks[0]; | 
|---|
|  | 745 | } | 
|---|
| [157] | 746 | } # if ($webvar{alloctype} =~ /^.i$/) | 
|---|
| [4] | 747 |  | 
|---|
| [397] | 748 | ## node hack | 
|---|
|  | 749 | if ($webvar{node} && $webvar{node} ne '-') { | 
|---|
|  | 750 | $sth = $ip_dbh->prepare("SELECT node_name FROM nodes WHERE node_id=?"); | 
|---|
|  | 751 | $sth->execute($webvar{node}); | 
|---|
|  | 752 | my ($nodename) = $sth->fetchrow_array(); | 
|---|
| [517] | 753 | $page->param(nodename => $nodename); | 
|---|
|  | 754 | $page->param(nodeid => $webvar{node}); | 
|---|
| [397] | 755 | } | 
|---|
|  | 756 | ## end node hack | 
|---|
|  | 757 |  | 
|---|
| [4] | 758 | # Stick in the allocation data | 
|---|
| [517] | 759 | $page->param(alloc_type => $webvar{alloctype}); | 
|---|
|  | 760 | $page->param(typefull => $q->escapeHTML($disp_alloctypes{$webvar{alloctype}})); | 
|---|
|  | 761 | $page->param(alloc_from => $alloc_from); | 
|---|
|  | 762 | $page->param(cidr => $cidr); | 
|---|
|  | 763 | $page->param(city => $q->escapeHTML($webvar{city})); | 
|---|
|  | 764 | $page->param(custid => $webvar{custid}); | 
|---|
|  | 765 | $page->param(circid => $q->escapeHTML($webvar{circid})); | 
|---|
|  | 766 | $page->param(desc => $q->escapeHTML($webvar{desc})); | 
|---|
| [4] | 767 |  | 
|---|
| [517] | 768 | ##fixme: find a way to have the displayed copy have <br> substitutions | 
|---|
|  | 769 | # for newlines, and the <input> value have either encoded or bare newlines. | 
|---|
|  | 770 | # Also applies to privdata. | 
|---|
|  | 771 | $page->param(notes => $q->escapeHTML($webvar{notes},'y')); | 
|---|
|  | 772 |  | 
|---|
| [284] | 773 | # Check to see if user is allowed to do anything with sensitive data | 
|---|
|  | 774 | my $privdata = ''; | 
|---|
| [517] | 775 | $page->param(privdata => $q->escapeHTML($webvar{privdata},'y')) | 
|---|
|  | 776 | if $IPDBacl{$authuser} =~ /s/; | 
|---|
|  | 777 |  | 
|---|
|  | 778 | # Yay!  This now has it's very own little home. | 
|---|
|  | 779 | $page->param(billinguser => $webvar{userid}) | 
|---|
| [299] | 780 | if $webvar{userid}; | 
|---|
| [284] | 781 |  | 
|---|
| [517] | 782 | ##fixme:  this is only needed iff confirm.tmpl and | 
|---|
|  | 783 | # confirmRemove.tmpl are merged (quite possible, just | 
|---|
|  | 784 | # a little tedious) | 
|---|
|  | 785 | $page->param(action => "insert"); | 
|---|
| [284] | 786 |  | 
|---|
| [4] | 787 | } # end confirmAssign | 
|---|
|  | 788 |  | 
|---|
|  | 789 |  | 
|---|
|  | 790 | # Do the work of actually inserting a block in the database. | 
|---|
|  | 791 | sub insertAssign { | 
|---|
| [233] | 792 | if ($IPDBacl{$authuser} !~ /a/) { | 
|---|
| [517] | 793 | $aclerr = 'addblock'; | 
|---|
| [233] | 794 | return; | 
|---|
|  | 795 | } | 
|---|
| [4] | 796 | # Some things are done more than once. | 
|---|
| [111] | 797 | return if !validateInput(); | 
|---|
| [4] | 798 |  | 
|---|
| [284] | 799 | if (!defined($webvar{privdata})) { | 
|---|
|  | 800 | $webvar{privdata} = ''; | 
|---|
|  | 801 | } | 
|---|
| [106] | 802 | # $code is "success" vs "failure", $msg contains OK for a | 
|---|
|  | 803 | # successful netblock allocation, the IP allocated for static | 
|---|
|  | 804 | # IP, or the error message if an error occurred. | 
|---|
| [517] | 805 |  | 
|---|
| [106] | 806 | my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from}, | 
|---|
|  | 807 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes}, | 
|---|
| [397] | 808 | $webvar{circid}, $webvar{privdata}, $webvar{node}); | 
|---|
| [4] | 809 |  | 
|---|
| [111] | 810 | if ($code eq 'OK') { | 
|---|
| [106] | 811 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
| [300] | 812 | $msg =~ s|/32||; | 
|---|
| [517] | 813 | $page->param(staticip => $msg); | 
|---|
|  | 814 | $page->param(custid => $webvar{custid}); | 
|---|
|  | 815 | $page->param(billinguser => $webvar{billinguser}); | 
|---|
| [416] | 816 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation", | 
|---|
|  | 817 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n". | 
|---|
|  | 818 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n"); | 
|---|
| [106] | 819 | } else { | 
|---|
| [301] | 820 | my $netblock = new NetAddr::IP $webvar{fullcidr}; | 
|---|
| [517] | 821 | $page->param(fullcidr => $webvar{fullcidr}); | 
|---|
|  | 822 | $page->param(alloctype => $disp_alloctypes{$webvar{alloctype}}); | 
|---|
|  | 823 | $page->param(custid => $webvar{custid}); | 
|---|
|  | 824 | if ($webvar{alloctype} eq 'pr' && $webvar{billinguser}) { | 
|---|
|  | 825 | $page->param(billinguser => $webvar{billinguser}); | 
|---|
|  | 826 | $page->param(custid => $webvar{custid}); | 
|---|
|  | 827 | $page->param(netaddr => $netblock->addr); | 
|---|
|  | 828 | $page->param(masklen => $netblock->masklen); | 
|---|
|  | 829 | } | 
|---|
| [416] | 830 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation", | 
|---|
|  | 831 | "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n". | 
|---|
|  | 832 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n"); | 
|---|
| [4] | 833 | } | 
|---|
| [106] | 834 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ". | 
|---|
| [256] | 835 | "'$webvar{alloctype}' ($msg)"; | 
|---|
| [111] | 836 | } else { | 
|---|
|  | 837 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ". | 
|---|
|  | 838 | "'$webvar{alloctype}' by $authuser failed: '$msg'"; | 
|---|
| [517] | 839 | $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'". | 
|---|
| [157] | 840 | " failed:<br>\n$msg\n"); | 
|---|
| [106] | 841 | } | 
|---|
| [4] | 842 |  | 
|---|
|  | 843 | } # end insertAssign() | 
|---|
|  | 844 |  | 
|---|
|  | 845 |  | 
|---|
|  | 846 | # Does some basic checks on common input data to make sure nothing | 
|---|
|  | 847 | # *really* weird gets in to the database through this script. | 
|---|
|  | 848 | # Does NOT do complete input validation!!! | 
|---|
|  | 849 | sub validateInput { | 
|---|
|  | 850 | if ($webvar{city} eq '-') { | 
|---|
| [517] | 851 | $page->param(err => 'Please choose a city'); | 
|---|
| [111] | 852 | return; | 
|---|
| [4] | 853 | } | 
|---|
| [138] | 854 |  | 
|---|
|  | 855 | # Alloctype check. | 
|---|
| [4] | 856 | chomp $webvar{alloctype}; | 
|---|
| [138] | 857 | if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) { | 
|---|
|  | 858 | # Danger! Danger!  alloctype should ALWAYS be set by a dropdown.  Anyone | 
|---|
|  | 859 | # managing to call things in such a way as to cause this deserves a cryptic error. | 
|---|
| [517] | 860 | $page->param(err => 'Invalid alloctype'); | 
|---|
| [138] | 861 | return; | 
|---|
|  | 862 | } | 
|---|
|  | 863 |  | 
|---|
|  | 864 | # CustID check | 
|---|
| [4] | 865 | # We have different handling for customer allocations and "internal" or "our" allocations | 
|---|
| [214] | 866 | if ($def_custids{$webvar{alloctype}} eq '') { | 
|---|
| [4] | 867 | if (!$webvar{custid}) { | 
|---|
| [517] | 868 | $page->param(err => 'Please enter a customer ID.'); | 
|---|
| [111] | 869 | return; | 
|---|
| [4] | 870 | } | 
|---|
| [400] | 871 | if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) { | 
|---|
|  | 872 | # Force uppercase for now... | 
|---|
|  | 873 | $webvar{custid} =~ tr/a-z/A-Z/; | 
|---|
|  | 874 | # Crosscheck with billing. | 
|---|
|  | 875 | my $status = CustIDCK->custid_exist($webvar{custid}); | 
|---|
|  | 876 | if ($CustIDCK::Error) { | 
|---|
| [517] | 877 | $page->param(err => "Error verifying customer ID: ".$CustIDCK::ErrMsg); | 
|---|
| [400] | 878 | return; | 
|---|
|  | 879 | } | 
|---|
|  | 880 | if (!$status) { | 
|---|
| [517] | 881 | $page->param(err => "Customer ID not valid.  Make sure the Customer ID ". | 
|---|
| [420] | 882 | "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ". | 
|---|
| [400] | 883 | "non-customer assignments."); | 
|---|
|  | 884 | return; | 
|---|
|  | 885 | } | 
|---|
| [4] | 886 | } | 
|---|
| [400] | 887 | #    print "<!-- [ In validateInput().  Insert customer ID cross-check here. ] -->\n"; | 
|---|
| [138] | 888 | } else { | 
|---|
| [167] | 889 | # New!  Improved!  And now Loaded From The Database!! | 
|---|
| [320] | 890 | if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) { | 
|---|
|  | 891 | $webvar{custid} = $def_custids{$webvar{alloctype}}; | 
|---|
|  | 892 | } | 
|---|
| [4] | 893 | } | 
|---|
| [111] | 894 |  | 
|---|
|  | 895 | # Check POP location | 
|---|
|  | 896 | my $flag; | 
|---|
| [187] | 897 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| [111] | 898 | $flag = 'for a routed netblock'; | 
|---|
|  | 899 | foreach (@poplist) { | 
|---|
|  | 900 | if (/^$webvar{city}$/) { | 
|---|
|  | 901 | $flag = 'n'; | 
|---|
|  | 902 | last; | 
|---|
|  | 903 | } | 
|---|
|  | 904 | } | 
|---|
|  | 905 | } else { | 
|---|
|  | 906 | $flag = 'n'; | 
|---|
| [442] | 907 | ##fixme:  hook to force-set POP or city on certain alloctypes | 
|---|
|  | 908 | # if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; } | 
|---|
| [400] | 909 | if ($webvar{pop} =~ /^-$/) { | 
|---|
| [111] | 910 | $flag = 'to route the block from/through'; | 
|---|
|  | 911 | } | 
|---|
|  | 912 | } | 
|---|
| [517] | 913 |  | 
|---|
|  | 914 | # if the alloctype has a restricted city/POP list as determined above, | 
|---|
|  | 915 | # and the reqested city/POP does not match that list, complain | 
|---|
| [111] | 916 | if ($flag ne 'n') { | 
|---|
| [517] | 917 | $page->param(err => "Please choose a valid POP location $flag.  Valid ". | 
|---|
| [111] | 918 | "POP locations are currently:<br>\n".join (" - ", @poplist)); | 
|---|
|  | 919 | return; | 
|---|
|  | 920 | } | 
|---|
|  | 921 |  | 
|---|
|  | 922 | return 'OK'; | 
|---|
| [4] | 923 | } # end validateInput | 
|---|
|  | 924 |  | 
|---|
|  | 925 |  | 
|---|
|  | 926 | # Displays details of a specific allocation in a form | 
|---|
|  | 927 | # Allows update/delete | 
|---|
|  | 928 | # action=edit | 
|---|
|  | 929 | sub edit { | 
|---|
|  | 930 |  | 
|---|
|  | 931 | my $sql; | 
|---|
|  | 932 |  | 
|---|
|  | 933 | # Two cases:  block is a netblock, or block is a static IP from a pool | 
|---|
|  | 934 | # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data | 
|---|
| [517] | 935 | ##fixme:  allow "SWIP" (publication to rWHOIS) of static IP data | 
|---|
| [4] | 936 | if ($webvar{block} =~ /\/32$/) { | 
|---|
| [455] | 937 | $sql = "select ip,custid,type,city,circuitid,description,notes,modifystamp,privdata from poolips where ip='$webvar{block}'"; | 
|---|
| [4] | 938 | } else { | 
|---|
| [455] | 939 | $sql = "select cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,swip from allocations where cidr='$webvar{block}'" | 
|---|
| [4] | 940 | } | 
|---|
|  | 941 |  | 
|---|
|  | 942 | # gotta snag block info from db | 
|---|
|  | 943 | $sth = $ip_dbh->prepare($sql); | 
|---|
|  | 944 | $sth->execute; | 
|---|
|  | 945 | my @data = $sth->fetchrow_array; | 
|---|
|  | 946 |  | 
|---|
|  | 947 | # Clean up extra whitespace on alloc type | 
|---|
|  | 948 | $data[2] =~ s/\s//; | 
|---|
|  | 949 |  | 
|---|
|  | 950 | # We can't let the city be changed here;  this block is a part of | 
|---|
|  | 951 | # a larger routed allocation and therefore by definition can't be moved. | 
|---|
|  | 952 | # block and city are static. | 
|---|
|  | 953 | ##fixme | 
|---|
|  | 954 | # Needs thinking.  Have to allow changes to city to correct errors, no? | 
|---|
| [517] | 955 | # Also have areas where a routed block at a POP serves "many" cities/towns/named crossroads | 
|---|
| [4] | 956 |  | 
|---|
| [517] | 957 | # @data: cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,swip | 
|---|
| [233] | 958 |  | 
|---|
| [517] | 959 | $page->param(block => $webvar{block}); | 
|---|
| [4] | 960 |  | 
|---|
| [517] | 961 | $page->param(custid => $data[1]); | 
|---|
|  | 962 | $page->param(city => $data[3]); | 
|---|
|  | 963 | $page->param(circid => $data[4]); | 
|---|
|  | 964 | $page->param(desc => $data[5]); | 
|---|
|  | 965 | $page->param(notes => $data[6]); | 
|---|
| [4] | 966 |  | 
|---|
| [187] | 967 | ##fixme The check here should be built from the database | 
|---|
| [517] | 968 | # Need to expand to support pool types too | 
|---|
|  | 969 | if ($data[2] =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) { | 
|---|
|  | 970 | $page->param(changetype => 1); | 
|---|
|  | 971 | $page->param(alloctype => [ | 
|---|
|  | 972 | { selme => ($data[2] eq 'me'), type => "me", disptype => "Dialup netblock" }, | 
|---|
|  | 973 | { selme => ($data[2] eq 'de'), type => "de", disptype => "Dynamic DSL netblock" }, | 
|---|
|  | 974 | { selme => ($data[2] eq 'ce'), type => "ce", disptype => "Dynamic cable netblock" }, | 
|---|
|  | 975 | { selme => ($data[2] eq 'we'), type => "we", disptype => "Dynamic wireless netblock" }, | 
|---|
|  | 976 | { selme => ($data[2] eq 'cn'), type => "cn", disptype => "Customer netblock" }, | 
|---|
|  | 977 | { selme => ($data[2] eq 'en'), type => "en", disptype => "End-use netblock" }, | 
|---|
|  | 978 | { selme => ($data[2] eq 'in'), type => "in", disptype => "Internal netblock" }, | 
|---|
|  | 979 | ] | 
|---|
|  | 980 | ); | 
|---|
|  | 981 | } else { | 
|---|
|  | 982 | $page->param(disptype => $disp_alloctypes{$data[2]}); | 
|---|
|  | 983 | $page->param(type => $data[2]); | 
|---|
|  | 984 | } | 
|---|
|  | 985 |  | 
|---|
| [397] | 986 | ## node hack | 
|---|
| [517] | 987 | $sth = $ip_dbh->prepare("SELECT nodes.node_id,node_name FROM nodes INNER JOIN noderef". | 
|---|
|  | 988 | " ON nodes.node_id=noderef.node_id WHERE noderef.block='$webvar{block}'"); | 
|---|
| [397] | 989 | $sth->execute; | 
|---|
| [517] | 990 | my ($nodeid,$nodename) = $sth->fetchrow_array(); | 
|---|
|  | 991 | $page->param(havenodeid => $nodeid); | 
|---|
|  | 992 |  | 
|---|
|  | 993 | if ($data[2] eq 'fr' || $data[2] eq 'bi') { | 
|---|
|  | 994 | $page->param(typesupportsnodes => 1); | 
|---|
|  | 995 | $page->param(nodename => $nodename); | 
|---|
|  | 996 |  | 
|---|
|  | 997 | ##fixme:  this whole hack needs cleanup and generalization for all alloctypes | 
|---|
|  | 998 | ##fixme:  arguably a bug that presence of a nodeid implies it can be changed.. | 
|---|
|  | 999 | #  but except for manual database changes, only the two types fr and bi can | 
|---|
|  | 1000 | #  (currently) have a nodeid set in the first place. | 
|---|
|  | 1001 | if ($IPDBacl{$authuser} =~ /c/) { | 
|---|
| [397] | 1002 | $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id"); | 
|---|
| [517] | 1003 | $sth->execute; | 
|---|
|  | 1004 | my @nodelist; | 
|---|
| [397] | 1005 | while (my ($nid,$nname) = $sth->fetchrow_array()) { | 
|---|
| [517] | 1006 | my %row = ( | 
|---|
|  | 1007 | selme => ($nodeid == $nid), | 
|---|
|  | 1008 | nodeid => $nid, | 
|---|
|  | 1009 | nodename => $nname, | 
|---|
|  | 1010 | ); | 
|---|
|  | 1011 | push (@nodelist, \%row); | 
|---|
| [397] | 1012 | } | 
|---|
| [517] | 1013 | $page->param(nodelist => \@nodelist); | 
|---|
| [397] | 1014 | } | 
|---|
|  | 1015 | } | 
|---|
|  | 1016 | ## end node hack | 
|---|
| [517] | 1017 |  | 
|---|
| [255] | 1018 | my ($lastmod,undef) = split /\s+/, $data[7]; | 
|---|
| [517] | 1019 | $page->param(lastmod => $lastmod); | 
|---|
| [33] | 1020 |  | 
|---|
| [517] | 1021 | # not happy with the upside-down logic, but... | 
|---|
|  | 1022 | $page->param(swipable => $data[2] !~ /.i/); | 
|---|
|  | 1023 | $page->param(swip => $data[10] ne 'n'); | 
|---|
| [320] | 1024 |  | 
|---|
| [284] | 1025 | # Check to see if we can display sensitive data | 
|---|
| [517] | 1026 | $page->param(nocling => $IPDBacl{$authuser} =~ /s/); | 
|---|
|  | 1027 | $page->param(privdata => $data[8]); | 
|---|
| [284] | 1028 |  | 
|---|
| [517] | 1029 | # ACL trickery - these two template booleans control the presence of all form/input tags | 
|---|
|  | 1030 | $page->param(maychange => $IPDBacl{$authuser} =~ /c/); | 
|---|
|  | 1031 | $page->param(maydel => $IPDBacl{$authuser} =~ /d/); | 
|---|
| [4] | 1032 |  | 
|---|
|  | 1033 | } # edit() | 
|---|
|  | 1034 |  | 
|---|
|  | 1035 |  | 
|---|
|  | 1036 | # Stuff new info about a block into the db | 
|---|
|  | 1037 | # action=update | 
|---|
|  | 1038 | sub update { | 
|---|
| [284] | 1039 | if ($IPDBacl{$authuser} !~ /c/) { | 
|---|
| [517] | 1040 | $aclerr = 'updateblock'; | 
|---|
| [284] | 1041 | return; | 
|---|
|  | 1042 | } | 
|---|
| [4] | 1043 |  | 
|---|
| [284] | 1044 | # Check to see if we can update restricted data | 
|---|
|  | 1045 | my $privdata = ''; | 
|---|
|  | 1046 | if ($IPDBacl{$authuser} =~ /s/) { | 
|---|
|  | 1047 | $privdata = ",privdata='$webvar{privdata}'"; | 
|---|
|  | 1048 | } | 
|---|
|  | 1049 |  | 
|---|
| [4] | 1050 | # Make sure incoming data is in correct format - custID among other things. | 
|---|
| [228] | 1051 | return if !validateInput; | 
|---|
| [4] | 1052 |  | 
|---|
|  | 1053 | # SQL transaction wrapper | 
|---|
|  | 1054 | eval { | 
|---|
|  | 1055 | # Relatively simple SQL transaction here. | 
|---|
|  | 1056 | my $sql; | 
|---|
| [157] | 1057 | if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) { | 
|---|
| [517] | 1058 | $sql = "UPDATE poolips SET custid='$webvar{custid}',". | 
|---|
|  | 1059 | "city=?,description=?,notes=?,". | 
|---|
|  | 1060 | "circuitid='$webvar{circid}',". | 
|---|
| [284] | 1061 | "$privdata where ip='$webvar{block}'"; | 
|---|
| [4] | 1062 | } else { | 
|---|
| [517] | 1063 | $sql = "UPDATE allocations SET custid='$webvar{custid}',". | 
|---|
|  | 1064 | "city=?,description=?,notes=?,". | 
|---|
|  | 1065 | "circuitid='$webvar{circid}'$privdata,". | 
|---|
|  | 1066 | "type='$webvar{alloctype}',". | 
|---|
| [320] | 1067 | "swip='".($webvar{swip} eq 'on' ? 'y' : 'n')."' ". | 
|---|
| [284] | 1068 | "where cidr='$webvar{block}'"; | 
|---|
| [4] | 1069 | } | 
|---|
| [157] | 1070 | # Log the details of the change. | 
|---|
|  | 1071 | syslog "debug", $sql; | 
|---|
| [4] | 1072 | $sth = $ip_dbh->prepare($sql); | 
|---|
| [517] | 1073 | $sth->execute($webvar{city}, $webvar{desc}, $webvar{notes}); | 
|---|
| [397] | 1074 | ## node hack | 
|---|
|  | 1075 | if ($webvar{node}) { | 
|---|
| [517] | 1076 | # done with delete/insert so we don't have to worry about funkyness updating a node ref that isn't there | 
|---|
| [397] | 1077 | $ip_dbh->do("DELETE FROM noderef WHERE block='$webvar{block}'"); | 
|---|
|  | 1078 | $sth = $ip_dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)"); | 
|---|
|  | 1079 | $sth->execute($webvar{block},$webvar{node}); | 
|---|
|  | 1080 | } | 
|---|
|  | 1081 | ## end node hack | 
|---|
| [4] | 1082 | $ip_dbh->commit; | 
|---|
|  | 1083 | }; | 
|---|
|  | 1084 | if ($@) { | 
|---|
| [166] | 1085 | my $msg = $@; | 
|---|
| [4] | 1086 | eval { $ip_dbh->rollback; }; | 
|---|
| [166] | 1087 | syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'"; | 
|---|
| [517] | 1088 | $page->param(err => "Could not update block/IP $webvar{block}: $msg"); | 
|---|
| [111] | 1089 | return; | 
|---|
| [4] | 1090 | } | 
|---|
|  | 1091 |  | 
|---|
|  | 1092 | # If we get here, the operation succeeded. | 
|---|
|  | 1093 | syslog "notice", "$authuser updated $webvar{block}"; | 
|---|
| [416] | 1094 | ##fixme:  need to wedge something in to allow "update:field" notifications | 
|---|
|  | 1095 | ## hmm.  how to tell what changed?  O_o | 
|---|
| [426] | 1096 | mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}", | 
|---|
| [416] | 1097 | "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on'; | 
|---|
| [4] | 1098 |  | 
|---|
| [517] | 1099 | ## node hack | 
|---|
|  | 1100 | if ($webvar{node} && $webvar{node} ne '-') { | 
|---|
|  | 1101 | $sth = $ip_dbh->prepare("SELECT node_name FROM nodes WHERE node_id=?"); | 
|---|
|  | 1102 | $sth->execute($webvar{node}); | 
|---|
|  | 1103 | my ($nodename) = $sth->fetchrow_array(); | 
|---|
|  | 1104 | $page->param(nodename => $nodename); | 
|---|
|  | 1105 | } | 
|---|
|  | 1106 | ## end node hack | 
|---|
|  | 1107 |  | 
|---|
| [380] | 1108 | # Link back to browse-routed or list-pool page on "Update complete" page. | 
|---|
|  | 1109 | my $cblock;   # to contain the CIDR of the container block we're retrieving. | 
|---|
|  | 1110 | my $sql; | 
|---|
|  | 1111 | if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) { | 
|---|
| [517] | 1112 | $page->param(backpool => 1); | 
|---|
| [380] | 1113 | $sql = "select pool from poolips where ip='$webvar{block}'"; | 
|---|
|  | 1114 | } else { | 
|---|
|  | 1115 | $sql = "select cidr from routed where cidr >>= '$webvar{block}'"; | 
|---|
|  | 1116 | } | 
|---|
|  | 1117 | # I define there to be no errors on this operation...  so we don't need to check for them. | 
|---|
|  | 1118 | $sth = $ip_dbh->prepare($sql); | 
|---|
|  | 1119 | $sth->execute; | 
|---|
|  | 1120 | $sth->bind_columns(\$cblock); | 
|---|
|  | 1121 | $sth->fetch(); | 
|---|
|  | 1122 | $sth->finish; | 
|---|
| [517] | 1123 | $page->param(backblock => $cblock); | 
|---|
| [380] | 1124 |  | 
|---|
| [517] | 1125 | $page->param(cidr => $webvar{block}); | 
|---|
|  | 1126 | $page->param(city => $webvar{city}); | 
|---|
|  | 1127 | $page->param(disptype => $disp_alloctypes{$webvar{alloctype}}); | 
|---|
|  | 1128 | $page->param(custid => $webvar{custid}); | 
|---|
|  | 1129 | $page->param(swip => $webvar{swip} eq 'on' ? 'Yes' : 'No'); | 
|---|
|  | 1130 | $page->param(circid => $q->escapeHTML($webvar{circid})); | 
|---|
|  | 1131 | $page->param(desc => $q->escapeHTML($webvar{desc})); | 
|---|
|  | 1132 | $page->param(notes => $q->escapeHTML($webvar{notes})); | 
|---|
|  | 1133 | $webvar{privdata} = ($webvar{privdata} ? $q->escapeHTML($webvar{privdata}) : " "); | 
|---|
|  | 1134 | $page->param(privdata => $webvar{privdata}) | 
|---|
|  | 1135 | if $IPDBacl{$authuser} =~ /s/; | 
|---|
| [4] | 1136 |  | 
|---|
|  | 1137 | } # update() | 
|---|
|  | 1138 |  | 
|---|
|  | 1139 |  | 
|---|
|  | 1140 | # Delete an allocation. | 
|---|
| [106] | 1141 | sub remove { | 
|---|
| [233] | 1142 | if ($IPDBacl{$authuser} !~ /d/) { | 
|---|
| [517] | 1143 | $aclerr = 'delblock'; | 
|---|
| [233] | 1144 | return; | 
|---|
|  | 1145 | } | 
|---|
|  | 1146 |  | 
|---|
| [4] | 1147 | # Serves'em right for getting here... | 
|---|
|  | 1148 | if (!defined($webvar{block})) { | 
|---|
| [517] | 1149 | $page->param(err => "Can't delete a block that doesn't exist"); | 
|---|
| [111] | 1150 | return; | 
|---|
| [4] | 1151 | } | 
|---|
|  | 1152 |  | 
|---|
| [284] | 1153 | my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata); | 
|---|
| [4] | 1154 |  | 
|---|
| [187] | 1155 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| [4] | 1156 | $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'"); | 
|---|
|  | 1157 | $sth->execute(); | 
|---|
|  | 1158 |  | 
|---|
|  | 1159 | # This feels...  extreme. | 
|---|
|  | 1160 | croak $sth->errstr() if($sth->errstr()); | 
|---|
|  | 1161 |  | 
|---|
|  | 1162 | $sth->bind_columns(\$cidr,\$city); | 
|---|
|  | 1163 | $sth->execute(); | 
|---|
|  | 1164 | $sth->fetch || croak $sth->errstr(); | 
|---|
|  | 1165 | $custid = "N/A"; | 
|---|
|  | 1166 | $alloctype = $webvar{alloctype}; | 
|---|
| [74] | 1167 | $circid = "N/A"; | 
|---|
| [4] | 1168 | $desc = "N/A"; | 
|---|
|  | 1169 | $notes = "N/A"; | 
|---|
| [517] | 1170 | $privdata = "N/A"; | 
|---|
| [4] | 1171 |  | 
|---|
|  | 1172 | } elsif ($webvar{alloctype} eq 'mm') { | 
|---|
| [517] | 1173 |  | 
|---|
| [4] | 1174 | $cidr = $webvar{block}; | 
|---|
|  | 1175 | $city = "N/A"; | 
|---|
|  | 1176 | $custid = "N/A"; | 
|---|
|  | 1177 | $alloctype = $webvar{alloctype}; | 
|---|
| [74] | 1178 | $circid = "N/A"; | 
|---|
| [4] | 1179 | $desc = "N/A"; | 
|---|
|  | 1180 | $notes = "N/A"; | 
|---|
| [517] | 1181 | $privdata = "N/A"; | 
|---|
|  | 1182 |  | 
|---|
| [189] | 1183 | } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m | 
|---|
| [4] | 1184 |  | 
|---|
|  | 1185 | # Unassigning a static IP | 
|---|
| [284] | 1186 | my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata". | 
|---|
|  | 1187 | " from poolips where ip='$webvar{block}'"); | 
|---|
| [4] | 1188 | $sth->execute(); | 
|---|
|  | 1189 | #  croak $sth->errstr() if($sth->errstr()); | 
|---|
|  | 1190 |  | 
|---|
| [284] | 1191 | $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid, | 
|---|
|  | 1192 | \$privdata); | 
|---|
| [4] | 1193 | $sth->fetch() || croak $sth->errstr; | 
|---|
|  | 1194 |  | 
|---|
| [157] | 1195 | } else { # done with alloctype=~ /^.i$/ | 
|---|
| [4] | 1196 |  | 
|---|
| [284] | 1197 | my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata". | 
|---|
|  | 1198 | " from allocations where cidr='$webvar{block}'"); | 
|---|
| [4] | 1199 | $sth->execute(); | 
|---|
|  | 1200 | #       croak $sth->errstr() if($sth->errstr()); | 
|---|
|  | 1201 |  | 
|---|
| [284] | 1202 | $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc, | 
|---|
|  | 1203 | \$notes, \$privdata); | 
|---|
| [74] | 1204 | $sth->fetch() || carp $sth->errstr; | 
|---|
| [4] | 1205 | } # end cases for different alloctypes | 
|---|
|  | 1206 |  | 
|---|
| [517] | 1207 | $page->param(block => $cidr); | 
|---|
|  | 1208 | $page->param(disptype => $disp_alloctypes{$alloctype}); | 
|---|
|  | 1209 | $page->param(type => $alloctype); | 
|---|
|  | 1210 | $page->param(city => $city); | 
|---|
|  | 1211 | $page->param(custid => $custid); | 
|---|
|  | 1212 | $page->param(circid => $circid); | 
|---|
|  | 1213 | $page->param(desc => $desc); | 
|---|
|  | 1214 | $page->param(notes => $notes); | 
|---|
|  | 1215 | $privdata = ' ' if $privdata eq ''; | 
|---|
|  | 1216 | $page->param(privdata => $privdata) if $IPDBacl{$authuser} =~ /s/; | 
|---|
|  | 1217 | $page->param(delpool => $alloctype =~ /^.[pd]$/); | 
|---|
| [4] | 1218 |  | 
|---|
| [517] | 1219 | } # end remove() | 
|---|
| [4] | 1220 |  | 
|---|
|  | 1221 |  | 
|---|
|  | 1222 | # Delete an allocation.  Return it to the freeblocks table;  munge | 
|---|
|  | 1223 | # data as necessary to keep as few records as possible in freeblocks | 
|---|
|  | 1224 | # to prevent weirdness when allocating blocks later. | 
|---|
|  | 1225 | # Remove IPs from pool listing if necessary | 
|---|
|  | 1226 | sub finalDelete { | 
|---|
| [233] | 1227 | if ($IPDBacl{$authuser} !~ /d/) { | 
|---|
| [517] | 1228 | $aclerr = 'delblock'; | 
|---|
| [233] | 1229 | return; | 
|---|
|  | 1230 | } | 
|---|
| [4] | 1231 |  | 
|---|
| [370] | 1232 | # need to retrieve block data before deleting so we can notify on that | 
|---|
|  | 1233 | my ($cidr,$custid,$type,$city,$description) = getBlockData($ip_dbh, $webvar{block}); | 
|---|
|  | 1234 |  | 
|---|
| [106] | 1235 | my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype}); | 
|---|
| [4] | 1236 |  | 
|---|
| [517] | 1237 | $page->param(block => $webvar{block}); | 
|---|
| [106] | 1238 | if ($code eq 'OK') { | 
|---|
| [370] | 1239 | syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}". | 
|---|
|  | 1240 | " $custid, $city, desc='$description'"; | 
|---|
| [416] | 1241 | mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}", | 
|---|
|  | 1242 | "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n". | 
|---|
|  | 1243 | "CustID: $custid\nCity: $city\nDescription: $description\n"); | 
|---|
| [106] | 1244 | } else { | 
|---|
| [517] | 1245 | $page->param(failmsg => $msg); | 
|---|
| [106] | 1246 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
|  | 1247 | syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'"; | 
|---|
|  | 1248 | } else { | 
|---|
|  | 1249 | syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'"; | 
|---|
| [517] | 1250 | $page->param(netblock => 1); | 
|---|
| [4] | 1251 | } | 
|---|
| [106] | 1252 | } | 
|---|
| [4] | 1253 |  | 
|---|
|  | 1254 | } # finalDelete | 
|---|