source: trunk/tiny-import.pl@ 361

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

/trunk

Checkpoint tiny-import.pl

  • Factor out rdata to general string conversion for generic types that actually put strings on the wire (TXT, RP, SPF for sure)
  • Add TXT support in generic records
  • Add stubs for RP and SSHFP records (observed in live data)
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 15.9 KB
RevLine 
[348]1#!/usr/bin/perl
2# dnsadmin shell-based import tool for tinydns flatfiles
3##
4# $Id: tiny-import.pl 361 2012-07-09 22:04:25Z kdeugau $
5# Copyright 2012 Kris Deugau <kdeugau@deepnet.cx>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19##
20
[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;
27
28use lib '.';
29use DNSDB qw(:ALL);
30
31if (!loadConfig()) {
32 warn "Using default configuration; unable to load custom settings: $DNSDB::errstr";
33}
34
35my $code;
36my ($dbh,$msg) = connectDB($config{dbname}, $config{dbuser}, $config{dbpass}, $config{dbhost});
37initGlobals($dbh) if $dbh;
38
39$dbh->{AutoCommit} = 0;
40$dbh->{RaiseError} = 1;
41
42my %cnt;
43my @deferred;
[353]44my $errstr = '';
[348]45
46foreach my $file (@ARGV) {
47 eval {
48 import(file => $file);
49# import(file => $file, nosoa => 1);
50 $dbh->rollback;
51# $dbh->commit;
52 };
53 if ($@) {
54 print "bleh: $@\n";
[353]55die "die harder: $errstr\n";
[348]56 }
57}
58
59 foreach (keys %cnt) {
60 print " $_ $cnt{$_}\n";
61 }
62
63exit 0;
64
65sub import {
66 our %args = @_;
67 my $flatfile = $args{file};
68 open FLAT, "<$flatfile";
69
70 our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl) ".
71 " VALUES (?,?,?,?,?,?,?,?,?)");
72
[353]73 my %deleg;
74
[348]75 while (<FLAT>) {
76 next if /^#/;
77 next if /^\s*$/;
78 chomp;
79 recslurp($_);
80 }
81
82 # Try the deferred records again, once.
83 foreach (@deferred) {
84 # print "trying $_ again\n";
85 recslurp($_, 1);
86 }
87
[353]88print scalar(@deferred)." deferred records in $flatfile\n";
89
[348]90 # Sub for various nonstandard types with lots of pure bytes expressed in octal
[353]91 # Takes a tinydns rdata string and count, returns a list of $count bytes as well
[348]92 # as trimming those logical bytes off the front of the rdata string.
93 sub _byteparse {
94 my $src = shift;
95 my $count = shift;
96 my @ret;
97 for (my $i = 0; $i < $count; $i++) {
98 if ($$src =~ /^\\/) {
99 # we should have an octal bit
100 my ($tmp) = ($$src =~ /^(\\\d{3})/);
101 $tmp =~ s/\\/0/;
102 push @ret, oct($tmp);
103 $$src =~ s/^\\\d{3}//;
104 } else {
105 # we seem to have a byte expressed as an ASCII character
106 my ($tmp) = ($$src =~ /^(.)/);
107 push @ret, ord($tmp);
108 $$src =~ s/^.//;
109 }
110 }
111 return @ret;
112 }
113
[353]114 # Convert octal-coded bytes back to something resembling normal characters, general case
115 sub _deoctal {
116 my $targ = shift;
117 while ($$targ =~ /\\(\d{3})/) {
118 my $sub = chr(oct($1));
119 $$targ =~ s/\\$1/$sub/g;
120 }
121 }
122
[361]123 sub _rdata2string {
124 my $rdata = shift;
125 my $tmpout = '';
126 while ($rdata) {
127 my $bytecount = 0;
128 if ($rdata =~ /^\\/) {
129 ($bytecount) = ($rdata =~ /^(\\\d{3})/);
130 $bytecount =~ s/\\/0/;
131 $bytecount = oct($bytecount);
132 $rdata =~ s/^\\\d{3}//;
133 } else {
134 ($bytecount) = ($rdata =~ /^(.)/);
135 $bytecount = ord($bytecount);
136 $rdata =~ s/^.//;
137 }
138 my @tmp = _byteparse(\$rdata, $bytecount);
139 foreach (@tmp) { $tmpout .= chr($_); }
140##fixme: warn or fail on long (>256? >512? >321?) strings
141 }
142 return $tmpout;
143 }
144
145
[348]146 sub recslurp {
147 my $rec = shift;
148 my $nodefer = shift || 0;
149
[360]150 $errstr = $rec; # this way at least we have some idea what went <splat>
151
[348]152 if ($rec =~ /^=/) {
153 $cnt{APTR}++;
[354]154
155##fixme: do checks like this for all types
156 if ($rec !~ /^=(?:\*|\\052)?[a-z0-9\._-]+:[\d\.]+:\d*/i) {
157 print "bad A+PTR $rec\n";
158 return;
159 }
[353]160 my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
[348]161 $host =~ s/^=//;
162 $host =~ s/\.$//;
[353]163 $ttl = 0 if !$ttl;
164 $stamp = '' if !$stamp;
[348]165 $loc = '' if !$loc;
[353]166 $loc = '' if $loc =~ /^:+$/;
[348]167 my $fparent = DNSDB::_hostparent($dbh, $host);
168 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($ip));
169 if ($fparent && $rparent) {
[353]170 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl);
[348]171 } else {
172 push @deferred, $rec unless $nodefer;
173 # print "$tmporig deferred; can't find both forward and reverse zone parents\n";
174 }
175
176 } elsif ($rec =~ /^C/) {
177 $cnt{CNAME}++;
[354]178
[353]179 my ($host,$targ,$ttl,$stamp,$loc) = split /:/, $rec, 5;
[348]180 $host =~ s/^C//;
181 $host =~ s/\.$//;
[360]182 $host =~ s/^\\052/*/;
[353]183 $ttl = 0 if !$ttl;
184 $stamp = '' if !$stamp;
[348]185 $loc = '' if !$loc;
[353]186 $loc = '' if $loc =~ /^:+$/;
187 if ($host =~ /\.arpa$/) {
188 ($code,$msg) = DNSDB::_zone2cidr($host);
189 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
190 $recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl);
[348]191
[353]192##fixme: automagically convert manually maintained sub-/24 delegations
193# my ($subip, $zone) = split /\./, $targ, 2;
194# ($code, $msg) = DNSDB::_zone2cidr($zone);
195# push @{$deleg{"$msg"}{iplist}}, $subip;
196#print "$msg $subip\n";
197
[348]198 } else {
[353]199 my $fparent = DNSDB::_hostparent($dbh, $host);
200 if ($fparent) {
201 $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl);
202 } else {
203 push @deferred, $rec unless $nodefer;
204 # print "$tmporig deferred; can't find parent zone\n";
205 }
[348]206 }
207
208 } elsif ($rec =~ /^\&/) {
209 $cnt{NS}++;
[354]210
[355]211 my ($zone,$ip,$ns,$ttl,$stamp,$loc) = split /:/, $rec, 6;
212 $zone =~ s/^\&//;
213 $zone =~ s/\.$//;
[357]214 $ns =~ s/\.$//;
[358]215 $ns = "$ns.ns.$zone" if $ns !~ /\./;
[355]216 $ttl = 0 if !$ttl;
217 $stamp = '' if !$stamp;
218 $loc = '' if !$loc;
219 $loc = '' if $loc =~ /^:+$/;
220 if ($zone =~ /\.arpa$/) {
221 ($code,$msg) = DNSDB::_zone2cidr($zone);
222 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >>= ?", undef, ("$msg"));
223##fixme, in concert with the CNAME check for same; automagically
224# create "delegate" record instead for subzone NSes: convert above to use = instead of >>=
225# ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"))
226# if !$rparent;
227 if ($rparent) {
228 $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl);
229 } else {
230 push @deferred, $rec unless $nodefer;
231 }
232 } else {
233 my $fparent = DNSDB::_hostparent($dbh, $zone);
234 if ($fparent) {
235 $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl);
[357]236 $recsth->execute($fparent, 0, $ns, 2, $ip, 0, 0, 0, $ttl) if $ip;
[355]237 } else {
238 push @deferred, $rec unless $nodefer;
239 }
240 }
241
[348]242 } elsif ($rec =~ /^\^/) {
243 $cnt{PTR}++;
[354]244
[356]245 my ($rip,$host,$ttl,$stamp,$loc) = split /:/, $rec, 5;
246 $rip =~ s/^\^//;
247 $rip =~ s/\.$//;
248 $ttl = 0 if !$ttl;
249 $stamp = '' if !$stamp;
250 $loc = '' if !$loc;
251 $loc = '' if $loc =~ /^:+$/;
252 my $rparent;
253 if (my ($i, $z) = ($rip =~ /^(\d+)\.(\d+-(?:\d+\.){4}in-addr.arpa)$/) ) {
254 ($code,$msg) = DNSDB::_zone2cidr($z);
255 # Exact matches only, because we're in a sub-/24 delegation
256##fixme: flag the type of delegation (range, subnet-with-dash, subnet-with-slash)
257# somewhere so we can recover it on export. probably best to do that in the revzone data.
258 ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ("$msg"));
259 $z =~ s/^[\d-]+//;
260 ($code,$msg) = DNSDB::_zone2cidr("$i.$z"); # Get the actual IP and normalize
261 } else {
262 ($code,$msg) = DNSDB::_zone2cidr($rip);
263 ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"));
264 }
265 if ($rparent) {
266 $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl);
267 } else {
268 push @deferred, $rec unless $nodefer;
269 }
270
[348]271 } elsif ($rec =~ /^\+/) {
272 $cnt{A}++;
[354]273
[359]274 my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
275 $host =~ s/^\+//;
276 $host =~ s/\.$//;
[360]277 $host =~ s/^\\052/*/;
[359]278 $ttl = 0 if !$ttl;
279 $stamp = '' if !$stamp;
280 $loc = '' if !$loc;
281 $loc = '' if $loc =~ /^:+$/;
282
283 my $domid = DNSDB::_hostparent($dbh, $host);
284 if ($domid) {
285 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl);
286 } else {
287 push @deferred, $rec unless $nodefer;
288 }
289
[348]290 } elsif ($rec =~ /^Z/) {
291 $cnt{SOA}++;
[354]292
[353]293 my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$stamp,$loc) = split /:/, $rec, 11;
[348]294 $zone =~ s/^Z//;
295 $zone =~ s/\.$//;
296 $master =~ s/\.$//;
297 $contact =~ s/\.$//;
[353]298 $ttl = 0 if !$ttl;
299 $stamp = '' if !$stamp;
[348]300 $loc = '' if !$loc;
[353]301 $loc = '' if $loc =~ /^:+$/;
[348]302 if ($zone =~ /\.arpa$/) {
303 ($code,$msg) = DNSDB::_zone2cidr($zone);
304 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
305 my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
[353]306 $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
[348]307 } else {
308 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($zone));
309 my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
[353]310 $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
[348]311 }
[354]312
[348]313 } elsif ($rec =~ /^\@/) {
314 $cnt{MX}++;
[354]315
[357]316 my ($zone,$ip,$host,$dist,$ttl,$stamp,$loc) = split /:/, $rec, 7;
[359]317 $zone =~ s/^\@//;
[357]318 $zone =~ s/\.$//;
[360]319 $zone =~ s/^\\052/*/;
[357]320 $host =~ s/\.$//;
321 $host = "$host.mx.$zone" if $host !~ /\./;
322 $ttl = 0 if !$ttl;
323 $stamp = '' if !$stamp;
324 $loc = '' if !$loc;
325 $loc = '' if $loc =~ /^:+$/;
326
327# note we don't check for reverse domains here, because MX records don't make any sense in reverse zones.
328# if this really ever becomes an issue for someone it can be expanded to handle those weirdos
329
330 # allow for subzone MXes, since it's perfectly legitimate to simply stuff it all in a single parent zone
331 my $domid = DNSDB::_hostparent($dbh, $zone);
332 if ($domid) {
333 $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl);
334 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl) if $ip;
335 } else {
336 push @deferred, $rec unless $nodefer;
337 }
338
[348]339 } elsif ($rec =~ /^'/) {
340 $cnt{TXT}++;
341
[353]342 my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
[348]343 $fqdn =~ s/^'//;
[360]344 $fqdn =~ s/^\\052/*/;
[348]345 _deoctal(\$rdata);
[353]346 $ttl = 0 if !$ttl;
347 $stamp = '' if !$stamp;
348 $loc = '' if !$loc;
349 $loc = '' if $loc =~ /^:+$/;
[348]350
[360]351 if ($fqdn =~ /\.arpa$/) {
352 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
353 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
354 $recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl);
[348]355 } else {
[360]356 my $domid = DNSDB::_hostparent($dbh, $fqdn);
357 if ($domid) {
358 $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl);
359 } else {
360 push @deferred, $rec unless $nodefer;
361 }
[348]362 }
363
364 } elsif ($rec =~ /^\./) {
365 $cnt{NSASOA}++;
[354]366
[353]367 my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
368 $fqdn =~ s/^\.//;
369 $fqdn =~ s/\.$//;
370 $ns =~ s/\.$//;
371 $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
372 $ttl = 0 if !$ttl;
373 $stamp = '' if !$stamp;
374 $loc = '' if !$loc;
375 $loc = '' if $loc =~ /^:+$/;
376
377 if ($fqdn =~ /\.arpa$/) {
378 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
379 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
380 if (!$rdns) {
381 $errstr = "adding revzone $msg";
382 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
383 ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
384# this would probably make a lot more sense to do hostmaster.$config{admindomain}
385 $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
386 }
387 $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl);
388##fixme: (?) implement full conversion of tinydns . records?
389# -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse
390#$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl)
391# ... auto-A-record simply does not make sense in reverse zones. Functionally
392# I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
393# zones has names for their nameservers already.
394# Even the auto-nameserver-fqdn comes out... ugly.
395
396 } else {
397 my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
398 undef, ($fqdn));
399 if (!$domid) {
400 $errstr = "adding domain $fqdn";
401 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($fqdn));
402 ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
403 $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
404 }
405 $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl);
406 $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl) if $ip;
407 }
408
409
410 } elsif ($rec =~ /^\%/) {
411 $cnt{VIEWS}++;
[354]412
[348]413 } elsif ($rec =~ /^:/) {
414 $cnt{NCUST}++;
415# Big section. Since tinydns can publish anything you can encode properly, but only provides official
416# recognition and handling for the core common types, this must deal with the leftovers.
417# :fqdn:type:rdata:ttl:time:loc
418
[354]419 my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
[360]420 $fqdn =~ s/\.$//;
421 $fqdn =~ s/^\\052/*/;
[354]422 $ttl = 0 if !$ttl;
423 $stamp = '' if !$stamp;
424 $loc = '' if !$loc;
425 $loc = '' if $loc =~ /^:+$/;
[348]426
[354]427 if ($type == 33) {
428 # SRV
429 my ($prio, $weight, $port, $target) = (0,0,0,0);
[348]430
[354]431 my @tmp = _byteparse(\$rdata, 2);
432 $prio = $tmp[0] * 256 + $tmp[1];
433 @tmp = _byteparse(\$rdata, 2);
434 $weight = $tmp[0] * 256 + $tmp[1];
435 @tmp = _byteparse(\$rdata, 2);
436 $port = $tmp[0] * 256 + $tmp[1];
[348]437
[354]438 $rdata =~ s/\\\d{3}/./g;
439 ($target) = ($rdata =~ /^\.(.+)\.$/);
[348]440# hmm. the above *should* work, but What If(TM) we have ASCII-range bytes
441# representing the target's fqdn part length(s)? axfr-get doesn't seem to,
442# probably because dec. 33->63 includes most punctuation and all the numbers
443# while ($rdata =~ /(\\\d{3})/) {
444# my $cnt = $1;
445# $rdata =~ s/^$cnt//;
446# $cnt =~ s/^\\/0/;
447# $cnt = oct($cnt);
448# my ($seg) = ($rdata =~ /^(.{$cnt})/);
449# $target .=
450# }
451
[354]452 my $domid = DNSDB::_hostparent($dbh, $fqdn);
453 if ($domid) {
454 $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl) if $domid;
455 } else {
456 push @deferred, $rec unless $nodefer;
457 }
[348]458
[354]459 } elsif ($type == 28) {
460 # AAAA
461 my @v6;
[348]462
[354]463 for (my $i=0; $i < 8; $i++) {
464 my @tmp = _byteparse(\$rdata, 2);
465 push @v6, sprintf("%0.4x", $tmp[0] * 256 + $tmp[1]);
466 }
467 my $val = NetAddr::IP->new(join(':', @v6));
[348]468
[360]469 my $fparent = DNSDB::_hostparent($dbh, $fqdn);
470 if ($fparent) {
471 $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl);
[354]472 } else {
473 push @deferred, $rec unless $nodefer;
474 }
[348]475
[361]476 } elsif ($type == 16) {
477 # TXT
478 my $txtstring = _rdata2string($rdata);
479
480 if ($fqdn =~ /\.arpa$/) {
481 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
482 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
483 if ($rparent) {
484 $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl);
485 } else {
486 push @deferred, $rec unless $nodefer;
487 }
488 } else {
489 my $domid = DNSDB::_hostparent($dbh, $fqdn);
490 if ($domid) {
491 $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl);
492 } else {
493 push @deferred, $rec unless $nodefer;
494 }
495 }
496
497 } elsif ($type == 17) {
498 # RP
499
500 } elsif ($type == 44) {
501 # SSHFP
502
[354]503 } else {
504 # ... uhhh, dunno
505 }
[348]506
507 } else {
508 $cnt{other}++;
[354]509 print " $_\n";
[348]510 }
511 }
512
513 close FLAT;
514}
Note: See TracBrowser for help on using the repository browser.