From 967d4ea6253ff7932b2c3a7dd1149d1dc022997a Mon Sep 17 00:00:00 2001
From: Jan Zeleny <jzeleny@redhat.com>
Date: Tue, 5 Jun 2012 08:41:29 -0400
Subject: [PATCH 2/8] Primary server support: IPA adaptation

This patch adds support for the primary server functionality into IPA
provider. No secondary servers are added at the moment, just the basic
support is in place.
---
 src/providers/ipa/ipa_common.c |  101 +++++++++++++++++++++++++++-------------
 src/providers/ipa/ipa_common.h |    3 +-
 src/providers/ipa/ipa_init.c   |    2 +-
 3 files changed, 72 insertions(+), 34 deletions(-)

diff --git a/src/providers/ipa/ipa_common.c b/src/providers/ipa/ipa_common.c
index 228e09b5012ad8b41951d25ffa82e7273b335cd8..07976130e26fd77f6dba53a85d8d03e116779805 100644
--- a/src/providers/ipa/ipa_common.c
+++ b/src/providers/ipa/ipa_common.c
@@ -745,18 +745,72 @@ static void ipa_resolve_callback(void *private_data, struct fo_server *server)
     talloc_free(tmp_ctx);
 }
 
+errno_t ipa_servers_init(struct be_ctx *ctx,
+                         struct ipa_service *service,
+                         struct ipa_options *options,
+                         const char *servers,
+                         bool primary)
+{
+    TALLOC_CTX *tmp_ctx;
+    char **list = NULL;
+    char *ipa_domain;
+    int ret;
+    int i;
+
+    tmp_ctx = talloc_new(NULL);
+    if (!tmp_ctx) {
+        return ENOMEM;
+    }
+
+    /* split server parm into a list */
+    ret = split_on_separator(tmp_ctx, servers, ',', true, &list, NULL);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to parse server list!\n"));
+        goto done;
+    }
+
+    /* now for each one add a new server to the failover service */
+    for (i = 0; list[i]; i++) {
+
+        talloc_steal(service, list[i]);
+
+        if (be_fo_is_srv_identifier(list[i])) {
+            ipa_domain = dp_opt_get_string(options->basic, IPA_DOMAIN);
+            ret = be_fo_add_srv_server(ctx, "IPA", "ldap", ipa_domain,
+                                       BE_FO_PROTO_TCP, false, NULL);
+            if (ret) {
+                DEBUG(SSSDBG_FATAL_FAILURE, ("Failed to add server\n"));
+                goto done;
+            }
+
+            DEBUG(SSSDBG_TRACE_FUNC, ("Added service lookup for service IPA\n"));
+            continue;
+        }
+
+        ret = be_fo_add_server(ctx, "IPA", list[i], 0, NULL, primary);
+        if (ret && ret != EEXIST) {
+            DEBUG(SSSDBG_FATAL_FAILURE, ("Failed to add server\n"));
+            goto done;
+        }
+
+        DEBUG(SSSDBG_TRACE_FUNC, ("Added Server %s\n", list[i]));
+    }
+
+done:
+    talloc_free(tmp_ctx);
+    return ret;
+}
+
 int ipa_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
-                     const char *servers,
+                     const char *primary_servers,
+                     const char *secondary_servers,
                      struct ipa_options *options,
                      struct ipa_service **_service)
 {
     TALLOC_CTX *tmp_ctx;
     struct ipa_service *service;
-    char **list = NULL;
     char *realm;
-    char *ipa_domain;
     int ret;
-    int i;
 
     tmp_ctx = talloc_new(memctx);
     if (!tmp_ctx) {
@@ -811,42 +865,25 @@ int ipa_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
         goto done;
     }
 
-    if (!servers) {
-        servers = BE_SRV_IDENTIFIER;
+    if (!primary_servers) {
+        if (secondary_servers) {
+            primary_servers = secondary_servers;
+            secondary_servers = NULL;
+        } else {
+            primary_servers = BE_SRV_IDENTIFIER;
+        }
     }
 
-    /* split server parm into a list */
-    ret = split_on_separator(tmp_ctx, servers, ',', true, &list, NULL);
+    ret = ipa_servers_init(ctx, service, options, primary_servers, true);
     if (ret != EOK) {
-        DEBUG(1, ("Failed to parse server list!\n"));
         goto done;
     }
 
-    /* now for each one add a new server to the failover service */
-    for (i = 0; list[i]; i++) {
-
-        talloc_steal(service, list[i]);
-
-        if (be_fo_is_srv_identifier(list[i])) {
-            ipa_domain = dp_opt_get_string(options->basic, IPA_DOMAIN);
-            ret = be_fo_add_srv_server(ctx, "IPA", "ldap", ipa_domain,
-                                       BE_FO_PROTO_TCP, false, NULL);
-            if (ret) {
-                DEBUG(0, ("Failed to add server\n"));
-                goto done;
-            }
-
-            DEBUG(6, ("Added service lookup for service IPA\n"));
-            continue;
-        }
-
-        ret = be_fo_add_server(ctx, "IPA", list[i], 0, NULL, true);
-        if (ret && ret != EEXIST) {
-            DEBUG(0, ("Failed to add server\n"));
+    if (secondary_servers) {
+        ret = ipa_servers_init(ctx, service, options, secondary_servers, false);
+        if (ret != EOK) {
             goto done;
         }
-
-        DEBUG(6, ("Added Server %s\n", list[i]));
     }
 
     ret = be_fo_service_add_callback(memctx, ctx, "IPA",
diff --git a/src/providers/ipa/ipa_common.h b/src/providers/ipa/ipa_common.h
index 9232260eb8790e6d1c87818d7c36d78c60974f20..524920261c98601158371a478e796f48f155cc9a 100644
--- a/src/providers/ipa/ipa_common.h
+++ b/src/providers/ipa/ipa_common.h
@@ -167,7 +167,8 @@ int ipa_autofs_init(struct be_ctx *be_ctx,
                     void **pvt_data);
 
 int ipa_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
-                     const char *servers,
+                     const char *primary_servers,
+                     const char *secondary_servers,
                      struct ipa_options *options,
                      struct ipa_service **_service);
 
diff --git a/src/providers/ipa/ipa_init.c b/src/providers/ipa/ipa_init.c
index 90acb1082ec47f4b10c8b04c15a7fc1eaba92fe3..5e79cb41de495d787671b63ea67c6300c71f57cb 100644
--- a/src/providers/ipa/ipa_init.c
+++ b/src/providers/ipa/ipa_init.c
@@ -97,7 +97,7 @@ int common_ipa_init(struct be_ctx *bectx)
         DEBUG(1, ("Missing ipa_server option - using service discovery!\n"));
     }
 
-    ret = ipa_service_init(ipa_options, bectx, ipa_servers, ipa_options,
+    ret = ipa_service_init(ipa_options, bectx, ipa_servers, NULL, ipa_options,
                            &ipa_options->service);
     if (ret != EOK) {
         DEBUG(0, ("Failed to init IPA failover service!\n"));
-- 
1.7.7.6

