Index: trunk/cgi-bin/admin.cgi
===================================================================
--- trunk/cgi-bin/admin.cgi	(revision 58)
+++ trunk/cgi-bin/admin.cgi	(revision 65)
@@ -3,4 +3,5 @@
 # Hack interface to make specific changes to IPDB that (for one reason
 # or another) can't be made through the main interface.
+#
 ###
 # SVN revision info
@@ -9,4 +10,5 @@
 # Last update by $Author$
 ###
+# Copyright (C) 2004 - Kris Deugau
 
 use strict;
@@ -85,4 +87,6 @@
 Manually update allocation data in this /24: <input name=allocfrom>
 <input type=submit value="Show allocations">
+</form>
+<hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates
 );
 } else {
@@ -120,6 +124,45 @@
 	$webvar{custid}, $webvar{city}, $webvar{desc}, $webvar{notes});
   #my ($dbh,from,block,$type,$custid,$city,$desc,$notes) = @_;
-} else {
-  print "webvar{action} check failed";
+} elsif ($webvar{action} eq 'showpools') {
+  print "IP Pools currently allocated:\n".
+	"<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n";
+  $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' order by cidr");
+  $sth->execute;
+  my %poolfree;
+  while (my @data = $sth->fetchrow_array) {
+    $poolfree{$data[0]} = 0;
+  }
+  $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip");
+  $sth->execute;
+  while (my @data = $sth->fetchrow_array) {
+    $poolfree{$data[0]}++;
+  }
+  foreach my $key (keys %poolfree) {
+    print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>).
+	"<td>$poolfree{$key}</td></tr>\n";
+  }
+  print "</table>\n";
+} elsif ($webvar{action} eq 'tweakpool') {
+  showPool($webvar{pool});
+} elsif ($webvar{action} eq 'updatepool') {
+  $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
+	"city='$webvar{city}', ptype='$webvar{ptype}', available='".
+	(($webvar{available} eq 'y') ? 'y' : 'n').
+	"', notes='$webvar{notes}', description='$webvar{desc}' ".
+	"where ip='$webvar{ip}'");
+  $sth->execute;
+  if ($sth->err) {
+    print "Error updating pool IP $webvar{ip}: $@<hr>\n";
+    syslog "err", "$authuser could not update pool IP $webvar{ip}: $@";
+  } else {  
+    $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
+    $sth->execute;
+    my @data = $sth->fetchrow_array;
+    print "$webvar{ip} in $data[0] updated\n<hr>\n";
+    syslog "notice", "$authuser updated pool IP $webvar{ip}";
+  }
+  showPool("$data[0]");
+#} else {
+#  print "webvar{action} check failed: $webvar{action}";
 }
 
@@ -155,7 +198,32 @@
   my $block = new NetAddr::IP $_[2];
 
