Fix bug #9196 - defer_open is triggered multiple times on the same request.
[Samba.git] / source4 / winbind / wb_cmd_getgrent.c
blob79a3aff852bbfc1a15abe0323ca27c62869a48b5
1 /*
2 Unix SMB/CIFS implementation.
4 Command backend for getgrent
6 Copyright (C) Matthieu Patou 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "libcli/composite/composite.h"
24 #include "winbind/wb_server.h"
25 #include "smbd/service_task.h"
27 struct cmd_getgrent_state {
28 struct composite_context *ctx;
29 struct wbsrv_service *service;
31 struct wbsrv_grent *grent;
32 uint32_t max_groups;
34 uint32_t num_groups;
35 struct winbindd_gr *result;
38 static void cmd_getgrent_recv_grnam(struct composite_context *ctx);
39 #if 0 /*FIXME: implement this*/
40 static void cmd_getgrent_recv_user_list(struct composite_context *ctx);
41 #endif
43 struct composite_context *wb_cmd_getgrent_send(TALLOC_CTX *mem_ctx,
44 struct wbsrv_service *service, struct wbsrv_grent *grent,
45 uint32_t max_groups)
47 struct composite_context *ctx, *result;
48 struct cmd_getgrent_state *state;
50 DEBUG(5, ("wb_cmd_getgrent_send called\n"));
52 result = composite_create(mem_ctx, service->task->event_ctx);
53 if (!result) return NULL;
55 state = talloc(mem_ctx, struct cmd_getgrent_state);
56 if (composite_nomem(state, result)) return result;
58 state->ctx = result;
59 result->private_data = state;
60 state->service = service;
61 state->grent = grent;
62 state->max_groups = max_groups;
63 state->num_groups = 0;
65 /* If there are groups left in the libnet_GroupList and we're below the
66 * maximum number of groups to get per winbind getgrent call, use
67 * getgrnam to get the winbindd_gr struct */
68 if (grent->page_index < grent->group_list->out.count) {
69 int idx = grent->page_index;
70 char *groupname = talloc_strdup(state,
71 grent->group_list->out.groups[idx].groupname);
73 grent->page_index++;
74 ctx = wb_cmd_getgrnam_send(state, service, groupname);
75 if (composite_nomem(ctx, state->ctx)) return result;
77 composite_continue(state->ctx, ctx, cmd_getgrent_recv_grnam,
78 state);
79 } else {
80 /* If there is no valid group left, call libnet_GroupList to get a new
81 * list of group. */
82 composite_error(state->ctx, NT_STATUS_NO_MORE_ENTRIES);
84 return result;
87 static void cmd_getgrent_recv_grnam(struct composite_context *ctx)
89 struct cmd_getgrent_state *state =
90 talloc_get_type(ctx->async.private_data,
91 struct cmd_getgrent_state);
92 struct winbindd_gr *gr;
94 DEBUG(5, ("cmd_getgrent_recv_grnam called\n"));
96 state->ctx->status = wb_cmd_getgrnam_recv(ctx, state, &gr);
97 if (!composite_is_ok(state->ctx)) return;
99 /*FIXME: Cheat for now and only get one group per call */
100 state->result = gr;
102 composite_done(state->ctx);
105 NTSTATUS wb_cmd_getgrent_recv(struct composite_context *ctx,
106 TALLOC_CTX *mem_ctx, struct winbindd_gr **gr,
107 uint32_t *num_groups)
109 NTSTATUS status = composite_wait(ctx);
111 DEBUG(5, ("wb_cmd_getgrent_recv called\n"));
113 if (NT_STATUS_IS_OK(status)) {
114 struct cmd_getgrent_state *state =
115 talloc_get_type(ctx->private_data,
116 struct cmd_getgrent_state);
117 *gr = talloc_steal(mem_ctx, state->result);
118 /*FIXME: Cheat and only get one group */
119 *num_groups = 1;
122 talloc_free(ctx);
123 return status;