From a08dcda2ce63ce527bbd680f12810bff66213339 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Wed, 13 Mar 2013 15:13:46 +0100
Subject: [PATCH 2/4] DNS sites support - simple SRV lookup plugin

https://fedorahosted.org/sssd/ticket/1032

This plugin mimics the current behaviour.

If discovery_domain is set it is the only domain that is tried.
If discovery_domain is not set, we try to autodetect domain first
and if that fails or SRV lookup on this domain fails, we fallback
to SSSD domain name.
---
 Makefile.am                   |   1 +
 src/providers/fail_over.h     |  26 ++
 src/providers/fail_over_srv.c | 642 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 669 insertions(+)
 create mode 100644 src/providers/fail_over_srv.c

diff --git a/Makefile.am b/Makefile.am
index 39e6e0745b778cead606f536759ce18576af24ce..fe800fd64d0b8c8098bff9d57fb8a79220e5b99b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -322,6 +322,7 @@ endif
 
 SSSD_FAILOVER_OBJ = \
     src/providers/fail_over.c \
+    src/providers/fail_over_srv.c \
     $(SSSD_RESOLV_OBJ)
 
 SSSD_LIBS = \
diff --git a/src/providers/fail_over.h b/src/providers/fail_over.h
index 1ff06688237b5fde1d78878f45bd87f7ca1df8cf..0ab87d3230408b420a3d78e30b335ab9902c7407 100644
--- a/src/providers/fail_over.h
+++ b/src/providers/fail_over.h
@@ -249,4 +249,30 @@ void fo_set_srv_lookup_plugin(struct fo_ctx *ctx,
                               srv_lookup_plugin_recv_t recv_fn,
                               void *pvt);
 
+/* Simple SRV lookup plugin */
+
+struct resolve_srv_simple_ctx;
+
+struct resolve_srv_simple_ctx *
+resolve_srv_simple_context_init(TALLOC_CTX *mem_ctx,
+                                struct resolv_ctx *resolv_ctx,
+                                enum host_database *host_dbs,
+                                enum restrict_family family_order,
+                                char *sssd_domain);
+
+struct tevent_req *resolve_srv_simple_send(TALLOC_CTX *mem_ctx,
+                                           struct tevent_context *ev,
+                                           const char *service,
+                                           const char *protocol,
+                                           const char *discovery_domain,
+                                           void *pvt);
+
+errno_t resolve_srv_simple_recv(TALLOC_CTX *mem_ctx,
+                                struct tevent_req *req,
+                                char **_dns_domain,
+                                struct fo_server_info **_primary_servers,
+                                size_t *_num_primary_servers,
+                                struct fo_server_info **_backup_servers,
+                                size_t *_num_backup_servers);
+
 #endif /* !__FAIL_OVER_H__ */
