source: trunk/DNSDB/ExportBIND.pm@ 853

Last change on this file since 853 was 853, checked in by Kris Deugau, 20 months ago

/trunk

BIND export, unwinding dev saves, 5 of many many

  • Refinement of zone-file-per-location assembly
  • Property svn:keywords set to Date Rev Author Id
File size: 8.3 KB
Line 
1# dns/trunk/DNSDB/ExportBIND.pm
2# BIND data export/publication
3# Call through DNSDB.pm's export() sub
4##
5# $Id: ExportBIND.pm 853 2022-09-15 19:46:07Z 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
22package DNSDB::ExportBIND;
23
24use strict;
25use warnings;
26
27sub 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 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
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) {
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
62 eval {
63
64 # write fresh records if:
65 # - we are not using the cache
66 # - force_refresh is set
67 # - the zone has changed
68 # - the cache file does not exist
69 # - the cache file is empty
70 if (!$self->{usecache} || $self->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
71 if ($self->{usecache}) {
72 open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
73 $zonefilehandle = *ZONECACHE;
74 }
75
76 # fetch a list of views/locations present in the zone. we need to publish a file for each one.
77 # 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);
79 push @loclist, $defloc unless grep /$defloc/, @loclist;
80 my $zonepath = $self->{bind_export_zone_path};
81 my %zonefiles; # to be a list of file handles.
82##fixme: convert logical revzone into .arpa name
83 foreach my $loc (@loclist) {
84 my $zfilepath = $zonepath;
85 $zfilepath =~ s/\%view/$loc/;
86 $zfilepath =~ s/\%zone/$revzone/;
87 # Just In Case(TM)
88 $zfilepath =~ s,[^\w./-],_,g;
89 #open $zonefiles{$loc}, ">", $zfilepath;
90print "open zonefile for '$loc', '$zfilepath'\n";
91 }
92
93
94 # need to fetch this separately since the rest of the records all (should) have real IPs in val
95 $soasth->execute($revid);
96 my (@zsoa) = $soasth->fetchrow_array();
97 $self->_printrec_tiny($zonefilehandle, $zsoa[7], 'y',\%recflags,$revzone,
98 $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
99
100 $recsth->execute($revid);
101 my $fullzone = _ZONE($tmpzone, 'ZONE', 'r', '.').($tmpzone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
102
103 while (my ($host, $type, $val, $dist, $weight, $port, $ttl, $recid, $loc, $stamp, $expires, $stampactive)
104 = $recsth->fetchrow_array) {
105 next if $recflags{$recid};
106
107 # Check for out-of-zone data
108 if ($val =~ /\.arpa$/) {
109 # val is non-IP
110 if ($val !~ /$fullzone$/) {
111 warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
112 next;
113 }
114 } else {
115 my $ipval = new NetAddr::IP $val;
116 if (!$tmpzone->contains($ipval)) {
117 warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
118 next;
119 }
120 } # is $val a raw .arpa name?
121
122 # Spaces are evil.
123 $val =~ s/^\s+//;
124 $val =~ s/\s+$//;
125 if ($typemap{$type} ne 'TXT') {
126 # Leading or trailng spaces could be legit in TXT records.
127 $host =~ s/^\s+//;
128 $host =~ s/\s+$//;
129 }
130
131 $self->_printrec_tiny($zonefilehandle, $recid, 'y', \%recflags, $revzone,
132 $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
133
134 $recflags{$recid} = 1;
135
136 } # while ($recsth)
137
138 if ($self->{usecache}) {
139 close ZONECACHE; # force the file to be written
140 # catch obvious write errors that leave an empty temp file
141 if (-s $tmpcache) {
142 rename $tmpcache, $cachefile
143 or die "Error overwriting cache file $cachefile with temporary file: $!\n";
144 }
145 }
146
147 } # if $changed or cache filesize is 0
148
149 };
150 if ($@) {
151 die "error writing ".($self->{usecache} ? 'new data for ' : '')."$revzone: $@\n";
152 # error! something borked, and we should be able to fall back on the old cache file
153 # report the error, somehow.
154 } else {
155 # mark zone as unmodified. Only do this if no errors, that way
156 # export failures should recover a little more automatically.
157 $zonesth->execute($revid);
158 }
159
160# if ($self->{usecache}) {
161# # We've already made as sure as we can that a cached zone file is "good",
162# # although possibly stale/obsolete due to errors creating a new one.
163# eval {
164# open CACHE, "<$cachefile" or die $!;
165# print $datafile $_ or die "error copying cached $revzone to master file: $!" while <CACHE>;
166# close CACHE;
167# };
168# die $@ if $@;
169# }
170
171
172
173 $soasth->execute($revid);
174 my (@zsoa) = $soasth->fetchrow_array();
175
176 } # revsth->fetch
177
178
179 # Write the view configuration last, because otherwise we have to be horribly inefficient
180 # at figuring out which zones are visible/present in which views
181 if ($viewlist) {
182 foreach my $view (@{$viewlist}) {
183#print Dumper($view);
184# print BINDCONF "view $view->{location} {\n";
185 print "view $view->{location} {\n";
186 # could also use an acl { ... }; statement, then match-clients { aclname; };, but that gets hairy
187 # note that some semantics of data visibility need to be handled by the record export, since it's
188 # not 100% clear if the semantics of a tinydns view with an empty IP list (matches anyone) are the
189 # same as a BIND view with match-clients { any; };
190 if ($view->{iplist}) {
191# print BINDCONF " match-clients { ".join("; ", $view->iplist)."; };\n";
192 print " match-clients { ".join("; ", split(/[\s,]+/, $view->{iplist}))."; };\n";
193 } else {
194# print BINDCONF " match-clients { any; };\n";
195 print " match-clients { any; };\n";
196 }
197 foreach my $zone (@{$viewzones{$view->{location}}}) {
198##fixme: notify settings, maybe per-zone?
199 print qq( zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n };\n);
200 }
201 print "};\n\n";
202 } # foreach @$viewlist
203 } # if $viewlist
204
205} # export()
206
2071;
Note: See TracBrowser for help on using the repository browser.