s4 idmap: Make the sid_to_xid and xid_to_sid calls static.
[nivanova/samba-autobuild/.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 };
36
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);
39
40 struct composite_context *wb_cmd_list_groups_send(TALLOC_CTX *mem_ctx,
41                 struct wbsrv_service *service, const char *domain_name)
42 {
43         struct composite_context *ctx, *result;
44         struct cmd_list_groups_state *state;
45
46         DEBUG(5, ("wb_cmd_list_groups_send called\n"));
47
48         result = composite_create(mem_ctx, service->task->event_ctx);
49         if (!result) return NULL;
50
51         state = talloc(result, struct cmd_list_groups_state);
52         if (composite_nomem(state, result)) return result;
53
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;
60
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. */
64
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;
71         }
72
73         ctx = wb_sid2domain_send(state, service, service->primary_sid);
74         if (composite_nomem(ctx, state->ctx)) return result;
75
76         composite_continue(state->ctx, ctx, cmd_list_groups_recv_domain, state);
77         return result;
78 }
79
80 static void cmd_list_groups_recv_domain(struct composite_context *ctx)
81 {
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;
86
87         DEBUG(5, ("cmd_list_groups_recv_domain called\n"));
88
89         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
90         if (!composite_is_ok(state->ctx)) return;
91
92         state->domain = domain;
93
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;
100         }
101
102         group_list = talloc(state, struct libnet_GroupList);
103         if (composite_nomem(group_list, state->ctx)) return;
104
105         group_list->in.domain_name = state->domain_name;
106
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.
109          */
110         group_list->in.page_size = 128;
111         group_list->in.resume_index = state->resume_index;
112
113         ctx = libnet_GroupList_send(domain->libnet_ctx, state, group_list,NULL);
114
115         composite_continue(state->ctx, ctx, cmd_list_groups_recv_group_list,
116                         state);
117 }
118
119 static void cmd_list_groups_recv_group_list(struct composite_context *ctx)
120 {
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;
126
127         DEBUG(5, ("cmd_list_groups_recv_group_list called\n"));
128
129         group_list = talloc(state, struct libnet_GroupList);
130         if (composite_nomem(group_list, state->ctx)) return;
131
132         status = libnet_GroupList_recv(ctx, state, group_list);
133
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;
139         }
140
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);
147         }
148
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;
157         }
158
159         DEBUG(5, ("list_GroupList_recv returned NT_STATUS_MORE_ENTRIES\n"));
160
161         /* Otherwise there's more groups to get, so call out to libnet and
162          * continue on this function here. */
163
164         group_list->in.domain_name = state->domain_name;
165         /* See comment above about the page size. 128 seems like a good default.
166          */
167         group_list->in.page_size = 128;
168         group_list->in.resume_index = group_list->out.resume_index;
169
170         ctx = libnet_GroupList_send(state->domain->libnet_ctx, state,group_list,
171                         NULL);
172
173         composite_continue(state->ctx, ctx, cmd_list_groups_recv_group_list,
174                         state);
175 }
176
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)
180 {
181         NTSTATUS status = composite_wait(ctx);
182
183         DEBUG(5, ("wb_cmd_list_groups_recv called\n"));
184
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);
188
189                 *extra_data_len = strlen(state->result);
190                 *extra_data = talloc_steal(mem_ctx, state->result);
191         }
192
193         talloc_free(ctx);
194         return status;
195 }
196
197