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