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 3 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, see <http://www.gnu.org/licenses/>.
20 a composite API for making handling a generic async session setup
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
26 #include "libcli/smb_composite/smb_composite.h"
27 #include "libcli/auth/libcli_auth.h"
28 #include "auth/auth.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/credentials/credentials.h"
33 struct sesssetup_state {
34 union smb_sesssetup setup;
35 NTSTATUS gensec_status;
36 struct smb_composite_sesssetup *io;
37 struct smbcli_request *req;
40 static int sesssetup_state_destructor(struct sesssetup_state *state)
43 talloc_free(state->req);
50 static NTSTATUS session_setup_old(struct composite_context *c,
51 struct smbcli_session *session,
52 struct smb_composite_sesssetup *io,
53 struct smbcli_request **req);
54 static NTSTATUS session_setup_nt1(struct composite_context *c,
55 struct smbcli_session *session,
56 struct smb_composite_sesssetup *io,
57 struct smbcli_request **req);
58 static NTSTATUS session_setup_spnego(struct composite_context *c,
59 struct smbcli_session *session,
60 struct smb_composite_sesssetup *io,
61 struct smbcli_request **req);
64 store the user session key for a transport
66 static void set_user_session_key(struct smbcli_session *session,
67 const DATA_BLOB *session_key)
69 session->user_session_key = data_blob_talloc(session,
75 handler for completion of a smbcli_request sub-request
77 static void request_handler(struct smbcli_request *req)
79 struct composite_context *c = req->async.private;
80 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
81 struct smbcli_session *session = req->session;
82 DATA_BLOB session_key = data_blob(NULL, 0);
83 DATA_BLOB null_data_blob = data_blob(NULL, 0);
84 NTSTATUS session_key_err, nt_status;
86 c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
89 switch (state->setup.old.level) {
90 case RAW_SESSSETUP_OLD:
91 state->io->out.vuid = state->setup.old.out.vuid;
92 /* This doesn't work, as this only happens on old
93 * protocols, where this comparison won't match. */
94 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
95 /* we neet to reset the vuid for a new try */
97 if (cli_credentials_wrong_password(state->io->in.credentials)) {
98 nt_status = session_setup_old(c, session,
101 if (NT_STATUS_IS_OK(nt_status)) {
102 c->status = nt_status;
103 composite_continue_smb(c, state->req, request_handler, c);
110 case RAW_SESSSETUP_NT1:
111 state->io->out.vuid = state->setup.nt1.out.vuid;
112 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
113 /* we neet to reset the vuid for a new try */
115 if (cli_credentials_wrong_password(state->io->in.credentials)) {
116 nt_status = session_setup_nt1(c, session,
119 if (NT_STATUS_IS_OK(nt_status)) {
120 c->status = nt_status;
121 composite_continue_smb(c, state->req, request_handler, c);
128 case RAW_SESSSETUP_SPNEGO:
129 state->io->out.vuid = state->setup.spnego.out.vuid;
130 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
131 /* we neet to reset the vuid for a new try */
133 if (cli_credentials_wrong_password(state->io->in.credentials)) {
134 nt_status = session_setup_spnego(c, session,
137 if (NT_STATUS_IS_OK(nt_status)) {
138 c->status = nt_status;
139 composite_continue_smb(c, state->req, request_handler, c);
144 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
145 !NT_STATUS_IS_OK(c->status)) {
148 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
150 /* The status value here, from the earlier pass at GENSEC is
151 * vital to the security of the system. Even if the other end
152 * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
153 * you must keep feeding it blobs, or else the remote
154 * host/attacker might avoid mutal authentication
157 state->gensec_status = gensec_update(session->gensec, state,
158 state->setup.spnego.out.secblob,
159 &state->setup.spnego.in.secblob);
160 c->status = state->gensec_status;
161 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
162 !NT_STATUS_IS_OK(c->status)) {
166 state->setup.spnego.in.secblob = data_blob(NULL, 0);
169 /* we need to do another round of session setup. We keep going until both sides
171 session_key_err = gensec_session_key(session->gensec, &session_key);
172 if (NT_STATUS_IS_OK(session_key_err)) {
173 set_user_session_key(session, &session_key);
174 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
177 if (state->setup.spnego.in.secblob.length) {
179 * set the session->vuid value only for calling
180 * smb_raw_sesssetup_send()
182 uint16_t vuid = session->vuid;
183 session->vuid = state->io->out.vuid;
184 state->req = smb_raw_sesssetup_send(session, &state->setup);
185 session->vuid = vuid;
186 composite_continue_smb(c, state->req, request_handler, c);
191 case RAW_SESSSETUP_SMB2:
192 c->status = NT_STATUS_INTERNAL_ERROR;
196 /* enforce the local signing required flag */
197 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
198 if (!session->transport->negotiate.sign_info.doing_signing
199 && session->transport->negotiate.sign_info.mandatory_signing) {
200 DEBUG(0, ("SMB signing required, but server does not support it\n"));
201 c->status = NT_STATUS_ACCESS_DENIED;
205 if (!NT_STATUS_IS_OK(c->status)) {
206 composite_error(c, c->status);
215 send a nt1 style session setup
217 static NTSTATUS session_setup_nt1(struct composite_context *c,
218 struct smbcli_session *session,
219 struct smb_composite_sesssetup *io,
220 struct smbcli_request **req)
223 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
224 const char *password = cli_credentials_get_password(io->in.credentials);
225 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
226 DATA_BLOB session_key;
227 int flags = CLI_CRED_NTLM_AUTH;
228 if (lp_client_lanman_auth()) {
229 flags |= CLI_CRED_LANMAN_AUTH;
232 if (lp_client_ntlmv2_auth()) {
233 flags |= CLI_CRED_NTLMv2_AUTH;
236 state->setup.nt1.level = RAW_SESSSETUP_NT1;
237 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
238 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
239 state->setup.nt1.in.vc_num = 1;
240 state->setup.nt1.in.sesskey = io->in.sesskey;
241 state->setup.nt1.in.capabilities = io->in.capabilities;
242 state->setup.nt1.in.os = "Unix";
243 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
245 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
246 &state->setup.nt1.in.user,
247 &state->setup.nt1.in.domain);
250 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
251 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
253 session->transport->negotiate.secblob,
255 &state->setup.nt1.in.password1,
256 &state->setup.nt1.in.password2,
258 NT_STATUS_NOT_OK_RETURN(nt_status);
260 smbcli_transport_simple_set_signing(session->transport, session_key,
261 state->setup.nt1.in.password2);
262 set_user_session_key(session, &session_key);
264 data_blob_free(&session_key);
265 } else if (lp_client_plaintext_auth()) {
266 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
267 state->setup.nt1.in.password2 = data_blob(NULL, 0);
269 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
270 return NT_STATUS_INVALID_PARAMETER;
273 *req = smb_raw_sesssetup_send(session, &state->setup);
275 return NT_STATUS_NO_MEMORY;
277 return (*req)->status;
282 old style session setup (pre NT1 protocol level)
284 static NTSTATUS session_setup_old(struct composite_context *c,
285 struct smbcli_session *session,
286 struct smb_composite_sesssetup *io,
287 struct smbcli_request **req)
290 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
291 const char *password = cli_credentials_get_password(io->in.credentials);
292 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
293 DATA_BLOB session_key;
295 if (lp_client_lanman_auth()) {
296 flags |= CLI_CRED_LANMAN_AUTH;
299 if (lp_client_ntlmv2_auth()) {
300 flags |= CLI_CRED_NTLMv2_AUTH;
303 state->setup.old.level = RAW_SESSSETUP_OLD;
304 state->setup.old.in.bufsize = session->transport->options.max_xmit;
305 state->setup.old.in.mpx_max = session->transport->options.max_mux;
306 state->setup.old.in.vc_num = 1;
307 state->setup.old.in.sesskey = io->in.sesskey;
308 state->setup.old.in.os = "Unix";
309 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
310 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
311 &state->setup.old.in.user,
312 &state->setup.old.in.domain);
314 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
315 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
317 session->transport->negotiate.secblob,
319 &state->setup.old.in.password,
322 NT_STATUS_NOT_OK_RETURN(nt_status);
323 set_user_session_key(session, &session_key);
325 data_blob_free(&session_key);
326 } else if (lp_client_plaintext_auth()) {
327 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
329 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
330 return NT_STATUS_INVALID_PARAMETER;
333 *req = smb_raw_sesssetup_send(session, &state->setup);
335 return NT_STATUS_NO_MEMORY;
337 return (*req)->status;
342 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
344 static NTSTATUS session_setup_spnego(struct composite_context *c,
345 struct smbcli_session *session,
346 struct smb_composite_sesssetup *io,
347 struct smbcli_request **req)
349 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
350 NTSTATUS status, session_key_err;
351 DATA_BLOB session_key = data_blob(NULL, 0);
352 DATA_BLOB null_data_blob = data_blob(NULL, 0);
353 const char *chosen_oid = NULL;
355 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
356 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
357 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
358 state->setup.spnego.in.vc_num = 1;
359 state->setup.spnego.in.sesskey = io->in.sesskey;
360 state->setup.spnego.in.capabilities = io->in.capabilities;
361 state->setup.spnego.in.os = "Unix";
362 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
363 state->setup.spnego.in.workgroup = io->in.workgroup;
365 smbcli_temp_set_signing(session->transport);
367 status = gensec_client_start(session, &session->gensec, c->event_ctx);
368 if (!NT_STATUS_IS_OK(status)) {
369 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
373 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
375 status = gensec_set_credentials(session->gensec, io->in.credentials);
376 if (!NT_STATUS_IS_OK(status)) {
377 DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n",
382 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
383 if (!NT_STATUS_IS_OK(status)) {
384 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
389 status = gensec_set_target_service(session->gensec, "cifs");
390 if (!NT_STATUS_IS_OK(status)) {
391 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
396 if (session->transport->negotiate.secblob.length) {
397 chosen_oid = GENSEC_OID_SPNEGO;
398 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
399 if (!NT_STATUS_IS_OK(status)) {
400 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
401 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
402 chosen_oid = GENSEC_OID_NTLMSSP;
403 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
404 if (!NT_STATUS_IS_OK(status)) {
405 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
406 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
411 /* without a sec blob, means raw NTLMSSP */
412 chosen_oid = GENSEC_OID_NTLMSSP;
413 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
414 if (!NT_STATUS_IS_OK(status)) {
415 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
416 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
420 if (chosen_oid == GENSEC_OID_SPNEGO) {
421 status = gensec_update(session->gensec, state,
422 session->transport->negotiate.secblob,
423 &state->setup.spnego.in.secblob);
425 status = gensec_update(session->gensec, state,
427 &state->setup.spnego.in.secblob);
431 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
432 !NT_STATUS_IS_OK(status)) {
433 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
434 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
437 state->gensec_status = status;
439 session_key_err = gensec_session_key(session->gensec, &session_key);
440 if (NT_STATUS_IS_OK(session_key_err)) {
441 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
444 *req = smb_raw_sesssetup_send(session, &state->setup);
446 return NT_STATUS_NO_MEMORY;
448 return (*req)->status;
453 composite session setup function that hides the details of all the
454 different session setup varients, including the multi-pass nature of
457 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
458 struct smb_composite_sesssetup *io)
460 struct composite_context *c;
461 struct sesssetup_state *state;
464 c = composite_create(session, session->transport->socket->event.ctx);
465 if (c == NULL) return NULL;
467 state = talloc_zero(c, struct sesssetup_state);
468 if (composite_nomem(state, c)) return c;
469 c->private_data = state;
473 talloc_set_destructor(state, sesssetup_state_destructor);
475 /* no session setup at all in earliest protocol varients */
476 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
477 ZERO_STRUCT(io->out);
482 /* see what session setup interface we will use */
483 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
484 status = session_setup_old(c, session, io, &state->req);
485 } else if (!session->transport->options.use_spnego ||
486 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
487 status = session_setup_nt1(c, session, io, &state->req);
489 status = session_setup_spnego(c, session, io, &state->req);
492 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
493 NT_STATUS_IS_OK(status)) {
494 composite_continue_smb(c, state->req, request_handler, c);
498 composite_error(c, status);
504 receive a composite session setup reply
506 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
509 status = composite_wait(c);
515 sync version of smb_composite_sesssetup
517 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
519 struct composite_context *c = smb_composite_sesssetup_send(session, io);
520 return smb_composite_sesssetup_recv(c);