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.

265 lines
6.4 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: secdesc.c
  3. *
  4. * Copyright (c) 1991, Microsoft Corporation
  5. *
  6. * Routines that support creation and deletion of security descriptors
  7. *
  8. * History:
  9. * 02-06-92 Davidc Created.
  10. \***************************************************************************/
  11. #include "sec.h"
  12. //
  13. // Private prototypes
  14. //
  15. PVOID
  16. CreateAccessAllowedAce(
  17. PSID Sid,
  18. ACCESS_MASK AccessMask,
  19. UCHAR AceFlags,
  20. UCHAR InheritFlags
  21. );
  22. VOID
  23. DestroyAce(
  24. PVOID Ace
  25. );
  26. /***************************************************************************\
  27. * CreateSecurityDescriptor
  28. *
  29. * Creates a security descriptor containing an ACL containing the specified ACEs
  30. *
  31. * A SD created with this routine should be destroyed using
  32. * DeleteSecurityDescriptor
  33. *
  34. * Returns a pointer to the security descriptor or NULL on failure.
  35. *
  36. * 02-06-92 Davidc Created.
  37. \***************************************************************************/
  38. PSECURITY_DESCRIPTOR
  39. CreateSecurityDescriptor(
  40. PMYACE MyAce,
  41. ACEINDEX AceCount
  42. )
  43. {
  44. NTSTATUS Status;
  45. ACEINDEX AceIndex;
  46. PACCESS_ALLOWED_ACE *Ace;
  47. PACL Acl = NULL;
  48. PSECURITY_DESCRIPTOR SecurityDescriptor = NULL;
  49. ULONG LengthAces;
  50. ULONG LengthAcl;
  51. ULONG LengthSd;
  52. //
  53. // Allocate space for the ACE pointer array
  54. //
  55. Ace = (PACCESS_ALLOWED_ACE *)Alloc(sizeof(PACCESS_ALLOWED_ACE) * AceCount);
  56. if (Ace == NULL) {
  57. DbgOnlyPrint("grptoreg failed to allocated ACE array\n\r");
  58. return(NULL);
  59. }
  60. //
  61. // Create the ACEs and calculate total ACE size
  62. //
  63. LengthAces = 0;
  64. for (AceIndex=0; AceIndex < AceCount; AceIndex ++) {
  65. Ace[AceIndex] = CreateAccessAllowedAce(MyAce[AceIndex].Sid,
  66. MyAce[AceIndex].AccessMask,
  67. 0,
  68. MyAce[AceIndex].InheritFlags);
  69. if (Ace[AceIndex] == NULL) {
  70. DbgOnlyPrint("grptoreg : Failed to allocate ace\n\r");
  71. } else {
  72. LengthAces += Ace[AceIndex]->Header.AceSize;
  73. }
  74. }
  75. //
  76. // Calculate ACL and SD sizes
  77. //
  78. LengthAcl = sizeof(ACL) + LengthAces;
  79. LengthSd = SECURITY_DESCRIPTOR_MIN_LENGTH;
  80. //
  81. // Create the ACL
  82. //
  83. Acl = Alloc(LengthAcl);
  84. if (Acl != NULL) {
  85. Status = RtlCreateAcl(Acl, LengthAcl, ACL_REVISION);
  86. ASSERT(NT_SUCCESS(Status));
  87. //
  88. // Add the ACES to the ACL and destroy the ACEs
  89. //
  90. for (AceIndex = 0; AceIndex < AceCount; AceIndex ++) {
  91. if (Ace[AceIndex] != NULL) {
  92. Status = RtlAddAce(Acl, ACL_REVISION, 0, Ace[AceIndex],
  93. Ace[AceIndex]->Header.AceSize);
  94. if (!NT_SUCCESS(Status)) {
  95. DbgOnlyPrint("grptoreg : AddAce failed, status = 0x%lx\n\r", Status);
  96. }
  97. DestroyAce(Ace[AceIndex]);
  98. }
  99. }
  100. } else {
  101. DbgOnlyPrint("grptoreg : Failed to allocate ACL\n\r");
  102. }
  103. //
  104. // Free the ACE pointer array
  105. //
  106. Free(Ace);
  107. //
  108. // Create the security descriptor
  109. //
  110. SecurityDescriptor = Alloc(LengthSd);
  111. if (SecurityDescriptor != NULL) {
  112. Status = RtlCreateSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
  113. ASSERT(NT_SUCCESS(Status));
  114. //
  115. // Set the DACL on the security descriptor
  116. //
  117. Status = RtlSetDaclSecurityDescriptor(SecurityDescriptor, TRUE, Acl, FALSE);
  118. if (!NT_SUCCESS(Status)) {
  119. DbgOnlyPrint("grptoreg : SetDACLSD failed, status = 0x%lx\n\r", Status);
  120. }
  121. } else {
  122. DbgOnlyPrint("grptoreg : Failed to allocate security descriptor\n\r");
  123. }
  124. //
  125. // Return with our spoils
  126. //
  127. return(SecurityDescriptor);
  128. }
  129. /***************************************************************************\
  130. * DeleteSecurityDescriptor
  131. *
  132. * Deletes a security descriptor created using CreateSecurityDescriptor
  133. *
  134. * Returns TRUE on success, FALSE on failure
  135. *
  136. * 02-06-92 Davidc Created.
  137. \***************************************************************************/
  138. BOOL
  139. DeleteSecurityDescriptor(
  140. PSECURITY_DESCRIPTOR SecurityDescriptor
  141. )
  142. {
  143. NTSTATUS Status;
  144. PACL Acl;
  145. BOOLEAN Present;
  146. BOOLEAN Defaulted;
  147. ASSERT(SecurityDescriptor != NULL);
  148. //
  149. // Get the ACL
  150. //
  151. Status = RtlGetDaclSecurityDescriptor(SecurityDescriptor,
  152. &Present, &Acl, &Defaulted);
  153. if (NT_SUCCESS(Status)) {
  154. //
  155. // Destroy the ACL
  156. //
  157. if (Present && (Acl != NULL)) {
  158. Free(Acl);
  159. }
  160. } else {
  161. DbgOnlyPrint("grptoreg : Failed to get DACL from security descriptor being destroyed, Status = 0x%lx\n\r", Status);
  162. }
  163. //
  164. // Destroy the Security Descriptor
  165. //
  166. Free(SecurityDescriptor);
  167. return(TRUE);
  168. }
  169. /***************************************************************************\
  170. * CreateAccessAllowedAce
  171. *
  172. * Allocates memory for an ACCESS_ALLOWED_ACE and fills it in.
  173. * The memory should be freed by calling DestroyACE.
  174. *
  175. * Returns pointer to ACE on success, NULL on failure
  176. *
  177. * History:
  178. * 12-05-91 Davidc Created
  179. \***************************************************************************/
  180. PVOID
  181. CreateAccessAllowedAce(
  182. PSID Sid,
  183. ACCESS_MASK AccessMask,
  184. UCHAR AceFlags,
  185. UCHAR InheritFlags
  186. )
  187. {
  188. ULONG LengthSid = RtlLengthSid(Sid);
  189. ULONG LengthACE = sizeof(ACE_HEADER) + sizeof(ACCESS_MASK) + LengthSid;
  190. PACCESS_ALLOWED_ACE Ace;
  191. Ace = (PACCESS_ALLOWED_ACE)Alloc(LengthACE);
  192. if (Ace == NULL) {
  193. DbgOnlyPrint("grptoreg : CreateAccessAllowedAce : Failed to allocate ace\n\r");
  194. return NULL;
  195. }
  196. Ace->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  197. Ace->Header.AceSize = (USHORT)LengthACE;
  198. Ace->Header.AceFlags = AceFlags | InheritFlags;
  199. Ace->Mask = AccessMask;
  200. RtlCopySid(LengthSid, (PSID)(&(Ace->SidStart)), Sid );
  201. return(Ace);
  202. }
  203. /***************************************************************************\
  204. * DestroyAce
  205. *
  206. * Frees the memory allocate for an ACE
  207. *
  208. * History:
  209. * 12-05-91 Davidc Created
  210. \***************************************************************************/
  211. VOID
  212. DestroyAce(
  213. PVOID Ace
  214. )
  215. {
  216. Free(Ace);
  217. }