| [4] | 1 | #!/usr/bin/perl
 | 
|---|
 | 2 | # ipdb/cgi-bin/main.cgi
 | 
|---|
| [8] | 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 | ###
 | 
|---|
| [417] | 9 | # Copyright (C) 2004-2010 - Kris Deugau
 | 
|---|
| [4] | 10 | 
 | 
|---|
 | 11 | use strict;             
 | 
|---|
 | 12 | use warnings;   
 | 
|---|
 | 13 | use CGI::Carp qw(fatalsToBrowser);
 | 
|---|
| [447] | 14 | use CGI::Simple;
 | 
|---|
| [449] | 15 | use HTML::Template;
 | 
|---|
| [4] | 16 | use DBI;
 | 
|---|
 | 17 | use CommonWeb qw(:ALL);
 | 
|---|
| [400] | 18 | use CustIDCK;
 | 
|---|
| [4] | 19 | use POSIX qw(ceil);
 | 
|---|
 | 20 | use NetAddr::IP;
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | use Sys::Syslog;
 | 
|---|
 | 23 | 
 | 
|---|
| [417] | 24 | # don't remove!  required for GNU/FHS-ish install from tarball
 | 
|---|
 | 25 | ##uselib##
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 | use MyIPDB;
 | 
|---|
 | 28 | 
 | 
|---|
| [431] | 29 | openlog "IPDB","pid","$IPDB::syslog_facility";
 | 
|---|
| [4] | 30 | 
 | 
|---|
| [478] | 31 | ## Environment.  Collect some things, process some things, set some things...
 | 
|---|
 | 32 | 
 | 
|---|
| [233] | 33 | # Collect the username from HTTP auth.  If undefined, we're in
 | 
|---|
 | 34 | # a test environment, or called without a username.
 | 
|---|
| [4] | 35 | my $authuser;
 | 
|---|
 | 36 | if (!defined($ENV{'REMOTE_USER'})) {
 | 
|---|
 | 37 |   $authuser = '__temptest';
 | 
|---|
 | 38 | } else {
 | 
|---|
 | 39 |   $authuser = $ENV{'REMOTE_USER'};
 | 
|---|
 | 40 | }
 | 
|---|
 | 41 | 
 | 
|---|
| [478] | 42 | # anyone got a better name?  :P
 | 
|---|
 | 43 | my $thingroot = $ENV{SCRIPT_FILENAME};
 | 
|---|
 | 44 | $thingroot =~ s|cgi-bin/main.cgi||;
 | 
|---|
 | 45 | 
 | 
|---|
| [402] | 46 | syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
 | 
|---|
| [4] | 47 | 
 | 
|---|
| [479] | 48 | ##fixme there *must* be a better order to do things in so this can go back where it was
 | 
|---|
 | 49 | # CGI fiddling done here so we can declare %webvar so we can alter $webvar{action}
 | 
|---|
 | 50 | # to show the right page on DB errors.
 | 
|---|
 | 51 | # Set up the CGI object...
 | 
|---|
 | 52 | my $q = new CGI::Simple;
 | 
|---|
 | 53 | # ... and get query-string params as well as POST params if necessary
 | 
|---|
 | 54 | $q->parse_query_string;
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | # Convenience;  saves changing all references to %webvar
 | 
|---|
 | 57 | ##fixme:  tweak for handling <select multiple='y' size=3> (list with multiple selection)
 | 
|---|
 | 58 | my %webvar = $q->Vars;
 | 
|---|
 | 59 | 
 | 
|---|
| [106] | 60 | # Why not a global DB handle?  (And a global statement handle, as well...)
 | 
|---|
 | 61 | # Use the connectDB function, otherwise we end up confusing ourselves
 | 
|---|
 | 62 | my $ip_dbh;
 | 
|---|
 | 63 | my $sth;
 | 
|---|
 | 64 | my $errstr;
 | 
|---|
| [142] | 65 | ($ip_dbh,$errstr) = connectDB_My;
 | 
|---|
| [106] | 66 | if (!$ip_dbh) {
 | 
|---|
| [479] | 67 |   $webvar{action} = "dberr";
 | 
|---|
 | 68 | } else {
 | 
|---|
 | 69 |   initIPDBGlobals($ip_dbh);
 | 
|---|
| [106] | 70 | }
 | 
|---|
| [4] | 71 | 
 | 
|---|
| [449] | 72 | # Set up some globals
 | 
|---|
| [478] | 73 | $ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
 | 
|---|
| [449] | 74 | 
 | 
|---|
| [451] | 75 | my $header = HTML::Template->new(filename => "header.tmpl");
 | 
|---|
| [463] | 76 | my $footer = HTML::Template->new(filename => "footer.tmpl");
 | 
|---|
 | 77 | 
 | 
|---|
| [451] | 78 | $header->param(version => $IPDB::VERSION);
 | 
|---|
 | 79 | $header->param(addperm => $IPDBacl{$authuser} =~ /a/);
 | 
|---|
 | 80 | print "Content-type: text/html\n\n", $header->output;
 | 
|---|
| [233] | 81 | 
 | 
|---|
| [4] | 82 | 
 | 
|---|
 | 83 | #main()
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 | if(!defined($webvar{action})) {
 | 
|---|
| [450] | 86 |   $webvar{action} = "index";    #shuts up the warnings.
 | 
|---|
| [4] | 87 | }
 | 
|---|
 | 88 | 
 | 
|---|
| [450] | 89 | my $page = HTML::Template->new(filename => "$webvar{action}.tmpl");
 | 
|---|
 | 90 | 
 | 
|---|
| [4] | 91 | if($webvar{action} eq 'index') {
 | 
|---|
 | 92 |   showSummary();
 | 
|---|
| [233] | 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 |   }
 | 
|---|
| [4] | 97 | } elsif ($webvar{action} eq 'newmaster') {
 | 
|---|
 | 98 | 
 | 
|---|
| [233] | 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};
 | 
|---|
| [473] | 103 |     $page->param(cidr => "$cidr");
 | 
|---|
| [4] | 104 | 
 | 
|---|
| [371] | 105 |     my ($code,$msg) = addMaster($ip_dbh, $webvar{cidr});
 | 
|---|
| [4] | 106 | 
 | 
|---|
| [371] | 107 |     if ($code eq 'FAIL') {
 | 
|---|
| [320] | 108 |       syslog "err", "Could not add master block '$webvar{cidr}' to database: '$msg'";
 | 
|---|
| [473] | 109 |       $page->param(err => $msg);
 | 
|---|
| [233] | 110 |     } else {
 | 
|---|
 | 111 |       syslog "info", "$authuser added master block $webvar{cidr}";
 | 
|---|
 | 112 |     }
 | 
|---|
| [4] | 113 | 
 | 
|---|
| [233] | 114 |   } # ACL check
 | 
|---|
 | 115 | 
 | 
|---|
| [4] | 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 | }
 | 
|---|
| [397] | 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";
 | 
