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