sess.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. * fs/cifs/sess.c
  3. *
  4. * SMB/CIFS session setup handling routines
  5. *
  6. * Copyright (c) International Business Machines Corp., 2006, 2009
  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. #include "cifspdu.h"
  24. #include "cifsglob.h"
  25. #include "cifsproto.h"
  26. #include "cifs_unicode.h"
  27. #include "cifs_debug.h"
  28. #include "ntlmssp.h"
  29. #include "nterr.h"
  30. #include <linux/utsname.h>
  31. #include "cifs_spnego.h"
  32. extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8,
  33. unsigned char *p24);
  34. /* Checks if this is the first smb session to be reconnected after
  35. the socket has been reestablished (so we know whether to use vc 0).
  36. Called while holding the cifs_tcp_ses_lock, so do not block */
  37. static bool is_first_ses_reconnect(struct cifsSesInfo *ses)
  38. {
  39. struct list_head *tmp;
  40. struct cifsSesInfo *tmp_ses;
  41. list_for_each(tmp, &ses->server->smb_ses_list) {
  42. tmp_ses = list_entry(tmp, struct cifsSesInfo,
  43. smb_ses_list);
  44. if (tmp_ses->need_reconnect == false)
  45. return false;
  46. }
  47. /* could not find a session that was already connected,
  48. this must be the first one we are reconnecting */
  49. return true;
  50. }
  51. /*
  52. * vc number 0 is treated specially by some servers, and should be the
  53. * first one we request. After that we can use vcnumbers up to maxvcs,
  54. * one for each smb session (some Windows versions set maxvcs incorrectly
  55. * so maxvc=1 can be ignored). If we have too many vcs, we can reuse
  56. * any vc but zero (some servers reset the connection on vcnum zero)
  57. *
  58. */
  59. static __le16 get_next_vcnum(struct cifsSesInfo *ses)
  60. {
  61. __u16 vcnum = 0;
  62. struct list_head *tmp;
  63. struct cifsSesInfo *tmp_ses;
  64. __u16 max_vcs = ses->server->max_vcs;
  65. __u16 i;
  66. int free_vc_found = 0;
  67. /* Quoting the MS-SMB specification: "Windows-based SMB servers set this
  68. field to one but do not enforce this limit, which allows an SMB client
  69. to establish more virtual circuits than allowed by this value ... but
  70. other server implementations can enforce this limit." */
  71. if (max_vcs < 2)
  72. max_vcs = 0xFFFF;
  73. write_lock(&cifs_tcp_ses_lock);
  74. if ((ses->need_reconnect) && is_first_ses_reconnect(ses))
  75. goto get_vc_num_exit; /* vcnum will be zero */
  76. for (i = ses->server->srv_count - 1; i < max_vcs; i++) {
  77. if (i == 0) /* this is the only connection, use vc 0 */
  78. break;
  79. free_vc_found = 1;
  80. list_for_each(tmp, &ses->server->smb_ses_list) {
  81. tmp_ses = list_entry(tmp, struct cifsSesInfo,
  82. smb_ses_list);
  83. if (tmp_ses->vcnum == i) {
  84. free_vc_found = 0;
  85. break; /* found duplicate, try next vcnum */
  86. }
  87. }
  88. if (free_vc_found)
  89. break; /* we found a vcnumber that will work - use it */
  90. }
  91. if (i == 0)
  92. vcnum = 0; /* for most common case, ie if one smb session, use
  93. vc zero. Also for case when no free vcnum, zero
  94. is safest to send (some clients only send zero) */
  95. else if (free_vc_found == 0)
  96. vcnum = 1; /* we can not reuse vc=0 safely, since some servers
  97. reset all uids on that, but 1 is ok. */
  98. else
  99. vcnum = i;
  100. ses->vcnum = vcnum;
  101. get_vc_num_exit:
  102. write_unlock(&cifs_tcp_ses_lock);
  103. return cpu_to_le16(vcnum);
  104. }
  105. static __u32 cifs_ssetup_hdr(struct cifsSesInfo *ses, SESSION_SETUP_ANDX *pSMB)
  106. {
  107. __u32 capabilities = 0;
  108. /* init fields common to all four types of SessSetup */
  109. /* Note that offsets for first seven fields in req struct are same */
  110. /* in CIFS Specs so does not matter which of 3 forms of struct */
  111. /* that we use in next few lines */
  112. /* Note that header is initialized to zero in header_assemble */
  113. pSMB->req.AndXCommand = 0xFF;
  114. pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
  115. pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
  116. pSMB->req.VcNumber = get_next_vcnum(ses);
  117. /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
  118. /* BB verify whether signing required on neg or just on auth frame
  119. (and NTLM case) */
  120. capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
  121. CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
  122. if (ses->server->secMode &
  123. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  124. pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  125. if (ses->capabilities & CAP_UNICODE) {
  126. pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
  127. capabilities |= CAP_UNICODE;
  128. }
  129. if (ses->capabilities & CAP_STATUS32) {
  130. pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
  131. capabilities |= CAP_STATUS32;
  132. }
  133. if (ses->capabilities & CAP_DFS) {
  134. pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
  135. capabilities |= CAP_DFS;
  136. }
  137. if (ses->capabilities & CAP_UNIX)
  138. capabilities |= CAP_UNIX;
  139. return capabilities;
  140. }
  141. static void
  142. unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
  143. {
  144. char *bcc_ptr = *pbcc_area;
  145. int bytes_ret = 0;
  146. /* Copy OS version */
  147. bytes_ret = cifs_strtoUCS((__le16 *)bcc_ptr, "Linux version ", 32,
  148. nls_cp);
  149. bcc_ptr += 2 * bytes_ret;
  150. bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, init_utsname()->release,
  151. 32, nls_cp);
  152. bcc_ptr += 2 * bytes_ret;
  153. bcc_ptr += 2; /* trailing null */
  154. bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
  155. 32, nls_cp);
  156. bcc_ptr += 2 * bytes_ret;
  157. bcc_ptr += 2; /* trailing null */
  158. *pbcc_area = bcc_ptr;
  159. }
  160. static void unicode_domain_string(char **pbcc_area, struct cifsSesInfo *ses,
  161. const struct nls_table *nls_cp)
  162. {
  163. char *bcc_ptr = *pbcc_area;
  164. int bytes_ret = 0;
  165. /* copy domain */
  166. if (ses->domainName == NULL) {
  167. /* Sending null domain better than using a bogus domain name (as
  168. we did briefly in 2.6.18) since server will use its default */
  169. *bcc_ptr = 0;
  170. *(bcc_ptr+1) = 0;
  171. bytes_ret = 0;
  172. } else
  173. bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->domainName,
  174. 256, nls_cp);
  175. bcc_ptr += 2 * bytes_ret;
  176. bcc_ptr += 2; /* account for null terminator */
  177. *pbcc_area = bcc_ptr;
  178. }
  179. static void unicode_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses,
  180. const struct nls_table *nls_cp)
  181. {
  182. char *bcc_ptr = *pbcc_area;
  183. int bytes_ret = 0;
  184. /* BB FIXME add check that strings total less
  185. than 335 or will need to send them as arrays */
  186. /* unicode strings, must be word aligned before the call */
  187. /* if ((long) bcc_ptr % 2) {
  188. *bcc_ptr = 0;
  189. bcc_ptr++;
  190. } */
  191. /* copy user */
  192. if (ses->userName == NULL) {
  193. /* null user mount */
  194. *bcc_ptr = 0;
  195. *(bcc_ptr+1) = 0;
  196. } else { /* 300 should be long enough for any conceivable user name */
  197. bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->userName,
  198. 300, nls_cp);
  199. }
  200. bcc_ptr += 2 * bytes_ret;
  201. bcc_ptr += 2; /* account for null termination */
  202. unicode_domain_string(&bcc_ptr, ses, nls_cp);
  203. unicode_oslm_strings(&bcc_ptr, nls_cp);
  204. *pbcc_area = bcc_ptr;
  205. }
  206. static void ascii_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses,
  207. const struct nls_table *nls_cp)
  208. {
  209. char *bcc_ptr = *pbcc_area;
  210. /* copy user */
  211. /* BB what about null user mounts - check that we do this BB */
  212. /* copy user */
  213. if (ses->userName == NULL) {
  214. /* BB what about null user mounts - check that we do this BB */
  215. } else { /* 300 should be long enough for any conceivable user name */
  216. strncpy(bcc_ptr, ses->userName, 300);
  217. }
  218. /* BB improve check for overflow */
  219. bcc_ptr += strnlen(ses->userName, 300);
  220. *bcc_ptr = 0;
  221. bcc_ptr++; /* account for null termination */
  222. /* copy domain */
  223. if (ses->domainName != NULL) {
  224. strncpy(bcc_ptr, ses->domainName, 256);
  225. bcc_ptr += strnlen(ses->domainName, 256);
  226. } /* else we will send a null domain name
  227. so the server will default to its own domain */
  228. *bcc_ptr = 0;
  229. bcc_ptr++;
  230. /* BB check for overflow here */
  231. strcpy(bcc_ptr, "Linux version ");
  232. bcc_ptr += strlen("Linux version ");
  233. strcpy(bcc_ptr, init_utsname()->release);
  234. bcc_ptr += strlen(init_utsname()->release) + 1;
  235. strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
  236. bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
  237. *pbcc_area = bcc_ptr;
  238. }
  239. static void
  240. decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifsSesInfo *ses,
  241. const struct nls_table *nls_cp)
  242. {
  243. int len;
  244. char *data = *pbcc_area;
  245. cFYI(1, ("bleft %d", bleft));
  246. /*
  247. * Windows servers do not always double null terminate their final
  248. * Unicode string. Check to see if there are an uneven number of bytes
  249. * left. If so, then add an extra NULL pad byte to the end of the
  250. * response.
  251. *
  252. * See section 2.7.2 in "Implementing CIFS" for details
  253. */
  254. if (bleft % 2) {
  255. data[bleft] = 0;
  256. ++bleft;
  257. }
  258. kfree(ses->serverOS);
  259. ses->serverOS = cifs_strndup_from_ucs(data, bleft, true, nls_cp);
  260. cFYI(1, ("serverOS=%s", ses->serverOS));
  261. len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
  262. data += len;
  263. bleft -= len;
  264. if (bleft <= 0)
  265. return;
  266. kfree(ses->serverNOS);
  267. ses->serverNOS = cifs_strndup_from_ucs(data, bleft, true, nls_cp);
  268. cFYI(1, ("serverNOS=%s", ses->serverNOS));
  269. len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
  270. data += len;
  271. bleft -= len;
  272. if (bleft <= 0)
  273. return;
  274. kfree(ses->serverDomain);
  275. ses->serverDomain = cifs_strndup_from_ucs(data, bleft, true, nls_cp);
  276. cFYI(1, ("serverDomain=%s", ses->serverDomain));
  277. return;
  278. }
  279. static int decode_ascii_ssetup(char **pbcc_area, int bleft,
  280. struct cifsSesInfo *ses,
  281. const struct nls_table *nls_cp)
  282. {
  283. int rc = 0;
  284. int len;
  285. char *bcc_ptr = *pbcc_area;
  286. cFYI(1, ("decode sessetup ascii. bleft %d", bleft));
  287. len = strnlen(bcc_ptr, bleft);
  288. if (len >= bleft)
  289. return rc;
  290. kfree(ses->serverOS);
  291. ses->serverOS = kzalloc(len + 1, GFP_KERNEL);
  292. if (ses->serverOS)
  293. strncpy(ses->serverOS, bcc_ptr, len);
  294. if (strncmp(ses->serverOS, "OS/2", 4) == 0) {
  295. cFYI(1, ("OS/2 server"));
  296. ses->flags |= CIFS_SES_OS2;
  297. }
  298. bcc_ptr += len + 1;
  299. bleft -= len + 1;
  300. len = strnlen(bcc_ptr, bleft);
  301. if (len >= bleft)
  302. return rc;
  303. kfree(ses->serverNOS);
  304. ses->serverNOS = kzalloc(len + 1, GFP_KERNEL);
  305. if (ses->serverNOS)
  306. strncpy(ses->serverNOS, bcc_ptr, len);
  307. bcc_ptr += len + 1;
  308. bleft -= len + 1;
  309. len = strnlen(bcc_ptr, bleft);
  310. if (len > bleft)
  311. return rc;
  312. /* No domain field in LANMAN case. Domain is
  313. returned by old servers in the SMB negprot response */
  314. /* BB For newer servers which do not support Unicode,
  315. but thus do return domain here we could add parsing
  316. for it later, but it is not very important */
  317. cFYI(1, ("ascii: bytes left %d", bleft));
  318. return rc;
  319. }
  320. static int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
  321. struct cifsSesInfo *ses)
  322. {
  323. CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
  324. if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
  325. cERROR(1, ("challenge blob len %d too small", blob_len));
  326. return -EINVAL;
  327. }
  328. if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
  329. cERROR(1, ("blob signature incorrect %s", pblob->Signature));
  330. return -EINVAL;
  331. }
  332. if (pblob->MessageType != NtLmChallenge) {
  333. cERROR(1, ("Incorrect message type %d", pblob->MessageType));
  334. return -EINVAL;
  335. }
  336. memcpy(ses->server->cryptKey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
  337. /* BB we could decode pblob->NegotiateFlags; some may be useful */
  338. /* In particular we can examine sign flags */
  339. /* BB spec says that if AvId field of MsvAvTimestamp is populated then
  340. we must set the MIC field of the AUTHENTICATE_MESSAGE */
  341. return 0;
  342. }
  343. #ifdef CONFIG_CIFS_EXPERIMENTAL
  344. /* BB Move to ntlmssp.c eventually */
  345. /* We do not malloc the blob, it is passed in pbuffer, because
  346. it is fixed size, and small, making this approach cleaner */
  347. static void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,
  348. struct cifsSesInfo *ses)
  349. {
  350. NEGOTIATE_MESSAGE *sec_blob = (NEGOTIATE_MESSAGE *)pbuffer;
  351. __u32 flags;
  352. memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
  353. sec_blob->MessageType = NtLmNegotiate;
  354. /* BB is NTLMV2 session security format easier to use here? */
  355. flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
  356. NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
  357. NTLMSSP_NEGOTIATE_NT_ONLY | NTLMSSP_NEGOTIATE_NTLM;
  358. if (ses->server->secMode &
  359. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  360. flags |= NTLMSSP_NEGOTIATE_SIGN;
  361. if (ses->server->secMode & SECMODE_SIGN_REQUIRED)
  362. flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
  363. sec_blob->NegotiateFlags |= cpu_to_le32(flags);
  364. sec_blob->WorkstationName.BufferOffset = 0;
  365. sec_blob->WorkstationName.Length = 0;
  366. sec_blob->WorkstationName.MaximumLength = 0;
  367. /* Domain name is sent on the Challenge not Negotiate NTLMSSP request */
  368. sec_blob->DomainName.BufferOffset = 0;
  369. sec_blob->DomainName.Length = 0;
  370. sec_blob->DomainName.MaximumLength = 0;
  371. }
  372. /* We do not malloc the blob, it is passed in pbuffer, because its
  373. maximum possible size is fixed and small, making this approach cleaner.
  374. This function returns the length of the data in the blob */
  375. static int build_ntlmssp_auth_blob(unsigned char *pbuffer,
  376. struct cifsSesInfo *ses,
  377. const struct nls_table *nls_cp, int first)
  378. {
  379. AUTHENTICATE_MESSAGE *sec_blob = (AUTHENTICATE_MESSAGE *)pbuffer;
  380. __u32 flags;
  381. unsigned char *tmp;
  382. char ntlm_session_key[CIFS_SESS_KEY_SIZE];
  383. memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
  384. sec_blob->MessageType = NtLmAuthenticate;
  385. flags = NTLMSSP_NEGOTIATE_56 |
  386. NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
  387. NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
  388. NTLMSSP_NEGOTIATE_NT_ONLY | NTLMSSP_NEGOTIATE_NTLM;
  389. if (ses->server->secMode &
  390. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  391. flags |= NTLMSSP_NEGOTIATE_SIGN;
  392. if (ses->server->secMode & SECMODE_SIGN_REQUIRED)
  393. flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
  394. tmp = pbuffer + sizeof(AUTHENTICATE_MESSAGE);
  395. sec_blob->NegotiateFlags |= cpu_to_le32(flags);
  396. sec_blob->LmChallengeResponse.BufferOffset =
  397. cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
  398. sec_blob->LmChallengeResponse.Length = 0;
  399. sec_blob->LmChallengeResponse.MaximumLength = 0;
  400. /* calculate session key, BB what about adding similar ntlmv2 path? */
  401. SMBNTencrypt(ses->password, ses->server->cryptKey, ntlm_session_key);
  402. if (first)
  403. cifs_calculate_mac_key(&ses->server->mac_signing_key,
  404. ntlm_session_key, ses->password);
  405. memcpy(tmp, ntlm_session_key, CIFS_SESS_KEY_SIZE);
  406. sec_blob->NtChallengeResponse.BufferOffset = cpu_to_le32(tmp - pbuffer);
  407. sec_blob->NtChallengeResponse.Length = cpu_to_le16(CIFS_SESS_KEY_SIZE);
  408. sec_blob->NtChallengeResponse.MaximumLength =
  409. cpu_to_le16(CIFS_SESS_KEY_SIZE);
  410. tmp += CIFS_SESS_KEY_SIZE;
  411. if (ses->domainName == NULL) {
  412. sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - pbuffer);
  413. sec_blob->DomainName.Length = 0;
  414. sec_blob->DomainName.MaximumLength = 0;
  415. tmp += 2;
  416. } else {
  417. int len;
  418. len = cifs_strtoUCS((__le16 *)tmp, ses->domainName,
  419. MAX_USERNAME_SIZE, nls_cp);
  420. len *= 2; /* unicode is 2 bytes each */
  421. len += 2; /* trailing null */
  422. sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - pbuffer);
  423. sec_blob->DomainName.Length = cpu_to_le16(len);
  424. sec_blob->DomainName.MaximumLength = cpu_to_le16(len);
  425. tmp += len;
  426. }
  427. if (ses->userName == NULL) {
  428. sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - pbuffer);
  429. sec_blob->UserName.Length = 0;
  430. sec_blob->UserName.MaximumLength = 0;
  431. tmp += 2;
  432. } else {
  433. int len;
  434. len = cifs_strtoUCS((__le16 *)tmp, ses->userName,
  435. MAX_USERNAME_SIZE, nls_cp);
  436. len *= 2; /* unicode is 2 bytes each */
  437. len += 2; /* trailing null */
  438. sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - pbuffer);
  439. sec_blob->UserName.Length = cpu_to_le16(len);
  440. sec_blob->UserName.MaximumLength = cpu_to_le16(len);
  441. tmp += len;
  442. }
  443. sec_blob->WorkstationName.BufferOffset = cpu_to_le32(tmp - pbuffer);
  444. sec_blob->WorkstationName.Length = 0;
  445. sec_blob->WorkstationName.MaximumLength = 0;
  446. tmp += 2;
  447. sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - pbuffer);
  448. sec_blob->SessionKey.Length = 0;
  449. sec_blob->SessionKey.MaximumLength = 0;
  450. return tmp - pbuffer;
  451. }
  452. static void setup_ntlmssp_neg_req(SESSION_SETUP_ANDX *pSMB,
  453. struct cifsSesInfo *ses)
  454. {
  455. build_ntlmssp_negotiate_blob(&pSMB->req.SecurityBlob[0], ses);
  456. pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
  457. return;
  458. }
  459. static int setup_ntlmssp_auth_req(SESSION_SETUP_ANDX *pSMB,
  460. struct cifsSesInfo *ses,
  461. const struct nls_table *nls, int first_time)
  462. {
  463. int bloblen;
  464. bloblen = build_ntlmssp_auth_blob(&pSMB->req.SecurityBlob[0], ses, nls,
  465. first_time);
  466. pSMB->req.SecurityBlobLength = cpu_to_le16(bloblen);
  467. return bloblen;
  468. }
  469. #endif
  470. int
  471. CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, int first_time,
  472. const struct nls_table *nls_cp)
  473. {
  474. int rc = 0;
  475. int wct;
  476. struct smb_hdr *smb_buf;
  477. char *bcc_ptr;
  478. char *str_area;
  479. SESSION_SETUP_ANDX *pSMB;
  480. __u32 capabilities;
  481. int count;
  482. int resp_buf_type;
  483. struct kvec iov[3];
  484. enum securityEnum type;
  485. __u16 action;
  486. int bytes_remaining;
  487. struct key *spnego_key = NULL;
  488. __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
  489. if (ses == NULL)
  490. return -EINVAL;
  491. type = ses->server->secType;
  492. cFYI(1, ("sess setup type %d", type));
  493. ssetup_ntlmssp_authenticate:
  494. if (phase == NtLmChallenge)
  495. phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
  496. if (type == LANMAN) {
  497. #ifndef CONFIG_CIFS_WEAK_PW_HASH
  498. /* LANMAN and plaintext are less secure and off by default.
  499. So we make this explicitly be turned on in kconfig (in the
  500. build) and turned on at runtime (changed from the default)
  501. in proc/fs/cifs or via mount parm. Unfortunately this is
  502. needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
  503. return -EOPNOTSUPP;
  504. #endif
  505. wct = 10; /* lanman 2 style sessionsetup */
  506. } else if ((type == NTLM) || (type == NTLMv2)) {
  507. /* For NTLMv2 failures eventually may need to retry NTLM */
  508. wct = 13; /* old style NTLM sessionsetup */
  509. } else /* same size: negotiate or auth, NTLMSSP or extended security */
  510. wct = 12;
  511. rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
  512. (void **)&smb_buf);
  513. if (rc)
  514. return rc;
  515. pSMB = (SESSION_SETUP_ANDX *)smb_buf;
  516. capabilities = cifs_ssetup_hdr(ses, pSMB);
  517. /* we will send the SMB in three pieces:
  518. a fixed length beginning part, an optional
  519. SPNEGO blob (which can be zero length), and a
  520. last part which will include the strings
  521. and rest of bcc area. This allows us to avoid
  522. a large buffer 17K allocation */
  523. iov[0].iov_base = (char *)pSMB;
  524. iov[0].iov_len = smb_buf->smb_buf_length + 4;
  525. /* setting this here allows the code at the end of the function
  526. to free the request buffer if there's an error */
  527. resp_buf_type = CIFS_SMALL_BUFFER;
  528. /* 2000 big enough to fit max user, domain, NOS name etc. */
  529. str_area = kmalloc(2000, GFP_KERNEL);
  530. if (str_area == NULL) {
  531. rc = -ENOMEM;
  532. goto ssetup_exit;
  533. }
  534. bcc_ptr = str_area;
  535. ses->flags &= ~CIFS_SES_LANMAN;
  536. iov[1].iov_base = NULL;
  537. iov[1].iov_len = 0;
  538. if (type == LANMAN) {
  539. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  540. char lnm_session_key[CIFS_SESS_KEY_SIZE];
  541. pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
  542. /* no capabilities flags in old lanman negotiation */
  543. pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
  544. /* BB calculate hash with password */
  545. /* and copy into bcc */
  546. calc_lanman_hash(ses->password, ses->server->cryptKey,
  547. ses->server->secMode & SECMODE_PW_ENCRYPT ?
  548. true : false, lnm_session_key);
  549. ses->flags |= CIFS_SES_LANMAN;
  550. memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_SESS_KEY_SIZE);
  551. bcc_ptr += CIFS_SESS_KEY_SIZE;
  552. /* can not sign if LANMAN negotiated so no need
  553. to calculate signing key? but what if server
  554. changed to do higher than lanman dialect and
  555. we reconnected would we ever calc signing_key? */
  556. cFYI(1, ("Negotiating LANMAN setting up strings"));
  557. /* Unicode not allowed for LANMAN dialects */
  558. ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
  559. #endif
  560. } else if (type == NTLM) {
  561. char ntlm_session_key[CIFS_SESS_KEY_SIZE];
  562. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  563. pSMB->req_no_secext.CaseInsensitivePasswordLength =
  564. cpu_to_le16(CIFS_SESS_KEY_SIZE);
  565. pSMB->req_no_secext.CaseSensitivePasswordLength =
  566. cpu_to_le16(CIFS_SESS_KEY_SIZE);
  567. /* calculate session key */
  568. SMBNTencrypt(ses->password, ses->server->cryptKey,
  569. ntlm_session_key);
  570. if (first_time) /* should this be moved into common code
  571. with similar ntlmv2 path? */
  572. cifs_calculate_mac_key(&ses->server->mac_signing_key,
  573. ntlm_session_key, ses->password);
  574. /* copy session key */
  575. memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE);
  576. bcc_ptr += CIFS_SESS_KEY_SIZE;
  577. memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE);
  578. bcc_ptr += CIFS_SESS_KEY_SIZE;
  579. if (ses->capabilities & CAP_UNICODE) {
  580. /* unicode strings must be word aligned */
  581. if (iov[0].iov_len % 2) {
  582. *bcc_ptr = 0;
  583. bcc_ptr++;
  584. }
  585. unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
  586. } else
  587. ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
  588. } else if (type == NTLMv2) {
  589. char *v2_sess_key =
  590. kmalloc(sizeof(struct ntlmv2_resp), GFP_KERNEL);
  591. /* BB FIXME change all users of v2_sess_key to
  592. struct ntlmv2_resp */
  593. if (v2_sess_key == NULL) {
  594. rc = -ENOMEM;
  595. goto ssetup_exit;
  596. }
  597. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  598. /* LM2 password would be here if we supported it */
  599. pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
  600. /* cpu_to_le16(LM2_SESS_KEY_SIZE); */
  601. pSMB->req_no_secext.CaseSensitivePasswordLength =
  602. cpu_to_le16(sizeof(struct ntlmv2_resp));
  603. /* calculate session key */
  604. setup_ntlmv2_rsp(ses, v2_sess_key, nls_cp);
  605. if (first_time) /* should this be moved into common code
  606. with similar ntlmv2 path? */
  607. /* cifs_calculate_ntlmv2_mac_key(ses->server->mac_signing_key,
  608. response BB FIXME, v2_sess_key); */
  609. /* copy session key */
  610. /* memcpy(bcc_ptr, (char *)ntlm_session_key,LM2_SESS_KEY_SIZE);
  611. bcc_ptr += LM2_SESS_KEY_SIZE; */
  612. memcpy(bcc_ptr, (char *)v2_sess_key,
  613. sizeof(struct ntlmv2_resp));
  614. bcc_ptr += sizeof(struct ntlmv2_resp);
  615. kfree(v2_sess_key);
  616. if (ses->capabilities & CAP_UNICODE) {
  617. if (iov[0].iov_len % 2) {
  618. *bcc_ptr = 0;
  619. bcc_ptr++;
  620. }
  621. unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
  622. } else
  623. ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
  624. } else if (type == Kerberos || type == MSKerberos) {
  625. #ifdef CONFIG_CIFS_UPCALL
  626. struct cifs_spnego_msg *msg;
  627. spnego_key = cifs_get_spnego_key(ses);
  628. if (IS_ERR(spnego_key)) {
  629. rc = PTR_ERR(spnego_key);
  630. spnego_key = NULL;
  631. goto ssetup_exit;
  632. }
  633. msg = spnego_key->payload.data;
  634. /* check version field to make sure that cifs.upcall is
  635. sending us a response in an expected form */
  636. if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
  637. cERROR(1, ("incorrect version of cifs.upcall (expected"
  638. " %d but got %d)",
  639. CIFS_SPNEGO_UPCALL_VERSION, msg->version));
  640. rc = -EKEYREJECTED;
  641. goto ssetup_exit;
  642. }
  643. /* bail out if key is too long */
  644. if (msg->sesskey_len >
  645. sizeof(ses->server->mac_signing_key.data.krb5)) {
  646. cERROR(1, ("Kerberos signing key too long (%u bytes)",
  647. msg->sesskey_len));
  648. rc = -EOVERFLOW;
  649. goto ssetup_exit;
  650. }
  651. if (first_time) {
  652. ses->server->mac_signing_key.len = msg->sesskey_len;
  653. memcpy(ses->server->mac_signing_key.data.krb5,
  654. msg->data, msg->sesskey_len);
  655. }
  656. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  657. capabilities |= CAP_EXTENDED_SECURITY;
  658. pSMB->req.Capabilities = cpu_to_le32(capabilities);
  659. iov[1].iov_base = msg->data + msg->sesskey_len;
  660. iov[1].iov_len = msg->secblob_len;
  661. pSMB->req.SecurityBlobLength = cpu_to_le16(iov[1].iov_len);
  662. if (ses->capabilities & CAP_UNICODE) {
  663. /* unicode strings must be word aligned */
  664. if ((iov[0].iov_len + iov[1].iov_len) % 2) {
  665. *bcc_ptr = 0;
  666. bcc_ptr++;
  667. }
  668. unicode_oslm_strings(&bcc_ptr, nls_cp);
  669. unicode_domain_string(&bcc_ptr, ses, nls_cp);
  670. } else
  671. /* BB: is this right? */
  672. ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
  673. #else /* ! CONFIG_CIFS_UPCALL */
  674. cERROR(1, ("Kerberos negotiated but upcall support disabled!"));
  675. rc = -ENOSYS;
  676. goto ssetup_exit;
  677. #endif /* CONFIG_CIFS_UPCALL */
  678. } else {
  679. #ifdef CONFIG_CIFS_EXPERIMENTAL
  680. if ((experimEnabled > 1) && (type == RawNTLMSSP)) {
  681. if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
  682. cERROR(1, ("NTLMSSP requires Unicode support"));
  683. rc = -ENOSYS;
  684. goto ssetup_exit;
  685. }
  686. cFYI(1, ("ntlmssp session setup phase %d", phase));
  687. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  688. capabilities |= CAP_EXTENDED_SECURITY;
  689. pSMB->req.Capabilities |= cpu_to_le32(capabilities);
  690. if (phase == NtLmNegotiate) {
  691. setup_ntlmssp_neg_req(pSMB, ses);
  692. iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
  693. } else if (phase == NtLmAuthenticate) {
  694. int blob_len;
  695. blob_len = setup_ntlmssp_auth_req(pSMB, ses,
  696. nls_cp,
  697. first_time);
  698. iov[1].iov_len = blob_len;
  699. /* Make sure that we tell the server that we
  700. are using the uid that it just gave us back
  701. on the response (challenge) */
  702. smb_buf->Uid = ses->Suid;
  703. } else {
  704. cERROR(1, ("invalid phase %d", phase));
  705. rc = -ENOSYS;
  706. goto ssetup_exit;
  707. }
  708. iov[1].iov_base = &pSMB->req.SecurityBlob[0];
  709. /* unicode strings must be word aligned */
  710. if ((iov[0].iov_len + iov[1].iov_len) % 2) {
  711. *bcc_ptr = 0;
  712. bcc_ptr++;
  713. }
  714. unicode_oslm_strings(&bcc_ptr, nls_cp);
  715. } else {
  716. cERROR(1, ("secType %d not supported!", type));
  717. rc = -ENOSYS;
  718. goto ssetup_exit;
  719. }
  720. #else
  721. cERROR(1, ("secType %d not supported!", type));
  722. rc = -ENOSYS;
  723. goto ssetup_exit;
  724. #endif
  725. }
  726. iov[2].iov_base = str_area;
  727. iov[2].iov_len = (long) bcc_ptr - (long) str_area;
  728. count = iov[1].iov_len + iov[2].iov_len;
  729. smb_buf->smb_buf_length += count;
  730. BCC_LE(smb_buf) = cpu_to_le16(count);
  731. rc = SendReceive2(xid, ses, iov, 3 /* num_iovecs */, &resp_buf_type,
  732. CIFS_STD_OP /* not long */ | CIFS_LOG_ERROR);
  733. /* SMB request buf freed in SendReceive2 */
  734. cFYI(1, ("ssetup rc from sendrecv2 is %d", rc));
  735. pSMB = (SESSION_SETUP_ANDX *)iov[0].iov_base;
  736. smb_buf = (struct smb_hdr *)iov[0].iov_base;
  737. if ((type == RawNTLMSSP) && (smb_buf->Status.CifsError ==
  738. cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))) {
  739. if (phase != NtLmNegotiate) {
  740. cERROR(1, ("Unexpected more processing error"));
  741. goto ssetup_exit;
  742. }
  743. /* NTLMSSP Negotiate sent now processing challenge (response) */
  744. phase = NtLmChallenge; /* process ntlmssp challenge */
  745. rc = 0; /* MORE_PROC rc is not an error here, but expected */
  746. }
  747. if (rc)
  748. goto ssetup_exit;
  749. if ((smb_buf->WordCount != 3) && (smb_buf->WordCount != 4)) {
  750. rc = -EIO;
  751. cERROR(1, ("bad word count %d", smb_buf->WordCount));
  752. goto ssetup_exit;
  753. }
  754. action = le16_to_cpu(pSMB->resp.Action);
  755. if (action & GUEST_LOGIN)
  756. cFYI(1, ("Guest login")); /* BB mark SesInfo struct? */
  757. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  758. cFYI(1, ("UID = %d ", ses->Suid));
  759. /* response can have either 3 or 4 word count - Samba sends 3 */
  760. /* and lanman response is 3 */
  761. bytes_remaining = BCC(smb_buf);
  762. bcc_ptr = pByteArea(smb_buf);
  763. if (smb_buf->WordCount == 4) {
  764. __u16 blob_len;
  765. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  766. if (blob_len > bytes_remaining) {
  767. cERROR(1, ("bad security blob length %d", blob_len));
  768. rc = -EINVAL;
  769. goto ssetup_exit;
  770. }
  771. if (phase == NtLmChallenge) {
  772. rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
  773. /* now goto beginning for ntlmssp authenticate phase */
  774. if (rc)
  775. goto ssetup_exit;
  776. }
  777. bcc_ptr += blob_len;
  778. bytes_remaining -= blob_len;
  779. }
  780. /* BB check if Unicode and decode strings */
  781. if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  782. /* unicode string area must be word-aligned */
  783. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  784. ++bcc_ptr;
  785. --bytes_remaining;
  786. }
  787. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses, nls_cp);
  788. } else {
  789. rc = decode_ascii_ssetup(&bcc_ptr, bytes_remaining,
  790. ses, nls_cp);
  791. }
  792. ssetup_exit:
  793. if (spnego_key) {
  794. key_revoke(spnego_key);
  795. key_put(spnego_key);
  796. }
  797. kfree(str_area);
  798. if (resp_buf_type == CIFS_SMALL_BUFFER) {
  799. cFYI(1, ("ssetup freeing small buf %p", iov[0].iov_base));
  800. cifs_small_buf_release(iov[0].iov_base);
  801. } else if (resp_buf_type == CIFS_LARGE_BUFFER)
  802. cifs_buf_release(iov[0].iov_base);
  803. /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
  804. if ((phase == NtLmChallenge) && (rc == 0))
  805. goto ssetup_ntlmssp_authenticate;
  806. return rc;
  807. }