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