sess.c 39 KB

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