source: trunk/tiny-import.pl@ 419

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

/trunk

Track and report count of tinydns records imported sucessfully

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 21.5 KB
RevLine 
[348]1#!/usr/bin/perl
2# dnsadmin shell-based import tool for tinydns flatfiles
3##
4# $Id: tiny-import.pl 396 2012-09-24 21:43:12Z 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
[373]35usage() if !@ARGV;
36
37my %importcfg = (
38 rw => 0,
39 conv => 0,
40 trial => 0,
41 );
42# Handle some command-line arguments
43while ($ARGV[0] =~ /^-/) {
44 my $arg = shift @ARGV;
45 usage() if $arg !~ /^-[rct]+$/;
46 # -r rewrite imported files to comment imported records
47 # -c coerce/downconvert A+PTR = records to PTR
48 # -t trial mode; don't commit to DB or actually rewrite flatfile (disables -r)
49 $arg =~ s/^-//;
50 my @tmp = split //, $arg;
51 foreach (@tmp) {
52 $importcfg{rw} = 1 if $_ eq 'r';
53 $importcfg{conv} = 1 if $_ eq 'c';
54 $importcfg{trial} = 1 if $_ eq 't';
55 }
56}
57$importcfg{rw} = 0 if $importcfg{trial};
58
59sub usage {
60 die q(usage: tiny-import.pl [-r] [-c] datafile1 datafile2 ... datafileN ...
61 -r Rewrite all specified data files with a warning header indicating the
62 records are now managed by web, and commenting out all imported records.
63 The directory containing any given datafile must be writable.
64 -c Convert any A+PTR (=) record to a bare PTR if the forward domain is
65 not present in the database. Note this does NOT look forward through
66 a single file, nor across multiple files handled in the same run.
67 Multiple passes may be necessary if SOA and = records are heavily
68 intermixed and not clustered together.
69 -t Trial run mode; spits out records that would be left unimported.
70 Disables -r if set.
71
72 -r and -c may be combined (-rc)
73
74 datafileN is any tinydns record data file.
75);
76}
77
[348]78my $code;
79my ($dbh,$msg) = connectDB($config{dbname}, $config{dbuser}, $config{dbpass}, $config{dbhost});
80initGlobals($dbh) if $dbh;
81
82$dbh->{AutoCommit} = 0;
83$dbh->{RaiseError} = 1;
84
85my %cnt;
86my @deferred;
[353]87my $errstr = '';
[348]88
89foreach my $file (@ARGV) {
90 eval {
91 import(file => $file);
92# import(file => $file, nosoa => 1);
[373]93 $dbh->rollback if $importcfg{trial};
94 $dbh->commit unless $importcfg{trial};
[348]95 };
96 if ($@) {
[373]97 print "Failure trying to import $file: $@\n $errstr\n";
98 unlink ".$file.$$" if $importcfg{rw}; # cleanup
99 $dbh->rollback;
[348]100 }
101}
102
[373]103# print summary count of record types encountered
104foreach (keys %cnt) {
105 print " $_ $cnt{$_}\n";
106}
[348]107
108exit 0;
109
110sub import {
111 our %args = @_;
112 my $flatfile = $args{file};
[373]113 my @fpath = split '/', $flatfile;
114 $fpath[$#fpath] = ".$fpath[$#fpath]";
115 my $rwfile = join('/', @fpath);#.".$$";
116
[348]117 open FLAT, "<$flatfile";
118
[373]119 if ($importcfg{rw}) {
120 open RWFLAT, ">$rwfile" or die "Couldn't open tempfile $rwfile for rewriting: $!\n";
121 print RWFLAT "# WARNING: Records in this file have been imported to the web UI.\n#\n";
122 }
123
[372]124 our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl,location) ".
125 " VALUES (?,?,?,?,?,?,?,?,?,?)");
[348]126
[353]127 my %deleg;
128
[396]129 my $ok = 0;
[348]130 while (<FLAT>) {
[373]131 if (/^#/ || /^\s*$/) {
132 print RWFLAT "#$_" if $importcfg{rw};
133 next;
134 }
[348]135 chomp;
[372]136 s/\s*$//;
[373]137 my $recstat = recslurp($_);
[396]138 $ok++ if $recstat;
[373]139 if ($importcfg{rw}) {
140 if ($recstat) {
141 print RWFLAT "#$_\n";
142 } else {
143 print RWFLAT "$_\n";
144 }
145 }
[348]146 }
147
[373]148 # Move the rewritten flatfile in place of the original, so that any
149 # external export processing will pick up any remaining records.
150 if ($importcfg{rw}) {
151 close RWFLAT;
152 rename "$rwfile", $flatfile;
153 }
154
155 # Show the failed records
[348]156 foreach (@deferred) {
[373]157 print "failed to import $_\n";
[348]158 }
159
[373]160##fixme: hmm. can't write the record back to the flatfile in the
161# main while above, then come down here and import it anyway, can we?
162# # Try the deferred records again, once.
163# foreach (@deferred) {
164# print "trying $_ again\n";
165# recslurp($_, 1);
166# }
[353]167
[373]168 # .. but we can at least say how many records weren't imported.
[396]169 print "$ok OK, ".scalar(@deferred)." deferred records in $flatfile\n";
[373]170 $#deferred = -1;
171
172
[348]173 # Sub for various nonstandard types with lots of pure bytes expressed in octal
[353]174 # Takes a tinydns rdata string and count, returns a list of $count bytes as well
[348]175 # as trimming those logical bytes off the front of the rdata string.
176 sub _byteparse {
177 my $src = shift;
178 my $count = shift;
179 my @ret;
180 for (my $i = 0; $i < $count; $i++) {
181 if ($$src =~ /^\\/) {
182 # we should have an octal bit
183 my ($tmp) = ($$src =~ /^(\\\d{3})/);
184 $tmp =~ s/\\/0/;
185 push @ret, oct($tmp);
186 $$src =~ s/^\\\d{3}//;
187 } else {
188 # we seem to have a byte expressed as an ASCII character
189 my ($tmp) = ($$src =~ /^(.)/);
190 push @ret, ord($tmp);
191 $$src =~ s/^.//;
192 }
193 }
194 return @ret;
195 }
196
[353]197 # Convert octal-coded bytes back to something resembling normal characters, general case
198 sub _deoctal {
199 my $targ = shift;
200 while ($$targ =~ /\\(\d{3})/) {
201 my $sub = chr(oct($1));
202 $$targ =~ s/\\$1/$sub/g;
203 }
204 }
205
[361]206 sub _rdata2string {
207 my $rdata = shift;
208 my $tmpout = '';
209 while ($rdata) {
210 my $bytecount = 0;
211 if ($rdata =~ /^\\/) {
212 ($bytecount) = ($rdata =~ /^(\\\d{3})/);
213 $bytecount =~ s/\\/0/;
214 $bytecount = oct($bytecount);
215 $rdata =~ s/^\\\d{3}//;
216 } else {
217 ($bytecount) = ($rdata =~ /^(.)/);
218 $bytecount = ord($bytecount);
219 $rdata =~ s/^.//;
220 }
221 my @tmp = _byteparse(\$rdata, $bytecount);
222 foreach (@tmp) { $tmpout .= chr($_); }
223##fixme: warn or fail on long (>256? >512? >321?) strings
224 }
225 return $tmpout;
226 }
227
[363]228 sub _rdata2hex {
229 my $rdata = shift;
230 my $tmpout = '';
231 while ($rdata) {
232 my $byte = '';
233 if ($rdata =~ /^\\/) {
234 ($byte) = ($rdata =~ /^(\\\d{3})/);
235 $byte =~ s/\\/0/;
236 $tmpout .= sprintf("%0.2x", oct($byte));
237 $rdata =~ s/^\\\d{3}//;
238 } else {
239 ($byte) = ($rdata =~ /^(.)/);
240 $tmpout .= sprintf("%0.2x", ord($byte));
241 $rdata =~ s/^.//;
242 }
243 }
244 return $tmpout;
245 }
[361]246
[363]247
[348]248 sub recslurp {
249 my $rec = shift;
250 my $nodefer = shift || 0;
[373]251 my $impok = 1;
[348]252
[360]253 $errstr = $rec; # this way at least we have some idea what went <splat>
254
[348]255 if ($rec =~ /^=/) {
256 $cnt{APTR}++;
[354]257
258##fixme: do checks like this for all types
259 if ($rec !~ /^=(?:\*|\\052)?[a-z0-9\._-]+:[\d\.]+:\d*/i) {
260 print "bad A+PTR $rec\n";
261 return;
262 }
[353]263 my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
[348]264 $host =~ s/^=//;
265 $host =~ s/\.$//;
[353]266 $ttl = 0 if !$ttl;
267 $stamp = '' if !$stamp;
[348]268 $loc = '' if !$loc;
[353]269 $loc = '' if $loc =~ /^:+$/;
[348]270 my $fparent = DNSDB::_hostparent($dbh, $host);
271 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($ip));
272 if ($fparent && $rparent) {
[372]273 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc);
[348]274 } else {
275 push @deferred, $rec unless $nodefer;
[373]276 $impok = 0;
[348]277 # print "$tmporig deferred; can't find both forward and reverse zone parents\n";
278 }
279
280 } elsif ($rec =~ /^C/) {
281 $cnt{CNAME}++;
[354]282
[353]283 my ($host,$targ,$ttl,$stamp,$loc) = split /:/, $rec, 5;
[348]284 $host =~ s/^C//;
285 $host =~ s/\.$//;
[360]286 $host =~ s/^\\052/*/;
[353]287 $ttl = 0 if !$ttl;
288 $stamp = '' if !$stamp;
[348]289 $loc = '' if !$loc;
[353]290 $loc = '' if $loc =~ /^:+$/;
291 if ($host =~ /\.arpa$/) {
292 ($code,$msg) = DNSDB::_zone2cidr($host);
293 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
[372]294 $recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl, $loc);
[348]295
[353]296##fixme: automagically convert manually maintained sub-/24 delegations
297# my ($subip, $zone) = split /\./, $targ, 2;
298# ($code, $msg) = DNSDB::_zone2cidr($zone);
299# push @{$deleg{"$msg"}{iplist}}, $subip;
300#print "$msg $subip\n";
301
[348]302 } else {
[353]303 my $fparent = DNSDB::_hostparent($dbh, $host);
304 if ($fparent) {
[372]305 $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl, $loc);
[353]306 } else {
307 push @deferred, $rec unless $nodefer;
[373]308 $impok = 0;
[353]309 # print "$tmporig deferred; can't find parent zone\n";
310 }
[348]311 }
312
313 } elsif ($rec =~ /^\&/) {
314 $cnt{NS}++;
[354]315
[355]316 my ($zone,$ip,$ns,$ttl,$stamp,$loc) = split /:/, $rec, 6;
317 $zone =~ s/^\&//;
318 $zone =~ s/\.$//;
[357]319 $ns =~ s/\.$//;
[358]320 $ns = "$ns.ns.$zone" if $ns !~ /\./;
[355]321 $ttl = 0 if !$ttl;
322 $stamp = '' if !$stamp;
323 $loc = '' if !$loc;
324 $loc = '' if $loc =~ /^:+$/;
325 if ($zone =~ /\.arpa$/) {
326 ($code,$msg) = DNSDB::_zone2cidr($zone);
327 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >>= ?", undef, ("$msg"));
328##fixme, in concert with the CNAME check for same; automagically
329# create "delegate" record instead for subzone NSes: convert above to use = instead of >>=
330# ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"))
331# if !$rparent;
332 if ($rparent) {
[372]333 $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl, $loc);
[355]334 } else {
335 push @deferred, $rec unless $nodefer;
[373]336 $impok = 0;
[355]337 }
338 } else {
339 my $fparent = DNSDB::_hostparent($dbh, $zone);
340 if ($fparent) {
[372]341 $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl, $loc);
342 $recsth->execute($fparent, 0, $ns, 2, $ip, 0, 0, 0, $ttl, $loc) if $ip;
[355]343 } else {
344 push @deferred, $rec unless $nodefer;
[373]345 $impok = 0;
[355]346 }
347 }
348
[348]349 } elsif ($rec =~ /^\^/) {
350 $cnt{PTR}++;
[354]351
[356]352 my ($rip,$host,$ttl,$stamp,$loc) = split /:/, $rec, 5;
353 $rip =~ s/^\^//;
354 $rip =~ s/\.$//;
355 $ttl = 0 if !$ttl;
356 $stamp = '' if !$stamp;
357 $loc = '' if !$loc;
358 $loc = '' if $loc =~ /^:+$/;
359 my $rparent;
360 if (my ($i, $z) = ($rip =~ /^(\d+)\.(\d+-(?:\d+\.){4}in-addr.arpa)$/) ) {
361 ($code,$msg) = DNSDB::_zone2cidr($z);
362 # Exact matches only, because we're in a sub-/24 delegation
363##fixme: flag the type of delegation (range, subnet-with-dash, subnet-with-slash)
364# somewhere so we can recover it on export. probably best to do that in the revzone data.
365 ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ("$msg"));
366 $z =~ s/^[\d-]+//;
367 ($code,$msg) = DNSDB::_zone2cidr("$i.$z"); # Get the actual IP and normalize
368 } else {
369 ($code,$msg) = DNSDB::_zone2cidr($rip);
370 ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"));
371 }
372 if ($rparent) {
[372]373 $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl, $loc);
[356]374 } else {
375 push @deferred, $rec unless $nodefer;
[373]376 $impok = 0;
[356]377 }
378
[348]379 } elsif ($rec =~ /^\+/) {
380 $cnt{A}++;
[354]381
[359]382 my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
383 $host =~ s/^\+//;
384 $host =~ s/\.$//;
[360]385 $host =~ s/^\\052/*/;
[359]386 $ttl = 0 if !$ttl;
387 $stamp = '' if !$stamp;
388 $loc = '' if !$loc;
389 $loc = '' if $loc =~ /^:+$/;
390
391 my $domid = DNSDB::_hostparent($dbh, $host);
392 if ($domid) {
[372]393 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc);
[359]394 } else {
395 push @deferred, $rec unless $nodefer;
[373]396 $impok = 0;
[359]397 }
398
[348]399 } elsif ($rec =~ /^Z/) {
400 $cnt{SOA}++;
[354]401
[353]402 my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$stamp,$loc) = split /:/, $rec, 11;
[348]403 $zone =~ s/^Z//;
404 $zone =~ s/\.$//;
405 $master =~ s/\.$//;
406 $contact =~ s/\.$//;
[353]407 $ttl = 0 if !$ttl;
408 $stamp = '' if !$stamp;
[348]409 $loc = '' if !$loc;
[353]410 $loc = '' if $loc =~ /^:+$/;
[348]411 if ($zone =~ /\.arpa$/) {
412 ($code,$msg) = DNSDB::_zone2cidr($zone);
[380]413 $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location) VALUES (?,1,1,?)",
414 undef, ($msg, $loc));
[348]415 my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
[372]416 $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc);
[348]417 } else {
[380]418 $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)",
419 undef, ($zone, $loc));
[348]420 my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
[372]421 $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc);
[348]422 }
[354]423
[348]424 } elsif ($rec =~ /^\@/) {
425 $cnt{MX}++;
[354]426
[357]427 my ($zone,$ip,$host,$dist,$ttl,$stamp,$loc) = split /:/, $rec, 7;
[359]428 $zone =~ s/^\@//;
[357]429 $zone =~ s/\.$//;
[360]430 $zone =~ s/^\\052/*/;
[357]431 $host =~ s/\.$//;
432 $host = "$host.mx.$zone" if $host !~ /\./;
433 $ttl = 0 if !$ttl;
434 $stamp = '' if !$stamp;
435 $loc = '' if !$loc;
436 $loc = '' if $loc =~ /^:+$/;
437
438# note we don't check for reverse domains here, because MX records don't make any sense in reverse zones.
439# if this really ever becomes an issue for someone it can be expanded to handle those weirdos
440
441 # allow for subzone MXes, since it's perfectly legitimate to simply stuff it all in a single parent zone
442 my $domid = DNSDB::_hostparent($dbh, $zone);
443 if ($domid) {
[372]444 $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl, $loc);
445 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip;
[357]446 } else {
447 push @deferred, $rec unless $nodefer;
[373]448 $impok = 0;
[357]449 }
450
[348]451 } elsif ($rec =~ /^'/) {
452 $cnt{TXT}++;
453
[353]454 my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
[348]455 $fqdn =~ s/^'//;
[360]456 $fqdn =~ s/^\\052/*/;
[348]457 _deoctal(\$rdata);
[353]458 $ttl = 0 if !$ttl;
459 $stamp = '' if !$stamp;
460 $loc = '' if !$loc;
461 $loc = '' if $loc =~ /^:+$/;
[348]462
[360]463 if ($fqdn =~ /\.arpa$/) {
464 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
465 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
[372]466 $recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl, $loc);
[348]467 } else {
[360]468 my $domid = DNSDB::_hostparent($dbh, $fqdn);
469 if ($domid) {
[372]470 $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl, $loc);
[360]471 } else {
472 push @deferred, $rec unless $nodefer;
[373]473 $impok = 0;
[360]474 }
[348]475 }
476
477 } elsif ($rec =~ /^\./) {
478 $cnt{NSASOA}++;
[354]479
[353]480 my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
481 $fqdn =~ s/^\.//;
482 $fqdn =~ s/\.$//;
483 $ns =~ s/\.$//;
484 $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
485 $ttl = 0 if !$ttl;
486 $stamp = '' if !$stamp;
487 $loc = '' if !$loc;
488 $loc = '' if $loc =~ /^:+$/;
489
490 if ($fqdn =~ /\.arpa$/) {
491 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
492 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
493 if (!$rdns) {
494 $errstr = "adding revzone $msg";
[380]495 $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location) VALUES (?,1,1,?)",
496 undef, ($msg, $loc));
[353]497 ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
498# this would probably make a lot more sense to do hostmaster.$config{admindomain}
[372]499 $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc);
[353]500 }
[372]501 $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl, $loc);
[353]502##fixme: (?) implement full conversion of tinydns . records?
503# -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse
504#$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl)
505# ... auto-A-record simply does not make sense in reverse zones. Functionally
506# I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
507# zones has names for their nameservers already.
508# Even the auto-nameserver-fqdn comes out... ugly.
509
510 } else {
511 my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
512 undef, ($fqdn));
513 if (!$domid) {
514 $errstr = "adding domain $fqdn";
[380]515 $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)",
516 undef, ($fqdn, $loc));
[353]517 ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
[372]518 $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc);
[353]519 }
[372]520 $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl, $loc);
521 $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip;
[353]522 }
523
524
525 } elsif ($rec =~ /^\%/) {
526 $cnt{VIEWS}++;
[354]527
[372]528 # unfortunate that we don't have a guaranteed way to get a description on these. :/
529 my ($loc,$cnet) = split /:/, $rec, 2;
530 $loc =~ s/^\%//;
531 if (my ($iplist) = $dbh->selectrow_array("SELECT iplist FROM locations WHERE location = ?", undef, ($loc))) {
532 if ($cnet) {
[375]533 $iplist .= ", $cnet";
[372]534 $dbh->do("UPDATE locations SET iplist = ? WHERE location = ?", undef, ($iplist, $loc));
535 } else {
536 # hmm. spit out a warning? if we already have entries for $loc, adding a null
537 # entry will almost certainly Do The Wrong Thing(TM)
538 }
539 } else {
540 $cnet = '' if !$cnet; # de-nullify
541 $dbh->do("INSERT INTO locations (location,iplist,description) VALUES (?,?,?)", undef, ($loc, $cnet, $loc));
542 }
543
[348]544 } elsif ($rec =~ /^:/) {
545 $cnt{NCUST}++;
546# Big section. Since tinydns can publish anything you can encode properly, but only provides official
547# recognition and handling for the core common types, this must deal with the leftovers.
548# :fqdn:type:rdata:ttl:time:loc
549
[354]550 my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
[360]551 $fqdn =~ s/\.$//;
552 $fqdn =~ s/^\\052/*/;
[354]553 $ttl = 0 if !$ttl;
554 $stamp = '' if !$stamp;
555 $loc = '' if !$loc;
556 $loc = '' if $loc =~ /^:+$/;
[348]557
[354]558 if ($type == 33) {
559 # SRV
560 my ($prio, $weight, $port, $target) = (0,0,0,0);
[348]561
[354]562 my @tmp = _byteparse(\$rdata, 2);
563 $prio = $tmp[0] * 256 + $tmp[1];
564 @tmp = _byteparse(\$rdata, 2);
565 $weight = $tmp[0] * 256 + $tmp[1];
566 @tmp = _byteparse(\$rdata, 2);
567 $port = $tmp[0] * 256 + $tmp[1];
[348]568
[354]569 $rdata =~ s/\\\d{3}/./g;
570 ($target) = ($rdata =~ /^\.(.+)\.$/);
[348]571# hmm. the above *should* work, but What If(TM) we have ASCII-range bytes
572# representing the target's fqdn part length(s)? axfr-get doesn't seem to,
573# probably because dec. 33->63 includes most punctuation and all the numbers
574# while ($rdata =~ /(\\\d{3})/) {
575# my $cnt = $1;
576# $rdata =~ s/^$cnt//;
577# $cnt =~ s/^\\/0/;
578# $cnt = oct($cnt);
579# my ($seg) = ($rdata =~ /^(.{$cnt})/);
580# $target .=
581# }
582
[354]583 my $domid = DNSDB::_hostparent($dbh, $fqdn);
584 if ($domid) {
[372]585 $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl, $loc) if $domid;
[354]586 } else {
587 push @deferred, $rec unless $nodefer;
[373]588 $impok = 0;
[354]589 }
[348]590
[354]591 } elsif ($type == 28) {
592 # AAAA
593 my @v6;
[348]594
[354]595 for (my $i=0; $i < 8; $i++) {
596 my @tmp = _byteparse(\$rdata, 2);
597 push @v6, sprintf("%0.4x", $tmp[0] * 256 + $tmp[1]);
598 }
599 my $val = NetAddr::IP->new(join(':', @v6));
[348]600
[360]601 my $fparent = DNSDB::_hostparent($dbh, $fqdn);
602 if ($fparent) {
[372]603 $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl, $loc);
[354]604 } else {
605 push @deferred, $rec unless $nodefer;
[373]606 $impok = 0;
[354]607 }
[348]608
[361]609 } elsif ($type == 16) {
610 # TXT
611 my $txtstring = _rdata2string($rdata);
612
613 if ($fqdn =~ /\.arpa$/) {
614 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
615 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
616 if ($rparent) {
[372]617 $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl, $loc);
[361]618 } else {
619 push @deferred, $rec unless $nodefer;
[373]620 $impok = 0;
[361]621 }
622 } else {
623 my $domid = DNSDB::_hostparent($dbh, $fqdn);
624 if ($domid) {
[372]625 $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl, $loc);
[361]626 } else {
627 push @deferred, $rec unless $nodefer;
[373]628 $impok = 0;
[361]629 }
630 }
631
632 } elsif ($type == 17) {
633 # RP
[362]634 my ($email, $txtrec) = split /\\000/, $rdata;
635 $email =~ s/\\\d{3}/./g;
636 $email =~ s/^\.//;
637 $txtrec =~ s/\\\d{3}/./g;
638 $txtrec =~ s/^\.//;
[361]639
[362]640 # these might actually make sense in a reverse zone... sort of.
641 if ($fqdn =~ /\.arpa$/) {
642 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
643 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
644 if ($rparent) {
[372]645 $recsth->execute(0, $rparent, "$email $txtrec", 17, "$msg", 0, 0, 0, $ttl, $loc);
[362]646 } else {
647 push @deferred, $rec unless $nodefer;
[373]648 $impok = 0;
[362]649 }
650 } else {
651 my $domid = DNSDB::_hostparent($dbh, $fqdn);
652 if ($domid) {
[372]653 $recsth->execute($domid, 0, $fqdn, 17, "$email $txtrec", 0, 0, 0, $ttl, $loc);
[362]654 } else {
655 push @deferred, $rec unless $nodefer;
[373]656 $impok = 0;
[362]657 }
658 }
659
[361]660 } elsif ($type == 44) {
661 # SSHFP
[363]662 my $sshfp = _byteparse(\$rdata, 1);
663 $sshfp .= " "._byteparse(\$rdata, 1);
664 $sshfp .= " "._rdata2hex($rdata);
[361]665
[363]666 # these do not make sense in a reverse zone, since they're logically attached to an A record
667 my $domid = DNSDB::_hostparent($dbh, $fqdn);
668 if ($domid) {
[372]669 $recsth->execute($domid, 0, $fqdn, 44, $sshfp, 0, 0, 0, $ttl, $loc);
[363]670 } else {
671 push @deferred, $rec unless $nodefer;
[373]672 $impok = 0;
[363]673 }
674
[354]675 } else {
[373]676 print "unhandled rec $rec\n";
677 $impok = 0;
[354]678 # ... uhhh, dunno
679 }
[348]680
681 } else {
682 $cnt{other}++;
[354]683 print " $_\n";
[348]684 }
685
[373]686 return $impok; # just to make sure
687 } # recslurp()
688
[348]689 close FLAT;
690}
Note: See TracBrowser for help on using the repository browser.