source: trunk/tiny-import.pl@ 359

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

/trunk

Checkpoint, tiny-import.pl

  • Fill in A record handling
  • Fix buglet in MX handling (didn't strip the right leading character)
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 14.3 KB
Line 
1#!/usr/bin/perl
2# dnsadmin shell-based import tool for tinydns flatfiles
3##
4# $Id: tiny-import.pl 359 2012-07-06 17:21:35Z 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 my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
249 $host =~ s/^\+//;
250 $host =~ s/\.$//;
251 $ttl = 0 if !$ttl;
252 $stamp = '' if !$stamp;
253 $loc = '' if !$loc;
254 $loc = '' if $loc =~ /^:+$/;
255
256 my $domid = DNSDB::_hostparent($dbh, $host);
257 if ($domid) {
258 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl);
259 } else {
260 push @deferred, $rec unless $nodefer;
261 }
262
263 } elsif ($rec =~ /^Z/) {
264 $cnt{SOA}++;
265
266 my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$stamp,$loc) = split /:/, $rec, 11;
267 $zone =~ s/^Z//;
268 $zone =~ s/\.$//;
269 $master =~ s/\.$//;
270 $contact =~ s/\.$//;
271 $ttl = 0 if !$ttl;
272 $stamp = '' if !$stamp;
273 $loc = '' if !$loc;
274 $loc = '' if $loc =~ /^:+$/;
275 if ($zone =~ /\.arpa$/) {
276 ($code,$msg) = DNSDB::_zone2cidr($zone);
277 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
278 my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
279 $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
280 } else {
281 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($zone));
282 my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
283 $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
284 }
285
286 } elsif ($rec =~ /^\@/) {
287 $cnt{MX}++;
288
289 my ($zone,$ip,$host,$dist,$ttl,$stamp,$loc) = split /:/, $rec, 7;
290 $zone =~ s/^\@//;
291 $zone =~ s/\.$//;
292 $host =~ s/\.$//;
293 $host = "$host.mx.$zone" if $host !~ /\./;
294 $ttl = 0 if !$ttl;
295 $stamp = '' if !$stamp;
296 $loc = '' if !$loc;
297 $loc = '' if $loc =~ /^:+$/;
298
299# note we don't check for reverse domains here, because MX records don't make any sense in reverse zones.
300# if this really ever becomes an issue for someone it can be expanded to handle those weirdos
301
302 # allow for subzone MXes, since it's perfectly legitimate to simply stuff it all in a single parent zone
303 my $domid = DNSDB::_hostparent($dbh, $zone);
304 if ($domid) {
305 $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl);
306 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl) if $ip;
307 } else {
308 push @deferred, $rec unless $nodefer;
309 }
310
311 } elsif ($rec =~ /^'/) {
312 $cnt{TXT}++;
313
314 my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
315 $fqdn =~ s/^'//;
316 _deoctal(\$rdata);
317 $ttl = 0 if !$ttl;
318 $stamp = '' if !$stamp;
319 $loc = '' if !$loc;
320 $loc = '' if $loc =~ /^:+$/;
321
322 my $domid = DNSDB::_hostparent($dbh, $fqdn);
323 if ($domid) {
324 $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl);
325 } else {
326 push @deferred, $rec unless $nodefer;
327 }
328
329 } elsif ($rec =~ /^\./) {
330 $cnt{NSASOA}++;
331
332 my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
333 $fqdn =~ s/^\.//;
334 $fqdn =~ s/\.$//;
335 $ns =~ s/\.$//;
336 $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
337 $ttl = 0 if !$ttl;
338 $stamp = '' if !$stamp;
339 $loc = '' if !$loc;
340 $loc = '' if $loc =~ /^:+$/;
341
342 if ($fqdn =~ /\.arpa$/) {
343 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
344 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
345 if (!$rdns) {
346 $errstr = "adding revzone $msg";
347 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
348 ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
349# this would probably make a lot more sense to do hostmaster.$config{admindomain}
350 $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
351 }
352 $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl);
353##fixme: (?) implement full conversion of tinydns . records?
354# -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse
355#$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl)
356# ... auto-A-record simply does not make sense in reverse zones. Functionally
357# I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
358# zones has names for their nameservers already.
359# Even the auto-nameserver-fqdn comes out... ugly.
360
361 } else {
362 my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
363 undef, ($fqdn));
364 if (!$domid) {
365 $errstr = "adding domain $fqdn";
366 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($fqdn));
367 ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
368 $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
369 }
370 $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl);
371 $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl) if $ip;
372 }
373
374
375 } elsif ($rec =~ /^\%/) {
376 $cnt{VIEWS}++;
377
378 } elsif ($rec =~ /^:/) {
379 $cnt{NCUST}++;
380# Big section. Since tinydns can publish anything you can encode properly, but only provides official
381# recognition and handling for the core common types, this must deal with the leftovers.
382# :fqdn:type:rdata:ttl:time:loc
383
384 my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
385 $ttl = 0 if !$ttl;
386 $stamp = '' if !$stamp;
387 $loc = '' if !$loc;
388 $loc = '' if $loc =~ /^:+$/;
389
390 if ($type == 33) {
391 # SRV
392 my ($prio, $weight, $port, $target) = (0,0,0,0);
393
394 my @tmp = _byteparse(\$rdata, 2);
395 $prio = $tmp[0] * 256 + $tmp[1];
396 @tmp = _byteparse(\$rdata, 2);
397 $weight = $tmp[0] * 256 + $tmp[1];
398 @tmp = _byteparse(\$rdata, 2);
399 $port = $tmp[0] * 256 + $tmp[1];
400
401 $rdata =~ s/\\\d{3}/./g;
402 ($target) = ($rdata =~ /^\.(.+)\.$/);
403# hmm. the above *should* work, but What If(TM) we have ASCII-range bytes
404# representing the target's fqdn part length(s)? axfr-get doesn't seem to,
405# probably because dec. 33->63 includes most punctuation and all the numbers
406# while ($rdata =~ /(\\\d{3})/) {
407# my $cnt = $1;
408# $rdata =~ s/^$cnt//;
409# $cnt =~ s/^\\/0/;
410# $cnt = oct($cnt);
411# my ($seg) = ($rdata =~ /^(.{$cnt})/);
412# $target .=
413# }
414
415 my $domid = DNSDB::_hostparent($dbh, $fqdn);
416 if ($domid) {
417 $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl) if $domid;
418 } else {
419 push @deferred, $rec unless $nodefer;
420 }
421
422 } elsif ($type == 28) {
423 # AAAA
424 my @v6;
425
426 for (my $i=0; $i < 8; $i++) {
427 my @tmp = _byteparse(\$rdata, 2);
428 push @v6, sprintf("%0.4x", $tmp[0] * 256 + $tmp[1]);
429 }
430 my $val = NetAddr::IP->new(join(':', @v6));
431
432 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$val"));
433 if ($rdns) {
434 $recsth->execute(0, $rdns, $fqdn, 28, $val->addr, 0, 0, 0, $ttl);
435 } else {
436 push @deferred, $rec unless $nodefer;
437 }
438
439 } else {
440 # ... uhhh, dunno
441 }
442
443 } else {
444 $cnt{other}++;
445 print " $_\n";
446 }
447 }
448
449 close FLAT;
450}
Note: See TracBrowser for help on using the repository browser.