From 4391f7f47317f3c5664f577b833ef91d3f8e15e2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 19 Mar 2013 15:53:44 +0100
Subject: [PATCH 1/4] DNS sites support - SRV lookup plugin interface

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

Introduces two new error codes:
- ERR_DNS_SRV_NOT_FOUND
- ERR_DNS_SRV_LOOKUP_ERROR
---
 src/providers/fail_over.c | 18 +++++++++++++++++
 src/providers/fail_over.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/util_errors.c    |  2 ++
 src/util/util_errors.h    |  2 ++
 4 files changed, 73 insertions(+)

diff --git a/src/providers/fail_over.c b/src/providers/fail_over.c
index e7c44174ded773a8e3bb99dc436c45d4e8ca277d..660fe8261aa279735af2b8d9d31596acbad19810 100644
--- a/src/providers/fail_over.c
+++ b/src/providers/fail_over.c
@@ -55,6 +55,10 @@ struct fo_ctx {
     struct server_common *server_common_list;
 
     struct fo_options *opts;
+
+    srv_lookup_plugin_send_t srv_send_fn;
+    srv_lookup_plugin_recv_t srv_recv_fn;
+    void *srv_pvt;
 };
 
 struct fo_service {
@@ -1591,3 +1595,17 @@ bool fo_svc_has_server(struct fo_service *service, struct fo_server *server)
 
     return false;
 }
+
+void fo_set_srv_lookup_plugin(struct fo_ctx *ctx,
+                              srv_lookup_plugin_send_t send_fn,
+                              srv_lookup_plugin_recv_t recv_fn,
+                              void *pvt)
+{
+    if (ctx == NULL) {
+        return;
+    }
+
+    ctx->srv_send_fn = send_fn;
+    ctx->srv_recv_fn = recv_fn;
+    ctx->srv_pvt = talloc_steal(ctx, pvt);
+}
diff --git a/src/providers/fail_over.h b/src/providers/fail_over.h
index 1ad081e78c866390a1a345feacc5c0899adf91a4..1ff06688237b5fde1d78878f45bd87f7ca1df8cf 100644
--- a/src/providers/fail_over.h
+++ b/src/providers/fail_over.h
@@ -198,4 +198,55 @@ void fo_reset_services(struct fo_ctx *fo_ctx);
 
 bool fo_svc_has_server(struct fo_service *service, struct fo_server *server);
 
+/* SRV lookup plugin interface */
+
+struct fo_server_info {
+    char *host;
+    int port;
+};
+
+/*
+ * If discovery_domain is NULL, it should be detected automatically.
+ */
+typedef struct tevent_req *
+(*srv_lookup_plugin_send_t)(TALLOC_CTX *mem_ctx,
+                            struct tevent_context *ev,
+                            const char *service,
+                            const char *protocol,
+                            const char *discovery_domain,
+                            void *pvt);
+
+/*
+ * Returns:
+ *   EOK - at least one primary or backup server was found
+ *   ERR_DNS_SRV_NOT_FOUND - no primary nor backup server found
+ *   ERR_DNS_SRV_LOOKUP_ERROR - error communicating with DNS server
+ *   other code - depends on plugin
+ *
+ * If EOK is returned:
+ * - and no primary server is found:
+ *   *_primary_servers = NULL
+ *   *_num_primary_servers = 0
+ * - and no backup server is found:
+ *   *_backup_servers = NULL
+ *   *_num_backup_servers = 0
+ * - *_dns_domain = DNS domain name where the servers were found
+ */
+typedef errno_t
+(*srv_lookup_plugin_recv_t)(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);
+
+/*
+ * pvt will be talloc_stealed to ctx
+ */
+void fo_set_srv_lookup_plugin(struct fo_ctx *ctx,
+                              srv_lookup_plugin_send_t send_fn,
+                              srv_lookup_plugin_recv_t recv_fn,
+                              void *pvt);
+
 #endif /* !__FAIL_OVER_H__ */
diff --git a/src/util/util_errors.c b/src/util/util_errors.c
index 475a3cbd8daca3ff3a95a433eced5ab57f847574..dffb1db2f8d6386135f5ea59f3c92fc2a2075a40 100644
--- a/src/util/util_errors.c
+++ b/src/util/util_errors.c
@@ -41,6 +41,8 @@ struct err_string error_to_str[] = {
     { "Account Expired" }, /* ERR_ACCOUNT_EXPIRED */
     { "Password Expired" }, /* ERR_PASSWORD_EXPIRED */
     { "Host Access Denied" }, /* ERR_ACCESS_DENIED */
+    { "SRV record not found" }, /* ERR_DNS_SRV_NOT_FOUND */
+    { "SRV lookup error" }, /* ERR_DNS_SRV_LOOKUP_ERROR */
 };
 
 
diff --git a/src/util/util_errors.h b/src/util/util_errors.h
index b4dfaf85f4f90bb4ca057121dde9559cb9e5b6ce..622b2f7cdbbd890de8d08bb8ddf6b3c538464d87 100644
--- a/src/util/util_errors.h
+++ b/src/util/util_errors.h
@@ -63,6 +63,8 @@ enum sssd_errors {
     ERR_ACCOUNT_EXPIRED,
     ERR_PASSWORD_EXPIRED,
     ERR_ACCESS_DENIED,
+    ERR_DNS_SRV_NOT_FOUND,
+    ERR_DNS_SRV_LOOKUP_ERROR,
     ERR_LAST            /* ALWAYS LAST */
 };
 
-- 
1.7.11.7

