Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/channels/rdp2tcp/client/rdp2tcp_main.c
Line
Count
Source (jump to first uncovered line)
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 = { 0 };
52
0
  STARTUPINFOA siStartInfo = { 0 }; /* Using ANSI type to match CreateProcessA */
53
0
  PROCESS_INFORMATION procInfo = { 0 };
54
55
0
  WINPR_ASSERT(plugin);
56
57
0
  saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
58
0
  saAttr.bInheritHandle = TRUE;
59
0
  saAttr.lpSecurityDescriptor = NULL;
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(NULL,
99
0
                      plugin->commandline, // command line
100
0
                      NULL,                // process security attributes
101
0
                      NULL,                // primary thread security attributes
102
0
                      TRUE,                // handles are inherited
103
0
                      0,                   // creation flags
104
0
                      NULL,                // use parent's environment
105
0
                      NULL,                // 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
    // NULL))
147
0
    if (!ReadFile(plugin->hStdOutputRead, buffer, bufsize, &dwRead, NULL))
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, NULL))
194
0
      closeChannel(plugin);
195
0
  }
196
197
0
  if (!WriteFile(plugin->hStdInputWrite, pData, dataLength, &dwWritten, NULL))
198
0
    closeChannel(plugin);
199
0
}
200
201
static void VCAPITYPE VirtualChannelOpenEventEx(LPVOID lpUserParam,
202
                                                WINPR_ATTR_UNUSED DWORD openHandle, UINT event,
203
                                                LPVOID pData, UINT32 dataLength, UINT32 totalLength,
204
                                                UINT32 dataFlags)
205
0
{
206
0
  Plugin* plugin = (Plugin*)lpUserParam;
207
208
0
  WINPR_ASSERT(plugin);
209
0
  switch (event)
210
0
  {
211
0
    case CHANNEL_EVENT_DATA_RECEIVED:
212
0
      dataReceived(plugin, pData, dataLength, totalLength, dataFlags);
213
0
      break;
214
215
0
    case CHANNEL_EVENT_WRITE_CANCELLED:
216
0
      free(pData);
217
0
      break;
218
0
    case CHANNEL_EVENT_WRITE_COMPLETE:
219
0
      (void)SetEvent(plugin->writeComplete);
220
0
      free(pData);
221
0
      break;
222
0
    default:
223
0
      break;
224
0
  }
225
0
}
226
227
static void channel_terminated(Plugin* plugin)
228
0
{
229
0
  if (!plugin)
230
0
    return;
231
232
0
  if (plugin->copyThread)
233
0
    (void)CloseHandle(plugin->copyThread);
234
0
  if (plugin->writeComplete)
235
0
    (void)CloseHandle(plugin->writeComplete);
236
237
0
  (void)CloseHandle(plugin->hStdInputWrite);
238
0
  (void)CloseHandle(plugin->hStdOutputRead);
239
0
  TerminateProcess(plugin->hProcess, 0);
240
0
  (void)CloseHandle(plugin->hProcess);
241
0
  free(plugin->commandline);
242
0
  free(plugin);
243
0
}
244
245
static void channel_initialized(Plugin* plugin)
246
0
{
247
0
  WINPR_ASSERT(plugin);
248
0
  WINPR_ASSERT(!plugin->writeComplete);
249
0
  plugin->writeComplete = CreateEvent(NULL, TRUE, FALSE, NULL);
250
251
0
  WINPR_ASSERT(!plugin->copyThread);
252
0
  plugin->copyThread = CreateThread(NULL, 0, copyThread, plugin, 0, NULL);
253
0
}
254
255
static VOID VCAPITYPE VirtualChannelInitEventEx(LPVOID lpUserParam, LPVOID pInitHandle, UINT event,
256
                                                WINPR_ATTR_UNUSED LPVOID pData,
257
                                                WINPR_ATTR_UNUSED UINT dataLength)
258
0
{
259
0
  Plugin* plugin = (Plugin*)lpUserParam;
260
261
0
  WINPR_ASSERT(plugin);
262
263
0
  switch (event)
264
0
  {
265
0
    case CHANNEL_EVENT_INITIALIZED:
266
0
      channel_initialized(plugin);
267
0
      break;
268
269
0
    case CHANNEL_EVENT_CONNECTED:
270
0
      WINPR_ASSERT(plugin);
271
0
      WINPR_ASSERT(plugin->channelEntryPoints.pVirtualChannelOpenEx);
272
0
      if (plugin->channelEntryPoints.pVirtualChannelOpenEx(
273
0
              pInitHandle, &plugin->openHandle, RDP2TCP_DVC_CHANNEL_NAME,
274
0
              VirtualChannelOpenEventEx) != CHANNEL_RC_OK)
275
0
        return;
276
277
0
      break;
278
279
0
    case CHANNEL_EVENT_DISCONNECTED:
280
0
      closeChannel(plugin);
281
0
      break;
282
283
0
    case CHANNEL_EVENT_TERMINATED:
284
0
      channel_terminated(plugin);
285
0
      break;
286
0
    default:
287
0
      break;
288
0
  }
289
0
}
290
291
#define VirtualChannelEntryEx rdp2tcp_VirtualChannelEntryEx
292
FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS pEntryPoints,
293
                                                         PVOID pInitHandle))
294
0
{
295
0
  CHANNEL_ENTRY_POINTS_FREERDP_EX* pEntryPointsEx =
296
0
      (CHANNEL_ENTRY_POINTS_FREERDP_EX*)pEntryPoints;
297
0
  WINPR_ASSERT(pEntryPointsEx);
298
0
  WINPR_ASSERT(pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX) &&
299
0
               pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER);
300
301
0
  Plugin* plugin = (Plugin*)calloc(1, sizeof(Plugin));
302
303
0
  if (!plugin)
304
0
    return FALSE;
305
306
0
  plugin->initHandle = pInitHandle;
307
0
  plugin->channelEntryPoints = *pEntryPointsEx;
308
309
0
  if (init_external_addin(plugin) < 0)
310
0
  {
311
0
    channel_terminated(plugin);
312
0
    return FALSE;
313
0
  }
314
315
0
  CHANNEL_DEF channelDef = { 0 };
316
0
  strncpy(channelDef.name, RDP2TCP_DVC_CHANNEL_NAME, sizeof(channelDef.name));
317
0
  channelDef.options =
318
0
      CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP | CHANNEL_OPTION_COMPRESS_RDP;
319
320
0
  if (pEntryPointsEx->pVirtualChannelInitEx(plugin, NULL, pInitHandle, &channelDef, 1,
321
0
                                            VIRTUAL_CHANNEL_VERSION_WIN2000,
322
0
                                            VirtualChannelInitEventEx) != CHANNEL_RC_OK)
323
0
  {
324
0
    channel_terminated(plugin);
325
0
    return FALSE;
326
0
  }
327
328
0
  return TRUE;
329
0
}