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.

262 lines
5.4 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. secutils.cpp
  5. Abstract:
  6. The utility functions for the shims.
  7. History:
  8. 02/09/2001 maonis Created
  9. 08/14/2001 robkenny Moved code inside the ShimLib namespace.
  10. --*/
  11. #include "secutils.h"
  12. #include "StrSafe.h"
  13. namespace ShimLib
  14. {
  15. /*++
  16. Function Description:
  17. Determine if the log on user is a member of the group.
  18. Arguments:
  19. IN dwGroup - specify the alias of the group.
  20. OUT pfIsMember - TRUE if it's a member, FALSE if not.
  21. Return Value:
  22. TRUE - we successfully determined if it's a member.
  23. FALSE otherwise.
  24. DevNote:
  25. We are assuming the calling thread is not impersonating.
  26. History:
  27. 02/12/2001 maonis Created
  28. --*/
  29. BOOL
  30. SearchGroupForSID(
  31. DWORD dwGroup,
  32. BOOL* pfIsMember
  33. )
  34. {
  35. PSID pSID = NULL;
  36. SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
  37. BOOL fRes = TRUE;
  38. if (!AllocateAndInitializeSid(
  39. &SIDAuth,
  40. 2,
  41. SECURITY_BUILTIN_DOMAIN_RID,
  42. dwGroup,
  43. 0,
  44. 0,
  45. 0,
  46. 0,
  47. 0,
  48. 0,
  49. &pSID))
  50. {
  51. DPF("SecurityUtils", eDbgLevelError, "[SearchGroupForSID] AllocateAndInitializeSid failed %d", GetLastError());
  52. return FALSE;
  53. }
  54. if (!CheckTokenMembership(NULL, pSID, pfIsMember))
  55. {
  56. DPF("SecurityUtils", eDbgLevelError, "[SearchGroupForSID] CheckTokenMembership failed: %d", GetLastError());
  57. fRes = FALSE;
  58. }
  59. FreeSid(pSID);
  60. return fRes;
  61. }
  62. /*++
  63. Function Description:
  64. Determine if we should shim this app or not.
  65. If the user is
  66. 1) a member of the Users and
  67. 2) not a member of the Administrators group and
  68. 3) not a member of the Power Users group and
  69. 3) not a member of the Guest group
  70. we'll apply the shim.
  71. Arguments:
  72. None.
  73. Return Value:
  74. TRUE - we should apply the shim.
  75. FALSE otherwise.
  76. History:
  77. 02/12/2001 maonis Created
  78. --*/
  79. BOOL
  80. ShouldApplyShim()
  81. {
  82. BOOL fIsUser, fIsAdmin, fIsPowerUser, fIsGuest;
  83. if (!SearchGroupForSID(DOMAIN_ALIAS_RID_USERS, &fIsUser) ||
  84. !SearchGroupForSID(DOMAIN_ALIAS_RID_ADMINS, &fIsAdmin) ||
  85. !SearchGroupForSID(DOMAIN_ALIAS_RID_POWER_USERS, &fIsPowerUser) ||
  86. !SearchGroupForSID(DOMAIN_ALIAS_RID_GUESTS, &fIsGuest))
  87. {
  88. //
  89. // Don't do anything if we are not sure.
  90. //
  91. return FALSE;
  92. }
  93. return (fIsUser && !fIsPowerUser && !fIsAdmin && !fIsGuest);
  94. }
  95. // The GENERIC_MAPPING from generic file access rights to specific and standard
  96. // access types.
  97. static GENERIC_MAPPING s_gmFile =
  98. {
  99. FILE_GENERIC_READ,
  100. FILE_GENERIC_WRITE,
  101. FILE_GENERIC_EXECUTE,
  102. FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE
  103. };
  104. /*++
  105. Function Description:
  106. Given the creation dispositon and the desired access when calling
  107. CreateFile, we determine if the caller is requesting write access.
  108. This is specific for files.
  109. Arguments:
  110. IN pszObject - name of the file or directory.
  111. OUT pam - points to the access mask of the user to this object.
  112. Return Value:
  113. TRUE - successfully got the access mask.
  114. FALSE otherwise.
  115. DevNote:
  116. UNDONE - This might not be a complete list...can add as we debug more apps.
  117. History:
  118. 02/12/2001 maonis Created
  119. --*/
  120. BOOL
  121. RequestWriteAccess(
  122. IN DWORD dwCreationDisposition,
  123. IN DWORD dwDesiredAccess
  124. )
  125. {
  126. MapGenericMask(&dwDesiredAccess, &s_gmFile);
  127. if ((dwCreationDisposition != OPEN_EXISTING) ||
  128. (dwDesiredAccess & DELETE) ||
  129. // Generally, app would not specify FILE_WRITE_DATA directly, and if
  130. // it specifies GENERIC_WRITE, it will get mapped to FILE_WRITE_DATA
  131. // OR other things so checking FILE_WRITE_DATA is sufficient.
  132. (dwDesiredAccess & FILE_WRITE_DATA))
  133. {
  134. return TRUE;
  135. }
  136. return FALSE;
  137. }
  138. /*++
  139. Function Description:
  140. Add or remove the SE_PRIVILEGE_ENABLED from the current process.
  141. Arguments:
  142. IN pwszPrivilege Name of the priv. to modify.
  143. IN fEnable Add or remove SE_PRIVILEGE_ENABLED
  144. Return Value:
  145. TRUE - if SE_PRIVILEGE_ENABLED was successfully added or removed.
  146. FALSE otherwise.
  147. 04/03/2001 maonis Created
  148. --*/
  149. BOOL
  150. AdjustPrivilege(
  151. LPCWSTR pwszPrivilege,
  152. BOOL fEnable
  153. )
  154. {
  155. HANDLE hToken;
  156. TOKEN_PRIVILEGES tp;
  157. BOOL fRes = FALSE;
  158. // Obtain the process token.
  159. if (OpenProcessToken(
  160. GetCurrentProcess(),
  161. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  162. &hToken))
  163. {
  164. // Get the LUID.
  165. if (LookupPrivilegeValueW(NULL, pwszPrivilege, &tp.Privileges[0].Luid))
  166. {
  167. tp.PrivilegeCount = 1;
  168. tp.Privileges[0].Attributes = (fEnable ? SE_PRIVILEGE_ENABLED : 0);
  169. // Enable or disable the privilege.
  170. if (AdjustTokenPrivileges(
  171. hToken,
  172. FALSE,
  173. &tp,
  174. 0,
  175. (PTOKEN_PRIVILEGES)NULL,
  176. 0))
  177. {
  178. fRes = TRUE;
  179. }
  180. }
  181. CloseHandle(hToken);
  182. }
  183. return fRes;
  184. }
  185. }; // end of namespace ShimLib