Blame SOURCES/0201-sdap-properly-handle-binary-objectGuid-attribute.patch

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