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
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 2 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, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "system/network.h"
29 #include "librpc/gen_ndr/ndr_epmapper.h"
30 #include "librpc/gen_ndr/ndr_dcerpc.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "libcli/raw/libcliraw.h"
33 #include "libcli/composite/composite.h"
34 #include "libcli/smb_composite/smb_composite.h"
35 #include "libcli/smb2/smb2.h"
38 struct dcerpc_pipe_connect;
40 struct pipe_np_smb_state {
41 struct smb_composite_connect conn;
42 struct smbcli_tree *tree;
43 struct dcerpc_pipe_connect io;
48 Stage 3 of ncacn_np_smb: Named pipe opened (or not)
50 void continue_pipe_open_smb(struct composite_context *ctx)
52 struct composite_context *c = talloc_get_type(ctx->async.private_data,
53 struct composite_context);
54 struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
55 struct pipe_np_smb_state);
57 /* receive result of named pipe open request on smb */
58 c->status = dcerpc_pipe_open_smb_recv(ctx);
59 if (!NT_STATUS_IS_OK(c->status)) {
60 DEBUG(0,("Failed to open pipe %s - %s\n", s->io.pipe_name, nt_errstr(c->status)));
61 composite_error(c, c->status);
69 Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
71 void continue_smb_connect(struct composite_context *ctx)
73 struct composite_context *open_ctx;
74 struct composite_context *c = talloc_get_type(ctx->async.private_data,
75 struct composite_context);
76 struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
77 struct pipe_np_smb_state);
79 /* receive result of smb connect request */
80 c->status = smb_composite_connect_recv(ctx, c);
81 if (!NT_STATUS_IS_OK(c->status)) {
82 DEBUG(0,("Failed to connect to %s - %s\n", s->io.binding->host, nt_errstr(c->status)));
83 composite_error(c, c->status);
87 /* prepare named pipe open parameters */
88 s->tree = s->conn.out.tree;
89 s->io.pipe_name = s->io.binding->endpoint;
91 /* send named pipe open request */
92 open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
93 if (open_ctx == NULL) {
94 composite_error(c, NT_STATUS_NO_MEMORY);
98 composite_continue(c, open_ctx, continue_pipe_open_smb, c);
103 Initiate async open of a rpc connection to a rpc pipe on SMB using
104 the binding structure to determine the endpoint and options
106 struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *tmp_ctx,
107 struct dcerpc_pipe_connect *io)
109 struct composite_context *c;
110 struct pipe_np_smb_state *s;
111 struct composite_context *conn_req;
112 struct smb_composite_connect *conn;
114 /* composite context allocation and setup */
115 c = talloc_zero(tmp_ctx, struct composite_context);
116 if (c == NULL) return NULL;
118 s = talloc_zero(c, struct pipe_np_smb_state);
120 composite_error(c, NT_STATUS_NO_MEMORY);
124 c->state = COMPOSITE_STATE_IN_PROGRESS;
126 c->event_ctx = io->pipe->conn->event_ctx;
131 /* prepare smb connection parameters: we're connecting to IPC$ share on
133 conn->in.dest_host = s->io.binding->host;
135 conn->in.called_name = strupper_talloc(tmp_ctx, s->io.binding->host);
136 conn->in.service = "IPC$";
137 conn->in.service_type = NULL;
138 conn->in.fallback_to_anonymous = False;
139 conn->in.workgroup = lp_workgroup();
142 * provide proper credentials - user supplied or anonymous in case this is
143 * schannel connection
145 if (s->io.binding->flags & DCERPC_SCHANNEL) {
146 struct cli_credentials *anon_creds;
148 anon_creds = cli_credentials_init(tmp_ctx);
150 composite_error(c, NT_STATUS_NO_MEMORY);
154 cli_credentials_set_anonymous(anon_creds);
155 cli_credentials_guess(anon_creds);
157 s->conn.in.credentials = anon_creds;
160 s->conn.in.credentials = s->io.creds;
163 /* send smb connect request */
164 conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, s->io.pipe->conn->event_ctx);
166 composite_error(c, NT_STATUS_NO_MEMORY);
170 composite_continue(c, conn_req, continue_smb_connect, c);
178 Receive result of a rpc connection to a rpc pipe on SMB
180 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
182 NTSTATUS status = composite_wait(c);
190 Sync version of a rpc connection to a rpc pipe on SMB
192 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb(TALLOC_CTX *tmp_ctx,
193 struct dcerpc_pipe_connect *io)
195 struct composite_context *c;
196 c = dcerpc_pipe_connect_ncacn_np_smb_send(tmp_ctx, io);
197 return dcerpc_pipe_connect_ncacn_np_smb_recv(c);
201 struct pipe_np_smb2_state {
202 struct smb2_tree *tree;
203 struct dcerpc_pipe_connect io;
208 Stage 3 of ncacn_np_smb: Named pipe opened (or not)
210 void continue_pipe_open_smb2(struct composite_context *ctx)
212 struct composite_context *c = talloc_get_type(ctx->async.private_data,
213 struct composite_context);
214 struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
215 struct pipe_np_smb2_state);
217 /* receive result of named pipe open request on smb2 */
218 c->status = dcerpc_pipe_open_smb2_recv(ctx);
219 if (!NT_STATUS_IS_OK(c->status)) {
220 DEBUG(0,("Failed to open pipe %s - %s\n", s->io.pipe_name, nt_errstr(c->status)));
221 composite_error(c, c->status);
230 Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
232 void continue_smb2_connect(struct composite_context *ctx)
234 struct composite_context *open_req;
235 struct composite_context *c = talloc_get_type(ctx->async.private_data,
236 struct composite_context);
237 struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
238 struct pipe_np_smb2_state);
240 /* receive result of smb2 connect request */
241 c->status = smb2_connect_recv(ctx, c, &s->tree);
242 if (!NT_STATUS_IS_OK(c->status)) {
243 DEBUG(0,("Failed to connect to %s - %s\n", s->io.binding->host, nt_errstr(c->status)));
244 composite_error(c, c->status);
248 /* prepare named pipe open parameters */
249 s->io.pipe_name = s->io.binding->endpoint;
251 /* send named pipe open request */
252 open_req = dcerpc_pipe_open_smb2_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
253 if (open_req == NULL) {
254 composite_error(c, NT_STATUS_NO_MEMORY);
258 composite_continue(c, open_req, continue_pipe_open_smb2, c);
263 Initiate async open of a rpc connection request on SMB2 using
264 the binding structure to determine the endpoint and options
266 struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_CTX *mem_ctx,
267 struct dcerpc_pipe_connect *io)
269 struct composite_context *c;
270 struct pipe_np_smb2_state *s;
271 struct composite_context *conn_req;
273 /* composite context allocation and setup */
274 c = talloc_zero(mem_ctx, struct composite_context);
275 if (c == NULL) return NULL;
277 s = talloc_zero(c, struct pipe_np_smb2_state);
279 composite_error(c, NT_STATUS_NO_MEMORY);
283 c->state = COMPOSITE_STATE_IN_PROGRESS;
285 c->event_ctx = io->pipe->conn->event_ctx;
290 * provide proper credentials - user supplied or anonymous in case this is
291 * schannel connection
293 if (s->io.binding->flags & DCERPC_SCHANNEL) {
294 s->io.creds = cli_credentials_init(mem_ctx);
296 composite_error(c, NT_STATUS_NO_MEMORY);
300 cli_credentials_set_anonymous(s->io.creds);
301 cli_credentials_guess(s->io.creds);
304 /* send smb2 connect request */
305 conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, "IPC$", s->io.creds,
307 if (conn_req == NULL) {
308 composite_error(c, NT_STATUS_NO_MEMORY);
312 composite_continue(c, conn_req, continue_smb2_connect, c);
320 Receive result of a rpc connection to a rpc pipe on SMB2
322 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
324 NTSTATUS status = composite_wait(c);
332 Sync version of a rpc connection to a rpc pipe on SMB2
334 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2(TALLOC_CTX *mem_ctx,
335 struct dcerpc_pipe_connect *io)
337 struct composite_context *c;
338 c = dcerpc_pipe_connect_ncacn_np_smb2_send(mem_ctx, io);
339 return dcerpc_pipe_connect_ncacn_np_smb2_recv(c);