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"
32 #include "param/param.h"
34 struct sesssetup_state {
35 union smb_sesssetup setup;
36 NTSTATUS gensec_status;
37 struct smb_composite_sesssetup *io;
38 struct smbcli_request *req;
41 static int sesssetup_state_destructor(struct sesssetup_state *state)
44 talloc_free(state->req);
51 static NTSTATUS session_setup_old(struct composite_context *c,
52 struct smbcli_session *session,
53 struct smb_composite_sesssetup *io,
54 struct smbcli_request **req);
55 static NTSTATUS session_setup_nt1(struct composite_context *c,
56 struct smbcli_session *session,
57 struct smb_composite_sesssetup *io,
58 struct smbcli_request **req);
59 static NTSTATUS session_setup_spnego(struct composite_context *c,
60 struct smbcli_session *session,
61 struct smb_composite_sesssetup *io,
62 struct smbcli_request **req);
65 store the user session key for a transport
67 static void set_user_session_key(struct smbcli_session *session,
68 const DATA_BLOB *session_key)
70 session->user_session_key = data_blob_talloc(session,
76 handler for completion of a smbcli_request sub-request
78 static void request_handler(struct smbcli_request *req)
80 struct composite_context *c = (struct composite_context *)req->async.private;
81 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
82 struct smbcli_session *session = req->session;
83 DATA_BLOB session_key = data_blob(NULL, 0);
84 DATA_BLOB null_data_blob = data_blob(NULL, 0);
85 NTSTATUS session_key_err, nt_status;
87 c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
90 switch (state->setup.old.level) {
91 case RAW_SESSSETUP_OLD:
92 state->io->out.vuid = state->setup.old.out.vuid;
93 /* This doesn't work, as this only happens on old
94 * protocols, where this comparison won't match. */
95 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
96 /* we neet to reset the vuid for a new try */
98 if (cli_credentials_wrong_password(state->io->in.credentials)) {
99 nt_status = session_setup_old(c, session,
102 if (NT_STATUS_IS_OK(nt_status)) {
103 c->status = nt_status;
104 composite_continue_smb(c, state->req, request_handler, c);
111 case RAW_SESSSETUP_NT1:
112 state->io->out.vuid = state->setup.nt1.out.vuid;
113 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
114 /* we neet to reset the vuid for a new try */
116 if (cli_credentials_wrong_password(state->io->in.credentials)) {
117 nt_status = session_setup_nt1(c, session,
120 if (NT_STATUS_IS_OK(nt_status)) {
121 c->status = nt_status;
122 composite_continue_smb(c, state->req, request_handler, c);
129 case RAW_SESSSETUP_SPNEGO:
130 state->io->out.vuid = state->setup.spnego.out.vuid;
131 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
132 /* we need to reset the vuid for a new try */
134 if (cli_credentials_wrong_password(state->io->in.credentials)) {
135 nt_status = session_setup_spnego(c, session,
138 if (NT_STATUS_IS_OK(nt_status)) {
139 c->status = nt_status;
140 composite_continue_smb(c, state->req, request_handler, c);
145 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
146 !NT_STATUS_IS_OK(c->status)) {
149 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
151 /* The status value here, from the earlier pass at GENSEC is
152 * vital to the security of the system. Even if the other end
153 * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
154 * you must keep feeding it blobs, or else the remote
155 * host/attacker might avoid mutal authentication
158 state->gensec_status = gensec_update(session->gensec, state,
159 state->setup.spnego.out.secblob,
160 &state->setup.spnego.in.secblob);
161 c->status = state->gensec_status;
162 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
163 !NT_STATUS_IS_OK(c->status)) {
167 state->setup.spnego.in.secblob = data_blob(NULL, 0);
170 /* we need to do another round of session setup. We keep going until both sides
172 session_key_err = gensec_session_key(session->gensec, &session_key);
173 if (NT_STATUS_IS_OK(session_key_err)) {
174 set_user_session_key(session, &session_key);
175 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
178 if (state->setup.spnego.in.secblob.length) {
180 * set the session->vuid value only for calling
181 * smb_raw_sesssetup_send()
183 uint16_t vuid = session->vuid;
184 session->vuid = state->io->out.vuid;
185 state->req = smb_raw_sesssetup_send(session, &state->setup);
186 session->vuid = vuid;
187 composite_continue_smb(c, state->req, request_handler, c);
192 case RAW_SESSSETUP_SMB2:
193 c->status = NT_STATUS_INTERNAL_ERROR;
197 /* enforce the local signing required flag */
198 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
199 if (!session->transport->negotiate.sign_info.doing_signing
200 && session->transport->negotiate.sign_info.mandatory_signing) {
201 DEBUG(0, ("SMB signing required, but server does not support it\n"));
202 c->status = NT_STATUS_ACCESS_DENIED;
206 if (!NT_STATUS_IS_OK(c->status)) {
207 composite_error(c, c->status);
216 send a nt1 style session setup
218 static NTSTATUS session_setup_nt1(struct composite_context *c,
219 struct smbcli_session *session,
220 struct smb_composite_sesssetup *io,
221 struct smbcli_request **req)
224 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
225 const char *password = cli_credentials_get_password(io->in.credentials);
226 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup(global_loadparm));
227 DATA_BLOB session_key;
228 int flags = CLI_CRED_NTLM_AUTH;
229 if (lp_client_lanman_auth(global_loadparm)) {
230 flags |= CLI_CRED_LANMAN_AUTH;
233 if (lp_client_ntlmv2_auth(global_loadparm)) {
234 flags |= CLI_CRED_NTLMv2_AUTH;
237 state->setup.nt1.level = RAW_SESSSETUP_NT1;
238 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
239 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
240 state->setup.nt1.in.vc_num = 1;
241 state->setup.nt1.in.sesskey = io->in.sesskey;
242 state->setup.nt1.in.capabilities = io->in.capabilities;
243 state->setup.nt1.in.os = "Unix";
244 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
246 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
247 &state->setup.nt1.in.user,
248 &state->setup.nt1.in.domain);
251 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
252 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
254 session->transport->negotiate.secblob,
256 &state->setup.nt1.in.password1,
257 &state->setup.nt1.in.password2,
259 NT_STATUS_NOT_OK_RETURN(nt_status);
261 smbcli_transport_simple_set_signing(session->transport, session_key,
262 state->setup.nt1.in.password2);
263 set_user_session_key(session, &session_key);
265 data_blob_free(&session_key);
266 } else if (lp_client_plaintext_auth(global_loadparm)) {
267 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
268 state->setup.nt1.in.password2 = data_blob(NULL, 0);
270 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
271 return NT_STATUS_INVALID_PARAMETER;
274 *req = smb_raw_sesssetup_send(session, &state->setup);
276 return NT_STATUS_NO_MEMORY;
278 return (*req)->status;
283 old style session setup (pre NT1 protocol level)
285 static NTSTATUS session_setup_old(struct composite_context *c,
286 struct smbcli_session *session,
287 struct smb_composite_sesssetup *io,
288 struct smbcli_request **req)
291 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
292 const char *password = cli_credentials_get_password(io->in.credentials);
293 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup(global_loadparm));
294 DATA_BLOB session_key;
296 if (lp_client_lanman_auth(global_loadparm)) {
297 flags |= CLI_CRED_LANMAN_AUTH;
300 if (lp_client_ntlmv2_auth(global_loadparm)) {
301 flags |= CLI_CRED_NTLMv2_AUTH;
304 state->setup.old.level = RAW_SESSSETUP_OLD;
305 state->setup.old.in.bufsize = session->transport->options.max_xmit;
306 state->setup.old.in.mpx_max = session->transport->options.max_mux;
307 state->setup.old.in.vc_num = 1;
308 state->setup.old.in.sesskey = io->in.sesskey;
309 state->setup.old.in.os = "Unix";
310 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
311 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
312 &state->setup.old.in.user,
313 &state->setup.old.in.domain);
315 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
316 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
318 session->transport->negotiate.secblob,
320 &state->setup.old.in.password,
323 NT_STATUS_NOT_OK_RETURN(nt_status);
324 set_user_session_key(session, &session_key);
326 data_blob_free(&session_key);
327 } else if (lp_client_plaintext_auth(global_loadparm)) {
328 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
330 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
331 return NT_STATUS_INVALID_PARAMETER;
334 *req = smb_raw_sesssetup_send(session, &state->setup);
336 return NT_STATUS_NO_MEMORY;
338 return (*req)->status;
343 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
345 static NTSTATUS session_setup_spnego(struct composite_context *c,
346 struct smbcli_session *session,
347 struct smb_composite_sesssetup *io,
348 struct smbcli_request **req)
350 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
351 NTSTATUS status, session_key_err;
352 DATA_BLOB session_key = data_blob(NULL, 0);
353 DATA_BLOB null_data_blob = data_blob(NULL, 0);
354 const char *chosen_oid = NULL;
356 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
357 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
358 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
359 state->setup.spnego.in.vc_num = 1;
360 state->setup.spnego.in.sesskey = io->in.sesskey;
361 state->setup.spnego.in.capabilities = io->in.capabilities;
362 state->setup.spnego.in.os = "Unix";
363 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
364 state->setup.spnego.in.workgroup = io->in.workgroup;
366 smbcli_temp_set_signing(session->transport);
368 status = gensec_client_start(session, &session->gensec, c->event_ctx,
370 if (!NT_STATUS_IS_OK(status)) {
371 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
375 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
377 status = gensec_set_credentials(session->gensec, io->in.credentials);
378 if (!NT_STATUS_IS_OK(status)) {
379 DEBUG(1, ("Failed to start set GENSEC client credentials: %s\n",
384 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
385 if (!NT_STATUS_IS_OK(status)) {
386 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
391 status = gensec_set_target_service(session->gensec, "cifs");
392 if (!NT_STATUS_IS_OK(status)) {
393 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
398 if (session->transport->negotiate.secblob.length) {
399 chosen_oid = GENSEC_OID_SPNEGO;
400 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
401 if (!NT_STATUS_IS_OK(status)) {
402 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
403 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
404 chosen_oid = GENSEC_OID_NTLMSSP;
405 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
406 if (!NT_STATUS_IS_OK(status)) {
407 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
408 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
413 /* without a sec blob, means raw NTLMSSP */
414 chosen_oid = GENSEC_OID_NTLMSSP;
415 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
416 if (!NT_STATUS_IS_OK(status)) {
417 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
418 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
422 if ((const void *)chosen_oid == (const void *)GENSEC_OID_SPNEGO) {
423 status = gensec_update(session->gensec, state,
424 session->transport->negotiate.secblob,
425 &state->setup.spnego.in.secblob);
427 status = gensec_update(session->gensec, state,
429 &state->setup.spnego.in.secblob);
433 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
434 !NT_STATUS_IS_OK(status)) {
435 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
436 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
439 state->gensec_status = status;
441 session_key_err = gensec_session_key(session->gensec, &session_key);
442 if (NT_STATUS_IS_OK(session_key_err)) {
443 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
446 *req = smb_raw_sesssetup_send(session, &state->setup);
448 return NT_STATUS_NO_MEMORY;
450 return (*req)->status;
455 composite session setup function that hides the details of all the
456 different session setup varients, including the multi-pass nature of
459 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
460 struct smb_composite_sesssetup *io)
462 struct composite_context *c;
463 struct sesssetup_state *state;
466 c = composite_create(session, session->transport->socket->event.ctx);
467 if (c == NULL) return NULL;
469 state = talloc_zero(c, struct sesssetup_state);
470 if (composite_nomem(state, c)) return c;
471 c->private_data = state;
475 talloc_set_destructor(state, sesssetup_state_destructor);
477 /* no session setup at all in earliest protocol varients */
478 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
479 ZERO_STRUCT(io->out);
484 /* see what session setup interface we will use */
485 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
486 status = session_setup_old(c, session, io, &state->req);
487 } else if (!session->transport->options.use_spnego ||
488 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
489 status = session_setup_nt1(c, session, io, &state->req);
491 status = session_setup_spnego(c, session, io, &state->req);
494 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
495 NT_STATUS_IS_OK(status)) {
496 composite_continue_smb(c, state->req, request_handler, c);
500 composite_error(c, status);
506 receive a composite session setup reply
508 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
511 status = composite_wait(c);
517 sync version of smb_composite_sesssetup
519 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
521 struct composite_context *c = smb_composite_sesssetup_send(session, io);
522 return smb_composite_sesssetup_recv(c);