source: trunk/tiny-import.pl@ 461

Last change on this file since 461 was 461, checked in by Kris Deugau, 11 years ago

/trunk

Actually handle the -c option in tiny-import.pl

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