r26313: Fix more uses of static loadparm.
[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                 composite_continue(c, pipe_tcp_req, continue_open_tcp, c);
97                 return c;
98
99         case NCALRPC:
100                 pipe_ncalrpc_req = dcerpc_pipe_open_pipe_send(s->pipe2->conn, lp_ncalrpc_dir(global_loadparm), 
101                                                               s->binding->endpoint);
102                 composite_continue(c, pipe_ncalrpc_req, continue_open_pipe, c);
103                 return c;
104
105         default:
106                 /* looks like a transport we don't support */
107                 composite_error(c, NT_STATUS_NOT_SUPPORTED);
108         }
109
110         return c;
111 }
112
113
114 /*
115   Stage 2 of secondary_connection: Receive result of pipe open request on smb
116 */
117 static void continue_open_smb(struct composite_context *ctx)
118 {
119         struct composite_context *c = talloc_get_type(ctx->async.private_data,
120                                                       struct composite_context);
121         
122         c->status = dcerpc_pipe_open_smb_recv(ctx);
123         if (!composite_is_ok(c)) return;
124
125         continue_pipe_open(c);
126 }
127
128
129 /*
130   Stage 2 of secondary_connection: Receive result of pipe open request on tcp/ip
131 */
132 static void continue_open_tcp(struct composite_context *ctx)
133 {
134         struct composite_context *c = talloc_get_type(ctx->async.private_data,
135                                                       struct composite_context);
136         
137         c->status = dcerpc_pipe_open_tcp_recv(ctx);
138         if (!composite_is_ok(c)) return;
139
140         continue_pipe_open(c);
141 }
142
143
144 /*
145   Stage 2 of secondary_connection: Receive result of pipe open request on ncalrpc
146 */
147 static void continue_open_pipe(struct composite_context *ctx)
148 {
149         struct composite_context *c = talloc_get_type(ctx->async.private_data,
150                                                       struct composite_context);
151
152         c->status = dcerpc_pipe_open_pipe_recv(ctx);
153         if (!composite_is_ok(c)) return;
154
155         continue_pipe_open(c);
156 }
157
158
159 /*
160   Stage 3 of secondary_connection: Get binding data and flags from primary pipe
161   and say if we're done ok.
162 */
163 static void continue_pipe_open(struct composite_context *c)
164 {
165         struct sec_conn_state *s;
166
167         s = talloc_get_type(c->private_data, struct sec_conn_state);
168
169         s->pipe2->conn->flags = s->pipe->conn->flags;
170         s->pipe2->binding     = s->binding;
171         if (!talloc_reference(s->pipe2, s->binding)) {
172                 composite_error(c, NT_STATUS_NO_MEMORY);
173                 return;
174         }
175
176         composite_done(c);
177 }
178
179
180 /*
181   Receive result of secondary rpc connection request and return
182   second dcerpc pipe.
183 */
184 NTSTATUS dcerpc_secondary_connection_recv(struct composite_context *c,
185                                           struct dcerpc_pipe **p2)
186 {
187         NTSTATUS status = composite_wait(c);
188         struct sec_conn_state *s;
189
190         s = talloc_get_type(c->private_data, struct sec_conn_state);
191
192         if (NT_STATUS_IS_OK(status)) {
193                 *p2 = talloc_steal(s->pipe, s->pipe2);
194         }
195
196         talloc_free(c);
197         return status;
198 }
199
200 /*
201   Create a secondary dcerpc connection from a primary connection
202   - sync version
203
204   If the primary is a SMB connection then the secondary connection
205   will be on the same SMB connection, but using a new fnum
206 */
207 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p,
208                                      struct dcerpc_pipe **p2,
209                                      struct dcerpc_binding *b)
210 {
211         struct composite_context *c;
212         
213         c = dcerpc_secondary_connection_send(p, b);
214         return dcerpc_secondary_connection_recv(c, p2);
215 }
216
217 /*
218   Create a secondary DCERPC connection, then bind (and possibly
219   authenticate) using the supplied credentials.
220
221   This creates a second connection, to the same host (and on ncacn_np on the same connection) as the first
222 */
223 struct sec_auth_conn_state {
224         struct dcerpc_pipe *pipe2;
225         struct dcerpc_binding *binding;
226         const struct ndr_interface_table *table;
227         struct cli_credentials *credentials;
228         struct composite_context *ctx;
229 };
230
231 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx);
232 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx);
233
234 struct composite_context* dcerpc_secondary_auth_connection_send(struct dcerpc_pipe *p,
235                                                                 struct dcerpc_binding *binding,
236                                                                 const struct ndr_interface_table *table,
237                                                                 struct cli_credentials *credentials)
238 {
239
240         struct composite_context *c, *secondary_conn_ctx;
241         struct sec_auth_conn_state *s;
242         
243         /* composite context allocation and setup */
244         c = composite_create(p, p->conn->event_ctx);
245         if (c == NULL) return NULL;
246
247         s = talloc_zero(c, struct sec_auth_conn_state);
248         if (composite_nomem(s, c)) return c;
249         c->private_data = s;
250         s->ctx = c;
251
252         s->binding  = binding;
253         s->table    = table;
254         s->credentials = credentials;
255         
256         secondary_conn_ctx = dcerpc_secondary_connection_send(p, binding);
257         
258         if (composite_nomem(secondary_conn_ctx, s->ctx)) {
259                 talloc_free(c);
260                 return NULL;
261         }
262
263         composite_continue(s->ctx, secondary_conn_ctx, dcerpc_secondary_auth_connection_bind,
264                            s);
265         return c;
266 }
267
268 /*
269   Stage 2 of secondary_auth_connection: 
270   Having made the secondary connection, we will need to do an (authenticated) bind
271 */
272 static void dcerpc_secondary_auth_connection_bind(struct composite_context *ctx)
273 {
274         struct composite_context *secondary_auth_ctx;
275         struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
276                                                         struct sec_auth_conn_state);
277         
278         s->ctx->status = dcerpc_secondary_connection_recv(ctx, &s->pipe2);
279         if (!composite_is_ok(s->ctx)) return;
280         
281         secondary_auth_ctx = dcerpc_pipe_auth_send(s->pipe2, s->binding, s->table, s->credentials);
282         composite_continue(s->ctx, secondary_auth_ctx, dcerpc_secondary_auth_connection_continue, s);
283         
284 }
285
286 /*
287   Stage 3 of secondary_auth_connection: Receive result of authenticated bind request
288 */
289 static void dcerpc_secondary_auth_connection_continue(struct composite_context *ctx)
290 {
291         struct sec_auth_conn_state *s = talloc_get_type(ctx->async.private_data,
292                                                         struct sec_auth_conn_state);
293
294         s->ctx->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe2);
295         if (!composite_is_ok(s->ctx)) return;
296         
297         composite_done(s->ctx);
298 }
299
300 /*
301   Receive an authenticated pipe, created as a secondary connection
302 */
303 NTSTATUS dcerpc_secondary_auth_connection_recv(struct composite_context *c, 
304                                                TALLOC_CTX *mem_ctx,
305                                                struct dcerpc_pipe **p)
306 {
307         NTSTATUS status = composite_wait(c);
308         struct sec_auth_conn_state *s;
309
310         s = talloc_get_type(c->private_data, struct sec_auth_conn_state);
311
312         if (NT_STATUS_IS_OK(status)) {
313                 *p = talloc_steal(mem_ctx, s->pipe2);
314         }
315
316         talloc_free(c);
317         return status;
318 }