03a408de79dc16bef2a80daacfe14dd9806cbc7e
[samba.git] / source / librpc / rpc / dcerpc_connect.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
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 2 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, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26
27 #include "includes.h"
28 #include "system/network.h"
29 #include "librpc/gen_ndr/ndr_epmapper.h"
30 #include "librpc/gen_ndr/ndr_dcerpc.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "libcli/raw/libcliraw.h"
33 #include "libcli/composite/composite.h"
34 #include "libcli/smb_composite/smb_composite.h"
35
36
37 struct dcerpc_pipe_connect;
38
39 struct pipe_np_smb_state {
40         struct smb_composite_connect conn;
41         struct smbcli_tree *tree;
42         struct dcerpc_pipe_connect io;
43 };
44
45
46 /*
47   Stage 3 of ncacn_np_smb: Named pipe opened (or not)
48 */
49 void continue_pipe_open_smb(struct composite_context *ctx)
50 {
51         struct composite_context *c = talloc_get_type(ctx->async.private_data,
52                                                       struct composite_context);
53         struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
54                                                       struct pipe_np_smb_state);
55
56         /* receive result of named pipe open request on smb */
57         c->status = dcerpc_pipe_open_smb_recv(ctx);
58         if (!NT_STATUS_IS_OK(c->status)) {
59                 DEBUG(0,("Failed to open pipe %s - %s\n", s->io.pipe_name, nt_errstr(c->status)));
60                 composite_error(c, c->status);
61                 return;
62         }
63
64         composite_done(c);
65 }
66
67 /*
68   Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
69 */
70 void continue_smb_connect(struct composite_context *ctx)
71 {
72         struct composite_context *open_ctx;
73         struct composite_context *c = talloc_get_type(ctx->async.private_data,
74                                                       struct composite_context);
75         struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
76                                                       struct pipe_np_smb_state);
77         
78         /* receive result of smb connect request */
79         c->status = smb_composite_connect_recv(ctx, c);
80         if (!NT_STATUS_IS_OK(c->status)) {
81                 DEBUG(0,("Failed to connect to %s - %s\n", s->io.binding->host, nt_errstr(c->status)));
82                 composite_error(c, c->status);
83                 return;
84         }
85
86         /* prepare named pipe open parameters */
87         s->tree         = s->conn.out.tree;
88         s->io.pipe_name = s->io.binding->endpoint;
89
90         /* send named pipe open request */
91         open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
92         if (open_ctx == NULL) {
93                 composite_error(c, NT_STATUS_NO_MEMORY);
94                 return;
95         }
96
97         composite_continue(c, open_ctx, continue_pipe_open_smb, c);
98 }
99
100
101 /*
102   Initiate async open of a rpc connection to a rpc pipe on SMB using
103   the binding structure to determine the endpoint and options
104 */
105 struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *tmp_ctx, 
106                                                                 struct dcerpc_pipe_connect *io)
107 {
108         struct composite_context *c;
109         struct pipe_np_smb_state *s;
110         struct composite_context *conn_req;
111         struct smb_composite_connect *conn;
112
113         /* composite context allocation and setup */
114         c = talloc_zero(tmp_ctx, struct composite_context);
115         if (c == NULL) return NULL;
116
117         s = talloc_zero(c, struct pipe_np_smb_state);
118         if (s == NULL) {
119                 composite_error(c, NT_STATUS_NO_MEMORY);
120                 goto done;
121         }
122
123         c->state = COMPOSITE_STATE_IN_PROGRESS;
124         c->private_data = s;
125         c->event_ctx = io->pipe->conn->event_ctx;
126
127         s->io  = *io;
128         conn   = &s->conn;
129
130         /* prepare smb connection parameters: we're connecting to IPC$ share on
131            remote rpc server */
132         conn->in.dest_host              = s->io.binding->host;
133         conn->in.port                   = 0;
134         conn->in.called_name            = strupper_talloc(tmp_ctx, s->io.binding->host);
135         conn->in.service                = "IPC$";
136         conn->in.service_type           = NULL;
137         conn->in.fallback_to_anonymous  = False;
138         conn->in.workgroup              = lp_workgroup();
139
140         /*
141          * provide proper credentials - user supplied or anonymous in case this is
142          * schannel connection
143          */
144         if (s->io.binding->flags & DCERPC_SCHANNEL) {
145                 struct cli_credentials *anon_creds;
146
147                 anon_creds = cli_credentials_init(tmp_ctx);
148                 if (!anon_creds) {
149                         composite_error(c, NT_STATUS_NO_MEMORY);
150                         goto done;
151                 }
152
153                 cli_credentials_set_anonymous(anon_creds);
154                 cli_credentials_guess(anon_creds);
155
156                 s->conn.in.credentials = anon_creds;
157
158         } else {
159                 s->conn.in.credentials = s->io.creds;
160         }
161
162         /* send smb connect request */
163         conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, s->io.pipe->conn->event_ctx);
164         if (!conn_req) {
165                 composite_error(c, NT_STATUS_NO_MEMORY);
166                 goto done;
167         }
168
169         composite_continue(c, conn_req, continue_smb_connect, c);
170
171 done:
172         return c;
173 }
174
175
176 /*
177   Receive result of a rpc connection to a rpc pipe on SMB
178 */
179 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
180 {
181         NTSTATUS status = composite_wait(c);
182
183         talloc_free(c);
184         return status;
185 }
186
187
188 /*
189   Sync version of a rpc connection to a rpc pipe on SMB
190 */
191 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb(TALLOC_CTX *tmp_ctx,
192                                           struct dcerpc_pipe_connect *io)
193 {
194         struct composite_context *c;
195         c = dcerpc_pipe_connect_ncacn_np_smb_send(tmp_ctx, io);
196         return dcerpc_pipe_connect_ncacn_np_smb_recv(c);
197 }
198
199
200 struct pipe_np_smb2_state {
201         struct smb2_tree *tree;
202         struct dcerpc_pipe_connect io;
203 };
204
205
206 /*
207   Stage 3 of ncacn_np_smb: Named pipe opened (or not)
208 */
209 void continue_pipe_open_smb2(struct composite_context *ctx)
210 {
211         struct composite_context *c = talloc_get_type(ctx->async.private_data,
212                                                       struct composite_context);
213         struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
214                                                        struct pipe_np_smb2_state);
215
216         /* receive result of named pipe open request on smb2 */
217         c->status = dcerpc_pipe_open_smb2_recv(ctx);
218         if (!NT_STATUS_IS_OK(c->status)) {
219                 DEBUG(0,("Failed to open pipe %s - %s\n", s->io.pipe_name, nt_errstr(c->status)));
220                 composite_error(c, c->status);
221                 return;
222         }
223
224         composite_done(c);
225 }
226
227
228 /*
229   Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
230 */
231 void continue_smb2_connect(struct composite_context *ctx)
232 {
233         struct composite_context *open_req;
234         struct composite_context *c = talloc_get_type(ctx->async.private_data,
235                                                       struct composite_context);
236         struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
237                                                        struct pipe_np_smb2_state);
238
239         /* receive result of smb2 connect request */
240         c->status = smb2_connect_recv(ctx, c, &s->tree);
241         if (!NT_STATUS_IS_OK(c->status)) {
242                 DEBUG(0,("Failed to connect to %s - %s\n", s->io.binding->host, nt_errstr(c->status)));
243                 composite_error(c, c->status);
244                 return;
245         }
246
247         /* prepare named pipe open parameters */
248         s->io.pipe_name = s->io.binding->endpoint;
249
250         /* send named pipe open request */
251         open_req = dcerpc_pipe_open_smb2_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
252         if (open_req == NULL) {
253                 composite_error(c, NT_STATUS_NO_MEMORY);
254                 return;
255         }
256
257         composite_continue(c, open_req, continue_pipe_open_smb2, c);
258 }
259
260
261 /* 
262    Initiate async open of a rpc connection request on SMB2 using
263    the binding structure to determine the endpoint and options
264 */
265 struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_CTX *mem_ctx,
266                                                                  struct dcerpc_pipe_connect *io)
267 {
268         struct composite_context *c;
269         struct pipe_np_smb2_state *s;
270         struct composite_context *conn_req;
271
272         /* composite context allocation and setup */
273         c = talloc_zero(mem_ctx, struct composite_context);
274         if (c == NULL) return NULL;
275
276         s = talloc_zero(c, struct pipe_np_smb2_state);
277         if (s == NULL) {
278                 composite_error(c, NT_STATUS_NO_MEMORY);
279                 goto done;
280         }
281         
282         c->state = COMPOSITE_STATE_IN_PROGRESS;
283         c->private_data = s;
284         c->event_ctx = io->pipe->conn->event_ctx;
285
286         s->io = *io;
287
288         /*
289          * provide proper credentials - user supplied or anonymous in case this is
290          * schannel connection
291          */
292         if (s->io.binding->flags & DCERPC_SCHANNEL) {
293                 s->io.creds = cli_credentials_init(mem_ctx);
294                 if (s->io.creds) {
295                         composite_error(c, NT_STATUS_NO_MEMORY);
296                         goto done;
297                 }
298
299                 cli_credentials_set_anonymous(s->io.creds);
300                 cli_credentials_guess(s->io.creds);
301         }
302
303         /* send smb2 connect request */
304         conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, "IPC$", s->io.creds,
305                                      c->event_ctx);
306         if (conn_req == NULL) {
307                 composite_error(c, NT_STATUS_NO_MEMORY);
308                 goto done;
309         }
310
311         composite_continue(c, conn_req, continue_smb2_connect, c);
312
313 done:
314         return c;
315 }
316
317
318 /*
319   Receive result of a rpc connection to a rpc pipe on SMB2
320 */
321 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
322 {
323         NTSTATUS status = composite_wait(c);
324         
325         talloc_free(c);
326         return status;
327 }
328
329
330 /*
331   Sync version of a rpc connection to a rpc pipe on SMB2
332 */
333 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2(TALLOC_CTX *mem_ctx,
334                                            struct dcerpc_pipe_connect *io)
335 {
336         struct composite_context *c;
337         c = dcerpc_pipe_connect_ncacn_np_smb2_send(mem_ctx, io);
338         return dcerpc_pipe_connect_ncacn_np_smb2_recv(c);
339 }