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