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 "smbd/service_task.h"
27 struct cmd_list_users_state
{
28 struct composite_context
*ctx
;
29 struct wbsrv_service
*service
;
31 struct wbsrv_domain
*domain
;
33 uint32_t resume_index
;
38 static void cmd_list_users_recv_domain(struct composite_context
*ctx
);
39 static void cmd_list_users_recv_user_list(struct composite_context
*ctx
);
41 struct composite_context
*wb_cmd_list_users_send(TALLOC_CTX
*mem_ctx
,
42 struct wbsrv_service
*service
, const char *domain_name
)
44 struct composite_context
*ctx
, *result
;
45 struct cmd_list_users_state
*state
;
47 DEBUG(5, ("wb_cmd_list_users_send called\n"));
49 result
= composite_create(mem_ctx
, service
->task
->event_ctx
);
50 if (!result
) return NULL
;
52 state
= talloc(result
, struct cmd_list_users_state
);
53 if (composite_nomem(state
, result
)) return result
;
56 result
->private_data
= state
;
57 state
->service
= service
;
58 state
->resume_index
= 0;
60 state
->result
= talloc_strdup(state
, "");
61 if (composite_nomem(state
->result
, state
->ctx
)) return result
;
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. */
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
))
72 state
->domain_name
= NULL
;
75 ctx
= wb_sid2domain_send(state
, service
, service
->primary_sid
);
76 if (composite_nomem(ctx
, state
->ctx
)) return result
;
78 composite_continue(state
->ctx
, ctx
, cmd_list_users_recv_domain
, state
);
82 static void cmd_list_users_recv_domain(struct composite_context
*ctx
)
84 struct cmd_list_users_state
*state
= talloc_get_type(
85 ctx
->async
.private_data
, struct cmd_list_users_state
);
86 struct wbsrv_domain
*domain
;
87 struct libnet_UserList
*user_list
;
89 DEBUG(5, ("cmd_list_users_recv_domain called\n"));
91 state
->ctx
->status
= wb_sid2domain_recv(ctx
, &domain
);
92 if (!composite_is_ok(state
->ctx
)) return;
94 state
->domain
= domain
;
96 /* If this is non-null, we've looked up the domain given in the winbind
97 * request, otherwise we'll just use the default name.*/
98 if (state
->domain_name
== NULL
) {
99 state
->domain_name
= talloc_strdup(state
,
100 domain
->libnet_ctx
->samr
.name
);
101 if (composite_nomem(state
->domain_name
, state
->ctx
)) return;
104 user_list
= talloc(state
, struct libnet_UserList
);
105 if (composite_nomem(user_list
, state
->ctx
)) return;
107 user_list
->in
.domain_name
= state
->domain_name
;
109 /* Rafal suggested that 128 is a good number here. I don't like magic
110 * numbers too much, but for now it'll have to do.
112 user_list
->in
.page_size
= 128;
113 user_list
->in
.resume_index
= state
->resume_index
;
115 ctx
= libnet_UserList_send(domain
->libnet_ctx
, state
, user_list
, NULL
);
117 composite_continue(state
->ctx
, ctx
, cmd_list_users_recv_user_list
,
121 static void cmd_list_users_recv_user_list(struct composite_context
*ctx
)
123 struct cmd_list_users_state
*state
= talloc_get_type(
124 ctx
->async
.private_data
, struct cmd_list_users_state
);
125 struct libnet_UserList
*user_list
;
129 DEBUG(5, ("cmd_list_users_recv_user_list called\n"));
131 user_list
= talloc(state
, struct libnet_UserList
);
132 if (composite_nomem(user_list
, state
->ctx
)) return;
134 status
= libnet_UserList_recv(ctx
, state
, user_list
);
136 /* If NTSTATUS is neither OK nor MORE_ENTRIES, something broke */
137 if (!NT_STATUS_IS_OK(status
) &&
138 !NT_STATUS_EQUAL(status
, STATUS_MORE_ENTRIES
) &&
139 !NT_STATUS_EQUAL(status
, NT_STATUS_NO_MORE_ENTRIES
)) {
140 composite_error(state
->ctx
, status
);
144 for (i
= 0; i
< user_list
->out
.count
; ++i
) {
145 DEBUG(5, ("Appending user '%s'\n", user_list
->out
.users
[i
].username
));
146 state
->result
= talloc_asprintf_append_buffer(state
->result
, "%s,",
147 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
,
181 char **extra_data
, uint32_t *num_users
)
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
);
193 *num_users
= state
->num_users
;