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>
33 extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8,
36 static __u32 cifs_ssetup_hdr(struct cifsSesInfo *ses, SESSION_SETUP_ANDX *pSMB)
38 __u32 capabilities = 0;
40 /* init fields common to all four types of SessSetup */
41 /* note that header is initialized to zero in header_assemble */
42 pSMB->req.AndXCommand = 0xFF;
43 pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
44 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
46 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
48 /* BB verify whether signing required on neg or just on auth frame
51 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
52 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
54 if (ses->server->secMode &
55 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
56 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
58 if (ses->capabilities & CAP_UNICODE) {
59 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
60 capabilities |= CAP_UNICODE;
62 if (ses->capabilities & CAP_STATUS32) {
63 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
64 capabilities |= CAP_STATUS32;
66 if (ses->capabilities & CAP_DFS) {
67 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
68 capabilities |= CAP_DFS;
70 if (ses->capabilities & CAP_UNIX) {
71 capabilities |= CAP_UNIX;
74 /* BB check whether to init vcnum BB */
78 static void unicode_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses,
79 const struct nls_table *nls_cp)
81 char *bcc_ptr = *pbcc_area;
84 /* BB FIXME add check that strings total less
85 than 335 or will need to send them as arrays */
87 /* unicode strings, must be word aligned before the call */
88 /* if ((long) bcc_ptr % 2) {
93 if (ses->userName == NULL) {
97 } else { /* 300 should be long enough for any conceivable user name */
98 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->userName,
101 bcc_ptr += 2 * bytes_ret;
102 bcc_ptr += 2; /* account for null termination */
104 if (ses->domainName == NULL) {
105 /* Sending null domain better than using a bogus domain name (as
106 we did briefly in 2.6.18) since server will use its default */
111 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->domainName,
113 bcc_ptr += 2 * bytes_ret;
114 bcc_ptr += 2; /* account for null terminator */
116 /* Copy OS version */
117 bytes_ret = cifs_strtoUCS((__le16 *)bcc_ptr, "Linux version ", 32,
119 bcc_ptr += 2 * bytes_ret;
120 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, init_utsname()->release,
122 bcc_ptr += 2 * bytes_ret;
123 bcc_ptr += 2; /* trailing null */
125 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
127 bcc_ptr += 2 * bytes_ret;
128 bcc_ptr += 2; /* trailing null */
130 *pbcc_area = bcc_ptr;
133 static void ascii_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses,
134 const struct nls_table *nls_cp)
136 char *bcc_ptr = *pbcc_area;
139 /* BB what about null user mounts - check that we do this BB */
141 if (ses->userName == NULL) {
142 /* BB what about null user mounts - check that we do this BB */
143 } else { /* 300 should be long enough for any conceivable user name */
144 strncpy(bcc_ptr, ses->userName, 300);
146 /* BB improve check for overflow */
147 bcc_ptr += strnlen(ses->userName, 300);
149 bcc_ptr++; /* account for null termination */
153 if (ses->domainName != NULL) {
154 strncpy(bcc_ptr, ses->domainName, 256);
155 bcc_ptr += strnlen(ses->domainName, 256);
156 } /* else we will send a null domain name
157 so the server will default to its own domain */
161 /* BB check for overflow here */
163 strcpy(bcc_ptr, "Linux version ");
164 bcc_ptr += strlen("Linux version ");
165 strcpy(bcc_ptr, init_utsname()->release);
166 bcc_ptr += strlen(init_utsname()->release) + 1;
168 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
169 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
171 *pbcc_area = bcc_ptr;
174 static int decode_unicode_ssetup(char **pbcc_area, int bleft,
175 struct cifsSesInfo *ses,
176 const struct nls_table *nls_cp)
180 char *data = *pbcc_area;
184 cFYI(1, ("bleft %d", bleft));
187 /* SMB header is unaligned, so cifs servers word align start of
190 bleft--; /* Windows servers do not always double null terminate
191 their final Unicode string - in which case we
192 now will not attempt to decode the byte of junk
195 words_left = bleft / 2;
197 /* save off server operating system */
198 len = UniStrnlen((wchar_t *) data, words_left);
200 /* We look for obvious messed up bcc or strings in response so we do not go off
201 the end since (at least) WIN2K and Windows XP have a major bug in not null
202 terminating last Unicode string in response */
203 if (len >= words_left)
207 kfree(ses->serverOS);
208 /* UTF-8 string will not grow more than four times as big as UCS-16 */
209 ses->serverOS = kzalloc(4 * len, GFP_KERNEL);
210 if (ses->serverOS != NULL) {
211 cifs_strfromUCS_le(ses->serverOS, (__le16 *)data, len,
214 data += 2 * (len + 1);
215 words_left -= len + 1;
217 /* save off server network operating system */
218 len = UniStrnlen((wchar_t *) data, words_left);
220 if (len >= words_left)
224 kfree(ses->serverNOS);
225 ses->serverNOS = kzalloc(4 * len, GFP_KERNEL); /* BB this is wrong length FIXME BB */
226 if (ses->serverNOS != NULL) {
227 cifs_strfromUCS_le(ses->serverNOS, (__le16 *)data, len,
229 if (strncmp(ses->serverNOS, "NT LAN Manager 4", 16) == 0) {
230 cFYI(1, ("NT4 server"));
231 ses->flags |= CIFS_SES_NT4;
234 data += 2 * (len + 1);
235 words_left -= len + 1;
237 /* save off server domain */
238 len = UniStrnlen((wchar_t *) data, words_left);
240 if (len > words_left)
243 if (ses->serverDomain)
244 kfree(ses->serverDomain);
245 ses->serverDomain = kzalloc(2 * (len + 1), GFP_KERNEL); /* BB FIXME wrong length */
246 if (ses->serverDomain != NULL) {
247 cifs_strfromUCS_le(ses->serverDomain, (__le16 *)data, len,
249 ses->serverDomain[2*len] = 0;
250 ses->serverDomain[(2*len) + 1] = 0;
252 data += 2 * (len + 1);
253 words_left -= len + 1;
255 cFYI(1, ("words left: %d", words_left));
260 static int decode_ascii_ssetup(char **pbcc_area, int bleft,
261 struct cifsSesInfo *ses,
262 const struct nls_table *nls_cp)
266 char *bcc_ptr = *pbcc_area;
268 cFYI(1, ("decode sessetup ascii. bleft %d", bleft));
270 len = strnlen(bcc_ptr, bleft);
275 kfree(ses->serverOS);
277 ses->serverOS = kzalloc(len + 1, GFP_KERNEL);
279 strncpy(ses->serverOS, bcc_ptr, len);
280 if (strncmp(ses->serverOS, "OS/2", 4) == 0) {
281 cFYI(1, ("OS/2 server"));
282 ses->flags |= CIFS_SES_OS2;
288 len = strnlen(bcc_ptr, bleft);
293 kfree(ses->serverNOS);
295 ses->serverNOS = kzalloc(len + 1, GFP_KERNEL);
297 strncpy(ses->serverNOS, bcc_ptr, len);
302 len = strnlen(bcc_ptr, bleft);
306 /* No domain field in LANMAN case. Domain is
307 returned by old servers in the SMB negprot response */
308 /* BB For newer servers which do not support Unicode,
309 but thus do return domain here we could add parsing
310 for it later, but it is not very important */
311 cFYI(1, ("ascii: bytes left %d", bleft));
317 CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, int first_time,
318 const struct nls_table *nls_cp)
322 struct smb_hdr *smb_buf;
325 SESSION_SETUP_ANDX *pSMB;
328 int resp_buf_type = 0;
330 enum securityEnum type;
337 type = ses->server->secType;
339 cFYI(1, ("sess setup type %d", type));
340 if (type == LANMAN) {
341 #ifndef CONFIG_CIFS_WEAK_PW_HASH
342 /* LANMAN and plaintext are less secure and off by default.
343 So we make this explicitly be turned on in kconfig (in the
344 build) and turned on at runtime (changed from the default)
345 in proc/fs/cifs or via mount parm. Unfortunately this is
346 needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
349 wct = 10; /* lanman 2 style sessionsetup */
350 } else if ((type == NTLM) || (type == NTLMv2)) {
351 /* For NTLMv2 failures eventually may need to retry NTLM */
352 wct = 13; /* old style NTLM sessionsetup */
353 } else /* same size: negotiate or auth, NTLMSSP or extended security */
356 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
361 pSMB = (SESSION_SETUP_ANDX *)smb_buf;
363 capabilities = cifs_ssetup_hdr(ses, pSMB);
365 /* we will send the SMB in two pieces,
366 a fixed length beginning part, and a
367 second part which will include the strings
368 and rest of bcc area, in order to avoid having
369 to do a large buffer 17K allocation */
370 iov[0].iov_base = (char *)pSMB;
371 iov[0].iov_len = smb_buf->smb_buf_length + 4;
373 /* 2000 big enough to fit max user, domain, NOS name etc. */
374 str_area = kmalloc(2000, GFP_KERNEL);
375 if (str_area == NULL) {
376 cifs_small_buf_release(smb_buf);
381 ses->flags &= ~CIFS_SES_LANMAN;
383 if (type == LANMAN) {
384 #ifdef CONFIG_CIFS_WEAK_PW_HASH
385 char lnm_session_key[CIFS_SESS_KEY_SIZE];
387 /* no capabilities flags in old lanman negotiation */
389 pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
390 /* BB calculate hash with password */
391 /* and copy into bcc */
393 calc_lanman_hash(ses, lnm_session_key);
394 ses->flags |= CIFS_SES_LANMAN;
395 /* #ifdef CONFIG_CIFS_DEBUG2
396 cifs_dump_mem("cryptkey: ",ses->server->cryptKey,
399 memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_SESS_KEY_SIZE);
400 bcc_ptr += CIFS_SESS_KEY_SIZE;
402 /* can not sign if LANMAN negotiated so no need
403 to calculate signing key? but what if server
404 changed to do higher than lanman dialect and
405 we reconnected would we ever calc signing_key? */
407 cFYI(1, ("Negotiating LANMAN setting up strings"));
408 /* Unicode not allowed for LANMAN dialects */
409 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
411 } else if (type == NTLM) {
412 char ntlm_session_key[CIFS_SESS_KEY_SIZE];
414 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
415 pSMB->req_no_secext.CaseInsensitivePasswordLength =
416 cpu_to_le16(CIFS_SESS_KEY_SIZE);
417 pSMB->req_no_secext.CaseSensitivePasswordLength =
418 cpu_to_le16(CIFS_SESS_KEY_SIZE);
420 /* calculate session key */
421 SMBNTencrypt(ses->password, ses->server->cryptKey,
424 if (first_time) /* should this be moved into common code
425 with similar ntlmv2 path? */
426 cifs_calculate_mac_key(&ses->server->mac_signing_key,
427 ntlm_session_key, ses->password);
428 /* copy session key */
430 memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE);
431 bcc_ptr += CIFS_SESS_KEY_SIZE;
432 memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE);
433 bcc_ptr += CIFS_SESS_KEY_SIZE;
434 if (ses->capabilities & CAP_UNICODE) {
435 /* unicode strings must be word aligned */
436 if (iov[0].iov_len % 2) {
440 unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
442 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
443 } else if (type == NTLMv2) {
445 kmalloc(sizeof(struct ntlmv2_resp), GFP_KERNEL);
447 /* BB FIXME change all users of v2_sess_key to
448 struct ntlmv2_resp */
450 if (v2_sess_key == NULL) {
451 cifs_small_buf_release(smb_buf);
455 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
457 /* LM2 password would be here if we supported it */
458 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
459 /* cpu_to_le16(LM2_SESS_KEY_SIZE); */
461 pSMB->req_no_secext.CaseSensitivePasswordLength =
462 cpu_to_le16(sizeof(struct ntlmv2_resp));
464 /* calculate session key */
465 setup_ntlmv2_rsp(ses, v2_sess_key, nls_cp);
466 if (first_time) /* should this be moved into common code
467 with similar ntlmv2 path? */
468 /* cifs_calculate_ntlmv2_mac_key(ses->server->mac_signing_key,
469 response BB FIXME, v2_sess_key); */
471 /* copy session key */
473 /* memcpy(bcc_ptr, (char *)ntlm_session_key,LM2_SESS_KEY_SIZE);
474 bcc_ptr += LM2_SESS_KEY_SIZE; */
475 memcpy(bcc_ptr, (char *)v2_sess_key,
476 sizeof(struct ntlmv2_resp));
477 bcc_ptr += sizeof(struct ntlmv2_resp);
479 if (ses->capabilities & CAP_UNICODE) {
480 if (iov[0].iov_len % 2) {
483 unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
485 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
486 } else /* NTLMSSP or SPNEGO */ {
487 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
488 capabilities |= CAP_EXTENDED_SECURITY;
489 pSMB->req.Capabilities = cpu_to_le32(capabilities);
490 /* BB set password lengths */
493 count = (long) bcc_ptr - (long) str_area;
494 smb_buf->smb_buf_length += count;
496 BCC_LE(smb_buf) = cpu_to_le16(count);
498 iov[1].iov_base = str_area;
499 iov[1].iov_len = count;
500 rc = SendReceive2(xid, ses, iov, 2 /* num_iovecs */, &resp_buf_type, 0);
501 /* SMB request buf freed in SendReceive2 */
503 cFYI(1, ("ssetup rc from sendrecv2 is %d", rc));
507 pSMB = (SESSION_SETUP_ANDX *)iov[0].iov_base;
508 smb_buf = (struct smb_hdr *)iov[0].iov_base;
510 if ((smb_buf->WordCount != 3) && (smb_buf->WordCount != 4)) {
512 cERROR(1, ("bad word count %d", smb_buf->WordCount));
515 action = le16_to_cpu(pSMB->resp.Action);
516 if (action & GUEST_LOGIN)
517 cFYI(1, ("Guest login")); /* BB mark SesInfo struct? */
518 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
519 cFYI(1, ("UID = %d ", ses->Suid));
520 /* response can have either 3 or 4 word count - Samba sends 3 */
521 /* and lanman response is 3 */
522 bytes_remaining = BCC(smb_buf);
523 bcc_ptr = pByteArea(smb_buf);
525 if (smb_buf->WordCount == 4) {
527 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
529 if (blob_len > bytes_remaining) {
530 cERROR(1, ("bad security blob length %d", blob_len));
534 bytes_remaining -= blob_len;
537 /* BB check if Unicode and decode strings */
538 if (smb_buf->Flags2 & SMBFLG2_UNICODE)
539 rc = decode_unicode_ssetup(&bcc_ptr, bytes_remaining,
542 rc = decode_ascii_ssetup(&bcc_ptr, bytes_remaining,
547 if (resp_buf_type == CIFS_SMALL_BUFFER) {
548 cFYI(1, ("ssetup freeing small buf %p", iov[0].iov_base));
549 cifs_small_buf_release(iov[0].iov_base);
550 } else if (resp_buf_type == CIFS_LARGE_BUFFER)
551 cifs_buf_release(iov[0].iov_base);