Changeset 871


Ignore:
Timestamp:
09/28/22 17:51:26 (19 months ago)
Author:
Kris Deugau
Message:

/trunk

BIND export, unwinding dev saves, 23 of many many

  • Accumulated cleanup for main reverse zone export loop; in looking at the copy-paste-edit to add the domain export loop, the committed code got badly out of sync with the dev save's complete loop.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB/ExportBIND.pm

    r870 r871  
    3535  my $viewlist = $dnsdb->getLocList(curgroup => 1);
    3636
     37
     38## export reverse zones
     39
    3740  my $soasth = $dnsdb->{dbh}->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".
    3841        "FROM records WHERE rdns_id=? AND type=6");
     
    6568#printf "non-octet? %s, %i\n", $cidr->masklen, $cidr->masklen % 8;
    6669
     70    # fetch a list of views/locations present in the zone.  we need to publish a file for each one.
     71    # in the event that no locations are present (~~ $viewlist is empty), /%view collapses to nothing in the zone path
     72#    my (@loclist) = $dnsdb->{dbh}->selectrow_array("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
     73    my $tmplocs = $dnsdb->{dbh}->selectall_arrayref("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
     74    my @loclist;
     75    foreach my $tloc (@{$tmplocs}) {
     76      push @loclist, ($tloc->[0] eq '' ? 'common' : $tloc->[0]);
     77    }
     78
     79    my %zonefiles;      # zone file handles
    6780
    6881    eval {
     
    7083      my $arpazone = DNSDB::_ZONE($cidr, 'ZONE', 'r', '.').($cidr->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
    7184
    72       # write fresh records if:
    73       #  - we are not using the cache
    74       #  - force_refresh is set
    75       #  - the zone has changed
    76       #  - the cache file does not exist
    77       #  - the cache file is empty
    78       if (!$dnsdb->{usecache} || $dnsdb->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
    79         if ($dnsdb->{usecache}) {
    80           open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
    81           $zonefilehandle = *ZONECACHE;
    82         }
    83 
    84         # fetch a list of views/locations present in the zone.  we need to publish a file for each one.
    85         # in the event that no locations are present (~~ $viewlist is empty), /%view collapses to nothing in the zone path
    86 #        my (@loclist) = $dnsdb->{dbh}->selectrow_array("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
    87         my $tmplocs = $dnsdb->{dbh}->selectall_arrayref("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
    88         my @loclist;
    89         foreach my $tloc (@{$tmplocs}) {
    90           push @loclist, ($tloc->[0] eq '' ? 'common' : $tloc->[0]);
    91         }
    92 
    93         push @loclist, $defloc unless grep /$defloc/, @loclist;
    94         push @loclist, 'common' unless grep /^common$/, @loclist;
    95         my $zonepath = $dnsdb->{bind_export_reverse_zone_path};
    96         my %zonefiles;  # to be a hash of file handles.
    9785##fixme:  need to open separate zone files for aggregated metazones eg /22 or /14
    98         foreach my $loc (@loclist) {
    99           my $zfilepath = $zonepath;
    100           $zfilepath =~ s/\%view/$loc/;
    101           $zfilepath =~ s/\%zone/$revzone/;
    102           $zfilepath =~ s/\%arpazone/$arpazone/;
    103           # Just In Case(TM)
    104           $zfilepath =~ s,[^\w./-],_,g;
    105           open $zonefiles{$loc}, ">", $zfilepath;
     86      foreach my $loc (@loclist) {
     87        my $zfilepath = $dnsdb->bind_export_reverse_zone_path};
     88        $zfilepath =~ s/\%view/$loc/;
     89        $zfilepath =~ s/\%zone/$revzone/;
     90        $zfilepath =~ s/\%arpazone/$arpazone/;
     91
     92        # Just In Case(TM)
     93        $zfilepath =~ s,[^\w./-],_,g;
     94
     95#        open $zonefiles{$loc}, ">", $zfilepath;
     96
     97        # write fresh records if:
     98        #  - we are not using the cache
     99        #  - force_refresh is set
     100        #  - the zone has changed
     101        #  - the cache file does not exist
     102        #  - the cache file is empty
     103        if ($dnsdb->{force_refresh} || $changed || !-e $zfilepath || -z $zfilepath) {
     104#        if (!$dnsdb->{usecache} || $dnsdb->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
     105#          if ($dnsdb->{usecache}) {
     106#            open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
     107#            $zonefilehandle = *ZONECACHE;
     108#          }
     109          open $zonefiles{$loc}, ">", $zfilepath or die "Error creating temporary file $zfilepath: $!\n";
     110
    106111          printf {$zonefiles{$loc}} "; %s in view %s exported %s\n", $arpazone, $loc, scalar(localtime)
    107112            or die "Error writing header [$zone, '$loc']: $!\n";;
    108           # tag the zonefile for publication in the view
    109           push @{$viewzones{$loc}}, $arpazone;
    110         } # foreach @loclist
    111 
    112         # need to fetch this separately since the rest of the records all (should) have real IPs in val
    113         $soasth->execute($revid);
    114         my (@zsoa) = $soasth->fetchrow_array();
    115 
     113
     114          # need to fetch this separately since the rest of the records all (should) have real IPs in val
     115          $soasth->execute($revid);
     116          my (@zsoa) = $soasth->fetchrow_array();
    116117##fixme: do we even need @loclist passed in?
    117         publishrec_bind(\%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $revzone,
    118           $zsoa[0], $zsoa[1], $zsoa[2], $zsoa[3], $zsoa[4], $zsoa[5], $zsoa[6], $loc, '');
    119 
    120         $recsth->execute($revid);
    121         my $fullzone = _ZONE($tmpzone, 'ZONE', 'r', '.').($tmpzone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
    122 
    123         while (my ($host, $type, $val, $dist, $weight, $port, $ttl, $recid, $loc, $stamp, $expires, $stampactive)
    124                 = $recsth->fetchrow_array) {
    125           next if $recflags{$recid};
    126 
    127           # Check for out-of-zone data
    128           if ($val =~ /\.arpa$/) {
    129             # val is non-IP
    130             if ($val !~ /$fullzone$/) {
    131               warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
    132               next;
    133             }
    134           } else {
    135             my $ipval = new NetAddr::IP $val;
    136             if (!$tmpzone->contains($ipval)) {
    137               warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
    138               next;
    139             }
    140           } # is $val a raw .arpa name?
    141 
    142           # Spaces are evil.
    143           $val =~ s/^\s+//;
    144           $val =~ s/\s+$//;
    145           if ($typemap{$type} ne 'TXT') {
    146             # Leading or trailng spaces could be legit in TXT records.
    147             $host =~ s/^\s+//;
    148             $host =~ s/\s+$//;
     118          publishrec_bind(\%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $revzone,
     119            $zsoa[0], $zsoa[1], $zsoa[2], $zsoa[3], $zsoa[4], $zsoa[5], $zsoa[6], $loc, '');
     120        } # if force_refresh etc
     121
     122        # tag the zonefile for publication in the view
     123        push @{$viewzones{$loc}}, $arpazone;
     124      } # foreach @loclist
     125
     126      # now the meat of the records
     127      $recsth->execute($revid);
     128      my $fullzone = _ZONE($tmpzone, 'ZONE', 'r', '.').($tmpzone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
     129
     130      while (my ($host, $type, $val, $dist, $weight, $port, $ttl, $recid, $loc, $stamp, $expires, $stampactive)
     131                = $recsth->fetchrow_array) {
     132        next if $recflags{$recid};
     133
     134        # Check for out-of-zone data
     135        if ($val =~ /\.arpa$/) {
     136          # val is non-IP
     137          if ($val !~ /$fullzone$/) {
     138            warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
     139            next;
    149140          }
    150 
    151           publishrec_bind(\%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
    152             $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    153 
    154           $recflags{$recid} = 1;
    155 
    156         } # while ($recsth)
    157 
    158         if ($dnsdb->{usecache}) {
    159           close ZONECACHE; # force the file to be written
    160           # catch obvious write errors that leave an empty temp file
    161           if (-s $tmpcache) {
    162             rename $tmpcache, $cachefile
    163               or die "Error overwriting cache file $cachefile with temporary file: $!\n";
     141        } else {
     142          my $ipval = new NetAddr::IP $val;
     143          if (!$tmpzone->contains($ipval)) {
     144            warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
     145            next;
    164146          }
     147        } # is $val a raw .arpa name?
     148
     149        # Spaces are evil.
     150        $val =~ s/^\s+//;
     151        $val =~ s/\s+$//;
     152        if ($typemap{$type} ne 'TXT') {
     153          # Leading or trailng spaces could be legit in TXT records.
     154          $host =~ s/^\s+//;
     155          $host =~ s/\s+$//;
    165156        }
    166157
    167       } # if $changed or cache filesize is 0
     158        publishrec_bind(\%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
     159                $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
     160
     161        $recflags{$recid} = 1;
     162
     163      } # while ($recsth)
     164
     165#      if ($dnsdb->{usecache}) {
     166#        close ZONECACHE; # force the file to be written
     167#        # catch obvious write errors that leave an empty temp file
     168#        if (-s $tmpcache) {
     169#          rename $tmpcache, $cachefile
     170#            or die "Error overwriting cache file $cachefile with temporary file: $!\n";
     171#        }
     172#      }
    168173
    169174    };
     
    189194#    }
    190195
    191 
    192 
    193196  } # revsth->fetch
     197
    194198
    195199
Note: See TracChangeset for help on using the changeset viewer.