source: trunk/tiny-import.pl@ 386

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

/trunk

Fix minor(ish) bug in import of locations; set default_location
on new zones. See #10.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 21.4 KB
Line 
1#!/usr/bin/perl
2# dnsadmin shell-based import tool for tinydns flatfiles
3##
4# $Id: tiny-import.pl 380 2012-08-10 20:06:31Z 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,default_location) VALUES (?,1,1,?)",
412 undef, ($msg, $loc));
413 my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
414 $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc);
415 } else {
416 $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)",
417 undef, ($zone, $loc));
418 my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
419 $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc);
420 }
421
422 } elsif ($rec =~ /^\@/) {
423 $cnt{MX}++;
424
425 my ($zone,$ip,$host,$dist,$ttl,$stamp,$loc) = split /:/, $rec, 7;
426 $zone =~ s/^\@//;
427 $zone =~ s/\.$//;
428 $zone =~ s/^\\052/*/;
429 $host =~ s/\.$//;
430 $host = "$host.mx.$zone" if $host !~ /\./;
431 $ttl = 0 if !$ttl;
432 $stamp = '' if !$stamp;
433 $loc = '' if !$loc;
434 $loc = '' if $loc =~ /^:+$/;
435
436# note we don't check for reverse domains here, because MX records don't make any sense in reverse zones.
437# if this really ever becomes an issue for someone it can be expanded to handle those weirdos
438
439 # allow for subzone MXes, since it's perfectly legitimate to simply stuff it all in a single parent zone
440 my $domid = DNSDB::_hostparent($dbh, $zone);
441 if ($domid) {
442 $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl, $loc);
443 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip;
444 } else {
445 push @deferred, $rec unless $nodefer;
446 $impok = 0;
447 }
448
449 } elsif ($rec =~ /^'/) {
450 $cnt{TXT}++;
451
452 my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
453 $fqdn =~ s/^'//;
454 $fqdn =~ s/^\\052/*/;
455 _deoctal(\$rdata);
456 $ttl = 0 if !$ttl;
457 $stamp = '' if !$stamp;
458 $loc = '' if !$loc;
459 $loc = '' if $loc =~ /^:+$/;
460
461 if ($fqdn =~ /\.arpa$/) {
462 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
463 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
464 $recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl, $loc);
465 } else {
466 my $domid = DNSDB::_hostparent($dbh, $fqdn);
467 if ($domid) {
468 $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl, $loc);
469 } else {
470 push @deferred, $rec unless $nodefer;
471 $impok = 0;
472 }
473 }
474
475 } elsif ($rec =~ /^\./) {
476 $cnt{NSASOA}++;
477
478 my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
479 $fqdn =~ s/^\.//;
480 $fqdn =~ s/\.$//;
481 $ns =~ s/\.$//;
482 $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
483 $ttl = 0 if !$ttl;
484 $stamp = '' if !$stamp;
485 $loc = '' if !$loc;
486 $loc = '' if $loc =~ /^:+$/;
487
488 if ($fqdn =~ /\.arpa$/) {
489 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
490 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
491 if (!$rdns) {
492 $errstr = "adding revzone $msg";
493 $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location) VALUES (?,1,1,?)",
494 undef, ($msg, $loc));
495 ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
496# this would probably make a lot more sense to do hostmaster.$config{admindomain}
497 $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc);
498 }
499 $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl, $loc);
500##fixme: (?) implement full conversion of tinydns . records?
501# -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse
502#$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl)
503# ... auto-A-record simply does not make sense in reverse zones. Functionally
504# I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
505# zones has names for their nameservers already.
506# Even the auto-nameserver-fqdn comes out... ugly.
507
508 } else {
509 my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
510 undef, ($fqdn));
511 if (!$domid) {
512 $errstr = "adding domain $fqdn";
513 $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)",
514 undef, ($fqdn, $loc));
515 ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
516 $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc);
517 }
518 $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl, $loc);
519 $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip;
520 }
521
522
523 } elsif ($rec =~ /^\%/) {
524 $cnt{VIEWS}++;
525
526 # unfortunate that we don't have a guaranteed way to get a description on these. :/
527 my ($loc,$cnet) = split /:/, $rec, 2;
528 $loc =~ s/^\%//;
529 if (my ($iplist) = $dbh->selectrow_array("SELECT iplist FROM locations WHERE location = ?", undef, ($loc))) {
530 if ($cnet) {
531 $iplist .= ", $cnet";
532 $dbh->do("UPDATE locations SET iplist = ? WHERE location = ?", undef, ($iplist, $loc));
533 } else {
534 # hmm. spit out a warning? if we already have entries for $loc, adding a null
535 # entry will almost certainly Do The Wrong Thing(TM)
536 }
537 } else {
538 $cnet = '' if !$cnet; # de-nullify
539 $dbh->do("INSERT INTO locations (location,iplist,description) VALUES (?,?,?)", undef, ($loc, $cnet, $loc));
540 }
541
542 } elsif ($rec =~ /^:/) {
543 $cnt{NCUST}++;
544# Big section. Since tinydns can publish anything you can encode properly, but only provides official
545# recognition and handling for the core common types, this must deal with the leftovers.
546# :fqdn:type:rdata:ttl:time:loc
547
548 my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
549 $fqdn =~ s/\.$//;
550 $fqdn =~ s/^\\052/*/;
551 $ttl = 0 if !$ttl;
552 $stamp = '' if !$stamp;
553 $loc = '' if !$loc;
554 $loc = '' if $loc =~ /^:+$/;
555
556 if ($type == 33) {
557 # SRV
558 my ($prio, $weight, $port, $target) = (0,0,0,0);
559
560 my @tmp = _byteparse(\$rdata, 2);
561 $prio = $tmp[0] * 256 + $tmp[1];
562 @tmp = _byteparse(\$rdata, 2);
563 $weight = $tmp[0] * 256 + $tmp[1];
564 @tmp = _byteparse(\$rdata, 2);
565 $port = $tmp[0] * 256 + $tmp[1];
566
567 $rdata =~ s/\\\d{3}/./g;
568 ($target) = ($rdata =~ /^\.(.+)\.$/);
569# hmm. the above *should* work, but What If(TM) we have ASCII-range bytes
570# representing the target's fqdn part length(s)? axfr-get doesn't seem to,
571# probably because dec. 33->63 includes most punctuation and all the numbers
572# while ($rdata =~ /(\\\d{3})/) {
573# my $cnt = $1;
574# $rdata =~ s/^$cnt//;
575# $cnt =~ s/^\\/0/;
576# $cnt = oct($cnt);
577# my ($seg) = ($rdata =~ /^(.{$cnt})/);
578# $target .=
579# }
580
581 my $domid = DNSDB::_hostparent($dbh, $fqdn);
582 if ($domid) {
583 $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl, $loc) if $domid;
584 } else {
585 push @deferred, $rec unless $nodefer;
586 $impok = 0;
587 }
588
589 } elsif ($type == 28) {
590 # AAAA
591 my @v6;
592
593 for (my $i=0; $i < 8; $i++) {
594 my @tmp = _byteparse(\$rdata, 2);
595 push @v6, sprintf("%0.4x", $tmp[0] * 256 + $tmp[1]);
596 }
597 my $val = NetAddr::IP->new(join(':', @v6));
598
599 my $fparent = DNSDB::_hostparent($dbh, $fqdn);
600 if ($fparent) {
601 $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl, $loc);
602 } else {
603 push @deferred, $rec unless $nodefer;
604 $impok = 0;
605 }
606
607 } elsif ($type == 16) {
608 # TXT
609 my $txtstring = _rdata2string($rdata);
610
611 if ($fqdn =~ /\.arpa$/) {
612 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
613 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
614 if ($rparent) {
615 $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl, $loc);
616 } else {
617 push @deferred, $rec unless $nodefer;
618 $impok = 0;
619 }
620 } else {
621 my $domid = DNSDB::_hostparent($dbh, $fqdn);
622 if ($domid) {
623 $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl, $loc);
624 } else {
625 push @deferred, $rec unless $nodefer;
626 $impok = 0;
627 }
628 }
629
630 } elsif ($type == 17) {
631 # RP
632 my ($email, $txtrec) = split /\\000/, $rdata;
633 $email =~ s/\\\d{3}/./g;
634 $email =~ s/^\.//;
635 $txtrec =~ s/\\\d{3}/./g;
636 $txtrec =~ s/^\.//;
637
638 # these might actually make sense in a reverse zone... sort of.
639 if ($fqdn =~ /\.arpa$/) {
640 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
641 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
642 if ($rparent) {
643 $recsth->execute(0, $rparent, "$email $txtrec", 17, "$msg", 0, 0, 0, $ttl, $loc);
644 } else {
645 push @deferred, $rec unless $nodefer;
646 $impok = 0;
647 }
648 } else {
649 my $domid = DNSDB::_hostparent($dbh, $fqdn);
650 if ($domid) {
651 $recsth->execute($domid, 0, $fqdn, 17, "$email $txtrec", 0, 0, 0, $ttl, $loc);
652 } else {
653 push @deferred, $rec unless $nodefer;
654 $impok = 0;
655 }
656 }
657
658 } elsif ($type == 44) {
659 # SSHFP
660 my $sshfp = _byteparse(\$rdata, 1);
661 $sshfp .= " "._byteparse(\$rdata, 1);
662 $sshfp .= " "._rdata2hex($rdata);
663
664 # these do not make sense in a reverse zone, since they're logically attached to an A record
665 my $domid = DNSDB::_hostparent($dbh, $fqdn);
666 if ($domid) {
667 $recsth->execute($domid, 0, $fqdn, 44, $sshfp, 0, 0, 0, $ttl, $loc);
668 } else {
669 push @deferred, $rec unless $nodefer;
670 $impok = 0;
671 }
672
673 } else {
674 print "unhandled rec $rec\n";
675 $impok = 0;
676 # ... uhhh, dunno
677 }
678
679 } else {
680 $cnt{other}++;
681 print " $_\n";
682 }
683
684 return $impok; # just to make sure
685 } # recslurp()
686
687 close FLAT;
688}
Note: See TracBrowser for help on using the repository browser.