Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

544 lines
20 KiB

  1. /*++ BUILD Version: 0009 // Increment this if a change has global effects
  2. Copyright (c) 1987-1993 Microsoft Corporation
  3. Module Name:
  4. sessetup.c
  5. Abstract:
  6. This module implements the Session setup related routines
  7. Author:
  8. Balan Sethu Raman (SethuR) 06-Mar-95 Created
  9. --*/
  10. #include "precomp.h"
  11. #pragma hdrstop
  12. #include <exsessup.h>
  13. #include "ntlsapi.h"
  14. #include "mrxsec.h"
  15. #ifdef ALLOC_PRAGMA
  16. #pragma alloc_text(PAGE, BuildSessionSetupSecurityInformation)
  17. #pragma alloc_text(PAGE, BuildTreeConnectSecurityInformation)
  18. #endif
  19. extern BOOLEAN EnablePlainTextPassword;
  20. NTSTATUS
  21. BuildSessionSetupSecurityInformation(
  22. PSMB_EXCHANGE pExchange,
  23. PBYTE pSmbBuffer,
  24. PULONG pSmbBufferSize)
  25. /*++
  26. Routine Description:
  27. This routine builds the security related information for the session setup SMB
  28. Arguments:
  29. pServer - the server instance
  30. pSmbBuffer - the SMB buffer
  31. pSmbBufferSize - the size of the buffer on input ( modified to size remaining on
  32. output)
  33. Return Value:
  34. RXSTATUS - The return status for the operation
  35. Notes:
  36. Eventhough the genral structure of the code tries to isolate dialect specific issues
  37. as much as possible this routine takes the opposite approach. This is because of the
  38. preamble and prologue to security interaction which far outweigh the dialect specific
  39. work required to be done. Therefore in the interests of a smaller footprint this approach
  40. has been adopted.
  41. --*/
  42. {
  43. NTSTATUS Status = STATUS_SUCCESS;
  44. UNICODE_STRING UserName;
  45. UNICODE_STRING DomainName;
  46. STRING CaseSensitiveResponse;
  47. STRING CaseInsensitiveResponse;
  48. PVOID pSecurityBlob;
  49. USHORT SecurityBlobSize;
  50. PSMBCE_SERVER pServer = SmbCeGetExchangeServer(pExchange);
  51. PSMBCE_SESSION pSession = SmbCeGetExchangeSession(pExchange);
  52. PSMBCEDB_SERVER_ENTRY pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  53. SECURITY_RESPONSE_CONTEXT ResponseContext;
  54. KAPC_STATE ApcState;
  55. BOOLEAN AttachToSystemProcess = FALSE;
  56. ULONG BufferSize = *pSmbBufferSize;
  57. PAGED_CODE();
  58. RxDbgTrace( +1, Dbg, ("BuildSessionSetupSecurityInformation -- Entry\n"));
  59. SmbLog(LOG,
  60. BuildSessionSetupSecurityInformation,
  61. LOGPTR(pSession)
  62. LOGULONG(pSession->LogonId.HighPart)
  63. LOGULONG(pSession->LogonId.LowPart));
  64. if ((pServer->DialectFlags & DF_EXTENDED_SECURITY) &&
  65. !FlagOn(pSession->Flags,SMBCE_SESSION_FLAGS_REMOTE_BOOT_SESSION)) {
  66. //
  67. // For uplevel servers, this gets handled all at once:
  68. //
  69. PREQ_NT_EXTENDED_SESSION_SETUP_ANDX pExtendedNtSessionSetupReq;
  70. PBYTE pBuffer = pSmbBuffer;
  71. // Position the buffer for copying the security blob
  72. pBuffer += FIELD_OFFSET(REQ_NT_EXTENDED_SESSION_SETUP_ANDX,Buffer);
  73. BufferSize -= FIELD_OFFSET(REQ_NT_EXTENDED_SESSION_SETUP_ANDX,Buffer);
  74. pExtendedNtSessionSetupReq = (PREQ_NT_EXTENDED_SESSION_SETUP_ANDX)pSmbBuffer;
  75. pSecurityBlob = pBuffer ;
  76. SecurityBlobSize = (USHORT) BufferSize ;
  77. Status = BuildExtendedSessionSetupResponsePrologue(
  78. pExchange,
  79. pSecurityBlob,
  80. &SecurityBlobSize,
  81. &ResponseContext);
  82. if ( NT_SUCCESS( Status ) )
  83. {
  84. SmbPutUshort(
  85. &pExtendedNtSessionSetupReq->SecurityBlobLength,
  86. SecurityBlobSize);
  87. BufferSize -= SecurityBlobSize;
  88. }
  89. } else {
  90. if (!MRxSmbUseKernelModeSecurity &&
  91. !FlagOn(pSession->Flags,SMBCE_SESSION_FLAGS_REMOTE_BOOT_SESSION)) {
  92. //NTRAID-455636-2/2/2000-yunlin We should consolidate three routines calling LSA into one
  93. Status = BuildExtendedSessionSetupResponsePrologueFake(pExchange);
  94. if (Status != STATUS_SUCCESS) {
  95. goto FINALLY;
  96. }
  97. }
  98. Status = BuildNtLanmanResponsePrologue(
  99. pExchange,
  100. &UserName,
  101. &DomainName,
  102. &CaseSensitiveResponse,
  103. &CaseInsensitiveResponse,
  104. &ResponseContext);
  105. if (Status == STATUS_SUCCESS) {
  106. // If the security package returns us the credentials corresponding to a
  107. // NULL session mark the session as a NULL session. This will avoid
  108. // conflicts with the user trying to present the credentials for a NULL
  109. // session, i.e., explicitly specified zero length passwords, user name
  110. // and domain name.
  111. RxDbgTrace(0,Dbg,("Session %lx UN Length %lx DN length %ld IR length %ld SR length %ld\n",
  112. pSession,UserName.Length,DomainName.Length,
  113. CaseInsensitiveResponse.Length,CaseSensitiveResponse.Length));
  114. if ((UserName.Length == 0) &&
  115. (DomainName.Length == 0) &&
  116. (CaseSensitiveResponse.Length == 0) &&
  117. (CaseInsensitiveResponse.Length == 1)) {
  118. RxDbgTrace(0,Dbg,("Implicit NULL session setup\n"));
  119. pSession->Flags |= SMBCE_SESSION_FLAGS_NULL_CREDENTIALS;
  120. } else {
  121. if( pServerEntry->SecuritySignaturesEnabled == TRUE &&
  122. pServerEntry->SecuritySignaturesActive == FALSE &&
  123. !FlagOn(pSession->Flags, SMBCE_SESSION_FLAGS_GUEST_SESSION)) {
  124. if (FlagOn(pSession->Flags, SMBCE_SESSION_FLAGS_LANMAN_SESSION_KEY_USED)) {
  125. UCHAR SessionKey[MSV1_0_USER_SESSION_KEY_LENGTH];
  126. RtlZeroMemory(SessionKey, sizeof(SessionKey));
  127. RtlCopyMemory(SessionKey, pSession->LanmanSessionKey, MSV1_0_LANMAN_SESSION_KEY_LENGTH);
  128. SmbInitializeSmbSecuritySignature(pServer,
  129. SessionKey,
  130. CaseInsensitiveResponse.Buffer,
  131. CaseInsensitiveResponse.Length);
  132. } else{
  133. SmbInitializeSmbSecuritySignature(pServer,
  134. pSession->UserSessionKey,
  135. CaseSensitiveResponse.Buffer,
  136. CaseSensitiveResponse.Length);
  137. }
  138. }
  139. }
  140. }
  141. }
  142. if (NT_SUCCESS(Status)) {
  143. PBYTE pBuffer = pSmbBuffer;
  144. if (pServer->Dialect == NTLANMAN_DIALECT) {
  145. if (FlagOn(pServer->DialectFlags,DF_EXTENDED_SECURITY) &&
  146. !FlagOn(pSession->Flags,SMBCE_SESSION_FLAGS_REMOTE_BOOT_SESSION)) {
  147. //
  148. // Already done above
  149. //
  150. NOTHING ;
  151. } else {
  152. //PREQ_SESSION_SETUP_ANDX pSessionSetupReq = (PREQ_SESSION_SETUP_ANDX)pSmbBuffer;
  153. PREQ_NT_SESSION_SETUP_ANDX pNtSessionSetupReq = (PREQ_NT_SESSION_SETUP_ANDX)pSmbBuffer;
  154. // It it is a NT server both the case insensitive and case sensitive passwords
  155. // need to be copied. for share-level, just copy a token 1-byte NULL password
  156. // Position the buffer for copying the password.
  157. pBuffer += FIELD_OFFSET(REQ_NT_SESSION_SETUP_ANDX,Buffer);
  158. BufferSize -= FIELD_OFFSET(REQ_NT_SESSION_SETUP_ANDX,Buffer);
  159. SmbPutUlong(&pNtSessionSetupReq->Reserved,0);
  160. if (pServer->SecurityMode == SECURITY_MODE_USER_LEVEL){
  161. RxDbgTrace( 0, Dbg, ("BuildSessionSetupSecurityInformation -- NtUserPasswords\n"));
  162. if (pServer->EncryptPasswords) {
  163. SmbPutUshort(
  164. &pNtSessionSetupReq->CaseInsensitivePasswordLength,
  165. CaseInsensitiveResponse.Length);
  166. SmbPutUshort(
  167. &pNtSessionSetupReq->CaseSensitivePasswordLength,
  168. CaseSensitiveResponse.Length);
  169. Status = SmbPutString(
  170. &pBuffer,
  171. &CaseInsensitiveResponse,
  172. &BufferSize);
  173. if (NT_SUCCESS(Status)) {
  174. Status = SmbPutString(
  175. &pBuffer,
  176. &CaseSensitiveResponse,
  177. &BufferSize);
  178. }
  179. } else if (EnablePlainTextPassword) {
  180. if (pSession->pPassword != NULL) {
  181. if (FlagOn(pServer->DialectFlags,DF_UNICODE)) {
  182. PBYTE pTempBuffer = pBuffer;
  183. *pBuffer = 0;
  184. pBuffer = ALIGN_SMB_WSTR(pBuffer);
  185. BufferSize -= (ULONG)(pBuffer - pTempBuffer);
  186. SmbPutUshort(
  187. &pNtSessionSetupReq->CaseInsensitivePasswordLength,
  188. 0);
  189. SmbPutUshort(
  190. &pNtSessionSetupReq->CaseSensitivePasswordLength,
  191. pSession->pPassword->Length + 2);
  192. Status = SmbPutUnicodeString(
  193. &pBuffer,
  194. pSession->pPassword,
  195. &BufferSize);
  196. } else {
  197. SmbPutUshort(
  198. &pNtSessionSetupReq->CaseInsensitivePasswordLength,
  199. pSession->pPassword->Length/2 + 1);
  200. SmbPutUshort(
  201. &pNtSessionSetupReq->CaseSensitivePasswordLength,
  202. 0);
  203. Status = SmbPutUnicodeStringAsOemString(
  204. &pBuffer,
  205. pSession->pPassword,
  206. &BufferSize);
  207. }
  208. } else {
  209. SmbPutUshort(&pNtSessionSetupReq->CaseSensitivePasswordLength,0);
  210. SmbPutUshort(&pNtSessionSetupReq->CaseInsensitivePasswordLength,1);
  211. *pBuffer++ = '\0';
  212. BufferSize -= sizeof(CHAR);
  213. }
  214. } else {
  215. Status = STATUS_LOGIN_WKSTA_RESTRICTION;
  216. }
  217. } else {
  218. RxDbgTrace( 0, Dbg, ("BuildSessionSetupSecurityInformation -- NtSharePasswords\n"));
  219. SmbPutUshort(&pNtSessionSetupReq->CaseInsensitivePasswordLength, 1);
  220. SmbPutUshort(&pNtSessionSetupReq->CaseSensitivePasswordLength, 1);
  221. *pBuffer = 0;
  222. *(pBuffer+1) = 0;
  223. pBuffer += 2;
  224. BufferSize -= 2;
  225. }
  226. }
  227. } else {
  228. PREQ_SESSION_SETUP_ANDX pSessionSetupReq = (PREQ_SESSION_SETUP_ANDX)pSmbBuffer;
  229. // Position the buffer for copying the password.
  230. pBuffer += FIELD_OFFSET(REQ_SESSION_SETUP_ANDX,Buffer);
  231. BufferSize -= FIELD_OFFSET(REQ_SESSION_SETUP_ANDX,Buffer);
  232. if ( (pServer->SecurityMode == SECURITY_MODE_USER_LEVEL)
  233. && (CaseInsensitiveResponse.Length > 0)) {
  234. if (pServer->EncryptPasswords) {
  235. // For other lanman servers only the case insensitive password is required.
  236. SmbPutUshort(
  237. &pSessionSetupReq->PasswordLength,
  238. CaseInsensitiveResponse.Length);
  239. // Copy the password
  240. Status = SmbPutString(
  241. &pBuffer,
  242. &CaseInsensitiveResponse,
  243. &BufferSize);
  244. } else {
  245. if (EnablePlainTextPassword) {
  246. if (pSession->pPassword != NULL) {
  247. SmbPutUshort(
  248. &pSessionSetupReq->PasswordLength,
  249. pSession->pPassword->Length/2 + 1);
  250. Status = SmbPutUnicodeStringAsOemString(
  251. &pBuffer,
  252. pSession->pPassword,
  253. &BufferSize);
  254. } else {
  255. SmbPutUshort(&pSessionSetupReq->PasswordLength,1);
  256. *pBuffer++ = '\0';
  257. BufferSize -= sizeof(CHAR);
  258. }
  259. } else {
  260. Status = STATUS_LOGIN_WKSTA_RESTRICTION;
  261. }
  262. }
  263. } else {
  264. // Share level security. Send a null string for the password
  265. SmbPutUshort(&pSessionSetupReq->PasswordLength,1);
  266. *pBuffer++ = '\0';
  267. BufferSize -= sizeof(CHAR);
  268. }
  269. }
  270. // The User name and the domain name strings can be either copied from
  271. // the information returned in the request response or the information
  272. // that is already present in the session entry.
  273. if (NT_SUCCESS(Status) &&
  274. (!FlagOn(pServer->DialectFlags,DF_EXTENDED_SECURITY) ||
  275. FlagOn(pSession->Flags,SMBCE_SESSION_FLAGS_REMOTE_BOOT_SESSION))) {
  276. if ((pServer->Dialect == NTLANMAN_DIALECT) &&
  277. (pServer->NtServer.NtCapabilities & CAP_UNICODE)) {
  278. // Copy the account/domain names as UNICODE strings
  279. PBYTE pTempBuffer = pBuffer;
  280. RxDbgTrace( 0, Dbg, ("BuildSessionSetupSecurityInformation -- account/domain as unicode\n"));
  281. *pBuffer = 0;
  282. pBuffer = ALIGN_SMB_WSTR(pBuffer);
  283. BufferSize -= (ULONG)(pBuffer - pTempBuffer);
  284. Status = SmbPutUnicodeString(
  285. &pBuffer,
  286. &UserName,
  287. &BufferSize);
  288. if (NT_SUCCESS(Status)) {
  289. Status = SmbPutUnicodeString(
  290. &pBuffer,
  291. &DomainName,
  292. &BufferSize);
  293. }
  294. } else {
  295. // Copy the account/domain names as ASCII strings.
  296. RxDbgTrace( 0, Dbg, ("BuildSessionSetupSecurityInformation -- account/domain as ascii\n"));
  297. Status = SmbPutUnicodeStringAsOemString(
  298. &pBuffer,
  299. &UserName,
  300. &BufferSize);
  301. if (NT_SUCCESS(Status)) {
  302. Status = SmbPutUnicodeStringAsOemString(
  303. &pBuffer,
  304. &DomainName,
  305. &BufferSize);
  306. }
  307. }
  308. }
  309. if (NT_SUCCESS(Status)) {
  310. *pSmbBufferSize = BufferSize;
  311. }
  312. }
  313. // Free the buffer allocated by the security package.
  314. if ((pServer->DialectFlags & DF_EXTENDED_SECURITY) &&
  315. !FlagOn(pSession->Flags,SMBCE_SESSION_FLAGS_REMOTE_BOOT_SESSION)) {
  316. BuildExtendedSessionSetupResponseEpilogue(&ResponseContext);
  317. } else {
  318. BuildNtLanmanResponseEpilogue(pExchange, &ResponseContext);
  319. }
  320. // Detach from the rdr process.
  321. FINALLY:
  322. RxDbgTrace( -1, Dbg, ("BuildSessionSetupSecurityInformation -- Exit, status=%08lx\n",Status));
  323. return Status;
  324. }
  325. NTSTATUS
  326. BuildTreeConnectSecurityInformation(
  327. PSMB_EXCHANGE pExchange,
  328. PBYTE pBuffer,
  329. PBYTE pPasswordLength,
  330. PULONG pSmbBufferSize)
  331. /*++
  332. Routine Description:
  333. This routine builds the security related information for the session setup SMB
  334. Arguments:
  335. pServer - the server instance
  336. pLogonId - the logon id. for which the session is being setup
  337. pPassword - the user supplied password if any
  338. pBuffer - the password buffer
  339. pPasswordLength - where the password length is to be stored
  340. pSmbBufferSize - the size of the buffer on input ( modified to size remaining on
  341. output)
  342. Return Value:
  343. NTSTATUS - The return status for the operation
  344. Notes:
  345. Eventhough the genral structure of the code tries to isolate dialect specific issues
  346. as much as possible this routine takes the opposite approach. This is because of the
  347. preamble and prologue to security interaction which far outweigh the dialect specific
  348. work required to be done. Therefore in the interests of a smaller footprint this approach
  349. has been adopted.
  350. --*/
  351. {
  352. NTSTATUS FinalStatus,Status;
  353. UNICODE_STRING UserName,DomainName;
  354. STRING CaseSensitiveChallengeResponse,CaseInsensitiveChallengeResponse;
  355. SECURITY_RESPONSE_CONTEXT ResponseContext;
  356. ULONG PasswordLength = 0;
  357. PSMBCE_SERVER pServer = SmbCeGetExchangeServer(pExchange);
  358. PSMBCE_SESSION pSession = SmbCeGetExchangeSession(pExchange);
  359. KAPC_STATE ApcState;
  360. BOOLEAN AttachToSystemProcess = FALSE;
  361. PAGED_CODE();
  362. Status = STATUS_SUCCESS;
  363. if (pServer->EncryptPasswords) {
  364. Status = BuildNtLanmanResponsePrologue(
  365. pExchange,
  366. &UserName,
  367. &DomainName,
  368. &CaseSensitiveChallengeResponse,
  369. &CaseInsensitiveChallengeResponse,
  370. &ResponseContext);
  371. if (NT_SUCCESS(Status)) {
  372. if (FlagOn(pServer->DialectFlags,DF_MIXEDCASEPW)) {
  373. RxDbgTrace( 0, Dbg, ("BuildTreeConnectSecurityInformation -- case sensitive password\n"));
  374. // Copy the password length onto the SMB buffer
  375. PasswordLength = CaseSensitiveChallengeResponse.Length;
  376. // Copy the password
  377. Status = SmbPutString(
  378. &pBuffer,
  379. &CaseSensitiveChallengeResponse,
  380. pSmbBufferSize);
  381. } else {
  382. RxDbgTrace( 0, Dbg, ("BuildTreeConnectSecurityInformation -- case insensitive password\n"));
  383. // Copy the password length onto the SMB buffer
  384. PasswordLength = CaseInsensitiveChallengeResponse.Length;
  385. // Copy the password
  386. Status = SmbPutString(
  387. &pBuffer,
  388. &CaseInsensitiveChallengeResponse,
  389. pSmbBufferSize);
  390. }
  391. BuildNtLanmanResponseEpilogue(pExchange, &ResponseContext);
  392. }
  393. } else {
  394. if (pSession->pPassword == NULL) {
  395. // The logon password cannot be sent as plain text. Send a Null string as password.
  396. PasswordLength = 1;
  397. if (*pSmbBufferSize >= 1) {
  398. *((PCHAR)pBuffer) = '\0';
  399. pBuffer += sizeof(CHAR);
  400. Status = STATUS_SUCCESS;
  401. } else {
  402. Status = STATUS_BUFFER_OVERFLOW;
  403. }
  404. } else {
  405. if (EnablePlainTextPassword) {
  406. OEM_STRING OemString;
  407. OemString.Length = OemString.MaximumLength = (USHORT)(*pSmbBufferSize - sizeof(CHAR));
  408. OemString.Buffer = pBuffer;
  409. Status = RtlUnicodeStringToOemString(
  410. &OemString,
  411. pSession->pPassword,
  412. FALSE);
  413. if (NT_SUCCESS(Status)) {
  414. PasswordLength = OemString.Length+1;
  415. }
  416. } else {
  417. Status = STATUS_LOGON_FAILURE;
  418. }
  419. }
  420. // reduce the byte count
  421. *pSmbBufferSize -= PasswordLength;
  422. }
  423. SmbPutUshort(pPasswordLength,(USHORT)PasswordLength);
  424. return Status;
  425. }