source: trunk/tiny-import.pl@ 380

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

/trunk

Fix minor(ish) bug in import of locations; set default_location
on new zones. See #10.

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