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