Changeset 880


Ignore:
Timestamp:
01/19/23 18:09:08 (16 months ago)
Author:
Kris Deugau
Message:

/trunk

BIND export, unwinding dev saves, home stretch

  • Untwist pattern/standard for sub names to better align with tinydns export
  • File off a couple more places missing a FQDN terminating dot
  • Correctly set the "hostname" for NS records in a reverse zone
  • Refine looping over views on generation of BIND config fragment
  • Move generation and substitution of reverse zone name into zone file path down a ways for better error-catching possibility
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB/ExportBIND.pm

    r878 r880  
    5858  while (my ($revid,$revzone,$revstat,$changed,$defloc) = $revsth->fetchrow_array) {
    5959    my $cidr = NetAddr::IP->new($revzone);
    60     my $zfile = $cidr->network->addr."-".$cidr->masklen;
    61 #    my $cachefile = "$dnsdb->{exportcache}/$zfile";
    62 #    my $tmpcache = "$dnsdb->{exportcache}/tmp.$zfile.$$";
    63     my $tmpcache = "tmp.$zfile.$$";      # safety net.  don't overwrite a previous known-good file
    6460
    6561##fixme:  convert logical revzone into .arpa name?  maybe take a slice of showrev_arpa?
     
    8581
    8682      my $arpazone = DNSDB::_ZONE($cidr, 'ZONE', 'r', '.').($cidr->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
     83      my $zfile = $cidr->network->addr."-".$cidr->masklen;
     84#      my $cachefile = "$dnsdb->{exportcache}/$zfile";
     85#      my $tmpcache = "$dnsdb->{exportcache}/tmp.$zfile.$$";
     86      my $tmpcache = "tmp.$zfile.$$";      # safety net.  don't overwrite a previous known-good file
    8787
    8888##fixme:  need to open separate zone files for aggregated metazones eg /22 or /14
     
    9090        my $zfilepath = $dnsdb->{bind_export_reverse_zone_path};
    9191        $zfilepath =~ s/\%view/$loc/;
    92         $zfilepath =~ s/\%zone/$revzone/;
     92        $zfilepath =~ s/\%zone/$zfile/;
    9393        $zfilepath =~ s/\%arpazone/$arpazone/;
    9494
     
    119119          my (@zsoa) = $soasth->fetchrow_array();
    120120##fixme: do we even need @loclist passed in?
    121           publishrec_bind($dnsdb, \%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $cidr,
     121          printrec_bind($dnsdb, \%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $cidr,
    122122            $zsoa[0], $zsoa[1], $zsoa[2], $zsoa[3], $zsoa[4], $zsoa[5], $zsoa[6], $loc, '');
    123123        } # if force_refresh etc
     
    158158        }
    159159
    160         publishrec_bind($dnsdb, \%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
     160        printrec_bind($dnsdb, \%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
    161161                $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    162162
     
    390390    open BINDCONF, ">", $tmpconf;
    391391
    392     foreach my $view (@{$viewlist}, 'common', { location => 'common', iplist => '' }) {
     392    foreach my $view (@{$viewlist}, { location => 'common', iplist => '' }) {
    393393#print Dumper($view);
    394394      print BINDCONF "view $view->{location} {\n";
     
    420420
    421421# Print individual records in BIND format
    422 sub publishrec_bind {
     422sub printrec_bind {
    423423  my $dnsdb = shift;
    424424
     
    433433  if ($loc eq '') {
    434434    foreach my $subloc (@{$loclist}) {
    435       publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val,
     435      printrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val,
    436436        $distance, $weight, $port, $ttl, $subloc, $stamp, $expires, $stampactive);
    437437    }
     
    466466#    print *{$zonefiles->{$loc}} "$zone $ttl    IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n"
    467467#       or die "couldn't write $zone SOA: $!";
    468     my $recdata = "$zone.       $ttl    IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n";
    469     recprint($zonefiles, $loclist, $loc, $recdata);
     468
     469    # Prepare the body of the record
     470    my $recdata = "$ttl        IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n";
     471
     472    # ... and prepend the zone name FQDN
     473    if ($revrec eq 'y') {
     474      my $zone2 = DNSDB::_ZONE($zone, 'ZONE', 'r', '.').($zone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
     475      $recdata = "$zone2.      $recdata";
     476    } else {
     477      $recdata = "$zone.       $recdata";
     478    }
     479
     480    __recprint($zonefiles, $loclist, $loc, $recdata);
    470481  } # SOA
    471482
     
    475486#    print {$zonefiles->{$loc}} "$host  $ttl    IN      A       $val\n" or die $!;
    476487    my $recdata = "$host.       $ttl    IN      A       $val\n";
    477     recprint($zonefiles, $loclist, $loc, $recdata);
     488    __recprint($zonefiles, $loclist, $loc, $recdata);
    478489  } # A
    479490
     
    487498
    488499      my $val2 = DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
     500      $host .= "." if $host !~ /\.$/;
    489501      my $recdata = "$val2.     $ttl    IN      NS      $host\n";
    490       recprint($zonefiles, $loclist, $loc, $recdata);
     502      __recprint($zonefiles, $loclist, $loc, $recdata);
    491503
    492504    } else {
    493505      my $recdata = "$host.     $ttl    IN      NS      $val.\n";
    494       recprint($zonefiles, $loclist, $loc, $recdata);
     506      __recprint($zonefiles, $loclist, $loc, $recdata);
    495507    }
    496508  } # NS
     
    500512#    print {$zonefiles->{$loc}} "$host  $ttl    IN      AAAA    $val\n" or die $!;
    501513    my $recdata = "$host.       $ttl    IN      AAAA    $val\n";
    502     recprint($zonefiles, $loclist, $loc, $recdata);
     514    __recprint($zonefiles, $loclist, $loc, $recdata);
    503515  } # AAAA
    504516
     
    508520# should arguably swap host and val first, but MX records really don't make any sense in reverse zones, so any silliness that results from finding one doesn't much matter.
    509521    my $recdata = "$host.       $ttl    IN      MX      $distance $val.\n";
    510     recprint($zonefiles, $loclist, $loc, $recdata);
     522    __recprint($zonefiles, $loclist, $loc, $recdata);
    511523  } # MX
    512524
     
    515527#    print {$zonefiles->{$loc}} "$host  $ttl    IN      TXT     \"$val\"\n" or die $!;
    516528    my $recdata = "$host.       $ttl    IN      TXT     \"$val\"\n";
    517     recprint($zonefiles, $loclist, $loc, $recdata);
     529    __recprint($zonefiles, $loclist, $loc, $recdata);
    518530  } # TXT
    519531
     
    522534#    print {$zonefiles->{$loc}} "$host  $ttl    IN      CNAME   $val\n" or die $!;
    523535    my $recdata = "$host.       $ttl    IN      CNAME   $val.\n";
    524     recprint($zonefiles, $loclist, $loc, $recdata);
     536    __recprint($zonefiles, $loclist, $loc, $recdata);
    525537  } # CNAME
    526538
     
    529541#    print {$zonefiles->{$loc}} "$host  $ttl    IN      SRV     $distance   $weight $port   $val\n" or die $!;
    530542    my $recdata = "$host        $ttl    IN      SRV     $distance       $weight $port   $val.\n";
    531     recprint($zonefiles, $loclist, $loc, $recdata);
     543    __recprint($zonefiles, $loclist, $loc, $recdata);
    532544  } # SRV
    533545
     
    536548#    print {$zonefiles->{$loc}} "$host  $ttl    IN      RP      $val\n" or die $!;
    537549    my $recdata = "$host.       $ttl    IN      RP      $val\n";
    538     recprint($zonefiles, $loclist, $loc, $recdata);
     550    __recprint($zonefiles, $loclist, $loc, $recdata);
    539551  } # RP
    540552
     
    550562#        print {$zonefiles->{$loc}} "$val       $ttl    IN      PTR     $host\n" or die $!;
    551563        my $recdata = "$val.    $ttl    IN      PTR     $host.\n";
    552         recprint($zonefiles, $loclist, $loc, $recdata);
     564        __recprint($zonefiles, $loclist, $loc, $recdata);
    553565      } else {
    554566        $zone = NetAddr::IP->new($zone);
     
    560572#            or die $!;
    561573          my $recdata = "$val.".DNSDB::_ZONE($zone, 'ZONE', 'r', '.').".in-addr.arpa.   $ttl    IN      PTR     $host.\n";
    562           recprint($zonefiles, $loclist, $loc, $recdata);
     574          __recprint($zonefiles, $loclist, $loc, $recdata);
    563575        } else {
    564576          # not going to care about strange results if $val is not an IP value and is resolveable in DNS
     
    569581          my $recdata = DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa').
    570582                ".      $ttl    IN      PTR     $host.\n";
    571           recprint($zonefiles, $loclist, $loc, $recdata);
     583          __recprint($zonefiles, $loclist, $loc, $recdata);
    572584        }
    573585      } # non-".arpa" $val
     
    579591#      print {$zonefiles->{$loc}} "$host        $ttl    IN      PTR     $val\n" or die $!;
    580592      my $recdata = "$host.     $ttl    IN      PTR     $val.\n";
    581       recprint($zonefiles, $loclist, $loc, $recdata);
     593      __recprint($zonefiles, $loclist, $loc, $recdata);
    582594    }
    583595  } # PTR
     
    589601#    $$recflags{$val}++;
    590602    if ($revrec eq 'y') {
    591       publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 12, $val,
     603      printrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 12, $val,
    592604       $distance, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    593605# ... but we need to tweak it for this case?  so the A record gets published...
    594606#$$recflags{$val} = 'a+ptr';
    595607#print {$zonefiles->{$loc}} "=$host:$val:$ttl:$stamp:$loc\n" or die $!;
    596 #          publishrec_bind($dnsdb, \%zonefiles, $recid, 'y', \@loclist, $revzone,
     608#          printrec_bind($dnsdb, \%zonefiles, $recid, 'y', \@loclist, $revzone,
    597609#            $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    598610#  my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
    599611#        $loc, $stamp, $expires, $stampactive) = @_;
    600612    } else {
    601       publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 1, $val,
     613      printrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 1, $val,
    602614        $distance, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    603615      # set a unique flag to skip template expansion for this IP in forward zones
     
    617629      }
    618630    } else {
    619       __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
     631      __publish_template_bind($dnsdb, $val, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
    620632    }
    621633  } # PTR template
     
    631643      }
    632644    } else {
    633       __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
     645      __publish_template_bind($dnsdb, $val, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
    634646    }
    635647  } # A+PTR template
     
    641653  } # AAAA+PTR template
    642654
    643 } # publishrec_bind()
     655} # printrec_bind()
    644656
    645657
     
    729741    }
    730742    # and finally
    731     recprint($zonefiles, $loclist, $loc, $recdata);
     743    __recprint($zonefiles, $loclist, $loc, $recdata);
    732744  } # foreach (@iplist)
    733745} # __publish_template_bind()
     
    736748# actual record printing sub
    737749# loop on the locations here so we don't end up with a huge pot of copypasta
    738 sub recprint {
     750sub __recprint {
    739751  my ($zonefiles, $loclist, $loc, $recdata) = @_;
    740752  if ($loc eq '') {
Note: See TracChangeset for help on using the changeset viewer.