|---|
| [477] | 153 |   my @nodelist;
 | 
|---|
| [397] | 154 |   while (my ($nid,$nname) = $sth->fetchrow_array()) {
 | 
|---|
| [477] | 155 |     my %row = (nodeid => $nid, nodename => $nname);
 | 
|---|
 | 156 |     push @nodelist, \%row;
 | 
|---|
| [397] | 157 |   }
 | 
|---|
| [477] | 158 |   $page->param(nodelist => \@nodelist);
 | 
|---|
| [397] | 159 | }
 | 
|---|
 | 160 | 
 | 
|---|
| [479] | 161 | # DB failure.  Can't do much here, really.
 | 
|---|
 | 162 | elsif ($webvar{action} eq 'dberr') {
 | 
|---|
 | 163 |   $page->param(errmsg => $errstr);
 | 
|---|
 | 164 | }
 | 
|---|
 | 165 | 
 | 
|---|
| [4] | 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 | }
 | 
|---|
| [111] | 178 | ## Finally! Done with that NASTY "case" emulation!
 | 
|---|
| [4] | 179 | 
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 | 
 | 
|---|
| [106] | 182 | # Clean up IPDB globals, DB handle, etc.
 | 
|---|
| [111] | 183 | finish($ip_dbh);
 | 
|---|
| [199] | 184 | 
 | 
|---|
| [449] | 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/;
 | 
|---|
| [199] | 188 | 
 | 
|---|
| [463] | 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 | 
 | 
|---|
| [450] | 194 | print $page->output;
 | 
|---|
 | 195 | 
 | 
|---|
| [449] | 196 | # include the admin tools link in the output?
 | 
|---|
 | 197 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
 | 
|---|
 | 198 | 
 | 
|---|
 | 199 | print $footer->output;
 | 
|---|
 | 200 | 
 | 
|---|
| [106] | 201 | # Just in case something waaaayyy down isn't in place
 | 
|---|
 | 202 | # properly... we exit explicitly.
 | 
|---|
| [479] | 203 | exit 0;
 | 
|---|
| [4] | 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 | 
 | 
|---|
| [135] | 218 | ELEMENT:  foreach my $element (@$rowRef) {
 | 
|---|
 | 219 |     if (!defined($element)) {
 | 
|---|
 | 220 |       print "<td></td>\n";
 | 
|---|
 | 221 |       next ELEMENT;
 | 
|---|
 | 222 |     }
 | 
|---|
| [4] | 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
 | 
|---|
| [118] | 243 | sub showSummary {
 | 
|---|
| [106] | 244 |   my %allocated;
 | 
|---|
 | 245 |   my %free;
 | 
|---|
 | 246 |   my %routed;
 | 
|---|
 | 247 |   my %bigfree;
 | 
|---|
 | 248 | 
 | 
|---|
| [118] | 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();
 | 
|---|
| [4] | 255 |   }
 | 
|---|
 | 256 | 
 | 
|---|
| [118] | 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();
 | 
|---|
| [4] | 263 |   }
 | 
|---|
 | 264 | 
 | 
|---|
| [118] | 265 |   # Count the free blocks.
 | 
|---|
| [231] | 266 |   $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
 | 
|---|
 | 267 |         "(routed='y' or routed='n')");
 | 
|---|
| [118] | 268 |   foreach my $master (@masterblocks) {
 | 
|---|
 | 269 |     $sth->execute("$master");
 | 
|---|
 | 270 |     $sth->bind_columns(\$free{"$master"});
 | 
|---|
 | 271 |     $sth->fetch();
 | 
|---|
| [4] | 272 |   }
 | 
|---|
 | 273 | 
 | 
|---|
| [118] | 274 |   # Find the largest free block in each master
 | 
|---|
| [231] | 275 |   $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
 | 
|---|
 | 276 |         "(routed='y' or routed='n') order by maskbits limit 1");
 | 
|---|
| [118] | 277 |   foreach my $master (@masterblocks) {
 | 
|---|
 | 278 |     $sth->execute("$master");
 | 
|---|
 | 279 |     $sth->bind_columns(\$bigfree{"$master"});
 | 
|---|
 | 280 |     $sth->fetch();
 | 
|---|
 | 281 |   }
 | 
|---|
 | 282 | 
 | 
|---|
| [450] | 283 |   # Assemble the data to stuff into the template.
 | 
|---|
 | 284 |   my @masterlist;
 | 
|---|
 | 285 |   my $rowclass=0;
 | 
|---|
| [4] | 286 |   foreach my $master (@masterblocks) {
 | 
|---|
| [478] | 287 |     my %row = (
 | 
|---|
 | 288 |         rowclass => $rowclass++ % 2,
 | 
|---|
| [450] | 289 |         master => "$master",
 | 
|---|
 | 290 |         routed => $routed{"$master"},
 | 
|---|
 | 291 |         allocated => $allocated{"$master"},
 | 
|---|
 | 292 |         free => $free{"$master"},
 | 
|---|
 | 293 |         bigfree => ( ($bigfree{"$master"} eq '') ? ("<NONE>") : ("/".$bigfree{"$master"}) )
 | 
|---|
| [4] | 294 |         );
 | 
|---|
| [450] | 295 |     push (@masterlist, \%row);
 | 
|---|
| [4] | 296 |   }
 | 
|---|
| [450] | 297 |   $page->param(masterlist => \@masterlist);
 | 
|---|
| [4] | 298 | 
 | 
|---|
| [450] | 299 |   $page->param(addmaster => ($IPDBacl{$authuser} =~ /a/) );
 | 
|---|
 | 300 | 
 | 
|---|
| [4] | 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 | 
 | 
|---|
| [465] | 311 |   $page->param(master => $webvar{block});
 | 
|---|
| [4] | 312 | 
 | 
|---|
| [106] | 313 |   my %allocated;
 | 
|---|
 | 314 |   my %free;
 | 
|---|
| [465] | 315 |   my %cities;
 | 
|---|
| [106] | 316 |   my %bigfree;
 | 
|---|
 | 317 | 
 | 
|---|
| [4] | 318 |   my $master = new NetAddr::IP $webvar{block};
 | 
|---|
 | 319 |   my @localmasters;
 | 
|---|
 | 320 | 
 | 
|---|
| [118] | 321 |   # Fetch only the blocks relevant to this master
 | 
|---|
| [157] | 322 |   $sth = $ip_dbh->prepare("select cidr,city from routed where cidr <<= '$master' order by cidr");
 | 
|---|
| [4] | 323 |   $sth->execute();
 | 
|---|
 | 324 | 
 | 
|---|
 | 325 |   my $i=0;
 | 
