9260a422077814e499dc148abb04fa88029a160f
[ira/wip.git] / source4 / winbind / wb_cmd_list_users.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Command backend for wbinfo -u
5
6    Copyright (C) Kai Blin 2007
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 "winbind/wb_async_helpers.h"
26 #include "winbind/wb_helper.h"
27 #include "smbd/service_task.h"
28 #include "nsswitch/winbindd_nss.h"
29 #include "libnet/libnet_proto.h"
30
31 struct cmd_list_users_state {
32         struct composite_context *ctx;
33         struct wbsrv_service *service;
34
35         struct wbsrv_domain *domain;
36         char *domain_name;
37         uint resume_index;
38         char *result;
39 };
40
41 static void cmd_list_users_recv_domain(struct composite_context *ctx);
42 static void cmd_list_users_recv_user_list(struct composite_context *ctx);
43
44 struct composite_context *wb_cmd_list_users_send(TALLOC_CTX *mem_ctx,
45                 struct wbsrv_service *service, const char *domain_name)
46 {
47         struct composite_context *ctx, *result;
48         struct cmd_list_users_state *state;
49
50         DEBUG(5, ("wb_cmd_list_users_send called\n"));
51
52         result = composite_create(mem_ctx, service->task->event_ctx);
53         if (!result) return NULL;
54
55         state = talloc(result, struct cmd_list_users_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->resume_index = 0;
62         state->result = talloc_strdup(state, "");
63         if (composite_nomem(state->result, state->ctx)) return result;
64
65         /*FIXME: We should look up the domain in the winbind request if it is
66          * set, not just take the primary domain. However, I want to get the
67          * libnet logic to work first. */
68
69         if (domain_name && *domain_name != '\0') {
70                 state->domain_name = talloc_strdup(state, domain_name);
71                 if (composite_nomem(state->domain_name, state->ctx))
72                         return result;
73         } else {
74                 state->domain_name = NULL;
75         }
76
77         ctx = wb_sid2domain_send(state, service, service->primary_sid);
78         if (composite_nomem(ctx, state->ctx)) return result;
79
80         composite_continue(state->ctx, ctx, cmd_list_users_recv_domain, state);
81         return result;
82 }
83
84 static void cmd_list_users_recv_domain(struct composite_context *ctx)
85 {
86         struct cmd_list_users_state *state = talloc_get_type(
87                         ctx->async.private_data, struct cmd_list_users_state);
88         struct wbsrv_domain *domain;
89         struct libnet_UserList *user_list;
90
91         DEBUG(5, ("cmd_list_users_recv_domain called\n"));
92
93         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
94         if (!composite_is_ok(state->ctx)) return;
95
96         state->domain = domain;
97
98         /* If this is non-null, we've looked up the domain given in the winbind
99          * request, otherwise we'll just use the default name.*/
100         if (state->domain_name == NULL) {
101                 state->domain_name = talloc_strdup(state,
102                                 domain->libnet_ctx->samr.name);
103                 if (composite_nomem(state->domain_name, state->ctx)) return;
104         }
105
106         user_list = talloc(state, struct libnet_UserList);
107         if (composite_nomem(user_list, state->ctx)) return;
108
109         user_list->in.domain_name = state->domain_name;
110
111         /* Rafal suggested that 128 is a good number here. I don't like magic
112          * numbers too much, but for now it'll have to do.
113          */
114         user_list->in.page_size = 128;
115         user_list->in.resume_index = state->resume_index;
116
117         ctx = libnet_UserList_send(domain->libnet_ctx, state, user_list, NULL);
118
119         composite_continue(state->ctx, ctx, cmd_list_users_recv_user_list,
120                         state);
121 }
122
123 static void cmd_list_users_recv_user_list(struct composite_context *ctx)
124 {
125         struct cmd_list_users_state *state = talloc_get_type(
126                         ctx->async.private_data, struct cmd_list_users_state);
127         struct libnet_UserList *user_list;
128         NTSTATUS status;
129         int i;
130
131         DEBUG(5, ("cmd_list_users_recv_user_list called\n"));
132
133         user_list = talloc(state, struct libnet_UserList);
134         if (composite_nomem(user_list, state->ctx)) return;
135
136         status = libnet_UserList_recv(ctx, state, user_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 < user_list->out.count; ++i) {
146                 DEBUG(5, ("Appending user '%s'\n", user_list->out.users[i].username));
147                 state->result = talloc_asprintf_append(state->result, "%s,",
148                                         user_list->out.users[i].username);
149         }
150
151         /* If the status is OK, we're finished, there's no more users.
152          * So we'll trim off the trailing ',' and are done.*/
153         if (NT_STATUS_IS_OK(status)) {
154                 int str_len = strlen(state->result);
155                 DEBUG(5, ("list_UserList_recv returned NT_STATUS_OK\n"));
156                 state->result[str_len - 1] = '\0';
157                 composite_done(state->ctx);
158                 return;
159         }
160
161         DEBUG(5, ("list_UserList_recv returned NT_STATUS_MORE_ENTRIES\n"));
162
163         /* Otherwise there's more users to get, so call out to libnet and
164          * continue on this function here. */
165
166         user_list->in.domain_name = state->domain_name;
167         /* See comment above about the page size. 128 seems like a good default.
168          */
169         user_list->in.page_size = 128;
170         user_list->in.resume_index = user_list->out.resume_index;
171
172         ctx = libnet_UserList_send(state->domain->libnet_ctx, state, user_list,
173                         NULL);
174
175         composite_continue(state->ctx, ctx, cmd_list_users_recv_user_list,
176                         state);
177 }
178
179 NTSTATUS wb_cmd_list_users_recv(struct composite_context *ctx,
180                 TALLOC_CTX *mem_ctx, uint32_t *extra_data_len,
181                 char **extra_data)
182 {
183         NTSTATUS status = composite_wait(ctx);
184
185         DEBUG(5, ("wb_cmd_list_users_recv called\n"));
186
187         if (NT_STATUS_IS_OK(status)) {
188                 struct cmd_list_users_state *state = talloc_get_type(
189                         ctx->private_data, struct cmd_list_users_state);
190
191                 *extra_data_len = strlen(state->result);
192                 *extra_data = talloc_steal(mem_ctx, state->result);
193         }
194
195         talloc_free(ctx);
196         return status;
197 }
198
199