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