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