sess.c 16 KB

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