|---|
 | 326 |   while (my @data = $sth->fetchrow_array()) {
 | 
|---|
 | 327 |     my $cidr = new NetAddr::IP $data[0];
 | 
|---|
| [118] | 328 |     $localmasters[$i++] = $cidr;
 | 
|---|
 | 329 |     $free{"$cidr"} = 0;
 | 
|---|
 | 330 |     $allocated{"$cidr"} = 0;
 | 
|---|
 | 331 |     $bigfree{"$cidr"} = 128;
 | 
|---|
| [4] | 332 |     # Retain the routing destination
 | 
|---|
| [465] | 333 |     $cities{"$cidr"} = $data[1];
 | 
|---|
| [4] | 334 |   }
 | 
|---|
 | 335 | 
 | 
|---|
| [118] | 336 |   # Check if there were actually any blocks routed from this master
 | 
|---|
| [4] | 337 |   if ($i > 0) {
 | 
|---|
 | 338 | 
 | 
|---|
| [118] | 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();
 | 
|---|
| [4] | 345 |     }
 | 
|---|
 | 346 | 
 | 
|---|
| [118] | 347 |     # Count the free blocks.
 | 
|---|
| [231] | 348 |     $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ".
 | 
|---|
 | 349 |         "(routed='y' or routed='n')");
 | 
|---|
| [118] | 350 |     foreach my $master (@localmasters) {
 | 
|---|
 | 351 |       $sth->execute("$master");
 | 
|---|
 | 352 |       $sth->bind_columns(\$free{"$master"});
 | 
|---|
 | 353 |       $sth->fetch();
 | 
|---|
| [4] | 354 |     }
 | 
|---|
 | 355 | 
 | 
|---|
| [118] | 356 |     # Get the size of the largest free block
 | 
|---|
| [231] | 357 |     $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ".
 | 
|---|
 | 358 |         "(routed='y' or routed='n') order by maskbits limit 1");
 | 
|---|
| [118] | 359 |     foreach my $master (@localmasters) {
 | 
|---|
 | 360 |       $sth->execute("$master");
 | 
|---|
 | 361 |       $sth->bind_columns(\$bigfree{"$master"});
 | 
|---|
 | 362 |       $sth->fetch();
 | 
|---|
| [4] | 363 |     }
 | 
|---|
 | 364 | 
 | 
|---|
| [465] | 365 |     my @routed;
 | 
|---|
 | 366 |     my $rowclass = 0;
 | 
|---|
| [4] | 367 |     foreach my $master (@localmasters) {
 | 
|---|
| [465] | 368 |       my %row = (
 | 
|---|
| [478] | 369 |         rowclass => $rowclass++ % 2,
 | 
|---|
| [465] | 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;
 | 
|---|
| [4] | 377 |     }
 | 
|---|
| [465] | 378 |     $page->param(routedlist => \@routed);
 | 
|---|
| [4] | 379 | 
 | 
|---|
 | 380 |   } # end check for existence of routed blocks in master
 | 
|---|
 | 381 | 
 | 
|---|
| [465] | 382 |   $page->param(delmaster => ($IPDBacl{$authuser} =~ /d/));
 | 
|---|
| [4] | 383 | 
 | 
|---|
 | 384 |   # Snag the free blocks.
 | 
|---|
 | 385 |   my $count = 0;
 | 
|---|
| [118] | 386 |   $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr <<='$master' and ".
 | 
|---|
 | 387 |         "routed='n' order by cidr");
 | 
|---|
| [4] | 388 |   $sth->execute();
 | 
|---|
| [465] | 389 |   my @unrouted;
 | 
|---|
 | 390 |   my $rowclass = 0;
 | 
|---|
| [4] | 391 |   while (my @data = $sth->fetchrow_array()) {
 | 
|---|
 | 392 |     my $cidr = new NetAddr::IP $data[0];
 | 
|---|
| [465] | 393 |     my %row = (
 | 
|---|
| [478] | 394 |         rowclass => $rowclass++ % 2,
 | 
|---|
| [465] | 395 |         fblock => "$cidr",
 | 
|---|
 | 396 |         frange => $cidr->range
 | 
|---|
 | 397 |         );
 | 
|---|
 | 398 |     push @unrouted, \%row;
 | 
|---|
| [4] | 399 |   }
 | 
|---|
| [465] | 400 |   $page->param(unrouted => \@unrouted);
 | 
|---|
| [4] | 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 | 
 | 
|---|
| [157] | 416 |   $sth = $ip_dbh->prepare("select city from routed where cidr='$master'");
 | 
|---|
| [4] | 417 |   $sth->execute;
 | 
|---|
| [466] | 418 |   my ($rcity) = $sth->fetchrow_array;
 | 
|---|
| [4] | 419 | 
 | 
|---|
| [466] | 420 |   $page->param(master => "$master");
 | 
|---|
 | 421 |   $page->param(rcity => $rcity);
 | 
|---|
| [4] | 422 | 
 | 
|---|
| [118] | 423 |   # Snag the allocations for this block
 | 
|---|
| [320] | 424 |   $sth = $ip_dbh->prepare("select cidr,city,type,custid,swip,description".
 | 
|---|
| [157] | 425 |         " from allocations where cidr <<= '$master' order by cidr");
 | 
|---|
| [4] | 426 |   $sth->execute();
 | 
|---|
 | 427 | 
 | 
|---|
| [380] | 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 | 
 | 
|---|
| [466] | 432 |   my $rowclass = 0;
 | 
|---|
 | 433 |   my @blocklist;
 | 
|---|
 | 434 |   while (my ($cidr,$city,$type,$custid,$swip,$desc) = $sth->fetchrow_array()) {
 | 
|---|
 | 435 |     $custsth->execute($custid);
 | 
|---|
| [380] | 436 |     my ($ncust) = $custsth->fetchrow_array();
 | 
|---|
 | 437 | 
 | 
|---|
| [466] | 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);
 | 
|---|
| [4] | 450 |   }
 | 
|---|
| [466] | 451 |   $page->param(blocklist => \@blocklist);
 | 
|---|
| [4] | 452 | 
 | 
|---|
| [466] | 453 |   $page->param(delrouted => $IPDBacl{$authuser} =~ /d/);
 | 
|---|
| [4] | 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.
 | 
|---|
| [466] | 457 |   $rowclass = 0;
 | 
|---|
 | 458 |   my @unassigned;
 | 
|---|
| [186] | 459 |   $sth = $ip_dbh->prepare("select cidr,routed from freeblocks where cidr <<= '$master'".
 | 
|---|
 | 460 |         " order by cidr");
 | 
|---|
| [4] | 461 |   $sth->execute();
 | 
|---|
| [466] | 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;
 | 
|---|
| [4] | 473 |   }
 | 
|---|
| [466] | 474 |   $page->param(unassigned => \@unassigned);
 | 
|---|
| [4] | 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 | 
 | 
|---|
| [471] | 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);
 | 
|---|
| [157] | 491 | 
 | 
|---|
| [4] | 492 |   # Snag pool info for heading
 | 
|---|
| [471] | 493 |   $sth = $ip_dbh->prepare("select type,city from allocations where cidr=?");
 | 
|---|
 | 494 |   $sth->execute($webvar{pool});
 | 
