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