source: trunk/tiny-import.pl@ 478

Last change on this file since 478 was 468, checked in by Kris Deugau, 12 years ago

/trunk

Object conversion of DNSDB.pm, 4 of <mumble>. See #11.

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