s3-registry: fix upgrade code
[Samba/gebeck_regimport.git] / source4 / winbind / wb_async_helpers.c
blob2af8567fd4cfe1358d74238f4baa007a5557b4cb
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 dcerpc_pipe *lsa_pipe,
50 struct policy_handle *handle,
51 uint32_t num_sids,
52 const struct dom_sid **sids)
54 struct composite_context *result;
55 struct lsa_lookupsids_state *state;
56 uint32_t i;
57 struct tevent_req *subreq;
59 result = composite_create(mem_ctx, lsa_pipe->conn->event_ctx);
60 if (result == NULL) goto failed;
62 state = talloc(result, struct lsa_lookupsids_state);
63 if (state == NULL) goto failed;
64 result->private_data = state;
65 state->ctx = result;
67 state->sids.num_sids = num_sids;
68 state->sids.sids = talloc_array(state, struct lsa_SidPtr, num_sids);
69 if (state->sids.sids == NULL) goto failed;
71 for (i=0; i<num_sids; i++) {
72 state->sids.sids[i].sid = dom_sid_dup(state->sids.sids,
73 sids[i]);
74 if (state->sids.sids[i].sid == NULL) goto failed;
77 state->domains = talloc(state, struct lsa_RefDomainList);
78 if (state->domains == NULL) goto failed;
80 state->count = 0;
81 state->num_sids = num_sids;
82 state->names.count = 0;
83 state->names.names = NULL;
85 state->r.in.handle = handle;
86 state->r.in.sids = &state->sids;
87 state->r.in.names = &state->names;
88 state->r.in.level = 1;
89 state->r.in.count = &state->count;
90 state->r.out.names = &state->names;
91 state->r.out.count = &state->count;
92 state->r.out.domains = &state->domains;
94 subreq = dcerpc_lsa_LookupSids_r_send(state,
95 result->event_ctx,
96 lsa_pipe->binding_handle,
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 state->result = talloc_array(state, struct wb_sid_object *,
126 state->num_sids);
127 if (composite_nomem(state->result, state->ctx)) return;
129 for (i=0; i<state->num_sids; i++) {
130 struct lsa_TranslatedName *name =
131 &state->r.out.names->names[i];
132 struct lsa_DomainInfo *dom;
133 struct lsa_RefDomainList *domains =
134 state->domains;
136 state->result[i] = talloc_zero(state->result,
137 struct wb_sid_object);
138 if (composite_nomem(state->result[i], state->ctx)) return;
140 state->result[i]->type = name->sid_type;
141 if (state->result[i]->type == SID_NAME_UNKNOWN) {
142 continue;
145 if (name->sid_index >= domains->count) {
146 composite_error(state->ctx,
147 NT_STATUS_INVALID_PARAMETER);
148 return;
151 dom = &domains->domains[name->sid_index];
152 state->result[i]->domain = talloc_reference(state->result[i],
153 dom->name.string);
154 if ((name->sid_type == SID_NAME_DOMAIN) ||
155 (name->name.string == NULL)) {
156 state->result[i]->name =
157 talloc_strdup(state->result[i], "");
158 } else {
159 state->result[i]->name =
160 talloc_steal(state->result[i],
161 name->name.string);
164 if (composite_nomem(state->result[i]->name, state->ctx)) {
165 return;
169 composite_done(state->ctx);
172 NTSTATUS wb_lsa_lookupsids_recv(struct composite_context *c,
173 TALLOC_CTX *mem_ctx,
174 struct wb_sid_object ***names)
176 NTSTATUS status = composite_wait(c);
177 if (NT_STATUS_IS_OK(status)) {
178 struct lsa_lookupsids_state *state =
179 talloc_get_type(c->private_data,
180 struct lsa_lookupsids_state);
181 *names = talloc_steal(mem_ctx, state->result);
183 talloc_free(c);
184 return status;
188 struct lsa_lookupnames_state {
189 struct composite_context *ctx;
190 uint32_t num_names;
191 struct lsa_LookupNames r;
192 struct lsa_TransSidArray sids;
193 struct lsa_RefDomainList *domains;
194 uint32_t count;
195 struct wb_sid_object **result;
198 static void lsa_lookupnames_recv_sids(struct tevent_req *subreq);
200 struct composite_context *wb_lsa_lookupnames_send(TALLOC_CTX *mem_ctx,
201 struct dcerpc_pipe *lsa_pipe,
202 struct policy_handle *handle,
203 uint32_t num_names,
204 const char **names)
206 struct composite_context *result;
207 struct lsa_lookupnames_state *state;
208 struct tevent_req *subreq;
210 struct lsa_String *lsa_names;
211 uint32_t i;
213 result = composite_create(mem_ctx, lsa_pipe->conn->event_ctx);
214 if (result == NULL) goto failed;
216 state = talloc(result, struct lsa_lookupnames_state);
217 if (state == NULL) goto failed;
218 result->private_data = state;
219 state->ctx = result;
221 state->sids.count = 0;
222 state->sids.sids = NULL;
223 state->num_names = num_names;
224 state->count = 0;
226 lsa_names = talloc_array(state, struct lsa_String, num_names);
227 if (lsa_names == NULL) goto failed;
229 for (i=0; i<num_names; i++) {
230 lsa_names[i].string = names[i];
233 state->domains = talloc(state, struct lsa_RefDomainList);
234 if (state->domains == NULL) goto failed;
236 state->r.in.handle = handle;
237 state->r.in.num_names = num_names;
238 state->r.in.names = lsa_names;
239 state->r.in.sids = &state->sids;
240 state->r.in.level = 1;
241 state->r.in.count = &state->count;
242 state->r.out.count = &state->count;
243 state->r.out.sids = &state->sids;
244 state->r.out.domains = &state->domains;
246 subreq = dcerpc_lsa_LookupNames_r_send(state,
247 result->event_ctx,
248 lsa_pipe->binding_handle,
249 &state->r);
250 if (subreq == NULL) goto failed;
251 tevent_req_set_callback(subreq, lsa_lookupnames_recv_sids, state);
253 return result;
255 failed:
256 talloc_free(result);
257 return NULL;
260 static void lsa_lookupnames_recv_sids(struct tevent_req *subreq)
262 struct lsa_lookupnames_state *state =
263 tevent_req_callback_data(subreq,
264 struct lsa_lookupnames_state);
265 uint32_t i;
267 state->ctx->status = dcerpc_lsa_LookupNames_r_recv(subreq, state);
268 TALLOC_FREE(subreq);
269 if (!composite_is_ok(state->ctx)) return;
270 state->ctx->status = state->r.out.result;
271 if (!NT_STATUS_IS_OK(state->ctx->status) &&
272 !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
273 composite_error(state->ctx, state->ctx->status);
274 return;
277 state->result = talloc_array(state, struct wb_sid_object *,
278 state->num_names);
279 if (composite_nomem(state->result, state->ctx)) return;
281 for (i=0; i<state->num_names; i++) {
282 struct lsa_TranslatedSid *sid = &state->r.out.sids->sids[i];
283 struct lsa_RefDomainList *domains = state->domains;
284 struct lsa_DomainInfo *dom;
286 state->result[i] = talloc_zero(state->result,
287 struct wb_sid_object);
288 if (composite_nomem(state->result[i], state->ctx)) return;
290 state->result[i]->type = sid->sid_type;
291 if (state->result[i]->type == SID_NAME_UNKNOWN) {
292 continue;
295 if (sid->sid_index >= domains->count) {
296 composite_error(state->ctx,
297 NT_STATUS_INVALID_PARAMETER);
298 return;
301 dom = &domains->domains[sid->sid_index];
303 state->result[i]->sid = dom_sid_add_rid(state->result[i],
304 dom->sid, sid->rid);
307 composite_done(state->ctx);
310 NTSTATUS wb_lsa_lookupnames_recv(struct composite_context *c,
311 TALLOC_CTX *mem_ctx,
312 struct wb_sid_object ***sids)
314 NTSTATUS status = composite_wait(c);
315 if (NT_STATUS_IS_OK(status)) {
316 struct lsa_lookupnames_state *state =
317 talloc_get_type(c->private_data,
318 struct lsa_lookupnames_state);
319 *sids = talloc_steal(mem_ctx, state->result);
321 talloc_free(c);
322 return status;
324 struct samr_getuserdomgroups_state {
325 struct composite_context *ctx;
326 struct dcerpc_pipe *samr_pipe;
328 uint32_t num_rids;
329 uint32_t *rids;
331 struct samr_RidWithAttributeArray *rid_array;
333 struct policy_handle *user_handle;
334 struct samr_OpenUser o;
335 struct samr_GetGroupsForUser g;
336 struct samr_Close c;
339 static void samr_usergroups_recv_open(struct tevent_req *subreq);
340 static void samr_usergroups_recv_groups(struct tevent_req *subreq);
341 static void samr_usergroups_recv_close(struct tevent_req *subreq);
343 struct composite_context *wb_samr_userdomgroups_send(TALLOC_CTX *mem_ctx,
344 struct dcerpc_pipe *samr_pipe,
345 struct policy_handle *domain_handle,
346 uint32_t rid)
348 struct composite_context *result;
349 struct samr_getuserdomgroups_state *state;
350 struct tevent_req *subreq;
352 result = composite_create(mem_ctx, samr_pipe->conn->event_ctx);
353 if (result == NULL) goto failed;
355 state = talloc(result, struct samr_getuserdomgroups_state);
356 if (state == NULL) goto failed;
357 result->private_data = state;
358 state->ctx = result;
360 state->samr_pipe = samr_pipe;
362 state->user_handle = talloc(state, struct policy_handle);
363 if (state->user_handle == NULL) goto failed;
365 state->o.in.domain_handle = domain_handle;
366 state->o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
367 state->o.in.rid = rid;
368 state->o.out.user_handle = state->user_handle;
370 subreq = dcerpc_samr_OpenUser_r_send(state,
371 result->event_ctx,
372 state->samr_pipe->binding_handle,
373 &state->o);
374 if (subreq == NULL) goto failed;
375 tevent_req_set_callback(subreq, samr_usergroups_recv_open, state);
377 return result;
379 failed:
380 talloc_free(result);
381 return NULL;
384 static void samr_usergroups_recv_open(struct tevent_req *subreq)
386 struct samr_getuserdomgroups_state *state =
387 tevent_req_callback_data(subreq,
388 struct samr_getuserdomgroups_state);
390 state->ctx->status = dcerpc_samr_OpenUser_r_recv(subreq, state);
391 TALLOC_FREE(subreq);
392 if (!composite_is_ok(state->ctx)) return;
393 state->ctx->status = state->o.out.result;
394 if (!composite_is_ok(state->ctx)) return;
396 state->g.in.user_handle = state->user_handle;
397 state->g.out.rids = &state->rid_array;
399 subreq = dcerpc_samr_GetGroupsForUser_r_send(state,
400 state->ctx->event_ctx,
401 state->samr_pipe->binding_handle,
402 &state->g);
403 if (composite_nomem(subreq, state->ctx)) return;
404 tevent_req_set_callback(subreq, samr_usergroups_recv_groups, state);
407 static void samr_usergroups_recv_groups(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_GetGroupsForUser_r_recv(subreq, state);
414 TALLOC_FREE(subreq);
415 if (!composite_is_ok(state->ctx)) return;
416 state->ctx->status = state->g.out.result;
417 if (!composite_is_ok(state->ctx)) return;
419 state->c.in.handle = state->user_handle;
420 state->c.out.handle = state->user_handle;
422 subreq = dcerpc_samr_Close_r_send(state,
423 state->ctx->event_ctx,
424 state->samr_pipe->binding_handle,
425 &state->c);
426 if (composite_nomem(subreq, state->ctx)) return;
427 tevent_req_set_callback(subreq, samr_usergroups_recv_close, state);
430 static void samr_usergroups_recv_close(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_Close_r_recv(subreq, state);
437 TALLOC_FREE(subreq);
438 if (!composite_is_ok(state->ctx)) return;
439 state->ctx->status = state->c.out.result;
440 if (!composite_is_ok(state->ctx)) return;
442 composite_done(state->ctx);
445 NTSTATUS wb_samr_userdomgroups_recv(struct composite_context *ctx,
446 TALLOC_CTX *mem_ctx,
447 uint32_t *num_rids, uint32_t **rids)
449 struct samr_getuserdomgroups_state *state =
450 talloc_get_type(ctx->private_data,
451 struct samr_getuserdomgroups_state);
453 uint32_t i;
454 NTSTATUS status = composite_wait(ctx);
455 if (!NT_STATUS_IS_OK(status)) goto done;
457 *num_rids = state->rid_array->count;
458 *rids = talloc_array(mem_ctx, uint32_t, *num_rids);
459 if (*rids == NULL) {
460 status = NT_STATUS_NO_MEMORY;
461 goto done;
464 for (i=0; i<*num_rids; i++) {
465 (*rids)[i] = state->rid_array->rids[i].rid;
468 done:
469 talloc_free(ctx);
470 return status;