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.

232 lines
4.5 KiB

  1. /*****************************************************************************\
  2. * MODULE: userdata.cxx
  3. *
  4. * The module contains class for user credentials
  5. *
  6. * Copyright (C) 1997-1998 Microsoft Corporation
  7. *
  8. * History:
  9. * 08/28/98 Weihaic Created
  10. *
  11. \*****************************************************************************/
  12. #include "precomp.h"
  13. #ifdef WINNT32
  14. #include "priv.h"
  15. CUserData::CUserData ():
  16. m_pSid(NULL),
  17. m_bValid(FALSE)
  18. {
  19. m_bValid = _GetSid ();
  20. }
  21. CUserData::~CUserData ()
  22. {
  23. LocalFree (m_pSid);
  24. }
  25. int
  26. CUserData::Compare (
  27. CUserData * second)
  28. {
  29. if (m_bValid && second->m_bValid) {
  30. return RtlEqualSid( m_pSid , second->m_pSid) == FALSE;
  31. }
  32. else {
  33. return TRUE;
  34. }
  35. }
  36. CUserData &
  37. CUserData::operator= (const CUserData &rhs)
  38. {
  39. ULONG ulSidLen;
  40. DWORD dwStatus;
  41. if (this == &rhs) {
  42. return *this;
  43. }
  44. LocalFree (m_pSid);
  45. m_pSid = NULL;
  46. m_bValid = FALSE;
  47. if (!rhs.m_bValid)
  48. goto Cleanup;
  49. ulSidLen = RtlLengthSid( rhs.m_pSid );
  50. m_pSid = LocalAlloc( LPTR, ulSidLen );
  51. if (NULL == m_pSid)
  52. goto Cleanup;
  53. dwStatus = RtlCopySid( ulSidLen, m_pSid, rhs.m_pSid );
  54. if (NT_ERROR(dwStatus)) {
  55. LocalFree(m_pSid);
  56. m_pSid = NULL;
  57. SetLastError( ERROR_INVALID_PARAMETER );
  58. goto Cleanup;
  59. }
  60. m_bValid = TRUE;
  61. Cleanup:
  62. return *this;
  63. }
  64. BOOL
  65. CUserData::_GetUserToken (
  66. PTOKEN_USER &TokenUserInfo)
  67. {
  68. DWORD ReturnStatus = 0;
  69. HANDLE ClientToken = NULL;
  70. BOOL bRet = FALSE;
  71. ULONG uSize = 0;
  72. //
  73. // Compare the username specified with that in
  74. // the impersonation token to ensure the caller isn't bogus.
  75. //
  76. // Do this by opening the token,
  77. // querying the token user info,
  78. // and ensuring the returned SID is for this user.
  79. //
  80. TokenUserInfo = NULL;
  81. if (!OpenThreadToken(
  82. GetCurrentThread(), // current thread handle
  83. TOKEN_QUERY, // access required
  84. FALSE, // open as self
  85. &ClientToken)) { // client token
  86. ReturnStatus = GetLastError();
  87. DBG_ASSERT( ReturnStatus , (TEXT("Err : OpenThreadToken: Failed but the lasterror = 0")));
  88. goto Cleanup;
  89. }
  90. //
  91. // Get the size of user's SID for the token.
  92. //
  93. ReturnStatus = NtQueryInformationToken(
  94. ClientToken,
  95. TokenUser,
  96. NULL,
  97. uSize,
  98. &uSize);
  99. if (!NT_ERROR (ReturnStatus) ||
  100. ReturnStatus != STATUS_BUFFER_TOO_SMALL) {
  101. // We expected to be told how big a buffer we needed and we weren't
  102. SetLastError (ERROR_INVALID_PARAMETER);
  103. goto Cleanup;
  104. }
  105. //
  106. // Allocate the user's SID
  107. //
  108. TokenUserInfo = (PTOKEN_USER) LocalAlloc (LPTR, uSize);
  109. if (TokenUserInfo == NULL)
  110. goto Cleanup;
  111. ReturnStatus = NtQueryInformationToken(
  112. ClientToken,
  113. TokenUser,
  114. TokenUserInfo,
  115. uSize,
  116. &uSize);
  117. if (NT_ERROR (ReturnStatus) ) {
  118. // Faild after the allocation
  119. LocalFree( TokenUserInfo );
  120. TokenUserInfo = NULL;
  121. SetLastError (ERROR_INVALID_PARAMETER);
  122. goto Cleanup;
  123. }
  124. //
  125. // Done
  126. //
  127. bRet = TRUE;
  128. Cleanup:
  129. CloseHandle(ClientToken);
  130. return bRet;
  131. }
  132. BOOL
  133. CUserData::_GetSid (VOID)
  134. {
  135. BOOL bRet = FALSE;
  136. PTOKEN_USER pUser;
  137. ULONG ulSidLen;
  138. DWORD dwStatus;
  139. if (!_GetUserToken(pUser))
  140. goto Cleanup;
  141. // Now find the Sid size and copy it locally, free the pUser when done.
  142. ulSidLen = RtlLengthSid(pUser->User.Sid);
  143. m_pSid = LocalAlloc( LPTR, ulSidLen );
  144. if (NULL == m_pSid)
  145. goto Cleanup;
  146. dwStatus = RtlCopySid( ulSidLen, m_pSid, pUser->User.Sid );
  147. if (NT_ERROR(dwStatus)) {
  148. LocalFree(m_pSid);
  149. m_pSid = NULL;
  150. SetLastError( ERROR_INVALID_PARAMETER );
  151. goto Cleanup;
  152. }
  153. bRet = TRUE;
  154. Cleanup:
  155. if (pUser)
  156. LocalFree(pUser);
  157. return bRet;
  158. }
  159. BOOL operator== (
  160. const CUserData &lhs,
  161. const CUserData &rhs)
  162. {
  163. if (lhs.m_bValid && rhs.m_bValid) {
  164. return RtlEqualSid(lhs.m_pSid , rhs.m_pSid );
  165. }
  166. else {
  167. return FALSE;
  168. }
  169. }
  170. BOOL operator!= (
  171. const CUserData &lhs,
  172. const CUserData &rhs)
  173. {
  174. return ! (lhs == rhs);
  175. }
  176. #endif