0f3a86256c8dbb1893f629f55cf8e5aa79081fd0
[kai/samba.git] / source4 / librpc / rpc / dcerpc_connect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc connect functions
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2004
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2007
9    Copyright (C) Rafal Szczesniak  2005
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25
26 #include "includes.h"
27 #include "libcli/composite/composite.h"
28 #include "libcli/smb_composite/smb_composite.h"
29 #include "lib/events/events.h"
30 #include "libcli/smb2/smb2.h"
31 #include "libcli/smb2/smb2_calls.h"
32 #include "librpc/rpc/dcerpc.h"
33 #include "librpc/rpc/dcerpc_proto.h"
34 #include "auth/credentials/credentials.h"
35 #include "param/param.h"
36 #include "libcli/resolve/resolve.h"
37
38
39 struct pipe_np_smb_state {
40         struct smb_composite_connect conn;
41         struct smbcli_tree *tree;
42         struct dcerpc_pipe_connect io;
43 };
44
45
46 /*
47   Stage 3 of ncacn_np_smb: Named pipe opened (or not)
48 */
49 static void continue_pipe_open_smb(struct composite_context *ctx)
50 {
51         struct composite_context *c = talloc_get_type(ctx->async.private_data,
52                                                       struct composite_context);
53
54         /* receive result of named pipe open request on smb */
55         c->status = dcerpc_pipe_open_smb_recv(ctx);
56         if (!composite_is_ok(c)) return;
57
58         composite_done(c);
59 }
60
61
62 /*
63   Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
64 */
65 static void continue_smb_connect(struct composite_context *ctx)
66 {
67         struct composite_context *open_ctx;
68         struct composite_context *c = talloc_get_type(ctx->async.private_data,
69                                                       struct composite_context);
70         struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
71                                                       struct pipe_np_smb_state);
72         
73         /* receive result of smb connect request */
74         c->status = smb_composite_connect_recv(ctx, c);
75         if (!composite_is_ok(c)) return;
76
77         /* prepare named pipe open parameters */
78         s->tree         = s->conn.out.tree;
79         s->io.pipe_name = s->io.binding->endpoint;
80
81         /* send named pipe open request */
82         open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe, s->tree, s->io.pipe_name);
83         if (composite_nomem(open_ctx, c)) return;
84
85         composite_continue(c, open_ctx, continue_pipe_open_smb, c);
86 }
87
88
89 /*
90   Initiate async open of a rpc connection to a rpc pipe on SMB using
91   the binding structure to determine the endpoint and options
92 */
93 static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_ctx, struct dcerpc_pipe_connect *io, struct loadparm_context *lp_ctx)
94 {
95         struct composite_context *c;
96         struct pipe_np_smb_state *s;
97         struct composite_context *conn_req;
98         struct smb_composite_connect *conn;
99
100         /* composite context allocation and setup */
101         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
102         if (c == NULL) return NULL;
103
104         s = talloc_zero(c, struct pipe_np_smb_state);
105         if (composite_nomem(s, c)) return c;
106         c->private_data = s;
107
108         s->io  = *io;
109         conn   = &s->conn;
110
111         /* prepare smb connection parameters: we're connecting to IPC$ share on
112            remote rpc server */
113         conn->in.dest_host              = s->io.binding->host;
114         conn->in.dest_ports                  = lpcfg_smb_ports(lp_ctx);
115         if (s->io.binding->target_hostname == NULL)
116                 conn->in.called_name = "*SMBSERVER"; /* FIXME: This is invalid */
117         else
118                 conn->in.called_name            = s->io.binding->target_hostname;
119         conn->in.socket_options         = lpcfg_socket_options(lp_ctx);
120         conn->in.service                = "IPC$";
121         conn->in.service_type           = NULL;
122         conn->in.workgroup              = lpcfg_workgroup(lp_ctx);
123         conn->in.gensec_settings = lpcfg_gensec_settings(conn, lp_ctx);
124
125         lpcfg_smbcli_options(lp_ctx, &conn->in.options);
126         lpcfg_smbcli_session_options(lp_ctx, &conn->in.session_options);
127
128         /*
129          * provide proper credentials - user supplied, but allow a
130          * fallback to anonymous if this is an schannel connection
131          * (might be NT4 not allowing machine logins at session
132          * setup) or if asked to do so by the caller (perhaps a SAMR password change?)
133          */
134         s->conn.in.credentials = s->io.creds;
135         if (s->io.binding->flags & (DCERPC_SCHANNEL|DCERPC_ANON_FALLBACK)) {
136                 conn->in.fallback_to_anonymous  = true;
137         } else {
138                 conn->in.fallback_to_anonymous  = false;
139         }
140
141         /* send smb connect request */
142         conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, 
143                                               s->io.resolve_ctx,
144                                               s->io.pipe->conn->event_ctx);
145         if (composite_nomem(conn_req, c)) return c;
146
147         composite_continue(c, conn_req, continue_smb_connect, c);
148         return c;
149 }
150
151
152 /*
153   Receive result of a rpc connection to a rpc pipe on SMB
154 */
155 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
156 {
157         NTSTATUS status = composite_wait(c);
158
159         talloc_free(c);
160         return status;
161 }
162
163
164 struct pipe_np_smb2_state {
165         struct smb2_tree *tree;
166         struct dcerpc_pipe_connect io;
167 };
168
169
170 /*
171   Stage 3 of ncacn_np_smb: Named pipe opened (or not)
172 */
173 static void continue_pipe_open_smb2(struct composite_context *ctx)
174 {
175         struct composite_context *c = talloc_get_type(ctx->async.private_data,
176                                                       struct composite_context);
177
178         /* receive result of named pipe open request on smb2 */
179         c->status = dcerpc_pipe_open_smb2_recv(ctx);
180         if (!composite_is_ok(c)) return;
181
182         composite_done(c);
183 }
184
185
186 /*
187   Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
188 */
189 static void continue_smb2_connect(struct composite_context *ctx)
190 {
191         struct composite_context *open_req;
192         struct composite_context *c = talloc_get_type(ctx->async.private_data,
193                                                       struct composite_context);
194         struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
195                                                        struct pipe_np_smb2_state);
196
197         /* receive result of smb2 connect request */
198         c->status = smb2_connect_recv(ctx, c, &s->tree);
199         if (!composite_is_ok(c)) return;
200
201         /* prepare named pipe open parameters */
202         s->io.pipe_name = s->io.binding->endpoint;
203
204         /* send named pipe open request */
205         open_req = dcerpc_pipe_open_smb2_send(s->io.pipe, s->tree, s->io.pipe_name);
206         if (composite_nomem(open_req, c)) return;
207
208         composite_continue(c, open_req, continue_pipe_open_smb2, c);
209 }
210
211
212 /* 
213    Initiate async open of a rpc connection request on SMB2 using
214    the binding structure to determine the endpoint and options
215 */
216 static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(
217                                         TALLOC_CTX *mem_ctx,
218                                         struct dcerpc_pipe_connect *io,
219                                         struct loadparm_context *lp_ctx)
220 {
221         struct composite_context *c;
222         struct pipe_np_smb2_state *s;
223         struct composite_context *conn_req;
224         struct smbcli_options options;
225
226         /* composite context allocation and setup */
227         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
228         if (c == NULL) return NULL;
229
230         s = talloc_zero(c, struct pipe_np_smb2_state);
231         if (composite_nomem(s, c)) return c;
232         c->private_data = s;
233
234         s->io = *io;
235
236         /*
237          * provide proper credentials - user supplied or anonymous in case this is
238          * schannel connection
239          */
240         if (s->io.binding->flags & DCERPC_SCHANNEL) {
241                 s->io.creds = cli_credentials_init(mem_ctx);
242                 if (composite_nomem(s->io.creds, c)) return c;
243
244                 cli_credentials_guess(s->io.creds, lp_ctx);
245         }
246
247         lpcfg_smbcli_options(lp_ctx, &options);
248
249         /* send smb2 connect request */
250         conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, 
251                         lpcfg_parm_string_list(mem_ctx, lp_ctx, NULL, "smb2", "ports", NULL),
252                                         "IPC$", 
253                                      s->io.resolve_ctx,
254                                      s->io.creds,
255                                      c->event_ctx,
256                                      &options,
257                                          lpcfg_socket_options(lp_ctx),
258                                          lpcfg_gensec_settings(mem_ctx, lp_ctx)
259                                          );
260         composite_continue(c, conn_req, continue_smb2_connect, c);
261         return c;
262 }
263
264
265 /*
266   Receive result of a rpc connection to a rpc pipe on SMB2
267 */
268 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
269 {
270         NTSTATUS status = composite_wait(c);
271         
272         talloc_free(c);
273         return status;
274 }
275
276
277 struct pipe_ip_tcp_state {
278         struct dcerpc_pipe_connect io;
279         const char *localaddr;
280         const char *host;
281         const char *target_hostname;
282         uint32_t port;
283 };
284
285
286 /*
287   Stage 2 of ncacn_ip_tcp: rpc pipe opened (or not)
288 */
289 static void continue_pipe_open_ncacn_ip_tcp(struct composite_context *ctx)
290 {
291         struct composite_context *c = talloc_get_type(ctx->async.private_data,
292                                                       struct composite_context);
293
294         /* receive result of named pipe open request on tcp/ip */
295         c->status = dcerpc_pipe_open_tcp_recv(ctx);
296         if (!composite_is_ok(c)) return;
297
298         composite_done(c);
299 }
300
301
302 /*
303   Initiate async open of a rpc connection to a rpc pipe on TCP/IP using
304   the binding structure to determine the endpoint and options
305 */
306 static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_ctx,
307                                                                        struct dcerpc_pipe_connect *io)
308 {
309         struct composite_context *c;
310         struct pipe_ip_tcp_state *s;
311         struct composite_context *pipe_req;
312
313         /* composite context allocation and setup */
314         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
315         if (c == NULL) return NULL;
316
317         s = talloc_zero(c, struct pipe_ip_tcp_state);
318         if (composite_nomem(s, c)) return c;
319         c->private_data = s;
320
321         /* store input parameters in state structure */
322         s->io               = *io;
323         s->localaddr        = talloc_reference(c, io->binding->localaddress);
324         s->host             = talloc_reference(c, io->binding->host);
325         s->target_hostname  = talloc_reference(c, io->binding->target_hostname);
326                              /* port number is a binding endpoint here */
327         s->port             = atoi(io->binding->endpoint);   
328
329         /* send pipe open request on tcp/ip */
330         pipe_req = dcerpc_pipe_open_tcp_send(s->io.pipe->conn, s->localaddr, s->host, s->target_hostname,
331                                              s->port, io->resolve_ctx);
332         composite_continue(c, pipe_req, continue_pipe_open_ncacn_ip_tcp, c);
333         return c;
334 }
335
336
337 /*
338   Receive result of a rpc connection to a rpc pipe on TCP/IP
339 */
340 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
341 {
342         NTSTATUS status = composite_wait(c);
343         
344         talloc_free(c);
345         return status;
346 }
347
348
349 struct pipe_unix_state {
350         struct dcerpc_pipe_connect io;
351         const char *path;
352 };
353
354
355 /*
356   Stage 2 of ncacn_unix: rpc pipe opened (or not)
357 */
358 static void continue_pipe_open_ncacn_unix_stream(struct composite_context *ctx)
359 {
360         struct composite_context *c = talloc_get_type(ctx->async.private_data,
361                                                       struct composite_context);
362
363         /* receive result of pipe open request on unix socket */
364         c->status = dcerpc_pipe_open_unix_stream_recv(ctx);
365         if (!composite_is_ok(c)) return;
366
367         composite_done(c);
368 }
369
370
371 /*
372   Initiate async open of a rpc connection to a rpc pipe on unix socket using
373   the binding structure to determine the endpoint and options
374 */
375 static struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX *mem_ctx,
376                                                                             struct dcerpc_pipe_connect *io)
377 {
378         struct composite_context *c;
379         struct pipe_unix_state *s;
380         struct composite_context *pipe_req;
381
382         /* composite context allocation and setup */
383         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
384         if (c == NULL) return NULL;
385
386         s = talloc_zero(c, struct pipe_unix_state);
387         if (composite_nomem(s, c)) return c;
388         c->private_data = s;
389
390         /* prepare pipe open parameters and store them in state structure
391            also, verify whether biding endpoint is not null */
392         s->io = *io;
393         
394         if (!io->binding->endpoint) {
395                 DEBUG(0, ("Path to unix socket not specified\n"));
396                 composite_error(c, NT_STATUS_INVALID_PARAMETER);
397                 return c;
398         }
399
400         s->path  = talloc_strdup(c, io->binding->endpoint);  /* path is a binding endpoint here */
401         if (composite_nomem(s->path, c)) return c;
402
403         /* send pipe open request on unix socket */
404         pipe_req = dcerpc_pipe_open_unix_stream_send(s->io.pipe->conn, s->path);
405         composite_continue(c, pipe_req, continue_pipe_open_ncacn_unix_stream, c);
406         return c;
407 }
408
409
410 /*
411   Receive result of a rpc connection to a pipe on unix socket
412 */
413 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
414 {
415         NTSTATUS status = composite_wait(c);
416
417         talloc_free(c);
418         return status;
419 }
420
421
422 struct pipe_ncalrpc_state {
423         struct dcerpc_pipe_connect io;
424 };
425
426 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c);
427
428 /*
429   Stage 2 of ncalrpc: rpc pipe opened (or not)
430 */
431 static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
432 {
433         struct composite_context *c = talloc_get_type(ctx->async.private_data,
434                                                       struct composite_context);
435
436         /* receive result of pipe open request on ncalrpc */
437         c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
438         if (!composite_is_ok(c)) return;
439
440         composite_done(c);
441 }
442
443
444 /* 
445    Initiate async open of a rpc connection request on NCALRPC using
446    the binding structure to determine the endpoint and options
447 */
448 static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
449                                                                   struct dcerpc_pipe_connect *io, struct loadparm_context *lp_ctx)
450 {
451         struct composite_context *c;
452         struct pipe_ncalrpc_state *s;
453         struct composite_context *pipe_req;
454
455         /* composite context allocation and setup */
456         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
457         if (c == NULL) return NULL;
458
459         s = talloc_zero(c, struct pipe_ncalrpc_state);
460         if (composite_nomem(s, c)) return c;
461         c->private_data = s;
462         
463         /* store input parameters in state structure */
464         s->io  = *io;
465
466         /* send pipe open request */
467         pipe_req = dcerpc_pipe_open_pipe_send(s->io.pipe->conn, lpcfg_ncalrpc_dir(lp_ctx),
468                                               s->io.binding->endpoint);
469         composite_continue(c, pipe_req, continue_pipe_open_ncalrpc, c);
470         return c;
471 }
472
473
474 /*
475   Receive result of a rpc connection to a rpc pipe on NCALRPC
476 */
477 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
478 {
479         NTSTATUS status = composite_wait(c);
480         
481         talloc_free(c);
482         return status;
483 }
484
485
486 struct pipe_connect_state {
487         struct dcerpc_pipe *pipe;
488         struct dcerpc_binding *binding;
489         const struct ndr_interface_table *table;
490         struct cli_credentials *credentials;
491         struct loadparm_context *lp_ctx;
492 };
493
494
495 static void continue_map_binding(struct composite_context *ctx);
496 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s);
497 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx);
498 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx);
499 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx);
500 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx);
501 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx);
502 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s);
503 static void continue_pipe_auth(struct composite_context *ctx);
504
505
506 /*
507   Stage 2 of pipe_connect_b: Receive result of endpoint mapping
508 */
509 static void continue_map_binding(struct composite_context *ctx)
510 {
511         struct composite_context *c = talloc_get_type(ctx->async.private_data,
512                                                       struct composite_context);
513         struct pipe_connect_state *s = talloc_get_type(c->private_data,
514                                                        struct pipe_connect_state);
515         
516         c->status = dcerpc_epm_map_binding_recv(ctx);
517         if (!composite_is_ok(c)) return;
518
519         DEBUG(2,("Mapped to DCERPC endpoint %s\n", s->binding->endpoint));
520         
521         continue_connect(c, s);
522 }
523
524
525 /*
526   Stage 2 of pipe_connect_b: Continue connection after endpoint is known
527 */
528 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s)
529 {
530         struct dcerpc_pipe_connect pc;
531
532         /* potential exits to another stage by sending an async request */
533         struct composite_context *ncacn_np_smb2_req;
534         struct composite_context *ncacn_np_smb_req;
535         struct composite_context *ncacn_ip_tcp_req;
536         struct composite_context *ncacn_unix_req;
537         struct composite_context *ncalrpc_req;
538
539         /* dcerpc pipe connect input parameters */
540         pc.pipe         = s->pipe;
541         pc.binding      = s->binding;
542         pc.interface    = s->table;
543         pc.creds        = s->credentials;
544         pc.resolve_ctx  = lpcfg_resolve_context(s->lp_ctx);
545
546         /* connect dcerpc pipe depending on required transport */
547         switch (s->binding->transport) {
548         case NCACN_NP:
549                 if (pc.binding->flags & DCERPC_SMB2) {
550                         /* new varient of SMB a.k.a. SMB2 */
551                         ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc, s->lp_ctx);
552                         composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
553                         return;
554
555                 } else {
556                         /* good old ordinary SMB */
557                         ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc, s->lp_ctx);
558                         composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
559                         return;
560                 }
561                 break;
562
563         case NCACN_IP_TCP:
564                 ncacn_ip_tcp_req = dcerpc_pipe_connect_ncacn_ip_tcp_send(c, &pc);
565                 composite_continue(c, ncacn_ip_tcp_req, continue_pipe_connect_ncacn_ip_tcp, c);
566                 return;
567
568         case NCACN_UNIX_STREAM:
569                 ncacn_unix_req = dcerpc_pipe_connect_ncacn_unix_stream_send(c, &pc);
570                 composite_continue(c, ncacn_unix_req, continue_pipe_connect_ncacn_unix, c);
571                 return;
572
573         case NCALRPC:
574                 ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc, s->lp_ctx);
575                 composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
576                 return;
577
578         default:
579                 /* looks like a transport we don't support now */
580                 composite_error(c, NT_STATUS_NOT_SUPPORTED);
581         }
582 }
583
584
585 /*
586   Stage 3 of pipe_connect_b: Receive result of pipe connect request on
587   named pipe on smb2
588 */
589 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx)
590 {
591         struct composite_context *c = talloc_get_type(ctx->async.private_data,
592                                                       struct composite_context);
593         struct pipe_connect_state *s = talloc_get_type(c->private_data,
594                                                        struct pipe_connect_state);
595
596         c->status = dcerpc_pipe_connect_ncacn_np_smb2_recv(ctx);
597         if (!composite_is_ok(c)) return;
598
599         continue_pipe_connect(c, s);
600 }
601
602
603 /*
604   Stage 3 of pipe_connect_b: Receive result of pipe connect request on
605   named pipe on smb
606 */
607 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx)
608 {
609         struct composite_context *c = talloc_get_type(ctx->async.private_data,
610                                                       struct composite_context);
611         struct pipe_connect_state *s = talloc_get_type(c->private_data,
612                                                        struct pipe_connect_state);
613
614         c->status = dcerpc_pipe_connect_ncacn_np_smb_recv(ctx);
615         if (!composite_is_ok(c)) return;
616         
617         continue_pipe_connect(c, s);
618 }
619
620
621 /*
622   Stage 3 of pipe_connect_b: Receive result of pipe connect request on tcp/ip
623 */
624 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx)
625 {
626         struct composite_context *c = talloc_get_type(ctx->async.private_data,
627                                                       struct composite_context);
628         struct pipe_connect_state *s = talloc_get_type(c->private_data,
629                                                        struct pipe_connect_state);
630
631         c->status = dcerpc_pipe_connect_ncacn_ip_tcp_recv(ctx);
632         if (!composite_is_ok(c)) return;
633
634         continue_pipe_connect(c, s);
635 }
636
637
638 /*
639   Stage 3 of pipe_connect_b: Receive result of pipe connect request on unix socket
640 */
641 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx)
642 {
643         struct composite_context *c = talloc_get_type(ctx->async.private_data,
644                                                       struct composite_context);
645         struct pipe_connect_state *s = talloc_get_type(c->private_data,
646                                                        struct pipe_connect_state);
647         
648         c->status = dcerpc_pipe_connect_ncacn_unix_stream_recv(ctx);
649         if (!composite_is_ok(c)) return;
650         
651         continue_pipe_connect(c, s);
652 }
653
654
655 /*
656   Stage 3 of pipe_connect_b: Receive result of pipe connect request on local rpc
657 */
658 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx)
659 {
660         struct composite_context *c = talloc_get_type(ctx->async.private_data,
661                                                       struct composite_context);
662         struct pipe_connect_state *s = talloc_get_type(c->private_data,
663                                                        struct pipe_connect_state);
664         
665         c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
666         if (!composite_is_ok(c)) return;
667
668         continue_pipe_connect(c, s);
669 }
670
671
672 /*
673   Stage 4 of pipe_connect_b: Start an authentication on connected dcerpc pipe
674   depending on credentials and binding flags passed.
675 */
676 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s)
677 {
678         struct composite_context *auth_bind_req;
679
680         s->pipe->binding = s->binding;
681         if (!talloc_reference(s->pipe, s->binding)) {
682                 composite_error(c, NT_STATUS_NO_MEMORY);
683                 return;
684         }
685
686         auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
687                                               s->credentials, s->lp_ctx);
688         composite_continue(c, auth_bind_req, continue_pipe_auth, c);
689 }
690
691
692 /*
693   Stage 5 of pipe_connect_b: Receive result of pipe authentication request
694   and say if all went ok
695 */
696 static void continue_pipe_auth(struct composite_context *ctx)
697 {
698         struct composite_context *c = talloc_get_type(ctx->async.private_data,
699                                                       struct composite_context);
700         struct pipe_connect_state *s = talloc_get_type(c->private_data, struct pipe_connect_state);
701
702         c->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe);
703         if (!composite_is_ok(c)) return;
704
705         composite_done(c);
706 }
707
708
709 /*
710   handle timeouts of a dcerpc connect
711 */
712 static void dcerpc_connect_timeout_handler(struct tevent_context *ev, struct tevent_timer *te, 
713                                            struct timeval t, void *private_data)
714 {
715         struct composite_context *c = talloc_get_type(private_data, struct composite_context);
716         composite_error(c, NT_STATUS_IO_TIMEOUT);
717 }
718
719 /*
720   start a request to open a rpc connection to a rpc pipe, using
721   specified binding structure to determine the endpoint and options
722 */
723 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
724                                                      struct dcerpc_binding *binding,
725                                                      const struct ndr_interface_table *table,
726                                                      struct cli_credentials *credentials,
727                                                      struct tevent_context *ev,
728                                                      struct loadparm_context *lp_ctx)
729 {
730         struct composite_context *c;
731         struct pipe_connect_state *s;
732         struct tevent_context *new_ev = NULL;
733
734         /* composite context allocation and setup */
735         c = composite_create(parent_ctx, ev);
736         if (c == NULL) {
737                 talloc_free(new_ev);
738                 return NULL;
739         }
740         talloc_steal(c, new_ev);
741
742         s = talloc_zero(c, struct pipe_connect_state);
743         if (composite_nomem(s, c)) return c;
744         c->private_data = s;
745
746         /* initialise dcerpc pipe structure */
747         s->pipe = dcerpc_pipe_init(c, ev);
748         if (composite_nomem(s->pipe, c)) return c;
749
750         if (DEBUGLEVEL >= 10)
751                 s->pipe->conn->packet_log_dir = lpcfg_lockdir(lp_ctx);
752
753         /* store parameters in state structure */
754         s->binding      = binding;
755         s->table        = table;
756         s->credentials  = credentials;
757         s->lp_ctx       = lp_ctx;
758
759         event_add_timed(c->event_ctx, c,
760                         timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
761                         dcerpc_connect_timeout_handler, c);
762         
763         switch (s->binding->transport) {
764         case NCA_UNKNOWN: {
765                 struct composite_context *binding_req;
766                 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
767                                                           s->pipe->conn->event_ctx,
768                                                           s->lp_ctx);
769                 composite_continue(c, binding_req, continue_map_binding, c);
770                 return c;
771                 }
772
773         case NCACN_NP:
774         case NCACN_IP_TCP:
775         case NCALRPC:
776                 if (!s->binding->endpoint) {
777                         struct composite_context *binding_req;
778                         binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
779                                                                   s->pipe->conn->event_ctx,
780                                                                   s->lp_ctx);
781                         composite_continue(c, binding_req, continue_map_binding, c);
782                         return c;
783                 }
784
785         default:
786                 break;
787         }
788
789         continue_connect(c, s);
790         return c;
791 }
792
793
794 /*
795   receive result of a request to open a rpc connection to a rpc pipe
796 */
797 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
798                                     struct dcerpc_pipe **p)
799 {
800         NTSTATUS status;
801         struct pipe_connect_state *s;
802         
803         status = composite_wait(c);
804         
805         if (NT_STATUS_IS_OK(status)) {
806                 s = talloc_get_type(c->private_data, struct pipe_connect_state);
807                 talloc_steal(mem_ctx, s->pipe);
808                 *p = s->pipe;
809         }
810         talloc_free(c);
811         return status;
812 }
813
814
815 /*
816   open a rpc connection to a rpc pipe, using the specified 
817   binding structure to determine the endpoint and options - sync version
818 */
819 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
820                                struct dcerpc_pipe **pp,
821                                struct dcerpc_binding *binding,
822                                const struct ndr_interface_table *table,
823                                struct cli_credentials *credentials,
824                                struct tevent_context *ev,
825                                struct loadparm_context *lp_ctx)
826 {
827         struct composite_context *c;
828         
829         c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
830                                        credentials, ev, lp_ctx);
831         return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
832 }
833
834
835 struct pipe_conn_state {
836         struct dcerpc_pipe *pipe;
837 };
838
839
840 static void continue_pipe_connect_b(struct composite_context *ctx);
841
842
843 /*
844   Initiate rpc connection to a rpc pipe, using the specified string
845   binding to determine the endpoint and options.
846   The string is to be parsed to a binding structure first.
847 */
848 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
849                                                    const char *binding,
850                                                    const struct ndr_interface_table *table,
851                                                    struct cli_credentials *credentials,
852                                                    struct tevent_context *ev, struct loadparm_context *lp_ctx)
853 {
854         struct composite_context *c;
855         struct pipe_conn_state *s;
856         struct dcerpc_binding *b;
857         struct composite_context *pipe_conn_req;
858
859         /* composite context allocation and setup */
860         c = composite_create(parent_ctx, ev);
861         if (c == NULL) {
862                 return NULL;
863         }
864
865         s = talloc_zero(c, struct pipe_conn_state);
866         if (composite_nomem(s, c)) return c;
867         c->private_data = s;
868
869         /* parse binding string to the structure */
870         c->status = dcerpc_parse_binding(c, binding, &b);
871         if (!NT_STATUS_IS_OK(c->status)) {
872                 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
873                 composite_error(c, c->status);
874                 return c;
875         }
876
877         DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(c, b)));
878
879         /* 
880            start connecting to a rpc pipe after binding structure
881            is established
882          */
883         pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
884                                                    credentials, ev, lp_ctx);
885         composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
886         return c;
887 }
888
889
890 /*
891   Stage 2 of pipe_connect: Receive result of actual pipe connect request
892   and say if we're done ok
893 */
894 static void continue_pipe_connect_b(struct composite_context *ctx)
895 {
896         struct composite_context *c = talloc_get_type(ctx->async.private_data,
897                                                       struct composite_context);
898         struct pipe_conn_state *s = talloc_get_type(c->private_data,
899                                                     struct pipe_conn_state);
900
901         c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
902         talloc_steal(s, s->pipe);
903         if (!composite_is_ok(c)) return;
904
905         composite_done(c);
906 }
907
908
909 /*
910   Receive result of pipe connect (using binding string) request
911   and return connected pipe structure.
912 */
913 NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
914                                   TALLOC_CTX *mem_ctx,
915                                   struct dcerpc_pipe **pp)
916 {
917         NTSTATUS status;
918         struct pipe_conn_state *s;
919
920         status = composite_wait(c);
921         if (NT_STATUS_IS_OK(status)) {
922                 s = talloc_get_type(c->private_data, struct pipe_conn_state);
923                 *pp = talloc_steal(mem_ctx, s->pipe);
924         }
925         talloc_free(c);
926         return status;
927 }
928
929
930 /*
931   Open a rpc connection to a rpc pipe, using the specified string
932   binding to determine the endpoint and options - sync version
933 */
934 _PUBLIC_ NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx, 
935                              struct dcerpc_pipe **pp, 
936                              const char *binding,
937                              const struct ndr_interface_table *table,
938                              struct cli_credentials *credentials,
939                              struct tevent_context *ev,
940                              struct loadparm_context *lp_ctx)
941 {
942         struct composite_context *c;
943         c = dcerpc_pipe_connect_send(parent_ctx, binding, 
944                                      table, credentials, ev, lp_ctx);
945         return dcerpc_pipe_connect_recv(c, parent_ctx, pp);
946 }
947