Changeset 302


Ignore:
Timestamp:
04/12/12 18:26:59 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Checkpoint converting importAXFR() to support reverse zones

  • added "merge A/AAAA and PTR records" functionality
  • Flip NS and CNAME records around if importing a revzone so they display properly
  • Consider dropping CNAMEs in revzones since they're essentially an implementation hack, and not really part of the logical delegation chain
  • Clean up some commented stale code

[ log -> ticket fixup: see #26 ]

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r301 r302  
    32363236
    32373237  my $rev = 'n';
    3238 my $code = 'OK';
    3239 my $msg = 'foobar?';
     3238  my $code = 'OK';
     3239  my $msg = 'foobar?';
    32403240
    32413241  # choke on possible bad setting in ifrom
     
    33483348      $nsflag = 1 if $type eq 'NS';
    33493349
    3350 #      my @vallist = ($zone_id, $rr->name, $reverse_typemap{$type}, $rr->ttl);
    3351 
    33523350# "Primary" types:
    33533351# A, NS, CNAME, SOA, PTR(warn in forward), MX, TXT, AAAA, SRV, A6(ob), SPF
    33543352# maybe KEY
     3353
     3354# BIND supports:
     3355# [standard]
     3356# A AAAA CNAME MX NS PTR SOA TXT
     3357# [variously experimental, obsolete, or obscure]
     3358# HINFO MB(ex) MD(ob) MF(ob) MG(ex) MINFO(ex) MR(ex) NULL WKS AFSDB(ex) ISDN(ex) RP(ex) RT(ex) X25(ex) PX
     3359# ... if one can ever find the right magic to format them correctly
     3360
     3361# Net::DNS supports:
     3362# RRSIG SIG NSAP NS NIMLOC NAPTR MX MR MINFO MG MB LOC ISDN IPSECKEY HINFO
     3363# EID DNAME CNAME CERT APL AFSDB AAAA A DS NXT NSEC3PARAM NSEC3 NSEC KEY
     3364# DNSKEY DLV X25 TXT TSIG TKEY SSHFP SRV SPF SOA RT RP PX PTR NULL APL::AplItem
    33553365
    33563366# nasty big ugly case-like thing here, since we have to do *some* different
     
    33643374# hmm.  should we warn here if subdomain NS'es are left alone?
    33653375        next if ($rwns && ($rr->name eq $zone));
    3366         $val = $rr->nsdname;
     3376        if ($rev eq 'y') {
     3377          # revzones have records more or less reversed from forward zones.
     3378          my ($tmpcode,$tmpmsg) = _zone2cidr($host);
     3379          die "Error converting NS record: $tmpmsg" if $tmpcode eq 'FAIL';      # hmm.  may not make sense...
     3380          $val = "$tmpmsg";
     3381          $host = $rr->nsdname;
     3382        } else {
     3383          $val = $rr->nsdname;
     3384        }
    33673385        $nsflag = 1;
    33683386      } elsif ($type eq 'CNAME') {
    3369         $val = $rr->cname;
     3387        if ($rev eq 'y') {
     3388
     3389# hmm.  do we even want to bother with storing these at this level?  Sub-octet delegation
     3390# by CNAME is essentially a record-publication hack, and we want to just represent the
     3391# "true" logical intentions as far down the stack as we can from the UI.
     3392
     3393# really wanna spin this bit off to somewhere common, since it's likely we'll need it
     3394# for more than CNAME and PTR...
     3395$val = $host;
     3396$host = $rr->cname;
     3397if ($val =~ /\.in-addr\.arpa\.?$/) {
     3398  $val =~ s/\.in-addr\.arpa\.?$//;
     3399  $val = join '.', reverse split /\./, $val;
     3400} else {
     3401  $val =~ s/\.ip6\.arpa\.?$//;
     3402  my @nibs = reverse split /\./, $val;
     3403  $val = '';
     3404  my $nc;
     3405  foreach (@nibs) {
     3406    $val .= $_;
     3407    $val .= ":" if ++$nc % 4 == 0 && $nc < 32;
     3408  }
     3409  # canonicalize with NetAddr::IP
     3410  $val = NetAddr::IP->new($val)->addr unless $val =~ /\*$/;
     3411}
     3412       
     3413        } else {
     3414          $val = $rr->cname;
     3415        }
    33703416      } elsif ($type eq 'SOA') {
    33713417        next if $rwsoa;
     
    34193465      }
    34203466
    3421 # BIND supports:
    3422 # [standard]
    3423 # A AAAA CNAME MX NS PTR SOA TXT
    3424 # [variously experimental, obsolete, or obscure]
    3425 # HINFO MB(ex) MD(ob) MF(ob) MG(ex) MINFO(ex) MR(ex) NULL WKS AFSDB(ex) ISDN(ex) RP(ex) RT(ex) X25(ex) PX
    3426 # ... if one can ever find the right magic to format them correctly
    3427 
    3428 # Net::DNS supports:
    3429 # RRSIG SIG NSAP NS NIMLOC NAPTR MX MR MINFO MG MB LOC ISDN IPSECKEY HINFO
    3430 # EID DNAME CNAME CERT APL AFSDB AAAA A DS NXT NSEC3PARAM NSEC3 NSEC KEY
    3431 # DNSKEY DLV X25 TXT TSIG TKEY SSHFP SRV SPF SOA RT RP PX PTR NULL APL::AplItem
    3432 
    3433 #      $sth = $dbh->prepare($sql.") VALUES (".$vallen.")")
    3434 #       or die "problem preparing record insert SQL: ".$dbh->errstr."\n";
    3435 #      $sth->execute(@vallist) or die "failed to insert ".$rr->string.": ".$sth->errstr."\n";
    3436 
     3467      my $logentry = "[AXFR $zone] ";
     3468
     3469$merge = 1;
     3470if ($merge) {
     3471  if ($rev eq 'n') {
     3472    # importing a domain;  we have A and AAAA records that could be merged with matching PTR records
     3473    my $etype;
     3474    my ($erdns,$erid,$ettl) = $dbh->selectrow_array("SELECT rdns_id,record_id,ttl FROM records ".
     3475        "WHERE host=? AND val=? AND type=12",
     3476        undef, ($host, $val) );
     3477    if ($erid) {
     3478      if ($type eq 'A') {       # PTR -> A+PTR
     3479        $etype = 65280;
     3480        $logentry .= "Merged A record with existing PTR record '$host A+PTR $val', TTL $ettl";
     3481      }
     3482      if ($type eq 'AAAA') {    # PTR -> AAAA+PTR
     3483        $etype = 65281;
     3484        $logentry .= "Merged AAAA record with existing PTR record '$host AAAA+PTR $val', TTL $ettl";
     3485      }
     3486      $ettl = ($ettl < $ttl ? $ettl : $ttl);    # use lower TTL
     3487      $dbh->do("UPDATE records SET domain_id=?,ttl=?,type=? WHERE record_id=?", undef,
     3488        ($domain_id, $ettl, $etype, $erid));
     3489      $nrecs++;
     3490      _log($dbh, (group_id => $group, domain_id => $domain_id, rdns_id => $erdns, entry => $logentry) );
     3491      next;     # while axfr_next
     3492    }
     3493  } else {
     3494    # importing a revzone, we have PTR records that could be merged with matching A/AAAA records
     3495    my ($domid,$erid,$ettl,$etype) = $dbh->selectrow_array("SELECT domain_id,record_id,ttl,type FROM records ".
     3496        "WHERE host=? AND val=? AND (type=1 OR type=28)",
     3497        undef, ($host, $val) );
     3498    if ($erid) {
     3499      if ($etype == 1) {        # A -> A+PTR
     3500        $etype = 65280;
     3501        $logentry .= "Merged PTR record with existing matching A record '$host A+PTR $val', TTL $ettl";
     3502      }
     3503      if ($etype == 28) {       # AAAA -> AAAA+PTR
     3504        $etype = 65281;
     3505        $logentry .= "Merged PTR record with existing matching AAAA record '$host AAAA+PTR $val', TTL $ettl";
     3506      }
     3507      $ettl = ($ettl < $ttl ? $ettl : $ttl);    # use lower TTL
     3508      $dbh->do("UPDATE records SET rdns_id=?,ttl=?,type=? WHERE record_id=?", undef,
     3509        ($rdns_id, $ettl, $etype, $erid));
     3510      $nrecs++;
     3511      _log($dbh, (group_id => $group, domain_id => $domid, rdns_id => $rdns_id, entry => $logentry) );
     3512      next;     # while axfr_next
     3513    }
     3514  }
     3515}
     3516
     3517      # Insert the new record
    34373518      $sth->execute($domain_id, $rdns_id, $host, $reverse_typemap{$type}, $val,
    34383519        $distance, $weight, $port, $ttl);
     
    34403521      $nrecs++;
    34413522
    3442       my $logentry = "[AXFR $zone] ";
    34433523      if ($type eq 'SOA') {
    34443524        # also !$rwsoa, but if that's set, it should be impossible to get here.
     
    34873567    die "Bad zone:  No NS records!\n" if !$nsflag;
    34883568
     3569#die "horribly\n";
    34893570    $dbh->commit;
    34903571
     
    35033584  return ('WARN',"OOOK!");
    35043585} # end importAXFR()
     3586
     3587
     3588## DNSDB::importBIND()
     3589sub importBIND {
     3590} # end importBIND()
     3591
     3592
     3593## DNSDB::import_tinydns()
     3594sub import_tinydns {
     3595} # end import_tinydns()
    35053596
    35063597
Note: See TracChangeset for help on using the changeset viewer.