addns: Async ads_dns_lookup_srv
authorVolker Lendecke <vl@samba.org>
Wed, 3 Jan 2018 12:26:54 +0000 (13:26 +0100)
committerVolker Lendecke <vl@samba.org>
Tue, 15 Jan 2019 06:53:22 +0000 (07:53 +0100)
Use dns_lookup_send/recv to get SRV records. This avoids synchronous libresolv
calls and provides the infrastructure to get dsgetdcname async.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/addns/dnsquery.c
lib/addns/dnsquery.h
lib/addns/wscript_build

index 30e5a564eec5158f6dd135d773be5b7ba801d99f..d91c139d22167fb2baebd06135d8350c364d21ff 100644 (file)
@@ -21,6 +21,9 @@
 #include "includes.h"
 #include "lib/util/util_net.h"
 #include "lib/util/tsort.h"
+#include "librpc/gen_ndr/dns.h"
+#include "libcli/dns/dns_lookup.h"
+#include "lib/util/tevent_ntstatus.h"
 #include "dnsquery.h"
 
 /* AIX resolv.h uses 'class' in struct ns_rr */
@@ -168,59 +171,6 @@ static bool ads_dns_parse_rr( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
 /*********************************************************************
 *********************************************************************/
 
-static bool ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
-                       uint8_t **ptr, struct dns_rr_srv *srv )
-{
-       struct dns_rr rr;
-       uint8_t *p;
-       char dcname[MAX_DNS_NAME_LENGTH];
-       int namelen;
-
-       if ( !start || !end || !srv || !*ptr)
-               return -1;
-
-       /* Parse the RR entry.  Coming out of the this, ptr is at the beginning
-          of the next record */
-
-       if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
-               DEBUG(1,("ads_dns_parse_rr_srv: Failed to parse RR record\n"));
-               return false;
-       }
-
-       if ( rr.type != T_SRV ) {
-               DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n",
-                                       rr.type));
-               return false;
-       }
-
-       p = rr.rdata;
-
-       srv->priority = RSVAL(p, 0);
-       srv->weight   = RSVAL(p, 2);
-       srv->port     = RSVAL(p, 4);
-
-       p += 6;
-
-       namelen = dn_expand( start, end, p, dcname, sizeof(dcname) );
-       if ( namelen < 0 ) {
-               DEBUG(1,("ads_dns_parse_rr_srv: Failed to uncompress name!\n"));
-               return false;
-       }
-
-       srv->hostname = talloc_strdup( ctx, dcname );
-
-       DEBUG(10,("ads_dns_parse_rr_srv: Parsed %s [%u, %u, %u]\n",
-                 srv->hostname,
-                 srv->priority,
-                 srv->weight,
-                 srv->port));
-
-       return true;
-}
-
-/*********************************************************************
-*********************************************************************/
-
 static bool ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
                        uint8_t **ptr, struct dns_rr_ns *nsrec )
 {
@@ -385,195 +335,180 @@ static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
        return last_dns_status;
 }
 
-/*********************************************************************
- Simple wrapper for a DNS SRV query
-*********************************************************************/
+struct ads_dns_lookup_srv_state {
+       struct dns_rr_srv *srvs;
+       size_t num_srvs;
+};
 
