source: trunk/tiny-import.pl@ 562

Last change on this file since 562 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
Line 
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 $
5# Copyright 2012,2013 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;
27use POSIX;
28use Time::TAI64 qw(:tai);
29
30use lib '.'; ##uselib##
31use DNSDB;
32
33my $dnsdb = new DNSDB;
34
35usage() if !@ARGV;
36
37my %importcfg = (
38 rw => 0,
39 conv => 0,
40 trial => 0,
41 legacy => 0,
42 );
43# Handle some command-line arguments
44while ($ARGV[0] =~ /^-/) {
45 my $arg = shift @ARGV;
46 usage() if $arg !~ /^-[rclt]+$/;
47 # -r rewrite imported files to comment imported records
48 # -c coerce/downconvert A+PTR = records to PTR
49 # -l swallow A+PTR as-is
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';
56 $importcfg{legacy} = 1 if $_ eq 'l';
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.
72 -l (for "legacy") Force import of A+PTR records as-is. Mutually exclusive
73 with -c. -l takes precedence as -c is lossy.
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
83my $code;
84my $dbh = $dnsdb->{dbh};
85
86$dbh->{AutoCommit} = 0;
87$dbh->{RaiseError} = 1;
88
89my %cnt;
90my @deferred;
91my $converted = 0;
92my $errstr = '';
93
94foreach my $file (@ARGV) {
95 eval {
96 import(file => $file);
97# import(file => $file, nosoa => 1);
98 $dbh->rollback if $importcfg{trial};
99 $dbh->commit unless $importcfg{trial};
100 };
101 if ($@) {
102 print "Failure trying to import $file: $@\n $errstr\n";
103 unlink ".$file.$$" if $importcfg{rw}; # cleanup
104 $dbh->rollback;
105 }
106}
107
108# print summary count of record types encountered
109foreach (keys %cnt) {
110 print " $_ $cnt{$_}\n";
111}
112
113exit 0;
114
115sub import {
116 our %args = @_;
117 my $flatfile = $args{file};
118 my @fpath = split '/', $flatfile;
119 $fpath[$#fpath] = ".$fpath[$#fpath]";
120 my $rwfile = join('/', @fpath);#.".$$";
121
122 open FLAT, "<$flatfile";
123
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
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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
131
132 my %deleg;
133
134 my $ok = 0;
135 while (<FLAT>) {
136 if (/^#/ || /^\s*$/) {
137 print RWFLAT "#$_" if $importcfg{rw};
138 next;
139 }
140 chomp;
141 s/\s*$//;
142 my $recstat = recslurp($_);
143 $ok++ if $recstat;
144 if ($importcfg{rw}) {
145 if ($recstat) {
146 print RWFLAT "#$_\n";
147 } else {
148 print RWFLAT "$_\n";
149 }
150 }
151 }
152
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
161 foreach (@deferred) {
162 print "failed to import $_\n";
163 }
164
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# }
172
173 # .. but we can at least say how many records weren't imported.
174 print "$ok OK, ".scalar(@deferred)." deferred, $converted downconverted records in $flatfile\n";
175 undef @deferred;
176 $converted = 0;
177
178 # Sub for various nonstandard types with lots of pure bytes expressed in octal
179 # Takes a tinydns rdata string and count, returns a list of $count bytes as well
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
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
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
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 }
251
252 sub calcstamp {
253 my $stampin = shift;
254 my $ttl = shift;
255 my $pzone = shift;
256 my $revrec = shift;
257
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
276 sub recslurp {
277 my $rec = shift;
278 my $nodefer = shift || 0;
279 my $impok = 1;
280 my $msg;
281
282 $errstr = $rec; # this way at least we have some idea what went <splat>
283
284 if ($rec =~ /^=/) {
285 $cnt{APTR}++;
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 }
292 my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
293 $host =~ s/^=//;
294 $host =~ s/\.$//;
295 $ttl = -1 if $ttl eq '';
296 $stamp = '' if !$stamp;
297 $loc = '' if !$loc;
298 $loc = '' if $loc =~ /^:+$/;
299 my $fparent = $dnsdb->_hostparent($host);
300 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($ip));
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
314 if ($fparent && $rparent) {
315 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
316 } else {
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) {
322 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
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}) {
329 # downconvert A+PTR if forward zone is not found
330 $recsth->execute(0, $rparent, $host, 12, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
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 }
337 }
338
339 } elsif ($rec =~ /^C/) {
340 $cnt{CNAME}++;
341
342 my ($host,$targ,$ttl,$stamp,$loc) = split /:/, $rec, 5;
343 $host =~ s/^C//;
344 $host =~ s/\.$//;
345 $host =~ s/^\\052/*/;
346 $ttl = -1 if $ttl eq '';
347 $stamp = '' if !$stamp;
348 $loc = '' if !$loc;
349 $loc = '' if $loc =~ /^:+$/;
350
351 my $stampactive = 'n';
352 my $expires = 'n';
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));
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 }
365
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
372 } else {
373 my $fparent = $dnsdb->_hostparent($host);
374 if ($fparent) {
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);
377 } else {
378 push @deferred, $rec unless $nodefer;
379 $impok = 0;
380 # print "$tmporig deferred; can't find parent zone\n";
381 }
382 }
383
384 } elsif ($rec =~ /^\&/) {
385 $cnt{NS}++;
386
387 my ($zone,$ip,$ns,$ttl,$stamp,$loc) = split /:/, $rec, 6;
388 $zone =~ s/^\&//;
389 $zone =~ s/\.$//;
390 $ns =~ s/\.$//;
391 $ns = "$ns.ns.$zone" if $ns !~ /\./;
392 $ttl = -1 if $ttl eq '';
393 $stamp = '' if !$stamp;
394 $loc = '' if !$loc;
395 $loc = '' if $loc =~ /^:+$/;
396
397 my $stampactive = 'n';
398 my $expires = 'n';
399
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) {
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);
410 } else {
411 push @deferred, $rec unless $nodefer;
412 $impok = 0;
413 }
414 } else {
415 my $fparent = $dnsdb->_hostparent($zone);
416 if ($fparent) {
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;
420 } else {
421 push @deferred, $rec unless $nodefer;
422 $impok = 0;
423 }
424 }
425
426 } elsif ($rec =~ /^\^/) {
427 $cnt{PTR}++;
428
429 my ($rip,$host,$ttl,$stamp,$loc) = split /:/, $rec, 5;
430 $rip =~ s/^\^//;
431 $rip =~ s/\.$//;
432 $ttl = -1 if $ttl eq '';
433 $stamp = '' if !$stamp;
434 $loc = '' if !$loc;
435 $loc = '' if $loc =~ /^:+$/;
436
437 my $stampactive = 'n';
438 my $expires = 'n';
439
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) {
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);
456 } else {
457 push @deferred, $rec unless $nodefer;
458 $impok = 0;
459 }
460
461 } elsif ($rec =~ /^\+/) {
462 $cnt{A}++;
463
464 my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
465 $host =~ s/^\+//;
466 $host =~ s/\.$//;
467 $host =~ s/^\\052/*/;
468 $ttl = -1 if $ttl eq '';
469 $stamp = '' if !$stamp;
470 $loc = '' if !$loc;
471 $loc = '' if $loc =~ /^:+$/;
472
473 my $stampactive = 'n';
474 my $expires = 'n';
475
476 my $domid = $dnsdb->_hostparent($host);
477 if ($domid) {
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);
480 } else {
481 push @deferred, $rec unless $nodefer;
482 $impok = 0;
483 }
484
485 } elsif ($rec =~ /^Z/) {
486 $cnt{SOA}++;
487
488 my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$stamp,$loc) = split /:/, $rec, 11;
489 $zone =~ s/^Z//;
490 $zone =~ s/\.$//;
491 $master =~ s/\.$//;
492 $contact =~ s/\.$//;
493 $ttl = -1 if $ttl eq '';
494 $stamp = '' if !$stamp;
495 $loc = '' if !$loc;
496 $loc = '' if $loc =~ /^:+$/;
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
514 if ($zone =~ /\.arpa$/) {
515 ($code,$msg) = DNSDB::_zone2cidr($zone);
516 $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location) VALUES (?,1,1,?)",
517 undef, ($msg, $loc));
518 my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
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);
524 } else {
525 $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)",
526 undef, ($zone, $loc));
527 my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
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);
533 }
534
535 } elsif ($rec =~ /^\@/) {
536 $cnt{MX}++;
537
538 my ($zone,$ip,$host,$dist,$ttl,$stamp,$loc) = split /:/, $rec, 7;
539 $zone =~ s/^\@//;
540 $zone =~ s/\.$//;
541 $zone =~ s/^\\052/*/;
542 $host =~ s/\.$//;
543 $host = "$host.mx.$zone" if $host !~ /\./;
544 $ttl = -1 if $ttl eq '';
545 $stamp = '' if !$stamp;
546 $loc = '' if !$loc;
547 $loc = '' if $loc =~ /^:+$/;
548
549 my $stampactive = 'n';
550 my $expires = 'n';
551
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
556 my $domid = $dnsdb->_hostparent($zone);
557 if ($domid) {
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;
561 } else {
562 push @deferred, $rec unless $nodefer;
563 $impok = 0;
564 }
565
566 } elsif ($rec =~ /^'/) {
567 $cnt{TXT}++;
568
569 my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
570 $fqdn =~ s/^'//;
571 $fqdn =~ s/^\\052/*/;
572 _deoctal(\$rdata);
573 $ttl = -1 if $ttl eq '';
574 $stamp = '' if !$stamp;
575 $loc = '' if !$loc;
576 $loc = '' if $loc =~ /^:+$/;
577
578 my $stampactive = 'n';
579 my $expires = 'n';
580
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));
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);
586 } else {
587 my $domid = $dnsdb->_hostparent($fqdn);
588 if ($domid) {
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);
591 } else {
592 push @deferred, $rec unless $nodefer;
593 $impok = 0;
594 }
595 }
596
597 } elsif ($rec =~ /^\./) {
598 $cnt{NSASOA}++;
599
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 !~ /\./;
605 $ttl = -1 if $ttl eq '';
606 $stamp = '' if !$stamp;
607 $loc = '' if !$loc;
608 $loc = '' if $loc =~ /^:+$/;
609
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
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";
630 $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location) VALUES (?,1,1,?)",
631 undef, ($msg, $loc));
632 ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
633 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, 2560, 0, 'y');
634# this would probably make a lot more sense to do hostmaster.$config{admindomain}
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);
639 }
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);
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
644#$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl, $stamp, $expires, $stampactive)
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";
655 $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)",
656 undef, ($fqdn, $loc));
657 ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
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);
661 }
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;
665 }
666
667
668 } elsif ($rec =~ /^\%/) {
669 $cnt{VIEWS}++;
670
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) {
676 $iplist .= ", $cnet";
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
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
693 my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
694 $fqdn =~ s/\.$//;
695 $fqdn =~ s/^\\052/*/;
696 $ttl = -1 if $ttl eq '';
697 $stamp = '' if !$stamp;
698 $loc = '' if !$loc;
699 $loc = '' if $loc =~ /^:+$/;
700
701 my $stampactive = 'n';
702 my $expires = 'n';
703
704 if ($type == 33) {
705 # SRV
706 my ($prio, $weight, $port, $target) = (0,0,0,0);
707
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];
714
715 $rdata =~ s/\\\d{3}/./g;
716 ($target) = ($rdata =~ /^\.(.+)\.$/);
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
729 my $domid = $dnsdb->_hostparent($fqdn);
730 if ($domid) {
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;
733 } else {
734 push @deferred, $rec unless $nodefer;
735 $impok = 0;
736 }
737
738 } elsif ($type == 28) {
739 # AAAA
740 my @v6;
741
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));
747
748 my $fparent = $dnsdb->_hostparent($fqdn);
749
750 if ($fparent) {
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);
753 } else {
754 push @deferred, $rec unless $nodefer;
755 $impok = 0;
756 }
757
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) {
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);
768 } else {
769 push @deferred, $rec unless $nodefer;
770 $impok = 0;
771 }
772 } else {
773 my $domid = $dnsdb->_hostparent($fqdn);
774 if ($domid) {
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);
777 } else {
778 push @deferred, $rec unless $nodefer;
779 $impok = 0;
780 }
781 }
782
783 } elsif ($type == 17) {
784 # RP
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/^\.//;
790
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) {
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 );
798 } else {
799 push @deferred, $rec unless $nodefer;
800 $impok = 0;
801 }
802 } else {
803 my $domid = $dnsdb->_hostparent($fqdn);
804 if ($domid) {
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);
807 } else {
808 push @deferred, $rec unless $nodefer;
809 $impok = 0;
810 }
811 }
812
813 } elsif ($type == 44) {
814 # SSHFP
815 my $sshfp = _byteparse(\$rdata, 1);
816 $sshfp .= " "._byteparse(\$rdata, 1);
817 $sshfp .= " "._rdata2hex($rdata);
818
819 # these do not make sense in a reverse zone, since they're logically attached to an A record
820 my $domid = $dnsdb->_hostparent($fqdn);
821 if ($domid) {
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);
824 } else {
825 push @deferred, $rec unless $nodefer;
826 $impok = 0;
827 }
828
829 } else {
830 print "unhandled rec $rec\n";
831 $impok = 0;
832 # ... uhhh, dunno
833 }
834
835 } else {
836 $cnt{other}++;
837 print " $_\n";
838 }
839
840 return $impok; # just to make sure
841 } # recslurp()
842
843 close FLAT;
844}
Note: See TracBrowser for help on using the repository browser.