Index: branches/stable/cgi-bin/admin.cgi
===================================================================
--- branches/stable/cgi-bin/admin.cgi	(revision 195)
+++ branches/stable/cgi-bin/admin.cgi	(revision 200)
@@ -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
@@ -16,5 +17,6 @@
 use DBI;
 use CommonWeb qw(:ALL);
-use IPDB qw(:ALL);
+use MyIPDB;
+use CustIDCK;
 #use POSIX qw(ceil);
 use NetAddr::IP;
@@ -42,48 +44,51 @@
 syslog "debug", "$authuser active";
 
+# Why not a global DB handle?  (And a global statement handle, as well...)
+# Use the connectDB function, otherwise we end up confusing ourselves
+my $ip_dbh;
+my $sth;
+my $errstr;
+($ip_dbh,$errstr) = connectDB_My;
+if (!$ip_dbh) {
+  printAndExit("Database error: $errstr\n");
+}
+initIPDBGlobals($ip_dbh);
+
 my %webvar = parse_post();
 cleanInput(\%webvar);
 
-my %full_alloc_types = (
-	"ci","Cable pool IP",
-	"di","DSL pool IP",
-	"si","Server pool IP",
-	"mi","Static dialup IP",
-	"wi","Static wireless IP",
-	"cp","Cable pool",
-	"dp","DSL pool",
-	"sp","Server pool",
-	"mp","Static dialup pool",
-	"wp","Static wireless pool",
-	"dn","Dialup netblock",
-	"dy","Dynamic DSL netblock",
-	"dc","Dynamic cable netblock",
-	"cn","Customer netblock",
-	"ee","End-use netblock",
-	"rr","Routed netblock",
-	"ii","Internal netblock",
-	"mm","Master block"
-);
-
-my $ip_dbh = connectDB;
-my $sth;
-
 print "Content-type: text/html\n\n".