|---|
 | 495 |   my ($pooltype, $poolcity) = $sth->fetchrow_array;
 | 
|---|
| [4] | 496 | 
 | 
|---|
| [471] | 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);
 | 
|---|
| [157] | 502 |   # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
 | 
|---|
| [471] | 503 |   $page->param(realblock => $pooltype =~ /^.d$/);
 | 
|---|
| [4] | 504 | 
 | 
|---|
 | 505 | # probably have to add an "edit IP allocation" link here somewhere.
 | 
|---|
 | 506 | 
 | 
|---|
| [157] | 507 |   $sth = $ip_dbh->prepare("select ip,custid,available,description,type".
 | 
|---|
 | 508 |         " from poolips where pool='$webvar{pool}' order by ip");
 | 
|---|
| [4] | 509 |   $sth->execute;
 | 
|---|
| [471] | 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'
 | 
|---|
| [4] | 521 |         );
 | 
|---|
| [471] | 522 |     push @poolips, \%row;
 | 
|---|
| [4] | 523 |   }
 | 
|---|
| [471] | 524 |   $page->param(poolips => \@poolips);
 | 
|---|
| [4] | 525 | 
 | 
|---|
 | 526 | } # end listPool
 | 
|---|
 | 527 | 
 | 
|---|
 | 528 | 
 | 
|---|
| [106] | 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.
 | 
|---|
| [4] | 531 | sub assignBlock {
 | 
|---|
 | 532 | 
 | 
|---|
| [233] | 533 |   if ($IPDBacl{$authuser} !~ /a/) {
 | 
|---|
 | 534 |     printError("You shouldn't have been able to get here.  Access denied.");
 | 
|---|
 | 535 |     return;
 | 
|---|
 | 536 |   }
 | 
|---|
 | 537 | 
 | 
|---|
| [456] | 538 |   # hack pthbttt eww
 | 
|---|
 | 539 |   $webvar{block} = '' if !$webvar{block};
 | 
|---|
| [21] | 540 | 
 | 
|---|
| [456] | 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 | 
 | 
|---|
| [21] | 547 |   # New special case- block to assign is specified
 | 
|---|
 | 548 |   if ($webvar{block} ne '') {
 | 
|---|
 | 549 |     my $block = new NetAddr::IP $webvar{block};
 | 
|---|
| [187] | 550 | 
 | 
|---|
| [456] | 551 |     # Handle contained freeblock allocation.
 | 
|---|
| [187] | 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') {
 | 
|---|
| [456] | 556 |       # Snag the type of the container block from the database.
 | 
|---|
| [187] | 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
 | 
|---|
| [456] | 561 |       $page->param(fbdisptype => $list_alloctypes{$data[0]});
 | 
|---|
 | 562 |       $page->param(type => $data[0]);
 | 
|---|
| [187] | 563 |     } else {
 | 
|---|
 | 564 |       $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 500 ".
 | 
|---|
| [191] | 565 |         "and type not like '_i' and type not like '_r' order by listorder");
 | 
|---|
| [187] | 566 |       $sth->execute;
 | 
|---|
 | 567 |       my @data = $sth->fetchrow_array;
 | 
|---|
| [456] | 568 |       my @typelist;
 | 
|---|
 | 569 |       my $selflag = 0;
 | 
|---|
| [187] | 570 |       while (my @data = $sth->fetchrow_array) {
 | 
|---|
| [456] | 571 |         my %row = (tval => $data[0],
 | 
|---|
 | 572 |                 type => $data[1],
 | 
|---|
 | 573 |                 sel => ($selflag == 0 ? ' selected' : '')
 | 
|---|
 | 574 |                 );
 | 
|---|
 | 575 |         push (@typelist, \%row);
 | 
|---|
 | 576 |         $selflag++;
 | 
|---|
| [187] | 577 |       }
 | 
|---|
| [456] | 578 |       $page->param(typelist => \@typelist);
 | 
|---|
| [106] | 579 |     }
 | 
|---|
| [21] | 580 |   } else {
 | 
|---|
| [456] | 581 |     my @masterlist;
 | 
|---|
| [31] | 582 |     foreach my $master (@masterblocks) {
 | 
|---|
| [456] | 583 |       my %row = (master => "$master");
 | 
|---|
 | 584 |       push (@masterlist, \%row);
 | 
|---|
| [31] | 585 |     }
 | 
|---|
| [456] | 586 |     $page->param(masterlist => \@masterlist);
 | 
|---|
 | 587 | 
 | 
|---|
 | 588 |     my @pops;
 | 
|---|
| [92] | 589 |     foreach my $pop (@poplist) {
 | 
|---|
| [456] | 590 |       my %row = (pop => $pop);
 | 
|---|
 | 591 |       push (@pops, \%row);
 | 
|---|
| [92] | 592 |     }
 | 
|---|
| [456] | 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.
 | 
|---|
| [463] | 597 |     $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder <= 500 order by listorder");
 | 
|---|
| [106] | 598 |     $sth->execute;
 | 
|---|
| [456] | 599 |     my @typelist;
 | 
|---|
 | 600 |     my $selflag = 0;
 | 
|---|
| [106] | 601 |     while (my @data = $sth->fetchrow_array) {
 | 
|---|
| [456] | 602 |       my %row = (tval => $data[0],
 | 
|---|
 | 603 |         type => $data[1],
 | 
|---|
 | 604 |         sel => ($selflag == 0 ? ' selected' : '')
 | 
|---|
 | 605 |         );
 | 
|---|
 | 606 |       push (@typelist, \%row);
 | 
|---|
 | 607 |       $selflag++;
 | 
|---|
| [106] | 608 |     }
 | 
|---|
| [456] | 609 |     $page->param(typelist => \@typelist);
 | 
|---|
| [21] | 610 |   }
 | 
|---|
| [456] | 611 | 
 | 
|---|
 | 612 |   my @cities;
 | 
|---|
| [92] | 613 |   foreach my $city (@citylist) {
 | 
|---|
| [456] | 614 |     my %row = (city => $city);
 | 
|---|
 | 615 |     push (@cities, \%row);
 | 
|---|
| [92] | 616 |   }
 | 
|---|
| [456] | 617 |   $page->param(citylist => \@cities);
 | 
|---|
| [4] | 618 | 
 | 
|---|
| [397] | 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";
 | 
|---|
| [456] | 622 |   my @nodes;
 | 
|---|
| [397] | 623 |   while (my ($nid,$nname) = $sth->fetchrow_array()) {
 | 
|---|
| [456] | 624 |     my %row = (nid => $nid, nname => $nname);
 | 
|---|
 | 625 |     push (@nodes, \%row);
 | 
|---|
| [397] | 626 |   }
 | 
|---|
| [456] | 627 |   $page->param(nodelist => \@nodes);
 | 
|---|
| [397] | 628 | ## end node hack
 | 
|---|
 | 629 | 
 | 
|---|
| [456] | 630 |   $page->param(privdata => $IPDBacl{$authuser} =~ /s/);
 | 
