Changeset 65


Ignore:
Timestamp:
11/12/04 15:12:45 (20 years ago)
Author:
Kris Deugau
Message:

/trunk

Major update to admin tool; adds more or less complete
support for IP pools

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/admin.cgi

    r58 r65  
    33# Hack interface to make specific changes to IPDB that (for one reason
    44# or another) can't be made through the main interface.
     5#
    56###
    67# SVN revision info
     
    910# Last update by $Author$
    1011###
     12# Copyright (C) 2004 - Kris Deugau
    1113
    1214use strict;
     
    8587Manually update allocation data in this /24: <input name=allocfrom>
    8688<input type=submit value="Show allocations">
     89</form>
     90<hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates
    8791);
    8892} else {
     
    120124        $webvar{custid}, $webvar{city}, $webvar{desc}, $webvar{notes});
    121125  #my ($dbh,from,block,$type,$custid,$city,$desc,$notes) = @_;
    122 } else {
    123   print "webvar{action} check failed";
     126} elsif ($webvar{action} eq 'showpools') {
     127  print "IP Pools currently allocated:\n".
     128        "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n";
     129  $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' order by cidr");
     130  $sth->execute;
     131  my %poolfree;
     132  while (my @data = $sth->fetchrow_array) {
     133    $poolfree{$data[0]} = 0;
     134  }
     135  $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip");
     136  $sth->execute;
     137  while (my @data = $sth->fetchrow_array) {
     138    $poolfree{$data[0]}++;
     139  }
     140  foreach my $key (keys %poolfree) {
     141    print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>).
     142        "<td>$poolfree{$key}</td></tr>\n";
     143  }
     144  print "</table>\n";
     145} elsif ($webvar{action} eq 'tweakpool') {
     146  showPool($webvar{pool});
     147} elsif ($webvar{action} eq 'updatepool') {
     148  $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
     149        "city='$webvar{city}', ptype='$webvar{ptype}', available='".
     150        (($webvar{available} eq 'y') ? 'y' : 'n').
     151        "', notes='$webvar{notes}', description='$webvar{desc}' ".
     152        "where ip='$webvar{ip}'");
     153  $sth->execute;
     154  if ($sth->err) {
     155    print "Error updating pool IP $webvar{ip}: $@<hr>\n";
     156    syslog "err", "$authuser could not update pool IP $webvar{ip}: $@";
     157  } else { 
     158    $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
     159    $sth->execute;
     160    my @data = $sth->fetchrow_array;
     161    print "$webvar{ip} in $data[0] updated\n<hr>\n";
     162    syslog "notice", "$authuser updated pool IP $webvar{ip}";
     163  }
     164  showPool("$data[0]");
     165#} else {
     166#  print "webvar{action} check failed: $webvar{action}";
    124167}
    125168
     
    155198  my $block = new NetAddr::IP $_[2];
    156199
    157   # First, figure out what free blocks will get mangled.
     200  local $ip_dbh->{AutoCommit} = 0;  # enable transactions, if possible
     201  local $ip_dbh->{RaiseError} = 1;  # Use local to limit to this sub
     202
    158203  if ($from eq $block) {
    159     # Whee!  Easy.  Just allocate the block
     204    eval {
     205      # common stuff for end-use, dialup, dynDSL, pools, etc, etc.
     206
     207      $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$block'");
     208      $sth->execute;
     209
     210      # Insert the allocations entry
     211      $sth = $ip_dbh->prepare("insert into allocations values ('$block',".
     212        "'$custid','$type','$city','$desc','$notes',".$block->masklen.")");
     213      $sth->execute;
     214
     215      $ip_dbh->commit;
     216    };  # end of eval
     217    if ($@) {
     218      carp "Transaction aborted because $@";
     219      eval { $ip_dbh->rollback; };
     220      syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
     221        "'$webvar{alloctype}' by $authuser failed: '$@'";
     222      printAndExit("Allocation of $cidr as $full_alloc_types{$webvar{alloctype}} failed.\n");
     223    } else {
     224      syslog "notice", "$authuser allocated '$block' to '$custid'".
     225        " as '$webvar{alloctype}'";
     226      print "Block $block allocated to $custid.<br>\n";
     227    }
    160228  } else {
    161229    # The complex case.  An allocation from a larger block.
     
    176244
    177245# insert the data here.  Woo.
    178       # Begin SQL transaction block
    179       eval {
    180         # Delete old freeblocks entry
    181         $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$from'");
    182         $sth->execute();
    183 
    184         # Insert the new freeblocks entries
    185         $sth = $ip_dbh->prepare("insert into freeblocks values (?, ?, ".
    186                 "(select city from routed where cidr >>= '$block'),'y')");
    187         foreach my $block (@newfreeblocks) {
    188           $sth->execute("$block", $block->masklen);
    189         }
    190         # Insert the allocations entry
    191         $sth = $ip_dbh->prepare("insert into allocations values ('$block',".
    192                 "'$custid','$type','$city','$desc','$notes',".$block->masklen.")");
    193         $sth->execute;
    194 
    195         $ip_dbh->commit;
    196       }; # end eval
    197       if ($@) {
    198         carp "Transaction aborted because $@";
    199         eval { $ip_dbh->rollback; };
    200         syslog "err", "Allocation of '$block' to '$custid' as ".
    201                 "'$type' by $authuser failed: '$@'";
    202         print "Allocation of $block as $full_alloc_types{$type} failed.\n";
    203       } else {
    204         syslog "notice", "$authuser allocated '$block' to '$custid'".
    205                 " as '$type'";
    206         print "OK!<br>\n";
     246    # Begin SQL transaction block
     247    eval {
     248      # Delete old freeblocks entry
     249      $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$from'");
     250      $sth->execute();
     251
     252      # Insert the new freeblocks entries
     253      $sth = $ip_dbh->prepare("insert into freeblocks values (?, ?, ".
     254        "(select city from routed where cidr >>= '$block'),'y')");
     255      foreach my $block (@newfreeblocks) {
     256        $sth->execute("$block", $block->masklen);
    207257      }
    208 
    209   }
     258      # Insert the allocations entry
     259      $sth = $ip_dbh->prepare("insert into allocations values ('$block',".
     260        "'$custid','$type','$city','$desc','$notes',".$block->masklen.")");
     261      $sth->execute;
     262
     263      $ip_dbh->commit;
     264    }; # end eval
     265    if ($@) {
     266      carp "Transaction aborted because $@";
     267      eval { $ip_dbh->rollback; };
     268      syslog "err", "Allocation of '$block' to '$custid' as ".
     269        "'$type' by $authuser failed: '$@'";
     270      print "Allocation of $block as $full_alloc_types{$type} failed.\n";
     271    } else {
     272      syslog "notice", "$authuser allocated '$block' to '$custid'".
     273        " as '$type'";
     274      print "Block $block allocated to $custid.<br>\n";
     275    } # done OK?/NOK! check after DB changes
     276
     277  } # done "hard" allocation case.
     278
    210279  # need to get /24 that block is part of
    211280  my @bits = split /\./, $webvar{block};
     
    213282  showAllocs((join ".", @bits));
    214283}
     284
    215285
    216286# List free blocks in a /24 for arbitrary manual allocation
     
    298368    carp "Transaction aborted because $@";
    299369    eval { $ip_dbh->rollback; };
    300     syslog "err", "$authuser could not update block/IP '$webvar{block}': '$@'";
     370    syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
    301371  } else {
    302372    # If we get here, the operation succeeded.
     
    309379  showAllocs((join ".", @bits));
    310380}
     381
     382
     383# showPool()
     384# List all IPs in a pool, and allow arbitrary admin changes to each
     385# Allow changes to ALL fields
     386sub showPool($) {
     387  my $pool = new NetAddr::IP $_[0];
     388  print qq(Listing pool $pool:\n<table border=1>
     389<form action=admin.cgi method=POST>
     390<input type=hidden name=action value=updatepool>
     391<tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
     392<tr><td align=right>Customer location:</td><td><input name=city></td></tr>
     393<tr><td align=right>Type:</td><td><select name=ptype><option selected>-</option>
     394<option value="s">Static IP - Server pool</option>
     395<option value="c">Static IP - Cable</option>
     396<option value="d">Static IP - DSL</option>
     397<option value="m">Static IP - Dialup</option>
     398<option value="w">Static IP - Wireless</option>
     399</select></td></tr>
     400<tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
     401<tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
     402<tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
     403<tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
     404).
     405        "</table>Update the following record:<table border=1>\n";
     406  $sth = $ip_dbh->prepare("select * from poolips where pool='$pool' order by ip");
     407  $sth->execute;
     408  while (my @data = $sth->fetchrow_array) {
     409    print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
     410        "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
     411        "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
     412  }
     413  print "</form></table>\n";
     414}
Note: See TracChangeset for help on using the changeset viewer.