From 1f5796f2ab0a848712a7f5bc85c9f7c2f22a8a0f Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Wed, 5 Nov 2014 21:01:08 +0100 Subject: [PATCH 095/104] sysdb: add sysdb_search_object_by_uuid() Related to https://fedorahosted.org/sssd/ticket/2481 Reviewed-by: Jakub Hrozek --- src/db/sysdb.h | 7 +++++ src/db/sysdb_ops.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ src/tests/sysdb-tests.c | 51 ++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) diff --git a/src/db/sysdb.h b/src/db/sysdb.h index f582f6a516e43a453741acacbe3ca6957e23fc37..7a51117f439dc54ded3433c230d2d262a4d660dc 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -185,6 +185,7 @@ #define SYSDB_NETGR_TRIPLES_FILTER "(|("SYSDB_NAME_ALIAS"=%s)("SYSDB_NAME"=%s)("SYSDB_NAME_ALIAS"=%s)("SYSDB_MEMBEROF"=%s))" #define SYSDB_SID_FILTER "(&(|("SYSDB_UC")("SYSDB_GC"))("SYSDB_SID_STR"=%s))" +#define SYSDB_UUID_FILTER "(&(|("SYSDB_UC")("SYSDB_GC"))("SYSDB_UUID"=%s))" #define SYSDB_HAS_ENUMERATED "has_enumerated" @@ -1030,6 +1031,12 @@ errno_t sysdb_search_object_by_sid(TALLOC_CTX *mem_ctx, const char **attrs, struct ldb_result **msg); +errno_t sysdb_search_object_by_uuid(TALLOC_CTX *mem_ctx, + struct sss_domain_info *domain, + const char *uuid_str, + const char **attrs, + struct ldb_result **res); + /* === Functions related to GPOs === */ #define SYSDB_GPO_CONTAINER "cn=gpos,cn=ad,cn=custom" diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c index c88ff9b4bf39c649bc7993f3ed56729b7bfde310..998046a2ca1c746b2032f430e5f9c4a7151e1dbc 100644 --- a/src/db/sysdb_ops.c +++ b/src/db/sysdb_ops.c @@ -3499,12 +3499,79 @@ done: return ret; } +static errno_t sysdb_search_object_by_str_attr(TALLOC_CTX *mem_ctx, + struct sss_domain_info *domain, + const char *filter_tmpl, + const char *str, + const char **attrs, + struct ldb_result **_res) +{ + TALLOC_CTX *tmp_ctx; + const char *def_attrs[] = { SYSDB_NAME, SYSDB_UIDNUM, SYSDB_GIDNUM, + ORIGINALAD_PREFIX SYSDB_NAME, + SYSDB_OBJECTCLASS, NULL }; + struct ldb_dn *basedn; + int ret; + struct ldb_result *res = NULL; + + tmp_ctx = talloc_new(NULL); + if (!tmp_ctx) { + return ENOMEM; + } + + basedn = ldb_dn_new_fmt(tmp_ctx, domain->sysdb->ldb, SYSDB_DOM_BASE, + domain->name); + if (basedn == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "ldb_dn_new_fmt failed.\n"); + ret = ENOMEM; + goto done; + } + + ret = ldb_search(domain->sysdb->ldb, tmp_ctx, &res, + basedn, LDB_SCOPE_SUBTREE, attrs?attrs:def_attrs, + filter_tmpl, str); + if (ret != EOK) { + ret = sysdb_error_to_errno(ret); + DEBUG(SSSDBG_OP_FAILURE, "ldb_search failed.\n"); + goto done; + } + + if (res->count > 1) { + DEBUG(SSSDBG_CRIT_FAILURE, "Search for [%s] with filter [%s] " \ + "returned more than one object.\n", + str, filter_tmpl); + ret = EINVAL; + goto done; + } else if (res->count == 0) { + ret = ENOENT; + goto done; + } + + *_res = talloc_steal(mem_ctx, res); + +done: + if (ret == ENOENT) { + DEBUG(SSSDBG_TRACE_FUNC, "No such entry.\n"); + } else if (ret) { + DEBUG(SSSDBG_OP_FAILURE, "Error: %d (%s)\n", ret, strerror(ret)); + } + + talloc_zfree(tmp_ctx); + return ret; +} + errno_t sysdb_search_object_by_sid(TALLOC_CTX *mem_ctx, struct sss_domain_info *domain, const char *sid_str, const char **attrs, struct ldb_result **msg) { +/* TODO: use + return sysdb_search_object_by_str_attr(mem_ctx, domain, SYSDB_SID_FILTER, + sid_str, attrs, res); + + when verified that all callers can handle ENOENT correctly. */ + TALLOC_CTX *tmp_ctx; const char *def_attrs[] = { SYSDB_NAME, SYSDB_UIDNUM, SYSDB_GIDNUM, ORIGINALAD_PREFIX SYSDB_NAME, @@ -3553,3 +3620,13 @@ done: talloc_zfree(tmp_ctx); return ret; } + +errno_t sysdb_search_object_by_uuid(TALLOC_CTX *mem_ctx, + struct sss_domain_info *domain, + const char *uuid_str, + const char **attrs, + struct ldb_result **res) +{ + return sysdb_search_object_by_str_attr(mem_ctx, domain, SYSDB_UUID_FILTER, + uuid_str, attrs, res); +} diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c index e01ddf4782c0a5a557f39d1adc2efd74b6234461..e9618727d4f8f4c544b28cce9d98b82000de3aad 100644 --- a/src/tests/sysdb-tests.c +++ b/src/tests/sysdb-tests.c @@ -5026,6 +5026,54 @@ START_TEST(test_sysdb_search_sid_str) } END_TEST +START_TEST(test_sysdb_search_object_by_uuid) +{ + errno_t ret; + struct sysdb_test_ctx *test_ctx; + struct ldb_result *res; + struct sysdb_attrs *attrs = NULL; + + /* Setup */ + ret = setup_sysdb_tests(&test_ctx); + fail_if(ret != EOK, "Could not set up the test"); + + attrs = sysdb_new_attrs(test_ctx); + fail_unless(attrs != NULL, "sysdb_new_attrs failed"); + + ret = sysdb_attrs_add_string(attrs, SYSDB_UUID, + "11111111-2222-3333-4444-555555555555"); + fail_unless(ret == EOK, "sysdb_attrs_add_string failed with [%d][%s].", + ret, strerror(ret)); + + ret = sysdb_add_user(test_ctx->domain, "UUIDuser", + 123456, 0, "UUID user", "/home/uuiduser", "/bin/bash", + NULL, attrs, 0, 0); + fail_unless(ret == EOK, "sysdb_add_user failed with [%d][%s].", + ret, strerror(ret)); + + ret = sysdb_search_object_by_uuid(test_ctx, test_ctx->domain, + "11111111-2222-3333-4444-555555555556", + NULL, &res); + fail_unless(ret == ENOENT, + "Unexpected return code from sysdb_search_object_by_uuid for " + "missing object, expected [%d], got [%d].", ENOENT, ret); + + ret = sysdb_search_object_by_uuid(test_ctx, test_ctx->domain, + "11111111-2222-3333-4444-555555555555", + NULL, &res); + fail_unless(ret == EOK, "sysdb_search_object_by_uuid failed with [%d][%s].", + ret, strerror(ret)); + fail_unless(res->count == 1, "Unexpected number of results, " \ + "expected [%u], get [%u].", 1, res->count); + fail_unless(strcmp(ldb_msg_find_attr_as_string(res->msgs[0], + SYSDB_NAME, ""), + "UUIDuser") == 0, "Unexpected object found, " \ + "expected [%s], got [%s].", "UUIDuser", + ldb_msg_find_attr_as_string(res->msgs[0],SYSDB_NAME, "")); + + talloc_free(test_ctx); +} +END_TEST START_TEST(test_sysdb_subdomain_create) { @@ -6090,6 +6138,9 @@ Suite *create_sysdb_suite(void) /* Test SID string searches */ tcase_add_test(tc_sysdb, test_sysdb_search_sid_str); + /* Test UUID string searches */ + tcase_add_test(tc_sysdb, test_sysdb_search_object_by_uuid); + /* Test canonicalizing names */ tcase_add_test(tc_sysdb, test_sysdb_get_real_name); -- 1.9.3