From a8931f6854be99b0c0409bff9bd9b928e007173f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 18 Feb 2014 13:16:27 +0100
Subject: [PATCH 01/13] sdap: move non async functions from sdap_async.c to
 sdap_utils.c

---
 Makefile.am                     |   1 +
 src/providers/ldap/sdap_async.c | 118 ---------------------------------
 src/providers/ldap/sdap_utils.c | 142 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 143 insertions(+), 118 deletions(-)
 create mode 100644 src/providers/ldap/sdap_utils.c

diff --git a/Makefile.am b/Makefile.am
index 9d5526a58ee42926b9b948edf772994a92194f56..10e23518cb36e40286fa7af5943eb70544f3e9ba 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1640,6 +1640,7 @@ libsss_ldap_common_la_SOURCES = \
     src/providers/ldap/sdap_reinit.c \
     src/providers/ldap/sdap_dyndns.c \
     src/providers/ldap/sdap_refresh.c \
+    src/providers/ldap/sdap_utils.c \
     src/providers/ldap/sdap.c
 libsss_ldap_common_la_LDFLAGS = \
     -avoid-version
diff --git a/src/providers/ldap/sdap_async.c b/src/providers/ldap/sdap_async.c
index 03951077718b59087d8a0986f70d4edac6b77610..a3da4acf01f8909107bc569e7a4e5b4ffcf193e9 100644
--- a/src/providers/ldap/sdap_async.c
+++ b/src/providers/ldap/sdap_async.c
@@ -24,26 +24,8 @@
 #include "util/strtonum.h"
 #include "providers/ldap/sdap_async_private.h"
 
-#define REALM_SEPARATOR '@'
 #define REPLY_REALLOC_INCREMENT 10
 
-void make_realm_upper_case(const char *upn)
-{
-    char *c;
-
-    c = strchr(upn, REALM_SEPARATOR);
-    if (c == NULL) {
-        DEBUG(SSSDBG_TRACE_ALL, "No realm delimiter found in upn [%s].\n", upn);
-        return;
-    }
-
-    while(*(++c) != '\0') {
-        c[0] = toupper(*c);
-    }
-
-    return;
-}
-
 /* ==LDAP-Memory-Handling================================================= */
 
 static int lmsg_destructor(void *mem)
@@ -2466,103 +2448,3 @@ bool sdap_has_deref_support(struct sdap_handle *sh, struct sdap_options *opts)
 
     return false;
 }
