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