-  # First, figure out what free blocks will get mangled.
+  local $ip_dbh->{AutoCommit} = 0;  # enable transactions, if possible
+  local $ip_dbh->{RaiseError} = 1;  # Use local to limit to this sub
+
   if ($from eq $block) {
-    # Whee!  Easy.  Just allocate the block
+    eval {
+      # common stuff for end-use, dialup, dynDSL, pools, etc, etc.
+
+      $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$block'");
+      $sth->execute;
+
+      # Insert the allocations entry
+      $sth = $ip_dbh->prepare("insert into allocations values ('$block',".
+	"'$custid','$type','$city','$desc','$notes',".$block->masklen.")");
+      $sth->execute;
+
+      $ip_dbh->commit;
+    };  # end of eval
+    if ($@) {
+      carp "Transaction aborted because $@";
+      eval { $ip_dbh->rollback; };
+      syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
+	"'$webvar{alloctype}' by $authuser failed: '$@'";
+      printAndExit("Allocation of $cidr as $full_alloc_types{$webvar{alloctype}} failed.\n");
+    } else {
+      syslog "notice", "$authuser allocated '$block' to '$custid'".
+	" as '$webvar{alloctype}'";
+      print "Block $block allocated to $custid.<br>\n";
+    }
   } else {
     # The complex case.  An allocation from a larger block.
@@ -176,36 +244,37 @@
 
 # insert the data here.  Woo.
-      # Begin SQL transaction block
-      eval {
-	# Delete old freeblocks entry
-	$sth = $ip_dbh->prepare("delete from freeblocks where cidr='$from'");
-	$sth->execute();
-
-	# Insert the new freeblocks entries
-	$sth = $ip_dbh->prepare("insert into freeblocks values (?, ?, ".
-		"(select city from routed where cidr >>= '$block'),'y')");
-	foreach my $block (@newfreeblocks) {
-	  $sth->execute("$block", $block->masklen);
-	}
-	# Insert the allocations entry
-	$sth = $ip_dbh->prepare("insert into allocations values ('$block',".
-		"'$custid','$type','$city','$desc','$notes',".$block->masklen.")");
-	$sth->execute;
-
-	$ip_dbh->commit;
-      }; # end eval
-      if ($@) {
-	carp "Transaction aborted because $@";
-	eval { $ip_dbh->rollback; };
-	syslog "err", "Allocation of '$block' to '$custid' as ".
-		"'$type' by $authuser failed: '$@'";
-	print "Allocation of $block as $full_alloc_types{$type} failed.\n";
-      } else {
-	syslog "notice", "$authuser allocated '$block' to '$custid'".
-		" as '$type'";
-	print "OK!<br>\n";
+    # Begin SQL transaction block
+    eval {
+      # Delete old freeblocks entry
+      $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$from'");
+      $sth->execute();
+
+      # Insert the new freeblocks entries
+      $sth = $ip_dbh->prepare("insert into freeblocks values (?, ?, ".
+	"(select city from routed where cidr >>= '$block'),'y')");
+      foreach my $block (@newfreeblocks) {
+	$sth->execute("$block", $block->masklen);
       }
-
-  }
+      # Insert the allocations entry
+      $sth = $ip_dbh->prepare("insert into allocations values ('$block',".
+	"'$custid','$type','$city','$desc','$notes',".$block->masklen.")");
+      $sth->execute;
+
+      $ip_dbh->commit;
+    }; # end eval
+    if ($@) {
+      carp "Transaction aborted because $@";
+      eval { $ip_dbh->rollback; };
+      syslog "err", "Allocation of '$block' to '$custid' as ".
+	"'$type' by $authuser failed: '$@'";
+      print "Allocation of $block as $full_alloc_types{$type} failed.\n";
+    } else {
+      syslog "notice", "$authuser allocated '$block' to '$custid'".
+	" as '$type'";
+      print "Block $block allocated to $custid.<br>\n";
+    } # done OK?/NOK! check after DB changes
+
+  } # done "hard" allocation case.
+
   # need to get /24 that block is part of
   my @bits = split /\./, $webvar{block};
@@ -213,4 +282,5 @@
   showAllocs((join ".", @bits));
 }
+
 
 # List free blocks in a /24 for arbitrary manual allocation
@@ -298,5 +368,5 @@
     carp "Transaction aborted because $@";
     eval { $ip_dbh->rollback; };
-    syslog "err", "$authuser could not update block/IP '$webvar{block}': '$@'";
+    syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
   } else {
     # If we get here, the operation succeeded.
@@ -309,2 +379,36 @@
   showAllocs((join ".", @bits));
 }
+
+
+# showPool()
+# List all IPs in a pool, and allow arbitrary admin changes to each
+# Allow changes to ALL fields
+sub showPool($) {
+  my $pool = new NetAddr::IP $_[0];
+  print qq(Listing pool $pool:\n<table border=1>
+<form action=admin.cgi method=POST>
+<input type=hidden name=action value=updatepool>
+<tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
+<tr><td align=right>Customer location:</td><td><input name=city></td></tr>
+<tr><td align=right>Type:</td><td><select name=ptype><option selected>-</option>
+<option value="s">Static IP - Server pool</option>
+<option value="c">Static IP - Cable</option>
+<option value="d">Static IP - DSL</option>
+<option value="m">Static IP - Dialup</option>
+<option value="w">Static IP - Wireless</option>
+</select></td></tr>
+<tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
+<tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
+<tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
+<tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
+).
+	"</table>Update the following record:<table border=1>\n";
+  $sth = $ip_dbh->prepare("select * from poolips where pool='$pool' order by ip");
+  $sth->execute;
+  while (my @data = $sth->fetchrow_array) {
+    print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
+	"<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
+	"<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
+  }
+  print "</form></table>\n";
+}
