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;
89 c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
92 switch (state->setup.old.level) {
93 case RAW_SESSSETUP_OLD:
94 state->io->out.vuid = state->setup.old.out.vuid;
95 /* This doesn't work, as this only happens on old
96 * protocols, where this comparison won't match. */
97 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
98 /* we neet to reset the vuid for a new try */
100 if (cli_credentials_wrong_password(state->io->in.credentials)) {
101 nt_status = session_setup_old(c, session,
104 if (NT_STATUS_IS_OK(nt_status)) {
105 c->status = nt_status;
106 composite_continue_smb(c, state->req, request_handler, c);
113 case RAW_SESSSETUP_NT1:
114 state->io->out.vuid = state->setup.nt1.out.vuid;
115 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
116 /* we neet to reset the vuid for a new try */
118 if (cli_credentials_wrong_password(state->io->in.credentials)) {
119 nt_status = session_setup_nt1(c, session,
122 if (NT_STATUS_IS_OK(nt_status)) {
123 c->status = nt_status;
124 composite_continue_smb(c, state->req, request_handler, c);
131 case RAW_SESSSETUP_SPNEGO:
132 state->io->out.vuid = state->setup.spnego.out.vuid;
133 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
134 /* we need to reset the vuid for a new try */
136 if (cli_credentials_wrong_password(state->io->in.credentials)) {
137 nt_status = session_setup_spnego(c, session,
140 if (NT_STATUS_IS_OK(nt_status)) {
141 c->status = nt_status;
142 composite_continue_smb(c, state->req, request_handler, c);
147 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
148 !NT_STATUS_IS_OK(c->status)) {
151 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
153 /* The status value here, from the earlier pass at GENSEC is
154 * vital to the security of the system. Even if the other end
155 * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
156 * you must keep feeding it blobs, or else the remote
157 * host/attacker might avoid mutal authentication
160 state->gensec_status = gensec_update(session->gensec, state,
161 state->setup.spnego.out.secblob,
162 &state->setup.spnego.in.secblob);
163 c->status = state->gensec_status;
164 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
165 !NT_STATUS_IS_OK(c->status)) {
169 state->setup.spnego.in.secblob = data_blob(NULL, 0);
172 /* we need to do another round of session setup. We keep going until both sides
174 session_key_err = gensec_session_key(session->gensec, &session_key);
175 if (NT_STATUS_IS_OK(session_key_err)) {
176 set_user_session_key(session, &session_key);
177 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
180 if (state->setup.spnego.in.secblob.length) {
182 * set the session->vuid value only for calling
183 * smb_raw_sesssetup_send()
185 uint16_t vuid = session->vuid;
186 session->vuid = state->io->out.vuid;
187 state->req = smb_raw_sesssetup_send(session, &state->setup);
188 session->vuid = vuid;
189 composite_continue_smb(c, state->req, request_handler, c);
194 case RAW_SESSSETUP_SMB2:
195 c->status = NT_STATUS_INTERNAL_ERROR;
199 /* enforce the local signing required flag */
200 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
201 if (!session->transport->negotiate.sign_info.doing_signing
202 && session->transport->negotiate.sign_info.mandatory_signing) {
203 DEBUG(0, ("SMB signing required, but server does not support it\n"));
204 c->status = NT_STATUS_ACCESS_DENIED;
208 if (!NT_STATUS_IS_OK(c->status)) {
209 composite_error(c, c->status);
218 send a nt1 style session setup
220 static NTSTATUS session_setup_nt1(struct composite_context *c,
221 struct smbcli_session *session,
222 struct smb_composite_sesssetup *io,
223 struct smbcli_request **req)
226 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
227 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_iconv_convenience(global_loadparm), session->transport->socket->hostname, lp_workgroup(global_loadparm));
228 DATA_BLOB session_key;
229 int flags = CLI_CRED_NTLM_AUTH;
230 if (session->options.lanman_auth) {
231 flags |= CLI_CRED_LANMAN_AUTH;
234 if (session->options.ntlmv2_auth) {
235 flags |= CLI_CRED_NTLMv2_AUTH;
238 state->setup.nt1.level = RAW_SESSSETUP_NT1;
239 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
240 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
241 state->setup.nt1.in.vc_num = 1;
242 state->setup.nt1.in.sesskey = io->in.sesskey;
243 state->setup.nt1.in.capabilities = io->in.capabilities;
244 state->setup.nt1.in.os = "Unix";
245 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
247 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
248 &state->setup.nt1.in.user,
249 &state->setup.nt1.in.domain);
252 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
253 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
255 session->transport->negotiate.secblob,
257 &state->setup.nt1.in.password1,
258 &state->setup.nt1.in.password2,
260 NT_STATUS_NOT_OK_RETURN(nt_status);
262 smbcli_transport_simple_set_signing(session->transport, session_key,
263 state->setup.nt1.in.password2);
264 set_user_session_key(session, &session_key);
266 data_blob_free(&session_key);
267 } else if (session->options.plaintext_auth) {
268 const char *password = cli_credentials_get_password(io->in.credentials);
269 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
270 state->setup.nt1.in.password2 = data_blob(NULL, 0);
272 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
273 return NT_STATUS_INVALID_PARAMETER;
276 *req = smb_raw_sesssetup_send(session, &state->setup);
278 return NT_STATUS_NO_MEMORY;
280 return (*req)->status;
285 old style session setup (pre NT1 protocol level)
287 static NTSTATUS session_setup_old(struct composite_context *c,
288 struct smbcli_session *session,
289 struct smb_composite_sesssetup *io,
290 struct smbcli_request **req)
293 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
294 const char *password = cli_credentials_get_password(io->in.credentials);
295 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_iconv_convenience(global_loadparm), session->transport->socket->hostname, lp_workgroup(global_loadparm));
296 DATA_BLOB session_key;
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.old.level = RAW_SESSSETUP_OLD;
307 state->setup.old.in.bufsize = session->transport->options.max_xmit;
308 state->setup.old.in.mpx_max = session->transport->options.max_mux;
309 state->setup.old.in.vc_num = 1;
310 state->setup.old.in.sesskey = io->in.sesskey;
311 state->setup.old.in.os = "Unix";
312 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
313 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
314 &state->setup.old.in.user,
315 &state->setup.old.in.domain);
317 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
318 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
320 session->transport->negotiate.secblob,
322 &state->setup.old.in.password,
325 NT_STATUS_NOT_OK_RETURN(nt_status);
326 set_user_session_key(session, &session_key);
328 data_blob_free(&session_key);
329 } else if (session->options.plaintext_auth) {
330 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
332 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
333 return NT_STATUS_INVALID_PARAMETER;
336 *req = smb_raw_sesssetup_send(session, &state->setup);
338 return NT_STATUS_NO_MEMORY;
340 return (*req)->status;
345 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
347 static NTSTATUS session_setup_spnego(struct composite_context *c,
348 struct smbcli_session *session,
349 struct smb_composite_sesssetup *io,
350 struct smbcli_request **req)
352 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
353 NTSTATUS status, session_key_err;
354 DATA_BLOB session_key = data_blob(NULL, 0);
355 DATA_BLOB null_data_blob = data_blob(NULL, 0);
356 const char *chosen_oid = NULL;
358 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
359 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
360 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
361 state->setup.spnego.in.vc_num = 1;
362 state->setup.spnego.in.sesskey = io->in.sesskey;
363 state->setup.spnego.in.capabilities = io->in.capabilities;
364 state->setup.spnego.in.os = "Unix";
365 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
366 state->setup.spnego.in.workgroup = io->in.workgroup;
368 smbcli_temp_set_signing(session->transport);
370 status = gensec_client_start(session, &session->gensec, c->event_ctx,
372 if (!NT_STATUS_IS_OK(status)) {
373 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
377 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
379 status = gensec_set_credentials(session->gensec, io->in.credentials);
380 if (!NT_STATUS_IS_OK(status)) {
381 DEBUG(1, ("Failed to start set GENSEC client credentials: %s\n",
386 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
387 if (!NT_STATUS_IS_OK(status)) {
388 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
393 status = gensec_set_target_service(session->gensec, "cifs");
394 if (!NT_STATUS_IS_OK(status)) {
395 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
400 if (session->transport->negotiate.secblob.length) {
401 chosen_oid = GENSEC_OID_SPNEGO;
402 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
403 if (!NT_STATUS_IS_OK(status)) {
404 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
405 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
406 chosen_oid = GENSEC_OID_NTLMSSP;
407 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
408 if (!NT_STATUS_IS_OK(status)) {
409 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
410 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
415 /* without a sec blob, means raw NTLMSSP */
416 chosen_oid = GENSEC_OID_NTLMSSP;
417 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
418 if (!NT_STATUS_IS_OK(status)) {
419 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
420 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
424 if ((const void *)chosen_oid == (const void *)GENSEC_OID_SPNEGO) {
425 status = gensec_update(session->gensec, state,
426 session->transport->negotiate.secblob,
427 &state->setup.spnego.in.secblob);
429 status = gensec_update(session->gensec, state,
431 &state->setup.spnego.in.secblob);
435 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
436 !NT_STATUS_IS_OK(status)) {
437 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
438 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
441 state->gensec_status = status;
443 session_key_err = gensec_session_key(session->gensec, &session_key);
444 if (NT_STATUS_IS_OK(session_key_err)) {
445 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
448 *req = smb_raw_sesssetup_send(session, &state->setup);
450 return NT_STATUS_NO_MEMORY;
452 return (*req)->status;
457 composite session setup function that hides the details of all the
458 different session setup varients, including the multi-pass nature of
461 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
462 struct smb_composite_sesssetup *io)
464 struct composite_context *c;
465 struct sesssetup_state *state;
468 c = composite_create(session, session->transport->socket->event.ctx);
469 if (c == NULL) return NULL;
471 state = talloc_zero(c, struct sesssetup_state);
472 if (composite_nomem(state, c)) return c;
473 c->private_data = state;
477 talloc_set_destructor(state, sesssetup_state_destructor);
479 /* no session setup at all in earliest protocol varients */
480 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
481 ZERO_STRUCT(io->out);
486 /* see what session setup interface we will use */
487 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
488 status = session_setup_old(c, session, io, &state->req);
489 } else if (!session->transport->options.use_spnego ||
490 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
491 status = session_setup_nt1(c, session, io, &state->req);
493 status = session_setup_spnego(c, session, io, &state->req);
496 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
497 NT_STATUS_IS_OK(status)) {
498 composite_continue_smb(c, state->req, request_handler, c);
502 composite_error(c, status);
508 receive a composite session setup reply
510 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
513 status = composite_wait(c);
519 sync version of smb_composite_sesssetup
521 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
523 struct composite_context *c = smb_composite_sesssetup_send(session, io);
524 return smb_composite_sesssetup_recv(c);