Changeset 873


Ignore:
Timestamp:
09/29/22 13:59:33 (2 years ago)
Author:
Kris Deugau
Message:

/trunk

BIND export, unwinding dev saves, 25 of many many

  • Add some docucomments, and tighten reproduceability with an ORDER BY on the domain record retrieval
  • Add a missing closing brace
  • Shift an in-zone check after a normalization
  • Fix up most places requiring dot-terminated FQDNs with either a terminating dot or publishing only the relevant hostname part when bind_export_fqdn is unset
  • Add MX record publication
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB/ExportBIND.pm

    r872 r873  
    4040  my $soasth = $dnsdb->{dbh}->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".
    4141        "FROM records WHERE rdns_id=? AND type=6");
     42  # record order matters for reverse zones because we need to override larger templates with smaller ones.
    4243  my $recsth = $dnsdb->{dbh}->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location,extract(epoch from stamp),expires,stampactive ".
    4344        "FROM records WHERE rdns_id=? AND NOT type=6 ".
     
    202203  $soasth = $dnsdb->{dbh}->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".
    203204        "FROM records WHERE domain_id=? AND type=6");
     205  # record order doesn't strictly matter, but ordering by conventional zone
     206  # file order doesn't really have a lever without tweaking the table structure.
     207  # ordering by nominal parent-child label hierarchy (as actually found live
     208  # in some AXFRed zone files) would be even hairier.
     209  # ordering by record_id has the advantage of consistent order export to export
    204210  $recsth = $dnsdb->{dbh}->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location,extract(epoch from stamp),expires,stampactive ".
    205         "FROM records WHERE domain_id=? AND NOT type=6");
     211        "FROM records WHERE domain_id=? AND NOT type=6 ORDER BY record_id");
    206212#      "FROM records WHERE domain_id=? AND type < 65280");     # Just exclude all types relating to rDNS
    207213
     
    280286#            $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
    281287
     288        } # if force_refresh etc
     289
    282290        # tag the zonefile for publication in the view
    283291        push @{$viewzones{$loc}}, $arpazone;
     
    287295      while (my ($host,$type,$val,$dist,$weight,$port,$ttl,$recid,$loc,$stamp,$expires,$stampactive) = $recsth->fetchrow_array) {
    288296        next if $recflags{$recid};
    289 
    290         # Check for out-of-zone data
    291         $host = $dom if $host eq '@';
    292         if ($host !~ /$dom$/i) {
    293           warn "Not exporting out-of-zone record $host $type $val, $ttl (zone $dom)\n";
    294           next;
    295         }
    296297
    297298        # Spaces are evil.
     
    303304          $val =~ s/\s+$//;
    304305        }
     306
     307        # Check for out-of-zone data
     308        $host = $dom if $host eq '@';
     309        if ($host !~ /$dom$/i) {
     310          warn "Not exporting out-of-zone record $host $type $val, $ttl (zone $dom)\n";
     311          next;
     312        }
     313
    305314        $recflags{$recid} = 1;
    306315
     
    429438#    print *{$zonefiles->{$loc}} "$zone $ttl    IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n"
    430439#       or die "couldn't write $zone SOA: $!";
    431     my $recdata = "$zone        $ttl    IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n";
     440    my $recdata = "$zone.       $ttl    IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n";
    432441    recprint($zonefiles, $loclist, $loc, $recdata);
    433442  } # SOA
    434443
    435444  elsif ($typemap{$type} eq 'A') {
    436 #    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
     445    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
    437446#    print $datafile "+$host:$val:$ttl:$stamp:$loc\n" or die $!;
    438447#    print {$zonefiles->{$loc}} "$host  $ttl    IN      A       $val\n" or die $!;
    439     my $recdata = "$host        $ttl    IN      A       $val\n";
     448    my $recdata = "$host.       $ttl    IN      A       $val\n";
    440449    recprint($zonefiles, $loclist, $loc, $recdata);
    441450  } # A
     
    449458#      }
    450459
    451 #      print {$zonefiles->{$loc}} "$zone       $ttl    IN      NS      $host\n";
    452 #      print "$zone     $ttl    IN      NS      $host\n" or die $!;
    453       my $recdata = "$zone      $ttl    IN      NS      $host\n";
     460      my $val2 = DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
     461      my $recdata = "$val2.     $ttl    IN      NS      $host\n";
    454462      recprint($zonefiles, $loclist, $loc, $recdata);
    455463
    456464    } else {
    457 #      print $datafile "\&$host"."::$val:$ttl:$stamp:$loc\n" or die $!;
     465      my $recdata = "$host.     $ttl    IN      NS      $val.\n"
     466      recprint($zonefiles, $loclist, $loc, $recdata);
    458467    }
    459468  } # NS
     
    462471#    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
    463472#    print {$zonefiles->{$loc}} "$host  $ttl    IN      AAAA    $val\n" or die $!;
    464     my $recdata = "$host        $ttl    IN      AAAA    $val\n";
     473    my $recdata = "$host.       $ttl    IN      AAAA    $val\n";
    465474    recprint($zonefiles, $loclist, $loc, $recdata);
    466475  } # AAAA
     476
     477  elsif ($typemap{$type} eq 'MX') {
     478#    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
     479#    print {$zonefiles->{$loc}} "$host  $ttl    IN      MX      $distance $val\n" or die $!;
     480# 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.
     481    my $recdata = "$host.       $ttl    IN      MX      $distance $val.\n";
     482    recprint($zonefiles, $loclist, $loc, $recdata);
     483  } # MX
    467484
    468485  elsif ($typemap{$type} eq 'TXT') {
    469486#    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
    470487#    print {$zonefiles->{$loc}} "$host  $ttl    IN      TXT     \"$val\"\n" or die $!;
    471     my $recdata = "$host        $ttl    IN      TXT     \"$val\"\n";
     488    my $recdata = "$host.       $ttl    IN      TXT     \"$val\"\n";
    472489    recprint($zonefiles, $loclist, $loc, $recdata);
    473490  } # TXT
     
    476493#    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
    477494#    print {$zonefiles->{$loc}} "$host  $ttl    IN      CNAME   $val\n" or die $!;
    478     my $recdata = "$host        $ttl    IN      CNAME   $val\n";
     495    my $recdata = "$host.       $ttl    IN      CNAME   $val.\n";
    479496    recprint($zonefiles, $loclist, $loc, $recdata);
    480497  } # CNAME
     
    494511  } # RP
    495512
    496 
    497513  elsif ($typemap{$type} eq 'PTR') {
    498514    $$recflags{$val}++;
     
    502518        # someone put in the formal .arpa name.  humor them.
    503519#        print {$zonefiles->{$loc}} "$val       $ttl    IN      PTR     $host\n" or die $!;
    504         my $recdata = "$val     $ttl    IN      PTR     $host\n";
     520        my $recdata = "$val.    $ttl    IN      PTR     $host.\n";
    505521        recprint($zonefiles, $loclist, $loc, $recdata);
    506522      } else {
     
    512528#               "       $ttl    IN      PTR     $host\n"
    513529#            or die $!;
    514           my $recdata = "$val.".DNSDB::_ZONE($zone, 'ZONE', 'r', '.').".in-addr.arpa    $ttl    IN      PTR     $host\n";
     530          my $recdata = "$val.".DNSDB::_ZONE($zone, 'ZONE', 'r', '.').".in-addr.arpa.   $ttl    IN      PTR     $host.\n";
    515531          recprint($zonefiles, $loclist, $loc, $recdata);
    516532        } else {
     
    521537#            or die $!;
    522538          my $recdata = DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa').
    523                 "       $ttl    IN      PTR     $host\n";
     539                ".      $ttl    IN      PTR     $host.\n";
    524540          recprint($zonefiles, $loclist, $loc, $recdata);
    525541        }
     
    606622
    607623  # do this conversion once, not (number-of-ips-in-subnet) times
    608   my $arpabase = DNSDB::_ZONE($zone, 'ZONE.in-addr.arpa', 'r', '.');
     624  my $arpabase = DNSDB::_ZONE($zone, 'ZONE.in-addr.arpa.', 'r', '.');
    609625
    610626  my $iplist = $sub->splitref(32);
     
    643659#        print $fh "$rec        $ttl    IN      A       $ip\n" or die $!;
    644660#      }
    645       $recdata = "$lastoct.$arpabase    $ttl    IN      PTR     $rec\n";
     661      if ($dnsdb->{bind_export_fqdn}) {
     662        $recdata = "$lastoct.$arpabase  $ttl    IN      PTR     $rec.\n";
     663      } else {
     664        $recdata = "$lastoct    $ttl    IN      PTR     $rec.\n";
     665      }
     666
    646667    } else {
    647668      # A record, not merged
    648669#      print $fh "=$rec:$ip:$ttl:$stamp:$loc\n" or die $!;
    649670#      print $fh "$rec  $ttl    IN      A       $ip\n" or die $!;
    650       $recdata = "$rec  $ttl    IN      A       $ip\n";
     671      $rec =~ s/\.$zone$// unless $dnsdb->{bind_export_fqdn};
     672      $recdata = "$rec. $ttl    IN      A       $ip\n";
    651673    }
    652674    # and finally
Note: See TracChangeset for help on using the changeset viewer.