s3:winbindd: talloc_steal the extra_data in winbindd_list_users_recv()
[sfrench/samba-autobuild/.git] / source3 / winbindd / winbindd_list_users.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_LIST_USERS
4    Copyright (C) Volker Lendecke 2009
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "lib/util/strv.h"
24
25 struct winbindd_list_users_domstate {
26         struct tevent_req *subreq;
27         struct winbindd_domain *domain;
28         char *users;
29 };
30
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;
36 };
37
38 static void winbindd_list_users_done(struct tevent_req *subreq);
39
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)
44 {
45         struct tevent_req *req;
46         struct winbindd_list_users_state *state;
47         struct winbindd_domain *domain;
48         size_t i;
49
50         req = tevent_req_create(mem_ctx, &state,
51                                 struct winbindd_list_users_state);
52         if (req == NULL) {
53                 return NULL;
54         }
55
56         /* Ensure null termination */
57         request->domain_name[sizeof(request->domain_name)-1]='\0';
58
59         DEBUG(3, ("list_users %s\n", request->domain_name));
60
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;
67                 }
68         }
69
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);
75         }
76
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);
83                 }
84         } else {
85                 i = 0;
86                 for (domain = domain_list(); domain; domain = domain->next) {
87                         state->domains[i++].domain = domain;
88                 }
89         }
90
91         for (i=0; i<state->num_domains; i++) {
92                 struct winbindd_list_users_domstate *d = &state->domains[i];
93
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);
99                 }
100                 tevent_req_set_callback(d->subreq, winbindd_list_users_done,
101                                         req);
102         }
103         state->num_received = 0;
104         return req;
105 }
106
107 static void winbindd_list_users_done(struct tevent_req *subreq)
108 {
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;
116
117         for (i=0; i<state->num_domains; i++) {
118                 if (subreq == state->domains[i].subreq) {
119                         break;
120                 }
121         }
122         if (i == state->num_domains) {
123                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
124                 return;
125         }
126
127         d = &state->domains[i];
128
129         status = wb_query_user_list_recv(subreq, state->domains,
130                                          &d->users);
131         TALLOC_FREE(subreq);
132         if (!NT_STATUS_IS_OK(status)) {
133                 /*
134                  * Just skip this domain
135                  */
136                 d->users = NULL;
137         }
138
139         state->num_received += 1;
140
141         if (state->num_received >= state->num_domains) {
142                 tevent_req_done(req);
143         }
144 }
145
146 NTSTATUS winbindd_list_users_recv(struct tevent_req *req,
147                                   struct winbindd_response *response)
148 {
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;
154
155         if (tevent_req_is_nterror(req, &status)) {
156                 return status;
157         }
158
159         result = NULL;
160
161         for (i=0; i<state->num_domains; i++) {
162                 struct winbindd_list_users_domstate *d = &state->domains[i];
163                 int ret;
164
165                 if (d->users == NULL) {
166                         continue;
167                 }
168
169                 ret = strv_append(state, &result, d->users);
170                 if (ret != 0) {
171                         return map_nt_error_from_unix(ret);
172                 }
173         }
174
175         len = talloc_get_size(result);
176
177         response->extra_data.data = talloc_steal(response, result);
178         response->length += len;
179         response->data.num_entries = 0;
180
181         if (len >= 1) {
182                 len -= 1;
183                 response->data.num_entries = 1;
184
185                 for (i=0; i<len; i++) {
186                         if (result[i] == '\0') {
187                                 result[i] = ',';
188                                 response->data.num_entries += 1;
189                         }
190                 }
191         }
192
193         return NT_STATUS_OK;
194 }