r12608: Remove some unused #include lines.
[ira/wip.git] / source / 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 (is_ipaddress(state->sock->hostname) &&
306             (state->io->in.called_name != NULL)) {
307                 /* If connecting to an IP address, we might want the real name
308                  * of the host for later kerberos. The called name is a better
309                  * approximation */
310                 state->sock->hostname =
311                         talloc_strdup(state->sock, io->in.called_name);
312                 NT_STATUS_HAVE_NO_MEMORY(state->sock->hostname);
313         }
314
315         make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
316
317         nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
318
319         /* we have a connected socket - next step is a session
320            request, if needed. Port 445 doesn't need it, so it goes
321            straight to the negprot */
322         if (state->sock->port == 445) {
323                 status = nbt_name_dup(state->transport, &called, 
324                                       &state->transport->called);
325                 NT_STATUS_NOT_OK_RETURN(status);
326                 return connect_send_negprot(c, io);
327         }
328
329         state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
330         NT_STATUS_HAVE_NO_MEMORY(state->req);
331
332         state->req->async.fn = request_handler;
333         state->req->async.private = c;
334         state->stage = CONNECT_SESSION_REQUEST;
335
336         return NT_STATUS_OK;
337 }
338
339
340 /*
341   called when name resolution is finished
342 */
343 static NTSTATUS connect_resolve(struct composite_context *c, 
344                                 struct smb_composite_connect *io)
345 {
346         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
347         NTSTATUS status;
348         const char *address;
349
350         status = resolve_name_recv(state->creq, state, &address);
351         NT_STATUS_NOT_OK_RETURN(status);
352
353         state->creq = smbcli_sock_connect_send(state, address, io->in.port,
354                                                io->in.dest_host, c->event_ctx);
355         NT_STATUS_HAVE_NO_MEMORY(state->creq);
356
357         state->stage = CONNECT_SOCKET;
358         state->creq->async.private_data = c;
359         state->creq->async.fn = composite_handler;
360
361         return NT_STATUS_OK;
362 }
363
364
365 /*
366   handle and dispatch state transitions
367 */
368 static void state_handler(struct composite_context *c)
369 {
370         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
371
372         switch (state->stage) {
373         case CONNECT_RESOLVE:
374                 c->status = connect_resolve(c, state->io);
375                 break;
376         case CONNECT_SOCKET:
377                 c->status = connect_socket(c, state->io);
378                 break;
379         case CONNECT_SESSION_REQUEST:
380                 c->status = connect_session_request(c, state->io);
381                 break;
382         case CONNECT_NEGPROT:
383                 c->status = connect_negprot(c, state->io);
384                 break;
385         case CONNECT_SESSION_SETUP:
386                 c->status = connect_session_setup(c, state->io);
387                 break;
388         case CONNECT_SESSION_SETUP_ANON:
389                 c->status = connect_session_setup_anon(c, state->io);
390                 break;
391         case CONNECT_TCON:
392                 c->status = connect_tcon(c, state->io);
393                 break;
394         }
395
396         if (!NT_STATUS_IS_OK(c->status)) {
397                 c->state = COMPOSITE_STATE_ERROR;
398         }
399
400         if (c->state >= COMPOSITE_STATE_DONE &&
401             c->async.fn) {
402                 c->async.fn(c);
403         }
404 }
405
406
407 /*
408   handler for completion of a smbcli_request sub-request
409 */
410 static void request_handler(struct smbcli_request *req)
411 {
412         struct composite_context *c = talloc_get_type(req->async.private, 
413                                                      struct composite_context);
414         state_handler(c);
415 }
416
417 /*
418   handler for completion of a smbcli_composite sub-request
419 */
420 static void composite_handler(struct composite_context *creq)
421 {
422         struct composite_context *c = talloc_get_type(creq->async.private_data, 
423                                                      struct composite_context);
424         state_handler(c);
425 }
426
427 /*
428   a function to establish a smbcli_tree from scratch
429 */
430 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
431                                                      TALLOC_CTX *mem_ctx,
432                                                      struct event_context *event_ctx)
433 {
434         struct composite_context *c;
435         struct connect_state *state;
436         struct nbt_name name;
437
438         c = talloc_zero(mem_ctx, struct composite_context);
439         if (c == NULL) goto failed;
440
441         state = talloc(c, struct connect_state);
442         if (state == NULL) goto failed;
443
444         if (event_ctx == NULL) {
445                 event_ctx = event_context_init(mem_ctx);
446         }
447
448         state->io = io;
449
450         c->state = COMPOSITE_STATE_IN_PROGRESS;
451         c->event_ctx = talloc_reference(c, event_ctx);
452         c->private_data = state;
453
454         state->stage = CONNECT_RESOLVE;
455         make_nbt_name_server(&name, io->in.dest_host);
456         state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
457
458         if (state->creq == NULL) goto failed;
459         state->creq->async.private_data = c;
460         state->creq->async.fn = composite_handler;
461
462         return c;
463 failed:
464         talloc_free(c);
465         return NULL;
466 }
467
468 /*
469   recv half of async composite connect code
470 */
471 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
472 {
473         NTSTATUS status;
474
475         status = composite_wait(c);
476
477         if (NT_STATUS_IS_OK(status)) {
478                 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
479                 talloc_steal(mem_ctx, state->io->out.tree);
480         }
481
482         talloc_free(c);
483         return status;
484 }
485
486 /*
487   sync version of smb_composite_connect 
488 */
489 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
490                                struct event_context *ev)
491 {
492         struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
493         return smb_composite_connect_recv(c, mem_ctx);
494 }