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