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.

154 lines
4.5 KiB

  1. /*++
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name:
  4. OperationsManager.cpp
  5. Abstract:
  6. The setup for OperationsManager needs to have LoadLibraryCWD applied.
  7. However, the setups name is random, so we need to DeRandomizeExeName.
  8. But, DeRandomizeExe name calls MoveFileEx to set the file to be deleted
  9. upon reboot. The setup program detects that there are pending file
  10. deletions, interprets them as an aborted install, and recommends that
  11. the user stop installation.
  12. This shim will shim RegQueryValueExA, watch for the
  13. "PendingFileRenameOperations" key, and remove any of our de-randomized exes
  14. from the return string.
  15. Notes:
  16. This is an app-specific shim.
  17. History:
  18. 05/07/2002 astritz Created
  19. --*/
  20. #include "precomp.h"
  21. IMPLEMENT_SHIM_BEGIN(OperationsManager)
  22. #include "ShimHookMacro.h"
  23. APIHOOK_ENUM_BEGIN
  24. APIHOOK_ENUM_ENTRY(RegQueryValueExA)
  25. APIHOOK_ENUM_END
  26. /*++
  27. Remove any of our de-randomized exe names from the PendingFileRenameOperations key.
  28. --*/
  29. LONG
  30. APIHOOK(RegQueryValueExA)(
  31. HKEY hKey, // handle to key
  32. LPCSTR lpValueName, // value name
  33. LPDWORD lpReserved, // reserved
  34. LPDWORD lpType, // type buffer
  35. LPBYTE lpData, // data buffer
  36. LPDWORD lpcbData // size of data buffer
  37. )
  38. {
  39. CHAR *pchBuff = NULL;
  40. LONG lRet = ORIGINAL_API(RegQueryValueExA)(hKey, lpValueName, lpReserved,
  41. lpType, lpData, lpcbData);
  42. if (ERROR_SUCCESS == lRet) {
  43. if (CompareStringA(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL),
  44. SORT_DEFAULT), NORM_IGNORECASE, lpValueName, -1,
  45. "PendingFileRenameOperations", -1) == CSTR_EQUAL) {
  46. //
  47. // Since we're only removing strings from the original data, a buffer
  48. // of the original's size will suffice, and we won't overflow it.
  49. //
  50. CHAR *pchSrc = (CHAR *) lpData;
  51. pchBuff = new CHAR [*lpcbData];
  52. if (NULL != pchBuff) {
  53. CHAR *pchDest = pchBuff;
  54. //
  55. // We want to loop through ALL the data in case there is more than
  56. // one instance of our de-randomized name in the data.
  57. //
  58. while (pchSrc <= (CHAR *)lpData + *lpcbData) {
  59. if (*pchSrc == NULL) {
  60. break;
  61. }
  62. CString csSrc(pchSrc);
  63. CString csFile;
  64. csSrc.GetLastPathComponent(csFile);
  65. if (csFile.CompareNoCase(L"MOM_SETUP_DERANDOMIZED.EXE") == 0) {
  66. // Skip this Src File.
  67. pchSrc += strlen(pchSrc) + 1;
  68. if (pchSrc > (CHAR *)lpData + *lpcbData) {
  69. goto Exit;
  70. }
  71. // Skip the Dest file as well (probably an empty string)
  72. pchSrc += strlen(pchSrc) + 1;
  73. } else {
  74. // Copy the src file.
  75. if (FAILED(StringCchCopyExA(pchDest,
  76. *lpcbData - (pchDest - pchBuff), pchSrc,
  77. &pchDest, NULL, 0))) {
  78. goto Exit;
  79. }
  80. pchSrc += strlen(pchSrc) + 1;
  81. if (pchSrc > (CHAR *)lpData + *lpcbData) {
  82. goto Exit;
  83. }
  84. // Copy the dest file.
  85. if (FAILED(StringCchCopyExA(pchDest,
  86. *lpcbData - (pchDest - pchBuff), pchSrc, &pchDest,
  87. NULL, 0))) {
  88. goto Exit;
  89. }
  90. pchSrc += strlen(pchSrc) + 1;
  91. }
  92. }
  93. // Add the extra NULL to terminate the list of strings.
  94. *pchDest++ = NULL;
  95. // Copy our buffer to the returned buffer.
  96. memcpy(lpData, pchBuff, pchDest - pchBuff);
  97. *lpcbData = pchDest - pchBuff;
  98. }
  99. }
  100. }
  101. Exit:
  102. if (NULL != pchBuff) {
  103. delete [] pchBuff;
  104. }
  105. return lRet;
  106. }
  107. /*++
  108. Register hooked functions
  109. --*/
  110. HOOK_BEGIN
  111. APIHOOK_ENTRY(ADVAPI32.DLL, RegQueryValueExA)
  112. HOOK_END
  113. IMPLEMENT_SHIM_END