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 data_blob_free(&lmv2_session_key);
203 state->setup.nt1.in.password1 = lmv2_response;
204 state->setup.nt1.in.password2 = ntlmv2_response;
208 state->setup.nt1.in.password2 = nt_blob(state, nt_hash,
209 session->transport->negotiate.secblob);
210 if (lp_client_lanman_auth()) {
211 state->setup.nt1.in.password1 = lanman_blob(state, password,
212 session->transport->negotiate.secblob);
214 /* if not sending the LM password, send the NT password twice */
215 state->setup.nt1.in.password1 = state->setup.nt1.in.password2;
218 session_key = data_blob_talloc(session, NULL, 16);
219 SMBsesskeygen_ntv1(nt_hash->hash, session_key.data);
222 smbcli_transport_simple_set_signing(session->transport, session_key,
223 state->setup.nt1.in.password2);
224 set_user_session_key(session, &session_key);
226 data_blob_free(&session_key);
228 } else if (lp_client_plaintext_auth()) {
229 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
230 state->setup.nt1.in.password2 = data_blob(NULL, 0);
232 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
233 return NT_STATUS_INVALID_PARAMETER;
236 *req = smb_raw_sesssetup_send(session, &state->setup);
238 return NT_STATUS_NO_MEMORY;
240 return (*req)->status;
245 old style session setup (pre NT1 protocol level)
247 static NTSTATUS session_setup_old(struct composite_context *c,
248 struct smbcli_session *session,
249 struct smb_composite_sesssetup *io,
250 struct smbcli_request **req)
252 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
253 const char *password = cli_credentials_get_password(io->in.credentials);
255 state->setup.old.level = RAW_SESSSETUP_OLD;
256 state->setup.old.in.bufsize = session->transport->options.max_xmit;
257 state->setup.old.in.mpx_max = session->transport->options.max_mux;
258 state->setup.old.in.vc_num = 1;
259 state->setup.old.in.sesskey = io->in.sesskey;
260 state->setup.old.in.os = "Unix";
261 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
262 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
263 &state->setup.old.in.user,
264 &state->setup.old.in.domain);
267 state->setup.old.in.password = data_blob(NULL, 0);
268 } else if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
269 state->setup.old.in.password = lanman_blob(state, password,
270 session->transport->negotiate.secblob);
272 state->setup.old.in.password = data_blob_talloc(state,
277 *req = smb_raw_sesssetup_send(session, &state->setup);
279 return NT_STATUS_NO_MEMORY;
281 return (*req)->status;
286 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
288 static NTSTATUS session_setup_spnego(struct composite_context *c,
289 struct smbcli_session *session,
290 struct smb_composite_sesssetup *io,
291 struct smbcli_request **req)
293 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
294 NTSTATUS status, session_key_err;
295 DATA_BLOB session_key = data_blob(NULL, 0);
296 DATA_BLOB null_data_blob = data_blob(NULL, 0);
297 const char *chosen_oid = NULL;
299 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
300 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
301 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
302 state->setup.spnego.in.vc_num = 1;
303 state->setup.spnego.in.sesskey = io->in.sesskey;
304 state->setup.spnego.in.capabilities = io->in.capabilities;
305 state->setup.spnego.in.os = "Unix";
306 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
307 state->setup.spnego.in.workgroup = io->in.workgroup;
309 state->setup.spnego.out.vuid = session->vuid;
311 smbcli_temp_set_signing(session->transport);
313 status = gensec_client_start(session, &session->gensec, c->event_ctx);
314 if (!NT_STATUS_IS_OK(status)) {
315 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
319 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
321 status = gensec_set_credentials(session->gensec, io->in.credentials);
322 if (!NT_STATUS_IS_OK(status)) {
323 DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n",
328 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
329 if (!NT_STATUS_IS_OK(status)) {
330 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
335 status = gensec_set_target_service(session->gensec, "cifs");
336 if (!NT_STATUS_IS_OK(status)) {
337 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
342 if (session->transport->negotiate.secblob.length) {
343 chosen_oid = GENSEC_OID_SPNEGO;
345 /* without a sec blob, means raw NTLMSSP */
346 chosen_oid = GENSEC_OID_NTLMSSP;
349 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
350 if (!NT_STATUS_IS_OK(status)) {
351 DEBUG(1, ("Failed to start set GENSEC client SPNEGO mechanism %s: %s\n",
352 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
356 status = gensec_update(session->gensec, state,
357 session->transport->negotiate.secblob,
358 &state->setup.spnego.in.secblob);
359 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
360 !NT_STATUS_IS_OK(status)) {
361 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
362 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
365 state->gensec_status = status;
367 session_key_err = gensec_session_key(session->gensec, &session_key);
368 if (NT_STATUS_IS_OK(session_key_err)) {
369 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
372 *req = smb_raw_sesssetup_send(session, &state->setup);
374 return NT_STATUS_NO_MEMORY;
376 return (*req)->status;
381 composite session setup function that hides the details of all the
382 different session setup varients, including the multi-pass nature of
385 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
386 struct smb_composite_sesssetup *io)
388 struct composite_context *c;
389 struct sesssetup_state *state;
392 c = talloc_zero(session, struct composite_context);
393 if (c == NULL) return NULL;
395 state = talloc(c, struct sesssetup_state);
397 c->state = COMPOSITE_STATE_ERROR;
398 c->status = NT_STATUS_NO_MEMORY;
403 c->state = COMPOSITE_STATE_IN_PROGRESS;
404 c->private_data = state;
405 c->event_ctx = session->transport->socket->event.ctx;
407 /* no session setup at all in earliest protocol varients */
408 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
409 ZERO_STRUCT(io->out);
410 c->state = COMPOSITE_STATE_DONE;
414 /* see what session setup interface we will use */
415 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
416 status = session_setup_old(c, session, io, &state->req);
417 } else if (!session->transport->options.use_spnego ||
418 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
419 status = session_setup_nt1(c, session, io, &state->req);
421 status = session_setup_spnego(c, session, io, &state->req);
424 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
425 NT_STATUS_IS_OK(status)) {
426 state->req->async.fn = request_handler;
427 state->req->async.private = c;
431 c->state = COMPOSITE_STATE_ERROR;
438 receive a composite session setup reply
440 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
443 status = composite_wait(c);
449 sync version of smb_composite_sesssetup
451 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
453 struct composite_context *c = smb_composite_sesssetup_send(session, io);
454 return smb_composite_sesssetup_recv(c);