Changeset 878 for trunk/DNSDB


Ignore:
Timestamp:
01/17/23 18:22:00 (2 years ago)
Author:
Kris Deugau
Message:

/trunk

BIND export, unwinding dev saves, intermediate cleanup

Do a "does this run without execution errors?" pass over BIND export.

  • Fix silly little syntax errors
  • Make sure copy-paste-adjust chunks have in fact gotten the "adjust" part for the different dataset
  • Remove duplicate variables and calls to fill them
  • Import DNSDB so we can actually create an instance to pass through to internal subs that rely on shared variables as well as DNSDB subs that expect a DNSDB object
  • Forcibly add the "common" view to the list for writing the BIND view list definition
  • Consistently print (or don't) BIND view definition lines to STDOUT or the config fragment file. Might be useful to have an option to print to both?
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB/ExportBIND.pm

    r877 r878  
    44##
    55# $Id$
    6 # Copyright 2022 Kris Deugau <kdeugau@deepnet.cx>
     6# Copyright 2022,2023 Kris Deugau <kdeugau@deepnet.cx>
    77#
    88#    This program is free software: you can redistribute it and/or modify
     
    2525use warnings;
    2626
     27use DNSDB;
     28
    2729sub export {
    2830  # expected to be a DNSDB object
     
    8688##fixme:  need to open separate zone files for aggregated metazones eg /22 or /14
    8789      foreach my $loc (@loclist) {
    88         my $zfilepath = $dnsdb->bind_export_reverse_zone_path};
     90        my $zfilepath = $dnsdb->{bind_export_reverse_zone_path};
    8991        $zfilepath =~ s/\%view/$loc/;
    9092        $zfilepath =~ s/\%zone/$revzone/;
     
    111113
    112114          printf {$zonefiles{$loc}} "; %s in view %s exported %s\n", $arpazone, $loc, scalar(localtime)
    113             or die "Error writing header [$zone, '$loc']: $!\n";;
     115            or die "Error writing header [$cidr, '$loc']: $!\n";
    114116
    115117          # need to fetch this separately since the rest of the records all (should) have real IPs in val
     
    117119          my (@zsoa) = $soasth->fetchrow_array();
    118120##fixme: do we even need @loclist passed in?
    119           publishrec_bind(\%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $cidr,
     121          publishrec_bind($dnsdb, \%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $cidr,
    120122            $zsoa[0], $zsoa[1], $zsoa[2], $zsoa[3], $zsoa[4], $zsoa[5], $zsoa[6], $loc, '');
    121123        } # if force_refresh etc
     
    127129      # now the meat of the records
    128130      $recsth->execute($revid);
    129       my $fullzone = _ZONE($tmpzone, 'ZONE', 'r', '.').($tmpzone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
    130131
    131132      while (my ($host, $type, $val, $dist, $weight, $port, $ttl, $recid, $loc, $stamp, $expires, $stampactive)
     
    136137        if ($val =~ /\.arpa$/) {
    137138          # val is non-IP
    138           if ($val !~ /$fullzone$/) {
    139             warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
     139          if ($val !~ /$arpazone$/) {
     140            warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $cidr)\n";
    140141            next;
    141142          }
    142143        } else {
    143144          my $ipval = new NetAddr::IP $val;
    144           if (!$tmpzone->contains($ipval)) {
    145             warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
     145          if (!$cidr->contains($ipval)) {
     146            warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $cidr)\n";
    146147            next;
    147148          }
     
    157158        }
    158159
    159         publishrec_bind(\%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
     160        publishrec_bind($dnsdb, \%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
    160161                $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    161162
     
    230231#  %recflags = ();
    231232
    232   while (my ($domid,$dom,$domstat,$changed) = $domsth->fetchrow_array) {
     233  while (my ($domid,$domain,$domstat,$changed) = $domsth->fetchrow_array) {
    233234
    234235    # fetch a list of views/locations present in the zone.  we need to publish a file for each one.
     
    244245
    245246##fixme:  use tmpfile module for more secure temp files?  want the zone name at least in it anyway, not sure that works...
    246       my $zfile = $dom;  # can probably drop this intermediate
     247      my $zfile = $domain;  # can probably drop this intermediate
    247248      my $tmpcache = "tmp.$zfile.$$";   # safety net.  don't overwrite a previous known-good file
    248249      foreach my $loc (@loclist) {
     
    289290
    290291          # drop in a header line so we know when things went KABOOM
    291           printf {$zonefiles{$loc}} "; %s in view %s exported %s\n", $arpazone, $loc, scalar(localtime)
    292                 or die "Error writing header [$cidr, '$loc']: $!\n";
    293 
    294           printrec_bind(\%zonefiles, \@loclist, $zsoa[7], 'n', \%recflags, $dom,
     292          printf {$zonefiles{$loc}} "; %s in view %s exported %s\n", $domain, $loc, scalar(localtime)
     293                or die "Error writing header [$domain, '$loc']: $!\n";
     294
     295          printrec_bind($dnsdb, \%zonefiles, \@loclist, $zsoa[7], 'n', \%recflags, $domain,
    295296            $zsoa[0], $zsoa[1], $zsoa[2], $zsoa[3], $zsoa[4], $zsoa[5], $zsoa[6], $loc, '');
    296297
    297 #          $self->_printrec_tiny($zonefilehandle, $zsoa[7], 'n',\%recflags,$dom,
     298#          $self->_printrec_tiny($zonefilehandle, $zsoa[7], 'n',\%recflags, $domain,
    298299#            $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
    299300
     
    301302
    302303        # tag the zonefile for publication in the view
    303         push @{$viewzones{$loc}}, $arpazone;
     304        push @{$viewzones{$loc}}, $domain;
    304305      } # foreach @loclist
    305306
     
    320321
    321322        # Check for out-of-zone data
    322         $host = $dom if $host eq '@';
    323         if ($host !~ /$dom$/i) {
    324           warn "Not exporting out-of-zone record $host $type $val, $ttl (zone $dom)\n";
     323        $host = $domain if $host eq '@';
     324        if ($host !~ /$domain$/i) {
     325          warn "Not exporting out-of-zone record $host $type $val, $ttl (zone $domain)\n";
    325326          next;
    326327        }
     
    328329        $recflags{$recid} = 1;
    329330
    330         printrec_bind(\%zonefiles, \@loclist, $recid, 'n', \%recflags, $dom,
     331        printrec_bind($dnsdb, \%zonefiles, \@loclist, $recid, 'n', \%recflags, $domain,
    331332          $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    332333
     
    339340        "JOIN domains d ON r.domain_id=d.domain_id ".
    340341        "WHERE r.type=2 AND d.domain LIKE ?");
    341       $subnssth->execute('%.'.$dom);
     342      $subnssth->execute('%.'.$domain);
    342343      while (my ($host,$val,$ttl,$recid,$loc,$stamp,$expires,$stampactive) = $subnssth->fetchrow_array) {
    343         printrec_bind(\%zonefiles, \@loclist, $recid, 'n', \%recflags, $dom,
     344        printrec_bind($dnsdb, \%zonefiles, \@loclist, $recid, 'n', \%recflags, $domain,
    344345          $host, 2, $val, '', '', '', $ttl, $loc, $stamp, $expires, $stampactive);
    345346      } # subdomain-ns-recsth
     
    355356#        }
    356357
    357       } # if $changed or cache filesize is 0
     358#      } # if $changed or cache filesize is 0
    358359
    359360    };
    360361    if ($@) {
    361       die "error writing ".($dnsdb->{usecache} ? 'new data for ' : '')."$revzone: $@\n";
     362      die "error writing ".($dnsdb->{usecache} ? 'new data for ' : '')."$domain: $@\n";
    362363      # error!  something borked, and we should be able to fall back on the old cache file
    363364      # report the error, somehow.
     
    365366      # mark zone as unmodified.  Only do this if no errors, that way
    366367      # export failures should recover a little more automatically.
    367       $zonesth->execute($revid);
     368      $zonesth->execute($domid);
    368369    }
    369370
     
    389390    open BINDCONF, ">", $tmpconf;
    390391
    391     foreach my $view (@{$viewlist}, 'common') {
     392    foreach my $view (@{$viewlist}, 'common', { location => 'common', iplist => '' }) {
    392393#print Dumper($view);
    393394      print BINDCONF "view $view->{location} {\n";
     
    406407      foreach my $zone (@{$viewzones{$view->{location}}}) {
    407408##fixme:  notify settings, maybe per-zone?
    408         print qq(  zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n  };\n);
     409        print BINDCONF qq(  zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n  };\n);
     410#        print qq(  zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n  };\n);
    409411      }
    410412      print BINDCONF "};\n\n";
     
    431433  if ($loc eq '') {
    432434    foreach my $subloc (@{$loclist}) {
    433       publishrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
    434         $subloc, $stamp, $expires, $stampactive);
     435      publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val,
     436        $distance, $weight, $port, $ttl, $subloc, $stamp, $expires, $stampactive);
    435437    }
    436438  }
     
    489491
    490492    } else {
    491       my $recdata = "$host.     $ttl    IN      NS      $val.\n"
     493      my $recdata = "$host.     $ttl    IN      NS      $val.\n";
    492494      recprint($zonefiles, $loclist, $loc, $recdata);
    493495    }
     
    587589#    $$recflags{$val}++;
    588590    if ($revrec eq 'y') {
    589       publishrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 12, $val, $distance, $weight, $port, $ttl,
    590        $loc, $stamp, $expires, $stampactive);
     591      publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 12, $val,
     592       $distance, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    591593# ... but we need to tweak it for this case?  so the A record gets published...
    592594#$$recflags{$val} = 'a+ptr';
    593595#print {$zonefiles->{$loc}} "=$host:$val:$ttl:$stamp:$loc\n" or die $!;
    594 #          publishrec_bind(\%zonefiles, $recid, 'y', \@loclist, $revzone,
     596#          publishrec_bind($dnsdb, \%zonefiles, $recid, 'y', \@loclist, $revzone,
    595597#            $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    596598#  my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
    597599#        $loc, $stamp, $expires, $stampactive) = @_;
    598600    } else {
    599       publishrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 1, $val, $distance, $weight, $port, $ttl,
    600         $loc, $stamp, $expires, $stampactive);
     601      publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 1, $val,
     602        $distance, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    601603      # set a unique flag to skip template expansion for this IP in forward zones
    602604      $$recflags{$val} = 'a';
     
    612614    if ($val->masklen <= 16) {
    613615      foreach my $sub ($val->split(16)) {
    614         __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
     616        __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
    615617      }
    616618    } else {
    617       __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
     619      __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
    618620    }
    619621  } # PTR template
     
    626628    if ($val->masklen < 16) {
    627629      foreach my $sub ($val->split(16)) {
    628         __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
     630        __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
    629631      }
    630632    } else {
    631       __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
     633      __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
    632634    }
    633635  } # A+PTR template
     
    643645
    644646sub __publish_template_bind {
     647  my $dnsdb = shift;
    645648  my $sub = shift;
    646649  my $recflags = shift;
Note: See TracChangeset for help on using the changeset viewer.