Changeset 543 for trunk/tiny-import.pl
- Timestamp:
- 12/10/13 16:22:10 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tiny-import.pl
r528 r543 25 25 use strict; 26 26 use warnings; 27 use POSIX; 28 use Time::TAI64 qw(:tai); 27 29 28 30 use lib '.'; ##uselib## … … 125 127 } 126 128 127 our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl,location ) ".128 " VALUES (?,?,?,?,?,?,?,?,?,? )");129 our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl,location,stamp,expires,stampactive) ". 130 " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"); 129 131 130 132 my %deleg; … … 248 250 } 249 251 252 sub calcstamp { 253 my $stampin = shift; 254 my $ttl = shift; 255 my $pzone = shift; 256 my $revrec = shift; 257 258 return ($ttl, 'n', 'n', '1970-01-01 00:00:00 -0') if !$stampin; 259 260 ##fixme Yes, this fails for records in 2038 sometime. No, I'm not going to care for a while. 261 $stampin = "\@$stampin"; # Time::TAI64 needs the leading @. Feh. 262 my $u = tai2unix($stampin); 263 $stampin = strftime("%Y-%m-%d %H:%M:%S %z", localtime($u)); 264 my $expires = 'n'; 265 if ($ttl) { 266 # TTL can stay put. 267 } else { 268 # TTL on import is 0, almost certainly wrong. Get the parent zone's SOA and use the minttl. 269 my $soa = $dnsdb->getSOA('n', $revrec, $pzone); 270 $ttl = $soa->{minttl}; 271 $expires = 'y'; 272 } 273 return ($ttl, 'y', $expires, $stampin); 274 } 250 275 251 276 sub recslurp { … … 274 299 my $fparent = $dnsdb->_hostparent($host); 275 300 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($ip)); 301 302 my $stampactive = 'n'; 303 my $expires = 'n'; 304 305 # can't set a timestamp on an orphaned record. we'll actually fail import of this record a little later. 306 if ($fparent || $rparent) { 307 if ($fparent) { 308 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $fparent, 'n'); 309 } else { 310 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y'); 311 } 312 } 313 276 314 if ($fparent && $rparent) { 277 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc );315 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 278 316 } else { 279 317 if ($importcfg{legacy}) { … … 282 320 $rparent = 0 if !$rparent; 283 321 if ($fparent || $rparent) { 284 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc );322 $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 285 323 } else { 286 324 # No parents found, cowardly refusing to add a dangling record … … 290 328 } elsif ($importcfg{conv}) { 291 329 # downconvert A+PTR if forward zone is not found 292 $recsth->execute(0, $rparent, $host, 12, $ip, 0, 0, 0, $ttl, $loc );330 $recsth->execute(0, $rparent, $host, 12, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 293 331 $converted++; 294 332 } else { … … 310 348 $loc = '' if !$loc; 311 349 $loc = '' if $loc =~ /^:+$/; 350 351 my $stampactive = 'n'; 352 my $expires = 'n'; 353 312 354 if ($host =~ /\.arpa$/) { 313 355 ($code,$msg) = DNSDB::_zone2cidr($host); 314 356 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg)); 315 $recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl, $loc); 357 if ($rparent) { 358 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y'); 359 $recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 360 } else { 361 push @deferred, $rec unless $nodefer; 362 $impok = 0; 363 # print "$tmporig deferred; can't find parent zone\n"; 364 } 316 365 317 366 ##fixme: automagically convert manually maintained sub-/24 delegations … … 324 373 my $fparent = $dnsdb->_hostparent($host); 325 374 if ($fparent) { 326 $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl, $loc); 375 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $fparent, 'n'); 376 $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 327 377 } else { 328 378 push @deferred, $rec unless $nodefer; … … 344 394 $loc = '' if !$loc; 345 395 $loc = '' if $loc =~ /^:+$/; 396 397 my $stampactive = 'n'; 398 my $expires = 'n'; 399 346 400 if ($zone =~ /\.arpa$/) { 347 401 ($code,$msg) = DNSDB::_zone2cidr($zone); … … 352 406 # if !$rparent; 353 407 if ($rparent) { 354 $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl, $loc); 408 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y'); 409 $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 355 410 } else { 356 411 push @deferred, $rec unless $nodefer; … … 360 415 my $fparent = $dnsdb->_hostparent($zone); 361 416 if ($fparent) { 362 $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl, $loc); 363 $recsth->execute($fparent, 0, $ns, 2, $ip, 0, 0, 0, $ttl, $loc) if $ip; 417 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $fparent, 'n'); 418 $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 419 $recsth->execute($fparent, 0, $ns, 2, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive) if $ip; 364 420 } else { 365 421 push @deferred, $rec unless $nodefer; … … 378 434 $loc = '' if !$loc; 379 435 $loc = '' if $loc =~ /^:+$/; 436 437 my $stampactive = 'n'; 438 my $expires = 'n'; 439 380 440 my $rparent; 381 441 if (my ($i, $z) = ($rip =~ /^(\d+)\.(\d+-(?:\d+\.){4}in-addr.arpa)$/) ) { … … 392 452 } 393 453 if ($rparent) { 394 $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl, $loc); 454 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y'); 455 $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 395 456 } else { 396 457 push @deferred, $rec unless $nodefer; … … 410 471 $loc = '' if $loc =~ /^:+$/; 411 472 473 my $stampactive = 'n'; 474 my $expires = 'n'; 475 412 476 my $domid = $dnsdb->_hostparent($host); 413 477 if ($domid) { 414 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc); 478 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n'); 479 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 415 480 } else { 416 481 push @deferred, $rec unless $nodefer; … … 430 495 $loc = '' if !$loc; 431 496 $loc = '' if $loc =~ /^:+$/; 497 498 my $stampactive = 'n'; 499 my $expires = 'n'; 500 501 ##fixme er... what do we do with an SOA with a timestamp? O_o 502 # fail for now, since there's no clean way I can see to handle this (yet) 503 # maybe (ab)use the -l flag to import as-is? 504 if ($stamp) { 505 push @deferred, $rec unless $nodefer; 506 return 0; 507 } 508 509 ##fixme: need more magic on TTL, so we can decide whether to use the minttl or newttl 510 # my $newttl; 511 # ($newttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $minttl, 0, 'n'); 512 # $ttl = $newttl if !$ttl; 513 432 514 if ($zone =~ /\.arpa$/) { 433 515 ($code,$msg) = DNSDB::_zone2cidr($zone); … … 435 517 undef, ($msg, $loc)); 436 518 my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')"); 437 $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc); 519 my $newttl; 520 ($newttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $minttl, 0, 'y'); 521 $ttl = $newttl if !$ttl; 522 $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, 523 $loc, $stamp, $expires, $stampactive); 438 524 } else { 439 525 $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)", 440 526 undef, ($zone, $loc)); 441 527 my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')"); 442 $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc); 528 my $newttl; 529 ($newttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $minttl, 0, 'n'); 530 $ttl = $newttl if !$ttl; 531 $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, 532 $loc, $stamp, $expires, $stampactive); 443 533 } 444 534 … … 457 547 $loc = '' if $loc =~ /^:+$/; 458 548 549 my $stampactive = 'n'; 550 my $expires = 'n'; 551 459 552 # note we don't check for reverse domains here, because MX records don't make any sense in reverse zones. 460 553 # if this really ever becomes an issue for someone it can be expanded to handle those weirdos … … 463 556 my $domid = $dnsdb->_hostparent($zone); 464 557 if ($domid) { 465 $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl, $loc); 466 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip; 558 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n'); 559 $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 560 $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive) if $ip; 467 561 } else { 468 562 push @deferred, $rec unless $nodefer; … … 482 576 $loc = '' if $loc =~ /^:+$/; 483 577 578 my $stampactive = 'n'; 579 my $expires = 'n'; 580 484 581 if ($fqdn =~ /\.arpa$/) { 485 582 ($code,$msg) = DNSDB::_zone2cidr($fqdn); 486 583 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg)); 487 $recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl, $loc); 584 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y'); 585 $recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 488 586 } else { 489 587 my $domid = $dnsdb->_hostparent($fqdn); 490 588 if ($domid) { 491 $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl, $loc); 589 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n'); 590 $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 492 591 } else { 493 592 push @deferred, $rec unless $nodefer; … … 508 607 $loc = '' if !$loc; 509 608 $loc = '' if $loc =~ /^:+$/; 609 610 my $stampactive = 'n'; 611 my $expires = 'n'; 612 613 ##fixme er... what do we do with an SOA with a timestamp? O_o 614 # fail for now, since there's no clean way I can see to handle this (yet) 615 # maybe (ab)use the -l flag to import as-is? 616 if ($stamp) { 617 push @deferred, $rec unless $nodefer; 618 return 0; 619 } 620 621 ##fixme: need more magic on TTL, so we can decide whether to use the minttl or newttl 622 # my $newttl; 623 # ($newttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $minttl, 0, 'n'); 510 624 511 625 if ($fqdn =~ /\.arpa$/) { … … 517 631 undef, ($msg, $loc)); 518 632 ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')"); 633 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, 2560, 0, 'y'); 519 634 # this would probably make a lot more sense to do hostmaster.$config{admindomain} 520 $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc); 521 } 522 $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl, $loc); 635 # otherwise, it's as per the tinydns defaults that work tolerably well on a small scale 636 # serial -> modtime of data file, ref -> 16384, ret -> 2048, exp -> 1048576, min -> 2560 637 $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", 638 $loc, $stamp, $expires, $stampactive); 639 } 640 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, 2560, $rdns, 'y') if !$stamp; 641 $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 523 642 ##fixme: (?) implement full conversion of tinydns . records? 524 643 # -> problem: A record for NS must be added to the appropriate *forward* zone, not the reverse 525 #$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl )644 #$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl, $stamp, $expires, $stampactive) 526 645 # ... auto-A-record simply does not make sense in reverse zones. Functionally 527 646 # I think it would work, sort of, but it's a nasty mess and anyone hosting reverse … … 537 656 undef, ($fqdn, $loc)); 538 657 ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')"); 539 $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc); 540 } 541 $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl, $loc); 542 $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip; 658 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, 2560, 0, 'n'); 659 $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", 660 $loc, $stamp, $expires, $stampactive); 661 } 662 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n') if !$stamp; 663 $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 664 $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive) if $ip; 543 665 } 544 666 … … 576 698 $loc = '' if !$loc; 577 699 $loc = '' if $loc =~ /^:+$/; 700 701 my $stampactive = 'n'; 702 my $expires = 'n'; 578 703 579 704 if ($type == 33) { … … 604 729 my $domid = $dnsdb->_hostparent($fqdn); 605 730 if ($domid) { 606 $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl, $loc) if $domid; 731 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n'); 732 $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive) if $domid; 607 733 } else { 608 734 push @deferred, $rec unless $nodefer; … … 621 747 622 748 my $fparent = $dnsdb->_hostparent($fqdn); 749 623 750 if ($fparent) { 624 $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl, $loc); 751 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $fparent, 'n'); 752 $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 625 753 } else { 626 754 push @deferred, $rec unless $nodefer; … … 636 764 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg)); 637 765 if ($rparent) { 638 $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl, $loc); 766 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y'); 767 $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 639 768 } else { 640 769 push @deferred, $rec unless $nodefer; … … 644 773 my $domid = $dnsdb->_hostparent($fqdn); 645 774 if ($domid) { 646 $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl, $loc); 775 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n'); 776 $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 647 777 } else { 648 778 push @deferred, $rec unless $nodefer; … … 664 794 my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg)); 665 795 if ($rparent) { 666 $recsth->execute(0, $rparent, "$email $txtrec", 17, "$msg", 0, 0, 0, $ttl, $loc); 796 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y'); 797 $recsth->execute(0, $rparent, "$email $txtrec", 17, "$msg", 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive ); 667 798 } else { 668 799 push @deferred, $rec unless $nodefer; … … 672 803 my $domid = $dnsdb->_hostparent($fqdn); 673 804 if ($domid) { 674 $recsth->execute($domid, 0, $fqdn, 17, "$email $txtrec", 0, 0, 0, $ttl, $loc); 805 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n'); 806 $recsth->execute($domid, 0, $fqdn, 17, "$email $txtrec", 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 675 807 } else { 676 808 push @deferred, $rec unless $nodefer; … … 688 820 my $domid = $dnsdb->_hostparent($fqdn); 689 821 if ($domid) { 690 $recsth->execute($domid, 0, $fqdn, 44, $sshfp, 0, 0, 0, $ttl, $loc); 822 ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n'); 823 $recsth->execute($domid, 0, $fqdn, 44, $sshfp, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive); 691 824 } else { 692 825 push @deferred, $rec unless $nodefer;
Note:
See TracChangeset
for help on using the changeset viewer.