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"
31 #include "librpc/gen_ndr/ndr_nbt.h"
33 /* the stages of this call */
34 enum connect_stage {CONNECT_RESOLVE,
36 CONNECT_SESSION_REQUEST,
38 CONNECT_SESSION_SETUP,
39 CONNECT_SESSION_SETUP_ANON,
42 struct connect_state {
43 enum connect_stage stage;
44 struct smbcli_socket *sock;
45 struct smbcli_transport *transport;
46 struct smbcli_session *session;
47 struct smb_composite_connect *io;
48 union smb_tcon *io_tcon;
49 struct smb_composite_sesssetup *io_setup;
50 struct smbcli_request *req;
51 struct composite_context *creq;
55 static void request_handler(struct smbcli_request *);
56 static void composite_handler(struct composite_context *);
61 static NTSTATUS connect_send_negprot(struct composite_context *c,
62 struct smb_composite_connect *io)
64 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
66 state->req = smb_raw_negotiate_send(state->transport, lp_cli_maxprotocol());
67 NT_STATUS_HAVE_NO_MEMORY(state->req);
69 state->req->async.fn = request_handler;
70 state->req->async.private = c;
71 state->stage = CONNECT_NEGPROT;
78 a tree connect request has completed
80 static NTSTATUS connect_tcon(struct composite_context *c,
81 struct smb_composite_connect *io)
83 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
86 status = smb_raw_tcon_recv(state->req, c, state->io_tcon);
87 NT_STATUS_NOT_OK_RETURN(status);
89 io->out.tree->tid = state->io_tcon->tconx.out.tid;
90 if (state->io_tcon->tconx.out.dev_type) {
91 io->out.tree->device = talloc_strdup(io->out.tree,
92 state->io_tcon->tconx.out.dev_type);
94 if (state->io_tcon->tconx.out.fs_type) {
95 io->out.tree->fs_type = talloc_strdup(io->out.tree,
96 state->io_tcon->tconx.out.fs_type);
100 c->state = COMPOSITE_STATE_DONE;
107 a session setup request with anonymous fallback has completed
109 static NTSTATUS connect_session_setup_anon(struct composite_context *c,
110 struct smb_composite_connect *io)
112 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
115 status = smb_composite_sesssetup_recv(state->creq);
116 NT_STATUS_NOT_OK_RETURN(status);
118 io->out.anonymous_fallback_done = True;
120 state->session->vuid = state->io_setup->out.vuid;
122 /* setup for a tconx */
123 io->out.tree = smbcli_tree_init(state->session, state, True);
124 NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
126 state->io_tcon = talloc(c, union smb_tcon);
127 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
129 /* connect to a share using a tree connect */
130 state->io_tcon->generic.level = RAW_TCON_TCONX;
131 state->io_tcon->tconx.in.flags = 0;
132 state->io_tcon->tconx.in.password = data_blob(NULL, 0);
134 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
138 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
139 if (!io->in.service_type) {
140 state->io_tcon->tconx.in.device = "?????";
142 state->io_tcon->tconx.in.device = io->in.service_type;
145 state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
146 NT_STATUS_HAVE_NO_MEMORY(state->req);
147 if (state->req->state == SMBCLI_REQUEST_ERROR) {
148 return state->req->status;
151 state->req->async.fn = request_handler;
152 state->req->async.private = c;
153 state->stage = CONNECT_TCON;
159 a session setup request has completed
161 static NTSTATUS connect_session_setup(struct composite_context *c,
162 struct smb_composite_connect *io)
164 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
167 status = smb_composite_sesssetup_recv(state->creq);
169 if (!NT_STATUS_IS_OK(status) &&
170 !cli_credentials_is_anonymous(state->io->in.credentials) &&
171 io->in.fallback_to_anonymous) {
173 state->io_setup->in.credentials = cli_credentials_init(state);
174 NT_STATUS_HAVE_NO_MEMORY(state->io_setup->in.credentials);
175 cli_credentials_set_conf(state->io_setup->in.credentials);
176 cli_credentials_set_anonymous(state->io_setup->in.credentials);
178 /* If the preceding attempt was with extended security, we
179 * have been given a uid in the NTLMSSP_CHALLENGE reply. This
180 * would lead to an invalid uid in the anonymous fallback */
181 state->session->vuid = 0;
183 state->creq = smb_composite_sesssetup_send(state->session,
185 NT_STATUS_HAVE_NO_MEMORY(state->creq);
186 if (state->creq->state == COMPOSITE_STATE_ERROR) {
187 return state->creq->status;
189 state->creq->async.fn = composite_handler;
190 state->creq->async.private_data = c;
191 state->stage = CONNECT_SESSION_SETUP_ANON;
196 NT_STATUS_NOT_OK_RETURN(status);
198 state->session->vuid = state->io_setup->out.vuid;
200 /* setup for a tconx */
201 io->out.tree = smbcli_tree_init(state->session, state, True);
202 NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
204 state->io_tcon = talloc(c, union smb_tcon);
205 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
207 /* connect to a share using a tree connect */
208 state->io_tcon->generic.level = RAW_TCON_TCONX;
209 state->io_tcon->tconx.in.flags = 0;
210 state->io_tcon->tconx.in.password = data_blob(NULL, 0);
212 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
216 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
217 if (!io->in.service_type) {
218 state->io_tcon->tconx.in.device = "?????";
220 state->io_tcon->tconx.in.device = io->in.service_type;
223 state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
224 NT_STATUS_HAVE_NO_MEMORY(state->req);
225 if (state->req->state == SMBCLI_REQUEST_ERROR) {
226 return state->req->status;
229 state->req->async.fn = request_handler;
230 state->req->async.private = c;
231 state->stage = CONNECT_TCON;
237 a negprot request has completed
239 static NTSTATUS connect_negprot(struct composite_context *c,
240 struct smb_composite_connect *io)
242 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
245 status = smb_raw_negotiate_recv(state->req);
246 NT_STATUS_NOT_OK_RETURN(status);
248 /* next step is a session setup */
249 state->session = smbcli_session_init(state->transport, state, True);
250 NT_STATUS_HAVE_NO_MEMORY(state->session);
252 state->io_setup = talloc(c, struct smb_composite_sesssetup);
253 NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
255 /* prepare a session setup to establish a security context */
256 state->io_setup->in.sesskey = state->transport->negotiate.sesskey;
257 state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
258 state->io_setup->in.credentials = io->in.credentials;
259 state->io_setup->in.workgroup = io->in.workgroup;
261 state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
262 NT_STATUS_HAVE_NO_MEMORY(state->creq);
263 if (state->creq->state == COMPOSITE_STATE_ERROR) {
264 return state->creq->status;
267 state->creq->async.fn = composite_handler;
268 state->creq->async.private_data = c;
269 state->stage = CONNECT_SESSION_SETUP;
276 a session request operation has completed
278 static NTSTATUS connect_session_request(struct composite_context *c,
279 struct smb_composite_connect *io)
281 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
284 status = smbcli_transport_connect_recv(state->req);
285 NT_STATUS_NOT_OK_RETURN(status);
287 /* next step is a negprot */
288 return connect_send_negprot(c, io);
292 a socket connection operation has completed
294 static NTSTATUS connect_socket(struct composite_context *c,
295 struct smb_composite_connect *io)
297 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
299 struct nbt_name calling, called;
301 status = smbcli_sock_connect_recv(state->creq, state, &state->sock);
302 NT_STATUS_NOT_OK_RETURN(status);
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 if (is_ipaddress(state->sock->hostname) &&
309 (state->io->in.called_name != NULL)) {
310 /* If connecting to an IP address, we might want the real name
311 * of the host for later kerberos. The called name is a better
313 state->sock->hostname =
314 talloc_strdup(state->sock, io->in.called_name);
315 NT_STATUS_HAVE_NO_MEMORY(state->sock->hostname);
318 make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
320 nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
322 /* we have a connected socket - next step is a session
323 request, if needed. Port 445 doesn't need it, so it goes
324 straight to the negprot */
325 if (state->sock->port == 445) {
326 status = nbt_name_dup(state->transport, &called,
327 &state->transport->called);
328 NT_STATUS_NOT_OK_RETURN(status);
329 return connect_send_negprot(c, io);
332 state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
333 NT_STATUS_HAVE_NO_MEMORY(state->req);
335 state->req->async.fn = request_handler;
336 state->req->async.private = c;
337 state->stage = CONNECT_SESSION_REQUEST;
344 called when name resolution is finished
346 static NTSTATUS connect_resolve(struct composite_context *c,
347 struct smb_composite_connect *io)
349 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
353 status = resolve_name_recv(state->creq, state, &address);
354 NT_STATUS_NOT_OK_RETURN(status);
356 state->creq = smbcli_sock_connect_send(state, address, io->in.port,
357 io->in.dest_host, c->event_ctx);
358 NT_STATUS_HAVE_NO_MEMORY(state->creq);
360 state->stage = CONNECT_SOCKET;
361 state->creq->async.private_data = c;
362 state->creq->async.fn = composite_handler;
369 handle and dispatch state transitions
371 static void state_handler(struct composite_context *c)
373 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
375 switch (state->stage) {
376 case CONNECT_RESOLVE:
377 c->status = connect_resolve(c, state->io);
380 c->status = connect_socket(c, state->io);
382 case CONNECT_SESSION_REQUEST:
383 c->status = connect_session_request(c, state->io);
385 case CONNECT_NEGPROT:
386 c->status = connect_negprot(c, state->io);
388 case CONNECT_SESSION_SETUP:
389 c->status = connect_session_setup(c, state->io);
391 case CONNECT_SESSION_SETUP_ANON:
392 c->status = connect_session_setup_anon(c, state->io);
395 c->status = connect_tcon(c, state->io);
399 if (!NT_STATUS_IS_OK(c->status)) {
400 c->state = COMPOSITE_STATE_ERROR;
403 if (c->state >= COMPOSITE_STATE_DONE &&
411 handler for completion of a smbcli_request sub-request
413 static void request_handler(struct smbcli_request *req)
415 struct composite_context *c = talloc_get_type(req->async.private,
416 struct composite_context);
421 handler for completion of a smbcli_composite sub-request
423 static void composite_handler(struct composite_context *creq)
425 struct composite_context *c = talloc_get_type(creq->async.private_data,
426 struct composite_context);
431 a function to establish a smbcli_tree from scratch
433 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
435 struct event_context *event_ctx)
437 struct composite_context *c;
438 struct connect_state *state;
439 struct nbt_name name;
441 c = talloc_zero(mem_ctx, struct composite_context);
442 if (c == NULL) goto failed;
444 state = talloc(c, struct connect_state);
445 if (state == NULL) goto failed;
447 if (event_ctx == NULL) {
448 event_ctx = event_context_init(mem_ctx);
453 c->state = COMPOSITE_STATE_IN_PROGRESS;
454 c->event_ctx = talloc_reference(c, event_ctx);
455 c->private_data = state;
457 state->stage = CONNECT_RESOLVE;
458 make_nbt_name_server(&name, io->in.dest_host);
459 state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
461 if (state->creq == NULL) goto failed;
462 state->creq->async.private_data = c;
463 state->creq->async.fn = composite_handler;
472 recv half of async composite connect code
474 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
478 status = composite_wait(c);
480 if (NT_STATUS_IS_OK(status)) {
481 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
482 talloc_steal(mem_ctx, state->io->out.tree);
490 sync version of smb_composite_connect
492 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
493 struct event_context *ev)
495 struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
496 return smb_composite_connect_recv(c, mem_ctx);