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