sess.c 40 KB

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