sq dcerpc_binding_set_smbXcli_pointers s4:librpc/rpc: avoid using dcerpc_secondary_sm...
[metze/samba/wip.git] / source4 / librpc / rpc / dcerpc_secondary.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc connect functions
5
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
10    
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.
15    
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.
20    
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/>.
23 */
24
25
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 "libcli/smb/smbXcli_base.h"
32 #include "auth/credentials/credentials.h"
33 #include "param/param.h"
34 #include "libcli/resolve/resolve.h"
35 #include "lib/util/util_net.h"
36
37 struct sec_conn_state {
38         struct dcerpc_pipe *pipe;
39         struct dcerpc_pipe *pipe2;
40         struct dcerpc_binding *binding;
41         struct {
42                 struct smbXcli_conn *conn;
43                 struct smbXcli_session *session;
44                 struct smbXcli_tcon *tcon;
45         } smb;
46 };
47
48
49 static void continue_open_smb(struct composite_context *ctx);
50 static void continue_open_tcp(struct composite_context *ctx);
51 static void continue_open_ncalrpc(struct composite_context *ctx);
52 static void continue_open_ncacn_unix(struct composite_context *ctx);
53 static void continue_pipe_open(struct composite_context *c);
54
55
56 /*
57   Send request to create a secondary dcerpc connection from a primary
58   connection
59 */
60 _PUBLIC_ struct composite_context* dcerpc_secondary_connection_send(struct dcerpc_pipe *p,
61                                                         const struct dcerpc_binding *b)
62 {
63         struct composite_context *c;
64         struct sec_conn_state *s;
65         struct composite_context *pipe_smb_req;
66         struct composite_context *pipe_tcp_req;
67         const char *localaddress = NULL;
68         struct composite_context *pipe_ncalrpc_req;
69         const char *ncalrpc_dir = NULL;
70         struct composite_context *pipe_unix_req;
71         const char *host;
72         const char *target_hostname;
73         const char *endpoint;
74
75         /* composite context allocation and setup */
76         c = composite_create(p, p->conn->event_ctx);
77         if (c == NULL) return NULL;
78
79         s = talloc_zero(c, struct sec_conn_state);
80         if (composite_nomem(s, c)) return c;
81         c->private_data = s;
82
83         s->pipe     = p;
84         s->binding  = dcerpc_binding_dup(s, b);
85         if (composite_nomem(s->binding, c)) return c;
86
87         /* initialise second dcerpc pipe based on primary pipe's event context */
88         s->pipe2 = dcerpc_pipe_init(c, s->pipe->conn->event_ctx);
89         if (composite_nomem(s->pipe2, c)) return c;
90
91         if (DEBUGLEVEL >= 10)
92                 s->pipe2->conn->packet_log_dir = s->pipe->conn->packet_log_dir;
93
94         host = dcerpc_binding_get_string_option(s->binding, "host");
95         if (host == NULL) {
96                 /*
97                  * We may fallback to the host of the given connection
98                  */
99                 host = dcerpc_binding_get_string_option(s->pipe->binding,
100                                                         "host");
101         }
102         target_hostname = dcerpc_binding_get_string_option(s->binding, "target_hostname");
103         if (target_hostname == NULL) {
104                 /*
105                  * We may fallback to the target_hostname of the given connection
106                  */
107                 target_hostname = dcerpc_binding_get_string_option(s->pipe->binding,
108                                                                    "target_hostname");
109         }
110         endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
111         if (endpoint == NULL) {
112                 /*
113                  * We may fallback to the endpoint of the given connection
114                  */
115                 endpoint = dcerpc_binding_get_string_option(s->pipe->binding, "endpoint");
116         }
117         if (endpoint == NULL) {
118                 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
119                 return c;
120         }
121
122         /* open second dcerpc pipe using the same transport as for primary pipe */
123         switch (s->pipe->conn->transport.transport) {
124         case NCACN_NP:
125                 c->status = dcerpc_binding_get_smbXcli_pointers(s->pipe->binding,
126                                                                 &s->smb.conn,
127                                                                 &s->smb.session,
128                                                                 &s->smb.tcon);
129                 if (!NT_STATUS_IS_OK(c->status)) {
130                         composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
131                         return c;
132                 }
133
134                 pipe_smb_req = dcerpc_pipe_open_smb_send(s->pipe2->conn,
135                                                          s->smb.conn,
136                                                          s->smb.session,
137                                                          s->smb.tcon,
138                                                          DCERPC_REQUEST_TIMEOUT * 1000,
139                                                          endpoint);
140                 composite_continue(c, pipe_smb_req, continue_open_smb, c);
141                 return c;
142
143         case NCACN_IP_TCP:
144                 if (host == NULL) {
145                         composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
146                         return c;
147                 }
148
149                 if (!is_ipaddress(host)) {
150                         /*
151                          * We may fallback to the host of the given connection
152                          */
153                         host = dcerpc_binding_get_string_option(s->pipe->binding,
154                                                                 "host");
155                         if (host == NULL) {
156                                 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
157                                 return c;
158                         }
159                         if (!is_ipaddress(host)) {
160                                 composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
161                                 return c;
162                         }
163                 }
164
165                 localaddress = dcerpc_binding_get_string_option(s->binding,
166                                                                 "localaddress");
167                 if (localaddress == NULL) {
168                         /*
169                          * We may fallback to the localaddress of the given connection
170                          */
171                         localaddress = dcerpc_binding_get_string_option(s->pipe->binding,
172                                                                         "localaddress");
173                 }
174
175                 pipe_tcp_req = dcerpc_pipe_open_tcp_send(s->pipe2->conn,
176                                                          localaddress,
177                                                          host,
178                                                          target_hostname,
179                                                          atoi(endpoint),
180                                                          resolve_context_init(s));
181                 composite_continue(c, pipe_tcp_req, continue_open_tcp, c);
182                 return c;
183
184         case NCALRPC:
185                 ncalrpc_dir = dcerpc_binding_get_string_option(s->binding,
186                                                                "ncalrpc_dir");
187                 if (ncalrpc_dir == NULL) {
188                         ncalrpc_dir = dcerpc_binding_get_string_option(s->pipe->binding,
189                                                                 "ncalrpc_dir");
190                 }
191                 if (ncalrpc_dir == NULL) {
192                         composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
193                         return c;
194                 }
195
196                 pipe_ncalrpc_req = dcerpc_pipe_open_pipe_send(s->pipe2->conn,
197                                                               ncalrpc_dir,
198                                                               endpoint);
199                 composite_continue(c, pipe_ncalrpc_req, continue_open_ncalrpc, c);
200                 return c;
201
202         case NCACN_UNIX_STREAM:
203                 pipe_unix_req = dcerpc_pipe_open_unix_stream_send(s->pipe2->conn,
204                                                                   endpoint);
205                 composite_continue(c, pipe_unix_req, continue_open_ncacn_unix, c);
206                 return c;
207
208         default:
209                 /* looks like a transport we don't support */
210                 composite_error(c, NT_STATUS_NOT_SUPPORTED);
211         }
212
213         return c;
214 }
215
216
217 /*
218   Stage 2 of secondary_connection: Receive result of pipe open request on smb
219 */
220 static void continue_open_smb(struct composite_context *ctx)
221 {
222         struct composite_context *c = talloc_get_type(ctx->async.private_data,
223                                                       struct composite_context);
224         struct sec_conn_state *s = talloc_get_type_abort(c->private_data,
225                                                          struct sec_conn_state);
226
227         c->status = dcerpc_pipe_open_smb_recv(ctx);
228         if (!composite_is_ok(c)) return;
229
230         c->status = dcerpc_binding_set_smbXcli_pointers(s->binding,
231                                                         s->smb.conn,
232                                                         s->smb.session,
233                                                         s->smb.tcon);
234         if (!composite_is_ok(c)) return;
235
236         continue_pipe_open(c);
237 }
238
239
240 /*
241   Stage 2 of secondary_connection: Receive result of pipe open request on tcp/ip
242 */
243 static void continue_open_tcp(struct composite_context *ctx)
244 {
245         struct composite_context *c = talloc_get_type(ctx->async.private_data,
246                                                       struct composite_context);
247         struct sec_conn_state *s = talloc_get_type_abort(c->private_data,
248                                                          struct sec_conn_state);
249         char *localaddr = NULL;
250         char *remoteaddr = NULL;
251
252         c->status = dcerpc_pipe_open_tcp_recv(ctx, s, &localaddr, &remoteaddr);
253         if (!composite_is_ok(c)) return;
254
255         c->status = dcerpc_binding_set_string_option(s->binding,
256                                                      "localaddress",
257                                                      localaddr);
258         if (!composite_is_ok(c)) return;
259
260         c->status = dcerpc_binding_set_string_option(s->binding,
261                                                      "host",
262                                                      remoteaddr);
263         if (!composite_is_ok(c)) return;
264
265         continue_pipe_open(c);
266 }
267
268 /*
269   Stage 2 of secondary_connection: Receive result of pipe open request on ncalrpc
270 */
271 static void continue_open_ncalrpc(struct composite_context *ctx)
272 {
273         struct composite_context *c = talloc_get_type(ctx->async.private_data,
274                                                       struct composite_context);
275
276         c->status = dcerpc_pipe_open_pipe_recv(ctx);
277         if (!composite_is_ok(c)) return;
278
279         continue_pipe_open(c);
280 }
281
282 /*
283   Stage 2 of secondary_connection: Receive result of pipe open request on ncacn_unix
284 */
285 static void continue_open_ncacn_unix(struct composite_context *ctx)
286 {
287         struct composite_context *c = talloc_get_type(ctx->async.private_data,
288                                                       struct composite_context);
289
290         c->status = dcerpc_pipe_open_unix_stream_recv(ctx);
291         if (!composite_is_ok(c)) return;
292
293         continue_pipe_open(c);
294 }
295
296
297 /*
298   Stage 3 of secondary_connection: Get binding data and flags from primary pipe
299   and say if we're done ok.
300 */
301 static void continue_pipe_open(struct composite_context *c)
302 {
303         struct sec_conn_state *s;
304
305         s = talloc_get_type(c->private_data, struct sec_conn_state);
306
307         s->pipe2->conn->flags = s->pipe->conn->flags;
308         s->pipe2->binding     = dcerpc_binding_dup(s->pipe2, s->binding);
309         if (composite_nomem(s->pipe2->binding, c)) {
310                 return;
311         }
312
313         composite_done(c);
314 }
315
316
317 /*
318   Receive result of secondary rpc connection request and return
319   second dcerpc pipe.
320 */
321 _PUBLIC_ NTSTATUS dcerpc_secondary_connection_recv(struct composite_context *c,
322                                           struct dcerpc_pipe **p2)
323 {
324         NTSTATUS status = composite_wait(c);
325         struct sec_conn_state *s;
326
327         s = talloc_get_type(c->private_data, struct sec_conn_state);
328
329         if (NT_STATUS_IS_OK(status)) {
330                 *p2 = talloc_steal(s->pipe, s->pipe2);
331         }
332
333         talloc_free(c);
334         return status;
335 }
336
337 /*
338   Create a secondary DCERPC connection, then bind (and possibly
339   authenticate) using the supplied credentials.
340
341   This creates a second connection, to the same host (and on ncacn_np on the same connection) as the first
342 */
343 struct sec_auth_conn_state {
344         struct dcerpc_pipe *pipe2;
345         const struct dcerpc_binding *binding;
346         const struct ndr_interface_table *table;
347         struct cli_credentials *credentials;
348         struct composite_context *ctx;
349         struct loadparm_context *lp_ctx;
350 };
351
352 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx);
353 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx);
354
355 _PUBLIC_ struct composite_context* dcerpc_secondary_auth_connection_send(struct dcerpc_pipe *p,
356                                                                 const struct dcerpc_binding *binding,
357                                                                 const struct ndr_interface_table *table,
358                                                                 struct cli_credentials *credentials,
359                                                                 struct loadparm_context *lp_ctx)
360 {
361
362         struct composite_context *c, *secondary_conn_ctx;
363         struct sec_auth_conn_state *s;
364         
365         /* composite context allocation and setup */
366         c = composite_create(p, p->conn->event_ctx);
367         if (c == NULL) return NULL;
368
369         s = talloc_zero(c, struct sec_auth_conn_state);
370         if (composite_nomem(s, c)) return c;
371         c->private_data = s;
372         s->ctx = c;
373
374         s->binding  = binding;
375         s->table    = table;
376         s->credentials = credentials;
377         s->lp_ctx = lp_ctx;
378         
379         secondary_conn_ctx = dcerpc_secondary_connection_send(p, binding);
380         
381         if (composite_nomem(secondary_conn_ctx, s->ctx)) {
382                 talloc_free(c);
383                 return NULL;
384         }
385
386         composite_continue(s->ctx, secondary_conn_ctx, dcerpc_secondary_auth_connection_bind,
387                            s);
388         return c;
389 }
390
391 /*
392   Stage 2 of secondary_auth_connection: 
393   Having made the secondary connection, we will need to do an (authenticated) bind
394 */
395 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx)
396 {
397         struct composite_context *secondary_auth_ctx;
398         struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
399                                                         struct sec_auth_conn_state);
400         
401         s->ctx->status = dcerpc_secondary_connection_recv(ctx, &s->pipe2);
402         if (!composite_is_ok(s->ctx)) return;
403         
404         secondary_auth_ctx = dcerpc_pipe_auth_send(s->pipe2, s->binding, s->table, s->credentials,
405                                                    s->lp_ctx);
406         composite_continue(s->ctx, secondary_auth_ctx, dcerpc_secondary_auth_connection_continue, s);
407         
408 }
409
410 /*
411   Stage 3 of secondary_auth_connection: Receive result of authenticated bind request
412 */
413 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx)
414 {
415         struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
416                                                         struct sec_auth_conn_state);
417
418         s->ctx->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe2);
419         if (!composite_is_ok(s->ctx)) return;
420         
421         composite_done(s->ctx);
422 }
423
424 /*
425   Receive an authenticated pipe, created as a secondary connection
426 */
427 _PUBLIC_ NTSTATUS dcerpc_secondary_auth_connection_recv(struct composite_context *c, 
428                                                TALLOC_CTX *mem_ctx,
429                                                struct dcerpc_pipe **p)
430 {
431         NTSTATUS status = composite_wait(c);
432         struct sec_auth_conn_state *s;
433
434         s = talloc_get_type(c->private_data, struct sec_auth_conn_state);
435
436         if (NT_STATUS_IS_OK(status)) {
437                 *p = talloc_steal(mem_ctx, s->pipe2);
438         }
439
440         talloc_free(c);
441         return status;
442 }
443
444 _PUBLIC_ NTSTATUS dcerpc_secondary_auth_connection(struct dcerpc_pipe *p,
445                                         const struct dcerpc_binding *binding,
446                                         const struct ndr_interface_table *table,
447                                         struct cli_credentials *credentials,
448                                         struct loadparm_context *lp_ctx,
449                                         TALLOC_CTX *mem_ctx,
450                                         struct dcerpc_pipe **p2)
451 {
452         struct composite_context *c;
453
454         c = dcerpc_secondary_auth_connection_send(p, binding, table,
455                                                   credentials, lp_ctx);
456         return dcerpc_secondary_auth_connection_recv(c, mem_ctx, p2);
457 }