source: trunk/tiny-import.pl@ 356

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

/trunk

Checkpoint: tiny-import.pl

  • Add warning to header
  • Remove development print in NS segment
  • Fill in PTR stub (See #26)
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 12.9 KB
Line 
1#!/usr/bin/perl
2# dnsadmin shell-based import tool for tinydns flatfiles
3##
4# $Id: tiny-import.pl 356 2012-06-29 21:48:31Z 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 $ttl = 0 if !$ttl;
189 $stamp = '' if !$stamp;
190 $loc = '' if !$loc;
191 $loc = '' if $loc =~ /^:+$/;
192 if ($zone =~ /\.arpa$/) {
193 ($code,$msg) = DNSDB::_zone2cidr($zone);
194 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >>= ?", undef, ("$msg"));
195##fixme, in concert with the CNAME check for same; automagically
196# create "delegate" record instead for subzone NSes: convert above to use = instead of >>=
197# ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"))
198# if !$rparent;
199 if ($rparent) {
200 $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl);
201 } else {
202 push @deferred, $rec unless $nodefer;
203 }
204 } else {
205 my $fparent = DNSDB::_hostparent($dbh, $zone);
206 if ($fparent) {
207 $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl);
208 } else {
209 push @deferred, $rec unless $nodefer;
210 }
211 }
212
213 } elsif ($rec =~ /^\^/) {
214 $cnt{PTR}++;
215
216 my ($rip,$host,$ttl,$stamp,$loc) = split /:/, $rec, 5;
217 $rip =~ s/^\^//;
218 $rip =~ s/\.$//;
219 $ttl = 0 if !$ttl;
220 $stamp = '' if !$stamp;
221 $loc = '' if !$loc;
222 $loc = '' if $loc =~ /^:+$/;
223 my $rparent;
224 if (my ($i, $z) = ($rip =~ /^(\d+)\.(\d+-(?:\d+\.){4}in-addr.arpa)$/) ) {
225 ($code,$msg) = DNSDB::_zone2cidr($z);
226 # Exact matches only, because we're in a sub-/24 delegation
227##fixme: flag the type of delegation (range, subnet-with-dash, subnet-with-slash)
228# somewhere so we can recover it on export. probably best to do that in the revzone data.
229 ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ("$msg"));
230 $z =~ s/^[\d-]+//;
231 ($code,$msg) = DNSDB::_zone2cidr("$i.$z"); # Get the actual IP and normalize
232 } else {
233 ($code,$msg) = DNSDB::_zone2cidr($rip);
234 ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"));
235 }
236 if ($rparent) {
237 $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl);
238 } else {
239 push @deferred, $rec unless $nodefer;
240 }
241
242 } elsif ($rec =~ /^\+/) {
243 $cnt{A}++;
244
245 } elsif ($rec =~ /^Z/) {
246 $cnt{SOA}++;
247
248 my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$stamp,$loc) = split /:/, $rec, 11;
249 $zone =~ s/^Z//;
250 $zone =~ s/\.$//;
251 $master =~ s/\.$//;
252 $contact =~ s/\.$//;
253 $ttl = 0 if !$ttl;
254 $stamp = '' if !$stamp;
255 $loc = '' if !$loc;
256 $loc = '' if $loc =~ /^:+$/;
257 if ($zone =~ /\.arpa$/) {
258 ($code,$msg) = DNSDB::_zone2cidr($zone);
259 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
260 my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
261 $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
262 } else {
263 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($zone));
264 my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
265 $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
266 }
267
268 } elsif ($rec =~ /^\@/) {
269 $cnt{MX}++;
270
271 } elsif ($rec =~ /^'/) {
272 $cnt{TXT}++;
273
274 my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
275 $fqdn =~ s/^'//;
276 _deoctal(\$rdata);
277 $ttl = 0 if !$ttl;
278 $stamp = '' if !$stamp;
279 $loc = '' if !$loc;
280 $loc = '' if $loc =~ /^:+$/;
281
282 my $domid = DNSDB::_hostparent($dbh, $fqdn);
283 if ($domid) {
284 $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl);
285 } else {
286 push @deferred, $rec unless $nodefer;
287 }
288
289 } elsif ($rec =~ /^\./) {
290 $cnt{NSASOA}++;
291
292 my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
293 $fqdn =~ s/^\.//;
294 $fqdn =~ s/\.$//;
295 $ns =~ s/\.$//;
296 $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
297 $ttl = 0 if !$ttl;
298 $stamp = '' if !$stamp;
299 $loc = '' if !$loc;
300 $loc = '' if $loc =~ /^:+$/;
301
302 if ($fqdn =~ /\.arpa$/) {
303 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
304 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
305 if (!$rdns) {
306 $errstr = "adding revzone $msg";
307 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
308 ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
309# this would probably make a lot more sense to do hostmaster.$config{admindomain}
310 $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
311 }
312 $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl);
313##fixme: (?) implement full conversion of tinydns . records?
314# -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse
315#$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl)
316# ... auto-A-record simply does not make sense in reverse zones. Functionally
317# I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
318# zones has names for their nameservers already.
319# Even the auto-nameserver-fqdn comes out... ugly.
320
321 } else {
322 my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
323 undef, ($fqdn));
324 if (!$domid) {
325 $errstr = "adding domain $fqdn";
326 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($fqdn));
327 ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
328 $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
329 }
330 $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl);
331 $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl) if $ip;
332 }
333
334
335 } elsif ($rec =~ /^\%/) {
336 $cnt{VIEWS}++;
337
338 } elsif ($rec =~ /^:/) {
339 $cnt{NCUST}++;
340# Big section. Since tinydns can publish anything you can encode properly, but only provides official
341# recognition and handling for the core common types, this must deal with the leftovers.
342# :fqdn:type:rdata:ttl:time:loc
343
344 my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
345 $ttl = 0 if !$ttl;
346 $stamp = '' if !$stamp;
347 $loc = '' if !$loc;
348 $loc = '' if $loc =~ /^:+$/;
349
350 if ($type == 33) {
351 # SRV
352 my ($prio, $weight, $port, $target) = (0,0,0,0);
353
354 my @tmp = _byteparse(\$rdata, 2);
355 $prio = $tmp[0] * 256 + $tmp[1];
356 @tmp = _byteparse(\$rdata, 2);
357 $weight = $tmp[0] * 256 + $tmp[1];
358 @tmp = _byteparse(\$rdata, 2);
359 $port = $tmp[0] * 256 + $tmp[1];
360
361 $rdata =~ s/\\\d{3}/./g;
362 ($target) = ($rdata =~ /^\.(.+)\.$/);
363# hmm. the above *should* work, but What If(TM) we have ASCII-range bytes
364# representing the target's fqdn part length(s)? axfr-get doesn't seem to,
365# probably because dec. 33->63 includes most punctuation and all the numbers
366# while ($rdata =~ /(\\\d{3})/) {
367# my $cnt = $1;
368# $rdata =~ s/^$cnt//;
369# $cnt =~ s/^\\/0/;
370# $cnt = oct($cnt);
371# my ($seg) = ($rdata =~ /^(.{$cnt})/);
372# $target .=
373# }
374
375 my $domid = DNSDB::_hostparent($dbh, $fqdn);
376 if ($domid) {
377 $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl) if $domid;
378 } else {
379 push @deferred, $rec unless $nodefer;
380 }
381
382 } elsif ($type == 28) {
383 # AAAA
384 my @v6;
385
386 for (my $i=0; $i < 8; $i++) {
387 my @tmp = _byteparse(\$rdata, 2);
388 push @v6, sprintf("%0.4x", $tmp[0] * 256 + $tmp[1]);
389 }
390 my $val = NetAddr::IP->new(join(':', @v6));
391
392 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$val"));
393 if ($rdns) {
394 $recsth->execute(0, $rdns, $fqdn, 28, $val->addr, 0, 0, 0, $ttl);
395 } else {
396 push @deferred, $rec unless $nodefer;
397 }
398
399 } else {
400 # ... uhhh, dunno
401 }
402
403 } else {
404 $cnt{other}++;
405 print " $_\n";
406 }
407 }
408
409 close FLAT;
410}
Note: See TracBrowser for help on using the repository browser.