|---|
| [284] | 631 | 
 | 
|---|
| [4] | 632 | } # assignBlock
 | 
|---|
 | 633 | 
 | 
|---|
 | 634 | 
 | 
|---|
 | 635 | # Take info on requested IP assignment and see what we can provide.
 | 
|---|
 | 636 | sub confirmAssign {
 | 
|---|
| [233] | 637 |   if ($IPDBacl{$authuser} !~ /a/) {
 | 
|---|
 | 638 |     printError("You shouldn't have been able to get here.  Access denied.");
 | 
|---|
 | 639 |     return;
 | 
|---|
 | 640 |   }
 | 
|---|
| [4] | 641 | 
 | 
|---|
 | 642 |   my $cidr;
 | 
|---|
 | 643 |   my $alloc_from;
 | 
|---|
 | 644 | 
 | 
|---|
 | 645 |   # Going to manually validate some items.
 | 
|---|
 | 646 |   # custid and city are automagic.
 | 
|---|
| [111] | 647 |   return if !validateInput();
 | 
|---|
| [4] | 648 | 
 | 
|---|
 | 649 | # Several different cases here.
 | 
|---|
 | 650 | # Static IP vs netblock
 | 
|---|
 | 651 | #  + Different flavours of static IP
 | 
|---|
 | 652 | #  + Different flavours of netblock
 | 
|---|
 | 653 | 
 | 
|---|
| [157] | 654 |   if ($webvar{alloctype} =~ /^.i$/) {
 | 
|---|
| [4] | 655 |     my ($base,undef) = split //, $webvar{alloctype};    # split into individual chars
 | 
|---|
 | 656 | 
 | 
|---|
| [214] | 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 ".
 | 
|---|
| [442] | 660 |         "poolips.pool=allocations.cidr AND allocations.city='$webvar{pop}' AND poolips.type LIKE '".$base."_' ".
 | 
|---|
| [214] | 661 |         "GROUP BY pool");
 | 
|---|
| [4] | 662 |     $sth->execute;
 | 
|---|
 | 663 |     my $optionlist;
 | 
|---|
| [463] | 664 | 
 | 
|---|
 | 665 |     my @poollist;
 | 
|---|
 | 666 |     while (my ($poolcit,$poolblock,$poolfree) = $sth->fetchrow_array) {
 | 
|---|
| [214] | 667 |       # city,pool cidr,free IP count
 | 
|---|
| [463] | 668 |       if ($poolfree > 0) {
 | 
|---|
 | 669 |         my %row = (poolcit => $poolcit, poolblock => $poolblock, poolfree => $poolfree);
 | 
|---|
 | 670 |         push (@poollist, \%row);
 | 
|---|
| [214] | 671 |       }
 | 
|---|
| [4] | 672 |     }
 | 
|---|
| [463] | 673 |     $page->param(staticip => 1);
 | 
|---|
 | 674 |     $page->param(poollist => \@poollist);
 | 
|---|
| [4] | 675 |     $cidr = "Single static IP";
 | 
|---|
| [463] | 676 | ##fixme:  need to handle "no available pools"
 | 
|---|
| [4] | 677 | 
 | 
|---|
 | 678 |   } else { # end show pool options
 | 
|---|
| [21] | 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}) {
 | 
|---|
| [463] | 686 |         $page->param(err => "Please specify a CIDR mask length.");
 | 
|---|
| [111] | 687 |         return;
 | 
|---|
| [21] | 688 |       }
 | 
|---|
 | 689 |       my $sql;
 | 
|---|
 | 690 |       my $city;
 | 
|---|
 | 691 |       my $failmsg;
 | 
|---|
| [320] | 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;
 | 
|---|
| [187] | 700 |       if ($webvar{alloctype} eq 'rm') {
 | 
|---|
| [31] | 701 |         if ($webvar{allocfrom} ne '-') {
 | 
|---|
 | 702 |           $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
 | 
|---|
| [320] | 703 |                 " and cidr <<= '$webvar{allocfrom}'";
 | 
|---|
 | 704 |           $sortorder = "maskbits desc";
 | 
|---|
| [31] | 705 |         } else {
 | 
|---|
| [320] | 706 |           $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'";
 | 
|---|
 | 707 |           $sortorder = "maskbits desc";
 | 
|---|
| [31] | 708 |         }
 | 
|---|
| [21] | 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.";
 | 
|---|
| [4] | 712 |       } else {
 | 
|---|
| [118] | 713 | ##fixme
 | 
|---|
 | 714 | # This section needs serious Pondering.
 | 
|---|
| [214] | 715 |         # Pools of most types get assigned to the POP they're "routed from"
 | 
|---|
| [187] | 716 |         # This includes WAN blocks and other netblock "containers"
 | 
|---|
| [214] | 717 |         # This does NOT include cable pools.
 | 
|---|
 | 718 |         if ($webvar{alloctype} =~ /^.[pc]$/) {
 | 
|---|
| [37] | 719 |           $city = $webvar{city};
 | 
|---|
| [21] | 720 |           $failmsg = "No suitable free block found.<br>\nYou will have to route another".
 | 
|---|
| [442] | 721 |             " superblock from one of the<br>\nmaster blocks or chose a smaller".
 | 
|---|
| [21] | 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".
 | 
|---|
| [442] | 726 |             " superblock to $webvar{pop}<br>\nfrom one of the master blocks or".
 | 
|---|
| [21] | 727 |             " chose a smaller blocksize.";
 | 
|---|
 | 728 |         }
 | 
|---|
| [300] | 729 |         if (defined $webvar{allocfrom} && $webvar{allocfrom} ne '-') {
 | 
|---|
| [157] | 730 |           $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
 | 
|---|
| [186] | 731 |                 " and cidr <<= '$webvar{allocfrom}' and routed='".
 | 
|---|
| [320] | 732 |                 (($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
 | 
|---|
 | 733 |           $sortorder = "maskbits desc,cidr";
 | 
|---|
| [31] | 734 |         } else {
 | 
|---|
| [157] | 735 |           $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
 | 
|---|
| [320] | 736 |                 " and routed='".(($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
 | 
|---|
 | 737 |           $sortorder = "maskbits desc,cidr";
 | 
|---|
| [31] | 738 |         }
 | 
|---|
| [4] | 739 |       }
 | 
|---|
| [320] | 740 |       $sql = $sql.$extracond." order by ".$sortorder;
 | 
|---|
| [21] | 741 |       $sth = $ip_dbh->prepare($sql);
 | 
|---|
 | 742 |       $sth->execute;
 | 
|---|
 | 743 |       my @data = $sth->fetchrow_array();
 | 
|---|
 | 744 |       if ($data[0] eq "") {
 | 
|---|
| [463] | 745 |         $page->param(err => $failmsg);
 | 
|---|
| [111] | 746 |         return;
 | 
|---|
| [21] | 747 |       }
 | 
|---|
 | 748 |       $cidr = new NetAddr::IP $data[0];
 | 
|---|
 | 749 |     } # check for freeblocks assignment or IPDB-controlled assignment
 | 
|---|
| [4] | 750 | 
 | 
|---|
| [463] | 751 |     $alloc_from = "$cidr";
 | 
|---|
| [4] | 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 |     }
 | 
|---|
| [157] | 763 |   } # if ($webvar{alloctype} =~ /^.i$/)
 | 
|---|
| [4] | 764 | 
 | 
|---|
| [397] | 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();
 | 
|---|
| [461] | 770 |     $page->param(nodename => $nodename);
 | 
|---|
 | 771 |     $page->param(nodeid => $webvar{node});
 | 
|---|
| [397] | 772 |   }
 | 
