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.

239 lines
7.1 KiB

  1. /**************************************************************************************************
  2. FILENAME: SecAttr.cpp
  3. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  4. DESCRIPTION:
  5. Security attribute related routines
  6. **************************************************************************************************/
  7. #include "stdafx.h"
  8. extern "C"{
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. }
  13. #include "Windows.h"
  14. #include <accctrl.h> // EXPLICIT_ACCESS, ACL related stuff
  15. #include <aclapi.h> // SetEntriesInAcl
  16. #include "secattr.h"
  17. BOOL
  18. ConstructSecurityAttributes(
  19. PSECURITY_ATTRIBUTES psaSecurityAttributes,
  20. SecurityAttributeType eSaType,
  21. BOOL bIncludeBackupOperator
  22. )
  23. {
  24. DWORD dwStatus;
  25. DWORD dwAccessMask = 0;
  26. BOOL bResult = TRUE;
  27. PSID psidBackupOperators = NULL;
  28. PSID psidAdministrators = NULL;
  29. PSID psidLocalSystem = NULL;
  30. PACL paclDiscretionaryAcl = NULL;
  31. SID_IDENTIFIER_AUTHORITY sidNtAuthority = SECURITY_NT_AUTHORITY;
  32. EXPLICIT_ACCESS eaExplicitAccess [3];
  33. switch (eSaType) {
  34. case esatMutex:
  35. dwAccessMask = MUTEX_ALL_ACCESS;
  36. break;
  37. case esatSemaphore:
  38. dwAccessMask = SEMAPHORE_ALL_ACCESS;
  39. break;
  40. case esatEvent:
  41. dwAccessMask = EVENT_ALL_ACCESS;
  42. break;
  43. case esatFile:
  44. dwAccessMask = FILE_ALL_ACCESS;
  45. break;
  46. default:
  47. bResult = FALSE;
  48. break;
  49. }
  50. /*
  51. ** Initialise the security descriptor.
  52. */
  53. if (bResult) {
  54. bResult = InitializeSecurityDescriptor(psaSecurityAttributes->lpSecurityDescriptor,
  55. SECURITY_DESCRIPTOR_REVISION
  56. );
  57. }
  58. if (bResult && bIncludeBackupOperator) {
  59. /*
  60. ** Create a SID for the Backup Operators group.
  61. */
  62. bResult = AllocateAndInitializeSid(&sidNtAuthority,
  63. 2,
  64. SECURITY_BUILTIN_DOMAIN_RID,
  65. DOMAIN_ALIAS_RID_BACKUP_OPS,
  66. 0, 0, 0, 0, 0, 0,
  67. &psidBackupOperators
  68. );
  69. }
  70. if (bResult) {
  71. /*
  72. ** Create a SID for the Administrators group.
  73. */
  74. bResult = AllocateAndInitializeSid(&sidNtAuthority,
  75. 2,
  76. SECURITY_BUILTIN_DOMAIN_RID,
  77. DOMAIN_ALIAS_RID_ADMINS,
  78. 0, 0, 0, 0, 0, 0,
  79. &psidAdministrators
  80. );
  81. }
  82. if (bResult) {
  83. /*
  84. ** Create a SID for the Local System.
  85. */
  86. bResult = AllocateAndInitializeSid(&sidNtAuthority,
  87. 1,
  88. SECURITY_LOCAL_SYSTEM_RID,
  89. 0, 0, 0, 0, 0, 0, 0,
  90. &psidLocalSystem
  91. );
  92. }
  93. if (bResult) {
  94. /*
  95. ** Initialize the array of EXPLICIT_ACCESS structures for an
  96. ** ACEs we are setting.
  97. **
  98. ** The first ACE allows the Backup Operators group full access
  99. ** and the second, allowa the Administrators group full
  100. ** access.
  101. */
  102. // Initialize an EXPLICIT_ACCESS structure for an ACE.
  103. // The ACE allows the Administrators group full access to the directory
  104. eaExplicitAccess[0].grfAccessPermissions = FILE_ALL_ACCESS;
  105. eaExplicitAccess[0].grfAccessMode = SET_ACCESS;
  106. eaExplicitAccess[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  107. eaExplicitAccess[0].Trustee.pMultipleTrustee = NULL;
  108. eaExplicitAccess[0].Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
  109. eaExplicitAccess[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  110. eaExplicitAccess[0].Trustee.TrusteeType = TRUSTEE_IS_USER;
  111. eaExplicitAccess[0].Trustee.ptstrName = (LPTSTR) psidLocalSystem;
  112. eaExplicitAccess[1].grfAccessPermissions = dwAccessMask;
  113. eaExplicitAccess[1].grfAccessMode = SET_ACCESS;
  114. eaExplicitAccess[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  115. eaExplicitAccess[1].Trustee.pMultipleTrustee = NULL;
  116. eaExplicitAccess[1].Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
  117. eaExplicitAccess[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  118. eaExplicitAccess[1].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
  119. eaExplicitAccess[1].Trustee.ptstrName = (LPTSTR) psidAdministrators;
  120. if (bIncludeBackupOperator) {
  121. eaExplicitAccess[2].grfAccessPermissions = dwAccessMask;
  122. eaExplicitAccess[2].grfAccessMode = SET_ACCESS;
  123. eaExplicitAccess[2].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  124. eaExplicitAccess[2].Trustee.pMultipleTrustee = NULL;
  125. eaExplicitAccess[2].Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
  126. eaExplicitAccess[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  127. eaExplicitAccess[2].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
  128. eaExplicitAccess[2].Trustee.ptstrName = (LPTSTR) psidBackupOperators;
  129. }
  130. /*
  131. ** Create a new ACL that contains the new ACEs.
  132. */
  133. dwStatus = SetEntriesInAcl(bIncludeBackupOperator ? 3 : 2,
  134. eaExplicitAccess,
  135. NULL,
  136. &paclDiscretionaryAcl);
  137. if (ERROR_SUCCESS != dwStatus) {
  138. bResult = FALSE;
  139. }
  140. }
  141. if (bResult) {
  142. /*
  143. ** Add the ACL to the security descriptor.
  144. */
  145. bResult = SetSecurityDescriptorDacl(psaSecurityAttributes->lpSecurityDescriptor,
  146. TRUE,
  147. paclDiscretionaryAcl,
  148. FALSE
  149. );
  150. }
  151. if (bResult) {
  152. paclDiscretionaryAcl = NULL;
  153. }
  154. /*
  155. ** Clean up any left over junk.
  156. */
  157. if (NULL != psidLocalSystem) {
  158. FreeSid (psidLocalSystem);
  159. psidLocalSystem = NULL;
  160. }
  161. if (NULL != psidAdministrators) {
  162. FreeSid (psidAdministrators);
  163. psidAdministrators = NULL;
  164. }
  165. if (NULL != psidBackupOperators) {
  166. FreeSid (psidBackupOperators);
  167. psidBackupOperators = NULL;
  168. }
  169. if (NULL != paclDiscretionaryAcl) {
  170. LocalFree (paclDiscretionaryAcl);
  171. paclDiscretionaryAcl = NULL;
  172. }
  173. return bResult;
  174. } /* ConstructSecurityAttributes () */
  175. VOID
  176. CleanupSecurityAttributes(
  177. PSECURITY_ATTRIBUTES psaSecurityAttributes
  178. )
  179. {
  180. BOOL bSucceeded;
  181. BOOL bDaclPresent = FALSE;
  182. BOOL bDaclDefaulted = TRUE;
  183. PACL paclDiscretionaryAcl = NULL;
  184. bSucceeded = GetSecurityDescriptorDacl (psaSecurityAttributes->lpSecurityDescriptor,
  185. &bDaclPresent,
  186. &paclDiscretionaryAcl,
  187. &bDaclDefaulted);
  188. if (bSucceeded && bDaclPresent && !bDaclDefaulted && (NULL != paclDiscretionaryAcl)) {
  189. LocalFree (paclDiscretionaryAcl);
  190. }
  191. } /* CleanupSecurityAttributes () */