Changeset 3


Ignore:
Timestamp:
08/18/09 18:04:14 (15 years ago)
Author:
Kris Deugau
Message:

/trunk

Checkpoint
Continued development to reach ~ VegaDNS equivalence

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r2 r3  
    2323@ISA            = qw(Exporter);
    2424@EXPORT_OK      = qw(
    25         &initGlobals &connectDB &finish &addDomain &domainName &getSOA &getDomRecs &addRec
     25        &initGlobals &connectDB &finish &addDomain &delDomain &domainName &getSOA &getRecLine &getDomRecs
     26        &addRec &delRec &domStatus
    2627        %typemap %reverse_typemap
    2728        );
     
    2930@EXPORT         = (); # Export nothing by default.
    3031%EXPORT_TAGS    = ( ALL => [qw(
    31                 &initGlobals &connectDB &finish &addDomain &domainName &getSOA &getDomRecs &addRec
     32                &initGlobals &connectDB &finish &addDomain &delDomain &domainName &getSOA &getRecLine &getDomRecs
     33                &addRec &delRec &domStatus
    3234                %typemap %reverse_typemap
    3335                )]
     
    159161  return ('FAIL',"Need domain status") if !defined($state);
    160162
     163  my $dom_id;
     164
    161165  # Allow transactions, and raise an exception on errors so we can catch it later.
    162166  # Use local to make sure these get "reset" properly on exiting this block
     
    173177    $sth = $dbh->prepare("select domain_id from domains where domain='$domain'");
    174178    $sth->execute;
    175     my ($dom_id) = $sth->fetchrow_array();
     179    ($dom_id) = $sth->fetchrow_array();
    176180
    177181    # ... and now we construct the standard records from the default set.  NB:  group should be variable.
    178     $sth = $dbh->prepare("select host,type,val,distance,ttl from default_records where group_id=$group");
    179     my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,ttl)".
    180         " values ($dom_id,?,?,?,?,?)");
     182    $sth = $dbh->prepare("select host,type,val,distance,weight,port,ttl from default_records where group_id=$group");
     183    my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,weight,port,ttl)".
     184        " values ($dom_id,?,?,?,?,?,?,?)");
    181185    $sth->execute;
    182     while (my ($host,$type,$val,$dist,$ttl) = $sth->fetchrow_array()) {
     186    while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) {
    183187      $host =~ s/DOMAIN/$domain/g;
    184       $sth_in->execute($host,$type,$val,$dist,$ttl);
     188      $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl);
    185189    }
     190
     191    # once we get here, we should have suceeded.
     192    $dbh->commit;
     193  }; # end eval
     194
     195  if ($@) {
     196    my $msg = $@;
     197    eval { $dbh->rollback; };
     198    return ('FAIL',$msg);
     199  } else {
     200    return ('OK',$dom_id);
     201  }
     202} # end addDomain
     203
     204
     205## DNSDB::delDomain()
     206# Delete a domain.
     207# for now, just delete the records, then the domain.
     208# later we may want to archive it in some way instead (status code 2, for example?)
     209sub delDomain {
     210  my $dbh = shift;
     211  my $domain = shift;
     212
     213  # Allow transactions, and raise an exception on errors so we can catch it later.
     214  # Use local to make sure these get "reset" properly on exiting this block
     215  local $dbh->{AutoCommit} = 0;
     216  local $dbh->{RaiseError} = 1;
     217
     218  # Wrap all the SQL in a transaction
     219  eval {
     220    # brute force.
     221    my $sth = $dbh->prepare("select domain_id from domains where domain=?");
     222    $sth->execute($domain);
     223    die "Domain not found, can't delete\n" if $sth->rows < 1;
     224    my ($id) = $sth->fetchrow_array;
     225
     226    $sth = $dbh->prepare("delete from records where domain_id=$id");
     227    $sth->execute;
     228    $sth = $dbh->prepare("delete from domains where domain=?");
     229    $sth->execute($domain);
    186230
    187231    # once we get here, we should have suceeded.
     
    196240    return ('OK','OK');
    197241  }
    198 } # end addDomain
     242
     243} # end delDomain()
    199244
    200245
     
    249294    $sql .= " records where domain_id=$id and type=$reverse_typemap{SOA}";
    250295  }
    251 print "MDEBUG: $sql<br>\n";
     296#print "getSOA DEBUG: $sql<br>\n";
    252297  my $sth = $dbh->prepare($sql);
    253298  $sth->execute;
     
    292337    return undef;
    293338  }
    294 my %ret;
     339  my %ret;
    295340  $ret{recid}   = $recid;
    296341  $ret{host}    = $host;
     
    338383
    339384
    340 ## DNDB::addRec()
     385## DNSDB::addRec()
    341386# Add a new record to a domain or a group's default records
    342387# Takes a database handle, default/live flag, group/domain ID,
     
    378423  my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallist)";
    379424# something is bugging me about this...
     425print "DEBUG: $sql<br>\n";
    380426  my $sth = $dbh->prepare($sql);
    381427  $sth->execute;
     
    387433
    388434
     435## DNSDB::delRec()
     436# Delete a record. 
     437sub delRec {
     438  $errstr = '';
     439  my $dbh = shift;
     440  my $defrec = shift;
     441  my $id = shift;
     442
     443  my $sth = $dbh->prepare("delete from ".($defrec eq 'y' ? 'default_' : '')."records where record_id=?");
     444  $sth->execute($id);
     445
     446  return ('FAIL',$sth->errstr) if $sth->err;
     447
     448  return ('OK','OK');
     449} # end delRec()
     450
     451
     452## DNSDB::domStatus()
     453# Sets and/or returns a domain's status
     454# Takes a database handle, domain ID and optionally a status argument
     455# Returns undef on errors.
     456sub domStatus {
     457  my $dbh = shift;
     458  my $id = shift;
     459  my $newstatus = shift;
     460
     461  return undef if $id !~ /^\d+$/;
     462
     463  my $sth;
     464
     465# ooo, fun!  let's see what we were passed for status
     466  if ($newstatus) {
     467    $sth = $dbh->prepare("update domains set status=? where domain_id=?");
     468    # ass-u-me caller knows what's going on in full
     469    if ($newstatus =~ /^[01]$/) {       # only two valid for now.
     470      $sth->execute($newstatus,$id);
     471    } elsif ($newstatus =~ /^domo(?:n|ff)$/) {
     472      $sth->execute(($newstatus eq 'domon' ? 1 : 0),$id);
     473    }
     474  }
     475
     476  $sth = $dbh->prepare("select status from domains where domain_id=?");
     477  $sth->execute($id);
     478  my ($status) = $sth->fetchrow_array;
     479  return $status;
     480} # end domStatus()
     481
     482
    389483# shut Perl up
    3904841;
  • trunk/dns.cgi

    r2 r3  
    77# Last update by $Author$
    88###
    9 # Copyright (C) 2008 - Kris Deugau <kdeugau@deepnet.cx>
     9# Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
    1010
    1111use strict;
     
    6363
    6464# default
    65 my $perpage = 15;
     65#my $perpage = 15;
     66my $perpage = 3;
    6667my $offset = ($webvar{offset} ? $webvar{offset} : 0);
    6768
     
    8283# Default page is a login page
    8384my $page;       # to be initialized as an HTML::Template entity sooner or later
     85
     86
    8487
    8588# decide which page to spit out...
     
    9396
    9497if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
     98
     99# hmm.  seeing problems in some possibly-not-so-corner cases.
     100  if (defined($webvar{action})) {
     101    domStatus($dbh,$webvar{id},$webvar{action});
     102  }
     103
    95104  my $sth = $dbh->prepare("select count(*) from domains");
    96105  $sth->execute;
    97106  my ($count) = ($sth->fetchrow_array);
    98107
    99   if ($count > $perpage) {
    100     # if there are more results than the default, always show the "all" link
    101     $page->param(navall => 1);
    102 
    103     if ($offset > 0) {
    104       $page->param(navfirst => 1);
    105       $page->param(navprev => 1);
    106       $page->param(prevoffs => $offset-1);
     108##fixme
     109  if ($offset eq 'all') {
     110    print "foo!  wanna see'em all\n";
     111  } else {
     112    # all these bits only have sensible behaviour if offset is numeric. err, probably.
     113    if ($count > $perpage) {
     114      # if there are more results than the default, always show the "all" link
     115      $page->param(navall => 1);
     116
     117      if ($offset > 0) {
     118        $page->param(navfirst => 1);
     119        $page->param(navprev => 1);
     120        $page->param(prevoffs => $offset-1);
     121      }
     122
     123      # show "next" and "last" links if we're not on the last page of results
     124      if ( (($offset+1) * $perpage - $count) < 0 ) {
     125        $page->param(navnext => 1);
     126        $page->param(nextoffs => $offset+1);
     127        $page->param(navlast => 1);
     128        $page->param(lastoffs => int $count/$perpage);
     129      }
    107130    }
    108 
    109     # show "next" and "last" links if we're not on the last page of results
    110     if ( (($offset+1) * $perpage - $count) < 0 ) {
    111       $page->param(navnext => 1);
    112       $page->param(nextoffs => $offset+1);
    113       $page->param(navlast => 1);
    114       $page->param(lastoffs => int $count/$perpage);
    115     }
    116131  }
    117132
    118133  $page->param(ndomains => $count);
    119 
     134  $page->param(nstart => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
     135  $page->param(npglast => ($offset eq 'all' ? $count :
     136        ( (($offset+1)*$perpage) > $count ? $count : (($offset+1)*$perpage) )
     137        ));
    120138  my @domlist;
    121139  $sth = $dbh->prepare("select domain_id,domain,status,groups.name from domains".
    122140        " inner join groups on domains.group_id=groups.group_id".
    123         " order by domain limit $perpage offset ".$offset*$perpage);
     141        " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage));
    124142  $sth->execute;
    125143  my $rownum = 0;
     
    128146    $row{domainid} = $data[0];
    129147    $row{domain} = $data[1];
    130     $row{status} = $data[2];
     148    $row{status} = ($data[2] ? 'Active' : 'Inactive');
    131149    $row{group} = $data[3];
    132150    $row{bg} = ($rownum++)%2;
    133     $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
     151#    $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
     152    $row{mkactive} = !$data[2];
    134153    $row{sid} = $sid;
     154    $row{offset} = $offset;
    135155##fixme:  need to clean up status indicator/usage/inversion
    136156    push @domlist, \%row;
     
    190210  print "whee!\n";
    191211
    192 newrec();
     212  # populate most fields as needed.  (eg, type list.)
     213  newrec();
    193214
    194215} elsif ($webvar{page} eq 'addrec') {
     
    221242  }
    222243
     244  $page->param(defrec => $webvar{defrec});
     245
    223246} elsif ($webvar{page} eq 'conf_del') {
    224247
     
    236259  if (!defined($webvar{del})) {
    237260    $page->param(del_getconf => 1);
    238   } else {
    239 #    $page->param(
    240   }
    241 ##work
     261    my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
     262    $page->param(host => $rec{host});
     263    $page->param(ftype => $typemap{$rec{type}});
     264    $page->param(recval => $rec{val});
     265  } else {
     266    my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
     267    if ($code ne 'OK') {
     268## need to find failure mode
     269      $page->param(del_failed => 1);
     270      $page->param(errmsg => $msg);
     271    }
     272##fixme:  group/parent instead of hardcoded 1
     273    showdomain('y',1);
     274  }
    242275
    243276} elsif ($webvar{page} eq 'editsoa') {
     
    258291  $sth = $dbh->prepare($sql);
    259292  $sth->execute;
    260 print "DEBUG: $sql<br>\n";
    261 #print "DEBUG: ".$DBI::errstr;
    262293
    263294  if ($sth->err) {
     
    279310# hokay, a bit of magic to decide which page we hit.
    280311  if ($code eq 'OK') {
    281     $page = HTML::Template->new(filename => "$templatedir/domlist.tmpl");
     312    $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
     313    showdomain(0,$msg);
     314##work
    282315  } else {
    283316# oooh, yeah, this is supposed to be a redirect.  er, maybe.  whee.
     
    288321  }
    289322
    290 #  my $sth = $dbh->prepare("insert into domains (domain,group_id,status) values (?,?,?)");
    291 #  $sth->execute($webvar{domain},1,($webvar{makeactive} ? 'active' : 'inactive')) or die $DBI::errstr;
    292 #  $sth = $dbh->prepare("select domain_id from domains where domain='$webvar{domain}'");
    293 #  $sth->execute;
    294 #  my ($dom_id) = $sth->fetchrow_array();
    295 #  $sth = $dbh->prepare("select host,type,val,distance,ttl from default_records where group_id=1");
    296 #  my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,ttl)".
    297 #       " values ($dom_id,?,?,?,?,?)");
    298 #  $sth->execute;
    299 #  while (my ($host,$type,$val,$dist,$ttl) = $sth->fetchrow_array()) {
    300 #    $host =~ s/DOMAIN/$webvar{domain}/g;
    301 #    $sth_in->execute($host,$type,$val,$dist,$ttl);
    302 #  }
    303 #  $dbh->commit or print "failed to add $webvar{domain}: ".$DBI::errstr."\n";
    304 
    305 #  $page->param(add_failed => 1);
     323
    306324}
    307325
     
    410428  $page->param(typelist => \@typelist);
    411429  $page->param(domain   => domainName($dbh,$webvar{domainid}));
    412   $page->param(parentid => $webvar{parentid});
    413430  $page->param(defrec   => $webvar{defrec});
    414431  $page->param(ttl      => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
    415 }
     432  if ($webvar{defrec} eq 'y') {
     433    ##fixme - should be groupid
     434    $page->param(parentid => 1);
     435  } else {
     436    $page->param(parentid => $webvar{parentid});
     437  }
     438}
  • trunk/templates/adddomain.tmpl

    r2 r3  
    22<TMPL_INCLUDE NAME="newdomain.tmpl">
    33<TMPL_ELSE>
    4 <TMPL_INCLUDE NAME="domdetail.tmpl">
     4<TMPL_INCLUDE NAME="reclist.tmpl">
    55</TMPL_IF>
  • trunk/templates/delrec.tmpl

    r2 r3  
    44
    55<td align="center">
    6 <h3>Are you really sure you want to delete <TMPL_VAR NAME=ftype> record <TMPL_VAR NAME=recval>?</h3>
     6<h3>Are you really sure you want to delete record:<br>
     7<TMPL_VAR NAME=host> <TMPL_VAR NAME=ftype> <TMPL_VAR NAME=recval></h3>
    78<a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=delrec&del=cancel&id=<TMPL_VAR NAME=id>&defrec=<TMPL_VAR NAME=defrec>">cancel</a> &nbsp; | &nbsp; <a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=delrec&del=ok&id=<TMPL_VAR NAME=id>&defrec=<TMPL_VAR NAME=defrec>">confirm</a>
    89</td></tr></table>
     
    1011<TMPL_ELSE>
    1112<TMPL_IF del_failed>
     13<!-- Need to find failure mode to cause/test this -->
     14<h1>FOO!</h1>
     15<TMPL_VAR NAME=errmsg>
    1216<TMPL_ELSE>
    1317<TMPL_INCLUDE NAME="reclist.tmpl">
  • trunk/templates/domlist.tmpl

    r2 r3  
    1212<TMPL_IF navnext><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=domlist&offset=<TMPL_VAR NAME=nextoffs>">next<img src="images/fwd.png" border=0></a><TMPL_ELSE>next<img src="images/fwd.png" border=0></TMPL_IF>&nbsp;
    1313<TMPL_IF navlast><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=domlist&offset=<TMPL_VAR NAME=lastoffs>">last<img src="images/ffwd.png" border=0></a><TMPL_ELSE>last<img src="images/ffwd.png" border=0></TMPL_IF>&nbsp;
    14 <TMPL_IF navall><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=domlist">all</a><TMPL_ELSE>all</TMPL_IF>
     14<TMPL_IF navall><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=domlist&offset=all">all</a><TMPL_ELSE>all</TMPL_IF>
    1515</td><td align=right>insert search box here</td></tr>
    1616
     
    4141        <td width="1%" nowrap><TMPL_VAR name=status></td>
    4242        <td width="1%" nowrap><TMPL_VAR name=group></td>
    43         <td width="1%" nowrap align=center><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=chgstatus&id=<TMPL_VAR NAME=domainid>&defrec=n"><TMPL_IF NAME=mkactive>activate<TMPL_ELSE>deactivate</TMPL_IF></a></td>
     43        <td width="1%" nowrap align=center><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=domlist<TMPL_IF NAME=offset>&offset=<TMPL_VAR NAME=offset></TMPL_IF>&id=<TMPL_VAR NAME=domainid>&action=<TMPL_IF NAME=mkactive>domon<TMPL_ELSE>domoff</TMPL_IF>"><TMPL_IF NAME=mkactive>activate<TMPL_ELSE>deactivate</TMPL_IF></a></td>
    4444        <td width="1%" nowrap align=center><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=del&id=<TMPL_VAR NAME=domainid>&defrec=w"><img src="images/trash2.png" border=0></a></td>
    4545</tr>
  • trunk/templates/reclist.tmpl

    r2 r3  
    4949<td width="5%" nowrap><TMPL_VAR NAME=port></td>
    5050<td width="5%" nowrap><TMPL_VAR NAME=ttl></td>
    51 <td width="5%" align="center" nowrap><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=conf_del&id=<TMPL_VAR NAME=record_id>&defrec=<TMPL_VAR NAME=defrec>"><img border=0 src="images/trash2.png"></a></td>
     51<td width="5%" align="center" nowrap><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=delrec&id=<TMPL_VAR NAME=record_id>&defrec=<TMPL_VAR NAME=defrec>"><img border=0 src="images/trash2.png"></a></td>
    5252</TMPL_LOOP>
    5353</table>
Note: See TracChangeset for help on using the changeset viewer.