source: branches/stable/cgi-bin/main.cgi @ 594

Last change on this file since 594 was 594, checked in by Kris Deugau, 8 years ago

/branches/stable

Merge SQL changes and other miscellaneous fixes from /trunk through r553.

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