torture: Fix copy and paste error in debug message.
[kai/samba.git] / source4 / winbind / wb_cmd_getgrent.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Command backend for getgrent
5
6    Copyright (C) Matthieu Patou 2010
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/composite/composite.h"
24 #include "winbind/wb_server.h"
25 #include "smbd/service_task.h"
26
27 struct cmd_getgrent_state {
28         struct composite_context *ctx;
29         struct wbsrv_service *service;
30
31         struct wbsrv_grent *grent;
32         uint32_t max_groups;
33
34         uint32_t num_groups;
35         struct winbindd_gr *result;
36 };
37
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
42
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)
46 {
47         struct composite_context *ctx, *result;
48         struct cmd_getgrent_state *state;
49
50         DEBUG(5, ("wb_cmd_getgrent_send called\n"));
51
52         result = composite_create(mem_ctx, service->task->event_ctx);
53         if (!result) return NULL;
54
55         state = talloc(mem_ctx, struct cmd_getgrent_state);
56         if (composite_nomem(state, result)) return result;
57
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;
64
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);
72
73                 grent->page_index++;
74                 ctx = wb_cmd_getgrnam_send(state, service, groupname);
75                 if (composite_nomem(ctx, state->ctx)) return result;
76
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);
83         }
84         return result;
85 }
86
87 static void cmd_getgrent_recv_grnam(struct composite_context *ctx)
88 {
89         struct cmd_getgrent_state *state =
90                 talloc_get_type(ctx->async.private_data,
91                                 struct cmd_getgrent_state);
92         struct winbindd_gr *gr;
93
94         DEBUG(5, ("cmd_getgrent_recv_grnam called\n"));
95
96         state->ctx->status = wb_cmd_getgrnam_recv(ctx, state, &gr);
97         if (!composite_is_ok(state->ctx)) return;
98
99         /*FIXME: Cheat for now and only get one group per call */
100         state->result = gr;
101
102         composite_done(state->ctx);
103 }
104
105 NTSTATUS wb_cmd_getgrent_recv(struct composite_context *ctx,
106                 TALLOC_CTX *mem_ctx, struct winbindd_gr **gr,
107                 uint32_t *num_groups)
108 {
109         NTSTATUS status = composite_wait(ctx);
110
111         DEBUG(5, ("wb_cmd_getgrent_recv called\n"));
112
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;
120         }
121
122         talloc_free(ctx);
123         return status;
124 }