From f46dc8010a7d5fbb398e282d680703e1bd5963f4 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Tue, 29 May 2018 15:33:34 +0200
Subject: [PATCH] sysdb: add sysdb_getgrgid_attrs()
sysdb_getgrgid() is the only MPG aware by GID request but only supports
a fixes set of attributes. The new call allows to add additional
arguments.
Related to https://pagure.io/SSSD/sssd/issue/3748
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
(cherry picked from commit 8aa56a9e8744a7611fa26a254c4f9228e919c8ed)
---
src/db/sysdb.h | 6 ++++++
src/db/sysdb_ops.c | 3 +++
src/db/sysdb_search.c | 31 ++++++++++++++++++++++++++-----
src/tests/sysdb-tests.c | 37 +++++++++++++++++++++++++++++++++++++
4 files changed, 72 insertions(+), 5 deletions(-)
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 2660314a75a574d7f5625c8672e5261587056d1a..affd1c9053e43ff24c98cc8fb23eec2c4b69f955 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -779,6 +779,12 @@ int sysdb_getgrgid(TALLOC_CTX *mem_ctx,
gid_t gid,
struct ldb_result **res);
+int sysdb_getgrgid_attrs(TALLOC_CTX *mem_ctx,
+ struct sss_domain_info *domain,
+ gid_t gid,
+ const char **attrs,
+ struct ldb_result **res);
+
int sysdb_enumgrent(TALLOC_CTX *mem_ctx,
struct sss_domain_info *domain,
struct ldb_result **res);
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
index 024683317cab99743681db804f7026c8dbb33a38..c0d343bdabd324cbe8b9745c65c2a6e5a56321e1 100644
--- a/src/db/sysdb_ops.c
+++ b/src/db/sysdb_ops.c
@@ -709,6 +709,9 @@ int sysdb_search_group_by_name(TALLOC_CTX *mem_ctx,
return sysdb_search_by_name(mem_ctx, domain, name, SYSDB_GROUP, attrs, msg);
}
+/* Please note that sysdb_search_group_by_gid() is not aware of MPGs. If MPG
+ * support is needed either the caller must handle it or sysdb_getgrgid() or
+ * sysdb_getgrgid_attrs() should be used. */
int sysdb_search_group_by_gid(TALLOC_CTX *mem_ctx,
struct sss_domain_info *domain,
gid_t gid,
diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c
index 9f37cbcd50a778145518c15b6146ad812a5b4fa3..3d78a38b36e65febd50e41fc1d1be29eceeb7649 100644
--- a/src/db/sysdb_search.c
+++ b/src/db/sysdb_search.c
@@ -1036,24 +1036,37 @@ done:
return ret;
}
-int sysdb_getgrgid(TALLOC_CTX *mem_ctx,
- struct sss_domain_info *domain,
- gid_t gid,
- struct ldb_result **_res)
+int sysdb_getgrgid_attrs(TALLOC_CTX *mem_ctx,
+ struct sss_domain_info *domain,
+ gid_t gid,
+ const char **additional_attrs,
+ struct ldb_result **_res)
{
TALLOC_CTX *tmp_ctx;
unsigned long int ul_gid = gid;
- static const char *attrs[] = SYSDB_GRSRC_ATTRS;
const char *fmt_filter;
struct ldb_dn *base_dn;
struct ldb_result *res;
int ret;
+ static const char *default_attrs[] = SYSDB_GRSRC_ATTRS;
+ const char **attrs = NULL;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
+ if (additional_attrs == NULL) {
+ attrs = default_attrs;
+ } else {
+ ret = add_strings_lists(tmp_ctx, additional_attrs, default_attrs,
+ false, discard_const(&attrs));
+ if (ret != EOK) {
+ DEBUG(SSSDBG_OP_FAILURE, "add_strings_lists failed.\n");
+ goto done;
+ }
+ }
+
if (domain->mpg) {
fmt_filter = SYSDB_GRGID_MPG_FILTER;
base_dn = ldb_dn_new_fmt(tmp_ctx, domain->sysdb->ldb,
@@ -1092,6 +1105,14 @@ done:
return ret;
}
+int sysdb_getgrgid(TALLOC_CTX *mem_ctx,
+ struct sss_domain_info *domain,
+ gid_t gid,
+ struct ldb_result **_res)
+{
+ return sysdb_getgrgid_attrs(mem_ctx, domain, gid, NULL, _res);
+}
+
int sysdb_enumgrent_filter(TALLOC_CTX *mem_ctx,
struct sss_domain_info *domain,
const char *name_filter,
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index fc9936968bcde8370c7054ba303de4463b35e15a..30574679f51362d03d7b9e3f8a1d55889817e4c1 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -1114,6 +1114,42 @@ done:
}
END_TEST
+START_TEST (test_sysdb_getgrgid_attrs)
+{
+ struct sysdb_test_ctx *test_ctx;
+ struct test_data *data;
+ struct ldb_result *res;
+ int ret;
+ const char *attrs[] = { SYSDB_CREATE_TIME, NULL };
+ uint64_t ctime;
+
+ /* Setup */
+ ret = setup_sysdb_tests(&test_ctx);
+ if (ret != EOK) {
+ fail("Could not set up the test");
+ return;
+ }
+
+ data = test_data_new_group(test_ctx, _i);
+ fail_if(data == NULL, "OOM");
+
+ ret = sysdb_getgrgid_attrs(test_ctx,
+ test_ctx->domain,
+ data->gid, attrs, &res);
+ if (ret) {
+ fail("sysdb_getgrgid_attrs failed for gid %d (%d: %s)",
+ data->gid, ret, strerror(ret));
+ goto done;
+ }
+
+ ctime = ldb_msg_find_attr_as_uint64(res->msgs[0], SYSDB_CREATE_TIME, 0);
+ fail_unless(ctime != 0, "Missing create time");
+
+done:
+ talloc_free(test_ctx);
+}
+END_TEST
+
START_TEST (test_sysdb_search_groups)
{
struct sysdb_test_ctx *test_ctx;
@@ -7072,6 +7108,7 @@ Suite *create_sysdb_suite(void)
/* Verify the groups can be queried by GID */
tcase_add_loop_test(tc_sysdb, test_sysdb_getgrgid, 28010, 28020);
+ tcase_add_loop_test(tc_sysdb, test_sysdb_getgrgid_attrs, 28010, 28020);
/* Find the users by GID using a filter */
tcase_add_loop_test(tc_sysdb, test_sysdb_search_groups, 28010, 28020);
--
2.14.4