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