Fetch the SID of the user we are running as and send with the common
[Samba/nascimento.git] / source4 / winbind / wb_cmd_list_groups.c
blobafb96b46fadc7d75b69c51e8ec5e79f38888a1f1
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 "smbd/service_task.h"
27 struct cmd_list_groups_state {
28 struct composite_context *ctx;
29 struct wbsrv_service *service;
31 struct wbsrv_domain *domain;
32 char *domain_name;
33 uint32_t resume_index;
34 char *result;
37 static void cmd_list_groups_recv_domain(struct composite_context *ctx);
38 static void cmd_list_groups_recv_group_list(struct composite_context *ctx);
40 struct composite_context *wb_cmd_list_groups_send(TALLOC_CTX *mem_ctx,
41 struct wbsrv_service *service, const char *domain_name)
43 struct composite_context *ctx, *result;
44 struct cmd_list_groups_state *state;
46 DEBUG(5, ("wb_cmd_list_groups_send called\n"));
48 result = composite_create(mem_ctx, service->task->event_ctx);
49 if (!result) return NULL;
51 state = talloc(result, struct cmd_list_groups_state);
52 if (composite_nomem(state, result)) return result;
54 state->ctx = result;
55 result->private_data = state;
56 state->service = service;
57 state->resume_index = 0;
58 state->result = talloc_strdup(state, "");
59 if (composite_nomem(state->result, state->ctx)) return result;
61 /*FIXME: We should look up the domain in the winbind request if it is
62 * set, not just take the primary domain. However, I want to get the
63 * libnet logic to work first. */
65 if (domain_name && *domain_name != '\0') {
66 state->domain_name = talloc_strdup(state, domain_name);
67 if (composite_nomem(state->domain_name, state->ctx))
68 return result;
69 } else {
70 state->domain_name = NULL;
73 ctx = wb_sid2domain_send(state, service, service->primary_sid);
74 if (composite_nomem(ctx, state->ctx)) return result;
76 composite_continue(state->ctx, ctx, cmd_list_groups_recv_domain, state);
77 return result;
80 static void cmd_list_groups_recv_domain(struct composite_context *ctx)
82 struct cmd_list_groups_state *state = talloc_get_type(
83 ctx->async.private_data, struct cmd_list_groups_state);
84 struct wbsrv_domain *domain;
85 struct libnet_GroupList *group_list;
87 DEBUG(5, ("cmd_list_groups_recv_domain called\n"));
89 state->ctx->status = wb_sid2domain_recv(ctx, &domain);
90 if (!composite_is_ok(state->ctx)) return;
92 state->domain = domain;
94 /* If this is non-null, we've looked up the domain given in the winbind
95 * request, otherwise we'll just use the default name.*/
96 if (state->domain_name == NULL) {
97 state->domain_name = talloc_strdup(state,
98 domain->libnet_ctx->samr.name);
99 if (composite_nomem(state->domain_name, state->ctx)) return;
102 group_list = talloc(state, struct libnet_GroupList);
103 if (composite_nomem(group_list, state->ctx)) return;
105 group_list->in.domain_name = state->domain_name;
107 /* Rafal suggested that 128 is a good number here. I don't like magic
108 * numbers too much, but for now it'll have to do.
110 group_list->in.page_size = 128;
111 group_list->in.resume_index = state->resume_index;
113 ctx = libnet_GroupList_send(domain->libnet_ctx, state, group_list,NULL);
115 composite_continue(state->ctx, ctx, cmd_list_groups_recv_group_list,
116 state);
119 static void cmd_list_groups_recv_group_list(struct composite_context *ctx)
121 struct cmd_list_groups_state *state = talloc_get_type(
122 ctx->async.private_data, struct cmd_list_groups_state);
123 struct libnet_GroupList *group_list;
124 NTSTATUS status;
125 int i;
127 DEBUG(5, ("cmd_list_groups_recv_group_list called\n"));
129 group_list = talloc(state, struct libnet_GroupList);
130 if (composite_nomem(group_list, state->ctx)) return;
132 status = libnet_GroupList_recv(ctx, state, group_list);
134 /* If NTSTATUS is neither OK nor MORE_ENTRIES, something broke */
135 if (!NT_STATUS_IS_OK(status) &&
136 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
137 composite_error(state->ctx, status);
138 return;
141 for (i = 0; i < group_list->out.count; ++i) {
142 DEBUG(5, ("Appending group '%s'\n",
143 group_list->out.groups[i].groupname));
144 state->result = talloc_asprintf_append_buffer(state->result,
145 "%s,",
146 group_list->out.groups[i].groupname);
149 /* If the status is OK, we're finished, there's no more groups.
150 * So we'll trim off the trailing ',' and are done.*/
151 if (NT_STATUS_IS_OK(status)) {
152 int str_len = strlen(state->result);
153 DEBUG(5, ("list_GroupList_recv returned NT_STATUS_OK\n"));
154 state->result[str_len - 1] = '\0';
155 composite_done(state->ctx);
156 return;
159 DEBUG(5, ("list_GroupList_recv returned NT_STATUS_MORE_ENTRIES\n"));
161 /* Otherwise there's more groups to get, so call out to libnet and
162 * continue on this function here. */
164 group_list->in.domain_name = state->domain_name;
165 /* See comment above about the page size. 128 seems like a good default.
167 group_list->in.page_size = 128;
168 group_list->in.resume_index = group_list->out.resume_index;
170 ctx = libnet_GroupList_send(state->domain->libnet_ctx, state,group_list,
171 NULL);
173 composite_continue(state->ctx, ctx, cmd_list_groups_recv_group_list,
174 state);
177 NTSTATUS wb_cmd_list_groups_recv(struct composite_context *ctx,
178 TALLOC_CTX *mem_ctx, uint32_t *extra_data_len,
179 char **extra_data)
181 NTSTATUS status = composite_wait(ctx);
183 DEBUG(5, ("wb_cmd_list_groups_recv called\n"));
185 if (NT_STATUS_IS_OK(status)) {
186 struct cmd_list_groups_state *state = talloc_get_type(
187 ctx->private_data, struct cmd_list_groups_state);
189 *extra_data_len = strlen(state->result);
190 *extra_data = talloc_steal(mem_ctx, state->result);
193 talloc_free(ctx);
194 return status;