16059ea4cc069d1557f41b450e95f5602c2c5e9f
[abartlet/samba.git/.git] / source4 / winbind / wb_cmd_list_groups.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Command backend for wbinfo -g
5
6    Copyright (C) Kai Blin 2009
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_list_groups_state {
28         struct composite_context *ctx;
29         struct wbsrv_service *service;
30
31         struct wbsrv_domain *domain;
32         char *domain_name;
33         uint32_t resume_index;
34         char *result;
35         uint32_t num_groups;
36 };
37
38 static void cmd_list_groups_recv_domain(struct composite_context *ctx);
39 static void cmd_list_groups_recv_group_list(struct composite_context *ctx);
40
41 struct composite_context *wb_cmd_list_groups_send(TALLOC_CTX *mem_ctx,
42                 struct wbsrv_service *service, const char *domain_name)
43 {
44         struct composite_context *ctx, *result;
45         struct cmd_list_groups_state *state;
46
47         DEBUG(5, ("wb_cmd_list_groups_send called\n"));
48
49         result = composite_create(mem_ctx, service->task->event_ctx);
50         if (!result) return NULL;
51
52         state = talloc(result, struct cmd_list_groups_state);
53         if (composite_nomem(state, result)) return result;
54
55         state->ctx = result;
56         result->private_data = state;
57         state->service = service;
58         state->resume_index = 0;
59         state->num_groups = 0;
60         state->result = talloc_strdup(state, "");
61         if (composite_nomem(state->result, state->ctx)) return result;
62
63         /*FIXME: We should look up the domain in the winbind request if it is
64          * set, not just take the primary domain. However, I want to get the
65          * libnet logic to work first. */
66
67         if (domain_name && *domain_name != '\0') {
68                 state->domain_name = talloc_strdup(state, domain_name);
69                 if (composite_nomem(state->domain_name, state->ctx))
70                         return result;
71         } else {
72                 state->domain_name = NULL;
73         }
74
75         ctx = wb_sid2domain_send(state, service, service->primary_sid);
76         if (composite_nomem(ctx, state->ctx)) return result;
77
78         composite_continue(state->ctx, ctx, cmd_list_groups_recv_domain, state);
79         return result;
80 }
81
82 static void cmd_list_groups_recv_domain(struct composite_context *ctx)
83 {
84         struct cmd_list_groups_state *state = talloc_get_type(
85                         ctx->async.private_data, struct cmd_list_groups_state);
86         struct wbsrv_domain *domain;
87         struct libnet_GroupList *group_list;
88
89         DEBUG(5, ("cmd_list_groups_recv_domain called\n"));
90
91         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
92         if (!composite_is_ok(state->ctx)) return;
93
94         /* we use this entry also for context purposes (libnet_GroupList) */
95         state->domain = domain;
96
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                                                    state->domain->libnet_ctx->samr.name);
102                 if (composite_nomem(state->domain_name, state->ctx)) return;
103         }
104
105         group_list = talloc(state, struct libnet_GroupList);
106         if (composite_nomem(group_list, state->ctx)) return;
107
108         group_list->in.domain_name = state->domain_name;
109
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.
112          */
113         group_list->in.page_size = 128;
114         group_list->in.resume_index = state->resume_index;
115
116         ctx = libnet_GroupList_send(state->domain->libnet_ctx, state,
117                                     group_list, NULL);
118
119         composite_continue(state->ctx, ctx, cmd_list_groups_recv_group_list,
120                            state);
121 }
122
123 static void cmd_list_groups_recv_group_list(struct composite_context *ctx)
124 {
125         struct cmd_list_groups_state *state = talloc_get_type(
126                         ctx->async.private_data, struct cmd_list_groups_state);
127         struct libnet_GroupList *group_list;
128         NTSTATUS status;
129         int i;
130
131         DEBUG(5, ("cmd_list_groups_recv_group_list called\n"));
132
133         group_list = talloc(state, struct libnet_GroupList);
134         if (composite_nomem(group_list, state->ctx)) return;
135
136         status = libnet_GroupList_recv(ctx, state, group_list);
137
138         /* If NTSTATUS is neither OK nor MORE_ENTRIES, something broke */
139         if (!NT_STATUS_IS_OK(status) &&
140             !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
141                 composite_error(state->ctx, status);
142                 return;
143         }
144
145         for (i = 0; i < group_list->out.count; ++i) {
146                 DEBUG(5, ("Appending group '%s'\n",
147                           group_list->out.groups[i].groupname));
148                 state->result = talloc_asprintf_append_buffer(state->result,
149                                                               "%s,",
150                                                               group_list->out.groups[i].groupname);
151                 state->num_groups++;
152         }
153
154         /* If the status is OK, we're finished, there's no more groups.
155          * So we'll trim off the trailing ',' and are done.*/
156         if (NT_STATUS_IS_OK(status)) {
157                 size_t str_len = strlen(state->result);
158                 DEBUG(5, ("list_GroupList_recv returned NT_STATUS_OK\n"));
159                 if (str_len > 0) {
160                         state->result[str_len - 1] = '\0';
161                 }
162                 composite_done(state->ctx);
163                 return;
164         }
165
166         DEBUG(5, ("list_GroupList_recv returned NT_STATUS_MORE_ENTRIES\n"));
167
168         /* Otherwise there's more groups to get, so call out to libnet and
169          * continue on this function here. */
170
171         group_list->in.domain_name = state->domain_name;
172         /* See comment above about the page size. 128 seems like a good default.
173          */
174         group_list->in.page_size = 128;
175         group_list->in.resume_index = group_list->out.resume_index;
176
177         ctx = libnet_GroupList_send(state->domain->libnet_ctx, state,group_list,
178                                     NULL);
179
180         composite_continue(state->ctx, ctx, cmd_list_groups_recv_group_list,
181                            state);
182 }
183
184 NTSTATUS wb_cmd_list_groups_recv(struct composite_context *ctx,
185                 TALLOC_CTX *mem_ctx, uint32_t *extra_data_len,
186                 char **extra_data, uint32_t *num_groups)
187 {
188         NTSTATUS status = composite_wait(ctx);
189
190         DEBUG(5, ("wb_cmd_list_groups_recv called\n"));
191
192         if (NT_STATUS_IS_OK(status)) {
193                 struct cmd_list_groups_state *state = talloc_get_type(
194                         ctx->private_data, struct cmd_list_groups_state);
195
196                 *extra_data_len = strlen(state->result);
197                 *extra_data = talloc_steal(mem_ctx, state->result);
198                 *num_groups = state->num_groups;
199         }
200
201         talloc_free(ctx);
202         return status;
203 }
204
205