buildtools: Rename perl vendorarch configure option.
[Samba/wip.git] / source4 / winbind / wb_async_helpers.c
blob5f0455a50e48cb27c875f28fb040ca56c882892e
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2005
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 a composite API for finding a DC and its name
23 #include "includes.h"
24 #include <tevent.h>
25 #include "libcli/composite/composite.h"
26 #include "winbind/wb_async_helpers.h"
28 #include "libcli/security/security.h"
29 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 #include "librpc/gen_ndr/ndr_samr_c.h"
32 #include "winbind/wb_helper.h"
35 struct lsa_lookupsids_state {
36 struct composite_context *ctx;
37 uint32_t num_sids;
38 struct lsa_LookupSids r;
39 struct lsa_SidArray sids;
40 struct lsa_TransNameArray names;
41 struct lsa_RefDomainList *domains;
42 uint32_t count;
43 struct wb_sid_object **result;
46 static void lsa_lookupsids_recv_names(struct tevent_req *subreq);
48 struct composite_context *wb_lsa_lookupsids_send(TALLOC_CTX *mem_ctx,
49 struct tevent_context *ev,
50 struct dcerpc_binding_handle *lsa_binding,
51 struct policy_handle *handle,
52 uint32_t num_sids,
53 const struct dom_sid **sids)
55 struct composite_context *result;
56 struct lsa_lookupsids_state *state;
57 uint32_t i;
58 struct tevent_req *subreq;
60 result = composite_create(mem_ctx, ev);
61 if (result == NULL) goto failed;
63 state = talloc(result, struct lsa_lookupsids_state);
64 if (state == NULL) goto failed;
65 result->private_data = state;
66 state->ctx = result;
68 state->sids.num_sids = num_sids;
69 state->sids.sids = talloc_array(state, struct lsa_SidPtr, num_sids);
70 if (state->sids.sids == NULL) goto failed;
72 for (i=0; i<num_sids; i++) {
73 state->sids.sids[i].sid = dom_sid_dup(state->sids.sids,
74 sids[i]);
75 if (state->sids.sids[i].sid == NULL) goto failed;
78 state->domains = talloc(state, struct lsa_RefDomainList);
79 if (state->domains == NULL) goto failed;
81 state->count = 0;
82 state->num_sids = num_sids;
83 state->names.count = 0;
84 state->names.names = NULL;
86 state->r.in.handle = handle;
87 state->r.in.sids = &state->sids;
88 state->r.in.names = &state->names;
89 state->r.in.level = 1;
90 state->r.in.count = &state->count;
91 state->r.out.names = &state->names;
92 state->r.out.count = &state->count;
93 state->r.out.domains = &state->domains;
95 subreq = dcerpc_lsa_LookupSids_r_send(state, ev,
96 lsa_binding,
97 &state->r);
98 if (subreq == NULL) goto failed;
99 tevent_req_set_callback(subreq, lsa_lookupsids_recv_names, state);
101 return result;
103 failed:
104 talloc_free(result);
105 return NULL;
108 static void lsa_lookupsids_recv_names(struct tevent_req *subreq)
110 struct lsa_lookupsids_state *state =
111 tevent_req_callback_data(subreq,
112 struct lsa_lookupsids_state);
113 uint32_t i;
115 state->ctx->status = dcerpc_lsa_LookupSids_r_recv(subreq, state);
116 TALLOC_FREE(subreq);
117 if (!composite_is_ok(state->ctx)) return;
118 state->ctx->status = state->r.out.result;
119 if (!NT_STATUS_IS_OK(state->ctx->status) &&
120 !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
121 composite_error(state->ctx, state->ctx->status);
122 return;
125 if (state->names.count != state->num_sids) {
126 composite_error(state->ctx,
127 NT_STATUS_INVALID_NETWORK_RESPONSE);
128 return;
131 state->result = talloc_array(state, struct wb_sid_object *,
132 state->num_sids);
133 if (composite_nomem(state->result, state->ctx)) return;
135 for (i=0; i<state->num_sids; i++) {
136 struct lsa_TranslatedName *name =
137 &state->r.out.names->names[i];
138 struct lsa_DomainInfo *dom;
139 struct lsa_RefDomainList *domains =
140 state->domains;
142 state->result[i] = talloc_zero(state->result,
143 struct wb_sid_object);
144 if (composite_nomem(state->result[i], state->ctx)) return;
146 state->result[i]->type = name->sid_type;
147 if (state->result[i]->type == SID_NAME_UNKNOWN) {
148 continue;
151 if (domains == NULL) {
152 composite_error(state->ctx,
153 NT_STATUS_INVALID_NETWORK_RESPONSE);
154 return;
156 if (name->sid_index >= domains->count) {
157 composite_error(state->ctx,
158 NT_STATUS_INVALID_NETWORK_RESPONSE);
159 return;
162 dom = &domains->domains[name->sid_index];
163 state->result[i]->domain = talloc_reference(state->result[i],
164 dom->name.string);
165 if ((name->sid_type == SID_NAME_DOMAIN) ||
166 (name->name.string == NULL)) {
167 state->result[i]->name =
168 talloc_strdup(state->result[i], "");
169 } else {
170 state->result[i]->name =
171 talloc_steal(state->result[i],
172 name->name.string);
175 if (composite_nomem(state->result[i]->name, state->ctx)) {
176 return;
180 composite_done(state->ctx);
183 NTSTATUS wb_lsa_lookupsids_recv(struct composite_context *c,
184 TALLOC_CTX *mem_ctx,
185 struct wb_sid_object ***names)
187 NTSTATUS status = composite_wait(c);
188 if (NT_STATUS_IS_OK(status)) {
189 struct lsa_lookupsids_state *state =
190 talloc_get_type(c->private_data,
191 struct lsa_lookupsids_state);
192 *names = talloc_steal(mem_ctx, state->result);
194 talloc_free(c);
195 return status;
199 struct lsa_lookupnames_state {
200 struct composite_context *ctx;
201 uint32_t num_names;
202 struct lsa_LookupNames r;
203 struct lsa_TransSidArray sids;
204 struct lsa_RefDomainList *domains;
205 uint32_t count;
206 struct wb_sid_object **result;
209 static void lsa_lookupnames_recv_sids(struct tevent_req *subreq);
211 struct composite_context *wb_lsa_lookupnames_send(TALLOC_CTX *mem_ctx,
212 struct tevent_context *ev,
213 struct dcerpc_binding_handle *lsa_binding,
214 struct policy_handle *handle,
215 uint32_t num_names,
216 const char **names)
218 struct composite_context *result;
219 struct lsa_lookupnames_state *state;
220 struct tevent_req *subreq;
222 struct lsa_String *lsa_names;
223 uint32_t i;
225 result = composite_create(mem_ctx, ev);
226 if (result == NULL) goto failed;
228 state = talloc(result, struct lsa_lookupnames_state);
229 if (state == NULL) goto failed;
230 result->private_data = state;
231 state->ctx = result;
233 state->sids.count = 0;
234 state->sids.sids = NULL;
235 state->num_names = num_names;
236 state->count = 0;
238 lsa_names = talloc_array(state, struct lsa_String, num_names);
239 if (lsa_names == NULL) goto failed;
241 for (i=0; i<num_names; i++) {
242 lsa_names[i].string = names[i];
245 state->domains = talloc(state, struct lsa_RefDomainList);
246 if (state->domains == NULL) goto failed;
248 state->r.in.handle = handle;
249 state->r.in.num_names = num_names;
250 state->r.in.names = lsa_names;
251 state->r.in.sids = &state->sids;
252 state->r.in.level = 1;
253 state->r.in.count = &state->count;
254 state->r.out.count = &state->count;
255 state->r.out.sids = &state->sids;
256 state->r.out.domains = &state->domains;
258 subreq = dcerpc_lsa_LookupNames_r_send(state, ev,
259 lsa_binding,
260 &state->r);
261 if (subreq == NULL) goto failed;
262 tevent_req_set_callback(subreq, lsa_lookupnames_recv_sids, state);
264 return result;
266 failed:
267 talloc_free(result);
268 return NULL;
271 static void lsa_lookupnames_recv_sids(struct tevent_req *subreq)
273 struct lsa_lookupnames_state *state =
274 tevent_req_callback_data(subreq,
275 struct lsa_lookupnames_state);
276 uint32_t i;
278 state->ctx->status = dcerpc_lsa_LookupNames_r_recv(subreq, state);
279 TALLOC_FREE(subreq);
280 if (!composite_is_ok(state->ctx)) return;
281 state->ctx->status = state->r.out.result;
282 if (!NT_STATUS_IS_OK(state->ctx->status) &&
283 !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
284 composite_error(state->ctx, state->ctx->status);
285 return;
288 if (state->sids.count != state->num_names) {
289 composite_error(state->ctx,
290 NT_STATUS_INVALID_NETWORK_RESPONSE);
291 return;
294 state->result = talloc_array(state, struct wb_sid_object *,
295 state->num_names);
296 if (composite_nomem(state->result, state->ctx)) return;
298 for (i=0; i<state->num_names; i++) {
299 struct lsa_TranslatedSid *sid = &state->r.out.sids->sids[i];
300 struct lsa_RefDomainList *domains = state->domains;
301 struct lsa_DomainInfo *dom;
303 state->result[i] = talloc_zero(state->result,
304 struct wb_sid_object);
305 if (composite_nomem(state->result[i], state->ctx)) return;
307 state->result[i]->type = sid->sid_type;
308 if (state->result[i]->type == SID_NAME_UNKNOWN) {
309 continue;
312 if (domains == NULL) {
313 composite_error(state->ctx,
314 NT_STATUS_INVALID_NETWORK_RESPONSE);
315 return;
317 if (sid->sid_index >= domains->count) {
318 composite_error(state->ctx,
319 NT_STATUS_INVALID_NETWORK_RESPONSE);
320 return;
323 dom = &domains->domains[sid->sid_index];
325 state->result[i]->sid = dom_sid_add_rid(state->result[i],
326 dom->sid, sid->rid);
329 composite_done(state->ctx);
332 NTSTATUS wb_lsa_lookupnames_recv(struct composite_context *c,
333 TALLOC_CTX *mem_ctx,
334 struct wb_sid_object ***sids)
336 NTSTATUS status = composite_wait(c);
337 if (NT_STATUS_IS_OK(status)) {
338 struct lsa_lookupnames_state *state =
339 talloc_get_type(c->private_data,
340 struct lsa_lookupnames_state);
341 *sids = talloc_steal(mem_ctx, state->result);
343 talloc_free(c);
344 return status;
346 struct samr_getuserdomgroups_state {
347 struct composite_context *ctx;
348 struct dcerpc_binding_handle *samr_binding;
350 uint32_t num_rids;
351 uint32_t *rids;
353 struct samr_RidWithAttributeArray *rid_array;
355 struct policy_handle *user_handle;
356 struct samr_OpenUser o;
357 struct samr_GetGroupsForUser g;
358 struct samr_Close c;
361 static void samr_usergroups_recv_open(struct tevent_req *subreq);
362 static void samr_usergroups_recv_groups(struct tevent_req *subreq);
363 static void samr_usergroups_recv_close(struct tevent_req *subreq);
365 struct composite_context *wb_samr_userdomgroups_send(TALLOC_CTX *mem_ctx,
366 struct tevent_context *ev,
367 struct dcerpc_binding_handle *samr_binding,
368 struct policy_handle *domain_handle,
369 uint32_t rid)
371 struct composite_context *result;
372 struct samr_getuserdomgroups_state *state;
373 struct tevent_req *subreq;
375 result = composite_create(mem_ctx, ev);
376 if (result == NULL) goto failed;
378 state = talloc(result, struct samr_getuserdomgroups_state);
379 if (state == NULL) goto failed;
380 result->private_data = state;
381 state->ctx = result;
383 state->samr_binding = samr_binding;
385 state->user_handle = talloc(state, struct policy_handle);
386 if (state->user_handle == NULL) goto failed;
388 state->o.in.domain_handle = domain_handle;
389 state->o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
390 state->o.in.rid = rid;
391 state->o.out.user_handle = state->user_handle;
393 subreq = dcerpc_samr_OpenUser_r_send(state,
394 state->ctx->event_ctx,
395 state->samr_binding,
396 &state->o);
397 if (subreq == NULL) goto failed;
398 tevent_req_set_callback(subreq, samr_usergroups_recv_open, state);
400 return result;
402 failed:
403 talloc_free(result);
404 return NULL;
407 static void samr_usergroups_recv_open(struct tevent_req *subreq)
409 struct samr_getuserdomgroups_state *state =
410 tevent_req_callback_data(subreq,
411 struct samr_getuserdomgroups_state);
413 state->ctx->status = dcerpc_samr_OpenUser_r_recv(subreq, state);
414 TALLOC_FREE(subreq);
415 if (!composite_is_ok(state->ctx)) return;
416 state->ctx->status = state->o.out.result;
417 if (!composite_is_ok(state->ctx)) return;
419 state->g.in.user_handle = state->user_handle;
420 state->g.out.rids = &state->rid_array;
422 subreq = dcerpc_samr_GetGroupsForUser_r_send(state,
423 state->ctx->event_ctx,
424 state->samr_binding,
425 &state->g);
426 if (composite_nomem(subreq, state->ctx)) return;
427 tevent_req_set_callback(subreq, samr_usergroups_recv_groups, state);
430 static void samr_usergroups_recv_groups(struct tevent_req *subreq)
432 struct samr_getuserdomgroups_state *state =
433 tevent_req_callback_data(subreq,
434 struct samr_getuserdomgroups_state);
436 state->ctx->status = dcerpc_samr_GetGroupsForUser_r_recv(subreq, state);
437 TALLOC_FREE(subreq);
438 if (!composite_is_ok(state->ctx)) return;
439 state->ctx->status = state->g.out.result;
440 if (!composite_is_ok(state->ctx)) return;
442 state->c.in.handle = state->user_handle;
443 state->c.out.handle = state->user_handle;
445 subreq = dcerpc_samr_Close_r_send(state,
446 state->ctx->event_ctx,
447 state->samr_binding,
448 &state->c);
449 if (composite_nomem(subreq, state->ctx)) return;
450 tevent_req_set_callback(subreq, samr_usergroups_recv_close, state);
453 static void samr_usergroups_recv_close(struct tevent_req *subreq)
455 struct samr_getuserdomgroups_state *state =
456 tevent_req_callback_data(subreq,
457 struct samr_getuserdomgroups_state);
459 state->ctx->status = dcerpc_samr_Close_r_recv(subreq, state);
460 TALLOC_FREE(subreq);
461 if (!composite_is_ok(state->ctx)) return;
462 state->ctx->status = state->c.out.result;
463 if (!composite_is_ok(state->ctx)) return;
465 composite_done(state->ctx);
468 NTSTATUS wb_samr_userdomgroups_recv(struct composite_context *ctx,
469 TALLOC_CTX *mem_ctx,
470 uint32_t *num_rids, uint32_t **rids)
472 struct samr_getuserdomgroups_state *state =
473 talloc_get_type(ctx->private_data,
474 struct samr_getuserdomgroups_state);
476 uint32_t i;
477 NTSTATUS status = composite_wait(ctx);
478 if (!NT_STATUS_IS_OK(status)) goto done;
480 *num_rids = state->rid_array->count;
481 *rids = talloc_array(mem_ctx, uint32_t, *num_rids);
482 if (*rids == NULL) {
483 status = NT_STATUS_NO_MEMORY;
484 goto done;
487 for (i=0; i<*num_rids; i++) {
488 (*rids)[i] = state->rid_array->rids[i].rid;
491 done:
492 talloc_free(ctx);
493 return status;