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/>.
27 #include "libcli/composite/composite.h"
28 #include "lib/events/events.h"
29 #include "librpc/rpc/dcerpc.h"
30 #include "auth/credentials/credentials.h"
31 #include "param/param.h"
34 struct sec_conn_state {
35 struct dcerpc_pipe *pipe;
36 struct dcerpc_pipe *pipe2;
37 struct dcerpc_binding *binding;
38 struct smbcli_tree *tree;
42 static void continue_open_smb(struct composite_context *ctx);
43 static void continue_open_tcp(struct composite_context *ctx);
44 static void continue_open_pipe(struct composite_context *ctx);
45 static void continue_pipe_open(struct composite_context *c);
49 Send request to create a secondary dcerpc connection from a primary
52 struct composite_context* dcerpc_secondary_connection_send(struct dcerpc_pipe *p,
53 struct dcerpc_binding *b)
55 struct composite_context *c;
56 struct sec_conn_state *s;
57 struct composite_context *pipe_smb_req;
58 struct composite_context *pipe_tcp_req;
59 struct composite_context *pipe_ncalrpc_req;
61 /* composite context allocation and setup */
62 c = composite_create(p, p->conn->event_ctx);
63 if (c == NULL) return NULL;
65 s = talloc_zero(c, struct sec_conn_state);
66 if (composite_nomem(s, c)) return c;
72 /* initialise second dcerpc pipe based on primary pipe's event context */
73 s->pipe2 = dcerpc_pipe_init(c, s->pipe->conn->event_ctx);
74 if (composite_nomem(s->pipe2, c)) return c;
76 /* open second dcerpc pipe using the same transport as for primary pipe */
77 switch (s->pipe->conn->transport.transport) {
79 /* get smb tree of primary dcerpc pipe opened on smb */
80 s->tree = dcerpc_smb_tree(s->pipe->conn);
82 composite_error(c, NT_STATUS_INVALID_PARAMETER);
86 pipe_smb_req = dcerpc_pipe_open_smb_send(s->pipe2, s->tree,
87 s->binding->endpoint);
88 composite_continue(c, pipe_smb_req, continue_open_smb, c);
92 pipe_tcp_req = dcerpc_pipe_open_tcp_send(s->pipe2->conn,
94 s->binding->target_hostname,
95 atoi(s->binding->endpoint),
96 lp_name_resolve_order(global_loadparm));
97 composite_continue(c, pipe_tcp_req, continue_open_tcp, c);
101 pipe_ncalrpc_req = dcerpc_pipe_open_pipe_send(s->pipe2->conn, lp_ncalrpc_dir(global_loadparm),
102 s->binding->endpoint);
103 composite_continue(c, pipe_ncalrpc_req, continue_open_pipe, c);
107 /* looks like a transport we don't support */
108 composite_error(c, NT_STATUS_NOT_SUPPORTED);
116 Stage 2 of secondary_connection: Receive result of pipe open request on smb
118 static void continue_open_smb(struct composite_context *ctx)
120 struct composite_context *c = talloc_get_type(ctx->async.private_data,
121 struct composite_context);
123 c->status = dcerpc_pipe_open_smb_recv(ctx);
124 if (!composite_is_ok(c)) return;
126 continue_pipe_open(c);
131 Stage 2 of secondary_connection: Receive result of pipe open request on tcp/ip
133 static void continue_open_tcp(struct composite_context *ctx)
135 struct composite_context *c = talloc_get_type(ctx->async.private_data,
136 struct composite_context);
138 c->status = dcerpc_pipe_open_tcp_recv(ctx);
139 if (!composite_is_ok(c)) return;
141 continue_pipe_open(c);
146 Stage 2 of secondary_connection: Receive result of pipe open request on ncalrpc
148 static void continue_open_pipe(struct composite_context *ctx)
150 struct composite_context *c = talloc_get_type(ctx->async.private_data,
151 struct composite_context);
153 c->status = dcerpc_pipe_open_pipe_recv(ctx);
154 if (!composite_is_ok(c)) return;
156 continue_pipe_open(c);
161 Stage 3 of secondary_connection: Get binding data and flags from primary pipe
162 and say if we're done ok.
164 static void continue_pipe_open(struct composite_context *c)
166 struct sec_conn_state *s;
168 s = talloc_get_type(c->private_data, struct sec_conn_state);
170 s->pipe2->conn->flags = s->pipe->conn->flags;
171 s->pipe2->binding = s->binding;
172 if (!talloc_reference(s->pipe2, s->binding)) {
173 composite_error(c, NT_STATUS_NO_MEMORY);
182 Receive result of secondary rpc connection request and return
185 NTSTATUS dcerpc_secondary_connection_recv(struct composite_context *c,
186 struct dcerpc_pipe **p2)
188 NTSTATUS status = composite_wait(c);
189 struct sec_conn_state *s;
191 s = talloc_get_type(c->private_data, struct sec_conn_state);
193 if (NT_STATUS_IS_OK(status)) {
194 *p2 = talloc_steal(s->pipe, s->pipe2);
202 Create a secondary dcerpc connection from a primary connection
205 If the primary is a SMB connection then the secondary connection
206 will be on the same SMB connection, but using a new fnum
208 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p,
209 struct dcerpc_pipe **p2,
210 struct dcerpc_binding *b)
212 struct composite_context *c;
214 c = dcerpc_secondary_connection_send(p, b);
215 return dcerpc_secondary_connection_recv(c, p2);
219 Create a secondary DCERPC connection, then bind (and possibly
220 authenticate) using the supplied credentials.
222 This creates a second connection, to the same host (and on ncacn_np on the same connection) as the first
224 struct sec_auth_conn_state {
225 struct dcerpc_pipe *pipe2;
226 struct dcerpc_binding *binding;
227 const struct ndr_interface_table *table;
228 struct cli_credentials *credentials;
229 struct composite_context *ctx;
230 struct loadparm_context *lp_ctx;
233 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx);
234 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx);
236 struct composite_context* dcerpc_secondary_auth_connection_send(struct dcerpc_pipe *p,
237 struct dcerpc_binding *binding,
238 const struct ndr_interface_table *table,
239 struct cli_credentials *credentials,
240 struct loadparm_context *lp_ctx)
243 struct composite_context *c, *secondary_conn_ctx;
244 struct sec_auth_conn_state *s;
246 /* composite context allocation and setup */
247 c = composite_create(p, p->conn->event_ctx);
248 if (c == NULL) return NULL;
250 s = talloc_zero(c, struct sec_auth_conn_state);
251 if (composite_nomem(s, c)) return c;
255 s->binding = binding;
257 s->credentials = credentials;
260 secondary_conn_ctx = dcerpc_secondary_connection_send(p, binding);
262 if (composite_nomem(secondary_conn_ctx, s->ctx)) {
267 composite_continue(s->ctx, secondary_conn_ctx, dcerpc_secondary_auth_connection_bind,
273 Stage 2 of secondary_auth_connection:
274 Having made the secondary connection, we will need to do an (authenticated) bind
276 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx)
278 struct composite_context *secondary_auth_ctx;
279 struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
280 struct sec_auth_conn_state);
282 s->ctx->status = dcerpc_secondary_connection_recv(ctx, &s->pipe2);
283 if (!composite_is_ok(s->ctx)) return;
285 secondary_auth_ctx = dcerpc_pipe_auth_send(s->pipe2, s->binding, s->table, s->credentials,
287 composite_continue(s->ctx, secondary_auth_ctx, dcerpc_secondary_auth_connection_continue, s);
292 Stage 3 of secondary_auth_connection: Receive result of authenticated bind request
294 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx)
296 struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
297 struct sec_auth_conn_state);
299 s->ctx->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe2);
300 if (!composite_is_ok(s->ctx)) return;
302 composite_done(s->ctx);
306 Receive an authenticated pipe, created as a secondary connection
308 NTSTATUS dcerpc_secondary_auth_connection_recv(struct composite_context *c,
310 struct dcerpc_pipe **p)
312 NTSTATUS status = composite_wait(c);
313 struct sec_auth_conn_state *s;
315 s = talloc_get_type(c->private_data, struct sec_auth_conn_state);
317 if (NT_STATUS_IS_OK(status)) {
318 *p = talloc_steal(mem_ctx, s->pipe2);