1cdfb00
From 4619742836ec22edf8f9d274d928bc896c5b0883 Mon Sep 17 00:00:00 2001
1cdfb00
From: Sumit Bose <sbose@redhat.com>
1cdfb00
Date: Tue, 17 Feb 2015 04:41:21 +0100
1cdfb00
Subject: [PATCH 43/99] sdap: properly handle binary objectGuid attribute
1cdfb00
1cdfb00
Although in the initial processing SSSD treats the binary value right at
1cdfb00
some point it mainly assumes that it is a string. Depending on the value
1cdfb00
this might end up with the correct binary value stored in the cache but
1cdfb00
in most cases there will be only a broken entry in the cache.
1cdfb00
1cdfb00
This patch converts the binary value into a string representation which
1cdfb00
is described in [MS-DTYP] and stores the result in the cache.
1cdfb00
1cdfb00
Resolves https://fedorahosted.org/sssd/ticket/2588
1cdfb00
1cdfb00
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
1cdfb00
---
1cdfb00
 Makefile.am                                |  16 ++++
1cdfb00
 src/db/sysdb.h                             |   6 ++
1cdfb00
 src/db/sysdb_ops.c                         |  52 +++++++++++
1cdfb00
 src/providers/ldap/sdap_async_groups.c     |  25 ++----
1cdfb00
 src/providers/ldap/sdap_async_initgroups.c |   7 +-
1cdfb00
 src/providers/ldap/sdap_async_users.c      |  23 ++---
1cdfb00
 src/tests/cmocka/test_string_utils.c       |  59 +++++++++++++
1cdfb00
 src/tests/cmocka/test_sysdb_utils.c        | 134 +++++++++++++++++++++++++++++
1cdfb00
 src/tests/cmocka/test_utils.c              |   1 +
1cdfb00
 src/tests/cmocka/test_utils.h              |   1 +
1cdfb00
 src/tests/cwrap/Makefile.am                |   2 +
1cdfb00
 src/util/string_utils.c                    |  25 ++++++
1cdfb00
 src/util/util.h                            |   7 ++
1cdfb00
 13 files changed, 324 insertions(+), 34 deletions(-)
1cdfb00
 create mode 100644 src/tests/cmocka/test_sysdb_utils.c
1cdfb00
1cdfb00
diff --git a/Makefile.am b/Makefile.am
1cdfb00
index 5099043549a46c15a9d7f6a581c864cbbe3137b5..df34840747bdcc3e2cc68ac1a3ca448b4aa67433 100644
1cdfb00
--- a/Makefile.am
1cdfb00
+++ b/Makefile.am
1cdfb00
@@ -214,6 +214,7 @@ if HAVE_CMOCKA
1cdfb00
         test_search_bases \
1cdfb00
         sdap-tests \
1cdfb00
         test_sysdb_views \
1cdfb00
+        test_sysdb_utils \
1cdfb00
         test_be_ptask \
1cdfb00
         test_copy_ccache \
1cdfb00
         test_copy_keytab \
1cdfb00
@@ -2113,6 +2114,21 @@ test_sysdb_views_LDADD = \
1cdfb00
     libsss_test_common.la \
1cdfb00
     $(NULL)
1cdfb00
 
1cdfb00
+test_sysdb_utils_SOURCES = \
1cdfb00
+    src/tests/cmocka/test_sysdb_utils.c \
1cdfb00
+    $(NULL)
1cdfb00
+test_sysdb_utils_CFLAGS = \
1cdfb00
+    $(AM_CFLAGS) \
1cdfb00
+    $(NULL)
1cdfb00
+test_sysdb_utils_LDADD = \
1cdfb00
+    $(CMOCKA_LIBS) \
1cdfb00
+    $(LDB_LIBS) \
1cdfb00
+    $(POPT_LIBS) \
1cdfb00
+    $(TALLOC_LIBS) \
1cdfb00
+    $(SSSD_INTERNAL_LTLIBS) \
1cdfb00
+    libsss_test_common.la \
1cdfb00
+    $(NULL)
1cdfb00
+
1cdfb00
 test_be_ptask_SOURCES = \
1cdfb00
     src/tests/cmocka/test_be_ptask.c \
1cdfb00
     src/providers/dp_ptask.c \
