330a6b4534c2ddc545284f85c8c7b1b290739f76
[ira/wip.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /*
21   a composite API for finding a DC and its name
22 */
23
24 #include "includes.h"
25 #include "libcli/composite/composite.h"
26 #include "libcli/smb_composite/smb_composite.h"
27 #include "winbind/wb_async_helpers.h"
28 #include "winbind/wb_server.h"
29 #include "smbd/service_stream.h"
30
31 #include "librpc/gen_ndr/nbt.h"
32 #include "librpc/gen_ndr/samr.h"
33 #include "lib/messaging/irpc.h"
34 #include "librpc/gen_ndr/irpc.h"
35 #include "librpc/gen_ndr/ndr_irpc.h"
36 #include "libcli/raw/libcliraw.h"
37 #include "librpc/gen_ndr/ndr_netlogon.h"
38 #include "librpc/gen_ndr/ndr_lsa.h"
39 #include "libcli/auth/credentials.h"
40
41 struct finddcs_state {
42         struct composite_context *ctx;
43         struct messaging_context *msg_ctx;
44
45         const char *domain_name;
46         const struct dom_sid *domain_sid;
47
48         struct nbtd_getdcname r;
49
50         int num_dcs;
51         struct nbt_dc_name *dcs;
52 };
53
54 static void finddcs_resolve(struct composite_context *ctx);
55 static void finddcs_getdc(struct irpc_request *ireq);
56
57 struct composite_context *wb_finddcs_send(TALLOC_CTX *mem_ctx,
58                                           const char *domain_name,
59                                           const struct dom_sid *domain_sid,
60                                           struct event_context *event_ctx,
61                                           struct messaging_context *msg_ctx)
62 {
63         struct composite_context *result, *ctx;
64         struct finddcs_state *state;
65         struct nbt_name name;
66
67         result = talloc(mem_ctx, struct composite_context);
68         if (result == NULL) goto failed;
69         result->state = COMPOSITE_STATE_IN_PROGRESS;
70         result->async.fn = NULL;
71         result->event_ctx = event_ctx;
72
73         state = talloc(result, struct finddcs_state);
74         if (state == NULL) goto failed;
75         state->ctx = result;
76         result->private_data = state;
77
78         state->domain_name = talloc_strdup(state, domain_name);
79         if (state->domain_name == NULL) goto failed;
80         state->domain_sid = dom_sid_dup(state, domain_sid);
81         if (state->domain_sid == NULL) goto failed;
82         state->msg_ctx = msg_ctx;
83
84         make_nbt_name(&name, state->domain_name, 0x1c);
85         ctx = resolve_name_send(&name, result->event_ctx,
86                                 lp_name_resolve_order());
87
88         if (ctx == NULL) goto failed;
89         ctx->async.fn = finddcs_resolve;
90         ctx->async.private_data = state;
91
92         return result;
93
94 failed:
95         talloc_free(result);
96         return NULL;
97 }
98
99 static void finddcs_resolve(struct composite_context *ctx)
100 {
101         struct finddcs_state *state =
102                 talloc_get_type(ctx->async.private_data, struct finddcs_state);
103         struct irpc_request *ireq;
104         uint32_t *nbt_servers;
105         const char *address;
106
107         state->ctx->status = resolve_name_recv(ctx, state, &address);
108         if (!composite_is_ok(state->ctx)) return;
109
110         state->num_dcs = 1;
111         state->dcs = talloc_array(state, struct nbt_dc_name, state->num_dcs);
112         if (composite_nomem(state->dcs, state->ctx)) return;
113
114         state->dcs[0].address = talloc_steal(state->dcs, address);
115
116         nbt_servers = irpc_servers_byname(state->msg_ctx, "nbt_server");
117         if ((nbt_servers == NULL) || (nbt_servers[0] == 0)) {
118                 composite_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
119                 return;
120         }
121
122         state->r.in.domainname = state->domain_name;
123         state->r.in.ip_address = state->dcs[0].address;
124         state->r.in.my_computername = lp_netbios_name();
125         state->r.in.my_accountname = talloc_asprintf(state, "%s$",
126                                                      lp_netbios_name());
127         if (composite_nomem(state->r.in.my_accountname, state->ctx)) return;
128         state->r.in.account_control = ACB_WSTRUST;
129         state->r.in.domain_sid = dom_sid_dup(state, state->domain_sid);
130         if (composite_nomem(state->r.in.domain_sid, state->ctx)) return;
131
132         ireq = irpc_call_send(state->msg_ctx, nbt_servers[0],
133                               &dcerpc_table_irpc, DCERPC_NBTD_GETDCNAME,
134                               &state->r, state);
135         composite_continue_irpc(state->ctx, ireq, finddcs_getdc, state);
136 }
137
138 static void finddcs_getdc(struct irpc_request *ireq)
139 {
140         struct finddcs_state *state =
141                 talloc_get_type(ireq->async.private, struct finddcs_state);
142
143         state->ctx->status = irpc_call_recv(ireq);
144         if (!composite_is_ok(state->ctx)) return;
145
146         state->dcs[0].name = talloc_steal(state->dcs, state->r.out.dcname);
147         composite_done(state->ctx);
148 }
149
150 NTSTATUS wb_finddcs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
151                          int *num_dcs, struct nbt_dc_name **dcs)
152 {
153         NTSTATUS status = composite_wait(c);
154         if (NT_STATUS_IS_OK(status)) {
155                 struct finddcs_state *state =
156                         talloc_get_type(c->private_data, struct finddcs_state);
157                 *num_dcs = state->num_dcs;
158                 *dcs = talloc_steal(mem_ctx, state->dcs);
159         }
160         talloc_free(c);
161         return status;
162 }
163
164 NTSTATUS wb_finddcs(TALLOC_CTX *mem_ctx,
165                     const char *domain_name, const struct dom_sid *domain_sid,
166                     struct event_context *event_ctx,
167                     struct messaging_context *msg_ctx,
168                     int *num_dcs, struct nbt_dc_name **dcs)
169 {
170         struct composite_context *c = wb_finddcs_send(mem_ctx,
171                                                       domain_name, domain_sid,
172                                                       event_ctx, msg_ctx);
173         return wb_finddcs_recv(c, mem_ctx, num_dcs, dcs);
174 }
175
176 struct get_schannel_creds_state {
177         struct cli_credentials *wks_creds;
178         struct dcerpc_pipe *p;
179         struct netr_ServerReqChallenge r;
180
181         struct creds_CredentialState *creds_state;
182         struct netr_Credential netr_cred;
183         uint32_t negotiate_flags;
184         struct netr_ServerAuthenticate2 a;
185 };
186
187 static void get_schannel_creds_recv_anonbind(struct composite_context *creq);
188 static void get_schannel_creds_recv_auth(struct rpc_request *req);
189 static void get_schannel_creds_recv_chal(struct rpc_request *req);
190 static void get_schannel_creds_recv_pipe(struct composite_context *ctx);
191
192 struct composite_context *wb_get_schannel_creds_send(TALLOC_CTX *mem_ctx,
193                                                      struct cli_credentials *wks_creds,
194                                                      struct smbcli_tree *tree,
195                                                      struct event_context *ev)
196 {
197         struct composite_context *c, *creq;
198         struct get_schannel_creds_state *state;
199
200         c = talloc_zero(mem_ctx, struct composite_context);
201         if (c == NULL) return NULL;
202
203         state = talloc(c, struct get_schannel_creds_state);
204         if (state == NULL) {
205                 c->status = NT_STATUS_NO_MEMORY;
206                 goto failed;
207         }
208
209         c->state = COMPOSITE_STATE_IN_PROGRESS;
210         c->private_data = state;
211         c->event_ctx = ev;
212
213         state->wks_creds = wks_creds;
214
215         state->p = dcerpc_pipe_init(state, ev);
216         if (state->p == NULL) {
217                 c->status = NT_STATUS_NO_MEMORY;
218                 goto failed;
219         }
220
221         creq = dcerpc_pipe_open_smb_send(state->p->conn, tree, "\\netlogon");
222         if (creq == NULL) {
223                 c->status = NT_STATUS_NO_MEMORY;
224                 goto failed;
225         }
226
227         creq->async.fn = get_schannel_creds_recv_pipe;
228         creq->async.private_data = c;
229
230         return c;
231
232  failed:
233         composite_error(c, c->status);
234         return c;
235 }
236
237 static void get_schannel_creds_recv_pipe(struct composite_context *creq)
238 {
239         struct composite_context *c =
240                 talloc_get_type(creq->async.private_data,
241                                 struct composite_context);
242         struct get_schannel_creds_state *state =
243                 talloc_get_type(c->private_data,
244                                 struct get_schannel_creds_state);
245
246         c->status = dcerpc_pipe_open_smb_recv(creq);
247         if (!composite_is_ok(c)) return;
248
249         creq = dcerpc_bind_auth_none_send(state, state->p,
250                                                                           &dcerpc_table_netlogon);
251         composite_continue(c, creq, get_schannel_creds_recv_anonbind, c);
252 }
253
254 static void get_schannel_creds_recv_anonbind(struct composite_context *creq)
255 {
256         struct composite_context *c =
257                 talloc_get_type(creq->async.private_data,
258                                 struct composite_context);
259         struct get_schannel_creds_state *state =
260                 talloc_get_type(c->private_data,
261                                 struct get_schannel_creds_state);
262         struct rpc_request *req;
263
264         c->status = dcerpc_bind_auth_none_recv(creq);
265         if (!composite_is_ok(c)) return;
266
267         state->r.in.computer_name =
268                 cli_credentials_get_workstation(state->wks_creds);
269         state->r.in.server_name =
270                 talloc_asprintf(state, "\\\\%s",
271                                 dcerpc_server_name(state->p));
272         if (composite_nomem(state->r.in.server_name, c)) return;
273
274         state->r.in.credentials = talloc(state, struct netr_Credential);
275         if (composite_nomem(state->r.in.credentials, c)) return;
276
277         state->r.out.credentials = talloc(state, struct netr_Credential);
278         if (composite_nomem(state->r.out.credentials, c)) return;
279
280         generate_random_buffer(state->r.in.credentials->data,
281                                sizeof(state->r.in.credentials->data));
282
283         req = dcerpc_netr_ServerReqChallenge_send(state->p, state, &state->r);
284         composite_continue_rpc(c, req, get_schannel_creds_recv_chal, c);
285 }
286
287 static void get_schannel_creds_recv_chal(struct rpc_request *req)
288 {
289         struct composite_context *c =
290                 talloc_get_type(req->async.private,
291                                 struct composite_context);
292         struct get_schannel_creds_state *state =
293                 talloc_get_type(c->private_data,
294                                 struct get_schannel_creds_state);
295         const struct samr_Password *mach_pwd;
296
297         c->status = dcerpc_ndr_request_recv(req);
298         if (!composite_is_ok(c)) return;
299         c->status = state->r.out.result;
300         if (!composite_is_ok(c)) return;
301
302         state->creds_state = talloc(state, struct creds_CredentialState);
303         if (composite_nomem(state->creds_state, c)) return;
304
305         mach_pwd = cli_credentials_get_nt_hash(state->wks_creds, state);
306         if (composite_nomem(mach_pwd, c)) return;
307
308         state->negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
309
310         creds_client_init(state->creds_state, state->r.in.credentials,
311                           state->r.out.credentials, mach_pwd,
312                           &state->netr_cred, state->negotiate_flags);
313
314         state->a.in.server_name =
315                 talloc_reference(state, state->r.in.server_name);
316         state->a.in.account_name =
317                 cli_credentials_get_username(state->wks_creds);
318         state->a.in.secure_channel_type =
319                 cli_credentials_get_secure_channel_type(state->wks_creds);
320         state->a.in.computer_name =
321                 cli_credentials_get_workstation(state->wks_creds);
322         state->a.in.negotiate_flags = &state->negotiate_flags;
323         state->a.out.negotiate_flags = &state->negotiate_flags;
324         state->a.in.credentials = &state->netr_cred;
325         state->a.out.credentials = &state->netr_cred;
326
327         req = dcerpc_netr_ServerAuthenticate2_send(state->p, state, &state->a);
328         composite_continue_rpc(c, req, get_schannel_creds_recv_auth, c);
329 }
330
331 static void get_schannel_creds_recv_auth(struct rpc_request *req)
332 {
333         struct composite_context *c =
334                 talloc_get_type(req->async.private,
335                                 struct composite_context);
336         struct get_schannel_creds_state *state =
337                 talloc_get_type(c->private_data,
338                                 struct get_schannel_creds_state);
339
340         c->status = dcerpc_ndr_request_recv(req);
341         if (!composite_is_ok(c)) return;
342         c->status = state->a.out.result;
343         if (!composite_is_ok(c)) return;
344
345         if (!creds_client_check(state->creds_state,
346                                 state->a.out.credentials)) {
347                 DEBUG(5, ("Server got us invalid creds\n"));
348                 composite_error(c, NT_STATUS_UNSUCCESSFUL);
349                 return;
350         }
351
352         cli_credentials_set_netlogon_creds(state->wks_creds,
353                                            state->creds_state);
354
355         composite_done(c);
356 }
357
358 NTSTATUS wb_get_schannel_creds_recv(struct composite_context *c,
359                                     TALLOC_CTX *mem_ctx,
360                                     struct dcerpc_pipe **netlogon_pipe)
361 {
362         NTSTATUS status = composite_wait(c);
363         if (NT_STATUS_IS_OK(status)) {
364                 struct get_schannel_creds_state *state =
365                         talloc_get_type(c->private_data,
366                                         struct get_schannel_creds_state);
367                 *netlogon_pipe = talloc_steal(mem_ctx, state->p);
368         }
369         talloc_free(c);
370         return status;
371 }
372
373 NTSTATUS wb_get_schannel_creds(TALLOC_CTX *mem_ctx,
374                                struct cli_credentials *wks_creds,
375                                struct smbcli_tree *tree,
376                                struct event_context *event_ctx,
377                                struct dcerpc_pipe **netlogon_pipe)
378 {
379         struct composite_context *c =
380                 wb_get_schannel_creds_send(mem_ctx, wks_creds, tree,
381                                            event_ctx);
382         return wb_get_schannel_creds_recv(c, mem_ctx, netlogon_pipe);
383 }
384
385 struct lsa_lookupsids_state {
386         struct composite_context *ctx;
387         int num_sids;
388         struct lsa_LookupSids r;
389         struct lsa_SidArray sids;
390         struct lsa_TransNameArray names;
391         uint32_t count;
392         struct wb_sid_object **result;
393 };
394
395 static void lsa_lookupsids_recv_names(struct rpc_request *req);
396
397 struct composite_context *wb_lsa_lookupsids_send(TALLOC_CTX *mem_ctx,
398                                                  struct dcerpc_pipe *lsa_pipe,
399                                                  struct policy_handle *handle,
400                                                  int num_sids,
401                                                  const struct dom_sid **sids)
402 {
403         struct composite_context *result;
404         struct rpc_request *req;
405         struct lsa_lookupsids_state *state;
406         int i;
407
408         result = talloc(mem_ctx, struct composite_context);
409         if (result == NULL) goto failed;
410         result->state = COMPOSITE_STATE_IN_PROGRESS;
411         result->async.fn = NULL;
412         result->event_ctx = lsa_pipe->conn->event_ctx;
413
414         state = talloc(result, struct lsa_lookupsids_state);
415         if (state == NULL) goto failed;
416         result->private_data = state;
417         state->ctx = result;
418
419         state->sids.num_sids = num_sids;
420         state->sids.sids = talloc_array(state, struct lsa_SidPtr, num_sids);
421         if (state->sids.sids == NULL) goto failed;
422
423         for (i=0; i<num_sids; i++) {
424                 state->sids.sids[i].sid = dom_sid_dup(state->sids.sids,
425                                                       sids[i]);
426                 if (state->sids.sids[i].sid == NULL) goto failed;
427         }
428
429         state->count = 0;
430         state->num_sids = num_sids;
431         state->names.count = 0;
432         state->names.names = NULL;
433
434         state->r.in.handle = handle;
435         state->r.in.sids = &state->sids;
436         state->r.in.names = &state->names;
437         state->r.in.level = 1;
438         state->r.in.count = &state->count;
439         state->r.out.names = &state->names;
440         state->r.out.count = &state->count;
441
442         req = dcerpc_lsa_LookupSids_send(lsa_pipe, state, &state->r);
443         if (req == NULL) goto failed;
444
445         req->async.callback = lsa_lookupsids_recv_names;
446         req->async.private = state;
447         return result;
448
449  failed:
450         talloc_free(result);
451         return NULL;
452 }
453
454 static void lsa_lookupsids_recv_names(struct rpc_request *req)
455 {
456         struct lsa_lookupsids_state *state =
457                 talloc_get_type(req->async.private,
458                                 struct lsa_lookupsids_state);
459         int i;
460
461         state->ctx->status = dcerpc_ndr_request_recv(req);
462         if (!composite_is_ok(state->ctx)) return;
463         state->ctx->status = state->r.out.result;
464         if (!NT_STATUS_IS_OK(state->ctx->status) &&
465             !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
466                 composite_error(state->ctx, state->ctx->status);
467                 return;
468         }
469
470         state->result = talloc_array(state, struct wb_sid_object *,
471                                      state->num_sids);
472         if (composite_nomem(state->result, state->ctx)) return;
473
474         for (i=0; i<state->num_sids; i++) {
475                 struct lsa_TranslatedName *name =
476                         &state->r.out.names->names[i];
477                 struct lsa_TrustInformation *dom;
478
479                 state->result[i] = talloc_zero(state->result,
480                                                struct wb_sid_object);
481                 if (composite_nomem(state->result[i], state->ctx)) return;
482
483                 state->result[i]->type = name->sid_type;
484                 if (state->result[i]->type == SID_NAME_UNKNOWN) {
485                         continue;
486                 }
487
488                 if (name->sid_index >= state->r.out.domains->count) {
489                         composite_error(state->ctx,
490                                         NT_STATUS_INVALID_PARAMETER);
491                         return;
492                 }
493
494                 dom = &state->r.out.domains->domains[name->sid_index];
495                 state->result[i]->domain = talloc_reference(state->result[i],
496                                                             dom->name.string);
497                 if ((name->sid_type == SID_NAME_DOMAIN) ||
498                     (name->name.string == NULL)) {
499                         state->result[i]->name =
500                                 talloc_strdup(state->result[i], "");
501                 } else {
502                         state->result[i]->name =
503                                 talloc_steal(state->result[i],
504                                              name->name.string);
505                 }
506
507                 if (composite_nomem(state->result[i]->name, state->ctx)) {
508                         return;
509                 }
510         }
511
512         composite_done(state->ctx);
513 }
514
515 NTSTATUS wb_lsa_lookupsids_recv(struct composite_context *c,
516                                 TALLOC_CTX *mem_ctx,
517                                 struct wb_sid_object ***names)
518 {
519         NTSTATUS status = composite_wait(c);
520         if (NT_STATUS_IS_OK(status)) {
521                 struct lsa_lookupsids_state *state =
522                         talloc_get_type(c->private_data,
523                                         struct lsa_lookupsids_state);
524                 *names = talloc_steal(mem_ctx, state->result);
525         }
526         talloc_free(c);
527         return status;
528 }
529
530 NTSTATUS wb_lsa_lookupsids(TALLOC_CTX *mem_ctx,
531                            struct dcerpc_pipe *lsa_pipe,
532                            struct policy_handle *handle,
533                            int num_sids, const struct dom_sid **sids,
534                            struct wb_sid_object ***names)
535 {
536         struct composite_context *c =
537                 wb_lsa_lookupsids_send(mem_ctx, lsa_pipe, handle,
538                                        num_sids, sids);
539         return wb_lsa_lookupnames_recv(c, mem_ctx, names);
540 }
541
542                            
543
544 struct lsa_lookupnames_state {
545         struct composite_context *ctx;
546         uint32_t num_names;
547         struct lsa_LookupNames r;
548         struct lsa_TransSidArray sids;
549         uint32_t count;
550         struct wb_sid_object **result;
551 };
552
553 static void lsa_lookupnames_recv_sids(struct rpc_request *req);
554
555 struct composite_context *wb_lsa_lookupnames_send(TALLOC_CTX *mem_ctx,
556                                                   struct dcerpc_pipe *lsa_pipe,
557                                                   struct policy_handle *handle,
558                                                   int num_names,
559                                                   const char **names)
560 {
561         struct composite_context *result;
562         struct rpc_request *req;
563         struct lsa_lookupnames_state *state;
564
565         struct lsa_String *lsa_names;
566         int i;
567
568         result = talloc(mem_ctx, struct composite_context);
569         if (result == NULL) goto failed;
570         result->state = COMPOSITE_STATE_IN_PROGRESS;
571         result->async.fn = NULL;
572         result->event_ctx = lsa_pipe->conn->event_ctx;
573
574         state = talloc(result, struct lsa_lookupnames_state);
575         if (state == NULL) goto failed;
576         result->private_data = state;
577         state->ctx = result;
578
579         state->sids.count = 0;
580         state->sids.sids = NULL;
581         state->num_names = num_names;
582         state->count = 0;
583
584         lsa_names = talloc_array(state, struct lsa_String, num_names);
585         if (lsa_names == NULL) goto failed;
586
587         for (i=0; i<num_names; i++) {
588                 lsa_names[i].string = names[i];
589         }
590
591         state->r.in.handle = handle;
592         state->r.in.num_names = num_names;
593         state->r.in.names = lsa_names;
594         state->r.in.sids = &state->sids;
595         state->r.in.level = 1;
596         state->r.in.count = &state->count;
597         state->r.out.count = &state->count;
598         state->r.out.sids = &state->sids;
599
600         req = dcerpc_lsa_LookupNames_send(lsa_pipe, state, &state->r);
601         if (req == NULL) goto failed;
602
603         req->async.callback = lsa_lookupnames_recv_sids;
604         req->async.private = state;
605         return result;
606
607  failed:
608         talloc_free(result);
609         return NULL;
610 }
611
612 static void lsa_lookupnames_recv_sids(struct rpc_request *req)
613 {
614         struct lsa_lookupnames_state *state =
615                 talloc_get_type(req->async.private,
616                                 struct lsa_lookupnames_state);
617         int i;
618
619         state->ctx->status = dcerpc_ndr_request_recv(req);
620         if (!composite_is_ok(state->ctx)) return;
621         state->ctx->status = state->r.out.result;
622         if (!NT_STATUS_IS_OK(state->ctx->status) &&
623             !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
624                 composite_error(state->ctx, state->ctx->status);
625                 return;
626         }
627
628         state->result = talloc_array(state, struct wb_sid_object *,
629                                      state->num_names);
630         if (composite_nomem(state->result, state->ctx)) return;
631
632         for (i=0; i<state->num_names; i++) {
633                 struct lsa_TranslatedSid *sid = &state->r.out.sids->sids[i];
634                 struct lsa_TrustInformation *dom;
635
636                 state->result[i] = talloc_zero(state->result,
637                                                struct wb_sid_object);
638                 if (composite_nomem(state->result[i], state->ctx)) return;
639
640                 state->result[i]->type = sid->sid_type;
641                 if (state->result[i]->type == SID_NAME_UNKNOWN) {
642                         continue;
643                 }
644
645                 if (sid->sid_index >= state->r.out.domains->count) {
646                         composite_error(state->ctx,
647                                         NT_STATUS_INVALID_PARAMETER);
648                         return;
649                 }
650
651                 dom = &state->r.out.domains->domains[sid->sid_index];
652
653                 state->result[i]->sid = dom_sid_add_rid(state->result[i],
654                                                         dom->sid, sid->rid);
655         }
656
657         composite_done(state->ctx);
658 }
659
660 NTSTATUS wb_lsa_lookupnames_recv(struct composite_context *c,
661                                  TALLOC_CTX *mem_ctx,
662                                  struct wb_sid_object ***sids)
663 {
664         NTSTATUS status = composite_wait(c);
665         if (NT_STATUS_IS_OK(status)) {
666                 struct lsa_lookupnames_state *state =
667                         talloc_get_type(c->private_data,
668                                         struct lsa_lookupnames_state);
669                 *sids = talloc_steal(mem_ctx, state->result);
670         }
671         talloc_free(c);
672         return status;
673 }
674
675 NTSTATUS wb_lsa_lookupnames(TALLOC_CTX *mem_ctx,
676                             struct dcerpc_pipe *lsa_pipe, 
677                             struct policy_handle *handle,
678                             int num_names, const char **names,
679                             struct wb_sid_object ***sids)
680 {
681         struct composite_context *c =
682                 wb_lsa_lookupnames_send(mem_ctx, lsa_pipe, handle,
683                                         num_names, names);
684         return wb_lsa_lookupnames_recv(c, mem_ctx, sids);
685 }
686
687 #if 0
688
689 struct cmd_checkmachacc_state {
690         struct composite_context *ctx;
691         struct wbsrv_call *call;
692         struct wbsrv_domain *domain;
693 };
694
695 static void cmd_checkmachacc_recv_init(struct composite_context *ctx);
696
697 struct composite_context *wb_cmd_checkmachacc_send(struct wbsrv_call *call)
698 {
699         struct composite_context *result, *ctx;
700         struct cmd_checkmachacc_state *state;
701         struct wbsrv_service *service = call->wbconn->listen_socket->service;
702
703         result = talloc(call, struct composite_context);
704         if (result == NULL) goto failed;
705         result->state = COMPOSITE_STATE_IN_PROGRESS;
706         result->async.fn = NULL;
707         result->event_ctx = call->event_ctx;
708
709         state = talloc(result, struct cmd_checkmachacc_state);
710         if (state == NULL) goto failed;
711         state->ctx = result;
712         result->private_data = state;
713         state->call = call;
714
715         state->domain = service->domains;
716
717         ctx = wb_init_domain_send(service, state->domain);
718         if (ctx == NULL) goto failed;
719         ctx->async.fn = cmd_checkmachacc_recv_init;
720         ctx->async.private_data = state;
721
722         return result;
723
724  failed:
725         talloc_free(result);
726         return NULL;
727 }
728
729 static void cmd_checkmachacc_recv_init(struct composite_context *ctx)
730 {
731         struct cmd_checkmachacc_state *state =
732                 talloc_get_type(ctx->async.private_data,
733                                 struct cmd_checkmachacc_state);
734
735         state->ctx->status = wb_init_domain_recv(ctx);
736         if (!composite_is_ok(state->ctx)) return;
737
738         composite_done(state->ctx);
739 }
740
741 NTSTATUS wb_cmd_checkmachacc_recv(struct composite_context *c)
742 {
743         NTSTATUS status = composite_wait(c);
744         talloc_free(c);
745         return status;
746 }
747
748 NTSTATUS wb_cmd_checkmachacc(struct wbsrv_call *call)
749 {
750         struct composite_context *c = wb_cmd_checkmachacc_send(call);
751         return wb_cmd_checkmachacc_recv(c);
752 }
753 #endif
754
755 struct samr_getuserdomgroups_state {
756         struct composite_context *ctx;
757         struct dcerpc_pipe *samr_pipe;
758
759         int num_rids;
760         uint32_t *rids;
761
762         struct policy_handle *user_handle;
763         struct samr_OpenUser o;
764         struct samr_GetGroupsForUser g;
765         struct samr_Close c;
766 };
767
768 static void samr_usergroups_recv_open(struct rpc_request *req);
769 static void samr_usergroups_recv_groups(struct rpc_request *req);
770 static void samr_usergroups_recv_close(struct rpc_request *req);
771
772 struct composite_context *wb_samr_userdomgroups_send(TALLOC_CTX *mem_ctx,
773                                                      struct dcerpc_pipe *samr_pipe,
774                                                      struct policy_handle *domain_handle,
775                                                      uint32_t rid)
776 {
777         struct composite_context *result;
778         struct rpc_request *req;
779         struct samr_getuserdomgroups_state *state;
780
781         result = talloc(mem_ctx, struct composite_context);
782         if (result == NULL) goto failed;
783         result->state = COMPOSITE_STATE_IN_PROGRESS;
784         result->async.fn = NULL;
785         result->event_ctx = samr_pipe->conn->event_ctx;
786
787         state = talloc(result, struct samr_getuserdomgroups_state);
788         if (state == NULL) goto failed;
789         result->private_data = state;
790         state->ctx = result;
791
792         state->samr_pipe = samr_pipe;
793
794         state->user_handle = talloc(state, struct policy_handle);
795         if (state->user_handle == NULL) goto failed;
796
797         state->o.in.domain_handle = domain_handle;
798         state->o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
799         state->o.in.rid = rid;
800         state->o.out.user_handle = state->user_handle;
801
802         req = dcerpc_samr_OpenUser_send(state->samr_pipe, state, &state->o);
803         if (req == NULL) goto failed;
804
805         req->async.callback = samr_usergroups_recv_open;
806         req->async.private = state;
807         return result;
808
809  failed:
810         talloc_free(result);
811         return NULL;
812 }
813                                               
814 static void samr_usergroups_recv_open(struct rpc_request *req)
815 {
816         struct samr_getuserdomgroups_state *state =
817                 talloc_get_type(req->async.private,
818                                 struct samr_getuserdomgroups_state);
819
820         state->ctx->status = dcerpc_ndr_request_recv(req);
821         if (!composite_is_ok(state->ctx)) return;
822         state->ctx->status = state->o.out.result;
823         if (!composite_is_ok(state->ctx)) return;
824
825         state->g.in.user_handle = state->user_handle;
826
827         req = dcerpc_samr_GetGroupsForUser_send(state->samr_pipe, state,
828                                                 &state->g);
829         composite_continue_rpc(state->ctx, req, samr_usergroups_recv_groups,
830                                state);
831 }
832
833 static void samr_usergroups_recv_groups(struct rpc_request *req)
834 {
835         struct samr_getuserdomgroups_state *state =
836                 talloc_get_type(req->async.private,
837                                 struct samr_getuserdomgroups_state);
838
839         state->ctx->status = dcerpc_ndr_request_recv(req);
840         if (!composite_is_ok(state->ctx)) return;
841         state->ctx->status = state->g.out.result;
842         if (!composite_is_ok(state->ctx)) return;
843
844         state->c.in.handle = state->user_handle;
845         state->c.out.handle = state->user_handle;
846
847         req = dcerpc_samr_Close_send(state->samr_pipe, state, &state->c);
848         composite_continue_rpc(state->ctx, req, samr_usergroups_recv_close,
849                                state);
850 }
851
852 static void samr_usergroups_recv_close(struct rpc_request *req)
853 {
854         struct samr_getuserdomgroups_state *state =
855                 talloc_get_type(req->async.private,
856                                 struct samr_getuserdomgroups_state);
857
858         state->ctx->status = dcerpc_ndr_request_recv(req);
859         if (!composite_is_ok(state->ctx)) return;
860         state->ctx->status = state->c.out.result;
861         if (!composite_is_ok(state->ctx)) return;
862
863         composite_done(state->ctx);
864 }
865
866 NTSTATUS wb_samr_userdomgroups_recv(struct composite_context *ctx,
867                                     TALLOC_CTX *mem_ctx,
868                                     int *num_rids, uint32_t **rids)
869 {
870         struct samr_getuserdomgroups_state *state =
871                 talloc_get_type(ctx->private_data,
872                                 struct samr_getuserdomgroups_state);
873
874         int i;
875         NTSTATUS status = composite_wait(ctx);
876         if (!NT_STATUS_IS_OK(status)) goto done;
877
878         *num_rids = state->g.out.rids->count;
879         *rids = talloc_array(mem_ctx, uint32_t, *num_rids);
880         if (*rids == NULL) {
881                 status = NT_STATUS_NO_MEMORY;
882                 goto done;
883         }
884
885         for (i=0; i<*num_rids; i++) {
886                 (*rids)[i] = state->g.out.rids->rids[i].rid;
887         }
888
889  done:
890         talloc_free(ctx);
891         return status;
892 }