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 if (cli_credentials_wrong_password(state->io->in.credentials)) {
84 nt_status = session_setup_old(c, session,
87 if (NT_STATUS_IS_OK(nt_status)) {
88 c->status = nt_status;
89 state->req->async.fn = request_handler;
90 state->req->async.private = c;
97 case RAW_SESSSETUP_NT1:
98 state->io->out.vuid = state->setup.nt1.out.vuid;
99 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
100 if (cli_credentials_wrong_password(state->io->in.credentials)) {
101 nt_status = session_setup_nt1(c, session,
104 if (NT_STATUS_IS_OK(nt_status)) {
105 c->status = nt_status;
106 state->req->async.fn = request_handler;
107 state->req->async.private = c;
114 case RAW_SESSSETUP_SPNEGO:
115 session->vuid = state->io->out.vuid = state->setup.spnego.out.vuid;
116 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
117 if (cli_credentials_wrong_password(state->io->in.credentials)) {
118 nt_status = session_setup_spnego(c, session,
121 if (NT_STATUS_IS_OK(nt_status)) {
122 c->status = nt_status;
123 state->req->async.fn = request_handler;
124 state->req->async.private = c;
129 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
130 !NT_STATUS_IS_OK(c->status)) {
133 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
135 /* The status value here, from the earlier pass at GENSEC is
136 * vital to the security of the system. Even if the other end
137 * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
138 * you must keep feeding it blobs, or else the remote
139 * host/attacker might avoid mutal authentication
142 state->gensec_status = gensec_update(session->gensec, state,
143 state->setup.spnego.out.secblob,
144 &state->setup.spnego.in.secblob);
145 c->status = state->gensec_status;
146 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
147 !NT_STATUS_IS_OK(c->status)) {
151 state->setup.spnego.in.secblob = data_blob(NULL, 0);
154 /* we need to do another round of session setup. We keep going until both sides
156 session_key_err = gensec_session_key(session->gensec, &session_key);
157 if (NT_STATUS_IS_OK(session_key_err)) {
158 set_user_session_key(session, &session_key);
159 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
162 if (state->setup.spnego.in.secblob.length) {
163 state->req = smb_raw_sesssetup_send(session, &state->setup);
164 state->req->async.fn = request_handler;
165 state->req->async.private = c;
170 case RAW_SESSSETUP_SMB2:
171 c->status = NT_STATUS_INTERNAL_ERROR;
175 /* enforce the local signing required flag */
176 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
177 if (!session->transport->negotiate.sign_info.doing_signing
178 && session->transport->negotiate.sign_info.mandatory_signing) {
179 DEBUG(0, ("SMB signing required, but server does not support it\n"));
180 c->status = NT_STATUS_ACCESS_DENIED;
184 if (NT_STATUS_IS_OK(c->status)) {
185 c->state = COMPOSITE_STATE_DONE;
187 c->state = COMPOSITE_STATE_ERROR;
196 send a nt1 style session setup
198 static NTSTATUS session_setup_nt1(struct composite_context *c,
199 struct smbcli_session *session,
200 struct smb_composite_sesssetup *io,
201 struct smbcli_request **req)
204 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
205 const char *password = cli_credentials_get_password(io->in.credentials);
206 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
207 DATA_BLOB session_key;
208 int flags = CLI_CRED_NTLM_AUTH;
209 if (lp_client_lanman_auth()) {
210 flags |= CLI_CRED_LANMAN_AUTH;
213 if (lp_client_ntlmv2_auth()) {
214 flags |= CLI_CRED_NTLMv2_AUTH;
217 state->setup.nt1.level = RAW_SESSSETUP_NT1;
218 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
219 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
220 state->setup.nt1.in.vc_num = 1;
221 state->setup.nt1.in.sesskey = io->in.sesskey;
222 state->setup.nt1.in.capabilities = io->in.capabilities;
223 state->setup.nt1.in.os = "Unix";
224 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
226 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
227 &state->setup.nt1.in.user,
228 &state->setup.nt1.in.domain);
231 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
232 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
234 session->transport->negotiate.secblob,
236 &state->setup.nt1.in.password1,
237 &state->setup.nt1.in.password2,
239 NT_STATUS_NOT_OK_RETURN(nt_status);
241 smbcli_transport_simple_set_signing(session->transport, session_key,
242 state->setup.nt1.in.password2);
243 set_user_session_key(session, &session_key);
245 data_blob_free(&session_key);
246 } else if (lp_client_plaintext_auth()) {
247 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
248 state->setup.nt1.in.password2 = data_blob(NULL, 0);
250 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
251 return NT_STATUS_INVALID_PARAMETER;
254 *req = smb_raw_sesssetup_send(session, &state->setup);
256 return NT_STATUS_NO_MEMORY;
258 return (*req)->status;
263 old style session setup (pre NT1 protocol level)
265 static NTSTATUS session_setup_old(struct composite_context *c,
266 struct smbcli_session *session,
267 struct smb_composite_sesssetup *io,
268 struct smbcli_request **req)
271 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
272 const char *password = cli_credentials_get_password(io->in.credentials);
273 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
274 DATA_BLOB session_key;
276 if (lp_client_lanman_auth()) {
277 flags |= CLI_CRED_LANMAN_AUTH;
280 if (lp_client_ntlmv2_auth()) {
281 flags |= CLI_CRED_NTLMv2_AUTH;
284 state->setup.old.level = RAW_SESSSETUP_OLD;
285 state->setup.old.in.bufsize = session->transport->options.max_xmit;
286 state->setup.old.in.mpx_max = session->transport->options.max_mux;
287 state->setup.old.in.vc_num = 1;
288 state->setup.old.in.sesskey = io->in.sesskey;
289 state->setup.old.in.os = "Unix";
290 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
291 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
292 &state->setup.old.in.user,
293 &state->setup.old.in.domain);
295 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
296 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
298 session->transport->negotiate.secblob,
300 &state->setup.old.in.password,
303 NT_STATUS_NOT_OK_RETURN(nt_status);
304 set_user_session_key(session, &session_key);
306 data_blob_free(&session_key);
307 } else if (lp_client_plaintext_auth()) {
308 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
310 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
311 return NT_STATUS_INVALID_PARAMETER;
314 *req = smb_raw_sesssetup_send(session, &state->setup);
316 return NT_STATUS_NO_MEMORY;
318 return (*req)->status;
323 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
325 static NTSTATUS session_setup_spnego(struct composite_context *c,
326 struct smbcli_session *session,
327 struct smb_composite_sesssetup *io,
328 struct smbcli_request **req)
330 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
331 NTSTATUS status, session_key_err;
332 DATA_BLOB session_key = data_blob(NULL, 0);
333 DATA_BLOB null_data_blob = data_blob(NULL, 0);
334 const char *chosen_oid = NULL;
336 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
337 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
338 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
339 state->setup.spnego.in.vc_num = 1;
340 state->setup.spnego.in.sesskey = io->in.sesskey;
341 state->setup.spnego.in.capabilities = io->in.capabilities;
342 state->setup.spnego.in.os = "Unix";
343 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
344 state->setup.spnego.in.workgroup = io->in.workgroup;
346 state->setup.spnego.out.vuid = session->vuid;
348 smbcli_temp_set_signing(session->transport);
350 status = gensec_client_start(session, &session->gensec, c->event_ctx);
351 if (!NT_STATUS_IS_OK(status)) {
352 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
356 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
358 status = gensec_set_credentials(session->gensec, io->in.credentials);
359 if (!NT_STATUS_IS_OK(status)) {
360 DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n",
365 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
366 if (!NT_STATUS_IS_OK(status)) {
367 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
372 status = gensec_set_target_service(session->gensec, "cifs");
373 if (!NT_STATUS_IS_OK(status)) {
374 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
379 if (session->transport->negotiate.secblob.length) {
380 chosen_oid = GENSEC_OID_SPNEGO;
381 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
382 if (!NT_STATUS_IS_OK(status)) {
383 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
384 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
385 chosen_oid = GENSEC_OID_NTLMSSP;
386 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
387 if (!NT_STATUS_IS_OK(status)) {
388 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
389 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
394 /* without a sec blob, means raw NTLMSSP */
395 chosen_oid = GENSEC_OID_NTLMSSP;
396 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
397 if (!NT_STATUS_IS_OK(status)) {
398 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
399 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
403 if (chosen_oid == GENSEC_OID_SPNEGO) {
404 status = gensec_update(session->gensec, state,
405 session->transport->negotiate.secblob,
406 &state->setup.spnego.in.secblob);
408 status = gensec_update(session->gensec, state,
410 &state->setup.spnego.in.secblob);
414 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
415 !NT_STATUS_IS_OK(status)) {
416 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
417 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
420 state->gensec_status = status;
422 session_key_err = gensec_session_key(session->gensec, &session_key);
423 if (NT_STATUS_IS_OK(session_key_err)) {
424 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
427 *req = smb_raw_sesssetup_send(session, &state->setup);
429 return NT_STATUS_NO_MEMORY;
431 return (*req)->status;
436 composite session setup function that hides the details of all the
437 different session setup varients, including the multi-pass nature of
440 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
441 struct smb_composite_sesssetup *io)
443 struct composite_context *c;
444 struct sesssetup_state *state;
447 c = talloc_zero(session, struct composite_context);
448 if (c == NULL) return NULL;
450 state = talloc(c, struct sesssetup_state);
458 c->state = COMPOSITE_STATE_IN_PROGRESS;
459 c->private_data = state;
460 c->event_ctx = session->transport->socket->event.ctx;
462 /* no session setup at all in earliest protocol varients */
463 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
464 ZERO_STRUCT(io->out);
469 /* see what session setup interface we will use */
470 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
471 status = session_setup_old(c, session, io, &state->req);
472 } else if (!session->transport->options.use_spnego ||
473 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
474 status = session_setup_nt1(c, session, io, &state->req);
476 status = session_setup_spnego(c, session, io, &state->req);
479 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
480 NT_STATUS_IS_OK(status)) {
481 state->req->async.fn = request_handler;
482 state->req->async.private = c;
486 c->state = COMPOSITE_STATE_ERROR;
493 receive a composite session setup reply
495 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
498 status = composite_wait(c);
504 sync version of smb_composite_sesssetup
506 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
508 struct composite_context *c = smb_composite_sesssetup_send(session, io);
509 return smb_composite_sesssetup_recv(c);