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