source: trunk/tiny-import.pl@ 377

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

/trunk

Checkpoint adding locations/views. See #10.

  • Tweak new locations table again with an internal serial for sorting and finding the "highest" previous location
  • Complete "Add location" stub
  • Fill in initial display page for "Edit location"
  • Tweak location add/edit/update template a little, add new comments field to match new field in table
  • Tweak location import so we don't end up with great long space-free fields similar to what can't be avoided with TXT (usually SPF/DomainKeys) records
  • Fill in addLoc() stub
  • Add getLoc()
  • Tweak location export to strip commas and whitespace properly
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 21.3 KB
RevLine 
[348]1#!/usr/bin/perl
2# dnsadmin shell-based import tool for tinydns flatfiles
3##
4# $Id: tiny-import.pl 375 2012-08-08 22:04:47Z 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);
411 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
412 my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
[372]413 $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc);
[348]414 } else {
415 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($zone));
416 my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
[372]417 $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc);
[348]418 }
[354]419
[348]420 } elsif ($rec =~ /^\@/) {
421 $cnt{MX}++;
[354]422
[357]423 my ($zone,$ip,$host,$dist,$ttl,$stamp,$loc) = split /:/, $rec, 7;
[359]424 $zone =~ s/^\@//;
[357]425 $zone =~ s/\.$//;
[360]426 $zone =~ s/^\\052/*/;
[357]427 $host =~ s/\.$//;
428 $host = "$host.mx.$zone" if $host !~ /\./;
429 $ttl = 0 if !$ttl;
430 $stamp = '' if !$stamp;
431 $loc = '' if !$loc;
432 $loc = '' if $loc =~ /^:+$/;
433
434# note we don't check for reverse domains here, because MX records don't make any sense in reverse zones.
435# if this really ever becomes an issue for someone it can be expanded to handle those weirdos
436
437 # allow for subzone MXes, since it's perfectly legitimate to simply stuff it all in a single parent zone
438 my $domid = DNSDB::_hostparent($dbh, $zone);
439 if ($domid) {
[372]440 $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl, $loc);
441 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip;
[357]442 } else {
443 push @deferred, $rec unless $nodefer;
[373]444 $impok = 0;
[357]445 }
446
[348]447 } elsif ($rec =~ /^'/) {
448 $cnt{TXT}++;
449
[353]450 my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
[348]451 $fqdn =~ s/^'//;
[360]452 $fqdn =~ s/^\\052/*/;
[348]453 _deoctal(\$rdata);
[353]454 $ttl = 0 if !$ttl;
455 $stamp = '' if !$stamp;
456 $loc = '' if !$loc;
457 $loc = '' if $loc =~ /^:+$/;
[348]458
[360]459 if ($fqdn =~ /\.arpa$/) {
460 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
461 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
[372]462 $recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl, $loc);
[348]463 } else {
[360]464 my $domid = DNSDB::_hostparent($dbh, $fqdn);
465 if ($domid) {
[372]466 $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl, $loc);
[360]467 } else {
468 push @deferred, $rec unless $nodefer;
[373]469 $impok = 0;
[360]470 }
[348]471 }
472
473 } elsif ($rec =~ /^\./) {
474 $cnt{NSASOA}++;
[354]475
[353]476 my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
477 $fqdn =~ s/^\.//;
478 $fqdn =~ s/\.$//;
479 $ns =~ s/\.$//;
480 $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
481 $ttl = 0 if !$ttl;
482 $stamp = '' if !$stamp;
483 $loc = '' if !$loc;
484 $loc = '' if $loc =~ /^:+$/;
485
486 if ($fqdn =~ /\.arpa$/) {
487 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
488 my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
489 if (!$rdns) {
490 $errstr = "adding revzone $msg";
491 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
492 ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
493# this would probably make a lot more sense to do hostmaster.$config{admindomain}
[372]494 $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc);
[353]495 }
[372]496 $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl, $loc);
[353]497##fixme: (?) implement full conversion of tinydns . records?
498# -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse
499#$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl)
500# ... auto-A-record simply does not make sense in reverse zones. Functionally
501# I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
502# zones has names for their nameservers already.
503# Even the auto-nameserver-fqdn comes out... ugly.
504
505 } else {
506 my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
507 undef, ($fqdn));
508 if (!$domid) {
509 $errstr = "adding domain $fqdn";
510 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($fqdn));
511 ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
[372]512 $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc);
[353]513 }
[372]514 $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl, $loc);
515 $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip;
[353]516 }
517
518
519 } elsif ($rec =~ /^\%/) {
520 $cnt{VIEWS}++;
[354]521
[372]522 # unfortunate that we don't have a guaranteed way to get a description on these. :/
523 my ($loc,$cnet) = split /:/, $rec, 2;
524 $loc =~ s/^\%//;
525 if (my ($iplist) = $dbh->selectrow_array("SELECT iplist FROM locations WHERE location = ?", undef, ($loc))) {
526 if ($cnet) {
[375]527 $iplist .= ", $cnet";
[372]528 $dbh->do("UPDATE locations SET iplist = ? WHERE location = ?", undef, ($iplist, $loc));
529 } else {
530 # hmm. spit out a warning? if we already have entries for $loc, adding a null
531 # entry will almost certainly Do The Wrong Thing(TM)
532 }
533 } else {
534 $cnet = '' if !$cnet; # de-nullify
535 $dbh->do("INSERT INTO locations (location,iplist,description) VALUES (?,?,?)", undef, ($loc, $cnet, $loc));
536 }
537
[348]538 } elsif ($rec =~ /^:/) {
539 $cnt{NCUST}++;
540# Big section. Since tinydns can publish anything you can encode properly, but only provides official
541# recognition and handling for the core common types, this must deal with the leftovers.
542# :fqdn:type:rdata:ttl:time:loc
543
[354]544 my (undef, $fqdn, $type, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 7;
[360]545 $fqdn =~ s/\.$//;
546 $fqdn =~ s/^\\052/*/;
[354]547 $ttl = 0 if !$ttl;
548 $stamp = '' if !$stamp;
549 $loc = '' if !$loc;
550 $loc = '' if $loc =~ /^:+$/;
[348]551
[354]552 if ($type == 33) {
553 # SRV
554 my ($prio, $weight, $port, $target) = (0,0,0,0);
[348]555
[354]556 my @tmp = _byteparse(\$rdata, 2);
557 $prio = $tmp[0] * 256 + $tmp[1];
558 @tmp = _byteparse(\$rdata, 2);
559 $weight = $tmp[0] * 256 + $tmp[1];
560 @tmp = _byteparse(\$rdata, 2);
561 $port = $tmp[0] * 256 + $tmp[1];
[348]562
[354]563 $rdata =~ s/\\\d{3}/./g;
564 ($target) = ($rdata =~ /^\.(.+)\.$/);
[348]565# hmm. the above *should* work, but What If(TM) we have ASCII-range bytes
566# representing the target's fqdn part length(s)? axfr-get doesn't seem to,
567# probably because dec. 33->63 includes most punctuation and all the numbers
568# while ($rdata =~ /(\\\d{3})/) {
569# my $cnt = $1;
570# $rdata =~ s/^$cnt//;
571# $cnt =~ s/^\\/0/;
572# $cnt = oct($cnt);
573# my ($seg) = ($rdata =~ /^(.{$cnt})/);
574# $target .=
575# }
576
[354]577 my $domid = DNSDB::_hostparent($dbh, $fqdn);
578 if ($domid) {
[372]579 $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl, $loc) if $domid;
[354]580 } else {
581 push @deferred, $rec unless $nodefer;
[373]582 $impok = 0;
[354]583 }
[348]584
[354]585 } elsif ($type == 28) {
586 # AAAA
587 my @v6;
[348]588
[354]589 for (my $i=0; $i < 8; $i++) {
590 my @tmp = _byteparse(\$rdata, 2);
591 push @v6, sprintf("%0.4x", $tmp[0] * 256 + $tmp[1]);
592 }
593 my $val = NetAddr::IP->new(join(':', @v6));
[348]594
[360]595 my $fparent = DNSDB::_hostparent($dbh, $fqdn);
596 if ($fparent) {
[372]597 $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl, $loc);
[354]598 } else {
599 push @deferred, $rec unless $nodefer;
[373]600 $impok = 0;
[354]601 }
[348]602
[361]603 } elsif ($type == 16) {
604 # TXT
605 my $txtstring = _rdata2string($rdata);
606
607 if ($fqdn =~ /\.arpa$/) {
608 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
609 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
610 if ($rparent) {
[372]611 $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl, $loc);
[361]612 } else {
613 push @deferred, $rec unless $nodefer;
[373]614 $impok = 0;
[361]615 }
616 } else {
617 my $domid = DNSDB::_hostparent($dbh, $fqdn);
618 if ($domid) {
[372]619 $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl, $loc);
[361]620 } else {
621 push @deferred, $rec unless $nodefer;
[373]622 $impok = 0;
[361]623 }
624 }
625
626 } elsif ($type == 17) {
627 # RP
[362]628 my ($email, $txtrec) = split /\\000/, $rdata;
629 $email =~ s/\\\d{3}/./g;
630 $email =~ s/^\.//;
631 $txtrec =~ s/\\\d{3}/./g;
632 $txtrec =~ s/^\.//;
[361]633
[362]634 # these might actually make sense in a reverse zone... sort of.
635 if ($fqdn =~ /\.arpa$/) {
636 ($code,$msg) = DNSDB::_zone2cidr($fqdn);
637 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
638 if ($rparent) {
[372]639 $recsth->execute(0, $rparent, "$email $txtrec", 17, "$msg", 0, 0, 0, $ttl, $loc);
[362]640 } else {
641 push @deferred, $rec unless $nodefer;
[373]642 $impok = 0;
[362]643 }
644 } else {
645 my $domid = DNSDB::_hostparent($dbh, $fqdn);
646 if ($domid) {
[372]647 $recsth->execute($domid, 0, $fqdn, 17, "$email $txtrec", 0, 0, 0, $ttl, $loc);
[362]648 } else {
649 push @deferred, $rec unless $nodefer;
[373]650 $impok = 0;
[362]651 }
652 }
653
[361]654 } elsif ($type == 44) {
655 # SSHFP
[363]656 my $sshfp = _byteparse(\$rdata, 1);
657 $sshfp .= " "._byteparse(\$rdata, 1);
658 $sshfp .= " "._rdata2hex($rdata);
[361]659
[363]660 # these do not make sense in a reverse zone, since they're logically attached to an A record
661 my $domid = DNSDB::_hostparent($dbh, $fqdn);
662 if ($domid) {
[372]663 $recsth->execute($domid, 0, $fqdn, 44, $sshfp, 0, 0, 0, $ttl, $loc);
[363]664 } else {
665 push @deferred, $rec unless $nodefer;
[373]666 $impok = 0;
[363]667 }
668
[354]669 } else {
[373]670 print "unhandled rec $rec\n";
671 $impok = 0;
[354]672 # ... uhhh, dunno
673 }
[348]674
675 } else {
676 $cnt{other}++;
[354]677 print " $_\n";
[348]678 }
679
[373]680 return $impok; # just to make sure
681 } # recslurp()
682
[348]683 close FLAT;
684}
Note: See TracBrowser for help on using the repository browser.