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