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