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/raw/raw_proto.h"
26 #include "libcli/composite/composite.h"
27 #include "libcli/smb_composite/smb_composite.h"
28 #include "libcli/smb_composite/proto.h"
29 #include "libcli/auth/libcli_auth.h"
30 #include "auth/auth.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/credentials/credentials.h"
34 #include "param/param.h"
36 struct sesssetup_state {
37 union smb_sesssetup setup;
38 NTSTATUS remote_status;
39 NTSTATUS gensec_status;
40 struct smb_composite_sesssetup *io;
41 struct smbcli_request *req;
44 static int sesssetup_state_destructor(struct sesssetup_state *state)
47 talloc_free(state->req);
54 static NTSTATUS session_setup_old(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_nt1(struct composite_context *c,
59 struct smbcli_session *session,
60 struct smb_composite_sesssetup *io,
61 struct smbcli_request **req);
62 static NTSTATUS session_setup_spnego(struct composite_context *c,
63 struct smbcli_session *session,
64 struct smb_composite_sesssetup *io,
65 struct smbcli_request **req);
68 store the user session key for a transport
70 static void set_user_session_key(struct smbcli_session *session,
71 const DATA_BLOB *session_key)
73 session->user_session_key = data_blob_talloc(session,
79 handler for completion of a smbcli_request sub-request
81 static void request_handler(struct smbcli_request *req)
83 struct composite_context *c = (struct composite_context *)req->async.private_data;
84 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
85 struct smbcli_session *session = req->session;
86 DATA_BLOB session_key = data_blob(NULL, 0);
87 DATA_BLOB null_data_blob = data_blob(NULL, 0);
88 NTSTATUS session_key_err, nt_status;
89 struct smbcli_request *check_req = NULL;
90 const char *os = NULL;
91 const char *lanman = NULL;
93 if (req->sign_caller_checks) {
94 req->do_not_free = true;
98 state->remote_status = smb_raw_sesssetup_recv(req, state, &state->setup);
99 c->status = state->remote_status;
103 * we only need to check the signature if the
104 * NT_STATUS_OK is returned
106 if (!NT_STATUS_IS_OK(state->remote_status)) {
107 talloc_free(check_req);
111 switch (state->setup.old.level) {
112 case RAW_SESSSETUP_OLD:
113 state->io->out.vuid = state->setup.old.out.vuid;
114 /* This doesn't work, as this only happens on old
115 * protocols, where this comparison won't match. */
116 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
117 /* we neet to reset the vuid for a new try */
119 if (cli_credentials_wrong_password(state->io->in.credentials)) {
120 nt_status = session_setup_old(c, session,
123 if (NT_STATUS_IS_OK(nt_status)) {
124 talloc_free(check_req);
125 c->status = nt_status;
126 composite_continue_smb(c, state->req, request_handler, c);
131 os = state->setup.old.out.os;
132 lanman = state->setup.old.out.lanman;
135 case RAW_SESSSETUP_NT1:
136 state->io->out.vuid = state->setup.nt1.out.vuid;
137 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
138 /* we neet to reset the vuid for a new try */
140 if (cli_credentials_wrong_password(state->io->in.credentials)) {
141 nt_status = session_setup_nt1(c, session,
144 if (NT_STATUS_IS_OK(nt_status)) {
145 talloc_free(check_req);
146 c->status = nt_status;
147 composite_continue_smb(c, state->req, request_handler, c);
152 os = state->setup.nt1.out.os;
153 lanman = state->setup.nt1.out.lanman;
156 case RAW_SESSSETUP_SPNEGO:
157 state->io->out.vuid = state->setup.spnego.out.vuid;
158 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
159 /* we need to reset the vuid for a new try */
161 if (cli_credentials_wrong_password(state->io->in.credentials)) {
162 nt_status = session_setup_spnego(c, session,
165 if (NT_STATUS_IS_OK(nt_status)) {
166 talloc_free(check_req);
167 c->status = nt_status;
168 composite_continue_smb(c, state->req, request_handler, c);
173 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
174 !NT_STATUS_IS_OK(c->status)) {
177 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
179 /* The status value here, from the earlier pass at GENSEC is
180 * vital to the security of the system. Even if the other end
181 * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
182 * you must keep feeding it blobs, or else the remote
183 * host/attacker might avoid mutal authentication
186 state->gensec_status = gensec_update(session->gensec, state,
187 state->setup.spnego.out.secblob,
188 &state->setup.spnego.in.secblob);
189 c->status = state->gensec_status;
190 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
191 !NT_STATUS_IS_OK(c->status)) {
195 state->setup.spnego.in.secblob = data_blob(NULL, 0);
198 if (NT_STATUS_IS_OK(state->remote_status)) {
199 if (state->setup.spnego.in.secblob.length) {
200 c->status = NT_STATUS_INTERNAL_ERROR;
203 session_key_err = gensec_session_key(session->gensec, session, &session->user_session_key);
204 if (NT_STATUS_IS_OK(session_key_err)) {
205 smbcli_transport_simple_set_signing(session->transport, session->user_session_key, null_data_blob);
209 if (state->setup.spnego.in.secblob.length) {
211 * set the session->vuid value only for calling
212 * smb_raw_sesssetup_send()
214 uint16_t vuid = session->vuid;
215 session->vuid = state->io->out.vuid;
216 state->req = smb_raw_sesssetup_send(session, &state->setup);
217 session->vuid = vuid;
219 state->req->sign_caller_checks = true;
221 composite_continue_smb(c, state->req, request_handler, c);
224 os = state->setup.spnego.out.os;
225 lanman = state->setup.spnego.out.lanman;
228 case RAW_SESSSETUP_SMB2:
229 c->status = NT_STATUS_INTERNAL_ERROR;
234 check_req->sign_caller_checks = false;
235 if (!smbcli_request_check_sign_mac(check_req)) {
236 c->status = NT_STATUS_ACCESS_DENIED;
238 talloc_free(check_req);
242 /* enforce the local signing required flag */
243 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
244 if (!session->transport->negotiate.sign_info.doing_signing
245 && session->transport->negotiate.sign_info.mandatory_signing) {
246 DEBUG(0, ("SMB signing required, but server does not support it\n"));
247 c->status = NT_STATUS_ACCESS_DENIED;
251 if (!NT_STATUS_IS_OK(c->status)) {
252 composite_error(c, c->status);
257 session->os = talloc_strdup(session, os);
258 if (composite_nomem(session->os, c)) return;
263 session->lanman = talloc_strdup(session, lanman);
264 if (composite_nomem(session->lanman, c)) return;
266 session->lanman = NULL;
274 send a nt1 style session setup
276 static NTSTATUS session_setup_nt1(struct composite_context *c,
277 struct smbcli_session *session,
278 struct smb_composite_sesssetup *io,
279 struct smbcli_request **req)
281 NTSTATUS nt_status = NT_STATUS_INTERNAL_ERROR;
282 struct sesssetup_state *state = talloc_get_type(c->private_data,
283 struct sesssetup_state);
284 const char *domain = cli_credentials_get_domain(io->in.credentials);
287 * domain controllers tend to reject the NTLM v2 blob
288 * if the netbiosname is not valid (e.g. IP address or FQDN)
289 * so just leave it away (as Windows client do)
291 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, NULL, domain);
293 DATA_BLOB session_key = data_blob(NULL, 0);
294 int flags = CLI_CRED_NTLM_AUTH;
296 smbcli_temp_set_signing(session->transport);
298 if (session->options.lanman_auth) {
299 flags |= CLI_CRED_LANMAN_AUTH;
302 if (session->options.ntlmv2_auth) {
303 flags |= CLI_CRED_NTLMv2_AUTH;
306 state->setup.nt1.level = RAW_SESSSETUP_NT1;
307 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
308 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
309 state->setup.nt1.in.vc_num = 1;
310 state->setup.nt1.in.sesskey = io->in.sesskey;
311 state->setup.nt1.in.capabilities = io->in.capabilities;
312 state->setup.nt1.in.os = "Unix";
313 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
315 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
316 &state->setup.nt1.in.user,
317 &state->setup.nt1.in.domain);
320 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
321 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
323 session->transport->negotiate.secblob,
325 &state->setup.nt1.in.password1,
326 &state->setup.nt1.in.password2,
328 NT_STATUS_NOT_OK_RETURN(nt_status);
329 } else if (session->options.plaintext_auth) {
330 const char *password = cli_credentials_get_password(io->in.credentials);
331 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
332 state->setup.nt1.in.password2 = data_blob(NULL, 0);
334 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
335 return NT_STATUS_INVALID_PARAMETER;
338 *req = smb_raw_sesssetup_send(session, &state->setup);
340 return NT_STATUS_NO_MEMORY;
343 if (NT_STATUS_IS_OK(nt_status)) {
344 smbcli_transport_simple_set_signing(session->transport, session_key,
345 state->setup.nt1.in.password2);
346 set_user_session_key(session, &session_key);
348 data_blob_free(&session_key);
351 return (*req)->status;
356 old style session setup (pre NT1 protocol level)
358 static NTSTATUS session_setup_old(struct composite_context *c,
359 struct smbcli_session *session,
360 struct smb_composite_sesssetup *io,
361 struct smbcli_request **req)
364 struct sesssetup_state *state = talloc_get_type(c->private_data,
365 struct sesssetup_state);
366 const char *password = cli_credentials_get_password(io->in.credentials);
367 const char *domain = cli_credentials_get_domain(io->in.credentials);
370 * domain controllers tend to reject the NTLM v2 blob
371 * if the netbiosname is not valid (e.g. IP address or FQDN)
372 * so just leave it away (as Windows client do)
374 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, NULL, domain);
376 DATA_BLOB session_key;
378 if (session->options.lanman_auth) {
379 flags |= CLI_CRED_LANMAN_AUTH;
382 if (session->options.ntlmv2_auth) {
383 flags |= CLI_CRED_NTLMv2_AUTH;
386 state->setup.old.level = RAW_SESSSETUP_OLD;
387 state->setup.old.in.bufsize = session->transport->options.max_xmit;
388 state->setup.old.in.mpx_max = session->transport->options.max_mux;
389 state->setup.old.in.vc_num = 1;
390 state->setup.old.in.sesskey = io->in.sesskey;
391 state->setup.old.in.os = "Unix";
392 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
393 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
394 &state->setup.old.in.user,
395 &state->setup.old.in.domain);
397 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
398 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
400 session->transport->negotiate.secblob,
402 &state->setup.old.in.password,
405 NT_STATUS_NOT_OK_RETURN(nt_status);
406 set_user_session_key(session, &session_key);
408 data_blob_free(&session_key);
409 } else if (session->options.plaintext_auth) {
410 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
412 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
413 return NT_STATUS_INVALID_PARAMETER;
416 *req = smb_raw_sesssetup_send(session, &state->setup);
418 return NT_STATUS_NO_MEMORY;
420 return (*req)->status;
425 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
427 static NTSTATUS session_setup_spnego(struct composite_context *c,
428 struct smbcli_session *session,
429 struct smb_composite_sesssetup *io,
430 struct smbcli_request **req)
432 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
434 const char *chosen_oid = NULL;
436 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
437 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
438 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
439 state->setup.spnego.in.vc_num = 1;
440 state->setup.spnego.in.sesskey = io->in.sesskey;
441 state->setup.spnego.in.capabilities = io->in.capabilities;
442 state->setup.spnego.in.os = "Unix";
443 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
444 state->setup.spnego.in.workgroup = io->in.workgroup;
446 smbcli_temp_set_signing(session->transport);
448 status = gensec_client_start(session, &session->gensec, c->event_ctx,
449 io->in.gensec_settings);
450 if (!NT_STATUS_IS_OK(status)) {
451 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
455 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
457 status = gensec_set_credentials(session->gensec, io->in.credentials);
458 if (!NT_STATUS_IS_OK(status)) {
459 DEBUG(1, ("Failed to start set GENSEC client credentials: %s\n",
464 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
465 if (!NT_STATUS_IS_OK(status)) {
466 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
471 status = gensec_set_target_service(session->gensec, "cifs");
472 if (!NT_STATUS_IS_OK(status)) {
473 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
478 if (session->transport->negotiate.secblob.length) {
479 chosen_oid = GENSEC_OID_SPNEGO;
480 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
481 if (!NT_STATUS_IS_OK(status)) {
482 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
483 gensec_get_name_by_oid(session->gensec, chosen_oid), nt_errstr(status)));
484 chosen_oid = GENSEC_OID_NTLMSSP;
485 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
486 if (!NT_STATUS_IS_OK(status)) {
487 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
488 gensec_get_name_by_oid(session->gensec, chosen_oid),
494 /* without a sec blob, means raw NTLMSSP */
495 chosen_oid = GENSEC_OID_NTLMSSP;
496 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
497 if (!NT_STATUS_IS_OK(status)) {
498 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
499 gensec_get_name_by_oid(session->gensec, chosen_oid), nt_errstr(status)));
503 if ((const void *)chosen_oid == (const void *)GENSEC_OID_SPNEGO) {
504 status = gensec_update(session->gensec, state,
505 session->transport->negotiate.secblob,
506 &state->setup.spnego.in.secblob);
508 status = gensec_update(session->gensec, state,
510 &state->setup.spnego.in.secblob);
514 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
515 !NT_STATUS_IS_OK(status)) {
516 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
517 gensec_get_name_by_oid(session->gensec, chosen_oid),
521 state->gensec_status = status;
523 *req = smb_raw_sesssetup_send(session, &state->setup);
525 return NT_STATUS_NO_MEMORY;
529 * we need to check the signature ourself
530 * as the session key might be the acceptor subkey
531 * which comes within the response itself
533 (*req)->sign_caller_checks = true;
535 return (*req)->status;
540 composite session setup function that hides the details of all the
541 different session setup varients, including the multi-pass nature of
544 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
545 struct smb_composite_sesssetup *io)
547 struct composite_context *c;
548 struct sesssetup_state *state;
551 c = composite_create(session, session->transport->socket->event.ctx);
552 if (c == NULL) return NULL;
554 state = talloc_zero(c, struct sesssetup_state);
555 if (composite_nomem(state, c)) return c;
556 c->private_data = state;
560 talloc_set_destructor(state, sesssetup_state_destructor);
562 /* no session setup at all in earliest protocol varients */
563 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
564 ZERO_STRUCT(io->out);
569 /* see what session setup interface we will use */
570 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
571 status = session_setup_old(c, session, io, &state->req);
572 } else if (!session->transport->options.use_spnego ||
573 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
574 status = session_setup_nt1(c, session, io, &state->req);
576 status = session_setup_spnego(c, session, io, &state->req);
579 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
580 NT_STATUS_IS_OK(status)) {
581 composite_continue_smb(c, state->req, request_handler, c);
585 composite_error(c, status);
591 receive a composite session setup reply
593 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
596 status = composite_wait(c);
602 sync version of smb_composite_sesssetup
604 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
606 struct composite_context *c = smb_composite_sesssetup_send(session, io);
607 return smb_composite_sesssetup_recv(c);