Changeset 372 for trunk/DNSDB.pm


Ignore:
Timestamp:
08/01/12 18:19:03 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Checkpoint adding location/view support. See #10.

  • minor location list template tweak
  • extend importer to deal with locations on records, and location definitions
  • extend exporter to handle locations

Also:

  • minor SQL error-log cleanup (boolean types use 'y'/'n' or 'true'/'false'). See #25, sort of
  • Fix handling of CNAME exports for reverse zones; the records look ugly but there's no simple way to autoconvert them to the template types on import
  • Fix lurking buglet in tabledef update; we're using a 4-char field for locations to allow for legacy uses.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r370 r372  
    42934293# of remaining data, allows up to 255 raw bytes
    42944294
     4295  # Locations/views - worth including in the caching setup?
     4296  my $lochash = $dbh->selectall_hashref("SELECT location,iplist FROM locations", 'location');
     4297  foreach my $location (keys %$lochash) {
     4298    foreach my $ipprefix (split /,/, $lochash->{$location}{iplist}) {
     4299      print $datafile "%$location:$ipprefix\n";
     4300    }
     4301    print $datafile "%$location\n" if !$lochash->{$location}{iplist};
     4302  }
     4303
    42954304  # tracking hash so we don't double-export A+PTR or AAAA+PTR records.
    42964305  my %recflags;
    42974306
    42984307  my $domsth = $dbh->prepare("SELECT domain_id,domain,status,changed FROM domains WHERE status=1");
    4299   my $recsth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id ".
     4308  my $recsth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".
    43004309        "FROM records WHERE domain_id=? AND type < 65280");     # Just exclude all types relating to rDNS
    4301   my $zonesth = $dbh->prepare("UPDATE domains SET changed=0 WHERE domain_id=?");
     4310  my $zonesth = $dbh->prepare("UPDATE domains SET changed='n' WHERE domain_id=?");
    43024311  $domsth->execute();
    43034312  while (my ($domid,$dom,$domstat,$changed) = $domsth->fetchrow_array) {
     
    43124321    if ($changed || -s "$config{exportcache}/$dom" == 0) {
    43134322      $recsth->execute($domid);
    4314       while (my ($host,$type,$val,$dist,$weight,$port,$ttl,$recid) = $recsth->fetchrow_array) {
     4323      while (my ($host,$type,$val,$dist,$weight,$port,$ttl,$recid,$loc) = $recsth->fetchrow_array) {
    43154324        next if $recflags{$recid};
    4316 ##fixme:  need to store location in the db, and retrieve it here.
    4317 # temporarily hardcoded to empty so we can include it further down.
    4318         my $loc = '';
     4325
     4326        $loc = '' if !$loc;     # de-nullify - just in case
     4327##fixme:  handle case of record-with-location-that-doesn't-exist better.
     4328# note this currently fails safe (tested) - records with a location that
     4329# doesn't exist will not be sent to any client
     4330#       $loc = '' if !$lochash->{$loc};
    43194331
    43204332##fixme:  record validity timestamp. tinydns supports fiddling with timestamps.
     
    43494361  my $revsth = $dbh->prepare("SELECT rdns_id,revnet,status,changed FROM revzones WHERE status=1 ".
    43504362        "ORDER BY masklen(revnet) DESC");
     4363
    43514364# For reasons unknown, we can't sanely UNION these statements.  Feh.
    43524365# Supposedly it should work though (note last 3 lines):
     
    43614374#and LIMIT can be attached to a subexpression if it is enclosed in parentheses. Without parentheses, these
    43624375#clauses will be taken to apply to the result of the UNION, not to its right-hand input expression.)
    4363 
    4364   my $soasth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id ".
     4376  my $soasth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".
    43654377        "FROM records WHERE rdns_id=? AND type=6");
    4366   $recsth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id ".
     4378  $recsth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".
    43674379        "FROM records WHERE rdns_id=? AND not type=6 ".
    43684380        "ORDER BY masklen(CAST(val AS inet)) DESC, CAST(val AS inet)");
    4369   $zonesth = $dbh->prepare("UPDATE revzones SET changed=0 WHERE rdns_id=?");
     4381  $zonesth = $dbh->prepare("UPDATE revzones SET changed='n' WHERE rdns_id=?");
    43704382  $revsth->execute();
    43714383  while (my ($revid,$revzone,$revstat,$changed) = $revsth->fetchrow_array) {
     
    43844396      my (@zsoa) = $soasth->fetchrow_array();
    43854397      _printrec_tiny($datafile,'y',\%recflags,$revzone,
    4386         $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],'','');
     4398        $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
    43874399
    43884400      $recsth->execute($revid);
    4389       while (my ($host,$type,$val,$dist,$weight,$port,$ttl,$recid) = $recsth->fetchrow_array) {
     4401      while (my ($host,$type,$val,$dist,$weight,$port,$ttl,$recid,$loc) = $recsth->fetchrow_array) {
    43904402        next if $recflags{$recid};
    4391 ##fixme:  need to store location in the db, and retrieve it here.
    4392 # temporarily hardcoded to empty so we can include it further down.
    4393         my $loc = '';
     4403
     4404        $loc = '' if !$loc;     # de-nullify - just in case
     4405##fixme:  handle case of record-with-location-that-doesn't-exist better.
     4406# note this currently fails safe (tested) - records with a location that
     4407# doesn't exist will not be sent to any client
     4408#       $loc = '' if !$lochash->{$loc};
    43944409
    43954410##fixme:  record validity timestamp. tinydns supports fiddling with timestamps.
     
    45874602      } elsif ($typemap{$type} eq 'CNAME') {
    45884603
    4589         print $datafile "C$host:$val:$ttl:$stamp:$loc\n";
     4604        if ($revrec eq 'n') {
     4605          print $datafile "C$host:$val:$ttl:$stamp:$loc\n";
     4606        } else {
     4607          my $val2 = NetAddr::IP->new($val);
     4608          print $datafile "C"._ZONE($val2, 'ZONE', 'r', '.').($val2->{isv6} ? '.ip6.arpa' : '.in-addr.arpa').
     4609                ":$host:$ttl:$stamp:$loc\n";
     4610        }
    45904611
    45914612      } elsif ($typemap{$type} eq 'SRV') {
Note: See TracChangeset for help on using the changeset viewer.