1 | #!/usr/bin/perl
|
---|
2 | # dnsadmin shell-based import tool for tinydns flatfiles
|
---|
3 | ##
|
---|
4 | # $Id: tiny-import.pl 363 2012-07-11 17:35: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 |
|
---|
25 | use strict;
|
---|
26 | use warnings;
|
---|
27 |
|
---|
28 | use lib '.';
|
---|
29 | use DNSDB qw(:ALL);
|
---|
30 |
|
---|
31 | if (!loadConfig()) {
|
---|
32 | warn "Using default configuration; unable to load custom settings: $DNSDB::errstr";
|
---|
33 | }
|
---|
34 |
|
---|
35 | my $code;
|
---|
36 | my ($dbh,$msg) = connectDB($config{dbname}, $config{dbuser}, $config{dbpass}, $config{dbhost});
|
---|
37 | initGlobals($dbh) if $dbh;
|
---|
38 |
|
---|
39 | $dbh->{AutoCommit} = 0;
|
---|
40 | $dbh->{RaiseError} = 1;
|
---|
41 |
|
---|
42 | my %cnt;
|
---|
43 | my @deferred;
|
---|
44 | my $errstr = '';
|
---|
45 |
|
---|
46 | foreach 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";
|
---|
55 | die "die harder: $errstr\n";
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | foreach (keys %cnt) {
|
---|
60 | print " $_ $cnt{$_}\n";
|
---|
61 | }
|
---|
62 |
|
---|
63 | exit 0;
|
---|
64 |
|
---|
65 | sub 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 |
|
---|
88 | print 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 _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 | sub _rdata2hex {
|
---|
146 | my $rdata = shift;
|
---|
147 | my $tmpout = '';
|
---|
148 | while ($rdata) {
|
---|
149 | my $byte = '';
|
---|
150 | if ($rdata =~ /^\\/) {
|
---|
151 | ($byte) = ($rdata =~ /^(\\\d{3})/);
|
---|
152 | $byte =~ s/\\/0/;
|
---|
153 | $tmpout .= sprintf("%0.2x", oct($byte));
|
---|
154 | $rdata =~ s/^\\\d{3}//;
|
---|
155 | } else {
|
---|
156 | ($byte) = ($rdata =~ /^(.)/);
|
---|
157 | $tmpout .= sprintf("%0.2x", ord($byte));
|
---|
158 | $rdata =~ s/^.//;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return $tmpout;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | sub recslurp {
|
---|
166 | my $rec = shift;
|
---|
167 | my $nodefer = shift || 0;
|
---|
168 |
|
---|
169 | $errstr = $rec; # this way at least we have some idea what went <splat>
|
---|
170 |
|
---|
171 | if ($rec =~ /^=/) {
|
---|
172 | $cnt{APTR}++;
|
---|
173 |
|
---|
174 | ##fixme: do checks like this for all types
|
---|
175 | if ($rec !~ /^=(?:\*|\\052)?[a-z0-9\._-]+:[\d\.]+:\d*/i) {
|
---|
176 | print "bad A+PTR $rec\n";
|
---|
177 | return;
|
---|
178 | }
|
---|
179 | my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
|
---|
180 | $host =~ s/^=//;
|
---|
181 | $host =~ s/\.$//;
|
---|
182 | $ttl = 0 if !$ttl;
|
---|
183 | $stamp = '' if !$stamp;
|
---|
184 | $loc = '' if !$loc;
|
---|
185 | $loc = '' if $loc =~ /^:+$/;
|
---|
186 | my $fparent = DNSDB::_hostparent($dbh, $host);
|
---|
187 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($ip));
|
---|
188 | if ($fparent && $rparent) {
|
---|
189 | $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl);
|
---|
190 | } else {
|
---|
191 | push @deferred, $rec unless $nodefer;
|
---|
192 | # print "$tmporig deferred; can't find both forward and reverse zone parents\n";
|
---|
193 | }
|
---|
194 |
|
---|
195 | } elsif ($rec =~ /^C/) {
|
---|
196 | $cnt{CNAME}++;
|
---|
197 |
|
---|
198 | my ($host,$targ,$ttl,$stamp,$loc) = split /:/, $rec, 5;
|
---|
199 | $host =~ s/^C//;
|
---|
200 | $host =~ s/\.$//;
|
---|
201 | $host =~ s/^\\052/*/;
|
---|
202 | $ttl = 0 if !$ttl;
|
---|
203 | $stamp = '' if !$stamp;
|
---|
204 | $loc = '' if !$loc;
|
---|
205 | $loc = '' if $loc =~ /^:+$/;
|
---|
206 | if ($host =~ /\.arpa$/) {
|
---|
207 | ($code,$msg) = DNSDB::_zone2cidr($host);
|
---|
208 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
|
---|
209 | $recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl);
|
---|
210 |
|
---|
211 | ##fixme: automagically convert manually maintained sub-/24 delegations
|
---|
212 | # my ($subip, $zone) = split /\./, $targ, 2;
|
---|
213 | # ($code, $msg) = DNSDB::_zone2cidr($zone);
|
---|
214 | # push @{$deleg{"$msg"}{iplist}}, $subip;
|
---|
215 | #print "$msg $subip\n";
|
---|
216 |
|
---|
217 | } else {
|
---|
218 | my $fparent = DNSDB::_hostparent($dbh, $host);
|
---|
219 | if ($fparent) {
|
---|
220 | $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl);
|
---|
221 | } else {
|
---|
222 | push @deferred, $rec unless $nodefer;
|
---|
223 | # print "$tmporig deferred; can't find parent zone\n";
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | } elsif ($rec =~ /^\&/) {
|
---|
228 | $cnt{NS}++;
|
---|
229 |
|
---|
230 | my ($zone,$ip,$ns,$ttl,$stamp,$loc) = split /:/, $rec, 6;
|
---|
231 | $zone =~ s/^\&//;
|
---|
232 | $zone =~ s/\.$//;
|
---|
233 | $ns =~ s/\.$//;
|
---|
234 | $ns = "$ns.ns.$zone" if $ns !~ /\./;
|
---|
235 | $ttl = 0 if !$ttl;
|
---|
236 | $stamp = '' if !$stamp;
|
---|
237 | $loc = '' if !$loc;
|
---|
238 | $loc = '' if $loc =~ /^:+$/;
|
---|
239 | if ($zone =~ /\.arpa$/) {
|
---|
240 | ($code,$msg) = DNSDB::_zone2cidr($zone);
|
---|
241 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >>= ?", undef, ("$msg"));
|
---|
242 | ##fixme, in concert with the CNAME check for same; automagically
|
---|
243 | # create "delegate" record instead for subzone NSes: convert above to use = instead of >>=
|
---|
244 | # ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"))
|
---|
245 | # if !$rparent;
|
---|
246 | if ($rparent) {
|
---|
247 | $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl);
|
---|
248 | } else {
|
---|
249 | push @deferred, $rec unless $nodefer;
|
---|
250 | }
|
---|
251 | } else {
|
---|
252 | my $fparent = DNSDB::_hostparent($dbh, $zone);
|
---|
253 | if ($fparent) {
|
---|
254 | $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl);
|
---|
255 | $recsth->execute($fparent, 0, $ns, 2, $ip, 0, 0, 0, $ttl) if $ip;
|
---|
256 | } else {
|
---|
257 | push @deferred, $rec unless $nodefer;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | } elsif ($rec =~ /^\^/) {
|
---|
262 | $cnt{PTR}++;
|
---|
263 |
|
---|
264 | my ($rip,$host,$ttl,$stamp,$loc) = split /:/, $rec, 5;
|
---|
265 | $rip =~ s/^\^//;
|
---|
266 | $rip =~ s/\.$//;
|
---|
267 | $ttl = 0 if !$ttl;
|
---|
268 | $stamp = '' if !$stamp;
|
---|
269 | $loc = '' if !$loc;
|
---|
270 | $loc = '' if $loc =~ /^:+$/;
|
---|
271 | my $rparent;
|
---|
272 | if (my ($i, $z) = ($rip =~ /^(\d+)\.(\d+-(?:\d+\.){4}in-addr.arpa)$/) ) {
|
---|
273 | ($code,$msg) = DNSDB::_zone2cidr($z);
|
---|
274 | # Exact matches only, because we're in a sub-/24 delegation
|
---|
275 | ##fixme: flag the type of delegation (range, subnet-with-dash, subnet-with-slash)
|
---|
276 | # somewhere so we can recover it on export. probably best to do that in the revzone data.
|
---|
277 | ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ("$msg"));
|
---|
278 | $z =~ s/^[\d-]+//;
|
---|
279 | ($code,$msg) = DNSDB::_zone2cidr("$i.$z"); # Get the actual IP and normalize
|
---|
280 | } else {
|
---|
281 | ($code,$msg) = DNSDB::_zone2cidr($rip);
|
---|
282 | ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"));
|
---|
283 | }
|
---|
284 | if ($rparent) {
|
---|
285 | $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl);
|
---|
286 | } else {
|
---|
287 | push @deferred, $rec unless $nodefer;
|
---|
288 | }
|
---|
289 |
|
---|
290 | } elsif ($rec =~ /^\+/) {
|
---|
291 | $cnt{A}++;
|
---|
292 |
|
---|
293 | my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
|
---|
294 | $host =~ s/^\+//;
|
---|
295 | $host =~ s/\.$//;
|
---|
296 | $host =~ s/^\\052/*/;
|
---|
297 | $ttl = 0 if !$ttl;
|
---|
298 | $stamp = '' if !$stamp;
|
---|
299 | $loc = '' if !$loc;
|
---|
300 | $loc = '' if $loc =~ /^:+$/;
|
---|
301 |
|
---|
302 | my $domid = DNSDB::_hostparent($dbh, $host);
|
---|
303 | if ($domid) {
|
---|
304 | $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl);
|
---|
305 | } else {
|
---|
306 | push @deferred, $rec unless $nodefer;
|
---|
307 | }
|
---|
308 |
|
---|
309 | } elsif ($rec =~ /^Z/) {
|
---|
310 | $cnt{SOA}++;
|
---|
311 |
|
---|
312 | my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$stamp,$loc) = split /:/, $rec, 11;
|
---|
313 | $zone =~ s/^Z//;
|
---|
314 | $zone =~ s/\.$//;
|
---|
315 | $master =~ s/\.$//;
|
---|
316 | $contact =~ s/\.$//;
|
---|
317 | $ttl = 0 if !$ttl;
|
---|
318 | $stamp = '' if !$stamp;
|
---|
319 | $loc = '' if !$loc;
|
---|
320 | $loc = '' if $loc =~ /^:+$/;
|
---|
321 | if ($zone =~ /\.arpa$/) {
|
---|
322 | ($code,$msg) = DNSDB::_zone2cidr($zone);
|
---|
323 | $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
|
---|
324 | my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
|
---|
325 | $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
|
---|
326 | } else {
|
---|
327 | $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($zone));
|
---|
328 | my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
|
---|
329 | $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
|
---|
330 | }
|
---|
331 |
|
---|
332 | } elsif ($rec =~ /^\@/) {
|
---|
333 | $cnt{MX}++;
|
---|
334 |
|
---|
335 | my ($zone,$ip,$host,$dist,$ttl,$stamp,$loc) = split /:/, $rec, 7;
|
---|
336 | $zone =~ s/^\@//;
|
---|
337 | $zone =~ s/\.$//;
|
---|
338 | $zone =~ s/^\\052/*/;
|
---|
339 | $host =~ s/\.$//;
|
---|
340 | $host = "$host.mx.$zone" if $host !~ /\./;
|
---|
341 | $ttl = 0 if !$ttl;
|
---|
342 | $stamp = '' if !$stamp;
|
---|
343 | $loc = '' if !$loc;
|
---|
344 | $loc = '' if $loc =~ /^:+$/;
|
---|
345 |
|
---|
346 | # note we don't check for reverse domains here, because MX records don't make any sense in reverse zones.
|
---|
347 | # if this really ever becomes an issue for someone it can be expanded to handle those weirdos
|
---|
348 |
|
---|
349 | # allow for subzone MXes, since it's perfectly legitimate to simply stuff it all in a single parent zone
|
---|
350 | my $domid = DNSDB::_hostparent($dbh, $zone);
|
---|
351 | if ($domid) {
|
---|
352 | $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl);
|
---|
353 | $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl) if $ip;
|
---|
354 | } else {
|
---|
355 | push @deferred, $rec unless $nodefer;
|
---|
356 | }
|
---|
357 |
|
---|
358 | } elsif ($rec =~ /^'/) {
|
---|
359 | $cnt{TXT}++;
|
---|
360 |
|
---|
361 | my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
|
---|
362 | $fqdn =~ s/^'//;
|
---|
363 | $fqdn =~ s/^\\052/*/;
|
---|
364 | _deoctal(\$rdata);
|
---|
365 | $ttl = 0 if !$ttl;
|
---|
366 | $stamp = '' if !$stamp;
|
---|
367 | $loc = '' if !$loc;
|
---|
368 | $loc = '' if $loc =~ /^:+$/;
|
---|
369 |
|
---|
370 | if ($fqdn =~ /\.arpa$/) {
|
---|
371 | ($code,$msg) = DNSDB::_zone2cidr($fqdn);
|
---|
372 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
|
---|
373 | $recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl);
|
---|
374 | } else {
|
---|
375 | my $domid = DNSDB::_hostparent($dbh, $fqdn);
|
---|
376 | if ($domid) {
|
---|
377 | $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl);
|
---|
378 | } else {
|
---|
379 | push @deferred, $rec unless $nodefer;
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | } elsif ($rec =~ /^\./) {
|
---|
384 | $cnt{NSASOA}++;
|
---|
385 |
|
---|
386 | my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
|
---|
387 | $fqdn =~ s/^\.//;
|
---|
388 | $fqdn =~ s/\.$//;
|
---|
389 | $ns =~ s/\.$//;
|
---|
390 | $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
|
---|
391 | $ttl = 0 if !$ttl;
|
---|
392 | $stamp = '' if !$stamp;
|
---|
393 | $loc = '' if !$loc;
|
---|
394 | $loc = '' if $loc =~ /^:+$/;
|
---|
395 |
|
---|
396 | if ($fqdn =~ /\.arpa$/) {
|
---|
397 | ($code,$msg) = DNSDB::_zone2cidr($fqdn);
|
---|
398 | my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
|
---|
399 | if (!$rdns) {
|
---|
400 | $errstr = "adding revzone $msg";
|
---|
401 | $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
|
---|
402 | ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
|
---|
403 | # this would probably make a lot more sense to do hostmaster.$config{admindomain}
|
---|
404 | $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
|
---|
405 | }
|
---|
406 | $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl);
|
---|
407 | ##fixme: (?) implement full conversion of tinydns . records?
|
---|
408 | # -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse
|
---|
409 | #$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl)
|
---|
410 | # ... auto-A-record simply does not make sense in reverse zones. Functionally
|
---|
411 | # I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
|
---|
412 | # zones has names for their nameservers already.
|
---|
413 | # Even the auto-nameserver-fqdn comes out... ugly.
|
---|
414 |
|
---|
415 | } else {
|
---|
416 | my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
|
---|
417 | undef, ($fqdn));
|
---|
418 | if (!$domid) {
|
---|
419 | $errstr = "adding domain $fqdn";
|
---|
420 | $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($fqdn));
|
---|
421 | ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
|
---|
422 | $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
|
---|
423 | }
|
---|
424 | $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl);
|
---|
425 | $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl) if $ip;
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | } elsif ($rec =~ /^\%/) {
|
---|
430 | $cnt{VIEWS}++;
|
---|
431 |
|
---|
432 | } elsif ($rec =~ /^:/) {
|
---|
433 | $cnt{NCUST}++;
|
---|
434 | # Big section. Since tinydns can publish anything you can encode properly, but only provides official
|
---|
435 | # recognition and handling for the core common types, this must deal with the leftovers.
|
---|
436 | # :fqdn:type:rdata:ttl:time:loc
|
---|
437 |
|
---|
438 | my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
|
---|
439 | $fqdn =~ s/\.$//;
|
---|
440 | $fqdn =~ s/^\\052/*/;
|
---|
441 | $ttl = 0 if !$ttl;
|
---|
442 | $stamp = '' if !$stamp;
|
---|
443 | $loc = '' if !$loc;
|
---|
444 | $loc = '' if $loc =~ /^:+$/;
|
---|
445 |
|
---|
446 | if ($type == 33) {
|
---|
447 | # SRV
|
---|
448 | my ($prio, $weight, $port, $target) = (0,0,0,0);
|
---|
449 |
|
---|
450 | my @tmp = _byteparse(\$rdata, 2);
|
---|
451 | $prio = $tmp[0] * 256 + $tmp[1];
|
---|
452 | @tmp = _byteparse(\$rdata, 2);
|
---|
453 | $weight = $tmp[0] * 256 + $tmp[1];
|
---|
454 | @tmp = _byteparse(\$rdata, 2);
|
---|
455 | $port = $tmp[0] * 256 + $tmp[1];
|
---|
456 |
|
---|
457 | $rdata =~ s/\\\d{3}/./g;
|
---|
458 | ($target) = ($rdata =~ /^\.(.+)\.$/);
|
---|
459 | # hmm. the above *should* work, but What If(TM) we have ASCII-range bytes
|
---|
460 | # representing the target's fqdn part length(s)? axfr-get doesn't seem to,
|
---|
461 | # probably because dec. 33->63 includes most punctuation and all the numbers
|
---|
462 | # while ($rdata =~ /(\\\d{3})/) {
|
---|
463 | # my $cnt = $1;
|
---|
464 | # $rdata =~ s/^$cnt//;
|
---|
465 | # $cnt =~ s/^\\/0/;
|
---|
466 | # $cnt = oct($cnt);
|
---|
467 | # my ($seg) = ($rdata =~ /^(.{$cnt})/);
|
---|
468 | # $target .=
|
---|
469 | # }
|
---|
470 |
|
---|
471 | my $domid = DNSDB::_hostparent($dbh, $fqdn);
|
---|
472 | if ($domid) {
|
---|
473 | $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl) if $domid;
|
---|
474 | } else {
|
---|
475 | push @deferred, $rec unless $nodefer;
|
---|
476 | }
|
---|
477 |
|
---|
478 | } elsif ($type == 28) {
|
---|
479 | # AAAA
|
---|
480 | my @v6;
|
---|
481 |
|
---|
482 | for (my $i=0; $i < 8; $i++) {
|
---|
483 | my @tmp = _byteparse(\$rdata, 2);
|
---|
484 | push @v6, sprintf("%0.4x", $tmp[0] * 256 + $tmp[1]);
|
---|
485 | }
|
---|
486 | my $val = NetAddr::IP->new(join(':', @v6));
|
---|
487 |
|
---|
488 | my $fparent = DNSDB::_hostparent($dbh, $fqdn);
|
---|
489 | if ($fparent) {
|
---|
490 | $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl);
|
---|
491 | } else {
|
---|
492 | push @deferred, $rec unless $nodefer;
|
---|
493 | }
|
---|
494 |
|
---|
495 | } elsif ($type == 16) {
|
---|
496 | # TXT
|
---|
497 | my $txtstring = _rdata2string($rdata);
|
---|
498 |
|
---|
499 | if ($fqdn =~ /\.arpa$/) {
|
---|
500 | ($code,$msg) = DNSDB::_zone2cidr($fqdn);
|
---|
501 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
|
---|
502 | if ($rparent) {
|
---|
503 | $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl);
|
---|
504 | } else {
|
---|
505 | push @deferred, $rec unless $nodefer;
|
---|
506 | }
|
---|
507 | } else {
|
---|
508 | my $domid = DNSDB::_hostparent($dbh, $fqdn);
|
---|
509 | if ($domid) {
|
---|
510 | $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl);
|
---|
511 | } else {
|
---|
512 | push @deferred, $rec unless $nodefer;
|
---|
513 | }
|
---|
514 | }
|
---|
515 |
|
---|
516 | } elsif ($type == 17) {
|
---|
517 | # RP
|
---|
518 | my ($email, $txtrec) = split /\\000/, $rdata;
|
---|
519 | $email =~ s/\\\d{3}/./g;
|
---|
520 | $email =~ s/^\.//;
|
---|
521 | $txtrec =~ s/\\\d{3}/./g;
|
---|
522 | $txtrec =~ s/^\.//;
|
---|
523 |
|
---|
524 | # these might actually make sense in a reverse zone... sort of.
|
---|
525 | if ($fqdn =~ /\.arpa$/) {
|
---|
526 | ($code,$msg) = DNSDB::_zone2cidr($fqdn);
|
---|
527 | my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
|
---|
528 | if ($rparent) {
|
---|
529 | $recsth->execute(0, $rparent, "$email $txtrec", 17, "$msg", 0, 0, 0, $ttl);
|
---|
530 | } else {
|
---|
531 | push @deferred, $rec unless $nodefer;
|
---|
532 | }
|
---|
533 | } else {
|
---|
534 | my $domid = DNSDB::_hostparent($dbh, $fqdn);
|
---|
535 | if ($domid) {
|
---|
536 | $recsth->execute($domid, 0, $fqdn, 17, "$email $txtrec", 0, 0, 0, $ttl);
|
---|
537 | } else {
|
---|
538 | push @deferred, $rec unless $nodefer;
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | } elsif ($type == 44) {
|
---|
543 | # SSHFP
|
---|
544 | my $sshfp = _byteparse(\$rdata, 1);
|
---|
545 | $sshfp .= " "._byteparse(\$rdata, 1);
|
---|
546 | $sshfp .= " "._rdata2hex($rdata);
|
---|
547 |
|
---|
548 | # these do not make sense in a reverse zone, since they're logically attached to an A record
|
---|
549 | my $domid = DNSDB::_hostparent($dbh, $fqdn);
|
---|
550 | if ($domid) {
|
---|
551 | $recsth->execute($domid, 0, $fqdn, 44, $sshfp, 0, 0, 0, $ttl);
|
---|
552 | } else {
|
---|
553 | push @deferred, $rec unless $nodefer;
|
---|
554 | }
|
---|
555 |
|
---|
556 | } else {
|
---|
557 | # ... uhhh, dunno
|
---|
558 | }
|
---|
559 |
|
---|
560 | } else {
|
---|
561 | $cnt{other}++;
|
---|
562 | print " $_\n";
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | close FLAT;
|
---|
567 | }
|
---|