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 gensec_status;
39 struct smb_composite_sesssetup *io;
40 struct smbcli_request *req;
43 static int sesssetup_state_destructor(struct sesssetup_state *state)
46 talloc_free(state->req);
53 static NTSTATUS session_setup_old(struct composite_context *c,
54 struct smbcli_session *session,
55 struct smb_composite_sesssetup *io,
56 struct smbcli_request **req);
57 static NTSTATUS session_setup_nt1(struct composite_context *c,
58 struct smbcli_session *session,
59 struct smb_composite_sesssetup *io,
60 struct smbcli_request **req);
61 static NTSTATUS session_setup_spnego(struct composite_context *c,
62 struct smbcli_session *session,
63 struct smb_composite_sesssetup *io,
64 struct smbcli_request **req);
67 store the user session key for a transport
69 static void set_user_session_key(struct smbcli_session *session,
70 const DATA_BLOB *session_key)
72 session->user_session_key = data_blob_talloc(session,
78 handler for completion of a smbcli_request sub-request
80 static void request_handler(struct smbcli_request *req)
82 struct composite_context *c = (struct composite_context *)req->async.private;
83 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
84 struct smbcli_session *session = req->session;
85 DATA_BLOB session_key = data_blob(NULL, 0);
86 DATA_BLOB null_data_blob = data_blob(NULL, 0);
87 NTSTATUS session_key_err, nt_status;
88 struct smbcli_request *check_req = NULL;
90 if (req->sign_caller_checks) {
91 req->do_not_free = true;
95 c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
98 switch (state->setup.old.level) {
99 case RAW_SESSSETUP_OLD:
100 state->io->out.vuid = state->setup.old.out.vuid;
101 /* This doesn't work, as this only happens on old
102 * protocols, where this comparison won't match. */
103 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
104 /* we neet to reset the vuid for a new try */
106 if (cli_credentials_wrong_password(state->io->in.credentials)) {
107 nt_status = session_setup_old(c, session,
110 if (NT_STATUS_IS_OK(nt_status)) {
111 talloc_free(check_req);
112 c->status = nt_status;
113 composite_continue_smb(c, state->req, request_handler, c);
120 case RAW_SESSSETUP_NT1:
121 state->io->out.vuid = state->setup.nt1.out.vuid;
122 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
123 /* we neet to reset the vuid for a new try */
125 if (cli_credentials_wrong_password(state->io->in.credentials)) {
126 nt_status = session_setup_nt1(c, session,
129 if (NT_STATUS_IS_OK(nt_status)) {
130 talloc_free(check_req);
131 c->status = nt_status;
132 composite_continue_smb(c, state->req, request_handler, c);
139 case RAW_SESSSETUP_SPNEGO:
140 state->io->out.vuid = state->setup.spnego.out.vuid;
141 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
142 /* we need to reset the vuid for a new try */
144 if (cli_credentials_wrong_password(state->io->in.credentials)) {
145 nt_status = session_setup_spnego(c, session,
148 if (NT_STATUS_IS_OK(nt_status)) {
149 talloc_free(check_req);
150 c->status = nt_status;
151 composite_continue_smb(c, state->req, request_handler, c);
156 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
157 !NT_STATUS_IS_OK(c->status)) {
158 talloc_free(check_req);
161 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
163 /* The status value here, from the earlier pass at GENSEC is
164 * vital to the security of the system. Even if the other end
165 * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
166 * you must keep feeding it blobs, or else the remote
167 * host/attacker might avoid mutal authentication
170 state->gensec_status = gensec_update(session->gensec, state,
171 state->setup.spnego.out.secblob,
172 &state->setup.spnego.in.secblob);
173 c->status = state->gensec_status;
174 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
175 !NT_STATUS_IS_OK(c->status)) {
176 talloc_free(check_req);
180 state->setup.spnego.in.secblob = data_blob(NULL, 0);
183 /* we need to do another round of session setup. We keep going until both sides
185 session_key_err = gensec_session_key(session->gensec, &session_key);
186 if (NT_STATUS_IS_OK(session_key_err)) {
187 set_user_session_key(session, &session_key);
188 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
191 if (state->setup.spnego.in.secblob.length) {
193 check_req->sign_caller_checks = false;
194 if (!smbcli_request_check_sign_mac(check_req)) {
195 talloc_free(check_req);
196 c->status = NT_STATUS_ACCESS_DENIED;
199 talloc_free(check_req);
203 * set the session->vuid value only for calling
204 * smb_raw_sesssetup_send()
206 uint16_t vuid = session->vuid;
207 session->vuid = state->io->out.vuid;
208 state->req = smb_raw_sesssetup_send(session, &state->setup);
209 session->vuid = vuid;
210 composite_continue_smb(c, state->req, request_handler, c);
215 case RAW_SESSSETUP_SMB2:
216 c->status = NT_STATUS_INTERNAL_ERROR;
221 check_req->sign_caller_checks = false;
222 if (!smbcli_request_check_sign_mac(check_req)) {
223 c->status = NT_STATUS_ACCESS_DENIED;
225 talloc_free(check_req);
228 /* enforce the local signing required flag */
229 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
230 if (!session->transport->negotiate.sign_info.doing_signing
231 && session->transport->negotiate.sign_info.mandatory_signing) {
232 DEBUG(0, ("SMB signing required, but server does not support it\n"));
233 c->status = NT_STATUS_ACCESS_DENIED;
237 if (!NT_STATUS_IS_OK(c->status)) {
238 composite_error(c, c->status);
247 send a nt1 style session setup
249 static NTSTATUS session_setup_nt1(struct composite_context *c,
250 struct smbcli_session *session,
251 struct smb_composite_sesssetup *io,
252 struct smbcli_request **req)
255 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
256 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_iconv_convenience(global_loadparm), session->transport->socket->hostname, lp_workgroup(global_loadparm));
257 DATA_BLOB session_key;
258 int flags = CLI_CRED_NTLM_AUTH;
259 if (session->options.lanman_auth) {
260 flags |= CLI_CRED_LANMAN_AUTH;
263 if (session->options.ntlmv2_auth) {
264 flags |= CLI_CRED_NTLMv2_AUTH;
267 state->setup.nt1.level = RAW_SESSSETUP_NT1;
268 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
269 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
270 state->setup.nt1.in.vc_num = 1;
271 state->setup.nt1.in.sesskey = io->in.sesskey;
272 state->setup.nt1.in.capabilities = io->in.capabilities;
273 state->setup.nt1.in.os = "Unix";
274 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
276 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
277 &state->setup.nt1.in.user,
278 &state->setup.nt1.in.domain);
281 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
282 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
284 session->transport->negotiate.secblob,
286 &state->setup.nt1.in.password1,
287 &state->setup.nt1.in.password2,
289 NT_STATUS_NOT_OK_RETURN(nt_status);
291 smbcli_transport_simple_set_signing(session->transport, session_key,
292 state->setup.nt1.in.password2);
293 set_user_session_key(session, &session_key);
295 data_blob_free(&session_key);
296 } else if (session->options.plaintext_auth) {
297 const char *password = cli_credentials_get_password(io->in.credentials);
298 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
299 state->setup.nt1.in.password2 = data_blob(NULL, 0);
301 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
302 return NT_STATUS_INVALID_PARAMETER;
305 *req = smb_raw_sesssetup_send(session, &state->setup);
307 return NT_STATUS_NO_MEMORY;
309 return (*req)->status;
314 old style session setup (pre NT1 protocol level)
316 static NTSTATUS session_setup_old(struct composite_context *c,
317 struct smbcli_session *session,
318 struct smb_composite_sesssetup *io,
319 struct smbcli_request **req)
322 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
323 const char *password = cli_credentials_get_password(io->in.credentials);
324 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_iconv_convenience(global_loadparm), session->transport->socket->hostname, lp_workgroup(global_loadparm));
325 DATA_BLOB session_key;
327 if (session->options.lanman_auth) {
328 flags |= CLI_CRED_LANMAN_AUTH;
331 if (session->options.ntlmv2_auth) {
332 flags |= CLI_CRED_NTLMv2_AUTH;
335 state->setup.old.level = RAW_SESSSETUP_OLD;
336 state->setup.old.in.bufsize = session->transport->options.max_xmit;
337 state->setup.old.in.mpx_max = session->transport->options.max_mux;
338 state->setup.old.in.vc_num = 1;
339 state->setup.old.in.sesskey = io->in.sesskey;
340 state->setup.old.in.os = "Unix";
341 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
342 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
343 &state->setup.old.in.user,
344 &state->setup.old.in.domain);
346 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
347 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
349 session->transport->negotiate.secblob,
351 &state->setup.old.in.password,
354 NT_STATUS_NOT_OK_RETURN(nt_status);
355 set_user_session_key(session, &session_key);
357 data_blob_free(&session_key);
358 } else if (session->options.plaintext_auth) {
359 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
361 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
362 return NT_STATUS_INVALID_PARAMETER;
365 *req = smb_raw_sesssetup_send(session, &state->setup);
367 return NT_STATUS_NO_MEMORY;
369 return (*req)->status;
374 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
376 static NTSTATUS session_setup_spnego(struct composite_context *c,
377 struct smbcli_session *session,
378 struct smb_composite_sesssetup *io,
379 struct smbcli_request **req)
381 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
382 NTSTATUS status, session_key_err;
383 DATA_BLOB session_key = data_blob(NULL, 0);
384 DATA_BLOB null_data_blob = data_blob(NULL, 0);
385 const char *chosen_oid = NULL;
387 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
388 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
389 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
390 state->setup.spnego.in.vc_num = 1;
391 state->setup.spnego.in.sesskey = io->in.sesskey;
392 state->setup.spnego.in.capabilities = io->in.capabilities;
393 state->setup.spnego.in.os = "Unix";
394 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
395 state->setup.spnego.in.workgroup = io->in.workgroup;
397 smbcli_temp_set_signing(session->transport);
399 status = gensec_client_start(session, &session->gensec, c->event_ctx,
401 if (!NT_STATUS_IS_OK(status)) {
402 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
406 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
408 status = gensec_set_credentials(session->gensec, io->in.credentials);
409 if (!NT_STATUS_IS_OK(status)) {
410 DEBUG(1, ("Failed to start set GENSEC client credentials: %s\n",
415 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
416 if (!NT_STATUS_IS_OK(status)) {
417 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
422 status = gensec_set_target_service(session->gensec, "cifs");
423 if (!NT_STATUS_IS_OK(status)) {
424 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
429 if (session->transport->negotiate.secblob.length) {
430 chosen_oid = GENSEC_OID_SPNEGO;
431 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
432 if (!NT_STATUS_IS_OK(status)) {
433 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
434 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
435 chosen_oid = GENSEC_OID_NTLMSSP;
436 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
437 if (!NT_STATUS_IS_OK(status)) {
438 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
439 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
444 /* without a sec blob, means raw NTLMSSP */
445 chosen_oid = GENSEC_OID_NTLMSSP;
446 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
447 if (!NT_STATUS_IS_OK(status)) {
448 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
449 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
453 if ((const void *)chosen_oid == (const void *)GENSEC_OID_SPNEGO) {
454 status = gensec_update(session->gensec, state,
455 session->transport->negotiate.secblob,
456 &state->setup.spnego.in.secblob);
458 status = gensec_update(session->gensec, state,
460 &state->setup.spnego.in.secblob);
464 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
465 !NT_STATUS_IS_OK(status)) {
466 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
467 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
470 state->gensec_status = status;
472 session_key_err = gensec_session_key(session->gensec, &session_key);
473 if (NT_STATUS_IS_OK(session_key_err)) {
474 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
477 *req = smb_raw_sesssetup_send(session, &state->setup);
479 return NT_STATUS_NO_MEMORY;
482 if (NT_STATUS_IS_OK(session_key_err)) {
484 * as the response might be signed with a session key from within the response
485 * we need to check the signiture of the response ourself.
487 (*req)->sign_caller_checks = true;
490 return (*req)->status;
495 composite session setup function that hides the details of all the
496 different session setup varients, including the multi-pass nature of
499 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
500 struct smb_composite_sesssetup *io)
502 struct composite_context *c;
503 struct sesssetup_state *state;
506 c = composite_create(session, session->transport->socket->event.ctx);
507 if (c == NULL) return NULL;
509 state = talloc_zero(c, struct sesssetup_state);
510 if (composite_nomem(state, c)) return c;
511 c->private_data = state;
515 talloc_set_destructor(state, sesssetup_state_destructor);
517 /* no session setup at all in earliest protocol varients */
518 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
519 ZERO_STRUCT(io->out);
524 /* see what session setup interface we will use */
525 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
526 status = session_setup_old(c, session, io, &state->req);
527 } else if (!session->transport->options.use_spnego ||
528 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
529 status = session_setup_nt1(c, session, io, &state->req);
531 status = session_setup_spnego(c, session, io, &state->req);
534 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
535 NT_STATUS_IS_OK(status)) {
536 composite_continue_smb(c, state->req, request_handler, c);
540 composite_error(c, status);
546 receive a composite session setup reply
548 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
551 status = composite_wait(c);
557 sync version of smb_composite_sesssetup
559 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
561 struct composite_context *c = smb_composite_sesssetup_send(session, io);
562 return smb_composite_sesssetup_recv(c);