26bccbbad7e2d1f548b8bd8d0dfec161d949f468
[jelmer/samba4-debian.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, struct dcerpc_pipe_connect *io, struct loadparm_context *lp_ctx)
93 {
94         struct composite_context *c;
95         struct pipe_np_smb_state *s;
96         struct composite_context *conn_req;
97         struct smb_composite_connect *conn;
98
99         /* composite context allocation and setup */
100         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
101         if (c == NULL) return NULL;
102
103         s = talloc_zero(c, struct pipe_np_smb_state);
104         if (composite_nomem(s, c)) return c;
105         c->private_data = s;
106
107         s->io  = *io;
108         conn   = &s->conn;
109
110         /* prepare smb connection parameters: we're connecting to IPC$ share on
111            remote rpc server */
112         conn->in.dest_host              = s->io.binding->host;
113         conn->in.dest_ports                  = lp_smb_ports(lp_ctx);
114         if (s->io.binding->target_hostname == NULL)
115                 conn->in.called_name = "*SMBSERVER"; /* FIXME: This is invalid */
116         else
117                 conn->in.called_name            = s->io.binding->target_hostname;
118         conn->in.service                = "IPC$";
119         conn->in.service_type           = NULL;
120         conn->in.workgroup              = lp_workgroup(lp_ctx);
121
122         conn->in.max_xmit = lp_max_xmit(lp_ctx);
123         conn->in.max_mux = lp_maxmux(lp_ctx);
124         conn->in.ntstatus_support = lp_nt_status_support(lp_ctx);
125         conn->in.max_protocol = lp_cli_maxprotocol(lp_ctx);
126         conn->in.unicode = lp_unicode(lp_ctx);
127         conn->in.use_spnego = lp_use_spnego(lp_ctx) && lp_nt_status_support(lp_ctx);
128
129         /*
130          * provide proper credentials - user supplied, but allow a
131          * fallback to anonymous if this is an schannel connection
132          * (might be NT4 not allowing machine logins at session
133          * setup).
134          */
135         s->conn.in.credentials = s->io.creds;
136         if (s->io.binding->flags & DCERPC_SCHANNEL) {
137                 conn->in.fallback_to_anonymous  = true;
138         } else {
139                 conn->in.fallback_to_anonymous  = false;
140         }
141
142         /* send smb connect request */
143         conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, 
144                                               lp_resolve_context(lp_ctx), 
145                                               s->io.pipe->conn->event_ctx);
146         if (composite_nomem(conn_req, c)) return c;
147
148         composite_continue(c, conn_req, continue_smb_connect, c);
149         return c;
150 }
151
152
153 /*
154   Receive result of a rpc connection to a rpc pipe on SMB
155 */
156 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
157 {
158         NTSTATUS status = composite_wait(c);
159
160         talloc_free(c);
161         return status;
162 }
163
164
165 struct pipe_np_smb2_state {
166         struct smb2_tree *tree;
167         struct dcerpc_pipe_connect io;
168 };
169
170
171 /*
172   Stage 3 of ncacn_np_smb: Named pipe opened (or not)
173 */
174 static void continue_pipe_open_smb2(struct composite_context *ctx)
175 {
176         struct composite_context *c = talloc_get_type(ctx->async.private_data,
177                                                       struct composite_context);
178
179         /* receive result of named pipe open request on smb2 */
180         c->status = dcerpc_pipe_open_smb2_recv(ctx);
181         if (!composite_is_ok(c)) return;
182
183         composite_done(c);
184 }
185
186
187 /*
188   Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
189 */
190 static void continue_smb2_connect(struct composite_context *ctx)
191 {
192         struct composite_context *open_req;
193         struct composite_context *c = talloc_get_type(ctx->async.private_data,
194                                                       struct composite_context);
195         struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
196                                                        struct pipe_np_smb2_state);
197
198         /* receive result of smb2 connect request */
199         c->status = smb2_connect_recv(ctx, c, &s->tree);
200         if (!composite_is_ok(c)) return;
201
202         /* prepare named pipe open parameters */
203         s->io.pipe_name = s->io.binding->endpoint;
204
205         /* send named pipe open request */
206         open_req = dcerpc_pipe_open_smb2_send(s->io.pipe, s->tree, s->io.pipe_name);
207         if (composite_nomem(open_req, c)) return;
208
209         composite_continue(c, open_req, continue_pipe_open_smb2, c);
210 }
211
212
213 /* 
214    Initiate async open of a rpc connection request on SMB2 using
215    the binding structure to determine the endpoint and options
216 */
217 static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(
218                                         TALLOC_CTX *mem_ctx,
219                                         struct dcerpc_pipe_connect *io,
220                                         struct loadparm_context *lp_ctx)
221 {
222         struct composite_context *c;
223         struct pipe_np_smb2_state *s;
224         struct composite_context *conn_req;
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         /* send smb2 connect request */
248         conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, "IPC$", 
249                                      s->io.resolve_ctx,
250                                      s->io.creds,
251                                      c->event_ctx);
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 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         if (ev == NULL) {
725                 new_ev = event_context_init(parent_ctx);
726                 if (new_ev == NULL) return NULL;
727                 ev = new_ev;
728         }
729
730         /* composite context allocation and setup */
731         c = composite_create(parent_ctx, ev);
732         if (c == NULL) {
733                 talloc_free(new_ev);
734                 return NULL;
735         }
736         talloc_steal(c, new_ev);
737
738         s = talloc_zero(c, struct pipe_connect_state);
739         if (composite_nomem(s, c)) return c;
740         c->private_data = s;
741
742         /* initialise dcerpc pipe structure */
743         s->pipe = dcerpc_pipe_init(c, ev, lp_iconv_convenience(lp_ctx));
744         if (composite_nomem(s->pipe, c)) return c;
745
746         /* store parameters in state structure */
747         s->binding      = binding;
748         s->table        = table;
749         s->credentials  = credentials;
750         s->lp_ctx       = lp_ctx;
751
752         event_add_timed(c->event_ctx, c,
753                         timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
754                         dcerpc_connect_timeout_handler, c);
755         
756         switch (s->binding->transport) {
757         case NCA_UNKNOWN: {
758                 struct composite_context *binding_req;
759                 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
760                                                           s->pipe->conn->event_ctx,
761                                                           s->lp_ctx);
762                 composite_continue(c, binding_req, continue_map_binding, c);
763                 return c;
764                 }
765
766         case NCACN_NP:
767         case NCACN_IP_TCP:
768         case NCALRPC:
769                 if (!s->binding->endpoint) {
770                         struct composite_context *binding_req;
771                         binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
772                                                                   s->pipe->conn->event_ctx,
773                                                                   s->lp_ctx);
774                         composite_continue(c, binding_req, continue_map_binding, c);
775                         return c;
776                 }
777
778         default:
779                 break;
780         }
781
782         continue_connect(c, s);
783         return c;
784 }
785
786
787 /*
788   receive result of a request to open a rpc connection to a rpc pipe
789 */
790 NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
791                                     struct dcerpc_pipe **p)
792 {
793         NTSTATUS status;
794         struct pipe_connect_state *s;
795         
796         status = composite_wait(c);
797         
798         if (NT_STATUS_IS_OK(status)) {
799                 s = talloc_get_type(c->private_data, struct pipe_connect_state);
800                 talloc_steal(mem_ctx, s->pipe);
801                 *p = s->pipe;
802         }
803         talloc_free(c);
804         return status;
805 }
806
807
808 /*
809   open a rpc connection to a rpc pipe, using the specified 
810   binding structure to determine the endpoint and options - sync version
811 */
812 NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
813                                struct dcerpc_pipe **pp,
814                                struct dcerpc_binding *binding,
815                                const struct ndr_interface_table *table,
816                                struct cli_credentials *credentials,
817                                struct event_context *ev,
818                                struct loadparm_context *lp_ctx)
819 {
820         struct composite_context *c;
821         
822         c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
823                                        credentials, ev, lp_ctx);
824         return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
825 }
826
827
828 struct pipe_conn_state {
829         struct dcerpc_pipe *pipe;
830 };
831
832
833 static void continue_pipe_connect_b(struct composite_context *ctx);
834
835
836 /*
837   Initiate rpc connection to a rpc pipe, using the specified string
838   binding to determine the endpoint and options.
839   The string is to be parsed to a binding structure first.
840 */
841 struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
842                                                    const char *binding,
843                                                    const struct ndr_interface_table *table,
844                                                    struct cli_credentials *credentials,
845                                                    struct event_context *ev, struct loadparm_context *lp_ctx)
846 {
847         struct composite_context *c;
848         struct pipe_conn_state *s;
849         struct dcerpc_binding *b;
850         struct composite_context *pipe_conn_req;
851         struct event_context *new_ev = NULL;
852
853         if (ev == NULL) {
854                 new_ev = event_context_init(parent_ctx);
855                 if (new_ev == NULL) return NULL;
856                 ev = new_ev;
857         }
858
859         /* composite context allocation and setup */
860         c = composite_create(parent_ctx, ev);
861         if (c == NULL) {
862                 talloc_free(new_ev);
863                 return NULL;
864         }
865         talloc_steal(c, new_ev);
866
867         s = talloc_zero(c, struct pipe_conn_state);
868         if (composite_nomem(s, c)) return c;
869         c->private_data = s;
870
871         /* parse binding string to the structure */
872         c->status = dcerpc_parse_binding(c, binding, &b);
873         if (!NT_STATUS_IS_OK(c->status)) {
874                 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
875                 composite_error(c, c->status);
876                 return c;
877         }
878
879         DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(c, b)));
880
881         /* 
882            start connecting to a rpc pipe after binding structure
883            is established
884          */
885         pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
886                                                    credentials, ev, lp_ctx);
887         composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
888         return c;
889 }
890
891
892 /*
893   Stage 2 of pipe_connect: Receive result of actual pipe connect request
894   and say if we're done ok
895 */
896 static void continue_pipe_connect_b(struct composite_context *ctx)
897 {
898         struct composite_context *c = talloc_get_type(ctx->async.private_data,
899                                                       struct composite_context);
900         struct pipe_conn_state *s = talloc_get_type(c->private_data,
901                                                     struct pipe_conn_state);
902
903         c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
904         talloc_steal(s, s->pipe);
905         if (!composite_is_ok(c)) return;
906
907         composite_done(c);
908 }
909
910
911 /*
912   Receive result of pipe connect (using binding string) request
913   and return connected pipe structure.
914 */
915 NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
916                                   TALLOC_CTX *mem_ctx,
917                                   struct dcerpc_pipe **pp)
918 {
919         NTSTATUS status;
920         struct pipe_conn_state *s;
921
922         status = composite_wait(c);
923         if (NT_STATUS_IS_OK(status)) {
924                 s = talloc_get_type(c->private_data, struct pipe_conn_state);
925                 *pp = talloc_steal(mem_ctx, s->pipe);
926         }
927         talloc_free(c);
928         return status;
929 }
930
931
932 /*
933   Open a rpc connection to a rpc pipe, using the specified string
934   binding to determine the endpoint and options - sync version
935 */
936 NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx, 
937                              struct dcerpc_pipe **pp, 
938                              const char *binding,
939                              const struct ndr_interface_table *table,
940                              struct cli_credentials *credentials,
941                              struct event_context *ev,
942                              struct loadparm_context *lp_ctx)
943 {
944         struct composite_context *c;
945         c = dcerpc_pipe_connect_send(parent_ctx, binding, 
946                                      table, credentials, ev, lp_ctx);
947         return dcerpc_pipe_connect_recv(c, parent_ctx, pp);
948 }
949