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