|---|
 | 773 | ## end node hack
 | 
|---|
 | 774 | 
 | 
|---|
| [4] | 775 |   # Stick in the allocation data
 | 
|---|
| [461] | 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}));
 | 
|---|
| [4] | 784 | 
 | 
|---|
| [461] | 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 | 
 | 
|---|
| [284] | 790 |   # Check to see if user is allowed to do anything with sensitive data
 | 
|---|
 | 791 |   my $privdata = '';
 | 
|---|
| [461] | 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})
 | 
|---|
| [299] | 797 |         if $webvar{userid};
 | 
|---|
| [284] | 798 | 
 | 
|---|
| [461] | 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");
 | 
|---|
| [284] | 803 | 
 | 
|---|
| [4] | 804 | } # end confirmAssign
 | 
|---|
 | 805 | 
 | 
|---|
 | 806 | 
 | 
|---|
 | 807 | # Do the work of actually inserting a block in the database.
 | 
|---|
 | 808 | sub insertAssign {
 | 
|---|
| [233] | 809 |   if ($IPDBacl{$authuser} !~ /a/) {
 | 
|---|
 | 810 |     printError("You shouldn't have been able to get here.  Access denied.");
 | 
|---|
 | 811 |     return;
 | 
|---|
 | 812 |   }
 | 
|---|
| [4] | 813 |   # Some things are done more than once.
 | 
|---|
| [111] | 814 |   return if !validateInput();
 | 
|---|
| [4] | 815 | 
 | 
|---|
| [284] | 816 |   if (!defined($webvar{privdata})) {
 | 
|---|
 | 817 |     $webvar{privdata} = '';
 | 
|---|
 | 818 |   }
 | 
|---|
| [106] | 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.
 | 
|---|
| [463] | 822 | 
 | 
|---|
| [106] | 823 |   my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
 | 
|---|
 | 824 |         $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
 | 
|---|
| [397] | 825 |         $webvar{circid}, $webvar{privdata}, $webvar{node});
 | 
|---|
| [4] | 826 | 
 | 
|---|
| [111] | 827 |   if ($code eq 'OK') {
 | 
|---|
| [106] | 828 |     if ($webvar{alloctype} =~ /^.i$/) {
 | 
|---|
| [300] | 829 |       $msg =~ s|/32||;
 | 
|---|
| [463] | 830 |       $page->param(staticip => $msg);
 | 
|---|
 | 831 |       $page->param(custid => $webvar{custid});
 | 
|---|
 | 832 |       $page->param(billinguser => $webvar{billinguser});
 | 
|---|
| [416] | 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");
 | 
|---|
| [106] | 836 |     } else {
 | 
|---|
| [301] | 837 |       my $netblock = new NetAddr::IP $webvar{fullcidr};
 | 
|---|
| [463] | 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 |       }
 | 
|---|
| [416] | 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");
 | 
|---|
| [4] | 850 |     }
 | 
|---|
| [106] | 851 |     syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
 | 
|---|
| [256] | 852 |         "'$webvar{alloctype}' ($msg)";
 | 
|---|
| [111] | 853 |   } else {
 | 
|---|
 | 854 |     syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
 | 
|---|
 | 855 |         "'$webvar{alloctype}' by $authuser failed: '$msg'";
 | 
|---|
| [463] | 856 |     $page->param(err => "Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'".
 | 
|---|
| [157] | 857 |         " failed:<br>\n$msg\n");
 | 
|---|
| [106] | 858 |   }
 | 
|---|
| [4] | 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 '-') {
 | 
|---|
| [111] | 868 |     printError("Please choose a city.");
 | 
|---|
 | 869 |     return;
 | 
|---|
| [4] | 870 |   }
 | 
|---|
| [138] | 871 | 
 | 
|---|
 | 872 |   # Alloctype check.
 | 
|---|
| [4] | 873 |   chomp $webvar{alloctype};
 | 
|---|
| [138] | 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
 | 
|---|
| [4] | 882 |   # We have different handling for customer allocations and "internal" or "our" allocations
 | 
|---|
| [214] | 883 |   if ($def_custids{$webvar{alloctype}} eq '') {
 | 
|---|
| [4] | 884 |     if (!$webvar{custid}) {
 | 
|---|
| [111] | 885 |       printError("Please enter a customer ID.");
 | 
|---|
 | 886 |       return;
 | 
|---|
| [4] | 887 |     }
 | 
|---|
| [400] | 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 ".
 | 
|---|
| [420] | 899 |           "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
 | 
|---|
| [400] | 900 |           "non-customer assignments.");
 | 
|---|
 | 901 |         return;
 | 
|---|
 | 902 |       }
 | 
|---|
| [4] | 903 |     }
 | 
|---|
| [400] | 904 | #    print "<!-- [ In validateInput().  Insert customer ID cross-check here. ] -->\n";
 | 
|---|
| [138] | 905 |   } else {
 | 
|---|
| [167] | 906 |     # New!  Improved!  And now Loaded From The Database!!
 | 
|---|
| [320] | 907 |     if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
 | 
|---|
 | 908 |       $webvar{custid} = $def_custids{$webvar{alloctype}};
 | 
|---|
 | 909 |     }
 | 
|---|
| [4] | 910 |   }
 | 
|---|
| [111] | 911 | 
 | 
|---|
 | 912 |   # Check POP location
 | 
|---|
 | 913 |   my $flag;
 | 
