source: trunk/DNSDB/ExportBIND.pm@ 868

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

/trunk

BIND export, unwinding dev saves, 20 of many many

  • Minor whitespace fiddling for coding style consistency
  • Rejig retrieval of locations/views for a zone
  • Include 'common' metaview in view list
  • Pass location list to printrec_bind(), and iterate over the locations found if the record has no set location
  • Property svn:keywords set to Date Rev Author Id
File size: 19.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 868 2022-09-22 21:36:43Z 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
50 my %recflags; # need this to be independent for forward vs reverse zones, as they're not merged
51
52 while (my ($revid,$revzone,$revstat,$changed,$defloc) = $revsth->fetchrow_array) {
53 my $cidr = NetAddr::IP->new($revzone);
54 my $zfile = $cidr->network->addr."-".$cidr->masklen;
55# my $cachefile = "$dnsdb->{exportcache}/$zfile";
56# my $tmpcache = "$dnsdb->{exportcache}/tmp.$zfile.$$";
57 my $tmpcache = "tmp.$zfile.$$"; # safety net. don't overwrite a previous known-good file
58
59##fixme: convert logical revzone into .arpa name? maybe take a slice of showrev_arpa?
60##fixme: need to bodge logical non-octet-boundary revzones into octet-boundary revzones
61##fixme: do we do cache files? views balloon the file count stupidly
62## foreach $octetzone $cidr->split(octet-boundary)
63## loclist = SELECT DISTINCT location FROM records WHERE rdns_id = $zid AND inetlazy(val) <<= $octetzone
64
65#printf "non-octet? %s, %i\n", $cidr->masklen, $cidr->masklen % 8;
66
67
68 eval {
69
70 my $arpazone = DNSDB::_ZONE($cidr, 'ZONE', 'r', '.').($cidr->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
71
72 # write fresh records if:
73 # - we are not using the cache
74 # - force_refresh is set
75 # - the zone has changed
76 # - the cache file does not exist
77 # - the cache file is empty
78 if (!$dnsdb->{usecache} || $dnsdb->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
79 if ($dnsdb->{usecache}) {
80 open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
81 $zonefilehandle = *ZONECACHE;
82 }
83
84 # fetch a list of views/locations present in the zone. we need to publish a file for each one.
85 # in the event that no locations are present (~~ $viewlist is empty), /%view collapses to nothing in the zone path
86# my (@loclist) = $dnsdb->{dbh}->selectrow_array("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
87 my $tmplocs = $dnsdb->{dbh}->selectall_arrayref("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);
88 my @loclist;
89 foreach my $tloc (@{$tmplocs}) {
90 push @loclist, ($tloc->[0] eq '' ? 'common' : $tloc->[0]);
91 }
92
93 push @loclist, $defloc unless grep /$defloc/, @loclist;
94 push @loclist, 'common' unless grep /^common$/, @loclist;
95 my $zonepath = $dnsdb->{bind_export_reverse_zone_path};
96 my %zonefiles; # to be a hash of file handles.
97##fixme: need to open separate zone files for aggregated metazones eg /22 or /14
98 foreach my $loc (@loclist) {
99 my $zfilepath = $zonepath;
100 $zfilepath =~ s/\%view/$loc/;
101 $zfilepath =~ s/\%zone/$revzone/;
102 $zfilepath =~ s/\%arpazone/$arpazone/;
103 # Just In Case(TM)
104 $zfilepath =~ s,[^\w./-],_,g;
105 open $zonefiles{$loc}, ">", $zfilepath;
106 printf {$zonefiles{$loc}} "; %s in view %s exported %s\n", $arpazone, $loc, scalar(localtime)
107 or die "Error writing SOA [$zone, '$loc']: $!\n";;
108 }
109
110 # need to fetch this separately since the rest of the records all (should) have real IPs in val
111 $soasth->execute($revid);
112 my (@zsoa) = $soasth->fetchrow_array();
113##fixme: do we even need @loclist passed in?
114 printrec_bind(\%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $revzone,
115 $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
116
117 $recsth->execute($revid);
118 my $fullzone = _ZONE($tmpzone, 'ZONE', 'r', '.').($tmpzone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
119
120 while (my ($host, $type, $val, $dist, $weight, $port, $ttl, $recid, $loc, $stamp, $expires, $stampactive)
121 = $recsth->fetchrow_array) {
122 next if $recflags{$recid};
123
124 # Check for out-of-zone data
125 if ($val =~ /\.arpa$/) {
126 # val is non-IP
127 if ($val !~ /$fullzone$/) {
128 warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
129 next;
130 }
131 } else {
132 my $ipval = new NetAddr::IP $val;
133 if (!$tmpzone->contains($ipval)) {
134 warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
135 next;
136 }
137 } # is $val a raw .arpa name?
138
139 # Spaces are evil.
140 $val =~ s/^\s+//;
141 $val =~ s/\s+$//;
142 if ($typemap{$type} ne 'TXT') {
143 # Leading or trailng spaces could be legit in TXT records.
144 $host =~ s/^\s+//;
145 $host =~ s/\s+$//;
146 }
147
148 printrec_bind(\%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
149 $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
150
151 $recflags{$recid} = 1;
152
153 } # while ($recsth)
154
155 if ($dnsdb->{usecache}) {
156 close ZONECACHE; # force the file to be written
157 # catch obvious write errors that leave an empty temp file
158 if (-s $tmpcache) {
159 rename $tmpcache, $cachefile
160 or die "Error overwriting cache file $cachefile with temporary file: $!\n";
161 }
162 }
163
164 } # if $changed or cache filesize is 0
165
166 };
167 if ($@) {
168 die "error writing ".($dnsdb->{usecache} ? 'new data for ' : '')."$revzone: $@\n";
169 # error! something borked, and we should be able to fall back on the old cache file
170 # report the error, somehow.
171 } else {
172 # mark zone as unmodified. Only do this if no errors, that way
173 # export failures should recover a little more automatically.
174 $zonesth->execute($revid);
175 }
176
177# if ($dnsdb->{usecache}) {
178# # We've already made as sure as we can that a cached zone file is "good",
179# # although possibly stale/obsolete due to errors creating a new one.
180# eval {
181# open CACHE, "<$cachefile" or die $!;
182# print $datafile $_ or die "error copying cached $revzone to master file: $!" while <CACHE>;
183# close CACHE;
184# };
185# die $@ if $@;
186# }
187
188
189
190 } # revsth->fetch
191
192
193 # Write the view configuration last, because otherwise we have to be horribly inefficient
194 # at figuring out which zones are visible/present in which views
195 if ($viewlist) {
196 my $tmpconf = "$dnsdb->{bind_zone_conf}.$$"; ##fixme: split filename for prefixing
197 open BINDCONF, ">", $tmpconf;
198
199 foreach my $view (@{$viewlist}) {
200#print Dumper($view);
201 print BINDCONF "view $view->{location} {\n";
202# print "view $view->{location} {\n";
203 # could also use an acl { ... }; statement, then match-clients { aclname; };, but that gets hairy
204 # note that some semantics of data visibility need to be handled by the record export, since it's
205 # not 100% clear if the semantics of a tinydns view with an empty IP list (matches anyone) are the
206 # same as a BIND view with match-clients { any; };
207 if ($view->{iplist}) {
208 print BINDCONF " match-clients { ".join("; ", $view->iplist)."; };\n";
209# print " match-clients { ".join("; ", split(/[\s,]+/, $view->{iplist}))."; };\n";
210 } else {
211 print BINDCONF " match-clients { any; };\n";
212# print " match-clients { any; };\n";
213 }
214 foreach my $zone (@{$viewzones{$view->{location}}}) {
215##fixme: notify settings, maybe per-zone?
216 print qq( zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n };\n);
217 }
218 print "};\n\n";
219 } # foreach @$viewlist
220 rename $tmpconf, $dnsdb->{bind_zone_conf};
221 } # if $viewlist
222
223} # export()
224
225
226# Print individual records in BIND format
227sub printrec_bind {
228 my $dnsdb = shift;
229
230# my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
231 my ($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
232 $loc, $stamp, $expires, $stampactive) = @_;
233
234# make sure "global" records get into all the right per-view zone files, without having to do this loop in each record-print location
235##fixme: maybe exclude the template types? those may be more expensive to export
236## *ponder* may be more efficient to loop in each record print due to substitution and manipulation from stored data to formal
237## record for .arpa zones for all records
238 if ($loc eq '') {
239 foreach my $subloc (@{$loclist}) {
240 printrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
241 $subloc, $stamp, $expires, $stampactive);
242 }
243 }
244
245 # Just In Case something is lingering in the DB
246 $loc = '' if !$loc;
247
248 ## And now to the records!
249
250 if ($typemap{$type} eq 'SOA') {
251 # host contains pri-ns:responsible
252 # val is abused to contain refresh:retry:expire:minttl
253 # let's be explicit about abusing $host and $val
254 my ($email, $primary) = (split /:/, $host)[0,1];
255 my ($refresh, $retry, $expire, $min_ttl) = (split /:/, $val)[0,1,2,3];
256 my $serial = 0; # fail less horribly than leaving it empty?
257 # just snarfing the right SOA serial for the zone type
258 if ($revrec eq 'y') {
259 ($serial) = $dnsdb->{dbh}->selectrow_array("SELECT zserial FROM revzones WHERE revnet=?", undef, $zone);
260 } else {
261 ($serial) = $dnsdb->{dbh}->selectrow_array("SELECT zserial FROM domains WHERE domain=?", undef, $zone);
262 } # revrec <> 'y'
263 # suppress a "uninitialized value" warning. should be impossible but...
264 # abuse hours as the last digit pair of the serial for simplicity
265##fixme?: alternate SOA serial schemes?
266 $serial = strftime("%Y%m%d%H", localtime()) if !$serial;
267 $primary .= "." if $primary !~ /\.$/;
268 $email .= "." if $email !~ /\.$/;
269# print *{$zonefiles->{$loc}} "Z$zone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n"
270# or die $!;
271 print *{$zonefiles->{$loc}} "$zone $ttl IN SOA $primary $email ( $serial $refresh $retry $expire $min_ttl )\n"
272 or die "couldn't write $zone SOA: $!";
273 } # SOA
274
275 elsif ($typemap{$type} eq 'A') {
276# ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
277# print $datafile "+$host:$val:$ttl:$stamp:$loc\n" or die $!;
278 print {$zonefiles->{$loc}} "$host $ttl IN A $val\n" or die $!;
279 } # A
280
281 elsif ($typemap{$type} eq 'NS') {
282 if ($revrec eq 'y') {
283 $val = NetAddr::IP->new($val);
284
285##fixme: conversion for sub-/24 delegations in reverse zones?
286# if (!$val->{isv6} && ($val->masklen > 24)) {
287# }
288
289# print {$zonefiles->{$loc}} "$zone $ttl IN NS $host\n";
290 print "$zone $ttl IN NS $host\n" or die $!;
291
292 } else {
293# print $datafile "\&$host"."::$val:$ttl:$stamp:$loc\n" or die $!;
294 }
295 } # NS
296
297 elsif ($typemap{$type} eq 'AAAA') {
298# ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
299 print {$zonefiles->{$loc}} "$host $ttl IN AAAA $val\n" or die $!;
300 } # AAAA
301
302 elsif ($typemap{$type} eq 'TXT') {
303# ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
304 print {$zonefiles->{$loc}} "$host $ttl IN TXT \"$val\"\n" or die $!;
305 } # TXT
306
307 elsif ($typemap{$type} eq 'CNAME') {
308# ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
309 print {$zonefiles->{$loc}} "$host $ttl IN CNAME $val\n" or die $!;
310 } # CNAME
311
312 elsif ($typemap{$type} eq 'SRV') {
313# ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
314 print {$zonefiles->{$loc}} "$host $ttl IN SRV $distance $weight $port $val\n" or die $!;
315 } # SRV
316
317 elsif ($typemap{$type} eq 'RP') {
318# ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
319 print {$zonefiles->{$loc}} "$host $ttl IN RP $val\n" or die $!;
320 } # RP
321
322
323 elsif ($typemap{$type} eq 'PTR') {
324 $$recflags{$val}++;
325 if ($revrec eq 'y') {
326
327 if ($val =~ /\.arpa$/) {
328 # someone put in the formal .arpa name. humor them.
329 print {$zonefiles->{$loc}} "$val $ttl IN PTR $host\n" or die $!;
330 } else {
331 $zone = NetAddr::IP->new($zone);
332 if (!$zone->{isv6} && $zone->masklen > 24) {
333 # sub-octet v4 zone
334 ($val) = ($val =~ /\.(\d+)$/);
335 print {$zonefiles->{$loc}} "$val.".DNSDB::_ZONE($zone, 'ZONE', 'r', '.').'.in-addr.arpa'.
336 " $ttl IN PTR $host\n"
337 or die $!;
338 } else {
339 # not going to care about strange results if $val is not an IP value and is resolveable in DNS
340 $val = NetAddr::IP->new($val);
341 print {$zonefiles->{$loc}} DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa').
342 " $ttl IN PTR $host\n"
343 or die $!;
344
345 }
346 } # non-".arpa" $val
347
348 } else {
349 # PTRs in forward zones are less bizarre and insane than some other record types
350 # in reverse zones... OTOH we can't validate them any which way, so we cross our
351 # fingers and close our eyes and make it Someone Else's Problem.
352 print {$zonefiles->{$loc}} "^$host:$val:$ttl:$stamp:$loc\n" or die $!;
353 }
354 } # PTR
355
356 elsif ($type == 65280) { # A+PTR
357 # Recurse to PTR or A as appropriate because BIND et al don't share
358 # the tinydns concept of merged forward/reverse records
359 $$recflags{$val}++;
360 if ($revrec eq 'y') {
361 printrec_bind($zonefiles, $recid, $revrec, $recflags, $zone, $host, 12, $val, $distance, $weight, $port, $ttl,
362 $loc, $stamp, $expires, $stampactive);
363#print {$zonefiles->{$loc}} "=$host:$val:$ttl:$stamp:$loc\n" or die $!;
364# printrec_bind(\%zonefiles, $recid, 'y', \@loclist, $revzone,
365# $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
366# my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
367# $loc, $stamp, $expires, $stampactive) = @_;
368 } else {
369 printrec_bind($zonefiles, $recid, $revrec, $recflags, $zone, $host, 1, $val, $distance, $weight, $port, $ttl,
370 $loc, $stamp, $expires, $stampactive);
371 }
372 } # A+PTR
373
374 elsif ($type == 65282) { # PTR template
375 # only useful for v4 with standard DNS software, since this expands all
376 # IPs in $zone (or possibly $val?) with autogenerated records
377 $val = NetAddr::IP->new($val);
378 return if $val->{isv6};
379
380 if ($val->masklen <= 16) {
381 foreach my $sub ($val->split(16)) {
382 __publish_template_bind($sub, $recflags, $host, $zonefiles->{$loc}, $ttl, $stamp, $loc, $zone, 1);
383 }
384 } else {
385 __publish_template_bind($val, $recflags, $host, $zonefiles->{$loc}, $ttl, $stamp, $loc, $zone, 1);
386 }
387 } # PTR template
388
389 elsif ($type == 65283) { # A+PTR template
390 $val = NetAddr::IP->new($val);
391 # Just In Case. An A+PTR should be impossible to add to a v6 revzone via API.
392 return if $val->{isv6};
393
394 if ($val->masklen < 16) {
395 foreach my $sub ($val->split(16)) {
396 __publish_template_bind($sub, $recflags, $host, $zonefiles->{$loc}, $ttl, $stamp, $loc, $zone, 'y');
397 }
398 } else {
399 __publish_template_bind($val, $recflags, $host, $zonefiles->{$loc}, $ttl, $stamp, $loc, $zone, 'y');
400 }
401 } # A+PTR template
402
403 elsif ($type == 65284) { # AAAA+PTR template
404 # Stub for completeness. Could be exported to DNS software that supports
405 # some degree of internal automagic in generic-record-creation
406 # (eg http://search.cpan.org/dist/AllKnowingDNS/ )
407 } # AAAA+PTR template
408
409} # printrec_bind()
410
411
412sub __publish_template_bind {
413 my $sub = shift;
414 my $recflags = shift;
415 my $hpat = shift;
416 my $fh = shift;
417 my $ttl = shift;
418 my $stamp = shift;
419 my $loc = shift;
420 my $zone = new NetAddr::IP shift;
421 my $revrec = shift || 'y';
422# my $ptrflag = shift || 0; ##fixme: default to PTR instead of A record for the BIND variant of this sub?
423
424 # do this conversion once, not (number-of-ips-in-subnet) times
425 my $arpabase = DNSDB::_ZONE($zone, 'ZONE.in-addr.arpa', 'r', '.');
426
427 my $iplist = $sub->splitref(32);
428 my $ipindex = -1;
429 foreach (@$iplist) {
430 my $ip = $_->addr;
431 $ipindex++;
432 # make as if we split the non-octet-aligned block into octet-aligned blocks as with SOA
433 my $lastoct = (split /\./, $ip)[3];
434
435 # Allow smaller entries to override longer ones, eg, a specific PTR will
436 # always publish, overriding any template record containing that IP.
437 # %blank% also needs to be per-IP here to properly cascade overrides with
438 # multiple nested templates
439 next if $$recflags{$ip}; # && $self->{skip_bcast_255}
440 $$recflags{$ip}++;
441 next if $hpat eq '%blank%';
442
443 my $rec = $hpat; # start fresh with the template for each IP
444##fixme: there really isn't a good way to handle sub-/24 zones here. This way at least
445# seems less bad than some alternatives.
446 $dnsdb->_template4_expand(\$rec, $ip, \$sub, $ipindex);
447 # _template4_expand may blank $rec; if so, don't publish a record
448 next if !$rec;
449##fixme: trim merged record type voodoo. "if ($ptrflag) {} else {}" ?
450# if ($ptrflag || $zone->masklen > 24) {
451 if ($revrec eq 'y' || $zone->masklen > 24) {
452# print $fh "^$lastoct.$arpabase:$rec:$ttl:$stamp:$loc\n" or die $!;
453##fixme: use $ORIGIN instead? make the FQDN output switchable-optional?
454 print $fh "$lastoct.$arpabase $ttl IN PTR $rec\n" or die $!;
455# if (!$ptrflag) {
456 if ($revrec ne 'y') {
457 # print a separate A record. Arguably we could use an = record here instead.
458# print $fh "+$rec:$ip:$ttl:$stamp:$loc\n" or die $!;
459 print $fh "$rec $ttl IN A $ip\n" or die $!;
460 }
461 } else {
462 # A record, not merged
463# print $fh "=$rec:$ip:$ttl:$stamp:$loc\n" or die $!;
464 print $fh "$rec $ttl IN A $ip\n" or die $!;
465 }
466 }
467} # __publish_template_bind()
468
4691;
Note: See TracBrowser for help on using the repository browser.