s4:librpc/rpc: allow a shortcut in dcerpc_pipe_connect_ncacn_np_smb[2]_send()
[Samba/wip.git] / source4 / librpc / rpc / dcerpc_connect.c
blobecb531503884b2c6a494be21161bbf4ebb20c82a
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);
77 static void continue_smb_open(struct composite_context *c);
80 Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
82 static void continue_smb_connect(struct composite_context *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 continue_smb_open(c);
107 static void continue_smb_open(struct composite_context *c)
109 struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
110 struct pipe_np_smb_state);
111 struct composite_context *open_ctx;
113 /* send named pipe open request */
114 open_ctx = dcerpc_pipe_open_smb_send(s->io.conn,
115 s->io.smb.conn,
116 s->io.smb.session,
117 s->io.smb.tcon,
118 DCERPC_REQUEST_TIMEOUT * 1000,
119 s->io.smb.pipe_name);
120 if (composite_nomem(open_ctx, c)) return;
122 composite_continue(c, open_ctx, continue_pipe_open_smb, c);
127 Initiate async open of a rpc connection to a rpc pipe on SMB using
128 the binding structure to determine the endpoint and options
130 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)
132 struct composite_context *c;
133 struct pipe_np_smb_state *s;
134 struct composite_context *conn_req;
135 struct smb_composite_connect *conn;
136 uint32_t flags;
138 /* composite context allocation and setup */
139 c = composite_create(mem_ctx, io->conn->event_ctx);
140 if (c == NULL) return NULL;
142 s = talloc_zero(c, struct pipe_np_smb_state);
143 if (composite_nomem(s, c)) return c;
144 c->private_data = s;
146 s->io = *io;
147 conn = &s->conn;
149 if (smbXcli_conn_is_connected(s->io.smb.conn)) {
150 continue_smb_open(c);
151 return c;
154 /* prepare smb connection parameters: we're connecting to IPC$ share on
155 remote rpc server */
156 conn->in.dest_host = dcerpc_binding_get_string_option(s->io.binding, "host");
157 conn->in.dest_ports = lpcfg_smb_ports(lp_ctx);
158 conn->in.called_name =
159 dcerpc_binding_get_string_option(s->io.binding, "target_hostname");
160 if (conn->in.called_name == NULL) {
161 conn->in.called_name = "*SMBSERVER";
163 conn->in.socket_options = lpcfg_socket_options(lp_ctx);
164 conn->in.service = "IPC$";
165 conn->in.service_type = NULL;
166 conn->in.workgroup = lpcfg_workgroup(lp_ctx);
167 conn->in.gensec_settings = lpcfg_gensec_settings(conn, lp_ctx);
169 lpcfg_smbcli_options(lp_ctx, &conn->in.options);
170 lpcfg_smbcli_session_options(lp_ctx, &conn->in.session_options);
173 * provide proper credentials - user supplied, but allow a
174 * fallback to anonymous if this is an schannel connection
175 * (might be NT4 not allowing machine logins at session
176 * setup) or if asked to do so by the caller (perhaps a SAMR password change?)
178 s->conn.in.credentials = s->io.creds;
179 flags = dcerpc_binding_get_flags(s->io.binding);
180 if (flags & (DCERPC_SCHANNEL|DCERPC_ANON_FALLBACK)) {
181 conn->in.fallback_to_anonymous = true;
182 } else {
183 conn->in.fallback_to_anonymous = false;
186 /* send smb connect request */
187 conn_req = smb_composite_connect_send(conn, s->io.conn,
188 s->io.resolve_ctx,
189 c->event_ctx);
190 if (composite_nomem(conn_req, c)) return c;
192 composite_continue(c, conn_req, continue_smb_connect, c);
193 return c;
198 Receive result of a rpc connection to a rpc pipe on SMB
200 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
202 NTSTATUS status = composite_wait(c);
204 talloc_free(c);
205 return status;
209 Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
211 static void continue_smb2_connect(struct tevent_req *subreq)
213 struct composite_context *c =
214 tevent_req_callback_data(subreq,
215 struct composite_context);
216 struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
217 struct pipe_np_smb_state);
218 struct smb2_tree *t;
220 /* receive result of smb2 connect request */
221 c->status = smb2_connect_recv(subreq, s->io.conn, &t);
222 TALLOC_FREE(subreq);
223 if (!composite_is_ok(c)) return;
225 s->io.smb.conn = t->session->transport->conn;
226 s->io.smb.session = t->session->smbXcli;
227 s->io.smb.tcon = t->smbXcli;
228 s->io.smb.pipe_name = dcerpc_binding_get_string_option(s->io.binding,
229 "endpoint");
231 continue_smb_open(c);
236 Initiate async open of a rpc connection request on SMB2 using
237 the binding structure to determine the endpoint and options
239 static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(
240 TALLOC_CTX *mem_ctx,
241 struct dcerpc_pipe_connect *io,
242 struct loadparm_context *lp_ctx)
244 struct composite_context *c;
245 struct pipe_np_smb_state *s;
246 struct tevent_req *subreq;
247 struct smbcli_options options;
248 const char *host;
249 uint32_t flags;
251 /* composite context allocation and setup */
252 c = composite_create(mem_ctx, io->conn->event_ctx);
253 if (c == NULL) return NULL;
255 s = talloc_zero(c, struct pipe_np_smb_state);
256 if (composite_nomem(s, c)) return c;
257 c->private_data = s;
259 s->io = *io;
261 if (smbXcli_conn_is_connected(s->io.smb.conn)) {
262 continue_smb_open(c);
263 return c;
266 host = dcerpc_binding_get_string_option(s->io.binding, "host");
267 flags = dcerpc_binding_get_flags(s->io.binding);
270 * provide proper credentials - user supplied or anonymous in case this is
271 * schannel connection
273 if (flags & DCERPC_SCHANNEL) {
274 s->io.creds = cli_credentials_init_anon(mem_ctx);
275 if (composite_nomem(s->io.creds, c)) return c;
278 lpcfg_smbcli_options(lp_ctx, &options);
280 /* send smb2 connect request */
281 subreq = smb2_connect_send(s, c->event_ctx,
282 host,
283 lpcfg_parm_string_list(mem_ctx, lp_ctx, NULL, "smb2", "ports", NULL),
284 "IPC$",
285 s->io.resolve_ctx,
286 s->io.creds,
287 0, /* previous_session_id */
288 &options,
289 lpcfg_socket_options(lp_ctx),
290 lpcfg_gensec_settings(mem_ctx, lp_ctx));
291 if (composite_nomem(subreq, c)) return c;
292 tevent_req_set_callback(subreq, continue_smb2_connect, c);
293 return c;
298 Receive result of a rpc connection to a rpc pipe on SMB2
300 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
302 NTSTATUS status = composite_wait(c);
304 talloc_free(c);
305 return status;
309 struct pipe_ip_tcp_state {
310 struct dcerpc_pipe_connect io;
311 const char *localaddr;
312 const char *host;
313 const char *target_hostname;
314 uint32_t port;
319 Stage 2 of ncacn_ip_tcp: rpc pipe opened (or not)
321 static void continue_pipe_open_ncacn_ip_tcp(struct composite_context *ctx)
323 struct composite_context *c = talloc_get_type(ctx->async.private_data,
324 struct composite_context);
325 struct pipe_ip_tcp_state *s = talloc_get_type(c->private_data,
326 struct pipe_ip_tcp_state);
327 char *localaddr = NULL;
328 char *remoteaddr = NULL;
330 /* receive result of named pipe open request on tcp/ip */
331 c->status = dcerpc_pipe_open_tcp_recv(ctx, s, &localaddr, &remoteaddr);
332 if (!composite_is_ok(c)) return;
334 c->status = dcerpc_binding_set_string_option(s->io.binding,
335 "localaddress",
336 localaddr);
337 if (!composite_is_ok(c)) return;
339 c->status = dcerpc_binding_set_string_option(s->io.binding,
340 "host",
341 remoteaddr);
342 if (!composite_is_ok(c)) return;
344 composite_done(c);
349 Initiate async open of a rpc connection to a rpc pipe on TCP/IP using
350 the binding structure to determine the endpoint and options
352 static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_ctx,
353 struct dcerpc_pipe_connect *io)
355 struct composite_context *c;
356 struct pipe_ip_tcp_state *s;
357 struct composite_context *pipe_req;
358 const char *endpoint;
360 /* composite context allocation and setup */
361 c = composite_create(mem_ctx, io->conn->event_ctx);
362 if (c == NULL) return NULL;
364 s = talloc_zero(c, struct pipe_ip_tcp_state);
365 if (composite_nomem(s, c)) return c;
366 c->private_data = s;
368 /* store input parameters in state structure */
369 s->io = *io;
370 s->localaddr = dcerpc_binding_get_string_option(io->binding,
371 "localaddress");
372 s->host = dcerpc_binding_get_string_option(io->binding, "host");
373 s->target_hostname = dcerpc_binding_get_string_option(io->binding,
374 "target_hostname");
375 endpoint = dcerpc_binding_get_string_option(io->binding, "endpoint");
376 /* port number is a binding endpoint here */
377 if (endpoint != NULL) {
378 s->port = atoi(endpoint);
381 if (s->port == 0) {
382 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
383 return c;
386 /* send pipe open request on tcp/ip */
387 pipe_req = dcerpc_pipe_open_tcp_send(s->io.conn, s->localaddr, s->host, s->target_hostname,
388 s->port, io->resolve_ctx);
389 composite_continue(c, pipe_req, continue_pipe_open_ncacn_ip_tcp, c);
390 return c;
395 Receive result of a rpc connection to a rpc pipe on TCP/IP
397 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
399 NTSTATUS status = composite_wait(c);
401 talloc_free(c);
402 return status;
406 struct pipe_unix_state {
407 struct dcerpc_pipe_connect io;
408 const char *path;
413 Stage 2 of ncacn_unix: rpc pipe opened (or not)
415 static void continue_pipe_open_ncacn_unix_stream(struct composite_context *ctx)
417 struct composite_context *c = talloc_get_type(ctx->async.private_data,
418 struct composite_context);
420 /* receive result of pipe open request on unix socket */
421 c->status = dcerpc_pipe_open_unix_stream_recv(ctx);
422 if (!composite_is_ok(c)) return;
424 composite_done(c);
429 Initiate async open of a rpc connection to a rpc pipe on unix socket using
430 the binding structure to determine the endpoint and options
432 static struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX *mem_ctx,
433 struct dcerpc_pipe_connect *io)
435 struct composite_context *c;
436 struct pipe_unix_state *s;
437 struct composite_context *pipe_req;
439 /* composite context allocation and setup */
440 c = composite_create(mem_ctx, io->conn->event_ctx);
441 if (c == NULL) return NULL;
443 s = talloc_zero(c, struct pipe_unix_state);
444 if (composite_nomem(s, c)) return c;
445 c->private_data = s;
447 /* prepare pipe open parameters and store them in state structure
448 also, verify whether biding endpoint is not null */
449 s->io = *io;
451 s->path = dcerpc_binding_get_string_option(io->binding, "endpoint");
452 if (s->path == NULL) {
453 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
454 return c;
457 /* send pipe open request on unix socket */
458 pipe_req = dcerpc_pipe_open_unix_stream_send(s->io.conn, s->path);
459 composite_continue(c, pipe_req, continue_pipe_open_ncacn_unix_stream, c);
460 return c;
465 Receive result of a rpc connection to a pipe on unix socket
467 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
469 NTSTATUS status = composite_wait(c);
471 talloc_free(c);
472 return status;
476 struct pipe_ncalrpc_state {
477 struct dcerpc_pipe_connect io;
480 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c);
483 Stage 2 of ncalrpc: rpc pipe opened (or not)
485 static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
487 struct composite_context *c = talloc_get_type(ctx->async.private_data,
488 struct composite_context);
490 /* receive result of pipe open request on ncalrpc */
491 c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
492 if (!composite_is_ok(c)) return;
494 composite_done(c);
499 Initiate async open of a rpc connection request on NCALRPC using
500 the binding structure to determine the endpoint and options
502 static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
503 struct dcerpc_pipe_connect *io)
505 struct composite_context *c;
506 struct pipe_ncalrpc_state *s;
507 struct composite_context *pipe_req;
508 const char *endpoint;
510 /* composite context allocation and setup */
511 c = composite_create(mem_ctx, io->conn->event_ctx);
512 if (c == NULL) return NULL;
514 s = talloc_zero(c, struct pipe_ncalrpc_state);
515 if (composite_nomem(s, c)) return c;
516 c->private_data = s;
518 /* store input parameters in state structure */
519 s->io = *io;
521 endpoint = dcerpc_binding_get_string_option(io->binding, "endpoint");
522 if (endpoint == NULL) {
523 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
524 return c;
527 /* send pipe open request */
528 pipe_req = dcerpc_pipe_open_pipe_send(s->io.conn,
529 s->io.ncalrpc.dir,
530 endpoint);
531 composite_continue(c, pipe_req, continue_pipe_open_ncalrpc, c);
532 return c;
537 Receive result of a rpc connection to a rpc pipe on NCALRPC
539 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
541 NTSTATUS status = composite_wait(c);
543 talloc_free(c);
544 return status;
548 struct pipe_connect_state {
549 struct dcerpc_pipe *pipe;
550 struct dcerpc_binding *binding;
551 const struct ndr_interface_table *table;
552 struct cli_credentials *credentials;
553 struct loadparm_context *lp_ctx;
557 static void continue_map_binding(struct composite_context *ctx);
558 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s);
559 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx);
560 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx);
561 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx);
562 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx);
563 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx);
564 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s);
565 static void continue_pipe_auth(struct composite_context *ctx);
569 Stage 2 of pipe_connect_b: Receive result of endpoint mapping
571 static void continue_map_binding(struct composite_context *ctx)
573 struct composite_context *c = talloc_get_type(ctx->async.private_data,
574 struct composite_context);
575 struct pipe_connect_state *s = talloc_get_type(c->private_data,
576 struct pipe_connect_state);
577 const char *endpoint;
579 c->status = dcerpc_epm_map_binding_recv(ctx);
580 if (!composite_is_ok(c)) return;
582 endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
583 DEBUG(4,("Mapped to DCERPC endpoint %s\n", endpoint));
585 continue_connect(c, s);
590 Stage 2 of pipe_connect_b: Continue connection after endpoint is known
592 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s)
594 struct dcerpc_pipe_connect pc;
596 /* potential exits to another stage by sending an async request */
597 struct composite_context *ncacn_np_smb2_req;
598 struct composite_context *ncacn_np_smb_req;
599 struct composite_context *ncacn_ip_tcp_req;
600 struct composite_context *ncacn_unix_req;
601 struct composite_context *ncalrpc_req;
602 enum dcerpc_transport_t transport;
603 uint32_t flags;
605 /* dcerpc pipe connect input parameters */
606 ZERO_STRUCT(pc);
607 pc.conn = s->pipe->conn;
608 pc.binding = s->binding;
609 pc.interface = s->table;
610 pc.creds = s->credentials;
611 pc.resolve_ctx = lpcfg_resolve_context(s->lp_ctx);
613 transport = dcerpc_binding_get_transport(s->binding);
614 flags = dcerpc_binding_get_flags(s->binding);
616 /* connect dcerpc pipe depending on required transport */
617 switch (transport) {
618 case NCACN_NP:
619 if (flags & DCERPC_SMB2) {
620 /* new varient of SMB a.k.a. SMB2 */
621 ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc, s->lp_ctx);
622 composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
623 return;
625 } else {
626 /* good old ordinary SMB */
627 ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc, s->lp_ctx);
628 composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
629 return;
631 break;
633 case NCACN_IP_TCP:
634 ncacn_ip_tcp_req = dcerpc_pipe_connect_ncacn_ip_tcp_send(c, &pc);
635 composite_continue(c, ncacn_ip_tcp_req, continue_pipe_connect_ncacn_ip_tcp, c);
636 return;
638 case NCACN_UNIX_STREAM:
639 ncacn_unix_req = dcerpc_pipe_connect_ncacn_unix_stream_send(c, &pc);
640 composite_continue(c, ncacn_unix_req, continue_pipe_connect_ncacn_unix, c);
641 return;
643 case NCALRPC:
644 pc.ncalrpc.dir = lpcfg_ncalrpc_dir(s->lp_ctx);
645 c->status = dcerpc_binding_set_string_option(s->binding, "ncalrpc_dir",
646 pc.ncalrpc.dir);
647 if (!composite_is_ok(c)) return;
648 ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc);
649 composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
650 return;
652 default:
653 /* looks like a transport we don't support now */
654 composite_error(c, NT_STATUS_NOT_SUPPORTED);
660 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
661 named pipe on smb2
663 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx)
665 struct composite_context *c = talloc_get_type(ctx->async.private_data,
666 struct composite_context);
667 struct pipe_connect_state *s = talloc_get_type(c->private_data,
668 struct pipe_connect_state);
670 c->status = dcerpc_pipe_connect_ncacn_np_smb2_recv(ctx);
671 if (!composite_is_ok(c)) return;
673 continue_pipe_connect(c, s);
678 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
679 named pipe on smb
681 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx)
683 struct composite_context *c = talloc_get_type(ctx->async.private_data,
684 struct composite_context);
685 struct pipe_connect_state *s = talloc_get_type(c->private_data,
686 struct pipe_connect_state);
688 c->status = dcerpc_pipe_connect_ncacn_np_smb_recv(ctx);
689 if (!composite_is_ok(c)) return;
691 continue_pipe_connect(c, s);
696 Stage 3 of pipe_connect_b: Receive result of pipe connect request on tcp/ip
698 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx)
700 struct composite_context *c = talloc_get_type(ctx->async.private_data,
701 struct composite_context);
702 struct pipe_connect_state *s = talloc_get_type(c->private_data,
703 struct pipe_connect_state);
705 c->status = dcerpc_pipe_connect_ncacn_ip_tcp_recv(ctx);
706 if (!composite_is_ok(c)) return;
708 continue_pipe_connect(c, s);
713 Stage 3 of pipe_connect_b: Receive result of pipe connect request on unix socket
715 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx)
717 struct composite_context *c = talloc_get_type(ctx->async.private_data,
718 struct composite_context);
719 struct pipe_connect_state *s = talloc_get_type(c->private_data,
720 struct pipe_connect_state);
722 c->status = dcerpc_pipe_connect_ncacn_unix_stream_recv(ctx);
723 if (!composite_is_ok(c)) return;
725 continue_pipe_connect(c, s);
730 Stage 3 of pipe_connect_b: Receive result of pipe connect request on local rpc
732 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx)
734 struct composite_context *c = talloc_get_type(ctx->async.private_data,
735 struct composite_context);
736 struct pipe_connect_state *s = talloc_get_type(c->private_data,
737 struct pipe_connect_state);
739 c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
740 if (!composite_is_ok(c)) return;
742 continue_pipe_connect(c, s);
747 Stage 4 of pipe_connect_b: Start an authentication on connected dcerpc pipe
748 depending on credentials and binding flags passed.
750 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s)
752 struct composite_context *auth_bind_req;
754 s->pipe->binding = dcerpc_binding_dup(s->pipe, s->binding);
755 if (composite_nomem(s->pipe->binding, c)) {
756 return;
759 auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
760 s->credentials, s->lp_ctx);
761 composite_continue(c, auth_bind_req, continue_pipe_auth, c);
766 Stage 5 of pipe_connect_b: Receive result of pipe authentication request
767 and say if all went ok
769 static void continue_pipe_auth(struct composite_context *ctx)
771 struct composite_context *c = talloc_get_type(ctx->async.private_data,
772 struct composite_context);
773 struct pipe_connect_state *s = talloc_get_type(c->private_data, struct pipe_connect_state);
775 c->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe);
776 if (!composite_is_ok(c)) return;
778 composite_done(c);
783 handle timeouts of a dcerpc connect
785 static void dcerpc_connect_timeout_handler(struct tevent_context *ev, struct tevent_timer *te,
786 struct timeval t, void *private_data)
788 struct composite_context *c = talloc_get_type_abort(private_data,
789 struct composite_context);
790 struct pipe_connect_state *s = talloc_get_type_abort(c->private_data, struct pipe_connect_state);
791 if (!s->pipe->inhibit_timeout_processing) {
792 composite_error(c, NT_STATUS_IO_TIMEOUT);
793 } else {
794 s->pipe->timed_out = true;
799 start a request to open a rpc connection to a rpc pipe, using
800 specified binding structure to determine the endpoint and options
802 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
803 const struct dcerpc_binding *binding,
804 const struct ndr_interface_table *table,
805 struct cli_credentials *credentials,
806 struct tevent_context *ev,
807 struct loadparm_context *lp_ctx)
809 struct composite_context *c;
810 struct pipe_connect_state *s;
811 enum dcerpc_transport_t transport;
812 const char *endpoint = NULL;
814 /* composite context allocation and setup */
815 c = composite_create(parent_ctx, ev);
816 if (c == NULL) {
817 return NULL;
820 s = talloc_zero(c, struct pipe_connect_state);
821 if (composite_nomem(s, c)) return c;
822 c->private_data = s;
824 /* initialise dcerpc pipe structure */
825 s->pipe = dcerpc_pipe_init(c, ev);
826 if (composite_nomem(s->pipe, c)) return c;
828 if (DEBUGLEVEL >= 10)
829 s->pipe->conn->packet_log_dir = lpcfg_lock_directory(lp_ctx);
831 /* store parameters in state structure */
832 s->binding = dcerpc_binding_dup(s, binding);
833 if (composite_nomem(s->binding, c)) return c;
834 s->table = table;
835 s->credentials = credentials;
836 s->lp_ctx = lp_ctx;
838 s->pipe->timed_out = false;
839 s->pipe->inhibit_timeout_processing = false;
841 tevent_add_timer(c->event_ctx, c,
842 timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
843 dcerpc_connect_timeout_handler, c);
845 transport = dcerpc_binding_get_transport(s->binding);
847 switch (transport) {
848 case NCACN_NP:
849 case NCACN_IP_TCP:
850 case NCALRPC:
851 endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
852 break;
853 default:
854 break;
857 if (endpoint == NULL) {
858 struct composite_context *binding_req;
860 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
861 s->pipe->conn->event_ctx,
862 s->lp_ctx);
863 composite_continue(c, binding_req, continue_map_binding, c);
864 return c;
867 continue_connect(c, s);
868 return c;
873 receive result of a request to open a rpc connection to a rpc pipe
875 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
876 struct dcerpc_pipe **p)
878 NTSTATUS status;
879 struct pipe_connect_state *s;
881 status = composite_wait(c);
883 if (NT_STATUS_IS_OK(status)) {
884 s = talloc_get_type(c->private_data, struct pipe_connect_state);
885 talloc_steal(mem_ctx, s->pipe);
886 *p = s->pipe;
888 talloc_free(c);
889 return status;
894 open a rpc connection to a rpc pipe, using the specified
895 binding structure to determine the endpoint and options - sync version
897 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
898 struct dcerpc_pipe **pp,
899 const struct dcerpc_binding *binding,
900 const struct ndr_interface_table *table,
901 struct cli_credentials *credentials,
902 struct tevent_context *ev,
903 struct loadparm_context *lp_ctx)
905 struct composite_context *c;
907 c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
908 credentials, ev, lp_ctx);
909 return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
913 struct pipe_conn_state {
914 struct dcerpc_pipe *pipe;
918 static void continue_pipe_connect_b(struct composite_context *ctx);
922 Initiate rpc connection to a rpc pipe, using the specified string
923 binding to determine the endpoint and options.
924 The string is to be parsed to a binding structure first.
926 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
927 const char *binding,
928 const struct ndr_interface_table *table,
929 struct cli_credentials *credentials,
930 struct tevent_context *ev, struct loadparm_context *lp_ctx)
932 struct composite_context *c;
933 struct pipe_conn_state *s;
934 struct dcerpc_binding *b;
935 struct composite_context *pipe_conn_req;
937 /* composite context allocation and setup */
938 c = composite_create(parent_ctx, ev);
939 if (c == NULL) {
940 return NULL;
943 s = talloc_zero(c, struct pipe_conn_state);
944 if (composite_nomem(s, c)) return c;
945 c->private_data = s;
947 /* parse binding string to the structure */
948 c->status = dcerpc_parse_binding(c, binding, &b);
949 if (!NT_STATUS_IS_OK(c->status)) {
950 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
951 composite_error(c, c->status);
952 return c;
955 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(c, b)));
958 start connecting to a rpc pipe after binding structure
959 is established
961 pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
962 credentials, ev, lp_ctx);
963 composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
964 return c;
969 Stage 2 of pipe_connect: Receive result of actual pipe connect request
970 and say if we're done ok
972 static void continue_pipe_connect_b(struct composite_context *ctx)
974 struct composite_context *c = talloc_get_type(ctx->async.private_data,
975 struct composite_context);
976 struct pipe_conn_state *s = talloc_get_type(c->private_data,
977 struct pipe_conn_state);
979 c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
980 talloc_steal(s, s->pipe);
981 if (!composite_is_ok(c)) return;
983 composite_done(c);
988 Receive result of pipe connect (using binding string) request
989 and return connected pipe structure.
991 NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
992 TALLOC_CTX *mem_ctx,
993 struct dcerpc_pipe **pp)
995 NTSTATUS status;
996 struct pipe_conn_state *s;
998 status = composite_wait(c);
999 if (NT_STATUS_IS_OK(status)) {
1000 s = talloc_get_type(c->private_data, struct pipe_conn_state);
1001 *pp = talloc_steal(mem_ctx, s->pipe);
1003 talloc_free(c);
1004 return status;
1009 Open a rpc connection to a rpc pipe, using the specified string
1010 binding to determine the endpoint and options - sync version
1012 _PUBLIC_ NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx,
1013 struct dcerpc_pipe **pp,
1014 const char *binding,
1015 const struct ndr_interface_table *table,
1016 struct cli_credentials *credentials,
1017 struct tevent_context *ev,
1018 struct loadparm_context *lp_ctx)
1020 struct composite_context *c;
1021 c = dcerpc_pipe_connect_send(parent_ctx, binding,
1022 table, credentials, ev, lp_ctx);
1023 return dcerpc_pipe_connect_recv(c, parent_ctx, pp);