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