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.

147 lines
3.4 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. //
  29. //-----------------------------------------------------------------------------
  30. BOOL
  31. SetAclPerms(PACL * ppAcl)
  32. {
  33. DWORD dwError = 0;
  34. SID_IDENTIFIER_AUTHORITY siaWorld = SECURITY_WORLD_SID_AUTHORITY;
  35. SID_IDENTIFIER_AUTHORITY siaNtAuth = SECURITY_NT_AUTHORITY;
  36. PSID psidWorldSid = NULL;
  37. PSID psidLocalSystemSid = NULL;
  38. int cbAcl;
  39. PACL pAcl = NULL;
  40. // Create a SID for all users
  41. if ( !AllocateAndInitializeSid(
  42. &siaWorld,
  43. 1,
  44. SECURITY_WORLD_RID,
  45. 0,
  46. 0,
  47. 0,
  48. 0,
  49. 0,
  50. 0,
  51. 0,
  52. &psidWorldSid))
  53. {
  54. dwError = GetLastError();
  55. goto Cleanup;
  56. }
  57. // Create a SID for Local System account
  58. if ( !AllocateAndInitializeSid(
  59. &siaNtAuth,
  60. 2,
  61. SECURITY_BUILTIN_DOMAIN_RID,
  62. DOMAIN_ALIAS_RID_ADMINS,
  63. 0,
  64. 0,
  65. 0,
  66. 0,
  67. 0,
  68. 0,
  69. &psidLocalSystemSid))
  70. {
  71. dwError = GetLastError();
  72. goto Cleanup;
  73. }
  74. // Calculate the length of required ACL buffer
  75. // with 2 ACEs.
  76. cbAcl = sizeof(ACL)
  77. + 2 * sizeof(ACCESS_ALLOWED_ACE)
  78. + GetLengthSid(psidWorldSid)
  79. + GetLengthSid(psidLocalSystemSid);
  80. pAcl = (PACL) LocalAlloc(0, cbAcl);
  81. if (NULL == pAcl)
  82. {
  83. dwError = ERROR_OUTOFMEMORY;
  84. goto Cleanup;
  85. }
  86. if ( ! InitializeAcl(pAcl, cbAcl, ACL_REVISION2))
  87. {
  88. dwError = GetLastError();
  89. goto Cleanup;
  90. }
  91. // Add ACE with EVENT_ALL_ACCESS for all users
  92. if ( ! AddAccessAllowedAce(pAcl,
  93. ACL_REVISION2,
  94. GENERIC_READ | GENERIC_EXECUTE,
  95. psidWorldSid))
  96. {
  97. dwError = GetLastError();
  98. goto Cleanup;
  99. }
  100. // Add ACE with EVENT_ALL_ACCESS for Local System
  101. if ( ! AddAccessAllowedAce(pAcl,
  102. ACL_REVISION2,
  103. GENERIC_ALL,
  104. psidLocalSystemSid))
  105. {
  106. dwError = GetLastError();
  107. goto Cleanup;
  108. }
  109. Cleanup:
  110. if (dwError)
  111. {
  112. if (pAcl)
  113. {
  114. LocalFree(pAcl);
  115. }
  116. }
  117. else
  118. {
  119. *ppAcl = pAcl;
  120. }
  121. if (psidWorldSid)
  122. {
  123. FreeSid(psidWorldSid);
  124. }
  125. if (psidLocalSystemSid)
  126. {
  127. FreeSid(psidLocalSystemSid);
  128. }
  129. return dwError ? FALSE : TRUE;
  130. }