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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "libcli/composite/composite.h"
28 #include "libcli/resolve/resolve.h"
29 #include "param/param.h"
31 struct smb2_connect_state {
32 struct cli_credentials *credentials;
33 struct resolve_context *resolve_ctx;
36 struct smbcli_options options;
37 struct smb2_negprot negprot;
38 struct smb2_tree_connect tcon;
39 struct smb2_session *session;
40 struct smb2_tree *tree;
44 continue after tcon reply
46 static void continue_tcon(struct smb2_request *req)
48 struct composite_context *c = talloc_get_type(req->async.private_data,
49 struct composite_context);
50 struct smb2_connect_state *state = talloc_get_type(c->private_data,
51 struct smb2_connect_state);
53 c->status = smb2_tree_connect_recv(req, &state->tcon);
54 if (!composite_is_ok(c)) return;
56 state->tree->tid = state->tcon.out.tid;
62 continue after a session setup
64 static void continue_session(struct composite_context *creq)
66 struct composite_context *c = talloc_get_type(creq->async.private_data,
67 struct composite_context);
68 struct smb2_connect_state *state = talloc_get_type(c->private_data,
69 struct smb2_connect_state);
70 struct smb2_request *req;
72 c->status = smb2_session_setup_spnego_recv(creq);
73 if (!composite_is_ok(c)) return;
75 state->tree = smb2_tree_init(state->session, state, true);
76 if (composite_nomem(state->tree, c)) return;
78 state->tcon.in.reserved = 0;
79 state->tcon.in.path = talloc_asprintf(state, "\\\\%s\\%s",
80 state->host, state->share);
81 if (composite_nomem(state->tcon.in.path, c)) return;
83 req = smb2_tree_connect_send(state->tree, &state->tcon);
84 if (composite_nomem(req, c)) return;
86 req->async.fn = continue_tcon;
87 req->async.private_data = c;
91 continue after negprot reply
93 static void continue_negprot(struct smb2_request *req)
95 struct composite_context *c = talloc_get_type(req->async.private_data,
96 struct composite_context);
97 struct smb2_connect_state *state = talloc_get_type(c->private_data,
98 struct smb2_connect_state);
99 struct smb2_transport *transport = req->transport;
100 struct composite_context *creq;
102 c->status = smb2_negprot_recv(req, c, &state->negprot);
103 if (!composite_is_ok(c)) return;
105 transport->negotiate.system_time = state->negprot.out.system_time;
106 transport->negotiate.server_start_time = state->negprot.out.server_start_time;
107 transport->negotiate.security_mode = state->negprot.out.security_mode;
109 switch (transport->options.signing) {
110 case SMB_SIGNING_OFF:
111 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
112 composite_error(c, NT_STATUS_ACCESS_DENIED);
115 transport->signing.doing_signing = false;
117 case SMB_SIGNING_SUPPORTED:
118 case SMB_SIGNING_AUTO:
119 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
120 transport->signing.doing_signing = true;
122 transport->signing.doing_signing = false;
125 case SMB_SIGNING_REQUIRED:
126 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
127 transport->signing.doing_signing = true;
129 composite_error(c, NT_STATUS_ACCESS_DENIED);
136 state->session = smb2_session_init(transport, global_loadparm, state, true);
137 if (composite_nomem(state->session, c)) return;
139 creq = smb2_session_setup_spnego_send(state->session, state->credentials);
141 composite_continue(c, creq, continue_session, c);
145 continue after a socket connect completes
147 static void continue_socket(struct composite_context *creq)
149 struct composite_context *c = talloc_get_type(creq->async.private_data,
150 struct composite_context);
151 struct smb2_connect_state *state = talloc_get_type(c->private_data,
152 struct smb2_connect_state);
153 struct smbcli_socket *sock;
154 struct smb2_transport *transport;
155 struct smb2_request *req;
156 uint16_t dialects[2];
158 c->status = smbcli_sock_connect_recv(creq, state, &sock);
159 if (!composite_is_ok(c)) return;
161 transport = smb2_transport_init(sock, state, &state->options);
162 if (composite_nomem(transport, c)) return;
164 ZERO_STRUCT(state->negprot);
165 state->negprot.in.dialect_count = 2;
166 switch (transport->options.signing) {
167 case SMB_SIGNING_OFF:
168 state->negprot.in.security_mode = 0;
170 case SMB_SIGNING_SUPPORTED:
171 case SMB_SIGNING_AUTO:
172 state->negprot.in.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
174 case SMB_SIGNING_REQUIRED:
175 state->negprot.in.security_mode =
176 SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED;
179 state->negprot.in.capabilities = 0;
180 unix_to_nt_time(&state->negprot.in.start_time, time(NULL));
182 dialects[1] = SMB2_DIALECT_REVISION;
183 state->negprot.in.dialects = dialects;
185 req = smb2_negprot_send(transport, &state->negprot);
186 if (composite_nomem(req, c)) return;
188 req->async.fn = continue_negprot;
189 req->async.private_data = c;
194 continue after a resolve finishes
196 static void continue_resolve(struct composite_context *creq)
198 struct composite_context *c = talloc_get_type(creq->async.private_data,
199 struct composite_context);
200 struct smb2_connect_state *state = talloc_get_type(c->private_data,
201 struct smb2_connect_state);
203 const char *ports[2] = { "445", NULL };
205 c->status = resolve_name_recv(creq, state, &addr);
206 if (!composite_is_ok(c)) return;
208 creq = smbcli_sock_connect_send(state, addr, ports, state->host, state->resolve_ctx, c->event_ctx);
210 composite_continue(c, creq, continue_socket, c);
214 a composite function that does a full negprot/sesssetup/tcon, returning
215 a connected smb2_tree
217 struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx,
220 struct resolve_context *resolve_ctx,
221 struct cli_credentials *credentials,
222 struct event_context *ev,
223 struct smbcli_options *options)
225 struct composite_context *c;
226 struct smb2_connect_state *state;
227 struct nbt_name name;
228 struct composite_context *creq;
230 c = composite_create(mem_ctx, ev);
231 if (c == NULL) return NULL;
233 state = talloc(c, struct smb2_connect_state);
234 if (composite_nomem(state, c)) return c;
235 c->private_data = state;
237 state->credentials = credentials;
238 state->options = *options;
239 state->host = talloc_strdup(c, host);
240 if (composite_nomem(state->host, c)) return c;
241 state->share = talloc_strdup(c, share);
242 if (composite_nomem(state->share, c)) return c;
243 state->resolve_ctx = talloc_reference(state, resolve_ctx);
248 creq = resolve_name_send(resolve_ctx, &name, c->event_ctx);
249 composite_continue(c, creq, continue_resolve, c);
254 receive a connect reply
256 NTSTATUS smb2_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
257 struct smb2_tree **tree)
260 struct smb2_connect_state *state = talloc_get_type(c->private_data,
261 struct smb2_connect_state);
262 status = composite_wait(c);
263 if (NT_STATUS_IS_OK(status)) {
264 *tree = talloc_steal(mem_ctx, state->tree);
271 sync version of smb2_connect
273 NTSTATUS smb2_connect(TALLOC_CTX *mem_ctx,
274 const char *host, const char *share,
275 struct resolve_context *resolve_ctx,
276 struct cli_credentials *credentials,
277 struct smb2_tree **tree,
278 struct event_context *ev,
279 struct smbcli_options *options)
281 struct composite_context *c = smb2_connect_send(mem_ctx, host, share,
283 credentials, ev, options);
284 return smb2_connect_recv(c, mem_ctx, tree);