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