1cdfb00
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
1cdfb00
index 1362f377837d25815b04b4929a2895ee3a6485a9..a1b6f207399555c85c14c8decf89edc498deb871 100644
1cdfb00
--- a/src/db/sysdb.h
1cdfb00
+++ b/src/db/sysdb.h
1cdfb00
@@ -1117,4 +1117,10 @@ errno_t sysdb_get_sids_of_members(TALLOC_CTX *mem_ctx,
1cdfb00
                                   const char ***_sids,
1cdfb00
                                   const char ***_dns,
1cdfb00
                                   size_t *_n);
1cdfb00
+
1cdfb00
+errno_t sysdb_handle_original_uuid(const char *orig_name,
1cdfb00
+                                   struct sysdb_attrs *src_attrs,
1cdfb00
+                                   const char *src_name,
1cdfb00
+                                   struct sysdb_attrs *dest_attrs,
1cdfb00
+                                   const char *dest_name);
1cdfb00
 #endif /* __SYS_DB_H__ */
1cdfb00
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
1cdfb00
index 06d24f220afc28b39f3856f3f0170818e11d9cf9..37529fd12c2c5c6896a2ca71293a61f93ba0eee3 100644
1cdfb00
--- a/src/db/sysdb_ops.c
1cdfb00
+++ b/src/db/sysdb_ops.c
1cdfb00
@@ -3696,3 +3696,55 @@ done:
1cdfb00
     talloc_free(tmp_ctx);
1cdfb00
     return ret;
1cdfb00
 }
