2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
21 a composite API for making a full SMB connection
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"
32 /* the stages of this call */
33 enum connect_stage {CONNECT_RESOLVE,
35 CONNECT_SESSION_REQUEST,
37 CONNECT_SESSION_SETUP,
38 CONNECT_SESSION_SETUP_ANON,
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;
54 static void request_handler(struct smbcli_request *);
55 static void composite_handler(struct composite_context *);
60 static NTSTATUS connect_send_negprot(struct composite_context *c,
61 struct smb_composite_connect *io)
63 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
65 state->req = smb_raw_negotiate_send(state->transport, lp_maxprotocol());
66 NT_STATUS_HAVE_NO_MEMORY(state->req);
68 state->req->async.fn = request_handler;
69 state->req->async.private = c;
70 state->stage = CONNECT_NEGPROT;
77 a tree connect request has completed
79 static NTSTATUS connect_tcon(struct composite_context *c,
80 struct smb_composite_connect *io)
82 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
85 status = smb_raw_tcon_recv(state->req, c, state->io_tcon);
86 NT_STATUS_NOT_OK_RETURN(status);
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);
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);
99 c->state = COMPOSITE_STATE_DONE;
106 a session setup request with anonymous fallback has completed
108 static NTSTATUS connect_session_setup_anon(struct composite_context *c,
109 struct smb_composite_connect *io)
111 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
114 status = smb_composite_sesssetup_recv(state->creq);
115 NT_STATUS_NOT_OK_RETURN(status);
117 io->out.anonymous_fallback_done = True;
119 state->session->vuid = state->io_setup->out.vuid;
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);
125 state->io_tcon = talloc(c, union smb_tcon);
126 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
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);
133 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
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 = "?????";
141 state->io_tcon->tconx.in.device = io->in.service_type;
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;
150 state->req->async.fn = request_handler;
151 state->req->async.private = c;
152 state->stage = CONNECT_TCON;
158 a session setup request has completed
160 static NTSTATUS connect_session_setup(struct composite_context *c,
161 struct smb_composite_connect *io)
163 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
166 status = smb_composite_sesssetup_recv(state->creq);
168 if (!NT_STATUS_IS_OK(status) &&
169 !cli_credentials_is_anonymous(state->io->in.credentials) &&
170 io->in.fallback_to_anonymous) {
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);
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;
182 state->creq = smb_composite_sesssetup_send(state->session,
184 NT_STATUS_HAVE_NO_MEMORY(state->creq);
185 if (state->creq->state == COMPOSITE_STATE_ERROR) {
186 return state->creq->status;
188 state->creq->async.fn = composite_handler;
189 state->creq->async.private_data = c;
190 state->stage = CONNECT_SESSION_SETUP_ANON;
195 NT_STATUS_NOT_OK_RETURN(status);
197 state->session->vuid = state->io_setup->out.vuid;
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);
203 state->io_tcon = talloc(c, union smb_tcon);
204 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
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);
211 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
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 = "?????";
219 state->io_tcon->tconx.in.device = io->in.service_type;
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;
228 state->req->async.fn = request_handler;
229 state->req->async.private = c;
230 state->stage = CONNECT_TCON;
236 a negprot request has completed
238 static NTSTATUS connect_negprot(struct composite_context *c,
239 struct smb_composite_connect *io)
241 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
244 status = smb_raw_negotiate_recv(state->req);
245 NT_STATUS_NOT_OK_RETURN(status);
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);
251 state->io_setup = talloc(c, struct smb_composite_sesssetup);
252 NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
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;
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;
266 state->creq->async.fn = composite_handler;
267 state->creq->async.private_data = c;
268 state->stage = CONNECT_SESSION_SETUP;
275 a session request operation has completed
277 static NTSTATUS connect_session_request(struct composite_context *c,
278 struct smb_composite_connect *io)
280 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
283 status = smbcli_transport_connect_recv(state->req);
284 NT_STATUS_NOT_OK_RETURN(status);
286 /* next step is a negprot */
287 return connect_send_negprot(c, io);
291 a socket connection operation has completed
293 static NTSTATUS connect_socket(struct composite_context *c,
294 struct smb_composite_connect *io)
296 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
298 struct nbt_name calling, called;
300 status = smbcli_sock_connect_recv(state->creq, state, &state->sock);
301 NT_STATUS_NOT_OK_RETURN(status);
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);
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
312 state->sock->hostname =
313 talloc_strdup(state->sock, io->in.called_name);
314 NT_STATUS_HAVE_NO_MEMORY(state->sock->hostname);
317 make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
319 nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
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);
331 state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
332 NT_STATUS_HAVE_NO_MEMORY(state->req);
334 state->req->async.fn = request_handler;
335 state->req->async.private = c;
336 state->stage = CONNECT_SESSION_REQUEST;
343 called when name resolution is finished
345 static NTSTATUS connect_resolve(struct composite_context *c,
346 struct smb_composite_connect *io)
348 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
352 status = resolve_name_recv(state->creq, state, &address);
353 NT_STATUS_NOT_OK_RETURN(status);
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);
359 state->stage = CONNECT_SOCKET;
360 state->creq->async.private_data = c;
361 state->creq->async.fn = composite_handler;
368 handle and dispatch state transitions
370 static void state_handler(struct composite_context *c)
372 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
374 switch (state->stage) {
375 case CONNECT_RESOLVE:
376 c->status = connect_resolve(c, state->io);
379 c->status = connect_socket(c, state->io);
381 case CONNECT_SESSION_REQUEST:
382 c->status = connect_session_request(c, state->io);
384 case CONNECT_NEGPROT:
385 c->status = connect_negprot(c, state->io);
387 case CONNECT_SESSION_SETUP:
388 c->status = connect_session_setup(c, state->io);
390 case CONNECT_SESSION_SETUP_ANON:
391 c->status = connect_session_setup_anon(c, state->io);
394 c->status = connect_tcon(c, state->io);
398 if (!NT_STATUS_IS_OK(c->status)) {
399 c->state = COMPOSITE_STATE_ERROR;
402 if (c->state >= COMPOSITE_STATE_DONE &&
410 handler for completion of a smbcli_request sub-request
412 static void request_handler(struct smbcli_request *req)
414 struct composite_context *c = talloc_get_type(req->async.private,
415 struct composite_context);
420 handler for completion of a smbcli_composite sub-request
422 static void composite_handler(struct composite_context *creq)
424 struct composite_context *c = talloc_get_type(creq->async.private_data,
425 struct composite_context);
430 a function to establish a smbcli_tree from scratch
432 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
434 struct event_context *event_ctx)
436 struct composite_context *c;
437 struct connect_state *state;
438 struct nbt_name name;
440 c = talloc_zero(mem_ctx, struct composite_context);
441 if (c == NULL) goto failed;
443 state = talloc(c, struct connect_state);
444 if (state == NULL) goto failed;
446 if (event_ctx == NULL) {
447 event_ctx = event_context_init(mem_ctx);
452 c->state = COMPOSITE_STATE_IN_PROGRESS;
453 c->event_ctx = talloc_reference(c, event_ctx);
454 c->private_data = state;
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());
460 if (state->creq == NULL) goto failed;
461 state->creq->async.private_data = c;
462 state->creq->async.fn = composite_handler;
471 recv half of async composite connect code
473 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
477 status = composite_wait(c);
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);
489 sync version of smb_composite_connect
491 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
492 struct event_context *ev)
494 struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
495 return smb_composite_connect_recv(c, mem_ctx);