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