From c9940aa7517a3d222e96502b877ab8ab0dbd10a5 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Sat, 16 Jul 2011 18:00:40 +0200
Subject: [PATCH 2/2] fo_get_server_name() getter for a server name

Allows to be more concise in tests and more defensive in resolve
callbacks
---
 src/providers/data_provider_fo.c |    2 +-
 src/providers/fail_over.c        |    9 +++++++++
 src/providers/fail_over.h        |    2 ++
 src/providers/ipa/ipa_common.c   |   10 +++++++++-
 src/providers/ldap/ldap_common.c |   11 ++++++++++-
 src/tests/fail_over-tests.c      |    2 +-
 6 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/src/providers/data_provider_fo.c b/src/providers/data_provider_fo.c
index 6f9c757..b5c4173 100644
--- a/src/providers/data_provider_fo.c
+++ b/src/providers/data_provider_fo.c
@@ -432,7 +432,7 @@ static void be_resolve_server_done(struct tevent_req *subreq)
 
     /* all fine we got the server */
 
-    if (debug_level >= 4) {
+    if (debug_level >= 4 && fo_get_server_name(state->srv)) {
         struct resolv_hostent *srvaddr;
         char ipaddr[128];
         srvaddr = fo_get_server_hostent(state->srv);
diff --git a/src/providers/fail_over.c b/src/providers/fail_over.c
index 2bf6984..6b967d4 100644
--- a/src/providers/fail_over.c
+++ b/src/providers/fail_over.c
@@ -1375,6 +1375,15 @@ fo_get_server_port(struct fo_server *server)
     return server->port;
 }
 
+const char *
+fo_get_server_name(struct fo_server *server)
+{
+    if (!server->common) {
+        return NULL;
+    }
+    return server->common->name;
+}
+
 const char *fo_get_server_str_name(struct fo_server *server)
 {
     if (!server->common) {
diff --git a/src/providers/fail_over.h b/src/providers/fail_over.h
index cb9e54e..3ca6096 100644
--- a/src/providers/fail_over.h
+++ b/src/providers/fail_over.h
@@ -167,6 +167,8 @@ void *fo_get_server_user_data(struct fo_server *server);
 
 int fo_get_server_port(struct fo_server *server);
 
+const char *fo_get_server_name(struct fo_server *server);
+
 const char *fo_get_server_str_name(struct fo_server *server);
 
 struct resolv_hostent *fo_get_server_hostent(struct fo_server *server);
diff --git a/src/providers/ipa/ipa_common.c b/src/providers/ipa/ipa_common.c
index 70272a2..08b0512 100644
--- a/src/providers/ipa/ipa_common.c
+++ b/src/providers/ipa/ipa_common.c
@@ -602,6 +602,7 @@ static void ipa_resolve_callback(void *private_data, struct fo_server *server)
     char *address;
     const char *safe_address;
     char *new_uri;
+    const char *srv_name;
     int ret;
 
     tmp_ctx = talloc_new(NULL);
@@ -639,7 +640,14 @@ static void ipa_resolve_callback(void *private_data, struct fo_server *server)
         return;
     }
 
-    new_uri = talloc_asprintf(service, "ldap://%s", fo_get_server_str_name(server));
+    srv_name = fo_get_server_name(server);
+    if (srv_name == NULL) {
+        DEBUG(1, ("Could not get server host name\n"));
+        talloc_free(tmp_ctx);
+        return;
+    }
+
+    new_uri = talloc_asprintf(service, "ldap://%s", srv_name);
     if (!new_uri) {
         DEBUG(2, ("Failed to copy URI ...\n"));
         talloc_free(tmp_ctx);
diff --git a/src/providers/ldap/ldap_common.c b/src/providers/ldap/ldap_common.c
index 656c102..78176ba 100644
--- a/src/providers/ldap/ldap_common.c
+++ b/src/providers/ldap/ldap_common.c
@@ -542,6 +542,7 @@ static void sdap_uri_callback(void *private_data, struct fo_server *server)
     struct resolv_hostent *srvaddr;
     struct sockaddr_storage *sockaddr;
     const char *tmp;
+    const char *srv_name;
     char *new_uri;
 
     tmp_ctx = talloc_new(NULL);
@@ -579,8 +580,16 @@ static void sdap_uri_callback(void *private_data, struct fo_server *server)
             DEBUG(1, ("Unknown service, using ldap\n"));
             tmp = SSS_LDAP_SRV_NAME;
         }
+
+        srv_name = fo_get_server_name(server);
+        if (srv_name == NULL) {
+            DEBUG(1, ("Could not get server host name\n"));
+            talloc_free(tmp_ctx);
+            return;
+        }
+
         new_uri = talloc_asprintf(service, "%s://%s:%d",
-                                  tmp, fo_get_server_str_name(server),
+                                  tmp, srv_name,
                                   fo_get_server_port(server));
     } else {
         new_uri = talloc_strdup(service, tmp);
diff --git a/src/tests/fail_over-tests.c b/src/tests/fail_over-tests.c
index d8a6d51..8beaa8a 100644
--- a/src/tests/fail_over-tests.c
+++ b/src/tests/fail_over-tests.c
@@ -170,7 +170,7 @@ test_resolve_service_callback(struct tevent_req *req)
     if (task->new_server_status >= 0)
         fo_set_server_status(server, task->new_server_status);
 
-    if (strcmp(fo_get_server_str_name(server), "unknown name")) {
+    if (fo_get_server_name(server) != NULL) {
         he = fo_get_server_hostent(server);
         fail_if(he == NULL, "%s: fo_get_server_hostent() returned NULL");
         for (i = 0; he->addr_list[i]; i++) {
-- 
1.7.6

