2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_LIST_USERS
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "lib/util/strv.h"
25 struct winbindd_list_users_domstate {
26 struct tevent_req *subreq;
27 struct winbindd_domain *domain;
31 struct winbindd_list_users_state {
35 struct winbindd_list_users_domstate *domains;
38 static void winbindd_list_users_done(struct tevent_req *subreq);
40 struct tevent_req *winbindd_list_users_send(TALLOC_CTX *mem_ctx,
41 struct tevent_context *ev,
42 struct winbindd_cli_state *cli,
43 struct winbindd_request *request)
45 struct tevent_req *req;
46 struct winbindd_list_users_state *state;
47 struct winbindd_domain *domain;
50 req = tevent_req_create(mem_ctx, &state,
51 struct winbindd_list_users_state);
55 D_NOTICE("[%s (%u)] Winbind external command LIST_USERS start.\n"
56 "WBFLAG_FROM_NSS is %s, winbind enum users is %d.\n",
58 (unsigned int)cli->pid,
59 request->wb_flags & WBFLAG_FROM_NSS ? "Set" : "Unset",
60 lp_winbind_enum_users());
62 if (request->wb_flags & WBFLAG_FROM_NSS && !lp_winbind_enum_users()) {
64 return tevent_req_post(req, ev);
67 /* Ensure null termination */
68 request->domain_name[sizeof(request->domain_name)-1]='\0';
70 D_NOTICE("Listing users for domain %s\n", request->domain_name);
71 if (request->domain_name[0] != '\0') {
72 state->num_domains = 1;
74 state->num_domains = 0;
75 for (domain = domain_list(); domain; domain = domain->next) {
76 state->num_domains += 1;
80 state->domains = talloc_array(state,
81 struct winbindd_list_users_domstate,
83 if (tevent_req_nomem(state->domains, req)) {
84 return tevent_req_post(req, ev);
87 if (request->domain_name[0] != '\0') {
88 state->domains[0].domain = find_domain_from_name_noinit(
89 request->domain_name);
90 if (state->domains[0].domain == NULL) {
91 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
92 return tevent_req_post(req, ev);
96 for (domain = domain_list(); domain; domain = domain->next) {
97 state->domains[i++].domain = domain;
101 for (i=0; i<state->num_domains; i++) {
102 struct winbindd_list_users_domstate *d = &state->domains[i];
104 * Use "state" as a talloc memory context since it has type
105 * "struct tevent_req". This is needed to make tevent call depth
106 * tracking working as expected.
107 * After calling wb_query_user_list_send(), re-parent back to
108 * "state->domains" to make TALLOC_FREE(state->domains) working.
110 d->subreq = wb_query_user_list_send(state, ev, d->domain);
111 d->subreq = talloc_reparent(state, state->domains, d->subreq);
112 if (tevent_req_nomem(d->subreq, req)) {
113 TALLOC_FREE(state->domains);
114 return tevent_req_post(req, ev);
116 tevent_req_set_callback(d->subreq, winbindd_list_users_done,
119 state->num_received = 0;
123 static void winbindd_list_users_done(struct tevent_req *subreq)
125 struct tevent_req *req = tevent_req_callback_data(
126 subreq, struct tevent_req);
127 struct winbindd_list_users_state *state = tevent_req_data(
128 req, struct winbindd_list_users_state);
129 struct winbindd_list_users_domstate *d;
133 for (i=0; i<state->num_domains; i++) {
134 if (subreq == state->domains[i].subreq) {
138 if (i == state->num_domains) {
139 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
143 d = &state->domains[i];
145 status = wb_query_user_list_recv(subreq, state->domains,
148 if (!NT_STATUS_IS_OK(status)) {
150 * Just skip this domain
155 state->num_received += 1;
157 if (state->num_received >= state->num_domains) {
158 tevent_req_done(req);
162 NTSTATUS winbindd_list_users_recv(struct tevent_req *req,
163 struct winbindd_response *response)
165 struct winbindd_list_users_state *state = tevent_req_data(
166 req, struct winbindd_list_users_state);
171 D_NOTICE("Winbind external command LIST_USERS end.\n");
172 if (tevent_req_is_nterror(req, &status)) {
173 D_WARNING("Failed with %s.\n", nt_errstr(status));
179 for (i=0; i<state->num_domains; i++) {
180 struct winbindd_list_users_domstate *d = &state->domains[i];
183 if (d->users == NULL) {
187 ret = strv_append(state, &result, d->users);
189 return map_nt_error_from_unix(ret);
193 len = talloc_get_size(result);
195 response->extra_data.data = talloc_steal(response, result);
196 response->length += len;
197 response->data.num_entries = 0;
199 if (result != NULL && len >= 1) {
201 response->data.num_entries = 1;
203 for (i=0; i<len; i++) {
204 if (result[i] == '\0') {
206 response->data.num_entries += 1;
211 D_NOTICE("Got %"PRIu32" user(s):\n%s\n",
212 response->data.num_entries,