WHATSNEW: Add release notes for Samba 4.9.17.
[Samba.git] / source3 / winbindd / winbindd_list_users.c
blobfcbe8beff6f394aa34f96bfd5e96809f193df912
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 /* Ensure null termination */
57 request->domain_name[sizeof(request->domain_name)-1]='\0';
59 DEBUG(3, ("list_users %s\n", request->domain_name));
61 if (request->domain_name[0] != '\0') {
62 state->num_domains = 1;
63 } else {
64 state->num_domains = 0;
65 for (domain = domain_list(); domain; domain = domain->next) {
66 state->num_domains += 1;
70 state->domains = talloc_array(state,
71 struct winbindd_list_users_domstate,
72 state->num_domains);
73 if (tevent_req_nomem(state->domains, req)) {
74 return tevent_req_post(req, ev);
77 if (request->domain_name[0] != '\0') {
78 state->domains[0].domain = find_domain_from_name_noinit(
79 request->domain_name);
80 if (state->domains[0].domain == NULL) {
81 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
82 return tevent_req_post(req, ev);
84 } else {
85 i = 0;
86 for (domain = domain_list(); domain; domain = domain->next) {
87 state->domains[i++].domain = domain;
91 for (i=0; i<state->num_domains; i++) {
92 struct winbindd_list_users_domstate *d = &state->domains[i];
94 d->subreq = wb_query_user_list_send(
95 state->domains, ev, d->domain);
96 if (tevent_req_nomem(d->subreq, req)) {
97 TALLOC_FREE(state->domains);
98 return tevent_req_post(req, ev);
100 tevent_req_set_callback(d->subreq, winbindd_list_users_done,
101 req);
103 state->num_received = 0;
104 return req;
107 static void winbindd_list_users_done(struct tevent_req *subreq)
109 struct tevent_req *req = tevent_req_callback_data(
110 subreq, struct tevent_req);
111 struct winbindd_list_users_state *state = tevent_req_data(
112 req, struct winbindd_list_users_state);
113 struct winbindd_list_users_domstate *d;
114 NTSTATUS status;
115 size_t i;
117 for (i=0; i<state->num_domains; i++) {
118 if (subreq == state->domains[i].subreq) {
119 break;
122 if (i == state->num_domains) {
123 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
124 return;
127 d = &state->domains[i];
129 status = wb_query_user_list_recv(subreq, state->domains,
130 &d->users);
131 TALLOC_FREE(subreq);
132 if (!NT_STATUS_IS_OK(status)) {
134 * Just skip this domain
136 d->users = NULL;
139 state->num_received += 1;
141 if (state->num_received >= state->num_domains) {
142 tevent_req_done(req);
146 NTSTATUS winbindd_list_users_recv(struct tevent_req *req,
147 struct winbindd_response *response)
149 struct winbindd_list_users_state *state = tevent_req_data(
150 req, struct winbindd_list_users_state);
151 NTSTATUS status;
152 char *result;
153 size_t i, len;
155 if (tevent_req_is_nterror(req, &status)) {
156 return status;
159 result = NULL;
161 for (i=0; i<state->num_domains; i++) {
162 struct winbindd_list_users_domstate *d = &state->domains[i];
163 int ret;
165 if (d->users == NULL) {
166 continue;
169 ret = strv_append(state, &result, d->users);
170 if (ret != 0) {
171 return map_nt_error_from_unix(ret);
175 len = talloc_get_size(result);
177 response->extra_data.data = talloc_steal(response, result);
178 response->length += len;
179 response->data.num_entries = 0;
181 if (result != NULL && len >= 1) {
182 len -= 1;
183 response->data.num_entries = 1;
185 for (i=0; i<len; i++) {
186 if (result[i] == '\0') {
187 result[i] = ',';
188 response->data.num_entries += 1;
193 return NT_STATUS_OK;