1cdfb00
+
1cdfb00
+errno_t sysdb_handle_original_uuid(const char *orig_name,
1cdfb00
+                                   struct sysdb_attrs *src_attrs,
1cdfb00
+                                   const char *src_name,
1cdfb00
+                                   struct sysdb_attrs *dest_attrs,
1cdfb00
+                                   const char *dest_name)
1cdfb00
+{
1cdfb00
+    int ret;
1cdfb00
+    struct ldb_message_element *el;
1cdfb00
+    char guid_str_buf[GUID_STR_BUF_SIZE];
1cdfb00
+
1cdfb00
+    if (orig_name == NULL || src_attrs == NULL || src_name == NULL
1cdfb00
+            || dest_attrs == NULL || dest_name == NULL) {
1cdfb00
+        return EINVAL;
1cdfb00
+    }
1cdfb00
+
1cdfb00
+    ret = sysdb_attrs_get_el_ext(src_attrs, src_name, false, &el);
1cdfb00
+    if (ret != EOK) {
1cdfb00
+        if (ret != ENOENT) {
1cdfb00
+            DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_get_el failed.\n");
1cdfb00
+        }
1cdfb00
+        return ret;
1cdfb00
+    }
1cdfb00
+
1cdfb00
+    if (el->num_values != 1) {
1cdfb00
+        DEBUG(SSSDBG_MINOR_FAILURE,
1cdfb00
+              "Found more than one UUID value, using the first.\n");
1cdfb00
+    }
1cdfb00
+
1cdfb00
+    /* Check if we got a binary AD objectGUID */
1cdfb00
+    if (el->values[0].length == GUID_BIN_LENGTH
1cdfb00
+            && strcasecmp(orig_name, "objectGUID") == 0) {
1cdfb00
+        ret = guid_blob_to_string_buf(el->values[0].data, guid_str_buf,
1cdfb00
+                                      GUID_STR_BUF_SIZE);
1cdfb00
+        if (ret != EOK) {
1cdfb00
+            DEBUG(SSSDBG_OP_FAILURE, "guid_blob_to_string_buf failed.\n");
1cdfb00
+            return ret;
1cdfb00
+        }
1cdfb00
+
1cdfb00
+        ret = sysdb_attrs_add_string(dest_attrs, dest_name, guid_str_buf);
1cdfb00
+    } else {
1cdfb00
+        ret = sysdb_attrs_add_string(dest_attrs, dest_name,
1cdfb00
+                                     (const char *)el->values[0].data);
1cdfb00
+    }
1cdfb00
+
1cdfb00
+    if (ret != EOK) {
1cdfb00
+        DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_add_string failed.\n");
1cdfb00
+        return ret;;
1cdfb00
+    }
1cdfb00
+
1cdfb00
+    return EOK;
1cdfb00
+}
1cdfb00
diff --git a/src/providers/ldap/sdap_async_groups.c b/src/providers/ldap/sdap_async_groups.c
1cdfb00
index 7779d499de87e2f0657512cb1f8e1980f9bf1f71..f9613ddd72d1e1796bc6a034daf98275b07a5c79 100644
1cdfb00
--- a/src/providers/ldap/sdap_async_groups.c
1cdfb00
+++ b/src/providers/ldap/sdap_async_groups.c
1cdfb00
@@ -511,7 +511,6 @@ static int sdap_save_group(TALLOC_CTX *memctx,
1cdfb00
     bool posix_group;
1cdfb00
     bool use_id_mapping;
1cdfb00
     char *sid_str;
1cdfb00
-    const char *uuid;
1cdfb00
     struct sss_domain_info *subdomain;
1cdfb00
     int32_t ad_group_type;
1cdfb00
 
1cdfb00
@@ -549,22 +548,14 @@ static int sdap_save_group(TALLOC_CTX *memctx,
1cdfb00
     }
1cdfb00
 
1cdfb00
     /* Always store UUID if available */
1cdfb00
-    ret = sysdb_attrs_get_string(attrs,
1cdfb00
-                                 opts->group_map[SDAP_AT_GROUP_UUID].sys_name,
1cdfb00
-                                 &uuid);
1cdfb00
-    if (ret == EOK) {
1cdfb00
-        ret = sysdb_attrs_add_string(group_attrs, SYSDB_UUID, uuid);
1cdfb00
-        if (ret != EOK) {
1cdfb00
-            DEBUG(SSSDBG_MINOR_FAILURE, "Could not add UUID string: [%s]\n",
1cdfb00
-                                         sss_strerror(ret));
1cdfb00
-            goto done;
1cdfb00
-        }
1cdfb00
-    } else if (ret == ENOENT) {
1cdfb00
-        DEBUG(SSSDBG_TRACE_ALL, "UUID not available for group [%s].\n",
1cdfb00
-                                 group_name);
1cdfb00
-    } else {
1cdfb00
-        DEBUG(SSSDBG_MINOR_FAILURE, "Could not identify UUID [%s]\n",
1cdfb00
-                                     sss_strerror(ret));
1cdfb00
+    ret = sysdb_handle_original_uuid(
1cdfb00
+                                   opts->group_map[SDAP_AT_GROUP_UUID].def_name,
1cdfb00
+                                   attrs,
1cdfb00
+                                   opts->group_map[SDAP_AT_GROUP_UUID].sys_name,
1cdfb00
+                                   group_attrs, SYSDB_UUID);
1cdfb00
+    if (ret != EOK) {
1cdfb00
+        DEBUG((ret == ENOENT) ? SSSDBG_TRACE_ALL : SSSDBG_MINOR_FAILURE,
1cdfb00
+              "Failed to retrieve UUID [%d][%s].\n", ret, sss_strerror(ret));
1cdfb00
     }
1cdfb00
 
1cdfb00
     /* If this object has a SID available, we will determine the correct
1cdfb00
diff --git a/src/providers/ldap/sdap_async_initgroups.c b/src/providers/ldap/sdap_async_initgroups.c
1cdfb00
index 2fd235f2868b877c0e5d5d9f7b1b76d269eee8ee..96617aecc4e9c948bbbdccb1ba75e81577a19c70 100644
1cdfb00
--- a/src/providers/ldap/sdap_async_initgroups.c
1cdfb00
+++ b/src/providers/ldap/sdap_async_initgroups.c
1cdfb00
@@ -196,8 +196,13 @@ errno_t sdap_add_incomplete_groups(struct sysdb_ctx *sysdb,
1cdfb00
                     original_dn = NULL;
1cdfb00
                 }
1cdfb00
 
1cdfb00
+                ret = sysdb_handle_original_uuid(
1cdfb00
+                                   opts->group_map[SDAP_AT_GROUP_UUID].def_name,
1cdfb00
+                                   ldap_groups[ai],
1cdfb00
+                                   opts->group_map[SDAP_AT_GROUP_UUID].sys_name,
1cdfb00
+                                   ldap_groups[ai], "uniqueIDstr");
1cdfb00
                 ret = sysdb_attrs_get_string(ldap_groups[ai],
1cdfb00
-                                             SYSDB_UUID,
1cdfb00
+                                             "uniqueIDstr",
1cdfb00
                                              &uuid);
1cdfb00
                 if (ret) {
1cdfb00
                     DEBUG(SSSDBG_FUNC_DATA,
1cdfb00
diff --git a/src/providers/ldap/sdap_async_users.c b/src/providers/ldap/sdap_async_users.c
1cdfb00
index 367e3d795ddd0db5c1c2f8e57d700419f371cd15..82b4df4793f5f0679046f259c251f5897af831cf 100644
1cdfb00
--- a/src/providers/ldap/sdap_async_users.c
1cdfb00
+++ b/src/providers/ldap/sdap_async_users.c
1cdfb00
@@ -140,7 +140,6 @@ int sdap_save_user(TALLOC_CTX *memctx,
1cdfb00
     TALLOC_CTX *tmpctx = NULL;
1cdfb00
     bool use_id_mapping;
1cdfb00
     char *sid_str;
1cdfb00
-    const char *uuid;
1cdfb00
     char *dom_sid_str = NULL;
1cdfb00
     struct sss_domain_info *subdomain;
1cdfb00
 
1cdfb00
@@ -179,21 +178,13 @@ int sdap_save_user(TALLOC_CTX *memctx,
1cdfb00
     }
1cdfb00
 
1cdfb00
     /* Always store UUID if available */
1cdfb00
-    ret = sysdb_attrs_get_string(attrs,
1cdfb00
-                                 opts->user_map[SDAP_AT_USER_UUID].sys_name,
1cdfb00
-                                 &uuid);
1cdfb00
-    if (ret == EOK) {
1cdfb00
-        ret = sysdb_attrs_add_string(user_attrs, SYSDB_UUID, uuid);
1cdfb00
-        if (ret != EOK) {
1cdfb00
-            DEBUG(SSSDBG_MINOR_FAILURE, "Could not add UUID string: [%s]\n",
1cdfb00
-                                         sss_strerror(ret));
1cdfb00
-            goto done;
1cdfb00
-        }
1cdfb00
-    } else if (ret == ENOENT) {
1cdfb00
-        DEBUG(SSSDBG_TRACE_ALL, "UUID not available for user.\n");
1cdfb00
-    } else {
1cdfb00
-        DEBUG(SSSDBG_MINOR_FAILURE, "Could not identify UUID [%s]\n",
1cdfb00
-                                     sss_strerror(ret));
1cdfb00
+    ret = sysdb_handle_original_uuid(opts->user_map[SDAP_AT_USER_UUID].def_name,
1cdfb00
+                                     attrs,
1cdfb00
+                                     opts->user_map[SDAP_AT_USER_UUID].sys_name,
1cdfb00
+                                     user_attrs, SYSDB_UUID);
1cdfb00
+    if (ret != EOK) {
1cdfb00
+        DEBUG((ret == ENOENT) ? SSSDBG_TRACE_ALL : SSSDBG_MINOR_FAILURE,
1cdfb00
+              "Failed to retrieve UUID [%d][%s].\n", ret, sss_strerror(ret));
1cdfb00
     }
1cdfb00
 
1cdfb00
     /* If this object has a SID available, we will determine the correct
1cdfb00
diff --git a/src/tests/cmocka/test_string_utils.c b/src/tests/cmocka/test_string_utils.c
1cdfb00
index e446387d6c429515360b23b428555befa915b49a..5d3fcf4fe454a0be3a4c72b778003481f66910bb 100644
1cdfb00
--- a/src/tests/cmocka/test_string_utils.c
1cdfb00
+++ b/src/tests/cmocka/test_string_utils.c
1cdfb00
@@ -133,3 +133,62 @@ void test_reverse_replace_whitespaces(void **state)
1cdfb00
     assert_true(check_leaks_pop(mem_ctx) == true);
1cdfb00
     talloc_free(mem_ctx);
1cdfb00
 }
1cdfb00
+
1cdfb00
+void test_guid_blob_to_string_buf(void **state)
1cdfb00
+{
1cdfb00
+    int ret;
1cdfb00
+    char str_buf[GUID_STR_BUF_SIZE];
1cdfb00
+    size_t c;
1cdfb00
+
1cdfb00
+    /* How to get test data:
1cdfb00
+     * The objectGUID attribute contains a 16byte long binary value
1cdfb00
+     * representing the GUID of the object. This data can be converted
1cdfb00
+     * manually to the string representation but it might be easier to use
1cdfb00
+     * LDAP_SERVER_EXTENDED_DN_OID as described in [MS-ADST] section
1cdfb00
+     * 3.1.1.3.4.1.5. This is an LDAP extended control which adds the GUID and
1cdfb00
+     * the SID to the DN of an object. This can be activate with the -E
1cdfb00
+     * ldapsearch option like:
1cdfb00
+     *
1cdfb00
+     *  ldapsearch -E 1.2.840.113556.1.4.529=::MAMCAQE= ....
1cdfb00
+     *
1cdfb00
+     * where 'MAMCAQE=' is the base64 encoded BER sequence with the integer
1cdfb00
+     * value 1 (see [MS-ADTS] for details about possible values).
1cdfb00
+     *
1cdfb00
+     * Btw, if you want to use the string representation of a GUID to search
1cdfb00
+     * for an object in AD you have to use the GUID as the search base in the
1cdfb00
+     * following form:
1cdfb00
+     *
1cdfb00
+     *  ldapsearch b '<GUID=fea80d8d-dbd5-4f84-8574-7db0477f962e>' ...
1cdfb00
+     *
1cdfb00
+     * (please note that the '<' and '>' are really needed).
1cdfb00
+     */
1cdfb00
+    struct test_data {
1cdfb00
+        uint8_t blob[16];
1cdfb00
+        const char *guid_str;
1cdfb00
+    } test_data[] = {
1cdfb00
+        {{0x8d, 0x0d, 0xa8, 0xfe, 0xd5, 0xdb, 0x84, 0x4f,
1cdfb00
+          0x85, 0x74, 0x7d, 0xb0, 0x47, 0x7f, 0x96, 0x2e},
1cdfb00
+        "fea80d8d-dbd5-4f84-8574-7db0477f962e"},
1cdfb00
+        {{0x91, 0x7e, 0x2e, 0xf8, 0x4e, 0x44, 0xfa, 0x4e,
1cdfb00
+         0xb1, 0x13, 0x08, 0x98, 0x63, 0x49, 0x6c, 0xc6},
1cdfb00
+        "f82e7e91-444e-4efa-b113-089863496cc6"},
1cdfb00
+        {{0}, NULL}
1cdfb00
+    };
1cdfb00
+
1cdfb00
+    ret = guid_blob_to_string_buf(NULL, str_buf, GUID_STR_BUF_SIZE);
1cdfb00
+    assert_int_equal(ret, EINVAL);
1cdfb00
+
1cdfb00
+    ret = guid_blob_to_string_buf((const uint8_t *) "1234567812345678", NULL,
1cdfb00
+                                  GUID_STR_BUF_SIZE);
1cdfb00
+    assert_int_equal(ret, EINVAL);
1cdfb00
+
1cdfb00
+    ret = guid_blob_to_string_buf((const uint8_t *) "1234567812345678", str_buf, 0);
1cdfb00
+    assert_int_equal(ret, EINVAL);
1cdfb00
+
1cdfb00
+    for (c = 0; test_data[c].guid_str != NULL; c++) {
1cdfb00
+        ret = guid_blob_to_string_buf(test_data[c].blob, str_buf,
1cdfb00
+                                      sizeof(str_buf));
1cdfb00
+        assert_int_equal(ret, EOK);
1cdfb00
+        assert_string_equal(test_data[c].guid_str, str_buf);
1cdfb00
+    }
1cdfb00
+}
1cdfb00
diff --git a/src/tests/cmocka/test_sysdb_utils.c b/src/tests/cmocka/test_sysdb_utils.c
1cdfb00
new file mode 100644
1cdfb00
index 0000000000000000000000000000000000000000..d217314ccb9234f8d0d329d87c5dc9e847acbcf0
1cdfb00
--- /dev/null
1cdfb00
+++ b/src/tests/cmocka/test_sysdb_utils.c
1cdfb00
@@ -0,0 +1,134 @@
1cdfb00
+/*
1cdfb00
+    SSSD
1cdfb00
+
1cdfb00
+    sysdb_utils - Tests for various sysdb calls
1cdfb00
+
1cdfb00
+    Authors:
1cdfb00
+        Sumit Bose <sbose@redhat.com>
1cdfb00
+
1cdfb00
+    Copyright (C) 2015 Red Hat
1cdfb00
+
1cdfb00
+    This program is free software; you can redistribute it and/or modify
1cdfb00
+    it under the terms of the GNU General Public License as published by
1cdfb00
+    the Free Software Foundation; either version 3 of the License, or
1cdfb00
+    (at your option) any later version.
1cdfb00
+
1cdfb00
+    This program is distributed in the hope that it will be useful,
1cdfb00
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
1cdfb00
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1cdfb00
+    GNU General Public License for more details.
1cdfb00
+
1cdfb00
+    You should have received a copy of the GNU General Public License
1cdfb00
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
1cdfb00
+*/
1cdfb00
+
1cdfb00
+#include <stdarg.h>
1cdfb00
+#include <stddef.h>
1cdfb00
+#include <setjmp.h>
1cdfb00
+#include <cmocka.h>
1cdfb00
+#include <popt.h>
1cdfb00
+
1cdfb00
+#include "tests/cmocka/common_mock.h"
1cdfb00
+
1cdfb00
+#define IPA_UUID "bcae7c40-97eb-11e4-88ca-525400e96a6b"
1cdfb00
+
1cdfb00
+#define AD_GUID_BIN {0x8d, 0x0d, 0xa8, 0xfe, 0xd5, 0xdb, 0x84, 0x4f, \
1cdfb00
+                     0x85, 0x74, 0x7d, 0xb0, 0x47, 0x7f, 0x96, 0x2e};
1cdfb00
+#define AD_GUID "fea80d8d-dbd5-4f84-8574-7db0477f962e"
1cdfb00
+static void test_sysdb_handle_original_uuid(void **state)
1cdfb00
+{
1cdfb00
+    int ret;
1cdfb00
+    struct sysdb_attrs *src_attrs;
1cdfb00
+    struct sysdb_attrs *dest_attrs;
1cdfb00
+    const char *guid;
1cdfb00
+    uint8_t bin_guid[] = AD_GUID_BIN;
1cdfb00
+    struct ldb_val guid_val = {bin_guid, 16};
1cdfb00
+
1cdfb00
+    ret = sysdb_handle_original_uuid(NULL, NULL, NULL, NULL, NULL);
1cdfb00
+    assert_int_equal(ret, EINVAL);
1cdfb00
+
1cdfb00
+    src_attrs = sysdb_new_attrs(NULL);
1cdfb00
+    assert_non_null(src_attrs);
1cdfb00
+
1cdfb00
+    dest_attrs = sysdb_new_attrs(NULL);
1cdfb00
+    assert_non_null(dest_attrs);
1cdfb00
+
1cdfb00
+    ret = sysdb_handle_original_uuid("xyz", src_attrs, "abc", dest_attrs,
1cdfb00
+                                     "def");
1cdfb00
+    assert_int_equal(ret, ENOENT);
1cdfb00
+
1cdfb00
+    ret = sysdb_attrs_add_val(src_attrs, "GUID", &guid_val);
1cdfb00
+    assert_int_equal(ret, EOK);
1cdfb00
+
1cdfb00
+    ret = sysdb_attrs_add_string(src_attrs, "UUID", IPA_UUID);
1cdfb00
+    assert_int_equal(ret, EOK);
1cdfb00
+
1cdfb00
+    ret = sysdb_handle_original_uuid("objectGUID", src_attrs, "GUID",
1cdfb00
+                                     dest_attrs, "def");
1cdfb00
+    assert_int_equal(ret, EOK);
1cdfb00
+    ret = sysdb_attrs_get_string(dest_attrs, "def", &guid);
1cdfb00
+    assert_int_equal(ret, EOK);
1cdfb00
+    assert_string_equal(guid, AD_GUID);
1cdfb00
+
1cdfb00
+    ret = sysdb_handle_original_uuid("ipaUniqueID", src_attrs, "UUID",
1cdfb00
+                                     dest_attrs, "ghi");
1cdfb00
+    assert_int_equal(ret, EOK);
1cdfb00
+    ret = sysdb_attrs_get_string(dest_attrs, "ghi", &guid);
1cdfb00
+    assert_int_equal(ret, EOK);
1cdfb00
+    assert_string_equal(guid, IPA_UUID);
1cdfb00
+
1cdfb00
+    talloc_free(src_attrs);
1cdfb00
+    src_attrs = sysdb_new_attrs(NULL);
1cdfb00
+    assert_non_null(src_attrs);
1cdfb00
+
1cdfb00
+    /* check objectGUID with length other than 16 */
1cdfb00
+    ret = sysdb_attrs_add_string(src_attrs, "GUID", IPA_UUID);
1cdfb00
+    assert_int_equal(ret, EOK);
1cdfb00
+    ret = sysdb_handle_original_uuid("objectGUID", src_attrs, "GUID",
1cdfb00
+                                     dest_attrs, "jkl");
1cdfb00
+    assert_int_equal(ret, EOK);
1cdfb00
+    ret = sysdb_attrs_get_string(dest_attrs, "jkl", &guid);
1cdfb00
+    assert_int_equal(ret, EOK);
1cdfb00
+    assert_string_equal(guid, IPA_UUID);
1cdfb00
+
1cdfb00
+    talloc_free(src_attrs);
1cdfb00
+    talloc_free(dest_attrs);
1cdfb00
+}
1cdfb00
+
1cdfb00
+int main(int argc, const char *argv[])
1cdfb00
+{
1cdfb00
+    int rv;
1cdfb00
+    poptContext pc;
1cdfb00
+    int opt;
1cdfb00
+    struct poptOption long_options[] = {
1cdfb00
+        POPT_AUTOHELP
1cdfb00
+        SSSD_DEBUG_OPTS
1cdfb00
+        POPT_TABLEEND
1cdfb00
+    };
1cdfb00
+
1cdfb00
+    const UnitTest tests[] = {
1cdfb00
+        unit_test(test_sysdb_handle_original_uuid),
1cdfb00
+    };
1cdfb00
+
1cdfb00
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
1cdfb00
+    debug_level = SSSDBG_INVALID;
1cdfb00
+
1cdfb00
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
1cdfb00
+    while((opt = poptGetNextOpt(pc)) != -1) {
1cdfb00
+        switch(opt) {
1cdfb00
+        default:
1cdfb00
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
1cdfb00
+                    poptBadOption(pc, 0), poptStrerror(opt));
1cdfb00
+            poptPrintUsage(pc, stderr, 0);
1cdfb00
+            return 1;
1cdfb00
+        }
1cdfb00
+    }
1cdfb00
+    poptFreeContext(pc);
1cdfb00
+
1cdfb00
+    DEBUG_CLI_INIT(debug_level);
1cdfb00
+
1cdfb00
+    tests_set_cwd();
1cdfb00
+    rv = run_tests(tests);
1cdfb00
+
1cdfb00
+    return rv;
1cdfb00
+}
1cdfb00
diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c
1cdfb00
index 2203e2c49efacab27e9cf4c6c699319cfdf5370c..4cc6ec85add6bb8f9ae17ddc9af38b0316c9d49f 100644
1cdfb00
--- a/src/tests/cmocka/test_utils.c
1cdfb00
+++ b/src/tests/cmocka/test_utils.c
1cdfb00
@@ -1127,6 +1127,7 @@ int main(int argc, const char *argv[])
1cdfb00
         cmocka_unit_test(test_textual_public_key),
