2 Unix SMB/CIFS implementation.
4 Command backend for wbinfo -u
6 Copyright (C) Kai Blin 2007
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/>.
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"
31 struct cmd_list_users_state {
32 struct composite_context *ctx;
33 struct wbsrv_service *service;
35 struct wbsrv_domain *domain;
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);
44 struct composite_context *wb_cmd_list_users_send(TALLOC_CTX *mem_ctx,
45 struct wbsrv_service *service, const char *domain_name)
47 struct composite_context *ctx, *result;
48 struct cmd_list_users_state *state;
50 DEBUG(5, ("wb_cmd_list_users_send called\n"));
52 result = composite_create(mem_ctx, service->task->event_ctx);
53 if (!result) return NULL;
55 state = talloc(result, struct cmd_list_users_state);
56 if (composite_nomem(state, result)) return 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;
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. */
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))
74 state->domain_name = NULL;
77 ctx = wb_sid2domain_send(state, service, service->primary_sid);
78 if (composite_nomem(ctx, state->ctx)) return result;
80 composite_continue(state->ctx, ctx, cmd_list_users_recv_domain, state);
84 static void cmd_list_users_recv_domain(struct composite_context *ctx)
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;
91 DEBUG(5, ("cmd_list_users_recv_domain called\n"));
93 state->ctx->status = wb_sid2domain_recv(ctx, &domain);
94 if (!composite_is_ok(state->ctx)) return;
96 state->domain = domain;
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;
106 user_list = talloc(state, struct libnet_UserList);
107 if (composite_nomem(user_list, state->ctx)) return;
109 user_list->in.domain_name = state->domain_name;
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.
114 user_list->in.page_size = 128;
115 user_list->in.resume_index = state->resume_index;
117 ctx = libnet_UserList_send(domain->libnet_ctx, state, user_list, NULL);
119 composite_continue(state->ctx, ctx, cmd_list_users_recv_user_list,
123 static void cmd_list_users_recv_user_list(struct composite_context *ctx)
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;
131 DEBUG(5, ("cmd_list_users_recv_user_list called\n"));
133 user_list = talloc(state, struct libnet_UserList);
134 if (composite_nomem(user_list, state->ctx)) return;
136 status = libnet_UserList_recv(ctx, state, user_list);
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);
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);
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);
161 DEBUG(5, ("list_UserList_recv returned NT_STATUS_MORE_ENTRIES\n"));
163 /* Otherwise there's more users to get, so call out to libnet and
164 * continue on this function here. */
166 user_list->in.domain_name = state->domain_name;
167 /* See comment above about the page size. 128 seems like a good default.
169 user_list->in.page_size = 128;
170 user_list->in.resume_index = user_list->out.resume_index;
172 ctx = libnet_UserList_send(state->domain->libnet_ctx, state, user_list,
175 composite_continue(state->ctx, ctx, cmd_list_users_recv_user_list,
179 NTSTATUS wb_cmd_list_users_recv(struct composite_context *ctx,
180 TALLOC_CTX *mem_ctx, uint32_t *extra_data_len,
183 NTSTATUS status = composite_wait(ctx);
185 DEBUG(5, ("wb_cmd_list_users_recv called\n"));
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);
191 *extra_data_len = strlen(state->result);
192 *extra_data = talloc_steal(mem_ctx, state->result);