Changeset 855


Ignore:
Timestamp:
09/15/22 16:33:50 (20 months ago)
Author:
Kris Deugau
Message:

/trunk

BIND export, unwinding dev saves, 7 of many many

  • Collection of minor calling/call-receiving refinements, mainly for consistency in handling the database handle
  • Pass a list of locations preparing for better iteration of output to locations/views
  • Change up preliminary SOA record output to actually print BIND format records
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB/ExportBIND.pm

    r854 r855  
    2727sub export {
    2828  # expected to be a DNSDB object
    29   my $self = shift;
    30   my $dbh = $self->{dbh};
     29  my $dnsdb = shift;
    3130
    3231  # to be a hash of views/locations, containing lists of zones
     
    3433
    3534  # allow for future exports of subgroups of records
    36   my $viewlist = $self->getLocList(curgroup => 1);
    37 
    38   my $soasth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".
     35  my $viewlist = $dnsdb->getLocList(curgroup => 1);
     36
     37  my $soasth = $dnsdb->{dbh}->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".
    3938        "FROM records WHERE rdns_id=? AND type=6");
    40   my $recsth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location,extract(epoch from stamp),expires,stampactive ".
     39  my $recsth = $dnsdb->{dbh}->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location,extract(epoch from stamp),expires,stampactive ".
    4140        "FROM records WHERE rdns_id=? AND NOT type=6 ".
    4241        "ORDER BY masklen(inetlazy(val)) DESC, inetlazy(val)");
    4342
    4443  # Fetch active zone list
    45   my $revsth = $self->{dbh}->prepare("SELECT rdns_id,revnet,status,changed,default_location FROM revzones WHERE status=1 ".
     44  my $revsth = $dnsdb->{dbh}->prepare("SELECT rdns_id,revnet,status,changed,default_location FROM revzones WHERE status=1 ".
    4645        "ORDER BY masklen(revnet) DESC, rdns_id");
    4746  # Unflag changed zones, so we can maybe cache the export and not redo everything every time
    48   my $zonesth = $self->{dbh}->prepare("UPDATE revzones SET changed='n' WHERE rdns_id=?");
     47  my $zonesth = $dnsdb->{dbh}->prepare("UPDATE revzones SET changed='n' WHERE rdns_id=?");
    4948  $revsth->execute();
    5049  while (my ($revid,$revzone,$revstat,$changed,$defloc) = $revsth->fetchrow_array) {
    5150    my $tmpzone = NetAddr::IP->new($revzone);
    5251    my $zfile = $tmpzone->network->addr."-".$tmpzone->masklen;
    53 #    my $cachefile = "$self->{exportcache}/$zfile";
    54 #    my $tmpcache = "$self->{exportcache}/tmp.$zfile.$$";
     52#    my $cachefile = "$dnsdb->{exportcache}/$zfile";
     53#    my $tmpcache = "$dnsdb->{exportcache}/tmp.$zfile.$$";
    5554    my $tmpcache = "tmp.$zfile.$$";      # safety net.  don't overwrite a previous known-good file
    5655
     
    6867      #  - the cache file does not exist
    6968      #  - the cache file is empty
    70       if (!$self->{usecache} || $self->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
    71         if ($self->{usecache}) {
     69      if (!$dnsdb->{usecache} || $dnsdb->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
     70        if ($dnsdb->{usecache}) {
    7271          open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
    7372          $zonefilehandle = *ZONECACHE;
     
    7675        # fetch a list of views/locations present in the zone.  we need to publish a file for each one.
    7776        # in the event that no locations are present (~~ $viewlist is empty), /%view collapses to nothing in the zone path
    78         my (@loclist) = $self->{dbh}->selectrow_array("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
     77        my (@loclist) = $dnsdb->{dbh}->selectrow_array("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
    7978        push @loclist, $defloc unless grep /$defloc/, @loclist;
    80         my $zonepath = $self->{bind_export_zone_path};
     79        my $zonepath = $dnsdb->{bind_export_zone_path};
    8180        my %zonefiles;  # to be a list of file handles.
    8281##fixme:  convert logical revzone into .arpa name
     
    9493        $soasth->execute($revid);
    9594        my (@zsoa) = $soasth->fetchrow_array();
    96         printrec_bind($zonefilehandle, $zsoa[7], 'y',\%recflags,$revzone,
     95        printrec_bind($zonefilehandle, $zsoa[7], 'y', \@loclist, $revzone,
    9796          $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
    9897
     
    135134        } # while ($recsth)
    136135
    137         if ($self->{usecache}) {
     136        if ($dnsdb->{usecache}) {
    138137          close ZONECACHE; # force the file to be written
    139138          # catch obvious write errors that leave an empty temp file
     
    148147    };
    149148    if ($@) {
    150       die "error writing ".($self->{usecache} ? 'new data for ' : '')."$revzone: $@\n";
     149      die "error writing ".($dnsdb->{usecache} ? 'new data for ' : '')."$revzone: $@\n";
    151150      # error!  something borked, and we should be able to fall back on the old cache file
    152151      # report the error, somehow.
     
    157156    }
    158157
    159 #    if ($self->{usecache}) {
     158#    if ($dnsdb->{usecache}) {
    160159#      # We've already made as sure as we can that a cached zone file is "good",
    161160#      # although possibly stale/obsolete due to errors creating a new one.
     
    204203# Print individual records in BIND format
    205204sub printrec_bind {
     205  my $dnsdb = shift;
    206206  my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
    207         $recloc, $stamp, $expires, $stampactive) = @_;
     207        $loc, $stamp, $expires, $stampactive) = @_;
    208208
    209209  # Just In Case something is lingering in the DB
    210   $recloc = '' if !$loc;
     210  $loc = '' if !$loc;
    211211
    212212  ## And now to the records!
     
    225225# anyone who says they need sub-nibble v6 delegations, at this time, needs their head examined.
    226226##fixme?:  alternate SOA serial schemes?
    227       ($serial) = $self->{dbh}->selectrow_array("SELECT zserial FROM revzones WHERE revnet=?", undef, $zone);
     227      ($serial) = $dnsdb->{dbh}->selectrow_array("SELECT zserial FROM revzones WHERE revnet=?", undef, $zone);
    228228      $zone = NetAddr::IP->new($zone);
    229229      # handle split-n-multiply SOA for off-octet (8 < mask < 16) or (16 < mask < 24) v4 zones
    230230      if (!$zone->{isv6} && ($zone->masklen < 24) && ($zone->masklen % 8 != 0)) {
    231231        foreach my $szone ($zone->split($zone->masklen + (8 - $zone->masklen % 8))) {
    232           $szone = _ZONE($szone, 'ZONE.in-addr.arpa', 'r', '.');
    233           print $datafile "Z$szone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n"
     232          $szone = DNSDB::_ZONE($szone, 'ZONE.in-addr.arpa', 'r', '.');
     233#          print $datafile "Z$szone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n"
     234print "$szone   $ttl    IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n"
    234235            or die $!;
    235236        }
    236237        return; # skips "default" bits just below
    237238      }
    238       $zone = _ZONE($zone, 'ZONE', 'r', '.').($zone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
     239      $zone = DNSDB::_ZONE($zone, 'ZONE', 'r', '.').($zone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
    239240    } else {
    240241      # just snarfing the right SOA serial for the zone type
    241242##fixme?:  alternate SOA serial schemes?
    242       ($serial) = $self->{dbh}->selectrow_array("SELECT zserial FROM domains WHERE domain=?", undef, $zone);
     243      ($serial) = $dnsdb->{dbh}->selectrow_array("SELECT zserial FROM domains WHERE domain=?", undef, $zone);
    243244    } # revrec <> 'y'
    244245    # suppress a "uninitialized value" warning.  should be impossible but...
    245246    # abuse hours as the last digit pair of the serial for simplicity
    246247    $serial = strftime("%Y%m%d%H", localtime()) if !$serial;
    247     print $zonefiles{$recloc} "Z$zone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n"
    248       or die $!;
     248#    print *{$zonefiles->{$loc}} "Z$zone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n"
     249#      or die $!;
     250    print *{$zonefiles->{$loc}} "$zone  $ttl    IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n"
     251       or die $!;
    249252  } # SOA
    250253
Note: See TracChangeset for help on using the changeset viewer.