Changeset 869


Ignore:
Timestamp:
09/28/22 15:29:29 (19 months ago)
Author:
Kris Deugau
Message:

/trunk

BIND export, unwinding dev saves, 21 of many many

  • Change record output concept to push the actual print down to new common sub recprint() so each record type doesn't have to manually loop over the locations
  • Rename printrec_bind() to publishrec_bind() to better reflect this additional layer
  • Pass location list to publishrec_bind() again since we need to hand it off to recprint()
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB/ExportBIND.pm

    r868 r869  
    111111        $soasth->execute($revid);
    112112        my (@zsoa) = $soasth->fetchrow_array();
     113
    113114##fixme: do we even need @loclist passed in?
    114         printrec_bind(\%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $revzone,
    115           $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
     115        publishrec_bind(\%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $revzone,
     116          $zsoa[0], $zsoa[1], $zsoa[2], $zsoa[3], $zsoa[4], $zsoa[5], $zsoa[6], $loc, '');
    116117
    117118        $recsth->execute($revid);
     
    146147          }
    147148
    148           printrec_bind(\%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
     149          publishrec_bind(\%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
    149150            $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    150151
     
    225226
    226227# Print individual records in BIND format
    227 sub printrec_bind {
     228sub publishrec_bind {
    228229  my $dnsdb = shift;
    229230
     
    238239  if ($loc eq '') {
    239240    foreach my $subloc (@{$loclist}) {
    240       printrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
     241      publishrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
    241242        $subloc, $stamp, $expires, $stampactive);
    242243    }
     
    269270#    print *{$zonefiles->{$loc}} "Z$zone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n"
    270271#      or die $!;
    271     print *{$zonefiles->{$loc}} "$zone  $ttl    IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n"
    272        or die "couldn't write $zone SOA: $!";
     272#    print *{$zonefiles->{$loc}} "$zone $ttl    IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n"
     273#       or die "couldn't write $zone SOA: $!";
     274    my $recdata = "$zone        $ttl    IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n";
     275    recprint($zonefiles, $loclist, $loc, $recdata);
    273276  } # SOA
    274277
     
    276279#    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
    277280#    print $datafile "+$host:$val:$ttl:$stamp:$loc\n" or die $!;
    278     print {$zonefiles->{$loc}} "$host  $ttl    IN      A       $val\n" or die $!;
     281#    print {$zonefiles->{$loc}} "$host  $ttl    IN      A       $val\n" or die $!;
     282    my $recdata = "$host        $ttl    IN      A       $val\n";
     283    recprint($zonefiles, $loclist, $loc, $recdata);
    279284  } # A
    280285
     
    288293
    289294#      print {$zonefiles->{$loc}} "$zone       $ttl    IN      NS      $host\n";
    290       print "$zone     $ttl    IN      NS      $host\n" or die $!;
     295#      print "$zone     $ttl    IN      NS      $host\n" or die $!;
     296      my $recdata = "$zone      $ttl    IN      NS      $host\n";
     297      recprint($zonefiles, $loclist, $loc, $recdata);
    291298
    292299    } else {
     
    297304  elsif ($typemap{$type} eq 'AAAA') {
    298305#    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
    299     print {$zonefiles->{$loc}} "$host  $ttl    IN      AAAA    $val\n" or die $!;
     306#    print {$zonefiles->{$loc}} "$host  $ttl    IN      AAAA    $val\n" or die $!;
     307    my $recdata = "$host        $ttl    IN      AAAA    $val\n";
     308    recprint($zonefiles, $loclist, $loc, $recdata);
    300309  } # AAAA
    301310
    302311  elsif ($typemap{$type} eq 'TXT') {
    303312#    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
    304     print {$zonefiles->{$loc}} "$host  $ttl    IN      TXT     \"$val\"\n" or die $!;
     313#    print {$zonefiles->{$loc}} "$host  $ttl    IN      TXT     \"$val\"\n" or die $!;
     314    my $recdata = "$host        $ttl    IN      TXT     \"$val\"\n";
     315    recprint($zonefiles, $loclist, $loc, $recdata);
    305316  } # TXT
    306317
    307318  elsif ($typemap{$type} eq 'CNAME') {
    308319#    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
    309     print {$zonefiles->{$loc}} "$host  $ttl    IN      CNAME   $val\n" or die $!;
     320#    print {$zonefiles->{$loc}} "$host  $ttl    IN      CNAME   $val\n" or die $!;
     321    my $recdata = "$host        $ttl    IN      CNAME   $val\n";
     322    recprint($zonefiles, $loclist, $loc, $recdata);
    310323  } # CNAME
    311324
    312325  elsif ($typemap{$type} eq 'SRV') {
    313326#    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
    314     print {$zonefiles->{$loc}} "$host  $ttl    IN      SRV     $distance   $weight $port   $val\n" or die $!;
     327#    print {$zonefiles->{$loc}} "$host  $ttl    IN      SRV     $distance   $weight $port   $val\n" or die $!;
     328    my $recdata = "$host        $ttl    IN      SRV     $distance       $weight $port   $val\n";
     329    recprint($zonefiles, $loclist, $loc, $recdata);
    315330  } # SRV
    316331
    317332  elsif ($typemap{$type} eq 'RP') {
    318333#    ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
    319     print {$zonefiles->{$loc}} "$host  $ttl    IN      RP      $val\n" or die $!;
     334#    print {$zonefiles->{$loc}} "$host  $ttl    IN      RP      $val\n" or die $!;
     335    my $recdata = "$host        $ttl    IN      RP      $val\n";
     336    recprint($zonefiles, $loclist, $loc, $recdata);
    320337  } # RP
    321338
     
    327344      if ($val =~ /\.arpa$/) {
    328345        # someone put in the formal .arpa name.  humor them.
    329         print {$zonefiles->{$loc}} "$val        $ttl    IN      PTR     $host\n" or die $!;
     346#        print {$zonefiles->{$loc}} "$val       $ttl    IN      PTR     $host\n" or die $!;
     347        my $recdata = "$val     $ttl    IN      PTR     $host\n";
     348        recprint($zonefiles, $loclist, $loc, $recdata);
    330349      } else {
    331350        $zone = NetAddr::IP->new($zone);
     
    333352          # sub-octet v4 zone
    334353          ($val) = ($val =~ /\.(\d+)$/);
    335           print {$zonefiles->{$loc}} "$val.".DNSDB::_ZONE($zone, 'ZONE', 'r', '.').'.in-addr.arpa'.
    336                 "       $ttl    IN      PTR     $host\n"
    337             or die $!;
     354#          print {$zonefiles->{$loc}} "$val.".DNSDB::_ZONE($zone, 'ZONE', 'r', '.').'.in-addr.arpa'.
     355#               "       $ttl    IN      PTR     $host\n"
     356#            or die $!;
     357          my $recdata = "$val.".DNSDB::_ZONE($zone, 'ZONE', 'r', '.').".in-addr.arpa    $ttl    IN      PTR     $host\n";
     358          recprint($zonefiles, $loclist, $loc, $recdata);
    338359        } else {
    339360          # not going to care about strange results if $val is not an IP value and is resolveable in DNS
    340361          $val = NetAddr::IP->new($val);
    341           print {$zonefiles->{$loc}} DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa').
    342                 "       $ttl    IN      PTR     $host\n"
    343             or die $!;
    344 
     362#          print {$zonefiles->{$loc}} DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa').
     363#               "       $ttl    IN      PTR     $host\n"
     364#            or die $!;
     365          my $recdata = DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa').
     366                "       $ttl    IN      PTR     $host\n";
     367          recprint($zonefiles, $loclist, $loc, $recdata);
    345368        }
    346369      } # non-".arpa" $val
     
    350373      # in reverse zones...  OTOH we can't validate them any which way, so we cross our
    351374      # fingers and close our eyes and make it Someone Else's Problem.
    352       print {$zonefiles->{$loc}} "^$host:$val:$ttl:$stamp:$loc\n" or die $!;
     375#      print {$zonefiles->{$loc}} "$host        $ttl    IN      PTR     $val\n" or die $!;
     376      my $recdata = "$host      $ttl    IN      PTR     $val\n";
     377      recprint($zonefiles, $loclist, $loc, $recdata);
    353378    }
    354379  } # PTR
     
    359384    $$recflags{$val}++;
    360385    if ($revrec eq 'y') {
    361       printrec_bind($zonefiles, $recid, $revrec, $recflags, $zone, $host, 12, $val, $distance, $weight, $port, $ttl,
     386      publishrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 12, $val, $distance, $weight, $port, $ttl,
    362387       $loc, $stamp, $expires, $stampactive);
    363388#print {$zonefiles->{$loc}} "=$host:$val:$ttl:$stamp:$loc\n" or die $!;
    364 #          printrec_bind(\%zonefiles, $recid, 'y', \@loclist, $revzone,
     389#          publishrec_bind(\%zonefiles, $recid, 'y', \@loclist, $revzone,
    365390#            $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    366391#  my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
    367392#        $loc, $stamp, $expires, $stampactive) = @_;
    368393    } else {
    369       printrec_bind($zonefiles, $recid, $revrec, $recflags, $zone, $host, 1, $val, $distance, $weight, $port, $ttl,
     394      publishrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 1, $val, $distance, $weight, $port, $ttl,
    370395       $loc, $stamp, $expires, $stampactive);
    371396    }
     
    380405    if ($val->masklen <= 16) {
    381406      foreach my $sub ($val->split(16)) {
    382         __publish_template_bind($sub, $recflags, $host, $zonefiles->{$loc}, $ttl, $stamp, $loc, $zone, 1);
     407        __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
    383408      }
    384409    } else {
    385       __publish_template_bind($val, $recflags, $host, $zonefiles->{$loc}, $ttl, $stamp, $loc, $zone, 1);
     410      __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
    386411    }
    387412  } # PTR template
     
    394419    if ($val->masklen < 16) {
    395420      foreach my $sub ($val->split(16)) {
    396         __publish_template_bind($sub, $recflags, $host, $zonefiles->{$loc}, $ttl, $stamp, $loc, $zone, 'y');
     421        __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
    397422      }
    398423    } else {
    399       __publish_template_bind($val, $recflags, $host, $zonefiles->{$loc}, $ttl, $stamp, $loc, $zone, 'y');
     424      __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
    400425    }
    401426  } # A+PTR template
     
    407432  } # AAAA+PTR template
    408433
    409 } # printrec_bind()
     434} # publishrec_bind()
    410435
    411436
     
    414439  my $recflags = shift;
    415440  my $hpat = shift;
    416   my $fh = shift;
     441  my $zonefiles = shift;
     442  my $loclist = shift;
    417443  my $ttl = shift;
    418444  my $stamp = shift;
     
    449475##fixme:  trim merged record type voodoo.  "if ($ptrflag) {} else {}" ?
    450476#    if ($ptrflag || $zone->masklen > 24) {
    451     if ($revrec eq 'y' || $zone->masklen > 24) {
     477    my $recdata;
     478    if ($revrec eq 'y') {
     479# || $zone->masklen > 24) {
    452480#      print $fh "^$lastoct.$arpabase:$rec:$ttl:$stamp:$loc\n" or die $!;
    453481##fixme: use $ORIGIN instead?  make the FQDN output switchable-optional?
    454       print $fh "$lastoct.$arpabase    $ttl    IN      PTR     $rec\n" or die $!;
    455 #      if (!$ptrflag) {
    456       if ($revrec ne 'y') {
     482#      print $fh "$lastoct.$arpabase    $ttl    IN      PTR     $rec\n" or die $!;
     483#      if ($revrec ne 'y') {
    457484        # print a separate A record.  Arguably we could use an = record here instead.
    458485#        print $fh "+$rec:$ip:$ttl:$stamp:$loc\n" or die $!;
    459         print $fh "$rec $ttl    IN      A       $ip\n" or die $!;
    460       }
     486#        print $fh "$rec        $ttl    IN      A       $ip\n" or die $!;
     487#      }
     488      $recdata = "$lastoct.$arpabase    $ttl    IN      PTR     $rec\n";
    461489    } else {
    462490      # A record, not merged
    463491#      print $fh "=$rec:$ip:$ttl:$stamp:$loc\n" or die $!;
    464       print $fh "$rec   $ttl    IN      A       $ip\n" or die $!;
    465     }
     492#      print $fh "$rec  $ttl    IN      A       $ip\n" or die $!;
     493      $recdata = "$rec  $ttl    IN      A       $ip\n";
     494    }
     495    # and finally
     496    recprint($zonefiles, $loclist, $loc, $recdata);
     497  } # foreach (@iplist)
     498} # __publish_template_bind()
     499
     500
     501# actual record printing sub
     502# loop on the locations here so we don't end up with a huge pot of copypasta
     503sub recprint {
     504  my ($zonefiles, $loclist, $loc, $recdata) = @_;
     505  if ($loc eq '') {
     506    # "common" record visible in all locations
     507    foreach my $rloc (@{$loclist}) {
     508      print {$zonefiles->{$rloc}} $recdata or die $!;
     509    }
     510  } else {
     511    # record with specific location tagged
     512    print {$zonefiles->{$loc}} $recdata or die $!;
    466513  }
    467 } # __publish_template_bind()
     514}
    468515
    4695161;
Note: See TracChangeset for help on using the changeset viewer.