4191c43ca6e447995d9d739bbe9c761a742c4471
[kamenim/samba.git] / source4 / libcli / smb_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 #include "libcli/smb_composite/smb_composite.h"
28 #include "lib/events/events.h"
29 #include "libcli/nbt/libnbt.h"
30
31 /* the stages of this call */
32 enum connect_stage {CONNECT_RESOLVE, 
33                     CONNECT_SOCKET, 
34                     CONNECT_SESSION_REQUEST,
35                     CONNECT_NEGPROT,
36                     CONNECT_SESSION_SETUP,
37                     CONNECT_SESSION_SETUP_ANON,
38                     CONNECT_TCON};
39
40 struct connect_state {
41         enum connect_stage stage;
42         struct smbcli_socket *sock;
43         struct smbcli_transport *transport;
44         struct smbcli_session *session;
45         struct smb_composite_connect *io;
46         union smb_tcon *io_tcon;
47         struct smb_composite_sesssetup *io_setup;
48         struct smbcli_request *req;
49         struct composite_context *creq;
50 };
51
52
53 static void request_handler(struct smbcli_request *);
54 static void composite_handler(struct composite_context *);
55
56 /*
57   setup a negprot send 
58 */
59 static NTSTATUS connect_send_negprot(struct composite_context *c, 
60                                      struct smb_composite_connect *io)
61 {
62         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
63
64         state->req = smb_raw_negotiate_send(state->transport, lp_maxprotocol());
65         NT_STATUS_HAVE_NO_MEMORY(state->req);
66
67         state->req->async.fn = request_handler;
68         state->req->async.private = c;
69         state->stage = CONNECT_NEGPROT;
70         
71         return NT_STATUS_OK;
72 }
73
74
75 /*
76   a tree connect request has completed
77 */
78 static NTSTATUS connect_tcon(struct composite_context *c, 
79                              struct smb_composite_connect *io)
80 {
81         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
82         NTSTATUS status;
83
84         status = smb_raw_tcon_recv(state->req, c, state->io_tcon);
85         NT_STATUS_NOT_OK_RETURN(status);
86
87         io->out.tree->tid = state->io_tcon->tconx.out.tid;
88         if (state->io_tcon->tconx.out.dev_type) {
89                 io->out.tree->device = talloc_strdup(io->out.tree, 
90                                                      state->io_tcon->tconx.out.dev_type);
91         }
92         if (state->io_tcon->tconx.out.fs_type) {
93                 io->out.tree->fs_type = talloc_strdup(io->out.tree, 
94                                                       state->io_tcon->tconx.out.fs_type);
95         }
96
97         /* all done! */
98         c->state = COMPOSITE_STATE_DONE;
99
100         return NT_STATUS_OK;
101 }
102
103
104 /*
105   a session setup request with anonymous fallback has completed
106 */
107 static NTSTATUS connect_session_setup_anon(struct composite_context *c, 
108                                            struct smb_composite_connect *io)
109 {
110         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
111         NTSTATUS status;
112
113         status = smb_composite_sesssetup_recv(state->creq);
114         NT_STATUS_NOT_OK_RETURN(status);
115
116         io->out.anonymous_fallback_done = True;
117         
118         state->session->vuid = state->io_setup->out.vuid;
119         
120         /* setup for a tconx */
121         io->out.tree = smbcli_tree_init(state->session, state, True);
122         NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
123
124         state->io_tcon = talloc(c, union smb_tcon);
125         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
126
127         /* connect to a share using a tree connect */
128         state->io_tcon->generic.level = RAW_TCON_TCONX;
129         state->io_tcon->tconx.in.flags = 0;
130         state->io_tcon->tconx.in.password = data_blob(NULL, 0); 
131         
132         state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon, 
133                                                  "\\\\%s\\%s", 
134                                                  io->in.called_name, 
135                                                  io->in.service);
136         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
137         if (!io->in.service_type) {
138                 state->io_tcon->tconx.in.device = "?????";
139         } else {
140                 state->io_tcon->tconx.in.device = io->in.service_type;
141         }
142
143         state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
144         NT_STATUS_HAVE_NO_MEMORY(state->req);
145         if (state->req->state == SMBCLI_REQUEST_ERROR) {
146                 return state->req->status;
147         }
148
149         state->req->async.fn = request_handler;
150         state->req->async.private = c;
151         state->stage = CONNECT_TCON;
152
153         return NT_STATUS_OK;
154 }
155
156 /*
157   a session setup request has completed
158 */
159 static NTSTATUS connect_session_setup(struct composite_context *c, 
160                                       struct smb_composite_connect *io)
161 {
162         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
163         NTSTATUS status;
164
165         status = smb_composite_sesssetup_recv(state->creq);
166
167         if (!NT_STATUS_IS_OK(status) &&
168             !cli_credentials_is_anonymous(state->io->in.credentials) &&
169             io->in.fallback_to_anonymous) {
170
171                 state->io_setup->in.credentials = cli_credentials_init(state);
172                 NT_STATUS_HAVE_NO_MEMORY(state->io_setup->in.credentials);
173                 cli_credentials_set_conf(state->io_setup->in.credentials);
174                 cli_credentials_set_anonymous(state->io_setup->in.credentials);
175
176                 /* If the preceding attempt was with extended security, we
177                  * have been given a uid in the NTLMSSP_CHALLENGE reply. This
178                  * would lead to an invalid uid in the anonymous fallback */
179                 state->session->vuid = 0;
180
181                 state->creq = smb_composite_sesssetup_send(state->session,
182                                                            state->io_setup);
183                 NT_STATUS_HAVE_NO_MEMORY(state->creq);
184                 if (state->creq->state == COMPOSITE_STATE_ERROR) {
185                         return state->creq->status;
186                 }
187                 state->creq->async.fn = composite_handler;
188                 state->creq->async.private_data = c;
189                 state->stage = CONNECT_SESSION_SETUP_ANON;
190
191                 return NT_STATUS_OK;
192         }
193
194         NT_STATUS_NOT_OK_RETURN(status);
195         
196         state->session->vuid = state->io_setup->out.vuid;
197         
198         /* setup for a tconx */
199         io->out.tree = smbcli_tree_init(state->session, state, True);
200         NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
201
202         state->io_tcon = talloc(c, union smb_tcon);
203         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
204
205         /* connect to a share using a tree connect */
206         state->io_tcon->generic.level = RAW_TCON_TCONX;
207         state->io_tcon->tconx.in.flags = 0;
208         state->io_tcon->tconx.in.password = data_blob(NULL, 0); 
209         
210         state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon, 
211                                                  "\\\\%s\\%s", 
212                                                  io->in.called_name, 
213                                                  io->in.service);
214         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
215         if (!io->in.service_type) {
216                 state->io_tcon->tconx.in.device = "?????";
217         } else {
218                 state->io_tcon->tconx.in.device = io->in.service_type;
219         }
220
221         state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
222         NT_STATUS_HAVE_NO_MEMORY(state->req);
223         if (state->req->state == SMBCLI_REQUEST_ERROR) {
224                 return state->req->status;
225         }
226
227         state->req->async.fn = request_handler;
228         state->req->async.private = c;
229         state->stage = CONNECT_TCON;
230
231         return NT_STATUS_OK;
232 }
233
234 /*
235   a negprot request has completed
236 */
237 static NTSTATUS connect_negprot(struct composite_context *c, 
238                                 struct smb_composite_connect *io)
239 {
240         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
241         NTSTATUS status;
242
243         status = smb_raw_negotiate_recv(state->req);
244         NT_STATUS_NOT_OK_RETURN(status);
245
246         /* next step is a session setup */
247         state->session = smbcli_session_init(state->transport, state, True);
248         NT_STATUS_HAVE_NO_MEMORY(state->session);
249
250         state->io_setup = talloc(c, struct smb_composite_sesssetup);
251         NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
252
253         /* prepare a session setup to establish a security context */
254         state->io_setup->in.sesskey      = state->transport->negotiate.sesskey;
255         state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
256         state->io_setup->in.credentials  = io->in.credentials;
257         state->io_setup->in.workgroup    = io->in.workgroup;
258
259         state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
260         NT_STATUS_HAVE_NO_MEMORY(state->creq);
261         if (state->creq->state == COMPOSITE_STATE_ERROR) {
262                 return state->creq->status;
263         }
264
265         state->creq->async.fn = composite_handler;
266         state->creq->async.private_data = c;
267         state->stage = CONNECT_SESSION_SETUP;
268         
269         return NT_STATUS_OK;
270 }
271
272
273 /*
274   a session request operation has completed
275 */
276 static NTSTATUS connect_session_request(struct composite_context *c, 
277                                         struct smb_composite_connect *io)
278 {
279         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
280         NTSTATUS status;
281
282         status = smbcli_transport_connect_recv(state->req);
283         NT_STATUS_NOT_OK_RETURN(status);
284
285         /* next step is a negprot */
286         return connect_send_negprot(c, io);
287 }
288
289 /*
290   a socket connection operation has completed
291 */
292 static NTSTATUS connect_socket(struct composite_context *c, 
293                                struct smb_composite_connect *io)
294 {
295         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
296         NTSTATUS status;
297         struct nbt_name calling, called;
298
299         status = smbcli_sock_connect_recv(state->creq, state, &state->sock);
300         NT_STATUS_NOT_OK_RETURN(status);
301
302         /* the socket is up - we can initialise the smbcli transport layer */
303         state->transport = smbcli_transport_init(state->sock, state, True);
304         NT_STATUS_HAVE_NO_MEMORY(state->transport);
305
306         if (is_ipaddress(state->sock->hostname) &&
307             (state->io->in.called_name != NULL)) {
308                 /* If connecting to an IP address, we might want the real name
309                  * of the host for later kerberos. The called name is a better
310                  * approximation */
311                 state->sock->hostname =
312                         talloc_strdup(state->sock, io->in.called_name);
313                 NT_STATUS_HAVE_NO_MEMORY(state->sock->hostname);
314         }
315
316         make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
317
318         nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
319
320         /* we have a connected socket - next step is a session
321            request, if needed. Port 445 doesn't need it, so it goes
322            straight to the negprot */
323         if (state->sock->port == 445) {
324                 status = nbt_name_dup(state->transport, &called, 
325                                       &state->transport->called);
326                 NT_STATUS_NOT_OK_RETURN(status);
327                 return connect_send_negprot(c, io);
328         }
329
330         state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
331         NT_STATUS_HAVE_NO_MEMORY(state->req);
332
333         state->req->async.fn = request_handler;
334         state->req->async.private = c;
335         state->stage = CONNECT_SESSION_REQUEST;
336
337         return NT_STATUS_OK;
338 }
339
340
341 /*
342   called when name resolution is finished
343 */
344 static NTSTATUS connect_resolve(struct composite_context *c, 
345                                 struct smb_composite_connect *io)
346 {
347         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
348         NTSTATUS status;
349         const char *address;
350
351         status = resolve_name_recv(state->creq, state, &address);
352         NT_STATUS_NOT_OK_RETURN(status);
353
354         state->creq = smbcli_sock_connect_send(state, address, io->in.port,
355                                                io->in.dest_host, c->event_ctx);
356         NT_STATUS_HAVE_NO_MEMORY(state->creq);
357
358         state->stage = CONNECT_SOCKET;
359         state->creq->async.private_data = c;
360         state->creq->async.fn = composite_handler;
361
362         return NT_STATUS_OK;
363 }
364
365
366 /*
367   handle and dispatch state transitions
368 */
369 static void state_handler(struct composite_context *c)
370 {
371         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
372
373         switch (state->stage) {
374         case CONNECT_RESOLVE:
375                 c->status = connect_resolve(c, state->io);
376                 break;
377         case CONNECT_SOCKET:
378                 c->status = connect_socket(c, state->io);
379                 break;
380         case CONNECT_SESSION_REQUEST:
381                 c->status = connect_session_request(c, state->io);
382                 break;
383         case CONNECT_NEGPROT:
384                 c->status = connect_negprot(c, state->io);
385                 break;
386         case CONNECT_SESSION_SETUP:
387                 c->status = connect_session_setup(c, state->io);
388                 break;
389         case CONNECT_SESSION_SETUP_ANON:
390                 c->status = connect_session_setup_anon(c, state->io);
391                 break;
392         case CONNECT_TCON:
393                 c->status = connect_tcon(c, state->io);
394                 break;
395         }
396
397         if (!NT_STATUS_IS_OK(c->status)) {
398                 c->state = COMPOSITE_STATE_ERROR;
399         }
400
401         if (c->state >= COMPOSITE_STATE_DONE &&
402             c->async.fn) {
403                 c->async.fn(c);
404         }
405 }
406
407
408 /*
409   handler for completion of a smbcli_request sub-request
410 */
411 static void request_handler(struct smbcli_request *req)
412 {
413         struct composite_context *c = talloc_get_type(req->async.private, 
414                                                      struct composite_context);
415         state_handler(c);
416 }
417
418 /*
419   handler for completion of a smbcli_composite sub-request
420 */
421 static void composite_handler(struct composite_context *creq)
422 {
423         struct composite_context *c = talloc_get_type(creq->async.private_data, 
424                                                      struct composite_context);
425         state_handler(c);
426 }
427
428 /*
429   a function to establish a smbcli_tree from scratch
430 */
431 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
432                                                      TALLOC_CTX *mem_ctx,
433                                                      struct event_context *event_ctx)
434 {
435         struct composite_context *c;
436         struct connect_state *state;
437         struct nbt_name name;
438
439         c = talloc_zero(mem_ctx, struct composite_context);
440         if (c == NULL) goto failed;
441
442         state = talloc(c, struct connect_state);
443         if (state == NULL) goto failed;
444
445         if (event_ctx == NULL) {
446                 event_ctx = event_context_init(mem_ctx);
447         }
448
449         state->io = io;
450
451         c->state = COMPOSITE_STATE_IN_PROGRESS;
452         c->event_ctx = talloc_reference(c, event_ctx);
453         c->private_data = state;
454
455         state->stage = CONNECT_RESOLVE;
456         make_nbt_name_server(&name, io->in.dest_host);
457         state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
458
459         if (state->creq == NULL) goto failed;
460         state->creq->async.private_data = c;
461         state->creq->async.fn = composite_handler;
462
463         return c;
464 failed:
465         talloc_free(c);
466         return NULL;
467 }
468
469 /*
470   recv half of async composite connect code
471 */
472 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
473 {
474         NTSTATUS status;
475
476         status = composite_wait(c);
477
478         if (NT_STATUS_IS_OK(status)) {
479                 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
480                 talloc_steal(mem_ctx, state->io->out.tree);
481         }
482
483         talloc_free(c);
484         return status;
485 }
486
487 /*
488   sync version of smb_composite_connect 
489 */
490 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
491                                struct event_context *ev)
492 {
493         struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
494         return smb_composite_connect_recv(c, mem_ctx);
495 }