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.

195 lines
3.9 KiB

  1. /*++
  2. Copyright (c) 2003 Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. FailSocket.cpp
  5. Abstract:
  6. This shim fails the socket call. (Goodnight Gracie...)
  7. Writes a message to the event log.
  8. Notes:
  9. This is a general purpose shim.
  10. History:
  11. 01/30/2003 mnikkel, rparsons Created
  12. 02/21/2003 robkenny Second attempt, fail WSAStartup
  13. --*/
  14. #include "precomp.h"
  15. IMPLEMENT_SHIM_BEGIN(FailSocket)
  16. #include "ShimHookMacro.h"
  17. #include "acmsg.h"
  18. APIHOOK_ENUM_BEGIN
  19. APIHOOK_ENUM_ENTRY(WSAStartup)
  20. APIHOOK_ENUM_ENTRY(WSACleanup)
  21. APIHOOK_ENUM_ENTRY(WSAEnumProtocolsA)
  22. APIHOOK_ENUM_ENTRY(socket)
  23. APIHOOK_ENUM_END
  24. //
  25. // Contains the name of the source from the registry.
  26. // This is passed on the command-line from the XML.
  27. // It appears in the 'Source' column in the Event Viewer.
  28. //
  29. CString* g_pcsEventSourceName = NULL;
  30. /*++
  31. Responsible for making the actual entry in the event log.
  32. --*/
  33. void
  34. MakeEventLogEntry(
  35. void
  36. )
  37. {
  38. HANDLE hEventLog = NULL;
  39. BOOL bResult;
  40. //
  41. // Get a handle to the event log on the local computer.
  42. //
  43. hEventLog = RegisterEventSource(NULL, g_pcsEventSourceName->Get());
  44. if (NULL == hEventLog) {
  45. LOGN(eDbgLevelError,
  46. "[MakeEventLogEntry] 0x%08X Failed to register event source",
  47. GetLastError());
  48. goto cleanup;
  49. }
  50. //
  51. // Write the event to the event log.
  52. //
  53. bResult = ReportEvent(hEventLog,
  54. EVENTLOG_INFORMATION_TYPE,
  55. 0,
  56. ID_SQL_PORTS_DISABLED,
  57. NULL,
  58. 0,
  59. 0,
  60. NULL,
  61. NULL);
  62. if (!bResult) {
  63. LOGN(eDbgLevelError,
  64. "[MakeEventLogEntry] 0x%08X Failed to make event log entry",
  65. GetLastError());
  66. goto cleanup;
  67. }
  68. cleanup:
  69. if (hEventLog) {
  70. DeregisterEventSource(hEventLog);
  71. hEventLog = NULL;
  72. }
  73. }
  74. // Tell the app that there are no protocols available.
  75. int
  76. APIHOOK(WSAEnumProtocolsA)(
  77. LPINT lpiProtocols,
  78. LPWSAPROTOCOL_INFO lpProtocolBuffer,
  79. LPDWORD lpdwBufferLength
  80. )
  81. {
  82. if (lpProtocolBuffer == NULL && lpiProtocols == NULL)
  83. {
  84. *lpdwBufferLength = 1; // SSnetlib.dll will allocate this much data for the struct
  85. }
  86. else
  87. {
  88. MakeEventLogEntry();
  89. }
  90. // There are not protocols available.
  91. LOGN(eDbgLevelError, "WSAEnumProtocolsA returning 0");
  92. return 0;
  93. }
  94. // Noop the call to WSAStartup, but return success
  95. int
  96. APIHOOK(WSAStartup)(
  97. WORD wVersionRequested,
  98. LPWSADATA lpWSAData
  99. )
  100. {
  101. MakeEventLogEntry();
  102. LOGN(eDbgLevelError, "WSAStartup call has been prevented");
  103. return 0;
  104. }
  105. // Since we noop WSAStartup, we must noop WSACleanup
  106. int
  107. APIHOOK(WSACleanup) (void)
  108. {
  109. return 0;
  110. }
  111. SOCKET
  112. APIHOOK(socket)(
  113. int af,
  114. int type,
  115. int protocol
  116. )
  117. {
  118. LOGN(eDbgLevelError,
  119. "Failing socket call: af = %d type = %d protocol = %d",
  120. af,
  121. type,
  122. protocol);
  123. MakeEventLogEntry();
  124. WSASetLastError(WSAENETDOWN);
  125. return INVALID_SOCKET;
  126. }
  127. /*++
  128. Register hooked functions
  129. --*/
  130. BOOL
  131. NOTIFY_FUNCTION(
  132. DWORD fdwReason
  133. )
  134. {
  135. if (fdwReason == DLL_PROCESS_ATTACH)
  136. {
  137. CSTRING_TRY
  138. {
  139. g_pcsEventSourceName = new CString(COMMAND_LINE);
  140. }
  141. CSTRING_CATCH
  142. {
  143. return FALSE;
  144. }
  145. }
  146. return TRUE;
  147. }
  148. HOOK_BEGIN
  149. CALL_NOTIFY_FUNCTION
  150. APIHOOK_ENTRY(WSOCK32.DLL, socket)
  151. APIHOOK_ENTRY(Wsock32.DLL, WSAStartup)
  152. APIHOOK_ENTRY(Wsock32.DLL, WSACleanup)
  153. APIHOOK_ENTRY(Ws2_32.DLL, WSAEnumProtocolsA)
  154. HOOK_END
  155. IMPLEMENT_SHIM_END