|---|
| [187] | 914 |   if ($webvar{alloctype} eq 'rm') {
 | 
|---|
| [111] | 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';
 | 
|---|
| [442] | 924 | ##fixme:  hook to force-set POP or city on certain alloctypes
 | 
|---|
 | 925 | # if ($webvar{alloctype =~ /foo,bar,bz/ { $webvar{pop} = 'blah'; }
 | 
|---|
| [400] | 926 |     if ($webvar{pop} =~ /^-$/) {
 | 
|---|
| [111] | 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';
 | 
|---|
| [4] | 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
 | 
|---|
| [470] | 949 | ##fixme:  allow "SWIP" (publication to rWHOIS) of static IP data
 | 
|---|
| [4] | 950 |   if ($webvar{block} =~ /\/32$/) {
 | 
|---|
| [468] | 951 |     $sql = "select ip,custid,type,city,circuitid,description,notes,modifystamp,privdata from poolips where ip='$webvar{block}'";
 | 
|---|
| [4] | 952 |   } else {
 | 
|---|
| [468] | 953 |     $sql = "select cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,swip from allocations where cidr='$webvar{block}'"
 | 
|---|
| [4] | 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?
 | 
|---|
| [469] | 969 | # Also have areas where a routed block at a POP serves "many" cities/towns/named crossroads
 | 
|---|
| [4] | 970 | 
 | 
|---|
| [470] | 971 | # @data: cidr,custid,type,city,circuitid,description,notes,modifystamp,privdata,swip
 | 
|---|
| [233] | 972 | 
 | 
|---|
| [470] | 973 |   $page->param(block => $webvar{block});
 | 
|---|
| [4] | 974 | 
 | 
|---|
| [470] | 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]);
 | 
|---|
| [4] | 980 | 
 | 
|---|
| [187] | 981 | ##fixme The check here should be built from the database
 | 
|---|
| [469] | 982 | # Need to expand to support pool types too
 | 
|---|
| [470] | 983 |   if ($data[2] =~ /^.[ne]$/ && $IPDBacl{$authuser} =~ /c/) {
 | 
|---|
 | 984 |     $page->param(changetype => 1);
 | 
|---|
 | 985 |     $page->param(alloctype => [
 | 
|---|
| [469] | 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 |         );
 | 
|---|
| [470] | 995 |   } else {
 | 
|---|
 | 996 |     $page->param(disptype => $disp_alloctypes{$data[2]});
 | 
|---|
 | 997 |     $page->param(type => $data[2]);
 | 
|---|
 | 998 |   }
 | 
|---|
| [469] | 999 | 
 | 
|---|
| [397] | 1000 | ## node hack
 | 
|---|
| [470] | 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 | 
 | 
|---|
| [469] | 1011 | ##fixme:  this whole hack needs cleanup and generalization for all alloctypes
 | 
|---|
| [470] | 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/) {
 | 
|---|
| [397] | 1016 |       $sth = $ip_dbh->prepare("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id");
 | 
|---|
| [470] | 1017 |       $sth->execute;
 | 
|---|
 | 1018 |       my @nodelist;
 | 
|---|
| [397] | 1019 |       while (my ($nid,$nname) = $sth->fetchrow_array()) {
 | 
|---|
| [470] | 1020 |         my %row = (
 | 
|---|
 | 1021 |                 selme => ($nodeid == $nid),
 | 
|---|
 | 1022 |                 nodeid => $nid,
 | 
|---|
 | 1023 |                 nodename => $nname,
 | 
|---|
 | 1024 |                 );
 | 
|---|
 | 1025 |         push (@nodelist, \%row);
 | 
|---|
| [397] | 1026 |       }
 | 
|---|
| [470] | 1027 |       $page->param(nodelist => \@nodelist);
 | 
|---|
| [397] | 1028 |     }
 | 
|---|
 | 1029 |   }
 | 
|---|
 | 1030 | ## end node hack
 | 
|---|
| [470] | 1031 | 
 | 
|---|
| [255] | 1032 |   my ($lastmod,undef) = split /\s+/, $data[7];
 | 
|---|
| [469] | 1033 |   $page->param(lastmod => $lastmod);
 | 
|---|
| [33] | 1034 | 
 | 
|---|
| [470] | 1035 |   # not happy with the upside-down logic, but...
 | 
|---|
 | 1036 |   $page->param(swipable => $data[2] !~ /.i/);
 | 
|---|
 | 1037 |   $page->param(swip => $data[10] ne 'n');
 | 
|---|
| [320] | 1038 | 
 | 
|---|
| [284] | 1039 |   # Check to see if we can display sensitive data
 | 
|---|
| [470] | 1040 |   $page->param(nocling => $IPDBacl{$authuser} =~ /s/);
 | 
|---|
 | 1041 |   $page->param(privdata => $data[8]);
 | 
|---|
| [469] | 1042 | 
 | 
|---|
| [470] | 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/);
 | 
|---|
| [284] | 1046 | 
 | 
|---|
| [4] | 1047 | } # edit()
 | 
|---|
 | 1048 | 
 | 
|---|
 | 1049 | 
 | 
|---|
 | 1050 | # Stuff new info about a block into the db
 | 
|---|
 | 1051 | # action=update
 | 
|---|
 | 1052 | sub update {
 | 
|---|
| [284] | 1053 |   if ($IPDBacl{$authuser} !~ /c/) {
 | 
|---|
 | 1054 |     printError("You shouldn't have been able to get here.  Access denied.");
 | 
|---|
 | 1055 |     return;
 | 
|---|
 | 1056 |   }
 | 
|---|
| [4] | 1057 | 
 | 
|---|
| [284] | 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 | 
 | 
|---|
| [4] | 1064 |   # Make sure incoming data is in correct format - custID among other things.
 | 
|---|
| [228] | 1065 |   return if !validateInput;
 | 
|---|
| [4] | 1066 | 
 | 
|---|
 | 1067 |   # SQL transaction wrapper
 | 
|---|
 | 1068 |   eval {
 | 
|---|
 | 1069 |     # Relatively simple SQL transaction here.
 | 
|---|
 | 1070 |     my $sql;
 | 
|---|
| [157] | 1071 |     if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
 | 
|---|
| [74] | 1072 |       $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',".
 | 
|---|
| [284] | 1073 |         "circuitid='$webvar{circid}',description='$webvar{desc}',city='$webvar{city}'".
 | 
|---|
 | 1074 |         "$privdata where ip='$webvar{block}'";
 | 
|---|
| [4] | 1075 |     } else {
 | 
|---|
 | 1076 |       $sql = "update allocations set custid='$webvar{custid}',".
 | 
|---|
| [33] | 1077 |         "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',".
 | 
|---|
| [320] | 1078 |         "type='$webvar{alloctype}',circuitid='$webvar{circid}'$privdata,".
 | 
|---|
 | 1079 |         "swip='".($webvar{swip} eq 'on' ? 'y' : 'n')."' ".
 | 
|---|
| [284] | 1080 |         "where cidr='$webvar{block}'";
 | 
|---|
| [4] | 1081 |     }
 | 
|---|
| [157] | 1082 |     # Log the details of the change.
 | 
|---|
 | 1083 |     syslog "debug", $sql;
 | 
|---|
| [4] | 1084 |     $sth = $ip_dbh->prepare($sql);
 | 
|---|
 | 1085 |     $sth->execute;
 | 
|---|
| [397] | 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
 | 
|---|
| [4] | 1093 |     $ip_dbh->commit;
 | 
|---|
 | 1094 |   };
 | 