1cdfb00
         cmocka_unit_test(test_replace_whitespaces),
1cdfb00
         cmocka_unit_test(test_reverse_replace_whitespaces),
1cdfb00
+        cmocka_unit_test(test_guid_blob_to_string_buf),
1cdfb00
         cmocka_unit_test_setup_teardown(test_add_strings_lists,
1cdfb00
                                         setup_add_strings_lists,
1cdfb00
                                         teardown_add_strings_lists),
1cdfb00
diff --git a/src/tests/cmocka/test_utils.h b/src/tests/cmocka/test_utils.h
1cdfb00
index f85ac2f2b3c50a60099970752b06adbad38b9fd1..61ef7e43a82649d775d9b932def9e957b0761bed 100644
1cdfb00
--- a/src/tests/cmocka/test_utils.h
1cdfb00
+++ b/src/tests/cmocka/test_utils.h
1cdfb00
@@ -29,5 +29,6 @@ void test_textual_public_key(void **state);
1cdfb00
 /* from src/tests/cmocka/test_string_utils.c */
1cdfb00
 void test_replace_whitespaces(void **state);
1cdfb00
 void test_reverse_replace_whitespaces(void **state);
1cdfb00
+void test_guid_blob_to_string_buf(void **state);
1cdfb00
 
1cdfb00
 #endif /* __TESTS__CMOCKA__TEST_UTILS_H__ */
