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 "librpc/rpc/dcerpc_proto.h"
31 #include "auth/credentials/credentials.h"
32 #include "param/param.h"
33 #include "libcli/resolve/resolve.h"
34 #include "lib/util/util_net.h"
36 struct sec_conn_state {
37 struct dcerpc_pipe *pipe;
38 struct dcerpc_pipe *pipe2;
39 struct dcerpc_binding *binding;
43 static void continue_open_smb(struct composite_context *ctx);
44 static void continue_open_tcp(struct composite_context *ctx);
45 static void continue_open_ncalrpc(struct composite_context *ctx);
46 static void continue_open_ncacn_unix(struct composite_context *ctx);
47 static void continue_pipe_open(struct composite_context *c);
51 Send request to create a secondary dcerpc connection from a primary
54 _PUBLIC_ struct composite_context* dcerpc_secondary_connection_send(struct dcerpc_pipe *p,
55 const struct dcerpc_binding *b)
57 struct composite_context *c;
58 struct sec_conn_state *s;
59 struct composite_context *pipe_smb_req;
60 struct composite_context *pipe_tcp_req;
61 const char *localaddress = NULL;
62 struct composite_context *pipe_ncalrpc_req;
63 const char *ncalrpc_dir = NULL;
64 struct composite_context *pipe_unix_req;
66 const char *target_hostname;
69 /* composite context allocation and setup */
70 c = composite_create(p, p->conn->event_ctx);
71 if (c == NULL) return NULL;
73 s = talloc_zero(c, struct sec_conn_state);
74 if (composite_nomem(s, c)) return c;
78 s->binding = dcerpc_binding_dup(s, b);
79 if (composite_nomem(s->binding, c)) return c;
81 /* initialise second dcerpc pipe based on primary pipe's event context */
82 s->pipe2 = dcerpc_pipe_init(c, s->pipe->conn->event_ctx);
83 if (composite_nomem(s->pipe2, c)) return c;
86 s->pipe2->conn->packet_log_dir = s->pipe->conn->packet_log_dir;
88 host = dcerpc_binding_get_string_option(s->binding, "host");
91 * We may fallback to the host of the given connection
93 host = dcerpc_binding_get_string_option(s->pipe->binding,
96 target_hostname = dcerpc_binding_get_string_option(s->binding, "target_hostname");
97 if (target_hostname == NULL) {
99 * We may fallback to the target_hostname of the given connection
101 target_hostname = dcerpc_binding_get_string_option(s->pipe->binding,
104 endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
105 if (endpoint == NULL) {
107 * We may fallback to the endpoint of the given connection
109 endpoint = dcerpc_binding_get_string_option(s->pipe->binding, "endpoint");
111 if (endpoint == NULL) {
112 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
116 /* open second dcerpc pipe using the same transport as for primary pipe */
117 switch (s->pipe->conn->transport.transport) {
119 pipe_smb_req = dcerpc_secondary_smb_send(s->pipe->conn,
122 composite_continue(c, pipe_smb_req, continue_open_smb, c);
127 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
131 if (!is_ipaddress(host)) {
133 * We may fallback to the host of the given connection
135 host = dcerpc_binding_get_string_option(s->pipe->binding,
138 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
141 if (!is_ipaddress(host)) {
142 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
147 localaddress = dcerpc_binding_get_string_option(s->binding,
149 if (localaddress == NULL) {
151 * We may fallback to the localaddress of the given connection
153 localaddress = dcerpc_binding_get_string_option(s->pipe->binding,
157 pipe_tcp_req = dcerpc_pipe_open_tcp_send(s->pipe2->conn,
162 resolve_context_init(s));
163 composite_continue(c, pipe_tcp_req, continue_open_tcp, c);
167 ncalrpc_dir = dcerpc_binding_get_string_option(s->binding,
169 if (ncalrpc_dir == NULL) {
170 ncalrpc_dir = dcerpc_binding_get_string_option(s->pipe->binding,
173 if (ncalrpc_dir == NULL) {
174 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
178 pipe_ncalrpc_req = dcerpc_pipe_open_pipe_send(s->pipe2->conn,
181 composite_continue(c, pipe_ncalrpc_req, continue_open_ncalrpc, c);
184 case NCACN_UNIX_STREAM:
185 pipe_unix_req = dcerpc_pipe_open_unix_stream_send(s->pipe2->conn,
187 composite_continue(c, pipe_unix_req, continue_open_ncacn_unix, c);
191 /* looks like a transport we don't support */
192 composite_error(c, NT_STATUS_NOT_SUPPORTED);
200 Stage 2 of secondary_connection: Receive result of pipe open request on smb
202 static void continue_open_smb(struct composite_context *ctx)
204 struct composite_context *c = talloc_get_type(ctx->async.private_data,
205 struct composite_context);
207 c->status = dcerpc_secondary_smb_recv(ctx);
208 if (!composite_is_ok(c)) return;
210 continue_pipe_open(c);
215 Stage 2 of secondary_connection: Receive result of pipe open request on tcp/ip
217 static void continue_open_tcp(struct composite_context *ctx)
219 struct composite_context *c = talloc_get_type(ctx->async.private_data,
220 struct composite_context);
221 struct sec_conn_state *s = talloc_get_type_abort(c->private_data,
222 struct sec_conn_state);
223 char *localaddr = NULL;
224 char *remoteaddr = NULL;
226 c->status = dcerpc_pipe_open_tcp_recv(ctx, s, &localaddr, &remoteaddr);
227 if (!composite_is_ok(c)) return;
229 c->status = dcerpc_binding_set_string_option(s->binding,
232 if (!composite_is_ok(c)) return;
234 c->status = dcerpc_binding_set_string_option(s->binding,
237 if (!composite_is_ok(c)) return;
239 continue_pipe_open(c);
243 Stage 2 of secondary_connection: Receive result of pipe open request on ncalrpc
245 static void continue_open_ncalrpc(struct composite_context *ctx)
247 struct composite_context *c = talloc_get_type(ctx->async.private_data,
248 struct composite_context);
250 c->status = dcerpc_pipe_open_pipe_recv(ctx);
251 if (!composite_is_ok(c)) return;
253 continue_pipe_open(c);
257 Stage 2 of secondary_connection: Receive result of pipe open request on ncacn_unix
259 static void continue_open_ncacn_unix(struct composite_context *ctx)
261 struct composite_context *c = talloc_get_type(ctx->async.private_data,
262 struct composite_context);
264 c->status = dcerpc_pipe_open_unix_stream_recv(ctx);
265 if (!composite_is_ok(c)) return;
267 continue_pipe_open(c);
272 Stage 3 of secondary_connection: Get binding data and flags from primary pipe
273 and say if we're done ok.
275 static void continue_pipe_open(struct composite_context *c)
277 struct sec_conn_state *s;
279 s = talloc_get_type(c->private_data, struct sec_conn_state);
281 s->pipe2->conn->flags = s->pipe->conn->flags;
282 s->pipe2->binding = dcerpc_binding_dup(s->pipe2, s->binding);
283 if (composite_nomem(s->pipe2->binding, c)) {
292 Receive result of secondary rpc connection request and return
295 _PUBLIC_ NTSTATUS dcerpc_secondary_connection_recv(struct composite_context *c,
296 struct dcerpc_pipe **p2)
298 NTSTATUS status = composite_wait(c);
299 struct sec_conn_state *s;
301 s = talloc_get_type(c->private_data, struct sec_conn_state);
303 if (NT_STATUS_IS_OK(status)) {
304 *p2 = talloc_steal(s->pipe, s->pipe2);
312 Create a secondary dcerpc connection from a primary connection
315 If the primary is a SMB connection then the secondary connection
316 will be on the same SMB connection, but using a new fnum
318 _PUBLIC_ NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p,
319 struct dcerpc_pipe **p2,
320 const struct dcerpc_binding *b)
322 struct composite_context *c;
324 c = dcerpc_secondary_connection_send(p, b);
325 return dcerpc_secondary_connection_recv(c, p2);
329 Create a secondary DCERPC connection, then bind (and possibly
330 authenticate) using the supplied credentials.
332 This creates a second connection, to the same host (and on ncacn_np on the same connection) as the first
334 struct sec_auth_conn_state {
335 struct dcerpc_pipe *pipe2;
336 const struct dcerpc_binding *binding;
337 const struct ndr_interface_table *table;
338 struct cli_credentials *credentials;
339 struct composite_context *ctx;
340 struct loadparm_context *lp_ctx;
343 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx);
344 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx);
346 _PUBLIC_ struct composite_context* dcerpc_secondary_auth_connection_send(struct dcerpc_pipe *p,
347 const struct dcerpc_binding *binding,
348 const struct ndr_interface_table *table,
349 struct cli_credentials *credentials,
350 struct loadparm_context *lp_ctx)
353 struct composite_context *c, *secondary_conn_ctx;
354 struct sec_auth_conn_state *s;
356 /* composite context allocation and setup */
357 c = composite_create(p, p->conn->event_ctx);
358 if (c == NULL) return NULL;
360 s = talloc_zero(c, struct sec_auth_conn_state);
361 if (composite_nomem(s, c)) return c;
365 s->binding = binding;
367 s->credentials = credentials;
370 secondary_conn_ctx = dcerpc_secondary_connection_send(p, binding);
372 if (composite_nomem(secondary_conn_ctx, s->ctx)) {
377 composite_continue(s->ctx, secondary_conn_ctx, dcerpc_secondary_auth_connection_bind,
383 Stage 2 of secondary_auth_connection:
384 Having made the secondary connection, we will need to do an (authenticated) bind
386 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx)
388 struct composite_context *secondary_auth_ctx;
389 struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
390 struct sec_auth_conn_state);
392 s->ctx->status = dcerpc_secondary_connection_recv(ctx, &s->pipe2);
393 if (!composite_is_ok(s->ctx)) return;
395 secondary_auth_ctx = dcerpc_pipe_auth_send(s->pipe2, s->binding, s->table, s->credentials,
397 composite_continue(s->ctx, secondary_auth_ctx, dcerpc_secondary_auth_connection_continue, s);
402 Stage 3 of secondary_auth_connection: Receive result of authenticated bind request
404 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx)
406 struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
407 struct sec_auth_conn_state);
409 s->ctx->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe2);
410 if (!composite_is_ok(s->ctx)) return;
412 composite_done(s->ctx);
416 Receive an authenticated pipe, created as a secondary connection
418 _PUBLIC_ NTSTATUS dcerpc_secondary_auth_connection_recv(struct composite_context *c,
420 struct dcerpc_pipe **p)
422 NTSTATUS status = composite_wait(c);
423 struct sec_auth_conn_state *s;
425 s = talloc_get_type(c->private_data, struct sec_auth_conn_state);
427 if (NT_STATUS_IS_OK(status)) {
428 *p = talloc_steal(mem_ctx, s->pipe2);
435 _PUBLIC_ NTSTATUS dcerpc_secondary_auth_connection(struct dcerpc_pipe *p,
436 const struct dcerpc_binding *binding,
437 const struct ndr_interface_table *table,
438 struct cli_credentials *credentials,
439 struct loadparm_context *lp_ctx,
441 struct dcerpc_pipe **p2)
443 struct composite_context *c;
445 c = dcerpc_secondary_auth_connection_send(p, binding, table,
446 credentials, lp_ctx);
447 return dcerpc_secondary_auth_connection_recv(c, mem_ctx, p2);