s3:smb2_negprot: add support for PROTOCOL_SMB3_00
[Samba/gebeck_regimport.git] / source4 / librpc / rpc / dcerpc_secondary.c
blob65466e45e03f944258a5f9a9895bb3295e9b733b
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 "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/socket/socket.h"
36 struct sec_conn_state {
37 struct dcerpc_pipe *pipe;
38 struct dcerpc_pipe *pipe2;
39 struct dcerpc_binding *binding;
40 struct smbcli_tree *tree;
41 struct socket_address *peer_addr;
45 static void continue_open_smb(struct composite_context *ctx);
46 static void continue_open_tcp(struct composite_context *ctx);
47 static void continue_open_pipe(struct composite_context *ctx);
48 static void continue_pipe_open(struct composite_context *c);
52 Send request to create a secondary dcerpc connection from a primary
53 connection
55 _PUBLIC_ struct composite_context* dcerpc_secondary_connection_send(struct dcerpc_pipe *p,
56 struct dcerpc_binding *b)
58 struct composite_context *c;
59 struct sec_conn_state *s;
60 struct composite_context *pipe_smb_req;
61 struct composite_context *pipe_tcp_req;
62 struct composite_context *pipe_ncalrpc_req;
64 /* composite context allocation and setup */
65 c = composite_create(p, p->conn->event_ctx);
66 if (c == NULL) return NULL;
68 s = talloc_zero(c, struct sec_conn_state);
69 if (composite_nomem(s, c)) return c;
70 c->private_data = s;
72 s->pipe = p;
73 s->binding = b;
75 /* initialise second dcerpc pipe based on primary pipe's event context */
76 s->pipe2 = dcerpc_pipe_init(c, s->pipe->conn->event_ctx);
77 if (composite_nomem(s->pipe2, c)) return c;
79 if (DEBUGLEVEL >= 10)
80 s->pipe2->conn->packet_log_dir = s->pipe->conn->packet_log_dir;
82 /* open second dcerpc pipe using the same transport as for primary pipe */
83 switch (s->pipe->conn->transport.transport) {
84 case NCACN_NP:
85 /* get smb tree of primary dcerpc pipe opened on smb */
86 s->tree = dcerpc_smb_tree(s->pipe->conn);
87 if (!s->tree) {
88 composite_error(c, NT_STATUS_INVALID_PARAMETER);
89 return c;
92 pipe_smb_req = dcerpc_pipe_open_smb_send(s->pipe2, s->tree,
93 s->binding->endpoint);
94 composite_continue(c, pipe_smb_req, continue_open_smb, c);
95 return c;
97 case NCACN_IP_TCP:
98 s->peer_addr = dcerpc_socket_peer_addr(s->pipe->conn, s);
99 if (!s->peer_addr) {
100 composite_error(c, NT_STATUS_INVALID_PARAMETER);
101 return c;
104 pipe_tcp_req = dcerpc_pipe_open_tcp_send(s->pipe2->conn,
105 s->binding->localaddress,
106 s->peer_addr->addr,
107 s->binding->target_hostname,
108 atoi(s->binding->endpoint),
109 resolve_context_init(s));
110 composite_continue(c, pipe_tcp_req, continue_open_tcp, c);
111 return c;
113 case NCALRPC:
114 case NCACN_UNIX_STREAM:
115 pipe_ncalrpc_req = dcerpc_pipe_open_unix_stream_send(s->pipe2->conn,
116 dcerpc_unix_socket_path(s->pipe->conn));
117 composite_continue(c, pipe_ncalrpc_req, continue_open_pipe, c);
118 return c;
120 default:
121 /* looks like a transport we don't support */
122 composite_error(c, NT_STATUS_NOT_SUPPORTED);
125 return c;
130 Stage 2 of secondary_connection: Receive result of pipe open request on smb
132 static void continue_open_smb(struct composite_context *ctx)
134 struct composite_context *c = talloc_get_type(ctx->async.private_data,
135 struct composite_context);
137 c->status = dcerpc_pipe_open_smb_recv(ctx);
138 if (!composite_is_ok(c)) return;
140 continue_pipe_open(c);
145 Stage 2 of secondary_connection: Receive result of pipe open request on tcp/ip
147 static void continue_open_tcp(struct composite_context *ctx)
149 struct composite_context *c = talloc_get_type(ctx->async.private_data,
150 struct composite_context);
152 c->status = dcerpc_pipe_open_tcp_recv(ctx);
153 if (!composite_is_ok(c)) return;
155 continue_pipe_open(c);
160 Stage 2 of secondary_connection: Receive result of pipe open request on ncalrpc
162 static void continue_open_pipe(struct composite_context *ctx)
164 struct composite_context *c = talloc_get_type(ctx->async.private_data,
165 struct composite_context);
167 c->status = dcerpc_pipe_open_pipe_recv(ctx);
168 if (!composite_is_ok(c)) return;
170 continue_pipe_open(c);
175 Stage 3 of secondary_connection: Get binding data and flags from primary pipe
176 and say if we're done ok.
178 static void continue_pipe_open(struct composite_context *c)
180 struct sec_conn_state *s;
182 s = talloc_get_type(c->private_data, struct sec_conn_state);
184 s->pipe2->conn->flags = s->pipe->conn->flags;
185 s->pipe2->binding = s->binding;
186 if (!talloc_reference(s->pipe2, s->binding)) {
187 composite_error(c, NT_STATUS_NO_MEMORY);
188 return;
191 composite_done(c);
196 Receive result of secondary rpc connection request and return
197 second dcerpc pipe.
199 _PUBLIC_ NTSTATUS dcerpc_secondary_connection_recv(struct composite_context *c,
200 struct dcerpc_pipe **p2)
202 NTSTATUS status = composite_wait(c);
203 struct sec_conn_state *s;
205 s = talloc_get_type(c->private_data, struct sec_conn_state);
207 if (NT_STATUS_IS_OK(status)) {
208 *p2 = talloc_steal(s->pipe, s->pipe2);
211 talloc_free(c);
212 return status;
216 Create a secondary dcerpc connection from a primary connection
217 - sync version
219 If the primary is a SMB connection then the secondary connection
220 will be on the same SMB connection, but using a new fnum
222 _PUBLIC_ NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p,
223 struct dcerpc_pipe **p2,
224 struct dcerpc_binding *b)
226 struct composite_context *c;
228 c = dcerpc_secondary_connection_send(p, b);
229 return dcerpc_secondary_connection_recv(c, p2);
233 Create a secondary DCERPC connection, then bind (and possibly
234 authenticate) using the supplied credentials.
236 This creates a second connection, to the same host (and on ncacn_np on the same connection) as the first
238 struct sec_auth_conn_state {
239 struct dcerpc_pipe *pipe2;
240 struct dcerpc_binding *binding;
241 const struct ndr_interface_table *table;
242 struct cli_credentials *credentials;
243 struct composite_context *ctx;
244 struct loadparm_context *lp_ctx;
247 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx);
248 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx);
250 _PUBLIC_ struct composite_context* dcerpc_secondary_auth_connection_send(struct dcerpc_pipe *p,
251 struct dcerpc_binding *binding,
252 const struct ndr_interface_table *table,
253 struct cli_credentials *credentials,
254 struct loadparm_context *lp_ctx)
257 struct composite_context *c, *secondary_conn_ctx;
258 struct sec_auth_conn_state *s;
260 /* composite context allocation and setup */
261 c = composite_create(p, p->conn->event_ctx);
262 if (c == NULL) return NULL;
264 s = talloc_zero(c, struct sec_auth_conn_state);
265 if (composite_nomem(s, c)) return c;
266 c->private_data = s;
267 s->ctx = c;
269 s->binding = binding;
270 s->table = table;
271 s->credentials = credentials;
272 s->lp_ctx = lp_ctx;
274 secondary_conn_ctx = dcerpc_secondary_connection_send(p, binding);
276 if (composite_nomem(secondary_conn_ctx, s->ctx)) {
277 talloc_free(c);
278 return NULL;
281 composite_continue(s->ctx, secondary_conn_ctx, dcerpc_secondary_auth_connection_bind,
283 return c;
287 Stage 2 of secondary_auth_connection:
288 Having made the secondary connection, we will need to do an (authenticated) bind
290 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx)
292 struct composite_context *secondary_auth_ctx;
293 struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
294 struct sec_auth_conn_state);
296 s->ctx->status = dcerpc_secondary_connection_recv(ctx, &s->pipe2);
297 if (!composite_is_ok(s->ctx)) return;
299 secondary_auth_ctx = dcerpc_pipe_auth_send(s->pipe2, s->binding, s->table, s->credentials,
300 s->lp_ctx);
301 composite_continue(s->ctx, secondary_auth_ctx, dcerpc_secondary_auth_connection_continue, s);
306 Stage 3 of secondary_auth_connection: Receive result of authenticated bind request
308 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx)
310 struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
311 struct sec_auth_conn_state);
313 s->ctx->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe2);
314 if (!composite_is_ok(s->ctx)) return;
316 composite_done(s->ctx);
320 Receive an authenticated pipe, created as a secondary connection
322 _PUBLIC_ NTSTATUS dcerpc_secondary_auth_connection_recv(struct composite_context *c,
323 TALLOC_CTX *mem_ctx,
324 struct dcerpc_pipe **p)
326 NTSTATUS status = composite_wait(c);
327 struct sec_auth_conn_state *s;
329 s = talloc_get_type(c->private_data, struct sec_auth_conn_state);
331 if (NT_STATUS_IS_OK(status)) {
332 *p = talloc_steal(mem_ctx, s->pipe2);
335 talloc_free(c);
336 return status;