WHATSNEW: Add release notes for Samba 4.13.0rc6.
[Samba.git] / source3 / winbindd / winbindd_list_users.c
blobd500463cacce9f927bafea858a7466b52d1577e3
1 /*
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
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;
28 char *users;
31 struct winbindd_list_users_state {
32 size_t num_received;
33 /* All domains */
34 size_t num_domains;
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;
48 size_t i;
50 req = tevent_req_create(mem_ctx, &state,
51 struct winbindd_list_users_state);
52 if (req == NULL) {
53 return NULL;
56 if (request->wb_flags & WBFLAG_FROM_NSS && !lp_winbind_enum_users()) {
57 tevent_req_done(req);
58 return tevent_req_post(req, ev);
61 /* Ensure null termination */
62 request->domain_name[sizeof(request->domain_name)-1]='\0';
64 DEBUG(3, ("list_users %s\n", request->domain_name));
66 if (request->domain_name[0] != '\0') {
67 state->num_domains = 1;
68 } else {
69 state->num_domains = 0;
70 for (domain = domain_list(); domain; domain = domain->next) {
71 state->num_domains += 1;
75 state->domains = talloc_array(state,
76 struct winbindd_list_users_domstate,
77 state->num_domains);
78 if (tevent_req_nomem(state->domains, req)) {
79 return tevent_req_post(req, ev);
82 if (request->domain_name[0] != '\0') {
83 state->domains[0].domain = find_domain_from_name_noinit(
84 request->domain_name);
85 if (state->domains[0].domain == NULL) {
86 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
87 return tevent_req_post(req, ev);
89 } else {
90 i = 0;
91 for (domain = domain_list(); domain; domain = domain->next) {
92 state->domains[i++].domain = domain;
96 for (i=0; i<state->num_domains; i++) {
97 struct winbindd_list_users_domstate *d = &state->domains[i];
99 d->subreq = wb_query_user_list_send(
100 state->domains, ev, d->domain);
101 if (tevent_req_nomem(d->subreq, req)) {
102 TALLOC_FREE(state->domains);
103 return tevent_req_post(req, ev);
105 tevent_req_set_callback(d->subreq, winbindd_list_users_done,
106 req);
108 state->num_received = 0;
109 return req;
112 static void winbindd_list_users_done(struct tevent_req *subreq)
114 struct tevent_req *req = tevent_req_callback_data(
115 subreq, struct tevent_req);
116 struct winbindd_list_users_state *state = tevent_req_data(
117 req, struct winbindd_list_users_state);
118 struct winbindd_list_users_domstate *d;
119 NTSTATUS status;
120 size_t i;
122 for (i=0; i<state->num_domains; i++) {
123 if (subreq == state->domains[i].subreq) {
124 break;
127 if (i == state->num_domains) {
128 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
129 return;
132 d = &state->domains[i];
134 status = wb_query_user_list_recv(subreq, state->domains,
135 &d->users);
136 TALLOC_FREE(subreq);
137 if (!NT_STATUS_IS_OK(status)) {
139 * Just skip this domain
141 d->users = NULL;
144 state->num_received += 1;
146 if (state->num_received >= state->num_domains) {
147 tevent_req_done(req);
151 NTSTATUS winbindd_list_users_recv(struct tevent_req *req,
152 struct winbindd_response *response)
154 struct winbindd_list_users_state *state = tevent_req_data(
155 req, struct winbindd_list_users_state);
156 NTSTATUS status;
157 char *result;
158 size_t i, len;
160 if (tevent_req_is_nterror(req, &status)) {
161 return status;
164 result = NULL;
166 for (i=0; i<state->num_domains; i++) {
167 struct winbindd_list_users_domstate *d = &state->domains[i];
168 int ret;
170 if (d->users == NULL) {
171 continue;
174 ret = strv_append(state, &result, d->users);
175 if (ret != 0) {
176 return map_nt_error_from_unix(ret);
180 len = talloc_get_size(result);
182 response->extra_data.data = talloc_steal(response, result);
183 response->length += len;
184 response->data.num_entries = 0;
186 if (result != NULL && len >= 1) {
187 len -= 1;
188 response->data.num_entries = 1;
190 for (i=0; i<len; i++) {
191 if (result[i] == '\0') {
192 result[i] = ',';
193 response->data.num_entries += 1;
198 return NT_STATUS_OK;