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