2 Unix SMB/CIFS implementation.
4 SMB2 composite connection setup
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "libcli/composite/composite.h"
29 struct smb2_connect_state {
30 struct smb2_request *req;
31 struct composite_context *creq;
32 struct cli_credentials *credentials;
35 struct smb2_negprot negprot;
36 struct smb2_tree_connect tcon;
37 struct smb2_session *session;
38 struct smb2_tree *tree;
42 continue after tcon reply
44 static void continue_tcon(struct smb2_request *req)
46 struct composite_context *c = talloc_get_type(req->async.private,
47 struct composite_context);
48 struct smb2_connect_state *state = talloc_get_type(c->private_data,
49 struct smb2_connect_state);
51 c->status = smb2_tree_connect_recv(req, &state->tcon);
52 if (!NT_STATUS_IS_OK(c->status)) {
53 composite_error(c, c->status);
57 state->tree->tid = state->tcon.out.tid;
63 continue after a session setup
65 static void continue_session(struct composite_context *creq)
67 struct composite_context *c = talloc_get_type(creq->async.private_data,
68 struct composite_context);
69 struct smb2_connect_state *state = talloc_get_type(c->private_data,
70 struct smb2_connect_state);
72 c->status = smb2_session_setup_spnego_recv(creq);
73 if (!NT_STATUS_IS_OK(c->status)) {
74 composite_error(c, c->status);
78 state->tree = smb2_tree_init(state->session, state, True);
79 if (state->tree == NULL) {
80 composite_error(c, NT_STATUS_NO_MEMORY);
84 state->tcon.in.unknown1 = 0x09;
85 state->tcon.in.path = talloc_asprintf(state, "\\\\%s\\%s",
86 state->host, state->share);
87 if (state->tcon.in.path == NULL) {
88 composite_error(c, NT_STATUS_NO_MEMORY);
92 state->req = smb2_tree_connect_send(state->tree, &state->tcon);
93 if (state->req == NULL) {
94 composite_error(c, NT_STATUS_NO_MEMORY);
98 state->req->async.fn = continue_tcon;
99 state->req->async.private = c;
103 continue after negprot reply
105 static void continue_negprot(struct smb2_request *req)
107 struct composite_context *c = talloc_get_type(req->async.private,
108 struct composite_context);
109 struct smb2_connect_state *state = talloc_get_type(c->private_data,
110 struct smb2_connect_state);
111 struct smb2_transport *transport = req->transport;
113 c->status = smb2_negprot_recv(req, c, &state->negprot);
114 if (!NT_STATUS_IS_OK(c->status)) {
115 composite_error(c, c->status);
119 state->session = smb2_session_init(transport, state, True);
120 if (state->session == NULL) {
121 composite_error(c, NT_STATUS_NO_MEMORY);
125 state->creq = smb2_session_setup_spnego_send(state->session, state->credentials);
126 if (state->creq == NULL) {
127 composite_error(c, NT_STATUS_NO_MEMORY);
131 state->creq->async.fn = continue_session;
132 state->creq->async.private_data = c;
136 continue after a socket connect completes
138 static void continue_socket(struct composite_context *creq)
140 struct composite_context *c = talloc_get_type(creq->async.private_data,
141 struct composite_context);
142 struct smb2_connect_state *state = talloc_get_type(c->private_data,
143 struct smb2_connect_state);
144 struct smbcli_socket *sock;
145 struct smb2_transport *transport;
147 c->status = smbcli_sock_connect_recv(creq, state, &sock);
148 if (!NT_STATUS_IS_OK(c->status)) {
149 composite_error(c, c->status);
153 transport = smb2_transport_init(sock, state);
154 if (transport == NULL) {
155 composite_error(c, NT_STATUS_NO_MEMORY);
159 ZERO_STRUCT(state->negprot);
160 state->negprot.in.unknown1 = 0x0001;
162 state->req = smb2_negprot_send(transport, &state->negprot);
163 if (state->req == NULL) {
164 composite_error(c, NT_STATUS_NO_MEMORY);
168 state->req->async.fn = continue_negprot;
169 state->req->async.private = c;
174 continue after a resolve finishes
176 static void continue_resolve(struct composite_context *creq)
178 struct composite_context *c = talloc_get_type(creq->async.private_data,
179 struct composite_context);
180 struct smb2_connect_state *state = talloc_get_type(c->private_data,
181 struct smb2_connect_state);
184 c->status = resolve_name_recv(creq, state, &addr);
185 if (!NT_STATUS_IS_OK(c->status)) {
186 composite_error(c, c->status);
190 state->creq = smbcli_sock_connect_send(state, addr, 445, state->host, c->event_ctx);
191 if (state->creq == NULL) {
192 composite_error(c, NT_STATUS_NO_MEMORY);
196 state->creq->async.private_data = c;
197 state->creq->async.fn = continue_socket;
201 a composite function that does a full negprot/sesssetup/tcon, returning
202 a connected smb2_tree
204 struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx,
207 struct cli_credentials *credentials,
208 struct event_context *ev)
210 struct composite_context *c;
211 struct smb2_connect_state *state;
212 struct nbt_name name;
214 c = talloc_zero(mem_ctx, struct composite_context);
215 if (c == NULL) return NULL;
217 state = talloc(c, struct smb2_connect_state);
219 c->status = NT_STATUS_NO_MEMORY;
223 c->state = COMPOSITE_STATE_IN_PROGRESS;
224 c->private_data = state;
227 state->credentials = credentials;
228 state->host = talloc_strdup(c, host);
229 state->share = talloc_strdup(c, share);
230 if (state->host == NULL || state->share == NULL) {
231 c->status = NT_STATUS_NO_MEMORY;
238 state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
239 if (state->creq == NULL) goto failed;
241 state->creq->async.private_data = c;
242 state->creq->async.fn = continue_resolve;
247 composite_trigger_error(c);
252 receive a connect reply
254 NTSTATUS smb2_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
255 struct smb2_tree **tree)
258 struct smb2_connect_state *state = talloc_get_type(c->private_data,
259 struct smb2_connect_state);
260 status = composite_wait(c);
261 if (NT_STATUS_IS_OK(status)) {
262 *tree = talloc_steal(mem_ctx, state->tree);
269 sync version of smb2_connect
271 NTSTATUS smb2_connect(TALLOC_CTX *mem_ctx,
272 const char *host, const char *share,
273 struct cli_credentials *credentials,
274 struct smb2_tree **tree,
275 struct event_context *ev)
277 struct composite_context *c = smb2_connect_send(mem_ctx, host, share,
279 return smb2_connect_recv(c, mem_ctx, tree);