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.

130 lines
2.7 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. FailOpenFile.cpp
  5. Abstract:
  6. Force OpenFile to fail for the specified files.
  7. History:
  8. 01/31/2001 robkenny created
  9. 03/13/2001 robkenny Converted to CString
  10. 02/07/2002 astritz Converted to StrSafe
  11. --*/
  12. #include "precomp.h"
  13. #include "charvector.h"
  14. IMPLEMENT_SHIM_BEGIN(FailOpenFile)
  15. #include "ShimHookMacro.h"
  16. APIHOOK_ENUM_BEGIN
  17. APIHOOK_ENUM_ENTRY(OpenFile)
  18. APIHOOK_ENUM_END
  19. CharVector * g_FailList = NULL;
  20. HFILE
  21. APIHOOK(OpenFile)(
  22. LPCSTR lpFileName, // file name
  23. LPOFSTRUCT lpReOpenBuff, // file information
  24. UINT uStyle // action and attributes
  25. )
  26. {
  27. int i;
  28. for (i = 0; i < g_FailList->Size(); ++i)
  29. {
  30. // Compare each fail name against the end of lpFileName
  31. const char * failName = g_FailList->Get(i);
  32. size_t failNameLen = strlen(failName);
  33. size_t fileNameLen = strlen(lpFileName);
  34. if (fileNameLen >= failNameLen)
  35. {
  36. if (_strnicmp(failName, lpFileName+fileNameLen-failNameLen, failNameLen) == 0)
  37. {
  38. // Force OpenFile to fail for this filename
  39. DPFN( eDbgLevelError, "Forcing OpenFile(%s) to fail", lpFileName);
  40. return FALSE;
  41. }
  42. }
  43. }
  44. HFILE returnValue = ORIGINAL_API(OpenFile)(lpFileName, lpReOpenBuff, uStyle);
  45. return returnValue;
  46. }
  47. /*++
  48. Parse the command line, push each filename onto the end of the g_FailList.
  49. --*/
  50. BOOL ParseCommandLine(const char * cl)
  51. {
  52. g_FailList = new CharVector;
  53. if (!g_FailList)
  54. {
  55. return FALSE;
  56. }
  57. if (cl != NULL && *cl)
  58. {
  59. int argc = 0;
  60. LPSTR * argv = _CommandLineToArgvA(cl, & argc);
  61. if (argv)
  62. {
  63. for (int i = 0; i < argc; ++i)
  64. {
  65. if (!g_FailList->Append(argv[i]))
  66. {
  67. // Memory failure
  68. delete g_FailList;
  69. g_FailList = NULL;
  70. break;
  71. }
  72. DPFN( eDbgLevelSpew, "Adding %s to fail list", argv[i]);
  73. }
  74. LocalFree(argv);
  75. }
  76. }
  77. return g_FailList != NULL;
  78. }
  79. /*++
  80. Handle attach and detach
  81. --*/
  82. BOOL
  83. NOTIFY_FUNCTION(DWORD fdwReason)
  84. {
  85. if (fdwReason == DLL_PROCESS_ATTACH)
  86. {
  87. return ParseCommandLine(COMMAND_LINE);
  88. }
  89. return TRUE;
  90. }
  91. /*++
  92. Register hooked functions
  93. --*/
  94. HOOK_BEGIN
  95. CALL_NOTIFY_FUNCTION
  96. APIHOOK_ENTRY(KERNEL32.DLL, OpenFile)
  97. HOOK_END
  98. IMPLEMENT_SHIM_END