diff options
author | Simo Sorce <ssorce@redhat.com> | 2010-02-28 03:19:13 -0500 |
---|---|---|
committer | Stephen Gallagher <sgallagh@redhat.com> | 2010-04-12 09:22:12 -0400 |
commit | 5c69fd7c03e762a6fb08a7224eb1d6fd2967d09c (patch) | |
tree | a7c997552551da35fe9edc3f747668edf74e5242 | |
parent | 1c733ece101ca43b84c59a8dc7953346312dbf64 (diff) | |
download | sssd-5c69fd7c03e762a6fb08a7224eb1d6fd2967d09c.tar.gz sssd-5c69fd7c03e762a6fb08a7224eb1d6fd2967d09c.tar.bz2 sssd-5c69fd7c03e762a6fb08a7224eb1d6fd2967d09c.zip |
sysdb: convert sysdb_search_group_by_name/gid
-rw-r--r-- | src/db/sysdb.h | 30 | ||||
-rw-r--r-- | src/db/sysdb_ops.c | 440 | ||||
-rw-r--r-- | src/tests/sysdb-tests.c | 183 | ||||
-rw-r--r-- | src/tools/sss_groupshow.c | 220 |
4 files changed, 277 insertions, 596 deletions
diff --git a/src/db/sysdb.h b/src/db/sysdb.h index 9e638f50..aeea6224 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -367,23 +367,19 @@ int sysdb_search_user_by_uid(TALLOC_CTX *mem_ctx, struct ldb_message **msg); /* Search Group (gy gid or name) */ -struct tevent_req *sysdb_search_group_by_name_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - struct sysdb_ctx *sysdb, - struct sysdb_handle *handle, - struct sss_domain_info *domain, - const char *name, - const char **attrs); -struct tevent_req *sysdb_search_group_by_gid_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - struct sysdb_ctx *sysdb, - struct sysdb_handle *handle, - struct sss_domain_info *domain, - gid_t gid, - const char **attrs); -int sysdb_search_group_recv(struct tevent_req *req, - TALLOC_CTX *mem_ctx, - struct ldb_message **msg); +int sysdb_search_group_by_name(TALLOC_CTX *mem_ctx, + struct sysdb_ctx *ctx, + struct sss_domain_info *domain, + const char *name, + const char **attrs, + struct ldb_message **msg); + +int sysdb_search_group_by_gid(TALLOC_CTX *mem_ctx, + struct sysdb_ctx *ctx, + struct sss_domain_info *domain, + gid_t gid, + const char **attrs, + struct ldb_message **msg); /* Replace entry attrs */ struct tevent_req *sysdb_set_entry_attr_send(TALLOC_CTX *mem_ctx, diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c index d0cdcc37..9bc24880 100644 --- a/src/db/sysdb_ops.c +++ b/src/db/sysdb_ops.c @@ -444,193 +444,95 @@ done: /* =Search-Group-by-[GID/NAME]============================================ */ -struct sysdb_search_group_state { - struct tevent_context *ev; - struct sysdb_handle *handle; - - struct ldb_dn *basedn; - const char **attrs; - const char *filter; - int scope; - - size_t msgs_count; - struct ldb_message **msgs; -}; - -static void sysdb_search_group_cont(struct tevent_req *subreq); - -struct tevent_req *sysdb_search_group_by_name_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - struct sysdb_ctx *sysdb, - struct sysdb_handle *handle, - struct sss_domain_info *domain, - const char *name, - const char **attrs) +int sysdb_search_group_by_name(TALLOC_CTX *mem_ctx, + struct sysdb_ctx *ctx, + struct sss_domain_info *domain, + const char *name, + const char **attrs, + struct ldb_message **msg) { - struct tevent_req *req, *subreq; - struct sysdb_search_group_state *state; + TALLOC_CTX *tmpctx; static const char *def_attrs[] = { SYSDB_NAME, SYSDB_GIDNUM, NULL }; + struct ldb_message **msgs = NULL; + struct ldb_dn *basedn; + size_t msgs_count = 0; int ret; - if (!sysdb && !handle) return NULL; - - req = tevent_req_create(mem_ctx, &state, struct sysdb_search_group_state); - if (!req) return NULL; - - state->ev = ev; - state->handle = handle; - state->msgs_count = 0; - state->msgs = NULL; - - state->attrs = attrs ? attrs : def_attrs; - state->filter = NULL; - state->scope = LDB_SCOPE_BASE; - - if (!sysdb) sysdb = handle->ctx; - - state->basedn = sysdb_group_dn(sysdb, state, domain->name, name); - if (!state->basedn) { - ERROR_OUT(ret, ENOMEM, fail); + tmpctx = talloc_new(mem_ctx); + if (!tmpctx) { + return ENOMEM; } - if (!handle) { - subreq = sysdb_operation_send(state, state->ev, sysdb); - if (!subreq) { - ERROR_OUT(ret, ENOMEM, fail); - } - tevent_req_set_callback(subreq, sysdb_search_group_cont, req); + basedn = sysdb_group_dn(ctx, tmpctx, domain->name, name); + if (!basedn) { + ret = ENOMEM; + goto done; } - else { - ret = sysdb_search_entry(state, state->handle->ctx, state->basedn, - state->scope, state->filter, state->attrs, - &state->msgs_count, &state->msgs); - if (ret) { - goto fail; - } - tevent_req_done(req); - tevent_req_post(req, ev); + + ret = sysdb_search_entry(tmpctx, ctx, basedn, LDB_SCOPE_BASE, NULL, + attrs?attrs:def_attrs, &msgs_count, &msgs); + if (ret) { + goto done; } - return req; + *msg = talloc_steal(mem_ctx, msgs[0]); -fail: - DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret))); - tevent_req_error(req, ret); - tevent_req_post(req, ev); - return req; +done: + if (ret) { + DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret))); + } + talloc_zfree(tmpctx); + return ret; } -struct tevent_req *sysdb_search_group_by_gid_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - struct sysdb_ctx *sysdb, - struct sysdb_handle *handle, - struct sss_domain_info *domain, - gid_t gid, - const char **attrs) +int sysdb_search_group_by_gid(TALLOC_CTX *mem_ctx, + struct sysdb_ctx *ctx, + struct sss_domain_info *domain, + gid_t gid, + const char **attrs, + struct ldb_message **msg) { - struct tevent_req *req, *subreq; - struct sysdb_search_group_state *state; - static const char *def_attrs[] = { SYSDB_NAME, SYSDB_GIDNUM, NULL }; + TALLOC_CTX *tmpctx; + const char *def_attrs[] = { SYSDB_NAME, SYSDB_UIDNUM, NULL }; + struct ldb_message **msgs = NULL; + struct ldb_dn *basedn; + size_t msgs_count = 0; + char *filter; int ret; - if (!sysdb && !handle) return NULL; - - req = tevent_req_create(mem_ctx, &state, struct sysdb_search_group_state); - if (!req) return NULL; - - state->ev = ev; - state->handle = handle; - state->msgs_count = 0; - state->msgs = NULL; - state->attrs = attrs ? attrs : def_attrs; - - if (!sysdb) sysdb = handle->ctx; - - state->basedn = ldb_dn_new_fmt(state, sysdb->ldb, - SYSDB_TMPL_GROUP_BASE, domain->name); - if (!state->basedn) { - ERROR_OUT(ret, ENOMEM, fail); - } - - state->filter = talloc_asprintf(state, SYSDB_GRGID_FILTER, - (unsigned long)gid); - if (!state->filter) { - ERROR_OUT(ret, ENOMEM, fail); + tmpctx = talloc_new(mem_ctx); + if (!tmpctx) { + return ENOMEM; } - state->scope = LDB_SCOPE_ONELEVEL; - - if (!handle) { - subreq = sysdb_operation_send(state, state->ev, sysdb); - if (!subreq) { - ERROR_OUT(ret, ENOMEM, fail); - } - tevent_req_set_callback(subreq, sysdb_search_group_cont, req); - } - else { - ret = sysdb_search_entry(state, state->handle->ctx, state->basedn, - state->scope, state->filter, state->attrs, - &state->msgs_count, &state->msgs); - if (ret) { - goto fail; - } - tevent_req_done(req); - tevent_req_post(req, ev); + basedn = ldb_dn_new_fmt(tmpctx, ctx->ldb, + SYSDB_TMPL_GROUP_BASE, domain->name); + if (!basedn) { + ret = ENOMEM; + goto done; } - return req; - -fail: - DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret))); - tevent_req_error(req, ret); - tevent_req_post(req, ev); - return req; -} - -static void sysdb_search_group_cont(struct tevent_req *subreq) -{ - struct tevent_req *req = tevent_req_callback_data(subreq, - struct tevent_req); - struct sysdb_search_group_state *state = tevent_req_data(req, - struct sysdb_search_group_state); - int ret; - - ret = sysdb_operation_recv(subreq, state, &state->handle); - talloc_zfree(subreq); - if (ret) { - DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret))); - tevent_req_error(req, ret); - return; + filter = talloc_asprintf(tmpctx, SYSDB_GRGID_FILTER, (unsigned long)gid); + if (!filter) { + ret = ENOMEM; + goto done; } - ret = sysdb_search_entry(state, state->handle->ctx, state->basedn, - state->scope, state->filter, state->attrs, - &state->msgs_count, &state->msgs); + ret = sysdb_search_entry(tmpctx, ctx, basedn, LDB_SCOPE_ONELEVEL, filter, + attrs?attrs:def_attrs, &msgs_count, &msgs); if (ret) { - tevent_req_error(req, ret); - return; + goto done; } - tevent_req_done(req); -} - -int sysdb_search_group_recv(struct tevent_req *req, - TALLOC_CTX *mem_ctx, - struct ldb_message **msg) -{ - struct sysdb_search_group_state *state = tevent_req_data(req, - struct sysdb_search_group_state); - - TEVENT_REQ_RETURN_ON_ERROR(req); + *msg = talloc_steal(mem_ctx, msgs[0]); - if (state->msgs_count > 1) { - DEBUG(1, ("More than one result found.\n")); - return EFAULT; +done: + if (ret) { + DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret))); } - *msg = talloc_move(mem_ctx, &state->msgs[0]); - - return EOK; + talloc_zfree(tmpctx); + return ret; } @@ -1329,7 +1231,6 @@ struct sysdb_add_user_state { int cache_timeout; }; -static void sysdb_add_user_group_check(struct tevent_req *subreq); static void sysdb_add_user_basic_done(struct tevent_req *subreq); static void sysdb_add_user_get_id_done(struct tevent_req *subreq); static void sysdb_add_user_set_id_done(struct tevent_req *subreq); @@ -1396,13 +1297,12 @@ struct tevent_req *sysdb_add_user_send(TALLOC_CTX *mem_ctx, * Don't worry about users, if we try to add a user with the same * name the operation will fail */ - subreq = sysdb_search_group_by_name_send(state, ev, NULL, handle, - domain, name, NULL); - if (!subreq) { - ERROR_OUT(ret, ENOMEM, fail); + ret = sysdb_search_group_by_name(state, handle->ctx, + domain, name, NULL, &msg); + if (ret != ENOENT) { + if (ret == EOK) ret = EEXIST; + goto fail; } - tevent_req_set_callback(subreq, sysdb_add_user_group_check, req); - return req; } /* check no other user with the same uid exist */ @@ -1432,52 +1332,6 @@ fail: return req; } -static void sysdb_add_user_group_check(struct tevent_req *subreq) -{ - struct tevent_req *req = tevent_req_callback_data(subreq, - struct tevent_req); - struct sysdb_add_user_state *state = tevent_req_data(req, - struct sysdb_add_user_state); - struct ldb_message *msg; - int ret; - - /* We can succeed only if we get an ENOENT error, which means no groups - * with the same name exist. - * If any other error is returned fail as well. */ - ret = sysdb_search_group_recv(subreq, state, &msg); - talloc_zfree(subreq); - if (ret != ENOENT) { - if (ret == EOK) ret = EEXIST; - tevent_req_error(req, ret); - return; - } - - /* check no other user with the same uid exist */ - if (state->uid != 0) { - ret = sysdb_search_user_by_uid(state, state->handle->ctx, - state->domain, state->uid, NULL, &msg); - if (ret != ENOENT) { - if (ret == EOK) ret = EEXIST; - tevent_req_error(req, ret); - return; - } - } - - /* try to add the user */ - subreq = sysdb_add_basic_user_send(state, state->ev, state->handle, - state->domain, state->name, - state->uid, state->gid, - state->gecos, - state->homedir, - state->shell); - if (!subreq) { - DEBUG(6, ("Error: Out of memory\n")); - tevent_req_error(req, ENOMEM); - return; - } - tevent_req_set_callback(subreq, sysdb_add_user_basic_done, req); -} - static void sysdb_add_user_basic_done(struct tevent_req *subreq) { struct tevent_req *req = tevent_req_callback_data(subreq, @@ -1744,7 +1598,6 @@ struct sysdb_add_group_state { int cache_timeout; }; -static void sysdb_add_group_gid_check(struct tevent_req *subreq); static void sysdb_add_group_basic_done(struct tevent_req *subreq); static void sysdb_add_group_get_id_done(struct tevent_req *subreq); static void sysdb_add_group_set_attrs(struct tevent_req *req); @@ -1797,13 +1650,12 @@ struct tevent_req *sysdb_add_group_send(TALLOC_CTX *mem_ctx, /* check no other groups with the same gid exist */ if (state->gid != 0) { - subreq = sysdb_search_group_by_gid_send(state, ev, NULL, handle, - domain, gid, NULL); - if (!subreq) { - ERROR_OUT(ret, ENOMEM, fail); + ret = sysdb_search_group_by_gid(state, handle->ctx, + domain, gid, NULL, &msg); + if (ret != ENOENT) { + if (ret == EOK) ret = EEXIST; + goto fail; } - tevent_req_set_callback(subreq, sysdb_add_group_gid_check, req); - return req; } /* try to add the group */ @@ -1822,38 +1674,6 @@ fail: return req; } -static void sysdb_add_group_gid_check(struct tevent_req *subreq) -{ - struct tevent_req *req = tevent_req_callback_data(subreq, - struct tevent_req); - struct sysdb_add_group_state *state = tevent_req_data(req, - struct sysdb_add_group_state); - struct ldb_message *msg; - int ret; - - /* We can succeed only if we get an ENOENT error, which means no group - * with the same gid exist. - * If any other error is returned fail as well. */ - ret = sysdb_search_group_recv(subreq, state, &msg); - talloc_zfree(subreq); - if (ret != ENOENT) { - if (ret == EOK) ret = EEXIST; - tevent_req_error(req, ret); - return; - } - - /* try to add the group */ - subreq = sysdb_add_basic_group_send(state, state->ev, - state->handle, state->domain, - state->name, state->gid); - if (!subreq) { - DEBUG(6, ("Error: Out of memory\n")); - tevent_req_error(req, ENOMEM); - return; - } - tevent_req_set_callback(subreq, sysdb_add_group_basic_done, req); -} - static void sysdb_add_group_basic_done(struct tevent_req *subreq) { struct tevent_req *req = tevent_req_callback_data(subreq, @@ -2277,7 +2097,6 @@ struct sysdb_store_group_state { uint64_t cache_timeout; }; -static void sysdb_store_group_check(struct tevent_req *subreq); static void sysdb_store_group_add_done(struct tevent_req *subreq); static void sysdb_store_group_attr_done(struct tevent_req *subreq); @@ -2294,6 +2113,9 @@ struct tevent_req *sysdb_store_group_send(TALLOC_CTX *mem_ctx, struct sysdb_store_group_state *state; static const char *src_attrs[] = { SYSDB_NAME, SYSDB_GIDNUM, SYSDB_ORIG_MODSTAMP, NULL }; + struct ldb_message *msg; + bool new_group = false; + time_t now; int ret; req = tevent_req_create(mem_ctx, &state, struct sysdb_store_group_state); @@ -2307,38 +2129,10 @@ struct tevent_req *sysdb_store_group_send(TALLOC_CTX *mem_ctx, state->attrs = attrs; state->cache_timeout = cache_timeout; - subreq = sysdb_search_group_by_name_send(state, ev, NULL, handle, - domain, name, src_attrs); - if (!subreq) { - ERROR_OUT(ret, ENOMEM, fail); - } - tevent_req_set_callback(subreq, sysdb_store_group_check, req); - - return req; - -fail: - DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret))); - tevent_req_error(req, ret); - tevent_req_post(req, ev); - return req; -} - -static void sysdb_store_group_check(struct tevent_req *subreq) -{ - struct tevent_req *req = tevent_req_callback_data(subreq, - struct tevent_req); - struct sysdb_store_group_state *state = tevent_req_data(req, - struct sysdb_store_group_state); - struct ldb_message *msg; - time_t now = time(NULL); - bool new_group = false; - int ret; - - ret = sysdb_search_group_recv(subreq, state, &msg); - talloc_zfree(subreq); + ret = sysdb_search_group_by_name(state, handle->ctx, + domain, name, src_attrs, &msg); if (ret && ret != ENOENT) { - tevent_req_error(req, ret); - return; + goto fail; } if (ret == ENOENT) { new_group = true; @@ -2354,13 +2148,12 @@ static void sysdb_store_group_check(struct tevent_req *subreq) state->gid, state->attrs, state->cache_timeout); if (!subreq) { - DEBUG(6, ("Error: Out of memory\n")); - tevent_req_error(req, ENOMEM); - return; + ret = ENOMEM; + goto fail; } tevent_req_set_callback(subreq, sysdb_store_group_add_done, req); - return; + return req; } /* the group exists, let's just replace attributes when set */ @@ -2368,47 +2161,43 @@ static void sysdb_store_group_check(struct tevent_req *subreq) if (!state->attrs) { state->attrs = sysdb_new_attrs(state); if (!state->attrs) { - DEBUG(6, ("Error: Out of memory\n")); - tevent_req_error(req, ENOMEM); - return; + ret = ENOMEM; + goto fail; } } if (state->gid) { ret = sysdb_attrs_add_uint32(state->attrs, SYSDB_GIDNUM, state->gid); - if (ret) { - DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret))); - tevent_req_error(req, ret); - return; - } + if (ret) goto fail; } + now = time(NULL); + ret = sysdb_attrs_add_time_t(state->attrs, SYSDB_LAST_UPDATE, now); - if (ret) { - DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret))); - tevent_req_error(req, ret); - return; - } + if (ret) goto fail; ret = sysdb_attrs_add_time_t(state->attrs, SYSDB_CACHE_EXPIRE, ((state->cache_timeout) ? (now + state->cache_timeout) : 0)); - if (ret) { - DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret))); - tevent_req_error(req, ret); - return; - } + if (ret) goto fail; subreq = sysdb_set_group_attr_send(state, state->ev, state->handle, state->domain, state->name, state->attrs, SYSDB_MOD_REP); if (!subreq) { - DEBUG(6, ("Error: Out of memory\n")); - tevent_req_error(req, ENOMEM); - return; + ret = ENOMEM; + goto fail; } tevent_req_set_callback(subreq, sysdb_store_group_attr_done, req); + + return req; + +fail: + DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret))); + tevent_req_error(req, ret); + tevent_req_post(req, ev); + return req; } static void sysdb_store_group_add_done(struct tevent_req *subreq) @@ -3893,7 +3682,7 @@ void sysdb_delete_group_check_handle(struct tevent_req *subreq) struct tevent_req); struct sysdb_delete_group_state *state = tevent_req_data(req, struct sysdb_delete_group_state); - static const char *attrs[] = { SYSDB_NAME, SYSDB_GIDNUM, NULL }; + struct ldb_message *msg; int ret; ret = sysdb_check_handle_recv(subreq, state, &state->handle); @@ -3904,33 +3693,14 @@ void sysdb_delete_group_check_handle(struct tevent_req *subreq) } if (state->name) { - subreq = sysdb_search_group_by_name_send(state, state->ev, NULL, - state->handle, state->domain, - state->name, attrs); + ret = sysdb_search_group_by_name(state, state->handle->ctx, + state->domain, state->name, + NULL, &msg); } else { - subreq = sysdb_search_group_by_gid_send(state, state->ev, NULL, - state->handle, state->domain, - state->gid, NULL); - } - - if (!subreq) { - tevent_req_error(req, ENOMEM); - return; + ret = sysdb_search_group_by_gid(state, state->handle->ctx, + state->domain, state->gid, + NULL, &msg); } - tevent_req_set_callback(subreq, sysdb_delete_group_done, req); -} - -static void sysdb_delete_group_done(struct tevent_req *subreq) -{ - struct tevent_req *req = tevent_req_callback_data(subreq, - struct tevent_req); - struct sysdb_delete_group_state *state = tevent_req_data(req, - struct sysdb_delete_group_state); - struct ldb_message *msg; - int ret; - - ret = sysdb_search_group_recv(subreq, state, &msg); - talloc_zfree(subreq); if (ret) { tevent_req_error(req, ret); return; diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c index d5e2e771..1a850c35 100644 --- a/src/tests/sysdb-tests.c +++ b/src/tests/sysdb-tests.c @@ -2804,7 +2804,6 @@ START_TEST (test_sysdb_memberof_check_memberuid_without_group_5) { struct sysdb_test_ctx *test_ctx; struct test_data *data; - struct tevent_req *req; int ret; /* Setup */ @@ -2824,43 +2823,30 @@ START_TEST (test_sysdb_memberof_check_memberuid_without_group_5) data->attrlist[0] = "memberuid"; data->attrlist[1] = NULL; - req = sysdb_search_group_by_gid_send(data, data->ev, test_ctx->sysdb, NULL, - data->ctx->domain, - _i + MBO_GROUP_BASE, - data->attrlist); - if (!req) { - ret = ENOMEM; - } - - if (ret == EOK) { - tevent_req_set_callback(req, test_search_done, data); + ret = sysdb_search_group_by_gid(data, test_ctx->sysdb, + data->ctx->domain, _i + MBO_GROUP_BASE, + data->attrlist, &data->msg); + if (_i == 5) { + fail_unless(ret == ENOENT, + "sysdb_search_group_by_gid found " + "already deleted group"); + if (ret == ENOENT) ret = EOK; - ret = test_loop(data); + fail_if(ret != EOK, "Could not check group %d", data->gid); + } else { + fail_if(ret != EOK, "Could not check group %d", data->gid); - ret = sysdb_search_group_recv(req, data, &data->msg); - talloc_zfree(req); - if (_i == 5) { - fail_unless(ret == ENOENT, - "sysdb_search_group_by_gid_send found " - "already deleted group"); - ret = EOK; - } else { - fail_unless(ret == EOK, "sysdb_search_group_by_gid_send failed"); - - fail_unless(data->msg->num_elements == 1, - "Wrong number of results, expected [1] got [%d]", - data->msg->num_elements); - fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0, - "Wrong attribute name"); - fail_unless(data->msg->elements[0].num_values == ((_i + 1) % 6), - "Wrong number of attribute values, " - "expected [%d] got [%d]", ((_i + 1) % 6), - data->msg->elements[0].num_values); - } + fail_unless(data->msg->num_elements == 1, + "Wrong number of results, expected [1] got [%d]", + data->msg->num_elements); + fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0, + "Wrong attribute name"); + fail_unless(data->msg->elements[0].num_values == ((_i + 1) % 6), + "Wrong number of attribute values, " + "expected [%d] got [%d]", ((_i + 1) % 6), + data->msg->elements[0].num_values); } - fail_if(ret != EOK, "Could not check group %d", data->gid); - talloc_free(test_ctx); } END_TEST @@ -2869,7 +2855,6 @@ START_TEST (test_sysdb_memberof_check_memberuid) { struct sysdb_test_ctx *test_ctx; struct test_data *data; - struct tevent_req *req; int ret; /* Setup */ @@ -2889,35 +2874,21 @@ START_TEST (test_sysdb_memberof_check_memberuid) data->attrlist[0] = "memberuid"; data->attrlist[1] = NULL; - req = sysdb_search_group_by_gid_send(data, data->ev, test_ctx->sysdb, NULL, - data->ctx->domain, - _i + MBO_GROUP_BASE, - data->attrlist); - if (!req) { - ret = ENOMEM; - } - - if (ret == EOK) { - tevent_req_set_callback(req, test_search_done, data); - - ret = test_loop(data); - - ret = sysdb_search_group_recv(req, data, &data->msg); - talloc_zfree(req); - fail_unless(ret == EOK, "sysdb_search_group_by_gid_send failed"); - - fail_unless(data->msg->num_elements == 1, - "Wrong number of results, expected [1] got [%d]", - data->msg->num_elements); - fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0, - "Wrong attribute name"); - fail_unless(data->msg->elements[0].num_values == _i + 1, - "Wrong number of attribute values, expected [%d] got [%d]", - _i + 1, data->msg->elements[0].num_values); - } + ret = sysdb_search_group_by_gid(data, test_ctx->sysdb, + data->ctx->domain, _i + MBO_GROUP_BASE, + data->attrlist, &data->msg); fail_if(ret != EOK, "Could not check group %d", data->gid); + fail_unless(data->msg->num_elements == 1, + "Wrong number of results, expected [1] got [%d]", + data->msg->num_elements); + fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0, + "Wrong attribute name"); + fail_unless(data->msg->elements[0].num_values == _i + 1, + "Wrong number of attribute values, expected [%d] got [%d]", + _i + 1, data->msg->elements[0].num_values); + talloc_free(test_ctx); } END_TEST @@ -2926,7 +2897,6 @@ START_TEST (test_sysdb_memberof_check_memberuid_loop) { struct sysdb_test_ctx *test_ctx; struct test_data *data; - struct tevent_req *req; int ret; /* Setup */ @@ -2946,35 +2916,21 @@ START_TEST (test_sysdb_memberof_check_memberuid_loop) data->attrlist[0] = "memberuid"; data->attrlist[1] = NULL; - req = sysdb_search_group_by_gid_send(data, data->ev, test_ctx->sysdb, NULL, - data->ctx->domain, - _i + MBO_GROUP_BASE, - data->attrlist); - if (!req) { - ret = ENOMEM; - } - - if (ret == EOK) { - tevent_req_set_callback(req, test_search_done, data); - - ret = test_loop(data); - - ret = sysdb_search_group_recv(req, data, &data->msg); - talloc_zfree(req); - fail_unless(ret == EOK, "sysdb_search_group_by_gid_send failed"); - - fail_unless(data->msg->num_elements == 1, - "Wrong number of results, expected [1] got [%d]", - data->msg->num_elements); - fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0, - "Wrong attribute name"); - fail_unless(data->msg->elements[0].num_values == 10, - "Wrong number of attribute values, expected [%d] got [%d]", - 10, data->msg->elements[0].num_values); - } + ret = sysdb_search_group_by_gid(data, test_ctx->sysdb, + data->ctx->domain, _i + MBO_GROUP_BASE, + data->attrlist, &data->msg); fail_if(ret != EOK, "Could not check group %d", data->gid); + fail_unless(data->msg->num_elements == 1, + "Wrong number of results, expected [1] got [%d]", + data->msg->num_elements); + fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0, + "Wrong attribute name"); + fail_unless(data->msg->elements[0].num_values == 10, + "Wrong number of attribute values, expected [%d] got [%d]", + 10, data->msg->elements[0].num_values); + talloc_free(test_ctx); } END_TEST @@ -2983,7 +2939,6 @@ START_TEST (test_sysdb_memberof_check_memberuid_loop_without_group_5) { struct sysdb_test_ctx *test_ctx; struct test_data *data; - struct tevent_req *req; int ret; /* Setup */ @@ -3003,42 +2958,30 @@ START_TEST (test_sysdb_memberof_check_memberuid_loop_without_group_5) data->attrlist[0] = "memberuid"; data->attrlist[1] = NULL; - req = sysdb_search_group_by_gid_send(data, data->ev, test_ctx->sysdb, NULL, - data->ctx->domain, - _i + MBO_GROUP_BASE, - data->attrlist); - if (!req) { - ret = ENOMEM; - } + ret = sysdb_search_group_by_gid(data, test_ctx->sysdb, + data->ctx->domain, _i + MBO_GROUP_BASE, + data->attrlist, &data->msg); - if (ret == EOK) { - tevent_req_set_callback(req, test_search_done, data); + if (_i == 5) { + fail_unless(ret == ENOENT, + "sysdb_search_group_by_gid_send found " + "already deleted group"); + if (ret == ENOENT) ret = EOK; - ret = test_loop(data); + fail_if(ret != EOK, "Could not check group %d", data->gid); + } else { + fail_if(ret != EOK, "Could not check group %d", data->gid); - ret = sysdb_search_group_recv(req, data, &data->msg); - talloc_zfree(req); - if (_i == 5) { - fail_unless(ret == ENOENT, - "sysdb_search_group_by_gid_send found " - "already deleted group"); - ret = EOK; - } else { - fail_unless(ret == EOK, "sysdb_search_group_by_gid_send failed"); - - fail_unless(data->msg->num_elements == 1, - "Wrong number of results, expected [1] got [%d]", - data->msg->num_elements); - fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0, - "Wrong attribute name"); - fail_unless(data->msg->elements[0].num_values == ((_i + 5) % 10), - "Wrong number of attribute values, expected [%d] got [%d]", - ((_i + 5) % 10), data->msg->elements[0].num_values); - } + fail_unless(data->msg->num_elements == 1, + "Wrong number of results, expected [1] got [%d]", + data->msg->num_elements); + fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0, + "Wrong attribute name"); + fail_unless(data->msg->elements[0].num_values == ((_i + 5) % 10), + "Wrong number of attribute values, expected [%d] got [%d]", + ((_i + 5) % 10), data->msg->elements[0].num_values); } - fail_if(ret != EOK, "Could not check group %d", data->gid); - talloc_free(test_ctx); } END_TEST diff --git a/src/tools/sss_groupshow.c b/src/tools/sss_groupshow.c index 95af22a4..20f1a765 100644 --- a/src/tools/sss_groupshow.c +++ b/src/tools/sss_groupshow.c @@ -274,7 +274,6 @@ struct group_show_state { bool recursive; }; -static void group_show_root_done(struct tevent_req *subreq); static void group_show_recurse_done(struct tevent_req *subreq); static void group_show_trim_done(struct tevent_req *subreq); @@ -305,55 +304,32 @@ struct tevent_req *group_show_send(TALLOC_CTX *mem_ctx, bool recursive, const char *name) { - struct group_show_state *search_state = NULL; + struct group_show_state *state = NULL; struct tevent_req *subreq = NULL; struct tevent_req *req = NULL; static const char *attrs[] = GROUP_SHOW_ATTRS; + struct ldb_message *msg = NULL; + const char **group_members = NULL; + int nmembers = 0; + int ret; + int i; - req = tevent_req_create(mem_ctx, &search_state, struct group_show_state); + req = tevent_req_create(mem_ctx, &state, struct group_show_state); if (req == NULL) { return NULL; } - search_state->ev = ev; - search_state->sysdb = sysdb; - search_state->handle = handle; - search_state->domain = domain; - search_state->recursive = recursive; + state->ev = ev; + state->sysdb = sysdb; + state->handle = handle; + state->domain = domain; + state->recursive = recursive; /* First, search for the root group */ - subreq = sysdb_search_group_by_name_send(search_state, - search_state->ev, - search_state->sysdb, - search_state->handle, - search_state->domain, - name, attrs); - if (!subreq) { - talloc_zfree(req); - return NULL; - } - tevent_req_set_callback(subreq, group_show_root_done, req); - - return req; -} - -static void group_show_root_done(struct tevent_req *subreq) -{ - struct tevent_req *req = tevent_req_callback_data(subreq, - struct tevent_req); - struct group_show_state *state = tevent_req_data(req, - struct group_show_state); - int ret; - int i; - struct ldb_message *msg = NULL; - const char **group_members = NULL; - int nmembers = 0; - - ret = sysdb_search_group_recv(subreq, state, &msg); - talloc_zfree(subreq); + ret = sysdb_search_group_by_name(state, state->sysdb, + state->domain, name, attrs, &msg); if (ret) { DEBUG(2, ("Search failed: %s (%d)\n", strerror(ret), ret)); - tevent_req_error(req, ret); - return; + goto done; } ret = process_group(state, @@ -363,8 +339,7 @@ static void group_show_root_done(struct tevent_req *subreq) if (ret != EOK) { DEBUG(2, ("Group processing failed: %s (%d)\n", strerror(ret), ret)); - tevent_req_error(req, ret); - return; + goto done; } if (state->recursive == false) { @@ -376,22 +351,22 @@ static void group_show_root_done(struct tevent_req *subreq) state->root->group_members[i] = talloc_zero(state->root, struct group_info); if (!state->root->group_members) { - tevent_req_error(req, ENOMEM); - return; + ret = ENOMEM; + goto done; } state->root->group_members[i]->name = talloc_strdup(state->root, group_members[i]); if (!state->root->group_members[i]->name) { - tevent_req_error(req, ENOMEM); - return; + ret = ENOMEM; + goto done; } } state->root->group_members[nmembers] = NULL; } if (state->root->memberofs == NULL) { - tevent_req_done(req); - return; + ret = EOK; + goto done; } /* if not recursive, only show the direct parent */ @@ -400,16 +375,17 @@ static void group_show_root_done(struct tevent_req *subreq) state->domain, state->root->name, state->root->memberofs); if (!subreq) { - tevent_req_error(req, ENOMEM); - return; + ret = ENOMEM; + goto done; } tevent_req_set_callback(subreq, group_show_trim_done, req); - return; + + return req; } if (group_members == NULL) { - tevent_req_done(req); - return; + ret = EOK; + goto done; } subreq = group_show_recurse_send(state->root, state, @@ -417,10 +393,21 @@ static void group_show_root_done(struct tevent_req *subreq) group_members, nmembers); if (!subreq) { - tevent_req_error(req, ret); - return; + ret = ENOMEM; + goto done; } tevent_req_set_callback(subreq, group_show_recurse_done, req); + + return req; + +done: + if (ret) { + tevent_req_error(req, ret); + } else { + tevent_req_done(req); + } + tevent_req_post(req, ev); + return req; } static void group_show_trim_done(struct tevent_req *subreq) @@ -644,9 +631,8 @@ struct group_show_recurse { static int group_show_recurse_search(struct tevent_req *, struct group_show_recurse *); -static void group_show_recurse_next(struct tevent_req *); static void group_show_recurse_level_done(struct tevent_req *); -static void group_show_recurse_cont(struct tevent_req *); +static int group_show_recurse_cont(struct tevent_req *); struct tevent_req *group_show_recurse_send(TALLOC_CTX *mem_ctx, struct group_show_state *state, @@ -656,6 +642,7 @@ struct tevent_req *group_show_recurse_send(TALLOC_CTX *mem_ctx, { struct tevent_req *req = NULL; struct group_show_recurse *recurse_state = NULL; + int ret; req = tevent_req_create(mem_ctx, &recurse_state, struct group_show_recurse); if (req == NULL) { @@ -675,9 +662,14 @@ struct tevent_req *group_show_recurse_send(TALLOC_CTX *mem_ctx, return NULL; } - if (group_show_recurse_search(req, recurse_state) != EOK) { - talloc_zfree(req); - return NULL; + ret = group_show_recurse_search(req, recurse_state); + if (ret == ENOENT) { + tevent_req_done(req); + tevent_req_post(req, state->ev); + } + if (ret) { + tevent_req_error(req, ret); + tevent_req_post(req, state->ev); } return req; @@ -686,8 +678,13 @@ struct tevent_req *group_show_recurse_send(TALLOC_CTX *mem_ctx, static int group_show_recurse_search(struct tevent_req *req, struct group_show_recurse *recurse_state) { + struct tevent_req *subreq; static const char *attrs[] = GROUP_SHOW_ATTRS; - struct tevent_req *subreq = NULL; + struct ldb_message *msg; + const char **group_members = NULL; + int nmembers = 0; + int ret; + /* Skip circular groups */ if (strcmp(recurse_state->names[recurse_state->current], @@ -696,39 +693,14 @@ static int group_show_recurse_search(struct tevent_req *req, return EOK; } - subreq = sysdb_search_group_by_name_send(recurse_state->state, - recurse_state->state->ev, - recurse_state->state->sysdb, - recurse_state->state->handle, - recurse_state->state->domain, - recurse_state->names[recurse_state->current], - attrs); - if (!subreq) { - return ENOMEM; - } - tevent_req_set_callback(subreq, group_show_recurse_next, req); - - return EOK; -} - -static void group_show_recurse_next(struct tevent_req *subreq) -{ - struct tevent_req *req = tevent_req_callback_data(subreq, - struct tevent_req); - struct group_show_recurse *recurse_state = tevent_req_data(req, - struct group_show_recurse); - const char **group_members = NULL; - int nmembers = 0; - struct ldb_message *msg = NULL; - int ret; - struct tevent_req *recurse_req = NULL; - - ret = sysdb_search_group_recv(subreq, recurse_state, &msg); - talloc_zfree(subreq); + ret = sysdb_search_group_by_name(recurse_state->state, + recurse_state->state->sysdb, + recurse_state->state->domain, + recurse_state->names[recurse_state->current], + attrs, &msg); if (ret) { DEBUG(2, ("Search failed: %s (%d)\n", strerror(ret), ret)); - tevent_req_error(req, EIO); - return; + return EIO; } ret = process_group(recurse_state->state->root, @@ -742,41 +714,39 @@ static void group_show_recurse_next(struct tevent_req *subreq) if (ret != EOK) { DEBUG(2, ("Group processing failed: %s (%d)\n", strerror(ret), ret)); - tevent_req_error(req, ret); - return; + return ret; } /* descend to another level */ if (nmembers > 0) { - recurse_req = group_show_recurse_send(recurse_state, - recurse_state->state, - recurse_state->groups[recurse_state->current], - group_members, nmembers); - if (!recurse_req) { - tevent_req_error(req, ENOMEM); - return; + subreq = group_show_recurse_send(recurse_state, + recurse_state->state, + recurse_state->groups[recurse_state->current], + group_members, nmembers); + if (!subreq) { + return ENOMEM; } /* to free group_members in the callback */ - group_members = talloc_move(recurse_req, &group_members); - tevent_req_set_callback(recurse_req, group_show_recurse_level_done, req); - return; + group_members = talloc_move(subreq, &group_members); + tevent_req_set_callback(subreq, group_show_recurse_level_done, req); + return EOK; } /* Move to next group in the same level */ - group_show_recurse_cont(req); + return group_show_recurse_cont(req); } -static void group_show_recurse_level_done(struct tevent_req *recurse_req) +static void group_show_recurse_level_done(struct tevent_req *subreq) { int ret; - struct tevent_req *req = tevent_req_callback_data(recurse_req, + struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req); - struct group_show_recurse *recurse_state = tevent_req_data(recurse_req, + struct group_show_recurse *recurse_state = tevent_req_data(subreq, struct group_show_recurse); - ret = group_show_recurse_recv(recurse_state->state->root, recurse_req, + ret = group_show_recurse_recv(recurse_state->state->root, subreq, &recurse_state->parent->group_members); - talloc_zfree(recurse_req); + talloc_zfree(subreq); if (ret) { DEBUG(2, ("Recursive search failed: %s (%d)\n", strerror(ret), ret)); tevent_req_error(req, EIO); @@ -784,28 +754,30 @@ static void group_show_recurse_level_done(struct tevent_req *recurse_req) } /* Move to next group on the upper level */ - group_show_recurse_cont(req); + ret = group_show_recurse_cont(req); + if (ret == ENOENT) { + tevent_req_done(req); + return; + } + if (ret) { + tevent_req_error(req, ret); + return; + } } -static void group_show_recurse_cont(struct tevent_req *req) +static int group_show_recurse_cont(struct tevent_req *req) { - struct group_show_recurse *recurse_state = tevent_req_data(req, - struct group_show_recurse); - int ret; + struct group_show_recurse *state = tevent_req_data(req, + struct group_show_recurse); - recurse_state->current++; - if (recurse_state->names[recurse_state->current] == NULL) { - recurse_state->groups[recurse_state->current] = NULL; /* Sentinel */ - tevent_req_done(req); - return; + state->current++; + if (state->names[state->current] == NULL) { + state->groups[state->current] = NULL; /* Sentinel */ + return ENOENT; } /* examine next group on the same level */ - ret = group_show_recurse_search(req, recurse_state); - if (ret != EOK) { - tevent_req_error(req, ret); - return; - } + return group_show_recurse_search(req, state); } static int group_show_recurse_recv(TALLOC_CTX *mem_ctx, |