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