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;
37 const char *socket_options;
38 struct smbcli_options options;
39 struct smb2_negprot negprot;
40 struct smb2_tree_connect tcon;
41 struct smb2_session *session;
42 struct smb2_tree *tree;
46 continue after tcon reply
48 static void continue_tcon(struct smb2_request *req)
50 struct composite_context *c = talloc_get_type(req->async.private_data,
51 struct composite_context);
52 struct smb2_connect_state *state = talloc_get_type(c->private_data,
53 struct smb2_connect_state);
55 c->status = smb2_tree_connect_recv(req, &state->tcon);
56 if (!composite_is_ok(c)) return;
58 state->tree->tid = state->tcon.out.tid;
64 continue after a session setup
66 static void continue_session(struct composite_context *creq)
68 struct composite_context *c = talloc_get_type(creq->async.private_data,
69 struct composite_context);
70 struct smb2_connect_state *state = talloc_get_type(c->private_data,
71 struct smb2_connect_state);
72 struct smb2_request *req;
74 c->status = smb2_session_setup_spnego_recv(creq);
75 if (!composite_is_ok(c)) return;
77 state->tree = smb2_tree_init(state->session, state, true);
78 if (composite_nomem(state->tree, c)) return;
80 state->tcon.in.reserved = 0;
81 state->tcon.in.path = talloc_asprintf(state, "\\\\%s\\%s",
82 state->host, state->share);
83 if (composite_nomem(state->tcon.in.path, c)) return;
85 req = smb2_tree_connect_send(state->tree, &state->tcon);
86 if (composite_nomem(req, c)) return;
88 req->async.fn = continue_tcon;
89 req->async.private_data = c;
93 continue after negprot reply
95 static void continue_negprot(struct smb2_request *req)
97 struct composite_context *c = talloc_get_type(req->async.private_data,
98 struct composite_context);
99 struct smb2_connect_state *state = talloc_get_type(c->private_data,
100 struct smb2_connect_state);
101 struct smb2_transport *transport = req->transport;
102 struct composite_context *creq;
104 c->status = smb2_negprot_recv(req, c, &state->negprot);
105 if (!composite_is_ok(c)) return;
107 transport->negotiate.system_time = state->negprot.out.system_time;
108 transport->negotiate.server_start_time = state->negprot.out.server_start_time;
109 transport->negotiate.security_mode = state->negprot.out.security_mode;
111 switch (transport->options.signing) {
112 case SMB_SIGNING_OFF:
113 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
114 composite_error(c, NT_STATUS_ACCESS_DENIED);
117 transport->signing_required = false;
119 case SMB_SIGNING_SUPPORTED:
120 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
121 transport->signing_required = true;
123 transport->signing_required = false;
126 case SMB_SIGNING_AUTO:
127 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
128 transport->signing_required = true;
130 transport->signing_required = false;
133 case SMB_SIGNING_REQUIRED:
134 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
135 transport->signing_required = true;
137 composite_error(c, NT_STATUS_ACCESS_DENIED);
143 state->session = smb2_session_init(transport, lp_gensec_settings(transport, global_loadparm), state, true);
144 if (composite_nomem(state->session, c)) return;
146 creq = smb2_session_setup_spnego_send(state->session, state->credentials);
148 composite_continue(c, creq, continue_session, c);
152 continue after a socket connect completes
154 static void continue_socket(struct composite_context *creq)
156 struct composite_context *c = talloc_get_type(creq->async.private_data,
157 struct composite_context);
158 struct smb2_connect_state *state = talloc_get_type(c->private_data,
159 struct smb2_connect_state);
160 struct smbcli_socket *sock;
161 struct smb2_transport *transport;
162 struct smb2_request *req;
163 uint16_t dialects[2];
165 c->status = smbcli_sock_connect_recv(creq, state, &sock);
166 if (!composite_is_ok(c)) return;
168 transport = smb2_transport_init(sock, state, &state->options);
169 if (composite_nomem(transport, c)) return;
171 ZERO_STRUCT(state->negprot);
172 state->negprot.in.dialect_count = 2;
173 switch (transport->options.signing) {
174 case SMB_SIGNING_OFF:
175 state->negprot.in.security_mode = 0;
177 case SMB_SIGNING_SUPPORTED:
178 case SMB_SIGNING_AUTO:
179 state->negprot.in.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
181 case SMB_SIGNING_REQUIRED:
182 state->negprot.in.security_mode =
183 SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED;
186 state->negprot.in.capabilities = 0;
187 unix_to_nt_time(&state->negprot.in.start_time, time(NULL));
188 dialects[0] = SMB2_DIALECT_REVISION;
190 state->negprot.in.dialects = dialects;
192 req = smb2_negprot_send(transport, &state->negprot);
193 if (composite_nomem(req, c)) return;
195 req->async.fn = continue_negprot;
196 req->async.private_data = c;
201 continue after a resolve finishes
203 static void continue_resolve(struct composite_context *creq)
205 struct composite_context *c = talloc_get_type(creq->async.private_data,
206 struct composite_context);
207 struct smb2_connect_state *state = talloc_get_type(c->private_data,
208 struct smb2_connect_state);
211 const char *default_ports[] = { "445", NULL };
213 c->status = resolve_name_recv(creq, state, &addr);
214 if (!composite_is_ok(c)) return;
216 if (state->ports == NULL) {
217 ports = default_ports;
219 ports = state->ports;
222 creq = smbcli_sock_connect_send(state, addr, ports, state->host, state->resolve_ctx, c->event_ctx, state->socket_options);
224 composite_continue(c, creq, continue_socket, c);
228 a composite function that does a full negprot/sesssetup/tcon, returning
229 a connected smb2_tree
231 struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx,
235 struct resolve_context *resolve_ctx,
236 struct cli_credentials *credentials,
237 struct event_context *ev,
238 struct smbcli_options *options,
239 const char *socket_options)
241 struct composite_context *c;
242 struct smb2_connect_state *state;
243 struct nbt_name name;
244 struct composite_context *creq;
246 c = composite_create(mem_ctx, ev);
247 if (c == NULL) return NULL;
249 state = talloc(c, struct smb2_connect_state);
250 if (composite_nomem(state, c)) return c;
251 c->private_data = state;
253 state->credentials = credentials;
254 state->options = *options;
255 state->host = talloc_strdup(c, host);
256 if (composite_nomem(state->host, c)) return c;
257 state->ports = talloc_reference(state, ports);
258 state->share = talloc_strdup(c, share);
259 if (composite_nomem(state->share, c)) return c;
260 state->resolve_ctx = talloc_reference(state, resolve_ctx);
261 state->socket_options = talloc_reference(state, socket_options);
262 if (composite_nomem(state->socket_options, c)) return c;
267 creq = resolve_name_send(resolve_ctx, &name, c->event_ctx);
268 composite_continue(c, creq, continue_resolve, c);
273 receive a connect reply
275 NTSTATUS smb2_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
276 struct smb2_tree **tree)
279 struct smb2_connect_state *state = talloc_get_type(c->private_data,
280 struct smb2_connect_state);
281 status = composite_wait(c);
282 if (NT_STATUS_IS_OK(status)) {
283 *tree = talloc_steal(mem_ctx, state->tree);
290 sync version of smb2_connect
292 NTSTATUS smb2_connect(TALLOC_CTX *mem_ctx,
293 const char *host, const char **ports,
295 struct resolve_context *resolve_ctx,
296 struct cli_credentials *credentials,
297 struct smb2_tree **tree,
298 struct event_context *ev,
299 struct smbcli_options *options,
300 const char *socket_options)
302 struct composite_context *c = smb2_connect_send(mem_ctx, host, ports,
304 credentials, ev, options,
306 return smb2_connect_recv(c, mem_ctx, tree);