FreeRDP
Loading...
Searching...
No Matches
rdp2tcp_main.c
1
20#include <stdio.h>
21#include <winpr/assert.h>
22
23#include <winpr/file.h>
24#include <winpr/pipe.h>
25#include <winpr/thread.h>
26
27#include <freerdp/freerdp.h>
28#include <freerdp/svc.h>
29#include <freerdp/channels/rdp2tcp.h>
30
31#include <freerdp/log.h>
32#define TAG CLIENT_TAG(RDP2TCP_DVC_CHANNEL_NAME)
33
34typedef struct
35{
36 HANDLE hStdOutputRead;
37 HANDLE hStdInputWrite;
38 HANDLE hProcess;
39 HANDLE copyThread;
40 HANDLE writeComplete;
41 DWORD openHandle;
42 void* initHandle;
43 CHANNEL_ENTRY_POINTS_FREERDP_EX channelEntryPoints;
44 char buffer[16 * 1024];
45 char* commandline;
46} Plugin;
47
48static int init_external_addin(Plugin* plugin)
49{
50 int rc = -1;
51 SECURITY_ATTRIBUTES saAttr = { 0 };
52 STARTUPINFOA siStartInfo = { 0 }; /* Using ANSI type to match CreateProcessA */
53 PROCESS_INFORMATION procInfo = { 0 };
54
55 WINPR_ASSERT(plugin);
56
57 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
58 saAttr.bInheritHandle = TRUE;
59 saAttr.lpSecurityDescriptor = NULL;
60 siStartInfo.cb = sizeof(STARTUPINFO);
61 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
62 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
63
64 // Create pipes
65 if (!CreatePipe(&plugin->hStdOutputRead, &siStartInfo.hStdOutput, &saAttr, 0))
66 {
67 WLog_ERR(TAG, "stdout CreatePipe");
68 goto fail;
69 }
70
71 if (!SetHandleInformation(plugin->hStdOutputRead, HANDLE_FLAG_INHERIT, 0))
72 {
73 WLog_ERR(TAG, "stdout SetHandleInformation");
74 goto fail;
75 }
76
77 if (!CreatePipe(&siStartInfo.hStdInput, &plugin->hStdInputWrite, &saAttr, 0))
78 {
79 WLog_ERR(TAG, "stdin CreatePipe");
80 goto fail;
81 }
82
83 if (!SetHandleInformation(plugin->hStdInputWrite, HANDLE_FLAG_INHERIT, 0))
84 {
85 WLog_ERR(TAG, "stdin SetHandleInformation");
86 goto fail;
87 }
88
89 // Execute plugin
90 const ADDIN_ARGV* args = (const ADDIN_ARGV*)plugin->channelEntryPoints.pExtendedData;
91 if (!args || (args->argc < 2))
92 {
93 WLog_ERR(TAG, "missing command line options");
94 goto fail;
95 }
96
97 plugin->commandline = _strdup(args->argv[1]);
98 if (!CreateProcessA(NULL,
99 plugin->commandline, // command line
100 NULL, // process security attributes
101 NULL, // primary thread security attributes
102 TRUE, // handles are inherited
103 0, // creation flags
104 NULL, // use parent's environment
105 NULL, // use parent's current directory
106 &siStartInfo, // STARTUPINFO pointer
107 &procInfo // receives PROCESS_INFORMATION
108 ))
109 {
110 WLog_ERR(TAG, "fork for addin");
111 goto fail;
112 }
113
114 plugin->hProcess = procInfo.hProcess;
115
116 rc = 0;
117fail:
118 (void)CloseHandle(procInfo.hThread);
119 (void)CloseHandle(siStartInfo.hStdOutput);
120 (void)CloseHandle(siStartInfo.hStdInput);
121 return rc;
122}
123
124static DWORD WINAPI copyThread(void* data)
125{
126 DWORD status = WAIT_OBJECT_0;
127 Plugin* plugin = (Plugin*)data;
128 size_t const bufsize = 16ULL * 1024ULL;
129
130 WINPR_ASSERT(plugin);
131
132 while (status == WAIT_OBJECT_0)
133 {
134 (void)ResetEvent(plugin->writeComplete);
135
136 DWORD dwRead = 0;
137 char* buffer = calloc(bufsize, sizeof(char));
138
139 if (!buffer)
140 {
141 (void)fprintf(stderr, "rdp2tcp copyThread: malloc failed\n");
142 goto fail;
143 }
144
145 // if (!ReadFile(plugin->hStdOutputRead, plugin->buffer, sizeof plugin->buffer, &dwRead,
146 // NULL))
147 if (!ReadFile(plugin->hStdOutputRead, buffer, bufsize, &dwRead, NULL))
148 {
149 free(buffer);
150 goto fail;
151 }
152
153 if (plugin->channelEntryPoints.pVirtualChannelWriteEx(
154 plugin->initHandle, plugin->openHandle, buffer, dwRead, buffer) != CHANNEL_RC_OK)
155 {
156 free(buffer);
157 (void)fprintf(stderr, "rdp2tcp copyThread failed %i\n", (int)dwRead);
158 goto fail;
159 }
160
161 HANDLE handles[] = { plugin->writeComplete,
162 freerdp_abort_event(plugin->channelEntryPoints.context) };
163 status = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
164 }
165
166fail:
167 ExitThread(0);
168 return 0;
169}
170
171static void closeChannel(Plugin* plugin)
172{
173 WINPR_ASSERT(plugin);
174 WINPR_ASSERT(plugin->channelEntryPoints.pVirtualChannelCloseEx);
175 plugin->channelEntryPoints.pVirtualChannelCloseEx(plugin->initHandle, plugin->openHandle);
176}
177
178static void dataReceived(Plugin* plugin, void* pData, UINT32 dataLength, UINT32 totalLength,
179 UINT32 dataFlags)
180{
181 DWORD dwWritten = 0;
182
183 WINPR_ASSERT(plugin);
184
185 if (dataFlags & CHANNEL_FLAG_SUSPEND)
186 return;
187
188 if (dataFlags & CHANNEL_FLAG_RESUME)
189 return;
190
191 if (dataFlags & CHANNEL_FLAG_FIRST)
192 {
193 if (!WriteFile(plugin->hStdInputWrite, &totalLength, sizeof(totalLength), &dwWritten, NULL))
194 closeChannel(plugin);
195 }
196
197 if (!WriteFile(plugin->hStdInputWrite, pData, dataLength, &dwWritten, NULL))
198 closeChannel(plugin);
199}
200
201static void VCAPITYPE VirtualChannelOpenEventEx(LPVOID lpUserParam,
202 WINPR_ATTR_UNUSED DWORD openHandle, UINT event,
203 LPVOID pData, UINT32 dataLength, UINT32 totalLength,
204 UINT32 dataFlags)
205{
206 Plugin* plugin = (Plugin*)lpUserParam;
207
208 WINPR_ASSERT(plugin);
209 switch (event)
210 {
211 case CHANNEL_EVENT_DATA_RECEIVED:
212 dataReceived(plugin, pData, dataLength, totalLength, dataFlags);
213 break;
214
215 case CHANNEL_EVENT_WRITE_CANCELLED:
216 free(pData);
217 break;
218 case CHANNEL_EVENT_WRITE_COMPLETE:
219 (void)SetEvent(plugin->writeComplete);
220 free(pData);
221 break;
222 default:
223 break;
224 }
225}
226
227static void channel_terminated(Plugin* plugin)
228{
229 if (!plugin)
230 return;
231
232 if (plugin->copyThread)
233 (void)CloseHandle(plugin->copyThread);
234 if (plugin->writeComplete)
235 (void)CloseHandle(plugin->writeComplete);
236
237 (void)CloseHandle(plugin->hStdInputWrite);
238 (void)CloseHandle(plugin->hStdOutputRead);
239 TerminateProcess(plugin->hProcess, 0);
240 (void)CloseHandle(plugin->hProcess);
241 free(plugin->commandline);
242 free(plugin);
243}
244
245static void channel_initialized(Plugin* plugin)
246{
247 WINPR_ASSERT(plugin);
248 WINPR_ASSERT(!plugin->writeComplete);
249 plugin->writeComplete = CreateEvent(NULL, TRUE, FALSE, NULL);
250
251 WINPR_ASSERT(!plugin->copyThread);
252 plugin->copyThread = CreateThread(NULL, 0, copyThread, plugin, 0, NULL);
253}
254
255static VOID VCAPITYPE VirtualChannelInitEventEx(LPVOID lpUserParam, LPVOID pInitHandle, UINT event,
256 WINPR_ATTR_UNUSED LPVOID pData,
257 WINPR_ATTR_UNUSED UINT dataLength)
258{
259 Plugin* plugin = (Plugin*)lpUserParam;
260
261 WINPR_ASSERT(plugin);
262
263 switch (event)
264 {
265 case CHANNEL_EVENT_INITIALIZED:
266 channel_initialized(plugin);
267 break;
268
269 case CHANNEL_EVENT_CONNECTED:
270 WINPR_ASSERT(plugin);
271 WINPR_ASSERT(plugin->channelEntryPoints.pVirtualChannelOpenEx);
272 if (plugin->channelEntryPoints.pVirtualChannelOpenEx(
273 pInitHandle, &plugin->openHandle, RDP2TCP_DVC_CHANNEL_NAME,
274 VirtualChannelOpenEventEx) != CHANNEL_RC_OK)
275 return;
276
277 break;
278
279 case CHANNEL_EVENT_DISCONNECTED:
280 closeChannel(plugin);
281 break;
282
283 case CHANNEL_EVENT_TERMINATED:
284 channel_terminated(plugin);
285 break;
286 default:
287 break;
288 }
289}
290
291#define VirtualChannelEntryEx rdp2tcp_VirtualChannelEntryEx
292FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS pEntryPoints,
293 PVOID pInitHandle))
294{
295 CHANNEL_ENTRY_POINTS_FREERDP_EX* pEntryPointsEx =
296 (CHANNEL_ENTRY_POINTS_FREERDP_EX*)pEntryPoints;
297 WINPR_ASSERT(pEntryPointsEx);
298 WINPR_ASSERT(pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX) &&
299 pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER);
300
301 Plugin* plugin = (Plugin*)calloc(1, sizeof(Plugin));
302
303 if (!plugin)
304 return FALSE;
305
306 plugin->initHandle = pInitHandle;
307 plugin->channelEntryPoints = *pEntryPointsEx;
308
309 if (init_external_addin(plugin) < 0)
310 {
311 channel_terminated(plugin);
312 return FALSE;
313 }
314
315 CHANNEL_DEF channelDef = { 0 };
316 strncpy(channelDef.name, RDP2TCP_DVC_CHANNEL_NAME, sizeof(channelDef.name));
317 channelDef.options =
318 CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP | CHANNEL_OPTION_COMPRESS_RDP;
319
320 if (pEntryPointsEx->pVirtualChannelInitEx(plugin, NULL, pInitHandle, &channelDef, 1,
321 VIRTUAL_CHANNEL_VERSION_WIN2000,
322 VirtualChannelInitEventEx) != CHANNEL_RC_OK)
323 {
324 channel_terminated(plugin);
325 return FALSE;
326 }
327
328 return TRUE;
329}
Definition svc.h:60