r11248: Fix anon fallback with spnego
[ab/samba.git/.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
30 /* the stages of this call */
31 enum connect_stage {CONNECT_RESOLVE, 
32                     CONNECT_SOCKET, 
33                     CONNECT_SESSION_REQUEST,
34                     CONNECT_NEGPROT,
35                     CONNECT_SESSION_SETUP,
36                     CONNECT_SESSION_SETUP_ANON,
37                     CONNECT_TCON};
38
39 struct connect_state {
40         enum connect_stage stage;
41         struct smbcli_socket *sock;
42         struct smbcli_transport *transport;
43         struct smbcli_session *session;
44         struct smb_composite_connectmulti *conn;
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 competed
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 competed
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 competed
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 competed
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 = smb_composite_connectmulti_recv(state->creq, state);
300         NT_STATUS_NOT_OK_RETURN(status);
301
302         state->sock = state->conn->out.socket;
303
304         /* the socket is up - we can initialise the smbcli transport layer */
305         state->transport = smbcli_transport_init(state->sock, state, True);
306         NT_STATUS_HAVE_NO_MEMORY(state->transport);
307
308         make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
309
310         nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
311
312         /* we have a connected socket - next step is a session
313            request, if needed. Port 445 doesn't need it, so it goes
314            straight to the negprot */
315         if (state->sock->port == 445) {
316                 status = nbt_name_dup(state->transport, &called, 
317                                       &state->transport->called);
318                 NT_STATUS_NOT_OK_RETURN(status);
319                 return connect_send_negprot(c, io);
320         }
321
322         state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
323         NT_STATUS_HAVE_NO_MEMORY(state->req);
324
325         state->req->async.fn = request_handler;
326         state->req->async.private = c;
327         state->stage = CONNECT_SESSION_REQUEST;
328
329         return NT_STATUS_OK;
330 }
331
332
333 /*
334   called when name resolution is finished
335 */
336 static NTSTATUS connect_resolve(struct composite_context *c, 
337                                 struct smb_composite_connect *io)
338 {
339         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
340         NTSTATUS status;
341         const char *address;
342         struct smb_composite_connectmulti *conn;
343
344         status = resolve_name_recv(state->creq, state, &address);
345         NT_STATUS_NOT_OK_RETURN(status);
346
347         conn = talloc(state, struct smb_composite_connectmulti);
348         NT_STATUS_HAVE_NO_MEMORY(conn);
349         state->conn = conn;
350         conn->in.num_dests = 1;
351
352         conn->in.addresses = talloc_array(state->conn, const char *, 1);
353         NT_STATUS_HAVE_NO_MEMORY(conn->in.addresses);
354         conn->in.addresses[0] = address;
355
356         conn->in.hostnames = talloc_array(state->conn, const char *, 1);
357         NT_STATUS_HAVE_NO_MEMORY(conn->in.hostnames);
358         conn->in.hostnames[0] = state->io->in.dest_host;
359         
360         conn->in.ports = NULL;
361         if (state->io->in.port != 0) {
362                 conn->in.ports = talloc_array(state->conn, int, 1);
363                 NT_STATUS_HAVE_NO_MEMORY(conn->in.ports);
364                 conn->in.ports[0] = state->io->in.port;
365         }
366
367         state->creq = smb_composite_connectmulti_send(conn, state,
368                                                       c->event_ctx);
369         NT_STATUS_HAVE_NO_MEMORY(state->creq);
370
371         state->stage = CONNECT_SOCKET;
372         state->creq->async.private_data = c;
373         state->creq->async.fn = composite_handler;
374
375         return NT_STATUS_OK;
376 }
377
378
379 /*
380   handle and dispatch state transitions
381 */
382 static void state_handler(struct composite_context *c)
383 {
384         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
385
386         switch (state->stage) {
387         case CONNECT_RESOLVE:
388                 c->status = connect_resolve(c, state->io);
389                 break;
390         case CONNECT_SOCKET:
391                 c->status = connect_socket(c, state->io);
392                 break;
393         case CONNECT_SESSION_REQUEST:
394                 c->status = connect_session_request(c, state->io);
395                 break;
396         case CONNECT_NEGPROT:
397                 c->status = connect_negprot(c, state->io);
398                 break;
399         case CONNECT_SESSION_SETUP:
400                 c->status = connect_session_setup(c, state->io);
401                 break;
402         case CONNECT_SESSION_SETUP_ANON:
403                 c->status = connect_session_setup_anon(c, state->io);
404                 break;
405         case CONNECT_TCON:
406                 c->status = connect_tcon(c, state->io);
407                 break;
408         }
409
410         if (!NT_STATUS_IS_OK(c->status)) {
411                 c->state = COMPOSITE_STATE_ERROR;
412         }
413
414         if (c->state >= COMPOSITE_STATE_DONE &&
415             c->async.fn) {
416                 c->async.fn(c);
417         }
418 }
419
420
421 /*
422   handler for completion of a smbcli_request sub-request
423 */
424 static void request_handler(struct smbcli_request *req)
425 {
426         struct composite_context *c = talloc_get_type(req->async.private, 
427                                                      struct composite_context);
428         state_handler(c);
429 }
430
431 /*
432   handler for completion of a smbcli_composite sub-request
433 */
434 static void composite_handler(struct composite_context *creq)
435 {
436         struct composite_context *c = talloc_get_type(creq->async.private_data, 
437                                                      struct composite_context);
438         state_handler(c);
439 }
440
441 /*
442   a function to establish a smbcli_tree from scratch
443 */
444 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
445                                                      TALLOC_CTX *mem_ctx,
446                                                      struct event_context *event_ctx)
447 {
448         struct composite_context *c;
449         struct connect_state *state;
450         struct nbt_name name;
451
452         c = talloc_zero(mem_ctx, struct composite_context);
453         if (c == NULL) goto failed;
454
455         state = talloc(c, struct connect_state);
456         if (state == NULL) goto failed;
457
458         if (event_ctx == NULL) {
459                 event_ctx = event_context_init(mem_ctx);
460         }
461
462         state->io = io;
463
464         c->state = COMPOSITE_STATE_IN_PROGRESS;
465         c->event_ctx = talloc_reference(c, event_ctx);
466         c->private_data = state;
467
468         state->stage = CONNECT_RESOLVE;
469         make_nbt_name_server(&name, io->in.dest_host);
470         state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
471
472         if (state->creq == NULL) goto failed;
473         state->creq->async.private_data = c;
474         state->creq->async.fn = composite_handler;
475
476         return c;
477 failed:
478         talloc_free(c);
479         return NULL;
480 }
481
482 /*
483   recv half of async composite connect code
484 */
485 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
486 {
487         NTSTATUS status;
488
489         status = composite_wait(c);
490
491         if (NT_STATUS_IS_OK(status)) {
492                 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
493                 talloc_steal(mem_ctx, state->io->out.tree);
494         }
495
496         talloc_free(c);
497         return status;
498 }
499
500 /*
501   sync version of smb_composite_connect 
502 */
503 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
504                                struct event_context *ev)
505 {
506         struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
507         return smb_composite_connect_recv(c, mem_ctx);
508 }