Changeset 33


Ignore:
Timestamp:
11/10/09 17:51:55 (15 years ago)
Author:
Kris Deugau
Message:

/trunk

checkpoint, fleshing out axfr import

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r29 r33  
    1515use Exporter;
    1616use DBI;
     17use Net::DNS;
    1718#use Net::SMTP;
    1819#use NetAddr::IP qw( Compact );
     
    811812
    812813
     814## DNSDB::importAXFR
     815# Import a domain via AXFR
     816sub importAXFR {
     817  my $dbh = shift;
     818  my $ifrom = shift;
     819  my $domain = shift;
     820  my $group = shift;
     821  my $status = shift || 1;
     822  my $rwsoa = shift || 0;
     823  my $rwns = shift || 0;
     824##fixme:  add mode to delete&replace, merge+overwrite, merge new?
     825
     826  my $res = Net::DNS::Resolver->new;
     827$res->axfr_start($domain);
     828my $nrecs = 0;
     829my $flags = 0;
     830
     831  # Allow transactions, and raise an exception on errors so we can catch it later.
     832  # Use local to make sure these get "reset" properly on exiting this block
     833  local $dbh->{AutoCommit} = 0;
     834  local $dbh->{RaiseError} = 1;
     835
     836  eval {
     837    # can't do this, can't nest transactions.  sigh.
     838    #my ($dcode, $dmsg) = addDomain($dbh, $domain, $group, $status);
     839
     840##fixme:  serial
     841    my $sth = $dbh->prepare("INSERT INTO domains (domain,group_id,status) VALUES (?,?,?)");
     842    $sth->execute($domain,$group,$state);
     843
     844    # get domain id so we can do the records
     845    $sth = $dbh->prepare("select domain_id from domains where domain='$domain'");
     846    $sth->execute;
     847    ($dom_id) = $sth->fetchrow_array();
     848
     849    while (my $rr = $res->axfr_next) {
     850      my $type = $rr->type;
     851# nasty big ugly case-like thing here, since we have to do *some* different
     852# processing depending on the record.  le sigh.
     853      my $sql = "INSERT INTO records (domain_id,host,type,val,ttl";
     854      my $vallen = "?,?,?,?,?";
     855      my $host,$val;
     856
     857##work
     858# gnnnnnh.  going to need to (rr->string)  ->  split  ->  <localvars>
     859my @vallist;
     860      if ($type eq 'SOA') {
     861        $host = $rr->mname.":".$rr->rname;
     862        $val = $rr->refresh.":".$rr->retry.":".$rr->expire.":".$rr->minimum;
     863      }
     864      if ($type eq 'MX' || $type eq 'SRV') {
     865        $sql .= ",distance";
     866        $vallen .= ",?";
     867        $sql .= ",weight,port" if $type eq 'SRV';
     868        $vallen .= ",?,?" if $type eq 'SRV';
     869      }
     870$sth = $dbh->prepare($sql.") VALUES (".$vallen.")");
     871$sth->execute(@vallist);
     872
     873      #print $rr->rdata."\n";
     874    }
     875  };
     876
     877  if ($@) {
     878    my $msg = $@;
     879    eval { $dbh->rollback; };
     880    return ('FAIL',$msg);
     881  } else {
     882    return ('OK',"ook");
     883  }
     884
     885  return ('WARN',"
     886} # end importAXFR()
     887
     888
    813889# shut Perl up
    8148901;
  • trunk/dns.cgi

    r31 r33  
    601601  $page->param(rwsoa => $webvar{rwsoa}) if $webvar{rwsoa};
    602602  $page->param(rwns => $webvar{rwns}) if $webvar{rwns};
     603  $page->param(dominactive => 1) unless $webvar{domactive};
    603604  $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
    604605##work
     606
     607##fixme: check group too?
     608  if ($webvar{doit} eq 'y' && !$webvar{ifrom}) {
     609    $page->param(errmsg => "Need to set host to import from");
     610  } elsif ($webvar{doit} eq 'y' && !$webvar{importdoms}) {
     611    $page->param(errmsg => "Need domains to import");
     612  } else {
     613    my @domlist = split /\s+/, $webvar{importdoms};
     614    my @results;
     615my $rnum = 0;
     616    foreach my $domain (@domlist) {
     617      # do stuff!  DNSDB::importAXFR($webvar{ifrom}, $webvar{rwsoa}, $webvar{rwns}, $domain, <flags>)
     618      my %row;
     619      $row{domain} = $domain;
     620#      $row{row} = $rnum++;
     621      if ($domain eq 'deepnet.cx' || $domain eq 'wh-int') {
     622        $row{domok} = 0;  $row{domerr} = "foo!  hahahaha!";
     623      } elsif ($domain eq 'waslokido.com') {
     624        $row{domwarn} = 0;  $row{domwarn} = "foo!  hahahaha!";
     625      } else {
     626        $row{domok} = 1;
     627      }
     628      push @results, \%row;
     629    }
     630    $page->param(axfrresults => \@results);
     631  }
     632
    605633  push @debugbits, "<pre>$webvar{importdoms}</pre>";
    606634}
  • trunk/templates/axfr.tmpl

    r32 r33  
    55<td align="center">
    66
    7 <TMPL_IF errmsg><div class="errmsg">Query error: <TMPL_VARNAME=errmsg></div></TMPL_IF>
     7<TMPL_IF errmsg><div class="errmsg">Error: <TMPL_VARNAME=errmsg></div></TMPL_IF>
    88
    99<form action="dns.cgi" method="POST">
     
    3535</tr>
    3636<tr class="datalinelight">
     37        <td>Import as active?</td>
     38        <td><input type=checkbox name="domactive"<TMPL_UNLESS dominactive> checked</TMPL_UNLESS>></td>
     39</tr>
     40<tr class="datalinelight">
    3741        <td valign="top">Domains to import:<br />(one per line)</td>
    3842        <td><textarea name="importdoms" rows=10><TMPL_IF importdoms><TMPL_VAR NAME=importdoms></TMPL_IF></textarea></td>
     
    4650
    4751<TMPL_IF axfrresults>
     52<br />
    4853<table>
    4954<tr class="tableheader"><td colspan=2>AXFR Import Results</td></tr>
    5055<TMPL_LOOP NAME=axfrresults>
    51 <TMPL_IF domok><tr class="tableheader"><td><TMPL_VAR NAME=domain></td><td>Imported OK</td></tr>
    52 <TMPL_ELSE><tr class="tableheader"><td><TMPL_VAR NAME=domain></td><td>Failed: <TMPL_VAR NAME=domerr></td></tr>
    5356<tr class="datalinelight">
    54         <td>Try again?</td>
    55         <td>
    56                 <input type=radio name=try<TMPL_VAR NAME=row> value="0">No/Discard<br />
    57                 <input type=radio name=try<TMPL_VAR NAME=row> value="R">Replace<br />
    58                 <input type=radio name=try<TMPL_VAR NAME=row> value="M">Merge + Overwrite<br />
    59                 <input type=radio name=try<TMPL_VAR NAME=row> value="M">Merge only new records
    60         </td>
     57        <td><TMPL_VAR NAME=domain></td>
     58<TMPL_IF domok> <td>Imported OK</td>
     59<TMPL_ELSE><TMPL_IF domwarn>    <td class="warn">Warning: <TMPL_VAR NAME=domwarn></td>
     60<TMPL_ELSE>     <td class="err">Failed: <TMPL_VAR NAME=domerr></td>
     61</TMPL_IF></TMPL_IF>
    6162</tr>
    62 </TMPL_IF>
    6363</TMPL_LOOP>
    6464</table>
  • trunk/templates/dns.css

    r24 r33  
    7070        background-color: #DDDDDD;
    7171}
     72td.err {
     73        border: solid 2px #FF0000;
     74        color: red;
     75        background-color: #e0e0e0;
     76}
     77td.warn {
     78        border: solid 2px #FFFF00;
     79        color: #333300;
     80        background-color: #e0e0e0;
     81}
     82
    7283.meat {
    7384        align: center;
Note: See TracChangeset for help on using the changeset viewer.