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.

219 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. HandleRegExpandSzRegistryKeys.cpp
  5. Abstract:
  6. This DLL catches REG_EXPAND_SZ registry keys and converts them to REG_SZ by
  7. expanding the embedded environment strings.
  8. History:
  9. 04/05/2000 markder Created
  10. 10/30/2000 maonis Bug fix
  11. --*/
  12. #include "precomp.h"
  13. IMPLEMENT_SHIM_BEGIN(HandleRegExpandSzRegistryKeys)
  14. #include "ShimHookMacro.h"
  15. APIHOOK_ENUM_BEGIN
  16. APIHOOK_ENUM_ENTRY(RegQueryValueExA)
  17. APIHOOK_ENUM_ENTRY(RegQueryValueExW)
  18. APIHOOK_ENUM_END
  19. /*++
  20. Expand REG_EXPAND_SZ strings.
  21. --*/
  22. LONG
  23. APIHOOK(RegQueryValueExA)(
  24. HKEY hKey, // handle to key
  25. LPCSTR lpValueName, // value name
  26. LPDWORD lpReserved, // reserved
  27. LPDWORD lpType, // dwType buffer
  28. LPBYTE lpData, // data buffer
  29. LPDWORD lpcbData // size of data buffer
  30. )
  31. {
  32. if (lpcbData == NULL)
  33. {
  34. return ERROR_INVALID_PARAMETER;
  35. }
  36. DWORD dwType;
  37. DWORD cbPassedInBuffer = *lpcbData;
  38. LONG uRet = ORIGINAL_API(RegQueryValueExA)(hKey, lpValueName, lpReserved, &dwType, lpData, lpcbData);
  39. if (lpType) {
  40. *lpType = dwType;
  41. }
  42. if ((uRet != ERROR_SUCCESS) && (uRet != ERROR_MORE_DATA)) {
  43. return uRet;
  44. }
  45. if (dwType != REG_EXPAND_SZ) {
  46. return uRet;
  47. }
  48. // At this point all return values have been properly set.
  49. //
  50. // The type is REG_EXPAND_SZ.
  51. // Change to REG_SZ so app doesn't try to expand the string itself.
  52. //
  53. CSTRING_TRY
  54. {
  55. CString csExpand(reinterpret_cast<char *>(lpData));
  56. if (csExpand.ExpandEnvironmentStringsW() > 0)
  57. {
  58. const char * pszExpanded = csExpand.GetAnsi();
  59. DWORD cbExpandedBuffer = (strlen(pszExpanded) + 1) * sizeof(char);
  60. // Now, make sure we have enough space in the dest buffer
  61. if (lpData != NULL)
  62. {
  63. if (cbPassedInBuffer < cbExpandedBuffer)
  64. {
  65. return ERROR_MORE_DATA;
  66. }
  67. // All safe to copy into the return values.
  68. if (StringCbCopyA((char *)lpData, cbPassedInBuffer, pszExpanded) != S_OK)
  69. {
  70. // Something failed
  71. return uRet;
  72. }
  73. }
  74. // The number of bytes placed into the buffer (including null character)
  75. *lpcbData = cbExpandedBuffer;
  76. if (lpType) {
  77. *lpType = REG_SZ;
  78. }
  79. }
  80. }
  81. CSTRING_CATCH
  82. {
  83. // Do nothing, we'll return original registry values.
  84. }
  85. return uRet;
  86. }
  87. /*++
  88. Expand REG_EXPAND_SZ strings.
  89. --*/
  90. LONG
  91. APIHOOK(RegQueryValueExW)(
  92. HKEY hKey, // handle to key
  93. LPCWSTR lpValueName, // value name
  94. LPDWORD lpReserved, // reserved
  95. LPDWORD lpType, // dwType buffer
  96. LPBYTE lpData, // data buffer
  97. LPDWORD lpcbData // size of data buffer
  98. )
  99. {
  100. if (lpcbData == NULL)
  101. {
  102. return ERROR_INVALID_PARAMETER;
  103. }
  104. DWORD dwType;
  105. DWORD cbPassedInBuffer = *lpcbData;
  106. LONG uRet = ORIGINAL_API(RegQueryValueExW)(hKey, lpValueName, lpReserved, &dwType, lpData, lpcbData);
  107. if (lpType) {
  108. *lpType = dwType;
  109. }
  110. if ((uRet != ERROR_SUCCESS) && (uRet != ERROR_MORE_DATA)) {
  111. return uRet;
  112. }
  113. if (dwType != REG_EXPAND_SZ) {
  114. return uRet;
  115. }
  116. // At this point all return values have been properly set.
  117. //
  118. // The type is REG_EXPAND_SZ.
  119. // Change to REG_SZ so app doesn't try to expand the string itself.
  120. //
  121. CSTRING_TRY
  122. {
  123. CString csExpand(reinterpret_cast<WCHAR *>(lpData));
  124. if (csExpand.ExpandEnvironmentStringsW() > 0)
  125. {
  126. DWORD cbExpandedBuffer = (csExpand.GetLength() + 1) * sizeof(WCHAR);
  127. // Now, make sure we have enough space in the dest buffer
  128. if (cbPassedInBuffer < cbExpandedBuffer)
  129. {
  130. return ERROR_MORE_DATA;
  131. }
  132. // All safe to copy into the return values.
  133. if (StringCbCopyW((WCHAR*)lpData, cbPassedInBuffer, csExpand) != S_OK)
  134. {
  135. // Something failed
  136. return uRet;
  137. }
  138. // The number of bytes placed into the buffer (including null character)
  139. *lpcbData = cbExpandedBuffer;
  140. if (lpType) {
  141. *lpType = REG_SZ;
  142. }
  143. }
  144. }
  145. CSTRING_CATCH
  146. {
  147. // Do nothing, we'll return original registry values.
  148. }
  149. return uRet;
  150. }
  151. /*++
  152. Register hooked functions
  153. --*/
  154. HOOK_BEGIN
  155. APIHOOK_ENTRY(ADVAPI32.DLL, RegQueryValueExA)
  156. APIHOOK_ENTRY(ADVAPI32.DLL, RegQueryValueExW)
  157. HOOK_END
  158. IMPLEMENT_SHIM_END