From: Samuel Cabrero Date: Mon, 6 Feb 2023 17:34:04 +0000 (+0100) Subject: winbind:varlink: Implement get group record by gid X-Git-Tag: tevent-0.17.0~739 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b841911f73555209a3b41ba829caff0997ba4045;p=thirdparty%2Fsamba.git winbind:varlink: Implement get group record by gid $> userdbctl -s org.samba.winbind group 20513 Enabled services: org.samba.winbind Group name: AFOREST+domain users Disposition: regular GID: 20513 Service: org.samba.winbind $> SYSTEMD_LOG_LEVEL=7 getent -sgroup:systemd group 20513 varlink: Setting state idle-client /run/systemd/userdb/org.samba.winbind: Sending message: {"method":"io.systemd.UserDatabase.GetGroupRecord","parameters":{"gid":20513,"service":"org.samba.winbind"}} /run/systemd/userdb/org.samba.winbind: Changing state idle-client → awaiting-reply /run/systemd/userdb/org.samba.winbind: New incoming message: {"parameters":{"incomplete":false,"record":{"gid":20513,"groupName":"AFOREST+domain users","members":["AFOREST+administrator","AFOREST+user1","AFOREST+krbtgt"],"service":"org.samba.winbind"}}} /run/systemd/userdb/org.samba.winbind: Changing state awaiting-reply → processing-reply /run/systemd/userdb/org.samba.winbind: Changing state processing-reply → idle-client varlink: Setting state idle-client /run/systemd/userdb/org.samba.winbind: Sending message: {"method":"io.systemd.UserDatabase.GetMemberships","parameters":{"groupName":"AFOREST+domain users","service":"org.samba.winbind"},"more":true} /run/systemd/userdb/org.samba.winbind: Changing state idle-client → awaiting-reply-more /run/systemd/userdb/org.samba.winbind: New incoming message: {"error":"io.systemd.UserDatabase.NoRecordFound"} /run/systemd/userdb/org.samba.winbind: Changing state awaiting-reply-more → processing-reply Got lookup error: io.systemd.UserDatabase.NoRecordFound /run/systemd/userdb/org.samba.winbind: Changing state processing-reply → idle-client AFOREST+domain users:x:20513:AFOREST+administrator,AFOREST+user1,AFOREST+krbtgt Signed-off-by: Samuel Cabrero Reviewed-by: Andreas Schneider --- diff --git a/source3/winbindd/winbindd_varlink.c b/source3/winbindd/winbindd_varlink.c index e1c089d5a6b..dcb3aae590c 100644 --- a/source3/winbindd/winbindd_varlink.c +++ b/source3/winbindd/winbindd_varlink.c @@ -289,6 +289,14 @@ static long io_systemd_getgrouprecord(VarlinkService *service, call, flags, parm_service); + } else if (parm_name == NULL && parm_gid >= 0) { + /* getgrgid */ + status = wb_vl_group_by_gid(state, + state->ev_ctx, + call, + flags, + parm_service, + parm_gid); } if (NT_STATUS_IS_ERR(status)) { diff --git a/source3/winbindd/winbindd_varlink.h b/source3/winbindd/winbindd_varlink.h index 44fcb87a068..10c6177b455 100644 --- a/source3/winbindd/winbindd_varlink.h +++ b/source3/winbindd/winbindd_varlink.h @@ -73,6 +73,12 @@ NTSTATUS wb_vl_group_enumerate(TALLOC_CTX *state, VarlinkCall *call, uint64_t flags, const char *service); +NTSTATUS wb_vl_group_by_gid(TALLOC_CTX *mem_ctx, + struct tevent_context *ev_ctx, + VarlinkCall *call, + uint64_t flags, + const char *service, + int64_t gid); bool winbind_setup_varlink(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx); diff --git a/source3/winbindd/winbindd_varlink_getgrouprecord.c b/source3/winbindd/winbindd_varlink_getgrouprecord.c index 7ad8c456100..55241b3beda 100644 --- a/source3/winbindd/winbindd_varlink_getgrouprecord.c +++ b/source3/winbindd/winbindd_varlink_getgrouprecord.c @@ -378,3 +378,125 @@ fail: TALLOC_FREE(s); return status; } + +/****************************************************************************** + * Group by gid + *****************************************************************************/ + +struct group_by_gid_state { + struct tevent_context *ev_ctx; + struct winbindd_request *fake_req; + struct winbindd_cli_state *fake_cli; + VarlinkCall *call; +}; + +static int group_by_gid_state_destructor(struct group_by_gid_state *s) +{ + if (s->call != NULL) { + s->call = varlink_call_unref(s->call); + } + + return 0; +} + +static void group_by_gid_getgrgid_done(struct tevent_req *req) +{ + struct group_by_gid_state *s = + tevent_req_callback_data(req, struct group_by_gid_state); + struct winbindd_response *response = NULL; + NTSTATUS status; + + /* winbindd_*_recv functions expect a talloc-allocated response */ + response = talloc_zero(s, struct winbindd_response); + if (response == NULL) { + DBG_ERR("No memory\n"); + varlink_call_reply_error( + s->call, + WB_VL_REPLY_ERROR_SERVICE_NOT_AVAILABLE, + NULL); + goto out; + } + + status = winbindd_getgrgid_recv(req, response); + TALLOC_FREE(req); + + if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) { + varlink_call_reply_error(s->call, + WB_VL_REPLY_ERROR_NO_RECORD_FOUND, + NULL); + goto out; + } else if (NT_STATUS_IS_ERR(status)) { + DBG_ERR("winbindd_getgrgid failed: %s\n", nt_errstr(status)); + varlink_call_reply_error( + s->call, + WB_VL_REPLY_ERROR_SERVICE_NOT_AVAILABLE, + NULL); + goto out; + } + + group_record_reply(s->call, + &response->data.gr, + response->extra_data.data, + false); +out: + TALLOC_FREE(s); +} + +NTSTATUS wb_vl_group_by_gid(TALLOC_CTX *mem_ctx, + struct tevent_context *ev_ctx, + VarlinkCall *call, + uint64_t flags, + const char *service, + int64_t gid) +{ + struct group_by_gid_state *s = NULL; + struct tevent_req *req = NULL; + NTSTATUS status; + + s = talloc_zero(mem_ctx, struct group_by_gid_state); + if (s == NULL) { + DBG_ERR("No memory\n"); + return NT_STATUS_NO_MEMORY; + } + talloc_set_destructor(s, group_by_gid_state_destructor); + + s->fake_cli = talloc_zero(s, struct winbindd_cli_state); + if (s->fake_cli == NULL) { + DBG_ERR("No memory\n"); + status = NT_STATUS_NO_MEMORY; + goto fail; + } + + s->fake_req = talloc_zero(s, struct winbindd_request); + if (s->fake_req == NULL) { + DBG_ERR("No memory\n"); + status = NT_STATUS_NO_MEMORY; + goto fail; + } + + s->ev_ctx = ev_ctx; + s->call = varlink_call_ref(call); + + status = wb_vl_fake_cli_state(call, service, s->fake_cli); + if (NT_STATUS_IS_ERR(status)) { + DBG_ERR("Failed to create fake winbindd_cli_state: %s\n", + nt_errstr(status)); + goto fail; + } + + s->fake_req->cmd = WINBINDD_GETGRGID; + s->fake_req->data.uid = gid; + + req = winbindd_getgrgid_send(s, s->ev_ctx, s->fake_cli, s->fake_req); + if (req == NULL) { + DBG_ERR("No memory\n"); + status = NT_STATUS_NO_MEMORY; + goto fail; + } + tevent_req_set_callback(req, group_by_gid_getgrgid_done, s); + + return NT_STATUS_OK; +fail: + TALLOC_FREE(s); + return status; +}