-	"<html>\n<head>\n\t<title>TEST [IPDB admin tools] TEST</title>\n</head>\n<body>\n".
+	"<html>\n<head>\n\t<title>TEST [IPDB admin tools] TEST</title>\n".
+	qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n).
+	"</head>\n<body>\n".
 	"<h2>IPDB - Administrative Tools</h2>\n<hr>\n";
 
 if(!defined($webvar{action})) {
   $webvar{action} = "<NULL>";   #shuts up the warnings.
+
+  my $typelist = '';
+  $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder");
+  $sth->execute;
+  my @data = $sth->fetchrow_array;
+  $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
+  while (my @data = $sth->fetchrow_array) {
+    $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
+  }
+
   print qq(WARNING:  There are FAR fewer controls on what you can do here.  Use the
 main interface if at all possible.
-<hr><form action="admin.cgi" method="POST">
+<hr>
+<a href="admin.cgi?action=newalloc">Add allocation</a>
+<hr>
+<form action="admin.cgi" method="POST">
 <input type=hidden name=action value=alloc>
-Allocate block from this /24: <input name=allocfrom>
-<input type=submit value="List available free blocks">
-</form>
+Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid>
+<input type=submit value=" GIMME!! "></form>
 <hr><form action="admin.cgi" method="POST">
 <input type=hidden name=action value=alloctweak>
 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 {
@@ -91,7 +96,130 @@
 }
 
+
+## Possible actions.
 if ($webvar{action} eq 'alloc') {
-  fix_allocfrom();
-  showfree($webvar{allocfrom});
+  # OK, we know what we're allocating.
+
+  if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) {
+    printAndExit("Can't allocate something that's not a netblock/ip");
+  }
+
+  $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'");
+  $sth->execute;
+  my @data = $sth->fetchrow_array;
+  my $custid = $data[0];
+  if ($custid eq '') {
+    if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
+      # Force uppercase for now...
+      $webvar{custid} =~ tr/a-z/A-Z/;
+      # Crosscheck with ... er...  something.
+      my $status = CustIDCK->custid_exist($webvar{custid});
+      if ($CustIDCK::Error) {
+	printError("Error verifying customer ID: ".$CustIDCK::ErrMsg);
+	return;
+      }
+      if (!$status) {
+	printError("Customer ID not valid.  Make sure the Customer ID ".
+	  "is correct.<br>\nUse STAFF for staff static IPs, and 6750400 for any other ".
+	  "non-customer assignments.");
+	return;
+      }
+    }
+    # Type that doesn't have a default custid
+    $custid = $webvar{custid};
+  }
+
+  my $cidr = new NetAddr::IP $webvar{cidr};
+  my @data;
+  if ($webvar{alloctype} eq 'rm') {
+    $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'");
+    $sth->execute;
+    @data = $sth->fetchrow_array;
+# User deserves errors if user can't be bothered to find the free block first.
+    printAndExit("Can't allocate from outside a free block!!\n")
+        if !$data[0];
+  } else {
+    $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')");
+    $sth->execute;
+    @data = $sth->fetchrow_array;
+# User deserves errors if user can't be bothered to find the free block first.
+    printAndExit("Can't allocate from outside a routed block!!\n")
+        if !$data[0];
+  }
+
+  my $alloc_from = new NetAddr::IP $data[0];
+  $sth->finish;
+
+  my $cities = '';
+  foreach my $city (@citylist) {
+    $cities .= "<option>$city</option>\n";
+  }
+
+  print qq(<table class=regular>
+<form method=POST action=admin.cgi>
+<tr class=color1>
+<td>Allocating:</td>
+<td>$cidr<input type=hidden name=cidr value="$cidr"></td>
+</tr><tr class=color2>
+<td>Type:</td><td>$disp_alloctypes{$webvar{alloctype}}
+<input type=hidden name=alloctype value="$webvar{alloctype}"></td>
+</tr><tr class=color1>
+<td>Allocated from:</td>
+<td>$alloc_from<input type=hidden name=alloc_from value="$alloc_from"></td>
+</tr><tr class="color2">
+<td>Customer ID:</td><td>$custid<input type=hidden name=custid value="$custid"></td>
+</tr><tr class=color1>
+<td>Customer location:</td><td>
+<select name="city"><option selected>-</option>
+$cities
+</select>
+&nbsp;<a href="javascript:popNotes('/ip/newcity.html')">Add new location</a>
+</td>
+</tr>
+<tr class="color2">
+<td>Circuit ID:</td><td><input name=circid size=40></td>
+</tr><tr class="color1">
+<td>Description/Name:</td><td><input name="desc" size=40></td>
+</tr><tr class="color2">
+<td>Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td>
+</tr><tr class="warning">
+<td colspan=2><center>WARNING:  This will IMMEDIATELY assign this block!!</center></td>
+</tr><tr class="color2">
+<td class="center" colspan="2"><input type="submit" value="  Assign  "></td>
+<input type="hidden" name="action" value="confirm">
+</tr>
+</table>
+);
+
+
+} elsif ($webvar{action} eq 'confirm') {
+
+  print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ".
+	"$disp_alloctypes{$webvar{alloctype}}...<br>\n";
+  # Only need to check city here.
+  if ($webvar{city} eq '-') {
+    printError("Invalid customer location!  Go back and select customer's location.");
+  } else {
+    my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
+	$webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
+	$webvar{circid});
+    if ($retcode eq 'OK') {
+      print "Allocation OK!\n";
+
+      if ($webvar{alloctype} =~ /^.i$/) {
+        # Notify tech@example.com
+        mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation",
+          "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
+          "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
+      }
+      syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
+	"'$webvar{alloctype}'";
+    } else {
+      print "Allocation failed!  IPDB::allocateBlock said:\n$msg\n";
+      syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
+	"'$webvar{type}' failed: '$msg'";
+    }
+  } # done city check
+
 } elsif ($webvar{action} eq 'alloctweak') {
   fix_allocfrom();
@@ -116,11 +244,46 @@
 
   print $html;
-} elsif ($webvar{action} eq 'confirm') {
-  print "Assigning $webvar{block} to $webvar{custid} (\"$webvar{desc}\")...\n";
-  allocBlock($ip_dbh, $webvar{allocfrom}, $webvar{block}, $webvar{alloctype},
-	$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' or type like '%d' 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}', type='$webvar{type}', 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}";
 }
 
@@ -149,69 +312,4 @@
 }
 
