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 "auth/auth.h"
31 struct sesssetup_state {
32 union smb_sesssetup setup;
33 NTSTATUS gensec_status;
34 struct smb_composite_sesssetup *io;
35 struct smbcli_request *req;
40 form an encrypted lanman password from a plaintext password
41 and the server supplied challenge
43 static DATA_BLOB lanman_blob(TALLOC_CTX *mem_ctx, const char *pass, DATA_BLOB challenge)
45 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, 24);
46 SMBencrypt(pass, challenge.data, blob.data);
51 form an encrypted NT password from a plaintext password
52 and the server supplied challenge
54 static DATA_BLOB nt_blob(TALLOC_CTX *mem_ctx, const struct samr_Password *nt_hash, DATA_BLOB challenge)
56 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, 24);
57 SMBOWFencrypt(nt_hash->hash, challenge.data, blob.data);
62 store the user session key for a transport
64 static void set_user_session_key(struct smbcli_session *session,
65 const DATA_BLOB *session_key)
67 session->user_session_key = data_blob_talloc(session,
73 handler for completion of a smbcli_request sub-request
75 static void request_handler(struct smbcli_request *req)
77 struct composite_context *c = req->async.private;
78 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
79 struct smbcli_session *session = req->session;
80 DATA_BLOB session_key = data_blob(NULL, 0);
81 DATA_BLOB null_data_blob = data_blob(NULL, 0);
82 NTSTATUS session_key_err;
84 c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
86 switch (state->setup.old.level) {
87 case RAW_SESSSETUP_OLD:
88 state->io->out.vuid = state->setup.old.out.vuid;
91 case RAW_SESSSETUP_NT1:
92 state->io->out.vuid = state->setup.nt1.out.vuid;
95 case RAW_SESSSETUP_SPNEGO:
96 session->vuid = state->io->out.vuid = state->setup.spnego.out.vuid;
97 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
98 !NT_STATUS_IS_OK(c->status)) {
101 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
103 /* The status value here, from the earlier pass at GENSEC is
104 * vital to the security of the system. Even if the other end
105 * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
106 * you must keep feeding it blobs, or else the remote
107 * host/attacker might avoid mutal authentication
110 state->gensec_status = gensec_update(session->gensec, state,
111 state->setup.spnego.out.secblob,
112 &state->setup.spnego.in.secblob);
113 c->status = state->gensec_status;
114 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
115 !NT_STATUS_IS_OK(c->status)) {
119 state->setup.spnego.in.secblob = data_blob(NULL, 0);
122 /* we need to do another round of session setup. We keep going until both sides
124 session_key_err = gensec_session_key(session->gensec, &session_key);
125 if (NT_STATUS_IS_OK(session_key_err)) {
126 set_user_session_key(session, &session_key);
127 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
130 if (state->setup.spnego.in.secblob.length) {
131 state->req = smb_raw_sesssetup_send(session, &state->setup);
132 state->req->async.fn = request_handler;
133 state->req->async.private = c;
138 /* enforce the local signing required flag */
139 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
140 if (!session->transport->negotiate.sign_info.doing_signing
141 && session->transport->negotiate.sign_info.mandatory_signing) {
142 DEBUG(0, ("SMB signing required, but server does not support it\n"));
143 c->status = NT_STATUS_ACCESS_DENIED;
147 if (NT_STATUS_IS_OK(c->status)) {
148 c->state = COMPOSITE_STATE_DONE;
150 c->state = COMPOSITE_STATE_ERROR;
159 send a nt1 style session setup
161 static NTSTATUS session_setup_nt1(struct composite_context *c,
162 struct smbcli_session *session,
163 struct smb_composite_sesssetup *io,
164 struct smbcli_request **req)
166 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
167 const struct samr_Password *nt_hash = cli_credentials_get_nt_hash(io->in.credentials, state);
168 const char *password = cli_credentials_get_password(io->in.credentials);
170 state->setup.nt1.level = RAW_SESSSETUP_NT1;
171 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
172 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
173 state->setup.nt1.in.vc_num = 1;
174 state->setup.nt1.in.sesskey = io->in.sesskey;
175 state->setup.nt1.in.capabilities = io->in.capabilities;
176 state->setup.nt1.in.os = "Unix";
177 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
178 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
179 &state->setup.nt1.in.user,
180 &state->setup.nt1.in.domain);
183 state->setup.nt1.in.password1 = data_blob(NULL, 0);
184 state->setup.nt1.in.password2 = data_blob(NULL, 0);
185 } else if (session->transport->negotiate.sec_mode &
186 NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
187 DATA_BLOB session_key;
188 if (lp_client_ntlmv2_auth()) {
189 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_netbios_name(), lp_workgroup());
190 DATA_BLOB lmv2_response, ntlmv2_response, lmv2_session_key;
192 if (!SMBNTLMv2encrypt_hash(state,
193 state->setup.nt1.in.user, state->setup.nt1.in.domain,
194 nt_hash->hash, &session->transport->negotiate.secblob,
196 &lmv2_response, &ntlmv2_response,
197 &lmv2_session_key, &session_key)) {
198 data_blob_free(&names_blob);
199 return NT_STATUS_NO_MEMORY;
201 data_blob_free(&names_blob);
202 state->setup.nt1.in.password1 = lmv2_response;
203 state->setup.nt1.in.password2 = ntlmv2_response;
205 smbcli_transport_simple_set_signing(session->transport, session_key,
206 state->setup.nt1.in.password2);
207 set_user_session_key(session, &session_key);
209 data_blob_free(&lmv2_session_key);
210 data_blob_free(&session_key);
213 state->setup.nt1.in.password2 = nt_blob(state, nt_hash,
214 session->transport->negotiate.secblob);
215 if (lp_client_lanman_auth()) {
216 state->setup.nt1.in.password1 = lanman_blob(state, password,
217 session->transport->negotiate.secblob);
219 /* if not sending the LM password, send the NT password twice */
220 state->setup.nt1.in.password1 = state->setup.nt1.in.password2;
223 session_key = data_blob_talloc(session, NULL, 16);
224 SMBsesskeygen_ntv1(nt_hash->hash, session_key.data);
225 smbcli_transport_simple_set_signing(session->transport, session_key,
226 state->setup.nt1.in.password2);
227 set_user_session_key(session, &session_key);
229 data_blob_free(&session_key);
232 } else if (lp_client_plaintext_auth()) {
233 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
234 state->setup.nt1.in.password2 = data_blob(NULL, 0);
236 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
237 return NT_STATUS_INVALID_PARAMETER;
240 *req = smb_raw_sesssetup_send(session, &state->setup);
242 return NT_STATUS_NO_MEMORY;
244 return (*req)->status;
249 old style session setup (pre NT1 protocol level)
251 static NTSTATUS session_setup_old(struct composite_context *c,
252 struct smbcli_session *session,
253 struct smb_composite_sesssetup *io,
254 struct smbcli_request **req)
256 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
257 const char *password = cli_credentials_get_password(io->in.credentials);
259 state->setup.old.level = RAW_SESSSETUP_OLD;
260 state->setup.old.in.bufsize = session->transport->options.max_xmit;
261 state->setup.old.in.mpx_max = session->transport->options.max_mux;
262 state->setup.old.in.vc_num = 1;
263 state->setup.old.in.sesskey = io->in.sesskey;
264 state->setup.old.in.os = "Unix";
265 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
266 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
267 &state->setup.old.in.user,
268 &state->setup.old.in.domain);
271 state->setup.old.in.password = data_blob(NULL, 0);
272 } else if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
273 state->setup.old.in.password = lanman_blob(state, password,
274 session->transport->negotiate.secblob);
276 state->setup.old.in.password = data_blob_talloc(state,
281 *req = smb_raw_sesssetup_send(session, &state->setup);
283 return NT_STATUS_NO_MEMORY;
285 return (*req)->status;
290 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
292 static NTSTATUS session_setup_spnego(struct composite_context *c,
293 struct smbcli_session *session,
294 struct smb_composite_sesssetup *io,
295 struct smbcli_request **req)
297 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
298 NTSTATUS status, session_key_err;
299 DATA_BLOB session_key = data_blob(NULL, 0);
300 DATA_BLOB null_data_blob = data_blob(NULL, 0);
301 const char *chosen_oid = NULL;
303 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
304 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
305 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
306 state->setup.spnego.in.vc_num = 1;
307 state->setup.spnego.in.sesskey = io->in.sesskey;
308 state->setup.spnego.in.capabilities = io->in.capabilities;
309 state->setup.spnego.in.os = "Unix";
310 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
311 state->setup.spnego.in.workgroup = io->in.workgroup;
313 state->setup.spnego.out.vuid = session->vuid;
315 smbcli_temp_set_signing(session->transport);
317 status = gensec_client_start(session, &session->gensec, c->event_ctx);
318 if (!NT_STATUS_IS_OK(status)) {
319 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
323 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
325 status = gensec_set_credentials(session->gensec, io->in.credentials);
326 if (!NT_STATUS_IS_OK(status)) {
327 DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n",
332 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
333 if (!NT_STATUS_IS_OK(status)) {
334 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
339 status = gensec_set_target_service(session->gensec, "cifs");
340 if (!NT_STATUS_IS_OK(status)) {
341 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
346 if (session->transport->negotiate.secblob.length) {
347 chosen_oid = GENSEC_OID_SPNEGO;
349 /* without a sec blob, means raw NTLMSSP */
350 chosen_oid = GENSEC_OID_NTLMSSP;
353 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
354 if (!NT_STATUS_IS_OK(status)) {
355 DEBUG(1, ("Failed to start set GENSEC client SPNEGO mechanism %s: %s\n",
356 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
360 status = gensec_update(session->gensec, state,
361 session->transport->negotiate.secblob,
362 &state->setup.spnego.in.secblob);
363 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
364 !NT_STATUS_IS_OK(status)) {
365 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
366 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
369 state->gensec_status = status;
371 session_key_err = gensec_session_key(session->gensec, &session_key);
372 if (NT_STATUS_IS_OK(session_key_err)) {
373 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
376 *req = smb_raw_sesssetup_send(session, &state->setup);
378 return NT_STATUS_NO_MEMORY;
380 return (*req)->status;
385 composite session setup function that hides the details of all the
386 different session setup varients, including the multi-pass nature of
389 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
390 struct smb_composite_sesssetup *io)
392 struct composite_context *c;
393 struct sesssetup_state *state;
396 c = talloc_zero(session, struct composite_context);
397 if (c == NULL) return NULL;
399 state = talloc(c, struct sesssetup_state);
401 c->state = COMPOSITE_STATE_ERROR;
402 c->status = NT_STATUS_NO_MEMORY;
407 c->state = COMPOSITE_STATE_IN_PROGRESS;
408 c->private_data = state;
409 c->event_ctx = session->transport->socket->event.ctx;
411 /* no session setup at all in earliest protocol varients */
412 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
413 ZERO_STRUCT(io->out);
414 c->state = COMPOSITE_STATE_DONE;
418 /* see what session setup interface we will use */
419 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
420 status = session_setup_old(c, session, io, &state->req);
421 } else if (!session->transport->options.use_spnego ||
422 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
423 status = session_setup_nt1(c, session, io, &state->req);
425 status = session_setup_spnego(c, session, io, &state->req);
428 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
429 NT_STATUS_IS_OK(status)) {
430 state->req->async.fn = request_handler;
431 state->req->async.private = c;
435 c->state = COMPOSITE_STATE_ERROR;
442 receive a composite session setup reply
444 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
447 status = composite_wait(c);
453 sync version of smb_composite_sesssetup
455 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
457 struct composite_context *c = smb_composite_sesssetup_send(session, io);
458 return smb_composite_sesssetup_recv(c);