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