r10852: Continuation-based programming can become a bit spaghetti...
[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->event_ctx = event_ctx;
70
71         state = talloc(result, struct finddcs_state);
72         if (state == NULL) goto failed;
73         state->ctx = result;
74         result->private_data = state;
75
76         state->domain_name = talloc_strdup(state, domain_name);
77         if (state->domain_name == NULL) goto failed;
78         state->domain_sid = dom_sid_dup(state, domain_sid);
79         if (state->domain_sid == NULL) goto failed;
80         state->msg_ctx = msg_ctx;
81
82         make_nbt_name(&name, state->domain_name, 0x1c);
83         ctx = resolve_name_send(&name, result->event_ctx,
84                                 lp_name_resolve_order());
85
86         if (ctx == NULL) goto failed;
87         ctx->async.fn = finddcs_resolve;
88         ctx->async.private_data = state;
89
90         return result;
91
92 failed:
93         talloc_free(result);
94         return NULL;
95 }
96
97 static void finddcs_resolve(struct composite_context *ctx)
98 {
99         struct finddcs_state *state =
100                 talloc_get_type(ctx->async.private_data, struct finddcs_state);
101         struct irpc_request *ireq;
102         uint32_t *nbt_servers;
103         const char *address;
104
105         state->ctx->status = resolve_name_recv(ctx, state, &address);
106         if (!comp_is_ok(state->ctx)) return;
107
108         state->num_dcs = 1;
109         state->dcs = talloc_array(state, struct nbt_dc_name, state->num_dcs);
110         if (comp_nomem(state->dcs, state->ctx)) return;
111
112         state->dcs[0].address = talloc_steal(state->dcs, address);
113
114         nbt_servers = irpc_servers_byname(state->msg_ctx, "nbt_server");
115         if ((nbt_servers == NULL) || (nbt_servers[0] == 0)) {
116                 comp_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
117                 return;
118         }
119
120         state->r.in.domainname = state->domain_name;
121         state->r.in.ip_address = state->dcs[0].address;
122         state->r.in.my_computername = lp_netbios_name();
123         state->r.in.my_accountname = talloc_asprintf(state, "%s$",
124                                                      lp_netbios_name());
125         if (comp_nomem(state->r.in.my_accountname, state->ctx)) return;
126         state->r.in.account_control = ACB_WSTRUST;
127         state->r.in.domain_sid = dom_sid_dup(state, state->domain_sid);
128         if (comp_nomem(state->r.in.domain_sid, state->ctx)) return;
129
130         ireq = irpc_call_send(state->msg_ctx, nbt_servers[0],
131                               &dcerpc_table_irpc, DCERPC_NBTD_GETDCNAME,
132                               &state->r, state);
133         irpc_cont(state->ctx, ireq, finddcs_getdc, state);
134 }
135
136 static void finddcs_getdc(struct irpc_request *ireq)
137 {
138         struct finddcs_state *state =
139                 talloc_get_type(ireq->async.private, struct finddcs_state);
140
141         state->ctx->status = irpc_call_recv(ireq);
142         if (!comp_is_ok(state->ctx)) return;
143
144         state->dcs[0].name = talloc_steal(state->dcs, state->r.out.dcname);
145         comp_done(state->ctx);
146 }
147
148 NTSTATUS wb_finddcs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
149                          int *num_dcs, struct nbt_dc_name **dcs)
150 {
151         NTSTATUS status =composite_wait(c);
152         if (NT_STATUS_IS_OK(status)) {
153                 struct finddcs_state *state =
154                         talloc_get_type(c->private_data, struct finddcs_state);
155                 *num_dcs = state->num_dcs;
156                 *dcs = talloc_steal(mem_ctx, state->dcs);
157         }
158         talloc_free(c);
159         return status;
160 }
161
162 NTSTATUS wb_finddcs(const char *domain_name, const struct dom_sid *domain_sid,
163                     struct event_context *event_ctx,
164                     struct messaging_context *msg_ctx,
165                     TALLOC_CTX *mem_ctx,
166                     int *num_dcs, struct nbt_dc_name **dcs)
167 {
168         struct composite_context *c = wb_finddcs_send(domain_name, domain_sid,
169                                                       event_ctx, msg_ctx);
170         return wb_finddcs_recv(c, mem_ctx, num_dcs, dcs);
171 }
172
173 struct get_schannel_creds_state {
174         struct composite_context *ctx;
175         struct cli_credentials *wks_creds;
176         struct dcerpc_pipe *p;
177         struct netr_ServerReqChallenge r;
178
179         struct creds_CredentialState *creds_state;
180         struct netr_Credential netr_cred;
181         uint32_t negotiate_flags;
182         struct netr_ServerAuthenticate2 a;
183 };
184
185 static void get_schannel_creds_recv_auth(struct rpc_request *req);
186 static void get_schannel_creds_recv_chal(struct rpc_request *req);
187 static void get_schannel_creds_recv_pipe(struct composite_context *ctx);
188
189 struct composite_context *wb_get_schannel_creds_send(struct cli_credentials *wks_creds,
190                                                      struct smbcli_tree *tree,
191                                                      struct event_context *ev)
192 {
193         struct composite_context *result, *ctx;
194         struct get_schannel_creds_state *state;
195
196         result = talloc_zero(NULL, struct composite_context);
197         if (result == NULL) goto failed;
198         result->state = COMPOSITE_STATE_IN_PROGRESS;
199         result->event_ctx = ev;
200
201         state = talloc(result, struct get_schannel_creds_state);
202         if (state == NULL) goto failed;
203         result->private_data = state;
204         state->ctx = result;
205
206         state->wks_creds = wks_creds;
207
208         state->p = dcerpc_pipe_init(state, ev);
209         if (state->p == NULL) goto failed;
210
211         ctx = dcerpc_pipe_open_smb_send(state->p->conn, tree, "\\netlogon");
212         if (ctx == NULL) goto failed;
213
214         ctx->async.fn = get_schannel_creds_recv_pipe;
215         ctx->async.private_data = state;
216         return result;
217
218  failed:
219         talloc_free(result);
220         return NULL;
221 }
222
223 static void get_schannel_creds_recv_pipe(struct composite_context *ctx)
224 {
225         struct get_schannel_creds_state *state =
226                 talloc_get_type(ctx->async.private_data,
227                                 struct get_schannel_creds_state);
228         struct rpc_request *req;
229
230         state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
231         if (!comp_is_ok(state->ctx)) return;
232
233         state->ctx->status = dcerpc_bind_auth_none(state->p,
234                                                    DCERPC_NETLOGON_UUID,
235                                                    DCERPC_NETLOGON_VERSION);
236         if (!comp_is_ok(state->ctx)) return;
237
238         state->r.in.computer_name =
239                 cli_credentials_get_workstation(state->wks_creds);
240         state->r.in.server_name =
241                 talloc_asprintf(state, "\\\\%s",
242                                 dcerpc_server_name(state->p));
243         if (comp_nomem(state->r.in.server_name, state->ctx)) return;
244
245         state->r.in.credentials = talloc(state, struct netr_Credential);
246         if (comp_nomem(state->r.in.credentials, state->ctx)) return;
247
248         state->r.out.credentials = talloc(state, struct netr_Credential);
249         if (comp_nomem(state->r.out.credentials, state->ctx)) return;
250
251         generate_random_buffer(state->r.in.credentials->data,
252                                sizeof(state->r.in.credentials->data));
253
254         req = dcerpc_netr_ServerReqChallenge_send(state->p, state, &state->r);
255         rpc_cont(state->ctx, req, get_schannel_creds_recv_chal, state);
256 }
257
258 static void get_schannel_creds_recv_chal(struct rpc_request *req)
259 {
260         struct get_schannel_creds_state *state =
261                 talloc_get_type(req->async.private,
262                                 struct get_schannel_creds_state);
263         const struct samr_Password *mach_pwd;
264
265         state->ctx->status = dcerpc_ndr_request_recv(req);
266         if (!comp_is_ok(state->ctx)) return;
267         state->ctx->status = state->r.out.result;
268         if (!comp_is_ok(state->ctx)) return;
269
270         state->creds_state = talloc(state, struct creds_CredentialState);
271         if (comp_nomem(state->creds_state, state->ctx)) return;
272
273         mach_pwd = cli_credentials_get_nt_hash(state->wks_creds, state);
274         if (comp_nomem(mach_pwd, state->ctx)) return;
275
276         state->negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
277
278         creds_client_init(state->creds_state, state->r.in.credentials,
279                           state->r.out.credentials, mach_pwd,
280                           &state->netr_cred, state->negotiate_flags);
281
282         state->a.in.server_name =
283                 talloc_reference(state, state->r.in.server_name);
284         state->a.in.account_name =
285                 cli_credentials_get_username(state->wks_creds);
286         state->a.in.secure_channel_type =
287                 cli_credentials_get_secure_channel_type(state->wks_creds);
288         state->a.in.computer_name =
289                 cli_credentials_get_workstation(state->wks_creds);
290         state->a.in.negotiate_flags = &state->negotiate_flags;
291         state->a.out.negotiate_flags = &state->negotiate_flags;
292         state->a.in.credentials = &state->netr_cred;
293         state->a.out.credentials = &state->netr_cred;
294
295         req = dcerpc_netr_ServerAuthenticate2_send(state->p, state, &state->a);
296         rpc_cont(state->ctx, req, get_schannel_creds_recv_auth, state);
297 }
298
299 static void get_schannel_creds_recv_auth(struct rpc_request *req)
300 {
301         struct get_schannel_creds_state *state =
302                 talloc_get_type(req->async.private,
303                                 struct get_schannel_creds_state);
304
305         state->ctx->status = dcerpc_ndr_request_recv(req);
306         if (!NT_STATUS_IS_OK(state->ctx->status)) goto done;
307         state->ctx->status = state->a.out.result;
308         if (!NT_STATUS_IS_OK(state->ctx->status)) goto done;
309
310         if (!creds_client_check(state->creds_state,
311                                 state->a.out.credentials)) {
312                 DEBUG(5, ("Server got us invalid creds\n"));
313                 state->ctx->status = NT_STATUS_UNSUCCESSFUL;
314                 goto done;
315         }
316
317         cli_credentials_set_netlogon_creds(state->wks_creds, state->creds_state);
318
319         state->ctx->state = COMPOSITE_STATE_DONE;
320
321  done:
322         if (!NT_STATUS_IS_OK(state->ctx->status)) {
323                 state->ctx->state = COMPOSITE_STATE_ERROR;
324         }
325         if ((state->ctx->state >= COMPOSITE_STATE_DONE) &&
326             (state->ctx->async.fn != NULL)) {
327                 state->ctx->async.fn(state->ctx);
328         }
329 }
330
331 NTSTATUS wb_get_schannel_creds_recv(struct composite_context *c,
332                                     TALLOC_CTX *mem_ctx,
333                                     struct dcerpc_pipe **netlogon_pipe)
334 {
335         NTSTATUS status = composite_wait(c);
336         if (NT_STATUS_IS_OK(status)) {
337                 struct get_schannel_creds_state *state =
338                         talloc_get_type(c->private_data,
339                                         struct get_schannel_creds_state);
340                 *netlogon_pipe = talloc_steal(mem_ctx, state->p);
341         }
342         talloc_free(c);
343         return status;
344 }
345
346 NTSTATUS wb_get_schannel_creds(struct cli_credentials *wks_creds,
347                                struct smbcli_tree *tree,
348                                struct event_context *event_ctx,
349                                TALLOC_CTX *mem_ctx,
350                                struct dcerpc_pipe **netlogon_pipe)
351 {
352         struct composite_context *c =
353                 wb_get_schannel_creds_send(wks_creds, tree, event_ctx);
354         return wb_get_schannel_creds_recv(c, mem_ctx, netlogon_pipe);
355 }
356
357 struct get_lsa_pipe_state {
358         struct composite_context *ctx;
359         const char *domain_name;
360         const struct dom_sid *domain_sid;
361
362         struct smb_composite_connect conn;
363         struct dcerpc_pipe *lsa_pipe;
364
365         struct lsa_ObjectAttribute objectattr;
366         struct lsa_OpenPolicy2 openpolicy;
367         struct policy_handle policy_handle;
368
369         struct lsa_QueryInfoPolicy queryinfo;
370
371         struct lsa_Close close;
372 };
373
374 static void get_lsa_pipe_recv_dcs(struct composite_context *ctx);
375 static void get_lsa_pipe_recv_tree(struct composite_context *ctx);
376 static void get_lsa_pipe_recv_pipe(struct composite_context *ctx);
377 static void get_lsa_pipe_recv_openpol(struct rpc_request *req);
378 static void get_lsa_pipe_recv_queryinfo(struct rpc_request *req);
379 static void get_lsa_pipe_recv_close(struct rpc_request *req);
380
381 struct composite_context *wb_get_lsa_pipe_send(struct event_context *event_ctx,
382                                                struct messaging_context *msg_ctx,
383                                                const char *domain_name,
384                                                const struct dom_sid *domain_sid)
385 {
386         struct composite_context *result, *ctx;
387         struct get_lsa_pipe_state *state;
388
389         result = talloc_zero(NULL, struct composite_context);
390         if (result == NULL) goto failed;
391         result->state = COMPOSITE_STATE_IN_PROGRESS;
392         result->event_ctx = event_ctx;
393
394         state = talloc(result, struct get_lsa_pipe_state);
395         if (state == NULL) goto failed;
396         result->private_data = state;
397         state->ctx = result;
398
399         state->domain_name = domain_name;
400         state->domain_sid = domain_sid;
401
402         ctx = wb_finddcs_send(domain_name, domain_sid, event_ctx, msg_ctx);
403         if (ctx == NULL) goto failed;
404
405         ctx->async.fn = get_lsa_pipe_recv_dcs;
406         ctx->async.private_data = state;
407         return result;
408
409  failed:
410         talloc_free(result);
411         return NULL;
412 }
413
414 static void get_lsa_pipe_recv_dcs(struct composite_context *ctx)
415 {
416         struct get_lsa_pipe_state *state =
417                 talloc_get_type(ctx->async.private_data,
418                                 struct get_lsa_pipe_state);
419
420         int num_dcs;
421         struct nbt_dc_name *dcs;
422
423         state->ctx->status = wb_finddcs_recv(ctx, state, &num_dcs, &dcs);
424         if (!comp_is_ok(state->ctx)) return;
425
426         if (num_dcs < 1) {
427                 comp_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
428                 return;
429         }
430
431         state->conn.in.dest_host = dcs[0].address;
432         state->conn.in.port = 0;
433         state->conn.in.called_name = dcs[0].name;
434         state->conn.in.service = "IPC$";
435         state->conn.in.service_type = "IPC";
436         state->conn.in.workgroup = state->domain_name;
437
438         state->conn.in.credentials = cli_credentials_init(state);
439         if (comp_nomem(state->conn.in.credentials, state->ctx)) return;
440         cli_credentials_set_conf(state->conn.in.credentials);
441         cli_credentials_set_anonymous(state->conn.in.credentials);
442
443         ctx = smb_composite_connect_send(&state->conn, state, 
444                                          state->ctx->event_ctx);
445         comp_cont(state->ctx, ctx, get_lsa_pipe_recv_tree, state);
446 }
447
448 static void get_lsa_pipe_recv_tree(struct composite_context *ctx)
449 {
450         struct get_lsa_pipe_state *state =
451                 talloc_get_type(ctx->async.private_data,
452                                 struct get_lsa_pipe_state);
453
454         state->ctx->status = smb_composite_connect_recv(ctx, state);
455         if (!comp_is_ok(state->ctx)) return;
456
457         state->lsa_pipe = dcerpc_pipe_init(state, state->ctx->event_ctx);
458         if (comp_nomem(state->lsa_pipe, state->ctx)) return;
459
460         ctx = dcerpc_pipe_open_smb_send(state->lsa_pipe->conn,
461                                         state->conn.out.tree, "\\lsarpc");
462         comp_cont(state->ctx, ctx, get_lsa_pipe_recv_pipe, state);
463 }
464
465 static void get_lsa_pipe_recv_pipe(struct composite_context *ctx)
466 {
467         struct get_lsa_pipe_state *state =
468                 talloc_get_type(ctx->async.private_data,
469                                 struct get_lsa_pipe_state);
470         struct rpc_request *req;
471
472         state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
473         if (!comp_is_ok(state->ctx)) return;
474
475         talloc_unlink(state, state->conn.out.tree); /* The pipe owns it now */
476         state->conn.out.tree = NULL;
477
478         state->ctx->status = dcerpc_bind_auth_none(state->lsa_pipe,
479                                                    DCERPC_LSARPC_UUID,
480                                                    DCERPC_LSARPC_VERSION);
481         if (!comp_is_ok(state->ctx)) return;
482
483         state->openpolicy.in.system_name =
484                 talloc_asprintf(state, "\\\\%s",
485                                 dcerpc_server_name(state->lsa_pipe));
486         if (comp_nomem(state->openpolicy.in.system_name, state->ctx)) return;
487
488         ZERO_STRUCT(state->objectattr);
489         state->openpolicy.in.attr = &state->objectattr;
490         state->openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
491         state->openpolicy.out.handle = &state->policy_handle;
492
493         req = dcerpc_lsa_OpenPolicy2_send(state->lsa_pipe, state,
494                                           &state->openpolicy);
495         rpc_cont(state->ctx, req, get_lsa_pipe_recv_openpol, state);
496 }
497
498 static void get_lsa_pipe_recv_openpol(struct rpc_request *req)
499 {
500         struct get_lsa_pipe_state *state =
501                 talloc_get_type(req->async.private, struct get_lsa_pipe_state);
502
503         state->ctx->status = dcerpc_ndr_request_recv(req);
504         if (!comp_is_ok(state->ctx)) return;
505         state->ctx->status = state->openpolicy.out.result;
506         if (!comp_is_ok(state->ctx)) return;
507
508         state->queryinfo.in.handle = &state->policy_handle;
509         state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
510
511         req = dcerpc_lsa_QueryInfoPolicy_send(state->lsa_pipe, state,
512                                               &state->queryinfo);
513         rpc_cont(state->ctx, req, get_lsa_pipe_recv_queryinfo, state);
514 }
515
516 static void get_lsa_pipe_recv_queryinfo(struct rpc_request *req)
517 {
518         struct get_lsa_pipe_state *state =
519                 talloc_get_type(req->async.private, struct get_lsa_pipe_state);
520         struct lsa_DomainInfo *dominfo;
521
522         state->ctx->status = dcerpc_ndr_request_recv(req);
523         if (!comp_is_ok(state->ctx)) return;
524         state->ctx->status = state->queryinfo.out.result;
525         if (!comp_is_ok(state->ctx)) return;
526
527         dominfo = &state->queryinfo.out.info->account_domain;
528
529         if (strcasecmp(state->domain_name, dominfo->name.string) != 0) {
530                 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
531                           state->domain_name,
532                           dcerpc_server_name(state->lsa_pipe),
533                           dominfo->name.string));
534                 comp_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
535                 return;
536         }
537
538         if (!dom_sid_equal(state->domain_sid, dominfo->sid)) {
539                 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
540                           dom_sid_string(state, state->domain_sid),
541                           dcerpc_server_name(state->lsa_pipe),
542                           dom_sid_string(state, dominfo->sid)));
543                 comp_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
544                 return;
545         }
546
547         state->close.in.handle = &state->policy_handle;
548         state->close.out.handle = &state->policy_handle;
549
550         req = dcerpc_lsa_Close_send(state->lsa_pipe, state,
551                                     &state->close);
552         rpc_cont(state->ctx, req, get_lsa_pipe_recv_close, state);
553 }
554
555 static void get_lsa_pipe_recv_close(struct rpc_request *req)
556 {
557         struct get_lsa_pipe_state *state =
558                 talloc_get_type(req->async.private, struct get_lsa_pipe_state);
559
560         state->ctx->status = dcerpc_ndr_request_recv(req);
561         if (!comp_is_ok(state->ctx)) return;
562         state->ctx->status = state->close.out.result;
563         if (!comp_is_ok(state->ctx)) return;
564
565         comp_done(state->ctx);
566 }
567
568 NTSTATUS wb_get_lsa_pipe_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
569                               struct dcerpc_pipe **pipe)
570 {
571         NTSTATUS status = composite_wait(c);
572         if (NT_STATUS_IS_OK(status)) {
573                 struct get_lsa_pipe_state *state =
574                         talloc_get_type(c->private_data,
575                                         struct get_lsa_pipe_state);
576                 *pipe = talloc_steal(mem_ctx, state->lsa_pipe);
577         }
578         talloc_free(c);
579         return status;
580 }
581
582 NTSTATUS wb_get_lsa_pipe(struct event_context *event_ctx,
583                          struct messaging_context *msg_ctx,
584                          const char *domain_name,
585                          const struct dom_sid *domain_sid,
586                          TALLOC_CTX *mem_ctx,
587                          struct dcerpc_pipe **pipe)
588 {
589         struct composite_context *c =
590                 wb_get_lsa_pipe_send(event_ctx, msg_ctx, domain_name,
591                                      domain_sid);
592         return wb_get_lsa_pipe_recv(c, mem_ctx, pipe);
593 }
594
595 struct lsa_lookupnames_state {
596         struct composite_context *ctx;
597         uint32_t num_names;
598         struct lsa_LookupNames r;
599         struct lsa_TransSidArray sids;
600         uint32_t count;
601         struct wb_sid_object **result;
602 };
603
604 static void lsa_lookupnames_recv_sids(struct rpc_request *req);
605
606 struct composite_context *wb_lsa_lookupnames_send(struct dcerpc_pipe *lsa_pipe,
607                                                   struct policy_handle *handle,
608                                                   int num_names,
609                                                   const char **names)
610 {
611         struct composite_context *result;
612         struct rpc_request *req;
613         struct lsa_lookupnames_state *state;
614
615         struct lsa_String *lsa_names;
616         int i;
617
618         result = talloc_zero(NULL, struct composite_context);
619         if (result == NULL) goto failed;
620         result->state = COMPOSITE_STATE_IN_PROGRESS;
621         result->event_ctx = lsa_pipe->conn->event_ctx;
622
623         state = talloc(result, struct lsa_lookupnames_state);
624         if (state == NULL) goto failed;
625         result->private_data = state;
626         state->ctx = result;
627
628         state->sids.count = 0;
629         state->sids.sids = NULL;
630         state->num_names = num_names;
631         state->count = 0;
632
633         lsa_names = talloc_array(state, struct lsa_String, num_names);
634         if (lsa_names == NULL) goto failed;
635
636         for (i=0; i<num_names; i++) {
637                 lsa_names[i].string = names[i];
638         }
639
640         state->r.in.handle = handle;
641         state->r.in.num_names = num_names;
642         state->r.in.names = lsa_names;
643         state->r.in.sids = &state->sids;
644         state->r.in.level = 1;
645         state->r.in.count = &state->count;
646         state->r.out.count = &state->count;
647         state->r.out.sids = &state->sids;
648
649         req = dcerpc_lsa_LookupNames_send(lsa_pipe, state, &state->r);
650         if (req == NULL) goto failed;
651
652         req->async.callback = lsa_lookupnames_recv_sids;
653         req->async.private = state;
654         return result;
655
656  failed:
657         talloc_free(result);
658         return NULL;
659 }
660
661 static void lsa_lookupnames_recv_sids(struct rpc_request *req)
662 {
663         struct lsa_lookupnames_state *state =
664                 talloc_get_type(req->async.private,
665                                 struct lsa_lookupnames_state);
666         int i;
667
668         state->ctx->status = dcerpc_ndr_request_recv(req);
669         if (!comp_is_ok(state->ctx)) return;
670         state->ctx->status = state->r.out.result;
671         if (!NT_STATUS_IS_OK(state->ctx->status) &&
672             !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
673                 comp_error(state->ctx, state->ctx->status);
674                 return;
675         }
676
677         state->result = talloc_array(state, struct wb_sid_object *,
678                                      state->num_names);
679         if (comp_nomem(state->result, state->ctx)) return;
680
681         for (i=0; i<state->num_names; i++) {
682                 struct lsa_TranslatedSid *sid = &state->r.out.sids->sids[i];
683                 struct lsa_TrustInformation *dom;
684
685                 state->result[i] = talloc_zero(state->result,
686                                                struct wb_sid_object);
687                 if (comp_nomem(state->result[i], state->ctx)) return;
688
689                 state->result[i]->type = sid->sid_type;
690                 if (state->result[i]->type == SID_NAME_UNKNOWN) {
691                         continue;
692                 }
693
694                 if (sid->sid_index >= state->r.out.domains->count) {
695                         comp_error(state->ctx, NT_STATUS_INVALID_PARAMETER);
696                         return;
697                 }
698
699                 dom = &state->r.out.domains->domains[sid->sid_index];
700
701                 state->result[i]->sid = dom_sid_add_rid(state->result[i],
702                                                         dom->sid, sid->rid);
703         }
704
705         comp_done(state->ctx);
706 }
707
708 NTSTATUS wb_lsa_lookupnames_recv(struct composite_context *c,
709                                  TALLOC_CTX *mem_ctx,
710                                  struct wb_sid_object ***sids)
711 {
712         NTSTATUS status = composite_wait(c);
713         if (NT_STATUS_IS_OK(status)) {
714                 struct lsa_lookupnames_state *state =
715                         talloc_get_type(c->private_data,
716                                         struct lsa_lookupnames_state);
717                 *sids = talloc_steal(mem_ctx, state->result);
718         }
719         talloc_free(c);
720         return status;
721 }
722
723 NTSTATUS wb_lsa_lookupnames(struct dcerpc_pipe *lsa_pipe, 
724                             struct policy_handle *handle,
725                             int num_names, const char **names,
726                             TALLOC_CTX *mem_ctx,
727                             struct wb_sid_object ***sids)
728 {
729         struct composite_context *c =
730                 wb_lsa_lookupnames_send(lsa_pipe, handle, num_names, names);
731         return wb_lsa_lookupnames_recv(c, mem_ctx, sids);
732 }
733
734 struct lsa_lookupname_state {
735         struct composite_context *ctx;
736         struct dcerpc_pipe *lsa_pipe;
737         const char *name;
738         struct wb_sid_object *sid;
739
740         struct lsa_ObjectAttribute objectattr;
741         struct lsa_OpenPolicy2 openpolicy;
742         struct policy_handle policy_handle;
743         struct lsa_Close close;
744 };
745
746 static void lsa_lookupname_recv_open(struct rpc_request *req);
747 static void lsa_lookupname_recv_sids(struct composite_context *ctx);
748
749 struct composite_context *wb_lsa_lookupname_send(struct dcerpc_pipe *lsa_pipe,
750                                                  const char *name)
751 {
752         struct composite_context *result;
753         struct rpc_request *req;
754         struct lsa_lookupname_state *state;
755
756         result = talloc_zero(NULL, struct composite_context);
757         if (result == NULL) goto failed;
758         result->state = COMPOSITE_STATE_IN_PROGRESS;
759         result->event_ctx = lsa_pipe->conn->event_ctx;
760
761         state = talloc(result, struct lsa_lookupname_state);
762         if (state == NULL) goto failed;
763         result->private_data = state;
764
765         state->lsa_pipe = lsa_pipe;
766         state->name = talloc_strdup(state, name);
767         if (state->name == NULL) goto failed;
768         state->ctx = result;
769
770         state->openpolicy.in.system_name =
771                 talloc_asprintf(state, "\\\\%s",
772                                 dcerpc_server_name(state->lsa_pipe));
773         ZERO_STRUCT(state->objectattr);
774         state->openpolicy.in.attr = &state->objectattr;
775         state->openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
776         state->openpolicy.out.handle = &state->policy_handle;
777
778         req = dcerpc_lsa_OpenPolicy2_send(state->lsa_pipe, state,
779                                           &state->openpolicy);
780         if (req == NULL) goto failed;
781
782         req->async.callback = lsa_lookupname_recv_open;
783         req->async.private = state;
784         return result;
785
786  failed:
787         talloc_free(result);
788         return NULL;
789 }
790
791 static void lsa_lookupname_recv_open(struct rpc_request *req)
792 {
793         struct lsa_lookupname_state *state =
794                 talloc_get_type(req->async.private,
795                                 struct lsa_lookupname_state);
796         struct composite_context *ctx;
797
798         state->ctx->status = dcerpc_ndr_request_recv(req);
799         if (!comp_is_ok(state->ctx)) return;
800         state->ctx->status = state->openpolicy.out.result;
801         if (!comp_is_ok(state->ctx)) return;
802
803         ctx = wb_lsa_lookupnames_send(state->lsa_pipe, &state->policy_handle,
804                                       1, &state->name);
805         comp_cont(state->ctx, ctx, lsa_lookupname_recv_sids, state);
806 }
807
808 static void lsa_lookupname_recv_sids(struct composite_context *ctx)
809 {
810         struct lsa_lookupname_state *state =
811                 talloc_get_type(ctx->async.private_data,
812                                 struct lsa_lookupname_state);
813         struct rpc_request *req;
814         struct wb_sid_object **sids;
815
816         state->ctx->status = wb_lsa_lookupnames_recv(ctx, state, &sids);
817
818         if (NT_STATUS_IS_OK(state->ctx->status)) {
819                 state->sid = NULL;
820                 if (sids != NULL) {
821                         state->sid = sids[0];
822                 }
823         }
824
825         state->close.in.handle = &state->policy_handle;
826         state->close.out.handle = &state->policy_handle;
827
828         req = dcerpc_lsa_Close_send(state->lsa_pipe, state,
829                                     &state->close);
830         if (req != NULL) {
831                 req->async.callback =
832                         (void(*)(struct rpc_request *))talloc_free;
833         }
834
835         comp_done(state->ctx);
836 }
837
838 NTSTATUS wb_lsa_lookupname_recv(struct composite_context *c,
839                                 TALLOC_CTX *mem_ctx,
840                                 struct wb_sid_object **sid)
841 {
842         NTSTATUS status = composite_wait(c);
843         if (NT_STATUS_IS_OK(status)) {
844                 struct lsa_lookupname_state *state =
845                         talloc_get_type(c->private_data,
846                                         struct lsa_lookupname_state);
847                 *sid = talloc_steal(mem_ctx, state->sid);
848         }
849         talloc_free(c);
850         return status;
851 }
852
853 NTSTATUS wb_lsa_lookupname(struct dcerpc_pipe *lsa_pipe, const char *name,
854                            TALLOC_CTX *mem_ctx, struct wb_sid_object **sid)
855 {
856         struct composite_context *c =
857                 wb_lsa_lookupname_send(lsa_pipe, name);
858         return wb_lsa_lookupname_recv(c, mem_ctx, sid);
859 }
860
861 struct cmd_lookupname_state {
862         struct composite_context *ctx;
863         struct wbsrv_call *call;
864         struct wbsrv_domain *domain;
865         const char *name;
866         struct wb_sid_object *result;
867 };
868
869 static void cmd_lookupname_recv_lsa(struct composite_context *ctx);
870 static void cmd_lookupname_recv_sid(struct composite_context *ctx);
871
872 struct composite_context *wb_cmd_lookupname_send(struct wbsrv_call *call,
873                                                  const char *name)
874 {
875         struct composite_context *result, *ctx;
876         struct cmd_lookupname_state *state;
877         struct wbsrv_service *service = call->wbconn->listen_socket->service;
878
879         result = talloc_zero(call, struct composite_context);
880         if (result == NULL) goto failed;
881         result->state = COMPOSITE_STATE_IN_PROGRESS;
882         result->event_ctx = call->event_ctx;
883
884         state = talloc(result, struct cmd_lookupname_state);
885         if (state == NULL) goto failed;
886         state->ctx = result;
887         result->private_data = state;
888
889         state->call = call;
890         state->name = talloc_strdup(state, name);
891
892         state->domain = service->domains;
893
894         if (state->domain->lsa_pipe != NULL) {
895                 ctx = wb_lsa_lookupname_send(state->domain->lsa_pipe, name);
896                 if (ctx == NULL) goto failed;
897                 ctx->async.fn = cmd_lookupname_recv_sid;
898                 ctx->async.private_data = state;
899                 return result;
900         }
901
902         ctx = wb_get_lsa_pipe_send(result->event_ctx, 
903                                    call->wbconn->conn->msg_ctx,
904                                    state->domain->name,
905                                    state->domain->sid);
906         if (ctx == NULL) goto failed;
907         ctx->async.fn = cmd_lookupname_recv_lsa;
908         ctx->async.private_data = state;
909         return result;
910
911  failed:
912         talloc_free(result);
913         return NULL;
914 }
915
916 static void cmd_lookupname_recv_lsa(struct composite_context *ctx)
917 {
918         struct cmd_lookupname_state *state =
919                 talloc_get_type(ctx->async.private_data,
920                                 struct cmd_lookupname_state);
921         struct wbsrv_service *service =
922                 state->call->wbconn->listen_socket->service;
923
924         struct dcerpc_pipe *pipe;
925
926         state->ctx->status = wb_get_lsa_pipe_recv(ctx, state, &pipe);
927         if (!comp_is_ok(state->ctx)) return;
928
929         if (state->domain->lsa_pipe == NULL) {
930                 /* Only put the new pipe in if nobody else was faster. */
931                 state->domain->lsa_pipe = talloc_steal(service, pipe);
932         }
933
934         ctx = wb_lsa_lookupname_send(state->domain->lsa_pipe, state->name);
935         comp_cont(state->ctx, ctx, cmd_lookupname_recv_sid, state);
936 }
937
938 static void cmd_lookupname_recv_sid(struct composite_context *ctx)
939 {
940         struct cmd_lookupname_state *state =
941                 talloc_get_type(ctx->async.private_data,
942                                 struct cmd_lookupname_state);
943
944         state->ctx->status = wb_lsa_lookupname_recv(ctx, state,
945                                                     &state->result);
946         if (!comp_is_ok(state->ctx)) return;
947
948         comp_done(state->ctx);
949 }
950
951 NTSTATUS wb_cmd_lookupname_recv(struct composite_context *c,
952                                 TALLOC_CTX *mem_ctx,
953                                 struct wb_sid_object **sid)
954 {
955         NTSTATUS status = composite_wait(c);
956         if (NT_STATUS_IS_OK(status)) {
957                 struct cmd_lookupname_state *state =
958                         talloc_get_type(c->private_data,
959                                         struct cmd_lookupname_state);
960                 *sid = talloc_steal(mem_ctx, state->result);
961         }
962         talloc_free(c);
963         return status;
964 }
965
966 NTSTATUS wb_cmd_lookupname(struct wbsrv_call *call, const char *name,
967                            TALLOC_CTX *mem_ctx, struct wb_sid_object **sid)
968 {
969         struct composite_context *c =
970                 wb_cmd_lookupname_send(call, name);
971         return wb_cmd_lookupname_recv(c, mem_ctx, sid);
972 }
973
974 struct cmd_checkmachacc_state {
975         struct composite_context *ctx;
976         struct wbsrv_call *call;
977         struct wbsrv_domain *domain;
978 };
979
980 static void cmd_checkmachacc_recv_init(struct composite_context *ctx);
981
982 struct composite_context *wb_cmd_checkmachacc_send(struct wbsrv_call *call)
983 {
984         struct composite_context *result, *ctx;
985         struct cmd_checkmachacc_state *state;
986         struct wbsrv_service *service = call->wbconn->listen_socket->service;
987
988         result = talloc(call, struct composite_context);
989         if (result == NULL) goto failed;
990         result->state = COMPOSITE_STATE_IN_PROGRESS;
991         result->event_ctx = call->event_ctx;
992
993         state = talloc(result, struct cmd_checkmachacc_state);
994         if (state == NULL) goto failed;
995         state->ctx = result;
996         result->private_data = state;
997         state->call = call;
998
999         state->domain = service->domains;
1000
1001         if (state->domain->schannel_creds != NULL) {
1002                 talloc_free(state->domain->schannel_creds);
1003         }
1004
1005         state->domain->schannel_creds = cli_credentials_init(service);
1006         if (state->domain->schannel_creds == NULL) goto failed;
1007
1008         cli_credentials_set_conf(state->domain->schannel_creds);
1009
1010         state->ctx->status =
1011                 cli_credentials_set_machine_account(state->domain->
1012                                                     schannel_creds);
1013         if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
1014
1015         ctx = wb_init_domain_send(state->domain, result->event_ctx, 
1016                                   call->wbconn->conn->msg_ctx);
1017         if (ctx == NULL) goto failed;
1018         ctx->async.fn = cmd_checkmachacc_recv_init;
1019         ctx->async.private_data = state;
1020
1021         return result;
1022
1023  failed:
1024         talloc_free(result);
1025         return NULL;
1026 }
1027
1028 static void cmd_checkmachacc_recv_init(struct composite_context *ctx)
1029 {
1030         struct cmd_checkmachacc_state *state =
1031                 talloc_get_type(ctx->async.private_data,
1032                                 struct cmd_checkmachacc_state);
1033
1034         state->ctx->status = wb_init_domain_recv(ctx);
1035         if (!comp_is_ok(state->ctx)) return;
1036
1037         comp_done(state->ctx);
1038 }
1039
1040 NTSTATUS wb_cmd_checkmachacc_recv(struct composite_context *c)
1041 {
1042         NTSTATUS status = composite_wait(c);
1043         talloc_free(c);
1044         return status;
1045 }
1046
1047 NTSTATUS wb_cmd_checkmachacc(struct wbsrv_call *call)
1048 {
1049         struct composite_context *c = wb_cmd_checkmachacc_send(call);
1050         return wb_cmd_checkmachacc_recv(c);
1051 }