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