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 "libcli/composite/composite.h"
29 #include "libcli/smb_composite/smb_composite.h"
30 #include "libcli/smb2/smb2.h"
31 #include "libcli/smb2/smb2_calls.h"
34 struct dcerpc_pipe_connect;
36 struct pipe_np_smb_state {
37 struct smb_composite_connect conn;
38 struct smbcli_tree *tree;
39 struct dcerpc_pipe_connect io;
44 Stage 3 of ncacn_np_smb: Named pipe opened (or not)
46 void continue_pipe_open_smb(struct composite_context *ctx)
48 struct composite_context *c = talloc_get_type(ctx->async.private_data,
49 struct composite_context);
50 struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
51 struct pipe_np_smb_state);
53 /* receive result of named pipe open request on smb */
54 c->status = dcerpc_pipe_open_smb_recv(ctx);
55 if (!NT_STATUS_IS_OK(c->status)) {
56 DEBUG(0,("Failed to open pipe %s - %s\n", s->io.pipe_name, nt_errstr(c->status)));
57 composite_error(c, c->status);
65 Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
67 void continue_smb_connect(struct composite_context *ctx)
69 struct composite_context *open_ctx;
70 struct composite_context *c = talloc_get_type(ctx->async.private_data,
71 struct composite_context);
72 struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
73 struct pipe_np_smb_state);
75 /* receive result of smb connect request */
76 c->status = smb_composite_connect_recv(ctx, c);
77 if (!NT_STATUS_IS_OK(c->status)) {
78 DEBUG(0,("Failed to connect to %s - %s\n", s->io.binding->host, nt_errstr(c->status)));
79 composite_error(c, c->status);
83 /* prepare named pipe open parameters */
84 s->tree = s->conn.out.tree;
85 s->io.pipe_name = s->io.binding->endpoint;
87 /* send named pipe open request */
88 open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
89 if (open_ctx == NULL) {
90 composite_error(c, NT_STATUS_NO_MEMORY);
94 composite_continue(c, open_ctx, continue_pipe_open_smb, c);
99 Initiate async open of a rpc connection to a rpc pipe on SMB using
100 the binding structure to determine the endpoint and options
102 struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_ctx,
103 struct dcerpc_pipe_connect *io)
105 struct composite_context *c;
106 struct pipe_np_smb_state *s;
107 struct composite_context *conn_req;
108 struct smb_composite_connect *conn;
110 /* composite context allocation and setup */
111 c = talloc_zero(mem_ctx, struct composite_context);
112 if (c == NULL) return NULL;
114 s = talloc_zero(c, struct pipe_np_smb_state);
116 composite_error(c, NT_STATUS_NO_MEMORY);
120 c->state = COMPOSITE_STATE_IN_PROGRESS;
122 c->event_ctx = io->pipe->conn->event_ctx;
127 /* prepare smb connection parameters: we're connecting to IPC$ share on
129 conn->in.dest_host = s->io.binding->host;
131 conn->in.called_name = strupper_talloc(mem_ctx, s->io.binding->host);
132 conn->in.service = "IPC$";
133 conn->in.service_type = NULL;
134 conn->in.fallback_to_anonymous = False;
135 conn->in.workgroup = lp_workgroup();
138 * provide proper credentials - user supplied or anonymous in case this is
139 * schannel connection
141 if (s->io.binding->flags & DCERPC_SCHANNEL) {
142 struct cli_credentials *anon_creds;
144 anon_creds = cli_credentials_init(mem_ctx);
146 composite_error(c, NT_STATUS_NO_MEMORY);
150 cli_credentials_set_anonymous(anon_creds);
151 cli_credentials_guess(anon_creds);
153 s->conn.in.credentials = anon_creds;
156 s->conn.in.credentials = s->io.creds;
159 /* send smb connect request */
160 conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, s->io.pipe->conn->event_ctx);
162 composite_error(c, NT_STATUS_NO_MEMORY);
166 composite_continue(c, conn_req, continue_smb_connect, c);
174 Receive result of a rpc connection to a rpc pipe on SMB
176 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
178 NTSTATUS status = composite_wait(c);
186 Sync version of a rpc connection to a rpc pipe on SMB
188 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb(TALLOC_CTX *mem_ctx,
189 struct dcerpc_pipe_connect *io)
191 struct composite_context *c;
192 c = dcerpc_pipe_connect_ncacn_np_smb_send(mem_ctx, io);
193 return dcerpc_pipe_connect_ncacn_np_smb_recv(c);
197 struct pipe_np_smb2_state {
198 struct smb2_tree *tree;
199 struct dcerpc_pipe_connect io;
204 Stage 3 of ncacn_np_smb: Named pipe opened (or not)
206 void continue_pipe_open_smb2(struct composite_context *ctx)
208 struct composite_context *c = talloc_get_type(ctx->async.private_data,
209 struct composite_context);
210 struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
211 struct pipe_np_smb2_state);
213 /* receive result of named pipe open request on smb2 */
214 c->status = dcerpc_pipe_open_smb2_recv(ctx);
215 if (!NT_STATUS_IS_OK(c->status)) {
216 DEBUG(0,("Failed to open pipe %s - %s\n", s->io.pipe_name, nt_errstr(c->status)));
217 composite_error(c, c->status);
226 Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
228 void continue_smb2_connect(struct composite_context *ctx)
230 struct composite_context *open_req;
231 struct composite_context *c = talloc_get_type(ctx->async.private_data,
232 struct composite_context);
233 struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
234 struct pipe_np_smb2_state);
236 /* receive result of smb2 connect request */
237 c->status = smb2_connect_recv(ctx, c, &s->tree);
238 if (!NT_STATUS_IS_OK(c->status)) {
239 DEBUG(0,("Failed to connect to %s - %s\n", s->io.binding->host, nt_errstr(c->status)));
240 composite_error(c, c->status);
244 /* prepare named pipe open parameters */
245 s->io.pipe_name = s->io.binding->endpoint;
247 /* send named pipe open request */
248 open_req = dcerpc_pipe_open_smb2_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
249 if (open_req == NULL) {
250 composite_error(c, NT_STATUS_NO_MEMORY);
254 composite_continue(c, open_req, continue_pipe_open_smb2, c);
259 Initiate async open of a rpc connection request on SMB2 using
260 the binding structure to determine the endpoint and options
262 struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_CTX *mem_ctx,
263 struct dcerpc_pipe_connect *io)
265 struct composite_context *c;
266 struct pipe_np_smb2_state *s;
267 struct composite_context *conn_req;
269 /* composite context allocation and setup */
270 c = talloc_zero(mem_ctx, struct composite_context);
271 if (c == NULL) return NULL;
273 s = talloc_zero(c, struct pipe_np_smb2_state);
275 composite_error(c, NT_STATUS_NO_MEMORY);
279 c->state = COMPOSITE_STATE_IN_PROGRESS;
281 c->event_ctx = io->pipe->conn->event_ctx;
286 * provide proper credentials - user supplied or anonymous in case this is
287 * schannel connection
289 if (s->io.binding->flags & DCERPC_SCHANNEL) {
290 s->io.creds = cli_credentials_init(mem_ctx);
292 composite_error(c, NT_STATUS_NO_MEMORY);
296 cli_credentials_set_anonymous(s->io.creds);
297 cli_credentials_guess(s->io.creds);
300 /* send smb2 connect request */
301 conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, "IPC$", s->io.creds,
303 if (conn_req == NULL) {
304 composite_error(c, NT_STATUS_NO_MEMORY);
308 composite_continue(c, conn_req, continue_smb2_connect, c);
316 Receive result of a rpc connection to a rpc pipe on SMB2
318 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
320 NTSTATUS status = composite_wait(c);
328 Sync version of a rpc connection to a rpc pipe on SMB2
330 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2(TALLOC_CTX *mem_ctx,
331 struct dcerpc_pipe_connect *io)
333 struct composite_context *c;
334 c = dcerpc_pipe_connect_ncacn_np_smb2_send(mem_ctx, io);
335 return dcerpc_pipe_connect_ncacn_np_smb2_recv(c);