summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSumit Bose <sbose@redhat.com>2014-11-05 21:01:08 +0100
committerJakub Hrozek <jhrozek@redhat.com>2014-11-20 10:52:33 +0100
commit907a7c626db407d19d4cae85c2db7d3561120349 (patch)
treebdfb9808f5706f6dd0010fb346e3bc1b469ec1c5
parenta5b55bdfcda8bfce8cb2ced981773998093d7857 (diff)
downloadsssd-907a7c626db407d19d4cae85c2db7d3561120349.zip
sssd-907a7c626db407d19d4cae85c2db7d3561120349.tar.gz
sssd-907a7c626db407d19d4cae85c2db7d3561120349.tar.xz
sysdb: add sysdb_search_object_by_uuid()
Related to https://fedorahosted.org/sssd/ticket/2481 Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
-rw-r--r--src/db/sysdb.h7
-rw-r--r--src/db/sysdb_ops.c77
-rw-r--r--src/tests/sysdb-tests.c51
3 files changed, 135 insertions, 0 deletions
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index f582f6a..7a51117 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 c88ff9b..998046a 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 d603a7a..26835d9 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -5032,6 +5032,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)
{
@@ -6096,6 +6144,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);