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