Changeset 58 for trunk/cgi-bin/admin.cgi


Ignore:
Timestamp:
11/10/04 16:57:34 (20 years ago)
Author:
Kris Deugau
Message:

/trunk

Finished first "complete" edition of admin interface for IPDB.

File:
1 edited

Legend:

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

    r54 r58  
    4343my %webvar = parse_post();
    4444cleanInput(\%webvar);
     45
     46my %full_alloc_types = (
     47        "ci","Cable pool IP",
     48        "di","DSL pool IP",
     49        "si","Server pool IP",
     50        "mi","Static dialup IP",
     51        "wi","Static wireless IP",
     52        "cp","Cable pool",
     53        "dp","DSL pool",
     54        "sp","Server pool",
     55        "mp","Static dialup pool",
     56        "wp","Static wireless pool",
     57        "dn","Dialup netblock",
     58        "dy","Dynamic DSL netblock",
     59        "dc","Dynamic cable netblock",
     60        "cn","Customer netblock",
     61        "ee","End-use netblock",
     62        "rr","Routed netblock",
     63        "ii","Internal netblock",
     64        "mm","Master block"
     65);
     66
    4567my $ip_dbh = connectDB;
    4668my $sth;
    4769
    4870print "Content-type: text/html\n\n".
    49         "<html>\n<head>\n\t<title>IPDB admin tools</title>\n</head>\n<body>\n".
     71        "<html>\n<head>\n\t<title>TEST [IPDB admin tools] TEST</title>\n</head>\n<body>\n".
    5072        "<h2>IPDB - Administrative Tools</h2>\n<hr>\n";
    5173
     
    7698} elsif ($webvar{action} eq 'update') {
    7799  update();
     100} elsif ($webvar{action} eq 'assign') {
     101  # Display a list of possible blocks within the requested block.
     102  open (HTML, "../admin_alloc.html")
     103        or croak "Could not open admin_alloc.html :$!";
     104  my $html = join('', <HTML>);
     105  $html =~ s/\$\$MASK\$\$/$webvar{masklen}/g;
     106  $html =~ s/\$\$ALLOCFROM\$\$/$webvar{allocfrom}/g;
     107
     108  my $from = new NetAddr::IP $webvar{allocfrom};
     109  my @blocklist = $from->split($webvar{masklen});
     110  my $availblocks;
     111  foreach (@blocklist) {
     112    $availblocks .= qq(<tr><td colspan=2 align=center><input type=radio name=block value="$_">$_</td></tr>\n);
     113  }
     114  $html =~ s/\$\$BLOCKLIST\$\$/$availblocks/g;
     115
     116  print $html;
     117} elsif ($webvar{action} eq 'confirm') {
     118  print "Assigning $webvar{block} to $webvar{custid} (\"$webvar{desc}\")...\n";
     119  allocBlock($ip_dbh, $webvar{allocfrom}, $webvar{block}, $webvar{alloctype},
     120        $webvar{custid}, $webvar{city}, $webvar{desc}, $webvar{notes});
     121  #my ($dbh,from,block,$type,$custid,$city,$desc,$notes) = @_;
     122} else {
     123  print "webvar{action} check failed";
    78124}
    79125
     
    90136exit;
    91137
    92 
    93 # List free blocks in a /24 for arbitrary manual allocation
    94 sub showfree($) {
    95   my $cidr = $_[0];
    96   print "Showing free blocks in $cidr\n";
    97 }
    98138
    99139# Tweak allocfrom into shape.
     
    106146    $webvar{allocfrom} .= "/24";
    107147  }
     148}
     149
     150
     151# Do the gruntwork of allocating a block.  This should really be in IPDB.pm.
     152sub allocBlock($$$$$$$$) {
     153  my ($dbh,undef,undef,$type,$custid,$city,$desc,$notes) = @_;
     154  my $from = new NetAddr::IP $_[1];
     155  my $block = new NetAddr::IP $_[2];
     156
     157  # First, figure out what free blocks will get mangled.
     158  if ($from eq $block) {
     159    # Whee!  Easy.  Just allocate the block
     160  } else {
     161    # The complex case.  An allocation from a larger block.
     162   
     163    # Gotta snag the free blocks left over.
     164    my $wantmaskbits = $block->masklen;
     165    my $maskbits = $from->masklen;
     166
     167    my @newfreeblocks;  # Holds free blocks generated from splitting the source freeblock.
     168
     169    my $i=0;
     170    my $tmp_from = $from;       # So we don't munge $from
     171    while ($maskbits++ < $wantmaskbits) {
     172      my @subblocks = $tmp_from->split($maskbits);
     173      $newfreeblocks[$i++] = (($block->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
     174      $tmp_from = ( ($block->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
     175    } # while
     176
     177# 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";
     207      }
     208
     209  }
     210  # need to get /24 that block is part of
     211  my @bits = split /\./, $webvar{block};
     212  $bits[3] = "0/24";
     213  showAllocs((join ".", @bits));
     214}
     215
     216# List free blocks in a /24 for arbitrary manual allocation
     217sub showfree($) {
     218  my $cidr = new NetAddr::IP $_[0];
     219  print "Showing free blocks in $cidr<br>\n".
     220        "<table border=1>\n";
     221  $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr");
     222  $sth->execute;
     223  while (my @data = $sth->fetchrow_array) {
     224    my $temp = new NetAddr::IP $data[0];
     225    print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n".
     226        qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n).
     227        "<td>".
     228        (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30'
     229          : "<select name=masklen><option>30</option>\n<option>29</option>\n") .
     230        (($temp->masklen < 29) ? "<option>28</option>\n" : '') .
     231        (($temp->masklen < 28) ? "<option>27</option>\n" : '') .
     232        (($temp->masklen < 27) ? "<option>26</option>\n" : '') .
     233        (($temp->masklen < 26) ? "<option>25</option>\n" : '') .
     234        (($temp->masklen < 25) ? "<option>24</option>\n" : '') .
     235        "</td>".
     236        qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>).
     237        "\n</form></tr>\n";
     238  }
     239  print "</table>\n";
    108240}
    109241
Note: See TracChangeset for help on using the changeset viewer.