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.

245 lines
5.6 KiB

  1. /*++
  2. Copyright (c) 1996, 1997 Microsoft Corporation
  3. Module Name:
  4. acl.cpp
  5. Abstract:
  6. This module contains routines to support core security operations in
  7. the Protected Storage Server.
  8. Author:
  9. Scott Field (sfield) 25-Nov-96
  10. --*/
  11. #include <pch.cpp>
  12. #pragma hdrstop
  13. BOOL
  14. FImpersonateClient(
  15. IN PST_PROVIDER_HANDLE *hPSTProv
  16. )
  17. {
  18. handle_t hBinding = ((PCALL_STATE)hPSTProv)->hBinding;
  19. RPC_STATUS RpcStatus;
  20. if(!FIsWinNT())
  21. return TRUE;
  22. if(hPSTProv == NULL)
  23. return FALSE;
  24. if (hBinding == NULL)
  25. {
  26. if ((hPSTProv->LowPart == 0) && (hPSTProv->HighPart == 0) )
  27. return ImpersonateSelf(SecurityImpersonation);
  28. else
  29. return FALSE;
  30. }
  31. RpcStatus = RpcImpersonateClient(hBinding);
  32. if(RpcStatus != RPC_S_OK) {
  33. SetLastError(RpcStatus);
  34. return FALSE;
  35. }
  36. return TRUE;
  37. }
  38. BOOL
  39. FRevertToSelf(
  40. IN PST_PROVIDER_HANDLE *hPSTProv
  41. )
  42. {
  43. handle_t hBinding = ((PCALL_STATE)hPSTProv)->hBinding;
  44. RPC_STATUS RpcStatus;
  45. if(!FIsWinNT())
  46. return TRUE;
  47. if(hPSTProv == NULL)
  48. return FALSE;
  49. if (hBinding == NULL)
  50. {
  51. if ((hPSTProv->LowPart == 0) && (hPSTProv->HighPart == 0) )
  52. return RevertToSelf();
  53. else
  54. return FALSE;
  55. }
  56. RpcStatus = RpcRevertToSelfEx(hBinding);
  57. if(RpcStatus != RPC_S_OK) {
  58. SetLastError(RpcStatus);
  59. return FALSE;
  60. }
  61. return TRUE;
  62. }
  63. // dispatch module callback interface given to providers to ask about callers
  64. BOOL
  65. FGetUserName(
  66. IN PST_PROVIDER_HANDLE *hPSTProv,
  67. OUT LPWSTR* ppszUser
  68. )
  69. /*++
  70. This routine obtains the username (Win95) or Textual Sid (WinNT)
  71. associated with the calling thread. If the cached entry is not present,
  72. the cached entry is initialized with the current user name, and for WinNT,
  73. the authentication Id associated with the username. For WinNT, on
  74. subsequent calls, the calling threads authentication Id is checked to see
  75. if it matches the cached authentication Id - if true, the cached user
  76. string is released, otherwise, the current thread is evaluated and the
  77. result released to the client (note this is unlikely to happen unless
  78. the client process is impersonating multiple users and using the same
  79. context handle).
  80. If ppszUser parameter is set to NULL, the function does not allocate
  81. and copy user string to caller. This is useful to initialize the cached
  82. entry or to determine if the user string is valid and available.
  83. --*/
  84. {
  85. DWORD cch = MAX_PATH;
  86. WCHAR szBuf[MAX_PATH];
  87. BOOL f = FALSE; // assume failure. indicates if we inited OK, too.
  88. if (FIsWinNT())
  89. {
  90. // impersonating client should be easy way of nabbing this info
  91. if(!FImpersonateClient(hPSTProv))
  92. return FALSE;
  93. f = GetUserTextualSid(
  94. NULL,
  95. szBuf,
  96. &cch);
  97. if(!FRevertToSelf(hPSTProv))
  98. return FALSE;
  99. } else {
  100. f = GetUserNameU(
  101. szBuf,
  102. &cch);
  103. if(!f) {
  104. // for Win95, if nobody is logged on, empty user name
  105. if(GetLastError() == ERROR_NOT_LOGGED_ON) {
  106. szBuf[ 0 ] = L'\0';
  107. cch = 1;
  108. f = TRUE;
  109. }
  110. }
  111. }
  112. if (!f)
  113. return FALSE;
  114. if( ppszUser ) {
  115. *ppszUser = (LPWSTR)SSAlloc( cch * sizeof(WCHAR) );
  116. if (*ppszUser == NULL)
  117. return FALSE;
  118. CopyMemory(*ppszUser, szBuf, cch * sizeof(WCHAR) );
  119. }
  120. return TRUE;
  121. }
  122. // gets the image name for the process
  123. BOOL
  124. FGetParentFileName(
  125. IN PST_PROVIDER_HANDLE *hPSTProv,
  126. OUT LPWSTR* ppszName,
  127. OUT DWORD_PTR *lpdwBaseAddress
  128. )
  129. /*++
  130. If ppszName parameter is set to NULL, the function does not allocate
  131. and copy string to caller. This is useful to initialize the cached
  132. entry or to determine if the string is valid and available.
  133. If lpdwBaseAddress is NULL, the caller is not provided the base address
  134. associated with the process image.
  135. --*/
  136. {
  137. CALL_STATE *pCallState = (CALL_STATE *)hPSTProv;
  138. if(pCallState->hProcess == NULL)
  139. return FALSE;
  140. if( ppszName ) {
  141. *ppszName = (LPWSTR)SSAlloc( sizeof(WCHAR) );
  142. if(*ppszName == NULL)
  143. return FALSE;
  144. ZeroMemory( *ppszName, sizeof(WCHAR) );
  145. }
  146. if(lpdwBaseAddress) {
  147. *lpdwBaseAddress = 0;
  148. }
  149. return TRUE;
  150. }
  151. #if 0
  152. BOOL
  153. FGetDiskHash(
  154. IN PST_PROVIDER_HANDLE *hPSTProv,
  155. IN LPWSTR szImageName,
  156. IN BYTE Hash[A_SHA_DIGEST_LEN]
  157. )
  158. {
  159. BOOL bImpersonated = FALSE;
  160. HANDLE hFile;
  161. BOOL bSuccess = FALSE;
  162. if (FIsWinNT())
  163. {
  164. //
  165. // impersonate around hashing disk image since file may be on network
  166. // if impersonation fails, just try it anyway
  167. //
  168. bImpersonated = FImpersonateClient(hPSTProv);
  169. }
  170. hFile = CreateFileU(
  171. szImageName,
  172. GENERIC_READ,
  173. FILE_SHARE_READ,
  174. NULL,
  175. OPEN_EXISTING,
  176. FILE_FLAG_SEQUENTIAL_SCAN,
  177. NULL
  178. );
  179. if( hFile != INVALID_HANDLE_VALUE ) {
  180. bSuccess = HashDiskImage( hFile, Hash );
  181. CloseHandle( hFile );
  182. }
  183. if(bImpersonated)
  184. FRevertToSelf(hPSTProv);
  185. return bSuccess;
  186. }
  187. #endif