blob: 892be9b4d1f362351de1c0075b2f9b3086aff514 [file] [log] [blame]
Steve French39798772006-05-31 22:40:51 +00001/*
2 * fs/cifs/sess.c
3 *
4 * SMB/CIFS session setup handling routines
5 *
Steve French790fe572007-07-07 19:25:05 +00006 * Copyright (c) International Business Machines Corp., 2006, 2007
Steve French39798772006-05-31 22:40:51 +00007 * Author(s): Steve French (sfrench@us.ibm.com)
8 *
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.
13 *
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.
18 *
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
22 */
23
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"
Steve French9c535882006-06-01 05:09:10 +000031#include <linux/utsname.h>
Steve French39798772006-05-31 22:40:51 +000032
Steve French39798772006-05-31 22:40:51 +000033extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8,
Steve French790fe572007-07-07 19:25:05 +000034 unsigned char *p24);
Steve French39798772006-05-31 22:40:51 +000035
Steve French39798772006-05-31 22:40:51 +000036static __u32 cifs_ssetup_hdr(struct cifsSesInfo *ses, SESSION_SETUP_ANDX *pSMB)
37{
38 __u32 capabilities = 0;
39
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);
45
46 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
47
Steve French790fe572007-07-07 19:25:05 +000048 /* BB verify whether signing required on neg or just on auth frame
Steve French39798772006-05-31 22:40:51 +000049 (and NTLM case) */
50
51 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
52 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
53
Steve French790fe572007-07-07 19:25:05 +000054 if (ses->server->secMode &
55 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
Steve French39798772006-05-31 22:40:51 +000056 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
57
58 if (ses->capabilities & CAP_UNICODE) {
59 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
60 capabilities |= CAP_UNICODE;
61 }
62 if (ses->capabilities & CAP_STATUS32) {
63 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
64 capabilities |= CAP_STATUS32;
65 }
66 if (ses->capabilities & CAP_DFS) {
67 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
68 capabilities |= CAP_DFS;
69 }
70 if (ses->capabilities & CAP_UNIX) {
71 capabilities |= CAP_UNIX;
72 }
73
74 /* BB check whether to init vcnum BB */
75 return capabilities;
76}
77
Steve French38702532007-07-08 15:40:40 +000078static void unicode_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses,
Steve French790fe572007-07-07 19:25:05 +000079 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +000080{
Steve French790fe572007-07-07 19:25:05 +000081 char *bcc_ptr = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +000082 int bytes_ret = 0;
83
84 /* BB FIXME add check that strings total less
85 than 335 or will need to send them as arrays */
86
Steve French0223cf02006-06-27 19:50:57 +000087 /* unicode strings, must be word aligned before the call */
88/* if ((long) bcc_ptr % 2) {
Steve French39798772006-05-31 22:40:51 +000089 *bcc_ptr = 0;
90 bcc_ptr++;
Steve French0223cf02006-06-27 19:50:57 +000091 } */
Steve French39798772006-05-31 22:40:51 +000092 /* copy user */
Steve French790fe572007-07-07 19:25:05 +000093 if (ses->userName == NULL) {
Steve French6e659c62006-11-08 23:10:46 +000094 /* null user mount */
95 *bcc_ptr = 0;
96 *(bcc_ptr+1) = 0;
Steve French39798772006-05-31 22:40:51 +000097 } else { /* 300 should be long enough for any conceivable user name */
98 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->userName,
99 300, nls_cp);
100 }
101 bcc_ptr += 2 * bytes_ret;
102 bcc_ptr += 2; /* account for null termination */
103 /* copy domain */
Steve French790fe572007-07-07 19:25:05 +0000104 if (ses->domainName == NULL) {
Steve French6e659c62006-11-08 23:10:46 +0000105 /* 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 */
107 *bcc_ptr = 0;
108 *(bcc_ptr+1) = 0;
109 bytes_ret = 0;
110 } else
Steve French38702532007-07-08 15:40:40 +0000111 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->domainName,
Steve French39798772006-05-31 22:40:51 +0000112 256, nls_cp);
113 bcc_ptr += 2 * bytes_ret;
114 bcc_ptr += 2; /* account for null terminator */
115
116 /* Copy OS version */
117 bytes_ret = cifs_strtoUCS((__le16 *)bcc_ptr, "Linux version ", 32,
118 nls_cp);
119 bcc_ptr += 2 * bytes_ret;
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700120 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, init_utsname()->release,
Steve French39798772006-05-31 22:40:51 +0000121 32, nls_cp);
122 bcc_ptr += 2 * bytes_ret;
123 bcc_ptr += 2; /* trailing null */
124
125 bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
Steve French790fe572007-07-07 19:25:05 +0000126 32, nls_cp);
Steve French39798772006-05-31 22:40:51 +0000127 bcc_ptr += 2 * bytes_ret;
128 bcc_ptr += 2; /* trailing null */
129
130 *pbcc_area = bcc_ptr;
131}
132
Steve French38702532007-07-08 15:40:40 +0000133static void ascii_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses,
Steve French790fe572007-07-07 19:25:05 +0000134 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000135{
Steve French790fe572007-07-07 19:25:05 +0000136 char *bcc_ptr = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000137
138 /* copy user */
139 /* BB what about null user mounts - check that we do this BB */
Steve French790fe572007-07-07 19:25:05 +0000140 /* copy user */
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);
145 }
Steve French39798772006-05-31 22:40:51 +0000146 /* BB improve check for overflow */
Steve French790fe572007-07-07 19:25:05 +0000147 bcc_ptr += strnlen(ses->userName, 300);
Steve French39798772006-05-31 22:40:51 +0000148 *bcc_ptr = 0;
Steve French790fe572007-07-07 19:25:05 +0000149 bcc_ptr++; /* account for null termination */
Steve French39798772006-05-31 22:40:51 +0000150
Steve French790fe572007-07-07 19:25:05 +0000151 /* copy domain */
152
153 if (ses->domainName != NULL) {
154 strncpy(bcc_ptr, ses->domainName, 256);
Steve French39798772006-05-31 22:40:51 +0000155 bcc_ptr += strnlen(ses->domainName, 256);
Steve French790fe572007-07-07 19:25:05 +0000156 } /* else we will send a null domain name
Steve French6e659c62006-11-08 23:10:46 +0000157 so the server will default to its own domain */
Steve French39798772006-05-31 22:40:51 +0000158 *bcc_ptr = 0;
159 bcc_ptr++;
160
161 /* BB check for overflow here */
162
163 strcpy(bcc_ptr, "Linux version ");
164 bcc_ptr += strlen("Linux version ");
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700165 strcpy(bcc_ptr, init_utsname()->release);
166 bcc_ptr += strlen(init_utsname()->release) + 1;
Steve French39798772006-05-31 22:40:51 +0000167
168 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
169 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
170
Steve French790fe572007-07-07 19:25:05 +0000171 *pbcc_area = bcc_ptr;
Steve French39798772006-05-31 22:40:51 +0000172}
173
Steve French790fe572007-07-07 19:25:05 +0000174static int decode_unicode_ssetup(char **pbcc_area, int bleft,
175 struct cifsSesInfo *ses,
176 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000177{
178 int rc = 0;
179 int words_left, len;
Steve French790fe572007-07-07 19:25:05 +0000180 char *data = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000181
182
183
Steve French790fe572007-07-07 19:25:05 +0000184 cFYI(1, ("bleft %d", bleft));
Steve French39798772006-05-31 22:40:51 +0000185
186
Steve French8e6f1952007-01-22 01:19:30 +0000187 /* SMB header is unaligned, so cifs servers word align start of
188 Unicode strings */
189 data++;
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
193 which follows it */
Steve French50c2f752007-07-13 00:33:32 +0000194
Steve French39798772006-05-31 22:40:51 +0000195 words_left = bleft / 2;
196
197 /* save off server operating system */
198 len = UniStrnlen((wchar_t *) data, words_left);
199
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 */
Steve French790fe572007-07-07 19:25:05 +0000203 if (len >= words_left)
Steve French39798772006-05-31 22:40:51 +0000204 return rc;
205
Steve French790fe572007-07-07 19:25:05 +0000206 if (ses->serverOS)
Steve French39798772006-05-31 22:40:51 +0000207 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);
Steve French790fe572007-07-07 19:25:05 +0000210 if (ses->serverOS != NULL) {
Steve French39798772006-05-31 22:40:51 +0000211 cifs_strfromUCS_le(ses->serverOS, (__le16 *)data, len,
212 nls_cp);
213 }
214 data += 2 * (len + 1);
215 words_left -= len + 1;
216
217 /* save off server network operating system */
218 len = UniStrnlen((wchar_t *) data, words_left);
219
Steve French790fe572007-07-07 19:25:05 +0000220 if (len >= words_left)
Steve French39798772006-05-31 22:40:51 +0000221 return rc;
222
Steve French790fe572007-07-07 19:25:05 +0000223 if (ses->serverNOS)
Steve French39798772006-05-31 22:40:51 +0000224 kfree(ses->serverNOS);
225 ses->serverNOS = kzalloc(4 * len, GFP_KERNEL); /* BB this is wrong length FIXME BB */
Steve French790fe572007-07-07 19:25:05 +0000226 if (ses->serverNOS != NULL) {
Steve French39798772006-05-31 22:40:51 +0000227 cifs_strfromUCS_le(ses->serverNOS, (__le16 *)data, len,
228 nls_cp);
Steve French790fe572007-07-07 19:25:05 +0000229 if (strncmp(ses->serverNOS, "NT LAN Manager 4", 16) == 0) {
230 cFYI(1, ("NT4 server"));
Steve French39798772006-05-31 22:40:51 +0000231 ses->flags |= CIFS_SES_NT4;
232 }
233 }
234 data += 2 * (len + 1);
235 words_left -= len + 1;
236
Steve French790fe572007-07-07 19:25:05 +0000237 /* save off server domain */
238 len = UniStrnlen((wchar_t *) data, words_left);
Steve French39798772006-05-31 22:40:51 +0000239
Steve French790fe572007-07-07 19:25:05 +0000240 if (len > words_left)
241 return rc;
Steve French39798772006-05-31 22:40:51 +0000242
Steve French790fe572007-07-07 19:25:05 +0000243 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,
248 nls_cp);
249 ses->serverDomain[2*len] = 0;
250 ses->serverDomain[(2*len) + 1] = 0;
251 }
252 data += 2 * (len + 1);
253 words_left -= len + 1;
254
255 cFYI(1, ("words left: %d", words_left));
Steve French39798772006-05-31 22:40:51 +0000256
257 return rc;
258}
259
Steve French790fe572007-07-07 19:25:05 +0000260static int decode_ascii_ssetup(char **pbcc_area, int bleft,
261 struct cifsSesInfo *ses,
262 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000263{
264 int rc = 0;
265 int len;
Steve French790fe572007-07-07 19:25:05 +0000266 char *bcc_ptr = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000267
Steve French790fe572007-07-07 19:25:05 +0000268 cFYI(1, ("decode sessetup ascii. bleft %d", bleft));
Steve French50c2f752007-07-13 00:33:32 +0000269
Steve French39798772006-05-31 22:40:51 +0000270 len = strnlen(bcc_ptr, bleft);
Steve French790fe572007-07-07 19:25:05 +0000271 if (len >= bleft)
Steve French39798772006-05-31 22:40:51 +0000272 return rc;
Steve French50c2f752007-07-13 00:33:32 +0000273
Steve French790fe572007-07-07 19:25:05 +0000274 if (ses->serverOS)
Steve French39798772006-05-31 22:40:51 +0000275 kfree(ses->serverOS);
276
277 ses->serverOS = kzalloc(len + 1, GFP_KERNEL);
Steve French790fe572007-07-07 19:25:05 +0000278 if (ses->serverOS)
Steve French39798772006-05-31 22:40:51 +0000279 strncpy(ses->serverOS, bcc_ptr, len);
Steve French790fe572007-07-07 19:25:05 +0000280 if (strncmp(ses->serverOS, "OS/2", 4) == 0) {
281 cFYI(1, ("OS/2 server"));
Steve French9ac00b72006-09-30 04:13:17 +0000282 ses->flags |= CIFS_SES_OS2;
283 }
Steve French39798772006-05-31 22:40:51 +0000284
285 bcc_ptr += len + 1;
286 bleft -= len + 1;
287
288 len = strnlen(bcc_ptr, bleft);
Steve French790fe572007-07-07 19:25:05 +0000289 if (len >= bleft)
Steve French39798772006-05-31 22:40:51 +0000290 return rc;
291
Steve French790fe572007-07-07 19:25:05 +0000292 if (ses->serverNOS)
Steve French39798772006-05-31 22:40:51 +0000293 kfree(ses->serverNOS);
294
295 ses->serverNOS = kzalloc(len + 1, GFP_KERNEL);
Steve French790fe572007-07-07 19:25:05 +0000296 if (ses->serverNOS)
Steve French39798772006-05-31 22:40:51 +0000297 strncpy(ses->serverNOS, bcc_ptr, len);
298
299 bcc_ptr += len + 1;
300 bleft -= len + 1;
301
Steve French790fe572007-07-07 19:25:05 +0000302 len = strnlen(bcc_ptr, bleft);
303 if (len > bleft)
304 return rc;
Steve French39798772006-05-31 22:40:51 +0000305
Steve French9ac00b72006-09-30 04:13:17 +0000306 /* 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 */
Steve French790fe572007-07-07 19:25:05 +0000311 cFYI(1, ("ascii: bytes left %d", bleft));
Steve French39798772006-05-31 22:40:51 +0000312
313 return rc;
314}
315
Steve French790fe572007-07-07 19:25:05 +0000316int
Steve French39798772006-05-31 22:40:51 +0000317CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, int first_time,
318 const struct nls_table *nls_cp)
319{
320 int rc = 0;
321 int wct;
Steve French39798772006-05-31 22:40:51 +0000322 struct smb_hdr *smb_buf;
323 char *bcc_ptr;
Steve French750d1152006-06-27 06:28:30 +0000324 char *str_area;
Steve French39798772006-05-31 22:40:51 +0000325 SESSION_SETUP_ANDX *pSMB;
326 __u32 capabilities;
327 int count;
328 int resp_buf_type = 0;
Steve French750d1152006-06-27 06:28:30 +0000329 struct kvec iov[2];
Steve French39798772006-05-31 22:40:51 +0000330 enum securityEnum type;
331 __u16 action;
332 int bytes_remaining;
Steve French254e55e2006-06-04 05:53:15 +0000333
Steve French790fe572007-07-07 19:25:05 +0000334 if (ses == NULL)
Steve French39798772006-05-31 22:40:51 +0000335 return -EINVAL;
336
337 type = ses->server->secType;
Steve Frenchf40c5622006-06-28 00:13:38 +0000338
Steve French790fe572007-07-07 19:25:05 +0000339 cFYI(1, ("sess setup type %d", type));
340 if (type == LANMAN) {
Steve French39798772006-05-31 22:40:51 +0000341#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 */
347 return -EOPNOTSUPP;
348#endif
349 wct = 10; /* lanman 2 style sessionsetup */
Steve French790fe572007-07-07 19:25:05 +0000350 } else if ((type == NTLM) || (type == NTLMv2)) {
Steve French9312f672006-06-04 22:21:07 +0000351 /* For NTLMv2 failures eventually may need to retry NTLM */
Steve French39798772006-05-31 22:40:51 +0000352 wct = 13; /* old style NTLM sessionsetup */
Steve French790fe572007-07-07 19:25:05 +0000353 } else /* same size: negotiate or auth, NTLMSSP or extended security */
Steve French39798772006-05-31 22:40:51 +0000354 wct = 12;
355
356 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
357 (void **)&smb_buf);
Steve French790fe572007-07-07 19:25:05 +0000358 if (rc)
Steve French39798772006-05-31 22:40:51 +0000359 return rc;
360
361 pSMB = (SESSION_SETUP_ANDX *)smb_buf;
362
363 capabilities = cifs_ssetup_hdr(ses, pSMB);
Steve French750d1152006-06-27 06:28:30 +0000364
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 */
Steve French790fe572007-07-07 19:25:05 +0000370 iov[0].iov_base = (char *)pSMB;
371 iov[0].iov_len = smb_buf->smb_buf_length + 4;
Steve French750d1152006-06-27 06:28:30 +0000372
373 /* 2000 big enough to fit max user, domain, NOS name etc. */
374 str_area = kmalloc(2000, GFP_KERNEL);
Cyrill Gorcunov5e6e6232007-08-18 00:15:20 +0000375 if (str_area == NULL) {
376 cifs_small_buf_release(smb_buf);
377 return -ENOMEM;
378 }
Steve French750d1152006-06-27 06:28:30 +0000379 bcc_ptr = str_area;
Steve French39798772006-05-31 22:40:51 +0000380
Steve French9ac00b72006-09-30 04:13:17 +0000381 ses->flags &= ~CIFS_SES_LANMAN;
382
Steve French790fe572007-07-07 19:25:05 +0000383 if (type == LANMAN) {
Steve French39798772006-05-31 22:40:51 +0000384#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French7c7b25b2006-06-01 19:20:10 +0000385 char lnm_session_key[CIFS_SESS_KEY_SIZE];
Steve French39798772006-05-31 22:40:51 +0000386
387 /* no capabilities flags in old lanman negotiation */
388
Steve French790fe572007-07-07 19:25:05 +0000389 pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
Steve French39798772006-05-31 22:40:51 +0000390 /* BB calculate hash with password */
391 /* and copy into bcc */
392
Steve French7c7b25b2006-06-01 19:20:10 +0000393 calc_lanman_hash(ses, lnm_session_key);
Steve French790fe572007-07-07 19:25:05 +0000394 ses->flags |= CIFS_SES_LANMAN;
Steve French750d1152006-06-27 06:28:30 +0000395/* #ifdef CONFIG_CIFS_DEBUG2
Steve French39798772006-05-31 22:40:51 +0000396 cifs_dump_mem("cryptkey: ",ses->server->cryptKey,
Steve French7c7b25b2006-06-01 19:20:10 +0000397 CIFS_SESS_KEY_SIZE);
Steve French750d1152006-06-27 06:28:30 +0000398#endif */
Steve French7c7b25b2006-06-01 19:20:10 +0000399 memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_SESS_KEY_SIZE);
400 bcc_ptr += CIFS_SESS_KEY_SIZE;
Steve French39798772006-05-31 22:40:51 +0000401
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? */
406
Steve French790fe572007-07-07 19:25:05 +0000407 cFYI(1, ("Negotiating LANMAN setting up strings"));
Steve French39798772006-05-31 22:40:51 +0000408 /* Unicode not allowed for LANMAN dialects */
409 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
Steve French790fe572007-07-07 19:25:05 +0000410#endif
Steve French39798772006-05-31 22:40:51 +0000411 } else if (type == NTLM) {
Steve French7c7b25b2006-06-01 19:20:10 +0000412 char ntlm_session_key[CIFS_SESS_KEY_SIZE];
Steve French39798772006-05-31 22:40:51 +0000413
414 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
415 pSMB->req_no_secext.CaseInsensitivePasswordLength =
Steve French7c7b25b2006-06-01 19:20:10 +0000416 cpu_to_le16(CIFS_SESS_KEY_SIZE);
Steve French39798772006-05-31 22:40:51 +0000417 pSMB->req_no_secext.CaseSensitivePasswordLength =
Steve French7c7b25b2006-06-01 19:20:10 +0000418 cpu_to_le16(CIFS_SESS_KEY_SIZE);
Steve French50c2f752007-07-13 00:33:32 +0000419
Steve French39798772006-05-31 22:40:51 +0000420 /* calculate session key */
421 SMBNTencrypt(ses->password, ses->server->cryptKey,
422 ntlm_session_key);
423
Steve French790fe572007-07-07 19:25:05 +0000424 if (first_time) /* should this be moved into common code
Steve French39798772006-05-31 22:40:51 +0000425 with similar ntlmv2 path? */
Steve Frenchb609f062007-07-09 07:55:14 +0000426 cifs_calculate_mac_key(&ses->server->mac_signing_key,
Steve French39798772006-05-31 22:40:51 +0000427 ntlm_session_key, ses->password);
428 /* copy session key */
429
Steve French790fe572007-07-07 19:25:05 +0000430 memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE);
Steve French7c7b25b2006-06-01 19:20:10 +0000431 bcc_ptr += CIFS_SESS_KEY_SIZE;
Steve French790fe572007-07-07 19:25:05 +0000432 memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE);
Steve French7c7b25b2006-06-01 19:20:10 +0000433 bcc_ptr += CIFS_SESS_KEY_SIZE;
Steve French790fe572007-07-07 19:25:05 +0000434 if (ses->capabilities & CAP_UNICODE) {
Steve French0223cf02006-06-27 19:50:57 +0000435 /* unicode strings must be word aligned */
436 if (iov[0].iov_len % 2) {
437 *bcc_ptr = 0;
Steve French790fe572007-07-07 19:25:05 +0000438 bcc_ptr++;
439 }
Steve French7c7b25b2006-06-01 19:20:10 +0000440 unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
Steve French0223cf02006-06-27 19:50:57 +0000441 } else
Steve French7c7b25b2006-06-01 19:20:10 +0000442 ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
443 } else if (type == NTLMv2) {
Steve French790fe572007-07-07 19:25:05 +0000444 char *v2_sess_key =
Steve French6d027cf2006-06-05 16:26:05 +0000445 kmalloc(sizeof(struct ntlmv2_resp), GFP_KERNEL);
Steve Frenchf64b23a2006-06-05 05:27:37 +0000446
447 /* BB FIXME change all users of v2_sess_key to
448 struct ntlmv2_resp */
Steve French7c7b25b2006-06-01 19:20:10 +0000449
Steve French790fe572007-07-07 19:25:05 +0000450 if (v2_sess_key == NULL) {
Steve French7c7b25b2006-06-01 19:20:10 +0000451 cifs_small_buf_release(smb_buf);
452 return -ENOMEM;
453 }
454
455 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
456
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); */
460
461 pSMB->req_no_secext.CaseSensitivePasswordLength =
Steve Frenchf64b23a2006-06-05 05:27:37 +0000462 cpu_to_le16(sizeof(struct ntlmv2_resp));
Steve French7c7b25b2006-06-01 19:20:10 +0000463
464 /* calculate session key */
Steve French1717ffc2006-06-08 05:41:32 +0000465 setup_ntlmv2_rsp(ses, v2_sess_key, nls_cp);
Steve French790fe572007-07-07 19:25:05 +0000466 if (first_time) /* should this be moved into common code
467 with similar ntlmv2 path? */
Steve French7c7b25b2006-06-01 19:20:10 +0000468 /* cifs_calculate_ntlmv2_mac_key(ses->server->mac_signing_key,
469 response BB FIXME, v2_sess_key); */
470
471 /* copy session key */
472
473 /* memcpy(bcc_ptr, (char *)ntlm_session_key,LM2_SESS_KEY_SIZE);
474 bcc_ptr += LM2_SESS_KEY_SIZE; */
Steve French38702532007-07-08 15:40:40 +0000475 memcpy(bcc_ptr, (char *)v2_sess_key,
476 sizeof(struct ntlmv2_resp));
Steve Frenchf64b23a2006-06-05 05:27:37 +0000477 bcc_ptr += sizeof(struct ntlmv2_resp);
478 kfree(v2_sess_key);
Steve French790fe572007-07-07 19:25:05 +0000479 if (ses->capabilities & CAP_UNICODE) {
480 if (iov[0].iov_len % 2) {
Steve French0223cf02006-06-27 19:50:57 +0000481 *bcc_ptr = 0;
482 } bcc_ptr++;
Steve French39798772006-05-31 22:40:51 +0000483 unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
Steve French0223cf02006-06-27 19:50:57 +0000484 } else
Steve French39798772006-05-31 22:40:51 +0000485 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 */
491 }
492
Steve French750d1152006-06-27 06:28:30 +0000493 count = (long) bcc_ptr - (long) str_area;
Steve French39798772006-05-31 22:40:51 +0000494 smb_buf->smb_buf_length += count;
495
Steve French39798772006-05-31 22:40:51 +0000496 BCC_LE(smb_buf) = cpu_to_le16(count);
497
Steve French750d1152006-06-27 06:28:30 +0000498 iov[1].iov_base = str_area;
Steve French790fe572007-07-07 19:25:05 +0000499 iov[1].iov_len = count;
Steve French750d1152006-06-27 06:28:30 +0000500 rc = SendReceive2(xid, ses, iov, 2 /* num_iovecs */, &resp_buf_type, 0);
Steve French39798772006-05-31 22:40:51 +0000501 /* SMB request buf freed in SendReceive2 */
502
Steve French790fe572007-07-07 19:25:05 +0000503 cFYI(1, ("ssetup rc from sendrecv2 is %d", rc));
504 if (rc)
Steve French39798772006-05-31 22:40:51 +0000505 goto ssetup_exit;
506
507 pSMB = (SESSION_SETUP_ANDX *)iov[0].iov_base;
508 smb_buf = (struct smb_hdr *)iov[0].iov_base;
509
Steve French790fe572007-07-07 19:25:05 +0000510 if ((smb_buf->WordCount != 3) && (smb_buf->WordCount != 4)) {
Steve French39798772006-05-31 22:40:51 +0000511 rc = -EIO;
Steve French790fe572007-07-07 19:25:05 +0000512 cERROR(1, ("bad word count %d", smb_buf->WordCount));
Steve French39798772006-05-31 22:40:51 +0000513 goto ssetup_exit;
514 }
515 action = le16_to_cpu(pSMB->resp.Action);
516 if (action & GUEST_LOGIN)
Steve French189acaa2006-06-23 02:33:48 +0000517 cFYI(1, ("Guest login")); /* BB mark SesInfo struct? */
Steve French39798772006-05-31 22:40:51 +0000518 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);
524
Steve French790fe572007-07-07 19:25:05 +0000525 if (smb_buf->WordCount == 4) {
Steve French39798772006-05-31 22:40:51 +0000526 __u16 blob_len;
527 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
528 bcc_ptr += blob_len;
Steve French790fe572007-07-07 19:25:05 +0000529 if (blob_len > bytes_remaining) {
530 cERROR(1, ("bad security blob length %d", blob_len));
Steve French39798772006-05-31 22:40:51 +0000531 rc = -EINVAL;
532 goto ssetup_exit;
533 }
534 bytes_remaining -= blob_len;
Steve French790fe572007-07-07 19:25:05 +0000535 }
Steve French39798772006-05-31 22:40:51 +0000536
537 /* BB check if Unicode and decode strings */
Steve French790fe572007-07-07 19:25:05 +0000538 if (smb_buf->Flags2 & SMBFLG2_UNICODE)
Steve French39798772006-05-31 22:40:51 +0000539 rc = decode_unicode_ssetup(&bcc_ptr, bytes_remaining,
540 ses, nls_cp);
541 else
Steve French63135e02007-07-17 17:34:02 +0000542 rc = decode_ascii_ssetup(&bcc_ptr, bytes_remaining,
543 ses, nls_cp);
Steve French50c2f752007-07-13 00:33:32 +0000544
Steve French39798772006-05-31 22:40:51 +0000545ssetup_exit:
Steve French750d1152006-06-27 06:28:30 +0000546 kfree(str_area);
Steve French790fe572007-07-07 19:25:05 +0000547 if (resp_buf_type == CIFS_SMALL_BUFFER) {
548 cFYI(1, ("ssetup freeing small buf %p", iov[0].iov_base));
Steve French39798772006-05-31 22:40:51 +0000549 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +0000550 } else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve French39798772006-05-31 22:40:51 +0000551 cifs_buf_release(iov[0].iov_base);
552
553 return rc;
554}