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.

179 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 2000-2002 Microsoft Corporation
  3. Module Name:
  4. CorrectCreateEventName.cpp
  5. Abstract:
  6. The \ character is not a legal character for an event.
  7. This shim will replace all \ characters with an underscore,
  8. except for Global\ or Local\ namespace tags.
  9. Notes:
  10. This is a general purpose shim.
  11. History:
  12. 07/19/1999 robkenny Created
  13. 03/15/2001 robkenny Converted to CString
  14. 02/26/2002 robkenny Security review. Was not properly handling Global\ and Local\ namespaces.
  15. Shim wasn't handling OpenEventA, making it pretty useless.
  16. --*/
  17. #include "precomp.h"
  18. IMPLEMENT_SHIM_BEGIN(CorrectCreateEventName)
  19. #include "ShimHookMacro.h"
  20. APIHOOK_ENUM_BEGIN
  21. APIHOOK_ENUM_ENTRY(CreateEventA)
  22. APIHOOK_ENUM_ENTRY(OpenEventA)
  23. APIHOOK_ENUM_END
  24. typedef HANDLE (WINAPI *_pfn_OpenEventA)(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName );
  25. BOOL CorrectEventName(CString & csBadEventName)
  26. {
  27. int nCount = 0;
  28. // Make sure we don't stomp Global\ or Local\ namespace prefixes.
  29. // Global and Local are case sensitive, and non-localized.
  30. if (csBadEventName.ComparePart(L"Global\\", 0, 7) == 0)
  31. {
  32. // This event exists in the global namespace
  33. csBadEventName.Delete(0, 7);
  34. nCount = csBadEventName.Replace(L'\\', '_');
  35. csBadEventName = L"Global\\" + csBadEventName;
  36. }
  37. else if (csBadEventName.ComparePart(L"Local\\", 0, 6) == 0)
  38. {
  39. // This event exists in the Local namespace
  40. csBadEventName.Delete(0, 6);
  41. nCount = csBadEventName.Replace(L'\\', '_');
  42. csBadEventName = L"Local\\" + csBadEventName;
  43. }
  44. else
  45. {
  46. nCount = csBadEventName.Replace(L'\\', '_');
  47. }
  48. return nCount != 0;
  49. }
  50. HANDLE
  51. APIHOOK(OpenEventA)(
  52. DWORD dwDesiredAccess, // access
  53. BOOL bInheritHandle, // inheritance option
  54. LPCSTR lpName // object name
  55. )
  56. {
  57. DPFN( eDbgLevelInfo, "OpenEventA called with event name = %s.", lpName);
  58. if (lpName)
  59. {
  60. CSTRING_TRY
  61. {
  62. const char * lpCorrectName = lpName;
  63. CString csName(lpName);
  64. if (CorrectEventName(csName))
  65. {
  66. lpCorrectName = csName.GetAnsiNIE();
  67. LOGN( eDbgLevelError,
  68. "CreateEventA corrected event name from (%s) to (%s)", lpName, lpCorrectName);
  69. }
  70. HANDLE returnValue = ORIGINAL_API(OpenEventA)(dwDesiredAccess,
  71. bInheritHandle,
  72. lpCorrectName);
  73. return returnValue;
  74. }
  75. CSTRING_CATCH
  76. {
  77. // Do nothing
  78. }
  79. }
  80. HANDLE returnValue = ORIGINAL_API(OpenEventA)(dwDesiredAccess,
  81. bInheritHandle,
  82. lpName);
  83. return returnValue;
  84. }
  85. /*+
  86. CreateEvent doesn't like event names that are similar to path names. This shim
  87. will replace all \ characters with an underscore, unless they \ is part of either
  88. the Global\ or Local\ namespace tag.
  89. --*/
  90. HANDLE
  91. APIHOOK(CreateEventA)(
  92. LPSECURITY_ATTRIBUTES lpEventAttributes, // SD
  93. BOOL bManualReset, // reset type
  94. BOOL bInitialState, // initial state
  95. LPCSTR lpName // object name
  96. )
  97. {
  98. DPFN( eDbgLevelInfo, "CreateEventA called with event name = %s.", lpName);
  99. if (lpName)
  100. {
  101. CSTRING_TRY
  102. {
  103. const char * lpCorrectName = lpName;
  104. CString csName(lpName);
  105. if (CorrectEventName(csName))
  106. {
  107. lpCorrectName = csName.GetAnsiNIE();
  108. LOGN( eDbgLevelError,
  109. "CreateEventA corrected event name from (%s) to (%s)", lpName, lpCorrectName);
  110. }
  111. HANDLE returnValue = ORIGINAL_API(CreateEventA)(lpEventAttributes,
  112. bManualReset,
  113. bInitialState,
  114. lpCorrectName);
  115. return returnValue;
  116. }
  117. CSTRING_CATCH
  118. {
  119. // Do nothing
  120. }
  121. }
  122. HANDLE returnValue = ORIGINAL_API(CreateEventA)(lpEventAttributes,
  123. bManualReset,
  124. bInitialState,
  125. lpName);
  126. return returnValue;
  127. }
  128. /*++
  129. Register hooked functions
  130. --*/
  131. HOOK_BEGIN
  132. APIHOOK_ENTRY(KERNEL32.DLL, CreateEventA)
  133. APIHOOK_ENTRY(KERNEL32.DLL, OpenEventA)
  134. HOOK_END
  135. IMPLEMENT_SHIM_END