sess.c 39 KB

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