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.

233 lines
6.2 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: setacl.cpp
  4. //
  5. // Module: PBSERVER.DLL
  6. //
  7. // Synopsis: Security/SID/ACL stuff for CM
  8. //
  9. // Copyright (c) 1998-2000 Microsoft Corporation
  10. //
  11. // Author: 09-Mar-2000 SumitC Created
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include <windows.h>
  15. //+----------------------------------------------------------------------------
  16. //
  17. // Func: SetAclPerms
  18. //
  19. // Desc: Sets appropriate permissions for CM/CPS's shared objects
  20. //
  21. // Args: [ppAcl] - location to return an allocated ACL
  22. //
  23. // Return: BOOL, TRUE for success, FALSE for failure
  24. //
  25. // Notes: fix for 30991: Security issue, don't use NULL DACLs.
  26. //
  27. // History: 09-Mar-2000 SumitC Created
  28. // 30-Jan-2002 SumitC added ACLs for other possible identities
  29. //
  30. //-----------------------------------------------------------------------------
  31. BOOL
  32. SetAclPerms(PACL * ppAcl)
  33. {
  34. DWORD dwError = 0;
  35. SID_IDENTIFIER_AUTHORITY siaWorld = SECURITY_WORLD_SID_AUTHORITY;
  36. SID_IDENTIFIER_AUTHORITY siaNtAuth = SECURITY_NT_AUTHORITY;
  37. PSID psidWorldSid = NULL;
  38. PSID psidLocalSystemSid = NULL;
  39. PSID psidLocalServiceSid = NULL;
  40. PSID psidNetworkServiceSid = NULL;
  41. int cbAcl;
  42. PACL pAcl = NULL;
  43. // Create a SID for all users
  44. if ( !AllocateAndInitializeSid(
  45. &siaWorld,
  46. 1,
  47. SECURITY_WORLD_RID,
  48. 0,
  49. 0,
  50. 0,
  51. 0,
  52. 0,
  53. 0,
  54. 0,
  55. &psidWorldSid))
  56. {
  57. dwError = GetLastError();
  58. goto Cleanup;
  59. }
  60. //
  61. // As an ISAPI, we can be run as LocalSystem, LocalService or NetworkService.
  62. //
  63. // The note below explains why we give permissions to ALL of these, instead
  64. // of just the identity we are currently running as.
  65. //
  66. // - perfmon accesses our shared memory object, and may hold a handle to the
  67. // object (thus keeping it alive) while PBS is recycled.
  68. // - when the user changes PBS's identity via the IIS UI, IIS recycles PBS.
  69. // - if the above 2 happened, and the shared memory object had been created
  70. // with only the ACL for the old permissions, the newly restarted PBS wouldn't
  71. // be able to access the shared memory object.
  72. //
  73. // Create a SID for Local System account
  74. if ( !AllocateAndInitializeSid(
  75. &siaNtAuth,
  76. 2,
  77. SECURITY_BUILTIN_DOMAIN_RID,
  78. DOMAIN_ALIAS_RID_ADMINS,
  79. 0,
  80. 0,
  81. 0,
  82. 0,
  83. 0,
  84. 0,
  85. &psidLocalSystemSid))
  86. {
  87. dwError = GetLastError();
  88. goto Cleanup;
  89. }
  90. // Create a SID for Local Service account
  91. if ( !AllocateAndInitializeSid(
  92. &siaNtAuth,
  93. 1,
  94. SECURITY_LOCAL_SERVICE_RID,
  95. 0,
  96. 0,
  97. 0,
  98. 0,
  99. 0,
  100. 0,
  101. 0,
  102. &psidLocalServiceSid))
  103. {
  104. dwError = GetLastError();
  105. goto Cleanup;
  106. }
  107. // Create a SID for Network Service account
  108. if ( !AllocateAndInitializeSid(
  109. &siaNtAuth,
  110. 1,
  111. SECURITY_NETWORK_SERVICE_RID,
  112. 0,
  113. 0,
  114. 0,
  115. 0,
  116. 0,
  117. 0,
  118. 0,
  119. &psidNetworkServiceSid))
  120. {
  121. dwError = GetLastError();
  122. goto Cleanup;
  123. }
  124. // Calculate the length of required ACL buffer
  125. // with 4 ACEs.
  126. cbAcl = sizeof(ACL)
  127. + 4 * sizeof(ACCESS_ALLOWED_ACE)
  128. + GetLengthSid(psidWorldSid)
  129. + GetLengthSid(psidLocalSystemSid)
  130. + GetLengthSid(psidLocalServiceSid)
  131. + GetLengthSid(psidNetworkServiceSid);
  132. pAcl = (PACL) LocalAlloc(0, cbAcl);
  133. if (NULL == pAcl)
  134. {
  135. dwError = ERROR_OUTOFMEMORY;
  136. goto Cleanup;
  137. }
  138. if ( ! InitializeAcl(pAcl, cbAcl, ACL_REVISION2))
  139. {
  140. dwError = GetLastError();
  141. goto Cleanup;
  142. }
  143. // Add ACE with EVENT_ALL_ACCESS for all users
  144. if ( ! AddAccessAllowedAce(pAcl,
  145. ACL_REVISION2,
  146. GENERIC_READ,
  147. psidWorldSid))
  148. {
  149. dwError = GetLastError();
  150. goto Cleanup;
  151. }
  152. // FUTURE-2002/03/11-SumitC Is there a way to tell IIS to disable this option (running as Local System) in the UI
  153. // Add ACE with EVENT_ALL_ACCESS for Local System
  154. if ( ! AddAccessAllowedAce(pAcl,
  155. ACL_REVISION2,
  156. GENERIC_WRITE,
  157. psidLocalSystemSid))
  158. {
  159. dwError = GetLastError();
  160. goto Cleanup;
  161. }
  162. // Add ACE with EVENT_ALL_ACCESS for Local Service
  163. if ( ! AddAccessAllowedAce(pAcl,
  164. ACL_REVISION2,
  165. GENERIC_WRITE,
  166. psidLocalServiceSid))
  167. {
  168. dwError = GetLastError();
  169. goto Cleanup;
  170. }
  171. // Add ACE with EVENT_ALL_ACCESS for Network Service
  172. if ( ! AddAccessAllowedAce(pAcl,
  173. ACL_REVISION2,
  174. GENERIC_WRITE,
  175. psidNetworkServiceSid))
  176. {
  177. dwError = GetLastError();
  178. goto Cleanup;
  179. }
  180. Cleanup:
  181. if (dwError)
  182. {
  183. if (pAcl)
  184. {
  185. LocalFree(pAcl);
  186. }
  187. }
  188. else
  189. {
  190. *ppAcl = pAcl;
  191. }
  192. if (psidWorldSid)
  193. {
  194. FreeSid(psidWorldSid);
  195. }
  196. if (psidLocalSystemSid)
  197. {
  198. FreeSid(psidLocalSystemSid);
  199. }
  200. if (psidLocalServiceSid)
  201. {
  202. FreeSid(psidLocalServiceSid);
  203. }
  204. if (psidNetworkServiceSid)
  205. {
  206. FreeSid(psidNetworkServiceSid);
  207. }
  208. return dwError ? FALSE : TRUE;
  209. }