/src/FreeRDP/channels/rdp2tcp/client/rdp2tcp_main.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * rdp2tcp Virtual Channel Extension |
4 | | * |
5 | | * Copyright 2017 Artur Zaprzala |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
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/utils/warnings.h> |
32 | | |
33 | | #include <freerdp/log.h> |
34 | 0 | #define TAG CLIENT_TAG(RDP2TCP_DVC_CHANNEL_NAME) |
35 | | |
36 | | typedef struct |
37 | | { |
38 | | HANDLE hStdOutputRead; |
39 | | HANDLE hStdInputWrite; |
40 | | HANDLE hProcess; |
41 | | HANDLE copyThread; |
42 | | HANDLE writeComplete; |
43 | | HANDLE writeTerminated; |
44 | | DWORD openHandle; |
45 | | void* initHandle; |
46 | | CHANNEL_ENTRY_POINTS_FREERDP_EX channelEntryPoints; |
47 | | char buffer[16 * 1024]; |
48 | | char* commandline; |
49 | | UINT32 totalLength; |
50 | | } Plugin; |
51 | | |
52 | | static int init_external_addin(Plugin* plugin) |
53 | 0 | { |
54 | 0 | int rc = -1; |
55 | 0 | SECURITY_ATTRIBUTES saAttr = WINPR_C_ARRAY_INIT; |
56 | 0 | STARTUPINFOA siStartInfo = WINPR_C_ARRAY_INIT; /* Using ANSI type to match CreateProcessA */ |
57 | 0 | PROCESS_INFORMATION procInfo = WINPR_C_ARRAY_INIT; |
58 | |
|
59 | 0 | WINPR_ASSERT(plugin); |
60 | |
|
61 | 0 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
62 | 0 | saAttr.bInheritHandle = TRUE; |
63 | 0 | saAttr.lpSecurityDescriptor = nullptr; |
64 | 0 | siStartInfo.cb = sizeof(STARTUPINFO); |
65 | 0 | siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
66 | 0 | siStartInfo.dwFlags = STARTF_USESTDHANDLES; |
67 | | |
68 | | // Create pipes |
69 | 0 | if (!CreatePipe(&plugin->hStdOutputRead, &siStartInfo.hStdOutput, &saAttr, 0)) |
70 | 0 | { |
71 | 0 | WLog_ERR(TAG, "stdout CreatePipe"); |
72 | 0 | goto fail; |
73 | 0 | } |
74 | | |
75 | 0 | if (!SetHandleInformation(plugin->hStdOutputRead, HANDLE_FLAG_INHERIT, 0)) |
76 | 0 | { |
77 | 0 | WLog_ERR(TAG, "stdout SetHandleInformation"); |
78 | 0 | goto fail; |
79 | 0 | } |
80 | | |
81 | 0 | if (!CreatePipe(&siStartInfo.hStdInput, &plugin->hStdInputWrite, &saAttr, 0)) |
82 | 0 | { |
83 | 0 | WLog_ERR(TAG, "stdin CreatePipe"); |
84 | 0 | goto fail; |
85 | 0 | } |
86 | | |
87 | 0 | if (!SetHandleInformation(plugin->hStdInputWrite, HANDLE_FLAG_INHERIT, 0)) |
88 | 0 | { |
89 | 0 | WLog_ERR(TAG, "stdin SetHandleInformation"); |
90 | 0 | goto fail; |
91 | 0 | } |
92 | | |
93 | | // Execute plugin |
94 | 0 | const ADDIN_ARGV* args = (const ADDIN_ARGV*)plugin->channelEntryPoints.pExtendedData; |
95 | 0 | if (!args || (args->argc < 2)) |
96 | 0 | { |
97 | 0 | WLog_ERR(TAG, "missing command line options"); |
98 | 0 | goto fail; |
99 | 0 | } |
100 | | |
101 | 0 | plugin->commandline = _strdup(args->argv[1]); |
102 | 0 | if (!CreateProcessA(nullptr, |
103 | 0 | plugin->commandline, // command line |
104 | 0 | nullptr, // process security attributes |
105 | 0 | nullptr, // primary thread security attributes |
106 | 0 | TRUE, // handles are inherited |
107 | 0 | 0, // creation flags |
108 | 0 | nullptr, // use parent's environment |
109 | 0 | nullptr, // use parent's current directory |
110 | 0 | &siStartInfo, // STARTUPINFO pointer |
111 | 0 | &procInfo // receives PROCESS_INFORMATION |
112 | 0 | )) |
113 | 0 | { |
114 | 0 | WLog_ERR(TAG, "fork for addin"); |
115 | 0 | goto fail; |
116 | 0 | } |
117 | | |
118 | 0 | plugin->hProcess = procInfo.hProcess; |
119 | |
|
120 | 0 | rc = 0; |
121 | 0 | fail: |
122 | 0 | (void)CloseHandle(procInfo.hThread); |
123 | 0 | (void)CloseHandle(siStartInfo.hStdOutput); |
124 | 0 | (void)CloseHandle(siStartInfo.hStdInput); |
125 | 0 | return rc; |
126 | 0 | } |
127 | | |
128 | | static DWORD WINAPI copyThread(void* data) |
129 | 0 | { |
130 | 0 | DWORD status = WAIT_OBJECT_0; |
131 | 0 | Plugin* plugin = (Plugin*)data; |
132 | 0 | size_t const bufsize = 16ULL * 1024ULL; |
133 | |
|
134 | 0 | WINPR_ASSERT(plugin); |
135 | |
|
136 | 0 | while (status == WAIT_OBJECT_0) |
137 | 0 | { |
138 | 0 | (void)ResetEvent(plugin->writeComplete); |
139 | |
|
140 | 0 | DWORD dwRead = 0; |
141 | 0 | char* buffer = calloc(bufsize, sizeof(char)); |
142 | |
|
143 | 0 | if (!buffer) |
144 | 0 | { |
145 | 0 | (void)fprintf(stderr, "rdp2tcp copyThread: malloc failed\n"); |
146 | 0 | goto fail; |
147 | 0 | } |
148 | | |
149 | | // if (!ReadFile(plugin->hStdOutputRead, plugin->buffer, sizeof plugin->buffer, &dwRead, |
150 | | // nullptr)) |
151 | 0 | if (!ReadFile(plugin->hStdOutputRead, buffer, bufsize, &dwRead, nullptr)) |
152 | 0 | { |
153 | 0 | free(buffer); |
154 | 0 | goto fail; |
155 | 0 | } |
156 | | |
157 | 0 | if (plugin->channelEntryPoints.pVirtualChannelWriteEx( |
158 | 0 | plugin->initHandle, plugin->openHandle, buffer, dwRead, buffer) != CHANNEL_RC_OK) |
159 | 0 | { |
160 | 0 | free(buffer); |
161 | 0 | (void)fprintf(stderr, "rdp2tcp copyThread failed %i\n", (int)dwRead); |
162 | 0 | goto fail; |
163 | 0 | } |
164 | | |
165 | 0 | HANDLE handles[] = { plugin->writeComplete, |
166 | 0 | freerdp_abort_event(plugin->channelEntryPoints.context), |
167 | 0 | plugin->writeTerminated }; |
168 | 0 | status = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE); |
169 | 0 | } |
170 | | |
171 | 0 | fail: |
172 | 0 | ExitThread(0); |
173 | 0 | return 0; |
174 | 0 | } |
175 | | |
176 | | static void closeChannel(Plugin* plugin) |
177 | 0 | { |
178 | 0 | WINPR_ASSERT(plugin); |
179 | 0 | WINPR_ASSERT(plugin->channelEntryPoints.pVirtualChannelCloseEx); |
180 | 0 | plugin->channelEntryPoints.pVirtualChannelCloseEx(plugin->initHandle, plugin->openHandle); |
181 | 0 | } |
182 | | |
183 | | static void dataReceived(Plugin* plugin, void* pData, UINT32 dataLength, UINT32 totalLength, |
184 | | UINT32 dataFlags) |
185 | 0 | { |
186 | 0 | DWORD dwWritten = 0; |
187 | |
|
188 | 0 | WINPR_ASSERT(plugin); |
189 | |
|
190 | 0 | if (dataFlags & CHANNEL_FLAG_SUSPEND) |
191 | 0 | return; |
192 | | |
193 | 0 | if (dataFlags & CHANNEL_FLAG_RESUME) |
194 | 0 | return; |
195 | | |
196 | 0 | if (dataFlags & CHANNEL_FLAG_FIRST) |
197 | 0 | { |
198 | 0 | plugin->totalLength = totalLength; |
199 | 0 | if (!WriteFile(plugin->hStdInputWrite, &totalLength, sizeof(totalLength), &dwWritten, |
200 | 0 | nullptr)) |
201 | 0 | closeChannel(plugin); |
202 | 0 | } |
203 | |
|
204 | 0 | if (!WriteFile(plugin->hStdInputWrite, pData, dataLength, &dwWritten, nullptr)) |
205 | 0 | closeChannel(plugin); |
206 | 0 | } |
207 | | |
208 | | static void VCAPITYPE VirtualChannelOpenEventEx(LPVOID lpUserParam, |
209 | | WINPR_ATTR_UNUSED DWORD openHandle, UINT event, |
210 | | LPVOID pData, UINT32 dataLength, UINT32 totalLength, |
211 | | UINT32 dataFlags) |
212 | 0 | { |
213 | 0 | Plugin* plugin = (Plugin*)lpUserParam; |
214 | |
|
215 | 0 | WINPR_ASSERT(plugin); |
216 | 0 | switch (event) |
217 | 0 | { |
218 | 0 | case CHANNEL_EVENT_DATA_RECEIVED: |
219 | 0 | dataReceived(plugin, pData, dataLength, totalLength, dataFlags); |
220 | 0 | break; |
221 | | |
222 | 0 | case CHANNEL_EVENT_WRITE_CANCELLED: |
223 | 0 | free(pData); |
224 | 0 | break; |
225 | 0 | case CHANNEL_EVENT_WRITE_COMPLETE: |
226 | 0 | (void)SetEvent(plugin->writeComplete); |
227 | 0 | free(pData); |
228 | 0 | break; |
229 | 0 | default: |
230 | 0 | break; |
231 | 0 | } |
232 | 0 | } |
233 | | |
234 | | static void channel_terminated(Plugin* plugin) |
235 | 0 | { |
236 | 0 | if (!plugin) |
237 | 0 | return; |
238 | | |
239 | 0 | if (plugin->writeTerminated) |
240 | 0 | SetEvent(plugin->writeTerminated); |
241 | |
|
242 | 0 | if (plugin->copyThread) |
243 | 0 | { |
244 | 0 | (void)WaitForSingleObject(plugin->copyThread, INFINITE); |
245 | 0 | (void)CloseHandle(plugin->copyThread); |
246 | 0 | } |
247 | 0 | if (plugin->writeComplete) |
248 | 0 | (void)CloseHandle(plugin->writeComplete); |
249 | 0 | if (plugin->writeTerminated) |
250 | 0 | (void)CloseHandle(plugin->writeTerminated); |
251 | |
|
252 | 0 | (void)CloseHandle(plugin->hStdInputWrite); |
253 | 0 | (void)CloseHandle(plugin->hStdOutputRead); |
254 | 0 | TerminateProcess(plugin->hProcess, 0); |
255 | 0 | (void)CloseHandle(plugin->hProcess); |
256 | 0 | free(plugin->commandline); |
257 | 0 | free(plugin); |
258 | 0 | } |
259 | | |
260 | | static void channel_initialized(Plugin* plugin) |
261 | 0 | { |
262 | 0 | WINPR_ASSERT(plugin); |
263 | 0 | WINPR_ASSERT(!plugin->writeComplete); |
264 | 0 | plugin->writeComplete = CreateEvent(nullptr, TRUE, FALSE, nullptr); |
265 | 0 | WINPR_ASSERT(plugin->writeComplete); |
266 | |
|
267 | 0 | WINPR_ASSERT(!plugin->writeTerminated); |
268 | 0 | plugin->writeTerminated = CreateEvent(nullptr, TRUE, FALSE, nullptr); |
269 | 0 | WINPR_ASSERT(plugin->writeTerminated); |
270 | |
|
271 | 0 | WINPR_ASSERT(!plugin->copyThread); |
272 | 0 | plugin->copyThread = CreateThread(nullptr, 0, copyThread, plugin, 0, nullptr); |
273 | 0 | WINPR_ASSERT(plugin->copyThread); |
274 | 0 | } |
275 | | |
276 | | static VOID VCAPITYPE VirtualChannelInitEventEx(LPVOID lpUserParam, LPVOID pInitHandle, UINT event, |
277 | | WINPR_ATTR_UNUSED LPVOID pData, |
278 | | WINPR_ATTR_UNUSED UINT dataLength) |
279 | 0 | { |
280 | 0 | Plugin* plugin = (Plugin*)lpUserParam; |
281 | |
|
282 | 0 | WINPR_ASSERT(plugin); |
283 | |
|
284 | 0 | switch (event) |
285 | 0 | { |
286 | 0 | case CHANNEL_EVENT_INITIALIZED: |
287 | 0 | channel_initialized(plugin); |
288 | 0 | break; |
289 | | |
290 | 0 | case CHANNEL_EVENT_CONNECTED: |
291 | 0 | WINPR_ASSERT(plugin); |
292 | 0 | WINPR_ASSERT(plugin->channelEntryPoints.pVirtualChannelOpenEx); |
293 | 0 | if (plugin->channelEntryPoints.pVirtualChannelOpenEx( |
294 | 0 | pInitHandle, &plugin->openHandle, RDP2TCP_DVC_CHANNEL_NAME, |
295 | 0 | VirtualChannelOpenEventEx) != CHANNEL_RC_OK) |
296 | 0 | return; |
297 | | |
298 | 0 | break; |
299 | | |
300 | 0 | case CHANNEL_EVENT_DISCONNECTED: |
301 | 0 | closeChannel(plugin); |
302 | 0 | break; |
303 | | |
304 | 0 | case CHANNEL_EVENT_TERMINATED: |
305 | 0 | channel_terminated(plugin); |
306 | 0 | break; |
307 | 0 | default: |
308 | 0 | break; |
309 | 0 | } |
310 | 0 | } |
311 | | |
312 | | #define VirtualChannelEntryEx rdp2tcp_VirtualChannelEntryEx |
313 | | FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS_EX pEntryPoints, |
314 | | PVOID pInitHandle)) |
315 | 0 | { |
316 | 0 | CHANNEL_ENTRY_POINTS_FREERDP_EX* pEntryPointsEx = |
317 | 0 | (CHANNEL_ENTRY_POINTS_FREERDP_EX*)pEntryPoints; |
318 | 0 | WINPR_ASSERT(pEntryPointsEx); |
319 | 0 | WINPR_ASSERT(pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX) && |
320 | 0 | pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER); |
321 | |
|
322 | 0 | freerdp_warn_unmaintained(WLog_Get(TAG), "CHANNEL_RDP2TCP_CLIENT"); |
323 | |
|
324 | 0 | Plugin* plugin = (Plugin*)calloc(1, sizeof(Plugin)); |
325 | |
|
326 | 0 | if (!plugin) |
327 | 0 | return FALSE; |
328 | | |
329 | 0 | plugin->initHandle = pInitHandle; |
330 | 0 | plugin->channelEntryPoints = *pEntryPointsEx; |
331 | |
|
332 | 0 | if (init_external_addin(plugin) < 0) |
333 | 0 | { |
334 | 0 | channel_terminated(plugin); |
335 | 0 | return FALSE; |
336 | 0 | } |
337 | | |
338 | 0 | CHANNEL_DEF channelDef = WINPR_C_ARRAY_INIT; |
339 | 0 | strncpy(channelDef.name, RDP2TCP_DVC_CHANNEL_NAME, sizeof(channelDef.name)); |
340 | 0 | channelDef.options = |
341 | 0 | CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP | CHANNEL_OPTION_COMPRESS_RDP; |
342 | |
|
343 | 0 | if (pEntryPointsEx->pVirtualChannelInitEx(plugin, nullptr, pInitHandle, &channelDef, 1, |
344 | 0 | VIRTUAL_CHANNEL_VERSION_WIN2000, |
345 | 0 | VirtualChannelInitEventEx) != CHANNEL_RC_OK) |
346 | 0 | { |
347 | 0 | channel_terminated(plugin); |
348 | 0 | return FALSE; |
349 | 0 | } |
350 | | |
351 | 0 | return TRUE; |
352 | 0 | } |