r23792: convert Samba4 to GPLv3
[mdw/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/composite/composite.h"
26 #include "libcli/smb_composite/smb_composite.h"
27 #include "lib/events/events.h"
28 #include "libcli/resolve/resolve.h"
29 #include "auth/credentials/credentials.h"
30 #include "librpc/gen_ndr/ndr_nbt.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_cli_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                 data_blob_free(&state->session->user_session_key);
182                 talloc_free(state->session->gensec);
183                 state->session->gensec = NULL;
184
185                 state->creq = smb_composite_sesssetup_send(state->session,
186                                                            state->io_setup);
187                 NT_STATUS_HAVE_NO_MEMORY(state->creq);
188                 if (state->creq->state == COMPOSITE_STATE_ERROR) {
189                         return state->creq->status;
190                 }
191                 state->creq->async.fn = composite_handler;
192                 state->creq->async.private_data = c;
193                 state->stage = CONNECT_SESSION_SETUP_ANON;
194
195                 return NT_STATUS_OK;
196         }
197
198         NT_STATUS_NOT_OK_RETURN(status);
199         
200         state->session->vuid = state->io_setup->out.vuid;
201         
202         /* setup for a tconx */
203         io->out.tree = smbcli_tree_init(state->session, state, True);
204         NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
205
206         state->io_tcon = talloc(c, union smb_tcon);
207         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
208
209         /* connect to a share using a tree connect */
210         state->io_tcon->generic.level = RAW_TCON_TCONX;
211         state->io_tcon->tconx.in.flags = 0;
212         state->io_tcon->tconx.in.password = data_blob(NULL, 0); 
213         
214         state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon, 
215                                                  "\\\\%s\\%s", 
216                                                  io->in.called_name, 
217                                                  io->in.service);
218         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
219         if (!io->in.service_type) {
220                 state->io_tcon->tconx.in.device = "?????";
221         } else {
222                 state->io_tcon->tconx.in.device = io->in.service_type;
223         }
224
225         state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
226         NT_STATUS_HAVE_NO_MEMORY(state->req);
227         if (state->req->state == SMBCLI_REQUEST_ERROR) {
228                 return state->req->status;
229         }
230
231         state->req->async.fn = request_handler;
232         state->req->async.private = c;
233         state->stage = CONNECT_TCON;
234
235         return NT_STATUS_OK;
236 }
237
238 /*
239   a negprot request has completed
240 */
241 static NTSTATUS connect_negprot(struct composite_context *c, 
242                                 struct smb_composite_connect *io)
243 {
244         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
245         NTSTATUS status;
246
247         status = smb_raw_negotiate_recv(state->req);
248         NT_STATUS_NOT_OK_RETURN(status);
249
250         /* next step is a session setup */
251         state->session = smbcli_session_init(state->transport, state, True);
252         NT_STATUS_HAVE_NO_MEMORY(state->session);
253
254         state->io_setup = talloc(c, struct smb_composite_sesssetup);
255         NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
256
257         /* prepare a session setup to establish a security context */
258         state->io_setup->in.sesskey      = state->transport->negotiate.sesskey;
259         state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
260         state->io_setup->in.credentials  = io->in.credentials;
261         state->io_setup->in.workgroup    = io->in.workgroup;
262
263         state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
264         NT_STATUS_HAVE_NO_MEMORY(state->creq);
265         if (state->creq->state == COMPOSITE_STATE_ERROR) {
266                 return state->creq->status;
267         }
268
269         state->creq->async.fn = composite_handler;
270         state->creq->async.private_data = c;
271         state->stage = CONNECT_SESSION_SETUP;
272         
273         return NT_STATUS_OK;
274 }
275
276
277 /*
278   a session request operation has completed
279 */
280 static NTSTATUS connect_session_request(struct composite_context *c, 
281                                         struct smb_composite_connect *io)
282 {
283         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
284         NTSTATUS status;
285
286         status = smbcli_transport_connect_recv(state->req);
287         NT_STATUS_NOT_OK_RETURN(status);
288
289         /* next step is a negprot */
290         return connect_send_negprot(c, io);
291 }
292
293 /*
294   a socket connection operation has completed
295 */
296 static NTSTATUS connect_socket(struct composite_context *c, 
297                                struct smb_composite_connect *io)
298 {
299         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
300         NTSTATUS status;
301         struct nbt_name calling, called;
302
303         status = smbcli_sock_connect_recv(state->creq, state, &state->sock);
304         NT_STATUS_NOT_OK_RETURN(status);
305
306         /* the socket is up - we can initialise the smbcli transport layer */
307         state->transport = smbcli_transport_init(state->sock, state, True);
308         NT_STATUS_HAVE_NO_MEMORY(state->transport);
309
310         if (is_ipaddress(state->sock->hostname) &&
311             (state->io->in.called_name != NULL)) {
312                 /* If connecting to an IP address, we might want the real name
313                  * of the host for later kerberos. The called name is a better
314                  * approximation */
315                 state->sock->hostname =
316                         talloc_strdup(state->sock, io->in.called_name);
317                 NT_STATUS_HAVE_NO_MEMORY(state->sock->hostname);
318         }
319
320         make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
321
322         nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
323
324         /* we have a connected socket - next step is a session
325            request, if needed. Port 445 doesn't need it, so it goes
326            straight to the negprot */
327         if (state->sock->port == 445) {
328                 status = nbt_name_dup(state->transport, &called, 
329                                       &state->transport->called);
330                 NT_STATUS_NOT_OK_RETURN(status);
331                 return connect_send_negprot(c, io);
332         }
333
334         state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
335         NT_STATUS_HAVE_NO_MEMORY(state->req);
336
337         state->req->async.fn = request_handler;
338         state->req->async.private = c;
339         state->stage = CONNECT_SESSION_REQUEST;
340
341         return NT_STATUS_OK;
342 }
343
344
345 /*
346   called when name resolution is finished
347 */
348 static NTSTATUS connect_resolve(struct composite_context *c, 
349                                 struct smb_composite_connect *io)
350 {
351         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
352         NTSTATUS status;
353         const char *address;
354
355         status = resolve_name_recv(state->creq, state, &address);
356         NT_STATUS_NOT_OK_RETURN(status);
357
358         state->creq = smbcli_sock_connect_send(state, address, io->in.port,
359                                                io->in.dest_host, c->event_ctx);
360         NT_STATUS_HAVE_NO_MEMORY(state->creq);
361
362         state->stage = CONNECT_SOCKET;
363         state->creq->async.private_data = c;
364         state->creq->async.fn = composite_handler;
365
366         return NT_STATUS_OK;
367 }
368
369
370 /*
371   handle and dispatch state transitions
372 */
373 static void state_handler(struct composite_context *c)
374 {
375         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
376
377         switch (state->stage) {
378         case CONNECT_RESOLVE:
379                 c->status = connect_resolve(c, state->io);
380                 break;
381         case CONNECT_SOCKET:
382                 c->status = connect_socket(c, state->io);
383                 break;
384         case CONNECT_SESSION_REQUEST:
385                 c->status = connect_session_request(c, state->io);
386                 break;
387         case CONNECT_NEGPROT:
388                 c->status = connect_negprot(c, state->io);
389                 break;
390         case CONNECT_SESSION_SETUP:
391                 c->status = connect_session_setup(c, state->io);
392                 break;
393         case CONNECT_SESSION_SETUP_ANON:
394                 c->status = connect_session_setup_anon(c, state->io);
395                 break;
396         case CONNECT_TCON:
397                 c->status = connect_tcon(c, state->io);
398                 break;
399         }
400
401         if (!NT_STATUS_IS_OK(c->status)) {
402                 c->state = COMPOSITE_STATE_ERROR;
403         }
404
405         if (c->state >= COMPOSITE_STATE_DONE &&
406             c->async.fn) {
407                 c->async.fn(c);
408         }
409 }
410
411
412 /*
413   handler for completion of a smbcli_request sub-request
414 */
415 static void request_handler(struct smbcli_request *req)
416 {
417         struct composite_context *c = talloc_get_type(req->async.private, 
418                                                      struct composite_context);
419         state_handler(c);
420 }
421
422 /*
423   handler for completion of a smbcli_composite sub-request
424 */
425 static void composite_handler(struct composite_context *creq)
426 {
427         struct composite_context *c = talloc_get_type(creq->async.private_data, 
428                                                      struct composite_context);
429         state_handler(c);
430 }
431
432 /*
433   a function to establish a smbcli_tree from scratch
434 */
435 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
436                                                      TALLOC_CTX *mem_ctx,
437                                                      struct event_context *event_ctx)
438 {
439         struct composite_context *c;
440         struct connect_state *state;
441         struct nbt_name name;
442
443         c = talloc_zero(mem_ctx, struct composite_context);
444         if (c == NULL) goto failed;
445
446         state = talloc_zero(c, struct connect_state);
447         if (state == NULL) goto failed;
448
449         if (event_ctx == NULL) {
450                 event_ctx = event_context_init(mem_ctx);
451         }
452
453         state->io = io;
454
455         c->state = COMPOSITE_STATE_IN_PROGRESS;
456         c->event_ctx = talloc_reference(c, event_ctx);
457         c->private_data = state;
458
459         state->stage = CONNECT_RESOLVE;
460         make_nbt_name_server(&name, io->in.dest_host);
461         state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
462
463         if (state->creq == NULL) goto failed;
464         state->creq->async.private_data = c;
465         state->creq->async.fn = composite_handler;
466
467         return c;
468 failed:
469         talloc_free(c);
470         return NULL;
471 }
472
473 /*
474   recv half of async composite connect code
475 */
476 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
477 {
478         NTSTATUS status;
479
480         status = composite_wait(c);
481
482         if (NT_STATUS_IS_OK(status)) {
483                 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
484                 talloc_steal(mem_ctx, state->io->out.tree);
485         }
486
487         talloc_free(c);
488         return status;
489 }
490
491 /*
492   sync version of smb_composite_connect 
493 */
494 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
495                                struct event_context *ev)
496 {
497         struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
498         return smb_composite_connect_recv(c, mem_ctx);
499 }