|---|
 | 1095 |   if ($@) {
 | 
|---|
| [166] | 1096 |     my $msg = $@;
 | 
|---|
 | 1097 |     carp "Transaction aborted because $msg";
 | 
|---|
| [4] | 1098 |     eval { $ip_dbh->rollback; };
 | 
|---|
| [166] | 1099 |     syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
 | 
|---|
 | 1100 |     printError("Could not update block/IP $webvar{block}: $msg");
 | 
|---|
| [111] | 1101 |     return;
 | 
|---|
| [4] | 1102 |   }
 | 
|---|
 | 1103 | 
 | 
|---|
 | 1104 |   # If we get here, the operation succeeded.
 | 
|---|
 | 1105 |   syslog "notice", "$authuser updated $webvar{block}";
 | 
|---|
| [416] | 1106 | ##fixme:  need to wedge something in to allow "update:field" notifications
 | 
|---|
 | 1107 | ## hmm.  how to tell what changed?  O_o
 | 
|---|
| [426] | 1108 | mailNotify($ip_dbh, 's:swi', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
 | 
|---|
| [416] | 1109 |         "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
 | 
|---|
| [4] | 1110 | 
 | 
|---|
| [476] | 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 | 
 | 
|---|
| [380] | 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$/) ) {
 | 
|---|
| [476] | 1124 |     $page->param(backpool => 1);
 | 
|---|
| [380] | 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;
 | 
|---|
| [476] | 1135 |   $page->param(backblock => $cblock);
 | 
|---|
| [380] | 1136 | 
 | 
|---|
| [476] | 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/;
 | 
|---|
| [4] | 1148 | 
 | 
|---|
 | 1149 | } # update()
 | 
|---|
 | 1150 | 
 | 
|---|
 | 1151 | 
 | 
|---|
 | 1152 | # Delete an allocation.
 | 
|---|
| [106] | 1153 | sub remove {
 | 
|---|
| [233] | 1154 |   if ($IPDBacl{$authuser} !~ /d/) {
 | 
|---|
 | 1155 |     printError("You shouldn't have been able to get here.  Access denied.");
 | 
|---|
 | 1156 |     return;
 | 
|---|
 | 1157 |   }
 | 
|---|
 | 1158 | 
 | 
|---|
| [4] | 1159 |   # Serves'em right for getting here...
 | 
|---|
 | 1160 |   if (!defined($webvar{block})) {
 | 
|---|
| [111] | 1161 |     printError("Error 332");
 | 
|---|
 | 1162 |     return;
 | 
|---|
| [4] | 1163 |   }
 | 
|---|
 | 1164 | 
 | 
|---|
| [284] | 1165 |   my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype, $privdata);
 | 
|---|
| [4] | 1166 | 
 | 
|---|
| [187] | 1167 |   if ($webvar{alloctype} eq 'rm') {
 | 
|---|
| [4] | 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};
 | 
|---|
| [74] | 1179 |     $circid = "N/A";
 | 
|---|
| [4] | 1180 |     $desc = "N/A";
 | 
|---|
 | 1181 |     $notes = "N/A";
 | 
|---|
| [474] | 1182 |     $privdata = "N/A";
 | 
|---|
| [4] | 1183 | 
 | 
|---|
 | 1184 |   } elsif ($webvar{alloctype} eq 'mm') {
 | 
|---|
| [474] | 1185 | 
 | 
|---|
| [4] | 1186 |     $cidr = $webvar{block};
 | 
|---|
 | 1187 |     $city = "N/A";
 | 
|---|
 | 1188 |     $custid = "N/A";
 | 
|---|
 | 1189 |     $alloctype = $webvar{alloctype};
 | 
|---|
| [74] | 1190 |     $circid = "N/A";
 | 
|---|
| [4] | 1191 |     $desc = "N/A";
 | 
|---|
 | 1192 |     $notes = "N/A";
 | 
|---|
| [474] | 1193 |     $privdata = "N/A";
 | 
|---|
 | 1194 | 
 | 
|---|
| [189] | 1195 |   } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m
 | 
|---|
| [4] | 1196 | 
 | 
|---|
 | 1197 |     # Unassigning a static IP
 | 
|---|
| [284] | 1198 |     my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid,privdata".
 | 
|---|
 | 1199 |         " from poolips where ip='$webvar{block}'");
 | 
|---|
| [4] | 1200 |     $sth->execute();
 | 
|---|
 | 1201 | #  croak $sth->errstr() if($sth->errstr());
 | 
|---|
 | 1202 | 
 | 
|---|
| [284] | 1203 |     $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid,
 | 
|---|
 | 1204 |         \$privdata);
 | 
|---|
| [4] | 1205 |     $sth->fetch() || croak $sth->errstr;
 | 
|---|
 | 1206 | 
 | 
|---|
| [157] | 1207 |   } else { # done with alloctype=~ /^.i$/
 | 
|---|
| [4] | 1208 | 
 | 
|---|
| [284] | 1209 |     my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes,privdata".
 | 
|---|
 | 1210 |         " from allocations where cidr='$webvar{block}'");
 | 
|---|
| [4] | 1211 |     $sth->execute();
 | 
|---|
 | 1212 | #       croak $sth->errstr() if($sth->errstr());
 | 
|---|
 | 1213 | 
 | 
|---|
| [284] | 1214 |     $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc,
 | 
|---|
 | 1215 |         \$notes, \$privdata);
 | 
|---|
| [74] | 1216 |     $sth->fetch() || carp $sth->errstr;
 | 
|---|
| [4] | 1217 |   } # end cases for different alloctypes
 | 
|---|
 | 1218 | 
 | 
|---|
| [474] | 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]$/);
 | 
|---|
| [4] | 1230 | 
 | 
|---|
| [474] | 1231 | } # end remove()
 | 
|---|
| [4] | 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 {
 | 
|---|
| [233] | 1239 |   if ($IPDBacl{$authuser} !~ /d/) {
 | 
|---|
| [476] | 1240 |     $page->param(aclerr => 1);
 | 
|---|
| [233] | 1241 |     return;
 | 
|---|
 | 1242 |   }
 | 
|---|
| [4] | 1243 | 
 | 
|---|
| [370] | 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 | 
 | 
|---|
| [106] | 1247 |   my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
 | 
|---|
| [4] | 1248 | 
 | 
|---|
| [476] | 1249 |   $page->param(block => $webvar{block});
 | 
|---|
| [106] | 1250 |   if ($code eq 'OK') {
 | 
|---|
| [370] | 1251 |     syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}".
 | 
|---|
 | 1252 |         " $custid, $city, desc='$description'";
 | 
|---|
| [416] | 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");
 | 
|---|
| [106] | 1256 |   } else {
 | 
|---|
| [476] | 1257 |     $page->param(failmsg => $msg);
 | 
|---|
| [106] | 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'";
 | 
|---|
| [476] | 1262 |       $page->param(netblock => 1);
 | 
|---|
| [4] | 1263 |     }
 | 
|---|
| [106] | 1264 |   }
 | 
|---|
| [4] | 1265 | 
 | 
|---|
 | 1266 | } # finalDelete
 | 
|---|