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