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 gensec_settings *gensec_settings;
39 struct smbcli_options options;
40 struct smb2_negprot negprot;
41 struct smb2_tree_connect tcon;
42 struct smb2_session *session;
43 struct smb2_tree *tree;
47 continue after tcon reply
49 static void continue_tcon(struct smb2_request *req)
51 struct composite_context *c = talloc_get_type(req->async.private_data,
52 struct composite_context);
53 struct smb2_connect_state *state = talloc_get_type(c->private_data,
54 struct smb2_connect_state);
56 c->status = smb2_tree_connect_recv(req, &state->tcon);
57 if (!composite_is_ok(c)) return;
59 state->tree->tid = state->tcon.out.tid;
65 continue after a session setup
67 static void continue_session(struct composite_context *creq)
69 struct composite_context *c = talloc_get_type(creq->async.private_data,
70 struct composite_context);
71 struct smb2_connect_state *state = talloc_get_type(c->private_data,
72 struct smb2_connect_state);
73 struct smb2_request *req;
75 c->status = smb2_session_setup_spnego_recv(creq);
76 if (!composite_is_ok(c)) return;
78 state->tree = smb2_tree_init(state->session, state, true);
79 if (composite_nomem(state->tree, c)) return;
81 state->tcon.in.reserved = 0;
82 state->tcon.in.path = talloc_asprintf(state, "\\\\%s\\%s",
83 state->host, state->share);
84 if (composite_nomem(state->tcon.in.path, c)) return;
86 req = smb2_tree_connect_send(state->tree, &state->tcon);
87 if (composite_nomem(req, c)) return;
89 req->async.fn = continue_tcon;
90 req->async.private_data = c;
94 continue after negprot reply
96 static void continue_negprot(struct smb2_request *req)
98 struct composite_context *c = talloc_get_type(req->async.private_data,
99 struct composite_context);
100 struct smb2_connect_state *state = talloc_get_type(c->private_data,
101 struct smb2_connect_state);
102 struct smb2_transport *transport = req->transport;
103 struct composite_context *creq;
105 c->status = smb2_negprot_recv(req, c, &state->negprot);
106 if (!composite_is_ok(c)) return;
108 transport->negotiate.system_time = state->negprot.out.system_time;
109 transport->negotiate.server_start_time = state->negprot.out.server_start_time;
110 transport->negotiate.security_mode = state->negprot.out.security_mode;
111 transport->negotiate.dialect_revision = state->negprot.out.dialect_revision;
113 switch (transport->options.signing) {
114 case SMB_SIGNING_OFF:
115 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
116 composite_error(c, NT_STATUS_ACCESS_DENIED);
119 transport->signing_required = false;
121 case SMB_SIGNING_SUPPORTED:
122 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
123 transport->signing_required = true;
125 transport->signing_required = false;
128 case SMB_SIGNING_AUTO:
129 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
130 transport->signing_required = true;
132 transport->signing_required = false;
135 case SMB_SIGNING_REQUIRED:
136 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
137 transport->signing_required = true;
139 composite_error(c, NT_STATUS_ACCESS_DENIED);
145 state->session = smb2_session_init(transport, state->gensec_settings, state, true);
146 if (composite_nomem(state->session, c)) return;
148 creq = smb2_session_setup_spnego_send(state->session, state->credentials);
150 composite_continue(c, creq, continue_session, c);
154 continue after a socket connect completes
156 static void continue_socket(struct composite_context *creq)
158 struct composite_context *c = talloc_get_type(creq->async.private_data,
159 struct composite_context);
160 struct smb2_connect_state *state = talloc_get_type(c->private_data,
161 struct smb2_connect_state);
162 struct smbcli_socket *sock;
163 struct smb2_transport *transport;
164 struct smb2_request *req;
165 uint16_t dialects[2];
167 c->status = smbcli_sock_connect_recv(creq, state, &sock);
168 if (!composite_is_ok(c)) return;
170 transport = smb2_transport_init(sock, state, &state->options);
171 if (composite_nomem(transport, c)) return;
173 ZERO_STRUCT(state->negprot);
174 state->negprot.in.dialect_count = 2;
175 switch (transport->options.signing) {
176 case SMB_SIGNING_OFF:
177 state->negprot.in.security_mode = 0;
179 case SMB_SIGNING_SUPPORTED:
180 case SMB_SIGNING_AUTO:
181 state->negprot.in.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
183 case SMB_SIGNING_REQUIRED:
184 state->negprot.in.security_mode =
185 SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED;
188 state->negprot.in.capabilities = 0;
189 unix_to_nt_time(&state->negprot.in.start_time, time(NULL));
190 dialects[0] = SMB2_DIALECT_REVISION;
191 dialects[1] = SMB21_DIALECT_REVISION;
192 state->negprot.in.dialects = dialects;
194 req = smb2_negprot_send(transport, &state->negprot);
195 if (composite_nomem(req, c)) return;
197 req->async.fn = continue_negprot;
198 req->async.private_data = c;
203 continue after a resolve finishes
205 static void continue_resolve(struct composite_context *creq)
207 struct composite_context *c = talloc_get_type(creq->async.private_data,
208 struct composite_context);
209 struct smb2_connect_state *state = talloc_get_type(c->private_data,
210 struct smb2_connect_state);
213 const char *default_ports[] = { "445", NULL };
215 c->status = resolve_name_recv(creq, state, &addr);
216 if (!composite_is_ok(c)) return;
218 if (state->ports == NULL) {
219 ports = default_ports;
221 ports = state->ports;
224 creq = smbcli_sock_connect_send(state, addr, ports, state->host, state->resolve_ctx, c->event_ctx, state->socket_options);
226 composite_continue(c, creq, continue_socket, c);
230 a composite function that does a full negprot/sesssetup/tcon, returning
231 a connected smb2_tree
233 struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx,
237 struct resolve_context *resolve_ctx,
238 struct cli_credentials *credentials,
239 struct tevent_context *ev,
240 struct smbcli_options *options,
241 const char *socket_options,
242 struct gensec_settings *gensec_settings)
244 struct composite_context *c;
245 struct smb2_connect_state *state;
246 struct nbt_name name;
247 struct composite_context *creq;
249 c = composite_create(mem_ctx, ev);
250 if (c == NULL) return NULL;
252 state = talloc(c, struct smb2_connect_state);
253 if (composite_nomem(state, c)) return c;
254 c->private_data = state;
256 state->credentials = credentials;
257 state->options = *options;
258 state->host = talloc_strdup(c, host);
259 if (composite_nomem(state->host, c)) return c;
260 state->ports = talloc_reference(state, ports);
261 state->share = talloc_strdup(c, share);
262 if (composite_nomem(state->share, c)) return c;
263 state->resolve_ctx = talloc_reference(state, resolve_ctx);
264 state->socket_options = talloc_reference(state, socket_options);
265 state->gensec_settings = talloc_reference(state, gensec_settings);
270 creq = resolve_name_send(resolve_ctx, &name, c->event_ctx);
271 composite_continue(c, creq, continue_resolve, c);
276 receive a connect reply
278 NTSTATUS smb2_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
279 struct smb2_tree **tree)
282 struct smb2_connect_state *state = talloc_get_type(c->private_data,
283 struct smb2_connect_state);
284 status = composite_wait(c);
285 if (NT_STATUS_IS_OK(status)) {
286 *tree = talloc_steal(mem_ctx, state->tree);
293 sync version of smb2_connect
295 NTSTATUS smb2_connect(TALLOC_CTX *mem_ctx,
296 const char *host, const char **ports,
298 struct resolve_context *resolve_ctx,
299 struct cli_credentials *credentials,
300 struct smb2_tree **tree,
301 struct tevent_context *ev,
302 struct smbcli_options *options,
303 const char *socket_options,
304 struct gensec_settings *gensec_settings)
306 struct composite_context *c = smb2_connect_send(mem_ctx, host, ports,
308 credentials, ev, options,
311 return smb2_connect_recv(c, mem_ctx, tree);