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"
28 /* the stages of this call */
29 enum connect_stage {CONNECT_RESOLVE,
31 CONNECT_SESSION_REQUEST,
33 CONNECT_SESSION_SETUP,
36 struct connect_state {
37 enum connect_stage stage;
38 struct smbcli_socket *sock;
39 struct smbcli_transport *transport;
40 struct smbcli_session *session;
41 struct smb_composite_connect *io;
42 union smb_tcon *io_tcon;
43 struct smb_composite_sesssetup *io_setup;
44 struct smbcli_request *req;
45 struct composite_context *creq;
49 static void request_handler(struct smbcli_request *);
50 static void composite_handler(struct composite_context *);
55 static NTSTATUS connect_send_negprot(struct composite_context *c,
56 struct smb_composite_connect *io)
58 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
60 state->req = smb_raw_negotiate_send(state->transport, lp_maxprotocol());
61 NT_STATUS_HAVE_NO_MEMORY(state->req);
63 state->req->async.fn = request_handler;
64 state->req->async.private = c;
65 state->stage = CONNECT_NEGPROT;
72 a tree connect request has competed
74 static NTSTATUS connect_tcon(struct composite_context *c,
75 struct smb_composite_connect *io)
77 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
80 status = smb_tree_connect_recv(state->req, c, state->io_tcon);
81 NT_STATUS_NOT_OK_RETURN(status);
83 io->out.tree->tid = state->io_tcon->tconx.out.tid;
84 if (state->io_tcon->tconx.out.dev_type) {
85 io->out.tree->device = talloc_strdup(io->out.tree,
86 state->io_tcon->tconx.out.dev_type);
88 if (state->io_tcon->tconx.out.fs_type) {
89 io->out.tree->fs_type = talloc_strdup(io->out.tree,
90 state->io_tcon->tconx.out.fs_type);
94 c->state = SMBCLI_REQUEST_DONE;
101 a session setup request has competed
103 static NTSTATUS connect_session_setup(struct composite_context *c,
104 struct smb_composite_connect *io)
106 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
109 status = smb_composite_sesssetup_recv(state->creq);
110 NT_STATUS_NOT_OK_RETURN(status);
112 state->session->vuid = state->io_setup->out.vuid;
114 /* setup for a tconx */
115 io->out.tree = smbcli_tree_init(state->session, state, True);
116 NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
118 state->io_tcon = talloc(c, union smb_tcon);
119 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
121 /* connect to a share using a tree connect */
122 state->io_tcon->generic.level = RAW_TCON_TCONX;
123 state->io_tcon->tconx.in.flags = 0;
124 state->io_tcon->tconx.in.password = data_blob(NULL, 0);
126 state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon,
130 NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
131 if (!io->in.service_type) {
132 state->io_tcon->tconx.in.device = "?????";
134 state->io_tcon->tconx.in.device = io->in.service_type;
137 state->req = smb_tree_connect_send(io->out.tree, state->io_tcon);
138 NT_STATUS_HAVE_NO_MEMORY(state->req);
140 state->req->async.fn = request_handler;
141 state->req->async.private = c;
142 state->stage = CONNECT_TCON;
148 a negprot request has competed
150 static NTSTATUS connect_negprot(struct composite_context *c,
151 struct smb_composite_connect *io)
153 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
156 status = smb_raw_negotiate_recv(state->req);
157 NT_STATUS_NOT_OK_RETURN(status);
159 /* next step is a session setup */
160 state->session = smbcli_session_init(state->transport, state, True);
161 NT_STATUS_HAVE_NO_MEMORY(state->session);
163 state->io_setup = talloc(c, struct smb_composite_sesssetup);
164 NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
166 /* prepare a session setup to establish a security context */
167 state->io_setup->in.sesskey = state->transport->negotiate.sesskey;
168 state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
169 state->io_setup->in.domain = io->in.domain;
170 state->io_setup->in.user = io->in.user;
171 state->io_setup->in.password = io->in.password;
173 state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
174 NT_STATUS_HAVE_NO_MEMORY(state->creq);
176 state->creq->async.fn = composite_handler;
177 state->creq->async.private = c;
178 state->stage = CONNECT_SESSION_SETUP;
185 a session request operation has competed
187 static NTSTATUS connect_session_request(struct composite_context *c,
188 struct smb_composite_connect *io)
190 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
193 status = smbcli_transport_connect_recv(state->req);
194 NT_STATUS_NOT_OK_RETURN(status);
196 /* next step is a negprot */
197 return connect_send_negprot(c, io);
201 a socket connection operation has competed
203 static NTSTATUS connect_socket(struct composite_context *c,
204 struct smb_composite_connect *io)
206 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
208 struct nbt_name calling, called;
210 status = smbcli_sock_connect_recv(state->creq);
211 NT_STATUS_NOT_OK_RETURN(status);
213 /* the socket is up - we can initialise the smbcli transport layer */
214 state->transport = smbcli_transport_init(state->sock, state, True);
215 NT_STATUS_HAVE_NO_MEMORY(state->transport);
217 calling.name = io->in.calling_name;
218 calling.type = NBT_NAME_CLIENT;
219 calling.scope = NULL;
221 nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
223 /* we have a connected socket - next step is a session
224 request, if needed. Port 445 doesn't need it, so it goes
225 straight to the negprot */
226 if (state->sock->port == 445) {
227 status = nbt_name_dup(state->transport, &called,
228 &state->transport->called);
229 NT_STATUS_NOT_OK_RETURN(status);
230 return connect_send_negprot(c, io);
233 state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
234 NT_STATUS_HAVE_NO_MEMORY(state->req);
236 state->req->async.fn = request_handler;
237 state->req->async.private = c;
238 state->stage = CONNECT_SESSION_REQUEST;
245 called when name resolution is finished
247 static NTSTATUS connect_resolve(struct composite_context *c,
248 struct smb_composite_connect *io)
250 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
254 status = resolve_name_recv(state->creq, state, &address);
255 NT_STATUS_NOT_OK_RETURN(status);
257 state->creq = smbcli_sock_connect_send(state->sock, address, state->io->in.port);
258 NT_STATUS_HAVE_NO_MEMORY(state->creq);
260 state->stage = CONNECT_SOCKET;
261 state->creq->async.private = c;
262 state->creq->async.fn = composite_handler;
269 handle and dispatch state transitions
271 static void state_handler(struct composite_context *c)
273 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
275 switch (state->stage) {
276 case CONNECT_RESOLVE:
277 c->status = connect_resolve(c, state->io);
280 c->status = connect_socket(c, state->io);
282 case CONNECT_SESSION_REQUEST:
283 c->status = connect_session_request(c, state->io);
285 case CONNECT_NEGPROT:
286 c->status = connect_negprot(c, state->io);
288 case CONNECT_SESSION_SETUP:
289 c->status = connect_session_setup(c, state->io);
292 c->status = connect_tcon(c, state->io);
296 if (!NT_STATUS_IS_OK(c->status)) {
297 c->state = SMBCLI_REQUEST_ERROR;
300 if (c->state >= SMBCLI_REQUEST_DONE &&
308 handler for completion of a smbcli_request sub-request
310 static void request_handler(struct smbcli_request *req)
312 struct composite_context *c = talloc_get_type(req->async.private,
313 struct composite_context);
314 return state_handler(c);
318 handler for completion of a smbcli_composite sub-request
320 static void composite_handler(struct composite_context *req)
322 struct composite_context *c = talloc_get_type(req->async.private,
323 struct composite_context);
324 return state_handler(c);
328 a function to establish a smbcli_tree from scratch
330 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
331 struct event_context *event_ctx)
333 struct composite_context *c;
334 struct connect_state *state;
335 struct nbt_name name;
337 c = talloc_zero(NULL, struct composite_context);
338 if (c == NULL) goto failed;
340 state = talloc(c, struct connect_state);
341 if (state == NULL) goto failed;
343 state->sock = smbcli_sock_init(state, event_ctx);
344 if (state->sock == NULL) goto failed;
347 state->stage = CONNECT_RESOLVE;
349 c->state = SMBCLI_REQUEST_SEND;
350 c->event_ctx = talloc_reference(c, state->sock->event.ctx);
353 name.name = io->in.dest_host;
354 name.type = NBT_NAME_SERVER;
357 state->creq = resolve_name_send(&name, c->event_ctx);
358 if (state->creq == NULL) goto failed;
360 state->creq->async.private = c;
361 state->creq->async.fn = composite_handler;
370 recv half of async composite connect code
372 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
376 status = composite_wait(c);
378 if (NT_STATUS_IS_OK(status)) {
379 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
380 talloc_steal(mem_ctx, state->io->out.tree);
388 sync version of smb_composite_connect
390 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx)
392 struct composite_context *c = smb_composite_connect_send(io, NULL);
393 return smb_composite_connect_recv(c, mem_ctx);