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,
38 struct connect_state {
39 enum connect_stage stage;
40 struct smbcli_socket *sock;
41 struct smbcli_transport *transport;
42 struct smbcli_session *session;
43 struct smb_composite_connectmulti *conn;
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 competed
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 has competed
106 static NTSTATUS connect_session_setup(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 state->session->vuid = state->io_setup->out.vuid;
117 /* setup for a tconx */
118 io->out.tree = smbcli_tree_init(state->session, state, True);
119 NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
121 state->io_tcon = talloc(c, union smb_tcon);
122 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
124 /* connect to a share using a tree connect */
125 state->io_tcon->generic.level = RAW_TCON_TCONX;
126 state->io_tcon->tconx.in.flags = 0;
127 state->io_tcon->tconx.in.password = data_blob(NULL, 0);
129 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
133 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
134 if (!io->in.service_type) {
135 state->io_tcon->tconx.in.device = "?????";
137 state->io_tcon->tconx.in.device = io->in.service_type;
140 state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
141 NT_STATUS_HAVE_NO_MEMORY(state->req);
142 if (state->req->state == SMBCLI_REQUEST_ERROR) {
143 return state->req->status;
146 state->req->async.fn = request_handler;
147 state->req->async.private = c;
148 state->stage = CONNECT_TCON;
154 a negprot request has competed
156 static NTSTATUS connect_negprot(struct composite_context *c,
157 struct smb_composite_connect *io)
159 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
162 status = smb_raw_negotiate_recv(state->req);
163 NT_STATUS_NOT_OK_RETURN(status);
165 /* next step is a session setup */
166 state->session = smbcli_session_init(state->transport, state, True);
167 NT_STATUS_HAVE_NO_MEMORY(state->session);
169 state->io_setup = talloc(c, struct smb_composite_sesssetup);
170 NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
172 /* prepare a session setup to establish a security context */
173 state->io_setup->in.sesskey = state->transport->negotiate.sesskey;
174 state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
175 state->io_setup->in.credentials = io->in.credentials;
176 state->io_setup->in.workgroup = io->in.workgroup;
178 state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
179 NT_STATUS_HAVE_NO_MEMORY(state->creq);
180 if (state->creq->state == COMPOSITE_STATE_ERROR) {
181 return state->creq->status;
184 state->creq->async.fn = composite_handler;
185 state->creq->async.private_data = c;
186 state->stage = CONNECT_SESSION_SETUP;
193 a session request operation has competed
195 static NTSTATUS connect_session_request(struct composite_context *c,
196 struct smb_composite_connect *io)
198 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
201 status = smbcli_transport_connect_recv(state->req);
202 NT_STATUS_NOT_OK_RETURN(status);
204 /* next step is a negprot */
205 return connect_send_negprot(c, io);
209 a socket connection operation has competed
211 static NTSTATUS connect_socket(struct composite_context *c,
212 struct smb_composite_connect *io)
214 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
216 struct nbt_name calling, called;
218 status = smb_composite_connectmulti_recv(state->creq, state);
219 NT_STATUS_NOT_OK_RETURN(status);
221 state->sock = state->conn->out.socket;
223 /* the socket is up - we can initialise the smbcli transport layer */
224 state->transport = smbcli_transport_init(state->sock, state, True);
225 NT_STATUS_HAVE_NO_MEMORY(state->transport);
227 make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
229 nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
231 /* we have a connected socket - next step is a session
232 request, if needed. Port 445 doesn't need it, so it goes
233 straight to the negprot */
234 if (state->sock->port == 445) {
235 status = nbt_name_dup(state->transport, &called,
236 &state->transport->called);
237 NT_STATUS_NOT_OK_RETURN(status);
238 return connect_send_negprot(c, io);
241 state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
242 NT_STATUS_HAVE_NO_MEMORY(state->req);
244 state->req->async.fn = request_handler;
245 state->req->async.private = c;
246 state->stage = CONNECT_SESSION_REQUEST;
253 called when name resolution is finished
255 static NTSTATUS connect_resolve(struct composite_context *c,
256 struct smb_composite_connect *io)
258 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
261 struct smb_composite_connectmulti *conn;
263 status = resolve_name_recv(state->creq, state, &address);
264 NT_STATUS_NOT_OK_RETURN(status);
266 conn = talloc(state, struct smb_composite_connectmulti);
267 NT_STATUS_HAVE_NO_MEMORY(conn);
269 conn->in.num_dests = 1;
271 conn->in.addresses = talloc_array(state->conn, const char *, 1);
272 NT_STATUS_HAVE_NO_MEMORY(conn->in.addresses);
273 conn->in.addresses[0] = address;
275 conn->in.hostnames = talloc_array(state->conn, const char *, 1);
276 NT_STATUS_HAVE_NO_MEMORY(conn->in.hostnames);
277 conn->in.hostnames[0] = state->io->in.dest_host;
279 conn->in.ports = NULL;
280 if (state->io->in.port != 0) {
281 conn->in.ports = talloc_array(state->conn, int, 1);
282 NT_STATUS_HAVE_NO_MEMORY(conn->in.ports);
283 conn->in.ports[0] = state->io->in.port;
286 state->creq = smb_composite_connectmulti_send(conn, state,
288 NT_STATUS_HAVE_NO_MEMORY(state->creq);
290 state->stage = CONNECT_SOCKET;
291 state->creq->async.private_data = c;
292 state->creq->async.fn = composite_handler;
299 handle and dispatch state transitions
301 static void state_handler(struct composite_context *c)
303 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
305 switch (state->stage) {
306 case CONNECT_RESOLVE:
307 c->status = connect_resolve(c, state->io);
310 c->status = connect_socket(c, state->io);
312 case CONNECT_SESSION_REQUEST:
313 c->status = connect_session_request(c, state->io);
315 case CONNECT_NEGPROT:
316 c->status = connect_negprot(c, state->io);
318 case CONNECT_SESSION_SETUP:
319 c->status = connect_session_setup(c, state->io);
322 c->status = connect_tcon(c, state->io);
326 if (!NT_STATUS_IS_OK(c->status)) {
327 c->state = COMPOSITE_STATE_ERROR;
330 if (c->state >= COMPOSITE_STATE_DONE &&
338 handler for completion of a smbcli_request sub-request
340 static void request_handler(struct smbcli_request *req)
342 struct composite_context *c = talloc_get_type(req->async.private,
343 struct composite_context);
348 handler for completion of a smbcli_composite sub-request
350 static void composite_handler(struct composite_context *creq)
352 struct composite_context *c = talloc_get_type(creq->async.private_data,
353 struct composite_context);
358 a function to establish a smbcli_tree from scratch
360 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
362 struct event_context *event_ctx)
364 struct composite_context *c;
365 struct connect_state *state;
366 struct nbt_name name;
368 c = talloc_zero(mem_ctx, struct composite_context);
369 if (c == NULL) goto failed;
371 state = talloc(c, struct connect_state);
372 if (state == NULL) goto failed;
374 if (event_ctx == NULL) {
375 event_ctx = event_context_init(mem_ctx);
380 c->state = COMPOSITE_STATE_IN_PROGRESS;
381 c->event_ctx = talloc_reference(c, event_ctx);
382 c->private_data = state;
384 state->stage = CONNECT_RESOLVE;
385 make_nbt_name_server(&name, io->in.dest_host);
386 state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
388 if (state->creq == NULL) goto failed;
389 state->creq->async.private_data = c;
390 state->creq->async.fn = composite_handler;
399 recv half of async composite connect code
401 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
405 status = composite_wait(c);
407 if (NT_STATUS_IS_OK(status)) {
408 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
409 talloc_steal(mem_ctx, state->io->out.tree);
417 sync version of smb_composite_connect
419 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
420 struct event_context *ev)
422 struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
423 return smb_composite_connect_recv(c, mem_ctx);