sess.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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. 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_USERNAME_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. memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_AUTH_RESP_SIZE);
  615. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  616. } else {
  617. pSMB->old_req.PasswordLength = 0;
  618. }
  619. /*
  620. * can not sign if LANMAN negotiated so no need
  621. * to calculate signing key? but what if server
  622. * changed to do higher than lanman dialect and
  623. * we reconnected would we ever calc signing_key?
  624. */
  625. cifs_dbg(FYI, "Negotiating LANMAN setting up strings\n");
  626. /* Unicode not allowed for LANMAN dialects */
  627. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  628. sess_data->iov[2].iov_len = (long) bcc_ptr -
  629. (long) sess_data->iov[2].iov_base;
  630. rc = sess_sendreceive(sess_data);
  631. if (rc)
  632. goto out;
  633. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  634. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  635. /* lanman response has a word count of 3 */
  636. if (smb_buf->WordCount != 3) {
  637. rc = -EIO;
  638. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  639. goto out;
  640. }
  641. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  642. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  643. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  644. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  645. bytes_remaining = get_bcc(smb_buf);
  646. bcc_ptr = pByteArea(smb_buf);
  647. /* BB check if Unicode and decode strings */
  648. if (bytes_remaining == 0) {
  649. /* no string area to decode, do nothing */
  650. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  651. /* unicode string area must be word-aligned */
  652. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  653. ++bcc_ptr;
  654. --bytes_remaining;
  655. }
  656. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  657. sess_data->nls_cp);
  658. } else {
  659. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  660. sess_data->nls_cp);
  661. }
  662. rc = sess_establish_session(sess_data);
  663. out:
  664. sess_data->result = rc;
  665. sess_data->func = NULL;
  666. sess_free_buffer(sess_data);
  667. }
  668. #endif
  669. static void
  670. sess_auth_ntlm(struct sess_data *sess_data)
  671. {
  672. int rc = 0;
  673. struct smb_hdr *smb_buf;
  674. SESSION_SETUP_ANDX *pSMB;
  675. char *bcc_ptr;
  676. struct cifs_ses *ses = sess_data->ses;
  677. __u32 capabilities;
  678. __u16 bytes_remaining;
  679. /* old style NTLM sessionsetup */
  680. /* wct = 13 */
  681. rc = sess_alloc_buffer(sess_data, 13);
  682. if (rc)
  683. goto out;
  684. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  685. bcc_ptr = sess_data->iov[2].iov_base;
  686. capabilities = cifs_ssetup_hdr(ses, pSMB);
  687. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  688. if (ses->user_name != NULL) {
  689. pSMB->req_no_secext.CaseInsensitivePasswordLength =
  690. cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  691. pSMB->req_no_secext.CaseSensitivePasswordLength =
  692. cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  693. /* calculate ntlm response and session key */
  694. rc = setup_ntlm_response(ses, sess_data->nls_cp);
  695. if (rc) {
  696. cifs_dbg(VFS, "Error %d during NTLM authentication\n",
  697. rc);
  698. goto out;
  699. }
  700. /* copy ntlm response */
  701. memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  702. CIFS_AUTH_RESP_SIZE);
  703. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  704. memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  705. CIFS_AUTH_RESP_SIZE);
  706. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  707. } else {
  708. pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
  709. pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
  710. }
  711. if (ses->capabilities & CAP_UNICODE) {
  712. /* unicode strings must be word aligned */
  713. if (sess_data->iov[0].iov_len % 2) {
  714. *bcc_ptr = 0;
  715. bcc_ptr++;
  716. }
  717. unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  718. } else {
  719. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  720. }
  721. sess_data->iov[2].iov_len = (long) bcc_ptr -
  722. (long) sess_data->iov[2].iov_base;
  723. rc = sess_sendreceive(sess_data);
  724. if (rc)
  725. goto out;
  726. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  727. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  728. if (smb_buf->WordCount != 3) {
  729. rc = -EIO;
  730. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  731. goto out;
  732. }
  733. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  734. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  735. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  736. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  737. bytes_remaining = get_bcc(smb_buf);
  738. bcc_ptr = pByteArea(smb_buf);
  739. /* BB check if Unicode and decode strings */
  740. if (bytes_remaining == 0) {
  741. /* no string area to decode, do nothing */
  742. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  743. /* unicode string area must be word-aligned */
  744. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  745. ++bcc_ptr;
  746. --bytes_remaining;
  747. }
  748. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  749. sess_data->nls_cp);
  750. } else {
  751. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  752. sess_data->nls_cp);
  753. }
  754. rc = sess_establish_session(sess_data);
  755. out:
  756. sess_data->result = rc;
  757. sess_data->func = NULL;
  758. sess_free_buffer(sess_data);
  759. kfree(ses->auth_key.response);
  760. ses->auth_key.response = NULL;
  761. }
  762. static void
  763. sess_auth_ntlmv2(struct sess_data *sess_data)
  764. {
  765. int rc = 0;
  766. struct smb_hdr *smb_buf;
  767. SESSION_SETUP_ANDX *pSMB;
  768. char *bcc_ptr;
  769. struct cifs_ses *ses = sess_data->ses;
  770. __u32 capabilities;
  771. __u16 bytes_remaining;
  772. /* old style NTLM sessionsetup */
  773. /* wct = 13 */
  774. rc = sess_alloc_buffer(sess_data, 13);
  775. if (rc)
  776. goto out;
  777. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  778. bcc_ptr = sess_data->iov[2].iov_base;
  779. capabilities = cifs_ssetup_hdr(ses, pSMB);
  780. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  781. /* LM2 password would be here if we supported it */
  782. pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
  783. if (ses->user_name != NULL) {
  784. /* calculate nlmv2 response and session key */
  785. rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
  786. if (rc) {
  787. cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
  788. goto out;
  789. }
  790. memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  791. ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  792. bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
  793. /* set case sensitive password length after tilen may get
  794. * assigned, tilen is 0 otherwise.
  795. */
  796. pSMB->req_no_secext.CaseSensitivePasswordLength =
  797. cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  798. } else {
  799. pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
  800. }
  801. if (ses->capabilities & CAP_UNICODE) {
  802. if (sess_data->iov[0].iov_len % 2) {
  803. *bcc_ptr = 0;
  804. bcc_ptr++;
  805. }
  806. unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  807. } else {
  808. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  809. }
  810. sess_data->iov[2].iov_len = (long) bcc_ptr -
  811. (long) sess_data->iov[2].iov_base;
  812. rc = sess_sendreceive(sess_data);
  813. if (rc)
  814. goto out;
  815. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  816. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  817. if (smb_buf->WordCount != 3) {
  818. rc = -EIO;
  819. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  820. goto out;
  821. }
  822. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  823. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  824. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  825. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  826. bytes_remaining = get_bcc(smb_buf);
  827. bcc_ptr = pByteArea(smb_buf);
  828. /* BB check if Unicode and decode strings */
  829. if (bytes_remaining == 0) {
  830. /* no string area to decode, do nothing */
  831. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  832. /* unicode string area must be word-aligned */
  833. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  834. ++bcc_ptr;
  835. --bytes_remaining;
  836. }
  837. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  838. sess_data->nls_cp);
  839. } else {
  840. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  841. sess_data->nls_cp);
  842. }
  843. rc = sess_establish_session(sess_data);
  844. out:
  845. sess_data->result = rc;
  846. sess_data->func = NULL;
  847. sess_free_buffer(sess_data);
  848. kfree(ses->auth_key.response);
  849. ses->auth_key.response = NULL;
  850. }
  851. #ifdef CONFIG_CIFS_UPCALL
  852. static void
  853. sess_auth_kerberos(struct sess_data *sess_data)
  854. {
  855. int rc = 0;
  856. struct smb_hdr *smb_buf;
  857. SESSION_SETUP_ANDX *pSMB;
  858. char *bcc_ptr;
  859. struct cifs_ses *ses = sess_data->ses;
  860. __u32 capabilities;
  861. __u16 bytes_remaining;
  862. struct key *spnego_key = NULL;
  863. struct cifs_spnego_msg *msg;
  864. u16 blob_len;
  865. /* extended security */
  866. /* wct = 12 */
  867. rc = sess_alloc_buffer(sess_data, 12);
  868. if (rc)
  869. goto out;
  870. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  871. bcc_ptr = sess_data->iov[2].iov_base;
  872. capabilities = cifs_ssetup_hdr(ses, pSMB);
  873. spnego_key = cifs_get_spnego_key(ses);
  874. if (IS_ERR(spnego_key)) {
  875. rc = PTR_ERR(spnego_key);
  876. spnego_key = NULL;
  877. goto out;
  878. }
  879. msg = spnego_key->payload.data[0];
  880. /*
  881. * check version field to make sure that cifs.upcall is
  882. * sending us a response in an expected form
  883. */
  884. if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
  885. cifs_dbg(VFS,
  886. "incorrect version of cifs.upcall (expected %d but got %d)",
  887. CIFS_SPNEGO_UPCALL_VERSION, msg->version);
  888. rc = -EKEYREJECTED;
  889. goto out_put_spnego_key;
  890. }
  891. ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
  892. GFP_KERNEL);
  893. if (!ses->auth_key.response) {
  894. cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory",
  895. msg->sesskey_len);
  896. rc = -ENOMEM;
  897. goto out_put_spnego_key;
  898. }
  899. ses->auth_key.len = msg->sesskey_len;
  900. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  901. capabilities |= CAP_EXTENDED_SECURITY;
  902. pSMB->req.Capabilities = cpu_to_le32(capabilities);
  903. sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
  904. sess_data->iov[1].iov_len = msg->secblob_len;
  905. pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
  906. if (ses->capabilities & CAP_UNICODE) {
  907. /* unicode strings must be word aligned */
  908. if ((sess_data->iov[0].iov_len
  909. + sess_data->iov[1].iov_len) % 2) {
  910. *bcc_ptr = 0;
  911. bcc_ptr++;
  912. }
  913. unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
  914. unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
  915. } else {
  916. /* BB: is this right? */
  917. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  918. }
  919. sess_data->iov[2].iov_len = (long) bcc_ptr -
  920. (long) sess_data->iov[2].iov_base;
  921. rc = sess_sendreceive(sess_data);
  922. if (rc)
  923. goto out_put_spnego_key;
  924. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  925. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  926. if (smb_buf->WordCount != 4) {
  927. rc = -EIO;
  928. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  929. goto out_put_spnego_key;
  930. }
  931. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  932. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  933. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  934. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  935. bytes_remaining = get_bcc(smb_buf);
  936. bcc_ptr = pByteArea(smb_buf);
  937. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  938. if (blob_len > bytes_remaining) {
  939. cifs_dbg(VFS, "bad security blob length %d\n",
  940. blob_len);
  941. rc = -EINVAL;
  942. goto out_put_spnego_key;
  943. }
  944. bcc_ptr += blob_len;
  945. bytes_remaining -= blob_len;
  946. /* BB check if Unicode and decode strings */
  947. if (bytes_remaining == 0) {
  948. /* no string area to decode, do nothing */
  949. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  950. /* unicode string area must be word-aligned */
  951. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  952. ++bcc_ptr;
  953. --bytes_remaining;
  954. }
  955. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  956. sess_data->nls_cp);
  957. } else {
  958. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  959. sess_data->nls_cp);
  960. }
  961. rc = sess_establish_session(sess_data);
  962. out_put_spnego_key:
  963. key_invalidate(spnego_key);
  964. key_put(spnego_key);
  965. out:
  966. sess_data->result = rc;
  967. sess_data->func = NULL;
  968. sess_free_buffer(sess_data);
  969. kfree(ses->auth_key.response);
  970. ses->auth_key.response = NULL;
  971. }
  972. #endif /* ! CONFIG_CIFS_UPCALL */
  973. /*
  974. * The required kvec buffers have to be allocated before calling this
  975. * function.
  976. */
  977. static int
  978. _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
  979. {
  980. struct smb_hdr *smb_buf;
  981. SESSION_SETUP_ANDX *pSMB;
  982. struct cifs_ses *ses = sess_data->ses;
  983. __u32 capabilities;
  984. char *bcc_ptr;
  985. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  986. smb_buf = (struct smb_hdr *)pSMB;
  987. capabilities = cifs_ssetup_hdr(ses, pSMB);
  988. if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
  989. cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
  990. return -ENOSYS;
  991. }
  992. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  993. capabilities |= CAP_EXTENDED_SECURITY;
  994. pSMB->req.Capabilities |= cpu_to_le32(capabilities);
  995. bcc_ptr = sess_data->iov[2].iov_base;
  996. /* unicode strings must be word aligned */
  997. if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
  998. *bcc_ptr = 0;
  999. bcc_ptr++;
  1000. }
  1001. unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
  1002. sess_data->iov[2].iov_len = (long) bcc_ptr -
  1003. (long) sess_data->iov[2].iov_base;
  1004. return 0;
  1005. }
  1006. static void
  1007. sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
  1008. static void
  1009. sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
  1010. {
  1011. int rc;
  1012. struct smb_hdr *smb_buf;
  1013. SESSION_SETUP_ANDX *pSMB;
  1014. struct cifs_ses *ses = sess_data->ses;
  1015. __u16 bytes_remaining;
  1016. char *bcc_ptr;
  1017. u16 blob_len;
  1018. cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
  1019. /*
  1020. * if memory allocation is successful, caller of this function
  1021. * frees it.
  1022. */
  1023. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  1024. if (!ses->ntlmssp) {
  1025. rc = -ENOMEM;
  1026. goto out;
  1027. }
  1028. ses->ntlmssp->sesskey_per_smbsess = false;
  1029. /* wct = 12 */
  1030. rc = sess_alloc_buffer(sess_data, 12);
  1031. if (rc)
  1032. goto out;
  1033. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1034. /* Build security blob before we assemble the request */
  1035. build_ntlmssp_negotiate_blob(pSMB->req.SecurityBlob, ses);
  1036. sess_data->iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
  1037. sess_data->iov[1].iov_base = pSMB->req.SecurityBlob;
  1038. pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
  1039. rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
  1040. if (rc)
  1041. goto out;
  1042. rc = sess_sendreceive(sess_data);
  1043. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1044. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  1045. /* If true, rc here is expected and not an error */
  1046. if (sess_data->buf0_type != CIFS_NO_BUFFER &&
  1047. smb_buf->Status.CifsError ==
  1048. cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
  1049. rc = 0;
  1050. if (rc)
  1051. goto out;
  1052. cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
  1053. if (smb_buf->WordCount != 4) {
  1054. rc = -EIO;
  1055. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  1056. goto out;
  1057. }
  1058. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  1059. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  1060. bytes_remaining = get_bcc(smb_buf);
  1061. bcc_ptr = pByteArea(smb_buf);
  1062. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  1063. if (blob_len > bytes_remaining) {
  1064. cifs_dbg(VFS, "bad security blob length %d\n",
  1065. blob_len);
  1066. rc = -EINVAL;
  1067. goto out;
  1068. }
  1069. rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
  1070. out:
  1071. sess_free_buffer(sess_data);
  1072. if (!rc) {
  1073. sess_data->func = sess_auth_rawntlmssp_authenticate;
  1074. return;
  1075. }
  1076. /* Else error. Cleanup */
  1077. kfree(ses->auth_key.response);
  1078. ses->auth_key.response = NULL;
  1079. kfree(ses->ntlmssp);
  1080. ses->ntlmssp = NULL;
  1081. sess_data->func = NULL;
  1082. sess_data->result = rc;
  1083. }
  1084. static void
  1085. sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
  1086. {
  1087. int rc;
  1088. struct smb_hdr *smb_buf;
  1089. SESSION_SETUP_ANDX *pSMB;
  1090. struct cifs_ses *ses = sess_data->ses;
  1091. __u16 bytes_remaining;
  1092. char *bcc_ptr;
  1093. unsigned char *ntlmsspblob = NULL;
  1094. u16 blob_len;
  1095. cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
  1096. /* wct = 12 */
  1097. rc = sess_alloc_buffer(sess_data, 12);
  1098. if (rc)
  1099. goto out;
  1100. /* Build security blob before we assemble the request */
  1101. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1102. smb_buf = (struct smb_hdr *)pSMB;
  1103. rc = build_ntlmssp_auth_blob(&ntlmsspblob,
  1104. &blob_len, ses, sess_data->nls_cp);
  1105. if (rc)
  1106. goto out_free_ntlmsspblob;
  1107. sess_data->iov[1].iov_len = blob_len;
  1108. sess_data->iov[1].iov_base = ntlmsspblob;
  1109. pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
  1110. /*
  1111. * Make sure that we tell the server that we are using
  1112. * the uid that it just gave us back on the response
  1113. * (challenge)
  1114. */
  1115. smb_buf->Uid = ses->Suid;
  1116. rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
  1117. if (rc)
  1118. goto out_free_ntlmsspblob;
  1119. rc = sess_sendreceive(sess_data);
  1120. if (rc)
  1121. goto out_free_ntlmsspblob;
  1122. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1123. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  1124. if (smb_buf->WordCount != 4) {
  1125. rc = -EIO;
  1126. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  1127. goto out_free_ntlmsspblob;
  1128. }
  1129. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  1130. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  1131. if (ses->Suid != smb_buf->Uid) {
  1132. ses->Suid = smb_buf->Uid;
  1133. cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
  1134. }
  1135. bytes_remaining = get_bcc(smb_buf);
  1136. bcc_ptr = pByteArea(smb_buf);
  1137. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  1138. if (blob_len > bytes_remaining) {
  1139. cifs_dbg(VFS, "bad security blob length %d\n",
  1140. blob_len);
  1141. rc = -EINVAL;
  1142. goto out_free_ntlmsspblob;
  1143. }
  1144. bcc_ptr += blob_len;
  1145. bytes_remaining -= blob_len;
  1146. /* BB check if Unicode and decode strings */
  1147. if (bytes_remaining == 0) {
  1148. /* no string area to decode, do nothing */
  1149. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  1150. /* unicode string area must be word-aligned */
  1151. if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
  1152. ++bcc_ptr;
  1153. --bytes_remaining;
  1154. }
  1155. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  1156. sess_data->nls_cp);
  1157. } else {
  1158. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  1159. sess_data->nls_cp);
  1160. }
  1161. out_free_ntlmsspblob:
  1162. kfree(ntlmsspblob);
  1163. out:
  1164. sess_free_buffer(sess_data);
  1165. if (!rc)
  1166. rc = sess_establish_session(sess_data);
  1167. /* Cleanup */
  1168. kfree(ses->auth_key.response);
  1169. ses->auth_key.response = NULL;
  1170. kfree(ses->ntlmssp);
  1171. ses->ntlmssp = NULL;
  1172. sess_data->func = NULL;
  1173. sess_data->result = rc;
  1174. }
  1175. static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
  1176. {
  1177. int type;
  1178. type = select_sectype(ses->server, ses->sectype);
  1179. cifs_dbg(FYI, "sess setup type %d\n", type);
  1180. if (type == Unspecified) {
  1181. cifs_dbg(VFS,
  1182. "Unable to select appropriate authentication method!");
  1183. return -EINVAL;
  1184. }
  1185. switch (type) {
  1186. case LANMAN:
  1187. /* LANMAN and plaintext are less secure and off by default.
  1188. * So we make this explicitly be turned on in kconfig (in the
  1189. * build) and turned on at runtime (changed from the default)
  1190. * in proc/fs/cifs or via mount parm. Unfortunately this is
  1191. * needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
  1192. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  1193. sess_data->func = sess_auth_lanman;
  1194. break;
  1195. #else
  1196. return -EOPNOTSUPP;
  1197. #endif
  1198. case NTLM:
  1199. sess_data->func = sess_auth_ntlm;
  1200. break;
  1201. case NTLMv2:
  1202. sess_data->func = sess_auth_ntlmv2;
  1203. break;
  1204. case Kerberos:
  1205. #ifdef CONFIG_CIFS_UPCALL
  1206. sess_data->func = sess_auth_kerberos;
  1207. break;
  1208. #else
  1209. cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
  1210. return -ENOSYS;
  1211. break;
  1212. #endif /* CONFIG_CIFS_UPCALL */
  1213. case RawNTLMSSP:
  1214. sess_data->func = sess_auth_rawntlmssp_negotiate;
  1215. break;
  1216. default:
  1217. cifs_dbg(VFS, "secType %d not supported!\n", type);
  1218. return -ENOSYS;
  1219. }
  1220. return 0;
  1221. }
  1222. int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
  1223. const struct nls_table *nls_cp)
  1224. {
  1225. int rc = 0;
  1226. struct sess_data *sess_data;
  1227. if (ses == NULL) {
  1228. WARN(1, "%s: ses == NULL!", __func__);
  1229. return -EINVAL;
  1230. }
  1231. sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
  1232. if (!sess_data)
  1233. return -ENOMEM;
  1234. rc = select_sec(ses, sess_data);
  1235. if (rc)
  1236. goto out;
  1237. sess_data->xid = xid;
  1238. sess_data->ses = ses;
  1239. sess_data->buf0_type = CIFS_NO_BUFFER;
  1240. sess_data->nls_cp = (struct nls_table *) nls_cp;
  1241. while (sess_data->func)
  1242. sess_data->func(sess_data);
  1243. /* Store result before we free sess_data */
  1244. rc = sess_data->result;
  1245. out:
  1246. kfree(sess_data);
  1247. return rc;
  1248. }