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