]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
winbind:varlink: Implement get group record by gid
authorSamuel Cabrero <scabrero@samba.org>
Mon, 6 Feb 2023 17:34:04 +0000 (18:34 +0100)
committerAndreas Schneider <asn@cryptomilk.org>
Thu, 20 Feb 2025 08:07:32 +0000 (08:07 +0000)
$> 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 <scabrero@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
source3/winbindd/winbindd_varlink.c
source3/winbindd/winbindd_varlink.h
source3/winbindd/winbindd_varlink_getgrouprecord.c

index e1c089d5a6be161a1c620569aeb454e09335597a..dcb3aae590c35d727ba51017c5eb02c7761581d7 100644 (file)
@@ -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)) {
index 44fcb87a0686730c4b7cde3154f0942c2217410c..10c6177b455ca33f1295e74170a9f1b0a0443908 100644 (file)
@@ -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);
 
index 7ad8c456100e6e00ffa551c7bb40f6657e505951..55241b3bedaf3ab189051d58b07e8da87b769515 100644 (file)
@@ -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;
+}