1cdfb00
diff --git a/src/tests/cwrap/Makefile.am b/src/tests/cwrap/Makefile.am
1cdfb00
index c1991a19c3a39f957b6547854126ff6d219394e0..b805e834977f60d6ba2efdf0f700061bb5e0e264 100644
1cdfb00
--- a/src/tests/cwrap/Makefile.am
1cdfb00
+++ b/src/tests/cwrap/Makefile.am
1cdfb00
@@ -78,6 +78,7 @@ server_tests_SOURCES = \
1cdfb00
     ../../../src/util/atomic_io.c \
1cdfb00
     ../../../src/util/signal.c \
1cdfb00
     ../../../src/util/util.c \
1cdfb00
+    ../../../src/util/string_utils.c \
1cdfb00
     ../../../src/util/strtonum.c \
1cdfb00
     ../../../src/util/util_errors.c \
1cdfb00
     ../../../src/util/safe-format-string.c \
1cdfb00
@@ -115,6 +116,7 @@ usertools_tests_SOURCES = \
1cdfb00
     ../../../src/util/domain_info_utils.c \
1cdfb00
     ../../../src/util/safe-format-string.c \
1cdfb00
     ../../../src/util/usertools.c \
1cdfb00
+    ../../../src/util/string_utils.c \
1cdfb00
     ../../../src/util/strtonum.c \
