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_connectmulti *conn;
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 competed
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 competed
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 competed
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 competed
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 = smb_composite_connectmulti_recv(state->creq, state);
300 NT_STATUS_NOT_OK_RETURN(status);
302 state->sock = state->conn->out.socket;
304 /* the socket is up - we can initialise the smbcli transport layer */
305 state->transport = smbcli_transport_init(state->sock, state, True);
306 NT_STATUS_HAVE_NO_MEMORY(state->transport);
308 make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
310 nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
312 /* we have a connected socket - next step is a session
313 request, if needed. Port 445 doesn't need it, so it goes
314 straight to the negprot */
315 if (state->sock->port == 445) {
316 status = nbt_name_dup(state->transport, &called,
317 &state->transport->called);
318 NT_STATUS_NOT_OK_RETURN(status);
319 return connect_send_negprot(c, io);
322 state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
323 NT_STATUS_HAVE_NO_MEMORY(state->req);
325 state->req->async.fn = request_handler;
326 state->req->async.private = c;
327 state->stage = CONNECT_SESSION_REQUEST;
334 called when name resolution is finished
336 static NTSTATUS connect_resolve(struct composite_context *c,
337 struct smb_composite_connect *io)
339 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
342 struct smb_composite_connectmulti *conn;
344 status = resolve_name_recv(state->creq, state, &address);
345 NT_STATUS_NOT_OK_RETURN(status);
347 conn = talloc(state, struct smb_composite_connectmulti);
348 NT_STATUS_HAVE_NO_MEMORY(conn);
350 conn->in.num_dests = 1;
352 conn->in.addresses = talloc_array(state->conn, const char *, 1);
353 NT_STATUS_HAVE_NO_MEMORY(conn->in.addresses);
354 conn->in.addresses[0] = address;
356 conn->in.hostnames = talloc_array(state->conn, const char *, 1);
357 NT_STATUS_HAVE_NO_MEMORY(conn->in.hostnames);
358 conn->in.hostnames[0] = state->io->in.dest_host;
360 conn->in.ports = NULL;
361 if (state->io->in.port != 0) {
362 conn->in.ports = talloc_array(state->conn, int, 1);
363 NT_STATUS_HAVE_NO_MEMORY(conn->in.ports);
364 conn->in.ports[0] = state->io->in.port;
367 state->creq = smb_composite_connectmulti_send(conn, state,
369 NT_STATUS_HAVE_NO_MEMORY(state->creq);
371 state->stage = CONNECT_SOCKET;
372 state->creq->async.private_data = c;
373 state->creq->async.fn = composite_handler;
380 handle and dispatch state transitions
382 static void state_handler(struct composite_context *c)
384 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
386 switch (state->stage) {
387 case CONNECT_RESOLVE:
388 c->status = connect_resolve(c, state->io);
391 c->status = connect_socket(c, state->io);
393 case CONNECT_SESSION_REQUEST:
394 c->status = connect_session_request(c, state->io);
396 case CONNECT_NEGPROT:
397 c->status = connect_negprot(c, state->io);
399 case CONNECT_SESSION_SETUP:
400 c->status = connect_session_setup(c, state->io);
402 case CONNECT_SESSION_SETUP_ANON:
403 c->status = connect_session_setup_anon(c, state->io);
406 c->status = connect_tcon(c, state->io);
410 if (!NT_STATUS_IS_OK(c->status)) {
411 c->state = COMPOSITE_STATE_ERROR;
414 if (c->state >= COMPOSITE_STATE_DONE &&
422 handler for completion of a smbcli_request sub-request
424 static void request_handler(struct smbcli_request *req)
426 struct composite_context *c = talloc_get_type(req->async.private,
427 struct composite_context);
432 handler for completion of a smbcli_composite sub-request
434 static void composite_handler(struct composite_context *creq)
436 struct composite_context *c = talloc_get_type(creq->async.private_data,
437 struct composite_context);
442 a function to establish a smbcli_tree from scratch
444 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
446 struct event_context *event_ctx)
448 struct composite_context *c;
449 struct connect_state *state;
450 struct nbt_name name;
452 c = talloc_zero(mem_ctx, struct composite_context);
453 if (c == NULL) goto failed;
455 state = talloc(c, struct connect_state);
456 if (state == NULL) goto failed;
458 if (event_ctx == NULL) {
459 event_ctx = event_context_init(mem_ctx);
464 c->state = COMPOSITE_STATE_IN_PROGRESS;
465 c->event_ctx = talloc_reference(c, event_ctx);
466 c->private_data = state;
468 state->stage = CONNECT_RESOLVE;
469 make_nbt_name_server(&name, io->in.dest_host);
470 state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
472 if (state->creq == NULL) goto failed;
473 state->creq->async.private_data = c;
474 state->creq->async.fn = composite_handler;
483 recv half of async composite connect code
485 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
489 status = composite_wait(c);
491 if (NT_STATUS_IS_OK(status)) {
492 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
493 talloc_steal(mem_ctx, state->io->out.tree);
501 sync version of smb_composite_connect
503 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
504 struct event_context *ev)
506 struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
507 return smb_composite_connect_recv(c, mem_ctx);