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