diff --git a/src/providers/fail_over_srv.c b/src/providers/fail_over_srv.c
new file mode 100644
index 0000000000000000000000000000000000000000..7550011030208ff52a374681e58f45d0aea47d15
--- /dev/null
+++ b/src/providers/fail_over_srv.c
@@ -0,0 +1,642 @@
+/*
+    Authors:
+        Pavel Březina <pbrezina@redhat.com>
+
+    Copyright (C) 2013 Red Hat
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <string.h>
+#include <talloc.h>
+#include <tevent.h>
+#include <unistd.h>
+#include <sys/socket.h>
+
+#include "util/util.h"
+#include "resolv/async_resolv.h"
+#include "providers/fail_over.h"
+
+struct resolve_srv_domain_state {
+    char *fqdn;
+    char hostname[HOST_NAME_MAX];
+};
+
+static void resolve_srv_domain_done(struct tevent_req *subreq);
+
+static struct tevent_req *
+resolve_srv_domain_send(TALLOC_CTX *mem_ctx,
+                        struct tevent_context *ev,
+                        struct resolv_ctx *resolv_ctx,
+                        enum host_database *host_dbs,
+                        enum restrict_family family_order)
+{
+    struct resolve_srv_domain_state *state = NULL;
+    struct tevent_req *req = NULL;
+    struct tevent_req *subreq = NULL;
+    errno_t ret;
+
+    req = tevent_req_create(mem_ctx, &state,
+                            struct resolve_srv_domain_state);
+    if (req == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, ("tevent_req_create() failed\n"));
+        return NULL;
+    }
+
+    state->fqdn = NULL;
+
+    ret = gethostname(state->hostname, HOST_NAME_MAX);
+    if (ret) {
+        ret = errno;
+        DEBUG(SSSDBG_CRIT_FAILURE, ("gethostname() failed: [%d]: %s\n",
+                                    ret, strerror(ret)));
+        goto immediately;
+    }
+    state->hostname[HOST_NAME_MAX-1] = '\0';
+    DEBUG(SSSDBG_TRACE_LIBS, ("Host name is: %s\n", state->hostname));
+
+    subreq = resolv_gethostbyname_send(state, ev, resolv_ctx, state->hostname,
+                                       family_order, host_dbs);
+    if (subreq == NULL) {
+        talloc_zfree(req);
+        return NULL;
+    }
+
+    tevent_req_set_callback(subreq, resolve_srv_domain_done, req);
+
+    return req;
+
+immediately:
+    if (ret == EOK) {
+        tevent_req_done(req);
+    } else {
+        tevent_req_error(req, ret);
+    }
+    tevent_req_post(req, ev);
+
+    return req;
+}
+
+static void resolve_srv_domain_done(struct tevent_req *subreq)
+{
+    struct resolve_srv_domain_state *state = NULL;
+    struct tevent_req *req = NULL;
+    struct resolv_hostent *rhostent;
+    int resolv_status;
+    errno_t ret;
+
+    req = tevent_req_callback_data(subreq, struct tevent_req);
+    state = tevent_req_data(req, struct resolve_srv_domain_state);
+
+    ret = resolv_gethostbyname_recv(subreq, req, &resolv_status,
+                                    NULL, &rhostent);
+    talloc_zfree(subreq);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_OP_FAILURE,
+              ("Could not get fully qualified name for host name %s "
+               "error [%d]: %s, resolver returned: [%d]: %s\n",
+               state->hostname, ret, strerror(ret), resolv_status,
+               resolv_strerror(resolv_status)));
+        /* We'll proceed with hostname in this case */
+    } else {
+        DEBUG(SSSDBG_TRACE_LIBS, ("The FQDN is: %s\n", rhostent->name));
+        state->fqdn = talloc_steal(state, rhostent->name);
+        talloc_zfree(rhostent);
+    }
+
+    tevent_req_done(req);
+}
+
+static errno_t resolve_srv_domain_recv(TALLOC_CTX *mem_ctx,
+                                       struct tevent_req *req,
+                                       char **_dns_domain)
+{
+    struct resolve_srv_domain_state *state = NULL;
+    char *dns_domain = NULL;
+    char *fqdn = NULL;
+    char *domptr = NULL;
+
+    state = tevent_req_data(req, struct resolve_srv_domain_state);
+
+    TEVENT_REQ_RETURN_ON_ERROR(req);
+
+    fqdn = state->fqdn;
+    if (fqdn == NULL) {
+        /* we were unable to lookup fqdn, use hostname instead */
+        fqdn = state->hostname;
+    }
+
+    domptr = strchr(fqdn, '.');
+    if (domptr == NULL || (*(domptr+1) == '\0')) {
+        /* If the FQDN did not contain a dot or the dot was the last character
+         * (broken DNS server perhaps) */
+        dns_domain = talloc_strdup(state, fqdn);
+    } else {
+        dns_domain = talloc_strdup(state, domptr + 1);
+    }
+
+    if (dns_domain == NULL) {
+        return ENOMEM;
+    }
+
+    *_dns_domain = talloc_steal(mem_ctx, dns_domain);
+
+    return EOK;
+}
+
+struct resolve_srv_state {
+    struct tevent_context *ev;
+    struct resolv_ctx *resolv_ctx;
+    char *service;
+    char *protocol;
+    char *detected_domain;
+    const char **discovery_domains;
+    int domain_index;
+
+    struct fo_server_info *servers;
+    size_t num_servers;
+};
+
+static void resolve_srv_step1_done(struct tevent_req *subreq);
+static errno_t resolve_srv_next_domain(struct tevent_req *req);
+static void resolve_srv_done(struct tevent_req *subreq);
+
+static struct tevent_req *resolve_srv_send(TALLOC_CTX *mem_ctx,
+                                           struct tevent_context *ev,
+                                           struct resolv_ctx *resolv_ctx,
+                                           enum host_database *host_dbs,
+                                           enum restrict_family family_order,
+                                           const char *service,
+                                           const char *protocol,
+                                           const char **discovery_domains,
+                                           bool detect_first_domain)
+{
+    struct resolve_srv_state *state = NULL;
+    struct tevent_req *req = NULL;
+    struct tevent_req *subreq = NULL;
+    errno_t ret;
+
+    req = tevent_req_create(mem_ctx, &state, struct resolve_srv_state);
+    if (req == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, ("tevent_req_create() failed\n"));
+        return NULL;
+    }
+
+    if (resolv_ctx == NULL || service == NULL || protocol == NULL) {
+        ret = EINVAL;
+        goto immediately;
+    }
+
+    if (detect_first_domain && host_dbs == NULL) {
+        ret = EINVAL;
+        goto immediately;
+    }
+
+    state->ev = ev;
+    state->resolv_ctx = resolv_ctx;
+    state->discovery_domains = discovery_domains;
+
+    state->service = talloc_strdup(state, service);
+    if (state->service == NULL) {
+        ret = ENOMEM;
+        goto immediately;
+    }
+
+    state->protocol = talloc_strdup(state, protocol);
+    if (state->protocol == NULL) {
+        ret = ENOMEM;
+        goto immediately;
+    }
+
+    if (detect_first_domain) {
+        DEBUG(SSSDBG_TRACE_FUNC, ("Looking up discovery domain for service "
+                                  "'%s'\n", state->service));
+
+        subreq = resolve_srv_domain_send(state, ev, resolv_ctx,
+                                         host_dbs, family_order);
+        if (subreq == NULL) {
+            ret = ENOMEM;
+            goto immediately;
+        }
+
+        tevent_req_set_callback(subreq, resolve_srv_step1_done, req);
+    } else {
+        state->domain_index = 0;
+        ret = resolve_srv_next_domain(req);
+        if (ret != EAGAIN) {
+            goto immediately;
+        }
+    }
+
+    return req;
+
+immediately:
+    if (ret == EOK) {
+        tevent_req_done(req);
+    } else {
+        tevent_req_error(req, ret);
+    }
+    tevent_req_post(req, ev);
+
+    return req;
+}
+
+static void resolve_srv_step1_done(struct tevent_req *subreq)
+{
+    struct resolve_srv_state *state = NULL;
+    struct tevent_req *req = NULL;
+    errno_t ret;
+
+    req = tevent_req_callback_data(subreq, struct tevent_req);
+    state = tevent_req_data(req, struct resolve_srv_state);
+
+    ret = resolve_srv_domain_recv(state, subreq, &state->detected_domain);
+    talloc_zfree(subreq);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, ("Unable to resolve DNS discovery domain "
+                                    "[%d]: %s\n", ret, strerror(ret)));
+        goto done;
+    }
+
+    state->domain_index = -1;
+    ret = resolve_srv_next_domain(req);
+
+done:
+    if (ret == EOK) {
+        /* EOK should not be returned at this point as we have at least
+         * one domain to search */
+        DEBUG(SSSDBG_CRIT_FAILURE, ("Unexcepted EOK was returned\n"));
+        tevent_req_error(req, EFAULT);
+    }
+    if (ret != EAGAIN) {
+        tevent_req_error(req, ret);
+    }
+
+    return;
+}
+
+static const char *resolve_srv_get_domain(struct resolve_srv_state *state,
+                                          int index)
+{
+    if (index == -1) {
+        return state->detected_domain;
+    }
+
+    if (state->discovery_domains != NULL) {
+        return state->discovery_domains[index];
+    }
+
+    return NULL;
+}
+
+static errno_t resolve_srv_next_domain(struct tevent_req *req)
+{
+    struct resolve_srv_state *state = NULL;
+    struct tevent_req *subreq = NULL;
+    const char *domain = NULL;
+    char *query = NULL;
+    errno_t ret;
+
+    state = tevent_req_data(req, struct resolve_srv_state);
+
+    do {
+        domain = resolve_srv_get_domain(state, state->domain_index);
+        if (domain == NULL) {
+            /* we're done */
+            ret = EOK;
+            goto done;
+        }
+
+        state->domain_index++;
+
+        /* detected domain may be the same as one of the specified domains,
+         * make sure we don't try the same domain twice */
+    } while (state->detected_domain != NULL
+                && domain != state->detected_domain
+                && strcmp(domain, state->detected_domain) == 0);
+
+    query = talloc_asprintf(state, "_%s._%s.%s", state->service,
+                            state->protocol, domain);
+    if (query == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    DEBUG(SSSDBG_TRACE_FUNC, ("SRV resolution of service '%s'. Will use DNS "
+          "discovery domain '%s'\n", state->service, domain));
+
+    subreq = resolv_getsrv_send(state, state->ev,
+                                state->resolv_ctx, query);
+    if (subreq == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    tevent_req_set_callback(subreq, resolve_srv_done, req);
+
+    ret = EAGAIN;
+
+done:
+    if (ret != EAGAIN) {
+        talloc_free(query);
+    }
+
+    return ret;
+}
+
+static void resolve_srv_done(struct tevent_req *subreq)
+{
+    struct resolve_srv_state *state = NULL;
+    struct tevent_req *req = NULL;
+    struct ares_srv_reply *reply_list = NULL;
+    struct ares_srv_reply *record = NULL;
+    int status;
+    int i;
+    errno_t ret;
+
+    req = tevent_req_callback_data(subreq, struct tevent_req);
+    state = tevent_req_data(req, struct resolve_srv_state);
+
+    ret = resolv_getsrv_recv(state, subreq, &status, NULL, &reply_list);
+    talloc_zfree(subreq);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_OP_FAILURE, ("SRV query failed [%d]: %s\n",
+                                  status, resolv_strerror(status)));
+
+        if (status == ARES_ENOTFOUND) {
+            /* continue with next discovery domain */
+            ret = resolve_srv_next_domain(req);
+            if (ret == EOK) {
+                /* there are no more domains to try */
+                ret = ERR_DNS_SRV_NOT_FOUND;
+            }
+
+            goto done;
+        }
+
+        /* critical error when fetching SRV record */
+        ret = ERR_DNS_SRV_LOOKUP_ERROR;
+        goto done;
+    }
+
+    DEBUG(SSSDBG_TRACE_FUNC, ("Got answer. Processing...\n"));
+
+    /* sort and store the answer */
+    ret = resolv_sort_srv_reply(state, &reply_list);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, ("Could not sort the answers from DNS "
+                                    "[%d]: %s\n", ret, strerror(ret)));
+        goto done;
+    }
+
+    state->num_servers = 0;
+    for (record = reply_list; record != NULL; record = record->next) {
+        state->num_servers++;
+    }
+
+    state->servers = talloc_array(state, struct fo_server_info,
+                                  state->num_servers);
+    if (state->servers == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    for (record = reply_list, i = 0;
+         record != NULL;
+         record = record->next, i++) {
+        state->servers[i].host = talloc_steal(state, record->host);
+        state->servers[i].port = record->port;
+    }
+
+    talloc_zfree(reply_list);
+
+done:
+    if (ret == EOK) {
+        tevent_req_done(req);
+    } else if (ret != EAGAIN) {
+        tevent_req_error(req, ret);
+    }
+
+    return;
+}
+
+static errno_t resolve_srv_recv(TALLOC_CTX *mem_ctx,
+                                struct tevent_req *req,
+                                char **_dns_domain,
+                                struct fo_server_info **_servers,
+                                size_t *_num_servers)
+{
+    struct resolve_srv_state *state = NULL;
+    const char *domain = NULL;
+    char *dup_domain = NULL;
+
+    state = tevent_req_data(req, struct resolve_srv_state);
+
+    TEVENT_REQ_RETURN_ON_ERROR(req);
+
+    if (_dns_domain != NULL) {
+        /* domain_index now points to selected domain + 1 */
+        domain = resolve_srv_get_domain(state, state->domain_index - 1);
+        if (domain == NULL) {
+            /* we should not get here */
+            return EFAULT;
+        }
+
+        dup_domain = talloc_strdup(mem_ctx, domain);
+        if (dup_domain == NULL) {
+            return ENOMEM;
+        }
+
+        *_dns_domain = dup_domain;
+    }
+
+    if (_servers != NULL) {
+        *_servers = talloc_steal(mem_ctx, state->servers);
+    }
+
+    if (_num_servers != NULL) {
+        *_num_servers = state->num_servers;
+    }
+
+    return EOK;
+}
+
+struct resolve_srv_simple_ctx {
+    struct resolv_ctx *resolv_ctx;
+    enum host_database *host_dbs;
+    enum restrict_family family_order;
+    char *sssd_domain;
+};
+
+struct resolve_srv_simple_state {
+    char *dns_domain;
+    struct fo_server_info *servers;
+    size_t num_servers;
+};
+
+static void resolve_srv_simple_done(struct tevent_req *subreq);
+
+struct resolve_srv_simple_ctx *
+resolve_srv_simple_context_init(TALLOC_CTX *mem_ctx,
+                                struct resolv_ctx *resolv_ctx,
+                                enum host_database *host_dbs,
+                                enum restrict_family family_order,
+                                char *sssd_domain)
+{
+    struct resolve_srv_simple_ctx *ctx = NULL;
+
+    ctx = talloc_zero(mem_ctx, struct resolve_srv_simple_ctx);
+    if (ctx == NULL) {
+        return NULL;
+    }
+
+    ctx->resolv_ctx = resolv_ctx;
+    ctx->host_dbs = host_dbs;
+    ctx->family_order = family_order;
+    ctx->sssd_domain = sssd_domain;
+
+    return ctx;
+}
+
+struct tevent_req *resolve_srv_simple_send(TALLOC_CTX *mem_ctx,
+                                           struct tevent_context *ev,
+                                           const char *service,
+                                           const char *protocol,
+                                           const char *discovery_domain,
+                                           void *pvt)
+{
+    struct resolve_srv_simple_state *state = NULL;
+    struct tevent_req *req = NULL;
+    struct tevent_req *subreq = NULL;
+    struct resolve_srv_simple_ctx *ctx = NULL;
+    const char **domains = NULL;
+    bool detect_first_domain;
+    errno_t ret;
+
+    req = tevent_req_create(mem_ctx, &state,
+                            struct resolve_srv_simple_state);
+    if (req == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, ("tevent_req_create() failed\n"));
+        return NULL;
+    }
+
+    ctx = talloc_get_type(pvt, struct resolve_srv_simple_ctx);
+    if (ctx == NULL) {
+        ret = EINVAL;
+        goto immediately;
+    }
+
+    domains = talloc_zero_array(state, const char*, 2);
+    if (discovery_domain == NULL) {
+        /* we want to autodetect first discovery domain, and fall back to
+         * sssd domain in case of an error */
+        detect_first_domain = true;
+        domains[0] = talloc_strdup(domains, ctx->sssd_domain);
+    } else {
+        /* we want to search only in the scope of provided discovery domain,
+         * this domain was likely set in sssd.conf */
+        detect_first_domain = false;
+        domains[0] = talloc_strdup(domains, discovery_domain);
+    }
+
+    if (domains[0] == NULL) {
+        ret = ENOMEM;
+        goto immediately;
+    }
+
+    subreq = resolve_srv_send(state, ev, ctx->resolv_ctx, ctx->host_dbs,
+                              ctx->family_order, service, protocol,
+                              domains, detect_first_domain);
+    if (subreq == NULL) {
+        ret = ENOMEM;
+        goto immediately;
+    }
+
+    tevent_req_set_callback(subreq, resolve_srv_simple_done, req);
+
+    return req;
+
+immediately:
+    if (ret == EOK) {
+        tevent_req_done(req);
+    } else {
+        tevent_req_error(req, ret);
+    }
+    tevent_req_post(req, ev);
+
+    return req;
+}
+
+static void resolve_srv_simple_done(struct tevent_req *subreq)
+{
+    struct resolve_srv_simple_state *state = NULL;
+    struct tevent_req *req = NULL;
+    errno_t ret;
+
+    req = tevent_req_callback_data(subreq, struct tevent_req);
+    state = tevent_req_data(req, struct resolve_srv_simple_state);
+
+    ret = resolve_srv_recv(state, subreq, &state->dns_domain,
+                           &state->servers, &state->num_servers);
+    talloc_zfree(subreq);
+    if (ret != EOK) {
+        goto done;
+    }
+
+done:
+    if (ret != EOK) {
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    tevent_req_done(req);
+}
+
+errno_t resolve_srv_simple_recv(TALLOC_CTX *mem_ctx,
+                                struct tevent_req *req,
+                                char **_dns_domain,
+                                struct fo_server_info **_primary_servers,
+                                size_t *_num_primary_servers,
+                                struct fo_server_info **_backup_servers,
+                                size_t *_num_backup_servers)
+{
+    struct resolve_srv_simple_state *state = NULL;
+    state = tevent_req_data(req, struct resolve_srv_simple_state);
+
+    TEVENT_REQ_RETURN_ON_ERROR(req);
+
+    if (_primary_servers) {
+        *_primary_servers = talloc_steal(mem_ctx, state->servers);
+    }
+
+    if (_num_primary_servers) {
+        *_num_primary_servers = state->num_servers;
+    }
+
+    /* backup servers are not supported by simple srv lookup */
+    if (_backup_servers) {
+        *_backup_servers = NULL;
+    }
+
+    if (_num_backup_servers) {
+        *_num_backup_servers = 0;
+    }
+
+    if (_dns_domain) {
+        *_dns_domain = talloc_steal(mem_ctx, state->dns_domain);
+    }
+
+    return EOK;
+}
+
-- 
1.7.11.7

