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"
30 /* the stages of this call */
31 enum connect_stage {CONNECT_RESOLVE,
33 CONNECT_SESSION_REQUEST,
35 CONNECT_SESSION_SETUP,
36 CONNECT_SESSION_SETUP_ANON,
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;
52 static void request_handler(struct smbcli_request *);
53 static void composite_handler(struct composite_context *);
58 static NTSTATUS connect_send_negprot(struct composite_context *c,
59 struct smb_composite_connect *io)
61 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
63 state->req = smb_raw_negotiate_send(state->transport, lp_maxprotocol());
64 NT_STATUS_HAVE_NO_MEMORY(state->req);
66 state->req->async.fn = request_handler;
67 state->req->async.private = c;
68 state->stage = CONNECT_NEGPROT;
75 a tree connect request has completed
77 static NTSTATUS connect_tcon(struct composite_context *c,
78 struct smb_composite_connect *io)
80 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
83 status = smb_raw_tcon_recv(state->req, c, state->io_tcon);
84 NT_STATUS_NOT_OK_RETURN(status);
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);
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);
97 c->state = COMPOSITE_STATE_DONE;
104 a session setup request with anonymous fallback has completed
106 static NTSTATUS connect_session_setup_anon(struct composite_context *c,
107 struct smb_composite_connect *io)
109 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
112 status = smb_composite_sesssetup_recv(state->creq);
113 NT_STATUS_NOT_OK_RETURN(status);
115 io->out.anonymous_fallback_done = True;
117 state->session->vuid = state->io_setup->out.vuid;
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);
123 state->io_tcon = talloc(c, union smb_tcon);
124 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
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);
131 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
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 = "?????";
139 state->io_tcon->tconx.in.device = io->in.service_type;
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;
148 state->req->async.fn = request_handler;
149 state->req->async.private = c;
150 state->stage = CONNECT_TCON;
156 a session setup request has completed
158 static NTSTATUS connect_session_setup(struct composite_context *c,
159 struct smb_composite_connect *io)
161 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
164 status = smb_composite_sesssetup_recv(state->creq);
166 if (!NT_STATUS_IS_OK(status) &&
167 !cli_credentials_is_anonymous(state->io->in.credentials) &&
168 io->in.fallback_to_anonymous) {
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);
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;
180 state->creq = smb_composite_sesssetup_send(state->session,
182 NT_STATUS_HAVE_NO_MEMORY(state->creq);
183 if (state->creq->state == COMPOSITE_STATE_ERROR) {
184 return state->creq->status;
186 state->creq->async.fn = composite_handler;
187 state->creq->async.private_data = c;
188 state->stage = CONNECT_SESSION_SETUP_ANON;
193 NT_STATUS_NOT_OK_RETURN(status);
195 state->session->vuid = state->io_setup->out.vuid;
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);
201 state->io_tcon = talloc(c, union smb_tcon);
202 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
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);
209 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
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 = "?????";
217 state->io_tcon->tconx.in.device = io->in.service_type;
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;
226 state->req->async.fn = request_handler;
227 state->req->async.private = c;
228 state->stage = CONNECT_TCON;
234 a negprot request has completed
236 static NTSTATUS connect_negprot(struct composite_context *c,
237 struct smb_composite_connect *io)
239 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
242 status = smb_raw_negotiate_recv(state->req);
243 NT_STATUS_NOT_OK_RETURN(status);
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);
249 state->io_setup = talloc(c, struct smb_composite_sesssetup);
250 NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
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;
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;
264 state->creq->async.fn = composite_handler;
265 state->creq->async.private_data = c;
266 state->stage = CONNECT_SESSION_SETUP;
273 a session request operation has completed
275 static NTSTATUS connect_session_request(struct composite_context *c,
276 struct smb_composite_connect *io)
278 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
281 status = smbcli_transport_connect_recv(state->req);
282 NT_STATUS_NOT_OK_RETURN(status);
284 /* next step is a negprot */
285 return connect_send_negprot(c, io);
289 a socket connection operation has completed
291 static NTSTATUS connect_socket(struct composite_context *c,
292 struct smb_composite_connect *io)
294 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
296 struct nbt_name calling, called;
298 status = smbcli_sock_connect_recv(state->creq, state, &state->sock);
299 NT_STATUS_NOT_OK_RETURN(status);
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);
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
310 state->sock->hostname =
311 talloc_strdup(state->sock, io->in.called_name);
312 NT_STATUS_HAVE_NO_MEMORY(state->sock->hostname);
315 make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
317 nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
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);
329 state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
330 NT_STATUS_HAVE_NO_MEMORY(state->req);
332 state->req->async.fn = request_handler;
333 state->req->async.private = c;
334 state->stage = CONNECT_SESSION_REQUEST;
341 called when name resolution is finished
343 static NTSTATUS connect_resolve(struct composite_context *c,
344 struct smb_composite_connect *io)
346 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
350 status = resolve_name_recv(state->creq, state, &address);
351 NT_STATUS_NOT_OK_RETURN(status);
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);
357 state->stage = CONNECT_SOCKET;
358 state->creq->async.private_data = c;
359 state->creq->async.fn = composite_handler;
366 handle and dispatch state transitions
368 static void state_handler(struct composite_context *c)
370 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
372 switch (state->stage) {
373 case CONNECT_RESOLVE:
374 c->status = connect_resolve(c, state->io);
377 c->status = connect_socket(c, state->io);
379 case CONNECT_SESSION_REQUEST:
380 c->status = connect_session_request(c, state->io);
382 case CONNECT_NEGPROT:
383 c->status = connect_negprot(c, state->io);
385 case CONNECT_SESSION_SETUP:
386 c->status = connect_session_setup(c, state->io);
388 case CONNECT_SESSION_SETUP_ANON:
389 c->status = connect_session_setup_anon(c, state->io);
392 c->status = connect_tcon(c, state->io);
396 if (!NT_STATUS_IS_OK(c->status)) {
397 c->state = COMPOSITE_STATE_ERROR;
400 if (c->state >= COMPOSITE_STATE_DONE &&
408 handler for completion of a smbcli_request sub-request
410 static void request_handler(struct smbcli_request *req)
412 struct composite_context *c = talloc_get_type(req->async.private,
413 struct composite_context);
418 handler for completion of a smbcli_composite sub-request
420 static void composite_handler(struct composite_context *creq)
422 struct composite_context *c = talloc_get_type(creq->async.private_data,
423 struct composite_context);
428 a function to establish a smbcli_tree from scratch
430 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
432 struct event_context *event_ctx)
434 struct composite_context *c;
435 struct connect_state *state;
436 struct nbt_name name;
438 c = talloc_zero(mem_ctx, struct composite_context);
439 if (c == NULL) goto failed;
441 state = talloc(c, struct connect_state);
442 if (state == NULL) goto failed;
444 if (event_ctx == NULL) {
445 event_ctx = event_context_init(mem_ctx);
450 c->state = COMPOSITE_STATE_IN_PROGRESS;
451 c->event_ctx = talloc_reference(c, event_ctx);
452 c->private_data = state;
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());
458 if (state->creq == NULL) goto failed;
459 state->creq->async.private_data = c;
460 state->creq->async.fn = composite_handler;
469 recv half of async composite connect code
471 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
475 status = composite_wait(c);
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);
487 sync version of smb_composite_connect
489 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
490 struct event_context *ev)
492 struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
493 return smb_composite_connect_recv(c, mem_ctx);