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.

154 lines
4.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: T R N R G S E C . C P P
  7. //
  8. // Contents: Atomic application of security to registry keys
  9. //
  10. //
  11. // Notes:
  12. //
  13. // Author: ckotze 10 July 2000
  14. //
  15. //----------------------------------------------------------------------------
  16. #include <pch.h>
  17. #pragma hdrstop
  18. #include <ncreg.h>
  19. #include <regkysec.h>
  20. #include <trnrgsec.h>
  21. //////////////////////////////////////////////////////////////////////
  22. // Construction/Destruction
  23. //////////////////////////////////////////////////////////////////////
  24. CTransactedRegistrySecurity::CTransactedRegistrySecurity()
  25. {
  26. m_listTransaction.clear();
  27. }
  28. CTransactedRegistrySecurity::~CTransactedRegistrySecurity()
  29. {
  30. }
  31. //+---------------------------------------------------------------------------
  32. //
  33. // Function: SetPermissionsForKeysFromList
  34. //
  35. // Purpose: Returns a HRESULT that is either S_OK or E_ABORT. E_ABORT
  36. // specifies that the transaction was cancelled and rolled back.
  37. //
  38. // Arguments:
  39. // psidUserOrGroup - The Security Identifier for the user or group that
  40. // needs have it's security changed on certain keys.
  41. // listRegKeyApply - An stl list of REGKEYDATA for the different keys and
  42. // the different permissions to set on those keys.
  43. // bGrantRights - Are we granting or revoke rights?
  44. //
  45. // Returns: S_OK if the permissions is set correctly, and error code otherwise
  46. //
  47. // Author: ckotze 10 July 2000
  48. //
  49. // Notes:
  50. //
  51. HRESULT CTransactedRegistrySecurity::SetPermissionsForKeysFromList(PCSID psidUserOrGroup, LISTREGKEYDATA& listRegKeyApply, BOOL bGrantRights)
  52. {
  53. HRESULT hr = E_FAIL;
  54. BOOL bAbort = FALSE;
  55. Assert(psidUserOrGroup != NULL);
  56. Assert(listRegKeyApply.size() > 0);
  57. for (REGKEYDATAITER i = listRegKeyApply.begin(); i != listRegKeyApply.end(); i++)
  58. {
  59. REGKEYDATA rkdInfo = *i;
  60. hr = ApplySecurityToKey(psidUserOrGroup, rkdInfo, bGrantRights);
  61. // We might return S_FALSE if we didn't change anything and we don't want to add it to the list
  62. if (S_OK == hr)
  63. {
  64. m_listTransaction.insert(m_listTransaction.end(), rkdInfo);
  65. }
  66. else if (FAILED(hr) && HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) != hr)
  67. {
  68. bAbort = TRUE;
  69. break;
  70. }
  71. }
  72. if(bAbort)
  73. {
  74. for (REGKEYDATAITER i = m_listTransaction.end(); i != m_listTransaction.begin(); i++)
  75. {
  76. REGKEYDATA rkdInfo = *i;
  77. hr = ApplySecurityToKey(psidUserOrGroup, rkdInfo, !bGrantRights);
  78. }
  79. hr = E_ABORT;
  80. }
  81. else
  82. {
  83. hr = S_OK;
  84. }
  85. m_listTransaction.clear();
  86. return hr;
  87. }
  88. //+---------------------------------------------------------------------------
  89. //
  90. // Function: SetPermissionsForKeysFromList
  91. //
  92. // Purpose: Returns a HRESULT that is either S_OK or E_ABORT. E_ABORT
  93. // specifies that the transaction was cancelled and rolled back.
  94. //
  95. // Arguments:
  96. // psidUserOrGroup - The Security Identifier for the user or group that
  97. // needs have it's security changed on certain keys.
  98. // rkdInfo - A REGKEYDATA structure for this key.
  99. //
  100. // bGrantRights - Are we granting or revoke rights?
  101. //
  102. // Returns: S_OK if the permissions is set correctly, and error code otherwise
  103. //
  104. // Author: ckotze 10 July 2000
  105. //
  106. // Notes:
  107. //
  108. HRESULT CTransactedRegistrySecurity::ApplySecurityToKey(PCSID psidUserOrGroup, const REGKEYDATA rkdInfo, const BOOL bGrantRights)
  109. {
  110. HRESULT hr = S_OK;
  111. hr = RegOpenKey(rkdInfo.hkeyRoot, rkdInfo.strKeyName.c_str());
  112. if (SUCCEEDED(hr))
  113. {
  114. hr = GetKeySecurity();
  115. if (SUCCEEDED(hr))
  116. {
  117. hr = GetSecurityDescriptorDacl();
  118. if (SUCCEEDED(hr))
  119. {
  120. if (bGrantRights)
  121. {
  122. hr = GrantRightsOnRegKey(psidUserOrGroup, rkdInfo.amMask, rkdInfo.kamMask);
  123. }
  124. else
  125. {
  126. hr = RevokeRightsOnRegKey(psidUserOrGroup, rkdInfo.amMask, rkdInfo.kamMask);
  127. }
  128. }
  129. }
  130. // we actually need the hr from the call above so we just assert here instead of returning the HRESULT
  131. Assert(SUCCEEDED(RegCloseKey()));
  132. }
  133. return hr;
  134. }