s3/docs: Remove unnecessary .sp.
[Samba/ekacnet.git] / source4 / winbind / wb_cmd_list_groups.c
blob04a4fc39df489c863923fcd049f47def3fe8c394
1 /*
2 Unix SMB/CIFS implementation.
4 Command backend for wbinfo -g
6 Copyright (C) Kai Blin 2009
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 "winbind/wb_async_helpers.h"
26 #include "winbind/wb_helper.h"
27 #include "smbd/service_task.h"
28 #include "libnet/libnet_proto.h"
30 struct cmd_list_groups_state {
31 struct composite_context *ctx;
32 struct wbsrv_service *service;
34 struct wbsrv_domain *domain;
35 char *domain_name;
36 uint32_t resume_index;
37 char *result;
40 static void cmd_list_groups_recv_domain(struct composite_context *ctx);
41 static void cmd_list_groups_recv_group_list(struct composite_context *ctx);
43 struct composite_context *wb_cmd_list_groups_send(TALLOC_CTX *mem_ctx,
44 struct wbsrv_service *service, const char *domain_name)
46 struct composite_context *ctx, *result;
47 struct cmd_list_groups_state *state;
49 DEBUG(5, ("wb_cmd_list_groups_send called\n"));
51 result = composite_create(mem_ctx, service->task->event_ctx);
52 if (!result) return NULL;
54 state = talloc(result, struct cmd_list_groups_state);
55 if (composite_nomem(state, result)) return result;
57 state->ctx = result;
58 result->private_data = state;
59 state->service = service;
60 state->resume_index = 0;
61 state->result = talloc_strdup(state, "");
62 if (composite_nomem(state->result, state->ctx)) return result;
64 /*FIXME: We should look up the domain in the winbind request if it is
65 * set, not just take the primary domain. However, I want to get the
66 * libnet logic to work first. */
68 if (domain_name && *domain_name != '\0') {
69 state->domain_name = talloc_strdup(state, domain_name);
70 if (composite_nomem(state->domain_name, state->ctx))
71 return result;
72 } else {
73 state->domain_name = NULL;
76 ctx = wb_sid2domain_send(state, service, service->primary_sid);
77 if (composite_nomem(ctx, state->ctx)) return result;
79 composite_continue(state->ctx, ctx, cmd_list_groups_recv_domain, state);
80 return result;
83 static void cmd_list_groups_recv_domain(struct composite_context *ctx)
85 struct cmd_list_groups_state *state = talloc_get_type(
86 ctx->async.private_data, struct cmd_list_groups_state);
87 struct wbsrv_domain *domain;
88 struct libnet_GroupList *group_list;
90 DEBUG(5, ("cmd_list_groups_recv_domain called\n"));
92 state->ctx->status = wb_sid2domain_recv(ctx, &domain);
93 if (!composite_is_ok(state->ctx)) return;
95 state->domain = domain;
97 /* If this is non-null, we've looked up the domain given in the winbind
98 * request, otherwise we'll just use the default name.*/
99 if (state->domain_name == NULL) {
100 state->domain_name = talloc_strdup(state,
101 domain->libnet_ctx->samr.name);
102 if (composite_nomem(state->domain_name, state->ctx)) return;
105 group_list = talloc(state, struct libnet_GroupList);
106 if (composite_nomem(group_list, state->ctx)) return;
108 group_list->in.domain_name = state->domain_name;
110 /* Rafal suggested that 128 is a good number here. I don't like magic
111 * numbers too much, but for now it'll have to do.
113 group_list->in.page_size = 128;
114 group_list->in.resume_index = state->resume_index;
116 ctx = libnet_GroupList_send(domain->libnet_ctx, state, group_list,NULL);
118 composite_continue(state->ctx, ctx, cmd_list_groups_recv_group_list,
119 state);
122 static void cmd_list_groups_recv_group_list(struct composite_context *ctx)
124 struct cmd_list_groups_state *state = talloc_get_type(
125 ctx->async.private_data, struct cmd_list_groups_state);
126 struct libnet_GroupList *group_list;
127 NTSTATUS status;
128 int i;
130 DEBUG(5, ("cmd_list_groups_recv_group_list called\n"));
132 group_list = talloc(state, struct libnet_GroupList);
133 if (composite_nomem(group_list, state->ctx)) return;
135 status = libnet_GroupList_recv(ctx, state, group_list);
137 /* If NTSTATUS is neither OK nor MORE_ENTRIES, something broke */
138 if (!NT_STATUS_IS_OK(status) &&
139 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
140 composite_error(state->ctx, status);
141 return;
144 for (i = 0; i < group_list->out.count; ++i) {
145 DEBUG(5, ("Appending group '%s'\n",
146 group_list->out.groups[i].groupname));
147 state->result = talloc_asprintf_append_buffer(state->result,
148 "%s,",
149 group_list->out.groups[i].groupname);
152 /* If the status is OK, we're finished, there's no more groups.
153 * So we'll trim off the trailing ',' and are done.*/
154 if (NT_STATUS_IS_OK(status)) {
155 int str_len = strlen(state->result);
156 DEBUG(5, ("list_GroupList_recv returned NT_STATUS_OK\n"));
157 state->result[str_len - 1] = '\0';
158 composite_done(state->ctx);
159 return;
162 DEBUG(5, ("list_GroupList_recv returned NT_STATUS_MORE_ENTRIES\n"));
164 /* Otherwise there's more groups to get, so call out to libnet and
165 * continue on this function here. */
167 group_list->in.domain_name = state->domain_name;
168 /* See comment above about the page size. 128 seems like a good default.
170 group_list->in.page_size = 128;
171 group_list->in.resume_index = group_list->out.resume_index;
173 ctx = libnet_GroupList_send(state->domain->libnet_ctx, state,group_list,
174 NULL);
176 composite_continue(state->ctx, ctx, cmd_list_groups_recv_group_list,
177 state);
180 NTSTATUS wb_cmd_list_groups_recv(struct composite_context *ctx,
181 TALLOC_CTX *mem_ctx, uint32_t *extra_data_len,
182 char **extra_data)
184 NTSTATUS status = composite_wait(ctx);
186 DEBUG(5, ("wb_cmd_list_groups_recv called\n"));
188 if (NT_STATUS_IS_OK(status)) {
189 struct cmd_list_groups_state *state = talloc_get_type(
190 ctx->private_data, struct cmd_list_groups_state);
192 *extra_data_len = strlen(state->result);
193 *extra_data = talloc_steal(mem_ctx, state->result);
196 talloc_free(ctx);
197 return status;