Changeset 851 for trunk


Ignore:
Timestamp:
09/01/22 17:57:34 (21 months ago)
Author:
Kris Deugau
Message:

/trunk

BIND export, unwinding dev saves, 4 of many many

  • Copypasta a chunk of export_tiny() from DNSDB.pm to lay out the structure of the zone record retrieval
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB/ExportBIND.pm

    r850 r851  
    7676print "open zonefile for '$loc', '$zonepath'\n";
    7777    }
     78
     79    eval {
     80
     81      # write fresh records if:
     82      #  - we are not using the cache
     83      #  - force_refresh is set
     84      #  - the zone has changed
     85      #  - the cache file does not exist
     86      #  - the cache file is empty
     87      if (!$self->{usecache} || $self->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
     88        if ($self->{usecache}) {
     89          open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
     90          $zonefilehandle = *ZONECACHE;
     91        }
     92
     93        # need to fetch this separately since the rest of the records all (should) have real IPs in val
     94        $soasth->execute($revid);
     95        my (@zsoa) = $soasth->fetchrow_array();
     96        $self->_printrec_tiny($zonefilehandle, $zsoa[7], 'y',\%recflags,$revzone,
     97          $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
     98
     99        $recsth->execute($revid);
     100        my $fullzone = _ZONE($tmpzone, 'ZONE', 'r', '.').($tmpzone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
     101
     102        while (my ($host, $type, $val, $dist, $weight, $port, $ttl, $recid, $loc, $stamp, $expires, $stampactive)
     103                = $recsth->fetchrow_array) {
     104          next if $recflags{$recid};
     105
     106          # Check for out-of-zone data
     107          if ($val =~ /\.arpa$/) {
     108            # val is non-IP
     109            if ($val !~ /$fullzone$/) {
     110              warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
     111              next;
     112            }
     113          } else {
     114            my $ipval = new NetAddr::IP $val;
     115            if (!$tmpzone->contains($ipval)) {
     116              warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
     117              next;
     118            }
     119          } # is $val a raw .arpa name?
     120
     121          # Spaces are evil.
     122          $val =~ s/^\s+//;
     123          $val =~ s/\s+$//;
     124          if ($typemap{$type} ne 'TXT') {
     125            # Leading or trailng spaces could be legit in TXT records.
     126            $host =~ s/^\s+//;
     127            $host =~ s/\s+$//;
     128          }
     129
     130          $self->_printrec_tiny($zonefilehandle, $recid, 'y', \%recflags, $revzone,
     131            $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
     132
     133          $recflags{$recid} = 1;
     134
     135        } # while ($recsth)
     136
     137        if ($self->{usecache}) {
     138          close ZONECACHE; # force the file to be written
     139          # catch obvious write errors that leave an empty temp file
     140          if (-s $tmpcache) {
     141            rename $tmpcache, $cachefile
     142              or die "Error overwriting cache file $cachefile with temporary file: $!\n";
     143          }
     144        }
     145
     146      } # if $changed or cache filesize is 0
     147
     148    };
     149    if ($@) {
     150      die "error writing ".($self->{usecache} ? 'new data for ' : '')."$revzone: $@\n";
     151      # error!  something borked, and we should be able to fall back on the old cache file
     152      # report the error, somehow.
     153    } else {
     154      # mark zone as unmodified.  Only do this if no errors, that way
     155      # export failures should recover a little more automatically.
     156      $zonesth->execute($revid);
     157    }
     158
     159#    if ($self->{usecache}) {
     160#      # We've already made as sure as we can that a cached zone file is "good",
     161#      # although possibly stale/obsolete due to errors creating a new one.
     162#      eval {
     163#        open CACHE, "<$cachefile" or die $!;
     164#        print $datafile $_ or die "error copying cached $revzone to master file: $!" while <CACHE>;
     165#        close CACHE;
     166#      };
     167#      die $@ if $@;
     168#    }
     169
     170
    78171
    79172    $soasth->execute($revid);
Note: See TracChangeset for help on using the changeset viewer.