Leaked source code of windows server 2003
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.

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