s4:librpc: avoid talloc_reference() in dcerpc_epm_map_binding_send()
[Samba.git] / source4 / librpc / rpc / dcerpc_connect.c
blobeb53a115e3c104d267d0206202863e791a7bd036
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 tevent_req *subreq)
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);
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;
203 /* prepare named pipe open parameters */
204 s->io.pipe_name = s->io.binding->endpoint;
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;
210 composite_continue(c, open_req, continue_pipe_open_smb2, c);
215 Initiate async open of a rpc connection request on SMB2 using
216 the binding structure to determine the endpoint and options
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)
223 struct composite_context *c;
224 struct pipe_np_smb2_state *s;
225 struct tevent_req *subreq;
226 struct smbcli_options options;
228 /* composite context allocation and setup */
229 c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
230 if (c == NULL) return NULL;
232 s = talloc_zero(c, struct pipe_np_smb2_state);
233 if (composite_nomem(s, c)) return c;
234 c->private_data = s;
236 s->io = *io;
239 * provide proper credentials - user supplied or anonymous in case this is
240 * schannel connection
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;
246 cli_credentials_guess(s->io.creds, lp_ctx);
249 lpcfg_smbcli_options(lp_ctx, &options);
251 /* send smb2 connect request */
252 subreq = smb2_connect_send(s, c->event_ctx,
253 s->io.binding->host,
254 lpcfg_parm_string_list(mem_ctx, lp_ctx, NULL, "smb2", "ports", NULL),
255 "IPC$",
256 s->io.resolve_ctx,
257 s->io.creds,
258 0, /* previous_session_id */
259 &options,
260 lpcfg_socket_options(lp_ctx),
261 lpcfg_gensec_settings(mem_ctx, lp_ctx));
262 if (composite_nomem(subreq, c)) return c;
263 tevent_req_set_callback(subreq, continue_smb2_connect, c);
264 return c;
269 Receive result of a rpc connection to a rpc pipe on SMB2
271 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
273 NTSTATUS status = composite_wait(c);
275 talloc_free(c);
276 return status;
280 struct pipe_ip_tcp_state {
281 struct dcerpc_pipe_connect io;
282 const char *localaddr;
283 const char *host;
284 const char *target_hostname;
285 uint32_t port;
290 Stage 2 of ncacn_ip_tcp: rpc pipe opened (or not)
292 static void continue_pipe_open_ncacn_ip_tcp(struct composite_context *ctx)
294 struct composite_context *c = talloc_get_type(ctx->async.private_data,
295 struct composite_context);
297 /* receive result of named pipe open request on tcp/ip */
298 c->status = dcerpc_pipe_open_tcp_recv(ctx);
299 if (!composite_is_ok(c)) return;
301 composite_done(c);
306 Initiate async open of a rpc connection to a rpc pipe on TCP/IP using
307 the binding structure to determine the endpoint and options
309 static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_ctx,
310 struct dcerpc_pipe_connect *io)
312 struct composite_context *c;
313 struct pipe_ip_tcp_state *s;
314 struct composite_context *pipe_req;
316 /* composite context allocation and setup */
317 c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
318 if (c == NULL) return NULL;
320 s = talloc_zero(c, struct pipe_ip_tcp_state);
321 if (composite_nomem(s, c)) return c;
322 c->private_data = s;
324 /* store input parameters in state structure */
325 s->io = *io;
326 if (io->binding->localaddress != NULL) {
327 s->localaddr = talloc_strdup(s, io->binding->localaddress);
328 if (composite_nomem(s->localaddr, c)) return c;
330 if (io->binding->host != NULL) {
331 s->host = talloc_strdup(s, io->binding->host);
332 if (composite_nomem(s->host, c)) return c;
334 if (io->binding->target_hostname != NULL) {
335 s->target_hostname = talloc_strdup(s, io->binding->target_hostname);
336 if (composite_nomem(s->target_hostname, c)) return c;
338 /* port number is a binding endpoint here */
339 s->port = atoi(io->binding->endpoint);
341 /* send pipe open request on tcp/ip */
342 pipe_req = dcerpc_pipe_open_tcp_send(s->io.pipe->conn, s->localaddr, s->host, s->target_hostname,
343 s->port, io->resolve_ctx);
344 composite_continue(c, pipe_req, continue_pipe_open_ncacn_ip_tcp, c);
345 return c;
350 Receive result of a rpc connection to a rpc pipe on TCP/IP
352 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
354 NTSTATUS status = composite_wait(c);
356 talloc_free(c);
357 return status;
361 struct pipe_unix_state {
362 struct dcerpc_pipe_connect io;
363 const char *path;
368 Stage 2 of ncacn_unix: rpc pipe opened (or not)
370 static void continue_pipe_open_ncacn_unix_stream(struct composite_context *ctx)
372 struct composite_context *c = talloc_get_type(ctx->async.private_data,
373 struct composite_context);
375 /* receive result of pipe open request on unix socket */
376 c->status = dcerpc_pipe_open_unix_stream_recv(ctx);
377 if (!composite_is_ok(c)) return;
379 composite_done(c);
384 Initiate async open of a rpc connection to a rpc pipe on unix socket using
385 the binding structure to determine the endpoint and options
387 static struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX *mem_ctx,
388 struct dcerpc_pipe_connect *io)
390 struct composite_context *c;
391 struct pipe_unix_state *s;
392 struct composite_context *pipe_req;
394 /* composite context allocation and setup */
395 c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
396 if (c == NULL) return NULL;
398 s = talloc_zero(c, struct pipe_unix_state);
399 if (composite_nomem(s, c)) return c;
400 c->private_data = s;
402 /* prepare pipe open parameters and store them in state structure
403 also, verify whether biding endpoint is not null */
404 s->io = *io;
406 if (!io->binding->endpoint) {
407 DEBUG(0, ("Path to unix socket not specified\n"));
408 composite_error(c, NT_STATUS_INVALID_PARAMETER);
409 return c;
412 s->path = talloc_strdup(c, io->binding->endpoint); /* path is a binding endpoint here */
413 if (composite_nomem(s->path, c)) return c;
415 /* send pipe open request on unix socket */
416 pipe_req = dcerpc_pipe_open_unix_stream_send(s->io.pipe->conn, s->path);
417 composite_continue(c, pipe_req, continue_pipe_open_ncacn_unix_stream, c);
418 return c;
423 Receive result of a rpc connection to a pipe on unix socket
425 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
427 NTSTATUS status = composite_wait(c);
429 talloc_free(c);
430 return status;
434 struct pipe_ncalrpc_state {
435 struct dcerpc_pipe_connect io;
438 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c);
441 Stage 2 of ncalrpc: rpc pipe opened (or not)
443 static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
445 struct composite_context *c = talloc_get_type(ctx->async.private_data,
446 struct composite_context);
448 /* receive result of pipe open request on ncalrpc */
449 c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
450 if (!composite_is_ok(c)) return;
452 composite_done(c);
457 Initiate async open of a rpc connection request on NCALRPC using
458 the binding structure to determine the endpoint and options
460 static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
461 struct dcerpc_pipe_connect *io, struct loadparm_context *lp_ctx)
463 struct composite_context *c;
464 struct pipe_ncalrpc_state *s;
465 struct composite_context *pipe_req;
467 /* composite context allocation and setup */
468 c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
469 if (c == NULL) return NULL;
471 s = talloc_zero(c, struct pipe_ncalrpc_state);
472 if (composite_nomem(s, c)) return c;
473 c->private_data = s;
475 /* store input parameters in state structure */
476 s->io = *io;
478 /* send pipe open request */
479 pipe_req = dcerpc_pipe_open_pipe_send(s->io.pipe->conn, lpcfg_ncalrpc_dir(lp_ctx),
480 s->io.binding->endpoint);
481 composite_continue(c, pipe_req, continue_pipe_open_ncalrpc, c);
482 return c;
487 Receive result of a rpc connection to a rpc pipe on NCALRPC
489 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
491 NTSTATUS status = composite_wait(c);
493 talloc_free(c);
494 return status;
498 struct pipe_connect_state {
499 struct dcerpc_pipe *pipe;
500 struct dcerpc_binding *binding;
501 const struct ndr_interface_table *table;
502 struct cli_credentials *credentials;
503 struct loadparm_context *lp_ctx;
507 static void continue_map_binding(struct composite_context *ctx);
508 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s);
509 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx);
510 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx);
511 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx);
512 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx);
513 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx);
514 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s);
515 static void continue_pipe_auth(struct composite_context *ctx);
519 Stage 2 of pipe_connect_b: Receive result of endpoint mapping
521 static void continue_map_binding(struct composite_context *ctx)
523 struct composite_context *c = talloc_get_type(ctx->async.private_data,
524 struct composite_context);
525 struct pipe_connect_state *s = talloc_get_type(c->private_data,
526 struct pipe_connect_state);
528 c->status = dcerpc_epm_map_binding_recv(ctx);
529 if (!composite_is_ok(c)) return;
531 DEBUG(4,("Mapped to DCERPC endpoint %s\n", s->binding->endpoint));
533 continue_connect(c, s);
538 Stage 2 of pipe_connect_b: Continue connection after endpoint is known
540 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s)
542 struct dcerpc_pipe_connect pc;
544 /* potential exits to another stage by sending an async request */
545 struct composite_context *ncacn_np_smb2_req;
546 struct composite_context *ncacn_np_smb_req;
547 struct composite_context *ncacn_ip_tcp_req;
548 struct composite_context *ncacn_unix_req;
549 struct composite_context *ncalrpc_req;
551 /* dcerpc pipe connect input parameters */
552 pc.pipe = s->pipe;
553 pc.binding = s->binding;
554 pc.pipe_name = NULL;
555 pc.interface = s->table;
556 pc.creds = s->credentials;
557 pc.resolve_ctx = lpcfg_resolve_context(s->lp_ctx);
559 /* connect dcerpc pipe depending on required transport */
560 switch (s->binding->transport) {
561 case NCACN_NP:
562 if (pc.binding->flags & DCERPC_SMB2) {
563 /* new varient of SMB a.k.a. SMB2 */
564 ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc, s->lp_ctx);
565 composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
566 return;
568 } else {
569 /* good old ordinary SMB */
570 ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc, s->lp_ctx);
571 composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
572 return;
574 break;
576 case NCACN_IP_TCP:
577 ncacn_ip_tcp_req = dcerpc_pipe_connect_ncacn_ip_tcp_send(c, &pc);
578 composite_continue(c, ncacn_ip_tcp_req, continue_pipe_connect_ncacn_ip_tcp, c);
579 return;
581 case NCACN_UNIX_STREAM:
582 ncacn_unix_req = dcerpc_pipe_connect_ncacn_unix_stream_send(c, &pc);
583 composite_continue(c, ncacn_unix_req, continue_pipe_connect_ncacn_unix, c);
584 return;
586 case NCALRPC:
587 ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc, s->lp_ctx);
588 composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
589 return;
591 default:
592 /* looks like a transport we don't support now */
593 composite_error(c, NT_STATUS_NOT_SUPPORTED);
599 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
600 named pipe on smb2
602 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx)
604 struct composite_context *c = talloc_get_type(ctx->async.private_data,
605 struct composite_context);
606 struct pipe_connect_state *s = talloc_get_type(c->private_data,
607 struct pipe_connect_state);
609 c->status = dcerpc_pipe_connect_ncacn_np_smb2_recv(ctx);
610 if (!composite_is_ok(c)) return;
612 continue_pipe_connect(c, s);
617 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
618 named pipe on smb
620 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx)
622 struct composite_context *c = talloc_get_type(ctx->async.private_data,
623 struct composite_context);
624 struct pipe_connect_state *s = talloc_get_type(c->private_data,
625 struct pipe_connect_state);
627 c->status = dcerpc_pipe_connect_ncacn_np_smb_recv(ctx);
628 if (!composite_is_ok(c)) return;
630 continue_pipe_connect(c, s);
635 Stage 3 of pipe_connect_b: Receive result of pipe connect request on tcp/ip
637 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx)
639 struct composite_context *c = talloc_get_type(ctx->async.private_data,
640 struct composite_context);
641 struct pipe_connect_state *s = talloc_get_type(c->private_data,
642 struct pipe_connect_state);
644 c->status = dcerpc_pipe_connect_ncacn_ip_tcp_recv(ctx);
645 if (!composite_is_ok(c)) return;
647 continue_pipe_connect(c, s);
652 Stage 3 of pipe_connect_b: Receive result of pipe connect request on unix socket
654 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx)
656 struct composite_context *c = talloc_get_type(ctx->async.private_data,
657 struct composite_context);
658 struct pipe_connect_state *s = talloc_get_type(c->private_data,
659 struct pipe_connect_state);
661 c->status = dcerpc_pipe_connect_ncacn_unix_stream_recv(ctx);
662 if (!composite_is_ok(c)) return;
664 continue_pipe_connect(c, s);
669 Stage 3 of pipe_connect_b: Receive result of pipe connect request on local rpc
671 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx)
673 struct composite_context *c = talloc_get_type(ctx->async.private_data,
674 struct composite_context);
675 struct pipe_connect_state *s = talloc_get_type(c->private_data,
676 struct pipe_connect_state);
678 c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
679 if (!composite_is_ok(c)) return;
681 continue_pipe_connect(c, s);
686 Stage 4 of pipe_connect_b: Start an authentication on connected dcerpc pipe
687 depending on credentials and binding flags passed.
689 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s)
691 struct composite_context *auth_bind_req;
693 s->pipe->binding = s->binding;
694 if (!talloc_reference(s->pipe, s->binding)) {
695 composite_error(c, NT_STATUS_NO_MEMORY);
696 return;
699 auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
700 s->credentials, s->lp_ctx);
701 composite_continue(c, auth_bind_req, continue_pipe_auth, c);
706 Stage 5 of pipe_connect_b: Receive result of pipe authentication request
707 and say if all went ok
709 static void continue_pipe_auth(struct composite_context *ctx)
711 struct composite_context *c = talloc_get_type(ctx->async.private_data,
712 struct composite_context);
713 struct pipe_connect_state *s = talloc_get_type(c->private_data, struct pipe_connect_state);
715 c->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe);
716 if (!composite_is_ok(c)) return;
718 composite_done(c);
723 handle timeouts of a dcerpc connect
725 static void dcerpc_connect_timeout_handler(struct tevent_context *ev, struct tevent_timer *te,
726 struct timeval t, void *private_data)
728 struct composite_context *c = talloc_get_type_abort(private_data,
729 struct composite_context);
730 struct pipe_connect_state *s = talloc_get_type_abort(c->private_data, struct pipe_connect_state);
731 if (!s->pipe->inhibit_timeout_processing) {
732 composite_error(c, NT_STATUS_IO_TIMEOUT);
733 } else {
734 s->pipe->timed_out = true;
739 start a request to open a rpc connection to a rpc pipe, using
740 specified binding structure to determine the endpoint and options
742 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
743 struct dcerpc_binding *binding,
744 const struct ndr_interface_table *table,
745 struct cli_credentials *credentials,
746 struct tevent_context *ev,
747 struct loadparm_context *lp_ctx)
749 struct composite_context *c;
750 struct pipe_connect_state *s;
752 /* composite context allocation and setup */
753 c = composite_create(parent_ctx, ev);
754 if (c == NULL) {
755 return NULL;
758 s = talloc_zero(c, struct pipe_connect_state);
759 if (composite_nomem(s, c)) return c;
760 c->private_data = s;
762 /* initialise dcerpc pipe structure */
763 s->pipe = dcerpc_pipe_init(c, ev);
764 if (composite_nomem(s->pipe, c)) return c;
766 if (DEBUGLEVEL >= 10)
767 s->pipe->conn->packet_log_dir = lpcfg_lockdir(lp_ctx);
769 /* store parameters in state structure */
770 s->binding = binding;
771 s->table = table;
772 s->credentials = credentials;
773 s->lp_ctx = lp_ctx;
775 s->pipe->timed_out = false;
776 s->pipe->inhibit_timeout_processing = false;
778 tevent_add_timer(c->event_ctx, c,
779 timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
780 dcerpc_connect_timeout_handler, c);
782 switch (s->binding->transport) {
783 case NCA_UNKNOWN: {
784 struct composite_context *binding_req;
785 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
786 s->pipe->conn->event_ctx,
787 s->lp_ctx);
788 composite_continue(c, binding_req, continue_map_binding, c);
789 return c;
792 case NCACN_NP:
793 case NCACN_IP_TCP:
794 case NCALRPC:
795 if (!s->binding->endpoint) {
796 struct composite_context *binding_req;
797 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
798 s->pipe->conn->event_ctx,
799 s->lp_ctx);
800 composite_continue(c, binding_req, continue_map_binding, c);
801 return c;
804 default:
805 break;
808 continue_connect(c, s);
809 return c;
814 receive result of a request to open a rpc connection to a rpc pipe
816 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
817 struct dcerpc_pipe **p)
819 NTSTATUS status;
820 struct pipe_connect_state *s;
822 status = composite_wait(c);
824 if (NT_STATUS_IS_OK(status)) {
825 s = talloc_get_type(c->private_data, struct pipe_connect_state);
826 talloc_steal(mem_ctx, s->pipe);
827 *p = s->pipe;
829 talloc_free(c);
830 return status;
835 open a rpc connection to a rpc pipe, using the specified
836 binding structure to determine the endpoint and options - sync version
838 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
839 struct dcerpc_pipe **pp,
840 struct dcerpc_binding *binding,
841 const struct ndr_interface_table *table,
842 struct cli_credentials *credentials,
843 struct tevent_context *ev,
844 struct loadparm_context *lp_ctx)
846 struct composite_context *c;
848 c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
849 credentials, ev, lp_ctx);
850 return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
854 struct pipe_conn_state {
855 struct dcerpc_pipe *pipe;
859 static void continue_pipe_connect_b(struct composite_context *ctx);
863 Initiate rpc connection to a rpc pipe, using the specified string
864 binding to determine the endpoint and options.
865 The string is to be parsed to a binding structure first.
867 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
868 const char *binding,
869 const struct ndr_interface_table *table,
870 struct cli_credentials *credentials,
871 struct tevent_context *ev, struct loadparm_context *lp_ctx)
873 struct composite_context *c;
874 struct pipe_conn_state *s;
875 struct dcerpc_binding *b;
876 struct composite_context *pipe_conn_req;
878 /* composite context allocation and setup */
879 c = composite_create(parent_ctx, ev);
880 if (c == NULL) {
881 return NULL;
884 s = talloc_zero(c, struct pipe_conn_state);
885 if (composite_nomem(s, c)) return c;
886 c->private_data = s;
888 /* parse binding string to the structure */
889 c->status = dcerpc_parse_binding(c, binding, &b);
890 if (!NT_STATUS_IS_OK(c->status)) {
891 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
892 composite_error(c, c->status);
893 return c;
896 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(c, b)));
899 start connecting to a rpc pipe after binding structure
900 is established
902 pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
903 credentials, ev, lp_ctx);
904 composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
905 return c;
910 Stage 2 of pipe_connect: Receive result of actual pipe connect request
911 and say if we're done ok
913 static void continue_pipe_connect_b(struct composite_context *ctx)
915 struct composite_context *c = talloc_get_type(ctx->async.private_data,
916 struct composite_context);
917 struct pipe_conn_state *s = talloc_get_type(c->private_data,
918 struct pipe_conn_state);
920 c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
921 talloc_steal(s, s->pipe);
922 if (!composite_is_ok(c)) return;
924 composite_done(c);
929 Receive result of pipe connect (using binding string) request
930 and return connected pipe structure.
932 NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
933 TALLOC_CTX *mem_ctx,
934 struct dcerpc_pipe **pp)
936 NTSTATUS status;
937 struct pipe_conn_state *s;
939 status = composite_wait(c);
940 if (NT_STATUS_IS_OK(status)) {
941 s = talloc_get_type(c->private_data, struct pipe_conn_state);
942 *pp = talloc_steal(mem_ctx, s->pipe);
944 talloc_free(c);
945 return status;
950 Open a rpc connection to a rpc pipe, using the specified string
951 binding to determine the endpoint and options - sync version
953 _PUBLIC_ NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx,
954 struct dcerpc_pipe **pp,
955 const char *binding,
956 const struct ndr_interface_table *table,
957 struct cli_credentials *credentials,
958 struct tevent_context *ev,
959 struct loadparm_context *lp_ctx)
961 struct composite_context *c;
962 c = dcerpc_pipe_connect_send(parent_ctx, binding,
963 table, credentials, ev, lp_ctx);
964 return dcerpc_pipe_connect_recv(c, parent_ctx, pp);