build: added WHYNEEDED=TARGET:DEPENDENCY
[Samba.git] / source4 / librpc / rpc / dcerpc_connect.c
blob842ef4320631013dc7c0c0702c4aab158323e3ff
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(4,("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.pipe_name = NULL;
543 pc.interface = s->table;
544 pc.creds = s->credentials;
545 pc.resolve_ctx = lpcfg_resolve_context(s->lp_ctx);
547 /* connect dcerpc pipe depending on required transport */
548 switch (s->binding->transport) {
549 case NCACN_NP:
550 if (pc.binding->flags & DCERPC_SMB2) {
551 /* new varient of SMB a.k.a. SMB2 */
552 ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc, s->lp_ctx);
553 composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
554 return;
556 } else {
557 /* good old ordinary SMB */
558 ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc, s->lp_ctx);
559 composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
560 return;
562 break;
564 case NCACN_IP_TCP:
565 ncacn_ip_tcp_req = dcerpc_pipe_connect_ncacn_ip_tcp_send(c, &pc);
566 composite_continue(c, ncacn_ip_tcp_req, continue_pipe_connect_ncacn_ip_tcp, c);
567 return;
569 case NCACN_UNIX_STREAM:
570 ncacn_unix_req = dcerpc_pipe_connect_ncacn_unix_stream_send(c, &pc);
571 composite_continue(c, ncacn_unix_req, continue_pipe_connect_ncacn_unix, c);
572 return;
574 case NCALRPC:
575 ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc, s->lp_ctx);
576 composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
577 return;
579 default:
580 /* looks like a transport we don't support now */
581 composite_error(c, NT_STATUS_NOT_SUPPORTED);
587 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
588 named pipe on smb2
590 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx)
592 struct composite_context *c = talloc_get_type(ctx->async.private_data,
593 struct composite_context);
594 struct pipe_connect_state *s = talloc_get_type(c->private_data,
595 struct pipe_connect_state);
597 c->status = dcerpc_pipe_connect_ncacn_np_smb2_recv(ctx);
598 if (!composite_is_ok(c)) return;
600 continue_pipe_connect(c, s);
605 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
606 named pipe on smb
608 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx)
610 struct composite_context *c = talloc_get_type(ctx->async.private_data,
611 struct composite_context);
612 struct pipe_connect_state *s = talloc_get_type(c->private_data,
613 struct pipe_connect_state);
615 c->status = dcerpc_pipe_connect_ncacn_np_smb_recv(ctx);
616 if (!composite_is_ok(c)) return;
618 continue_pipe_connect(c, s);
623 Stage 3 of pipe_connect_b: Receive result of pipe connect request on tcp/ip
625 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx)
627 struct composite_context *c = talloc_get_type(ctx->async.private_data,
628 struct composite_context);
629 struct pipe_connect_state *s = talloc_get_type(c->private_data,
630 struct pipe_connect_state);
632 c->status = dcerpc_pipe_connect_ncacn_ip_tcp_recv(ctx);
633 if (!composite_is_ok(c)) return;
635 continue_pipe_connect(c, s);
640 Stage 3 of pipe_connect_b: Receive result of pipe connect request on unix socket
642 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx)
644 struct composite_context *c = talloc_get_type(ctx->async.private_data,
645 struct composite_context);
646 struct pipe_connect_state *s = talloc_get_type(c->private_data,
647 struct pipe_connect_state);
649 c->status = dcerpc_pipe_connect_ncacn_unix_stream_recv(ctx);
650 if (!composite_is_ok(c)) return;
652 continue_pipe_connect(c, s);
657 Stage 3 of pipe_connect_b: Receive result of pipe connect request on local rpc
659 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx)
661 struct composite_context *c = talloc_get_type(ctx->async.private_data,
662 struct composite_context);
663 struct pipe_connect_state *s = talloc_get_type(c->private_data,
664 struct pipe_connect_state);
666 c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
667 if (!composite_is_ok(c)) return;
669 continue_pipe_connect(c, s);
674 Stage 4 of pipe_connect_b: Start an authentication on connected dcerpc pipe
675 depending on credentials and binding flags passed.
677 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s)
679 struct composite_context *auth_bind_req;
681 s->pipe->binding = s->binding;
682 if (!talloc_reference(s->pipe, s->binding)) {
683 composite_error(c, NT_STATUS_NO_MEMORY);
684 return;
687 auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
688 s->credentials, s->lp_ctx);
689 composite_continue(c, auth_bind_req, continue_pipe_auth, c);
694 Stage 5 of pipe_connect_b: Receive result of pipe authentication request
695 and say if all went ok
697 static void continue_pipe_auth(struct composite_context *ctx)
699 struct composite_context *c = talloc_get_type(ctx->async.private_data,
700 struct composite_context);
701 struct pipe_connect_state *s = talloc_get_type(c->private_data, struct pipe_connect_state);
703 c->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe);
704 if (!composite_is_ok(c)) return;
706 composite_done(c);
711 handle timeouts of a dcerpc connect
713 static void dcerpc_connect_timeout_handler(struct tevent_context *ev, struct tevent_timer *te,
714 struct timeval t, void *private_data)
716 struct composite_context *c = talloc_get_type(private_data, struct composite_context);
717 composite_error(c, NT_STATUS_IO_TIMEOUT);
721 start a request to open a rpc connection to a rpc pipe, using
722 specified binding structure to determine the endpoint and options
724 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
725 struct dcerpc_binding *binding,
726 const struct ndr_interface_table *table,
727 struct cli_credentials *credentials,
728 struct tevent_context *ev,
729 struct loadparm_context *lp_ctx)
731 struct composite_context *c;
732 struct pipe_connect_state *s;
733 struct tevent_context *new_ev = NULL;
735 /* composite context allocation and setup */
736 c = composite_create(parent_ctx, ev);
737 if (c == NULL) {
738 talloc_free(new_ev);
739 return NULL;
741 talloc_steal(c, new_ev);
743 s = talloc_zero(c, struct pipe_connect_state);
744 if (composite_nomem(s, c)) return c;
745 c->private_data = s;
747 /* initialise dcerpc pipe structure */
748 s->pipe = dcerpc_pipe_init(c, ev);
749 if (composite_nomem(s->pipe, c)) return c;
751 if (DEBUGLEVEL >= 10)
752 s->pipe->conn->packet_log_dir = lpcfg_lockdir(lp_ctx);
754 /* store parameters in state structure */
755 s->binding = binding;
756 s->table = table;
757 s->credentials = credentials;
758 s->lp_ctx = lp_ctx;
760 event_add_timed(c->event_ctx, c,
761 timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
762 dcerpc_connect_timeout_handler, c);
764 switch (s->binding->transport) {
765 case NCA_UNKNOWN: {
766 struct composite_context *binding_req;
767 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
768 s->pipe->conn->event_ctx,
769 s->lp_ctx);
770 composite_continue(c, binding_req, continue_map_binding, c);
771 return c;
774 case NCACN_NP:
775 case NCACN_IP_TCP:
776 case NCALRPC:
777 if (!s->binding->endpoint) {
778 struct composite_context *binding_req;
779 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
780 s->pipe->conn->event_ctx,
781 s->lp_ctx);
782 composite_continue(c, binding_req, continue_map_binding, c);
783 return c;
786 default:
787 break;
790 continue_connect(c, s);
791 return c;
796 receive result of a request to open a rpc connection to a rpc pipe
798 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
799 struct dcerpc_pipe **p)
801 NTSTATUS status;
802 struct pipe_connect_state *s;
804 status = composite_wait(c);
806 if (NT_STATUS_IS_OK(status)) {
807 s = talloc_get_type(c->private_data, struct pipe_connect_state);
808 talloc_steal(mem_ctx, s->pipe);
809 *p = s->pipe;
811 talloc_free(c);
812 return status;
817 open a rpc connection to a rpc pipe, using the specified
818 binding structure to determine the endpoint and options - sync version
820 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
821 struct dcerpc_pipe **pp,
822 struct dcerpc_binding *binding,
823 const struct ndr_interface_table *table,
824 struct cli_credentials *credentials,
825 struct tevent_context *ev,
826 struct loadparm_context *lp_ctx)
828 struct composite_context *c;
830 c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
831 credentials, ev, lp_ctx);
832 return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
836 struct pipe_conn_state {
837 struct dcerpc_pipe *pipe;
841 static void continue_pipe_connect_b(struct composite_context *ctx);
845 Initiate rpc connection to a rpc pipe, using the specified string
846 binding to determine the endpoint and options.
847 The string is to be parsed to a binding structure first.
849 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
850 const char *binding,
851 const struct ndr_interface_table *table,
852 struct cli_credentials *credentials,
853 struct tevent_context *ev, struct loadparm_context *lp_ctx)
855 struct composite_context *c;
856 struct pipe_conn_state *s;
857 struct dcerpc_binding *b;
858 struct composite_context *pipe_conn_req;
860 /* composite context allocation and setup */
861 c = composite_create(parent_ctx, ev);
862 if (c == NULL) {
863 return NULL;
866 s = talloc_zero(c, struct pipe_conn_state);
867 if (composite_nomem(s, c)) return c;
868 c->private_data = s;
870 /* parse binding string to the structure */
871 c->status = dcerpc_parse_binding(c, binding, &b);
872 if (!NT_STATUS_IS_OK(c->status)) {
873 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
874 composite_error(c, c->status);
875 return c;
878 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(c, b)));
881 start connecting to a rpc pipe after binding structure
882 is established
884 pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
885 credentials, ev, lp_ctx);
886 composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
887 return c;
892 Stage 2 of pipe_connect: Receive result of actual pipe connect request
893 and say if we're done ok
895 static void continue_pipe_connect_b(struct composite_context *ctx)
897 struct composite_context *c = talloc_get_type(ctx->async.private_data,
898 struct composite_context);
899 struct pipe_conn_state *s = talloc_get_type(c->private_data,
900 struct pipe_conn_state);
902 c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
903 talloc_steal(s, s->pipe);
904 if (!composite_is_ok(c)) return;
906 composite_done(c);
911 Receive result of pipe connect (using binding string) request
912 and return connected pipe structure.
914 NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
915 TALLOC_CTX *mem_ctx,
916 struct dcerpc_pipe **pp)
918 NTSTATUS status;
919 struct pipe_conn_state *s;
921 status = composite_wait(c);
922 if (NT_STATUS_IS_OK(status)) {
923 s = talloc_get_type(c->private_data, struct pipe_conn_state);
924 *pp = talloc_steal(mem_ctx, s->pipe);
926 talloc_free(c);
927 return status;
932 Open a rpc connection to a rpc pipe, using the specified string
933 binding to determine the endpoint and options - sync version
935 _PUBLIC_ NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx,
936 struct dcerpc_pipe **pp,
937 const char *binding,
938 const struct ndr_interface_table *table,
939 struct cli_credentials *credentials,
940 struct tevent_context *ev,
941 struct loadparm_context *lp_ctx)
943 struct composite_context *c;
944 c = dcerpc_pipe_connect_send(parent_ctx, binding,
945 table, credentials, ev, lp_ctx);
946 return dcerpc_pipe_connect_recv(c, parent_ctx, pp);