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 3 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, see <http://www.gnu.org/licenses/>.
20 a composite API for making a full SMB connection
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
26 #include "libcli/smb_composite/smb_composite.h"
27 #include "lib/events/events.h"
28 #include "libcli/resolve/resolve.h"
29 #include "auth/credentials/credentials.h"
30 #include "librpc/gen_ndr/ndr_nbt.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_cli_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;
181 data_blob_free(&state->session->user_session_key);
182 talloc_free(state->session->gensec);
183 state->session->gensec = NULL;
185 state->creq = smb_composite_sesssetup_send(state->session,
187 NT_STATUS_HAVE_NO_MEMORY(state->creq);
188 if (state->creq->state == COMPOSITE_STATE_ERROR) {
189 return state->creq->status;
191 state->creq->async.fn = composite_handler;
192 state->creq->async.private_data = c;
193 state->stage = CONNECT_SESSION_SETUP_ANON;
198 NT_STATUS_NOT_OK_RETURN(status);
200 state->session->vuid = state->io_setup->out.vuid;
202 /* setup for a tconx */
203 io->out.tree = smbcli_tree_init(state->session, state, True);
204 NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
206 state->io_tcon = talloc(c, union smb_tcon);
207 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
209 /* connect to a share using a tree connect */
210 state->io_tcon->generic.level = RAW_TCON_TCONX;
211 state->io_tcon->tconx.in.flags = 0;
212 state->io_tcon->tconx.in.password = data_blob(NULL, 0);
214 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
218 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
219 if (!io->in.service_type) {
220 state->io_tcon->tconx.in.device = "?????";
222 state->io_tcon->tconx.in.device = io->in.service_type;
225 state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
226 NT_STATUS_HAVE_NO_MEMORY(state->req);
227 if (state->req->state == SMBCLI_REQUEST_ERROR) {
228 return state->req->status;
231 state->req->async.fn = request_handler;
232 state->req->async.private = c;
233 state->stage = CONNECT_TCON;
239 a negprot request has completed
241 static NTSTATUS connect_negprot(struct composite_context *c,
242 struct smb_composite_connect *io)
244 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
247 status = smb_raw_negotiate_recv(state->req);
248 NT_STATUS_NOT_OK_RETURN(status);
250 /* next step is a session setup */
251 state->session = smbcli_session_init(state->transport, state, True);
252 NT_STATUS_HAVE_NO_MEMORY(state->session);
254 state->io_setup = talloc(c, struct smb_composite_sesssetup);
255 NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
257 /* prepare a session setup to establish a security context */
258 state->io_setup->in.sesskey = state->transport->negotiate.sesskey;
259 state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
260 state->io_setup->in.credentials = io->in.credentials;
261 state->io_setup->in.workgroup = io->in.workgroup;
263 state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
264 NT_STATUS_HAVE_NO_MEMORY(state->creq);
265 if (state->creq->state == COMPOSITE_STATE_ERROR) {
266 return state->creq->status;
269 state->creq->async.fn = composite_handler;
270 state->creq->async.private_data = c;
271 state->stage = CONNECT_SESSION_SETUP;
278 a session request operation has completed
280 static NTSTATUS connect_session_request(struct composite_context *c,
281 struct smb_composite_connect *io)
283 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
286 status = smbcli_transport_connect_recv(state->req);
287 NT_STATUS_NOT_OK_RETURN(status);
289 /* next step is a negprot */
290 return connect_send_negprot(c, io);
294 a socket connection operation has completed
296 static NTSTATUS connect_socket(struct composite_context *c,
297 struct smb_composite_connect *io)
299 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
301 struct nbt_name calling, called;
303 status = smbcli_sock_connect_recv(state->creq, state, &state->sock);
304 NT_STATUS_NOT_OK_RETURN(status);
306 /* the socket is up - we can initialise the smbcli transport layer */
307 state->transport = smbcli_transport_init(state->sock, state, True);
308 NT_STATUS_HAVE_NO_MEMORY(state->transport);
310 if (is_ipaddress(state->sock->hostname) &&
311 (state->io->in.called_name != NULL)) {
312 /* If connecting to an IP address, we might want the real name
313 * of the host for later kerberos. The called name is a better
315 state->sock->hostname =
316 talloc_strdup(state->sock, io->in.called_name);
317 NT_STATUS_HAVE_NO_MEMORY(state->sock->hostname);
320 make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
322 nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
324 /* we have a connected socket - next step is a session
325 request, if needed. Port 445 doesn't need it, so it goes
326 straight to the negprot */
327 if (state->sock->port == 445) {
328 status = nbt_name_dup(state->transport, &called,
329 &state->transport->called);
330 NT_STATUS_NOT_OK_RETURN(status);
331 return connect_send_negprot(c, io);
334 state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
335 NT_STATUS_HAVE_NO_MEMORY(state->req);
337 state->req->async.fn = request_handler;
338 state->req->async.private = c;
339 state->stage = CONNECT_SESSION_REQUEST;
346 called when name resolution is finished
348 static NTSTATUS connect_resolve(struct composite_context *c,
349 struct smb_composite_connect *io)
351 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
355 status = resolve_name_recv(state->creq, state, &address);
356 NT_STATUS_NOT_OK_RETURN(status);
358 state->creq = smbcli_sock_connect_send(state, address, io->in.port,
359 io->in.dest_host, c->event_ctx);
360 NT_STATUS_HAVE_NO_MEMORY(state->creq);
362 state->stage = CONNECT_SOCKET;
363 state->creq->async.private_data = c;
364 state->creq->async.fn = composite_handler;
371 handle and dispatch state transitions
373 static void state_handler(struct composite_context *c)
375 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
377 switch (state->stage) {
378 case CONNECT_RESOLVE:
379 c->status = connect_resolve(c, state->io);
382 c->status = connect_socket(c, state->io);
384 case CONNECT_SESSION_REQUEST:
385 c->status = connect_session_request(c, state->io);
387 case CONNECT_NEGPROT:
388 c->status = connect_negprot(c, state->io);
390 case CONNECT_SESSION_SETUP:
391 c->status = connect_session_setup(c, state->io);
393 case CONNECT_SESSION_SETUP_ANON:
394 c->status = connect_session_setup_anon(c, state->io);
397 c->status = connect_tcon(c, state->io);
401 if (!NT_STATUS_IS_OK(c->status)) {
402 c->state = COMPOSITE_STATE_ERROR;
405 if (c->state >= COMPOSITE_STATE_DONE &&
413 handler for completion of a smbcli_request sub-request
415 static void request_handler(struct smbcli_request *req)
417 struct composite_context *c = talloc_get_type(req->async.private,
418 struct composite_context);
423 handler for completion of a smbcli_composite sub-request
425 static void composite_handler(struct composite_context *creq)
427 struct composite_context *c = talloc_get_type(creq->async.private_data,
428 struct composite_context);
433 a function to establish a smbcli_tree from scratch
435 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
437 struct event_context *event_ctx)
439 struct composite_context *c;
440 struct connect_state *state;
441 struct nbt_name name;
443 c = talloc_zero(mem_ctx, struct composite_context);
444 if (c == NULL) goto failed;
446 state = talloc_zero(c, struct connect_state);
447 if (state == NULL) goto failed;
449 if (event_ctx == NULL) {
450 event_ctx = event_context_init(mem_ctx);
455 c->state = COMPOSITE_STATE_IN_PROGRESS;
456 c->event_ctx = talloc_reference(c, event_ctx);
457 c->private_data = state;
459 state->stage = CONNECT_RESOLVE;
460 make_nbt_name_server(&name, io->in.dest_host);
461 state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
463 if (state->creq == NULL) goto failed;
464 state->creq->async.private_data = c;
465 state->creq->async.fn = composite_handler;
474 recv half of async composite connect code
476 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
480 status = composite_wait(c);
482 if (NT_STATUS_IS_OK(status)) {
483 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
484 talloc_steal(mem_ctx, state->io->out.tree);
492 sync version of smb_composite_connect
494 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
495 struct event_context *ev)
497 struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
498 return smb_composite_connect_recv(c, mem_ctx);