sess.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * fs/cifs/sess.c
  3. *
  4. * SMB/CIFS session setup handling routines
  5. *
  6. * Copyright (c) International Business Machines Corp., 2006
  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. extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8,
  32. unsigned char *p24);
  33. #ifdef CONFIG_CIFS_EXPERIMENTAL
  34. static __u32 cifs_ssetup_hdr(struct cifsSesInfo *ses, SESSION_SETUP_ANDX *pSMB)
  35. {
  36. __u32 capabilities = 0;
  37. /* init fields common to all four types of SessSetup */
  38. /* note that header is initialized to zero in header_assemble */
  39. pSMB->req.AndXCommand = 0xFF;
  40. pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
  41. pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
  42. /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
  43. /* BB verify whether signing required on neg or just on auth frame
  44. (and NTLM case) */
  45. capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
  46. CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
  47. if(ses->server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  48. pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  49. if (ses->capabilities & CAP_UNICODE) {
  50. pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
  51. capabilities |= CAP_UNICODE;
  52. }
  53. if (ses->capabilities & CAP_STATUS32) {
  54. pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
  55. capabilities |= CAP_STATUS32;
  56. }
  57. if (ses->capabilities & CAP_DFS) {
  58. pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
  59. capabilities |= CAP_DFS;
  60. }
  61. if (ses->capabilities & CAP_UNIX) {
  62. capabilities |= CAP_UNIX;
  63. }
  64. /* BB check whether to init vcnum BB */
  65. return capabilities;
  66. }
  67. static void unicode_ssetup_strings(char ** pbcc_area, struct cifsSesInfo *ses,
  68. const struct nls_table * nls_cp)
  69. {
  70. char * bcc_ptr = *pbcc_area;
  71. int bytes_ret = 0;
  72. /* BB FIXME add check that strings total less
  73. than 335 or will need to send them as arrays */
  74. /* align unicode strings, must be word aligned */
  75. if ((long) bcc_ptr % 2) {
  76. *bcc_ptr = 0;
  77. bcc_ptr++;
  78. }
  79. /* copy user */
  80. if(ses->userName == NULL) {
  81. /* BB what about null user mounts - check that we do this BB */
  82. } else { /* 300 should be long enough for any conceivable user name */
  83. bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->userName,
  84. 300, nls_cp);
  85. }
  86. bcc_ptr += 2 * bytes_ret;
  87. bcc_ptr += 2; /* account for null termination */
  88. /* copy domain */
  89. if(ses->domainName == NULL)
  90. bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr,
  91. "CIFS_LINUX_DOM", 32, nls_cp);
  92. else
  93. bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->domainName,
  94. 256, nls_cp);
  95. bcc_ptr += 2 * bytes_ret;
  96. bcc_ptr += 2; /* account for null terminator */
  97. /* Copy OS version */
  98. bytes_ret = cifs_strtoUCS((__le16 *)bcc_ptr, "Linux version ", 32,
  99. nls_cp);
  100. bcc_ptr += 2 * bytes_ret;
  101. bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, system_utsname.release,
  102. 32, nls_cp);
  103. bcc_ptr += 2 * bytes_ret;
  104. bcc_ptr += 2; /* trailing null */
  105. bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
  106. 32, nls_cp);
  107. bcc_ptr += 2 * bytes_ret;
  108. bcc_ptr += 2; /* trailing null */
  109. *pbcc_area = bcc_ptr;
  110. }
  111. static void ascii_ssetup_strings(char ** pbcc_area, struct cifsSesInfo *ses,
  112. const struct nls_table * nls_cp)
  113. {
  114. char * bcc_ptr = *pbcc_area;
  115. /* copy user */
  116. /* BB what about null user mounts - check that we do this BB */
  117. /* copy user */
  118. if(ses->userName == NULL) {
  119. /* BB what about null user mounts - check that we do this BB */
  120. } else { /* 300 should be long enough for any conceivable user name */
  121. strncpy(bcc_ptr, ses->userName, 300);
  122. }
  123. /* BB improve check for overflow */
  124. bcc_ptr += strnlen(ses->userName, 200);
  125. *bcc_ptr = 0;
  126. bcc_ptr++; /* account for null termination */
  127. /* copy domain */
  128. if(ses->domainName == NULL) {
  129. strcpy(bcc_ptr, "CIFS_LINUX_DOM");
  130. bcc_ptr += 14; /* strlen(CIFS_LINUX_DOM) */
  131. } else {
  132. strncpy(bcc_ptr, ses->domainName, 256);
  133. bcc_ptr += strnlen(ses->domainName, 256);
  134. }
  135. *bcc_ptr = 0;
  136. bcc_ptr++;
  137. /* BB check for overflow here */
  138. strcpy(bcc_ptr, "Linux version ");
  139. bcc_ptr += strlen("Linux version ");
  140. strcpy(bcc_ptr, system_utsname.release);
  141. bcc_ptr += strlen(system_utsname.release) + 1;
  142. strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
  143. bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
  144. *pbcc_area = bcc_ptr;
  145. }
  146. static int decode_unicode_ssetup(char ** pbcc_area, int bleft, struct cifsSesInfo *ses,
  147. const struct nls_table * nls_cp)
  148. {
  149. int rc = 0;
  150. int words_left, len;
  151. char * data = *pbcc_area;
  152. cFYI(1,("bleft %d",bleft));
  153. /* word align, if bytes remaining is not even */
  154. if(bleft % 2) {
  155. bleft--;
  156. data++;
  157. }
  158. words_left = bleft / 2;
  159. /* save off server operating system */
  160. len = UniStrnlen((wchar_t *) data, words_left);
  161. /* We look for obvious messed up bcc or strings in response so we do not go off
  162. the end since (at least) WIN2K and Windows XP have a major bug in not null
  163. terminating last Unicode string in response */
  164. if(len >= words_left)
  165. return rc;
  166. if(ses->serverOS)
  167. kfree(ses->serverOS);
  168. /* UTF-8 string will not grow more than four times as big as UCS-16 */
  169. ses->serverOS = kzalloc(4 * len, GFP_KERNEL);
  170. if(ses->serverOS != NULL) {
  171. cifs_strfromUCS_le(ses->serverOS, (__le16 *)data, len,
  172. nls_cp);
  173. }
  174. data += 2 * (len + 1);
  175. words_left -= len + 1;
  176. /* save off server network operating system */
  177. len = UniStrnlen((wchar_t *) data, words_left);
  178. if(len >= words_left)
  179. return rc;
  180. if(ses->serverNOS)
  181. kfree(ses->serverNOS);
  182. ses->serverNOS = kzalloc(4 * len, GFP_KERNEL); /* BB this is wrong length FIXME BB */
  183. if(ses->serverNOS != NULL) {
  184. cifs_strfromUCS_le(ses->serverNOS, (__le16 *)data, len,
  185. nls_cp);
  186. if(strncmp(ses->serverNOS, "NT LAN Manager 4",16) == 0) {
  187. cFYI(1,("NT4 server"));
  188. ses->flags |= CIFS_SES_NT4;
  189. }
  190. }
  191. data += 2 * (len + 1);
  192. words_left -= len + 1;
  193. /* save off server domain */
  194. len = UniStrnlen((wchar_t *) data, words_left);
  195. if(len > words_left)
  196. return rc;
  197. if(ses->serverDomain)
  198. kfree(ses->serverDomain);
  199. ses->serverDomain = kzalloc(2 * (len + 1), GFP_KERNEL); /* BB FIXME wrong length */
  200. if(ses->serverDomain != NULL) {
  201. cifs_strfromUCS_le(ses->serverDomain, (__le16 *)data, len,
  202. nls_cp);
  203. ses->serverDomain[2*len] = 0;
  204. ses->serverDomain[(2*len) + 1] = 0;
  205. }
  206. data += 2 * (len + 1);
  207. words_left -= len + 1;
  208. cFYI(1,("words left: %d",words_left));
  209. return rc;
  210. }
  211. static int decode_ascii_ssetup(char ** pbcc_area, int bleft, struct cifsSesInfo *ses,
  212. const struct nls_table * nls_cp)
  213. {
  214. int rc = 0;
  215. int len;
  216. char * bcc_ptr = *pbcc_area;
  217. cFYI(1,("decode sessetup ascii. bleft %d", bleft));
  218. len = strnlen(bcc_ptr, bleft);
  219. if(len >= bleft)
  220. return rc;
  221. if(ses->serverOS)
  222. kfree(ses->serverOS);
  223. ses->serverOS = kzalloc(len + 1, GFP_KERNEL);
  224. if(ses->serverOS)
  225. strncpy(ses->serverOS, bcc_ptr, len);
  226. bcc_ptr += len + 1;
  227. bleft -= len + 1;
  228. len = strnlen(bcc_ptr, bleft);
  229. if(len >= bleft)
  230. return rc;
  231. if(ses->serverNOS)
  232. kfree(ses->serverNOS);
  233. ses->serverNOS = kzalloc(len + 1, GFP_KERNEL);
  234. if(ses->serverNOS)
  235. strncpy(ses->serverNOS, bcc_ptr, len);
  236. bcc_ptr += len + 1;
  237. bleft -= len + 1;
  238. len = strnlen(bcc_ptr, bleft);
  239. if(len > bleft)
  240. return rc;
  241. if(ses->serverDomain)
  242. kfree(ses->serverDomain);
  243. ses->serverDomain = kzalloc(len + 1, GFP_KERNEL);
  244. if(ses->serverOS)
  245. strncpy(ses->serverOS, bcc_ptr, len);
  246. bcc_ptr += len + 1;
  247. bleft -= len + 1;
  248. cFYI(1,("ascii: bytes left %d",bleft));
  249. return rc;
  250. }
  251. int
  252. CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, int first_time,
  253. const struct nls_table *nls_cp)
  254. {
  255. int rc = 0;
  256. int wct;
  257. struct smb_hdr *smb_buf;
  258. char *bcc_ptr;
  259. SESSION_SETUP_ANDX *pSMB;
  260. __u32 capabilities;
  261. int count;
  262. int resp_buf_type = 0;
  263. struct kvec iov[1];
  264. enum securityEnum type;
  265. __u16 action;
  266. int bytes_remaining;
  267. cFYI(1,("new sess setup"));
  268. if(ses == NULL)
  269. return -EINVAL;
  270. type = ses->server->secType;
  271. if(type == LANMAN) {
  272. #ifndef CONFIG_CIFS_WEAK_PW_HASH
  273. /* LANMAN and plaintext are less secure and off by default.
  274. So we make this explicitly be turned on in kconfig (in the
  275. build) and turned on at runtime (changed from the default)
  276. in proc/fs/cifs or via mount parm. Unfortunately this is
  277. needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
  278. return -EOPNOTSUPP;
  279. #endif
  280. wct = 10; /* lanman 2 style sessionsetup */
  281. } else if((type == NTLM) || (type == NTLMv2)) {
  282. /* For NTLMv2 failures eventually may need to retry NTLM */
  283. wct = 13; /* old style NTLM sessionsetup */
  284. } else /* same size for negotiate or auth, NTLMSSP or extended security */
  285. wct = 12;
  286. rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
  287. (void **)&smb_buf);
  288. if(rc)
  289. return rc;
  290. pSMB = (SESSION_SETUP_ANDX *)smb_buf;
  291. capabilities = cifs_ssetup_hdr(ses, pSMB);
  292. bcc_ptr = pByteArea(smb_buf);
  293. if(type == LANMAN) {
  294. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  295. char lnm_session_key[CIFS_SESS_KEY_SIZE];
  296. /* no capabilities flags in old lanman negotiation */
  297. pSMB->old_req.PasswordLength = CIFS_SESS_KEY_SIZE;
  298. /* BB calculate hash with password */
  299. /* and copy into bcc */
  300. calc_lanman_hash(ses, lnm_session_key);
  301. #ifdef CONFIG_CIFS_DEBUG2
  302. cifs_dump_mem("cryptkey: ",ses->server->cryptKey,
  303. CIFS_SESS_KEY_SIZE);
  304. #endif
  305. memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_SESS_KEY_SIZE);
  306. bcc_ptr += CIFS_SESS_KEY_SIZE;
  307. /* can not sign if LANMAN negotiated so no need
  308. to calculate signing key? but what if server
  309. changed to do higher than lanman dialect and
  310. we reconnected would we ever calc signing_key? */
  311. cERROR(1,("Negotiating LANMAN setting up strings"));
  312. /* Unicode not allowed for LANMAN dialects */
  313. ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
  314. #endif
  315. } else if (type == NTLM) {
  316. char ntlm_session_key[CIFS_SESS_KEY_SIZE];
  317. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  318. pSMB->req_no_secext.CaseInsensitivePasswordLength =
  319. cpu_to_le16(CIFS_SESS_KEY_SIZE);
  320. pSMB->req_no_secext.CaseSensitivePasswordLength =
  321. cpu_to_le16(CIFS_SESS_KEY_SIZE);
  322. /* calculate session key */
  323. SMBNTencrypt(ses->password, ses->server->cryptKey,
  324. ntlm_session_key);
  325. if(first_time) /* should this be moved into common code
  326. with similar ntlmv2 path? */
  327. cifs_calculate_mac_key( ses->server->mac_signing_key,
  328. ntlm_session_key, ses->password);
  329. /* copy session key */
  330. memcpy(bcc_ptr, (char *)ntlm_session_key,CIFS_SESS_KEY_SIZE);
  331. bcc_ptr += CIFS_SESS_KEY_SIZE;
  332. memcpy(bcc_ptr, (char *)ntlm_session_key,CIFS_SESS_KEY_SIZE);
  333. bcc_ptr += CIFS_SESS_KEY_SIZE;
  334. if(ses->capabilities & CAP_UNICODE)
  335. unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
  336. else
  337. ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
  338. } else if (type == NTLMv2) {
  339. char * v2_sess_key = kmalloc(V2_SESS_KEY_SIZE, GFP_KERNEL);
  340. if(v2_sess_key == NULL) {
  341. cifs_small_buf_release(smb_buf);
  342. return -ENOMEM;
  343. }
  344. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  345. /* LM2 password would be here if we supported it */
  346. pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
  347. /* cpu_to_le16(LM2_SESS_KEY_SIZE); */
  348. pSMB->req_no_secext.CaseSensitivePasswordLength =
  349. cpu_to_le16(V2_SESS_KEY_SIZE);
  350. /* calculate session key */
  351. CalcNTLMv2_response(ses, v2_sess_key);
  352. if(first_time) /* should this be moved into common code
  353. with similar ntlmv2 path? */
  354. /* cifs_calculate_ntlmv2_mac_key(ses->server->mac_signing_key,
  355. response BB FIXME, v2_sess_key); */
  356. /* copy session key */
  357. /* memcpy(bcc_ptr, (char *)ntlm_session_key,LM2_SESS_KEY_SIZE);
  358. bcc_ptr += LM2_SESS_KEY_SIZE; */
  359. memcpy(bcc_ptr, (char *)v2_sess_key, V2_SESS_KEY_SIZE);
  360. bcc_ptr += V2_SESS_KEY_SIZE;
  361. if(ses->capabilities & CAP_UNICODE)
  362. unicode_ssetup_strings(&bcc_ptr, ses, nls_cp);
  363. else
  364. ascii_ssetup_strings(&bcc_ptr, ses, nls_cp);
  365. } else /* NTLMSSP or SPNEGO */ {
  366. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  367. capabilities |= CAP_EXTENDED_SECURITY;
  368. pSMB->req.Capabilities = cpu_to_le32(capabilities);
  369. /* BB set password lengths */
  370. }
  371. count = (long) bcc_ptr - (long) pByteArea(smb_buf);
  372. smb_buf->smb_buf_length += count;
  373. /* if we switch to small buffers, count will need to be fewer
  374. than 383 (strings less than 335 bytes) */
  375. BCC_LE(smb_buf) = cpu_to_le16(count);
  376. /* BB FIXME check for other non ntlm code paths */
  377. /* BB check is this too big for a small smb? */
  378. iov[0].iov_base = (char *)pSMB;
  379. iov[0].iov_len = smb_buf->smb_buf_length + 4;
  380. rc = SendReceive2(xid, ses, iov, 1 /* num_iovecs */, &resp_buf_type, 0);
  381. /* SMB request buf freed in SendReceive2 */
  382. cFYI(1,("ssetup rc from sendrecv2 is %d",rc));
  383. if(rc)
  384. goto ssetup_exit;
  385. pSMB = (SESSION_SETUP_ANDX *)iov[0].iov_base;
  386. smb_buf = (struct smb_hdr *)iov[0].iov_base;
  387. if((smb_buf->WordCount != 3) && (smb_buf->WordCount != 4)) {
  388. rc = -EIO;
  389. cERROR(1,("bad word count %d", smb_buf->WordCount));
  390. goto ssetup_exit;
  391. }
  392. action = le16_to_cpu(pSMB->resp.Action);
  393. if (action & GUEST_LOGIN)
  394. cFYI(1, (" Guest login")); /* BB mark SesInfo struct? */
  395. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  396. cFYI(1, ("UID = %d ", ses->Suid));
  397. /* response can have either 3 or 4 word count - Samba sends 3 */
  398. /* and lanman response is 3 */
  399. bytes_remaining = BCC(smb_buf);
  400. bcc_ptr = pByteArea(smb_buf);
  401. if(smb_buf->WordCount == 4) {
  402. __u16 blob_len;
  403. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  404. bcc_ptr += blob_len;
  405. if(blob_len > bytes_remaining) {
  406. cERROR(1,("bad security blob length %d", blob_len));
  407. rc = -EINVAL;
  408. goto ssetup_exit;
  409. }
  410. bytes_remaining -= blob_len;
  411. }
  412. /* BB check if Unicode and decode strings */
  413. if(smb_buf->Flags2 & SMBFLG2_UNICODE)
  414. rc = decode_unicode_ssetup(&bcc_ptr, bytes_remaining,
  415. ses, nls_cp);
  416. else
  417. rc = decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,nls_cp);
  418. ssetup_exit:
  419. if(resp_buf_type == CIFS_SMALL_BUFFER) {
  420. cFYI(1,("ssetup freeing small buf %p", iov[0].iov_base));
  421. cifs_small_buf_release(iov[0].iov_base);
  422. } else if(resp_buf_type == CIFS_LARGE_BUFFER)
  423. cifs_buf_release(iov[0].iov_base);
  424. return rc;
  425. }
  426. #endif /* CONFIG_CIFS_EXPERIMENTAL */