source: trunk/DNSDB/ExportBIND.pm@ 855

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

/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
  • Property svn:keywords set to Date Rev Author Id
File size: 10.9 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 855 2022-09-15 20:33:50Z 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 $dnsdb = shift;
30
31 # to be a hash of views/locations, containing lists of zones
32 my %viewzones;
33
34 # allow for future exports of subgroups of records
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 ".
38 "FROM records WHERE rdns_id=? AND type=6");
39 my $recsth = $dnsdb->{dbh}->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location,extract(epoch from stamp),expires,stampactive ".
40 "FROM records WHERE rdns_id=? AND NOT type=6 ".
41 "ORDER BY masklen(inetlazy(val)) DESC, inetlazy(val)");
42
43 # Fetch active zone list
44 my $revsth = $dnsdb->{dbh}->prepare("SELECT rdns_id,revnet,status,changed,default_location FROM revzones WHERE status=1 ".
45 "ORDER BY masklen(revnet) DESC, rdns_id");
46 # Unflag changed zones, so we can maybe cache the export and not redo everything every time
47 my $zonesth = $dnsdb->{dbh}->prepare("UPDATE revzones SET changed='n' WHERE rdns_id=?");
48 $revsth->execute();
49 while (my ($revid,$revzone,$revstat,$changed,$defloc) = $revsth->fetchrow_array) {
50 my $tmpzone = NetAddr::IP->new($revzone);
51 my $zfile = $tmpzone->network->addr."-".$tmpzone->masklen;
52# my $cachefile = "$dnsdb->{exportcache}/$zfile";
53# my $tmpcache = "$dnsdb->{exportcache}/tmp.$zfile.$$";
54 my $tmpcache = "tmp.$zfile.$$"; # safety net. don't overwrite a previous known-good file
55
56##fixme: convert logical revzone into .arpa name? maybe take a slice of showrev_arpa?
57##fixme: need to bodge logical non-octet-boundary revzones into octet-boundary revzones
58##fixme: do we do cache files? views balloon the file count stupidly
59
60
61 eval {
62
63 # write fresh records if:
64 # - we are not using the cache
65 # - force_refresh is set
66 # - the zone has changed
67 # - the cache file does not exist
68 # - the cache file is empty
69 if (!$dnsdb->{usecache} || $dnsdb->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
70 if ($dnsdb->{usecache}) {
71 open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
72 $zonefilehandle = *ZONECACHE;
73 }
74
75 # fetch a list of views/locations present in the zone. we need to publish a file for each one.
76 # in the event that no locations are present (~~ $viewlist is empty), /%view collapses to nothing in the zone path
77 my (@loclist) = $dnsdb->{dbh}->selectrow_array("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
78 push @loclist, $defloc unless grep /$defloc/, @loclist;
79 my $zonepath = $dnsdb->{bind_export_zone_path};
80 my %zonefiles; # to be a list of file handles.
81##fixme: convert logical revzone into .arpa name
82 foreach my $loc (@loclist) {
83 my $zfilepath = $zonepath;
84 $zfilepath =~ s/\%view/$loc/;
85 $zfilepath =~ s/\%zone/$revzone/;
86 # Just In Case(TM)
87 $zfilepath =~ s,[^\w./-],_,g;
88 #open $zonefiles{$loc}, ">", $zfilepath;
89print "open zonefile for '$loc', '$zfilepath'\n";
90 }
91
92 # need to fetch this separately since the rest of the records all (should) have real IPs in val
93 $soasth->execute($revid);
94 my (@zsoa) = $soasth->fetchrow_array();
95 printrec_bind($zonefilehandle, $zsoa[7], 'y', \@loclist, $revzone,
96 $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
97
98 $recsth->execute($revid);
99 my $fullzone = _ZONE($tmpzone, 'ZONE', 'r', '.').($tmpzone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
100
101 while (my ($host, $type, $val, $dist, $weight, $port, $ttl, $recid, $loc, $stamp, $expires, $stampactive)
102 = $recsth->fetchrow_array) {
103 next if $recflags{$recid};
104
105 # Check for out-of-zone data
106 if ($val =~ /\.arpa$/) {
107 # val is non-IP
108 if ($val !~ /$fullzone$/) {
109 warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
110 next;
111 }
112 } else {
113 my $ipval = new NetAddr::IP $val;
114 if (!$tmpzone->contains($ipval)) {
115 warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
116 next;
117 }
118 } # is $val a raw .arpa name?
119
120 # Spaces are evil.
121 $val =~ s/^\s+//;
122 $val =~ s/\s+$//;
123 if ($typemap{$type} ne 'TXT') {
124 # Leading or trailng spaces could be legit in TXT records.
125 $host =~ s/^\s+//;
126 $host =~ s/\s+$//;
127 }
128
129 printrec_bind($zonefilehandle, $recid, 'y', \%recflags, $revzone,
130 $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
131
132 $recflags{$recid} = 1;
133
134 } # while ($recsth)
135
136 if ($dnsdb->{usecache}) {
137 close ZONECACHE; # force the file to be written
138 # catch obvious write errors that leave an empty temp file
139 if (-s $tmpcache) {
140 rename $tmpcache, $cachefile
141 or die "Error overwriting cache file $cachefile with temporary file: $!\n";
142 }
143 }
144
145 } # if $changed or cache filesize is 0
146
147 };
148 if ($@) {
149 die "error writing ".($dnsdb->{usecache} ? 'new data for ' : '')."$revzone: $@\n";
150 # error! something borked, and we should be able to fall back on the old cache file
151 # report the error, somehow.
152 } else {
153 # mark zone as unmodified. Only do this if no errors, that way
154 # export failures should recover a little more automatically.
155 $zonesth->execute($revid);
156 }
157
158# if ($dnsdb->{usecache}) {
159# # We've already made as sure as we can that a cached zone file is "good",
160# # although possibly stale/obsolete due to errors creating a new one.
161# eval {
162# open CACHE, "<$cachefile" or die $!;
163# print $datafile $_ or die "error copying cached $revzone to master file: $!" while <CACHE>;
164# close CACHE;
165# };
166# die $@ if $@;
167# }
168
169
170
171 } # revsth->fetch
172
173
174 # Write the view configuration last, because otherwise we have to be horribly inefficient
175 # at figuring out which zones are visible/present in which views
176 if ($viewlist) {
177 foreach my $view (@{$viewlist}) {
178#print Dumper($view);
179# print BINDCONF "view $view->{location} {\n";
180 print "view $view->{location} {\n";
181 # could also use an acl { ... }; statement, then match-clients { aclname; };, but that gets hairy
182 # note that some semantics of data visibility need to be handled by the record export, since it's
183 # not 100% clear if the semantics of a tinydns view with an empty IP list (matches anyone) are the
184 # same as a BIND view with match-clients { any; };
185 if ($view->{iplist}) {
186# print BINDCONF " match-clients { ".join("; ", $view->iplist)."; };\n";
187 print " match-clients { ".join("; ", split(/[\s,]+/, $view->{iplist}))."; };\n";
188 } else {
189# print BINDCONF " match-clients { any; };\n";
190 print " match-clients { any; };\n";
191 }
192 foreach my $zone (@{$viewzones{$view->{location}}}) {
193##fixme: notify settings, maybe per-zone?
194 print qq( zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n };\n);
195 }
196 print "};\n\n";
197 } # foreach @$viewlist
198 } # if $viewlist
199
200} # export()
201
202
203# Print individual records in BIND format
204sub printrec_bind {
205 my $dnsdb = shift;
206 my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
207 $loc, $stamp, $expires, $stampactive) = @_;
208
209 # Just In Case something is lingering in the DB
210 $loc = '' if !$loc;
211
212 ## And now to the records!
213
214 if ($typemap{$type} eq 'SOA') {
215 # host contains pri-ns:responsible
216 # val is abused to contain refresh:retry:expire:minttl
217 # let's be explicit about abusing $host and $val
218 my ($email, $primary) = (split /:/, $host)[0,1];
219 my ($refresh, $retry, $expire, $min_ttl) = (split /:/, $val)[0,1,2,3];
220 my $serial = 0; # fail less horribly than leaving it empty?
221 if ($revrec eq 'y') {
222##fixme: have to publish SOA records for each v4 /24 in sub-/16, and each /16 in sub-/8
223# what about v6?
224# -> only need SOA for local chunks offset from reverse delegation boundaries, so v6 is fine
225# anyone who says they need sub-nibble v6 delegations, at this time, needs their head examined.
226##fixme?: alternate SOA serial schemes?
227 ($serial) = $dnsdb->{dbh}->selectrow_array("SELECT zserial FROM revzones WHERE revnet=?", undef, $zone);
228 $zone = NetAddr::IP->new($zone);
229 # handle split-n-multiply SOA for off-octet (8 < mask < 16) or (16 < mask < 24) v4 zones
230 if (!$zone->{isv6} && ($zone->masklen < 24) && ($zone->masklen % 8 != 0)) {
231 foreach my $szone ($zone->split($zone->masklen + (8 - $zone->masklen % 8))) {
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"
235 or die $!;
236 }
237 return; # skips "default" bits just below
238 }
239 $zone = DNSDB::_ZONE($zone, 'ZONE', 'r', '.').($zone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
240 } else {
241 # just snarfing the right SOA serial for the zone type
242##fixme?: alternate SOA serial schemes?
243 ($serial) = $dnsdb->{dbh}->selectrow_array("SELECT zserial FROM domains WHERE domain=?", undef, $zone);
244 } # revrec <> 'y'
245 # suppress a "uninitialized value" warning. should be impossible but...
246 # abuse hours as the last digit pair of the serial for simplicity
247 $serial = strftime("%Y%m%d%H", localtime()) if !$serial;
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 $!;
252 } # SOA
253
254} # printrec_bind()
255
256
2571;
Note: See TracBrowser for help on using the repository browser.