-
-errno_t
-sdap_attrs_add_ldap_attr(struct sysdb_attrs *ldap_attrs,
-                         const char *attr_name,
-                         const char *attr_desc,
-                         bool multivalued,
-                         const char *name,
-                         struct sysdb_attrs *attrs)
-{
-    errno_t ret;
-    struct ldb_message_element *el;
-    const char *objname = name ?: "object";
-    const char *desc = attr_desc ?: attr_name;
-    unsigned int num_values, i;
-
-    ret = sysdb_attrs_get_el(ldap_attrs, attr_name, &el);
-    if (ret) {
-        DEBUG(SSSDBG_OP_FAILURE, "Could not get %s from the "
-              "list of the LDAP attributes [%d]: %s\n",
-              attr_name, ret, strerror(ret));
-        return ret;
-    }
-
-    if (el->num_values == 0) {
-        DEBUG(SSSDBG_TRACE_INTERNAL, "%s is not available "
-              "for [%s].\n", desc, objname);
-    } else {
-        num_values = multivalued ? el->num_values : 1;
-        for (i = 0; i < num_values; i++) {
-            DEBUG(SSSDBG_TRACE_INTERNAL, "Adding %s [%s] to attributes "
-                  "of [%s].\n", desc, el->values[i].data, objname);
-
-            ret = sysdb_attrs_add_mem(attrs, attr_name, el->values[i].data,
-                                      el->values[i].length);
-            if (ret) {
-                return ret;
-            }
-        }
-    }
-
-    return EOK;
-}
-
-errno_t
-sdap_save_all_names(const char *name,
-                    struct sysdb_attrs *ldap_attrs,
-                    struct sss_domain_info *dom,
-                    struct sysdb_attrs *attrs)
-{
-    const char **aliases = NULL;
-    const char *domname;
-    errno_t ret;
-    TALLOC_CTX *tmp_ctx;
-    int i;
-    bool lowercase = !dom->case_sensitive;
-
-    tmp_ctx = talloc_new(NULL);
-    if (!tmp_ctx) {
-        ret = ENOMEM;
-        goto done;
-    }
-
-    ret = sysdb_attrs_get_aliases(tmp_ctx, ldap_attrs, name,
-                                  lowercase, &aliases);
-    if (ret != EOK) {
-        DEBUG(SSSDBG_OP_FAILURE, "Failed to get the alias list");
-        goto done;
-    }
-
-    for (i = 0; aliases[i]; i++) {
-        domname = sss_get_domain_name(tmp_ctx, aliases[i], dom);
-        if (domname == NULL) {
-            ret = ENOMEM;
-            goto done;
-        }
-
-        if (lowercase) {
-            ret = sysdb_attrs_add_lc_name_alias(attrs, domname);
-            if (ret) {
-                DEBUG(SSSDBG_OP_FAILURE, "Failed to add lower-cased version "
-                                          "of alias [%s] into the "
-                                          "attribute list\n", aliases[i]);
-                goto done;
-            }
-        } else {
-            ret = sysdb_attrs_add_string(attrs, SYSDB_NAME_ALIAS, domname);
-            if (ret) {
-                DEBUG(SSSDBG_OP_FAILURE, "Failed to add alias [%s] into the "
-                                          "attribute list\n", aliases[i]);
-                goto done;
-            }
-        }
-
-    }
-
-    ret = EOK;
-done:
-    talloc_free(tmp_ctx);
-    return ret;
-}
diff --git a/src/providers/ldap/sdap_utils.c b/src/providers/ldap/sdap_utils.c
new file mode 100644
index 0000000000000000000000000000000000000000..194f4a133c6752e4a77009aecc3368a00ecc708e
--- /dev/null
+++ b/src/providers/ldap/sdap_utils.c
@@ -0,0 +1,142 @@
+/*
+    Authors:
+        Simo Sorce <ssorce@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 <ctype.h>
+#include "util/util.h"
+#include "providers/ldap/sdap_async.h"
+
+#define REALM_SEPARATOR '@'
+
+void make_realm_upper_case(const char *upn)
+{
+    char *c;
+
+    c = strchr(upn, REALM_SEPARATOR);
+    if (c == NULL) {
+        DEBUG(SSSDBG_TRACE_ALL, "No realm delimiter found in upn [%s].\n", upn);
+        return;
+    }
+
+    while(*(++c) != '\0') {
+        c[0] = toupper(*c);
+    }
+
+    return;
+}
+
+errno_t
+sdap_attrs_add_ldap_attr(struct sysdb_attrs *ldap_attrs,
+                         const char *attr_name,
+                         const char *attr_desc,
+                         bool multivalued,
+                         const char *name,
+                         struct sysdb_attrs *attrs)
+{
+    errno_t ret;
+    struct ldb_message_element *el;
+    const char *objname = name ?: "object";
+    const char *desc = attr_desc ?: attr_name;
+    unsigned int num_values, i;
+
+    ret = sysdb_attrs_get_el(ldap_attrs, attr_name, &el);
+    if (ret) {
+        DEBUG(SSSDBG_OP_FAILURE, "Could not get %s from the "
+              "list of the LDAP attributes [%d]: %s\n",
+              attr_name, ret, strerror(ret));
+        return ret;
+    }
+
+    if (el->num_values == 0) {
+        DEBUG(SSSDBG_TRACE_INTERNAL, "%s is not available "
+              "for [%s].\n", desc, objname);
+    } else {
+        num_values = multivalued ? el->num_values : 1;
+        for (i = 0; i < num_values; i++) {
+            DEBUG(SSSDBG_TRACE_INTERNAL, "Adding %s [%s] to attributes "
+                  "of [%s].\n", desc, el->values[i].data, objname);
+
+            ret = sysdb_attrs_add_mem(attrs, attr_name, el->values[i].data,
+                                      el->values[i].length);
+            if (ret) {
+                return ret;
+            }
+        }
+    }
+
+    return EOK;
+}
+
+errno_t
+sdap_save_all_names(const char *name,
+                    struct sysdb_attrs *ldap_attrs,
+                    struct sss_domain_info *dom,
+                    struct sysdb_attrs *attrs)
+{
+    const char **aliases = NULL;
+    const char *domname;
+    errno_t ret;
+    TALLOC_CTX *tmp_ctx;
+    int i;
+    bool lowercase = !dom->case_sensitive;
+
+    tmp_ctx = talloc_new(NULL);
+    if (!tmp_ctx) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    ret = sysdb_attrs_get_aliases(tmp_ctx, ldap_attrs, name,
+                                  lowercase, &aliases);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_OP_FAILURE, "Failed to get the alias list");
+        goto done;
+    }
+
+    for (i = 0; aliases[i]; i++) {
+        domname = sss_get_domain_name(tmp_ctx, aliases[i], dom);
+        if (domname == NULL) {
+            ret = ENOMEM;
+            goto done;
+        }
+
+        if (lowercase) {
+            ret = sysdb_attrs_add_lc_name_alias(attrs, domname);
+            if (ret) {
+                DEBUG(SSSDBG_OP_FAILURE, "Failed to add lower-cased version "
+                                          "of alias [%s] into the "
+                                          "attribute list\n", aliases[i]);
+                goto done;
+            }
+        } else {
+            ret = sysdb_attrs_add_string(attrs, SYSDB_NAME_ALIAS, domname);
+            if (ret) {
+                DEBUG(SSSDBG_OP_FAILURE, "Failed to add alias [%s] into the "
+                                          "attribute list\n", aliases[i]);
+                goto done;
+            }
+        }
+
+    }
+
+    ret = EOK;
+done:
+    talloc_free(tmp_ctx);
+    return ret;
+}
-- 
1.7.11.7

