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.

120 lines
2.8 KiB

  1. /*
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. ProfilesEnvStrings.cpp
  5. Abstract:
  6. This DLL hooks GetEnvironmentVariableA and ExpandEnvironmentStringsA. Any application
  7. that is looking for %USERPROFILE% will be told the location of %ALLUSERSPROFILE% instead.
  8. This shim is designed to fool install apps that use env variables obtain the users profile
  9. location.
  10. Notes:
  11. History:
  12. 08/07/2000 reinerf Created
  13. 02/28/2001 robkenny Converted to CString
  14. */
  15. #include "precomp.h"
  16. IMPLEMENT_SHIM_BEGIN(ProfilesEnvStrings)
  17. #include "ShimHookMacro.h"
  18. APIHOOK_ENUM_BEGIN
  19. APIHOOK_ENUM_ENTRY(GetEnvironmentVariableA)
  20. APIHOOK_ENUM_ENTRY(ExpandEnvironmentStringsA)
  21. APIHOOK_ENUM_END
  22. // if apps try to read the %USERPROFILE% env variable, we lie to them
  23. DWORD
  24. APIHOOK(GetEnvironmentVariableA)(
  25. LPCSTR lpName, // environment variable name
  26. LPSTR lpBuffer, // buffer for variable value
  27. DWORD nSize // size of buffer
  28. )
  29. {
  30. if (lstrcmpiA(lpName, "USERPROFILE") == 0) {
  31. LOGN(
  32. eDbgLevelInfo,
  33. "[GetEnvironmentVariableA] overriding USERPROFILE with ALLUSERSPROFILE.");
  34. return ORIGINAL_API(GetEnvironmentVariableA)("ALLUSERSPROFILE", lpBuffer, nSize);
  35. }
  36. return ORIGINAL_API(GetEnvironmentVariableA)(lpName, lpBuffer, nSize);
  37. }
  38. DWORD
  39. APIHOOK(ExpandEnvironmentStringsA)(
  40. LPCSTR lpSrc, // string with environment variables
  41. LPSTR lpDst, // string with expanded strings
  42. DWORD nSize // maximum characters in expanded string
  43. )
  44. {
  45. DWORD dwRet = 0;
  46. CSTRING_TRY
  47. {
  48. // replace UserProfile with AllUserProfile
  49. CString csEnvironments(lpSrc);
  50. csEnvironments.ReplaceI(L"%userprofile%", L"%alluserprofile%");
  51. dwRet = ORIGINAL_API(ExpandEnvironmentStringsA)(csEnvironments.GetAnsi(), lpDst, nSize);
  52. }
  53. CSTRING_CATCH
  54. {
  55. dwRet = ORIGINAL_API(ExpandEnvironmentStringsA)(lpSrc, lpDst, nSize);
  56. }
  57. return dwRet;
  58. }
  59. BOOL
  60. NOTIFY_FUNCTION(
  61. DWORD fdwReason
  62. )
  63. {
  64. if (fdwReason == DLL_PROCESS_ATTACH) {
  65. OSVERSIONINFOEX osvi = {0};
  66. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  67. if (GetVersionEx((OSVERSIONINFO*)&osvi)) {
  68. if (!((VER_SUITE_TERMINAL & osvi.wSuiteMask) &&
  69. !(VER_SUITE_SINGLEUSERTS & osvi.wSuiteMask))) {
  70. //
  71. // Only install hooks if we are not on a "Terminal Server"
  72. // (aka "Application Server") machine.
  73. //
  74. APIHOOK_ENTRY(KERNEL32.DLL, GetEnvironmentVariableA);
  75. APIHOOK_ENTRY(KERNEL32.DLL, ExpandEnvironmentStringsA);
  76. }
  77. }
  78. }
  79. return TRUE;
  80. }
  81. HOOK_BEGIN
  82. CALL_NOTIFY_FUNCTION
  83. HOOK_END
  84. IMPLEMENT_SHIM_END