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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 a composite API for making handling a generic async session setup
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27 #include "libcli/smb_composite/smb_composite.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "auth/auth.h"
32 struct sesssetup_state {
33 union smb_sesssetup setup;
34 NTSTATUS gensec_status;
35 struct smb_composite_sesssetup *io;
36 struct smbcli_request *req;
39 static NTSTATUS session_setup_old(struct composite_context *c,
40 struct smbcli_session *session,
41 struct smb_composite_sesssetup *io,
42 struct smbcli_request **req);
43 static NTSTATUS session_setup_nt1(struct composite_context *c,
44 struct smbcli_session *session,
45 struct smb_composite_sesssetup *io,
46 struct smbcli_request **req);
47 static NTSTATUS session_setup_spnego(struct composite_context *c,
48 struct smbcli_session *session,
49 struct smb_composite_sesssetup *io,
50 struct smbcli_request **req);
53 store the user session key for a transport
55 static void set_user_session_key(struct smbcli_session *session,
56 const DATA_BLOB *session_key)
58 session->user_session_key = data_blob_talloc(session,
64 handler for completion of a smbcli_request sub-request
66 static void request_handler(struct smbcli_request *req)
68 struct composite_context *c = req->async.private;
69 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
70 struct smbcli_session *session = req->session;
71 DATA_BLOB session_key = data_blob(NULL, 0);
72 DATA_BLOB null_data_blob = data_blob(NULL, 0);
73 NTSTATUS session_key_err, nt_status;
75 c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
77 switch (state->setup.old.level) {
78 case RAW_SESSSETUP_OLD:
79 state->io->out.vuid = state->setup.old.out.vuid;
80 /* This doesn't work, as this only happens on old
81 * protocols, where this comparison won't match. */
82 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
83 /* we neet to reset the vuid for a new try */
85 if (cli_credentials_wrong_password(state->io->in.credentials)) {
86 nt_status = session_setup_old(c, session,
89 if (NT_STATUS_IS_OK(nt_status)) {
90 c->status = nt_status;
91 state->req->async.fn = request_handler;
92 state->req->async.private = c;
99 case RAW_SESSSETUP_NT1:
100 state->io->out.vuid = state->setup.nt1.out.vuid;
101 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
102 /* we neet to reset the vuid for a new try */
104 if (cli_credentials_wrong_password(state->io->in.credentials)) {
105 nt_status = session_setup_nt1(c, session,
108 if (NT_STATUS_IS_OK(nt_status)) {
109 c->status = nt_status;
110 state->req->async.fn = request_handler;
111 state->req->async.private = c;
118 case RAW_SESSSETUP_SPNEGO:
119 state->io->out.vuid = state->setup.spnego.out.vuid;
120 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
121 /* we neet to reset the vuid for a new try */
123 if (cli_credentials_wrong_password(state->io->in.credentials)) {
124 nt_status = session_setup_spnego(c, session,
127 if (NT_STATUS_IS_OK(nt_status)) {
128 c->status = nt_status;
129 state->req->async.fn = request_handler;
130 state->req->async.private = c;
135 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
136 !NT_STATUS_IS_OK(c->status)) {
139 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
141 /* The status value here, from the earlier pass at GENSEC is
142 * vital to the security of the system. Even if the other end
143 * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
144 * you must keep feeding it blobs, or else the remote
145 * host/attacker might avoid mutal authentication
148 state->gensec_status = gensec_update(session->gensec, state,
149 state->setup.spnego.out.secblob,
150 &state->setup.spnego.in.secblob);
151 c->status = state->gensec_status;
152 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
153 !NT_STATUS_IS_OK(c->status)) {
157 state->setup.spnego.in.secblob = data_blob(NULL, 0);
160 /* we need to do another round of session setup. We keep going until both sides
162 session_key_err = gensec_session_key(session->gensec, &session_key);
163 if (NT_STATUS_IS_OK(session_key_err)) {
164 set_user_session_key(session, &session_key);
165 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
168 if (state->setup.spnego.in.secblob.length) {
170 * set the session->vuid value only for calling
171 * smb_raw_sesssetup_send()
173 uint16_t vuid = session->vuid;
174 session->vuid = state->io->out.vuid;
175 state->req = smb_raw_sesssetup_send(session, &state->setup);
176 session->vuid = vuid;
177 state->req->async.fn = request_handler;
178 state->req->async.private = c;
183 case RAW_SESSSETUP_SMB2:
184 c->status = NT_STATUS_INTERNAL_ERROR;
188 /* enforce the local signing required flag */
189 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
190 if (!session->transport->negotiate.sign_info.doing_signing
191 && session->transport->negotiate.sign_info.mandatory_signing) {
192 DEBUG(0, ("SMB signing required, but server does not support it\n"));
193 c->status = NT_STATUS_ACCESS_DENIED;
197 if (NT_STATUS_IS_OK(c->status)) {
198 c->state = COMPOSITE_STATE_DONE;
200 c->state = COMPOSITE_STATE_ERROR;
209 send a nt1 style session setup
211 static NTSTATUS session_setup_nt1(struct composite_context *c,
212 struct smbcli_session *session,
213 struct smb_composite_sesssetup *io,
214 struct smbcli_request **req)
217 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
218 const char *password = cli_credentials_get_password(io->in.credentials);
219 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
220 DATA_BLOB session_key;
221 int flags = CLI_CRED_NTLM_AUTH;
222 if (lp_client_lanman_auth()) {
223 flags |= CLI_CRED_LANMAN_AUTH;
226 if (lp_client_ntlmv2_auth()) {
227 flags |= CLI_CRED_NTLMv2_AUTH;
230 state->setup.nt1.level = RAW_SESSSETUP_NT1;
231 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
232 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
233 state->setup.nt1.in.vc_num = 1;
234 state->setup.nt1.in.sesskey = io->in.sesskey;
235 state->setup.nt1.in.capabilities = io->in.capabilities;
236 state->setup.nt1.in.os = "Unix";
237 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
239 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
240 &state->setup.nt1.in.user,
241 &state->setup.nt1.in.domain);
244 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
245 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
247 session->transport->negotiate.secblob,
249 &state->setup.nt1.in.password1,
250 &state->setup.nt1.in.password2,
252 NT_STATUS_NOT_OK_RETURN(nt_status);
254 smbcli_transport_simple_set_signing(session->transport, session_key,
255 state->setup.nt1.in.password2);
256 set_user_session_key(session, &session_key);
258 data_blob_free(&session_key);
259 } else if (lp_client_plaintext_auth()) {
260 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
261 state->setup.nt1.in.password2 = data_blob(NULL, 0);
263 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
264 return NT_STATUS_INVALID_PARAMETER;
267 *req = smb_raw_sesssetup_send(session, &state->setup);
269 return NT_STATUS_NO_MEMORY;
271 return (*req)->status;
276 old style session setup (pre NT1 protocol level)
278 static NTSTATUS session_setup_old(struct composite_context *c,
279 struct smbcli_session *session,
280 struct smb_composite_sesssetup *io,
281 struct smbcli_request **req)
284 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
285 const char *password = cli_credentials_get_password(io->in.credentials);
286 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
287 DATA_BLOB session_key;
289 if (lp_client_lanman_auth()) {
290 flags |= CLI_CRED_LANMAN_AUTH;
293 if (lp_client_ntlmv2_auth()) {
294 flags |= CLI_CRED_NTLMv2_AUTH;
297 state->setup.old.level = RAW_SESSSETUP_OLD;
298 state->setup.old.in.bufsize = session->transport->options.max_xmit;
299 state->setup.old.in.mpx_max = session->transport->options.max_mux;
300 state->setup.old.in.vc_num = 1;
301 state->setup.old.in.sesskey = io->in.sesskey;
302 state->setup.old.in.os = "Unix";
303 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
304 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
305 &state->setup.old.in.user,
306 &state->setup.old.in.domain);
308 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
309 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
311 session->transport->negotiate.secblob,
313 &state->setup.old.in.password,
316 NT_STATUS_NOT_OK_RETURN(nt_status);
317 set_user_session_key(session, &session_key);
319 data_blob_free(&session_key);
320 } else if (lp_client_plaintext_auth()) {
321 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
323 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
324 return NT_STATUS_INVALID_PARAMETER;
327 *req = smb_raw_sesssetup_send(session, &state->setup);
329 return NT_STATUS_NO_MEMORY;
331 return (*req)->status;
336 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
338 static NTSTATUS session_setup_spnego(struct composite_context *c,
339 struct smbcli_session *session,
340 struct smb_composite_sesssetup *io,
341 struct smbcli_request **req)
343 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
344 NTSTATUS status, session_key_err;
345 DATA_BLOB session_key = data_blob(NULL, 0);
346 DATA_BLOB null_data_blob = data_blob(NULL, 0);
347 const char *chosen_oid = NULL;
349 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
350 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
351 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
352 state->setup.spnego.in.vc_num = 1;
353 state->setup.spnego.in.sesskey = io->in.sesskey;
354 state->setup.spnego.in.capabilities = io->in.capabilities;
355 state->setup.spnego.in.os = "Unix";
356 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
357 state->setup.spnego.in.workgroup = io->in.workgroup;
359 smbcli_temp_set_signing(session->transport);
361 status = gensec_client_start(session, &session->gensec, c->event_ctx);
362 if (!NT_STATUS_IS_OK(status)) {
363 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
367 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
369 status = gensec_set_credentials(session->gensec, io->in.credentials);
370 if (!NT_STATUS_IS_OK(status)) {
371 DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n",
376 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
377 if (!NT_STATUS_IS_OK(status)) {
378 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
383 status = gensec_set_target_service(session->gensec, "cifs");
384 if (!NT_STATUS_IS_OK(status)) {
385 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
390 if (session->transport->negotiate.secblob.length) {
391 chosen_oid = GENSEC_OID_SPNEGO;
392 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
393 if (!NT_STATUS_IS_OK(status)) {
394 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
395 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
396 chosen_oid = GENSEC_OID_NTLMSSP;
397 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
398 if (!NT_STATUS_IS_OK(status)) {
399 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
400 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
405 /* without a sec blob, means raw NTLMSSP */
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 GENSEC client mechanism %s: %s\n",
410 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
414 if (chosen_oid == GENSEC_OID_SPNEGO) {
415 status = gensec_update(session->gensec, state,
416 session->transport->negotiate.secblob,
417 &state->setup.spnego.in.secblob);
419 status = gensec_update(session->gensec, state,
421 &state->setup.spnego.in.secblob);
425 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
426 !NT_STATUS_IS_OK(status)) {
427 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
428 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
431 state->gensec_status = status;
433 session_key_err = gensec_session_key(session->gensec, &session_key);
434 if (NT_STATUS_IS_OK(session_key_err)) {
435 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
438 *req = smb_raw_sesssetup_send(session, &state->setup);
440 return NT_STATUS_NO_MEMORY;
442 return (*req)->status;
447 composite session setup function that hides the details of all the
448 different session setup varients, including the multi-pass nature of
451 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
452 struct smb_composite_sesssetup *io)
454 struct composite_context *c;
455 struct sesssetup_state *state;
458 c = talloc_zero(session, struct composite_context);
459 if (c == NULL) return NULL;
461 state = talloc(c, struct sesssetup_state);
469 c->state = COMPOSITE_STATE_IN_PROGRESS;
470 c->private_data = state;
471 c->event_ctx = session->transport->socket->event.ctx;
473 /* no session setup at all in earliest protocol varients */
474 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
475 ZERO_STRUCT(io->out);
480 /* see what session setup interface we will use */
481 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
482 status = session_setup_old(c, session, io, &state->req);
483 } else if (!session->transport->options.use_spnego ||
484 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
485 status = session_setup_nt1(c, session, io, &state->req);
487 status = session_setup_spnego(c, session, io, &state->req);
490 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
491 NT_STATUS_IS_OK(status)) {
492 state->req->async.fn = request_handler;
493 state->req->async.private = c;
497 c->state = COMPOSITE_STATE_ERROR;
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);