4ea528bf0f52ee61631ad1a532f7bc374af3febb
[samba.git] / source4 / winbind / wb_async_helpers.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Volker Lendecke 2005
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   a composite API for finding a DC and its name
21 */
22
23 #include "includes.h"
24 #include <tevent.h>
25 #include "libcli/composite/composite.h"
26 #include "winbind/wb_async_helpers.h"
27
28 #include "libcli/security/security.h"
29 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 #include "librpc/gen_ndr/ndr_samr_c.h"
31
32 #include "winbind/wb_helper.h"
33
34
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;
44 };
45
46 static void lsa_lookupsids_recv_names(struct tevent_req *subreq);
47
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)
54 {
55         struct composite_context *result;
56         struct lsa_lookupsids_state *state;
57         uint32_t i;
58         struct tevent_req *subreq;
59
60         result = composite_create(mem_ctx, ev);
61         if (result == NULL) goto failed;
62
63         state = talloc(result, struct lsa_lookupsids_state);
64         if (state == NULL) goto failed;
65         result->private_data = state;
66         state->ctx = result;
67
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;
71
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;
76         }
77
78         state->domains = talloc(state, struct lsa_RefDomainList);
79         if (state->domains == NULL) goto failed;
80
81         state->count = 0;
82         state->num_sids = num_sids;
83         state->names.count = 0;
84         state->names.names = NULL;
85
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;
94
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);
100
101         return result;
102
103  failed:
104         talloc_free(result);
105         return NULL;
106 }
107
108 static void lsa_lookupsids_recv_names(struct tevent_req *subreq)
109 {
110         struct lsa_lookupsids_state *state =
111                 tevent_req_callback_data(subreq,
112                 struct lsa_lookupsids_state);
113         uint32_t i;
114
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;
123         }
124
125         if (state->names.count != state->num_sids) {
126                 composite_error(state->ctx,
127                                 NT_STATUS_INVALID_NETWORK_RESPONSE);
128                 return;
129         }
130
131         state->result = talloc_array(state, struct wb_sid_object *,
132                                      state->num_sids);
133         if (composite_nomem(state->result, state->ctx)) return;
134
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;
141
142                 state->result[i] = talloc_zero(state->result,
143                                                struct wb_sid_object);
144                 if (composite_nomem(state->result[i], state->ctx)) return;
145
146                 state->result[i]->type = name->sid_type;
147                 if (state->result[i]->type == SID_NAME_UNKNOWN) {
148                         continue;
149                 }
150
151                 if (domains == NULL) {
152                         composite_error(state->ctx,
153                                         NT_STATUS_INVALID_NETWORK_RESPONSE);
154                         return;
155                 }
156                 if (name->sid_index >= domains->count) {
157                         composite_error(state->ctx,
158                                         NT_STATUS_INVALID_NETWORK_RESPONSE);
159                         return;
160                 }
161
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);
173                 }
174
175                 if (composite_nomem(state->result[i]->name, state->ctx)) {
176                         return;
177                 }
178         }
179
180         composite_done(state->ctx);
181 }
182
183 NTSTATUS wb_lsa_lookupsids_recv(struct composite_context *c,
184                                 TALLOC_CTX *mem_ctx,
185                                 struct wb_sid_object ***names)
186 {
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);
193         }
194         talloc_free(c);
195         return status;
196 }
197
198
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;
207 };
208
209 static void lsa_lookupnames_recv_sids(struct tevent_req *subreq);
210
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)
217 {
218         struct composite_context *result;
219         struct lsa_lookupnames_state *state;
220         struct tevent_req *subreq;
221
222         struct lsa_String *lsa_names;
223         uint32_t i;
224
225         result = composite_create(mem_ctx, ev);
226         if (result == NULL) goto failed;
227
228         state = talloc(result, struct lsa_lookupnames_state);
229         if (state == NULL) goto failed;
230         result->private_data = state;
231         state->ctx = result;
232
233         state->sids.count = 0;
234         state->sids.sids = NULL;
235         state->num_names = num_names;
236         state->count = 0;
237
238         lsa_names = talloc_array(state, struct lsa_String, num_names);
239         if (lsa_names == NULL) goto failed;
240
241         for (i=0; i<num_names; i++) {
242                 lsa_names[i].string = names[i];
243         }
244
245         state->domains = talloc(state, struct lsa_RefDomainList);
246         if (state->domains == NULL) goto failed;
247
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;
257
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);
263
264         return result;
265
266  failed:
267         talloc_free(result);
268         return NULL;
269 }
270
271 static void lsa_lookupnames_recv_sids(struct tevent_req *subreq)
272 {
273         struct lsa_lookupnames_state *state =
274                 tevent_req_callback_data(subreq,
275                 struct lsa_lookupnames_state);
276         uint32_t i;
277
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;
286         }
287
288         if (state->sids.count != state->num_names) {
289                 composite_error(state->ctx,
290                                 NT_STATUS_INVALID_NETWORK_RESPONSE);
291                 return;
292         }
293
294         state->result = talloc_array(state, struct wb_sid_object *,
295                                      state->num_names);
296         if (composite_nomem(state->result, state->ctx)) return;
297
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;
302
303                 state->result[i] = talloc_zero(state->result,
304                                                struct wb_sid_object);
305                 if (composite_nomem(state->result[i], state->ctx)) return;
306
307                 state->result[i]->type = sid->sid_type;
308                 if (state->result[i]->type == SID_NAME_UNKNOWN) {
309                         continue;
310                 }
311
312                 if (domains == NULL) {
313                         composite_error(state->ctx,
314                                         NT_STATUS_INVALID_NETWORK_RESPONSE);
315                         return;
316                 }
317                 if (sid->sid_index >= domains->count) {
318                         composite_error(state->ctx,
319                                         NT_STATUS_INVALID_NETWORK_RESPONSE);
320                         return;
321                 }
322
323                 dom = &domains->domains[sid->sid_index];
324
325                 state->result[i]->sid = dom_sid_add_rid(state->result[i],
326                                                         dom->sid, sid->rid);
327         }
328
329         composite_done(state->ctx);
330 }
331
332 NTSTATUS wb_lsa_lookupnames_recv(struct composite_context *c,
333                                  TALLOC_CTX *mem_ctx,
334                                  struct wb_sid_object ***sids)
335 {
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);
342         }
343         talloc_free(c);
344         return status;
345 }
346 struct samr_getuserdomgroups_state {
347         struct composite_context *ctx;
348         struct dcerpc_pipe *samr_pipe;
349
350         uint32_t num_rids;
351         uint32_t *rids;
352
353         struct samr_RidWithAttributeArray *rid_array;
354
355         struct policy_handle *user_handle;
356         struct samr_OpenUser o;
357         struct samr_GetGroupsForUser g;
358         struct samr_Close c;
359 };
360
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);
364
365 struct composite_context *wb_samr_userdomgroups_send(TALLOC_CTX *mem_ctx,
366                                                      struct dcerpc_pipe *samr_pipe,
367                                                      struct policy_handle *domain_handle,
368                                                      uint32_t rid)
369 {
370         struct composite_context *result;
371         struct samr_getuserdomgroups_state *state;
372         struct tevent_req *subreq;
373
374         result = composite_create(mem_ctx, samr_pipe->conn->event_ctx);
375         if (result == NULL) goto failed;
376
377         state = talloc(result, struct samr_getuserdomgroups_state);
378         if (state == NULL) goto failed;
379         result->private_data = state;
380         state->ctx = result;
381
382         state->samr_pipe = samr_pipe;
383
384         state->user_handle = talloc(state, struct policy_handle);
385         if (state->user_handle == NULL) goto failed;
386
387         state->o.in.domain_handle = domain_handle;
388         state->o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
389         state->o.in.rid = rid;
390         state->o.out.user_handle = state->user_handle;
391
392         subreq = dcerpc_samr_OpenUser_r_send(state,
393                                              result->event_ctx,
394                                              state->samr_pipe->binding_handle,
395                                              &state->o);
396         if (subreq == NULL) goto failed;
397         tevent_req_set_callback(subreq, samr_usergroups_recv_open, state);
398
399         return result;
400
401  failed:
402         talloc_free(result);
403         return NULL;
404 }
405                                               
406 static void samr_usergroups_recv_open(struct tevent_req *subreq)
407 {
408         struct samr_getuserdomgroups_state *state =
409                 tevent_req_callback_data(subreq,
410                 struct samr_getuserdomgroups_state);
411
412         state->ctx->status = dcerpc_samr_OpenUser_r_recv(subreq, state);
413         TALLOC_FREE(subreq);
414         if (!composite_is_ok(state->ctx)) return;
415         state->ctx->status = state->o.out.result;
416         if (!composite_is_ok(state->ctx)) return;
417
418         state->g.in.user_handle = state->user_handle;
419         state->g.out.rids = &state->rid_array;
420
421         subreq = dcerpc_samr_GetGroupsForUser_r_send(state,
422                                                      state->ctx->event_ctx,
423                                                      state->samr_pipe->binding_handle,
424                                                      &state->g);
425         if (composite_nomem(subreq, state->ctx)) return;
426         tevent_req_set_callback(subreq, samr_usergroups_recv_groups, state);
427 }
428
429 static void samr_usergroups_recv_groups(struct tevent_req *subreq)
430 {
431         struct samr_getuserdomgroups_state *state =
432                 tevent_req_callback_data(subreq,
433                 struct samr_getuserdomgroups_state);
434
435         state->ctx->status = dcerpc_samr_GetGroupsForUser_r_recv(subreq, state);
436         TALLOC_FREE(subreq);
437         if (!composite_is_ok(state->ctx)) return;
438         state->ctx->status = state->g.out.result;
439         if (!composite_is_ok(state->ctx)) return;
440
441         state->c.in.handle = state->user_handle;
442         state->c.out.handle = state->user_handle;
443
444         subreq = dcerpc_samr_Close_r_send(state,
445                                           state->ctx->event_ctx,
446                                           state->samr_pipe->binding_handle,
447                                           &state->c);
448         if (composite_nomem(subreq, state->ctx)) return;
449         tevent_req_set_callback(subreq, samr_usergroups_recv_close, state);
450 }
451
452 static void samr_usergroups_recv_close(struct tevent_req *subreq)
453 {
454         struct samr_getuserdomgroups_state *state =
455                 tevent_req_callback_data(subreq,
456                 struct samr_getuserdomgroups_state);
457
458         state->ctx->status = dcerpc_samr_Close_r_recv(subreq, state);
459         TALLOC_FREE(subreq);
460         if (!composite_is_ok(state->ctx)) return;
461         state->ctx->status = state->c.out.result;
462         if (!composite_is_ok(state->ctx)) return;
463
464         composite_done(state->ctx);
465 }
466
467 NTSTATUS wb_samr_userdomgroups_recv(struct composite_context *ctx,
468                                     TALLOC_CTX *mem_ctx,
469                                     uint32_t *num_rids, uint32_t **rids)
470 {
471         struct samr_getuserdomgroups_state *state =
472                 talloc_get_type(ctx->private_data,
473                                 struct samr_getuserdomgroups_state);
474
475         uint32_t i;
476         NTSTATUS status = composite_wait(ctx);
477         if (!NT_STATUS_IS_OK(status)) goto done;
478
479         *num_rids = state->rid_array->count;
480         *rids = talloc_array(mem_ctx, uint32_t, *num_rids);
481         if (*rids == NULL) {
482                 status = NT_STATUS_NO_MEMORY;
483                 goto done;
484         }
485
486         for (i=0; i<*num_rids; i++) {
487                 (*rids)[i] = state->rid_array->rids[i].rid;
488         }
489
490  done:
491         talloc_free(ctx);
492         return status;
493 }