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.

166 lines
4.6 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: R E G K Y S E C . H
  7. //
  8. // Contents: CRegKeySecurity class and related data types
  9. //
  10. // Notes:
  11. //
  12. // Author: ckotze 8 July 2000
  13. //
  14. //---------------------------------------------------------------------------
  15. #include <pch.h>
  16. #pragma hdrstop
  17. #include "regkysec.h"
  18. #include "trnrgsec.h"
  19. #include "regkyexp.h"
  20. //////////////////////////////////////////////////////////////////////
  21. // Construction/Destruction
  22. //////////////////////////////////////////////////////////////////////
  23. CRegKeyExplorer::CRegKeyExplorer()
  24. {
  25. }
  26. CRegKeyExplorer::~CRegKeyExplorer()
  27. {
  28. }
  29. //+---------------------------------------------------------------------------
  30. //
  31. // Function: GetRegKeyList
  32. //
  33. // Purpose: Get a list of all keys and subkeys that we are interested in.
  34. //
  35. // Arguments:
  36. //
  37. // rkeBuildFrom - and array of REGKEYS that contain the relevant
  38. // information on how to constuct the keys.
  39. //
  40. // dwNumEntries - The number of entries in the above Array.
  41. //
  42. // listRegKeyEntries - An output parameter containing a list of all
  43. // the keys that need to have their security
  44. // modified.
  45. //
  46. // Returns: An S_OK if the key was successfully opened, and error code
  47. // otherwise
  48. //
  49. // Author: ckotze 8 July 2000
  50. //
  51. // Notes:
  52. //
  53. HRESULT CRegKeyExplorer::GetRegKeyList(const REGKEYS rkeBuildFrom[], DWORD dwNumEntries, LISTREGKEYDATA &listRegKeyEntries)
  54. {
  55. REGKEYDATA rkeCurrentKey;
  56. for (DWORD i = 0; i < dwNumEntries; i++)
  57. {
  58. rkeCurrentKey.hkeyRoot = rkeBuildFrom[i].hkeyRoot;
  59. rkeCurrentKey.strKeyName = rkeBuildFrom[i].strRootKeyName;
  60. rkeCurrentKey.amMask = rkeBuildFrom[i].amMask;
  61. rkeCurrentKey.kamMask = rkeBuildFrom[i].kamMask;
  62. listRegKeyEntries.insert(listRegKeyEntries.end(), rkeCurrentKey);
  63. if (rkeBuildFrom[i].bEnumerateRelativeEntries)
  64. {
  65. EnumerateKeysAndAddToList(rkeBuildFrom[i], listRegKeyEntries);
  66. }
  67. }
  68. return S_OK;
  69. }
  70. //+---------------------------------------------------------------------------
  71. //
  72. // Function: EnumerateKeysAndAddToList
  73. //
  74. // Purpose: Enumerates all direct children and adds them and the required
  75. // permission to the list.
  76. //
  77. // Arguments:
  78. // rkeCurrent - the current key.
  79. //
  80. // listRegKeyEntries - Output parameter containing the list of keys to be
  81. // changed
  82. //
  83. // Returns: An S_OK if the key was successfully opened, and error code
  84. // otherwise
  85. //
  86. // Author: ckotze 8 July 2000
  87. //
  88. // Notes:
  89. //
  90. HRESULT CRegKeyExplorer::EnumerateKeysAndAddToList(REGKEYS rkeCurrent, LISTREGKEYDATA &listRegKeyEntries)
  91. {
  92. HKEY hkeyCurrent;
  93. DWORD dwIndex = 0;
  94. LONG lErr = ERROR_SUCCESS;
  95. LPTSTR szSubkeyName;
  96. LPTSTR szSubKeyExpandedName;
  97. DWORD cbName = MAX_PATH + 1;
  98. REGKEYDATA rkeSubKey;
  99. if (ERROR_SUCCESS == (lErr = RegOpenKeyEx(rkeCurrent.hkeyRoot, rkeCurrent.strRootKeyName, 0, KEY_READ, &hkeyCurrent)))
  100. {
  101. szSubkeyName = new TCHAR[MAX_PATH + 1];
  102. if (!szSubkeyName)
  103. {
  104. return E_OUTOFMEMORY;
  105. }
  106. szSubKeyExpandedName = new TCHAR[MAX_PATH + 1];
  107. if (!szSubKeyExpandedName)
  108. {
  109. delete[] szSubkeyName;
  110. return E_OUTOFMEMORY;
  111. }
  112. do
  113. {
  114. ZeroMemory(szSubkeyName, (MAX_PATH + 1) * sizeof(TCHAR));
  115. ZeroMemory(szSubKeyExpandedName, (MAX_PATH + 1) * sizeof(TCHAR));
  116. lErr = RegEnumKey(hkeyCurrent, dwIndex++, szSubkeyName, cbName);
  117. if (ERROR_SUCCESS == lErr)
  118. {
  119. wsprintf(szSubKeyExpandedName, rkeCurrent.strRelativeKey, szSubkeyName);
  120. rkeSubKey.hkeyRoot = rkeCurrent.hkeyRelativeRoot;
  121. rkeSubKey.strKeyName = szSubKeyExpandedName;
  122. rkeSubKey.amMask = rkeCurrent.amChildMask;
  123. rkeSubKey.kamMask = rkeCurrent.kamChildMask;
  124. listRegKeyEntries.insert(listRegKeyEntries.end(), rkeSubKey);
  125. }
  126. }
  127. while (ERROR_SUCCESS == lErr);
  128. RegCloseKey(hkeyCurrent);
  129. delete[] szSubKeyExpandedName;
  130. delete[] szSubkeyName;
  131. if (ERROR_NO_MORE_ITEMS != lErr)
  132. {
  133. return HRESULT_FROM_WIN32(lErr);
  134. }
  135. }
  136. else
  137. {
  138. return HRESULT_FROM_WIN32(lErr);
  139. }
  140. return S_OK;
  141. }