4 * SMB/CIFS session setup handling routines
6 * Copyright (c) International Business Machines Corp., 2006, 2007
7 * Author(s): Steve French (sfrench@us.ibm.com)
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "cifsproto.h"
27 #include "cifs_unicode.h"
28 #include "cifs_debug.h"
31 #include <linux/utsname.h>
32 #include "cifs_spnego.h"
34 extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8,
37 static __u32 cifs_ssetup_hdr(struct cifsSesInfo *ses, SESSION_SETUP_ANDX *pSMB)
39 __u32 capabilities = 0;
41 /* init fields common to all four types of SessSetup */
42 /* note that header is initialized to zero in header_assemble */
43 pSMB->req.AndXCommand = 0xFF;
44 pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
45 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
47 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
49 /* BB verify whether signing required on neg or just on auth frame
52 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
53 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
55 if (ses->server->secMode &
56 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
57 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
59 if (ses->capabilities & CAP_UNICODE) {
60 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
61 capabilities |= CAP_UNICODE;
63 if (ses->capabilities & CAP_STATUS32) {
64 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
65 capabilities |= CAP_STATUS32;
67 if (ses->capabilities & CAP_DFS) {
68 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
69 capabilities |= CAP_DFS;
71 if (ses->capabilities & CAP_UNIX)
72 capabilities |= CAP_UNIX;
74 /* BB check whether to init vcnum BB */
79 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
81 char *bcc_ptr = *pbcc_area;
85 bytes_ret = cifs_strtoUCS((__le16 *)bcc_ptr, "Linux version ", 32,
87 bcc_ptr += 2 * bytes_ret;
88 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, init_utsname()->release,
90 bcc_ptr += 2 * bytes_ret;
91 bcc_ptr += 2; /* trailing null */
93 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
95 bcc_ptr += 2 * bytes_ret;
96 bcc_ptr += 2; /* trailing null */
101 static void unicode_domain_string(char **pbcc_area, struct cifsSesInfo *ses,
102 const struct nls_table *nls_cp)
104 char *bcc_ptr = *pbcc_area;
108 if (ses->domainName == NULL) {
109 /* Sending null domain better than using a bogus domain name (as
110 we did briefly in 2.6.18) since server will use its default */
115 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->domainName,
117 bcc_ptr += 2 * bytes_ret;
118 bcc_ptr += 2; /* account for null terminator */
120 *pbcc_area = bcc_ptr;
124 static void unicode_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses,
125 const struct nls_table *nls_cp)
127 char *bcc_ptr = *pbcc_area;
130 /* BB FIXME add check that strings total less
131 than 335 or will need to send them as arrays */
133 /* unicode strings, must be word aligned before the call */
134 /* if ((long) bcc_ptr % 2) {
139 if (ses->userName == NULL) {
140 /* null user mount */
143 } else { /* 300 should be long enough for any conceivable user name */
144 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->userName,
147 bcc_ptr += 2 * bytes_ret;
148 bcc_ptr += 2; /* account for null termination */
150 unicode_domain_string(&bcc_ptr, ses, nls_cp);
151 unicode_oslm_strings(&bcc_ptr, nls_cp);
153 *pbcc_area = bcc_ptr;
156 static void ascii_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses,
157 const struct nls_table *nls_cp)
159 char *bcc_ptr = *pbcc_area;
162 /* BB what about null user mounts - check that we do this BB */
164 if (ses->userName == NULL) {
165 /* BB what about null user mounts - check that we do this BB */
166 } else { /* 300 should be long enough for any conceivable user name */
167 strncpy(bcc_ptr, ses->userName, 300);
169 /* BB improve check for overflow */
170 bcc_ptr += strnlen(ses->userName, 300);
172 bcc_ptr++; /* account for null termination */
176 if (ses->domainName != NULL) {
177 strncpy(bcc_ptr, ses->domainName, 256);
178 bcc_ptr += strnlen(ses->domainName, 256);
179 } /* else we will send a null domain name
180 so the server will default to its own domain */
184 /* BB check for overflow here */
186 strcpy(bcc_ptr, "Linux version ");
187 bcc_ptr += strlen("Linux version ");
188 strcpy(bcc_ptr, init_utsname()->release);
189 bcc_ptr += strlen(init_utsname()->release) + 1;
191 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
192 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
194 *pbcc_area = bcc_ptr;
197 static int decode_unicode_ssetup(char **pbcc_area, int bleft,
198 struct cifsSesInfo *ses,
199 const struct nls_table *nls_cp)
203 char *data = *pbcc_area;
207 cFYI(1, ("bleft %d", bleft));
210 /* SMB header is unaligned, so cifs servers word align start of
213 bleft--; /* Windows servers do not always double null terminate
214 their final Unicode string - in which case we
215 now will not attempt to decode the byte of junk
218 words_left = bleft / 2;
220 /* save off server operating system */
221 len = UniStrnlen((wchar_t *) data, words_left);
223 /* We look for obvious messed up bcc or strings in response so we do not go off
224 the end since (at least) WIN2K and Windows XP have a major bug in not null
225 terminating last Unicode string in response */
226 if (len >= words_left)
229 kfree(ses->serverOS);
230 /* UTF-8 string will not grow more than four times as big as UCS-16 */
231 ses->serverOS = kzalloc(4 * len, GFP_KERNEL);
232 if (ses->serverOS != NULL)
233 cifs_strfromUCS_le(ses->serverOS, (__le16 *)data, len, nls_cp);
234 data += 2 * (len + 1);
235 words_left -= len + 1;
237 /* save off server network operating system */
238 len = UniStrnlen((wchar_t *) data, words_left);
240 if (len >= words_left)
243 kfree(ses->serverNOS);
244 ses->serverNOS = kzalloc(4 * len, GFP_KERNEL); /* BB this is wrong length FIXME BB */
245 if (ses->serverNOS != NULL) {
246 cifs_strfromUCS_le(ses->serverNOS, (__le16 *)data, len,
248 if (strncmp(ses->serverNOS, "NT LAN Manager 4", 16) == 0) {
249 cFYI(1, ("NT4 server"));
250 ses->flags |= CIFS_SES_NT4;
253 data += 2 * (len + 1);
254 words_left -= len + 1;
256 /* save off server domain */
257 len = UniStrnlen((wchar_t *) data, words_left);
259 if (len > words_left)
262 kfree(ses->serverDomain);
263 ses->serverDomain = kzalloc(2 * (len + 1), GFP_KERNEL); /* BB FIXME wrong length */
264 if (ses->serverDomain != NULL) {
265 cifs_strfromUCS_le(ses->serverDomain, (__le16 *)data, len,
267 ses->serverDomain[2*len] = 0;
268 ses->serverDomain[(2*len) + 1] = 0;
270 data += 2 * (len + 1);
271 words_left -= len + 1;
273 cFYI(1, ("words left: %d", words_left));
278 static int decode_ascii_ssetup(char **pbcc_area, int bleft,
279 struct cifsSesInfo *ses,
280 const struct nls_table *nls_cp)
284 char *bcc_ptr = *pbcc_area;
286 cFYI(1, ("decode sessetup ascii. bleft %d", bleft));
288 len = strnlen(bcc_ptr, bleft);
292 kfree(ses->serverOS);
294 ses->serverOS = kzalloc(len + 1, GFP_KERNEL);
296 strncpy(ses->serverOS, bcc_ptr, len);
297 if (strncmp(ses->serverOS, "OS/2", 4) == 0) {
298 cFYI(1, ("OS/2 server"));
299 ses->flags |= CIFS_SES_OS2;
305 len = strnlen(bcc_ptr, bleft);
309 kfree(ses->serverNOS);
311 ses->serverNOS = kzalloc(len + 1, GFP_KERNEL);
313 strncpy(ses->serverNOS, bcc_ptr, len);
318 len = strnlen(bcc_ptr, bleft);
322 /* No domain field in LANMAN case. Domain is
323 returned by old servers in the SMB negprot response */
324 /* BB For newer servers which do not support Unicode,
325 but thus do return domain here we could add parsing
326 for it later, but it is not very important */
327 cFYI(1, ("ascii: bytes left %d", bleft));
333 CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, int first_time,
334 const struct nls_table *nls_cp)
338 struct smb_hdr *smb_buf;
341 SESSION_SETUP_ANDX *pSMB;
346 enum securityEnum type;
349 struct key *spnego_key = NULL;
354 type = ses->server->secType;
356 cFYI(1, ("sess setup type %d", type));
357 if (type == LANMAN) {
358 #ifndef CONFIG_CIFS_WEAK_PW_HASH
359 /* LANMAN and plaintext are less secure and off by default.
360 So we make this explicitly be turned on in kconfig (in the
361 build) and turned on at runtime (changed from the default)
362 in proc/fs/cifs or via mount parm. Unfortunately this is
363 needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
366 wct = 10; /* lanman 2 style sessionsetup */
367 } else if ((type == NTLM) || (type == NTLMv2)) {
368 /* For NTLMv2 failures eventually may need to retry NTLM */
369 wct = 13; /* old style NTLM sessionsetup */
370 } else /* same size: negotiate or auth, NTLMSSP or extended security */
373 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
378 pSMB = (SESSION_SETUP_ANDX *)smb_buf;
380 capabilities = cifs_ssetup_hdr(ses, pSMB);
382 /* we will send the SMB in three pieces:
383 a fixed length beginning part, an optional
384 SPNEGO blob (which can be zero length), and a
385 last part which will include the strings
386 and rest of bcc area. This allows us to avoid
387 a large buffer 17K allocation */
388 iov[0].iov_base = (char *)pSMB;
389 iov[0].iov_len = smb_buf->smb_buf_length + 4;
391 /* setting this here allows the code at the end of the function
392 to free the request buffer if there's an error */
393 resp_buf_type = CIFS_SMALL_BUFFER;
395 /* 2000 big enough to fit max user, domain, NOS name etc. */
396 str_area = kmalloc(2000, GFP_KERNEL);
397 if (str_area == NULL) {
403 ses->flags &= ~CIFS_SES_LANMAN;
405 iov[1].iov_base = NULL;
408 if (type == LANMAN) {
409 #ifdef CONFIG_CIFS_WEAK_PW_HASH
410 char lnm_session_key[CIFS_SESS_KEY_SIZE];
412 pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
414 /* no capabilities flags in old lanman negotiation */
416 pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
417 /* BB calculate hash with password */
418 /* and copy into bcc */
420 calc_lanman_hash(ses->password, ses->server->cryptKey,
421 ses->server->secMode & SECMODE_PW_ENCRYPT ?
422 true : false, lnm_session_key);
424 ses->flags |= CIFS_SES_LANMAN;
425 memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_SESS_KEY_SIZE);
426 bcc_ptr += CIFS_SESS_KEY_SIZE;
428 /* can not sign if LANMAN negotiated so no need
429 to calculate signing key? but what if server
430 changed to do higher than lanman dialect and
431 we reconnected would we ever calc signing_key? */
433 cFYI(1, ("Negotiating LANMAN setting up strings"));
434 /* Unicode not allowed for LANMAN dialects */
435 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
437 } else if (type == NTLM) {
438 char ntlm_session_key[CIFS_SESS_KEY_SIZE];
440 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
441 pSMB->req_no_secext.CaseInsensitivePasswordLength =
442 cpu_to_le16(CIFS_SESS_KEY_SIZE);
443 pSMB->req_no_secext.CaseSensitivePasswordLength =
444 cpu_to_le16(CIFS_SESS_KEY_SIZE);
446 /* calculate session key */
447 SMBNTencrypt(ses->password, ses->server->cryptKey,
450 if (first_time) /* should this be moved into common code
451 with similar ntlmv2 path? */
452 cifs_calculate_mac_key(&ses->server->mac_signing_key,
453 ntlm_session_key, ses->password);
454 /* copy session key */
456 memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE);
457 bcc_ptr += CIFS_SESS_KEY_SIZE;
458 memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE);
459 bcc_ptr += CIFS_SESS_KEY_SIZE;
460 if (ses->capabilities & CAP_UNICODE) {
461 /* unicode strings must be word aligned */
462 if (iov[0].iov_len % 2) {
466 unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
468 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
469 } else if (type == NTLMv2) {
471 kmalloc(sizeof(struct ntlmv2_resp), GFP_KERNEL);
473 /* BB FIXME change all users of v2_sess_key to
474 struct ntlmv2_resp */
476 if (v2_sess_key == NULL) {
481 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
483 /* LM2 password would be here if we supported it */
484 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
485 /* cpu_to_le16(LM2_SESS_KEY_SIZE); */
487 pSMB->req_no_secext.CaseSensitivePasswordLength =
488 cpu_to_le16(sizeof(struct ntlmv2_resp));
490 /* calculate session key */
491 setup_ntlmv2_rsp(ses, v2_sess_key, nls_cp);
492 if (first_time) /* should this be moved into common code
493 with similar ntlmv2 path? */
494 /* cifs_calculate_ntlmv2_mac_key(ses->server->mac_signing_key,
495 response BB FIXME, v2_sess_key); */
497 /* copy session key */
499 /* memcpy(bcc_ptr, (char *)ntlm_session_key,LM2_SESS_KEY_SIZE);
500 bcc_ptr += LM2_SESS_KEY_SIZE; */
501 memcpy(bcc_ptr, (char *)v2_sess_key,
502 sizeof(struct ntlmv2_resp));
503 bcc_ptr += sizeof(struct ntlmv2_resp);
505 if (ses->capabilities & CAP_UNICODE) {
506 if (iov[0].iov_len % 2) {
510 unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
512 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
513 } else if (type == Kerberos || type == MSKerberos) {
514 #ifdef CONFIG_CIFS_UPCALL
515 struct cifs_spnego_msg *msg;
516 spnego_key = cifs_get_spnego_key(ses);
517 if (IS_ERR(spnego_key)) {
518 rc = PTR_ERR(spnego_key);
523 msg = spnego_key->payload.data;
524 /* check version field to make sure that cifs.upcall is
525 sending us a response in an expected form */
526 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
527 cERROR(1, ("incorrect version of cifs.upcall (expected"
529 CIFS_SPNEGO_UPCALL_VERSION, msg->version));
533 /* bail out if key is too long */
534 if (msg->sesskey_len >
535 sizeof(ses->server->mac_signing_key.data.krb5)) {
536 cERROR(1, ("Kerberos signing key too long (%u bytes)",
542 ses->server->mac_signing_key.len = msg->sesskey_len;
543 memcpy(ses->server->mac_signing_key.data.krb5,
544 msg->data, msg->sesskey_len);
546 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
547 capabilities |= CAP_EXTENDED_SECURITY;
548 pSMB->req.Capabilities = cpu_to_le32(capabilities);
549 iov[1].iov_base = msg->data + msg->sesskey_len;
550 iov[1].iov_len = msg->secblob_len;
551 pSMB->req.SecurityBlobLength = cpu_to_le16(iov[1].iov_len);
553 if (ses->capabilities & CAP_UNICODE) {
554 /* unicode strings must be word aligned */
555 if ((iov[0].iov_len + iov[1].iov_len) % 2) {
559 unicode_oslm_strings(&bcc_ptr, nls_cp);
560 unicode_domain_string(&bcc_ptr, ses, nls_cp);
562 /* BB: is this right? */
563 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
564 #else /* ! CONFIG_CIFS_UPCALL */
565 cERROR(1, ("Kerberos negotiated but upcall support disabled!"));
568 #endif /* CONFIG_CIFS_UPCALL */
570 cERROR(1, ("secType %d not supported!", type));
575 iov[2].iov_base = str_area;
576 iov[2].iov_len = (long) bcc_ptr - (long) str_area;
578 count = iov[1].iov_len + iov[2].iov_len;
579 smb_buf->smb_buf_length += count;
581 BCC_LE(smb_buf) = cpu_to_le16(count);
583 rc = SendReceive2(xid, ses, iov, 3 /* num_iovecs */, &resp_buf_type,
584 CIFS_STD_OP /* not long */ | CIFS_LOG_ERROR);
585 /* SMB request buf freed in SendReceive2 */
587 cFYI(1, ("ssetup rc from sendrecv2 is %d", rc));
591 pSMB = (SESSION_SETUP_ANDX *)iov[0].iov_base;
592 smb_buf = (struct smb_hdr *)iov[0].iov_base;
594 if ((smb_buf->WordCount != 3) && (smb_buf->WordCount != 4)) {
596 cERROR(1, ("bad word count %d", smb_buf->WordCount));
599 action = le16_to_cpu(pSMB->resp.Action);
600 if (action & GUEST_LOGIN)
601 cFYI(1, ("Guest login")); /* BB mark SesInfo struct? */
602 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
603 cFYI(1, ("UID = %d ", ses->Suid));
604 /* response can have either 3 or 4 word count - Samba sends 3 */
605 /* and lanman response is 3 */
606 bytes_remaining = BCC(smb_buf);
607 bcc_ptr = pByteArea(smb_buf);
609 if (smb_buf->WordCount == 4) {
611 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
613 if (blob_len > bytes_remaining) {
614 cERROR(1, ("bad security blob length %d", blob_len));
618 bytes_remaining -= blob_len;
621 /* BB check if Unicode and decode strings */
622 if (smb_buf->Flags2 & SMBFLG2_UNICODE)
623 rc = decode_unicode_ssetup(&bcc_ptr, bytes_remaining,
626 rc = decode_ascii_ssetup(&bcc_ptr, bytes_remaining,
631 key_revoke(spnego_key);
635 if (resp_buf_type == CIFS_SMALL_BUFFER) {
636 cFYI(1, ("ssetup freeing small buf %p", iov[0].iov_base));
637 cifs_small_buf_release(iov[0].iov_base);
638 } else if (resp_buf_type == CIFS_LARGE_BUFFER)
639 cifs_buf_release(iov[0].iov_base);