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