Changeset 199 for trunk


Ignore:
Timestamp:
03/17/05 12:30:51 (19 years ago)
Author:
Kris Deugau
Message:

/trunk

Update admin.cgi (such as it was) to newer IPDB backend code.
Add conditional link in "main" interface for specific users.

Location:
trunk
Files:
3 edited

Legend:

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

    r146 r199  
    1717use DBI;
    1818use CommonWeb qw(:ALL);
    19 use IPDB qw(:ALL);
     19use MyIPDB;
    2020#use POSIX qw(ceil);
    2121use NetAddr::IP;
     
    4343syslog "debug", "$authuser active";
    4444
     45# Why not a global DB handle?  (And a global statement handle, as well...)
     46# Use the connectDB function, otherwise we end up confusing ourselves
     47my $ip_dbh;
     48my $sth;
     49my $errstr;
     50($ip_dbh,$errstr) = connectDB_My;
     51if (!$ip_dbh) {
     52  printAndExit("Database error: $errstr\n");
     53}
     54initIPDBGlobals($ip_dbh);
     55
    4556my %webvar = parse_post();
    4657cleanInput(\%webvar);
    4758
    48 my %full_alloc_types = (
    49         "ci","Cable pool IP",
    50         "di","DSL pool IP",
    51         "si","Server pool IP",
    52         "mi","Static dialup IP",
    53         "wi","Static wireless IP",
    54         "cp","Cable pool",
    55         "dp","DSL pool",
    56         "sp","Server pool",
    57         "mp","Static dialup pool",
    58         "wp","Static wireless pool",
    59         "dn","Dialup netblock",
    60         "dy","Dynamic DSL netblock",
    61         "dc","Dynamic cable netblock",
    62         "cn","Customer netblock",
    63         "ee","End-use netblock",
    64         "rr","Routed netblock",
    65         "ii","Internal netblock",
    66         "mm","Master block"
    67 );
    68 
    69 my $ip_dbh = connectDB;
    70 my $sth;
    71 
    7259print "Content-type: text/html\n\n".
    73         "<html>\n<head>\n\t<title>TEST [IPDB admin tools] TEST</title>\n</head>\n<body>\n".
     60        "<html>\n<head>\n\t<title>TEST [IPDB admin tools] TEST</title>\n".
     61        qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n).
     62        "</head>\n<body>\n".
    7463        "<h2>IPDB - Administrative Tools</h2>\n<hr>\n";
    7564
    7665if(!defined($webvar{action})) {
    7766  $webvar{action} = "<NULL>";   #shuts up the warnings.
     67
     68  my $typelist = '';
     69  $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder");
     70  $sth->execute;
     71  my @data = $sth->fetchrow_array;
     72  $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
     73  while (my @data = $sth->fetchrow_array) {
     74    $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
     75  }
     76
    7877  print qq(WARNING:  There are FAR fewer controls on what you can do here.  Use the
    7978main interface if at all possible.
    80 <hr><form action="admin.cgi" method="POST">
     79<hr>
     80<a href="admin.cgi?action=newalloc">Add allocation</a>
     81<hr>
     82<form action="admin.cgi" method="POST">
    8183<input type=hidden name=action value=alloc>
    82 Allocate block from this /24: <input name=allocfrom>
    83 <input type=submit value="List available free blocks">
    84 </form>
     84Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid>
     85<input type=submit value=" GIMME!! "></form>
    8586<hr><form action="admin.cgi" method="POST">
    8687<input type=hidden name=action value=alloctweak>
     
    9495}
    9596
     97
     98## Possible actions.
    9699if ($webvar{action} eq 'alloc') {
    97   fix_allocfrom();
    98   showfree($webvar{allocfrom});
     100  # OK, we know what we're allocating.
     101
     102  if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) {
     103    printAndExit("Can't allocate something that's not a netblock/ip");
     104  }
     105
     106  $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'");
     107  $sth->execute;
     108  my @data = $sth->fetchrow_array;
     109  my $custid = $data[0];
     110  if ($custid eq '') {
     111    # Type that doesn't have a default custid
     112    $custid = $webvar{custid};
     113  }
     114##fixme Check billing DB here
     115
     116  my $cidr = new NetAddr::IP $webvar{cidr};
     117  my @data;
     118  if ($webvar{alloctype} eq 'rm') {
     119    $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'");
     120    $sth->execute;
     121    @data = $sth->fetchrow_array;
     122# User deserves errors if user can't be bothered to find the free block first.
     123    printAndExit("Can't allocate from outside a free block!!\n")
     124        if !$data[0];
     125  } else {
     126    $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')");
     127    $sth->execute;
     128    @data = $sth->fetchrow_array;
     129# User deserves errors if user can't be bothered to find the free block first.
     130    printAndExit("Can't allocate from outside a routed block!!\n")
     131        if !$data[0];
     132  }
     133
     134  my $alloc_from = new NetAddr::IP $data[0];
     135  $sth->finish;
     136
     137  my $cities = '';
     138  foreach my $city (@citylist) {
     139    $cities .= "<option>$city</option>\n";
     140  }
     141
     142  print qq(<table class=regular>
     143<form method=POST action=admin.cgi>
     144<tr class=color1>
     145<td>Allocating:</td>
     146<td>$cidr<input type=hidden name=cidr value="$cidr"></td>
     147</tr><tr class=color2>
     148<td>Type:</td><td>$disp_alloctypes{$webvar{alloctype}}
     149<input type=hidden name=alloctype value="$webvar{alloctype}"></td>
     150</tr><tr class=color1>
     151<td>Allocated from:</td>
     152<td>$alloc_from<input type=hidden name=alloc_from value="$alloc_from"></td>
     153</tr><tr class="color2">
     154<td>Customer ID:</td><td>$custid<input type=hidden name=custid value="$custid"></td>
     155</tr><tr class=color1>
     156<td>Customer location:</td><td>
     157<select name="city"><option selected>-</option>
     158$cities
     159</select>
     160&nbsp;<a href="javascript:popNotes('/ip/newcity.html')">Add new location</a>
     161</td>
     162</tr>
     163<tr class="color2">
     164<td>Circuit ID:</td><td><input name=circid size=40></td>
     165</tr><tr class="color1">
     166<td>Description/Name:</td><td><input name="desc" size=40></td>
     167</tr><tr class="color2">
     168<td>Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td>
     169</tr><tr class="warning">
     170<td colspan=2><center>WARNING:  This will IMMEDIATELY assign this block!!</center></td>
     171</tr><tr class="color2">
     172<td class="center" colspan="2"><input type="submit" value="  Assign  "></td>
     173<input type="hidden" name="action" value="confirm">
     174</tr>
     175</table>
     176);
     177
     178
     179} elsif ($webvar{action} eq 'confirm') {
     180
     181  print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ".
     182        "$disp_alloctypes{$webvar{alloctype}}...<br>\n";
     183  # Only need to check city here.
     184  if ($webvar{city} eq '-') {
     185    printError("Invalid customer location!  Go back and select customer's location.");
     186  } else {
     187    my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
     188        $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
     189        $webvar{circid});
     190    if ($retcode eq 'OK') {
     191      print "Allocation OK!\n";
     192
     193      if ($webvar{alloctype} =~ /^.i$/) {
     194        # Notify tech@example.com
     195        mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation",
     196          "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
     197          "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
     198      }
     199      syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
     200        "'$webvar{alloctype}'";
     201    } else {
     202      print "Allocation failed!  IPDB::allocateBlock said:\n$msg\n";
     203      syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
     204        "'$webvar{type}' failed: '$msg'";
     205    }
     206  } # done city check
     207
    99208} elsif ($webvar{action} eq 'alloctweak') {
    100209  fix_allocfrom();
     
    119228
    120229  print $html;
    121 } elsif ($webvar{action} eq 'confirm') {
    122   print "Assigning $webvar{block} to $webvar{custid} (\"$webvar{desc}\")...\n";
    123   allocBlock($ip_dbh, $webvar{allocfrom}, $webvar{block}, $webvar{alloctype},
    124         $webvar{custid}, $webvar{city}, $webvar{desc}, $webvar{notes});
    125   #my ($dbh,from,block,$type,$custid,$city,$desc,$notes) = @_;
    126230} elsif ($webvar{action} eq 'showpools') {
    127231  print "IP Pools currently allocated:\n".
    128232        "<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");
     233  $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
    130234  $sth->execute;
    131235  my %poolfree;
     
    146250  showPool($webvar{pool});
    147251} elsif ($webvar{action} eq 'updatepool') {
     252
    148253  $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
    149         "city='$webvar{city}', ptype='$webvar{ptype}', available='".
     254        "city='$webvar{city}', type='$webvar{type}', available='".
    150255        (($webvar{available} eq 'y') ? 'y' : 'n').
    151256        "', notes='$webvar{notes}', description='$webvar{desc}' ".
     
    162267    syslog "notice", "$authuser updated pool IP $webvar{ip}";
    163268  }
    164   showPool("$data[0]");
     269#  showPool("$data[0]");
    165270#} else {
    166271#  print "webvar{action} check failed: $webvar{action}";
     
    189294    $webvar{allocfrom} .= "/24";
    190295  }
    191 }
    192 
    193 
    194 # Do the gruntwork of allocating a block.  This should really be in IPDB.pm.
    195 sub allocBlock($$$$$$$$) {
    196   my ($dbh,undef,undef,$type,$custid,$city,$desc,$notes) = @_;
    197   my $from = new NetAddr::IP $_[1];
    198   my $block = new NetAddr::IP $_[2];
    199 
    200   local $ip_dbh->{AutoCommit} = 0;  # enable transactions, if possible
    201   local $ip_dbh->{RaiseError} = 1;  # Use local to limit to this sub
    202 
    203   if ($from eq $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     }
    228   } else {
    229     # The complex case.  An allocation from a larger block.
    230    
    231     # Gotta snag the free blocks left over.
    232     my $wantmaskbits = $block->masklen;
    233     my $maskbits = $from->masklen;
    234 
    235     my @newfreeblocks;  # Holds free blocks generated from splitting the source freeblock.
    236 
    237     my $i=0;
    238     my $tmp_from = $from;       # So we don't munge $from
    239     while ($maskbits++ < $wantmaskbits) {
    240       my @subblocks = $tmp_from->split($maskbits);
    241       $newfreeblocks[$i++] = (($block->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
    242       $tmp_from = ( ($block->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
    243     } # while
    244 
    245 # insert the data here.  Woo.
    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);
    257       }
    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 
    279   # need to get /24 that block is part of
    280   my @bits = split /\./, $webvar{block};
    281   $bits[3] = "0/24";
    282   showAllocs((join ".", @bits));
    283296}
    284297
     
    330343        (($data[2] eq 'mi') ? ' selected' : '') ." value='mi'>Static IP - Dialup</option>\n<option".
    331344        (($data[2] eq 'wi') ? ' selected' : '') ." value='wi'>Static IP - Wireless</option>\n<option".
    332         (($data[2] eq 'sp') ? ' selected' : '') ." value='sp'>Static Pool - Server pool</option>\n<option".
    333         (($data[2] eq 'cp') ? ' selected' : '') ." value='cp'>Static Pool - Cable</option>\n<option".
     345        (($data[2] eq 'sd') ? ' selected' : '') ." value='sd'>Static Pool - Server pool</option>\n<option".
     346        (($data[2] eq 'cd') ? ' selected' : '') ." value='cd'>Static Pool - Cable</option>\n<option".
    334347        (($data[2] eq 'dp') ? ' selected' : '') ." value='dp'>Static Pool - DSL</option>\n<option".
    335348        (($data[2] eq 'mp') ? ' selected' : '') ." value='mp'>Static Pool - Dialup</option>\n<option".
    336349        (($data[2] eq 'wp') ? ' selected' : '') ." value='wp'>Static Pool - Wireless</option>\n<option".
    337         (($data[2] eq 'ee') ? ' selected' : '') ." value='ee'>End-use netblock</option>\n<option".
    338         (($data[2] eq 'dn') ? ' selected' : '') ." value='dn'>Dialup netblock</option>\n<option".
    339         (($data[2] eq 'dy') ? ' selected' : '') ." value='dy'>Dynamic DSL netblock</option>\n<option".
    340         (($data[2] eq 'dc') ? ' selected' : '') ." value='dc'>Dynamic cable netblock</option>\n<option".
    341         (($data[2] eq 'ii') ? ' selected' : '') ." value='ii'>Internal netblock</option>\n".
     350        (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option".
     351        (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
     352        (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".
     353        (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option".
     354        (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic WiFi netblock</option>\n<option".
     355        (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
    342356        "</select></td>\n";
    343357    print qq(<td><input name=city value="$data[3]"></td>\n).
     
    391405<tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
    392406<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>
     407<tr><td align=right>Type:</td><td><select name=type><option selected>-</option>
     408<option value="si">Static IP - Server pool</option>
     409<option value="ci">Static IP - Cable</option>
     410<option value="di">Static IP - DSL</option>
     411<option value="mi">Static IP - Dialup</option>
     412<option value="wi">Static IP - Wireless</option>
    399413</select></td></tr>
    400414<tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
     
    404418).
    405419        "</table>Update the following record:<table border=1>\n";
    406   $sth = $ip_dbh->prepare("select * from poolips where pool='$pool' order by ip");
     420  $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
    407421  $sth->execute;
    408422  while (my @data = $sth->fetchrow_array) {
  • trunk/cgi-bin/main.cgi

    r191 r199  
    168168# Clean up IPDB globals, DB handle, etc.
    169169finish($ip_dbh);
     170
     171print qq(<div align=right style="position: absolute; right: 30px;">).
     172        qq(<a href="/ip/cgi-bin/admin.cgi">Admin tools</a></div><br>\n)
     173        if $authuser =~ /kdeugau|jodyh/;
     174
    170175# We print the footer here, so we don't have to do it elsewhere.
    171176printFooter;
  • trunk/ipdb.css

    r4 r199  
    3232
    3333tr.warning {
    34         background-color: #304E5A;
     34        background-color: #000000;
    3535        font-family: Verdana, Arial, Helvetica, sans-serif;
     36        font-weight: bold;
     37        color: red;
    3638}
    3739
Note: See TracChangeset for help on using the changeset viewer.