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.

205 lines
6.3 KiB

  1. #include "ids.h"
  2. #include "cmmn.h"
  3. #include <tchar.h>
  4. #include <io.h>
  5. #include <objbase.h>
  6. #ifndef UNICODE
  7. #error This has to be UNICODE
  8. #endif
  9. #define ARRAYSIZE(a) (sizeof((a))/sizeof((a)[0]))
  10. static SECURITY_ATTRIBUTES _sa = {0};
  11. static ACL* _pacl = NULL;
  12. static SID* _psidLocalUsers = NULL;
  13. static SECURITY_DESCRIPTOR* _psd = NULL;
  14. HRESULT _InitSecurityDescriptor();
  15. VOID InstanceThread(LPVOID lpvParam)
  16. {
  17. BYTE bRequest[4096];
  18. DWORD cbBytesRead;
  19. BOOL fSuccess;
  20. HANDLE hPipe = (HANDLE)lpvParam;
  21. fSuccess = ReadFile(hPipe, bRequest, sizeof(bRequest), &cbBytesRead,
  22. NULL);
  23. if (fSuccess && cbBytesRead)
  24. {
  25. if (!g_fPaused)
  26. {
  27. SendMessage(GetDlgItem(g_hwndDlg, IDC_EDIT1), EM_SETSEL, (WPARAM)-2,
  28. (WPARAM)-2);
  29. SendMessage(GetDlgItem(g_hwndDlg, IDC_EDIT1), EM_REPLACESEL, 0,
  30. (LPARAM)(LPWSTR)bRequest);
  31. }
  32. }
  33. DisconnectNamedPipe(hPipe);
  34. CloseHandle(hPipe);
  35. }
  36. DWORD WINAPI Do(PVOID )
  37. {
  38. TCHAR szPipeName[MAX_PATH] = TEXT("\\\\.\\pipe\\ShellService_Diagnostic");
  39. HRESULT hres = _InitSecurityDescriptor();
  40. if (SUCCEEDED(hres))
  41. {
  42. g_hEvent = CreateEvent(NULL, TRUE, TRUE, TEXT("ShellService_Diagnostic"));
  43. if (g_hEvent)
  44. {
  45. // The main loop creates an instance of the named pipe and
  46. // then waits for a client to connect to it. When the client
  47. // connects, a thread is created to handle communications
  48. // with that client, and the loop is repeated.
  49. do
  50. {
  51. HANDLE hPipe = CreateNamedPipe(
  52. szPipeName, // pipe name
  53. PIPE_ACCESS_DUPLEX, // read/write access
  54. PIPE_TYPE_MESSAGE | // message type pipe
  55. PIPE_READMODE_MESSAGE | // message-read mode
  56. PIPE_WAIT, // blocking mode
  57. PIPE_UNLIMITED_INSTANCES, // max. instances
  58. 256, // output buffer size
  59. 4096, // input buffer size
  60. 10 * 1000, // client time-out
  61. &_sa);
  62. if (hPipe != INVALID_HANDLE_VALUE)
  63. {
  64. // Wait for the client to connect; if it succeeds,
  65. // the function returns a nonzero value. If the function returns
  66. // zero, GetLastError returns ERROR_PIPE_CONNECTED.
  67. BOOL fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE :
  68. (GetLastError() == ERROR_PIPE_CONNECTED);
  69. if (fConnected)
  70. {
  71. DWORD dwThreadId;
  72. // Create a thread for this client.
  73. HANDLE hThread = CreateThread(
  74. NULL, // no security attribute
  75. 0, // default stack size
  76. (LPTHREAD_START_ROUTINE) InstanceThread,
  77. (LPVOID) hPipe, // thread parameter
  78. 0, // not suspended
  79. &dwThreadId); // returns thread ID
  80. if (hThread)
  81. {
  82. CloseHandle(hThread);
  83. }
  84. }
  85. else
  86. {
  87. // The client could not connect, so close the pipe.
  88. CloseHandle(hPipe);
  89. }
  90. }
  91. }
  92. #pragma warning(push)
  93. #pragma warning(disable : 4127)
  94. while (1);
  95. #pragma warning(pop)
  96. }
  97. }
  98. return 0;
  99. }
  100. HRESULT _InitSecurityDescriptor()
  101. {
  102. HRESULT hres;
  103. if (_pacl)
  104. {
  105. hres = S_OK;
  106. }
  107. else
  108. {
  109. hres = E_FAIL;
  110. SID_IDENTIFIER_AUTHORITY sidAuthNT = SECURITY_WORLD_SID_AUTHORITY;
  111. if (AllocateAndInitializeSid(&sidAuthNT, 1, SECURITY_WORLD_RID,
  112. 0, 0, 0, 0, 0, 0, 0, (void**)&_psidLocalUsers))
  113. {
  114. DWORD cbacl = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) -
  115. sizeof(DWORD/*ACCESS_ALLOWED_ACE.SidStart*/) +
  116. GetLengthSid(_psidLocalUsers);
  117. _pacl = (ACL*)LocalAlloc(LPTR, cbacl);
  118. if (_pacl)
  119. {
  120. if (InitializeAcl(_pacl, cbacl, ACL_REVISION))
  121. {
  122. if (AddAccessAllowedAce(_pacl, ACL_REVISION, FILE_ALL_ACCESS,
  123. _psidLocalUsers))
  124. {
  125. _psd = (SECURITY_DESCRIPTOR*)LocalAlloc(LPTR,
  126. sizeof(SECURITY_DESCRIPTOR));
  127. if (_psd)
  128. {
  129. if (InitializeSecurityDescriptor(_psd,
  130. SECURITY_DESCRIPTOR_REVISION))
  131. {
  132. if (SetSecurityDescriptorDacl(_psd, TRUE,
  133. _pacl, FALSE))
  134. {
  135. if (IsValidSecurityDescriptor(_psd))
  136. {
  137. _sa.nLength = sizeof(_sa);
  138. _sa.lpSecurityDescriptor = _psd;
  139. _sa.bInheritHandle = TRUE;
  140. hres = S_OK;
  141. }
  142. }
  143. }
  144. }
  145. else
  146. {
  147. hres = E_OUTOFMEMORY;
  148. }
  149. }
  150. }
  151. }
  152. else
  153. {
  154. hres = E_OUTOFMEMORY;
  155. }
  156. }
  157. if (FAILED(hres))
  158. {
  159. if (_psidLocalUsers)
  160. {
  161. FreeSid(_psidLocalUsers);
  162. }
  163. if (_pacl)
  164. {
  165. LocalFree((HLOCAL)_pacl);
  166. }
  167. if (_psd)
  168. {
  169. LocalFree((HLOCAL)_psd);
  170. }
  171. }
  172. }
  173. return hres;
  174. }