source: trunk/tiny-import.pl@ 881

Last change on this file since 881 was 843, checked in by Kris Deugau, 3 years ago

/trunk

Minor accumulated refinements on tiny-import.pl

  • Update include path finder - see #80
  • Clean up extra trailing :'s that sometimes confused the location
  • Better handle "empty" TTL values
  • Slightly improve SOA serial type tagging; only handles the most common case of "let tinydns handle it" UNIX-epoch serials for now
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 33.5 KB
RevLine 
[348]1#!/usr/bin/perl
2# dnsadmin shell-based import tool for tinydns flatfiles
3##
4# $Id: tiny-import.pl 843 2022-04-29 19:45:16Z kdeugau $
[843]5# Copyright 2012-2014,2020-2022 Kris Deugau <kdeugau@deepnet.cx>
[348]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
[356]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
[348]25use strict;
26use warnings;
[543]27use POSIX;
28use Time::TAI64 qw(:tai);
[348]29
[843]30# Taint-safe (ish) voodoo to push "the directory the script is in" into @INC.
31use File::Spec ();
32use File::Basename ();
33my $path;
34BEGIN {
35 $path = File::Basename::dirname(File::Spec->rel2abs($0));
36 if ($path =~ /(.*)/) {
37 $path = $1;
38 }
39}
40use lib $path;
[797]41
[468]42use DNSDB;
[348]43
[468]44my $dnsdb = new DNSDB;
[348]45
[373]46usage() if !@ARGV;
47
48my %importcfg = (
49 rw => 0,
50 conv => 0,
51 trial => 0,
[575]52 legacy => 0,
[579]53 merge => 0,
[575]54 group => 1,
[373]55 );
[575]56my $gnum = '';
[373]57# Handle some command-line arguments
58while ($ARGV[0] =~ /^-/) {
59 my $arg = shift @ARGV;
[579]60 usage() if $arg !~ /^-(?:[rclmt]+|g\d*)$/;
[373]61 # -r rewrite imported files to comment imported records
62 # -c coerce/downconvert A+PTR = records to PTR
[522]63 # -l swallow A+PTR as-is
[579]64 # -m merge PTR and A/AAAA as possible
[373]65 # -t trial mode; don't commit to DB or actually rewrite flatfile (disables -r)
[575]66 # -g import to specified group (name or ID) instead of group 1
[373]67 $arg =~ s/^-//;
[575]68# for Reasons (none clear), $arg is undefined yet defined, but only when number characters are involved. Ebbeh?
69no warnings qw(uninitialized);
70 if ($arg =~ /^g/) {
71 if ($arg eq 'g') {
72 $importcfg{group} = shift @ARGV;
73 } else {
74 $arg =~ s/^g//;
75 $importcfg{group} = $arg;
76 }
77 } else {
78 my @tmp = split //, $arg;
79 foreach (@tmp) {
80 $importcfg{rw} = 1 if $_ eq 'r';
81 $importcfg{conv} = 1 if $_ eq 'c';
82 $importcfg{legacy} = 1 if $_ eq 'l';
[579]83 $importcfg{merge} = 1 if $_ eq 'm';
[575]84 $importcfg{trial} = 1 if $_ eq 't';
85 }
[373]86 }
[575]87 use warnings qw(uninitialized);
[373]88}
89$importcfg{rw} = 0 if $importcfg{trial};
90
[575]91# allow group names
92if ($importcfg{group} =~ /^\d+$/) {
93 $importcfg{groupname} = $dnsdb->groupName($importcfg{group});
94} else {
95 $importcfg{groupname} = $importcfg{group};
96 $importcfg{group} = $dnsdb->groupID($importcfg{groupname});
97}
98
99die usage() if $importcfg{group} !~ /^\d+$/;
100
[373]101sub usage {
[575]102 die q(usage: tiny-import.pl [-rclt] [-gnn] [-g name] datafile1 datafile2 ... datafileN ...
[373]103 -r Rewrite all specified data files with a warning header indicating the
104 records are now managed by web, and commenting out all imported records.
105 The directory containing any given datafile must be writable.
106 -c Convert any A+PTR (=) record to a bare PTR if the forward domain is
107 not present in the database. Note this does NOT look forward through
108 a single file, nor across multiple files handled in the same run.
109 Multiple passes may be necessary if SOA and = records are heavily
110 intermixed and not clustered together.
[522]111 -l (for "legacy") Force import of A+PTR records as-is. Mutually exclusive
112 with -c. -l takes precedence as -c is lossy.
[579]113 -m Merge PTR and A or AAAA records to A+PTR or AAAA+PTR records where possible
[575]114 -gnnn or -g nnn or -g name
115 Import new zones into this group (group name or ID accepted) instead of
116 the root/default group 1
[373]117 -t Trial run mode; spits out records that would be left unimported.
118 Disables -r if set.
119
120 -r and -c may be combined (-rc)
121
122 datafileN is any tinydns record data file.
123);
124}
125
[348]126my $code;
[468]127my $dbh = $dnsdb->{dbh};
[348]128
[579]129# collect some things for logging
130($dnsdb->{logusername}, undef, undef, undef, undef, undef, $dnsdb->{logfullname}) = getpwuid($<);
[770]131$dnsdb->{logfullname} =~ s/,//g;
[579]132$dnsdb->{loguserid} = 0; # not worth setting up a pseudouser the way the RPC system does
133$dnsdb->{logusername} = $dnsdb->{logusername}."/tiny-import.pl";
134$dnsdb->{logfullname} = $dnsdb->{logusername} if !$dnsdb->{logfullname};
[791]135$dnsdb->{logfullname} = $dnsdb->{logfullname}."/tiny-import.pl";
[579]136
[348]137$dbh->{AutoCommit} = 0;
138$dbh->{RaiseError} = 1;
139
140my %cnt;
141my @deferred;
[461]142my $converted = 0;
[353]143my $errstr = '';
[348]144
145foreach my $file (@ARGV) {
[791]146 my %filecount;
147 my $logentry = "Import records from $file: ";
[348]148 eval {
[791]149 import(file => $file, cnt => \%filecount);
150 if (%filecount) {
151 foreach (sort keys %filecount) {
152 $logentry .= "$_ $filecount{$_}, ";
153 $cnt{$_} += $filecount{$_};
154 }
155 $logentry =~ s/[\s,]+$//;
156 $dnsdb->_log(group_id => $importcfg{group}, entry => $logentry);
157 }
[373]158 $dbh->rollback if $importcfg{trial};
159 $dbh->commit unless $importcfg{trial};
[348]160 };
161 if ($@) {
[373]162 print "Failure trying to import $file: $@\n $errstr\n";
163 unlink ".$file.$$" if $importcfg{rw}; # cleanup
164 $dbh->rollback;
[348]165 }
166}
167
[373]168# print summary count of record types encountered
[791]169foreach (sort keys %cnt) {
[373]170 print " $_ $cnt{$_}\n";
171}
[348]172
173exit 0;
174
175sub import {
176 our %args = @_;
177 my $flatfile = $args{file};
[791]178 my $filecnt = $args{cnt};
[373]179 my @fpath = split '/', $flatfile;
180 $fpath[$#fpath] = ".$fpath[$#fpath]";
181 my $rwfile = join('/', @fpath);#.".$$";
182
[348]183 open FLAT, "<$flatfile";
184
[373]185 if ($importcfg{rw}) {
186 open RWFLAT, ">$rwfile" or die "Couldn't open tempfile $rwfile for rewriting: $!\n";
187 print RWFLAT "# WARNING: Records in this file have been imported to the web UI.\n#\n";
188 }
189
[543]190 our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl,location,stamp,expires,stampactive) ".
191 " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
[348]192
[579]193 # for A/AAAA records
194 our $revcheck = $dbh->prepare("SELECT rdns_id,record_id,ttl FROM records WHERE host=? AND val=? AND type=12");
195 our $mergefwd = $dbh->prepare("UPDATE records SET type=?,domain_id=?,ttl=? WHERE record_id=?");
196 # for PTR records
197 our $fwdcheck = $dbh->prepare("SELECT domain_id,record_id,ttl FROM records WHERE host=? AND val=? AND (type=1 OR type=28)");
198 our $mergerev = $dbh->prepare("UPDATE records SET type=?,rdns_id=?,ttl=? WHERE record_id=?");
199
[353]200 my %deleg;
201
[396]202 my $ok = 0;
[348]203 while (<FLAT>) {
[373]204 if (/^#/ || /^\s*$/) {
205 print RWFLAT "#$_" if $importcfg{rw};
206 next;
207 }
[348]208 chomp;
[372]209 s/\s*$//;
[843]210 s/:+$//;
[791]211 my $recstat = recslurp($_, $filecnt);
[396]212 $ok++ if $recstat;
[373]213 if ($importcfg{rw}) {
214 if ($recstat) {
215 print RWFLAT "#$_\n";
216 } else {
217 print RWFLAT "$_\n";
218 }
219 }
[348]220 }
221
[373]222 # Move the rewritten flatfile in place of the original, so that any
223 # external export processing will pick up any remaining records.
224 if ($importcfg{rw}) {
225 close RWFLAT;
226 rename "$rwfile", $flatfile;
227 }
228
229 # Show the failed records
[348]230 foreach (@deferred) {
[373]231 print "failed to import $_\n";
[348]232 }
233
[373]234##fixme: hmm. can't write the record back to the flatfile in the
235# main while above, then come down here and import it anyway, can we?
236# # Try the deferred records again, once.
237# foreach (@deferred) {
238# print "trying $_ again\n";
239# recslurp($_, 1);
240# }
[353]241
[373]242 # .. but we can at least say how many records weren't imported.
[461]243 print "$ok OK, ".scalar(@deferred)." deferred, $converted downconverted records in $flatfile\n";
244 undef @deferred;
245 $converted = 0;
[373]246
[348]247 # Sub for various nonstandard types with lots of pure bytes expressed in octal
[353]248 # Takes a tinydns rdata string and count, returns a list of $count bytes as well
[348]249 # as trimming those logical bytes off the front of the rdata string.
250 sub _byteparse {
251 my $src = shift;
252 my $count = shift;
253 my @ret;
254 for (my $i = 0; $i < $count; $i++) {
255 if ($$src =~ /^\\/) {
256 # we should have an octal bit
257 my ($tmp) = ($$src =~ /^(\\\d{3})/);
258 $tmp =~ s/\\/0/;
259 push @ret, oct($tmp);
260 $$src =~ s/^\\\d{3}//;
261 } else {
262 # we seem to have a byte expressed as an ASCII character
263 my ($tmp) = ($$src =~ /^(.)/);
264 push @ret, ord($tmp);
265 $$src =~ s/^.//;
266 }
267 }
268 return @ret;
269 }
270
[353]271 # Convert octal-coded bytes back to something resembling normal characters, general case
272 sub _deoctal {
273 my $targ = shift;
274 while ($$targ =~ /\\(\d{3})/) {
275 my $sub = chr(oct($1));
276 $$targ =~ s/\\$1/$sub/g;
277 }
278 }
279
[361]280 sub _rdata2string {
281 my $rdata = shift;
282 my $tmpout = '';
283 while ($rdata) {
284 my $bytecount = 0;
285 if ($rdata =~ /^\\/) {
286 ($bytecount) = ($rdata =~ /^(\\\d{3})/);
287 $bytecount =~ s/\\/0/;
288 $bytecount = oct($bytecount);
289 $rdata =~ s/^\\\d{3}//;
290 } else {
291 ($bytecount) = ($rdata =~ /^(.)/);
292 $bytecount = ord($bytecount);
293 $rdata =~ s/^.//;
294 }
295 my @tmp = _byteparse(\$rdata, $bytecount);
296 foreach (@tmp) { $tmpout .= chr($_); }
297##fixme: warn or fail on long (>256? >512? >321?) strings
298 }
299 return $tmpout;
300 }
301
[363]302 sub _rdata2hex {
303 my $rdata = shift;
304 my $tmpout = '';
305 while ($rdata) {
306 my $byte = '';
307 if ($rdata =~ /^\\/) {
308 ($byte) = ($rdata =~ /^(\\\d{3})/);
309 $byte =~ s/\\/0/;
310 $tmpout .= sprintf("%0.2x", oct($byte));
311 $rdata =~ s/^\\\d{3}//;
312 } else {
313 ($byte) = ($rdata =~ /^(.)/);
314 $tmpout .= sprintf("%0.2x", ord($byte));
315 $rdata =~ s/^.//;
316 }
317 }
318 return $tmpout;
319 }
[361]320
[543]321 sub calcstamp {
322 my $stampin = shift;
323 my $ttl = shift;
324 my $pzone = shift;
325 my $revrec = shift;
[363]326
[543]327 return ($ttl, 'n', 'n', '1970-01-01 00:00:00 -0') if !$stampin;
328
329##fixme Yes, this fails for records in 2038 sometime. No, I'm not going to care for a while.
330 $stampin = "\@$stampin"; # Time::TAI64 needs the leading @. Feh.
331 my $u = tai2unix($stampin);
332 $stampin = strftime("%Y-%m-%d %H:%M:%S %z", localtime($u));
333 my $expires = 'n';
334 if ($ttl) {
335 # TTL can stay put.
336 } else {
337 # TTL on import is 0, almost certainly wrong. Get the parent zone's SOA and use the minttl.
338 my $soa = $dnsdb->getSOA('n', $revrec, $pzone);
339 $ttl = $soa->{minttl};
340 $expires = 'y';
341 }
342 return ($ttl, 'y', $expires, $stampin);
343 }
344
[348]345 sub recslurp {
346 my $rec = shift;
[791]347 my $filecnt = shift;
[348]348 my $nodefer = shift || 0;
[373]349 my $impok = 1;
[468]350 my $msg;
[348]351
[360]352 $errstr = $rec; # this way at least we have some idea what went <splat>
353
[348]354 if ($rec =~ /^=/) {
[791]355 $filecnt->{'A+PTR'}++;
[354]356
357##fixme: do checks like this for all types
358 if ($rec !~ /^=(?:\*|\\052)?[a-z0-9\._-]+:[\d\.]+:\d*/i) {
359 print "bad A+PTR $rec\n";
360 return;
361 }
[353]362 my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
[348]363 $host =~ s/^=//;
364 $host =~ s/\.$//;
[519]365 $ttl = -1 if $ttl eq '';
[353]366 $stamp = '' if !$stamp;
[348]367 $loc = '' if !$loc;
[353]368 $loc = '' if $loc =~ /^:+$/;
[468]369 my $fparent = $dnsdb->_hostparent($host);
[348]370 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($ip));
[543]371
372 my $stampactive = 'n';
373 my $expires = 'n';
374
375 # can't set a timestamp on an orphaned record. we'll actually fail import of this record a little later.
376 if ($fparent || $rparent) {
377 if ($fparent) {
378 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $fparent, 'n');
379 } else {
380 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y');
381 }
382 }
383
[348]384 if ($fparent && $rparent) {
[543]385 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[348]386 } else {
[522]387 if ($importcfg{legacy}) {
388 # Just import it already! Record may still be subject to downconversion on editing.
389 $fparent = 0 if !$fparent;
390 $rparent = 0 if !$rparent;
391 if ($fparent || $rparent) {
[543]392 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[522]393 } else {
394 # No parents found, cowardly refusing to add a dangling record
395 push @deferred, $rec unless $nodefer;
396 $impok = 0;
397 }
398 } elsif ($importcfg{conv}) {
[461]399 # downconvert A+PTR if forward zone is not found
[543]400 $recsth->execute(0, $rparent, $host, 12, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[461]401 $converted++;
402 } else {
403 push @deferred, $rec unless $nodefer;
404 $impok = 0;
405 # print "$tmporig deferred; can't find both forward and reverse zone parents\n";
406 }
[348]407 }
408
409 } elsif ($rec =~ /^C/) {
[791]410 $filecnt->{CNAME}++;
[354]411
[353]412 my ($host,$targ,$ttl,$stamp,$loc) = split /:/, $rec, 5;
[348]413 $host =~ s/^C//;
414 $host =~ s/\.$//;
[360]415 $host =~ s/^\\052/*/;
[843]416 $ttl = -1 if !defined($ttl) || $ttl eq '';
[353]417 $stamp = '' if !$stamp;
[348]418 $loc = '' if !$loc;
[353]419 $loc = '' if $loc =~ /^:+$/;
[543]420
421 my $stampactive = 'n';
422 my $expires = 'n';
423
[353]424 if ($host =~ /\.arpa$/) {
425 ($code,$msg) = DNSDB::_zone2cidr($host);
426 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
[543]427 if ($rparent) {
428 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y');
429 $recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
430 } else {
431 push @deferred, $rec unless $nodefer;
432 $impok = 0;
433 # print "$tmporig deferred; can't find parent zone\n";
434 }
[348]435
[353]436##fixme: automagically convert manually maintained sub-/24 delegations
437# my ($subip, $zone) = split /\./, $targ, 2;
438# ($code, $msg) = DNSDB::_zone2cidr($zone);
439# push @{$deleg{"$msg"}{iplist}}, $subip;
440#print "$msg $subip\n";
441
[348]442 } else {
[468]443 my $fparent = $dnsdb->_hostparent($host);
[353]444 if ($fparent) {
[543]445 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $fparent, 'n');
446 $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[353]447 } else {
448 push @deferred, $rec unless $nodefer;
[373]449 $impok = 0;
[353]450 # print "$tmporig deferred; can't find parent zone\n";
451 }
[348]452 }
453
454 } elsif ($rec =~ /^\&/) {
[791]455 $filecnt->{NS}++;
[354]456
[355]457 my ($zone,$ip,$ns,$ttl,$stamp,$loc) = split /:/, $rec, 6;
458 $zone =~ s/^\&//;
459 $zone =~ s/\.$//;
[357]460 $ns =~ s/\.$//;
[358]461 $ns = "$ns.ns.$zone" if $ns !~ /\./;
[519]462 $ttl = -1 if $ttl eq '';
[355]463 $stamp = '' if !$stamp;
464 $loc = '' if !$loc;
465 $loc = '' if $loc =~ /^:+$/;
[543]466
467 my $stampactive = 'n';
468 my $expires = 'n';
469
[355]470 if ($zone =~ /\.arpa$/) {
471 ($code,$msg) = DNSDB::_zone2cidr($zone);
472 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >>= ?", undef, ("$msg"));
473##fixme, in concert with the CNAME check for same; automagically
474# create "delegate" record instead for subzone NSes: convert above to use = instead of >>=
475# ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"))
476# if !$rparent;
477 if ($rparent) {
[543]478 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y');
479 $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[355]480 } else {
481 push @deferred, $rec unless $nodefer;
[373]482 $impok = 0;
[355]483 }
484 } else {
[468]485 my $fparent = $dnsdb->_hostparent($zone);
[355]486 if ($fparent) {
[543]487 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $fparent, 'n');
488 $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
489 $recsth->execute($fparent, 0, $ns, 2, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive) if $ip;
[355]490 } else {
491 push @deferred, $rec unless $nodefer;
[373]492 $impok = 0;
[355]493 }
494 }
495
[348]496 } elsif ($rec =~ /^\^/) {
[791]497 $filecnt->{PTR}++;
[354]498
[356]499 my ($rip,$host,$ttl,$stamp,$loc) = split /:/, $rec, 5;
500 $rip =~ s/^\^//;
501 $rip =~ s/\.$//;
[520]502 $ttl = -1 if $ttl eq '';
[356]503 $stamp = '' if !$stamp;
504 $loc = '' if !$loc;
505 $loc = '' if $loc =~ /^:+$/;
[543]506
507 my $stampactive = 'n';
508 my $expires = 'n';
509
[356]510 my $rparent;
511 if (my ($i, $z) = ($rip =~ /^(\d+)\.(\d+-(?:\d+\.){4}in-addr.arpa)$/) ) {
512 ($code,$msg) = DNSDB::_zone2cidr($z);
513 # Exact matches only, because we're in a sub-/24 delegation
514##fixme: flag the type of delegation (range, subnet-with-dash, subnet-with-slash)
515# somewhere so we can recover it on export. probably best to do that in the revzone data.
516 ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ("$msg"));
517 $z =~ s/^[\d-]+//;
518 ($code,$msg) = DNSDB::_zone2cidr("$i.$z"); # Get the actual IP and normalize
519 } else {
520 ($code,$msg) = DNSDB::_zone2cidr($rip);
521 ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"));
522 }
[579]523
[356]524 if ($rparent) {
[579]525##fixme: really want to pull this DB call inside an if $importcfg{merge},
526# but then we need to duplicate the insert for the case where the matching
527# reverse doesn't exist.
528 $host =~ s/\.$//g; # pure sytactic sugar, we don't store this trailing dot.
529 $fwdcheck->execute($host, $msg->addr);
530 my ($domid, $recid, $rttl) = $fwdcheck->fetchrow_array;
531 if ($importcfg{merge} && $domid) {
532 $ttl = ($rttl < $ttl ? $rttl : $ttl); # Take the shorter TTL
533 $mergerev->execute(($msg->{isv6} ? 65281 : 65280), $rparent, $ttl, $recid);
534 $dnsdb->_log(rdns_id => $rparent, domain_id => $domid, group_id => $importcfg{group},
535 entry => "[ import ] PTR ".$msg->addr." -> $host merged with matching ".
536 ($msg->{isv6} ? 'AAAA' : 'A')." record");
537 } else {
538 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y');
539 $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
540 }
[356]541 } else {
542 push @deferred, $rec unless $nodefer;
[373]543 $impok = 0;
[356]544 }
545
[348]546 } elsif ($rec =~ /^\+/) {
[791]547 $filecnt->{A}++;
[354]548
[359]549 my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
550 $host =~ s/^\+//;
551 $host =~ s/\.$//;
[360]552 $host =~ s/^\\052/*/;
[843]553 $ttl = -1 if !defined($ttl) || $ttl eq '';
[359]554 $stamp = '' if !$stamp;
555 $loc = '' if !$loc;
556 $loc = '' if $loc =~ /^:+$/;
557
[543]558 my $stampactive = 'n';
559 my $expires = 'n';
560
[468]561 my $domid = $dnsdb->_hostparent($host);
[359]562 if ($domid) {
[579]563##fixme: really want to pull this DB call inside an if $importcfg{merge},
564# but then we need to duplicate the insert for the case where the matching
565# reverse doesn't exist.
566 $revcheck->execute($host, $ip);
567 my ($revid, $recid, $rttl) = $revcheck->fetchrow_array;
568 if ($importcfg{merge} && $revid) {
569 $ttl = ($rttl < $ttl ? $rttl : $ttl); # Take the shorter TTL
570 $mergefwd->execute(65280, $domid, $ttl, $recid);
571 $dnsdb->_log(rdns_id => $revid, domain_id => $domid, group_id => $importcfg{group},
572 entry => "[ import ] ".($msg->{isv6} ? 'AAAA' : 'A')." record $host -> $ip".
573 " merged with matching PTR record");
574 } else {
575 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n');
576 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
577 }
[359]578 } else {
579 push @deferred, $rec unless $nodefer;
[373]580 $impok = 0;
[359]581 }
582
[348]583 } elsif ($rec =~ /^Z/) {
[791]584 $filecnt->{SOA}++;
[354]585
[353]586 my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$stamp,$loc) = split /:/, $rec, 11;
[348]587 $zone =~ s/^Z//;
588 $zone =~ s/\.$//;
589 $master =~ s/\.$//;
590 $contact =~ s/\.$//;
[519]591 $ttl = -1 if $ttl eq '';
[353]592 $stamp = '' if !$stamp;
[348]593 $loc = '' if !$loc;
[353]594 $loc = '' if $loc =~ /^:+$/;
[777]595# Default to UNIX epoch for zones with no existing serial value
596 $serial = scalar(time) if !$serial;
[543]597
598 my $stampactive = 'n';
599 my $expires = 'n';
600
601##fixme er... what do we do with an SOA with a timestamp? O_o
602# fail for now, since there's no clean way I can see to handle this (yet)
603# maybe (ab)use the -l flag to import as-is?
604 if ($stamp) {
605 push @deferred, $rec unless $nodefer;
606 return 0;
607 }
608
609##fixme: need more magic on TTL, so we can decide whether to use the minttl or newttl
610# my $newttl;
611# ($newttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $minttl, 0, 'n');
612# $ttl = $newttl if !$ttl;
613
[348]614 if ($zone =~ /\.arpa$/) {
615 ($code,$msg) = DNSDB::_zone2cidr($zone);
[843]616 $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location,sertype,zserial) VALUES (?,?,1,?,'U',?)",
[777]617 undef, ($msg, $importcfg{group}, $loc, $serial));
[348]618 my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
[543]619 my $newttl;
620 ($newttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $minttl, 0, 'y');
621 $ttl = $newttl if !$ttl;
622 $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl,
623 $loc, $stamp, $expires, $stampactive);
[348]624 } else {
[843]625 $dbh->do("INSERT INTO domains (domain,group_id,status,default_location,sertype,zserial) VALUES (?,?,1,?,'U',?)",
[777]626 undef, ($zone, $importcfg{group}, $loc, $serial));
[348]627 my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
[543]628 my $newttl;
629 ($newttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $minttl, 0, 'n');
630 $ttl = $newttl if !$ttl;
631 $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl,
632 $loc, $stamp, $expires, $stampactive);
[348]633 }
[354]634
[348]635 } elsif ($rec =~ /^\@/) {
[791]636 $filecnt->{MX}++;
[354]637
[357]638 my ($zone,$ip,$host,$dist,$ttl,$stamp,$loc) = split /:/, $rec, 7;
[359]639 $zone =~ s/^\@//;
[357]640 $zone =~ s/\.$//;
[360]641 $zone =~ s/^\\052/*/;
[357]642 $host =~ s/\.$//;
643 $host = "$host.mx.$zone" if $host !~ /\./;
[519]644 $ttl = -1 if $ttl eq '';
[357]645 $stamp = '' if !$stamp;
646 $loc = '' if !$loc;
647 $loc = '' if $loc =~ /^:+$/;
648
[543]649 my $stampactive = 'n';
650 my $expires = 'n';
651
[357]652# note we don't check for reverse domains here, because MX records don't make any sense in reverse zones.
653# if this really ever becomes an issue for someone it can be expanded to handle those weirdos
654
655 # allow for subzone MXes, since it's perfectly legitimate to simply stuff it all in a single parent zone
[468]656 my $domid = $dnsdb->_hostparent($zone);
[357]657 if ($domid) {
[543]658 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n');
659 $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
660 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive) if $ip;
[357]661 } else {
662 push @deferred, $rec unless $nodefer;
[373]663 $impok = 0;
[357]664 }
665
[348]666 } elsif ($rec =~ /^'/) {
[791]667 $filecnt->{TXT}++;
[348]668
[353]669 my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
[348]670 $fqdn =~ s/^'//;
[360]671 $fqdn =~ s/^\\052/*/;
[348]672 _deoctal(\$rdata);
[519]673 $ttl = -1 if $ttl eq '';
[353]674 $stamp = '' if !$stamp;
675 $loc = '' if !$loc;
676 $loc = '' if $loc =~ /^:+$/;
[348]677
[543]678 my $stampactive = 'n';
679 my $expires = 'n';
680
[360]681 if ($fqdn =~ /\.arpa$/) {
682 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
683 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
[543]684 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y');
685 $recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[348]686 } else {
[468]687 my $domid = $dnsdb->_hostparent($fqdn);
[360]688 if ($domid) {
[543]689 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n');
690 $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[360]691 } else {
692 push @deferred, $rec unless $nodefer;
[373]693 $impok = 0;
[360]694 }
[348]695 }
696
697 } elsif ($rec =~ /^\./) {
[791]698 $filecnt->{NSASOA}++;
[354]699
[353]700 my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
701 $fqdn =~ s/^\.//;
702 $fqdn =~ s/\.$//;
703 $ns =~ s/\.$//;
704 $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
[519]705 $ttl = -1 if $ttl eq '';
[353]706 $stamp = '' if !$stamp;
707 $loc = '' if !$loc;
708 $loc = '' if $loc =~ /^:+$/;
709
[543]710 my $stampactive = 'n';
711 my $expires = 'n';
712
713##fixme er... what do we do with an SOA with a timestamp? O_o
714# fail for now, since there's no clean way I can see to handle this (yet)
715# maybe (ab)use the -l flag to import as-is?
716 if ($stamp) {
717 push @deferred, $rec unless $nodefer;
718 return 0;
719 }
720
721##fixme: need more magic on TTL, so we can decide whether to use the minttl or newttl
722# my $newttl;
723# ($newttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $minttl, 0, 'n');
724
[353]725 if ($fqdn =~ /\.arpa$/) {
726 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
727 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
728 if (!$rdns) {
729 $errstr = "adding revzone $msg";
[380]730 $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location) VALUES (?,1,1,?)",
731 undef, ($msg, $loc));
[353]732 ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
[598]733 my $soattl;
734 ($soattl, $stampactive, $expires, $stamp) = calcstamp($stamp, 2560, 0, 'y');
[353]735# this would probably make a lot more sense to do hostmaster.$config{admindomain}
[543]736# otherwise, it's as per the tinydns defaults that work tolerably well on a small scale
737# serial -> modtime of data file, ref -> 16384, ret -> 2048, exp -> 1048576, min -> 2560
[598]738# the SOA also gets the default 2560 TTL, no matter what was set on the . entry.
739 $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, $soattl,
[543]740 $loc, $stamp, $expires, $stampactive);
[353]741 }
[598]742 # NS records get the specified TTL from the original . entry
743 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rdns, 'y') if !$stamp;
[543]744 $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[353]745##fixme: (?) implement full conversion of tinydns . records?
746# -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse
[543]747#$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl, $stamp, $expires, $stampactive)
[353]748# ... auto-A-record simply does not make sense in reverse zones. Functionally
749# I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
750# zones has names for their nameservers already.
751# Even the auto-nameserver-fqdn comes out... ugly.
752
753 } else {
754 my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
755 undef, ($fqdn));
756 if (!$domid) {
757 $errstr = "adding domain $fqdn";
[380]758 $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)",
759 undef, ($fqdn, $loc));
[353]760 ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
[543]761 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, 2560, 0, 'n');
762 $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560",
763 $loc, $stamp, $expires, $stampactive);
[353]764 }
[543]765 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n') if !$stamp;
766 $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
767 $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive) if $ip;
[353]768 }
769
770
771 } elsif ($rec =~ /^\%/) {
[791]772 $filecnt->{VIEWS}++;
[354]773
[372]774 # unfortunate that we don't have a guaranteed way to get a description on these. :/
775 my ($loc,$cnet) = split /:/, $rec, 2;
776 $loc =~ s/^\%//;
777 if (my ($iplist) = $dbh->selectrow_array("SELECT iplist FROM locations WHERE location = ?", undef, ($loc))) {
778 if ($cnet) {
[375]779 $iplist .= ", $cnet";
[372]780 $dbh->do("UPDATE locations SET iplist = ? WHERE location = ?", undef, ($iplist, $loc));
781 } else {
782 # hmm. spit out a warning? if we already have entries for $loc, adding a null
783 # entry will almost certainly Do The Wrong Thing(TM)
784 }
785 } else {
786 $cnet = '' if !$cnet; # de-nullify
787 $dbh->do("INSERT INTO locations (location,iplist,description) VALUES (?,?,?)", undef, ($loc, $cnet, $loc));
788 }
789
[348]790 } elsif ($rec =~ /^:/) {
[791]791 $filecnt->{NCUST}++;
[348]792# Big section. Since tinydns can publish anything you can encode properly, but only provides official
793# recognition and handling for the core common types, this must deal with the leftovers.
794# :fqdn:type:rdata:ttl:time:loc
795
[354]796 my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
[360]797 $fqdn =~ s/\.$//;
798 $fqdn =~ s/^\\052/*/;
[519]799 $ttl = -1 if $ttl eq '';
[354]800 $stamp = '' if !$stamp;
801 $loc = '' if !$loc;
802 $loc = '' if $loc =~ /^:+$/;
[348]803
[543]804 my $stampactive = 'n';
805 my $expires = 'n';
806
[354]807 if ($type == 33) {
808 # SRV
809 my ($prio, $weight, $port, $target) = (0,0,0,0);
[348]810
[354]811 my @tmp = _byteparse(\$rdata, 2);
812 $prio = $tmp[0] * 256 + $tmp[1];
813 @tmp = _byteparse(\$rdata, 2);
814 $weight = $tmp[0] * 256 + $tmp[1];
815 @tmp = _byteparse(\$rdata, 2);
816 $port = $tmp[0] * 256 + $tmp[1];
[348]817
[354]818 $rdata =~ s/\\\d{3}/./g;
819 ($target) = ($rdata =~ /^\.(.+)\.$/);
[348]820# hmm. the above *should* work, but What If(TM) we have ASCII-range bytes
821# representing the target's fqdn part length(s)? axfr-get doesn't seem to,
822# probably because dec. 33->63 includes most punctuation and all the numbers
823# while ($rdata =~ /(\\\d{3})/) {
824# my $cnt = $1;
825# $rdata =~ s/^$cnt//;
826# $cnt =~ s/^\\/0/;
827# $cnt = oct($cnt);
828# my ($seg) = ($rdata =~ /^(.{$cnt})/);
829# $target .=
830# }
831
[468]832 my $domid = $dnsdb->_hostparent($fqdn);
[354]833 if ($domid) {
[543]834 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n');
835 $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive) if $domid;
[354]836 } else {
837 push @deferred, $rec unless $nodefer;
[373]838 $impok = 0;
[354]839 }
[348]840
[354]841 } elsif ($type == 28) {
842 # AAAA
843 my @v6;
[348]844
[354]845 for (my $i=0; $i < 8; $i++) {
846 my @tmp = _byteparse(\$rdata, 2);
847 push @v6, sprintf("%0.4x", $tmp[0] * 256 + $tmp[1]);
848 }
849 my $val = NetAddr::IP->new(join(':', @v6));
[348]850
[468]851 my $fparent = $dnsdb->_hostparent($fqdn);
[543]852
[579]853##fixme: really want to pull this DB call inside an if $importcfg{merge},
854# but then we need to duplicate the insert for the case where the matching
855# reverse doesn't exist.
856 $revcheck->execute($fqdn, $val);
857 my ($revid, $recid, $rttl) = $revcheck->fetchrow_array;
[348]858
[579]859 # If we have a revzone and merging is enabled, update the existing
860 # record with a reverse ID, set the type to one of the internal
861 # pseudotypes, and set the TTL to the lower of the two.
862 if ($importcfg{merge} && $revid) {
863 $ttl = ($rttl < $ttl ? $rttl : $ttl); # Take the shorter TTL
864 $mergefwd->execute(65281, $fparent, $ttl, $recid);
865 $dnsdb->_log(rdns_id => $revid, domain_id => $fparent, group_id => $importcfg{group},
866 entry => "[ import ] ".($msg->{isv6} ? 'AAAA' : 'A')." record $fqdn -> $val".
867 " merged with matching PTR record");
868 } else {
869 if ($fparent) {
870 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $fparent, 'n');
871 $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
872 } else {
873 push @deferred, $rec unless $nodefer;
874 $impok = 0;
875 }
876 }
877
[361]878 } elsif ($type == 16) {
879 # TXT
880 my $txtstring = _rdata2string($rdata);
881
882 if ($fqdn =~ /\.arpa$/) {
883 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
884 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
885 if ($rparent) {
[543]886 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y');
887 $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[361]888 } else {
889 push @deferred, $rec unless $nodefer;
[373]890 $impok = 0;
[361]891 }
892 } else {
[468]893 my $domid = $dnsdb->_hostparent($fqdn);
[361]894 if ($domid) {
[543]895 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n');
896 $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[361]897 } else {
898 push @deferred, $rec unless $nodefer;
[373]899 $impok = 0;
[361]900 }
901 }
902
903 } elsif ($type == 17) {
904 # RP
[362]905 my ($email, $txtrec) = split /\\000/, $rdata;
906 $email =~ s/\\\d{3}/./g;
907 $email =~ s/^\.//;
908 $txtrec =~ s/\\\d{3}/./g;
909 $txtrec =~ s/^\.//;
[361]910
[362]911 # these might actually make sense in a reverse zone... sort of.
912 if ($fqdn =~ /\.arpa$/) {
913 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
914 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
915 if ($rparent) {
[543]916 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y');
917 $recsth->execute(0, $rparent, "$email $txtrec", 17, "$msg", 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive );
[362]918 } else {
919 push @deferred, $rec unless $nodefer;
[373]920 $impok = 0;
[362]921 }
922 } else {
[468]923 my $domid = $dnsdb->_hostparent($fqdn);
[362]924 if ($domid) {
[543]925 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n');
926 $recsth->execute($domid, 0, $fqdn, 17, "$email $txtrec", 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[362]927 } else {
928 push @deferred, $rec unless $nodefer;
[373]929 $impok = 0;
[362]930 }
931 }
932
[361]933 } elsif ($type == 44) {
934 # SSHFP
[363]935 my $sshfp = _byteparse(\$rdata, 1);
936 $sshfp .= " "._byteparse(\$rdata, 1);
937 $sshfp .= " "._rdata2hex($rdata);
[361]938
[363]939 # these do not make sense in a reverse zone, since they're logically attached to an A record
[468]940 my $domid = $dnsdb->_hostparent($fqdn);
[363]941 if ($domid) {
[543]942 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n');
943 $recsth->execute($domid, 0, $fqdn, 44, $sshfp, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
[363]944 } else {
945 push @deferred, $rec unless $nodefer;
[373]946 $impok = 0;
[363]947 }
948
[354]949 } else {
[373]950 print "unhandled rec $rec\n";
951 $impok = 0;
[354]952 # ... uhhh, dunno
953 }
[348]954
955 } else {
[791]956 $filecnt->{other}++;
[354]957 print " $_\n";
[348]958 }
959
[373]960 return $impok; # just to make sure
961 } # recslurp()
962
[348]963 close FLAT;
964}
Note: See TracBrowser for help on using the repository browser.