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.

311 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1995-2000 Microsoft Corporation
  3. Module Name:
  4. security.c
  5. Abstract:
  6. Routines to deal with security-related stuff.
  7. Externally exposed routines:
  8. pSetupIsUserAdmin
  9. pSetupDoesUserHavePrivilege
  10. pSetupEnablePrivilege
  11. Author:
  12. Ted Miller (tedm) 14-Jun-1995
  13. Revision History:
  14. Jamie Hunter (jamiehun) Jun-27-2000
  15. Moved functions to sputils
  16. --*/
  17. #include "precomp.h"
  18. #include <lmaccess.h>
  19. #pragma hdrstop
  20. #ifndef SPUTILSW
  21. BOOL
  22. pSetupIsUserAdmin(
  23. VOID
  24. )
  25. /*++
  26. Routine Description:
  27. This routine returns TRUE if the caller's process has admin privs
  28. Caller is NOT expected to be impersonating anyone and IS
  29. expected to be able to open their own process and process
  30. token.
  31. Though we could use CheckTokenMembership
  32. this function may be expected to work on older platforms...
  33. Arguments:
  34. None.
  35. Return Value:
  36. TRUE - Caller has Administrator privs.
  37. FALSE - Caller does not have Administrator privs.
  38. --*/
  39. {
  40. BOOL fAdmin = FALSE;
  41. HANDLE hToken = NULL;
  42. DWORD dwStatus;
  43. DWORD dwACLSize;
  44. DWORD cbps = sizeof(PRIVILEGE_SET);
  45. PACL pACL = NULL;
  46. PSID psidAdmin = NULL;
  47. PSECURITY_DESCRIPTOR psdAdmin = NULL;
  48. PRIVILEGE_SET ps;
  49. GENERIC_MAPPING gm;
  50. SID_IDENTIFIER_AUTHORITY sia = SECURITY_NT_AUTHORITY;
  51. //
  52. // Prepare some memory
  53. //
  54. ZeroMemory(&ps, sizeof(ps));
  55. ZeroMemory(&gm, sizeof(gm));
  56. //
  57. // Get the Administrators SID
  58. //
  59. if (AllocateAndInitializeSid(&sia, 2,
  60. SECURITY_BUILTIN_DOMAIN_RID,
  61. DOMAIN_ALIAS_RID_ADMINS,
  62. 0, 0, 0, 0, 0, 0, &psidAdmin) ) {
  63. //
  64. // Get the Asministrators Security Descriptor (SD)
  65. //
  66. psdAdmin = pSetupCheckedMalloc(SECURITY_DESCRIPTOR_MIN_LENGTH);
  67. if (psdAdmin) {
  68. if(InitializeSecurityDescriptor(psdAdmin,SECURITY_DESCRIPTOR_REVISION)) {
  69. //
  70. // Compute size needed for the ACL then allocate the
  71. // memory for it
  72. //
  73. dwACLSize = sizeof(ACCESS_ALLOWED_ACE) + 8 +
  74. GetLengthSid(psidAdmin) - sizeof(DWORD);
  75. pACL = (PACL)pSetupCheckedMalloc(dwACLSize);
  76. if(pACL) {
  77. //
  78. // Initialize the new ACL
  79. //
  80. if(InitializeAcl(pACL, dwACLSize, ACL_REVISION2)) {
  81. //
  82. // Add the access-allowed ACE to the DACL
  83. //
  84. if(AddAccessAllowedAce(pACL,ACL_REVISION2,
  85. (ACCESS_READ | ACCESS_WRITE),psidAdmin)) {
  86. //
  87. // Set our DACL to the Administrator's SD
  88. //
  89. if (SetSecurityDescriptorDacl(psdAdmin, TRUE, pACL, FALSE)) {
  90. //
  91. // AccessCheck is downright picky about what is in the SD,
  92. // so set the group and owner
  93. //
  94. SetSecurityDescriptorGroup(psdAdmin,psidAdmin,FALSE);
  95. SetSecurityDescriptorOwner(psdAdmin,psidAdmin,FALSE);
  96. //
  97. // Initialize GenericMapping structure even though we
  98. // won't be using generic rights
  99. //
  100. gm.GenericRead = ACCESS_READ;
  101. gm.GenericWrite = ACCESS_WRITE;
  102. gm.GenericExecute = 0;
  103. gm.GenericAll = ACCESS_READ | ACCESS_WRITE;
  104. //
  105. // AccessCheck requires an impersonation token, so lets
  106. // indulge it
  107. //
  108. ImpersonateSelf(SecurityImpersonation);
  109. if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken)) {
  110. if (!AccessCheck(psdAdmin, hToken, ACCESS_READ, &gm,
  111. &ps,&cbps,&dwStatus,&fAdmin)) {
  112. fAdmin = FALSE;
  113. }
  114. CloseHandle(hToken);
  115. }
  116. }
  117. }
  118. }
  119. pSetupFree(pACL);
  120. }
  121. }
  122. pSetupFree(psdAdmin);
  123. }
  124. FreeSid(psidAdmin);
  125. }
  126. RevertToSelf();
  127. return(fAdmin);
  128. }
  129. #endif // !SPUTILSW
  130. BOOL
  131. pSetupDoesUserHavePrivilege(
  132. PCTSTR PrivilegeName
  133. )
  134. /*++
  135. Routine Description:
  136. This routine returns TRUE if the caller's process has
  137. the specified privilege. The privilege does not have
  138. to be currently enabled. This routine is used to indicate
  139. whether the caller has the potential to enable the privilege.
  140. Caller is NOT expected to be impersonating anyone and IS
  141. expected to be able to open their own process and process
  142. token.
  143. Arguments:
  144. Privilege - the name form of privilege ID (such as
  145. SE_SECURITY_NAME).
  146. Return Value:
  147. TRUE - Caller has the specified privilege.
  148. FALSE - Caller does not have the specified privilege.
  149. --*/
  150. {
  151. HANDLE Token;
  152. ULONG BytesRequired;
  153. PTOKEN_PRIVILEGES Privileges;
  154. BOOL b;
  155. DWORD i;
  156. LUID Luid;
  157. //
  158. // Open the process token.
  159. //
  160. if(!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&Token)) {
  161. return(FALSE);
  162. }
  163. b = FALSE;
  164. Privileges = NULL;
  165. //
  166. // Get privilege information.
  167. //
  168. if(!GetTokenInformation(Token,TokenPrivileges,NULL,0,&BytesRequired)
  169. && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  170. && (Privileges = pSetupCheckedMalloc(BytesRequired))
  171. && GetTokenInformation(Token,TokenPrivileges,Privileges,BytesRequired,&BytesRequired)
  172. && LookupPrivilegeValue(NULL,PrivilegeName,&Luid)) {
  173. //
  174. // See if we have the requested privilege
  175. //
  176. for(i=0; i<Privileges->PrivilegeCount; i++) {
  177. if((Luid.LowPart == Privileges->Privileges[i].Luid.LowPart)
  178. && (Luid.HighPart == Privileges->Privileges[i].Luid.HighPart)) {
  179. b = TRUE;
  180. break;
  181. }
  182. }
  183. }
  184. //
  185. // Clean up and return.
  186. //
  187. if(Privileges) {
  188. pSetupFree(Privileges);
  189. }
  190. CloseHandle(Token);
  191. return(b);
  192. }
  193. BOOL
  194. pSetupEnablePrivilege(
  195. IN PCTSTR PrivilegeName,
  196. IN BOOL Enable
  197. )
  198. /*++
  199. Routine Description:
  200. Enable or disable a given named privilege.
  201. Arguments:
  202. PrivilegeName - supplies the name of a system privilege.
  203. Enable - flag indicating whether to enable or disable the privilege.
  204. Return Value:
  205. Boolean value indicating whether the operation was successful.
  206. --*/
  207. {
  208. HANDLE Token;
  209. BOOL b;
  210. TOKEN_PRIVILEGES NewPrivileges;
  211. LUID Luid;
  212. if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&Token)) {
  213. return(FALSE);
  214. }
  215. if(!LookupPrivilegeValue(NULL,PrivilegeName,&Luid)) {
  216. CloseHandle(Token);
  217. return(FALSE);
  218. }
  219. NewPrivileges.PrivilegeCount = 1;
  220. NewPrivileges.Privileges[0].Luid = Luid;
  221. NewPrivileges.Privileges[0].Attributes = Enable ? SE_PRIVILEGE_ENABLED : 0;
  222. b = AdjustTokenPrivileges(
  223. Token,
  224. FALSE,
  225. &NewPrivileges,
  226. 0,
  227. NULL,
  228. NULL
  229. );
  230. CloseHandle(Token);
  231. return(b);
  232. }