diff options
-rw-r--r-- | src/db/sysdb.h | 2 | ||||
-rw-r--r-- | src/db/sysdb_search.c | 63 | ||||
-rw-r--r-- | src/responder/nss/nsssrv_cmd.c | 357 | ||||
-rw-r--r-- | src/tests/sysdb-tests.c | 79 | ||||
-rw-r--r-- | src/tools/sss_groupdel.c | 2 | ||||
-rw-r--r-- | src/tools/sss_groupmod.c | 2 | ||||
-rw-r--r-- | src/tools/sss_sync_ops.c | 81 | ||||
-rw-r--r-- | src/tools/sss_sync_ops.h | 2 | ||||
-rw-r--r-- | src/tools/sss_useradd.c | 57 | ||||
-rw-r--r-- | src/tools/tools_util.c | 2 |
10 files changed, 253 insertions, 394 deletions
diff --git a/src/db/sysdb.h b/src/db/sysdb.h index cb5cbba11..65b57bdfc 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -271,7 +271,7 @@ int sysdb_getgrnam(TALLOC_CTX *mem_ctx, struct sysdb_ctx *ctx, struct sss_domain_info *domain, const char *name, - sysdb_callback_t fn, void *ptr); + struct ldb_result **res); int sysdb_getgrgid(TALLOC_CTX *mem_ctx, struct sysdb_ctx *ctx, diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c index 3d375214d..144f6da51 100644 --- a/src/db/sysdb_search.c +++ b/src/db/sysdb_search.c @@ -363,6 +363,20 @@ static int mpg_convert(struct ldb_message *msg) return EOK; } +static int mpg_res_convert(struct ldb_result *res) +{ + int ret; + int i; + + for (i = 0; i < res->count; i++) { + ret = mpg_convert(res->msgs[i]); + if (ret) { + return ret; + } + } + return EOK; +} + static void grp_search(struct tevent_req *treq) { struct sysdb_search_ctx *sctx; @@ -408,40 +422,55 @@ int sysdb_getgrnam(TALLOC_CTX *mem_ctx, struct sysdb_ctx *ctx, struct sss_domain_info *domain, const char *name, - sysdb_callback_t fn, void *ptr) + struct ldb_result **_res) { - struct sysdb_search_ctx *sctx; - struct tevent_req *req; + TALLOC_CTX *tmpctx; + static const char *attrs[] = SYSDB_GRSRC_ATTRS; + const char *fmt_filter; + struct ldb_dn *base_dn; + struct ldb_result *res; + int ret; if (!domain) { return EINVAL; } - sctx = init_src_ctx(mem_ctx, domain, ctx, fn, ptr); - if (!sctx) { + tmpctx = talloc_new(mem_ctx); + if (!tmpctx) { return ENOMEM; } if (ctx->mpg) { - sctx->gen_conv_mpg_users = true; - sctx->expression = talloc_asprintf(sctx, SYSDB_GRNAM_MPG_FILTER, name); + fmt_filter = SYSDB_GRNAM_MPG_FILTER; + base_dn = ldb_dn_new_fmt(tmpctx, ctx->ldb, + SYSDB_DOM_BASE, domain->name); } else { - sctx->expression = talloc_asprintf(sctx, SYSDB_GRNAM_FILTER, name); + fmt_filter = SYSDB_GRNAM_FILTER; + base_dn = ldb_dn_new_fmt(tmpctx, ctx->ldb, + SYSDB_TMPL_GROUP_BASE, domain->name); } - if (!sctx->expression) { - talloc_free(sctx); - return ENOMEM; + if (!base_dn) { + ret = ENOMEM; + goto done; } - req = sysdb_operation_send(mem_ctx, ctx->ev, ctx); - if (!req) { - talloc_free(sctx); - return ENOMEM; + ret = ldb_search(ctx->ldb, tmpctx, &res, base_dn, + LDB_SCOPE_SUBTREE, attrs, fmt_filter, name); + if (ret) { + ret = sysdb_error_to_errno(ret); + goto done; } - tevent_req_set_callback(req, grp_search, sctx); + ret = mpg_res_convert(res); + if (ret) { + goto done; + } - return EOK; + *_res = talloc_steal(mem_ctx, res); + +done: + talloc_zfree(tmpctx); + return ret; } int sysdb_getgrgid(TALLOC_CTX *mem_ctx, diff --git a/src/responder/nss/nsssrv_cmd.c b/src/responder/nss/nsssrv_cmd.c index c74969d10..18f8d3995 100644 --- a/src/responder/nss/nsssrv_cmd.c +++ b/src/responder/nss/nsssrv_cmd.c @@ -1582,152 +1582,146 @@ done: return EOK; } +static int nss_cmd_getgr_send_reply(struct nss_dom_ctx *dctx, bool filter) +{ + struct nss_cmd_ctx *cmdctx = dctx->cmdctx; + struct cli_ctx *cctx = cmdctx->cctx; + struct nss_ctx *nctx; + int ret; + int i; + + nctx = talloc_get_type(cctx->rctx->pvt_ctx, struct nss_ctx); + + ret = sss_packet_new(cctx->creq, 0, + sss_packet_get_cmd(cctx->creq->in), + &cctx->creq->out); + if (ret != EOK) { + return EFAULT; + } + i = dctx->res->count; + ret = fill_grent(cctx->creq->out, + dctx->domain, + nctx, filter, + dctx->res->msgs, 1, &i); + if (ret) { + return ret; + } + sss_packet_set_error(cctx->creq->out, EOK); + sss_cmd_done(cctx, cmdctx); + return EOK; +} + static void nss_cmd_getgrnam_dp_callback(uint16_t err_maj, uint32_t err_min, const char *err_msg, void *ptr); -static void nss_cmd_getgrnam_callback(void *ptr, int status, - struct ldb_result *res) +/* search for a group. + * Returns: + * ENOENT, if group is definitely not found + * EAGAIN, if group is beeing fetched from backend via async operations + * EOK, if found + * anything else on a fatal error + */ + +static int nss_cmd_getgrnam_search(struct nss_dom_ctx *dctx) { - struct nss_dom_ctx *dctx = talloc_get_type(ptr, struct nss_dom_ctx); struct nss_cmd_ctx *cmdctx = dctx->cmdctx; + struct sss_domain_info *dom = dctx->domain; struct cli_ctx *cctx = cmdctx->cctx; - struct sss_domain_info *dom; + const char *name = cmdctx->name; struct sysdb_ctx *sysdb; struct nss_ctx *nctx; - uint8_t *body; - size_t blen; - bool neghit = false; - int ncret; - int i, ret; + int ret; nctx = talloc_get_type(cctx->rctx->pvt_ctx, struct nss_ctx); - if (status != LDB_SUCCESS) { - ret = nss_cmd_send_error(cmdctx, status); - if (ret != EOK) { - NSS_CMD_FATAL_ERROR(cctx); + while (dom) { + /* if it is a domainless search, skip domains that require fully + * qualified names instead */ + while (dom && cmdctx->check_next && dom->fqnames) { + dom = dom->next; } - sss_cmd_done(cctx, cmdctx); - return; - } - if (dctx->check_provider) { - ret = check_cache(dctx, nctx, res, - SSS_DP_GROUP, cmdctx->name, 0, - nss_cmd_getgrnam_dp_callback); - if (ret != EOK) { - /* Anything but EOK means we should reenter the mainloop - * because we may be refreshing the cache - */ - return; + if (!dom) break; + + if (dom != dctx->domain) { + /* make sure we reset the check_provider flag when we check + * a new domain */ + dctx->check_provider = NEED_CHECK_PROVIDER(dom->provider); } - } - switch (res->count) { - case 0: - if (cmdctx->check_next) { + /* make sure to update the dctx if we changed domain */ + dctx->domain = dom; - ret = EOK; + /* verify this group has not yet been negatively cached, + * or has been permanently filtered */ + ret = nss_ncache_check_group(nctx->ncache, nctx->neg_timeout, + dom->name, name); - /* skip domains that require FQnames or have negative caches */ - for (dom = dctx->domain->next; dom; dom = dom->next) { + /* if neg cached, return we didn't find it */ + if (ret == EEXIST) { + DEBUG(2, ("Group [%s] does not exist! (negative cache)\n", name)); + return ENOENT; + } - if (dom->fqnames) continue; + DEBUG(4, ("Requesting info for [%s@%s]\n", name, dom->name)); - ncret = nss_ncache_check_group(nctx->ncache, - nctx->neg_timeout, - dom->name, cmdctx->name); - if (ncret == ENOENT) break; + ret = sysdb_get_ctx_from_list(cctx->rctx->db_list, dom, &sysdb); + if (ret != EOK) { + DEBUG(0, ("Fatal: Sysdb CTX not found for this domain!\n")); + return EIO; + } - neghit = true; - } - /* reset neghit if we still have a domain to check */ - if (dom) neghit = false; + ret = sysdb_getgrnam(cmdctx, sysdb, dom, name, &dctx->res); + if (ret != EOK) { + DEBUG(1, ("Failed to make request to our cache!\n")); + return EIO; + } - if (neghit) { - DEBUG(2, ("Group [%s] does not exist! (negative cache)\n", - cmdctx->name)); - ret = ENOENT; - } - if (dom == NULL) { - DEBUG(2, ("No matching domain found for [%s], fail!\n", - cmdctx->name)); - ret = ENOENT; - } + if (dctx->res->count > 1) { + DEBUG(0, ("getgrnam call returned more than one result !?!\n")); + return ENOENT; + } - if (ret == EOK) { - dctx->domain = dom; - dctx->check_provider = NEED_CHECK_PROVIDER(dctx->domain->provider); - if (dctx->res) talloc_free(res); - dctx->res = NULL; + if (dctx->res->count == 0 && !dctx->check_provider) { + /* if a multidomain search, try with next */ + if (cmdctx->check_next) { + dom = dom->next; + continue; + } - DEBUG(4, ("Requesting info for [%s@%s]\n", - cmdctx->name, dctx->domain->name)); + DEBUG(2, ("No results for getgrnam call\n")); - ret = sysdb_get_ctx_from_list(cctx->rctx->db_list, - dctx->domain, &sysdb); - if (ret != EOK) { - DEBUG(0, ("Fatal: Sysdb CTX not found for this domain!\n")); - NSS_CMD_FATAL_ERROR(cctx); - } - ret = sysdb_getgrnam(cmdctx, sysdb, - dctx->domain, cmdctx->name, - nss_cmd_getgrnam_callback, dctx); - if (ret != EOK) { - DEBUG(1, ("Failed to make request to our cache!\n")); - } + /* set negative cache only if not result of cache check */ + ret = nss_ncache_set_group(nctx->ncache, false, dom->name, name); + if (ret != EOK) { + return ret; } - /* we made another call, end here */ - if (ret == EOK) return; + return ENOENT; } - - DEBUG(2, ("No results for getgrnam call\n")); - - /* set negative cache only if not result of cache check */ - if (!neghit) { - ret = nss_ncache_set_group(nctx->ncache, false, - dctx->domain->name, cmdctx->name); + /* if this is a caching provider (or if we haven't checked the cache + * yet) then verify that the cache is uptodate */ + if (dctx->check_provider) { + ret = check_cache(dctx, nctx, dctx->res, + SSS_DP_GROUP, name, 0, + nss_cmd_getgrnam_dp_callback); if (ret != EOK) { - NSS_CMD_FATAL_ERROR(cctx); + /* Anything but EOK means we should reenter the mainloop + * because we may be refreshing the cache + */ + return ret; } } - ret = sss_packet_new(cctx->creq, 2*sizeof(uint32_t), - sss_packet_get_cmd(cctx->creq->in), - &cctx->creq->out); - if (ret != EOK) { - NSS_CMD_FATAL_ERROR(cctx); - } - sss_packet_get_body(cctx->creq->out, &body, &blen); - ((uint32_t *)body)[0] = 0; /* 0 results */ - ((uint32_t *)body)[1] = 0; /* reserved */ - break; - - default: - - DEBUG(6, ("Returning info for group [%s]\n", cmdctx->name)); + /* One result found */ + DEBUG(6, ("Returning info for group [%s@%s]\n", name, dom->name)); - /* create response packet */ - ret = sss_packet_new(cctx->creq, 0, - sss_packet_get_cmd(cctx->creq->in), - &cctx->creq->out); - if (ret != EOK) { - NSS_CMD_FATAL_ERROR(cctx); - } - i = res->count; - ret = fill_grent(cctx->creq->out, - dctx->domain, - nctx, false, - res->msgs, 1, &i); - if (ret == ENOENT) { - ret = fill_empty(cctx->creq->out); - } - sss_packet_set_error(cctx->creq->out, ret); + return EOK; } - sss_cmd_done(cctx, cmdctx); + DEBUG(2, ("No matching domain found for [%s], fail!\n", name)); + return ENOENT; } static void nss_cmd_getgrnam_dp_callback(uint16_t err_maj, uint32_t err_min, @@ -1736,7 +1730,6 @@ static void nss_cmd_getgrnam_dp_callback(uint16_t err_maj, uint32_t err_min, struct nss_dom_ctx *dctx = talloc_get_type(ptr, struct nss_dom_ctx); struct nss_cmd_ctx *cmdctx = dctx->cmdctx; struct cli_ctx *cctx = cmdctx->cctx; - struct sysdb_ctx *sysdb; int ret; if (err_maj) { @@ -1745,39 +1738,33 @@ static void nss_cmd_getgrnam_dp_callback(uint16_t err_maj, uint32_t err_min, "Will try to return what we have in cache\n", (unsigned int)err_maj, (unsigned int)err_min, err_msg)); - if (!dctx->res) { - /* return 0 results */ - dctx->res = talloc_zero(dctx, struct ldb_result); - if (!dctx->res) { - ret = ENOMEM; - goto done; - } + if (dctx->res && dctx->res->count == 1) { + ret = nss_cmd_getgr_send_reply(dctx, false); + goto done; } - nss_cmd_getgrnam_callback(dctx, LDB_SUCCESS, dctx->res); - return; + /* no previous results, just loop to next domain if possible */ + if (dctx->domain->next && cmdctx->check_next) { + dctx->domain = dctx->domain->next; + dctx->check_provider = NEED_CHECK_PROVIDER(dctx->domain->provider); + } else { + /* nothing vailable */ + ret = ENOENT; + goto done; + } } - ret = sysdb_get_ctx_from_list(cctx->rctx->db_list, - dctx->domain, &sysdb); - if (ret != EOK) { - DEBUG(0, ("Fatal: Sysdb CTX not found for this domain!\n")); - NSS_CMD_FATAL_ERROR(cctx); + /* ok the backend returned, search to see if we have updated results */ + ret = nss_cmd_getgrnam_search(dctx); + if (ret == EOK) { + /* we have results to return */ + ret = nss_cmd_getgr_send_reply(dctx, false); } - ret = sysdb_getgrnam(cmdctx, sysdb, - dctx->domain, cmdctx->name, - nss_cmd_getgrnam_callback, dctx); done: - if (ret != EOK) { - DEBUG(1, ("Failed to make request to our cache! (%d [%s])\n", - ret, strerror(ret))); - - ret = nss_cmd_send_error(cmdctx, ret); - if (ret != EOK) { - NSS_CMD_FATAL_ERROR(cctx); - } - sss_cmd_done(cctx, cmdctx); + ret = nss_cmd_done(cmdctx, ret); + if (ret) { + NSS_CMD_FATAL_ERROR(cctx); } } @@ -1785,18 +1772,11 @@ static int nss_cmd_getgrnam(struct cli_ctx *cctx) { struct nss_cmd_ctx *cmdctx; struct nss_dom_ctx *dctx; - struct sss_domain_info *dom; - struct sysdb_ctx *sysdb; - struct nss_ctx *nctx; const char *rawname; char *domname; uint8_t *body; size_t blen; int ret; - int ncret; - bool neghit = false; - - nctx = talloc_get_type(cctx->rctx->pvt_ctx, struct nss_ctx); cmdctx = talloc_zero(cctx, struct nss_cmd_ctx); if (!cmdctx) { @@ -1839,92 +1819,23 @@ static int nss_cmd_getgrnam(struct cli_ctx *cctx) ret = ENOENT; goto done; } - - /* verify this user has not yet been negatively cached, - * or has been permanently filtered */ - ncret = nss_ncache_check_group(nctx->ncache, nctx->neg_timeout, - dctx->domain->name, cmdctx->name); - if (ncret == EEXIST) { - neghit = true; - } - } - else { - /* skip domains that require FQnames or have negative caches */ - for (dom = cctx->rctx->domains; dom; dom = dom->next) { - - if (dom->fqnames) continue; - - /* verify this user has not yet been negatively cached, - * or has been permanently filtered */ - ncret = nss_ncache_check_group(nctx->ncache, nctx->neg_timeout, - dom->name, cmdctx->name); - if (ncret == ENOENT) break; - - neghit = true; - } - /* reset neghit if we still have a domain to check */ - if (dom) neghit = false; - - dctx->domain = dom; - } - if (neghit) { - DEBUG(2, ("Group [%s] does not exist! (negative cache)\n", rawname)); - ret = ENOENT; - goto done; - } - if (dctx->domain == NULL) { - DEBUG(2, ("No matching domain found for [%s], fail!\n", rawname)); - ret = ENOENT; - goto done; - } - - dctx->check_provider = NEED_CHECK_PROVIDER(dctx->domain->provider); - - if (!domname) { + } else { /* this is a multidomain search */ + dctx->domain = cctx->rctx->domains; cmdctx->check_next = true; } - DEBUG(4, ("Requesting info for [%s@%s]\n", - cmdctx->name, dctx->domain->name)); + dctx->check_provider = NEED_CHECK_PROVIDER(dctx->domain->provider); - ret = sysdb_get_ctx_from_list(cctx->rctx->db_list, - dctx->domain, &sysdb); - if (ret != EOK) { - DEBUG(0, ("Fatal: Sysdb CTX not found for this domain!\n")); - ret = EFAULT; - goto done; - } - ret = sysdb_getgrnam(cmdctx, sysdb, - dctx->domain, cmdctx->name, - nss_cmd_getgrnam_callback, dctx); - if (ret != EOK) { - DEBUG(1, ("Failed to make request to our cache!\n")); + /* ok, find it ! */ + ret = nss_cmd_getgrnam_search(dctx); + if (ret == EOK) { + /* we have results to return */ + ret = nss_cmd_getgr_send_reply(dctx, false); } done: - if (ret != EOK) { - if (ret == ENOENT) { - /* we do not have any entry to return */ - ret = sss_packet_new(cctx->creq, 2*sizeof(uint32_t), - sss_packet_get_cmd(cctx->creq->in), - &cctx->creq->out); - if (ret == EOK) { - sss_packet_get_body(cctx->creq->out, &body, &blen); - ((uint32_t *)body)[0] = 0; /* 0 results */ - ((uint32_t *)body)[1] = 0; /* reserved */ - } - } - if (ret != EOK) { - ret = nss_cmd_send_error(cmdctx, ret); - } - if (ret == EOK) { - sss_cmd_done(cctx, cmdctx); - } - return ret; - } - - return EOK; + return nss_cmd_done(cmdctx, ret); } static void nss_cmd_getgrgid_dp_callback(uint16_t err_maj, uint32_t err_min, diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c index 6633136d2..d700f0b50 100644 --- a/src/tests/sysdb-tests.c +++ b/src/tests/sysdb-tests.c @@ -306,31 +306,6 @@ static int test_remove_group_by_gid(struct test_data *data) return ret; } -static void test_getgrent(void *pvt, int error, struct ldb_result *res) -{ - struct test_data *data = talloc_get_type(pvt, struct test_data); - data->finished = true; - - if (error != EOK) { - data->error = error; - return; - } - - switch (res->count) { - case 0: - data->error = ENOENT; - break; - - case 1: - data->gid = ldb_msg_find_attr_as_uint(res->msgs[0], SYSDB_GIDNUM, 0); - break; - - default: - data->error = EFAULT; - break; - } -} - static void test_getgrgid(void *pvt, int error, struct ldb_result *res) { struct test_data *data = talloc_get_type(pvt, struct test_data); @@ -845,8 +820,9 @@ END_TEST START_TEST (test_sysdb_getgrnam) { struct sysdb_test_ctx *test_ctx; - struct test_data *data; - struct test_data *data_uc; + struct ldb_result *res; + const char *groupname; + gid_t gid; int ret; /* Setup */ @@ -856,46 +832,45 @@ START_TEST (test_sysdb_getgrnam) return; } - data = talloc_zero(test_ctx, struct test_data); - data->ctx = test_ctx; - data->groupname = talloc_asprintf(data, "testgroup%d", _i); + groupname = talloc_asprintf(test_ctx, "testgroup%d", _i); ret = sysdb_getgrnam(test_ctx, test_ctx->sysdb, - data->ctx->domain, - data->groupname, - test_getgrent, - data); - if (ret == EOK) { - ret = test_loop(data); - } - + test_ctx->domain, + groupname, &res); if (ret) { fail("sysdb_getgrnam failed for groupname %s (%d: %s)", - data->groupname, ret, strerror(ret)); + groupname, ret, strerror(ret)); + goto done; + } + + if (res->count != 1) { + fail("Invalid number of replies. Expected 1, got %d", res->count); goto done; } - fail_unless(data->gid == _i, + + gid = ldb_msg_find_attr_as_uint(res->msgs[0], SYSDB_GIDNUM, 0); + fail_unless(gid == _i, "Did not find the expected GID (found %d expected %d)", - data->gid, _i); + gid, _i); /* Search for the group with the wrong case */ - data_uc = talloc_zero(test_ctx, struct test_data); - data_uc->ctx = test_ctx; - data_uc->groupname = talloc_asprintf(data_uc, "TESTGROUP%d", _i); + groupname = talloc_asprintf(test_ctx, "TESTGROUP%d", _i); ret = sysdb_getgrnam(test_ctx, test_ctx->sysdb, - data_uc->ctx->domain, - data_uc->groupname, - test_getgrent, - data_uc); - if (ret == EOK) { - ret = test_loop(data_uc); + test_ctx->domain, + groupname, &res); + if (ret) { + fail("sysdb_getgrnam failed for groupname %s (%d: %s)", + groupname, ret, strerror(ret)); + goto done; + } + + if (res->count != 0) { + fail("The upper-case groupname search should fail."); } - fail_unless(ret == ENOENT, - "The upper-case groupname search should fail. "); done: talloc_free(test_ctx); } diff --git a/src/tools/sss_groupdel.c b/src/tools/sss_groupdel.c index 32335571d..fe7e73096 100644 --- a/src/tools/sss_groupdel.c +++ b/src/tools/sss_groupdel.c @@ -96,7 +96,7 @@ int main(int argc, const char **argv) ret = sysdb_getgrnam_sync(tctx, tctx->ev, tctx->sysdb, tctx->octx->name, tctx->local, - &tctx->octx); + tctx->octx); if (ret != EOK) { /* Error message will be printed in the switch */ goto done; diff --git a/src/tools/sss_groupmod.c b/src/tools/sss_groupmod.c index 6c784240f..14ae68684 100644 --- a/src/tools/sss_groupmod.c +++ b/src/tools/sss_groupmod.c @@ -125,7 +125,7 @@ int main(int argc, const char **argv) /* check the username to be able to give sensible error message */ ret = sysdb_getgrnam_sync(tctx, tctx->ev, tctx->sysdb, tctx->octx->name, tctx->local, - &tctx->octx); + tctx->octx); if (ret != EOK) { ERROR("Cannot find group in local domain, " "modifying groups is allowed only in local domain\n"); diff --git a/src/tools/sss_sync_ops.c b/src/tools/sss_sync_ops.c index 70203cf2f..f3721a018 100644 --- a/src/tools/sss_sync_ops.c +++ b/src/tools/sss_sync_ops.c @@ -666,75 +666,42 @@ int sysdb_getpwnam_sync(TALLOC_CTX *mem_ctx, return EOK; } -static void sss_getgrnam_done(void *ptr, int status, - struct ldb_result *lrs); - int sysdb_getgrnam_sync(TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct sysdb_ctx *sysdb, const char *name, struct sss_domain_info *domain, - struct ops_ctx **out) + struct ops_ctx *out) { + struct ldb_result *res; + const char *str; int ret; - struct sync_op_res *res = NULL; - res = talloc_zero(mem_ctx, struct sync_op_res); - if (!res) { - return ENOMEM; - } - - if (out == NULL) { - DEBUG(1, ("NULL passed for storage pointer\n")); - return EINVAL; + ret = sysdb_getgrnam(mem_ctx, sysdb, domain, name, &res); + if (ret) { + return ret; } - res->data = *out; - - ret = sysdb_getgrnam(mem_ctx, - sysdb, - domain, - name, - sss_getgrnam_done, - res); - SYNC_LOOP(res, ret); - - return ret; -} + switch (res->count) { + case 0: + DEBUG(1, ("No result for sysdb_getgrnam call\n")); + return ENOENT; -static void sss_getgrnam_done(void *ptr, int status, - struct ldb_result *lrs) -{ - struct sync_op_res *res = talloc_get_type(ptr, struct sync_op_res ); - const char *str; + case 1: + /* fill ops_ctx */ + out->gid = ldb_msg_find_attr_as_uint64(res->msgs[0], SYSDB_GIDNUM, 0); + str = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME, NULL); + out->name = talloc_strdup(out, str); + if (out->name == NULL) { + return ENOMEM; + } + break; - res->done = true; - - if (status != LDB_SUCCESS) { - res->error = status; - return; - } - - switch (lrs->count) { - case 0: - DEBUG(1, ("No result for sysdb_getgrnam call\n")); - res->error = ENOENT; - break; - - /* sysdb_getgrnam also returns members */ - default: - res->error = EOK; - /* fill ops_ctx */ - res->data->gid = ldb_msg_find_attr_as_uint64(lrs->msgs[0], - SYSDB_GIDNUM, 0); - str = ldb_msg_find_attr_as_string(lrs->msgs[0], - SYSDB_NAME, NULL); - res->data->name = talloc_strdup(res, str); - if (res->data->name == NULL) { - res->error = ENOMEM; - return; - } - break; + default: + DEBUG(1, ("More than one result for sysdb_getgrnam call\n")); + return EIO; } + + return EOK; } diff --git a/src/tools/sss_sync_ops.h b/src/tools/sss_sync_ops.h index 98fbc47f2..e5ba5beec 100644 --- a/src/tools/sss_sync_ops.h +++ b/src/tools/sss_sync_ops.h @@ -104,7 +104,7 @@ int sysdb_getgrnam_sync(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb, const char *name, struct sss_domain_info *domain, - struct ops_ctx **out); + struct ops_ctx *out); #endif /* __SSS_OPS_H__ */ diff --git a/src/tools/sss_useradd.c b/src/tools/sss_useradd.c index 0dc1993e5..620c3b2f0 100644 --- a/src/tools/sss_useradd.c +++ b/src/tools/sss_useradd.c @@ -32,39 +32,13 @@ #include "tools/tools_util.h" #include "tools/sss_sync_ops.h" -static void get_gid_callback(void *ptr, int error, struct ldb_result *res) -{ - struct tools_ctx *tctx = talloc_get_type(ptr, struct tools_ctx); - - if (error) { - tctx->error = error; - return; - } - - switch (res->count) { - case 0: - tctx->error = ENOENT; - break; - - case 1: - tctx->octx->gid = ldb_msg_find_attr_as_uint(res->msgs[0], SYSDB_GIDNUM, 0); - if (tctx->octx->gid == 0) { - tctx->error = ERANGE; - } - break; - - default: - tctx->error = EFAULT; - break; - } -} - /* Returns a gid for a given groupname. If a numerical gid * is given, returns that as integer (rationale: shadow-utils) * On error, returns -EINVAL */ static int get_gid(struct tools_ctx *tctx, const char *groupname) { + struct ldb_result *res; char *end_ptr; int ret; @@ -75,27 +49,30 @@ static int get_gid(struct tools_ctx *tctx, const char *groupname) /* Does not look like a gid - find the group name */ ret = sysdb_getgrnam(tctx->octx, tctx->sysdb, - tctx->octx->domain, groupname, - get_gid_callback, tctx); + tctx->octx->domain, groupname, &res); if (ret != EOK) { DEBUG(1, ("sysdb_getgrnam failed: %d\n", ret)); - goto done; + return ret; } - tctx->error = EOK; - tctx->octx->gid = 0; - while ((tctx->error == EOK) && (tctx->octx->gid == 0)) { - tevent_loop_once(tctx->ev); - } + switch (res->count) { + case 0: + return ENOENT; - if (tctx->error) { - DEBUG(1, ("sysdb_getgrnam failed: %d\n", ret)); - goto done; + case 1: + tctx->octx->gid = ldb_msg_find_attr_as_uint(res->msgs[0], + SYSDB_GIDNUM, 0); + if (tctx->octx->gid == 0) { + return ERANGE; + } + break; + + default: + return EFAULT; } } -done: - return ret; + return EOK; } int main(int argc, const char **argv) diff --git a/src/tools/tools_util.c b/src/tools/tools_util.c index 9f9382a60..cae6f5c11 100644 --- a/src/tools/tools_util.c +++ b/src/tools/tools_util.c @@ -215,7 +215,7 @@ int check_group_names(struct tools_ctx *tctx, tctx->sysdb, grouplist[i], tctx->local, - &groupinfo); + groupinfo); if (ret) { DEBUG(6, ("Cannot find group %s, ret: %d\n", grouplist[i], ret)); break; |