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.

216 lines
7.3 KiB

  1. /****************************************************************************/
  2. // omission.cpp
  3. //
  4. // Copyright (C) 2001 Microsoft Corp.
  5. /****************************************************************************/
  6. #include "precomp.h"
  7. #include "omission.h"
  8. extern NTSTATUS TermsrvGetRegPath(IN HANDLE hKey,
  9. IN POBJECT_ATTRIBUTES pObjectAttr,
  10. IN PWCHAR pInstPath,
  11. IN ULONG ulbuflen);
  12. /*****************************************************************************
  13. *
  14. * RegPathExistsInOmissionList
  15. *
  16. * Determine whether the registry key exists in the list of registry values
  17. * defined in the omission key
  18. *
  19. * ENTRY:
  20. *
  21. * IN PWCHAR pwchKeyToCheck: Registry key to check
  22. *
  23. *
  24. * EXIT:
  25. * Returns: True if the key matches one of those in the list
  26. *
  27. ****************************************************************************/
  28. BOOL RegPathExistsInOmissionList(PWCHAR pwchKeyToCheck)
  29. {
  30. BOOL bExists = FALSE;
  31. HKEY hOmissionKey = NULL;
  32. PKEY_FULL_INFORMATION pDefKeyInfo = NULL;
  33. ULONG ultemp = 0;
  34. if (pwchKeyToCheck == NULL)
  35. return FALSE;
  36. // Get the key info
  37. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGISTRY_ENTRIES, 0, KEY_READ, &hOmissionKey) != ERROR_SUCCESS)
  38. return FALSE;
  39. // Get a buffer for the key info
  40. ultemp = sizeof(KEY_FULL_INFORMATION) + MAX_PATH * sizeof(WCHAR);
  41. pDefKeyInfo = (PKEY_FULL_INFORMATION)(RtlAllocateHeap(RtlProcessHeap(), 0, ultemp));
  42. if (pDefKeyInfo && hOmissionKey)
  43. {
  44. if NT_SUCCESS(NtQueryKey(hOmissionKey,
  45. KeyFullInformation,
  46. pDefKeyInfo,
  47. ultemp,
  48. &ultemp))
  49. {
  50. bExists = ExistsInEnumeratedKeys(hOmissionKey, pDefKeyInfo, pwchKeyToCheck);
  51. }
  52. }
  53. if (pDefKeyInfo)
  54. RtlFreeHeap(RtlProcessHeap(), 0, pDefKeyInfo);
  55. if (hOmissionKey)
  56. RegCloseKey(hOmissionKey);
  57. return bExists;
  58. }
  59. /*****************************************************************************
  60. *
  61. * HKeyExistsInOmissionList
  62. *
  63. * Determine whether the registry key exists in the list of registry values
  64. * defined in the omission key. Assumes the key is in the TERMSRV_INSTALL path
  65. *
  66. * ENTRY:
  67. *
  68. * IN PHKEY phKeyToCheck: Registry key to check
  69. *
  70. *
  71. * EXIT:
  72. * Returns: True if the key matches one of those in the list
  73. *
  74. ****************************************************************************/
  75. BOOL HKeyExistsInOmissionList(HKEY hKeyToCheck)
  76. {
  77. BOOL bExists = FALSE;
  78. ULONG ulMaxPathLength = 0;
  79. PWCHAR pUserPath = NULL;
  80. PWCHAR pUserSubPath = NULL;
  81. if (hKeyToCheck == NULL)
  82. return FALSE;
  83. // Get a buffer to hold the user's path in the registry
  84. ulMaxPathLength = MAX_PATH * sizeof(WCHAR);
  85. pUserPath = RtlAllocateHeap(RtlProcessHeap(), 0, ulMaxPathLength);
  86. if (pUserPath)
  87. {
  88. // Get the full path associated with this object attribute structure
  89. if NT_SUCCESS(TermsrvGetRegPath(hKeyToCheck, NULL, pUserPath, ulMaxPathLength))
  90. {
  91. // Skip over first part of path + backslash
  92. if (pUserPath)
  93. {
  94. if (wcslen(pUserPath) >= (sizeof(TERMSRV_INSTALL)/sizeof(WCHAR)))
  95. {
  96. pUserSubPath = pUserPath + (sizeof(TERMSRV_INSTALL)/sizeof(WCHAR)) - 1;
  97. if (pUserSubPath)
  98. {
  99. if (wcslen(pUserSubPath) >= sizeof(SOFTWARE_PATH)/sizeof(WCHAR))
  100. {
  101. //Make sure the next part of the key path is SOFTWARE_PATH
  102. if (!_wcsnicmp(pUserSubPath, SOFTWARE_PATH, sizeof(SOFTWARE_PATH)/sizeof(WCHAR) - 1))
  103. bExists = RegPathExistsInOmissionList(pUserSubPath);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. if (pUserPath)
  111. RtlFreeHeap(RtlProcessHeap(), 0, pUserPath);
  112. return bExists;
  113. }
  114. /*****************************************************************************
  115. *
  116. * ExistsInEnumeratedKeys
  117. *
  118. * Determine whether the registry key exists in the list of registry
  119. * values passed in thru the pDefKeyInfo structure
  120. *
  121. * ENTRY:
  122. *
  123. * IN HKEY hOmissionKey: Key containing the values against which to compare pwchKeyToCheck
  124. * IN PKEY_FULL_INFORMATION pDefKeyInfo: Structure containing information about
  125. * the list of values against which
  126. * to compare pwchKeyToCheck
  127. *
  128. * IN PWCHAR pwchKeyToCheck: Key to check against the list
  129. *
  130. *
  131. * EXIT:
  132. * Returns: True if the key matches one of those in the list
  133. *
  134. ****************************************************************************/
  135. BOOL ExistsInEnumeratedKeys(HKEY hOmissionKey, PKEY_FULL_INFORMATION pDefKeyInfo, PWCHAR pwchKeyToCheck)
  136. {
  137. BOOL bExists = FALSE;
  138. PKEY_VALUE_BASIC_INFORMATION pKeyValInfo = NULL;
  139. ULONG ulbufsize = 0;
  140. ULONG ulkey = 0;
  141. ULONG ultemp = 0;
  142. NTSTATUS Status;
  143. if (!hOmissionKey || !pDefKeyInfo || !pwchKeyToCheck)
  144. return FALSE;
  145. if (wcslen(pwchKeyToCheck) <= (sizeof(SOFTWARE_PATH)/sizeof(WCHAR)))
  146. return FALSE;
  147. pwchKeyToCheck += (sizeof(SOFTWARE_PATH)/sizeof(WCHAR));
  148. // Traverse the values for this key
  149. if (pDefKeyInfo->Values)
  150. {
  151. ulbufsize = sizeof(KEY_VALUE_BASIC_INFORMATION) +
  152. (pDefKeyInfo->MaxValueNameLen + 1) * sizeof(WCHAR) +
  153. pDefKeyInfo->MaxValueDataLen;
  154. pKeyValInfo = (PKEY_VALUE_BASIC_INFORMATION)(RtlAllocateHeap(RtlProcessHeap(), 0, ulbufsize));
  155. // Get a buffer to hold current value of the key (for existence check)
  156. if (pKeyValInfo)
  157. {
  158. for (ulkey = 0; ulkey < pDefKeyInfo->Values; ulkey++)
  159. {
  160. Status = NtEnumerateValueKey(hOmissionKey,
  161. ulkey,
  162. KeyValueBasicInformation,
  163. pKeyValInfo,
  164. ulbufsize,
  165. &ultemp);
  166. if ((Status == STATUS_SUCCESS) && (pwchKeyToCheck) && (pKeyValInfo->Name))
  167. {
  168. if (wcslen(pwchKeyToCheck) >= (pKeyValInfo->NameLength/sizeof(WCHAR)))
  169. {
  170. if ((pwchKeyToCheck[pKeyValInfo->NameLength/sizeof(WCHAR)] == L'\\') ||
  171. (pwchKeyToCheck[pKeyValInfo->NameLength/sizeof(WCHAR)] == 0))
  172. {
  173. if (!_wcsnicmp(pwchKeyToCheck, pKeyValInfo->Name, (pKeyValInfo->NameLength/sizeof(WCHAR))))
  174. {
  175. bExists = TRUE;
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. if (pKeyValInfo)
  184. RtlFreeHeap(RtlProcessHeap(), 0, pKeyValInfo);
  185. }
  186. return bExists;
  187. }