s4:librpc/rpc: remember some smbXcli_* pointers within struct dcerpc_pipe_connect
[Samba.git] / source4 / librpc / rpc / dcerpc_connect.c
blob7b1ed48db8c589c31828d03717990f26c3f418a1
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 "libcli/smb/smbXcli_base.h"
33 #include "librpc/rpc/dcerpc.h"
34 #include "librpc/rpc/dcerpc_proto.h"
35 #include "auth/credentials/credentials.h"
36 #include "param/param.h"
37 #include "libcli/resolve/resolve.h"
39 struct dcerpc_pipe_connect {
40 struct dcecli_connection *conn;
41 struct dcerpc_binding *binding;
42 const struct ndr_interface_table *interface;
43 struct cli_credentials *creds;
44 struct resolve_context *resolve_ctx;
45 struct {
46 const char *dir;
47 } ncalrpc;
48 struct {
49 struct smbXcli_conn *conn;
50 struct smbXcli_session *session;
51 struct smbXcli_tcon *tcon;
52 const char *pipe_name;
53 } smb;
56 struct pipe_np_smb_state {
57 struct smb_composite_connect conn;
58 struct dcerpc_pipe_connect io;
63 Stage 3 of ncacn_np_smb: Named pipe opened (or not)
65 static void continue_pipe_open_smb(struct composite_context *ctx)
67 struct composite_context *c = talloc_get_type(ctx->async.private_data,
68 struct composite_context);
70 /* receive result of named pipe open request on smb */
71 c->status = dcerpc_pipe_open_smb_recv(ctx);
72 if (!composite_is_ok(c)) return;
74 composite_done(c);
79 Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
81 static void continue_smb_connect(struct composite_context *ctx)
83 struct composite_context *open_ctx;
84 struct composite_context *c = talloc_get_type(ctx->async.private_data,
85 struct composite_context);
86 struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
87 struct pipe_np_smb_state);
88 struct smbcli_tree *t;
90 /* receive result of smb connect request */
91 c->status = smb_composite_connect_recv(ctx, s->io.conn);
92 if (!composite_is_ok(c)) return;
94 t = s->conn.out.tree;
96 /* prepare named pipe open parameters */
97 s->io.smb.conn = t->session->transport->conn;
98 s->io.smb.session = t->session->smbXcli;
99 s->io.smb.tcon = t->smbXcli;
100 smb1cli_tcon_set_id(s->io.smb.tcon, t->tid);
101 s->io.smb.pipe_name = dcerpc_binding_get_string_option(s->io.binding,
102 "endpoint");
104 /* send named pipe open request */
105 open_ctx = dcerpc_pipe_open_smb_send(s->io.conn,
106 s->io.smb.conn,
107 s->io.smb.session,
108 s->io.smb.tcon,
109 DCERPC_REQUEST_TIMEOUT * 1000,
110 s->io.smb.pipe_name);
111 if (composite_nomem(open_ctx, c)) return;
113 composite_continue(c, open_ctx, continue_pipe_open_smb, c);
118 Initiate async open of a rpc connection to a rpc pipe on SMB using
119 the binding structure to determine the endpoint and options
121 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)
123 struct composite_context *c;
124 struct pipe_np_smb_state *s;
125 struct composite_context *conn_req;
126 struct smb_composite_connect *conn;
127 uint32_t flags;
129 /* composite context allocation and setup */
130 c = composite_create(mem_ctx, io->conn->event_ctx);
131 if (c == NULL) return NULL;
133 s = talloc_zero(c, struct pipe_np_smb_state);
134 if (composite_nomem(s, c)) return c;
135 c->private_data = s;
137 s->io = *io;
138 conn = &s->conn;
140 /* prepare smb connection parameters: we're connecting to IPC$ share on
141 remote rpc server */
142 conn->in.dest_host = dcerpc_binding_get_string_option(s->io.binding, "host");
143 conn->in.dest_ports = lpcfg_smb_ports(lp_ctx);
144 conn->in.called_name =
145 dcerpc_binding_get_string_option(s->io.binding, "target_hostname");
146 if (conn->in.called_name == NULL) {
147 conn->in.called_name = "*SMBSERVER";
149 conn->in.socket_options = lpcfg_socket_options(lp_ctx);
150 conn->in.service = "IPC$";
151 conn->in.service_type = NULL;
152 conn->in.workgroup = lpcfg_workgroup(lp_ctx);
153 conn->in.gensec_settings = lpcfg_gensec_settings(conn, lp_ctx);
155 lpcfg_smbcli_options(lp_ctx, &conn->in.options);
156 lpcfg_smbcli_session_options(lp_ctx, &conn->in.session_options);
159 * provide proper credentials - user supplied, but allow a
160 * fallback to anonymous if this is an schannel connection
161 * (might be NT4 not allowing machine logins at session
162 * setup) or if asked to do so by the caller (perhaps a SAMR password change?)
164 s->conn.in.credentials = s->io.creds;
165 flags = dcerpc_binding_get_flags(s->io.binding);
166 if (flags & (DCERPC_SCHANNEL|DCERPC_ANON_FALLBACK)) {
167 conn->in.fallback_to_anonymous = true;
168 } else {
169 conn->in.fallback_to_anonymous = false;
172 /* send smb connect request */
173 conn_req = smb_composite_connect_send(conn, s->io.conn,
174 s->io.resolve_ctx,
175 c->event_ctx);
176 if (composite_nomem(conn_req, c)) return c;
178 composite_continue(c, conn_req, continue_smb_connect, c);
179 return c;
184 Receive result of a rpc connection to a rpc pipe on SMB
186 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
188 NTSTATUS status = composite_wait(c);
190 talloc_free(c);
191 return status;
195 struct pipe_np_smb2_state {
196 struct dcerpc_pipe_connect io;
201 Stage 3 of ncacn_np_smb: Named pipe opened (or not)
203 static void continue_pipe_open_smb2(struct composite_context *ctx)
205 struct composite_context *c = talloc_get_type(ctx->async.private_data,
206 struct composite_context);
208 /* receive result of named pipe open request on smb2 */
209 c->status = dcerpc_pipe_open_smb_recv(ctx);
210 if (!composite_is_ok(c)) return;
212 composite_done(c);
217 Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
219 static void continue_smb2_connect(struct tevent_req *subreq)
221 struct composite_context *open_req;
222 struct composite_context *c =
223 tevent_req_callback_data(subreq,
224 struct composite_context);
225 struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
226 struct pipe_np_smb2_state);
227 struct smb2_tree *t;
229 /* receive result of smb2 connect request */
230 c->status = smb2_connect_recv(subreq, s->io.conn, &t);
231 TALLOC_FREE(subreq);
232 if (!composite_is_ok(c)) return;
234 s->io.smb.conn = t->session->transport->conn;
235 s->io.smb.session = t->session->smbXcli;
236 s->io.smb.tcon = t->smbXcli;
237 s->io.smb.pipe_name = dcerpc_binding_get_string_option(s->io.binding,
238 "endpoint");
240 /* send named pipe open request */
241 open_req = dcerpc_pipe_open_smb_send(s->io.conn,
242 s->io.smb.conn,
243 s->io.smb.session,
244 s->io.smb.tcon,
245 DCERPC_REQUEST_TIMEOUT * 1000,
246 s->io.smb.pipe_name);
247 if (composite_nomem(open_req, c)) return;
249 composite_continue(c, open_req, continue_pipe_open_smb2, c);
254 Initiate async open of a rpc connection request on SMB2 using
255 the binding structure to determine the endpoint and options
257 static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(
258 TALLOC_CTX *mem_ctx,
259 struct dcerpc_pipe_connect *io,
260 struct loadparm_context *lp_ctx)
262 struct composite_context *c;
263 struct pipe_np_smb2_state *s;
264 struct tevent_req *subreq;
265 struct smbcli_options options;
266 const char *host;
267 uint32_t flags;
269 /* composite context allocation and setup */
270 c = composite_create(mem_ctx, io->conn->event_ctx);
271 if (c == NULL) return NULL;
273 s = talloc_zero(c, struct pipe_np_smb2_state);
274 if (composite_nomem(s, c)) return c;
275 c->private_data = s;
277 s->io = *io;
279 host = dcerpc_binding_get_string_option(s->io.binding, "host");
280 flags = dcerpc_binding_get_flags(s->io.binding);
283 * provide proper credentials - user supplied or anonymous in case this is
284 * schannel connection
286 if (flags & DCERPC_SCHANNEL) {
287 s->io.creds = cli_credentials_init_anon(mem_ctx);
288 if (composite_nomem(s->io.creds, c)) return c;
291 lpcfg_smbcli_options(lp_ctx, &options);
293 /* send smb2 connect request */
294 subreq = smb2_connect_send(s, c->event_ctx,
295 host,
296 lpcfg_parm_string_list(mem_ctx, lp_ctx, NULL, "smb2", "ports", NULL),
297 "IPC$",
298 s->io.resolve_ctx,
299 s->io.creds,
300 0, /* previous_session_id */
301 &options,
302 lpcfg_socket_options(lp_ctx),
303 lpcfg_gensec_settings(mem_ctx, lp_ctx));
304 if (composite_nomem(subreq, c)) return c;
305 tevent_req_set_callback(subreq, continue_smb2_connect, c);
306 return c;
311 Receive result of a rpc connection to a rpc pipe on SMB2
313 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
315 NTSTATUS status = composite_wait(c);
317 talloc_free(c);
318 return status;
322 struct pipe_ip_tcp_state {
323 struct dcerpc_pipe_connect io;
324 const char *localaddr;
325 const char *host;
326 const char *target_hostname;
327 uint32_t port;
332 Stage 2 of ncacn_ip_tcp: rpc pipe opened (or not)
334 static void continue_pipe_open_ncacn_ip_tcp(struct composite_context *ctx)
336 struct composite_context *c = talloc_get_type(ctx->async.private_data,
337 struct composite_context);
338 struct pipe_ip_tcp_state *s = talloc_get_type(c->private_data,
339 struct pipe_ip_tcp_state);
340 char *localaddr = NULL;
341 char *remoteaddr = NULL;
343 /* receive result of named pipe open request on tcp/ip */
344 c->status = dcerpc_pipe_open_tcp_recv(ctx, s, &localaddr, &remoteaddr);
345 if (!composite_is_ok(c)) return;
347 c->status = dcerpc_binding_set_string_option(s->io.binding,
348 "localaddress",
349 localaddr);
350 if (!composite_is_ok(c)) return;
352 c->status = dcerpc_binding_set_string_option(s->io.binding,
353 "host",
354 remoteaddr);
355 if (!composite_is_ok(c)) return;
357 composite_done(c);
362 Initiate async open of a rpc connection to a rpc pipe on TCP/IP using
363 the binding structure to determine the endpoint and options
365 static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_ctx,
366 struct dcerpc_pipe_connect *io)
368 struct composite_context *c;
369 struct pipe_ip_tcp_state *s;
370 struct composite_context *pipe_req;
371 const char *endpoint;
373 /* composite context allocation and setup */
374 c = composite_create(mem_ctx, io->conn->event_ctx);
375 if (c == NULL) return NULL;
377 s = talloc_zero(c, struct pipe_ip_tcp_state);
378 if (composite_nomem(s, c)) return c;
379 c->private_data = s;
381 /* store input parameters in state structure */
382 s->io = *io;
383 s->localaddr = dcerpc_binding_get_string_option(io->binding,
384 "localaddress");
385 s->host = dcerpc_binding_get_string_option(io->binding, "host");
386 s->target_hostname = dcerpc_binding_get_string_option(io->binding,
387 "target_hostname");
388 endpoint = dcerpc_binding_get_string_option(io->binding, "endpoint");
389 /* port number is a binding endpoint here */
390 if (endpoint != NULL) {
391 s->port = atoi(endpoint);
394 if (s->port == 0) {
395 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
396 return c;
399 /* send pipe open request on tcp/ip */
400 pipe_req = dcerpc_pipe_open_tcp_send(s->io.conn, s->localaddr, s->host, s->target_hostname,
401 s->port, io->resolve_ctx);
402 composite_continue(c, pipe_req, continue_pipe_open_ncacn_ip_tcp, c);
403 return c;
408 Receive result of a rpc connection to a rpc pipe on TCP/IP
410 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
412 NTSTATUS status = composite_wait(c);
414 talloc_free(c);
415 return status;
419 struct pipe_unix_state {
420 struct dcerpc_pipe_connect io;
421 const char *path;
426 Stage 2 of ncacn_unix: rpc pipe opened (or not)
428 static void continue_pipe_open_ncacn_unix_stream(struct composite_context *ctx)
430 struct composite_context *c = talloc_get_type(ctx->async.private_data,
431 struct composite_context);
433 /* receive result of pipe open request on unix socket */
434 c->status = dcerpc_pipe_open_unix_stream_recv(ctx);
435 if (!composite_is_ok(c)) return;
437 composite_done(c);
442 Initiate async open of a rpc connection to a rpc pipe on unix socket using
443 the binding structure to determine the endpoint and options
445 static struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX *mem_ctx,
446 struct dcerpc_pipe_connect *io)
448 struct composite_context *c;
449 struct pipe_unix_state *s;
450 struct composite_context *pipe_req;
452 /* composite context allocation and setup */
453 c = composite_create(mem_ctx, io->conn->event_ctx);
454 if (c == NULL) return NULL;
456 s = talloc_zero(c, struct pipe_unix_state);
457 if (composite_nomem(s, c)) return c;
458 c->private_data = s;
460 /* prepare pipe open parameters and store them in state structure
461 also, verify whether biding endpoint is not null */
462 s->io = *io;
464 s->path = dcerpc_binding_get_string_option(io->binding, "endpoint");
465 if (s->path == NULL) {
466 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
467 return c;
470 /* send pipe open request on unix socket */
471 pipe_req = dcerpc_pipe_open_unix_stream_send(s->io.conn, s->path);
472 composite_continue(c, pipe_req, continue_pipe_open_ncacn_unix_stream, c);
473 return c;
478 Receive result of a rpc connection to a pipe on unix socket
480 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
482 NTSTATUS status = composite_wait(c);
484 talloc_free(c);
485 return status;
489 struct pipe_ncalrpc_state {
490 struct dcerpc_pipe_connect io;
493 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c);
496 Stage 2 of ncalrpc: rpc pipe opened (or not)
498 static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
500 struct composite_context *c = talloc_get_type(ctx->async.private_data,
501 struct composite_context);
503 /* receive result of pipe open request on ncalrpc */
504 c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
505 if (!composite_is_ok(c)) return;
507 composite_done(c);
512 Initiate async open of a rpc connection request on NCALRPC using
513 the binding structure to determine the endpoint and options
515 static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
516 struct dcerpc_pipe_connect *io)
518 struct composite_context *c;
519 struct pipe_ncalrpc_state *s;
520 struct composite_context *pipe_req;
521 const char *endpoint;
523 /* composite context allocation and setup */
524 c = composite_create(mem_ctx, io->conn->event_ctx);
525 if (c == NULL) return NULL;
527 s = talloc_zero(c, struct pipe_ncalrpc_state);
528 if (composite_nomem(s, c)) return c;
529 c->private_data = s;
531 /* store input parameters in state structure */
532 s->io = *io;
534 endpoint = dcerpc_binding_get_string_option(io->binding, "endpoint");
535 if (endpoint == NULL) {
536 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
537 return c;
540 /* send pipe open request */
541 pipe_req = dcerpc_pipe_open_pipe_send(s->io.conn,
542 s->io.ncalrpc.dir,
543 endpoint);
544 composite_continue(c, pipe_req, continue_pipe_open_ncalrpc, c);
545 return c;
550 Receive result of a rpc connection to a rpc pipe on NCALRPC
552 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
554 NTSTATUS status = composite_wait(c);
556 talloc_free(c);
557 return status;
561 struct pipe_connect_state {
562 struct dcerpc_pipe *pipe;
563 struct dcerpc_binding *binding;
564 const struct ndr_interface_table *table;
565 struct cli_credentials *credentials;
566 struct loadparm_context *lp_ctx;
570 static void continue_map_binding(struct composite_context *ctx);
571 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s);
572 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx);
573 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx);
574 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx);
575 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx);
576 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx);
577 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s);
578 static void continue_pipe_auth(struct composite_context *ctx);
582 Stage 2 of pipe_connect_b: Receive result of endpoint mapping
584 static void continue_map_binding(struct composite_context *ctx)
586 struct composite_context *c = talloc_get_type(ctx->async.private_data,
587 struct composite_context);
588 struct pipe_connect_state *s = talloc_get_type(c->private_data,
589 struct pipe_connect_state);
590 const char *endpoint;
592 c->status = dcerpc_epm_map_binding_recv(ctx);
593 if (!composite_is_ok(c)) return;
595 endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
596 DEBUG(4,("Mapped to DCERPC endpoint %s\n", endpoint));
598 continue_connect(c, s);
603 Stage 2 of pipe_connect_b: Continue connection after endpoint is known
605 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s)
607 struct dcerpc_pipe_connect pc;
609 /* potential exits to another stage by sending an async request */
610 struct composite_context *ncacn_np_smb2_req;
611 struct composite_context *ncacn_np_smb_req;
612 struct composite_context *ncacn_ip_tcp_req;
613 struct composite_context *ncacn_unix_req;
614 struct composite_context *ncalrpc_req;
615 enum dcerpc_transport_t transport;
616 uint32_t flags;
618 /* dcerpc pipe connect input parameters */
619 ZERO_STRUCT(pc);
620 pc.conn = s->pipe->conn;
621 pc.binding = s->binding;
622 pc.interface = s->table;
623 pc.creds = s->credentials;
624 pc.resolve_ctx = lpcfg_resolve_context(s->lp_ctx);
626 transport = dcerpc_binding_get_transport(s->binding);
627 flags = dcerpc_binding_get_flags(s->binding);
629 /* connect dcerpc pipe depending on required transport */
630 switch (transport) {
631 case NCACN_NP:
632 if (flags & DCERPC_SMB2) {
633 /* new varient of SMB a.k.a. SMB2 */
634 ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc, s->lp_ctx);
635 composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
636 return;
638 } else {
639 /* good old ordinary SMB */
640 ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc, s->lp_ctx);
641 composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
642 return;
644 break;
646 case NCACN_IP_TCP:
647 ncacn_ip_tcp_req = dcerpc_pipe_connect_ncacn_ip_tcp_send(c, &pc);
648 composite_continue(c, ncacn_ip_tcp_req, continue_pipe_connect_ncacn_ip_tcp, c);
649 return;
651 case NCACN_UNIX_STREAM:
652 ncacn_unix_req = dcerpc_pipe_connect_ncacn_unix_stream_send(c, &pc);
653 composite_continue(c, ncacn_unix_req, continue_pipe_connect_ncacn_unix, c);
654 return;
656 case NCALRPC:
657 pc.ncalrpc.dir = lpcfg_ncalrpc_dir(s->lp_ctx);
658 c->status = dcerpc_binding_set_string_option(s->binding, "ncalrpc_dir",
659 pc.ncalrpc.dir);
660 if (!composite_is_ok(c)) return;
661 ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc);
662 composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
663 return;
665 default:
666 /* looks like a transport we don't support now */
667 composite_error(c, NT_STATUS_NOT_SUPPORTED);
673 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
674 named pipe on smb2
676 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx)
678 struct composite_context *c = talloc_get_type(ctx->async.private_data,
679 struct composite_context);
680 struct pipe_connect_state *s = talloc_get_type(c->private_data,
681 struct pipe_connect_state);
683 c->status = dcerpc_pipe_connect_ncacn_np_smb2_recv(ctx);
684 if (!composite_is_ok(c)) return;
686 continue_pipe_connect(c, s);
691 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
692 named pipe on smb
694 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx)
696 struct composite_context *c = talloc_get_type(ctx->async.private_data,
697 struct composite_context);
698 struct pipe_connect_state *s = talloc_get_type(c->private_data,
699 struct pipe_connect_state);
701 c->status = dcerpc_pipe_connect_ncacn_np_smb_recv(ctx);
702 if (!composite_is_ok(c)) return;
704 continue_pipe_connect(c, s);
709 Stage 3 of pipe_connect_b: Receive result of pipe connect request on tcp/ip
711 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx)
713 struct composite_context *c = talloc_get_type(ctx->async.private_data,
714 struct composite_context);
715 struct pipe_connect_state *s = talloc_get_type(c->private_data,
716 struct pipe_connect_state);
718 c->status = dcerpc_pipe_connect_ncacn_ip_tcp_recv(ctx);
719 if (!composite_is_ok(c)) return;
721 continue_pipe_connect(c, s);
726 Stage 3 of pipe_connect_b: Receive result of pipe connect request on unix socket
728 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx)
730 struct composite_context *c = talloc_get_type(ctx->async.private_data,
731 struct composite_context);
732 struct pipe_connect_state *s = talloc_get_type(c->private_data,
733 struct pipe_connect_state);
735 c->status = dcerpc_pipe_connect_ncacn_unix_stream_recv(ctx);
736 if (!composite_is_ok(c)) return;
738 continue_pipe_connect(c, s);
743 Stage 3 of pipe_connect_b: Receive result of pipe connect request on local rpc
745 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx)
747 struct composite_context *c = talloc_get_type(ctx->async.private_data,
748 struct composite_context);
749 struct pipe_connect_state *s = talloc_get_type(c->private_data,
750 struct pipe_connect_state);
752 c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
753 if (!composite_is_ok(c)) return;
755 continue_pipe_connect(c, s);
760 Stage 4 of pipe_connect_b: Start an authentication on connected dcerpc pipe
761 depending on credentials and binding flags passed.
763 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s)
765 struct composite_context *auth_bind_req;
767 s->pipe->binding = dcerpc_binding_dup(s->pipe, s->binding);
768 if (composite_nomem(s->pipe->binding, c)) {
769 return;
772 auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
773 s->credentials, s->lp_ctx);
774 composite_continue(c, auth_bind_req, continue_pipe_auth, c);
779 Stage 5 of pipe_connect_b: Receive result of pipe authentication request
780 and say if all went ok
782 static void continue_pipe_auth(struct composite_context *ctx)
784 struct composite_context *c = talloc_get_type(ctx->async.private_data,
785 struct composite_context);
786 struct pipe_connect_state *s = talloc_get_type(c->private_data, struct pipe_connect_state);
788 c->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe);
789 if (!composite_is_ok(c)) return;
791 composite_done(c);
796 handle timeouts of a dcerpc connect
798 static void dcerpc_connect_timeout_handler(struct tevent_context *ev, struct tevent_timer *te,
799 struct timeval t, void *private_data)
801 struct composite_context *c = talloc_get_type_abort(private_data,
802 struct composite_context);
803 struct pipe_connect_state *s = talloc_get_type_abort(c->private_data, struct pipe_connect_state);
804 if (!s->pipe->inhibit_timeout_processing) {
805 composite_error(c, NT_STATUS_IO_TIMEOUT);
806 } else {
807 s->pipe->timed_out = true;
812 start a request to open a rpc connection to a rpc pipe, using
813 specified binding structure to determine the endpoint and options
815 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
816 const struct dcerpc_binding *binding,
817 const struct ndr_interface_table *table,
818 struct cli_credentials *credentials,
819 struct tevent_context *ev,
820 struct loadparm_context *lp_ctx)
822 struct composite_context *c;
823 struct pipe_connect_state *s;
824 enum dcerpc_transport_t transport;
825 const char *endpoint = NULL;
827 /* composite context allocation and setup */
828 c = composite_create(parent_ctx, ev);
829 if (c == NULL) {
830 return NULL;
833 s = talloc_zero(c, struct pipe_connect_state);
834 if (composite_nomem(s, c)) return c;
835 c->private_data = s;
837 /* initialise dcerpc pipe structure */
838 s->pipe = dcerpc_pipe_init(c, ev);
839 if (composite_nomem(s->pipe, c)) return c;
841 if (DEBUGLEVEL >= 10)
842 s->pipe->conn->packet_log_dir = lpcfg_lock_directory(lp_ctx);
844 /* store parameters in state structure */
845 s->binding = dcerpc_binding_dup(s, binding);
846 if (composite_nomem(s->binding, c)) return c;
847 s->table = table;
848 s->credentials = credentials;
849 s->lp_ctx = lp_ctx;
851 s->pipe->timed_out = false;
852 s->pipe->inhibit_timeout_processing = false;
854 tevent_add_timer(c->event_ctx, c,
855 timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
856 dcerpc_connect_timeout_handler, c);
858 transport = dcerpc_binding_get_transport(s->binding);
860 switch (transport) {
861 case NCACN_NP:
862 case NCACN_IP_TCP:
863 case NCALRPC:
864 endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
865 break;
866 default:
867 break;
870 if (endpoint == NULL) {
871 struct composite_context *binding_req;
873 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
874 s->pipe->conn->event_ctx,
875 s->lp_ctx);
876 composite_continue(c, binding_req, continue_map_binding, c);
877 return c;
880 continue_connect(c, s);
881 return c;
886 receive result of a request to open a rpc connection to a rpc pipe
888 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
889 struct dcerpc_pipe **p)
891 NTSTATUS status;
892 struct pipe_connect_state *s;
894 status = composite_wait(c);
896 if (NT_STATUS_IS_OK(status)) {
897 s = talloc_get_type(c->private_data, struct pipe_connect_state);
898 talloc_steal(mem_ctx, s->pipe);
899 *p = s->pipe;
901 talloc_free(c);
902 return status;
907 open a rpc connection to a rpc pipe, using the specified
908 binding structure to determine the endpoint and options - sync version
910 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
911 struct dcerpc_pipe **pp,
912 const struct dcerpc_binding *binding,
913 const struct ndr_interface_table *table,
914 struct cli_credentials *credentials,
915 struct tevent_context *ev,
916 struct loadparm_context *lp_ctx)
918 struct composite_context *c;
920 c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
921 credentials, ev, lp_ctx);
922 return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
926 struct pipe_conn_state {
927 struct dcerpc_pipe *pipe;
931 static void continue_pipe_connect_b(struct composite_context *ctx);
935 Initiate rpc connection to a rpc pipe, using the specified string
936 binding to determine the endpoint and options.
937 The string is to be parsed to a binding structure first.
939 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
940 const char *binding,
941 const struct ndr_interface_table *table,
942 struct cli_credentials *credentials,
943 struct tevent_context *ev, struct loadparm_context *lp_ctx)
945 struct composite_context *c;
946 struct pipe_conn_state *s;
947 struct dcerpc_binding *b;
948 struct composite_context *pipe_conn_req;
950 /* composite context allocation and setup */
951 c = composite_create(parent_ctx, ev);
952 if (c == NULL) {
953 return NULL;
956 s = talloc_zero(c, struct pipe_conn_state);
957 if (composite_nomem(s, c)) return c;
958 c->private_data = s;
960 /* parse binding string to the structure */
961 c->status = dcerpc_parse_binding(c, binding, &b);
962 if (!NT_STATUS_IS_OK(c->status)) {
963 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
964 composite_error(c, c->status);
965 return c;
968 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(c, b)));
971 start connecting to a rpc pipe after binding structure
972 is established
974 pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
975 credentials, ev, lp_ctx);
976 composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
977 return c;
982 Stage 2 of pipe_connect: Receive result of actual pipe connect request
983 and say if we're done ok
985 static void continue_pipe_connect_b(struct composite_context *ctx)
987 struct composite_context *c = talloc_get_type(ctx->async.private_data,
988 struct composite_context);
989 struct pipe_conn_state *s = talloc_get_type(c->private_data,
990 struct pipe_conn_state);
992 c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
993 talloc_steal(s, s->pipe);
994 if (!composite_is_ok(c)) return;
996 composite_done(c);
1001 Receive result of pipe connect (using binding string) request
1002 and return connected pipe structure.
1004 NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
1005 TALLOC_CTX *mem_ctx,
1006 struct dcerpc_pipe **pp)
1008 NTSTATUS status;
1009 struct pipe_conn_state *s;
1011 status = composite_wait(c);
1012 if (NT_STATUS_IS_OK(status)) {
1013 s = talloc_get_type(c->private_data, struct pipe_conn_state);
1014 *pp = talloc_steal(mem_ctx, s->pipe);
1016 talloc_free(c);
1017 return status;
1022 Open a rpc connection to a rpc pipe, using the specified string
1023 binding to determine the endpoint and options - sync version
1025 _PUBLIC_ NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx,
1026 struct dcerpc_pipe **pp,
1027 const char *binding,
1028 const struct ndr_interface_table *table,
1029 struct cli_credentials *credentials,
1030 struct tevent_context *ev,
1031 struct loadparm_context *lp_ctx)
1033 struct composite_context *c;
1034 c = dcerpc_pipe_connect_send(parent_ctx, binding,
1035 table, credentials, ev, lp_ctx);
1036 return dcerpc_pipe_connect_recv(c, parent_ctx, pp);