Coverage Report

Created: 2026-03-04 06:17

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