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