source: trunk/DNSDB/ExportBIND.pm@ 851

Last change on this file since 851 was 851, checked in by Kris Deugau, 21 months ago

/trunk

BIND export, unwinding dev saves, 4 of many many

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