-NTSTATUS ads_dns_lookup_srv(TALLOC_CTX *ctx,
-                               const char *name,
-                               struct dns_rr_srv **dclist,
-                               int *numdcs)
+static void ads_dns_lookup_srv_done(struct tevent_req *subreq);
+
+struct tevent_req *ads_dns_lookup_srv_send(TALLOC_CTX *mem_ctx,
+                                          struct tevent_context *ev,
+                                          const char *name)
 {
-       uint8_t *buffer = NULL;
-       int resp_len = 0;
-       struct dns_rr_srv *dcs = NULL;
-       int query_count, answer_count, auth_count, additional_count;
-       uint8_t *p = buffer;
-       int rrnum;
-       int idx = 0;
-       NTSTATUS status;
+       struct tevent_req *req, *subreq;
+       struct ads_dns_lookup_srv_state *state;
 
-       if ( !ctx || !name || !dclist ) {
-               return NT_STATUS_INVALID_PARAMETER;
+       req = tevent_req_create(mem_ctx, &state,
+                               struct ads_dns_lookup_srv_state);
+       if (req == NULL) {
+               return NULL;
        }
 
-       /* Send the request.  May have to loop several times in case
-          of large replies */
+       subreq = dns_lookup_send(
+               state,
+               ev,
+               NULL,
+               name,
+               DNS_QCLASS_IN,
+               DNS_QTYPE_SRV);
 
-       status = dns_send_req( ctx, name, T_SRV, &buffer, &resp_len );
-       if ( !NT_STATUS_IS_OK(status) ) {
-               DEBUG(3,("ads_dns_lookup_srv: Failed to send DNS query (%s)\n",
-                       nt_errstr(status)));
-               return status;
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
        }
-       p = buffer;
-
-       /* For some insane reason, the ns_initparse() et. al. routines are only
-          available in libresolv.a, and not the shared lib.  Who knows why....
-          So we have to parse the DNS reply ourselves */
-
-       /* Pull the answer RR's count from the header.
-        * Use the NMB ordering macros */
-
-       query_count      = RSVAL( p, 4 );
-       answer_count     = RSVAL( p, 6 );
-       auth_count       = RSVAL( p, 8 );
-       additional_count = RSVAL( p, 10 );
-
-       DEBUG(4,("ads_dns_lookup_srv: "
-               "%d records returned in the answer section.\n",
-               answer_count));
+       tevent_req_set_callback(subreq, ads_dns_lookup_srv_done, req);
+       return req;
+}
 
-       if (answer_count) {
-               if ((dcs = talloc_zero_array(ctx, struct dns_rr_srv,
-                                               answer_count)) == NULL ) {
-                       DEBUG(0,("ads_dns_lookup_srv: "
-                               "talloc() failure for %d char*'s\n",
-                               answer_count));
-                       return NT_STATUS_NO_MEMORY;
-               }
-       } else {
-               dcs = NULL;
+static void ads_dns_lookup_srv_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct ads_dns_lookup_srv_state *state = tevent_req_data(
+               req, struct ads_dns_lookup_srv_state);
+       int ret;
+       struct dns_name_packet *reply;
+       uint16_t i, idx;
+
+       ret = dns_lookup_recv(subreq, state, &reply);
+       TALLOC_FREE(subreq);
+       if (ret != 0) {
+               tevent_req_nterror(req, map_nt_error_from_unix_common(ret));
+               return;
        }
 
-       /* now skip the header */
-
-       p += NS_HFIXEDSZ;
-
-       /* parse the query section */
-
-       for ( rrnum=0; rrnum<query_count; rrnum++ ) {
-               struct dns_query q;
-
-               if (!ads_dns_parse_query(ctx, buffer,
-                                       buffer+resp_len, &p, &q)) {
-                       DEBUG(1,("ads_dns_lookup_srv: "
-                                "Failed to parse query record [%d]!\n", rrnum));
-                       return NT_STATUS_UNSUCCESSFUL;
+       for (i=0; i<reply->ancount; i++) {
+               if (reply->answers[i].rr_type == DNS_QTYPE_SRV) {
+                       state->num_srvs += 1;
                }
        }
 
-       /* now we are at the answer section */
-
-       for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
-               if (!ads_dns_parse_rr_srv(ctx, buffer, buffer+resp_len,
-                                       &p, &dcs[rrnum])) {
-                       DEBUG(1,("ads_dns_lookup_srv: "
-                                "Failed to parse answer recordi [%d]!\n", rrnum));
-                       return NT_STATUS_UNSUCCESSFUL;
-               }
+       state->srvs = talloc_array(state, struct dns_rr_srv, state->num_srvs);
+       if (tevent_req_nomem(state->srvs, req)) {
+               return;
        }
-       idx = rrnum;
 
-       /* Parse the authority section */
-       /* just skip these for now */
+       idx = 0;
 
-       for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
-               struct dns_rr rr;
+       for (i=0; i<reply->ancount; i++) {
+               struct dns_res_rec *an = &reply->answers[i];
+               struct dns_rr_srv *dst = &state->srvs[idx];
+               struct dns_srv_record *src;
 
-               if (!ads_dns_parse_rr( ctx, buffer,
-                                       buffer+resp_len, &p, &rr)) {
-                       DEBUG(1,("ads_dns_lookup_srv: "
-                                "Failed to parse authority record! [%d]\n", rrnum));
-                       return NT_STATUS_UNSUCCESSFUL;
+               if (an->rr_type != DNS_QTYPE_SRV) {
+                       continue;
                }
+               src = &an->rdata.srv_record;
+
+               *dst = (struct dns_rr_srv) {
+                       .hostname = talloc_move(state->srvs, &src->target),
+                       .priority = src->priority,
+                       .weight = src->weight,
+                       .port = src->port,
+               };
+               idx += 1;
        }
 
-       /* Parse the additional records section */
-
-       for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
-               struct dns_rr rr;
-               int i;
+       for (i=0; i<reply->arcount; i++) {
+               struct dns_res_rec *ar = &reply->additional[i];
+               struct sockaddr_storage addr;
+               bool ok;
+               size_t j;
 
-               if (!ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
-                                       &p, &rr)) {
-                       DEBUG(1,("ads_dns_lookup_srv: Failed "
-                                "to parse additional records section! [%d]\n", rrnum));
-                       return NT_STATUS_UNSUCCESSFUL;
+               ok = dns_res_rec_get_sockaddr(ar, &addr);
+               if (!ok) {
+                       continue;
                }
 
-               /* Only interested in A or AAAA records as a shortcut for having
-                * to come back later and lookup the name. For multi-homed
-                * hosts, the number of additional records and exceed the
-                * number of answer records. */
+               for (j=0; j<state->num_srvs; j++) {
+                       struct dns_rr_srv *srv = &state->srvs[j];
+                       struct sockaddr_storage *tmp;
 
-               if (rr.type != T_A || rr.rdatalen != 4) {
-#if defined(HAVE_IPV6)
-                       /* RFC2874 defines A6 records. This
-                        * requires recusive and horribly complex lookups.
-                        * Bastards. Ignore this for now.... JRA.
-                        * Luckily RFC3363 reprecates A6 records.
-                        */
-                       if (rr.type != T_AAAA || rr.rdatalen != 16)
-#endif
+                       if (strcmp(srv->hostname, ar->name) != 0) {
                                continue;
-               }
+                       }
 
-               for ( i=0; i<idx; i++ ) {
-                       if ( strcmp( rr.hostname, dcs[i].hostname ) == 0 ) {
-                               int num_ips = dcs[i].num_ips;
-                               struct sockaddr_storage *tmp_ss_s;
-
-                               /* allocate new memory */
-
-                               if (dcs[i].num_ips == 0) {
-                                       if ((dcs[i].ss_s = talloc_array(dcs,
-                                               struct sockaddr_storage, 1 ))
-                                                       == NULL ) {
-                                               return NT_STATUS_NO_MEMORY;
-                                       }
-                               } else {
-                                       if ((tmp_ss_s = talloc_realloc(dcs,
-                                                       dcs[i].ss_s,
-                                                       struct sockaddr_storage,
-                                                       dcs[i].num_ips+1))
-                                                               == NULL ) {
-                                               return NT_STATUS_NO_MEMORY;
-                                       }
-
-                                       dcs[i].ss_s = tmp_ss_s;
-                               }
-                               dcs[i].num_ips++;
+                       tmp = talloc_realloc(
+                               state->srvs,
+                               srv->ss_s,
+                               struct sockaddr_storage,
+                               srv->num_ips+1);
 
-                               /* copy the new IP address */
-                               if (rr.type == T_A) {
-                                       struct in_addr ip;
-                                       memcpy(&ip, rr.rdata, 4);
-                                       in_addr_to_sockaddr_storage(
-                                                       &dcs[i].ss_s[num_ips],
-                                                       ip);
-                               }
-#if defined(HAVE_IPV6)
-                               if (rr.type == T_AAAA) {
-                                       struct in6_addr ip6;
-                                       memcpy(&ip6, rr.rdata, rr.rdatalen);
-                                       in6_addr_to_sockaddr_storage(
-                                                       &dcs[i].ss_s[num_ips],
-                                                       ip6);
-                               }
-#endif
+                       if (tevent_req_nomem(tmp, req)) {
+                               return;
                        }
+                       srv->ss_s = tmp;
+
+                       srv->ss_s[srv->num_ips] = addr;
+                       srv->num_ips += 1;
                }
        }
 
-       TYPESAFE_QSORT(dcs, idx, dnssrvcmp );
+       TYPESAFE_QSORT(state->srvs, state->num_srvs, dnssrvcmp);
+
+       tevent_req_done(req);
+}
 
-       *dclist = dcs;
-       *numdcs = idx;
+NTSTATUS ads_dns_lookup_srv_recv(struct tevent_req *req,
+                                TALLOC_CTX *mem_ctx,
+                                struct dns_rr_srv **srvs,
+                                size_t *num_srvs)
+{
+       struct ads_dns_lookup_srv_state *state = tevent_req_data(
+               req, struct ads_dns_lookup_srv_state);
+       NTSTATUS status;
 
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       *srvs = talloc_move(mem_ctx, &state->srvs);
+       *num_srvs = state->num_srvs;
+       tevent_req_received(req);
        return NT_STATUS_OK;
 }
 
+/*********************************************************************
+ Simple wrapper for a DNS SRV query
+*********************************************************************/
+
+NTSTATUS ads_dns_lookup_srv(TALLOC_CTX *ctx,
+                               const char *name,
+                               struct dns_rr_srv **dclist,
+                               int *numdcs)
+{
+       struct tevent_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+       size_t num_srvs;
+
+       ev = samba_tevent_context_init(ctx);
+       if (ev == NULL) {
+               goto fail;
+       }
+       req = ads_dns_lookup_srv_send(ev, ev, name);
+       if (req == NULL) {
+               goto fail;
+       }
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+               goto fail;
+       }
+       status = ads_dns_lookup_srv_recv(req, ctx, dclist, &num_srvs);
+       *numdcs = num_srvs;     /* size_t->int */
+fail:
+       TALLOC_FREE(ev);
+       return status;
+}
+
 /*********************************************************************
  Simple wrapper for a DNS NS query
 *********************************************************************/
index 213ed325a6e36f77d6138e0aaf98e7d5676955b1..8dc2806f91bfe6edaffcf8b08bb25ff7aac65f53 100644 (file)
 #ifndef _ADS_DNS_H
 #define _ADS_DNS_H
 
+#include "replace.h"
+#include <tevent.h>
 #include "libcli/dns/dns.h"
 
 /* The following definitions come from libads/dns.c  */
 
+struct tevent_req *ads_dns_lookup_srv_send(TALLOC_CTX *mem_ctx,
+                                          struct tevent_context *ev,
+                                          const char *name);
+NTSTATUS ads_dns_lookup_srv_recv(struct tevent_req *req,
+                                TALLOC_CTX *mem_ctx,
+                                struct dns_rr_srv **srvs,
+                                size_t *num_srvs);
 NTSTATUS ads_dns_lookup_srv(TALLOC_CTX *ctx,
                                const char *name,
                                struct dns_rr_srv **dclist,
index b1948ba35b18cf28901c56a831cbfb578b34f4f9..c38e93ccc95e37acbcf760719c516d7a4d5bf4c6 100644 (file)
@@ -2,6 +2,6 @@
 
 bld.SAMBA_LIBRARY('addns',
                    source='dnsquery.c dnsrecord.c dnsutils.c dnssock.c dnsgss.c dnsmarshall.c error.c',
-                   public_deps='samba-util gssapi ndr resolv',
+                   public_deps='samba-util gssapi ndr resolv dns_lookup',
                    private_library=True,
                    vars=locals())