Commit | Line | Data |
---|---|---|
3979877e SF |
1 | /* |
2 | * fs/cifs/sess.c | |
3 | * | |
4 | * SMB/CIFS session setup handling routines | |
5 | * | |
790fe579 | 6 | * Copyright (c) International Business Machines Corp., 2006, 2007 |
3979877e SF |
7 | * 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" | |
9c53588e | 31 | #include <linux/utsname.h> |
2442421b | 32 | #include "cifs_spnego.h" |
3979877e | 33 | |
3979877e | 34 | extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8, |
790fe579 | 35 | unsigned char *p24); |
3979877e | 36 | |
3979877e SF |
37 | static __u32 cifs_ssetup_hdr(struct cifsSesInfo *ses, SESSION_SETUP_ANDX *pSMB) |
38 | { | |
39 | __u32 capabilities = 0; | |
40 | ||
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); | |
46 | ||
47 | /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */ | |
48 | ||
790fe579 | 49 | /* BB verify whether signing required on neg or just on auth frame |
3979877e SF |
50 | (and NTLM case) */ |
51 | ||
52 | capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS | | |
53 | CAP_LARGE_WRITE_X | CAP_LARGE_READ_X; | |
54 | ||
790fe579 SF |
55 | if (ses->server->secMode & |
56 | (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) | |
3979877e SF |
57 | pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
58 | ||
59 | if (ses->capabilities & CAP_UNICODE) { | |
60 | pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE; | |
61 | capabilities |= CAP_UNICODE; | |
62 | } | |
63 | if (ses->capabilities & CAP_STATUS32) { | |
64 | pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS; | |
65 | capabilities |= CAP_STATUS32; | |
66 | } | |
67 | if (ses->capabilities & CAP_DFS) { | |
68 | pSMB->req.hdr.Flags2 |= SMBFLG2_DFS; | |
69 | capabilities |= CAP_DFS; | |
70 | } | |
26f57364 | 71 | if (ses->capabilities & CAP_UNIX) |
3979877e | 72 | capabilities |= CAP_UNIX; |
3979877e SF |
73 | |
74 | /* BB check whether to init vcnum BB */ | |
75 | return capabilities; | |
76 | } | |
77 | ||
0d3a01fa JL |
78 | static void |
79 | unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp) | |
80 | { | |
81 | char *bcc_ptr = *pbcc_area; | |
82 | int bytes_ret = 0; | |
83 | ||
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 */ | |
92 | ||
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 */ | |
97 | ||
98 | *pbcc_area = bcc_ptr; | |
99 | } | |
100 | ||
101 | static void unicode_domain_string(char **pbcc_area, struct cifsSesInfo *ses, | |
102 | const struct nls_table *nls_cp) | |
103 | { | |
104 | char *bcc_ptr = *pbcc_area; | |
105 | int bytes_ret = 0; | |
106 | ||
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 */ | |
119 | ||
120 | *pbcc_area = bcc_ptr; | |
121 | } | |
122 | ||
123 | ||
3870253e | 124 | static void unicode_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses, |
790fe579 | 125 | const struct nls_table *nls_cp) |
3979877e | 126 | { |
790fe579 | 127 | char *bcc_ptr = *pbcc_area; |
3979877e SF |
128 | int bytes_ret = 0; |
129 | ||
130 | /* BB FIXME add check that strings total less | |
131 | than 335 or will need to send them as arrays */ | |
132 | ||
0223cf0b SF |
133 | /* unicode strings, must be word aligned before the call */ |
134 | /* if ((long) bcc_ptr % 2) { | |
3979877e SF |
135 | *bcc_ptr = 0; |
136 | bcc_ptr++; | |
0223cf0b | 137 | } */ |
3979877e | 138 | /* copy user */ |
790fe579 | 139 | if (ses->userName == NULL) { |
6e659c63 SF |
140 | /* null user mount */ |
141 | *bcc_ptr = 0; | |
142 | *(bcc_ptr+1) = 0; | |
3979877e SF |
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); | |
146 | } | |
147 | bcc_ptr += 2 * bytes_ret; | |
148 | bcc_ptr += 2; /* account for null termination */ | |
3979877e | 149 | |
0d3a01fa JL |
150 | unicode_domain_string(&bcc_ptr, ses, nls_cp); |
151 | unicode_oslm_strings(&bcc_ptr, nls_cp); | |
3979877e SF |
152 | |
153 | *pbcc_area = bcc_ptr; | |
154 | } | |
155 | ||
3870253e | 156 | static void ascii_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses, |
790fe579 | 157 | const struct nls_table *nls_cp) |
3979877e | 158 | { |
790fe579 | 159 | char *bcc_ptr = *pbcc_area; |
3979877e SF |
160 | |
161 | /* copy user */ | |
162 | /* BB what about null user mounts - check that we do this BB */ | |
790fe579 SF |
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); | |
168 | } | |
3979877e | 169 | /* BB improve check for overflow */ |
790fe579 | 170 | bcc_ptr += strnlen(ses->userName, 300); |
3979877e | 171 | *bcc_ptr = 0; |
790fe579 | 172 | bcc_ptr++; /* account for null termination */ |
3979877e | 173 | |
790fe579 SF |
174 | /* copy domain */ |
175 | ||
176 | if (ses->domainName != NULL) { | |
177 | strncpy(bcc_ptr, ses->domainName, 256); | |
3979877e | 178 | bcc_ptr += strnlen(ses->domainName, 256); |
790fe579 | 179 | } /* else we will send a null domain name |
6e659c63 | 180 | so the server will default to its own domain */ |
3979877e SF |
181 | *bcc_ptr = 0; |
182 | bcc_ptr++; | |
183 | ||
184 | /* BB check for overflow here */ | |
185 | ||
186 | strcpy(bcc_ptr, "Linux version "); | |
187 | bcc_ptr += strlen("Linux version "); | |
96b644bd SH |
188 | strcpy(bcc_ptr, init_utsname()->release); |
189 | bcc_ptr += strlen(init_utsname()->release) + 1; | |
3979877e SF |
190 | |
191 | strcpy(bcc_ptr, CIFS_NETWORK_OPSYS); | |
192 | bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1; | |
193 | ||
790fe579 | 194 | *pbcc_area = bcc_ptr; |
3979877e SF |
195 | } |
196 | ||
790fe579 SF |
197 | static int decode_unicode_ssetup(char **pbcc_area, int bleft, |
198 | struct cifsSesInfo *ses, | |
199 | const struct nls_table *nls_cp) | |
3979877e SF |
200 | { |
201 | int rc = 0; | |
202 | int words_left, len; | |
790fe579 | 203 | char *data = *pbcc_area; |
3979877e SF |
204 | |
205 | ||
206 | ||
790fe579 | 207 | cFYI(1, ("bleft %d", bleft)); |
3979877e SF |
208 | |
209 | ||
8e6f195a SF |
210 | /* SMB header is unaligned, so cifs servers word align start of |
211 | Unicode strings */ | |
212 | data++; | |
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 | |
216 | which follows it */ | |
50c2f753 | 217 | |
3979877e SF |
218 | words_left = bleft / 2; |
219 | ||
220 | /* save off server operating system */ | |
221 | len = UniStrnlen((wchar_t *) data, words_left); | |
222 | ||
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 */ | |
790fe579 | 226 | if (len >= words_left) |
3979877e SF |
227 | return rc; |
228 | ||
26f57364 | 229 | kfree(ses->serverOS); |
3979877e SF |
230 | /* UTF-8 string will not grow more than four times as big as UCS-16 */ |
231 | ses->serverOS = kzalloc(4 * len, GFP_KERNEL); | |
26f57364 SF |
232 | if (ses->serverOS != NULL) |
233 | cifs_strfromUCS_le(ses->serverOS, (__le16 *)data, len, nls_cp); | |
3979877e SF |
234 | data += 2 * (len + 1); |
235 | words_left -= len + 1; | |
236 | ||
237 | /* save off server network operating system */ | |
238 | len = UniStrnlen((wchar_t *) data, words_left); | |
239 | ||
790fe579 | 240 | if (len >= words_left) |
3979877e SF |
241 | return rc; |
242 | ||
26f57364 | 243 | kfree(ses->serverNOS); |
3979877e | 244 | ses->serverNOS = kzalloc(4 * len, GFP_KERNEL); /* BB this is wrong length FIXME BB */ |
790fe579 | 245 | if (ses->serverNOS != NULL) { |
3979877e SF |
246 | cifs_strfromUCS_le(ses->serverNOS, (__le16 *)data, len, |
247 | nls_cp); | |
790fe579 SF |
248 | if (strncmp(ses->serverNOS, "NT LAN Manager 4", 16) == 0) { |
249 | cFYI(1, ("NT4 server")); | |
3979877e SF |
250 | ses->flags |= CIFS_SES_NT4; |
251 | } | |
252 | } | |
253 | data += 2 * (len + 1); | |
254 | words_left -= len + 1; | |
255 | ||
790fe579 SF |
256 | /* save off server domain */ |
257 | len = UniStrnlen((wchar_t *) data, words_left); | |
258 | ||
259 | if (len > words_left) | |
260 | return rc; | |
261 | ||
26f57364 | 262 | kfree(ses->serverDomain); |
790fe579 SF |
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, | |
266 | nls_cp); | |
267 | ses->serverDomain[2*len] = 0; | |
268 | ses->serverDomain[(2*len) + 1] = 0; | |
269 | } | |
270 | data += 2 * (len + 1); | |
271 | words_left -= len + 1; | |
272 | ||
273 | cFYI(1, ("words left: %d", words_left)); | |
3979877e SF |
274 | |
275 | return rc; | |
276 | } | |
277 | ||
790fe579 SF |
278 | static int decode_ascii_ssetup(char **pbcc_area, int bleft, |
279 | struct cifsSesInfo *ses, | |
280 | const struct nls_table *nls_cp) | |
3979877e SF |
281 | { |
282 | int rc = 0; | |
283 | int len; | |
790fe579 | 284 | char *bcc_ptr = *pbcc_area; |
3979877e | 285 | |
790fe579 | 286 | cFYI(1, ("decode sessetup ascii. bleft %d", bleft)); |
50c2f753 | 287 | |
3979877e | 288 | len = strnlen(bcc_ptr, bleft); |
790fe579 | 289 | if (len >= bleft) |
3979877e | 290 | return rc; |
50c2f753 | 291 | |
26f57364 | 292 | kfree(ses->serverOS); |
3979877e SF |
293 | |
294 | ses->serverOS = kzalloc(len + 1, GFP_KERNEL); | |
790fe579 | 295 | if (ses->serverOS) |
3979877e | 296 | strncpy(ses->serverOS, bcc_ptr, len); |
790fe579 SF |
297 | if (strncmp(ses->serverOS, "OS/2", 4) == 0) { |
298 | cFYI(1, ("OS/2 server")); | |
9ac00b7d SF |
299 | ses->flags |= CIFS_SES_OS2; |
300 | } | |
3979877e SF |
301 | |
302 | bcc_ptr += len + 1; | |
303 | bleft -= len + 1; | |
304 | ||
305 | len = strnlen(bcc_ptr, bleft); | |
790fe579 | 306 | if (len >= bleft) |
3979877e SF |
307 | return rc; |
308 | ||
26f57364 | 309 | kfree(ses->serverNOS); |
3979877e SF |
310 | |
311 | ses->serverNOS = kzalloc(len + 1, GFP_KERNEL); | |
790fe579 | 312 | if (ses->serverNOS) |
3979877e SF |
313 | strncpy(ses->serverNOS, bcc_ptr, len); |
314 | ||
315 | bcc_ptr += len + 1; | |
316 | bleft -= len + 1; | |
317 | ||
790fe579 SF |
318 | len = strnlen(bcc_ptr, bleft); |
319 | if (len > bleft) | |
320 | return rc; | |
3979877e | 321 | |
9ac00b7d SF |
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 */ | |
790fe579 | 327 | cFYI(1, ("ascii: bytes left %d", bleft)); |
3979877e SF |
328 | |
329 | return rc; | |
330 | } | |
331 | ||
790fe579 | 332 | int |
3979877e SF |
333 | CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, int first_time, |
334 | const struct nls_table *nls_cp) | |
335 | { | |
336 | int rc = 0; | |
337 | int wct; | |
3979877e SF |
338 | struct smb_hdr *smb_buf; |
339 | char *bcc_ptr; | |
750d1151 | 340 | char *str_area; |
3979877e SF |
341 | SESSION_SETUP_ANDX *pSMB; |
342 | __u32 capabilities; | |
343 | int count; | |
2442421b SF |
344 | int resp_buf_type; |
345 | struct kvec iov[3]; | |
3979877e SF |
346 | enum securityEnum type; |
347 | __u16 action; | |
348 | int bytes_remaining; | |
2442421b | 349 | struct key *spnego_key = NULL; |
254e55ed | 350 | |
790fe579 | 351 | if (ses == NULL) |
3979877e SF |
352 | return -EINVAL; |
353 | ||
354 | type = ses->server->secType; | |
f40c5628 | 355 | |
790fe579 SF |
356 | cFYI(1, ("sess setup type %d", type)); |
357 | if (type == LANMAN) { | |
3979877e SF |
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 */ | |
364 | return -EOPNOTSUPP; | |
365 | #endif | |
366 | wct = 10; /* lanman 2 style sessionsetup */ | |
790fe579 | 367 | } else if ((type == NTLM) || (type == NTLMv2)) { |
9312f675 | 368 | /* For NTLMv2 failures eventually may need to retry NTLM */ |
3979877e | 369 | wct = 13; /* old style NTLM sessionsetup */ |
790fe579 | 370 | } else /* same size: negotiate or auth, NTLMSSP or extended security */ |
3979877e SF |
371 | wct = 12; |
372 | ||
373 | rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses, | |
374 | (void **)&smb_buf); | |
790fe579 | 375 | if (rc) |
3979877e SF |
376 | return rc; |
377 | ||
378 | pSMB = (SESSION_SETUP_ANDX *)smb_buf; | |
379 | ||
380 | capabilities = cifs_ssetup_hdr(ses, pSMB); | |
750d1151 | 381 | |
2442421b SF |
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 */ | |
790fe579 SF |
388 | iov[0].iov_base = (char *)pSMB; |
389 | iov[0].iov_len = smb_buf->smb_buf_length + 4; | |
750d1151 | 390 | |
2442421b SF |
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; | |
394 | ||
750d1151 SF |
395 | /* 2000 big enough to fit max user, domain, NOS name etc. */ |
396 | str_area = kmalloc(2000, GFP_KERNEL); | |
5e6e6232 | 397 | if (str_area == NULL) { |
2442421b SF |
398 | rc = -ENOMEM; |
399 | goto ssetup_exit; | |
5e6e6232 | 400 | } |
750d1151 | 401 | bcc_ptr = str_area; |
3979877e | 402 | |
9ac00b7d SF |
403 | ses->flags &= ~CIFS_SES_LANMAN; |
404 | ||
2442421b SF |
405 | iov[1].iov_base = NULL; |
406 | iov[1].iov_len = 0; | |
407 | ||
790fe579 | 408 | if (type == LANMAN) { |
3979877e | 409 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
7c7b25bc | 410 | char lnm_session_key[CIFS_SESS_KEY_SIZE]; |
3979877e SF |
411 | |
412 | /* no capabilities flags in old lanman negotiation */ | |
413 | ||
790fe579 | 414 | pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE); |
3979877e SF |
415 | /* BB calculate hash with password */ |
416 | /* and copy into bcc */ | |
417 | ||
7c7b25bc | 418 | calc_lanman_hash(ses, lnm_session_key); |
790fe579 | 419 | ses->flags |= CIFS_SES_LANMAN; |
7c7b25bc SF |
420 | memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_SESS_KEY_SIZE); |
421 | bcc_ptr += CIFS_SESS_KEY_SIZE; | |
3979877e SF |
422 | |
423 | /* can not sign if LANMAN negotiated so no need | |
424 | to calculate signing key? but what if server | |
425 | changed to do higher than lanman dialect and | |
426 | we reconnected would we ever calc signing_key? */ | |
427 | ||
790fe579 | 428 | cFYI(1, ("Negotiating LANMAN setting up strings")); |
3979877e SF |
429 | /* Unicode not allowed for LANMAN dialects */ |
430 | ascii_ssetup_strings(&bcc_ptr, ses, nls_cp); | |
790fe579 | 431 | #endif |
3979877e | 432 | } else if (type == NTLM) { |
7c7b25bc | 433 | char ntlm_session_key[CIFS_SESS_KEY_SIZE]; |
3979877e SF |
434 | |
435 | pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities); | |
436 | pSMB->req_no_secext.CaseInsensitivePasswordLength = | |
7c7b25bc | 437 | cpu_to_le16(CIFS_SESS_KEY_SIZE); |
3979877e | 438 | pSMB->req_no_secext.CaseSensitivePasswordLength = |
7c7b25bc | 439 | cpu_to_le16(CIFS_SESS_KEY_SIZE); |
50c2f753 | 440 | |
3979877e SF |
441 | /* calculate session key */ |
442 | SMBNTencrypt(ses->password, ses->server->cryptKey, | |
443 | ntlm_session_key); | |
444 | ||
790fe579 | 445 | if (first_time) /* should this be moved into common code |
3979877e | 446 | with similar ntlmv2 path? */ |
b609f06a | 447 | cifs_calculate_mac_key(&ses->server->mac_signing_key, |
3979877e SF |
448 | ntlm_session_key, ses->password); |
449 | /* copy session key */ | |
450 | ||
790fe579 | 451 | memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE); |
7c7b25bc | 452 | bcc_ptr += CIFS_SESS_KEY_SIZE; |
790fe579 | 453 | memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE); |
7c7b25bc | 454 | bcc_ptr += CIFS_SESS_KEY_SIZE; |
790fe579 | 455 | if (ses->capabilities & CAP_UNICODE) { |
0223cf0b SF |
456 | /* unicode strings must be word aligned */ |
457 | if (iov[0].iov_len % 2) { | |
458 | *bcc_ptr = 0; | |
790fe579 SF |
459 | bcc_ptr++; |
460 | } | |
7c7b25bc | 461 | unicode_ssetup_strings(&bcc_ptr, ses, nls_cp); |
0223cf0b | 462 | } else |
7c7b25bc SF |
463 | ascii_ssetup_strings(&bcc_ptr, ses, nls_cp); |
464 | } else if (type == NTLMv2) { | |
790fe579 | 465 | char *v2_sess_key = |
6d027cfd | 466 | kmalloc(sizeof(struct ntlmv2_resp), GFP_KERNEL); |
f64b23ae SF |
467 | |
468 | /* BB FIXME change all users of v2_sess_key to | |
469 | struct ntlmv2_resp */ | |
7c7b25bc | 470 | |
790fe579 | 471 | if (v2_sess_key == NULL) { |
2442421b SF |
472 | rc = -ENOMEM; |
473 | goto ssetup_exit; | |
7c7b25bc SF |
474 | } |
475 | ||
476 | pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities); | |
477 | ||
478 | /* LM2 password would be here if we supported it */ | |
479 | pSMB->req_no_secext.CaseInsensitivePasswordLength = 0; | |
480 | /* cpu_to_le16(LM2_SESS_KEY_SIZE); */ | |
481 | ||
482 | pSMB->req_no_secext.CaseSensitivePasswordLength = | |
f64b23ae | 483 | cpu_to_le16(sizeof(struct ntlmv2_resp)); |
7c7b25bc SF |
484 | |
485 | /* calculate session key */ | |
1717ffc5 | 486 | setup_ntlmv2_rsp(ses, v2_sess_key, nls_cp); |
790fe579 SF |
487 | if (first_time) /* should this be moved into common code |
488 | with similar ntlmv2 path? */ | |
7c7b25bc SF |
489 | /* cifs_calculate_ntlmv2_mac_key(ses->server->mac_signing_key, |
490 | response BB FIXME, v2_sess_key); */ | |
491 | ||
492 | /* copy session key */ | |
493 | ||
494 | /* memcpy(bcc_ptr, (char *)ntlm_session_key,LM2_SESS_KEY_SIZE); | |
495 | bcc_ptr += LM2_SESS_KEY_SIZE; */ | |
3870253e SF |
496 | memcpy(bcc_ptr, (char *)v2_sess_key, |
497 | sizeof(struct ntlmv2_resp)); | |
f64b23ae SF |
498 | bcc_ptr += sizeof(struct ntlmv2_resp); |
499 | kfree(v2_sess_key); | |
790fe579 SF |
500 | if (ses->capabilities & CAP_UNICODE) { |
501 | if (iov[0].iov_len % 2) { | |
0223cf0b | 502 | *bcc_ptr = 0; |
26f57364 SF |
503 | bcc_ptr++; |
504 | } | |
3979877e | 505 | unicode_ssetup_strings(&bcc_ptr, ses, nls_cp); |
0223cf0b | 506 | } else |
3979877e | 507 | ascii_ssetup_strings(&bcc_ptr, ses, nls_cp); |
2442421b SF |
508 | } else if (type == Kerberos) { |
509 | #ifdef CONFIG_CIFS_UPCALL | |
510 | struct cifs_spnego_msg *msg; | |
511 | spnego_key = cifs_get_spnego_key(ses); | |
512 | if (IS_ERR(spnego_key)) { | |
513 | rc = PTR_ERR(spnego_key); | |
514 | spnego_key = NULL; | |
515 | goto ssetup_exit; | |
516 | } | |
517 | ||
518 | msg = spnego_key->payload.data; | |
519 | /* bail out if key is too long */ | |
520 | if (msg->sesskey_len > | |
521 | sizeof(ses->server->mac_signing_key.data.krb5)) { | |
522 | cERROR(1, ("Kerberos signing key too long (%u bytes)", | |
523 | msg->sesskey_len)); | |
524 | rc = -EOVERFLOW; | |
525 | goto ssetup_exit; | |
526 | } | |
1a67570c JL |
527 | if (first_time) { |
528 | ses->server->mac_signing_key.len = msg->sesskey_len; | |
529 | memcpy(ses->server->mac_signing_key.data.krb5, | |
530 | msg->data, msg->sesskey_len); | |
531 | } | |
3979877e SF |
532 | pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC; |
533 | capabilities |= CAP_EXTENDED_SECURITY; | |
534 | pSMB->req.Capabilities = cpu_to_le32(capabilities); | |
2442421b SF |
535 | iov[1].iov_base = msg->data + msg->sesskey_len; |
536 | iov[1].iov_len = msg->secblob_len; | |
537 | pSMB->req.SecurityBlobLength = cpu_to_le16(iov[1].iov_len); | |
538 | ||
539 | if (ses->capabilities & CAP_UNICODE) { | |
540 | /* unicode strings must be word aligned */ | |
28c5a02a | 541 | if ((iov[0].iov_len + iov[1].iov_len) % 2) { |
2442421b SF |
542 | *bcc_ptr = 0; |
543 | bcc_ptr++; | |
544 | } | |
545 | unicode_oslm_strings(&bcc_ptr, nls_cp); | |
546 | unicode_domain_string(&bcc_ptr, ses, nls_cp); | |
547 | } else | |
548 | /* BB: is this right? */ | |
549 | ascii_ssetup_strings(&bcc_ptr, ses, nls_cp); | |
550 | #else /* ! CONFIG_CIFS_UPCALL */ | |
551 | cERROR(1, ("Kerberos negotiated but upcall support disabled!")); | |
552 | rc = -ENOSYS; | |
553 | goto ssetup_exit; | |
554 | #endif /* CONFIG_CIFS_UPCALL */ | |
555 | } else { | |
556 | cERROR(1, ("secType %d not supported!", type)); | |
557 | rc = -ENOSYS; | |
558 | goto ssetup_exit; | |
3979877e SF |
559 | } |
560 | ||
2442421b SF |
561 | iov[2].iov_base = str_area; |
562 | iov[2].iov_len = (long) bcc_ptr - (long) str_area; | |
563 | ||
564 | count = iov[1].iov_len + iov[2].iov_len; | |
3979877e SF |
565 | smb_buf->smb_buf_length += count; |
566 | ||
3979877e SF |
567 | BCC_LE(smb_buf) = cpu_to_le16(count); |
568 | ||
2442421b | 569 | rc = SendReceive2(xid, ses, iov, 3 /* num_iovecs */, &resp_buf_type, |
133672ef | 570 | CIFS_STD_OP /* not long */ | CIFS_LOG_ERROR); |
3979877e SF |
571 | /* SMB request buf freed in SendReceive2 */ |
572 | ||
790fe579 SF |
573 | cFYI(1, ("ssetup rc from sendrecv2 is %d", rc)); |
574 | if (rc) | |
3979877e SF |
575 | goto ssetup_exit; |
576 | ||
577 | pSMB = (SESSION_SETUP_ANDX *)iov[0].iov_base; | |
578 | smb_buf = (struct smb_hdr *)iov[0].iov_base; | |
579 | ||
790fe579 | 580 | if ((smb_buf->WordCount != 3) && (smb_buf->WordCount != 4)) { |
3979877e | 581 | rc = -EIO; |
790fe579 | 582 | cERROR(1, ("bad word count %d", smb_buf->WordCount)); |
3979877e SF |
583 | goto ssetup_exit; |
584 | } | |
585 | action = le16_to_cpu(pSMB->resp.Action); | |
586 | if (action & GUEST_LOGIN) | |
189acaae | 587 | cFYI(1, ("Guest login")); /* BB mark SesInfo struct? */ |
3979877e SF |
588 | ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */ |
589 | cFYI(1, ("UID = %d ", ses->Suid)); | |
590 | /* response can have either 3 or 4 word count - Samba sends 3 */ | |
591 | /* and lanman response is 3 */ | |
592 | bytes_remaining = BCC(smb_buf); | |
593 | bcc_ptr = pByteArea(smb_buf); | |
594 | ||
790fe579 | 595 | if (smb_buf->WordCount == 4) { |
3979877e SF |
596 | __u16 blob_len; |
597 | blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength); | |
598 | bcc_ptr += blob_len; | |
790fe579 SF |
599 | if (blob_len > bytes_remaining) { |
600 | cERROR(1, ("bad security blob length %d", blob_len)); | |
3979877e SF |
601 | rc = -EINVAL; |
602 | goto ssetup_exit; | |
603 | } | |
604 | bytes_remaining -= blob_len; | |
790fe579 | 605 | } |
3979877e SF |
606 | |
607 | /* BB check if Unicode and decode strings */ | |
790fe579 | 608 | if (smb_buf->Flags2 & SMBFLG2_UNICODE) |
3979877e SF |
609 | rc = decode_unicode_ssetup(&bcc_ptr, bytes_remaining, |
610 | ses, nls_cp); | |
611 | else | |
63135e08 SF |
612 | rc = decode_ascii_ssetup(&bcc_ptr, bytes_remaining, |
613 | ses, nls_cp); | |
50c2f753 | 614 | |
3979877e | 615 | ssetup_exit: |
2442421b SF |
616 | if (spnego_key) |
617 | key_put(spnego_key); | |
750d1151 | 618 | kfree(str_area); |
790fe579 SF |
619 | if (resp_buf_type == CIFS_SMALL_BUFFER) { |
620 | cFYI(1, ("ssetup freeing small buf %p", iov[0].iov_base)); | |
3979877e | 621 | cifs_small_buf_release(iov[0].iov_base); |
790fe579 | 622 | } else if (resp_buf_type == CIFS_LARGE_BUFFER) |
3979877e SF |
623 | cifs_buf_release(iov[0].iov_base); |
624 | ||
625 | return rc; | |
626 | } |