source: trunk/tiny-import.pl@ 374

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

/trunk

Polish tiny-import.pl for live use

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