r26334: Avoid loadparm_context.
[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 "auth/credentials/credentials.h"
31 #include "param/param.h"
32
33
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;
39 };
40
41
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);
46
47
48 /*
49   Send request to create a secondary dcerpc connection from a primary
50   connection
51 */
52 struct composite_context* dcerpc_secondary_connection_send(struct dcerpc_pipe *p,
53                                                            struct dcerpc_binding *b)
54 {
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;
60         
61         /* composite context allocation and setup */
62         c = composite_create(p, p->conn->event_ctx);
63         if (c == NULL) return NULL;
64
65         s = talloc_zero(c, struct sec_conn_state);
66         if (composite_nomem(s, c)) return c;
67         c->private_data = s;
68
69         s->pipe     = p;
70         s->binding  = b;
71
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;
75
76         /* open second dcerpc pipe using the same transport as for primary pipe */
77         switch (s->pipe->conn->transport.transport) {
78         case NCACN_NP:
79                 /* get smb tree of primary dcerpc pipe opened on smb */
80                 s->tree = dcerpc_smb_tree(s->pipe->conn);
81                 if (!s->tree) {
82                         composite_error(c, NT_STATUS_INVALID_PARAMETER);
83                         return c;
84                 }
85
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);
89                 return c;
90
91         case NCACN_IP_TCP:
92                 pipe_tcp_req = dcerpc_pipe_open_tcp_send(s->pipe2->conn,
93                                                          s->binding->host,
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);
98                 return c;
99
100         case NCALRPC:
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);
104                 return c;
105
106         default:
107                 /* looks like a transport we don't support */
108                 composite_error(c, NT_STATUS_NOT_SUPPORTED);
109         }
110
111         return c;
112 }
113
114
115 /*
116   Stage 2 of secondary_connection: Receive result of pipe open request on smb
117 */
118 static void continue_open_smb(struct composite_context *ctx)
119 {
120         struct composite_context *c = talloc_get_type(ctx->async.private_data,
121                                                       struct composite_context);
122         
123         c->status = dcerpc_pipe_open_smb_recv(ctx);
124         if (!composite_is_ok(c)) return;
125
126         continue_pipe_open(c);
127 }
128
129
130 /*
131   Stage 2 of secondary_connection: Receive result of pipe open request on tcp/ip
132 */
133 static void continue_open_tcp(struct composite_context *ctx)
134 {
135         struct composite_context *c = talloc_get_type(ctx->async.private_data,
136                                                       struct composite_context);
137         
138         c->status = dcerpc_pipe_open_tcp_recv(ctx);
139         if (!composite_is_ok(c)) return;
140
141         continue_pipe_open(c);
142 }
143
144
145 /*
146   Stage 2 of secondary_connection: Receive result of pipe open request on ncalrpc
147 */
148 static void continue_open_pipe(struct composite_context *ctx)
149 {
150         struct composite_context *c = talloc_get_type(ctx->async.private_data,
151                                                       struct composite_context);
152
153         c->status = dcerpc_pipe_open_pipe_recv(ctx);
154         if (!composite_is_ok(c)) return;
155
156         continue_pipe_open(c);
157 }
158
159
160 /*
161   Stage 3 of secondary_connection: Get binding data and flags from primary pipe
162   and say if we're done ok.
163 */
164 static void continue_pipe_open(struct composite_context *c)
165 {
166         struct sec_conn_state *s;
167
168         s = talloc_get_type(c->private_data, struct sec_conn_state);
169
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);
174                 return;
175         }
176
177         composite_done(c);
178 }
179
180
181 /*
182   Receive result of secondary rpc connection request and return
183   second dcerpc pipe.
184 */
185 NTSTATUS dcerpc_secondary_connection_recv(struct composite_context *c,
186                                           struct dcerpc_pipe **p2)
187 {
188         NTSTATUS status = composite_wait(c);
189         struct sec_conn_state *s;
190
191         s = talloc_get_type(c->private_data, struct sec_conn_state);
192
193         if (NT_STATUS_IS_OK(status)) {
194                 *p2 = talloc_steal(s->pipe, s->pipe2);
195         }
196
197         talloc_free(c);
198         return status;
199 }
200
201 /*
202   Create a secondary dcerpc connection from a primary connection
203   - sync version
204
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
207 */
208 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p,
209                                      struct dcerpc_pipe **p2,
210                                      struct dcerpc_binding *b)
211 {
212         struct composite_context *c;
213         
214         c = dcerpc_secondary_connection_send(p, b);
215         return dcerpc_secondary_connection_recv(c, p2);
216 }
217
218 /*
219   Create a secondary DCERPC connection, then bind (and possibly
220   authenticate) using the supplied credentials.
221
222   This creates a second connection, to the same host (and on ncacn_np on the same connection) as the first
223 */
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;
231 };
232
233 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx);
234 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx);
235
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)
241 {
242
243         struct composite_context *c, *secondary_conn_ctx;
244         struct sec_auth_conn_state *s;
245         
246         /* composite context allocation and setup */
247         c = composite_create(p, p->conn->event_ctx);
248         if (c == NULL) return NULL;
249
250         s = talloc_zero(c, struct sec_auth_conn_state);
251         if (composite_nomem(s, c)) return c;
252         c->private_data = s;
253         s->ctx = c;
254
255         s->binding  = binding;
256         s->table    = table;
257         s->credentials = credentials;
258         s->lp_ctx = lp_ctx;
259         
260         secondary_conn_ctx = dcerpc_secondary_connection_send(p, binding);
261         
262         if (composite_nomem(secondary_conn_ctx, s->ctx)) {
263                 talloc_free(c);
264                 return NULL;
265         }
266
267         composite_continue(s->ctx, secondary_conn_ctx, dcerpc_secondary_auth_connection_bind,
268                            s);
269         return c;
270 }
271
272 /*
273   Stage 2 of secondary_auth_connection: 
274   Having made the secondary connection, we will need to do an (authenticated) bind
275 */
276 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx)
277 {
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);
281         
282         s->ctx->status = dcerpc_secondary_connection_recv(ctx, &s->pipe2);
283         if (!composite_is_ok(s->ctx)) return;
284         
285         secondary_auth_ctx = dcerpc_pipe_auth_send(s->pipe2, s->binding, s->table, s->credentials,
286                                                    s->lp_ctx);
287         composite_continue(s->ctx, secondary_auth_ctx, dcerpc_secondary_auth_connection_continue, s);
288         
289 }
290
291 /*
292   Stage 3 of secondary_auth_connection: Receive result of authenticated bind request
293 */
294 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx)
295 {
296         struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
297                                                         struct sec_auth_conn_state);
298
299         s->ctx->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe2);
300         if (!composite_is_ok(s->ctx)) return;
301         
302         composite_done(s->ctx);
303 }
304
305 /*
306   Receive an authenticated pipe, created as a secondary connection
307 */
308 NTSTATUS dcerpc_secondary_auth_connection_recv(struct composite_context *c, 
309                                                TALLOC_CTX *mem_ctx,
310                                                struct dcerpc_pipe **p)
311 {
312         NTSTATUS status = composite_wait(c);
313         struct sec_auth_conn_state *s;
314
315         s = talloc_get_type(c->private_data, struct sec_auth_conn_state);
316
317         if (NT_STATUS_IS_OK(status)) {
318                 *p = talloc_steal(mem_ctx, s->pipe2);
319         }
320
321         talloc_free(c);
322         return status;
323 }