[847] | 1 | # dns/trunk/DNSDB/ExportBIND.pm
|
---|
| 2 | # BIND data export/publication
|
---|
| 3 | # Call through DNSDB.pm's export() sub
|
---|
| 4 | ##
|
---|
| 5 | # $Id: ExportBIND.pm 850 2022-09-01 21:49:59Z kdeugau $
|
---|
| 6 | # Copyright 2022 Kris Deugau <kdeugau@deepnet.cx>
|
---|
| 7 | #
|
---|
| 8 | # This program is free software: you can redistribute it and/or modify
|
---|
| 9 | # it under the terms of the GNU General Public License as published by
|
---|
| 10 | # the Free Software Foundation, either version 3 of the License, or
|
---|
| 11 | # (at your option) any later version.
|
---|
| 12 | #
|
---|
| 13 | # This program is distributed in the hope that it will be useful,
|
---|
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | # GNU General Public License for more details.
|
---|
| 17 | #
|
---|
| 18 | # You should have received a copy of the GNU General Public License
|
---|
| 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | ##
|
---|
| 21 |
|
---|
| 22 | package DNSDB::ExportBIND;
|
---|
| 23 |
|
---|
| 24 | use strict;
|
---|
| 25 | use warnings;
|
---|
| 26 |
|
---|
| 27 | sub export {
|
---|
| 28 | # expected to be a DNSDB object
|
---|
| 29 | my $self = shift;
|
---|
| 30 | my $dbh = $self->{dbh};
|
---|
| 31 |
|
---|
[849] | 32 | # to be a hash of views/locations, containing lists of zones
|
---|
| 33 | my %viewzones;
|
---|
| 34 |
|
---|
[848] | 35 | # allow for future exports of subgroups of records
|
---|
[849] | 36 | my $viewlist = $self->getLocList(curgroup => 1);
|
---|
[847] | 37 |
|
---|
[850] | 38 | my $soasth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".
|
---|
| 39 | "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 ".
|
---|
| 41 | "FROM records WHERE rdns_id=? AND NOT type=6 ".
|
---|
| 42 | "ORDER BY masklen(inetlazy(val)) DESC, inetlazy(val)");
|
---|
| 43 |
|
---|
[849] | 44 | # Fetch active zone list
|
---|
| 45 | my $revsth = $self->{dbh}->prepare("SELECT rdns_id,revnet,status,changed,default_location FROM revzones WHERE status=1 ".
|
---|
| 46 | "ORDER BY masklen(revnet) DESC, rdns_id");
|
---|
| 47 | # 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=?");
|
---|
| 49 | $revsth->execute();
|
---|
| 50 | while (my ($revid,$revzone,$revstat,$changed,$defloc) = $revsth->fetchrow_array) {
|
---|
[850] | 51 | my $tmpzone = NetAddr::IP->new($revzone);
|
---|
| 52 | my $zfile = $tmpzone->network->addr."-".$tmpzone->masklen;
|
---|
| 53 | # my $cachefile = "$self->{exportcache}/$zfile";
|
---|
| 54 | # my $tmpcache = "$self->{exportcache}/tmp.$zfile.$$";
|
---|
| 55 | my $tmpcache = "tmp.$zfile.$$"; # safety net. don't overwrite a previous known-good file
|
---|
| 56 |
|
---|
| 57 | ##fixme: convert logical revzone into .arpa name? maybe take a slice of showrev_arpa?
|
---|
| 58 | ##fixme: need to bodge logical non-octet-boundary revzones into octet-boundary revzones
|
---|
| 59 | ##fixme: do we do cache files? views balloon the file count stupidly
|
---|
| 60 |
|
---|
| 61 |
|
---|
[849] | 62 | # fetch a list of views/locations present in the zone. we need to publish a file for each one.
|
---|
| 63 | # in the event that no locations are present (~~ $viewlist is empty), /%view collapses to nothing in the zone path
|
---|
| 64 | my (@loclist) = $self->{dbh}->selectrow_array("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
|
---|
[850] | 65 | push @loclist, $defloc unless grep /$defloc/, @loclist;
|
---|
[849] | 66 | my $zonepath = $self->{bind_export_zone_path};
|
---|
| 67 | my %zonefiles; # to be a list of file handles.
|
---|
| 68 | ##fixme: convert logical revzone into .arpa name
|
---|
| 69 | foreach my $loc (@loclist) {
|
---|
[850] | 70 | my $zfilepath = $zonepath;
|
---|
| 71 | $zonepath =~ s/\%view/$loc/;
|
---|
| 72 | $zonepath =~ s/\%zone/$revzone/;
|
---|
| 73 | # Just In Case(TM)
|
---|
| 74 | $zonepath =~ s,[^\w./-],_,g;
|
---|
| 75 | #open $zonefiles{$loc}, ">", $zfilepath;
|
---|
| 76 | print "open zonefile for '$loc', '$zonepath'\n";
|
---|
[849] | 77 | }
|
---|
| 78 |
|
---|
[850] | 79 | $soasth->execute($revid);
|
---|
| 80 | my (@zsoa) = $soasth->fetchrow_array();
|
---|
| 81 |
|
---|
[849] | 82 | } # revsth->fetch
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | # Write the view configuration last, because otherwise we have to be horribly inefficient
|
---|
| 86 | # at figuring out which zones are visible/present in which views
|
---|
[848] | 87 | if ($viewlist) {
|
---|
| 88 | foreach my $view (@{$viewlist}) {
|
---|
| 89 | #print Dumper($view);
|
---|
| 90 | # print BINDCONF "view $view->{location} {\n";
|
---|
| 91 | print "view $view->{location} {\n";
|
---|
| 92 | # could also use an acl { ... }; statement, then match-clients { aclname; };, but that gets hairy
|
---|
| 93 | # note that some semantics of data visibility need to be handled by the record export, since it's
|
---|
| 94 | # not 100% clear if the semantics of a tinydns view with an empty IP list (matches anyone) are the
|
---|
| 95 | # same as a BIND view with match-clients { any; };
|
---|
| 96 | if ($view->{iplist}) {
|
---|
| 97 | # print BINDCONF " match-clients { ".join("; ", $view->iplist)."; };\n";
|
---|
| 98 | print " match-clients { ".join("; ", split(/[\s,]+/, $view->{iplist}))."; };\n";
|
---|
| 99 | } else {
|
---|
[849] | 100 | # print BINDCONF " match-clients { any; };\n";
|
---|
| 101 | print " match-clients { any; };\n";
|
---|
[848] | 102 | }
|
---|
[849] | 103 | foreach my $zone (@{$viewzones{$view->{location}}}) {
|
---|
| 104 | ##fixme: notify settings, maybe per-zone?
|
---|
| 105 | print qq( zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n };\n);
|
---|
| 106 | }
|
---|
[848] | 107 | print "};\n\n";
|
---|
| 108 | } # foreach @$viewlist
|
---|
| 109 | } # if $viewlist
|
---|
| 110 |
|
---|
| 111 | } # export()
|
---|
| 112 |
|
---|
[847] | 113 | 1;
|
---|