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.

243 lines
4.8 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. BOOL
  4. IsUserAdmin(
  5. VOID
  6. )
  7. /*++
  8. Routine Description:
  9. This routine returns TRUE if the caller's process is a
  10. member of the Administrators local group.
  11. Caller is NOT expected to be impersonating anyone and IS
  12. expected to be able to open their own process and process
  13. token.
  14. Arguments:
  15. None.
  16. Return Value:
  17. TRUE - Caller has Administrators local group.
  18. FALSE - Caller does not have Administrators local group.
  19. --*/
  20. {
  21. HANDLE Token;
  22. DWORD BytesRequired;
  23. PTOKEN_GROUPS Groups;
  24. BOOL b;
  25. DWORD i;
  26. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  27. PSID AdministratorsGroup;
  28. //
  29. // On non-NT platforms the user is administrator.
  30. //
  31. if(!ISNT()) {
  32. return(TRUE);
  33. }
  34. //
  35. // Open the process token.
  36. //
  37. if(!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&Token)) {
  38. return(FALSE);
  39. }
  40. b = FALSE;
  41. Groups = NULL;
  42. //
  43. // Get group information.
  44. //
  45. if(!GetTokenInformation(Token,TokenGroups,NULL,0,&BytesRequired)
  46. && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  47. && (Groups = (PTOKEN_GROUPS)LocalAlloc(LPTR,BytesRequired))
  48. && GetTokenInformation(Token,TokenGroups,Groups,BytesRequired,&BytesRequired)) {
  49. b = AllocateAndInitializeSid(
  50. &NtAuthority,
  51. 2,
  52. SECURITY_BUILTIN_DOMAIN_RID,
  53. DOMAIN_ALIAS_RID_ADMINS,
  54. 0, 0, 0, 0, 0, 0,
  55. &AdministratorsGroup
  56. );
  57. if(b) {
  58. //
  59. // See if the user has the administrator group.
  60. //
  61. b = FALSE;
  62. for(i=0; i<Groups->GroupCount; i++) {
  63. if(EqualSid(Groups->Groups[i].Sid,AdministratorsGroup)) {
  64. b = TRUE;
  65. break;
  66. }
  67. }
  68. FreeSid(AdministratorsGroup);
  69. }
  70. }
  71. //
  72. // Clean up and return.
  73. //
  74. if(Groups) {
  75. LocalFree((HLOCAL)Groups);
  76. }
  77. CloseHandle(Token);
  78. return(b);
  79. }
  80. BOOL
  81. DoesUserHavePrivilege(
  82. PTSTR PrivilegeName
  83. )
  84. /*++
  85. Routine Description:
  86. This routine returns TRUE if the caller's process has
  87. the specified privilege. The privilege does not have
  88. to be currently enabled. This routine is used to indicate
  89. whether the caller has the potential to enable the privilege.
  90. Caller is NOT expected to be impersonating anyone and IS
  91. expected to be able to open their own process and process
  92. token.
  93. Arguments:
  94. Privilege - the name form of privilege ID (such as
  95. SE_SECURITY_NAME).
  96. Return Value:
  97. TRUE - Caller has the specified privilege.
  98. FALSE - Caller does not have the specified privilege.
  99. --*/
  100. {
  101. HANDLE Token;
  102. ULONG BytesRequired;
  103. PTOKEN_PRIVILEGES Privileges;
  104. BOOL b;
  105. DWORD i;
  106. LUID Luid;
  107. //
  108. // On non-NT platforms the user has all privileges
  109. //
  110. if(!ISNT()) {
  111. return(TRUE);
  112. }
  113. //
  114. // Open the process token.
  115. //
  116. if(!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&Token)) {
  117. return(FALSE);
  118. }
  119. b = FALSE;
  120. Privileges = NULL;
  121. //
  122. // Get privilege information.
  123. //
  124. if(!GetTokenInformation(Token,TokenPrivileges,NULL,0,&BytesRequired)
  125. && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  126. && (Privileges = (PTOKEN_PRIVILEGES)LocalAlloc(LPTR,BytesRequired))
  127. && GetTokenInformation(Token,TokenPrivileges,Privileges,BytesRequired,&BytesRequired)
  128. && LookupPrivilegeValue(NULL,PrivilegeName,&Luid)) {
  129. //
  130. // See if we have the requested privilege
  131. //
  132. for(i=0; i<Privileges->PrivilegeCount; i++) {
  133. if(!memcmp(&Luid,&Privileges->Privileges[i].Luid,sizeof(LUID))) {
  134. b = TRUE;
  135. break;
  136. }
  137. }
  138. }
  139. //
  140. // Clean up and return.
  141. //
  142. if(Privileges) {
  143. LocalFree((HLOCAL)Privileges);
  144. }
  145. CloseHandle(Token);
  146. return(b);
  147. }
  148. BOOL
  149. EnablePrivilege(
  150. IN PTSTR PrivilegeName,
  151. IN BOOL Enable
  152. )
  153. {
  154. HANDLE Token;
  155. BOOL b;
  156. TOKEN_PRIVILEGES NewPrivileges;
  157. LUID Luid;
  158. //
  159. // On non-NT platforms the user already has all privileges
  160. //
  161. if(!ISNT()) {
  162. return(TRUE);
  163. }
  164. if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&Token)) {
  165. return(FALSE);
  166. }
  167. if(!LookupPrivilegeValue(NULL,PrivilegeName,&Luid)) {
  168. CloseHandle(Token);
  169. return(FALSE);
  170. }
  171. NewPrivileges.PrivilegeCount = 1;
  172. NewPrivileges.Privileges[0].Luid = Luid;
  173. NewPrivileges.Privileges[0].Attributes = Enable ? SE_PRIVILEGE_ENABLED : 0;
  174. b = AdjustTokenPrivileges(
  175. Token,
  176. FALSE,
  177. &NewPrivileges,
  178. 0,
  179. NULL,
  180. NULL
  181. );
  182. CloseHandle(Token);
  183. return(b);
  184. }