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.

171 lines
6.0 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: MemberOfGroup.cpp
  4. //
  5. // Module: Common Code
  6. //
  7. // Synopsis: Implements the function IsMemberOfGroup (plus accessor functions).
  8. //
  9. // Copyright (c) 2002 Microsoft Corporation
  10. //
  11. // Author: SumitC Created 26-Jan-2002
  12. //
  13. //-----------------------------------------------------------------------------
  14. //+----------------------------------------------------------------------------
  15. //
  16. // Function: IsMemberOfGroup
  17. //
  18. // Synopsis: This function return TRUE if the current user is a member of
  19. // the passed and FALSE passed in Group RID.
  20. //
  21. // Arguments: DWORD dwGroupRID -- the RID of the group to check membership of
  22. // BOOL bUseBuiltinDomainRid -- whether the SECURITY_BUILTIN_DOMAIN_RID
  23. // RID should be used to build the Group
  24. // SID
  25. //
  26. // Returns: BOOL - TRUE if the user is a member of the specified group
  27. //
  28. // History: quintinb Shamelessly stolen from MSDN 02/19/98
  29. // quintinb Reworked and renamed 06/18/99
  30. // to apply to more than just Admins
  31. // quintinb Rewrote to use CheckTokenMemberShip 08/18/99
  32. // since the MSDN method was no longer
  33. // correct on NT5 -- 389229
  34. // tomkel Taken from cmstp and modified for use 05/09/2001
  35. // in cmdial
  36. // sumitc Made common code 01/26/2002
  37. //
  38. //+----------------------------------------------------------------------------
  39. BOOL IsMemberOfGroup(DWORD dwGroupRID, BOOL bUseBuiltinDomainRid)
  40. {
  41. PSID psidGroup = NULL;
  42. SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
  43. BOOL bSuccess = FALSE;
  44. if (OS_NT5)
  45. {
  46. //
  47. // Make a SID for the Group we are checking for, Note that we if we need the Built
  48. // in Domain RID (for Groups like Administrators, PowerUsers, Users, etc)
  49. // then we will have two entries to pass to AllocateAndInitializeSid. Otherwise,
  50. // (for groups like Authenticated Users) we will only have one.
  51. //
  52. BYTE byNum;
  53. DWORD dwFirstRID;
  54. DWORD dwSecondRID;
  55. if (bUseBuiltinDomainRid)
  56. {
  57. byNum = 2;
  58. dwFirstRID = SECURITY_BUILTIN_DOMAIN_RID;
  59. dwSecondRID = dwGroupRID;
  60. }
  61. else
  62. {
  63. byNum = 1;
  64. dwFirstRID = dwGroupRID;
  65. dwSecondRID = 0;
  66. }
  67. if (AllocateAndInitializeSid(&siaNtAuthority, byNum, dwFirstRID, dwSecondRID,
  68. 0, 0, 0, 0, 0, 0, &psidGroup))
  69. {
  70. //
  71. // Now we need to dynamically load the CheckTokenMemberShip API from
  72. // advapi32.dll since it is a Win2k only API.
  73. //
  74. // some modules using this may have advapi32 loaded already...
  75. HMODULE hAdvapi = GetModuleHandleA("advapi32.dll");
  76. if (NULL == hAdvapi)
  77. {
  78. // ... if they don't, load it.
  79. hAdvapi = LoadLibraryExA("advapi32.dll", NULL, 0);
  80. }
  81. if (hAdvapi)
  82. {
  83. typedef BOOL (WINAPI *pfnCheckTokenMembershipSpec)(HANDLE, PSID, PBOOL);
  84. pfnCheckTokenMembershipSpec pfnCheckTokenMembership;
  85. pfnCheckTokenMembership = (pfnCheckTokenMembershipSpec)GetProcAddress(hAdvapi, "CheckTokenMembership");
  86. if (pfnCheckTokenMembership)
  87. {
  88. //
  89. // Check to see if the user is actually a member of the group in question
  90. //
  91. if (!(pfnCheckTokenMembership)(NULL, psidGroup, &bSuccess))
  92. {
  93. bSuccess = FALSE;
  94. CMASSERTMSG(FALSE, TEXT("CheckTokenMemberShip Failed."));
  95. }
  96. }
  97. else
  98. {
  99. CMASSERTMSG(FALSE, TEXT("IsMemberOfGroup -- GetProcAddress failed for CheckTokenMemberShip"));
  100. }
  101. }
  102. else
  103. {
  104. CMASSERTMSG(FALSE, TEXT("IsMemberOfGroup -- Unable to get the module handle for advapi32.dll"));
  105. }
  106. FreeSid (psidGroup);
  107. if (hAdvapi)
  108. {
  109. FreeLibrary(hAdvapi);
  110. }
  111. }
  112. }
  113. return bSuccess;
  114. }
  115. //+----------------------------------------------------------------------------
  116. //
  117. // Function: IsAdmin
  118. //
  119. // Synopsis: Check to see if the user is a member of the Administrators group
  120. // or not.
  121. //
  122. // Arguments: None
  123. //
  124. // Returns: BOOL - TRUE if the current user is an Administrator
  125. //
  126. // History: quintinb Created Header 8/18/99
  127. // tomkel Taken from cmstp 05/09/2001
  128. // sumitc Made common code 01/26/2002
  129. //
  130. //+----------------------------------------------------------------------------
  131. BOOL IsAdmin(VOID)
  132. {
  133. return IsMemberOfGroup(DOMAIN_ALIAS_RID_ADMINS, TRUE); // TRUE == bUseBuiltinDomainRid
  134. }
  135. //+----------------------------------------------------------------------------
  136. //
  137. // Function: IsAuthenticatedUser
  138. //
  139. // Synopsis: Check to see if the current user is a member of the
  140. // Authenticated Users group.
  141. //
  142. // Arguments: None
  143. //
  144. // Returns: BOOL - TRUE if the current user is a member of the
  145. // Authenticated Users group.
  146. //
  147. // History: quintinb Created Header 8/18/99
  148. // sumitc Made common code 01/26/2002
  149. //
  150. //+----------------------------------------------------------------------------
  151. BOOL IsAuthenticatedUser(void)
  152. {
  153. return IsMemberOfGroup(SECURITY_AUTHENTICATED_USER_RID, FALSE); // FALSE == bUseBuiltinDomainRid
  154. }