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.

237 lines
6.9 KiB

  1. #include <tchar.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4. #include <objbase.h>
  5. #ifndef UNICODE
  6. #error This has to be UNICODE
  7. #endif
  8. #define ARRAYSIZE(a) (sizeof((a))/sizeof((a)[0]))
  9. int Do(int argc, wchar_t* argv[]);
  10. static BOOL fUnicode = TRUE;
  11. static SECURITY_ATTRIBUTES _sa = {0};
  12. static ACL* _pacl = NULL;
  13. static SID* _psidLocalUsers = NULL;
  14. static SECURITY_DESCRIPTOR* _psd = NULL;
  15. HRESULT _InitSecurityDescriptor();
  16. extern "C"
  17. {
  18. int __cdecl wmain(int argc, wchar_t* argv[])
  19. {
  20. return Do(argc, argv);
  21. }
  22. }
  23. VOID InstanceThread(LPVOID lpvParam)
  24. {
  25. BYTE bRequest[4096];
  26. DWORD cbBytesRead;
  27. BOOL fSuccess;
  28. HANDLE hPipe = (HANDLE)lpvParam;
  29. /* EnterCriticalSection(&g_cs);
  30. ++g_cReply;
  31. cReply = g_cReply;
  32. LeaveCriticalSection(&g_cs);*/
  33. fSuccess = ReadFile(hPipe, bRequest, sizeof(bRequest), &cbBytesRead,
  34. NULL);
  35. if (fSuccess && cbBytesRead)
  36. {
  37. // printf(TEXT("[%08u]"), cReply);
  38. if (fUnicode)
  39. {
  40. wprintf((LPWSTR)bRequest);
  41. }
  42. else
  43. {
  44. printf((LPSTR)bRequest);
  45. }
  46. }
  47. DisconnectNamedPipe(hPipe);
  48. CloseHandle(hPipe);
  49. }
  50. int Do(int argc, wchar_t* argv[])
  51. {
  52. if (argc >= 3)
  53. {
  54. TCHAR szPipeName[MAX_PATH];
  55. wsprintf(szPipeName, TEXT("\\\\%s\\pipe\\%s"), argv[1], argv[2]);
  56. if (4 == argc)
  57. {
  58. if (lstrcmpi(argv[3], TEXT("/a")))
  59. {
  60. fUnicode = FALSE;
  61. }
  62. }
  63. // The main loop creates an instance of the named pipe and
  64. // then waits for a client to connect to it. When the client
  65. // connects, a thread is created to handle communications
  66. // with that client, and the loop is repeated.
  67. do
  68. {
  69. HRESULT hres = _InitSecurityDescriptor();
  70. if (SUCCEEDED(hres))
  71. {
  72. HANDLE hPipe = CreateNamedPipe(
  73. szPipeName, // pipe name
  74. PIPE_ACCESS_DUPLEX, // read/write access
  75. PIPE_TYPE_MESSAGE | // message type pipe
  76. PIPE_READMODE_MESSAGE | // message-read mode
  77. PIPE_WAIT, // blocking mode
  78. PIPE_UNLIMITED_INSTANCES, // max. instances
  79. 256, // output buffer size
  80. 4096, // input buffer size
  81. 10 * 1000, // client time-out
  82. &_sa);
  83. if (hPipe != INVALID_HANDLE_VALUE)
  84. {
  85. // Wait for the client to connect; if it succeeds,
  86. // the function returns a nonzero value. If the function returns
  87. // zero, GetLastError returns ERROR_PIPE_CONNECTED.
  88. BOOL fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE :
  89. (GetLastError() == ERROR_PIPE_CONNECTED);
  90. if (fConnected)
  91. {
  92. DWORD dwThreadId;
  93. // Create a thread for this client.
  94. HANDLE hThread = CreateThread(
  95. NULL, // no security attribute
  96. 0, // default stack size
  97. (LPTHREAD_START_ROUTINE) InstanceThread,
  98. (LPVOID) hPipe, // thread parameter
  99. 0, // not suspended
  100. &dwThreadId); // returns thread ID
  101. if (hThread)
  102. {
  103. BOOL f = CloseHandle(hThread);
  104. }
  105. }
  106. else
  107. {
  108. // The client could not connect, so close the pipe.
  109. CloseHandle(hPipe);
  110. }
  111. }
  112. }
  113. }
  114. while (1);
  115. }
  116. else
  117. {
  118. wprintf(L"\nUsage: \n\n");
  119. wprintf(L"pipeclnt MachineName PipeName [/a]\n\n");
  120. wprintf(L" MachineName: e.g.: stephstm_dev (no leading '\\\\')\n");
  121. wprintf(L" Use '.' for local machine\n");
  122. wprintf(L" PipeName: The pipename, usually the debuggee module name\n");
  123. wprintf(L" [/a]: Treat the incoming data as ANSI (default is UNICODE)\n\n");
  124. }
  125. return 0;
  126. }
  127. HRESULT _InitSecurityDescriptor()
  128. {
  129. HRESULT hres;
  130. if (_pacl)
  131. {
  132. hres = S_OK;
  133. }
  134. else
  135. {
  136. hres = E_FAIL;
  137. SID_IDENTIFIER_AUTHORITY sidAuthNT = SECURITY_WORLD_SID_AUTHORITY;
  138. if (AllocateAndInitializeSid(&sidAuthNT, 1, SECURITY_WORLD_RID,
  139. 0, 0, 0, 0, 0, 0, 0, (void**)&_psidLocalUsers))
  140. {
  141. DWORD cbacl = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) -
  142. sizeof(DWORD/*ACCESS_ALLOWED_ACE.SidStart*/) +
  143. GetLengthSid(_psidLocalUsers);
  144. _pacl = (ACL*)LocalAlloc(LPTR, cbacl);
  145. if (_pacl)
  146. {
  147. if (InitializeAcl(_pacl, cbacl, ACL_REVISION))
  148. {
  149. if (AddAccessAllowedAce(_pacl, ACL_REVISION, FILE_ALL_ACCESS,
  150. _psidLocalUsers))
  151. {
  152. _psd = (SECURITY_DESCRIPTOR*)LocalAlloc(LPTR,
  153. sizeof(SECURITY_DESCRIPTOR));
  154. if (_psd)
  155. {
  156. if (InitializeSecurityDescriptor(_psd,
  157. SECURITY_DESCRIPTOR_REVISION))
  158. {
  159. if (SetSecurityDescriptorDacl(_psd, TRUE,
  160. _pacl, FALSE))
  161. {
  162. if (IsValidSecurityDescriptor(_psd))
  163. {
  164. _sa.nLength = sizeof(_sa);
  165. _sa.lpSecurityDescriptor = _psd;
  166. _sa.bInheritHandle = TRUE;
  167. hres = S_OK;
  168. }
  169. }
  170. }
  171. }
  172. else
  173. {
  174. hres = E_OUTOFMEMORY;
  175. }
  176. }
  177. }
  178. }
  179. else
  180. {
  181. hres = E_OUTOFMEMORY;
  182. }
  183. }
  184. if (FAILED(hres))
  185. {
  186. if (_psidLocalUsers)
  187. {
  188. FreeSid(_psidLocalUsers);
  189. }
  190. if (_pacl)
  191. {
  192. LocalFree((HLOCAL)_pacl);
  193. }
  194. if (_psd)
  195. {
  196. LocalFree((HLOCAL)_psd);
  197. }
  198. }
  199. }
  200. return hres;
  201. }