1 | # dns/trunk/DNSDB/ExportBIND.pm
|
---|
2 | # BIND data export/publication
|
---|
3 | # Call through DNSDB.pm's export() sub
|
---|
4 | ##
|
---|
5 | # $Id: ExportBIND.pm 869 2022-09-28 19:29:29Z 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 $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 |
|
---|
114 | ##fixme: do we even need @loclist passed in?
|
---|
115 | publishrec_bind(\%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $revzone,
|
---|
116 | $zsoa[0], $zsoa[1], $zsoa[2], $zsoa[3], $zsoa[4], $zsoa[5], $zsoa[6], $loc, '');
|
---|
117 |
|
---|
118 | $recsth->execute($revid);
|
---|
119 | my $fullzone = _ZONE($tmpzone, 'ZONE', 'r', '.').($tmpzone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
|
---|
120 |
|
---|
121 | while (my ($host, $type, $val, $dist, $weight, $port, $ttl, $recid, $loc, $stamp, $expires, $stampactive)
|
---|
122 | = $recsth->fetchrow_array) {
|
---|
123 | next if $recflags{$recid};
|
---|
124 |
|
---|
125 | # Check for out-of-zone data
|
---|
126 | if ($val =~ /\.arpa$/) {
|
---|
127 | # val is non-IP
|
---|
128 | if ($val !~ /$fullzone$/) {
|
---|
129 | warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
|
---|
130 | next;
|
---|
131 | }
|
---|
132 | } else {
|
---|
133 | my $ipval = new NetAddr::IP $val;
|
---|
134 | if (!$tmpzone->contains($ipval)) {
|
---|
135 | warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $tmpzone)\n";
|
---|
136 | next;
|
---|
137 | }
|
---|
138 | } # is $val a raw .arpa name?
|
---|
139 |
|
---|
140 | # Spaces are evil.
|
---|
141 | $val =~ s/^\s+//;
|
---|
142 | $val =~ s/\s+$//;
|
---|
143 | if ($typemap{$type} ne 'TXT') {
|
---|
144 | # Leading or trailng spaces could be legit in TXT records.
|
---|
145 | $host =~ s/^\s+//;
|
---|
146 | $host =~ s/\s+$//;
|
---|
147 | }
|
---|
148 |
|
---|
149 | publishrec_bind(\%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
|
---|
150 | $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
|
---|
151 |
|
---|
152 | $recflags{$recid} = 1;
|
---|
153 |
|
---|
154 | } # while ($recsth)
|
---|
155 |
|
---|
156 | if ($dnsdb->{usecache}) {
|
---|
157 | close ZONECACHE; # force the file to be written
|
---|
158 | # catch obvious write errors that leave an empty temp file
|
---|
159 | if (-s $tmpcache) {
|
---|
160 | rename $tmpcache, $cachefile
|
---|
161 | or die "Error overwriting cache file $cachefile with temporary file: $!\n";
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | } # if $changed or cache filesize is 0
|
---|
166 |
|
---|
167 | };
|
---|
168 | if ($@) {
|
---|
169 | die "error writing ".($dnsdb->{usecache} ? 'new data for ' : '')."$revzone: $@\n";
|
---|
170 | # error! something borked, and we should be able to fall back on the old cache file
|
---|
171 | # report the error, somehow.
|
---|
172 | } else {
|
---|
173 | # mark zone as unmodified. Only do this if no errors, that way
|
---|
174 | # export failures should recover a little more automatically.
|
---|
175 | $zonesth->execute($revid);
|
---|
176 | }
|
---|
177 |
|
---|
178 | # if ($dnsdb->{usecache}) {
|
---|
179 | # # We've already made as sure as we can that a cached zone file is "good",
|
---|
180 | # # although possibly stale/obsolete due to errors creating a new one.
|
---|
181 | # eval {
|
---|
182 | # open CACHE, "<$cachefile" or die $!;
|
---|
183 | # print $datafile $_ or die "error copying cached $revzone to master file: $!" while <CACHE>;
|
---|
184 | # close CACHE;
|
---|
185 | # };
|
---|
186 | # die $@ if $@;
|
---|
187 | # }
|
---|
188 |
|
---|
189 |
|
---|
190 |
|
---|
191 | } # revsth->fetch
|
---|
192 |
|
---|
193 |
|
---|
194 | # Write the view configuration last, because otherwise we have to be horribly inefficient
|
---|
195 | # at figuring out which zones are visible/present in which views
|
---|
196 | if ($viewlist) {
|
---|
197 | my $tmpconf = "$dnsdb->{bind_zone_conf}.$$"; ##fixme: split filename for prefixing
|
---|
198 | open BINDCONF, ">", $tmpconf;
|
---|
199 |
|
---|
200 | foreach my $view (@{$viewlist}) {
|
---|
201 | #print Dumper($view);
|
---|
202 | print BINDCONF "view $view->{location} {\n";
|
---|
203 | # print "view $view->{location} {\n";
|
---|
204 | # could also use an acl { ... }; statement, then match-clients { aclname; };, but that gets hairy
|
---|
205 | # note that some semantics of data visibility need to be handled by the record export, since it's
|
---|
206 | # not 100% clear if the semantics of a tinydns view with an empty IP list (matches anyone) are the
|
---|
207 | # same as a BIND view with match-clients { any; };
|
---|
208 | if ($view->{iplist}) {
|
---|
209 | print BINDCONF " match-clients { ".join("; ", $view->iplist)."; };\n";
|
---|
210 | # print " match-clients { ".join("; ", split(/[\s,]+/, $view->{iplist}))."; };\n";
|
---|
211 | } else {
|
---|
212 | print BINDCONF " match-clients { any; };\n";
|
---|
213 | # print " match-clients { any; };\n";
|
---|
214 | }
|
---|
215 | foreach my $zone (@{$viewzones{$view->{location}}}) {
|
---|
216 | ##fixme: notify settings, maybe per-zone?
|
---|
217 | print qq( zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n };\n);
|
---|
218 | }
|
---|
219 | print "};\n\n";
|
---|
220 | } # foreach @$viewlist
|
---|
221 | rename $tmpconf, $dnsdb->{bind_zone_conf};
|
---|
222 | } # if $viewlist
|
---|
223 |
|
---|
224 | } # export()
|
---|
225 |
|
---|
226 |
|
---|
227 | # Print individual records in BIND format
|
---|
228 | sub publishrec_bind {
|
---|
229 | my $dnsdb = shift;
|
---|
230 |
|
---|
231 | # my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
|
---|
232 | my ($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
|
---|
233 | $loc, $stamp, $expires, $stampactive) = @_;
|
---|
234 |
|
---|
235 | # make sure "global" records get into all the right per-view zone files, without having to do this loop in each record-print location
|
---|
236 | ##fixme: maybe exclude the template types? those may be more expensive to export
|
---|
237 | ## *ponder* may be more efficient to loop in each record print due to substitution and manipulation from stored data to formal
|
---|
238 | ## record for .arpa zones for all records
|
---|
239 | if ($loc eq '') {
|
---|
240 | foreach my $subloc (@{$loclist}) {
|
---|
241 | publishrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
|
---|
242 | $subloc, $stamp, $expires, $stampactive);
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | # Just In Case something is lingering in the DB
|
---|
247 | $loc = '' if !$loc;
|
---|
248 |
|
---|
249 | ## And now to the records!
|
---|
250 |
|
---|
251 | if ($typemap{$type} eq 'SOA') {
|
---|
252 | # host contains pri-ns:responsible
|
---|
253 | # val is abused to contain refresh:retry:expire:minttl
|
---|
254 | # let's be explicit about abusing $host and $val
|
---|
255 | my ($email, $primary) = (split /:/, $host)[0,1];
|
---|
256 | my ($refresh, $retry, $expire, $min_ttl) = (split /:/, $val)[0,1,2,3];
|
---|
257 | my $serial = 0; # fail less horribly than leaving it empty?
|
---|
258 | # just snarfing the right SOA serial for the zone type
|
---|
259 | if ($revrec eq 'y') {
|
---|
260 | ($serial) = $dnsdb->{dbh}->selectrow_array("SELECT zserial FROM revzones WHERE revnet=?", undef, $zone);
|
---|
261 | } else {
|
---|
262 | ($serial) = $dnsdb->{dbh}->selectrow_array("SELECT zserial FROM domains WHERE domain=?", undef, $zone);
|
---|
263 | } # revrec <> 'y'
|
---|
264 | # suppress a "uninitialized value" warning. should be impossible but...
|
---|
265 | # abuse hours as the last digit pair of the serial for simplicity
|
---|
266 | ##fixme?: alternate SOA serial schemes?
|
---|
267 | $serial = strftime("%Y%m%d%H", localtime()) if !$serial;
|
---|
268 | $primary .= "." if $primary !~ /\.$/;
|
---|
269 | $email .= "." if $email !~ /\.$/;
|
---|
270 | # print *{$zonefiles->{$loc}} "Z$zone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n"
|
---|
271 | # or die $!;
|
---|
272 | # print *{$zonefiles->{$loc}} "$zone $ttl IN SOA $primary $email ( $serial $refresh $retry $expire $min_ttl )\n"
|
---|
273 | # or die "couldn't write $zone SOA: $!";
|
---|
274 | my $recdata = "$zone $ttl IN SOA $primary $email ( $serial $refresh $retry $expire $min_ttl )\n";
|
---|
275 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
276 | } # SOA
|
---|
277 |
|
---|
278 | elsif ($typemap{$type} eq 'A') {
|
---|
279 | # ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
|
---|
280 | # print $datafile "+$host:$val:$ttl:$stamp:$loc\n" or die $!;
|
---|
281 | # print {$zonefiles->{$loc}} "$host $ttl IN A $val\n" or die $!;
|
---|
282 | my $recdata = "$host $ttl IN A $val\n";
|
---|
283 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
284 | } # A
|
---|
285 |
|
---|
286 | elsif ($typemap{$type} eq 'NS') {
|
---|
287 | if ($revrec eq 'y') {
|
---|
288 | $val = NetAddr::IP->new($val);
|
---|
289 |
|
---|
290 | ##fixme: conversion for sub-/24 delegations in reverse zones?
|
---|
291 | # if (!$val->{isv6} && ($val->masklen > 24)) {
|
---|
292 | # }
|
---|
293 |
|
---|
294 | # print {$zonefiles->{$loc}} "$zone $ttl IN NS $host\n";
|
---|
295 | # print "$zone $ttl IN NS $host\n" or die $!;
|
---|
296 | my $recdata = "$zone $ttl IN NS $host\n";
|
---|
297 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
298 |
|
---|
299 | } else {
|
---|
300 | # print $datafile "\&$host"."::$val:$ttl:$stamp:$loc\n" or die $!;
|
---|
301 | }
|
---|
302 | } # NS
|
---|
303 |
|
---|
304 | elsif ($typemap{$type} eq 'AAAA') {
|
---|
305 | # ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
|
---|
306 | # print {$zonefiles->{$loc}} "$host $ttl IN AAAA $val\n" or die $!;
|
---|
307 | my $recdata = "$host $ttl IN AAAA $val\n";
|
---|
308 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
309 | } # AAAA
|
---|
310 |
|
---|
311 | elsif ($typemap{$type} eq 'TXT') {
|
---|
312 | # ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
|
---|
313 | # print {$zonefiles->{$loc}} "$host $ttl IN TXT \"$val\"\n" or die $!;
|
---|
314 | my $recdata = "$host $ttl IN TXT \"$val\"\n";
|
---|
315 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
316 | } # TXT
|
---|
317 |
|
---|
318 | elsif ($typemap{$type} eq 'CNAME') {
|
---|
319 | # ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
|
---|
320 | # print {$zonefiles->{$loc}} "$host $ttl IN CNAME $val\n" or die $!;
|
---|
321 | my $recdata = "$host $ttl IN CNAME $val\n";
|
---|
322 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
323 | } # CNAME
|
---|
324 |
|
---|
325 | elsif ($typemap{$type} eq 'SRV') {
|
---|
326 | # ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
|
---|
327 | # print {$zonefiles->{$loc}} "$host $ttl IN SRV $distance $weight $port $val\n" or die $!;
|
---|
328 | my $recdata = "$host $ttl IN SRV $distance $weight $port $val\n";
|
---|
329 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
330 | } # SRV
|
---|
331 |
|
---|
332 | elsif ($typemap{$type} eq 'RP') {
|
---|
333 | # ($host,$val) = __revswap($host,$val) if $revrec eq 'y';
|
---|
334 | # print {$zonefiles->{$loc}} "$host $ttl IN RP $val\n" or die $!;
|
---|
335 | my $recdata = "$host $ttl IN RP $val\n";
|
---|
336 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
337 | } # RP
|
---|
338 |
|
---|
339 |
|
---|
340 | elsif ($typemap{$type} eq 'PTR') {
|
---|
341 | $$recflags{$val}++;
|
---|
342 | if ($revrec eq 'y') {
|
---|
343 |
|
---|
344 | if ($val =~ /\.arpa$/) {
|
---|
345 | # someone put in the formal .arpa name. humor them.
|
---|
346 | # print {$zonefiles->{$loc}} "$val $ttl IN PTR $host\n" or die $!;
|
---|
347 | my $recdata = "$val $ttl IN PTR $host\n";
|
---|
348 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
349 | } else {
|
---|
350 | $zone = NetAddr::IP->new($zone);
|
---|
351 | if (!$zone->{isv6} && $zone->masklen > 24) {
|
---|
352 | # sub-octet v4 zone
|
---|
353 | ($val) = ($val =~ /\.(\d+)$/);
|
---|
354 | # print {$zonefiles->{$loc}} "$val.".DNSDB::_ZONE($zone, 'ZONE', 'r', '.').'.in-addr.arpa'.
|
---|
355 | # " $ttl IN PTR $host\n"
|
---|
356 | # or die $!;
|
---|
357 | my $recdata = "$val.".DNSDB::_ZONE($zone, 'ZONE', 'r', '.').".in-addr.arpa $ttl IN PTR $host\n";
|
---|
358 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
359 | } else {
|
---|
360 | # not going to care about strange results if $val is not an IP value and is resolveable in DNS
|
---|
361 | $val = NetAddr::IP->new($val);
|
---|
362 | # print {$zonefiles->{$loc}} DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa').
|
---|
363 | # " $ttl IN PTR $host\n"
|
---|
364 | # or die $!;
|
---|
365 | my $recdata = DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa').
|
---|
366 | " $ttl IN PTR $host\n";
|
---|
367 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
368 | }
|
---|
369 | } # non-".arpa" $val
|
---|
370 |
|
---|
371 | } else {
|
---|
372 | # PTRs in forward zones are less bizarre and insane than some other record types
|
---|
373 | # in reverse zones... OTOH we can't validate them any which way, so we cross our
|
---|
374 | # fingers and close our eyes and make it Someone Else's Problem.
|
---|
375 | # print {$zonefiles->{$loc}} "$host $ttl IN PTR $val\n" or die $!;
|
---|
376 | my $recdata = "$host $ttl IN PTR $val\n";
|
---|
377 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
378 | }
|
---|
379 | } # PTR
|
---|
380 |
|
---|
381 | elsif ($type == 65280) { # A+PTR
|
---|
382 | # Recurse to PTR or A as appropriate because BIND et al don't share
|
---|
383 | # the tinydns concept of merged forward/reverse records
|
---|
384 | $$recflags{$val}++;
|
---|
385 | if ($revrec eq 'y') {
|
---|
386 | publishrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 12, $val, $distance, $weight, $port, $ttl,
|
---|
387 | $loc, $stamp, $expires, $stampactive);
|
---|
388 | #print {$zonefiles->{$loc}} "=$host:$val:$ttl:$stamp:$loc\n" or die $!;
|
---|
389 | # publishrec_bind(\%zonefiles, $recid, 'y', \@loclist, $revzone,
|
---|
390 | # $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
|
---|
391 | # my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
|
---|
392 | # $loc, $stamp, $expires, $stampactive) = @_;
|
---|
393 | } else {
|
---|
394 | publishrec_bind($zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 1, $val, $distance, $weight, $port, $ttl,
|
---|
395 | $loc, $stamp, $expires, $stampactive);
|
---|
396 | }
|
---|
397 | } # A+PTR
|
---|
398 |
|
---|
399 | elsif ($type == 65282) { # PTR template
|
---|
400 | # only useful for v4 with standard DNS software, since this expands all
|
---|
401 | # IPs in $zone (or possibly $val?) with autogenerated records
|
---|
402 | $val = NetAddr::IP->new($val);
|
---|
403 | return if $val->{isv6};
|
---|
404 |
|
---|
405 | if ($val->masklen <= 16) {
|
---|
406 | foreach my $sub ($val->split(16)) {
|
---|
407 | __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
|
---|
408 | }
|
---|
409 | } else {
|
---|
410 | __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
|
---|
411 | }
|
---|
412 | } # PTR template
|
---|
413 |
|
---|
414 | elsif ($type == 65283) { # A+PTR template
|
---|
415 | $val = NetAddr::IP->new($val);
|
---|
416 | # Just In Case. An A+PTR should be impossible to add to a v6 revzone via API.
|
---|
417 | return if $val->{isv6};
|
---|
418 |
|
---|
419 | if ($val->masklen < 16) {
|
---|
420 | foreach my $sub ($val->split(16)) {
|
---|
421 | __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
|
---|
422 | }
|
---|
423 | } else {
|
---|
424 | __publish_template_bind($sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
|
---|
425 | }
|
---|
426 | } # A+PTR template
|
---|
427 |
|
---|
428 | elsif ($type == 65284) { # AAAA+PTR template
|
---|
429 | # Stub for completeness. Could be exported to DNS software that supports
|
---|
430 | # some degree of internal automagic in generic-record-creation
|
---|
431 | # (eg http://search.cpan.org/dist/AllKnowingDNS/ )
|
---|
432 | } # AAAA+PTR template
|
---|
433 |
|
---|
434 | } # publishrec_bind()
|
---|
435 |
|
---|
436 |
|
---|
437 | sub __publish_template_bind {
|
---|
438 | my $sub = shift;
|
---|
439 | my $recflags = shift;
|
---|
440 | my $hpat = shift;
|
---|
441 | my $zonefiles = shift;
|
---|
442 | my $loclist = shift;
|
---|
443 | my $ttl = shift;
|
---|
444 | my $stamp = shift;
|
---|
445 | my $loc = shift;
|
---|
446 | my $zone = new NetAddr::IP shift;
|
---|
447 | my $revrec = shift || 'y';
|
---|
448 | # my $ptrflag = shift || 0; ##fixme: default to PTR instead of A record for the BIND variant of this sub?
|
---|
449 |
|
---|
450 | # do this conversion once, not (number-of-ips-in-subnet) times
|
---|
451 | my $arpabase = DNSDB::_ZONE($zone, 'ZONE.in-addr.arpa', 'r', '.');
|
---|
452 |
|
---|
453 | my $iplist = $sub->splitref(32);
|
---|
454 | my $ipindex = -1;
|
---|
455 | foreach (@$iplist) {
|
---|
456 | my $ip = $_->addr;
|
---|
457 | $ipindex++;
|
---|
458 | # make as if we split the non-octet-aligned block into octet-aligned blocks as with SOA
|
---|
459 | my $lastoct = (split /\./, $ip)[3];
|
---|
460 |
|
---|
461 | # Allow smaller entries to override longer ones, eg, a specific PTR will
|
---|
462 | # always publish, overriding any template record containing that IP.
|
---|
463 | # %blank% also needs to be per-IP here to properly cascade overrides with
|
---|
464 | # multiple nested templates
|
---|
465 | next if $$recflags{$ip}; # && $self->{skip_bcast_255}
|
---|
466 | $$recflags{$ip}++;
|
---|
467 | next if $hpat eq '%blank%';
|
---|
468 |
|
---|
469 | my $rec = $hpat; # start fresh with the template for each IP
|
---|
470 | ##fixme: there really isn't a good way to handle sub-/24 zones here. This way at least
|
---|
471 | # seems less bad than some alternatives.
|
---|
472 | $dnsdb->_template4_expand(\$rec, $ip, \$sub, $ipindex);
|
---|
473 | # _template4_expand may blank $rec; if so, don't publish a record
|
---|
474 | next if !$rec;
|
---|
475 | ##fixme: trim merged record type voodoo. "if ($ptrflag) {} else {}" ?
|
---|
476 | # if ($ptrflag || $zone->masklen > 24) {
|
---|
477 | my $recdata;
|
---|
478 | if ($revrec eq 'y') {
|
---|
479 | # || $zone->masklen > 24) {
|
---|
480 | # print $fh "^$lastoct.$arpabase:$rec:$ttl:$stamp:$loc\n" or die $!;
|
---|
481 | ##fixme: use $ORIGIN instead? make the FQDN output switchable-optional?
|
---|
482 | # print $fh "$lastoct.$arpabase $ttl IN PTR $rec\n" or die $!;
|
---|
483 | # if ($revrec ne 'y') {
|
---|
484 | # print a separate A record. Arguably we could use an = record here instead.
|
---|
485 | # print $fh "+$rec:$ip:$ttl:$stamp:$loc\n" or die $!;
|
---|
486 | # print $fh "$rec $ttl IN A $ip\n" or die $!;
|
---|
487 | # }
|
---|
488 | $recdata = "$lastoct.$arpabase $ttl IN PTR $rec\n";
|
---|
489 | } else {
|
---|
490 | # A record, not merged
|
---|
491 | # print $fh "=$rec:$ip:$ttl:$stamp:$loc\n" or die $!;
|
---|
492 | # print $fh "$rec $ttl IN A $ip\n" or die $!;
|
---|
493 | $recdata = "$rec $ttl IN A $ip\n";
|
---|
494 | }
|
---|
495 | # and finally
|
---|
496 | recprint($zonefiles, $loclist, $loc, $recdata);
|
---|
497 | } # foreach (@iplist)
|
---|
498 | } # __publish_template_bind()
|
---|
499 |
|
---|
500 |
|
---|
501 | # actual record printing sub
|
---|
502 | # loop on the locations here so we don't end up with a huge pot of copypasta
|
---|
503 | sub recprint {
|
---|
504 | my ($zonefiles, $loclist, $loc, $recdata) = @_;
|
---|
505 | if ($loc eq '') {
|
---|
506 | # "common" record visible in all locations
|
---|
507 | foreach my $rloc (@{$loclist}) {
|
---|
508 | print {$zonefiles->{$rloc}} $recdata or die $!;
|
---|
509 | }
|
---|
510 | } else {
|
---|
511 | # record with specific location tagged
|
---|
512 | print {$zonefiles->{$loc}} $recdata or die $!;
|
---|
513 | }
|
---|
514 | }
|
---|
515 |
|
---|
516 | 1;
|
---|