Ignore:
Timestamp:
05/15/13 16:17:00 (12 years ago)
Author:
Kris Deugau
Message:

/branches/stable

Merge SQL changes and other miscellaneous fixes from /trunk through r553.

Location:
branches/stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/stable

  • branches/stable/cgi-bin/admin.cgi

    r593 r594  
    6969}
    7070
     71if(!defined($webvar{action})) {
     72  $webvar{action} = "main";   #shuts up the warnings.
     73}
     74
    7175# handle DB error output
    7276if ($webvar{action} eq 'dberr') {
     
    8892my $header = HTML::Template->new(filename => "admin/header.tmpl");
    8993
    90 if(!defined($webvar{action})) {
    91   $webvar{action} = "main";   #shuts up the warnings.
    92 }
    93 
    9494my $page;
    9595if (-e "$ENV{HTML_TEMPLATE_ROOT}/admin/$webvar{action}.tmpl") {
     
    103103  $header->param(mainpage => 1);
    104104
    105   $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder");
    106   $sth->execute;
    107 
    108   my @typelist;
    109   my $count = 0;
    110   while (my ($type,$listname) = $sth->fetchrow_array) {
    111     my %row = (
    112         selected => $count++,
    113         type => $type,
    114         dispname => $listname
    115         );
    116     push @typelist, \%row;
    117   }
    118   $page->param(typelist => \@typelist);
    119 
    120   my @masterlist;
    121   $sth = $ip_dbh->prepare("select cidr,mtime from masterblocks order by cidr");
    122   $sth->execute;
    123   while (my ($cidr,$mtime) = $sth->fetchrow_array) {
    124     my %row = (
    125         master => $cidr,
    126         masterdate => $mtime
    127         );
    128     push @masterlist, \%row;
    129   }
    130   $page->param(masterlist => \@masterlist);
    131 
     105  my $tlist = getTypeList($ip_dbh, 'a');
     106  $tlist->[0]->{sel} = 1;
     107  $page->param(typelist => $tlist);
     108
     109  my $mlist = getMasterList($ip_dbh, 'm');
     110  $page->param(masterlist => $mlist);
    132111}
    133112
     
    136115elsif ($webvar{action} eq 'alloc') {
    137116
    138   if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) {
     117  my $cidr = new NetAddr::IP $webvar{cidr};
     118  if (!$cidr || "$cidr" =~ /^0/) {
    139119    $page->param(errmsg => "Can't allocate something that's not a netblock/ip");
    140120    goto ERRJUMP;
    141121  }
    142122
    143   $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'");
    144   $sth->execute;
    145   my @data = $sth->fetchrow_array;
    146   my $custid = $data[0];
     123  my $custid = $def_custids{$webvar{alloctype}};
    147124  if ($custid eq '') {
    148     if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
    149       # Force uppercase for now...
    150       $webvar{custid} =~ tr/a-z/A-Z/;
    151       # Crosscheck with billing.
    152       my $status = CustIDCK->custid_exist($webvar{custid});
    153       if ($CustIDCK::Error) {
    154         $page->param(errmsg => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
    155         goto ERRJUMP;
    156       }
    157       if (!$status) {
    158         $page->param(errmsg => "Customer ID not valid.  Make sure the Customer ID ".
    159           "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
    160           "non-customer assignments.");
    161         goto ERRJUMP;
    162       }
     125    # Crosscheck with billing.
     126    my $status = CustIDCK->custid_exist($webvar{custid});
     127    if ($CustIDCK::Error) {
     128      $page->param(errmsg => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
     129      goto ERRJUMP;
     130    }
     131    if (!$status) {
     132      $page->param(errmsg => "Customer ID not valid.  Make sure the Customer ID ".
     133        "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
     134        "non-customer assignments.");
     135      goto ERRJUMP;
    163136    }
    164137    # Type that doesn't have a default custid
     
    166139  }
    167140
    168   my $cidr = new NetAddr::IP $webvar{cidr};
    169   my @data;
     141  my $maskbits = $cidr->masklen;
     142  my $fbtmp = findAllocateFrom($ip_dbh, $maskbits, $webvar{alloctype}, '','',
     143        (gimme => "$cidr", allowpriv => 1));
     144
    170145  if ($webvar{alloctype} eq 'rm') {
    171     $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'");
    172     $sth->execute;
    173     @data = $sth->fetchrow_array;
    174 # User deserves errors if user can't be bothered to find the free block first.
    175     if (!$data[0]) {
     146    if (!$fbtmp) {
    176147      $page->param(errmsg => "Can't allocate from outside a free block!!");
    177148      goto ERRJUMP;
    178149    }
    179150  } elsif ($webvar{alloctype} =~ /^(.)i$/) {
    180     $sth = $ip_dbh->prepare("select cidr from allocations where cidr >>='$cidr' and (type like '_d' or type like '_p')");
    181     $sth->execute;
    182     @data = $sth->fetchrow_array;
    183 # User deserves errors if user can't be bothered to find the pool and a free IP first.
    184     if (!$data[0]) {
     151    my $iptype = $1;
     152    my $ptmp = ipParent($ip_dbh, "$cidr");
     153    if ($ptmp->{type} =~ /^(.)[dp]$/) {
     154      my $newiptype = "$1i";
     155      $fbtmp = $ptmp->{cidr};
     156      if ($ptmp->{type} !~ /^$iptype./) {
     157        $page->param(warnmsg => "Warning:  Allocating IP as '".$disp_alloctypes{$newiptype}."' instead of '".
     158                $disp_alloctypes{$webvar{alloctype}}."' to match pool $fbtmp\n");
     159        $webvar{alloctype} = $newiptype;
     160      }
     161    }
     162    if (!$fbtmp) {
    185163      $page->param(errmsg => "Can't allocate static IP from outside a pool!!");
    186164      goto ERRJUMP;
    187165    }
    188166  } else {
    189     $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')");
    190     $sth->execute;
    191     @data = $sth->fetchrow_array;
    192 # User deserves errors if user can't be bothered to find the free block first.
    193     if (!$data[0]) {
     167    if (!$fbtmp) {
    194168      $page->param(errmsg => "Can't allocate from outside a routed block!!");
    195169      goto ERRJUMP;
     
    197171  }
    198172
    199   my $alloc_from = new NetAddr::IP $data[0];
    200   $sth->finish;
     173  my $alloc_from = new NetAddr::IP $fbtmp;
    201174
    202175  my @cities;
     
    226199    $page->param(locerr => "Invalid customer location!  Go back and select customer's location.");
    227200    goto ERRJUMP;
    228   } else {
    229     if ($webvar{alloctype} =~ /^.i$/) {
    230       $sth = $ip_dbh->prepare("update poolips set available='n', custid='$webvar{custid}', ".
    231         "city='$webvar{city}', description='$webvar{desc}', notes='$webvar{notes}' ".
    232         "where ip='$webvar{cidr}'");
    233       $sth->execute;
    234       if ($sth->err) {
    235         $page->param(errmsg => $sth->errstr);
    236         syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
    237                 "'$webvar{alloctype}' failed: '".$sth->errstr."'";
    238       } else {
    239         syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
    240                 "'$webvar{alloctype}'";
    241         mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
    242           "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer $webvar{custid}\n".
    243           "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
    244       }
    245     } else {
    246       my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
     201  }
     202
     203  my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
    247204        $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
    248205        $webvar{circid});
    249       if ($retcode eq 'OK') {
    250         syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
    251                 "'$webvar{alloctype}'";
    252       } else {
    253         $page->param(errmsg => $msg);
    254         syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
    255                 "'$webvar{alloctype}' failed: '$msg'";
    256       }
    257     } # static IP vs netblock
    258 
    259   } # done city check
     206  if ($retcode eq 'OK') {
     207    syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
     208        "'$webvar{alloctype}'";
     209    if ($webvar{alloctype} =~ /^.i$/) {
     210      mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
     211        "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer $webvar{custid}\n".
     212        "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
     213    }
     214  } else {
     215    $page->param(errmsg => $msg);
     216    syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
     217        "'$webvar{alloctype}' failed: '$msg'";
     218  }
    260219
    261220} elsif ($webvar{action} eq 'alloctweak') {
     
    270229} elsif ($webvar{action} eq 'touch') {
    271230
    272   $page->param(master => $webvar{whichmaster});
    273   $sth = $ip_dbh->prepare("update masterblocks set mtime=now() where cidr='$webvar{whichmaster}'");
    274   $sth->execute;
    275   if ($sth->err) {
    276     $page->param(errmsg => $sth->errstr);
    277   }
     231  my ($code,$msg) = touchMaster($ip_dbh, $webvar{whichmaster});
     232  $page->param(errmsg => $msg) if $code eq 'FAIL';
    278233
    279234} elsif ($webvar{action} eq 'listcust') {
     
    612567# List all IPs in a pool, and allow arbitrary admin changes to each
    613568# Allow changes to ALL fields
    614 sub showPool($) {
     569sub showPool {
    615570  my $pool = new NetAddr::IP $_[0];
    616571
     
    627582  $page->param(typelist => \@typelist);
    628583
    629   $sth = $ip_dbh->prepare("select ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
    630   $sth->execute;
     584  $sth = $ip_dbh->prepare("SELECT ip,custid,city,type,available,description,notes from poolips".
     585        " WHERE pool=? ORDER BY ip");
     586  $sth->execute($pool);
    631587  my @iplist;
    632588  while (my ($ip,$custid,$city,$type,$avail,$desc,$notes) = $sth->fetchrow_array) {
Note: See TracChangeset for help on using the changeset viewer.