thinkpad-acpi: R52 brightness_mode has been confirmed
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / cifs / sess.c
blob52c72ea97ef65324f079d1507576fce16f6e3f8a
1 /*
2 * fs/cifs/sess.c
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
24 #include "cifspdu.h"
25 #include "cifsglob.h"
26 #include "cifsproto.h"
27 #include "cifs_unicode.h"
28 #include "cifs_debug.h"
29 #include "ntlmssp.h"
30 #include "nterr.h"
31 #include <linux/utsname.h>
32 #include "cifs_spnego.h"
34 extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8,
35 unsigned char *p24);
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
50 (and NTLM case) */
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 */
75 return capabilities;
78 static void
79 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
81 char *bcc_ptr = *pbcc_area;
82 int bytes_ret = 0;
84 /* Copy OS version */
85 bytes_ret = cifs_strtoUCS((__le16 *)bcc_ptr, "Linux version ", 32,
86 nls_cp);
87 bcc_ptr += 2 * bytes_ret;
88 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, init_utsname()->release,
89 32, nls_cp);
90 bcc_ptr += 2 * bytes_ret;
91 bcc_ptr += 2; /* trailing null */
93 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
94 32, nls_cp);
95 bcc_ptr += 2 * bytes_ret;
96 bcc_ptr += 2; /* trailing null */
98 *pbcc_area = bcc_ptr;
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;
105 int bytes_ret = 0;
107 /* copy domain */
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 */
111 *bcc_ptr = 0;
112 *(bcc_ptr+1) = 0;
113 bytes_ret = 0;
114 } else
115 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->domainName,
116 256, nls_cp);
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;
128 int bytes_ret = 0;
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) {
135 *bcc_ptr = 0;
136 bcc_ptr++;
137 } */
138 /* copy user */
139 if (ses->userName == NULL) {
140 /* null user mount */
141 *bcc_ptr = 0;
142 *(bcc_ptr+1) = 0;
143 } else { /* 300 should be long enough for any conceivable user name */
144 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->userName,
145 300, nls_cp);
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;
161 /* copy user */
162 /* BB what about null user mounts - check that we do this BB */
163 /* copy user */
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);
171 *bcc_ptr = 0;
172 bcc_ptr++; /* account for null termination */
174 /* copy domain */
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 */
181 *bcc_ptr = 0;
182 bcc_ptr++;
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)
201 int rc = 0;
202 int words_left, len;
203 char *data = *pbcc_area;
205 cFYI(1, ("bleft %d", bleft));
208 * Windows servers do not always double null terminate their final
209 * Unicode string. Check to see if there are an uneven number of bytes
210 * left. If so, then add an extra NULL pad byte to the end of the
211 * response.
213 * See section 2.7.2 in "Implementing CIFS" for details
215 if (bleft % 2) {
216 data[bleft] = 0;
217 ++bleft;
220 words_left = bleft / 2;
222 /* save off server operating system */
223 len = UniStrnlen((wchar_t *) data, words_left);
225 if (len >= words_left)
226 return rc;
228 kfree(ses->serverOS);
229 /* UTF-8 string will not grow more than four times as big as UCS-16 */
230 ses->serverOS = kzalloc((4 * len) + 2 /* trailing null */, GFP_KERNEL);
231 if (ses->serverOS != NULL)
232 cifs_strfromUCS_le(ses->serverOS, (__le16 *)data, len, nls_cp);
233 data += 2 * (len + 1);
234 words_left -= len + 1;
236 /* save off server network operating system */
237 len = UniStrnlen((wchar_t *) data, words_left);
239 if (len >= words_left)
240 return rc;
242 kfree(ses->serverNOS);
243 ses->serverNOS = kzalloc((4 * len) + 2 /* trailing null */, GFP_KERNEL);
244 if (ses->serverNOS != NULL) {
245 cifs_strfromUCS_le(ses->serverNOS, (__le16 *)data, len,
246 nls_cp);
247 if (strncmp(ses->serverNOS, "NT LAN Manager 4", 16) == 0) {
248 cFYI(1, ("NT4 server"));
249 ses->flags |= CIFS_SES_NT4;
252 data += 2 * (len + 1);
253 words_left -= len + 1;
255 /* save off server domain */
256 len = UniStrnlen((wchar_t *) data, words_left);
258 if (len > words_left)
259 return rc;
261 kfree(ses->serverDomain);
262 ses->serverDomain = kzalloc((4 * len) + 2, GFP_KERNEL);
263 if (ses->serverDomain != NULL)
264 cifs_strfromUCS_le(ses->serverDomain, (__le16 *)data, len,
265 nls_cp);
266 data += 2 * (len + 1);
267 words_left -= len + 1;
269 cFYI(1, ("words left: %d", words_left));
271 return rc;
274 static int decode_ascii_ssetup(char **pbcc_area, int bleft,
275 struct cifsSesInfo *ses,
276 const struct nls_table *nls_cp)
278 int rc = 0;
279 int len;
280 char *bcc_ptr = *pbcc_area;
282 cFYI(1, ("decode sessetup ascii. bleft %d", bleft));
284 len = strnlen(bcc_ptr, bleft);
285 if (len >= bleft)
286 return rc;
288 kfree(ses->serverOS);
290 ses->serverOS = kzalloc(len + 1, GFP_KERNEL);
291 if (ses->serverOS)
292 strncpy(ses->serverOS, bcc_ptr, len);
293 if (strncmp(ses->serverOS, "OS/2", 4) == 0) {
294 cFYI(1, ("OS/2 server"));
295 ses->flags |= CIFS_SES_OS2;
298 bcc_ptr += len + 1;
299 bleft -= len + 1;
301 len = strnlen(bcc_ptr, bleft);
302 if (len >= bleft)
303 return rc;
305 kfree(ses->serverNOS);
307 ses->serverNOS = kzalloc(len + 1, GFP_KERNEL);
308 if (ses->serverNOS)
309 strncpy(ses->serverNOS, bcc_ptr, len);
311 bcc_ptr += len + 1;
312 bleft -= len + 1;
314 len = strnlen(bcc_ptr, bleft);
315 if (len > bleft)
316 return rc;
318 /* No domain field in LANMAN case. Domain is
319 returned by old servers in the SMB negprot response */
320 /* BB For newer servers which do not support Unicode,
321 but thus do return domain here we could add parsing
322 for it later, but it is not very important */
323 cFYI(1, ("ascii: bytes left %d", bleft));
325 return rc;
329 CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, int first_time,
330 const struct nls_table *nls_cp)
332 int rc = 0;
333 int wct;
334 struct smb_hdr *smb_buf;
335 char *bcc_ptr;
336 char *str_area;
337 SESSION_SETUP_ANDX *pSMB;
338 __u32 capabilities;
339 int count;
340 int resp_buf_type;
341 struct kvec iov[3];
342 enum securityEnum type;
343 __u16 action;
344 int bytes_remaining;
345 struct key *spnego_key = NULL;
347 if (ses == NULL)
348 return -EINVAL;
350 type = ses->server->secType;
352 cFYI(1, ("sess setup type %d", type));
353 if (type == LANMAN) {
354 #ifndef CONFIG_CIFS_WEAK_PW_HASH
355 /* LANMAN and plaintext are less secure and off by default.
356 So we make this explicitly be turned on in kconfig (in the
357 build) and turned on at runtime (changed from the default)
358 in proc/fs/cifs or via mount parm. Unfortunately this is
359 needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
360 return -EOPNOTSUPP;
361 #endif
362 wct = 10; /* lanman 2 style sessionsetup */
363 } else if ((type == NTLM) || (type == NTLMv2)) {
364 /* For NTLMv2 failures eventually may need to retry NTLM */
365 wct = 13; /* old style NTLM sessionsetup */
366 } else /* same size: negotiate or auth, NTLMSSP or extended security */
367 wct = 12;
369 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
370 (void **)&smb_buf);
371 if (rc)
372 return rc;
374 pSMB = (SESSION_SETUP_ANDX *)smb_buf;
376 capabilities = cifs_ssetup_hdr(ses, pSMB);
378 /* we will send the SMB in three pieces:
379 a fixed length beginning part, an optional
380 SPNEGO blob (which can be zero length), and a
381 last part which will include the strings
382 and rest of bcc area. This allows us to avoid
383 a large buffer 17K allocation */
384 iov[0].iov_base = (char *)pSMB;
385 iov[0].iov_len = smb_buf->smb_buf_length + 4;
387 /* setting this here allows the code at the end of the function
388 to free the request buffer if there's an error */
389 resp_buf_type = CIFS_SMALL_BUFFER;
391 /* 2000 big enough to fit max user, domain, NOS name etc. */
392 str_area = kmalloc(2000, GFP_KERNEL);
393 if (str_area == NULL) {
394 rc = -ENOMEM;
395 goto ssetup_exit;
397 bcc_ptr = str_area;
399 ses->flags &= ~CIFS_SES_LANMAN;
401 iov[1].iov_base = NULL;
402 iov[1].iov_len = 0;
404 if (type == LANMAN) {
405 #ifdef CONFIG_CIFS_WEAK_PW_HASH
406 char lnm_session_key[CIFS_SESS_KEY_SIZE];
408 pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
410 /* no capabilities flags in old lanman negotiation */
412 pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
413 /* BB calculate hash with password */
414 /* and copy into bcc */
416 calc_lanman_hash(ses, lnm_session_key);
417 ses->flags |= CIFS_SES_LANMAN;
418 memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_SESS_KEY_SIZE);
419 bcc_ptr += CIFS_SESS_KEY_SIZE;
421 /* can not sign if LANMAN negotiated so no need
422 to calculate signing key? but what if server
423 changed to do higher than lanman dialect and
424 we reconnected would we ever calc signing_key? */
426 cFYI(1, ("Negotiating LANMAN setting up strings"));
427 /* Unicode not allowed for LANMAN dialects */
428 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
429 #endif
430 } else if (type == NTLM) {
431 char ntlm_session_key[CIFS_SESS_KEY_SIZE];
433 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
434 pSMB->req_no_secext.CaseInsensitivePasswordLength =
435 cpu_to_le16(CIFS_SESS_KEY_SIZE);
436 pSMB->req_no_secext.CaseSensitivePasswordLength =
437 cpu_to_le16(CIFS_SESS_KEY_SIZE);
439 /* calculate session key */
440 SMBNTencrypt(ses->password, ses->server->cryptKey,
441 ntlm_session_key);
443 if (first_time) /* should this be moved into common code
444 with similar ntlmv2 path? */
445 cifs_calculate_mac_key(&ses->server->mac_signing_key,
446 ntlm_session_key, ses->password);
447 /* copy session key */
449 memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE);
450 bcc_ptr += CIFS_SESS_KEY_SIZE;
451 memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE);
452 bcc_ptr += CIFS_SESS_KEY_SIZE;
453 if (ses->capabilities & CAP_UNICODE) {
454 /* unicode strings must be word aligned */
455 if (iov[0].iov_len % 2) {
456 *bcc_ptr = 0;
457 bcc_ptr++;
459 unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
460 } else
461 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
462 } else if (type == NTLMv2) {
463 char *v2_sess_key =
464 kmalloc(sizeof(struct ntlmv2_resp), GFP_KERNEL);
466 /* BB FIXME change all users of v2_sess_key to
467 struct ntlmv2_resp */
469 if (v2_sess_key == NULL) {
470 rc = -ENOMEM;
471 goto ssetup_exit;
474 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
476 /* LM2 password would be here if we supported it */
477 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
478 /* cpu_to_le16(LM2_SESS_KEY_SIZE); */
480 pSMB->req_no_secext.CaseSensitivePasswordLength =
481 cpu_to_le16(sizeof(struct ntlmv2_resp));
483 /* calculate session key */
484 setup_ntlmv2_rsp(ses, v2_sess_key, nls_cp);
485 /* FIXME: calculate MAC key */
486 memcpy(bcc_ptr, (char *)v2_sess_key,
487 sizeof(struct ntlmv2_resp));
488 bcc_ptr += sizeof(struct ntlmv2_resp);
489 kfree(v2_sess_key);
490 if (ses->capabilities & CAP_UNICODE) {
491 if (iov[0].iov_len % 2) {
492 *bcc_ptr = 0;
493 bcc_ptr++;
495 unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
496 } else
497 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
498 } else if (type == Kerberos || type == MSKerberos) {
499 #ifdef CONFIG_CIFS_UPCALL
500 struct cifs_spnego_msg *msg;
501 spnego_key = cifs_get_spnego_key(ses);
502 if (IS_ERR(spnego_key)) {
503 rc = PTR_ERR(spnego_key);
504 spnego_key = NULL;
505 goto ssetup_exit;
508 msg = spnego_key->payload.data;
509 /* check version field to make sure that cifs.upcall is
510 sending us a response in an expected form */
511 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
512 cERROR(1, ("incorrect version of cifs.upcall (expected"
513 " %d but got %d)",
514 CIFS_SPNEGO_UPCALL_VERSION, msg->version));
515 rc = -EKEYREJECTED;
516 goto ssetup_exit;
518 /* bail out if key is too long */
519 if (msg->sesskey_len >
520 sizeof(ses->server->mac_signing_key.data.krb5)) {
521 cERROR(1, ("Kerberos signing key too long (%u bytes)",
522 msg->sesskey_len));
523 rc = -EOVERFLOW;
524 goto ssetup_exit;
526 if (first_time) {
527 ses->server->mac_signing_key.len = msg->sesskey_len;
528 memcpy(ses->server->mac_signing_key.data.krb5,
529 msg->data, msg->sesskey_len);
531 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
532 capabilities |= CAP_EXTENDED_SECURITY;
533 pSMB->req.Capabilities = cpu_to_le32(capabilities);
534 iov[1].iov_base = msg->data + msg->sesskey_len;
535 iov[1].iov_len = msg->secblob_len;
536 pSMB->req.SecurityBlobLength = cpu_to_le16(iov[1].iov_len);
538 if (ses->capabilities & CAP_UNICODE) {
539 /* unicode strings must be word aligned */
540 if ((iov[0].iov_len + iov[1].iov_len) % 2) {
541 *bcc_ptr = 0;
542 bcc_ptr++;
544 unicode_oslm_strings(&bcc_ptr, nls_cp);
545 unicode_domain_string(&bcc_ptr, ses, nls_cp);
546 } else
547 /* BB: is this right? */
548 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
549 #else /* ! CONFIG_CIFS_UPCALL */
550 cERROR(1, ("Kerberos negotiated but upcall support disabled!"));
551 rc = -ENOSYS;
552 goto ssetup_exit;
553 #endif /* CONFIG_CIFS_UPCALL */
554 } else {
555 cERROR(1, ("secType %d not supported!", type));
556 rc = -ENOSYS;
557 goto ssetup_exit;
560 iov[2].iov_base = str_area;
561 iov[2].iov_len = (long) bcc_ptr - (long) str_area;
563 count = iov[1].iov_len + iov[2].iov_len;
564 smb_buf->smb_buf_length += count;
566 BCC_LE(smb_buf) = cpu_to_le16(count);
568 rc = SendReceive2(xid, ses, iov, 3 /* num_iovecs */, &resp_buf_type,
569 CIFS_STD_OP /* not long */ | CIFS_LOG_ERROR);
570 /* SMB request buf freed in SendReceive2 */
572 cFYI(1, ("ssetup rc from sendrecv2 is %d", rc));
573 if (rc)
574 goto ssetup_exit;
576 pSMB = (SESSION_SETUP_ANDX *)iov[0].iov_base;
577 smb_buf = (struct smb_hdr *)iov[0].iov_base;
579 if ((smb_buf->WordCount != 3) && (smb_buf->WordCount != 4)) {
580 rc = -EIO;
581 cERROR(1, ("bad word count %d", smb_buf->WordCount));
582 goto ssetup_exit;
584 action = le16_to_cpu(pSMB->resp.Action);
585 if (action & GUEST_LOGIN)
586 cFYI(1, ("Guest login")); /* BB mark SesInfo struct? */
587 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
588 cFYI(1, ("UID = %d ", ses->Suid));
589 /* response can have either 3 or 4 word count - Samba sends 3 */
590 /* and lanman response is 3 */
591 bytes_remaining = BCC(smb_buf);
592 bcc_ptr = pByteArea(smb_buf);
594 if (smb_buf->WordCount == 4) {
595 __u16 blob_len;
596 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
597 bcc_ptr += blob_len;
598 if (blob_len > bytes_remaining) {
599 cERROR(1, ("bad security blob length %d", blob_len));
600 rc = -EINVAL;
601 goto ssetup_exit;
603 bytes_remaining -= blob_len;
606 /* BB check if Unicode and decode strings */
607 if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
608 /* unicode string area must be word-aligned */
609 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
610 ++bcc_ptr;
611 --bytes_remaining;
613 rc = decode_unicode_ssetup(&bcc_ptr, bytes_remaining,
614 ses, nls_cp);
615 } else {
616 rc = decode_ascii_ssetup(&bcc_ptr, bytes_remaining,
617 ses, nls_cp);
620 ssetup_exit:
621 if (spnego_key)
622 key_put(spnego_key);
623 kfree(str_area);
624 if (resp_buf_type == CIFS_SMALL_BUFFER) {
625 cFYI(1, ("ssetup freeing small buf %p", iov[0].iov_base));
626 cifs_small_buf_release(iov[0].iov_base);
627 } else if (resp_buf_type == CIFS_LARGE_BUFFER)
628 cifs_buf_release(iov[0].iov_base);
630 return rc;