Changeset 37 for trunk/DNSDB.pm


Ignore:
Timestamp:
11/18/09 13:42:22 (15 years ago)
Author:
Kris Deugau
Message:

/trunk

AXFR import is now basically functional

  • could maybe use a few more recognized types
  • need to find someplace to stuff the serial from the imported SOA

Fix missing template substitution on regular new domain creation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r35 r37  
    200200    while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) {
    201201      $host =~ s/DOMAIN/$domain/g;
     202      $val =~ s/DOMAIN/$domain/g;
    202203      $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl);
    203204    }
     
    814815## DNSDB::importAXFR
    815816# Import a domain via AXFR
     817# Takes AXFR host, domain to transfer, group to put the domain in,
     818# and optionally:
     819#  - active/inactive state flag (defaults to active)
     820#  - overwrite-SOA flag (defaults to off)
     821#  - overwrite-NS flag (defaults to off, doesn't affect subdomain NS records)
     822# Returns a status code (OK, WARN, or FAIL) and message - message should be blank
     823# if status is OK, but WARN includes conditions that are not fatal but should
     824# really be reported.
    816825sub importAXFR {
    817826  my $dbh = shift;
     
    822831  my $rwsoa = shift || 0;
    823832  my $rwns = shift || 0;
     833
    824834##fixme:  add mode to delete&replace, merge+overwrite, merge new?
    825835
    826 my $nrecs = 0;
    827 my $soaflag = 0;
    828 my $nsflag = 0;
    829 my $warnmsg = '';
    830 my $ifrom;
     836  my $nrecs = 0;
     837  my $soaflag = 0;
     838  my $nsflag = 0;
     839  my $warnmsg = '';
     840  my $ifrom;
    831841
    832842  # choke on possible bad setting in ifrom
    833   # IPv4 and v6!
     843  # IPv4 and v6, and valid hostnames!
    834844  ($ifrom) = ($ifrom_in =~ /^([0-9a-f\:.]+|[0-9a-z_.-]+)$/i);
    835845  return ('FAIL', "Bad AXFR source host $ifrom")
     
    841851  local $dbh->{RaiseError} = 1;
    842852
    843   my $sth_domin = $dbh->prepare("INSERT INTO domains (domain,group_id,status) VALUES (?,?,?)");
    844   my $sth_getid = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
     853  my $sth = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
    845854  my $dom_id;
    846855
    847856# quick check to start to see if we've already got one
    848   $sth_getid->execute($domain);
    849   ($dom_id) = $sth_getid->fetchrow_array;
     857  $sth->execute($domain);
     858  ($dom_id) = $sth->fetchrow_array;
    850859
    851860  return ('FAIL', "Domain already exists") if $dom_id;
     
    856865
    857866##fixme:  serial
    858     $sth_domin->execute($domain,$group,$status);
     867    my $sth = $dbh->prepare("INSERT INTO domains (domain,group_id,status) VALUES (?,?,?)");
     868    $sth->execute($domain,$group,$status);
    859869
    860870## bizarre DBI<->Net::DNS interaction bug:
    861871## sometimes a zone will cause an immediate commit-and-exit (sort of) of the while()
     872## fixed, apparently I was doing *something* odd, but not certain what it was that
     873## caused a commit instead of barfing
    862874
    863875    # get domain id so we can do the records
    864     $sth_getid->execute($domain);
    865     ($dom_id) = $sth_getid->fetchrow_array();
     876    $sth = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
     877    $sth->execute($domain);
     878    ($dom_id) = $sth->fetchrow_array();
    866879
    867880    my $res = Net::DNS::Resolver->new;
     
    870883        or die "Couldn't begin AXFR\n";
    871884
    872 #my $foobar = $res->axfr_next() if $domain =~ /loki/;
    873 #die "outside the loop with ".$foobar->type."\n" if $domain =~ /loki/;
    874 
    875885    while (my $rr = $res->axfr_next()) {
    876886      my $type = $rr->type;
    877887
    878 #die "first record! $type\n" if $domain =~ /lok/;
    879 
    880888      my $sql = "INSERT INTO records (domain_id,host,type,ttl,val";
    881889      my $vallen = "?,?,?,?,?";
    882 #      my $host = $rr->name;
    883 #      my $ttl = $rr->ttl;
    884 #      my $val;
    885 
    886 $soaflag = 1 if $type eq 'SOA';
    887 $nsflag = 1 if $type eq 'NS';
    888 
    889 ##work
     890
     891      $soaflag = 1 if $type eq 'SOA';
     892      $nsflag = 1 if $type eq 'NS';
     893
    890894      my @vallist = ($dom_id, $rr->name, $reverse_typemap{$type}, $rr->ttl);
    891895
     
    900904        push @vallist, $rr->address;
    901905      } elsif ($type eq 'NS') {
     906# hmm.  should we warn here if subdomain NS'es are left alone?
     907        next if ($rwns && ($rr->name eq $domain));
    902908        push @vallist, $rr->nsdname;
    903909        $nsflag = 1;
     
    905911        push @vallist, $rr->cname;
    906912      } elsif ($type eq 'SOA') {
     913        next if $rwsoa;
    907914        $vallist[1] = $rr->mname.":".$rr->rname;
    908915        push @vallist, ($rr->refresh.":".$rr->retry.":".$rr->expire.":".$rr->minimum);
     
    927934        $sql .= ",distance,weight,port" if $type eq 'SRV';
    928935        $vallen .= ",?,?,?" if $type eq 'SRV';
     936        push @vallist, $rr->target;
    929937        push @vallist, $rr->priority;
    930938        push @vallist, $rr->weight;
     
    934942        push @vallist, ($rr->flags.":".$rr->protocol.":".$rr->algorithm.":".$rr->key.":".$rr->keytag.":".$rr->privatekeyname);
    935943      } else {
     944        push @vallist, $rr->rdatastr;
    936945        # Finding a different record type is not fatal.... just problematic.
     946        # We may not be able to export it correctly.
    937947        $warnmsg .= "Unusual record ".$rr->name." ($type) found\n";
    938948      }
     
    948958# DNSKEY DLV X25 TXT TSIG TKEY SSHFP SRV SPF SOA RT RP PX PTR NULL APL::AplItem
    949959
    950 # MX and SRV have known extras to stuff in.
    951       if ($type eq 'MX') {
    952         push @vallist, $rr->preference;
     960      $sth = $dbh->prepare($sql.") VALUES (".$vallen.")") or die "problem preparing record insert SQL\n";
     961      $sth->execute(@vallist) or die "failed to insert ".$rr->string.": ".$sth->errstr."\n";
     962
     963      $nrecs++;
     964
     965    } # while axfr_next
     966
     967    # Overwrite SOA record
     968    if ($rwsoa) {
     969      $soaflag = 1;
     970      my $sthgetsoa = $dbh->prepare("SELECT host,val,ttl FROM default_records WHERE group_id=? AND type=?");
     971      my $sthputsoa = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,ttl) VALUES (?,?,?,?,?)");
     972      $sthgetsoa->execute($group,$reverse_typemap{SOA});
     973      while (my ($host,$val,$ttl) = $sthgetsoa->fetchrow_array()) {
     974        $host =~ s/DOMAIN/$domain/g;
     975        $val =~ s/DOMAIN/$domain/g;
     976        $sthputsoa->execute($dom_id,$host,$reverse_typemap{SOA},$val,$ttl);
    953977      }
    954       if ($type eq 'SRV') {
     978    }
     979
     980    # Overwrite NS records
     981    if ($rwns) {
     982      $nsflag = 1;
     983      my $sthgetns = $dbh->prepare("SELECT host,val,ttl FROM default_records WHERE group_id=? AND type=?");
     984      my $sthputns = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,ttl) VALUES (?,?,?,?,?)");
     985      $sthgetns->execute($group,$reverse_typemap{NS});
     986      while (my ($host,$val,$ttl) = $sthgetns->fetchrow_array()) {
     987        $host =~ s/DOMAIN/$domain/g;
     988        $val =~ s/DOMAIN/$domain/g;
     989        $sthputns->execute($dom_id,$host,$reverse_typemap{NS},$val,$ttl);
    955990      }
    956 
    957       my $sth_rr = $dbh->prepare($sql.") VALUES (".$vallen.")");
    958       $sth_rr->execute(@vallist) or die "failed to insert ".$rr->string.": ".$sth_rr->errstr."\n" if $sth_rr->err;
    959 
    960       if ($type eq 'SOA') {
    961       }
    962 
    963 #die "die.die.die!\n";
    964 
    965       $nrecs++;
    966 
    967     } # while axfr_next
     991    }
    968992
    969993    die "No records found;  either $ifrom is not authoritative or doesn't allow transfers\n" if !$nrecs;
     
    971995    die "Bad zone:  No NS records!\n" if !$nsflag;
    972996
    973 $dbh->rollback;
    974 #    $dbh->commit;
    975 
    976 #    die "Bad zone.  BAD zone!  No cookie!\n";
    977 #die "don't insert me!  I'm nasty, I am!\n";
     997    $dbh->commit;
     998
    978999  };
    9791000
     
    9811002    my $msg = $@;
    9821003    eval { $dbh->rollback; };
    983 #    $dbh->do("delete from domains where domain='$domain'");
    9841004    return ('FAIL',$msg." $warnmsg");
    9851005  } else {
    986 #    $dbh->do("delete from domains where domain='$domain'");
    9871006    return ('WARN', $warnmsg) if $warnmsg;
    9881007    return ('OK',"ook");
    9891008  }
    9901009
     1010  # it should be impossible to get here.
    9911011  return ('WARN',"OOOK!");
    9921012} # end importAXFR()
Note: See TracChangeset for help on using the changeset viewer.