source: trunk/tiny-import.pl@ 358

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

/trunk

Minor bugfix that slipped through adding the NS hostname expansion in r357

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 13.9 KB
Line 
1#!/usr/bin/perl
2# dnsadmin shell-based import tool for tinydns flatfiles
3##
4# $Id: tiny-import.pl 358 2012-07-05 21:52:39Z kdeugau $
5# Copyright 2012 Kris Deugau <kdeugau@deepnet.cx>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19##
20
21# WARNING: This is NOT a heavy-duty validator; it is assumed that the data
22# being imported is more or less sane. Only minor structural validation will
23# be done to weed out the most broken records.
24
25use strict;
26use warnings;
27
28use lib '.';
29use DNSDB qw(:ALL);
30
31if (!loadConfig()) {
32 warn "Using default configuration; unable to load custom settings: $DNSDB::errstr";
33}
34
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;
44my $errstr = '';
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";
55die "die harder: $errstr\n";
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
73 my %deleg;
74
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
88print scalar(@deferred)." deferred records in $flatfile\n";
89
90 # Sub for various nonstandard types with lots of pure bytes expressed in octal
91 # Takes a tinydns rdata string and count, returns a list of $count bytes as well
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
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
123 sub recslurp {
124 my $rec = shift;
125 my $nodefer = shift || 0;
126
127 if ($rec =~ /^=/) {
128 $cnt{APTR}++;
129
130##fixme: do checks like this for all types
131 if ($rec !~ /^=(?:\*|\\052)?[a-z0-9\._-]+:[\d\.]+:\d*/i) {
132 print "bad A+PTR $rec\n";
133 return;
134 }
135 my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
136 $host =~ s/^=//;
137 $host =~ s/\.$//;
138 $ttl = 0 if !$ttl;
139 $stamp = '' if !$stamp;
140 $loc = '' if !$loc;
141 $loc = '' if $loc =~ /^:+$/;
142 my $fparent = DNSDB::_hostparent($dbh, $host);
143 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($ip));
144 if ($fparent && $rparent) {
145 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl);
146 } else {
147 push @deferred, $rec unless $nodefer;
148 # print "$tmporig deferred; can't find both forward and reverse zone parents\n";
149 }
150
151 } elsif ($rec =~ /^C/) {
152 $cnt{CNAME}++;
153
154 my ($host,$targ,$ttl,$stamp,$loc) = split /:/, $rec, 5;
155 $host =~ s/^C//;
156 $host =~ s/\.$//;
157 $ttl = 0 if !$ttl;
158 $stamp = '' if !$stamp;
159 $loc = '' if !$loc;
160 $loc = '' if $loc =~ /^:+$/;
161 if ($host =~ /\.arpa$/) {
162 ($code,$msg) = DNSDB::_zone2cidr($host);
163 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
164 $recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl);
165
166##fixme: automagically convert manually maintained sub-/24 delegations
167# my ($subip, $zone) = split /\./, $targ, 2;
168# ($code, $msg) = DNSDB::_zone2cidr($zone);
169# push @{$deleg{"$msg"}{iplist}}, $subip;
170#print "$msg $subip\n";
171
172 } else {
173 my $fparent = DNSDB::_hostparent($dbh, $host);
174 if ($fparent) {
175 $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl);
176 } else {
177 push @deferred, $rec unless $nodefer;
178 # print "$tmporig deferred; can't find parent zone\n";
179 }
180 }
181
182 } elsif ($rec =~ /^\&/) {
183 $cnt{NS}++;
184
185 my ($zone,$ip,$ns,$ttl,$stamp,$loc) = split /:/, $rec, 6;
186 $zone =~ s/^\&//;
187 $zone =~ s/\.$//;
188 $ns =~ s/\.$//;
189 $ns = "$ns.ns.$zone" if $ns !~ /\./;
190 $ttl = 0 if !$ttl;
191 $stamp = '' if !$stamp;
192 $loc = '' if !$loc;
193 $loc = '' if $loc =~ /^:+$/;
194 if ($zone =~ /\.arpa$/) {
195 ($code,$msg) = DNSDB::_zone2cidr($zone);
196 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >>= ?", undef, ("$msg"));
197##fixme, in concert with the CNAME check for same; automagically
198# create "delegate" record instead for subzone NSes: convert above to use = instead of >>=
199# ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"))
200# if !$rparent;
201 if ($rparent) {
202 $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl);
203 } else {
204 push @deferred, $rec unless $nodefer;
205 }
206 } else {
207 my $fparent = DNSDB::_hostparent($dbh, $zone);
208 if ($fparent) {
209 $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl);
210 $recsth->execute($fparent, 0, $ns, 2, $ip, 0, 0, 0, $ttl) if $ip;
211 } else {
212 push @deferred, $rec unless $nodefer;
213 }
214 }
215
216 } elsif ($rec =~ /^\^/) {
217 $cnt{PTR}++;
218
219 my ($rip,$host,$ttl,$stamp,$loc) = split /:/, $rec, 5;
220 $rip =~ s/^\^//;
221 $rip =~ s/\.$//;
222 $ttl = 0 if !$ttl;
223 $stamp = '' if !$stamp;
224 $loc = '' if !$loc;
225 $loc = '' if $loc =~ /^:+$/;
226 my $rparent;
227 if (my ($i, $z) = ($rip =~ /^(\d+)\.(\d+-(?:\d+\.){4}in-addr.arpa)$/) ) {
228 ($code,$msg) = DNSDB::_zone2cidr($z);
229 # Exact matches only, because we're in a sub-/24 delegation
230##fixme: flag the type of delegation (range, subnet-with-dash, subnet-with-slash)
231# somewhere so we can recover it on export. probably best to do that in the revzone data.
232 ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ("$msg"));
233 $z =~ s/^[\d-]+//;
234 ($code,$msg) = DNSDB::_zone2cidr("$i.$z"); # Get the actual IP and normalize
235 } else {
236 ($code,$msg) = DNSDB::_zone2cidr($rip);
237 ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"));
238 }
239 if ($rparent) {
240 $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl);
241 } else {
242 push @deferred, $rec unless $nodefer;
243 }
244
245 } elsif ($rec =~ /^\+/) {
246 $cnt{A}++;
247
248 } elsif ($rec =~ /^Z/) {
249 $cnt{SOA}++;
250
251 my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$stamp,$loc) = split /:/, $rec, 11;
252 $zone =~ s/^Z//;
253 $zone =~ s/\.$//;
254 $master =~ s/\.$//;
255 $contact =~ s/\.$//;
256 $ttl = 0 if !$ttl;
257 $stamp = '' if !$stamp;
258 $loc = '' if !$loc;
259 $loc = '' if $loc =~ /^:+$/;
260 if ($zone =~ /\.arpa$/) {
261 ($code,$msg) = DNSDB::_zone2cidr($zone);
262 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
263 my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
264 $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
265 } else {
266 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($zone));
267 my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
268 $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
269 }
270
271 } elsif ($rec =~ /^\@/) {
272 $cnt{MX}++;
273
274 my ($zone,$ip,$host,$dist,$ttl,$stamp,$loc) = split /:/, $rec, 7;
275 $zone =~ s/^\.//;
276 $zone =~ s/\.$//;
277 $host =~ s/\.$//;
278 $host = "$host.mx.$zone" if $host !~ /\./;
279 $ttl = 0 if !$ttl;
280 $stamp = '' if !$stamp;
281 $loc = '' if !$loc;
282 $loc = '' if $loc =~ /^:+$/;
283
284# note we don't check for reverse domains here, because MX records don't make any sense in reverse zones.
285# if this really ever becomes an issue for someone it can be expanded to handle those weirdos
286
287 # allow for subzone MXes, since it's perfectly legitimate to simply stuff it all in a single parent zone
288 my $domid = DNSDB::_hostparent($dbh, $zone);
289 if ($domid) {
290 $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl);
291 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl) if $ip;
292 } else {
293 push @deferred, $rec unless $nodefer;
294 }
295
296 } elsif ($rec =~ /^'/) {
297 $cnt{TXT}++;
298
299 my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
300 $fqdn =~ s/^'//;
301 _deoctal(\$rdata);
302 $ttl = 0 if !$ttl;
303 $stamp = '' if !$stamp;
304 $loc = '' if !$loc;
305 $loc = '' if $loc =~ /^:+$/;
306
307 my $domid = DNSDB::_hostparent($dbh, $fqdn);
308 if ($domid) {
309 $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl);
310 } else {
311 push @deferred, $rec unless $nodefer;
312 }
313
314 } elsif ($rec =~ /^\./) {
315 $cnt{NSASOA}++;
316
317 my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
318 $fqdn =~ s/^\.//;
319 $fqdn =~ s/\.$//;
320 $ns =~ s/\.$//;
321 $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
322 $ttl = 0 if !$ttl;
323 $stamp = '' if !$stamp;
324 $loc = '' if !$loc;
325 $loc = '' if $loc =~ /^:+$/;
326
327 if ($fqdn =~ /\.arpa$/) {
328 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
329 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
330 if (!$rdns) {
331 $errstr = "adding revzone $msg";
332 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
333 ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
334# this would probably make a lot more sense to do hostmaster.$config{admindomain}
335 $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
336 }
337 $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl);
338##fixme: (?) implement full conversion of tinydns . records?
339# -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse
340#$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl)
341# ... auto-A-record simply does not make sense in reverse zones. Functionally
342# I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
343# zones has names for their nameservers already.
344# Even the auto-nameserver-fqdn comes out... ugly.
345
346 } else {
347 my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
348 undef, ($fqdn));
349 if (!$domid) {
350 $errstr = "adding domain $fqdn";
351 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($fqdn));
352 ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
353 $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
354 }
355 $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl);
356 $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl) if $ip;
357 }
358
359
360 } elsif ($rec =~ /^\%/) {
361 $cnt{VIEWS}++;
362
363 } elsif ($rec =~ /^:/) {
364 $cnt{NCUST}++;
365# Big section. Since tinydns can publish anything you can encode properly, but only provides official
366# recognition and handling for the core common types, this must deal with the leftovers.
367# :fqdn:type:rdata:ttl:time:loc
368
369 my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
370 $ttl = 0 if !$ttl;
371 $stamp = '' if !$stamp;
372 $loc = '' if !$loc;
373 $loc = '' if $loc =~ /^:+$/;
374
375 if ($type == 33) {
376 # SRV
377 my ($prio, $weight, $port, $target) = (0,0,0,0);
378
379 my @tmp = _byteparse(\$rdata, 2);
380 $prio = $tmp[0] * 256 + $tmp[1];
381 @tmp = _byteparse(\$rdata, 2);
382 $weight = $tmp[0] * 256 + $tmp[1];
383 @tmp = _byteparse(\$rdata, 2);
384 $port = $tmp[0] * 256 + $tmp[1];
385
386 $rdata =~ s/\\\d{3}/./g;
387 ($target) = ($rdata =~ /^\.(.+)\.$/);
388# hmm. the above *should* work, but What If(TM) we have ASCII-range bytes
389# representing the target's fqdn part length(s)? axfr-get doesn't seem to,
390# probably because dec. 33->63 includes most punctuation and all the numbers
391# while ($rdata =~ /(\\\d{3})/) {
392# my $cnt = $1;
393# $rdata =~ s/^$cnt//;
394# $cnt =~ s/^\\/0/;
395# $cnt = oct($cnt);
396# my ($seg) = ($rdata =~ /^(.{$cnt})/);
397# $target .=
398# }
399
400 my $domid = DNSDB::_hostparent($dbh, $fqdn);
401 if ($domid) {
402 $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl) if $domid;
403 } else {
404 push @deferred, $rec unless $nodefer;
405 }
406
407 } elsif ($type == 28) {
408 # AAAA
409 my @v6;
410
411 for (my $i=0; $i < 8; $i++) {
412 my @tmp = _byteparse(\$rdata, 2);
413 push @v6, sprintf("%0.4x", $tmp[0] * 256 + $tmp[1]);
414 }
415 my $val = NetAddr::IP->new(join(':', @v6));
416
417 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$val"));
418 if ($rdns) {
419 $recsth->execute(0, $rdns, $fqdn, 28, $val->addr, 0, 0, 0, $ttl);
420 } else {
421 push @deferred, $rec unless $nodefer;
422 }
423
424 } else {
425 # ... uhhh, dunno
426 }
427
428 } else {
429 $cnt{other}++;
430 print " $_\n";
431 }
432 }
433
434 close FLAT;
435}
Note: See TracBrowser for help on using the repository browser.