source: trunk/tiny-import.pl@ 558

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

/trunk

Implement most of the UI and back end for handling scheduled changes
to records. See #40.

This turned out to be most of what I had vaguely imagined; only SOA
records can't sanely be set for scheduled changes yet (can't think of
a scenario where this would even be useful) and there's only a small
dusting of UI chrome left for another time.

Bumped up from projected 1.4 to 1.2 per request from Reid Sutherland.

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