1 | # dns/trunk/DNSDB/ExportBIND.pm
|
---|
2 | # BIND data export/publication
|
---|
3 | # Call through DNSDB.pm's export() sub
|
---|
4 | ##
|
---|
5 | # $Id: ExportBIND.pm 849 2022-09-01 17:17:46Z 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 |
|
---|
32 | # to be a hash of views/locations, containing lists of zones
|
---|
33 | my %viewzones;
|
---|
34 |
|
---|
35 | # allow for future exports of subgroups of records
|
---|
36 | my $viewlist = $self->getLocList(curgroup => 1);
|
---|
37 |
|
---|
38 | # Fetch active zone list
|
---|
39 | my $revsth = $self->{dbh}->prepare("SELECT rdns_id,revnet,status,changed,default_location FROM revzones WHERE status=1 ".
|
---|
40 | "ORDER BY masklen(revnet) DESC, rdns_id");
|
---|
41 | # Unflag changed zones, so we can maybe cache the export and not redo everything every time
|
---|
42 | my $zonesth = $self->{dbh}->prepare("UPDATE revzones SET changed='n' WHERE rdns_id=?");
|
---|
43 | $revsth->execute();
|
---|
44 | while (my ($revid,$revzone,$revstat,$changed,$defloc) = $revsth->fetchrow_array) {
|
---|
45 | # fetch a list of views/locations present in the zone. we need to publish a file for each one.
|
---|
46 | # in the event that no locations are present (~~ $viewlist is empty), /%view collapses to nothing in the zone path
|
---|
47 | my (@loclist) = $self->{dbh}->selectrow_array("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
|
---|
48 | push @loclist, $defloc;
|
---|
49 | my $zonepath = $self->{bind_export_zone_path};
|
---|
50 | my %zonefiles; # to be a list of file handles.
|
---|
51 | ##fixme: convert logical revzone into .arpa name
|
---|
52 | foreach my $loc (@loclist) {
|
---|
53 | local $zonepath =~ s/\%view/$loc/;
|
---|
54 | print "open $zonefiles{$loc}, $zonepath\n";
|
---|
55 | }
|
---|
56 |
|
---|
57 | } # revsth->fetch
|
---|
58 |
|
---|
59 |
|
---|
60 | # Write the view configuration last, because otherwise we have to be horribly inefficient
|
---|
61 | # at figuring out which zones are visible/present in which views
|
---|
62 | if ($viewlist) {
|
---|
63 | foreach my $view (@{$viewlist}) {
|
---|
64 | #print Dumper($view);
|
---|
65 | # print BINDCONF "view $view->{location} {\n";
|
---|
66 | print "view $view->{location} {\n";
|
---|
67 | # could also use an acl { ... }; statement, then match-clients { aclname; };, but that gets hairy
|
---|
68 | # note that some semantics of data visibility need to be handled by the record export, since it's
|
---|
69 | # not 100% clear if the semantics of a tinydns view with an empty IP list (matches anyone) are the
|
---|
70 | # same as a BIND view with match-clients { any; };
|
---|
71 | if ($view->{iplist}) {
|
---|
72 | # print BINDCONF " match-clients { ".join("; ", $view->iplist)."; };\n";
|
---|
73 | print " match-clients { ".join("; ", split(/[\s,]+/, $view->{iplist}))."; };\n";
|
---|
74 | } else {
|
---|
75 | # print BINDCONF " match-clients { any; };\n";
|
---|
76 | print " match-clients { any; };\n";
|
---|
77 | }
|
---|
78 | foreach my $zone (@{$viewzones{$view->{location}}}) {
|
---|
79 | ##fixme: notify settings, maybe per-zone?
|
---|
80 | print qq( zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n };\n);
|
---|
81 | }
|
---|
82 | print "};\n\n";
|
---|
83 | } # foreach @$viewlist
|
---|
84 | } # if $viewlist
|
---|
85 |
|
---|
86 | } # export()
|
---|
87 |
|
---|
88 | 1;
|
---|