sess.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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 <linux/slab.h>
  32. #include "cifs_spnego.h"
  33. static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, SESSION_SETUP_ANDX *pSMB)
  34. {
  35. __u32 capabilities = 0;
  36. /* init fields common to all four types of SessSetup */
  37. /* Note that offsets for first seven fields in req struct are same */
  38. /* in CIFS Specs so does not matter which of 3 forms of struct */
  39. /* that we use in next few lines */
  40. /* Note that header is initialized to zero in header_assemble */
  41. pSMB->req.AndXCommand = 0xFF;
  42. pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
  43. CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
  44. USHRT_MAX));
  45. pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
  46. pSMB->req.VcNumber = cpu_to_le16(1);
  47. /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
  48. /* BB verify whether signing required on neg or just on auth frame
  49. (and NTLM case) */
  50. capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
  51. CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
  52. if (ses->server->sign)
  53. pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  54. if (ses->capabilities & CAP_UNICODE) {
  55. pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
  56. capabilities |= CAP_UNICODE;
  57. }
  58. if (ses->capabilities & CAP_STATUS32) {
  59. pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
  60. capabilities |= CAP_STATUS32;
  61. }
  62. if (ses->capabilities & CAP_DFS) {
  63. pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
  64. capabilities |= CAP_DFS;
  65. }
  66. if (ses->capabilities & CAP_UNIX)
  67. capabilities |= CAP_UNIX;
  68. return capabilities;
  69. }
  70. static void
  71. unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
  72. {
  73. char *bcc_ptr = *pbcc_area;
  74. int bytes_ret = 0;
  75. /* Copy OS version */
  76. bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
  77. nls_cp);
  78. bcc_ptr += 2 * bytes_ret;
  79. bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
  80. 32, nls_cp);
  81. bcc_ptr += 2 * bytes_ret;
  82. bcc_ptr += 2; /* trailing null */
  83. bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
  84. 32, nls_cp);
  85. bcc_ptr += 2 * bytes_ret;
  86. bcc_ptr += 2; /* trailing null */
  87. *pbcc_area = bcc_ptr;
  88. }
  89. static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
  90. const struct nls_table *nls_cp)
  91. {
  92. char *bcc_ptr = *pbcc_area;
  93. int bytes_ret = 0;
  94. /* copy domain */
  95. if (ses->domainName == NULL) {
  96. /* Sending null domain better than using a bogus domain name (as
  97. we did briefly in 2.6.18) since server will use its default */
  98. *bcc_ptr = 0;
  99. *(bcc_ptr+1) = 0;
  100. bytes_ret = 0;
  101. } else
  102. bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
  103. CIFS_MAX_DOMAINNAME_LEN, nls_cp);
  104. bcc_ptr += 2 * bytes_ret;
  105. bcc_ptr += 2; /* account for null terminator */
  106. *pbcc_area = bcc_ptr;
  107. }
  108. static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
  109. const struct nls_table *nls_cp)
  110. {
  111. char *bcc_ptr = *pbcc_area;
  112. int bytes_ret = 0;
  113. /* BB FIXME add check that strings total less
  114. than 335 or will need to send them as arrays */
  115. /* unicode strings, must be word aligned before the call */
  116. /* if ((long) bcc_ptr % 2) {
  117. *bcc_ptr = 0;
  118. bcc_ptr++;
  119. } */
  120. /* copy user */
  121. if (ses->user_name == NULL) {
  122. /* null user mount */
  123. *bcc_ptr = 0;
  124. *(bcc_ptr+1) = 0;
  125. } else {
  126. bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
  127. CIFS_MAX_USERNAME_LEN, nls_cp);
  128. }
  129. bcc_ptr += 2 * bytes_ret;
  130. bcc_ptr += 2; /* account for null termination */
  131. unicode_domain_string(&bcc_ptr, ses, nls_cp);
  132. unicode_oslm_strings(&bcc_ptr, nls_cp);
  133. *pbcc_area = bcc_ptr;
  134. }
  135. static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
  136. const struct nls_table *nls_cp)
  137. {
  138. char *bcc_ptr = *pbcc_area;
  139. /* copy user */
  140. /* BB what about null user mounts - check that we do this BB */
  141. /* copy user */
  142. if (ses->user_name != NULL) {
  143. strncpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
  144. bcc_ptr += strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
  145. }
  146. /* else null user mount */
  147. *bcc_ptr = 0;
  148. bcc_ptr++; /* account for null termination */
  149. /* copy domain */
  150. if (ses->domainName != NULL) {
  151. strncpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
  152. bcc_ptr += strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
  153. } /* else we will send a null domain name
  154. so the server will default to its own domain */
  155. *bcc_ptr = 0;
  156. bcc_ptr++;
  157. /* BB check for overflow here */
  158. strcpy(bcc_ptr, "Linux version ");
  159. bcc_ptr += strlen("Linux version ");
  160. strcpy(bcc_ptr, init_utsname()->release);
  161. bcc_ptr += strlen(init_utsname()->release) + 1;
  162. strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
  163. bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
  164. *pbcc_area = bcc_ptr;
  165. }
  166. static void
  167. decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
  168. const struct nls_table *nls_cp)
  169. {
  170. int len;
  171. char *data = *pbcc_area;
  172. cifs_dbg(FYI, "bleft %d\n", bleft);
  173. kfree(ses->serverOS);
  174. ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
  175. cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
  176. len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
  177. data += len;
  178. bleft -= len;
  179. if (bleft <= 0)
  180. return;
  181. kfree(ses->serverNOS);
  182. ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
  183. cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
  184. len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
  185. data += len;
  186. bleft -= len;
  187. if (bleft <= 0)
  188. return;
  189. kfree(ses->serverDomain);
  190. ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
  191. cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
  192. return;
  193. }
  194. static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
  195. struct cifs_ses *ses,
  196. const struct nls_table *nls_cp)
  197. {
  198. int len;
  199. char *bcc_ptr = *pbcc_area;
  200. cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
  201. len = strnlen(bcc_ptr, bleft);
  202. if (len >= bleft)
  203. return;
  204. kfree(ses->serverOS);
  205. ses->serverOS = kzalloc(len + 1, GFP_KERNEL);
  206. if (ses->serverOS) {
  207. strncpy(ses->serverOS, bcc_ptr, len);
  208. if (strncmp(ses->serverOS, "OS/2", 4) == 0)
  209. cifs_dbg(FYI, "OS/2 server\n");
  210. }
  211. bcc_ptr += len + 1;
  212. bleft -= len + 1;
  213. len = strnlen(bcc_ptr, bleft);
  214. if (len >= bleft)
  215. return;
  216. kfree(ses->serverNOS);
  217. ses->serverNOS = kzalloc(len + 1, GFP_KERNEL);
  218. if (ses->serverNOS)
  219. strncpy(ses->serverNOS, bcc_ptr, len);
  220. bcc_ptr += len + 1;
  221. bleft -= len + 1;
  222. len = strnlen(bcc_ptr, bleft);
  223. if (len > bleft)
  224. return;
  225. /* No domain field in LANMAN case. Domain is
  226. returned by old servers in the SMB negprot response */
  227. /* BB For newer servers which do not support Unicode,
  228. but thus do return domain here we could add parsing
  229. for it later, but it is not very important */
  230. cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
  231. }
  232. int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
  233. struct cifs_ses *ses)
  234. {
  235. unsigned int tioffset; /* challenge message target info area */
  236. unsigned int tilen; /* challenge message target info area length */
  237. CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
  238. if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
  239. cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
  240. return -EINVAL;
  241. }
  242. if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
  243. cifs_dbg(VFS, "blob signature incorrect %s\n",
  244. pblob->Signature);
  245. return -EINVAL;
  246. }
  247. if (pblob->MessageType != NtLmChallenge) {
  248. cifs_dbg(VFS, "Incorrect message type %d\n",
  249. pblob->MessageType);
  250. return -EINVAL;
  251. }
  252. memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
  253. /* BB we could decode pblob->NegotiateFlags; some may be useful */
  254. /* In particular we can examine sign flags */
  255. /* BB spec says that if AvId field of MsvAvTimestamp is populated then
  256. we must set the MIC field of the AUTHENTICATE_MESSAGE */
  257. ses->ntlmssp->server_flags = le32_to_cpu(pblob->NegotiateFlags);
  258. tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
  259. tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
  260. if (tioffset > blob_len || tioffset + tilen > blob_len) {
  261. cifs_dbg(VFS, "tioffset + tilen too high %u + %u",
  262. tioffset, tilen);
  263. return -EINVAL;
  264. }
  265. if (tilen) {
  266. ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
  267. GFP_KERNEL);
  268. if (!ses->auth_key.response) {
  269. cifs_dbg(VFS, "Challenge target info alloc failure");
  270. return -ENOMEM;
  271. }
  272. ses->auth_key.len = tilen;
  273. }
  274. return 0;
  275. }
  276. /* BB Move to ntlmssp.c eventually */
  277. /* We do not malloc the blob, it is passed in pbuffer, because
  278. it is fixed size, and small, making this approach cleaner */
  279. void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,
  280. struct cifs_ses *ses)
  281. {
  282. NEGOTIATE_MESSAGE *sec_blob = (NEGOTIATE_MESSAGE *)pbuffer;
  283. __u32 flags;
  284. memset(pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
  285. memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
  286. sec_blob->MessageType = NtLmNegotiate;
  287. /* BB is NTLMV2 session security format easier to use here? */
  288. flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
  289. NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
  290. NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
  291. NTLMSSP_NEGOTIATE_SEAL;
  292. if (ses->server->sign)
  293. flags |= NTLMSSP_NEGOTIATE_SIGN;
  294. if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
  295. flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
  296. sec_blob->NegotiateFlags = cpu_to_le32(flags);
  297. sec_blob->WorkstationName.BufferOffset = 0;
  298. sec_blob->WorkstationName.Length = 0;
  299. sec_blob->WorkstationName.MaximumLength = 0;
  300. /* Domain name is sent on the Challenge not Negotiate NTLMSSP request */
  301. sec_blob->DomainName.BufferOffset = 0;
  302. sec_blob->DomainName.Length = 0;
  303. sec_blob->DomainName.MaximumLength = 0;
  304. }
  305. static int size_of_ntlmssp_blob(struct cifs_ses *ses)
  306. {
  307. int sz = sizeof(AUTHENTICATE_MESSAGE) + ses->auth_key.len
  308. - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
  309. if (ses->domainName)
  310. sz += 2 * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
  311. else
  312. sz += 2;
  313. if (ses->user_name)
  314. sz += 2 * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
  315. else
  316. sz += 2;
  317. return sz;
  318. }
  319. int build_ntlmssp_auth_blob(unsigned char **pbuffer,
  320. u16 *buflen,
  321. struct cifs_ses *ses,
  322. const struct nls_table *nls_cp)
  323. {
  324. int rc;
  325. AUTHENTICATE_MESSAGE *sec_blob;
  326. __u32 flags;
  327. unsigned char *tmp;
  328. rc = setup_ntlmv2_rsp(ses, nls_cp);
  329. if (rc) {
  330. cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
  331. *buflen = 0;
  332. goto setup_ntlmv2_ret;
  333. }
  334. *pbuffer = kmalloc(size_of_ntlmssp_blob(ses), GFP_KERNEL);
  335. sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
  336. memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
  337. sec_blob->MessageType = NtLmAuthenticate;
  338. flags = NTLMSSP_NEGOTIATE_56 |
  339. NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
  340. NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
  341. NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
  342. NTLMSSP_NEGOTIATE_SEAL;
  343. if (ses->server->sign)
  344. flags |= NTLMSSP_NEGOTIATE_SIGN;
  345. if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
  346. flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
  347. tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
  348. sec_blob->NegotiateFlags = cpu_to_le32(flags);
  349. sec_blob->LmChallengeResponse.BufferOffset =
  350. cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
  351. sec_blob->LmChallengeResponse.Length = 0;
  352. sec_blob->LmChallengeResponse.MaximumLength = 0;
  353. sec_blob->NtChallengeResponse.BufferOffset =
  354. cpu_to_le32(tmp - *pbuffer);
  355. if (ses->user_name != NULL) {
  356. memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  357. ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  358. tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
  359. sec_blob->NtChallengeResponse.Length =
  360. cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  361. sec_blob->NtChallengeResponse.MaximumLength =
  362. cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  363. } else {
  364. /*
  365. * don't send an NT Response for anonymous access
  366. */
  367. sec_blob->NtChallengeResponse.Length = 0;
  368. sec_blob->NtChallengeResponse.MaximumLength = 0;
  369. }
  370. if (ses->domainName == NULL) {
  371. sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  372. sec_blob->DomainName.Length = 0;
  373. sec_blob->DomainName.MaximumLength = 0;
  374. tmp += 2;
  375. } else {
  376. int len;
  377. len = cifs_strtoUTF16((__le16 *)tmp, ses->domainName,
  378. CIFS_MAX_DOMAINNAME_LEN, nls_cp);
  379. len *= 2; /* unicode is 2 bytes each */
  380. sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  381. sec_blob->DomainName.Length = cpu_to_le16(len);
  382. sec_blob->DomainName.MaximumLength = cpu_to_le16(len);
  383. tmp += len;
  384. }
  385. if (ses->user_name == NULL) {
  386. sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  387. sec_blob->UserName.Length = 0;
  388. sec_blob->UserName.MaximumLength = 0;
  389. tmp += 2;
  390. } else {
  391. int len;
  392. len = cifs_strtoUTF16((__le16 *)tmp, ses->user_name,
  393. CIFS_MAX_USERNAME_LEN, nls_cp);
  394. len *= 2; /* unicode is 2 bytes each */
  395. sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  396. sec_blob->UserName.Length = cpu_to_le16(len);
  397. sec_blob->UserName.MaximumLength = cpu_to_le16(len);
  398. tmp += len;
  399. }
  400. sec_blob->WorkstationName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  401. sec_blob->WorkstationName.Length = 0;
  402. sec_blob->WorkstationName.MaximumLength = 0;
  403. tmp += 2;
  404. if (((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) ||
  405. (ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
  406. && !calc_seckey(ses)) {
  407. memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
  408. sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  409. sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
  410. sec_blob->SessionKey.MaximumLength =
  411. cpu_to_le16(CIFS_CPHTXT_SIZE);
  412. tmp += CIFS_CPHTXT_SIZE;
  413. } else {
  414. sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  415. sec_blob->SessionKey.Length = 0;
  416. sec_blob->SessionKey.MaximumLength = 0;
  417. }
  418. *buflen = tmp - *pbuffer;
  419. setup_ntlmv2_ret:
  420. return rc;
  421. }
  422. enum securityEnum
  423. cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
  424. {
  425. switch (server->negflavor) {
  426. case CIFS_NEGFLAVOR_EXTENDED:
  427. switch (requested) {
  428. case Kerberos:
  429. case RawNTLMSSP:
  430. return requested;
  431. case Unspecified:
  432. if (server->sec_ntlmssp &&
  433. (global_secflags & CIFSSEC_MAY_NTLMSSP))
  434. return RawNTLMSSP;
  435. if ((server->sec_kerberos || server->sec_mskerberos) &&
  436. (global_secflags & CIFSSEC_MAY_KRB5))
  437. return Kerberos;
  438. /* Fallthrough */
  439. default:
  440. return Unspecified;
  441. }
  442. case CIFS_NEGFLAVOR_UNENCAP:
  443. switch (requested) {
  444. case NTLM:
  445. case NTLMv2:
  446. return requested;
  447. case Unspecified:
  448. if (global_secflags & CIFSSEC_MAY_NTLMV2)
  449. return NTLMv2;
  450. if (global_secflags & CIFSSEC_MAY_NTLM)
  451. return NTLM;
  452. default:
  453. /* Fallthrough to attempt LANMAN authentication next */
  454. break;
  455. }
  456. case CIFS_NEGFLAVOR_LANMAN:
  457. switch (requested) {
  458. case LANMAN:
  459. return requested;
  460. case Unspecified:
  461. if (global_secflags & CIFSSEC_MAY_LANMAN)
  462. return LANMAN;
  463. /* Fallthrough */
  464. default:
  465. return Unspecified;
  466. }
  467. default:
  468. return Unspecified;
  469. }
  470. }
  471. struct sess_data {
  472. unsigned int xid;
  473. struct cifs_ses *ses;
  474. struct nls_table *nls_cp;
  475. void (*func)(struct sess_data *);
  476. int result;
  477. /* we will send the SMB in three pieces:
  478. * a fixed length beginning part, an optional
  479. * SPNEGO blob (which can be zero length), and a
  480. * last part which will include the strings
  481. * and rest of bcc area. This allows us to avoid
  482. * a large buffer 17K allocation
  483. */
  484. int buf0_type;
  485. struct kvec iov[3];
  486. };
  487. static int
  488. sess_alloc_buffer(struct sess_data *sess_data, int wct)
  489. {
  490. int rc;
  491. struct cifs_ses *ses = sess_data->ses;
  492. struct smb_hdr *smb_buf;
  493. rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
  494. (void **)&smb_buf);
  495. if (rc)
  496. return rc;
  497. sess_data->iov[0].iov_base = (char *)smb_buf;
  498. sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
  499. /*
  500. * This variable will be used to clear the buffer
  501. * allocated above in case of any error in the calling function.
  502. */
  503. sess_data->buf0_type = CIFS_SMALL_BUFFER;
  504. /* 2000 big enough to fit max user, domain, NOS name etc. */
  505. sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
  506. if (!sess_data->iov[2].iov_base) {
  507. rc = -ENOMEM;
  508. goto out_free_smb_buf;
  509. }
  510. return 0;
  511. out_free_smb_buf:
  512. kfree(smb_buf);
  513. sess_data->iov[0].iov_base = NULL;
  514. sess_data->iov[0].iov_len = 0;
  515. sess_data->buf0_type = CIFS_NO_BUFFER;
  516. return rc;
  517. }
  518. static void
  519. sess_free_buffer(struct sess_data *sess_data)
  520. {
  521. free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
  522. sess_data->buf0_type = CIFS_NO_BUFFER;
  523. kfree(sess_data->iov[2].iov_base);
  524. }
  525. static int
  526. sess_establish_session(struct sess_data *sess_data)
  527. {
  528. struct cifs_ses *ses = sess_data->ses;
  529. mutex_lock(&ses->server->srv_mutex);
  530. if (!ses->server->session_estab) {
  531. if (ses->server->sign) {
  532. ses->server->session_key.response =
  533. kmemdup(ses->auth_key.response,
  534. ses->auth_key.len, GFP_KERNEL);
  535. if (!ses->server->session_key.response) {
  536. mutex_unlock(&ses->server->srv_mutex);
  537. return -ENOMEM;
  538. }
  539. ses->server->session_key.len =
  540. ses->auth_key.len;
  541. }
  542. ses->server->sequence_number = 0x2;
  543. ses->server->session_estab = true;
  544. }
  545. mutex_unlock(&ses->server->srv_mutex);
  546. cifs_dbg(FYI, "CIFS session established successfully\n");
  547. spin_lock(&GlobalMid_Lock);
  548. ses->status = CifsGood;
  549. ses->need_reconnect = false;
  550. spin_unlock(&GlobalMid_Lock);
  551. return 0;
  552. }
  553. static int
  554. sess_sendreceive(struct sess_data *sess_data)
  555. {
  556. int rc;
  557. struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
  558. __u16 count;
  559. struct kvec rsp_iov = { NULL, 0 };
  560. count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
  561. smb_buf->smb_buf_length =
  562. cpu_to_be32(be32_to_cpu(smb_buf->smb_buf_length) + count);
  563. put_bcc(count, smb_buf);
  564. rc = SendReceive2(sess_data->xid, sess_data->ses,
  565. sess_data->iov, 3 /* num_iovecs */,
  566. &sess_data->buf0_type,
  567. CIFS_LOG_ERROR, &rsp_iov);
  568. cifs_small_buf_release(sess_data->iov[0].iov_base);
  569. memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
  570. return rc;
  571. }
  572. /*
  573. * LANMAN and plaintext are less secure and off by default.
  574. * So we make this explicitly be turned on in kconfig (in the
  575. * build) and turned on at runtime (changed from the default)
  576. * in proc/fs/cifs or via mount parm. Unfortunately this is
  577. * needed for old Win (e.g. Win95), some obscure NAS and OS/2
  578. */
  579. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  580. static void
  581. sess_auth_lanman(struct sess_data *sess_data)
  582. {
  583. int rc = 0;
  584. struct smb_hdr *smb_buf;
  585. SESSION_SETUP_ANDX *pSMB;
  586. char *bcc_ptr;
  587. struct cifs_ses *ses = sess_data->ses;
  588. char lnm_session_key[CIFS_AUTH_RESP_SIZE];
  589. __u32 capabilities;
  590. __u16 bytes_remaining;
  591. /* lanman 2 style sessionsetup */
  592. /* wct = 10 */
  593. rc = sess_alloc_buffer(sess_data, 10);
  594. if (rc)
  595. goto out;
  596. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  597. bcc_ptr = sess_data->iov[2].iov_base;
  598. capabilities = cifs_ssetup_hdr(ses, pSMB);
  599. pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
  600. if (ses->user_name != NULL) {
  601. /* no capabilities flags in old lanman negotiation */
  602. pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  603. /* Calculate hash with password and copy into bcc_ptr.
  604. * Encryption Key (stored as in cryptkey) gets used if the
  605. * security mode bit in Negottiate Protocol response states
  606. * to use challenge/response method (i.e. Password bit is 1).
  607. */
  608. rc = calc_lanman_hash(ses->password, ses->server->cryptkey,
  609. ses->server->sec_mode & SECMODE_PW_ENCRYPT ?
  610. true : false, lnm_session_key);
  611. if (rc)
  612. goto out;
  613. memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_AUTH_RESP_SIZE);
  614. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  615. } else {
  616. pSMB->old_req.PasswordLength = 0;
  617. }
  618. /*
  619. * can not sign if LANMAN negotiated so no need
  620. * to calculate signing key? but what if server
  621. * changed to do higher than lanman dialect and
  622. * we reconnected would we ever calc signing_key?
  623. */
  624. cifs_dbg(FYI, "Negotiating LANMAN setting up strings\n");
  625. /* Unicode not allowed for LANMAN dialects */
  626. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  627. sess_data->iov[2].iov_len = (long) bcc_ptr -
  628. (long) sess_data->iov[2].iov_base;
  629. rc = sess_sendreceive(sess_data);
  630. if (rc)
  631. goto out;
  632. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  633. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  634. /* lanman response has a word count of 3 */
  635. if (smb_buf->WordCount != 3) {
  636. rc = -EIO;
  637. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  638. goto out;
  639. }
  640. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  641. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  642. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  643. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  644. bytes_remaining = get_bcc(smb_buf);
  645. bcc_ptr = pByteArea(smb_buf);
  646. /* BB check if Unicode and decode strings */
  647. if (bytes_remaining == 0) {
  648. /* no string area to decode, do nothing */
  649. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  650. /* unicode string area must be word-aligned */
  651. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  652. ++bcc_ptr;
  653. --bytes_remaining;
  654. }
  655. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  656. sess_data->nls_cp);
  657. } else {
  658. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  659. sess_data->nls_cp);
  660. }
  661. rc = sess_establish_session(sess_data);
  662. out:
  663. sess_data->result = rc;
  664. sess_data->func = NULL;
  665. sess_free_buffer(sess_data);
  666. }
  667. #endif
  668. static void
  669. sess_auth_ntlm(struct sess_data *sess_data)
  670. {
  671. int rc = 0;
  672. struct smb_hdr *smb_buf;
  673. SESSION_SETUP_ANDX *pSMB;
  674. char *bcc_ptr;
  675. struct cifs_ses *ses = sess_data->ses;
  676. __u32 capabilities;
  677. __u16 bytes_remaining;
  678. /* old style NTLM sessionsetup */
  679. /* wct = 13 */
  680. rc = sess_alloc_buffer(sess_data, 13);
  681. if (rc)
  682. goto out;
  683. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  684. bcc_ptr = sess_data->iov[2].iov_base;
  685. capabilities = cifs_ssetup_hdr(ses, pSMB);
  686. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  687. if (ses->user_name != NULL) {
  688. pSMB->req_no_secext.CaseInsensitivePasswordLength =
  689. cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  690. pSMB->req_no_secext.CaseSensitivePasswordLength =
  691. cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  692. /* calculate ntlm response and session key */
  693. rc = setup_ntlm_response(ses, sess_data->nls_cp);
  694. if (rc) {
  695. cifs_dbg(VFS, "Error %d during NTLM authentication\n",
  696. rc);
  697. goto out;
  698. }
  699. /* copy ntlm response */
  700. memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  701. CIFS_AUTH_RESP_SIZE);
  702. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  703. memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  704. CIFS_AUTH_RESP_SIZE);
  705. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  706. } else {
  707. pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
  708. pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
  709. }
  710. if (ses->capabilities & CAP_UNICODE) {
  711. /* unicode strings must be word aligned */
  712. if (sess_data->iov[0].iov_len % 2) {
  713. *bcc_ptr = 0;
  714. bcc_ptr++;
  715. }
  716. unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  717. } else {
  718. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  719. }
  720. sess_data->iov[2].iov_len = (long) bcc_ptr -
  721. (long) sess_data->iov[2].iov_base;
  722. rc = sess_sendreceive(sess_data);
  723. if (rc)
  724. goto out;
  725. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  726. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  727. if (smb_buf->WordCount != 3) {
  728. rc = -EIO;
  729. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  730. goto out;
  731. }
  732. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  733. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  734. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  735. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  736. bytes_remaining = get_bcc(smb_buf);
  737. bcc_ptr = pByteArea(smb_buf);
  738. /* BB check if Unicode and decode strings */
  739. if (bytes_remaining == 0) {
  740. /* no string area to decode, do nothing */
  741. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  742. /* unicode string area must be word-aligned */
  743. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  744. ++bcc_ptr;
  745. --bytes_remaining;
  746. }
  747. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  748. sess_data->nls_cp);
  749. } else {
  750. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  751. sess_data->nls_cp);
  752. }
  753. rc = sess_establish_session(sess_data);
  754. out:
  755. sess_data->result = rc;
  756. sess_data->func = NULL;
  757. sess_free_buffer(sess_data);
  758. kfree(ses->auth_key.response);
  759. ses->auth_key.response = NULL;
  760. }
  761. static void
  762. sess_auth_ntlmv2(struct sess_data *sess_data)
  763. {
  764. int rc = 0;
  765. struct smb_hdr *smb_buf;
  766. SESSION_SETUP_ANDX *pSMB;
  767. char *bcc_ptr;
  768. struct cifs_ses *ses = sess_data->ses;
  769. __u32 capabilities;
  770. __u16 bytes_remaining;
  771. /* old style NTLM sessionsetup */
  772. /* wct = 13 */
  773. rc = sess_alloc_buffer(sess_data, 13);
  774. if (rc)
  775. goto out;
  776. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  777. bcc_ptr = sess_data->iov[2].iov_base;
  778. capabilities = cifs_ssetup_hdr(ses, pSMB);
  779. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  780. /* LM2 password would be here if we supported it */
  781. pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
  782. if (ses->user_name != NULL) {
  783. /* calculate nlmv2 response and session key */
  784. rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
  785. if (rc) {
  786. cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
  787. goto out;
  788. }
  789. memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  790. ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  791. bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
  792. /* set case sensitive password length after tilen may get
  793. * assigned, tilen is 0 otherwise.
  794. */
  795. pSMB->req_no_secext.CaseSensitivePasswordLength =
  796. cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  797. } else {
  798. pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
  799. }
  800. if (ses->capabilities & CAP_UNICODE) {
  801. if (sess_data->iov[0].iov_len % 2) {
  802. *bcc_ptr = 0;
  803. bcc_ptr++;
  804. }
  805. unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  806. } else {
  807. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  808. }
  809. sess_data->iov[2].iov_len = (long) bcc_ptr -
  810. (long) sess_data->iov[2].iov_base;
  811. rc = sess_sendreceive(sess_data);
  812. if (rc)
  813. goto out;
  814. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  815. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  816. if (smb_buf->WordCount != 3) {
  817. rc = -EIO;
  818. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  819. goto out;
  820. }
  821. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  822. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  823. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  824. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  825. bytes_remaining = get_bcc(smb_buf);
  826. bcc_ptr = pByteArea(smb_buf);
  827. /* BB check if Unicode and decode strings */
  828. if (bytes_remaining == 0) {
  829. /* no string area to decode, do nothing */
  830. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  831. /* unicode string area must be word-aligned */
  832. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  833. ++bcc_ptr;
  834. --bytes_remaining;
  835. }
  836. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  837. sess_data->nls_cp);
  838. } else {
  839. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  840. sess_data->nls_cp);
  841. }
  842. rc = sess_establish_session(sess_data);
  843. out:
  844. sess_data->result = rc;
  845. sess_data->func = NULL;
  846. sess_free_buffer(sess_data);
  847. kfree(ses->auth_key.response);
  848. ses->auth_key.response = NULL;
  849. }
  850. #ifdef CONFIG_CIFS_UPCALL
  851. static void
  852. sess_auth_kerberos(struct sess_data *sess_data)
  853. {
  854. int rc = 0;
  855. struct smb_hdr *smb_buf;
  856. SESSION_SETUP_ANDX *pSMB;
  857. char *bcc_ptr;
  858. struct cifs_ses *ses = sess_data->ses;
  859. __u32 capabilities;
  860. __u16 bytes_remaining;
  861. struct key *spnego_key = NULL;
  862. struct cifs_spnego_msg *msg;
  863. u16 blob_len;
  864. /* extended security */
  865. /* wct = 12 */
  866. rc = sess_alloc_buffer(sess_data, 12);
  867. if (rc)
  868. goto out;
  869. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  870. bcc_ptr = sess_data->iov[2].iov_base;
  871. capabilities = cifs_ssetup_hdr(ses, pSMB);
  872. spnego_key = cifs_get_spnego_key(ses);
  873. if (IS_ERR(spnego_key)) {
  874. rc = PTR_ERR(spnego_key);
  875. spnego_key = NULL;
  876. goto out;
  877. }
  878. msg = spnego_key->payload.data[0];
  879. /*
  880. * check version field to make sure that cifs.upcall is
  881. * sending us a response in an expected form
  882. */
  883. if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
  884. cifs_dbg(VFS,
  885. "incorrect version of cifs.upcall (expected %d but got %d)",
  886. CIFS_SPNEGO_UPCALL_VERSION, msg->version);
  887. rc = -EKEYREJECTED;
  888. goto out_put_spnego_key;
  889. }
  890. ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
  891. GFP_KERNEL);
  892. if (!ses->auth_key.response) {
  893. cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory",
  894. msg->sesskey_len);
  895. rc = -ENOMEM;
  896. goto out_put_spnego_key;
  897. }
  898. ses->auth_key.len = msg->sesskey_len;
  899. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  900. capabilities |= CAP_EXTENDED_SECURITY;
  901. pSMB->req.Capabilities = cpu_to_le32(capabilities);
  902. sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
  903. sess_data->iov[1].iov_len = msg->secblob_len;
  904. pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
  905. if (ses->capabilities & CAP_UNICODE) {
  906. /* unicode strings must be word aligned */
  907. if ((sess_data->iov[0].iov_len
  908. + sess_data->iov[1].iov_len) % 2) {
  909. *bcc_ptr = 0;
  910. bcc_ptr++;
  911. }
  912. unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
  913. unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
  914. } else {
  915. /* BB: is this right? */
  916. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  917. }
  918. sess_data->iov[2].iov_len = (long) bcc_ptr -
  919. (long) sess_data->iov[2].iov_base;
  920. rc = sess_sendreceive(sess_data);
  921. if (rc)
  922. goto out_put_spnego_key;
  923. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  924. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  925. if (smb_buf->WordCount != 4) {
  926. rc = -EIO;
  927. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  928. goto out_put_spnego_key;
  929. }
  930. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  931. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  932. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  933. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  934. bytes_remaining = get_bcc(smb_buf);
  935. bcc_ptr = pByteArea(smb_buf);
  936. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  937. if (blob_len > bytes_remaining) {
  938. cifs_dbg(VFS, "bad security blob length %d\n",
  939. blob_len);
  940. rc = -EINVAL;
  941. goto out_put_spnego_key;
  942. }
  943. bcc_ptr += blob_len;
  944. bytes_remaining -= blob_len;
  945. /* BB check if Unicode and decode strings */
  946. if (bytes_remaining == 0) {
  947. /* no string area to decode, do nothing */
  948. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  949. /* unicode string area must be word-aligned */
  950. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  951. ++bcc_ptr;
  952. --bytes_remaining;
  953. }
  954. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  955. sess_data->nls_cp);
  956. } else {
  957. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  958. sess_data->nls_cp);
  959. }
  960. rc = sess_establish_session(sess_data);
  961. out_put_spnego_key:
  962. key_invalidate(spnego_key);
  963. key_put(spnego_key);
  964. out:
  965. sess_data->result = rc;
  966. sess_data->func = NULL;
  967. sess_free_buffer(sess_data);
  968. kfree(ses->auth_key.response);
  969. ses->auth_key.response = NULL;
  970. }
  971. #endif /* ! CONFIG_CIFS_UPCALL */
  972. /*
  973. * The required kvec buffers have to be allocated before calling this
  974. * function.
  975. */
  976. static int
  977. _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
  978. {
  979. struct smb_hdr *smb_buf;
  980. SESSION_SETUP_ANDX *pSMB;
  981. struct cifs_ses *ses = sess_data->ses;
  982. __u32 capabilities;
  983. char *bcc_ptr;
  984. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  985. smb_buf = (struct smb_hdr *)pSMB;
  986. capabilities = cifs_ssetup_hdr(ses, pSMB);
  987. if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
  988. cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
  989. return -ENOSYS;
  990. }
  991. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  992. capabilities |= CAP_EXTENDED_SECURITY;
  993. pSMB->req.Capabilities |= cpu_to_le32(capabilities);
  994. bcc_ptr = sess_data->iov[2].iov_base;
  995. /* unicode strings must be word aligned */
  996. if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
  997. *bcc_ptr = 0;
  998. bcc_ptr++;
  999. }
  1000. unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
  1001. sess_data->iov[2].iov_len = (long) bcc_ptr -
  1002. (long) sess_data->iov[2].iov_base;
  1003. return 0;
  1004. }
  1005. static void
  1006. sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
  1007. static void
  1008. sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
  1009. {
  1010. int rc;
  1011. struct smb_hdr *smb_buf;
  1012. SESSION_SETUP_ANDX *pSMB;
  1013. struct cifs_ses *ses = sess_data->ses;
  1014. __u16 bytes_remaining;
  1015. char *bcc_ptr;
  1016. u16 blob_len;
  1017. cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
  1018. /*
  1019. * if memory allocation is successful, caller of this function
  1020. * frees it.
  1021. */
  1022. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  1023. if (!ses->ntlmssp) {
  1024. rc = -ENOMEM;
  1025. goto out;
  1026. }
  1027. ses->ntlmssp->sesskey_per_smbsess = false;
  1028. /* wct = 12 */
  1029. rc = sess_alloc_buffer(sess_data, 12);
  1030. if (rc)
  1031. goto out;
  1032. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1033. /* Build security blob before we assemble the request */
  1034. build_ntlmssp_negotiate_blob(pSMB->req.SecurityBlob, ses);
  1035. sess_data->iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
  1036. sess_data->iov[1].iov_base = pSMB->req.SecurityBlob;
  1037. pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
  1038. rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
  1039. if (rc)
  1040. goto out;
  1041. rc = sess_sendreceive(sess_data);
  1042. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1043. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  1044. /* If true, rc here is expected and not an error */
  1045. if (sess_data->buf0_type != CIFS_NO_BUFFER &&
  1046. smb_buf->Status.CifsError ==
  1047. cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
  1048. rc = 0;
  1049. if (rc)
  1050. goto out;
  1051. cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
  1052. if (smb_buf->WordCount != 4) {
  1053. rc = -EIO;
  1054. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  1055. goto out;
  1056. }
  1057. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  1058. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  1059. bytes_remaining = get_bcc(smb_buf);
  1060. bcc_ptr = pByteArea(smb_buf);
  1061. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  1062. if (blob_len > bytes_remaining) {
  1063. cifs_dbg(VFS, "bad security blob length %d\n",
  1064. blob_len);
  1065. rc = -EINVAL;
  1066. goto out;
  1067. }
  1068. rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
  1069. out:
  1070. sess_free_buffer(sess_data);
  1071. if (!rc) {
  1072. sess_data->func = sess_auth_rawntlmssp_authenticate;
  1073. return;
  1074. }
  1075. /* Else error. Cleanup */
  1076. kfree(ses->auth_key.response);
  1077. ses->auth_key.response = NULL;
  1078. kfree(ses->ntlmssp);
  1079. ses->ntlmssp = NULL;
  1080. sess_data->func = NULL;
  1081. sess_data->result = rc;
  1082. }
  1083. static void
  1084. sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
  1085. {
  1086. int rc;
  1087. struct smb_hdr *smb_buf;
  1088. SESSION_SETUP_ANDX *pSMB;
  1089. struct cifs_ses *ses = sess_data->ses;
  1090. __u16 bytes_remaining;
  1091. char *bcc_ptr;
  1092. unsigned char *ntlmsspblob = NULL;
  1093. u16 blob_len;
  1094. cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
  1095. /* wct = 12 */
  1096. rc = sess_alloc_buffer(sess_data, 12);
  1097. if (rc)
  1098. goto out;
  1099. /* Build security blob before we assemble the request */
  1100. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1101. smb_buf = (struct smb_hdr *)pSMB;
  1102. rc = build_ntlmssp_auth_blob(&ntlmsspblob,
  1103. &blob_len, ses, sess_data->nls_cp);
  1104. if (rc)
  1105. goto out_free_ntlmsspblob;
  1106. sess_data->iov[1].iov_len = blob_len;
  1107. sess_data->iov[1].iov_base = ntlmsspblob;
  1108. pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
  1109. /*
  1110. * Make sure that we tell the server that we are using
  1111. * the uid that it just gave us back on the response
  1112. * (challenge)
  1113. */
  1114. smb_buf->Uid = ses->Suid;
  1115. rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
  1116. if (rc)
  1117. goto out_free_ntlmsspblob;
  1118. rc = sess_sendreceive(sess_data);
  1119. if (rc)
  1120. goto out_free_ntlmsspblob;
  1121. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1122. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  1123. if (smb_buf->WordCount != 4) {
  1124. rc = -EIO;
  1125. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  1126. goto out_free_ntlmsspblob;
  1127. }
  1128. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  1129. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  1130. if (ses->Suid != smb_buf->Uid) {
  1131. ses->Suid = smb_buf->Uid;
  1132. cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
  1133. }
  1134. bytes_remaining = get_bcc(smb_buf);
  1135. bcc_ptr = pByteArea(smb_buf);
  1136. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  1137. if (blob_len > bytes_remaining) {
  1138. cifs_dbg(VFS, "bad security blob length %d\n",
  1139. blob_len);
  1140. rc = -EINVAL;
  1141. goto out_free_ntlmsspblob;
  1142. }
  1143. bcc_ptr += blob_len;
  1144. bytes_remaining -= blob_len;
  1145. /* BB check if Unicode and decode strings */
  1146. if (bytes_remaining == 0) {
  1147. /* no string area to decode, do nothing */
  1148. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  1149. /* unicode string area must be word-aligned */
  1150. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  1151. ++bcc_ptr;
  1152. --bytes_remaining;
  1153. }
  1154. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  1155. sess_data->nls_cp);
  1156. } else {
  1157. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  1158. sess_data->nls_cp);
  1159. }
  1160. out_free_ntlmsspblob:
  1161. kfree(ntlmsspblob);
  1162. out:
  1163. sess_free_buffer(sess_data);
  1164. if (!rc)
  1165. rc = sess_establish_session(sess_data);
  1166. /* Cleanup */
  1167. kfree(ses->auth_key.response);
  1168. ses->auth_key.response = NULL;
  1169. kfree(ses->ntlmssp);
  1170. ses->ntlmssp = NULL;
  1171. sess_data->func = NULL;
  1172. sess_data->result = rc;
  1173. }
  1174. static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
  1175. {
  1176. int type;
  1177. type = cifs_select_sectype(ses->server, ses->sectype);
  1178. cifs_dbg(FYI, "sess setup type %d\n", type);
  1179. if (type == Unspecified) {
  1180. cifs_dbg(VFS,
  1181. "Unable to select appropriate authentication method!");
  1182. return -EINVAL;
  1183. }
  1184. switch (type) {
  1185. case LANMAN:
  1186. /* LANMAN and plaintext are less secure and off by default.
  1187. * So we make this explicitly be turned on in kconfig (in the
  1188. * build) and turned on at runtime (changed from the default)
  1189. * in proc/fs/cifs or via mount parm. Unfortunately this is
  1190. * needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
  1191. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  1192. sess_data->func = sess_auth_lanman;
  1193. break;
  1194. #else
  1195. return -EOPNOTSUPP;
  1196. #endif
  1197. case NTLM:
  1198. sess_data->func = sess_auth_ntlm;
  1199. break;
  1200. case NTLMv2:
  1201. sess_data->func = sess_auth_ntlmv2;
  1202. break;
  1203. case Kerberos:
  1204. #ifdef CONFIG_CIFS_UPCALL
  1205. sess_data->func = sess_auth_kerberos;
  1206. break;
  1207. #else
  1208. cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
  1209. return -ENOSYS;
  1210. break;
  1211. #endif /* CONFIG_CIFS_UPCALL */
  1212. case RawNTLMSSP:
  1213. sess_data->func = sess_auth_rawntlmssp_negotiate;
  1214. break;
  1215. default:
  1216. cifs_dbg(VFS, "secType %d not supported!\n", type);
  1217. return -ENOSYS;
  1218. }
  1219. return 0;
  1220. }
  1221. int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
  1222. const struct nls_table *nls_cp)
  1223. {
  1224. int rc = 0;
  1225. struct sess_data *sess_data;
  1226. if (ses == NULL) {
  1227. WARN(1, "%s: ses == NULL!", __func__);
  1228. return -EINVAL;
  1229. }
  1230. sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
  1231. if (!sess_data)
  1232. return -ENOMEM;
  1233. rc = select_sec(ses, sess_data);
  1234. if (rc)
  1235. goto out;
  1236. sess_data->xid = xid;
  1237. sess_data->ses = ses;
  1238. sess_data->buf0_type = CIFS_NO_BUFFER;
  1239. sess_data->nls_cp = (struct nls_table *) nls_cp;
  1240. while (sess_data->func)
  1241. sess_data->func(sess_data);
  1242. /* Store result before we free sess_data */
  1243. rc = sess_data->result;
  1244. out:
  1245. kfree(sess_data);
  1246. return rc;
  1247. }