4 * SMB/CIFS session setup handling routines
6 * Copyright (c) International Business Machines Corp., 2006
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
& (SECMODE_SIGN_REQUIRED
| SECMODE_SIGN_ENABLED
))
55 pSMB
->req
.hdr
.Flags2
|= SMBFLG2_SECURITY_SIGNATURE
;
57 if (ses
->capabilities
& CAP_UNICODE
) {
58 pSMB
->req
.hdr
.Flags2
|= SMBFLG2_UNICODE
;
59 capabilities
|= CAP_UNICODE
;
61 if (ses
->capabilities
& CAP_STATUS32
) {
62 pSMB
->req
.hdr
.Flags2
|= SMBFLG2_ERR_STATUS
;
63 capabilities
|= CAP_STATUS32
;
65 if (ses
->capabilities
& CAP_DFS
) {
66 pSMB
->req
.hdr
.Flags2
|= SMBFLG2_DFS
;
67 capabilities
|= CAP_DFS
;
69 if (ses
->capabilities
& CAP_UNIX
) {
70 capabilities
|= CAP_UNIX
;
73 /* BB check whether to init vcnum BB */
77 static void unicode_ssetup_strings(char ** pbcc_area
, struct cifsSesInfo
*ses
,
78 const struct nls_table
* nls_cp
)
80 char * bcc_ptr
= *pbcc_area
;
83 /* BB FIXME add check that strings total less
84 than 335 or will need to send them as arrays */
86 /* unicode strings, must be word aligned before the call */
87 /* if ((long) bcc_ptr % 2) {
92 if(ses
->userName
== NULL
) {
96 } else { /* 300 should be long enough for any conceivable user name */
97 bytes_ret
= cifs_strtoUCS((__le16
*) bcc_ptr
, ses
->userName
,
100 bcc_ptr
+= 2 * bytes_ret
;
101 bcc_ptr
+= 2; /* account for null termination */
103 if(ses
->domainName
== NULL
) {
104 /* Sending null domain better than using a bogus domain name (as
105 we did briefly in 2.6.18) since server will use its default */
110 bytes_ret
= cifs_strtoUCS((__le16
*) bcc_ptr
, ses
->domainName
,
112 bcc_ptr
+= 2 * bytes_ret
;
113 bcc_ptr
+= 2; /* account for null terminator */
115 /* Copy OS version */
116 bytes_ret
= cifs_strtoUCS((__le16
*)bcc_ptr
, "Linux version ", 32,
118 bcc_ptr
+= 2 * bytes_ret
;
119 bytes_ret
= cifs_strtoUCS((__le16
*) bcc_ptr
, init_utsname()->release
,
121 bcc_ptr
+= 2 * bytes_ret
;
122 bcc_ptr
+= 2; /* trailing null */
124 bytes_ret
= cifs_strtoUCS((__le16
*) bcc_ptr
, CIFS_NETWORK_OPSYS
,
126 bcc_ptr
+= 2 * bytes_ret
;
127 bcc_ptr
+= 2; /* trailing null */
129 *pbcc_area
= bcc_ptr
;
132 static void ascii_ssetup_strings(char ** pbcc_area
, struct cifsSesInfo
*ses
,
133 const struct nls_table
* nls_cp
)
135 char * bcc_ptr
= *pbcc_area
;
138 /* BB what about null user mounts - check that we do this BB */
140 if(ses
->userName
== NULL
) {
141 /* BB what about null user mounts - check that we do this BB */
142 } else { /* 300 should be long enough for any conceivable user name */
143 strncpy(bcc_ptr
, ses
->userName
, 300);
145 /* BB improve check for overflow */
146 bcc_ptr
+= strnlen(ses
->userName
, 300);
148 bcc_ptr
++; /* account for null termination */
152 if(ses
->domainName
!= NULL
) {
153 strncpy(bcc_ptr
, ses
->domainName
, 256);
154 bcc_ptr
+= strnlen(ses
->domainName
, 256);
155 } /* else we will send a null domain name
156 so the server will default to its own domain */
160 /* BB check for overflow here */
162 strcpy(bcc_ptr
, "Linux version ");
163 bcc_ptr
+= strlen("Linux version ");
164 strcpy(bcc_ptr
, init_utsname()->release
);
165 bcc_ptr
+= strlen(init_utsname()->release
) + 1;
167 strcpy(bcc_ptr
, CIFS_NETWORK_OPSYS
);
168 bcc_ptr
+= strlen(CIFS_NETWORK_OPSYS
) + 1;
170 *pbcc_area
= bcc_ptr
;
173 static int decode_unicode_ssetup(char ** pbcc_area
, int bleft
, struct cifsSesInfo
*ses
,
174 const struct nls_table
* nls_cp
)
178 char * data
= *pbcc_area
;
182 cFYI(1,("bleft %d",bleft
));
185 /* SMB header is unaligned, so cifs servers word align start of
188 bleft
--; /* Windows servers do not always double null terminate
189 their final Unicode string - in which case we
190 now will not attempt to decode the byte of junk
193 words_left
= bleft
/ 2;
195 /* save off server operating system */
196 len
= UniStrnlen((wchar_t *) data
, words_left
);
198 /* We look for obvious messed up bcc or strings in response so we do not go off
199 the end since (at least) WIN2K and Windows XP have a major bug in not null
200 terminating last Unicode string in response */
201 if(len
>= words_left
)
205 kfree(ses
->serverOS
);
206 /* UTF-8 string will not grow more than four times as big as UCS-16 */
207 ses
->serverOS
= kzalloc(4 * len
, GFP_KERNEL
);
208 if(ses
->serverOS
!= NULL
) {
209 cifs_strfromUCS_le(ses
->serverOS
, (__le16
*)data
, len
,
212 data
+= 2 * (len
+ 1);
213 words_left
-= len
+ 1;
215 /* save off server network operating system */
216 len
= UniStrnlen((wchar_t *) data
, words_left
);
218 if(len
>= words_left
)
222 kfree(ses
->serverNOS
);
223 ses
->serverNOS
= kzalloc(4 * len
, GFP_KERNEL
); /* BB this is wrong length FIXME BB */
224 if(ses
->serverNOS
!= NULL
) {
225 cifs_strfromUCS_le(ses
->serverNOS
, (__le16
*)data
, len
,
227 if(strncmp(ses
->serverNOS
, "NT LAN Manager 4",16) == 0) {
228 cFYI(1,("NT4 server"));
229 ses
->flags
|= CIFS_SES_NT4
;
232 data
+= 2 * (len
+ 1);
233 words_left
-= len
+ 1;
235 /* save off server domain */
236 len
= UniStrnlen((wchar_t *) data
, words_left
);
241 if(ses
->serverDomain
)
242 kfree(ses
->serverDomain
);
243 ses
->serverDomain
= kzalloc(2 * (len
+ 1), GFP_KERNEL
); /* BB FIXME wrong length */
244 if(ses
->serverDomain
!= NULL
) {
245 cifs_strfromUCS_le(ses
->serverDomain
, (__le16
*)data
, len
,
247 ses
->serverDomain
[2*len
] = 0;
248 ses
->serverDomain
[(2*len
) + 1] = 0;
250 data
+= 2 * (len
+ 1);
251 words_left
-= len
+ 1;
253 cFYI(1,("words left: %d",words_left
));
258 static int decode_ascii_ssetup(char ** pbcc_area
, int bleft
, struct cifsSesInfo
*ses
,
259 const struct nls_table
* nls_cp
)
263 char * bcc_ptr
= *pbcc_area
;
265 cFYI(1,("decode sessetup ascii. bleft %d", bleft
));
267 len
= strnlen(bcc_ptr
, bleft
);
272 kfree(ses
->serverOS
);
274 ses
->serverOS
= kzalloc(len
+ 1, GFP_KERNEL
);
276 strncpy(ses
->serverOS
, bcc_ptr
, len
);
277 if(strncmp(ses
->serverOS
, "OS/2",4) == 0) {
278 cFYI(1,("OS/2 server"));
279 ses
->flags
|= CIFS_SES_OS2
;
285 len
= strnlen(bcc_ptr
, bleft
);
290 kfree(ses
->serverNOS
);
292 ses
->serverNOS
= kzalloc(len
+ 1, GFP_KERNEL
);
294 strncpy(ses
->serverNOS
, bcc_ptr
, len
);
299 len
= strnlen(bcc_ptr
, bleft
);
303 /* No domain field in LANMAN case. Domain is
304 returned by old servers in the SMB negprot response */
305 /* BB For newer servers which do not support Unicode,
306 but thus do return domain here we could add parsing
307 for it later, but it is not very important */
308 cFYI(1,("ascii: bytes left %d",bleft
));
314 CIFS_SessSetup(unsigned int xid
, struct cifsSesInfo
*ses
, int first_time
,
315 const struct nls_table
*nls_cp
)
319 struct smb_hdr
*smb_buf
;
322 SESSION_SETUP_ANDX
*pSMB
;
325 int resp_buf_type
= 0;
327 enum securityEnum type
;
334 type
= ses
->server
->secType
;
336 cFYI(1,("sess setup type %d",type
));
338 #ifndef CONFIG_CIFS_WEAK_PW_HASH
339 /* LANMAN and plaintext are less secure and off by default.
340 So we make this explicitly be turned on in kconfig (in the
341 build) and turned on at runtime (changed from the default)
342 in proc/fs/cifs or via mount parm. Unfortunately this is
343 needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
346 wct
= 10; /* lanman 2 style sessionsetup */
347 } else if((type
== NTLM
) || (type
== NTLMv2
)) {
348 /* For NTLMv2 failures eventually may need to retry NTLM */
349 wct
= 13; /* old style NTLM sessionsetup */
350 } else /* same size for negotiate or auth, NTLMSSP or extended security */
353 rc
= small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX
, wct
, ses
,
358 pSMB
= (SESSION_SETUP_ANDX
*)smb_buf
;
360 capabilities
= cifs_ssetup_hdr(ses
, pSMB
);
362 /* we will send the SMB in two pieces,
363 a fixed length beginning part, and a
364 second part which will include the strings
365 and rest of bcc area, in order to avoid having
366 to do a large buffer 17K allocation */
367 iov
[0].iov_base
= (char *)pSMB
;
368 iov
[0].iov_len
= smb_buf
->smb_buf_length
+ 4;
370 /* 2000 big enough to fit max user, domain, NOS name etc. */
371 str_area
= kmalloc(2000, GFP_KERNEL
);
374 ses
->flags
&= ~CIFS_SES_LANMAN
;
377 #ifdef CONFIG_CIFS_WEAK_PW_HASH
378 char lnm_session_key
[CIFS_SESS_KEY_SIZE
];
380 /* no capabilities flags in old lanman negotiation */
382 pSMB
->old_req
.PasswordLength
= cpu_to_le16(CIFS_SESS_KEY_SIZE
);
383 /* BB calculate hash with password */
384 /* and copy into bcc */
386 calc_lanman_hash(ses
, lnm_session_key
);
387 ses
->flags
|= CIFS_SES_LANMAN
;
388 /* #ifdef CONFIG_CIFS_DEBUG2
389 cifs_dump_mem("cryptkey: ",ses->server->cryptKey,
392 memcpy(bcc_ptr
, (char *)lnm_session_key
, CIFS_SESS_KEY_SIZE
);
393 bcc_ptr
+= CIFS_SESS_KEY_SIZE
;
395 /* can not sign if LANMAN negotiated so no need
396 to calculate signing key? but what if server
397 changed to do higher than lanman dialect and
398 we reconnected would we ever calc signing_key? */
400 cFYI(1,("Negotiating LANMAN setting up strings"));
401 /* Unicode not allowed for LANMAN dialects */
402 ascii_ssetup_strings(&bcc_ptr
, ses
, nls_cp
);
404 } else if (type
== NTLM
) {
405 char ntlm_session_key
[CIFS_SESS_KEY_SIZE
];
407 pSMB
->req_no_secext
.Capabilities
= cpu_to_le32(capabilities
);
408 pSMB
->req_no_secext
.CaseInsensitivePasswordLength
=
409 cpu_to_le16(CIFS_SESS_KEY_SIZE
);
410 pSMB
->req_no_secext
.CaseSensitivePasswordLength
=
411 cpu_to_le16(CIFS_SESS_KEY_SIZE
);
413 /* calculate session key */
414 SMBNTencrypt(ses
->password
, ses
->server
->cryptKey
,
417 if(first_time
) /* should this be moved into common code
418 with similar ntlmv2 path? */
419 cifs_calculate_mac_key(ses
->server
->mac_signing_key
,
420 ntlm_session_key
, ses
->password
);
421 /* copy session key */
423 memcpy(bcc_ptr
, (char *)ntlm_session_key
,CIFS_SESS_KEY_SIZE
);
424 bcc_ptr
+= CIFS_SESS_KEY_SIZE
;
425 memcpy(bcc_ptr
, (char *)ntlm_session_key
,CIFS_SESS_KEY_SIZE
);
426 bcc_ptr
+= CIFS_SESS_KEY_SIZE
;
427 if(ses
->capabilities
& CAP_UNICODE
) {
428 /* unicode strings must be word aligned */
429 if (iov
[0].iov_len
% 2) {
433 unicode_ssetup_strings(&bcc_ptr
, ses
, nls_cp
);
435 ascii_ssetup_strings(&bcc_ptr
, ses
, nls_cp
);
436 } else if (type
== NTLMv2
) {
438 kmalloc(sizeof(struct ntlmv2_resp
), GFP_KERNEL
);
440 /* BB FIXME change all users of v2_sess_key to
441 struct ntlmv2_resp */
443 if(v2_sess_key
== NULL
) {
444 cifs_small_buf_release(smb_buf
);
448 pSMB
->req_no_secext
.Capabilities
= cpu_to_le32(capabilities
);
450 /* LM2 password would be here if we supported it */
451 pSMB
->req_no_secext
.CaseInsensitivePasswordLength
= 0;
452 /* cpu_to_le16(LM2_SESS_KEY_SIZE); */
454 pSMB
->req_no_secext
.CaseSensitivePasswordLength
=
455 cpu_to_le16(sizeof(struct ntlmv2_resp
));
457 /* calculate session key */
458 setup_ntlmv2_rsp(ses
, v2_sess_key
, nls_cp
);
459 if(first_time
) /* should this be moved into common code
460 with similar ntlmv2 path? */
461 /* cifs_calculate_ntlmv2_mac_key(ses->server->mac_signing_key,
462 response BB FIXME, v2_sess_key); */
464 /* copy session key */
466 /* memcpy(bcc_ptr, (char *)ntlm_session_key,LM2_SESS_KEY_SIZE);
467 bcc_ptr += LM2_SESS_KEY_SIZE; */
468 memcpy(bcc_ptr
, (char *)v2_sess_key
, sizeof(struct ntlmv2_resp
));
469 bcc_ptr
+= sizeof(struct ntlmv2_resp
);
471 if(ses
->capabilities
& CAP_UNICODE
) {
472 if(iov
[0].iov_len
% 2) {
475 unicode_ssetup_strings(&bcc_ptr
, ses
, nls_cp
);
477 ascii_ssetup_strings(&bcc_ptr
, ses
, nls_cp
);
478 } else /* NTLMSSP or SPNEGO */ {
479 pSMB
->req
.hdr
.Flags2
|= SMBFLG2_EXT_SEC
;
480 capabilities
|= CAP_EXTENDED_SECURITY
;
481 pSMB
->req
.Capabilities
= cpu_to_le32(capabilities
);
482 /* BB set password lengths */
485 count
= (long) bcc_ptr
- (long) str_area
;
486 smb_buf
->smb_buf_length
+= count
;
488 BCC_LE(smb_buf
) = cpu_to_le16(count
);
490 iov
[1].iov_base
= str_area
;
491 iov
[1].iov_len
= count
;
492 rc
= SendReceive2(xid
, ses
, iov
, 2 /* num_iovecs */, &resp_buf_type
,
493 CIFS_STD_OP
/* not long */ | CIFS_LOG_ERROR
);
494 /* SMB request buf freed in SendReceive2 */
496 cFYI(1,("ssetup rc from sendrecv2 is %d",rc
));
500 pSMB
= (SESSION_SETUP_ANDX
*)iov
[0].iov_base
;
501 smb_buf
= (struct smb_hdr
*)iov
[0].iov_base
;
503 if((smb_buf
->WordCount
!= 3) && (smb_buf
->WordCount
!= 4)) {
505 cERROR(1,("bad word count %d", smb_buf
->WordCount
));
508 action
= le16_to_cpu(pSMB
->resp
.Action
);
509 if (action
& GUEST_LOGIN
)
510 cFYI(1, ("Guest login")); /* BB mark SesInfo struct? */
511 ses
->Suid
= smb_buf
->Uid
; /* UID left in wire format (le) */
512 cFYI(1, ("UID = %d ", ses
->Suid
));
513 /* response can have either 3 or 4 word count - Samba sends 3 */
514 /* and lanman response is 3 */
515 bytes_remaining
= BCC(smb_buf
);
516 bcc_ptr
= pByteArea(smb_buf
);
518 if(smb_buf
->WordCount
== 4) {
520 blob_len
= le16_to_cpu(pSMB
->resp
.SecurityBlobLength
);
522 if(blob_len
> bytes_remaining
) {
523 cERROR(1,("bad security blob length %d", blob_len
));
527 bytes_remaining
-= blob_len
;
530 /* BB check if Unicode and decode strings */
531 if(smb_buf
->Flags2
& SMBFLG2_UNICODE
)
532 rc
= decode_unicode_ssetup(&bcc_ptr
, bytes_remaining
,
535 rc
= decode_ascii_ssetup(&bcc_ptr
, bytes_remaining
, ses
,nls_cp
);
539 if(resp_buf_type
== CIFS_SMALL_BUFFER
) {
540 cFYI(1,("ssetup freeing small buf %p", iov
[0].iov_base
));
541 cifs_small_buf_release(iov
[0].iov_base
);
542 } else if(resp_buf_type
== CIFS_LARGE_BUFFER
)
543 cifs_buf_release(iov
[0].iov_base
);