1 | #!/usr/bin/perl
|
---|
2 | # dnsadmin shell-based import tool for tinydns flatfiles
|
---|
3 | ##
|
---|
4 | # $Id: tiny-import.pl 522 2013-06-12 21:03:30Z kdeugau $
|
---|
5 | # Copyright 2012,2013 Kris Deugau <kdeugau@deepnet.cx>
|
---|
6 | #
|
---|
7 | # This program is free software: you can redistribute it and/or modify
|
---|
8 | # it under the terms of the GNU General Public License as published by
|
---|
9 | # the Free Software Foundation, either version 3 of the License, or
|
---|
10 | # (at your option) any later version.
|
---|
11 | #
|
---|
12 | # This program is distributed in the hope that it will be useful,
|
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | # GNU General Public License for more details.
|
---|
16 | #
|
---|
17 | # You should have received a copy of the GNU General Public License
|
---|
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | ##
|
---|
20 |
|
---|
21 | # WARNING: This is NOT a heavy-duty validator; it is assumed that the data
|
---|
22 | # being imported is more or less sane. Only minor structural validation will
|
---|
23 | # be done to weed out the most broken records.
|
---|
24 |
|
---|
25 | use strict;
|
---|
26 | use warnings;
|
---|
27 |
|
---|
28 | use lib '.';
|
---|
29 | use DNSDB;
|
---|
30 |
|
---|
31 | my $dnsdb = new DNSDB;
|
---|
32 |
|
---|
33 | usage() if !@ARGV;
|
---|
34 |
|
---|
35 | my %importcfg = (
|
---|
36 | rw => 0,
|
---|
37 | conv => 0,
|
---|
38 | trial => 0,
|
---|
39 | legacy => 0,
|
---|
40 | );
|
---|
41 | # Handle some command-line arguments
|
---|
42 | while ($ARGV[0] =~ /^-/) {
|
---|
43 | my $arg = shift @ARGV;
|
---|
44 | usage() if $arg !~ /^-[rclt]+$/;
|
---|
45 | # -r rewrite imported files to comment imported records
|
---|
46 | # -c coerce/downconvert A+PTR = records to PTR
|
---|
47 | # -l swallow A+PTR as-is
|
---|
48 | # -t trial mode; don't commit to DB or actually rewrite flatfile (disables -r)
|
---|
49 | $arg =~ s/^-//;
|
---|
50 | my @tmp = split //, $arg;
|
---|
51 | foreach (@tmp) {
|
---|
52 | $importcfg{rw} = 1 if $_ eq 'r';
|
---|
53 | $importcfg{conv} = 1 if $_ eq 'c';
|
---|
54 | $importcfg{legacy} = 1 if $_ eq 'l';
|
---|
55 | $importcfg{trial} = 1 if $_ eq 't';
|
---|
56 | }
|
---|
57 | }
|
---|
58 | $importcfg{rw} = 0 if $importcfg{trial};
|
---|
59 |
|
---|
60 | sub usage {
|
---|
61 | die q(usage: tiny-import.pl [-r] [-c] datafile1 datafile2 ... datafileN ...
|
---|
62 | -r Rewrite all specified data files with a warning header indicating the
|
---|
63 | records are now managed by web, and commenting out all imported records.
|
---|
64 | The directory containing any given datafile must be writable.
|
---|
65 | -c Convert any A+PTR (=) record to a bare PTR if the forward domain is
|
---|
66 | not present in the database. Note this does NOT look forward through
|
---|
67 | a single file, nor across multiple files handled in the same run.
|
---|
68 | Multiple passes may be necessary if SOA and = records are heavily
|
---|
69 | intermixed and not clustered together.
|
---|
70 | -l (for "legacy") Force import of A+PTR records as-is. Mutually exclusive
|
---|
71 | with -c. -l takes precedence as -c is lossy.
|
---|
72 | -t Trial run mode; spits out records that would be left unimported.
|
---|
73 | Disables -r if set.
|
---|
74 |
|
---|
75 | -r and -c may be combined (-rc)
|
---|
76 |
|
---|
77 | datafileN is any tinydns record data file.
|
---|
78 | );
|
---|
79 | }
|
---|
80 |
|
---|
81 | my $code;
|
---|
82 | my $dbh = $dnsdb->{dbh};
|
---|
83 |
|
---|
84 | $dbh->{AutoCommit} = 0;
|
---|
85 | $dbh->{RaiseError} = 1;
|
---|
86 |
|
---|
87 | my %cnt;
|
---|
88 | my @deferred;
|
---|
89 | my $converted = 0;
|
---|
90 | my $errstr = '';
|
---|
91 |
|
---|
92 | foreach my $file (@ARGV) {
|
---|
93 | eval {
|
---|
94 | import(file => $file);
|
---|
95 | # import(file => $file, nosoa => 1);
|
---|
96 | $dbh->rollback if $importcfg{trial};
|
---|
97 | $dbh->commit unless $importcfg{trial};
|
---|
98 | };
|
---|
99 | if ($@) {
|
---|
100 | print "Failure trying to import $file: $@\n $errstr\n";
|
---|
101 | unlink ".$file.$$" if $importcfg{rw}; # cleanup
|
---|
102 | $dbh->rollback;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | # print summary count of record types encountered
|
---|
107 | foreach (keys %cnt) {
|
---|
108 | print " $_ $cnt{$_}\n";
|
---|
109 | }
|
---|
110 |
|
---|
111 | exit 0;
|
---|
112 |
|
---|
113 | sub import {
|
---|
114 | our %args = @_;
|
---|
115 | my $flatfile = $args{file};
|
---|
116 | my @fpath = split '/', $flatfile;
|
---|
117 | $fpath[$#fpath] = ".$fpath[$#fpath]";
|
---|
118 | my $rwfile = join('/', @fpath);#.".$$";
|
---|
119 |
|
---|
120 | open FLAT, "<$flatfile";
|
---|
121 |
|
---|
122 | if ($importcfg{rw}) {
|
---|
123 | open RWFLAT, ">$rwfile" or die "Couldn't open tempfile $rwfile for rewriting: $!\n";
|
---|
124 | print RWFLAT "# WARNING: Records in this file have been imported to the web UI.\n#\n";
|
---|
125 | }
|
---|
126 |
|
---|
127 | our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl,location) ".
|
---|
128 | " VALUES (?,?,?,?,?,?,?,?,?,?)");
|
---|
129 |
|
---|
130 | my %deleg;
|
---|
131 |
|
---|
132 | my $ok = 0;
|
---|
133 | while (<FLAT>) {
|
---|
134 | if (/^#/ || /^\s*$/) {
|
---|
135 | print RWFLAT "#$_" if $importcfg{rw};
|
---|
136 | next;
|
---|
137 | }
|
---|
138 | chomp;
|
---|
139 | s/\s*$//;
|
---|
140 | my $recstat = recslurp($_);
|
---|
141 | $ok++ if $recstat;
|
---|
142 | if ($importcfg{rw}) {
|
---|
143 | if ($recstat) {
|
---|
144 | print RWFLAT "#$_\n";
|
---|
145 | } else {
|
---|
146 | print RWFLAT "$_\n";
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | # Move the rewritten flatfile in place of the original, so that any
|
---|
152 | # external export processing will pick up any remaining records.
|
---|
153 | if ($importcfg{rw}) {
|
---|
154 | close RWFLAT;
|
---|
155 | rename "$rwfile", $flatfile;
|
---|
156 | }
|
---|
157 |
|
---|
158 | # Show the failed records
|
---|
159 | foreach (@deferred) {
|
---|
160 | print "failed to import $_\n";
|
---|
161 | }
|
---|
162 |
|
---|
163 | ##fixme: hmm. can't write the record back to the flatfile in the
|
---|
164 | # main while above, then come down here and import it anyway, can we?
|
---|
165 | # # Try the deferred records again, once.
|
---|
166 | # foreach (@deferred) {
|
---|
167 | # print "trying $_ again\n";
|
---|
168 | # recslurp($_, 1);
|
---|
169 | # }
|
---|
170 |
|
---|
171 | # .. but we can at least say how many records weren't imported.
|
---|
172 | print "$ok OK, ".scalar(@deferred)." deferred, $converted downconverted records in $flatfile\n";
|
---|
173 | undef @deferred;
|
---|
174 | $converted = 0;
|
---|
175 |
|
---|
176 | # Sub for various nonstandard types with lots of pure bytes expressed in octal
|
---|
177 | # Takes a tinydns rdata string and count, returns a list of $count bytes as well
|
---|
178 | # as trimming those logical bytes off the front of the rdata string.
|
---|
179 | sub _byteparse {
|
---|
180 | my $src = shift;
|
---|
181 | my $count = shift;
|
---|
182 | my @ret;
|
---|
183 | for (my $i = 0; $i < $count; $i++) {
|
---|
184 | if ($$src =~ /^\\/) {
|
---|
185 | # we should have an octal bit
|
---|
186 | my ($tmp) = ($$src =~ /^(\\\d{3})/);
|
---|
187 | $tmp =~ s/\\/0/;
|
---|
188 | push @ret, oct($tmp);
|
---|
189 | $$src =~ s/^\\\d{3}//;
|
---|
190 | } else {
|
---|
191 | # we seem to have a byte expressed as an ASCII character
|
---|
192 | my ($tmp) = ($$src =~ /^(.)/);
|
---|
193 | push @ret, ord($tmp);
|
---|
194 | $$src =~ s/^.//;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | return @ret;
|
---|
198 | }
|
---|
199 |
|
---|
200 | # Convert octal-coded bytes back to something resembling normal characters, general case
|
---|
201 | sub _deoctal {
|
---|
202 | my $targ = shift;
|
---|
203 | while ($$targ =~ /\\(\d{3})/) {
|
---|
204 | my $sub = chr(oct($1));
|
---|
205 | $$targ =~ s/\\$1/$sub/g;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | sub _rdata2string {
|
---|
210 | my $rdata = shift;
|
---|
211 | my $tmpout = '';
|
---|
212 | while ($rdata) {
|
---|
213 | my $bytecount = 0;
|
---|
214 | if ($rdata =~ /^\\/) {
|
---|
215 | ($bytecount) = ($rdata =~ /^(\\\d{3})/);
|
---|
216 | $bytecount =~ s/\\/0/;
|
---|
217 | $bytecount = oct($bytecount);
|
---|
218 | $rdata =~ s/^\\\d{3}//;
|
---|
219 | } else {
|
---|
220 | ($bytecount) = ($rdata =~ /^(.)/);
|
---|
221 | $bytecount = ord($bytecount);
|
---|
222 | $rdata =~ s/^.//;
|
---|
223 | }
|
---|
224 | my @tmp = _byteparse(\$rdata, $bytecount);
|
---|
225 | foreach (@tmp) { $tmpout .= chr($_); }
|
---|
226 | ##fixme: warn or fail on long (>256? >512? >321?) strings
|
---|
227 | }
|
---|
228 | return $tmpout;
|
---|
229 | }
|
---|
230 |
|
---|
231 | sub _rdata2hex {
|
---|
232 | my $rdata = shift;
|
---|
233 | my $tmpout = '';
|
---|
234 | while ($rdata) {
|
---|
235 | my $byte = '';
|
---|
236 | if ($rdata =~ /^\\/) {
|
---|
237 | ($byte) = ($rdata =~ /^(\\\d{3})/);
|
---|
238 | $byte =~ s/\\/0/;
|
---|
239 | $tmpout .= sprintf("%0.2x", oct($byte));
|
---|
240 | $rdata =~ s/^\\\d{3}//;
|
---|
241 | } else {
|
---|
242 | ($byte) = ($rdata =~ /^(.)/);
|
---|
243 | $tmpout .= sprintf("%0.2x", ord($byte));
|
---|
244 | $rdata =~ s/^.//;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | return $tmpout;
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | sub recslurp {
|
---|
252 | my $rec = shift;
|
---|
253 | my $nodefer = shift || 0;
|
---|
254 | my $impok = 1;
|
---|
255 | my $msg;
|
---|
256 |
|
---|
257 | $errstr = $rec; # this way at least we have some idea what went <splat>
|
---|
258 |
|
---|
259 | if ($rec =~ /^=/) {
|
---|
260 | $cnt{APTR}++;
|
---|
261 |
|
---|
262 | ##fixme: do checks like this for all types
|
---|
263 | if ($rec !~ /^=(?:\*|\\052)?[a-z0-9\._-]+:[\d\.]+:\d*/i) {
|
---|
264 | print "bad A+PTR $rec\n";
|
---|
265 | return;
|
---|
266 | }
|
---|
267 | my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
|
---|
268 | $host =~ s/^=//;
|
---|
269 | $host =~ s/\.$//;
|
---|
270 | $ttl = -1 if $ttl eq '';
|
---|
271 | $stamp = '' if !$stamp;
|
---|
272 | $loc = '' if !$loc;
|
---|
273 | $loc = '' if $loc =~ /^:+$/;
|
---|
274 | my $fparent = $dnsdb->_hostparent($host);
|
---|
275 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($ip));
|
---|
276 | if ($fparent && $rparent) {
|
---|
277 | $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc);
|
---|
278 | } else {
|
---|
279 | if ($importcfg{legacy}) {
|
---|
280 | # Just import it already! Record may still be subject to downconversion on editing.
|
---|
281 | $fparent = 0 if !$fparent;
|
---|
282 | $rparent = 0 if !$rparent;
|
---|
283 | if ($fparent || $rparent) {
|
---|
284 | $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc);
|
---|
285 | } else {
|
---|
286 | # No parents found, cowardly refusing to add a dangling record
|
---|
287 | push @deferred, $rec unless $nodefer;
|
---|
288 | $impok = 0;
|
---|
289 | }
|
---|
290 | } elsif ($importcfg{conv}) {
|
---|
291 | # downconvert A+PTR if forward zone is not found
|
---|
292 | $recsth->execute(0, $rparent, $host, 12, $ip, 0, 0, 0, $ttl, $loc);
|
---|
293 | $converted++;
|
---|
294 | } else {
|
---|
295 | push @deferred, $rec unless $nodefer;
|
---|
296 | $impok = 0;
|
---|
297 | # print "$tmporig deferred; can't find both forward and reverse zone parents\n";
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | } elsif ($rec =~ /^C/) {
|
---|
302 | $cnt{CNAME}++;
|
---|
303 |
|
---|
304 | my ($host,$targ,$ttl,$stamp,$loc) = split /:/, $rec, 5;
|
---|
305 | $host =~ s/^C//;
|
---|
306 | $host =~ s/\.$//;
|
---|
307 | $host =~ s/^\\052/*/;
|
---|
308 | $ttl = -1 if $ttl eq '';
|
---|
309 | $stamp = '' if !$stamp;
|
---|
310 | $loc = '' if !$loc;
|
---|
311 | $loc = '' if $loc =~ /^:+$/;
|
---|
312 | if ($host =~ /\.arpa$/) {
|
---|
313 | ($code,$msg) = DNSDB::_zone2cidr($host);
|
---|
314 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
|
---|
315 | $recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl, $loc);
|
---|
316 |
|
---|
317 | ##fixme: automagically convert manually maintained sub-/24 delegations
|
---|
318 | # my ($subip, $zone) = split /\./, $targ, 2;
|
---|
319 | # ($code, $msg) = DNSDB::_zone2cidr($zone);
|
---|
320 | # push @{$deleg{"$msg"}{iplist}}, $subip;
|
---|
321 | #print "$msg $subip\n";
|
---|
322 |
|
---|
323 | } else {
|
---|
324 | my $fparent = $dnsdb->_hostparent($host);
|
---|
325 | if ($fparent) {
|
---|
326 | $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl, $loc);
|
---|
327 | } else {
|
---|
328 | push @deferred, $rec unless $nodefer;
|
---|
329 | $impok = 0;
|
---|
330 | # print "$tmporig deferred; can't find parent zone\n";
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | } elsif ($rec =~ /^\&/) {
|
---|
335 | $cnt{NS}++;
|
---|
336 |
|
---|
337 | my ($zone,$ip,$ns,$ttl,$stamp,$loc) = split /:/, $rec, 6;
|
---|
338 | $zone =~ s/^\&//;
|
---|
339 | $zone =~ s/\.$//;
|
---|
340 | $ns =~ s/\.$//;
|
---|
341 | $ns = "$ns.ns.$zone" if $ns !~ /\./;
|
---|
342 | $ttl = -1 if $ttl eq '';
|
---|
343 | $stamp = '' if !$stamp;
|
---|
344 | $loc = '' if !$loc;
|
---|
345 | $loc = '' if $loc =~ /^:+$/;
|
---|
346 | if ($zone =~ /\.arpa$/) {
|
---|
347 | ($code,$msg) = DNSDB::_zone2cidr($zone);
|
---|
348 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >>= ?", undef, ("$msg"));
|
---|
349 | ##fixme, in concert with the CNAME check for same; automagically
|
---|
350 | # create "delegate" record instead for subzone NSes: convert above to use = instead of >>=
|
---|
351 | # ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"))
|
---|
352 | # if !$rparent;
|
---|
353 | if ($rparent) {
|
---|
354 | $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl, $loc);
|
---|
355 | } else {
|
---|
356 | push @deferred, $rec unless $nodefer;
|
---|
357 | $impok = 0;
|
---|
358 | }
|
---|
359 | } else {
|
---|
360 | my $fparent = $dnsdb->_hostparent($zone);
|
---|
361 | if ($fparent) {
|
---|
362 | $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl, $loc);
|
---|
363 | $recsth->execute($fparent, 0, $ns, 2, $ip, 0, 0, 0, $ttl, $loc) if $ip;
|
---|
364 | } else {
|
---|
365 | push @deferred, $rec unless $nodefer;
|
---|
366 | $impok = 0;
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | } elsif ($rec =~ /^\^/) {
|
---|
371 | $cnt{PTR}++;
|
---|
372 |
|
---|
373 | my ($rip,$host,$ttl,$stamp,$loc) = split /:/, $rec, 5;
|
---|
374 | $rip =~ s/^\^//;
|
---|
375 | $rip =~ s/\.$//;
|
---|
376 | $ttl = -1 if $ttl eq '';
|
---|
377 | $stamp = '' if !$stamp;
|
---|
378 | $loc = '' if !$loc;
|
---|
379 | $loc = '' if $loc =~ /^:+$/;
|
---|
380 | my $rparent;
|
---|
381 | if (my ($i, $z) = ($rip =~ /^(\d+)\.(\d+-(?:\d+\.){4}in-addr.arpa)$/) ) {
|
---|
382 | ($code,$msg) = DNSDB::_zone2cidr($z);
|
---|
383 | # Exact matches only, because we're in a sub-/24 delegation
|
---|
384 | ##fixme: flag the type of delegation (range, subnet-with-dash, subnet-with-slash)
|
---|
385 | # somewhere so we can recover it on export. probably best to do that in the revzone data.
|
---|
386 | ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ("$msg"));
|
---|
387 | $z =~ s/^[\d-]+//;
|
---|
388 | ($code,$msg) = DNSDB::_zone2cidr("$i.$z"); # Get the actual IP and normalize
|
---|
389 | } else {
|
---|
390 | ($code,$msg) = DNSDB::_zone2cidr($rip);
|
---|
391 | ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"));
|
---|
392 | }
|
---|
393 | if ($rparent) {
|
---|
394 | $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl, $loc);
|
---|
395 | } else {
|
---|
396 | push @deferred, $rec unless $nodefer;
|
---|
397 | $impok = 0;
|
---|
398 | }
|
---|
399 |
|
---|
400 | } elsif ($rec =~ /^\+/) {
|
---|
401 | $cnt{A}++;
|
---|
402 |
|
---|
403 | my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
|
---|
404 | $host =~ s/^\+//;
|
---|
405 | $host =~ s/\.$//;
|
---|
406 | $host =~ s/^\\052/*/;
|
---|
407 | $ttl = -1 if $ttl eq '';
|
---|
408 | $stamp = '' if !$stamp;
|
---|
409 | $loc = '' if !$loc;
|
---|
410 | $loc = '' if $loc =~ /^:+$/;
|
---|
411 |
|
---|
412 | my $domid = $dnsdb->_hostparent($host);
|
---|
413 | if ($domid) {
|
---|
414 | $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc);
|
---|
415 | } else {
|
---|
416 | push @deferred, $rec unless $nodefer;
|
---|
417 | $impok = 0;
|
---|
418 | }
|
---|
419 |
|
---|
420 | } elsif ($rec =~ /^Z/) {
|
---|
421 | $cnt{SOA}++;
|
---|
422 |
|
---|
423 | my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$stamp,$loc) = split /:/, $rec, 11;
|
---|
424 | $zone =~ s/^Z//;
|
---|
425 | $zone =~ s/\.$//;
|
---|
426 | $master =~ s/\.$//;
|
---|
427 | $contact =~ s/\.$//;
|
---|
428 | $ttl = -1 if $ttl eq '';
|
---|
429 | $stamp = '' if !$stamp;
|
---|
430 | $loc = '' if !$loc;
|
---|
431 | $loc = '' if $loc =~ /^:+$/;
|
---|
432 | if ($zone =~ /\.arpa$/) {
|
---|
433 | ($code,$msg) = DNSDB::_zone2cidr($zone);
|
---|
434 | $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location) VALUES (?,1,1,?)",
|
---|
435 | undef, ($msg, $loc));
|
---|
436 | my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
|
---|
437 | $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc);
|
---|
438 | } else {
|
---|
439 | $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)",
|
---|
440 | undef, ($zone, $loc));
|
---|
441 | my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
|
---|
442 | $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc);
|
---|
443 | }
|
---|
444 |
|
---|
445 | } elsif ($rec =~ /^\@/) {
|
---|
446 | $cnt{MX}++;
|
---|
447 |
|
---|
448 | my ($zone,$ip,$host,$dist,$ttl,$stamp,$loc) = split /:/, $rec, 7;
|
---|
449 | $zone =~ s/^\@//;
|
---|
450 | $zone =~ s/\.$//;
|
---|
451 | $zone =~ s/^\\052/*/;
|
---|
452 | $host =~ s/\.$//;
|
---|
453 | $host = "$host.mx.$zone" if $host !~ /\./;
|
---|
454 | $ttl = -1 if $ttl eq '';
|
---|
455 | $stamp = '' if !$stamp;
|
---|
456 | $loc = '' if !$loc;
|
---|
457 | $loc = '' if $loc =~ /^:+$/;
|
---|
458 |
|
---|
459 | # note we don't check for reverse domains here, because MX records don't make any sense in reverse zones.
|
---|
460 | # if this really ever becomes an issue for someone it can be expanded to handle those weirdos
|
---|
461 |
|
---|
462 | # allow for subzone MXes, since it's perfectly legitimate to simply stuff it all in a single parent zone
|
---|
463 | my $domid = $dnsdb->_hostparent($zone);
|
---|
464 | if ($domid) {
|
---|
465 | $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl, $loc);
|
---|
466 | $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip;
|
---|
467 | } else {
|
---|
468 | push @deferred, $rec unless $nodefer;
|
---|
469 | $impok = 0;
|
---|
470 | }
|
---|
471 |
|
---|
472 | } elsif ($rec =~ /^'/) {
|
---|
473 | $cnt{TXT}++;
|
---|
474 |
|
---|
475 | my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
|
---|
476 | $fqdn =~ s/^'//;
|
---|
477 | $fqdn =~ s/^\\052/*/;
|
---|
478 | _deoctal(\$rdata);
|
---|
479 | $ttl = -1 if $ttl eq '';
|
---|
480 | $stamp = '' if !$stamp;
|
---|
481 | $loc = '' if !$loc;
|
---|
482 | $loc = '' if $loc =~ /^:+$/;
|
---|
483 |
|
---|
484 | if ($fqdn =~ /\.arpa$/) {
|
---|
485 | ($code,$msg) = DNSDB::_zone2cidr($fqdn);
|
---|
486 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
|
---|
487 | $recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl, $loc);
|
---|
488 | } else {
|
---|
489 | my $domid = $dnsdb->_hostparent($fqdn);
|
---|
490 | if ($domid) {
|
---|
491 | $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl, $loc);
|
---|
492 | } else {
|
---|
493 | push @deferred, $rec unless $nodefer;
|
---|
494 | $impok = 0;
|
---|
495 | }
|
---|
496 | }
|
---|
497 |
|
---|
498 | } elsif ($rec =~ /^\./) {
|
---|
499 | $cnt{NSASOA}++;
|
---|
500 |
|
---|
501 | my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
|
---|
502 | $fqdn =~ s/^\.//;
|
---|
503 | $fqdn =~ s/\.$//;
|
---|
504 | $ns =~ s/\.$//;
|
---|
505 | $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
|
---|
506 | $ttl = -1 if $ttl eq '';
|
---|
507 | $stamp = '' if !$stamp;
|
---|
508 | $loc = '' if !$loc;
|
---|
509 | $loc = '' if $loc =~ /^:+$/;
|
---|
510 |
|
---|
511 | if ($fqdn =~ /\.arpa$/) {
|
---|
512 | ($code,$msg) = DNSDB::_zone2cidr($fqdn);
|
---|
513 | my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
|
---|
514 | if (!$rdns) {
|
---|
515 | $errstr = "adding revzone $msg";
|
---|
516 | $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location) VALUES (?,1,1,?)",
|
---|
517 | undef, ($msg, $loc));
|
---|
518 | ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
|
---|
519 | # this would probably make a lot more sense to do hostmaster.$config{admindomain}
|
---|
520 | $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc);
|
---|
521 | }
|
---|
522 | $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl, $loc);
|
---|
523 | ##fixme: (?) implement full conversion of tinydns . records?
|
---|
524 | # -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse
|
---|
525 | #$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl)
|
---|
526 | # ... auto-A-record simply does not make sense in reverse zones. Functionally
|
---|
527 | # I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
|
---|
528 | # zones has names for their nameservers already.
|
---|
529 | # Even the auto-nameserver-fqdn comes out... ugly.
|
---|
530 |
|
---|
531 | } else {
|
---|
532 | my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
|
---|
533 | undef, ($fqdn));
|
---|
534 | if (!$domid) {
|
---|
535 | $errstr = "adding domain $fqdn";
|
---|
536 | $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)",
|
---|
537 | undef, ($fqdn, $loc));
|
---|
538 | ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
|
---|
539 | $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc);
|
---|
540 | }
|
---|
541 | $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl, $loc);
|
---|
542 | $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip;
|
---|
543 | }
|
---|
544 |
|
---|
545 |
|
---|
546 | } elsif ($rec =~ /^\%/) {
|
---|
547 | $cnt{VIEWS}++;
|
---|
548 |
|
---|
549 | # unfortunate that we don't have a guaranteed way to get a description on these. :/
|
---|
550 | my ($loc,$cnet) = split /:/, $rec, 2;
|
---|
551 | $loc =~ s/^\%//;
|
---|
552 | if (my ($iplist) = $dbh->selectrow_array("SELECT iplist FROM locations WHERE location = ?", undef, ($loc))) {
|
---|
553 | if ($cnet) {
|
---|
554 | $iplist .= ", $cnet";
|
---|
555 | $dbh->do("UPDATE locations SET iplist = ? WHERE location = ?", undef, ($iplist, $loc));
|
---|
556 | } else {
|
---|
557 | # hmm. spit out a warning? if we already have entries for $loc, adding a null
|
---|
558 | # entry will almost certainly Do The Wrong Thing(TM)
|
---|
559 | }
|
---|
560 | } else {
|
---|
561 | $cnet = '' if !$cnet; # de-nullify
|
---|
562 | $dbh->do("INSERT INTO locations (location,iplist,description) VALUES (?,?,?)", undef, ($loc, $cnet, $loc));
|
---|
563 | }
|
---|
564 |
|
---|
565 | } elsif ($rec =~ /^:/) {
|
---|
566 | $cnt{NCUST}++;
|
---|
567 | # Big section. Since tinydns can publish anything you can encode properly, but only provides official
|
---|
568 | # recognition and handling for the core common types, this must deal with the leftovers.
|
---|
569 | # :fqdn:type:rdata:ttl:time:loc
|
---|
570 |
|
---|
571 | my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
|
---|
572 | $fqdn =~ s/\.$//;
|
---|
573 | $fqdn =~ s/^\\052/*/;
|
---|
574 | $ttl = -1 if $ttl eq '';
|
---|
575 | $stamp = '' if !$stamp;
|
---|
576 | $loc = '' if !$loc;
|
---|
577 | $loc = '' if $loc =~ /^:+$/;
|
---|
578 |
|
---|
579 | if ($type == 33) {
|
---|
580 | # SRV
|
---|
581 | my ($prio, $weight, $port, $target) = (0,0,0,0);
|
---|
582 |
|
---|
583 | my @tmp = _byteparse(\$rdata, 2);
|
---|
584 | $prio = $tmp[0] * 256 + $tmp[1];
|
---|
585 | @tmp = _byteparse(\$rdata, 2);
|
---|
586 | $weight = $tmp[0] * 256 + $tmp[1];
|
---|
587 | @tmp = _byteparse(\$rdata, 2);
|
---|
588 | $port = $tmp[0] * 256 + $tmp[1];
|
---|
589 |
|
---|
590 | $rdata =~ s/\\\d{3}/./g;
|
---|
591 | ($target) = ($rdata =~ /^\.(.+)\.$/);
|
---|
592 | # hmm. the above *should* work, but What If(TM) we have ASCII-range bytes
|
---|
593 | # representing the target's fqdn part length(s)? axfr-get doesn't seem to,
|
---|
594 | # probably because dec. 33->63 includes most punctuation and all the numbers
|
---|
595 | # while ($rdata =~ /(\\\d{3})/) {
|
---|
596 | # my $cnt = $1;
|
---|
597 | # $rdata =~ s/^$cnt//;
|
---|
598 | # $cnt =~ s/^\\/0/;
|
---|
599 | # $cnt = oct($cnt);
|
---|
600 | # my ($seg) = ($rdata =~ /^(.{$cnt})/);
|
---|
601 | # $target .=
|
---|
602 | # }
|
---|
603 |
|
---|
604 | my $domid = $dnsdb->_hostparent($fqdn);
|
---|
605 | if ($domid) {
|
---|
606 | $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl, $loc) if $domid;
|
---|
607 | } else {
|
---|
608 | push @deferred, $rec unless $nodefer;
|
---|
609 | $impok = 0;
|
---|
610 | }
|
---|
611 |
|
---|
612 | } elsif ($type == 28) {
|
---|
613 | # AAAA
|
---|
614 | my @v6;
|
---|
615 |
|
---|
616 | for (my $i=0; $i < 8; $i++) {
|
---|
617 | my @tmp = _byteparse(\$rdata, 2);
|
---|
618 | push @v6, sprintf("%0.4x", $tmp[0] * 256 + $tmp[1]);
|
---|
619 | }
|
---|
620 | my $val = NetAddr::IP->new(join(':', @v6));
|
---|
621 |
|
---|
622 | my $fparent = $dnsdb->_hostparent($fqdn);
|
---|
623 | if ($fparent) {
|
---|
624 | $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl, $loc);
|
---|
625 | } else {
|
---|
626 | push @deferred, $rec unless $nodefer;
|
---|
627 | $impok = 0;
|
---|
628 | }
|
---|
629 |
|
---|
630 | } elsif ($type == 16) {
|
---|
631 | # TXT
|
---|
632 | my $txtstring = _rdata2string($rdata);
|
---|
633 |
|
---|
634 | if ($fqdn =~ /\.arpa$/) {
|
---|
635 | ($code,$msg) = DNSDB::_zone2cidr($fqdn);
|
---|
636 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
|
---|
637 | if ($rparent) {
|
---|
638 | $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl, $loc);
|
---|
639 | } else {
|
---|
640 | push @deferred, $rec unless $nodefer;
|
---|
641 | $impok = 0;
|
---|
642 | }
|
---|
643 | } else {
|
---|
644 | my $domid = $dnsdb->_hostparent($fqdn);
|
---|
645 | if ($domid) {
|
---|
646 | $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl, $loc);
|
---|
647 | } else {
|
---|
648 | push @deferred, $rec unless $nodefer;
|
---|
649 | $impok = 0;
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 | } elsif ($type == 17) {
|
---|
654 | # RP
|
---|
655 | my ($email, $txtrec) = split /\\000/, $rdata;
|
---|
656 | $email =~ s/\\\d{3}/./g;
|
---|
657 | $email =~ s/^\.//;
|
---|
658 | $txtrec =~ s/\\\d{3}/./g;
|
---|
659 | $txtrec =~ s/^\.//;
|
---|
660 |
|
---|
661 | # these might actually make sense in a reverse zone... sort of.
|
---|
662 | if ($fqdn =~ /\.arpa$/) {
|
---|
663 | ($code,$msg) = DNSDB::_zone2cidr($fqdn);
|
---|
664 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
|
---|
665 | if ($rparent) {
|
---|
666 | $recsth->execute(0, $rparent, "$email $txtrec", 17, "$msg", 0, 0, 0, $ttl, $loc);
|
---|
667 | } else {
|
---|
668 | push @deferred, $rec unless $nodefer;
|
---|
669 | $impok = 0;
|
---|
670 | }
|
---|
671 | } else {
|
---|
672 | my $domid = $dnsdb->_hostparent($fqdn);
|
---|
673 | if ($domid) {
|
---|
674 | $recsth->execute($domid, 0, $fqdn, 17, "$email $txtrec", 0, 0, 0, $ttl, $loc);
|
---|
675 | } else {
|
---|
676 | push @deferred, $rec unless $nodefer;
|
---|
677 | $impok = 0;
|
---|
678 | }
|
---|
679 | }
|
---|
680 |
|
---|
681 | } elsif ($type == 44) {
|
---|
682 | # SSHFP
|
---|
683 | my $sshfp = _byteparse(\$rdata, 1);
|
---|
684 | $sshfp .= " "._byteparse(\$rdata, 1);
|
---|
685 | $sshfp .= " "._rdata2hex($rdata);
|
---|
686 |
|
---|
687 | # these do not make sense in a reverse zone, since they're logically attached to an A record
|
---|
688 | my $domid = $dnsdb->_hostparent($fqdn);
|
---|
689 | if ($domid) {
|
---|
690 | $recsth->execute($domid, 0, $fqdn, 44, $sshfp, 0, 0, 0, $ttl, $loc);
|
---|
691 | } else {
|
---|
692 | push @deferred, $rec unless $nodefer;
|
---|
693 | $impok = 0;
|
---|
694 | }
|
---|
695 |
|
---|
696 | } else {
|
---|
697 | print "unhandled rec $rec\n";
|
---|
698 | $impok = 0;
|
---|
699 | # ... uhhh, dunno
|
---|
700 | }
|
---|
701 |
|
---|
702 | } else {
|
---|
703 | $cnt{other}++;
|
---|
704 | print " $_\n";
|
---|
705 | }
|
---|
706 |
|
---|
707 | return $impok; # just to make sure
|
---|
708 | } # recslurp()
|
---|
709 |
|
---|
710 | close FLAT;
|
---|
711 | }
|
---|