[SSSD] [PATCH] added a ASQ search API for sysdb
Sumit Bose
sbose at redhat.com
Tue Oct 27 16:45:58 UTC 2009
On Tue, Oct 27, 2009 at 10:27:40AM -0400, Simo Sorce wrote:
> On Mon, 2009-10-26 at 17:43 +0100, Sumit Bose wrote:
> > Hi,
> >
> > this patch adds a sysdb interface for ASQ (attribute scoped query)
> > searches. These are useful to limit searches to objects listed in
> > member/memberof attributes (this is not limited to member/memberof you
> > can take any attribute you want).
>
> The sysdb part looks good, so I'd say: ACK
>
> The tests should work fine although I have a couple of remarks on the
> style, and what is tested.
>
> The tests use a synchronous style, so in this case the _done() function
> should just get you out of the loop.
> The _recv() function should be called after the test_loop() returns.
> We are being synchronous here so no need to suffer the pain of jumping
> through functions to see the program flow.
>
> Also the test itself is not a request, so the request structure in this
> case should be named "req" and not "subreq".
>
> You are testing the return of only one result, it may be worth testing
> with something that will return at least 2 results to exercise the
> realloc machinery and the checks with more than one result.
>
> Simo.
>
Thanks for review. I have modified the test accordingly and fixed the
indentation in sysdb.h and sysdb_ops.c.
bye,
Sumit
-------------- next part --------------
>From bfd161aa6e2cbf1c03be3269d4f79e6b5684c360 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose at redhat.com>
Date: Mon, 26 Oct 2009 13:03:22 +0100
Subject: [PATCH] added a ASQ search API for sysdb
---
server/db/sysdb.h | 12 +++
server/db/sysdb_ops.c | 218 ++++++++++++++++++++++++++++++++++++++++++++
server/tests/sysdb-tests.c | 126 +++++++++++++++++++++++++
3 files changed, 356 insertions(+), 0 deletions(-)
diff --git a/server/db/sysdb.h b/server/db/sysdb.h
index dfb53aa..7e7a29c 100644
--- a/server/db/sysdb.h
+++ b/server/db/sysdb.h
@@ -551,4 +551,16 @@ struct tevent_req *sysdb_delete_custom_send(TALLOC_CTX *mem_ctx,
const char *object_name,
const char *subtree_name);
int sysdb_delete_custom_recv(struct tevent_req *req);
+
+struct tevent_req *sysdb_asq_search_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct sysdb_ctx *sysdb,
+ struct sysdb_handle *handle,
+ struct sss_domain_info *domain,
+ struct ldb_dn *base_dn,
+ const char *expression,
+ const char *asq_attribute,
+ const char **attrs);
+int sysdb_asq_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+ size_t *msgs_count, struct ldb_message ***msgs);
#endif /* __SYS_DB_H__ */
diff --git a/server/db/sysdb_ops.c b/server/db/sysdb_ops.c
index e045ad7..ae5c7f0 100644
--- a/server/db/sysdb_ops.c
+++ b/server/db/sysdb_ops.c
@@ -3828,3 +3828,221 @@ int sysdb_delete_custom_recv(struct tevent_req *req)
return EOK;
}
+
+/* = ASQ search request ======================================== */
+struct sysdb_asq_search_state {
+ struct tevent_context *ev;
+ struct sysdb_ctx *sysdb;
+ struct sysdb_handle *handle;
+ struct sss_domain_info *domain;
+ struct ldb_dn *base_dn;
+ const char *asq_attribute;
+ const char **attrs;
+ const char *expression;
+
+ int msgs_count;
+ struct ldb_message **msgs;
+};
+
+void sysdb_asq_search_check_handle_done(struct tevent_req *subreq);
+static void sysdb_asq_search_done(struct tevent_req *subreq);
+
+struct tevent_req *sysdb_asq_search_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct sysdb_ctx *sysdb,
+ struct sysdb_handle *handle,
+ struct sss_domain_info *domain,
+ struct ldb_dn *base_dn,
+ const char *expression,
+ const char *asq_attribute,
+ const char **attrs)
+{
+ struct tevent_req *req;
+ struct tevent_req *subreq;
+ struct sysdb_asq_search_state *state;
+ int ret;
+
+ if (sysdb == NULL && handle == NULL) {
+ DEBUG(1, ("Sysdb context not available.\n"));
+ return NULL;
+ }
+
+ req = tevent_req_create(mem_ctx, &state, struct sysdb_asq_search_state);
+ if (req == NULL) {
+ DEBUG(1, ("tevent_req_create failed.\n"));
+ return NULL;
+ }
+
+ state->ev = ev;
+ state->sysdb = (sysdb == NULL) ? handle->ctx : sysdb;
+ state->handle = handle;
+ state->domain = domain;
+ state->base_dn = base_dn;
+ state->expression = expression;
+ state->asq_attribute = asq_attribute;
+ state->attrs = attrs;
+
+ state->msgs_count = 0;
+ state->msgs = NULL;
+
+ subreq = sysdb_check_handle_send(state, state->ev, state->sysdb,
+ state->handle);
+ if (!subreq) {
+ DEBUG(1, ("sysdb_check_handle_send failed.\n"));
+ ret = ENOMEM;
+ goto fail;
+ }
+ tevent_req_set_callback(subreq, sysdb_asq_search_check_handle_done, req);
+
+ return req;
+
+fail:
+ tevent_req_error(req, ret);
+ tevent_req_post(req, ev);
+ return req;
+}
+
+void sysdb_asq_search_check_handle_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(subreq,
+ struct tevent_req);
+ struct sysdb_asq_search_state *state = tevent_req_data(req,
+ struct sysdb_asq_search_state);
+ struct ldb_request *ldb_req;
+ struct ldb_control **ctrl;
+ struct ldb_asq_control *asq_control;
+ int ret;
+
+ ret = sysdb_check_handle_recv(subreq, state, &state->handle);
+ talloc_zfree(subreq);
+ if (ret != EOK) {
+ tevent_req_error(req, ret);
+ return;
+ }
+
+ ctrl = talloc_array(state, struct ldb_control *, 2);
+ if (ctrl == NULL) {
+ ret = ENOMEM;
+ goto fail;
+ }
+
+ ctrl[0] = talloc(ctrl, struct ldb_control);
+ if (ctrl[0] == NULL) {
+ ret = ENOMEM;
+ goto fail;
+ }
+ ctrl[1] = NULL;
+
+ ctrl[0]->oid = LDB_CONTROL_ASQ_OID;
+ ctrl[0]->critical = 1;
+
+ asq_control = talloc(ctrl[0], struct ldb_asq_control);
+ if (asq_control == NULL) {
+ ret = ENOMEM;
+ goto fail;
+ }
+
+ asq_control->request = 1;
+ asq_control->source_attribute = talloc_strdup(asq_control,
+ state->asq_attribute);
+ if (asq_control->source_attribute == NULL) {
+ ret = ENOMEM;
+ goto fail;
+ }
+ asq_control->src_attr_len = strlen(asq_control->source_attribute);
+ ctrl[0]->data = asq_control;
+
+ ret = ldb_build_search_req(&ldb_req, state->handle->ctx->ldb, state,
+ state->base_dn, LDB_SCOPE_BASE,
+ state->expression, state->attrs, ctrl,
+ NULL, NULL, NULL);
+ if (ret != LDB_SUCCESS) {
+ ret = sysdb_error_to_errno(ret);
+ goto fail;
+ }
+
+ subreq = sldb_request_send(state, state->ev, state->handle->ctx->ldb,
+ ldb_req);
+ if (!subreq) {
+ ret = ENOMEM;
+ goto fail;
+ }
+
+ tevent_req_set_callback(subreq, sysdb_asq_search_done, req);
+ return;
+
+fail:
+ tevent_req_error(req, ret);
+ return;
+}
+
+static void sysdb_asq_search_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(subreq,
+ struct tevent_req);
+ struct sysdb_asq_search_state *state = tevent_req_data(req,
+ struct sysdb_asq_search_state);
+ struct ldb_reply *ldbreply;
+ int ret;
+
+ ret = sldb_request_recv(subreq, state, &ldbreply);
+ if (ret != EOK) {
+ talloc_free(subreq);
+ DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret)));
+ tevent_req_error(req, ret);
+ return;
+ }
+
+ switch (ldbreply->type) {
+ case LDB_REPLY_ENTRY:
+ state->msgs = talloc_realloc(state, state->msgs,
+ struct ldb_message *,
+ state->msgs_count + 2);
+ if (state->msgs == NULL) {
+ tevent_req_error(req, ENOMEM);
+ return;
+ }
+
+ state->msgs[state->msgs_count + 1] = NULL;
+
+ state->msgs[state->msgs_count] = talloc_steal(state->msgs,
+ ldbreply->message);
+ state->msgs_count++;
+
+ talloc_zfree(ldbreply);
+ return;
+
+ case LDB_REPLY_DONE:
+ break;
+
+ default:
+ DEBUG(1, ("Unknown ldb reply type [%d].\n", ldbreply->type));
+ tevent_req_error(req, EINVAL);
+ return;
+ }
+
+ talloc_zfree(subreq);
+ tevent_req_done(req);
+}
+
+int sysdb_asq_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+ size_t *msgs_count, struct ldb_message ***msgs)
+{
+ struct sysdb_asq_search_state *state = tevent_req_data(req,
+ struct sysdb_asq_search_state);
+ enum tevent_req_state tstate;
+ uint64_t err;
+ int i;
+
+ if (tevent_req_is_error(req, &tstate, &err)) {
+ return err;
+ }
+
+ *msgs_count = state->msgs_count;
+ for (i = 0; i < state->msgs_count; i++) {
+ talloc_steal(mem_ctx, state->msgs[i]);
+ }
+ *msgs = talloc_move(mem_ctx, &state->msgs);
+
+ return EOK;
+}
diff --git a/server/tests/sysdb-tests.c b/server/tests/sysdb-tests.c
index ce69aa0..eb45619 100644
--- a/server/tests/sysdb-tests.c
+++ b/server/tests/sysdb-tests.c
@@ -40,6 +40,9 @@
#define CUSTOM_TEST_CONTAINER "custom_test_container"
#define CUSTOM_TEST_OBJECT "custom_test_object"
+#define ASQ_TEST_USER "testuser27010"
+#define ASQ_TEST_USER_UID 27010
+
struct sysdb_test_ctx {
struct sysdb_ctx *sysdb;
struct confdb_ctx *confdb;
@@ -955,6 +958,14 @@ static void test_delete_custom_done(struct tevent_req *subreq)
return test_return(data, ret);
}
+static void test_asq_search_done(struct tevent_req *req)
+{
+ struct test_data *data = tevent_req_callback_data(req, struct test_data);
+
+ data->finished = true;
+ return;
+}
+
START_TEST (test_sysdb_store_user)
{
struct sysdb_test_ctx *test_ctx;
@@ -1979,6 +1990,117 @@ START_TEST (test_sysdb_delete_custom)
}
END_TEST
+START_TEST (test_sysdb_prepare_asq_test_user)
+{
+ struct sysdb_test_ctx *test_ctx;
+ struct test_data *data;
+ struct tevent_req *req;
+ int ret;
+
+ /* Setup */
+ ret = setup_sysdb_tests(&test_ctx);
+ if (ret != EOK) {
+ fail("Could not set up the test");
+ return;
+ }
+
+ data = talloc_zero(test_ctx, struct test_data);
+ data->ctx = test_ctx;
+ data->ev = test_ctx->ev;
+ data->groupname = talloc_asprintf(data, "testgroup%d", _i);
+ data->uid = ASQ_TEST_USER_UID;
+
+ req = sysdb_transaction_send(data, data->ev, test_ctx->sysdb);
+ if (!req) {
+ ret = ENOMEM;
+ }
+
+ if (ret == EOK) {
+ tevent_req_set_callback(req, test_add_group_member, data);
+
+ ret = test_loop(data);
+ }
+
+ fail_if(ret != EOK, "Could not modify group %s", data->groupname);
+ talloc_free(test_ctx);
+}
+END_TEST
+
+START_TEST (test_sysdb_asq_search)
+{
+ struct sysdb_test_ctx *test_ctx;
+ struct test_data *data;
+ struct tevent_req *req;
+ struct ldb_dn *user_dn;
+ int ret;
+ size_t msgs_count;
+ struct ldb_message **msgs;
+ int i;
+ char *gid_str;
+
+ /* Setup */
+ ret = setup_sysdb_tests(&test_ctx);
+ if (ret != EOK) {
+ fail("Could not set up the test");
+ return;
+ }
+
+ data = talloc_zero(test_ctx, struct test_data);
+ data->ctx = test_ctx;
+ data->ev = test_ctx->ev;
+ data->attrlist = talloc_array(data, const char *, 2);
+ fail_unless(data->attrlist != NULL, "talloc_array failed");
+
+ data->attrlist[0] = "gidNumber";
+ data->attrlist[1] = NULL;
+
+ user_dn = sysdb_user_dn(data->ctx->sysdb, data, "LOCAL", ASQ_TEST_USER);
+ fail_unless(user_dn != NULL, "sysdb_user_dn failed");
+
+ req = sysdb_asq_search_send(data, data->ev, test_ctx->sysdb, NULL,
+ test_ctx->domain, user_dn, NULL, "memberof",
+ data->attrlist);
+ if (!req) {
+ ret = ENOMEM;
+ }
+
+ if (ret == EOK) {
+ tevent_req_set_callback(req, test_asq_search_done, data);
+
+ ret = test_loop(data);
+
+ ret = sysdb_asq_search_recv(req, data, &msgs_count, &msgs);
+ talloc_zfree(req);
+ fail_unless(ret == EOK, "sysdb_asq_search_send failed");
+
+ fail_unless(msgs_count == 10, "wrong number of results, "
+ "found [%d] expected [10]", msgs_count);
+
+ for (i = 0; i < msgs_count; i++) {
+ fail_unless(msgs[i]->num_elements == 1, "wrong number of elements, "
+ "found [%d] expected [1]",
+ msgs[i]->num_elements);
+
+ fail_unless(msgs[i]->elements[0].num_values == 1,
+ "wrong number of values, found [%d] expected [1]",
+ msgs[i]->elements[0].num_values);
+
+ gid_str = talloc_asprintf(data, "%d", 28010 + i);
+ fail_unless(gid_str != NULL, "talloc_asprintf failed.");
+ fail_unless(strncmp(gid_str,
+ (const char *) msgs[i]->elements[0].values[0].data,
+ msgs[i]->elements[0].values[0].length) == 0,
+ "wrong value, found [%.*s] expected [%s]",
+ msgs[i]->elements[0].values[0].length,
+ msgs[i]->elements[0].values[0].data, gid_str);
+ }
+ }
+
+ fail_if(ret != EOK, "Failed to send ASQ search request.\n");
+ talloc_free(test_ctx);
+}
+END_TEST
+
Suite *create_sysdb_suite(void)
{
Suite *s = suite_create("sysdb");
@@ -2041,6 +2163,10 @@ Suite *create_sysdb_suite(void)
/* Add some members to the groups */
tcase_add_loop_test(tc_sysdb, test_sysdb_add_group_member, 28010, 28020);
+ /* ASQ search test */
+ tcase_add_loop_test(tc_sysdb, test_sysdb_prepare_asq_test_user, 28011, 28020);
+ tcase_add_test(tc_sysdb, test_sysdb_asq_search);
+
/* Remove the members from the groups */
tcase_add_loop_test(tc_sysdb, test_sysdb_remove_group_member, 28010, 28020);
--
1.6.2.5
More information about the sssd-devel
mailing list