1cdfb00
     ../../../src/util/backup_file.c \
1cdfb00
     ../../../src/util/atomic_io.c \
1cdfb00
diff --git a/src/util/string_utils.c b/src/util/string_utils.c
1cdfb00
index a39b950e852de7ed43d6e8a32de3e7fb08a0dc56..71b2a092018076fd9c20ef9ac39a11964876cfc3 100644
1cdfb00
--- a/src/util/string_utils.c
1cdfb00
+++ b/src/util/string_utils.c
1cdfb00
@@ -83,3 +83,28 @@ char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx,
1cdfb00
 
1cdfb00
     return replace_char(mem_ctx, orig_name, subst, ' ');
1cdfb00
 }
1cdfb00
+
1cdfb00
+errno_t guid_blob_to_string_buf(const uint8_t *blob, char *str_buf,
1cdfb00
+                                size_t buf_size)
1cdfb00
+{
1cdfb00
+    int ret;
1cdfb00
+
1cdfb00
+    if (blob == NULL || str_buf == NULL || buf_size < GUID_STR_BUF_SIZE) {
1cdfb00
+        DEBUG(SSSDBG_CRIT_FAILURE, "Buffer too small.\n");
1cdfb00
+        return EINVAL;
1cdfb00
+    }
1cdfb00
+
1cdfb00
+    ret = snprintf(str_buf, buf_size,
1cdfb00
+         "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1cdfb00
+         blob[3], blob[2], blob[1], blob[0],
1cdfb00
+         blob[5], blob[4],
1cdfb00
+         blob[7], blob[6],
1cdfb00
+         blob[8], blob[9],
1cdfb00
+         blob[10], blob[11],blob[12], blob[13],blob[14], blob[15]);;
1cdfb00
+    if (ret != (GUID_STR_BUF_SIZE -1)) {
1cdfb00
+        DEBUG(SSSDBG_CRIT_FAILURE, "snprintf failed.\n");
1cdfb00
+        return EIO;
1cdfb00
+    }
1cdfb00
+
1cdfb00
+    return EOK;
1cdfb00
+}
1cdfb00
diff --git a/src/util/util.h b/src/util/util.h
1cdfb00
index 22a67a55855282441379477236a323362c8bdb4d..91df09914abfa1a72e9280ab708e11abf9e07e18 100644
1cdfb00
--- a/src/util/util.h
1cdfb00
+++ b/src/util/util.h
1cdfb00
@@ -618,6 +618,13 @@ char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx,
1cdfb00
                                  const char *orig_name,
1cdfb00
                                  const char replace_char);
1cdfb00
 
1cdfb00
+#define GUID_BIN_LENGTH 16
1cdfb00
+/* 16 2-digit hex values + 4 dashes + terminating 0 */
1cdfb00
+#define GUID_STR_BUF_SIZE (2 * GUID_BIN_LENGTH + 4 + 1)
1cdfb00
+
1cdfb00
+errno_t guid_blob_to_string_buf(const uint8_t *blob, char *str_buf,
1cdfb00
+                                size_t buf_size);
1cdfb00
+
1cdfb00
 /* from become_user.c */
1cdfb00
 errno_t become_user(uid_t uid, gid_t gid);
1cdfb00
 struct sss_creds;
1cdfb00
-- 
1cdfb00
2.4.0
1cdfb00