d6d87380ec1586d04c76deaba012d4113418b020
[samba.git] / source4 / libcli / composite / connect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /*
21   a composite API for making a full SMB connection
22 */
23
24 #include "includes.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27
28 /* the stages of this call */
29 enum connect_stage {CONNECT_RESOLVE, 
30                     CONNECT_SOCKET, 
31                     CONNECT_SESSION_REQUEST, 
32                     CONNECT_NEGPROT,
33                     CONNECT_SESSION_SETUP,
34                     CONNECT_TCON};
35
36 struct connect_state {
37         enum connect_stage stage;
38         struct smbcli_socket *sock;
39         struct smbcli_transport *transport;
40         struct smbcli_session *session;
41         struct smb_composite_connect *io;
42         union smb_tcon *io_tcon;
43         struct smb_composite_sesssetup *io_setup;
44         struct smbcli_request *req;
45         struct composite_context *creq;
46 };
47
48
49 static void request_handler(struct smbcli_request *);
50 static void composite_handler(struct composite_context *);
51
52 /*
53   setup a negprot send 
54 */
55 static NTSTATUS connect_send_negprot(struct composite_context *c, 
56                                      struct smb_composite_connect *io)
57 {
58         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
59
60         state->req = smb_raw_negotiate_send(state->transport, lp_maxprotocol());
61         NT_STATUS_HAVE_NO_MEMORY(state->req);
62
63         state->req->async.fn = request_handler;
64         state->req->async.private = c;
65         state->stage = CONNECT_NEGPROT;
66         
67         return NT_STATUS_OK;
68 }
69
70
71 /*
72   a tree connect request has competed
73 */
74 static NTSTATUS connect_tcon(struct composite_context *c, 
75                              struct smb_composite_connect *io)
76 {
77         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
78         NTSTATUS status;
79
80         status = smb_tree_connect_recv(state->req, c, state->io_tcon);
81         NT_STATUS_NOT_OK_RETURN(status);
82
83         io->out.tree->tid = state->io_tcon->tconx.out.tid;
84         if (state->io_tcon->tconx.out.dev_type) {
85                 io->out.tree->device = talloc_strdup(io->out.tree, 
86                                                      state->io_tcon->tconx.out.dev_type);
87         }
88         if (state->io_tcon->tconx.out.fs_type) {
89                 io->out.tree->fs_type = talloc_strdup(io->out.tree, 
90                                                       state->io_tcon->tconx.out.fs_type);
91         }
92
93         /* all done! */
94         c->state = SMBCLI_REQUEST_DONE;
95
96         return NT_STATUS_OK;
97 }
98
99
100 /*
101   a session setup request has competed
102 */
103 static NTSTATUS connect_session_setup(struct composite_context *c, 
104                                       struct smb_composite_connect *io)
105 {
106         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
107         NTSTATUS status;
108
109         status = smb_composite_sesssetup_recv(state->creq);
110         NT_STATUS_NOT_OK_RETURN(status);
111         
112         state->session->vuid = state->io_setup->out.vuid;
113         
114         /* setup for a tconx */
115         io->out.tree = smbcli_tree_init(state->session, state, True);
116         NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
117
118         state->io_tcon = talloc(c, union smb_tcon);
119         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
120
121         /* connect to a share using a tree connect */
122         state->io_tcon->generic.level = RAW_TCON_TCONX;
123         state->io_tcon->tconx.in.flags = 0;
124         state->io_tcon->tconx.in.password = data_blob(NULL, 0); 
125         
126         state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon, 
127                                                  "\\\\%s\\%s", 
128                                                  io->in.called_name, 
129                                                  io->in.service);
130         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
131         if (!io->in.service_type) {
132                 state->io_tcon->tconx.in.device = "?????";
133         } else {
134                 state->io_tcon->tconx.in.device = io->in.service_type;
135         }
136
137         state->req = smb_tree_connect_send(io->out.tree, state->io_tcon);
138         NT_STATUS_HAVE_NO_MEMORY(state->req);
139
140         state->req->async.fn = request_handler;
141         state->req->async.private = c;
142         state->stage = CONNECT_TCON;
143
144         return NT_STATUS_OK;
145 }
146
147 /*
148   a negprot request has competed
149 */
150 static NTSTATUS connect_negprot(struct composite_context *c, 
151                                 struct smb_composite_connect *io)
152 {
153         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
154         NTSTATUS status;
155
156         status = smb_raw_negotiate_recv(state->req);
157         NT_STATUS_NOT_OK_RETURN(status);
158
159         /* next step is a session setup */
160         state->session = smbcli_session_init(state->transport, state, True);
161         NT_STATUS_HAVE_NO_MEMORY(state->session);
162
163         state->io_setup = talloc(c, struct smb_composite_sesssetup);
164         NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
165
166         /* prepare a session setup to establish a security context */
167         state->io_setup->in.sesskey      = state->transport->negotiate.sesskey;
168         state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
169         state->io_setup->in.credentials  = io->in.credentials;
170         state->io_setup->in.workgroup    = io->in.workgroup;
171
172         state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
173         NT_STATUS_HAVE_NO_MEMORY(state->creq);
174
175         state->creq->async.fn = composite_handler;
176         state->creq->async.private = c;
177         state->stage = CONNECT_SESSION_SETUP;
178         
179         return NT_STATUS_OK;
180 }
181
182
183 /*
184   a session request operation has competed
185 */
186 static NTSTATUS connect_session_request(struct composite_context *c, 
187                                         struct smb_composite_connect *io)
188 {
189         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
190         NTSTATUS status;
191
192         status = smbcli_transport_connect_recv(state->req);
193         NT_STATUS_NOT_OK_RETURN(status);
194
195         /* next step is a negprot */
196         return connect_send_negprot(c, io);
197 }
198
199 /*
200   a socket connection operation has competed
201 */
202 static NTSTATUS connect_socket(struct composite_context *c, 
203                                struct smb_composite_connect *io)
204 {
205         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
206         NTSTATUS status;
207         struct nbt_name calling, called;
208
209         status = smbcli_sock_connect_recv(state->creq);
210         NT_STATUS_NOT_OK_RETURN(status);
211
212         /* the socket is up - we can initialise the smbcli transport layer */
213         state->transport = smbcli_transport_init(state->sock, state, True);
214         NT_STATUS_HAVE_NO_MEMORY(state->transport);
215
216         make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
217
218         nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
219
220         /* we have a connected socket - next step is a session
221            request, if needed. Port 445 doesn't need it, so it goes
222            straight to the negprot */
223         if (state->sock->port == 445) {
224                 status = nbt_name_dup(state->transport, &called, 
225                                       &state->transport->called);
226                 NT_STATUS_NOT_OK_RETURN(status);
227                 return connect_send_negprot(c, io);
228         }
229
230         state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
231         NT_STATUS_HAVE_NO_MEMORY(state->req);
232
233         state->req->async.fn = request_handler;
234         state->req->async.private = c;
235         state->stage = CONNECT_SESSION_REQUEST;
236
237         return NT_STATUS_OK;
238 }
239
240
241 /*
242   called when name resolution is finished
243 */
244 static NTSTATUS connect_resolve(struct composite_context *c, 
245                                 struct smb_composite_connect *io)
246 {
247         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
248         NTSTATUS status;
249         const char *address;
250
251         status = resolve_name_recv(state->creq, state, &address);
252         NT_STATUS_NOT_OK_RETURN(status);
253
254         state->creq = smbcli_sock_connect_send(state->sock, address, state->io->in.port, io->in.dest_host);
255         NT_STATUS_HAVE_NO_MEMORY(state->creq);
256
257         state->stage = CONNECT_SOCKET;
258         state->creq->async.private = c;
259         state->creq->async.fn = composite_handler;
260
261         return NT_STATUS_OK;
262 }
263
264
265 /*
266   handle and dispatch state transitions
267 */
268 static void state_handler(struct composite_context *c)
269 {
270         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
271
272         switch (state->stage) {
273         case CONNECT_RESOLVE:
274                 c->status = connect_resolve(c, state->io);
275                 break;
276         case CONNECT_SOCKET:
277                 c->status = connect_socket(c, state->io);
278                 break;
279         case CONNECT_SESSION_REQUEST:
280                 c->status = connect_session_request(c, state->io);
281                 break;
282         case CONNECT_NEGPROT:
283                 c->status = connect_negprot(c, state->io);
284                 break;
285         case CONNECT_SESSION_SETUP:
286                 c->status = connect_session_setup(c, state->io);
287                 break;
288         case CONNECT_TCON:
289                 c->status = connect_tcon(c, state->io);
290                 break;
291         }
292
293         if (!NT_STATUS_IS_OK(c->status)) {
294                 c->state = SMBCLI_REQUEST_ERROR;
295         }
296
297         if (c->state >= SMBCLI_REQUEST_DONE &&
298             c->async.fn) {
299                 c->async.fn(c);
300         }
301 }
302
303
304 /*
305   handler for completion of a smbcli_request sub-request
306 */
307 static void request_handler(struct smbcli_request *req)
308 {
309         struct composite_context *c = talloc_get_type(req->async.private, 
310                                                      struct composite_context);
311         state_handler(c);
312 }
313
314 /*
315   handler for completion of a smbcli_composite sub-request
316 */
317 static void composite_handler(struct composite_context *req)
318 {
319         struct composite_context *c = talloc_get_type(req->async.private, 
320                                                      struct composite_context);
321         state_handler(c);
322 }
323
324 /*
325   a function to establish a smbcli_tree from scratch
326 */
327 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
328                                                     struct event_context *event_ctx)
329 {
330         struct composite_context *c;
331         struct connect_state *state;
332         struct nbt_name name;
333
334         c = talloc_zero(NULL, struct composite_context);
335         if (c == NULL) goto failed;
336
337         state = talloc(c, struct connect_state);
338         if (state == NULL) goto failed;
339
340         state->sock = smbcli_sock_init(state, event_ctx);
341         if (state->sock == NULL) goto failed;
342
343         state->io = io;
344
345         c->state = SMBCLI_REQUEST_SEND;
346         c->event_ctx = talloc_reference(c, state->sock->event.ctx);
347         c->private = state;
348
349         /* if the destination is an IP address, then skip the name resolution part */
350         if (is_ipaddress(io->in.dest_host)) {
351                 state->stage = CONNECT_SOCKET;
352                 state->creq = smbcli_sock_connect_send(state->sock, io->in.dest_host, 
353                                                        state->io->in.port, 
354                                                        io->in.dest_host);
355         } else {
356                 state->stage = CONNECT_RESOLVE;
357                 make_nbt_name_server(&name, io->in.dest_host);
358                 state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
359         }
360
361         if (state->creq == NULL) goto failed;
362         state->creq->async.private = c;
363         state->creq->async.fn = composite_handler;
364
365         return c;
366 failed:
367         talloc_free(c);
368         return NULL;
369 }
370
371 /*
372   recv half of async composite connect code
373 */
374 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
375 {
376         NTSTATUS status;
377
378         status = composite_wait(c);
379
380         if (NT_STATUS_IS_OK(status)) {
381                 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
382                 talloc_steal(mem_ctx, state->io->out.tree);
383         }
384
385         talloc_free(c);
386         return status;
387 }
388
389 /*
390   sync version of smb_composite_connect 
391 */
392 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
393                                struct event_context *ev)
394 {
395         struct composite_context *c = smb_composite_connect_send(io, ev);
396         return smb_composite_connect_recv(c, mem_ctx);
397 }