Add unique IP address binding for client connections (EPM and ncacn_ip_tcp levels)
[Samba.git] / source4 / librpc / rpc / dcerpc_connect.c
blob0f3a86256c8dbb1893f629f55cf8e5aa79081fd0
1 /*
2 Unix SMB/CIFS implementation.
4 dcerpc connect functions
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
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.
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.
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/>.
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"
39 struct pipe_np_smb_state {
40 struct smb_composite_connect conn;
41 struct smbcli_tree *tree;
42 struct dcerpc_pipe_connect io;
47 Stage 3 of ncacn_np_smb: Named pipe opened (or not)
49 static void continue_pipe_open_smb(struct composite_context *ctx)
51 struct composite_context *c = talloc_get_type(ctx->async.private_data,
52 struct composite_context);
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;
58 composite_done(c);
63 Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
65 static void continue_smb_connect(struct composite_context *ctx)
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);
73 /* receive result of smb connect request */
74 c->status = smb_composite_connect_recv(ctx, c);
75 if (!composite_is_ok(c)) return;
77 /* prepare named pipe open parameters */
78 s->tree = s->conn.out.tree;
79 s->io.pipe_name = s->io.binding->endpoint;
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;
85 composite_continue(c, open_ctx, continue_pipe_open_smb, c);
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
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)
95 struct composite_context *c;
96 struct pipe_np_smb_state *s;
97 struct composite_context *conn_req;
98 struct smb_composite_connect *conn;
100 /* composite context allocation and setup */
101 c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
102 if (c == NULL) return NULL;
104 s = talloc_zero(c, struct pipe_np_smb_state);
105 if (composite_nomem(s, c)) return c;
106 c->private_data = s;
108 s->io = *io;
109 conn = &s->conn;
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);
125 lpcfg_smbcli_options(lp_ctx, &conn->in.options);
126 lpcfg_smbcli_session_options(lp_ctx, &conn->in.session_options);
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?)
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;
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;
147 composite_continue(c, conn_req, continue_smb_connect, c);
148 return c;
153 Receive result of a rpc connection to a rpc pipe on SMB
155 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
157 NTSTATUS status = composite_wait(c);
159 talloc_free(c);
160 return status;
164 struct pipe_np_smb2_state {
165 struct smb2_tree *tree;
166 struct dcerpc_pipe_connect io;
171 Stage 3 of ncacn_np_smb: Named pipe opened (or not)
173 static void continue_pipe_open_smb2(struct composite_context *ctx)
175 struct composite_context *c = talloc_get_type(ctx->async.private_data,
176 struct composite_context);
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;
182 composite_done(c);
187 Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
189 static void continue_smb2_connect(struct composite_context *ctx)
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);
197 /* receive result of smb2 connect request */
198 c->status = smb2_connect_recv(ctx, c, &s->tree);
199 if (!composite_is_ok(c)) return;
201 /* prepare named pipe open parameters */
202 s->io.pipe_name = s->io.binding->endpoint;
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;
208 composite_continue(c, open_req, continue_pipe_open_smb2, c);
213 Initiate async open of a rpc connection request on SMB2 using
214 the binding structure to determine the endpoint and options
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)
221 struct composite_context *c;
222 struct pipe_np_smb2_state *s;
223 struct composite_context *conn_req;
224 struct smbcli_options options;
226 /* composite context allocation and setup */
227 c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
228 if (c == NULL) return NULL;
230 s = talloc_zero(c, struct pipe_np_smb2_state);
231 if (composite_nomem(s, c)) return c;
232 c->private_data = s;
234 s->io = *io;
237 * provide proper credentials - user supplied or anonymous in case this is
238 * schannel connection
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;
244 cli_credentials_guess(s->io.creds, lp_ctx);
247 lpcfg_smbcli_options(lp_ctx, &options);
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)
260 composite_continue(c, conn_req, continue_smb2_connect, c);
261 return c;
266 Receive result of a rpc connection to a rpc pipe on SMB2
268 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
270 NTSTATUS status = composite_wait(c);
272 talloc_free(c);
273 return status;
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;
287 Stage 2 of ncacn_ip_tcp: rpc pipe opened (or not)
289 static void continue_pipe_open_ncacn_ip_tcp(struct composite_context *ctx)
291 struct composite_context *c = talloc_get_type(ctx->async.private_data,
292 struct composite_context);
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;
298 composite_done(c);
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
306 static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_ctx,
307 struct dcerpc_pipe_connect *io)
309 struct composite_context *c;
310 struct pipe_ip_tcp_state *s;
311 struct composite_context *pipe_req;
313 /* composite context allocation and setup */
314 c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
315 if (c == NULL) return NULL;
317 s = talloc_zero(c, struct pipe_ip_tcp_state);
318 if (composite_nomem(s, c)) return c;
319 c->private_data = s;
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);
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;
338 Receive result of a rpc connection to a rpc pipe on TCP/IP
340 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
342 NTSTATUS status = composite_wait(c);
344 talloc_free(c);
345 return status;
349 struct pipe_unix_state {
350 struct dcerpc_pipe_connect io;
351 const char *path;
356 Stage 2 of ncacn_unix: rpc pipe opened (or not)
358 static void continue_pipe_open_ncacn_unix_stream(struct composite_context *ctx)
360 struct composite_context *c = talloc_get_type(ctx->async.private_data,
361 struct composite_context);
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;
367 composite_done(c);
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
375 static struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX *mem_ctx,
376 struct dcerpc_pipe_connect *io)
378 struct composite_context *c;
379 struct pipe_unix_state *s;
380 struct composite_context *pipe_req;
382 /* composite context allocation and setup */
383 c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
384 if (c == NULL) return NULL;
386 s = talloc_zero(c, struct pipe_unix_state);
387 if (composite_nomem(s, c)) return c;
388 c->private_data = s;
390 /* prepare pipe open parameters and store them in state structure
391 also, verify whether biding endpoint is not null */
392 s->io = *io;
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;
400 s->path = talloc_strdup(c, io->binding->endpoint); /* path is a binding endpoint here */
401 if (composite_nomem(s->path, c)) return c;
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;
411 Receive result of a rpc connection to a pipe on unix socket
413 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
415 NTSTATUS status = composite_wait(c);
417 talloc_free(c);
418 return status;
422 struct pipe_ncalrpc_state {
423 struct dcerpc_pipe_connect io;
426 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c);
429 Stage 2 of ncalrpc: rpc pipe opened (or not)
431 static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
433 struct composite_context *c = talloc_get_type(ctx->async.private_data,
434 struct composite_context);
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;
440 composite_done(c);
445 Initiate async open of a rpc connection request on NCALRPC using
446 the binding structure to determine the endpoint and options
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)
451 struct composite_context *c;
452 struct pipe_ncalrpc_state *s;
453 struct composite_context *pipe_req;
455 /* composite context allocation and setup */
456 c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
457 if (c == NULL) return NULL;
459 s = talloc_zero(c, struct pipe_ncalrpc_state);
460 if (composite_nomem(s, c)) return c;
461 c->private_data = s;
463 /* store input parameters in state structure */
464 s->io = *io;
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;
475 Receive result of a rpc connection to a rpc pipe on NCALRPC
477 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
479 NTSTATUS status = composite_wait(c);
481 talloc_free(c);
482 return status;
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;
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);
507 Stage 2 of pipe_connect_b: Receive result of endpoint mapping
509 static void continue_map_binding(struct composite_context *ctx)
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);
516 c->status = dcerpc_epm_map_binding_recv(ctx);
517 if (!composite_is_ok(c)) return;
519 DEBUG(2,("Mapped to DCERPC endpoint %s\n", s->binding->endpoint));
521 continue_connect(c, s);
526 Stage 2 of pipe_connect_b: Continue connection after endpoint is known
528 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s)
530 struct dcerpc_pipe_connect pc;
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;
539 /* dcerpc pipe connect input parameters */
540 pc.pipe = s->pipe;
541 pc.binding = s->binding;
542 pc.interface = s->table;
543 pc.creds = s->credentials;
544 pc.resolve_ctx = lpcfg_resolve_context(s->lp_ctx);
546 /* connect dcerpc pipe depending on required transport */
547 switch (s->binding->transport) {
548 case NCACN_NP:
549 if (pc.binding->flags & DCERPC_SMB2) {
550 /* new varient of SMB a.k.a. SMB2 */
551 ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc, s->lp_ctx);
552 composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
553 return;
555 } else {
556 /* good old ordinary SMB */
557 ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc, s->lp_ctx);
558 composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
559 return;
561 break;
563 case NCACN_IP_TCP:
564 ncacn_ip_tcp_req = dcerpc_pipe_connect_ncacn_ip_tcp_send(c, &pc);
565 composite_continue(c, ncacn_ip_tcp_req, continue_pipe_connect_ncacn_ip_tcp, c);
566 return;
568 case NCACN_UNIX_STREAM:
569 ncacn_unix_req = dcerpc_pipe_connect_ncacn_unix_stream_send(c, &pc);
570 composite_continue(c, ncacn_unix_req, continue_pipe_connect_ncacn_unix, c);
571 return;
573 case NCALRPC:
574 ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc, s->lp_ctx);
575 composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
576 return;
578 default:
579 /* looks like a transport we don't support now */
580 composite_error(c, NT_STATUS_NOT_SUPPORTED);
586 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
587 named pipe on smb2
589 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx)
591 struct composite_context *c = talloc_get_type(ctx->async.private_data,
592 struct composite_context);
593 struct pipe_connect_state *s = talloc_get_type(c->private_data,
594 struct pipe_connect_state);
596 c->status = dcerpc_pipe_connect_ncacn_np_smb2_recv(ctx);
597 if (!composite_is_ok(c)) return;
599 continue_pipe_connect(c, s);
604 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
605 named pipe on smb
607 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx)
609 struct composite_context *c = talloc_get_type(ctx->async.private_data,
610 struct composite_context);
611 struct pipe_connect_state *s = talloc_get_type(c->private_data,
612 struct pipe_connect_state);
614 c->status = dcerpc_pipe_connect_ncacn_np_smb_recv(ctx);
615 if (!composite_is_ok(c)) return;
617 continue_pipe_connect(c, s);
622 Stage 3 of pipe_connect_b: Receive result of pipe connect request on tcp/ip
624 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx)
626 struct composite_context *c = talloc_get_type(ctx->async.private_data,
627 struct composite_context);
628 struct pipe_connect_state *s = talloc_get_type(c->private_data,
629 struct pipe_connect_state);
631 c->status = dcerpc_pipe_connect_ncacn_ip_tcp_recv(ctx);
632 if (!composite_is_ok(c)) return;
634 continue_pipe_connect(c, s);
639 Stage 3 of pipe_connect_b: Receive result of pipe connect request on unix socket
641 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx)
643 struct composite_context *c = talloc_get_type(ctx->async.private_data,
644 struct composite_context);
645 struct pipe_connect_state *s = talloc_get_type(c->private_data,
646 struct pipe_connect_state);
648 c->status = dcerpc_pipe_connect_ncacn_unix_stream_recv(ctx);
649 if (!composite_is_ok(c)) return;
651 continue_pipe_connect(c, s);
656 Stage 3 of pipe_connect_b: Receive result of pipe connect request on local rpc
658 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx)
660 struct composite_context *c = talloc_get_type(ctx->async.private_data,
661 struct composite_context);
662 struct pipe_connect_state *s = talloc_get_type(c->private_data,
663 struct pipe_connect_state);
665 c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
666 if (!composite_is_ok(c)) return;
668 continue_pipe_connect(c, s);
673 Stage 4 of pipe_connect_b: Start an authentication on connected dcerpc pipe
674 depending on credentials and binding flags passed.
676 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s)
678 struct composite_context *auth_bind_req;
680 s->pipe->binding = s->binding;
681 if (!talloc_reference(s->pipe, s->binding)) {
682 composite_error(c, NT_STATUS_NO_MEMORY);
683 return;
686 auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
687 s->credentials, s->lp_ctx);
688 composite_continue(c, auth_bind_req, continue_pipe_auth, c);
693 Stage 5 of pipe_connect_b: Receive result of pipe authentication request
694 and say if all went ok
696 static void continue_pipe_auth(struct composite_context *ctx)
698 struct composite_context *c = talloc_get_type(ctx->async.private_data,
699 struct composite_context);
700 struct pipe_connect_state *s = talloc_get_type(c->private_data, struct pipe_connect_state);
702 c->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe);
703 if (!composite_is_ok(c)) return;
705 composite_done(c);
710 handle timeouts of a dcerpc connect
712 static void dcerpc_connect_timeout_handler(struct tevent_context *ev, struct tevent_timer *te,
713 struct timeval t, void *private_data)
715 struct composite_context *c = talloc_get_type(private_data, struct composite_context);
716 composite_error(c, NT_STATUS_IO_TIMEOUT);
720 start a request to open a rpc connection to a rpc pipe, using
721 specified binding structure to determine the endpoint and options
723 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
724 struct dcerpc_binding *binding,
725 const struct ndr_interface_table *table,
726 struct cli_credentials *credentials,
727 struct tevent_context *ev,
728 struct loadparm_context *lp_ctx)
730 struct composite_context *c;
731 struct pipe_connect_state *s;
732 struct tevent_context *new_ev = NULL;
734 /* composite context allocation and setup */
735 c = composite_create(parent_ctx, ev);
736 if (c == NULL) {
737 talloc_free(new_ev);
738 return NULL;
740 talloc_steal(c, new_ev);
742 s = talloc_zero(c, struct pipe_connect_state);
743 if (composite_nomem(s, c)) return c;
744 c->private_data = s;
746 /* initialise dcerpc pipe structure */
747 s->pipe = dcerpc_pipe_init(c, ev);
748 if (composite_nomem(s->pipe, c)) return c;
750 if (DEBUGLEVEL >= 10)
751 s->pipe->conn->packet_log_dir = lpcfg_lockdir(lp_ctx);
753 /* store parameters in state structure */
754 s->binding = binding;
755 s->table = table;
756 s->credentials = credentials;
757 s->lp_ctx = lp_ctx;
759 event_add_timed(c->event_ctx, c,
760 timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
761 dcerpc_connect_timeout_handler, c);
763 switch (s->binding->transport) {
764 case NCA_UNKNOWN: {
765 struct composite_context *binding_req;
766 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
767 s->pipe->conn->event_ctx,
768 s->lp_ctx);
769 composite_continue(c, binding_req, continue_map_binding, c);
770 return c;
773 case NCACN_NP:
774 case NCACN_IP_TCP:
775 case NCALRPC:
776 if (!s->binding->endpoint) {
777 struct composite_context *binding_req;
778 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
779 s->pipe->conn->event_ctx,
780 s->lp_ctx);
781 composite_continue(c, binding_req, continue_map_binding, c);
782 return c;
785 default:
786 break;
789 continue_connect(c, s);
790 return c;
795 receive result of a request to open a rpc connection to a rpc pipe
797 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
798 struct dcerpc_pipe **p)
800 NTSTATUS status;
801 struct pipe_connect_state *s;
803 status = composite_wait(c);
805 if (NT_STATUS_IS_OK(status)) {
806 s = talloc_get_type(c->private_data, struct pipe_connect_state);
807 talloc_steal(mem_ctx, s->pipe);
808 *p = s->pipe;
810 talloc_free(c);
811 return status;
816 open a rpc connection to a rpc pipe, using the specified
817 binding structure to determine the endpoint and options - sync version
819 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
820 struct dcerpc_pipe **pp,
821 struct dcerpc_binding *binding,
822 const struct ndr_interface_table *table,
823 struct cli_credentials *credentials,
824 struct tevent_context *ev,
825 struct loadparm_context *lp_ctx)
827 struct composite_context *c;
829 c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
830 credentials, ev, lp_ctx);
831 return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
835 struct pipe_conn_state {
836 struct dcerpc_pipe *pipe;
840 static void continue_pipe_connect_b(struct composite_context *ctx);
844 Initiate rpc connection to a rpc pipe, using the specified string
845 binding to determine the endpoint and options.
846 The string is to be parsed to a binding structure first.
848 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
849 const char *binding,
850 const struct ndr_interface_table *table,
851 struct cli_credentials *credentials,
852 struct tevent_context *ev, struct loadparm_context *lp_ctx)
854 struct composite_context *c;
855 struct pipe_conn_state *s;
856 struct dcerpc_binding *b;
857 struct composite_context *pipe_conn_req;
859 /* composite context allocation and setup */
860 c = composite_create(parent_ctx, ev);
861 if (c == NULL) {
862 return NULL;
865 s = talloc_zero(c, struct pipe_conn_state);
866 if (composite_nomem(s, c)) return c;
867 c->private_data = s;
869 /* parse binding string to the structure */
870 c->status = dcerpc_parse_binding(c, binding, &b);
871 if (!NT_STATUS_IS_OK(c->status)) {
872 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
873 composite_error(c, c->status);
874 return c;
877 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(c, b)));
880 start connecting to a rpc pipe after binding structure
881 is established
883 pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
884 credentials, ev, lp_ctx);
885 composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
886 return c;
891 Stage 2 of pipe_connect: Receive result of actual pipe connect request
892 and say if we're done ok
894 static void continue_pipe_connect_b(struct composite_context *ctx)
896 struct composite_context *c = talloc_get_type(ctx->async.private_data,
897 struct composite_context);
898 struct pipe_conn_state *s = talloc_get_type(c->private_data,
899 struct pipe_conn_state);
901 c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
902 talloc_steal(s, s->pipe);
903 if (!composite_is_ok(c)) return;
905 composite_done(c);
910 Receive result of pipe connect (using binding string) request
911 and return connected pipe structure.
913 NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
914 TALLOC_CTX *mem_ctx,
915 struct dcerpc_pipe **pp)
917 NTSTATUS status;
918 struct pipe_conn_state *s;
920 status = composite_wait(c);
921 if (NT_STATUS_IS_OK(status)) {
922 s = talloc_get_type(c->private_data, struct pipe_conn_state);
923 *pp = talloc_steal(mem_ctx, s->pipe);
925 talloc_free(c);
926 return status;
931 Open a rpc connection to a rpc pipe, using the specified string
932 binding to determine the endpoint and options - sync version
934 _PUBLIC_ NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx,
935 struct dcerpc_pipe **pp,
936 const char *binding,
937 const struct ndr_interface_table *table,
938 struct cli_credentials *credentials,
939 struct tevent_context *ev,
940 struct loadparm_context *lp_ctx)
942 struct composite_context *c;
943 c = dcerpc_pipe_connect_send(parent_ctx, binding,
944 table, credentials, ev, lp_ctx);
945 return dcerpc_pipe_connect_recv(c, parent_ctx, pp);