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