sess.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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 ([email protected])
  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. if (!*pbuffer) {
  336. rc = -ENOMEM;
  337. cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
  338. *buflen = 0;
  339. goto setup_ntlmv2_ret;
  340. }
  341. sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
  342. memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
  343. sec_blob->MessageType = NtLmAuthenticate;
  344. flags = NTLMSSP_NEGOTIATE_56 |
  345. NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
  346. NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
  347. NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
  348. NTLMSSP_NEGOTIATE_SEAL;
  349. if (ses->server->sign)
  350. flags |= NTLMSSP_NEGOTIATE_SIGN;
  351. if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
  352. flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
  353. tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
  354. sec_blob->NegotiateFlags = cpu_to_le32(flags);
  355. sec_blob->LmChallengeResponse.BufferOffset =
  356. cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
  357. sec_blob->LmChallengeResponse.Length = 0;
  358. sec_blob->LmChallengeResponse.MaximumLength = 0;
  359. sec_blob->NtChallengeResponse.BufferOffset =
  360. cpu_to_le32(tmp - *pbuffer);
  361. if (ses->user_name != NULL) {
  362. memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  363. ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  364. tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
  365. sec_blob->NtChallengeResponse.Length =
  366. cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  367. sec_blob->NtChallengeResponse.MaximumLength =
  368. cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  369. } else {
  370. /*
  371. * don't send an NT Response for anonymous access
  372. */
  373. sec_blob->NtChallengeResponse.Length = 0;
  374. sec_blob->NtChallengeResponse.MaximumLength = 0;
  375. }
  376. if (ses->domainName == NULL) {
  377. sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  378. sec_blob->DomainName.Length = 0;
  379. sec_blob->DomainName.MaximumLength = 0;
  380. tmp += 2;
  381. } else {
  382. int len;
  383. len = cifs_strtoUTF16((__le16 *)tmp, ses->domainName,
  384. CIFS_MAX_DOMAINNAME_LEN, nls_cp);
  385. len *= 2; /* unicode is 2 bytes each */
  386. sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  387. sec_blob->DomainName.Length = cpu_to_le16(len);
  388. sec_blob->DomainName.MaximumLength = cpu_to_le16(len);
  389. tmp += len;
  390. }
  391. if (ses->user_name == NULL) {
  392. sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  393. sec_blob->UserName.Length = 0;
  394. sec_blob->UserName.MaximumLength = 0;
  395. tmp += 2;
  396. } else {
  397. int len;
  398. len = cifs_strtoUTF16((__le16 *)tmp, ses->user_name,
  399. CIFS_MAX_USERNAME_LEN, nls_cp);
  400. len *= 2; /* unicode is 2 bytes each */
  401. sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  402. sec_blob->UserName.Length = cpu_to_le16(len);
  403. sec_blob->UserName.MaximumLength = cpu_to_le16(len);
  404. tmp += len;
  405. }
  406. sec_blob->WorkstationName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  407. sec_blob->WorkstationName.Length = 0;
  408. sec_blob->WorkstationName.MaximumLength = 0;
  409. tmp += 2;
  410. if (((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) ||
  411. (ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
  412. && !calc_seckey(ses)) {
  413. memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
  414. sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  415. sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
  416. sec_blob->SessionKey.MaximumLength =
  417. cpu_to_le16(CIFS_CPHTXT_SIZE);
  418. tmp += CIFS_CPHTXT_SIZE;
  419. } else {
  420. sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  421. sec_blob->SessionKey.Length = 0;
  422. sec_blob->SessionKey.MaximumLength = 0;
  423. }
  424. *buflen = tmp - *pbuffer;
  425. setup_ntlmv2_ret:
  426. return rc;
  427. }
  428. enum securityEnum
  429. select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
  430. {
  431. switch (server->negflavor) {
  432. case CIFS_NEGFLAVOR_EXTENDED:
  433. switch (requested) {
  434. case Kerberos:
  435. case RawNTLMSSP:
  436. return requested;
  437. case Unspecified:
  438. if (server->sec_ntlmssp &&
  439. (global_secflags & CIFSSEC_MAY_NTLMSSP))
  440. return RawNTLMSSP;
  441. if ((server->sec_kerberos || server->sec_mskerberos) &&
  442. (global_secflags & CIFSSEC_MAY_KRB5))
  443. return Kerberos;
  444. /* Fallthrough */
  445. default:
  446. return Unspecified;
  447. }
  448. case CIFS_NEGFLAVOR_UNENCAP:
  449. switch (requested) {
  450. case NTLM:
  451. case NTLMv2:
  452. return requested;
  453. case Unspecified:
  454. if (global_secflags & CIFSSEC_MAY_NTLMV2)
  455. return NTLMv2;
  456. if (global_secflags & CIFSSEC_MAY_NTLM)
  457. return NTLM;
  458. default:
  459. /* Fallthrough to attempt LANMAN authentication next */
  460. break;
  461. }
  462. case CIFS_NEGFLAVOR_LANMAN:
  463. switch (requested) {
  464. case LANMAN:
  465. return requested;
  466. case Unspecified:
  467. if (global_secflags & CIFSSEC_MAY_LANMAN)
  468. return LANMAN;
  469. /* Fallthrough */
  470. default:
  471. return Unspecified;
  472. }
  473. default:
  474. return Unspecified;
  475. }
  476. }
  477. struct sess_data {
  478. unsigned int xid;
  479. struct cifs_ses *ses;
  480. struct nls_table *nls_cp;
  481. void (*func)(struct sess_data *);
  482. int result;
  483. /* we will send the SMB in three pieces:
  484. * a fixed length beginning part, an optional
  485. * SPNEGO blob (which can be zero length), and a
  486. * last part which will include the strings
  487. * and rest of bcc area. This allows us to avoid
  488. * a large buffer 17K allocation
  489. */
  490. int buf0_type;
  491. struct kvec iov[3];
  492. };
  493. static int
  494. sess_alloc_buffer(struct sess_data *sess_data, int wct)
  495. {
  496. int rc;
  497. struct cifs_ses *ses = sess_data->ses;
  498. struct smb_hdr *smb_buf;
  499. rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
  500. (void **)&smb_buf);
  501. if (rc)
  502. return rc;
  503. sess_data->iov[0].iov_base = (char *)smb_buf;
  504. sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
  505. /*
  506. * This variable will be used to clear the buffer
  507. * allocated above in case of any error in the calling function.
  508. */
  509. sess_data->buf0_type = CIFS_SMALL_BUFFER;
  510. /* 2000 big enough to fit max user, domain, NOS name etc. */
  511. sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
  512. if (!sess_data->iov[2].iov_base) {
  513. rc = -ENOMEM;
  514. goto out_free_smb_buf;
  515. }
  516. return 0;
  517. out_free_smb_buf:
  518. kfree(smb_buf);
  519. sess_data->iov[0].iov_base = NULL;
  520. sess_data->iov[0].iov_len = 0;
  521. sess_data->buf0_type = CIFS_NO_BUFFER;
  522. return rc;
  523. }
  524. static void
  525. sess_free_buffer(struct sess_data *sess_data)
  526. {
  527. free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
  528. sess_data->buf0_type = CIFS_NO_BUFFER;
  529. kfree(sess_data->iov[2].iov_base);
  530. }
  531. static int
  532. sess_establish_session(struct sess_data *sess_data)
  533. {
  534. struct cifs_ses *ses = sess_data->ses;
  535. mutex_lock(&ses->server->srv_mutex);
  536. if (!ses->server->session_estab) {
  537. if (ses->server->sign) {
  538. ses->server->session_key.response =
  539. kmemdup(ses->auth_key.response,
  540. ses->auth_key.len, GFP_KERNEL);
  541. if (!ses->server->session_key.response) {
  542. mutex_unlock(&ses->server->srv_mutex);
  543. return -ENOMEM;
  544. }
  545. ses->server->session_key.len =
  546. ses->auth_key.len;
  547. }
  548. ses->server->sequence_number = 0x2;
  549. ses->server->session_estab = true;
  550. }
  551. mutex_unlock(&ses->server->srv_mutex);
  552. cifs_dbg(FYI, "CIFS session established successfully\n");
  553. spin_lock(&GlobalMid_Lock);
  554. ses->status = CifsGood;
  555. ses->need_reconnect = false;
  556. spin_unlock(&GlobalMid_Lock);
  557. return 0;
  558. }
  559. static int
  560. sess_sendreceive(struct sess_data *sess_data)
  561. {
  562. int rc;
  563. struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
  564. __u16 count;
  565. count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
  566. smb_buf->smb_buf_length =
  567. cpu_to_be32(be32_to_cpu(smb_buf->smb_buf_length) + count);
  568. put_bcc(count, smb_buf);
  569. rc = SendReceive2(sess_data->xid, sess_data->ses,
  570. sess_data->iov, 3 /* num_iovecs */,
  571. &sess_data->buf0_type,
  572. CIFS_LOG_ERROR);
  573. return rc;
  574. }
  575. /*
  576. * LANMAN and plaintext are less secure and off by default.
  577. * So we make this explicitly be turned on in kconfig (in the
  578. * build) and turned on at runtime (changed from the default)
  579. * in proc/fs/cifs or via mount parm. Unfortunately this is
  580. * needed for old Win (e.g. Win95), some obscure NAS and OS/2
  581. */
  582. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  583. static void
  584. sess_auth_lanman(struct sess_data *sess_data)
  585. {
  586. int rc = 0;
  587. struct smb_hdr *smb_buf;
  588. SESSION_SETUP_ANDX *pSMB;
  589. char *bcc_ptr;
  590. struct cifs_ses *ses = sess_data->ses;
  591. char lnm_session_key[CIFS_AUTH_RESP_SIZE];
  592. __u32 capabilities;
  593. __u16 bytes_remaining;
  594. /* lanman 2 style sessionsetup */
  595. /* wct = 10 */
  596. rc = sess_alloc_buffer(sess_data, 10);
  597. if (rc)
  598. goto out;
  599. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  600. bcc_ptr = sess_data->iov[2].iov_base;
  601. capabilities = cifs_ssetup_hdr(ses, pSMB);
  602. pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
  603. if (ses->user_name != NULL) {
  604. /* no capabilities flags in old lanman negotiation */
  605. pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  606. /* Calculate hash with password and copy into bcc_ptr.
  607. * Encryption Key (stored as in cryptkey) gets used if the
  608. * security mode bit in Negottiate Protocol response states
  609. * to use challenge/response method (i.e. Password bit is 1).
  610. */
  611. rc = calc_lanman_hash(ses->password, ses->server->cryptkey,
  612. ses->server->sec_mode & SECMODE_PW_ENCRYPT ?
  613. true : false, lnm_session_key);
  614. if (rc)
  615. goto out;
  616. memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_AUTH_RESP_SIZE);
  617. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  618. } else {
  619. pSMB->old_req.PasswordLength = 0;
  620. }
  621. /*
  622. * can not sign if LANMAN negotiated so no need
  623. * to calculate signing key? but what if server
  624. * changed to do higher than lanman dialect and
  625. * we reconnected would we ever calc signing_key?
  626. */
  627. cifs_dbg(FYI, "Negotiating LANMAN setting up strings\n");
  628. /* Unicode not allowed for LANMAN dialects */
  629. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  630. sess_data->iov[2].iov_len = (long) bcc_ptr -
  631. (long) sess_data->iov[2].iov_base;
  632. rc = sess_sendreceive(sess_data);
  633. if (rc)
  634. goto out;
  635. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  636. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  637. /* lanman response has a word count of 3 */
  638. if (smb_buf->WordCount != 3) {
  639. rc = -EIO;
  640. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  641. goto out;
  642. }
  643. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  644. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  645. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  646. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  647. bytes_remaining = get_bcc(smb_buf);
  648. bcc_ptr = pByteArea(smb_buf);
  649. /* BB check if Unicode and decode strings */
  650. if (bytes_remaining == 0) {
  651. /* no string area to decode, do nothing */
  652. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  653. /* unicode string area must be word-aligned */
  654. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  655. ++bcc_ptr;
  656. --bytes_remaining;
  657. }
  658. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  659. sess_data->nls_cp);
  660. } else {
  661. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  662. sess_data->nls_cp);
  663. }
  664. rc = sess_establish_session(sess_data);
  665. out:
  666. sess_data->result = rc;
  667. sess_data->func = NULL;
  668. sess_free_buffer(sess_data);
  669. }
  670. #endif
  671. static void
  672. sess_auth_ntlm(struct sess_data *sess_data)
  673. {
  674. int rc = 0;
  675. struct smb_hdr *smb_buf;
  676. SESSION_SETUP_ANDX *pSMB;
  677. char *bcc_ptr;
  678. struct cifs_ses *ses = sess_data->ses;
  679. __u32 capabilities;
  680. __u16 bytes_remaining;
  681. /* old style NTLM sessionsetup */
  682. /* wct = 13 */
  683. rc = sess_alloc_buffer(sess_data, 13);
  684. if (rc)
  685. goto out;
  686. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  687. bcc_ptr = sess_data->iov[2].iov_base;
  688. capabilities = cifs_ssetup_hdr(ses, pSMB);
  689. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  690. if (ses->user_name != NULL) {
  691. pSMB->req_no_secext.CaseInsensitivePasswordLength =
  692. cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  693. pSMB->req_no_secext.CaseSensitivePasswordLength =
  694. cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  695. /* calculate ntlm response and session key */
  696. rc = setup_ntlm_response(ses, sess_data->nls_cp);
  697. if (rc) {
  698. cifs_dbg(VFS, "Error %d during NTLM authentication\n",
  699. rc);
  700. goto out;
  701. }
  702. /* copy ntlm response */
  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. memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  707. CIFS_AUTH_RESP_SIZE);
  708. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  709. } else {
  710. pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
  711. pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
  712. }
  713. if (ses->capabilities & CAP_UNICODE) {
  714. /* unicode strings must be word aligned */
  715. if (sess_data->iov[0].iov_len % 2) {
  716. *bcc_ptr = 0;
  717. bcc_ptr++;
  718. }
  719. unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  720. } else {
  721. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  722. }
  723. sess_data->iov[2].iov_len = (long) bcc_ptr -
  724. (long) sess_data->iov[2].iov_base;
  725. rc = sess_sendreceive(sess_data);
  726. if (rc)
  727. goto out;
  728. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  729. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  730. if (smb_buf->WordCount != 3) {
  731. rc = -EIO;
  732. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  733. goto out;
  734. }
  735. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  736. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  737. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  738. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  739. bytes_remaining = get_bcc(smb_buf);
  740. bcc_ptr = pByteArea(smb_buf);
  741. /* BB check if Unicode and decode strings */
  742. if (bytes_remaining == 0) {
  743. /* no string area to decode, do nothing */
  744. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  745. /* unicode string area must be word-aligned */
  746. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  747. ++bcc_ptr;
  748. --bytes_remaining;
  749. }
  750. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  751. sess_data->nls_cp);
  752. } else {
  753. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  754. sess_data->nls_cp);
  755. }
  756. rc = sess_establish_session(sess_data);
  757. out:
  758. sess_data->result = rc;
  759. sess_data->func = NULL;
  760. sess_free_buffer(sess_data);
  761. kfree(ses->auth_key.response);
  762. ses->auth_key.response = NULL;
  763. }
  764. static void
  765. sess_auth_ntlmv2(struct sess_data *sess_data)
  766. {
  767. int rc = 0;
  768. struct smb_hdr *smb_buf;
  769. SESSION_SETUP_ANDX *pSMB;
  770. char *bcc_ptr;
  771. struct cifs_ses *ses = sess_data->ses;
  772. __u32 capabilities;
  773. __u16 bytes_remaining;
  774. /* old style NTLM sessionsetup */
  775. /* wct = 13 */
  776. rc = sess_alloc_buffer(sess_data, 13);
  777. if (rc)
  778. goto out;
  779. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  780. bcc_ptr = sess_data->iov[2].iov_base;
  781. capabilities = cifs_ssetup_hdr(ses, pSMB);
  782. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  783. /* LM2 password would be here if we supported it */
  784. pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
  785. if (ses->user_name != NULL) {
  786. /* calculate nlmv2 response and session key */
  787. rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
  788. if (rc) {
  789. cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
  790. goto out;
  791. }
  792. memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  793. ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  794. bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
  795. /* set case sensitive password length after tilen may get
  796. * assigned, tilen is 0 otherwise.
  797. */
  798. pSMB->req_no_secext.CaseSensitivePasswordLength =
  799. cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  800. } else {
  801. pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
  802. }
  803. if (ses->capabilities & CAP_UNICODE) {
  804. if (sess_data->iov[0].iov_len % 2) {
  805. *bcc_ptr = 0;
  806. bcc_ptr++;
  807. }
  808. unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  809. } else {
  810. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  811. }
  812. sess_data->iov[2].iov_len = (long) bcc_ptr -
  813. (long) sess_data->iov[2].iov_base;
  814. rc = sess_sendreceive(sess_data);
  815. if (rc)
  816. goto out;
  817. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  818. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  819. if (smb_buf->WordCount != 3) {
  820. rc = -EIO;
  821. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  822. goto out;
  823. }
  824. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  825. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  826. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  827. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  828. bytes_remaining = get_bcc(smb_buf);
  829. bcc_ptr = pByteArea(smb_buf);
  830. /* BB check if Unicode and decode strings */
  831. if (bytes_remaining == 0) {
  832. /* no string area to decode, do nothing */
  833. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  834. /* unicode string area must be word-aligned */
  835. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  836. ++bcc_ptr;
  837. --bytes_remaining;
  838. }
  839. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  840. sess_data->nls_cp);
  841. } else {
  842. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  843. sess_data->nls_cp);
  844. }
  845. rc = sess_establish_session(sess_data);
  846. out:
  847. sess_data->result = rc;
  848. sess_data->func = NULL;
  849. sess_free_buffer(sess_data);
  850. kfree(ses->auth_key.response);
  851. ses->auth_key.response = NULL;
  852. }
  853. #ifdef CONFIG_CIFS_UPCALL
  854. static void
  855. sess_auth_kerberos(struct sess_data *sess_data)
  856. {
  857. int rc = 0;
  858. struct smb_hdr *smb_buf;
  859. SESSION_SETUP_ANDX *pSMB;
  860. char *bcc_ptr;
  861. struct cifs_ses *ses = sess_data->ses;
  862. __u32 capabilities;
  863. __u16 bytes_remaining;
  864. struct key *spnego_key = NULL;
  865. struct cifs_spnego_msg *msg;
  866. u16 blob_len;
  867. /* extended security */
  868. /* wct = 12 */
  869. rc = sess_alloc_buffer(sess_data, 12);
  870. if (rc)
  871. goto out;
  872. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  873. bcc_ptr = sess_data->iov[2].iov_base;
  874. capabilities = cifs_ssetup_hdr(ses, pSMB);
  875. spnego_key = cifs_get_spnego_key(ses);
  876. if (IS_ERR(spnego_key)) {
  877. rc = PTR_ERR(spnego_key);
  878. spnego_key = NULL;
  879. goto out;
  880. }
  881. msg = spnego_key->payload.data[0];
  882. /*
  883. * check version field to make sure that cifs.upcall is
  884. * sending us a response in an expected form
  885. */
  886. if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
  887. cifs_dbg(VFS,
  888. "incorrect version of cifs.upcall (expected %d but got %d)",
  889. CIFS_SPNEGO_UPCALL_VERSION, msg->version);
  890. rc = -EKEYREJECTED;
  891. goto out_put_spnego_key;
  892. }
  893. ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
  894. GFP_KERNEL);
  895. if (!ses->auth_key.response) {
  896. cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory",
  897. msg->sesskey_len);
  898. rc = -ENOMEM;
  899. goto out_put_spnego_key;
  900. }
  901. ses->auth_key.len = msg->sesskey_len;
  902. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  903. capabilities |= CAP_EXTENDED_SECURITY;
  904. pSMB->req.Capabilities = cpu_to_le32(capabilities);
  905. sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
  906. sess_data->iov[1].iov_len = msg->secblob_len;
  907. pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
  908. if (ses->capabilities & CAP_UNICODE) {
  909. /* unicode strings must be word aligned */
  910. if ((sess_data->iov[0].iov_len
  911. + sess_data->iov[1].iov_len) % 2) {
  912. *bcc_ptr = 0;
  913. bcc_ptr++;
  914. }
  915. unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
  916. unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
  917. } else {
  918. /* BB: is this right? */
  919. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  920. }
  921. sess_data->iov[2].iov_len = (long) bcc_ptr -
  922. (long) sess_data->iov[2].iov_base;
  923. rc = sess_sendreceive(sess_data);
  924. if (rc)
  925. goto out_put_spnego_key;
  926. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  927. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  928. if (smb_buf->WordCount != 4) {
  929. rc = -EIO;
  930. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  931. goto out_put_spnego_key;
  932. }
  933. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  934. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  935. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  936. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  937. bytes_remaining = get_bcc(smb_buf);
  938. bcc_ptr = pByteArea(smb_buf);
  939. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  940. if (blob_len > bytes_remaining) {
  941. cifs_dbg(VFS, "bad security blob length %d\n",
  942. blob_len);
  943. rc = -EINVAL;
  944. goto out_put_spnego_key;
  945. }
  946. bcc_ptr += blob_len;
  947. bytes_remaining -= blob_len;
  948. /* BB check if Unicode and decode strings */
  949. if (bytes_remaining == 0) {
  950. /* no string area to decode, do nothing */
  951. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  952. /* unicode string area must be word-aligned */
  953. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  954. ++bcc_ptr;
  955. --bytes_remaining;
  956. }
  957. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  958. sess_data->nls_cp);
  959. } else {
  960. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  961. sess_data->nls_cp);
  962. }
  963. rc = sess_establish_session(sess_data);
  964. out_put_spnego_key:
  965. key_invalidate(spnego_key);
  966. key_put(spnego_key);
  967. out:
  968. sess_data->result = rc;
  969. sess_data->func = NULL;
  970. sess_free_buffer(sess_data);
  971. kfree(ses->auth_key.response);
  972. ses->auth_key.response = NULL;
  973. }
  974. #endif /* ! CONFIG_CIFS_UPCALL */
  975. /*
  976. * The required kvec buffers have to be allocated before calling this
  977. * function.
  978. */
  979. static int
  980. _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
  981. {
  982. struct smb_hdr *smb_buf;
  983. SESSION_SETUP_ANDX *pSMB;
  984. struct cifs_ses *ses = sess_data->ses;
  985. __u32 capabilities;
  986. char *bcc_ptr;
  987. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  988. smb_buf = (struct smb_hdr *)pSMB;
  989. capabilities = cifs_ssetup_hdr(ses, pSMB);
  990. if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
  991. cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
  992. return -ENOSYS;
  993. }
  994. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  995. capabilities |= CAP_EXTENDED_SECURITY;
  996. pSMB->req.Capabilities |= cpu_to_le32(capabilities);
  997. bcc_ptr = sess_data->iov[2].iov_base;
  998. /* unicode strings must be word aligned */
  999. if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
  1000. *bcc_ptr = 0;
  1001. bcc_ptr++;
  1002. }
  1003. unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
  1004. sess_data->iov[2].iov_len = (long) bcc_ptr -
  1005. (long) sess_data->iov[2].iov_base;
  1006. return 0;
  1007. }
  1008. static void
  1009. sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
  1010. static void
  1011. sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
  1012. {
  1013. int rc;
  1014. struct smb_hdr *smb_buf;
  1015. SESSION_SETUP_ANDX *pSMB;
  1016. struct cifs_ses *ses = sess_data->ses;
  1017. __u16 bytes_remaining;
  1018. char *bcc_ptr;
  1019. u16 blob_len;
  1020. cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
  1021. /*
  1022. * if memory allocation is successful, caller of this function
  1023. * frees it.
  1024. */
  1025. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  1026. if (!ses->ntlmssp) {
  1027. rc = -ENOMEM;
  1028. goto out;
  1029. }
  1030. ses->ntlmssp->sesskey_per_smbsess = false;
  1031. /* wct = 12 */
  1032. rc = sess_alloc_buffer(sess_data, 12);
  1033. if (rc)
  1034. goto out;
  1035. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1036. /* Build security blob before we assemble the request */
  1037. build_ntlmssp_negotiate_blob(pSMB->req.SecurityBlob, ses);
  1038. sess_data->iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
  1039. sess_data->iov[1].iov_base = pSMB->req.SecurityBlob;
  1040. pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
  1041. rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
  1042. if (rc)
  1043. goto out;
  1044. rc = sess_sendreceive(sess_data);
  1045. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1046. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  1047. /* If true, rc here is expected and not an error */
  1048. if (sess_data->buf0_type != CIFS_NO_BUFFER &&
  1049. smb_buf->Status.CifsError ==
  1050. cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
  1051. rc = 0;
  1052. if (rc)
  1053. goto out;
  1054. cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
  1055. if (smb_buf->WordCount != 4) {
  1056. rc = -EIO;
  1057. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  1058. goto out;
  1059. }
  1060. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  1061. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  1062. bytes_remaining = get_bcc(smb_buf);
  1063. bcc_ptr = pByteArea(smb_buf);
  1064. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  1065. if (blob_len > bytes_remaining) {
  1066. cifs_dbg(VFS, "bad security blob length %d\n",
  1067. blob_len);
  1068. rc = -EINVAL;
  1069. goto out;
  1070. }
  1071. rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
  1072. out:
  1073. sess_free_buffer(sess_data);
  1074. if (!rc) {
  1075. sess_data->func = sess_auth_rawntlmssp_authenticate;
  1076. return;
  1077. }
  1078. /* Else error. Cleanup */
  1079. kfree(ses->auth_key.response);
  1080. ses->auth_key.response = NULL;
  1081. kfree(ses->ntlmssp);
  1082. ses->ntlmssp = NULL;
  1083. sess_data->func = NULL;
  1084. sess_data->result = rc;
  1085. }
  1086. static void
  1087. sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
  1088. {
  1089. int rc;
  1090. struct smb_hdr *smb_buf;
  1091. SESSION_SETUP_ANDX *pSMB;
  1092. struct cifs_ses *ses = sess_data->ses;
  1093. __u16 bytes_remaining;
  1094. char *bcc_ptr;
  1095. unsigned char *ntlmsspblob = NULL;
  1096. u16 blob_len;
  1097. cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
  1098. /* wct = 12 */
  1099. rc = sess_alloc_buffer(sess_data, 12);
  1100. if (rc)
  1101. goto out;
  1102. /* Build security blob before we assemble the request */
  1103. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1104. smb_buf = (struct smb_hdr *)pSMB;
  1105. rc = build_ntlmssp_auth_blob(&ntlmsspblob,
  1106. &blob_len, ses, sess_data->nls_cp);
  1107. if (rc)
  1108. goto out_free_ntlmsspblob;
  1109. sess_data->iov[1].iov_len = blob_len;
  1110. sess_data->iov[1].iov_base = ntlmsspblob;
  1111. pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
  1112. /*
  1113. * Make sure that we tell the server that we are using
  1114. * the uid that it just gave us back on the response
  1115. * (challenge)
  1116. */
  1117. smb_buf->Uid = ses->Suid;
  1118. rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
  1119. if (rc)
  1120. goto out_free_ntlmsspblob;
  1121. rc = sess_sendreceive(sess_data);
  1122. if (rc)
  1123. goto out_free_ntlmsspblob;
  1124. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1125. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  1126. if (smb_buf->WordCount != 4) {
  1127. rc = -EIO;
  1128. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  1129. goto out_free_ntlmsspblob;
  1130. }
  1131. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  1132. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  1133. if (ses->Suid != smb_buf->Uid) {
  1134. ses->Suid = smb_buf->Uid;
  1135. cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
  1136. }
  1137. bytes_remaining = get_bcc(smb_buf);
  1138. bcc_ptr = pByteArea(smb_buf);
  1139. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  1140. if (blob_len > bytes_remaining) {
  1141. cifs_dbg(VFS, "bad security blob length %d\n",
  1142. blob_len);
  1143. rc = -EINVAL;
  1144. goto out_free_ntlmsspblob;
  1145. }
  1146. bcc_ptr += blob_len;
  1147. bytes_remaining -= blob_len;
  1148. /* BB check if Unicode and decode strings */
  1149. if (bytes_remaining == 0) {
  1150. /* no string area to decode, do nothing */
  1151. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  1152. /* unicode string area must be word-aligned */
  1153. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  1154. ++bcc_ptr;
  1155. --bytes_remaining;
  1156. }
  1157. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  1158. sess_data->nls_cp);
  1159. } else {
  1160. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  1161. sess_data->nls_cp);
  1162. }
  1163. out_free_ntlmsspblob:
  1164. kfree(ntlmsspblob);
  1165. out:
  1166. sess_free_buffer(sess_data);
  1167. if (!rc)
  1168. rc = sess_establish_session(sess_data);
  1169. /* Cleanup */
  1170. kfree(ses->auth_key.response);
  1171. ses->auth_key.response = NULL;
  1172. kfree(ses->ntlmssp);
  1173. ses->ntlmssp = NULL;
  1174. sess_data->func = NULL;
  1175. sess_data->result = rc;
  1176. }
  1177. static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
  1178. {
  1179. int type;
  1180. type = select_sectype(ses->server, ses->sectype);
  1181. cifs_dbg(FYI, "sess setup type %d\n", type);
  1182. if (type == Unspecified) {
  1183. cifs_dbg(VFS,
  1184. "Unable to select appropriate authentication method!");
  1185. return -EINVAL;
  1186. }
  1187. switch (type) {
  1188. case LANMAN:
  1189. /* LANMAN and plaintext are less secure and off by default.
  1190. * So we make this explicitly be turned on in kconfig (in the
  1191. * build) and turned on at runtime (changed from the default)
  1192. * in proc/fs/cifs or via mount parm. Unfortunately this is
  1193. * needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
  1194. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  1195. sess_data->func = sess_auth_lanman;
  1196. break;
  1197. #else
  1198. return -EOPNOTSUPP;
  1199. #endif
  1200. case NTLM:
  1201. sess_data->func = sess_auth_ntlm;
  1202. break;
  1203. case NTLMv2:
  1204. sess_data->func = sess_auth_ntlmv2;
  1205. break;
  1206. case Kerberos:
  1207. #ifdef CONFIG_CIFS_UPCALL
  1208. sess_data->func = sess_auth_kerberos;
  1209. break;
  1210. #else
  1211. cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
  1212. return -ENOSYS;
  1213. break;
  1214. #endif /* CONFIG_CIFS_UPCALL */
  1215. case RawNTLMSSP:
  1216. sess_data->func = sess_auth_rawntlmssp_negotiate;
  1217. break;
  1218. default:
  1219. cifs_dbg(VFS, "secType %d not supported!\n", type);
  1220. return -ENOSYS;
  1221. }
  1222. return 0;
  1223. }
  1224. int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
  1225. const struct nls_table *nls_cp)
  1226. {
  1227. int rc = 0;
  1228. struct sess_data *sess_data;
  1229. if (ses == NULL) {
  1230. WARN(1, "%s: ses == NULL!", __func__);
  1231. return -EINVAL;
  1232. }
  1233. sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
  1234. if (!sess_data)
  1235. return -ENOMEM;
  1236. rc = select_sec(ses, sess_data);
  1237. if (rc)
  1238. goto out;
  1239. sess_data->xid = xid;
  1240. sess_data->ses = ses;
  1241. sess_data->buf0_type = CIFS_NO_BUFFER;
  1242. sess_data->nls_cp = (struct nls_table *) nls_cp;
  1243. while (sess_data->func)
  1244. sess_data->func(sess_data);
  1245. /* Store result before we free sess_data */
  1246. rc = sess_data->result;
  1247. out:
  1248. kfree(sess_data);
  1249. return rc;
  1250. }