-
-# Do the gruntwork of allocating a block.  This should really be in IPDB.pm.
-sub allocBlock($$$$$$$$) {
-  my ($dbh,undef,undef,$type,$custid,$city,$desc,$notes) = @_;
-  my $from = new NetAddr::IP $_[1];
-  my $block = new NetAddr::IP $_[2];
-
-  # First, figure out what free blocks will get mangled.
-  if ($from eq $block) {
-    # Whee!  Easy.  Just allocate the block
-  } else {
-    # The complex case.  An allocation from a larger block.
-    
-    # Gotta snag the free blocks left over.
-    my $wantmaskbits = $block->masklen;
-    my $maskbits = $from->masklen;
-
-    my @newfreeblocks;	# Holds free blocks generated from splitting the source freeblock.
-
-    my $i=0;
-    my $tmp_from = $from;	# So we don't munge $from
-    while ($maskbits++ < $wantmaskbits) {
-      my @subblocks = $tmp_from->split($maskbits);
-      $newfreeblocks[$i++] = (($block->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
-      $tmp_from = ( ($block->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
-    } # while
-
-# 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";
-      }
-
-  }
-  # need to get /24 that block is part of
-  my @bits = split /\./, $webvar{block};
-  $bits[3] = "0/24";
-  showAllocs((join ".", @bits));
-}
 
 # List free blocks in a /24 for arbitrary manual allocation
@@ -261,14 +359,15 @@
 	(($data[2] eq 'mi') ? ' selected' : '') ." value='mi'>Static IP - Dialup</option>\n<option".
 	(($data[2] eq 'wi') ? ' selected' : '') ." value='wi'>Static IP - Wireless</option>\n<option".
-	(($data[2] eq 'sp') ? ' selected' : '') ." value='sp'>Static Pool - Server pool</option>\n<option".
-	(($data[2] eq 'cp') ? ' selected' : '') ." value='cp'>Static Pool - Cable</option>\n<option".
+	(($data[2] eq 'sd') ? ' selected' : '') ." value='sd'>Static Pool - Server pool</option>\n<option".
+	(($data[2] eq 'cd') ? ' selected' : '') ." value='cd'>Static Pool - Cable</option>\n<option".
 	(($data[2] eq 'dp') ? ' selected' : '') ." value='dp'>Static Pool - DSL</option>\n<option".
 	(($data[2] eq 'mp') ? ' selected' : '') ." value='mp'>Static Pool - Dialup</option>\n<option".
 	(($data[2] eq 'wp') ? ' selected' : '') ." value='wp'>Static Pool - Wireless</option>\n<option".
-	(($data[2] eq 'ee') ? ' selected' : '') ." value='ee'>End-use netblock</option>\n<option".
-	(($data[2] eq 'dn') ? ' selected' : '') ." value='dn'>Dialup netblock</option>\n<option".
-	(($data[2] eq 'dy') ? ' selected' : '') ." value='dy'>Dynamic DSL netblock</option>\n<option".
-	(($data[2] eq 'dc') ? ' selected' : '') ." value='dc'>Dynamic cable netblock</option>\n<option".
-	(($data[2] eq 'ii') ? ' selected' : '') ." value='ii'>Internal netblock</option>\n".
+	(($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option".
+	(($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
+	(($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".
+	(($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option".
+	(($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic WiFi netblock</option>\n<option".
+	(($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
         "</select></td>\n";
     print qq(<td><input name=city value="$data[3]"></td>\n).
@@ -299,5 +398,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.
@@ -310,2 +409,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=type><option selected>-</option>
+<option value="si">Static IP - Server pool</option>
+<option value="ci">Static IP - Cable</option>
+<option value="di">Static IP - DSL</option>
+<option value="mi">Static IP - Dialup</option>
+<option value="wi">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 pool,ip,custid,city,type,available,description,notes 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";
+}
Index: branches/stable/cgi-bin/main.cgi
===================================================================
--- branches/stable/cgi-bin/main.cgi	(revision 195)
+++ branches/stable/cgi-bin/main.cgi	(revision 200)
@@ -169,4 +169,9 @@
 # Clean up IPDB globals, DB handle, etc.
 finish($ip_dbh);
+
+print qq(<div align=right style="position: absolute; right: 30px;">).
+	qq(<a href="/ip/cgi-bin/admin.cgi">Admin tools</a></div><br>\n)
+	if $authuser =~ /kdeugau|jodyh/;
+
 # We print the footer here, so we don't have to do it elsewhere.
 printFooter;
Index: branches/stable/ipdb.css
===================================================================
--- branches/stable/ipdb.css	(revision 195)
+++ branches/stable/ipdb.css	(revision 200)
@@ -32,6 +32,8 @@
 
 tr.warning {
-	background-color: #304E5A;
+	background-color: #000000;
 	font-family: Verdana, Arial, Helvetica, sans-serif;
+	font-weight: bold;
+	color: red;
 }
 
