From fb263dd17c3fc00d9e5e8f366d60adbf5e76edf0 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Wed, 8 Oct 2008 14:01:06 -0400 Subject: Add getpwuid support --- server/nss/nsssrv_cmd.c | 40 ++++++++++++++++++++++++++++++----- server/nss/nsssrv_ldb.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++--- server/nss/nsssrv_ldb.h | 6 ++++++ 3 files changed, 93 insertions(+), 8 deletions(-) diff --git a/server/nss/nsssrv_cmd.c b/server/nss/nsssrv_cmd.c index 8e15bcf5..5a490a6b 100644 --- a/server/nss/nsssrv_cmd.c +++ b/server/nss/nsssrv_cmd.c @@ -88,8 +88,8 @@ static int fill_pwent(struct nss_packet *packet, gid = ldb_msg_find_attr_as_uint64(msg, NSS_PW_GIDNUM, 0); if (!name || !fullname || !homedir || !shell || !uid || !gid) { - DEBUG(1, ("Incomplede user object for %s! Skipping\n", - name?name:"")); + DEBUG(1, ("Incomplede user object for %s[%llu]! Skipping\n", + name?name:"", (unsigned long long int)uid)); continue; } @@ -131,8 +131,8 @@ done: return RES_SUCCESS; } -static int nss_cmd_getpwnam_callback(void *ptr, int status, - struct ldb_result *res) +static int nss_cmd_getpw_callback(void *ptr, int status, + struct ldb_result *res) { struct nss_cmd_ctx *nctx = talloc_get_type(ptr, struct nss_cmd_ctx); struct cli_ctx *cctx = nctx->cctx; @@ -203,7 +203,36 @@ static int nss_cmd_getpwnam(struct cli_ctx *cctx) nctx->cctx = cctx; ret = nss_ldb_getpwnam(nctx, cctx->ev, cctx->ldb, name, - nss_cmd_getpwnam_callback, nctx); + nss_cmd_getpw_callback, nctx); + + return ret; +} + +static int nss_cmd_getpwuid(struct cli_ctx *cctx) +{ + struct nss_cmd_ctx *nctx; + uint8_t *body; + size_t blen; + int ret; + uint64_t uid; + + /* get user name to query */ + nss_get_body(cctx->creq->in, &body, &blen); + + if (blen != sizeof(uint64_t)) { + return RES_INVALID_DATA; + } + + uid = *((uint64_t *)body); + + nctx = talloc(cctx, struct nss_cmd_ctx); + if (!nctx) { + return RES_NOMEM; + } + nctx->cctx = cctx; + + ret = nss_ldb_getpwuid(nctx, cctx->ev, cctx->ldb, uid, + nss_cmd_getpw_callback, nctx); return ret; } @@ -211,6 +240,7 @@ static int nss_cmd_getpwnam(struct cli_ctx *cctx) struct nss_cmd_table nss_cmds[] = { {SSS_NSS_GET_VERSION, nss_cmd_get_version}, {SSS_NSS_GETPWNAM, nss_cmd_getpwnam}, + {SSS_NSS_GETPWUID, nss_cmd_getpwuid}, {SSS_NSS_NULL, NULL} }; diff --git a/server/nss/nsssrv_ldb.c b/server/nss/nsssrv_ldb.c index 9ceb8f10..272ee366 100644 --- a/server/nss/nsssrv_ldb.c +++ b/server/nss/nsssrv_ldb.c @@ -43,8 +43,8 @@ static int request_done(struct nss_ldb_search_ctx *sctx) return sctx->callback(sctx->ptr, LDB_SUCCESS, sctx->res); } -static int getpwnam_callback(struct ldb_request *req, - struct ldb_reply *ares) +static int getpw_callback(struct ldb_request *req, + struct ldb_reply *ares) { struct nss_ldb_search_ctx *sctx; struct ldb_result *res; @@ -136,7 +136,56 @@ int nss_ldb_getpwnam(TALLOC_CTX *mem_ctx, ldb_dn_new(sctx, ldb, NSS_USER_BASE), LDB_SCOPE_SUBTREE, expression, attrs, NULL, - sctx, getpwnam_callback, + sctx, getpw_callback, + NULL); + if (ret != LDB_SUCCESS) { + return RES_ERROR; + } + + ret = ldb_request(ldb, req); + if (ret != LDB_SUCCESS) { + return RES_ERROR; + } + + return RES_SUCCESS; +} + +int nss_ldb_getpwuid(TALLOC_CTX *mem_ctx, + struct event_context *ev, + struct ldb_context *ldb, + uint64_t uid, + nss_ldb_callback_t fn, void *ptr) +{ + struct nss_ldb_search_ctx *sctx; + struct ldb_request *req; + static const char *attrs[] = NSS_PW_ATTRS; + unsigned long long int filter_uid = uid; + char *expression; + int ret; + + sctx = talloc(mem_ctx, struct nss_ldb_search_ctx); + if (!sctx) { + return RES_NOMEM; + } + sctx->callback = fn; + sctx->ptr = ptr; + sctx->res = talloc_zero(sctx, struct ldb_result); + if (!sctx->res) { + talloc_free(sctx); + return RES_NOMEM; + } + + expression = talloc_asprintf(sctx, NSS_PWUID_FILTER, filter_uid); + if (!expression) { + talloc_free(sctx); + return RES_NOMEM; + } + + ret = ldb_build_search_req(&req, ldb, sctx, + ldb_dn_new(sctx, ldb, NSS_USER_BASE), + LDB_SCOPE_SUBTREE, + expression, attrs, NULL, + sctx, getpw_callback, NULL); if (ret != LDB_SUCCESS) { return RES_ERROR; diff --git a/server/nss/nsssrv_ldb.h b/server/nss/nsssrv_ldb.h index a50216d0..924c41d7 100644 --- a/server/nss/nsssrv_ldb.h +++ b/server/nss/nsssrv_ldb.h @@ -17,3 +17,9 @@ int nss_ldb_getpwnam(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, const char *name, nss_ldb_callback_t fn, void *ptr); + +int nss_ldb_getpwuid(TALLOC_CTX *mem_ctx, + struct event_context *ev, + struct ldb_context *ldb, + uint64_t uid, + nss_ldb_callback_t fn, void *ptr); -- cgit