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/nbt/libnbt.h"
31 /* the stages of this call */
32 enum connect_stage {CONNECT_RESOLVE,
34 CONNECT_SESSION_REQUEST,
36 CONNECT_SESSION_SETUP,
37 CONNECT_SESSION_SETUP_ANON,
40 struct connect_state {
41 enum connect_stage stage;
42 struct smbcli_socket *sock;
43 struct smbcli_transport *transport;
44 struct smbcli_session *session;
45 struct smb_composite_connect *io;
46 union smb_tcon *io_tcon;
47 struct smb_composite_sesssetup *io_setup;
48 struct smbcli_request *req;
49 struct composite_context *creq;
53 static void request_handler(struct smbcli_request *);
54 static void composite_handler(struct composite_context *);
59 static NTSTATUS connect_send_negprot(struct composite_context *c,
60 struct smb_composite_connect *io)
62 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
64 state->req = smb_raw_negotiate_send(state->transport, lp_maxprotocol());
65 NT_STATUS_HAVE_NO_MEMORY(state->req);
67 state->req->async.fn = request_handler;
68 state->req->async.private = c;
69 state->stage = CONNECT_NEGPROT;
76 a tree connect request has completed
78 static NTSTATUS connect_tcon(struct composite_context *c,
79 struct smb_composite_connect *io)
81 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
84 status = smb_raw_tcon_recv(state->req, c, state->io_tcon);
85 NT_STATUS_NOT_OK_RETURN(status);
87 io->out.tree->tid = state->io_tcon->tconx.out.tid;
88 if (state->io_tcon->tconx.out.dev_type) {
89 io->out.tree->device = talloc_strdup(io->out.tree,
90 state->io_tcon->tconx.out.dev_type);
92 if (state->io_tcon->tconx.out.fs_type) {
93 io->out.tree->fs_type = talloc_strdup(io->out.tree,
94 state->io_tcon->tconx.out.fs_type);
98 c->state = COMPOSITE_STATE_DONE;
105 a session setup request with anonymous fallback has completed
107 static NTSTATUS connect_session_setup_anon(struct composite_context *c,
108 struct smb_composite_connect *io)
110 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
113 status = smb_composite_sesssetup_recv(state->creq);
114 NT_STATUS_NOT_OK_RETURN(status);
116 io->out.anonymous_fallback_done = True;
118 state->session->vuid = state->io_setup->out.vuid;
120 /* setup for a tconx */
121 io->out.tree = smbcli_tree_init(state->session, state, True);
122 NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
124 state->io_tcon = talloc(c, union smb_tcon);
125 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
127 /* connect to a share using a tree connect */
128 state->io_tcon->generic.level = RAW_TCON_TCONX;
129 state->io_tcon->tconx.in.flags = 0;
130 state->io_tcon->tconx.in.password = data_blob(NULL, 0);
132 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
136 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
137 if (!io->in.service_type) {
138 state->io_tcon->tconx.in.device = "?????";
140 state->io_tcon->tconx.in.device = io->in.service_type;
143 state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
144 NT_STATUS_HAVE_NO_MEMORY(state->req);
145 if (state->req->state == SMBCLI_REQUEST_ERROR) {
146 return state->req->status;
149 state->req->async.fn = request_handler;
150 state->req->async.private = c;
151 state->stage = CONNECT_TCON;
157 a session setup request has completed
159 static NTSTATUS connect_session_setup(struct composite_context *c,
160 struct smb_composite_connect *io)
162 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
165 status = smb_composite_sesssetup_recv(state->creq);
167 if (!NT_STATUS_IS_OK(status) &&
168 !cli_credentials_is_anonymous(state->io->in.credentials) &&
169 io->in.fallback_to_anonymous) {
171 state->io_setup->in.credentials = cli_credentials_init(state);
172 NT_STATUS_HAVE_NO_MEMORY(state->io_setup->in.credentials);
173 cli_credentials_set_conf(state->io_setup->in.credentials);
174 cli_credentials_set_anonymous(state->io_setup->in.credentials);
176 /* If the preceding attempt was with extended security, we
177 * have been given a uid in the NTLMSSP_CHALLENGE reply. This
178 * would lead to an invalid uid in the anonymous fallback */
179 state->session->vuid = 0;
181 state->creq = smb_composite_sesssetup_send(state->session,
183 NT_STATUS_HAVE_NO_MEMORY(state->creq);
184 if (state->creq->state == COMPOSITE_STATE_ERROR) {
185 return state->creq->status;
187 state->creq->async.fn = composite_handler;
188 state->creq->async.private_data = c;
189 state->stage = CONNECT_SESSION_SETUP_ANON;
194 NT_STATUS_NOT_OK_RETURN(status);
196 state->session->vuid = state->io_setup->out.vuid;
198 /* setup for a tconx */
199 io->out.tree = smbcli_tree_init(state->session, state, True);
200 NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
202 state->io_tcon = talloc(c, union smb_tcon);
203 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
205 /* connect to a share using a tree connect */
206 state->io_tcon->generic.level = RAW_TCON_TCONX;
207 state->io_tcon->tconx.in.flags = 0;
208 state->io_tcon->tconx.in.password = data_blob(NULL, 0);
210 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
214 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
215 if (!io->in.service_type) {
216 state->io_tcon->tconx.in.device = "?????";
218 state->io_tcon->tconx.in.device = io->in.service_type;
221 state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
222 NT_STATUS_HAVE_NO_MEMORY(state->req);
223 if (state->req->state == SMBCLI_REQUEST_ERROR) {
224 return state->req->status;
227 state->req->async.fn = request_handler;
228 state->req->async.private = c;
229 state->stage = CONNECT_TCON;
235 a negprot request has completed
237 static NTSTATUS connect_negprot(struct composite_context *c,
238 struct smb_composite_connect *io)
240 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
243 status = smb_raw_negotiate_recv(state->req);
244 NT_STATUS_NOT_OK_RETURN(status);
246 /* next step is a session setup */
247 state->session = smbcli_session_init(state->transport, state, True);
248 NT_STATUS_HAVE_NO_MEMORY(state->session);
250 state->io_setup = talloc(c, struct smb_composite_sesssetup);
251 NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
253 /* prepare a session setup to establish a security context */
254 state->io_setup->in.sesskey = state->transport->negotiate.sesskey;
255 state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
256 state->io_setup->in.credentials = io->in.credentials;
257 state->io_setup->in.workgroup = io->in.workgroup;
259 state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
260 NT_STATUS_HAVE_NO_MEMORY(state->creq);
261 if (state->creq->state == COMPOSITE_STATE_ERROR) {
262 return state->creq->status;
265 state->creq->async.fn = composite_handler;
266 state->creq->async.private_data = c;
267 state->stage = CONNECT_SESSION_SETUP;
274 a session request operation has completed
276 static NTSTATUS connect_session_request(struct composite_context *c,
277 struct smb_composite_connect *io)
279 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
282 status = smbcli_transport_connect_recv(state->req);
283 NT_STATUS_NOT_OK_RETURN(status);
285 /* next step is a negprot */
286 return connect_send_negprot(c, io);
290 a socket connection operation has completed
292 static NTSTATUS connect_socket(struct composite_context *c,
293 struct smb_composite_connect *io)
295 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
297 struct nbt_name calling, called;
299 status = smbcli_sock_connect_recv(state->creq, state, &state->sock);
300 NT_STATUS_NOT_OK_RETURN(status);
302 /* the socket is up - we can initialise the smbcli transport layer */
303 state->transport = smbcli_transport_init(state->sock, state, True);
304 NT_STATUS_HAVE_NO_MEMORY(state->transport);
306 if (is_ipaddress(state->sock->hostname) &&
307 (state->io->in.called_name != NULL)) {
308 /* If connecting to an IP address, we might want the real name
309 * of the host for later kerberos. The called name is a better
311 state->sock->hostname =
312 talloc_strdup(state->sock, io->in.called_name);
313 NT_STATUS_HAVE_NO_MEMORY(state->sock->hostname);
316 make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
318 nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
320 /* we have a connected socket - next step is a session
321 request, if needed. Port 445 doesn't need it, so it goes
322 straight to the negprot */
323 if (state->sock->port == 445) {
324 status = nbt_name_dup(state->transport, &called,
325 &state->transport->called);
326 NT_STATUS_NOT_OK_RETURN(status);
327 return connect_send_negprot(c, io);
330 state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
331 NT_STATUS_HAVE_NO_MEMORY(state->req);
333 state->req->async.fn = request_handler;
334 state->req->async.private = c;
335 state->stage = CONNECT_SESSION_REQUEST;
342 called when name resolution is finished
344 static NTSTATUS connect_resolve(struct composite_context *c,
345 struct smb_composite_connect *io)
347 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
351 status = resolve_name_recv(state->creq, state, &address);
352 NT_STATUS_NOT_OK_RETURN(status);
354 state->creq = smbcli_sock_connect_send(state, address, io->in.port,
355 io->in.dest_host, c->event_ctx);
356 NT_STATUS_HAVE_NO_MEMORY(state->creq);
358 state->stage = CONNECT_SOCKET;
359 state->creq->async.private_data = c;
360 state->creq->async.fn = composite_handler;
367 handle and dispatch state transitions
369 static void state_handler(struct composite_context *c)
371 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
373 switch (state->stage) {
374 case CONNECT_RESOLVE:
375 c->status = connect_resolve(c, state->io);
378 c->status = connect_socket(c, state->io);
380 case CONNECT_SESSION_REQUEST:
381 c->status = connect_session_request(c, state->io);
383 case CONNECT_NEGPROT:
384 c->status = connect_negprot(c, state->io);
386 case CONNECT_SESSION_SETUP:
387 c->status = connect_session_setup(c, state->io);
389 case CONNECT_SESSION_SETUP_ANON:
390 c->status = connect_session_setup_anon(c, state->io);
393 c->status = connect_tcon(c, state->io);
397 if (!NT_STATUS_IS_OK(c->status)) {
398 c->state = COMPOSITE_STATE_ERROR;
401 if (c->state >= COMPOSITE_STATE_DONE &&
409 handler for completion of a smbcli_request sub-request
411 static void request_handler(struct smbcli_request *req)
413 struct composite_context *c = talloc_get_type(req->async.private,
414 struct composite_context);
419 handler for completion of a smbcli_composite sub-request
421 static void composite_handler(struct composite_context *creq)
423 struct composite_context *c = talloc_get_type(creq->async.private_data,
424 struct composite_context);
429 a function to establish a smbcli_tree from scratch
431 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
433 struct event_context *event_ctx)
435 struct composite_context *c;
436 struct connect_state *state;
437 struct nbt_name name;
439 c = talloc_zero(mem_ctx, struct composite_context);
440 if (c == NULL) goto failed;
442 state = talloc(c, struct connect_state);
443 if (state == NULL) goto failed;
445 if (event_ctx == NULL) {
446 event_ctx = event_context_init(mem_ctx);
451 c->state = COMPOSITE_STATE_IN_PROGRESS;
452 c->event_ctx = talloc_reference(c, event_ctx);
453 c->private_data = state;
455 state->stage = CONNECT_RESOLVE;
456 make_nbt_name_server(&name, io->in.dest_host);
457 state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
459 if (state->creq == NULL) goto failed;
460 state->creq->async.private_data = c;
461 state->creq->async.fn = composite_handler;
470 recv half of async composite connect code
472 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
476 status = composite_wait(c);
478 if (NT_STATUS_IS_OK(status)) {
479 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
480 talloc_steal(mem_ctx, state->io->out.tree);
488 sync version of smb_composite_connect
490 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
491 struct event_context *ev)
493 struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
494 return smb_composite_connect_recv(c, mem_ctx);