s4:librpc/rpc: remove pipe_np_smb2_state and use pipe_np_smb_state
[Samba.git] / source4 / librpc / rpc / dcerpc_connect.c
blob42ca0df755fc175636fbfb7b5f857f9ec049cb7d
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 Stage 3 of ncacn_np_smb: Named pipe opened (or not)
197 static void continue_pipe_open_smb2(struct composite_context *ctx)
199 struct composite_context *c = talloc_get_type(ctx->async.private_data,
200 struct composite_context);
202 /* receive result of named pipe open request on smb2 */
203 c->status = dcerpc_pipe_open_smb_recv(ctx);
204 if (!composite_is_ok(c)) return;
206 composite_done(c);
211 Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
213 static void continue_smb2_connect(struct tevent_req *subreq)
215 struct composite_context *open_req;
216 struct composite_context *c =
217 tevent_req_callback_data(subreq,
218 struct composite_context);
219 struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
220 struct pipe_np_smb_state);
221 struct smb2_tree *t;
223 /* receive result of smb2 connect request */
224 c->status = smb2_connect_recv(subreq, s->io.conn, &t);
225 TALLOC_FREE(subreq);
226 if (!composite_is_ok(c)) return;
228 s->io.smb.conn = t->session->transport->conn;
229 s->io.smb.session = t->session->smbXcli;
230 s->io.smb.tcon = t->smbXcli;
231 s->io.smb.pipe_name = dcerpc_binding_get_string_option(s->io.binding,
232 "endpoint");
234 /* send named pipe open request */
235 open_req = dcerpc_pipe_open_smb_send(s->io.conn,
236 s->io.smb.conn,
237 s->io.smb.session,
238 s->io.smb.tcon,
239 DCERPC_REQUEST_TIMEOUT * 1000,
240 s->io.smb.pipe_name);
241 if (composite_nomem(open_req, c)) return;
243 composite_continue(c, open_req, continue_pipe_open_smb2, c);
248 Initiate async open of a rpc connection request on SMB2 using
249 the binding structure to determine the endpoint and options
251 static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(
252 TALLOC_CTX *mem_ctx,
253 struct dcerpc_pipe_connect *io,
254 struct loadparm_context *lp_ctx)
256 struct composite_context *c;
257 struct pipe_np_smb_state *s;
258 struct tevent_req *subreq;
259 struct smbcli_options options;
260 const char *host;
261 uint32_t flags;
263 /* composite context allocation and setup */
264 c = composite_create(mem_ctx, io->conn->event_ctx);
265 if (c == NULL) return NULL;
267 s = talloc_zero(c, struct pipe_np_smb_state);
268 if (composite_nomem(s, c)) return c;
269 c->private_data = s;
271 s->io = *io;
273 host = dcerpc_binding_get_string_option(s->io.binding, "host");
274 flags = dcerpc_binding_get_flags(s->io.binding);
277 * provide proper credentials - user supplied or anonymous in case this is
278 * schannel connection
280 if (flags & DCERPC_SCHANNEL) {
281 s->io.creds = cli_credentials_init_anon(mem_ctx);
282 if (composite_nomem(s->io.creds, c)) return c;
285 lpcfg_smbcli_options(lp_ctx, &options);
287 /* send smb2 connect request */
288 subreq = smb2_connect_send(s, c->event_ctx,
289 host,
290 lpcfg_parm_string_list(mem_ctx, lp_ctx, NULL, "smb2", "ports", NULL),
291 "IPC$",
292 s->io.resolve_ctx,
293 s->io.creds,
294 0, /* previous_session_id */
295 &options,
296 lpcfg_socket_options(lp_ctx),
297 lpcfg_gensec_settings(mem_ctx, lp_ctx));
298 if (composite_nomem(subreq, c)) return c;
299 tevent_req_set_callback(subreq, continue_smb2_connect, c);
300 return c;
305 Receive result of a rpc connection to a rpc pipe on SMB2
307 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
309 NTSTATUS status = composite_wait(c);
311 talloc_free(c);
312 return status;
316 struct pipe_ip_tcp_state {
317 struct dcerpc_pipe_connect io;
318 const char *localaddr;
319 const char *host;
320 const char *target_hostname;
321 uint32_t port;
326 Stage 2 of ncacn_ip_tcp: rpc pipe opened (or not)
328 static void continue_pipe_open_ncacn_ip_tcp(struct composite_context *ctx)
330 struct composite_context *c = talloc_get_type(ctx->async.private_data,
331 struct composite_context);
332 struct pipe_ip_tcp_state *s = talloc_get_type(c->private_data,
333 struct pipe_ip_tcp_state);
334 char *localaddr = NULL;
335 char *remoteaddr = NULL;
337 /* receive result of named pipe open request on tcp/ip */
338 c->status = dcerpc_pipe_open_tcp_recv(ctx, s, &localaddr, &remoteaddr);
339 if (!composite_is_ok(c)) return;
341 c->status = dcerpc_binding_set_string_option(s->io.binding,
342 "localaddress",
343 localaddr);
344 if (!composite_is_ok(c)) return;
346 c->status = dcerpc_binding_set_string_option(s->io.binding,
347 "host",
348 remoteaddr);
349 if (!composite_is_ok(c)) return;
351 composite_done(c);
356 Initiate async open of a rpc connection to a rpc pipe on TCP/IP using
357 the binding structure to determine the endpoint and options
359 static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_ctx,
360 struct dcerpc_pipe_connect *io)
362 struct composite_context *c;
363 struct pipe_ip_tcp_state *s;
364 struct composite_context *pipe_req;
365 const char *endpoint;
367 /* composite context allocation and setup */
368 c = composite_create(mem_ctx, io->conn->event_ctx);
369 if (c == NULL) return NULL;
371 s = talloc_zero(c, struct pipe_ip_tcp_state);
372 if (composite_nomem(s, c)) return c;
373 c->private_data = s;
375 /* store input parameters in state structure */
376 s->io = *io;
377 s->localaddr = dcerpc_binding_get_string_option(io->binding,
378 "localaddress");
379 s->host = dcerpc_binding_get_string_option(io->binding, "host");
380 s->target_hostname = dcerpc_binding_get_string_option(io->binding,
381 "target_hostname");
382 endpoint = dcerpc_binding_get_string_option(io->binding, "endpoint");
383 /* port number is a binding endpoint here */
384 if (endpoint != NULL) {
385 s->port = atoi(endpoint);
388 if (s->port == 0) {
389 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
390 return c;
393 /* send pipe open request on tcp/ip */
394 pipe_req = dcerpc_pipe_open_tcp_send(s->io.conn, s->localaddr, s->host, s->target_hostname,
395 s->port, io->resolve_ctx);
396 composite_continue(c, pipe_req, continue_pipe_open_ncacn_ip_tcp, c);
397 return c;
402 Receive result of a rpc connection to a rpc pipe on TCP/IP
404 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
406 NTSTATUS status = composite_wait(c);
408 talloc_free(c);
409 return status;
413 struct pipe_unix_state {
414 struct dcerpc_pipe_connect io;
415 const char *path;
420 Stage 2 of ncacn_unix: rpc pipe opened (or not)
422 static void continue_pipe_open_ncacn_unix_stream(struct composite_context *ctx)
424 struct composite_context *c = talloc_get_type(ctx->async.private_data,
425 struct composite_context);
427 /* receive result of pipe open request on unix socket */
428 c->status = dcerpc_pipe_open_unix_stream_recv(ctx);
429 if (!composite_is_ok(c)) return;
431 composite_done(c);
436 Initiate async open of a rpc connection to a rpc pipe on unix socket using
437 the binding structure to determine the endpoint and options
439 static struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX *mem_ctx,
440 struct dcerpc_pipe_connect *io)
442 struct composite_context *c;
443 struct pipe_unix_state *s;
444 struct composite_context *pipe_req;
446 /* composite context allocation and setup */
447 c = composite_create(mem_ctx, io->conn->event_ctx);
448 if (c == NULL) return NULL;
450 s = talloc_zero(c, struct pipe_unix_state);
451 if (composite_nomem(s, c)) return c;
452 c->private_data = s;
454 /* prepare pipe open parameters and store them in state structure
455 also, verify whether biding endpoint is not null */
456 s->io = *io;
458 s->path = dcerpc_binding_get_string_option(io->binding, "endpoint");
459 if (s->path == NULL) {
460 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
461 return c;
464 /* send pipe open request on unix socket */
465 pipe_req = dcerpc_pipe_open_unix_stream_send(s->io.conn, s->path);
466 composite_continue(c, pipe_req, continue_pipe_open_ncacn_unix_stream, c);
467 return c;
472 Receive result of a rpc connection to a pipe on unix socket
474 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
476 NTSTATUS status = composite_wait(c);
478 talloc_free(c);
479 return status;
483 struct pipe_ncalrpc_state {
484 struct dcerpc_pipe_connect io;
487 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c);
490 Stage 2 of ncalrpc: rpc pipe opened (or not)
492 static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
494 struct composite_context *c = talloc_get_type(ctx->async.private_data,
495 struct composite_context);
497 /* receive result of pipe open request on ncalrpc */
498 c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
499 if (!composite_is_ok(c)) return;
501 composite_done(c);
506 Initiate async open of a rpc connection request on NCALRPC using
507 the binding structure to determine the endpoint and options
509 static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
510 struct dcerpc_pipe_connect *io)
512 struct composite_context *c;
513 struct pipe_ncalrpc_state *s;
514 struct composite_context *pipe_req;
515 const char *endpoint;
517 /* composite context allocation and setup */
518 c = composite_create(mem_ctx, io->conn->event_ctx);
519 if (c == NULL) return NULL;
521 s = talloc_zero(c, struct pipe_ncalrpc_state);
522 if (composite_nomem(s, c)) return c;
523 c->private_data = s;
525 /* store input parameters in state structure */
526 s->io = *io;
528 endpoint = dcerpc_binding_get_string_option(io->binding, "endpoint");
529 if (endpoint == NULL) {
530 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
531 return c;
534 /* send pipe open request */
535 pipe_req = dcerpc_pipe_open_pipe_send(s->io.conn,
536 s->io.ncalrpc.dir,
537 endpoint);
538 composite_continue(c, pipe_req, continue_pipe_open_ncalrpc, c);
539 return c;
544 Receive result of a rpc connection to a rpc pipe on NCALRPC
546 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
548 NTSTATUS status = composite_wait(c);
550 talloc_free(c);
551 return status;
555 struct pipe_connect_state {
556 struct dcerpc_pipe *pipe;
557 struct dcerpc_binding *binding;
558 const struct ndr_interface_table *table;
559 struct cli_credentials *credentials;
560 struct loadparm_context *lp_ctx;
564 static void continue_map_binding(struct composite_context *ctx);
565 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s);
566 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx);
567 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx);
568 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx);
569 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx);
570 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx);
571 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s);
572 static void continue_pipe_auth(struct composite_context *ctx);
576 Stage 2 of pipe_connect_b: Receive result of endpoint mapping
578 static void continue_map_binding(struct composite_context *ctx)
580 struct composite_context *c = talloc_get_type(ctx->async.private_data,
581 struct composite_context);
582 struct pipe_connect_state *s = talloc_get_type(c->private_data,
583 struct pipe_connect_state);
584 const char *endpoint;
586 c->status = dcerpc_epm_map_binding_recv(ctx);
587 if (!composite_is_ok(c)) return;
589 endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
590 DEBUG(4,("Mapped to DCERPC endpoint %s\n", endpoint));
592 continue_connect(c, s);
597 Stage 2 of pipe_connect_b: Continue connection after endpoint is known
599 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s)
601 struct dcerpc_pipe_connect pc;
603 /* potential exits to another stage by sending an async request */
604 struct composite_context *ncacn_np_smb2_req;
605 struct composite_context *ncacn_np_smb_req;
606 struct composite_context *ncacn_ip_tcp_req;
607 struct composite_context *ncacn_unix_req;
608 struct composite_context *ncalrpc_req;
609 enum dcerpc_transport_t transport;
610 uint32_t flags;
612 /* dcerpc pipe connect input parameters */
613 ZERO_STRUCT(pc);
614 pc.conn = s->pipe->conn;
615 pc.binding = s->binding;
616 pc.interface = s->table;
617 pc.creds = s->credentials;
618 pc.resolve_ctx = lpcfg_resolve_context(s->lp_ctx);
620 transport = dcerpc_binding_get_transport(s->binding);
621 flags = dcerpc_binding_get_flags(s->binding);
623 /* connect dcerpc pipe depending on required transport */
624 switch (transport) {
625 case NCACN_NP:
626 if (flags & DCERPC_SMB2) {
627 /* new varient of SMB a.k.a. SMB2 */
628 ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc, s->lp_ctx);
629 composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
630 return;
632 } else {
633 /* good old ordinary SMB */
634 ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc, s->lp_ctx);
635 composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
636 return;
638 break;
640 case NCACN_IP_TCP:
641 ncacn_ip_tcp_req = dcerpc_pipe_connect_ncacn_ip_tcp_send(c, &pc);
642 composite_continue(c, ncacn_ip_tcp_req, continue_pipe_connect_ncacn_ip_tcp, c);
643 return;
645 case NCACN_UNIX_STREAM:
646 ncacn_unix_req = dcerpc_pipe_connect_ncacn_unix_stream_send(c, &pc);
647 composite_continue(c, ncacn_unix_req, continue_pipe_connect_ncacn_unix, c);
648 return;
650 case NCALRPC:
651 pc.ncalrpc.dir = lpcfg_ncalrpc_dir(s->lp_ctx);
652 c->status = dcerpc_binding_set_string_option(s->binding, "ncalrpc_dir",
653 pc.ncalrpc.dir);
654 if (!composite_is_ok(c)) return;
655 ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc);
656 composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
657 return;
659 default:
660 /* looks like a transport we don't support now */
661 composite_error(c, NT_STATUS_NOT_SUPPORTED);
667 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
668 named pipe on smb2
670 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx)
672 struct composite_context *c = talloc_get_type(ctx->async.private_data,
673 struct composite_context);
674 struct pipe_connect_state *s = talloc_get_type(c->private_data,
675 struct pipe_connect_state);
677 c->status = dcerpc_pipe_connect_ncacn_np_smb2_recv(ctx);
678 if (!composite_is_ok(c)) return;
680 continue_pipe_connect(c, s);
685 Stage 3 of pipe_connect_b: Receive result of pipe connect request on
686 named pipe on smb
688 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx)
690 struct composite_context *c = talloc_get_type(ctx->async.private_data,
691 struct composite_context);
692 struct pipe_connect_state *s = talloc_get_type(c->private_data,
693 struct pipe_connect_state);
695 c->status = dcerpc_pipe_connect_ncacn_np_smb_recv(ctx);
696 if (!composite_is_ok(c)) return;
698 continue_pipe_connect(c, s);
703 Stage 3 of pipe_connect_b: Receive result of pipe connect request on tcp/ip
705 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx)
707 struct composite_context *c = talloc_get_type(ctx->async.private_data,
708 struct composite_context);
709 struct pipe_connect_state *s = talloc_get_type(c->private_data,
710 struct pipe_connect_state);
712 c->status = dcerpc_pipe_connect_ncacn_ip_tcp_recv(ctx);
713 if (!composite_is_ok(c)) return;
715 continue_pipe_connect(c, s);
720 Stage 3 of pipe_connect_b: Receive result of pipe connect request on unix socket
722 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx)
724 struct composite_context *c = talloc_get_type(ctx->async.private_data,
725 struct composite_context);
726 struct pipe_connect_state *s = talloc_get_type(c->private_data,
727 struct pipe_connect_state);
729 c->status = dcerpc_pipe_connect_ncacn_unix_stream_recv(ctx);
730 if (!composite_is_ok(c)) return;
732 continue_pipe_connect(c, s);
737 Stage 3 of pipe_connect_b: Receive result of pipe connect request on local rpc
739 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx)
741 struct composite_context *c = talloc_get_type(ctx->async.private_data,
742 struct composite_context);
743 struct pipe_connect_state *s = talloc_get_type(c->private_data,
744 struct pipe_connect_state);
746 c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
747 if (!composite_is_ok(c)) return;
749 continue_pipe_connect(c, s);
754 Stage 4 of pipe_connect_b: Start an authentication on connected dcerpc pipe
755 depending on credentials and binding flags passed.
757 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s)
759 struct composite_context *auth_bind_req;
761 s->pipe->binding = dcerpc_binding_dup(s->pipe, s->binding);
762 if (composite_nomem(s->pipe->binding, c)) {
763 return;
766 auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
767 s->credentials, s->lp_ctx);
768 composite_continue(c, auth_bind_req, continue_pipe_auth, c);
773 Stage 5 of pipe_connect_b: Receive result of pipe authentication request
774 and say if all went ok
776 static void continue_pipe_auth(struct composite_context *ctx)
778 struct composite_context *c = talloc_get_type(ctx->async.private_data,
779 struct composite_context);
780 struct pipe_connect_state *s = talloc_get_type(c->private_data, struct pipe_connect_state);
782 c->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe);
783 if (!composite_is_ok(c)) return;
785 composite_done(c);
790 handle timeouts of a dcerpc connect
792 static void dcerpc_connect_timeout_handler(struct tevent_context *ev, struct tevent_timer *te,
793 struct timeval t, void *private_data)
795 struct composite_context *c = talloc_get_type_abort(private_data,
796 struct composite_context);
797 struct pipe_connect_state *s = talloc_get_type_abort(c->private_data, struct pipe_connect_state);
798 if (!s->pipe->inhibit_timeout_processing) {
799 composite_error(c, NT_STATUS_IO_TIMEOUT);
800 } else {
801 s->pipe->timed_out = true;
806 start a request to open a rpc connection to a rpc pipe, using
807 specified binding structure to determine the endpoint and options
809 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
810 const struct dcerpc_binding *binding,
811 const struct ndr_interface_table *table,
812 struct cli_credentials *credentials,
813 struct tevent_context *ev,
814 struct loadparm_context *lp_ctx)
816 struct composite_context *c;
817 struct pipe_connect_state *s;
818 enum dcerpc_transport_t transport;
819 const char *endpoint = NULL;
821 /* composite context allocation and setup */
822 c = composite_create(parent_ctx, ev);
823 if (c == NULL) {
824 return NULL;
827 s = talloc_zero(c, struct pipe_connect_state);
828 if (composite_nomem(s, c)) return c;
829 c->private_data = s;
831 /* initialise dcerpc pipe structure */
832 s->pipe = dcerpc_pipe_init(c, ev);
833 if (composite_nomem(s->pipe, c)) return c;
835 if (DEBUGLEVEL >= 10)
836 s->pipe->conn->packet_log_dir = lpcfg_lock_directory(lp_ctx);
838 /* store parameters in state structure */
839 s->binding = dcerpc_binding_dup(s, binding);
840 if (composite_nomem(s->binding, c)) return c;
841 s->table = table;
842 s->credentials = credentials;
843 s->lp_ctx = lp_ctx;
845 s->pipe->timed_out = false;
846 s->pipe->inhibit_timeout_processing = false;
848 tevent_add_timer(c->event_ctx, c,
849 timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
850 dcerpc_connect_timeout_handler, c);
852 transport = dcerpc_binding_get_transport(s->binding);
854 switch (transport) {
855 case NCACN_NP:
856 case NCACN_IP_TCP:
857 case NCALRPC:
858 endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
859 break;
860 default:
861 break;
864 if (endpoint == NULL) {
865 struct composite_context *binding_req;
867 binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
868 s->pipe->conn->event_ctx,
869 s->lp_ctx);
870 composite_continue(c, binding_req, continue_map_binding, c);
871 return c;
874 continue_connect(c, s);
875 return c;
880 receive result of a request to open a rpc connection to a rpc pipe
882 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
883 struct dcerpc_pipe **p)
885 NTSTATUS status;
886 struct pipe_connect_state *s;
888 status = composite_wait(c);
890 if (NT_STATUS_IS_OK(status)) {
891 s = talloc_get_type(c->private_data, struct pipe_connect_state);
892 talloc_steal(mem_ctx, s->pipe);
893 *p = s->pipe;
895 talloc_free(c);
896 return status;
901 open a rpc connection to a rpc pipe, using the specified
902 binding structure to determine the endpoint and options - sync version
904 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
905 struct dcerpc_pipe **pp,
906 const struct dcerpc_binding *binding,
907 const struct ndr_interface_table *table,
908 struct cli_credentials *credentials,
909 struct tevent_context *ev,
910 struct loadparm_context *lp_ctx)
912 struct composite_context *c;
914 c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
915 credentials, ev, lp_ctx);
916 return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
920 struct pipe_conn_state {
921 struct dcerpc_pipe *pipe;
925 static void continue_pipe_connect_b(struct composite_context *ctx);
929 Initiate rpc connection to a rpc pipe, using the specified string
930 binding to determine the endpoint and options.
931 The string is to be parsed to a binding structure first.
933 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
934 const char *binding,
935 const struct ndr_interface_table *table,
936 struct cli_credentials *credentials,
937 struct tevent_context *ev, struct loadparm_context *lp_ctx)
939 struct composite_context *c;
940 struct pipe_conn_state *s;
941 struct dcerpc_binding *b;
942 struct composite_context *pipe_conn_req;
944 /* composite context allocation and setup */
945 c = composite_create(parent_ctx, ev);
946 if (c == NULL) {
947 return NULL;
950 s = talloc_zero(c, struct pipe_conn_state);
951 if (composite_nomem(s, c)) return c;
952 c->private_data = s;
954 /* parse binding string to the structure */
955 c->status = dcerpc_parse_binding(c, binding, &b);
956 if (!NT_STATUS_IS_OK(c->status)) {
957 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
958 composite_error(c, c->status);
959 return c;
962 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(c, b)));
965 start connecting to a rpc pipe after binding structure
966 is established
968 pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
969 credentials, ev, lp_ctx);
970 composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
971 return c;
976 Stage 2 of pipe_connect: Receive result of actual pipe connect request
977 and say if we're done ok
979 static void continue_pipe_connect_b(struct composite_context *ctx)
981 struct composite_context *c = talloc_get_type(ctx->async.private_data,
982 struct composite_context);
983 struct pipe_conn_state *s = talloc_get_type(c->private_data,
984 struct pipe_conn_state);
986 c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
987 talloc_steal(s, s->pipe);
988 if (!composite_is_ok(c)) return;
990 composite_done(c);
995 Receive result of pipe connect (using binding string) request
996 and return connected pipe structure.
998 NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
999 TALLOC_CTX *mem_ctx,
1000 struct dcerpc_pipe **pp)
1002 NTSTATUS status;
1003 struct pipe_conn_state *s;
1005 status = composite_wait(c);
1006 if (NT_STATUS_IS_OK(status)) {
1007 s = talloc_get_type(c->private_data, struct pipe_conn_state);
1008 *pp = talloc_steal(mem_ctx, s->pipe);
1010 talloc_free(c);
1011 return status;
1016 Open a rpc connection to a rpc pipe, using the specified string
1017 binding to determine the endpoint and options - sync version
1019 _PUBLIC_ NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx,
1020 struct dcerpc_pipe **pp,
1021 const char *binding,
1022 const struct ndr_interface_table *table,
1023 struct cli_credentials *credentials,
1024 struct tevent_context *ev,
1025 struct loadparm_context *lp_ctx)
1027 struct composite_context *c;
1028 c = dcerpc_pipe_connect_send(parent_ctx, binding,
1029 table, credentials, ev, lp_ctx);
1030 return dcerpc_pipe_connect_recv(c, parent_ctx, pp);