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 /* enforce the local signing required flag */
171 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
172 if (!session->transport->negotiate.sign_info.doing_signing
173 && session->transport->negotiate.sign_info.mandatory_signing) {
174 DEBUG(0, ("SMB signing required, but server does not support it\n"));
175 c->status = NT_STATUS_ACCESS_DENIED;
179 if (NT_STATUS_IS_OK(c->status)) {
180 c->state = COMPOSITE_STATE_DONE;
182 c->state = COMPOSITE_STATE_ERROR;
191 send a nt1 style session setup
193 static NTSTATUS session_setup_nt1(struct composite_context *c,
194 struct smbcli_session *session,
195 struct smb_composite_sesssetup *io,
196 struct smbcli_request **req)
199 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
200 const char *password = cli_credentials_get_password(io->in.credentials);
201 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
202 DATA_BLOB session_key;
203 int flags = CLI_CRED_NTLM_AUTH;
204 if (lp_client_lanman_auth()) {
205 flags |= CLI_CRED_LANMAN_AUTH;
208 if (lp_client_ntlmv2_auth()) {
209 flags |= CLI_CRED_NTLMv2_AUTH;
212 state->setup.nt1.level = RAW_SESSSETUP_NT1;
213 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
214 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
215 state->setup.nt1.in.vc_num = 1;
216 state->setup.nt1.in.sesskey = io->in.sesskey;
217 state->setup.nt1.in.capabilities = io->in.capabilities;
218 state->setup.nt1.in.os = "Unix";
219 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
221 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
222 &state->setup.nt1.in.user,
223 &state->setup.nt1.in.domain);
226 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
227 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
229 session->transport->negotiate.secblob,
231 &state->setup.nt1.in.password1,
232 &state->setup.nt1.in.password2,
234 NT_STATUS_NOT_OK_RETURN(nt_status);
236 smbcli_transport_simple_set_signing(session->transport, session_key,
237 state->setup.nt1.in.password2);
238 set_user_session_key(session, &session_key);
240 data_blob_free(&session_key);
241 } else if (lp_client_plaintext_auth()) {
242 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
243 state->setup.nt1.in.password2 = data_blob(NULL, 0);
245 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
246 return NT_STATUS_INVALID_PARAMETER;
249 *req = smb_raw_sesssetup_send(session, &state->setup);
251 return NT_STATUS_NO_MEMORY;
253 return (*req)->status;
258 old style session setup (pre NT1 protocol level)
260 static NTSTATUS session_setup_old(struct composite_context *c,
261 struct smbcli_session *session,
262 struct smb_composite_sesssetup *io,
263 struct smbcli_request **req)
266 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
267 const char *password = cli_credentials_get_password(io->in.credentials);
268 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
269 DATA_BLOB session_key;
271 if (lp_client_lanman_auth()) {
272 flags |= CLI_CRED_LANMAN_AUTH;
275 if (lp_client_ntlmv2_auth()) {
276 flags |= CLI_CRED_NTLMv2_AUTH;
279 state->setup.old.level = RAW_SESSSETUP_OLD;
280 state->setup.old.in.bufsize = session->transport->options.max_xmit;
281 state->setup.old.in.mpx_max = session->transport->options.max_mux;
282 state->setup.old.in.vc_num = 1;
283 state->setup.old.in.sesskey = io->in.sesskey;
284 state->setup.old.in.os = "Unix";
285 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
286 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
287 &state->setup.old.in.user,
288 &state->setup.old.in.domain);
290 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
291 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
293 session->transport->negotiate.secblob,
295 &state->setup.old.in.password,
298 NT_STATUS_NOT_OK_RETURN(nt_status);
299 set_user_session_key(session, &session_key);
301 data_blob_free(&session_key);
302 } else if (lp_client_plaintext_auth()) {
303 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
305 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
306 return NT_STATUS_INVALID_PARAMETER;
309 *req = smb_raw_sesssetup_send(session, &state->setup);
311 return NT_STATUS_NO_MEMORY;
313 return (*req)->status;
318 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
320 static NTSTATUS session_setup_spnego(struct composite_context *c,
321 struct smbcli_session *session,
322 struct smb_composite_sesssetup *io,
323 struct smbcli_request **req)
325 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
326 NTSTATUS status, session_key_err;
327 DATA_BLOB session_key = data_blob(NULL, 0);
328 DATA_BLOB null_data_blob = data_blob(NULL, 0);
329 const char *chosen_oid = NULL;
331 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
332 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
333 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
334 state->setup.spnego.in.vc_num = 1;
335 state->setup.spnego.in.sesskey = io->in.sesskey;
336 state->setup.spnego.in.capabilities = io->in.capabilities;
337 state->setup.spnego.in.os = "Unix";
338 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
339 state->setup.spnego.in.workgroup = io->in.workgroup;
341 state->setup.spnego.out.vuid = session->vuid;
343 smbcli_temp_set_signing(session->transport);
345 status = gensec_client_start(session, &session->gensec, c->event_ctx);
346 if (!NT_STATUS_IS_OK(status)) {
347 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
351 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
353 status = gensec_set_credentials(session->gensec, io->in.credentials);
354 if (!NT_STATUS_IS_OK(status)) {
355 DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n",
360 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
361 if (!NT_STATUS_IS_OK(status)) {
362 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
367 status = gensec_set_target_service(session->gensec, "cifs");
368 if (!NT_STATUS_IS_OK(status)) {
369 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
374 if (session->transport->negotiate.secblob.length) {
375 chosen_oid = GENSEC_OID_SPNEGO;
376 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
377 if (!NT_STATUS_IS_OK(status)) {
378 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
379 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
380 chosen_oid = GENSEC_OID_NTLMSSP;
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 (fallback) GENSEC client mechanism %s: %s\n",
384 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
389 /* without a sec blob, means raw NTLMSSP */
390 chosen_oid = GENSEC_OID_NTLMSSP;
391 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
392 if (!NT_STATUS_IS_OK(status)) {
393 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
394 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
398 if (chosen_oid == GENSEC_OID_SPNEGO) {
399 status = gensec_update(session->gensec, state,
400 session->transport->negotiate.secblob,
401 &state->setup.spnego.in.secblob);
403 status = gensec_update(session->gensec, state,
405 &state->setup.spnego.in.secblob);
409 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
410 !NT_STATUS_IS_OK(status)) {
411 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
412 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
415 state->gensec_status = status;
417 session_key_err = gensec_session_key(session->gensec, &session_key);
418 if (NT_STATUS_IS_OK(session_key_err)) {
419 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
422 *req = smb_raw_sesssetup_send(session, &state->setup);
424 return NT_STATUS_NO_MEMORY;
426 return (*req)->status;
431 composite session setup function that hides the details of all the
432 different session setup varients, including the multi-pass nature of
435 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
436 struct smb_composite_sesssetup *io)
438 struct composite_context *c;
439 struct sesssetup_state *state;
442 c = talloc_zero(session, struct composite_context);
443 if (c == NULL) return NULL;
445 state = talloc(c, struct sesssetup_state);
453 c->state = COMPOSITE_STATE_IN_PROGRESS;
454 c->private_data = state;
455 c->event_ctx = session->transport->socket->event.ctx;
457 /* no session setup at all in earliest protocol varients */
458 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
459 ZERO_STRUCT(io->out);
460 c->state = COMPOSITE_STATE_DONE;
464 /* see what session setup interface we will use */
465 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
466 status = session_setup_old(c, session, io, &state->req);
467 } else if (!session->transport->options.use_spnego ||
468 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
469 status = session_setup_nt1(c, session, io, &state->req);
471 status = session_setup_spnego(c, session, io, &state->req);
474 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
475 NT_STATUS_IS_OK(status)) {
476 state->req->async.fn = request_handler;
477 state->req->async.private = c;
481 c->state = COMPOSITE_STATE_ERROR;
488 receive a composite session setup reply
490 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
493 status = composite_wait(c);
499 sync version of smb_composite_sesssetup
501 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
503 struct composite_context *c = smb_composite_sesssetup_send(session, io);
504 return smb_composite_sesssetup_recv(c);