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