Coverage Report

Created: 2026-03-07 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/core/server.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Server Channels
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <stdint.h>
28
29
#include <winpr/atexit.h>
30
#include <winpr/wtypes.h>
31
#include <winpr/crt.h>
32
#include <winpr/synch.h>
33
#include <winpr/stream.h>
34
#include <winpr/assert.h>
35
#include <winpr/cast.h>
36
37
#include <freerdp/log.h>
38
#include <freerdp/constants.h>
39
#include <freerdp/server/channels.h>
40
#include <freerdp/channels/drdynvc.h>
41
#include <freerdp/utils/drdynvc.h>
42
43
#include "rdp.h"
44
45
#include "server.h"
46
47
#define TAG FREERDP_TAG("core.server")
48
#ifdef WITH_DEBUG_DVC
49
#define DEBUG_DVC(...) WLog_DBG(TAG, __VA_ARGS__)
50
#else
51
#define DEBUG_DVC(...) \
52
0
  do                 \
53
0
  {                  \
54
0
  } while (0)
55
#endif
56
57
0
#define DVC_MAX_DATA_PDU_SIZE 1600
58
59
typedef struct
60
{
61
  UINT16 channelId;
62
  UINT16 reserved;
63
  UINT32 length;
64
  UINT32 offset;
65
} wtsChannelMessage;
66
67
static const DWORD g_err_oom = WINPR_CXX_COMPAT_CAST(DWORD, E_OUTOFMEMORY);
68
69
static DWORD g_SessionId = 1;
70
static wHashTable* g_ServerHandles = nullptr;
71
static INIT_ONCE g_HandleInitializer = INIT_ONCE_STATIC_INIT;
72
73
static rdpPeerChannel* wts_get_dvc_channel_by_id(WTSVirtualChannelManager* vcm, UINT32 ChannelId)
74
0
{
75
0
  WINPR_ASSERT(vcm);
76
0
  return HashTable_GetItemValue(vcm->dynamicVirtualChannels, &ChannelId);
77
0
}
78
79
static BOOL wts_queue_receive_data(rdpPeerChannel* channel, const BYTE* Buffer, UINT32 Length)
80
0
{
81
0
  BYTE* buffer = nullptr;
82
0
  wtsChannelMessage* messageCtx = nullptr;
83
84
0
  WINPR_ASSERT(channel);
85
0
  messageCtx = (wtsChannelMessage*)malloc(sizeof(wtsChannelMessage) + Length);
86
87
0
  if (!messageCtx)
88
0
    return FALSE;
89
90
0
  WINPR_ASSERT(channel->channelId <= UINT16_MAX);
91
0
  messageCtx->channelId = (UINT16)channel->channelId;
92
0
  messageCtx->length = Length;
93
0
  messageCtx->offset = 0;
94
0
  buffer = (BYTE*)(messageCtx + 1);
95
0
  CopyMemory(buffer, Buffer, Length);
96
0
  return MessageQueue_Post(channel->queue, messageCtx, 0, nullptr, nullptr);
97
0
}
98
99
static BOOL wts_queue_send_item(rdpPeerChannel* channel, BYTE* Buffer, UINT32 Length)
100
0
{
101
0
  BYTE* buffer = nullptr;
102
0
  UINT32 length = 0;
103
104
0
  WINPR_ASSERT(channel);
105
0
  WINPR_ASSERT(channel->vcm);
106
0
  buffer = Buffer;
107
0
  length = Length;
108
109
0
  WINPR_ASSERT(channel->channelId <= UINT16_MAX);
110
0
  const UINT16 channelId = (UINT16)channel->channelId;
111
0
  return MessageQueue_Post(channel->vcm->queue, (void*)(UINT_PTR)channelId, 0, (void*)buffer,
112
0
                           (void*)(UINT_PTR)length);
113
0
}
114
115
static unsigned wts_read_variable_uint(wStream* s, int cbLen, UINT32* val)
116
0
{
117
0
  WINPR_ASSERT(s);
118
0
  WINPR_ASSERT(val);
119
0
  switch (cbLen)
120
0
  {
121
0
    case 0:
122
0
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
123
0
        return 0;
124
125
0
      Stream_Read_UINT8(s, *val);
126
0
      return 1;
127
128
0
    case 1:
129
0
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
130
0
        return 0;
131
132
0
      Stream_Read_UINT16(s, *val);
133
0
      return 2;
134
135
0
    case 2:
136
0
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
137
0
        return 0;
138
139
0
      Stream_Read_UINT32(s, *val);
140
0
      return 4;
141
142
0
    default:
143
0
      WLog_ERR(TAG, "invalid wts variable uint len %d", cbLen);
144
0
      return 0;
145
0
  }
146
0
}
147
148
static BOOL wts_read_drdynvc_capabilities_response(rdpPeerChannel* channel, UINT32 length)
149
0
{
150
0
  UINT16 Version = 0;
151
152
0
  WINPR_ASSERT(channel);
153
0
  WINPR_ASSERT(channel->vcm);
154
0
  if (length < 3)
155
0
    return FALSE;
156
157
0
  Stream_Seek_UINT8(channel->receiveData); /* Pad (1 byte) */
158
0
  Stream_Read_UINT16(channel->receiveData, Version);
159
0
  DEBUG_DVC("Version: %" PRIu16 "", Version);
160
161
0
  if (Version < 1)
162
0
  {
163
0
    WLog_ERR(TAG, "invalid version %" PRIu16 " for DRDYNVC", Version);
164
0
    return FALSE;
165
0
  }
166
167
0
  WTSVirtualChannelManager* vcm = channel->vcm;
168
0
  vcm->drdynvc_state = DRDYNVC_STATE_READY;
169
170
  /* we only support version 1 for now (no compression yet) */
171
0
  vcm->dvc_spoken_version = MAX(Version, 1);
172
173
0
  return SetEvent(MessageQueue_Event(vcm->queue));
174
0
}
175
176
static BOOL wts_read_drdynvc_create_response(rdpPeerChannel* channel, wStream* s, UINT32 length)
177
0
{
178
0
  UINT32 CreationStatus = 0;
179
0
  BOOL status = TRUE;
180
181
0
  WINPR_ASSERT(channel);
182
0
  WINPR_ASSERT(s);
183
0
  if (length < 4)
184
0
    return FALSE;
185
186
0
  Stream_Read_UINT32(s, CreationStatus);
187
188
0
  if ((INT32)CreationStatus < 0)
189
0
  {
190
0
    DEBUG_DVC("ChannelId %" PRIu32 " creation failed (%" PRId32 ")", channel->channelId,
191
0
              (INT32)CreationStatus);
192
0
    channel->dvc_open_state = DVC_OPEN_STATE_FAILED;
193
0
  }
194
0
  else
195
0
  {
196
0
    DEBUG_DVC("ChannelId %" PRIu32 " creation succeeded", channel->channelId);
197
0
    channel->dvc_open_state = DVC_OPEN_STATE_SUCCEEDED;
198
0
  }
199
200
0
  channel->creationStatus = (INT32)CreationStatus;
201
0
  IFCALLRET(channel->vcm->dvc_creation_status, status, channel->vcm->dvc_creation_status_userdata,
202
0
            channel->channelId, (INT32)CreationStatus);
203
0
  if (!status)
204
0
    WLog_ERR(TAG, "vcm->dvc_creation_status failed!");
205
206
0
  return status;
207
0
}
208
209
static BOOL wts_read_drdynvc_data_first(rdpPeerChannel* channel, wStream* s, int cbLen,
210
                                        UINT32 length)
211
0
{
212
0
  WINPR_ASSERT(channel);
213
0
  WINPR_ASSERT(s);
214
0
  const UINT32 value = wts_read_variable_uint(s, cbLen, &channel->dvc_total_length);
215
216
0
  if (value == 0)
217
0
    return FALSE;
218
0
  if (value > length)
219
0
    length = 0;
220
0
  else
221
0
    length -= value;
222
223
0
  if (length > channel->dvc_total_length)
224
0
    return FALSE;
225
226
0
  Stream_ResetPosition(channel->receiveData);
227
228
0
  if (!Stream_EnsureRemainingCapacity(channel->receiveData, channel->dvc_total_length))
229
0
    return FALSE;
230
231
0
  Stream_Write(channel->receiveData, Stream_ConstPointer(s), length);
232
0
  return TRUE;
233
0
}
234
235
static BOOL wts_read_drdynvc_data(rdpPeerChannel* channel, wStream* s, UINT32 length)
236
0
{
237
0
  BOOL ret = FALSE;
238
239
0
  WINPR_ASSERT(channel);
240
0
  WINPR_ASSERT(s);
241
0
  if (channel->dvc_total_length > 0)
242
0
  {
243
0
    if (Stream_GetPosition(channel->receiveData) + length > channel->dvc_total_length)
244
0
    {
245
0
      channel->dvc_total_length = 0;
246
0
      WLog_ERR(TAG, "incorrect fragment data, discarded.");
247
0
      return FALSE;
248
0
    }
249
250
0
    Stream_Write(channel->receiveData, Stream_ConstPointer(s), length);
251
252
0
    if (Stream_GetPosition(channel->receiveData) >= channel->dvc_total_length)
253
0
    {
254
0
      ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData),
255
0
                                   channel->dvc_total_length);
256
0
      channel->dvc_total_length = 0;
257
0
    }
258
0
    else
259
0
      ret = TRUE;
260
0
  }
261
0
  else
262
0
  {
263
0
    ret = wts_queue_receive_data(channel, Stream_ConstPointer(s), length);
264
0
  }
265
266
0
  return ret;
267
0
}
268
269
static void wts_read_drdynvc_close_response(rdpPeerChannel* channel)
270
0
{
271
0
  WINPR_ASSERT(channel);
272
0
  DEBUG_DVC("ChannelId %" PRIu32 " close response", channel->channelId);
273
0
  channel->dvc_open_state = DVC_OPEN_STATE_CLOSED;
274
0
  MessageQueue_PostQuit(channel->queue, 0);
275
0
}
276
277
static BOOL wts_read_drdynvc_pdu(rdpPeerChannel* channel)
278
0
{
279
0
  UINT8 Cmd = 0;
280
0
  UINT8 Sp = 0;
281
0
  UINT8 cbChId = 0;
282
0
  UINT32 ChannelId = 0;
283
0
  rdpPeerChannel* dvc = nullptr;
284
285
0
  WINPR_ASSERT(channel);
286
0
  WINPR_ASSERT(channel->vcm);
287
288
0
  size_t length = Stream_GetPosition(channel->receiveData);
289
290
0
  if ((length < 1) || (length > UINT32_MAX))
291
0
    return FALSE;
292
293
0
  Stream_ResetPosition(channel->receiveData);
294
0
  const UINT8 value = Stream_Get_UINT8(channel->receiveData);
295
0
  length--;
296
0
  Cmd = (value & 0xf0) >> 4;
297
0
  Sp = (value & 0x0c) >> 2;
298
0
  cbChId = (value & 0x03) >> 0;
299
300
0
  if (Cmd == CAPABILITY_REQUEST_PDU)
301
0
    return wts_read_drdynvc_capabilities_response(channel, (UINT32)length);
302
303
0
  if (channel->vcm->drdynvc_state == DRDYNVC_STATE_READY)
304
0
  {
305
0
    BOOL haveChannelId = 0;
306
0
    switch (Cmd)
307
0
    {
308
0
      case SOFT_SYNC_REQUEST_PDU:
309
0
      case SOFT_SYNC_RESPONSE_PDU:
310
0
        haveChannelId = FALSE;
311
0
        break;
312
0
      default:
313
0
        haveChannelId = TRUE;
314
0
        break;
315
0
    }
316
317
0
    if (haveChannelId)
318
0
    {
319
0
      const unsigned val = wts_read_variable_uint(channel->receiveData, cbChId, &ChannelId);
320
0
      if (val == 0)
321
0
        return FALSE;
322
323
0
      length -= val;
324
325
0
      DEBUG_DVC("Cmd %s ChannelId %" PRIu32 " length %" PRIuz "",
326
0
                drdynvc_get_packet_type(Cmd), ChannelId, length);
327
0
      dvc = wts_get_dvc_channel_by_id(channel->vcm, ChannelId);
328
0
      if (!dvc)
329
0
      {
330
0
        DEBUG_DVC("ChannelId %" PRIu32 " does not exist.", ChannelId);
331
0
        return TRUE;
332
0
      }
333
0
    }
334
335
0
    switch (Cmd)
336
0
    {
337
0
      case CREATE_REQUEST_PDU:
338
0
        return wts_read_drdynvc_create_response(dvc, channel->receiveData, (UINT32)length);
339
340
0
      case DATA_FIRST_PDU:
341
0
        if (dvc->dvc_open_state != DVC_OPEN_STATE_SUCCEEDED)
342
0
        {
343
0
          WLog_ERR(TAG,
344
0
                   "ChannelId %" PRIu32 " did not open successfully. "
345
0
                   "Ignoring DYNVC_DATA_FIRST PDU",
346
0
                   ChannelId);
347
0
          return TRUE;
348
0
        }
349
350
0
        return wts_read_drdynvc_data_first(dvc, channel->receiveData, Sp, (UINT32)length);
351
352
0
      case DATA_PDU:
353
0
        if (dvc->dvc_open_state != DVC_OPEN_STATE_SUCCEEDED)
354
0
        {
355
0
          WLog_ERR(TAG,
356
0
                   "ChannelId %" PRIu32 " did not open successfully. "
357
0
                   "Ignoring DYNVC_DATA PDU",
358
0
                   ChannelId);
359
0
          return TRUE;
360
0
        }
361
362
0
        return wts_read_drdynvc_data(dvc, channel->receiveData, (UINT32)length);
363
364
0
      case CLOSE_REQUEST_PDU:
365
0
        wts_read_drdynvc_close_response(dvc);
366
0
        break;
367
368
0
      case DATA_FIRST_COMPRESSED_PDU:
369
0
      case DATA_COMPRESSED_PDU:
370
0
        WLog_ERR(TAG, "Compressed data not handled");
371
0
        break;
372
373
0
      case SOFT_SYNC_RESPONSE_PDU:
374
0
        WLog_ERR(TAG, "SoftSync response not handled yet(and rather strange to receive "
375
0
                      "that packet as our code doesn't send SoftSync requests");
376
0
        break;
377
378
0
      case SOFT_SYNC_REQUEST_PDU:
379
0
        WLog_ERR(TAG, "Not expecting a SoftSyncRequest on the server");
380
0
        return FALSE;
381
382
0
      default:
383
0
        WLog_ERR(TAG, "Cmd %d not recognized.", Cmd);
384
0
        break;
385
0
    }
386
0
  }
387
0
  else
388
0
  {
389
0
    WLog_ERR(TAG, "received Cmd %d but channel is not ready.", Cmd);
390
0
  }
391
392
0
  return TRUE;
393
0
}
394
395
static int wts_write_variable_uint(wStream* s, UINT32 val)
396
0
{
397
0
  int cb = 0;
398
399
0
  WINPR_ASSERT(s);
400
0
  if (val <= 0xFF)
401
0
  {
402
0
    cb = 0;
403
0
    Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, val));
404
0
  }
405
0
  else if (val <= 0xFFFF)
406
0
  {
407
0
    cb = 1;
408
0
    Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, val));
409
0
  }
410
0
  else
411
0
  {
412
0
    cb = 2;
413
0
    Stream_Write_UINT32(s, val);
414
0
  }
415
416
0
  return cb;
417
0
}
418
419
static void wts_write_drdynvc_header(wStream* s, BYTE Cmd, UINT32 ChannelId)
420
0
{
421
0
  BYTE* bm = nullptr;
422
0
  int cbChId = 0;
423
424
0
  WINPR_ASSERT(s);
425
426
0
  Stream_GetPointer(s, bm);
427
0
  Stream_Seek_UINT8(s);
428
0
  cbChId = wts_write_variable_uint(s, ChannelId);
429
0
  *bm = (((Cmd & 0x0F) << 4) | cbChId) & 0xFF;
430
0
}
431
432
static BOOL wts_write_drdynvc_create_request(wStream* s, UINT32 ChannelId, const char* ChannelName)
433
0
{
434
0
  size_t len = 0;
435
436
0
  WINPR_ASSERT(s);
437
0
  WINPR_ASSERT(ChannelName);
438
439
0
  wts_write_drdynvc_header(s, CREATE_REQUEST_PDU, ChannelId);
440
0
  len = strlen(ChannelName) + 1;
441
442
0
  if (!Stream_EnsureRemainingCapacity(s, len))
443
0
    return FALSE;
444
445
0
  Stream_Write(s, ChannelName, len);
446
0
  return TRUE;
447
0
}
448
449
static BOOL WTSProcessChannelData(rdpPeerChannel* channel, UINT16 channelId, const BYTE* data,
450
                                  size_t s, UINT32 flags, size_t t)
451
0
{
452
0
  BOOL ret = TRUE;
453
0
  const size_t size = s;
454
0
  const size_t totalSize = t;
455
456
0
  WINPR_ASSERT(channel);
457
0
  WINPR_ASSERT(channel->vcm);
458
0
  WINPR_UNUSED(channelId);
459
460
0
  if (flags & CHANNEL_FLAG_FIRST)
461
0
  {
462
0
    Stream_ResetPosition(channel->receiveData);
463
0
  }
464
465
0
  if (!Stream_EnsureRemainingCapacity(channel->receiveData, size))
466
0
    return FALSE;
467
468
0
  Stream_Write(channel->receiveData, data, size);
469
470
0
  if (flags & CHANNEL_FLAG_LAST)
471
0
  {
472
0
    if (Stream_GetPosition(channel->receiveData) != totalSize)
473
0
    {
474
0
      WLog_ERR(TAG, "read error");
475
0
    }
476
477
0
    if (channel == channel->vcm->drdynvc_channel)
478
0
    {
479
0
      ret = wts_read_drdynvc_pdu(channel);
480
0
    }
481
0
    else
482
0
    {
483
0
      const size_t pos = Stream_GetPosition(channel->receiveData);
484
0
      if (pos > UINT32_MAX)
485
0
        ret = FALSE;
486
0
      else
487
0
        ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData),
488
0
                                     (UINT32)pos);
489
0
    }
490
491
0
    Stream_ResetPosition(channel->receiveData);
492
0
  }
493
494
0
  return ret;
495
0
}
496
497
static BOOL WTSReceiveChannelData(freerdp_peer* client, UINT16 channelId, const BYTE* data,
498
                                  size_t size, UINT32 flags, size_t totalSize)
499
0
{
500
0
  rdpMcs* mcs = nullptr;
501
502
0
  WINPR_ASSERT(client);
503
0
  WINPR_ASSERT(client->context);
504
0
  WINPR_ASSERT(client->context->rdp);
505
506
0
  mcs = client->context->rdp->mcs;
507
0
  WINPR_ASSERT(mcs);
508
509
0
  for (UINT32 i = 0; i < mcs->channelCount; i++)
510
0
  {
511
0
    rdpMcsChannel* cur = &mcs->channels[i];
512
0
    if (cur->ChannelId == channelId)
513
0
    {
514
0
      rdpPeerChannel* channel = (rdpPeerChannel*)cur->handle;
515
516
0
      if (channel)
517
0
        return WTSProcessChannelData(channel, channelId, data, size, flags, totalSize);
518
0
    }
519
0
  }
520
521
0
  WLog_WARN(TAG, "unknown channelId %" PRIu16 " ignored", channelId);
522
523
0
  return TRUE;
524
0
}
525
526
#if defined(WITH_FREERDP_DEPRECATED)
527
void WTSVirtualChannelManagerGetFileDescriptor(HANDLE hServer, void** fds, int* fds_count)
528
{
529
  void* fd = nullptr;
530
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
531
  WINPR_ASSERT(vcm);
532
  WINPR_ASSERT(fds);
533
  WINPR_ASSERT(fds_count);
534
535
  fd = GetEventWaitObject(MessageQueue_Event(vcm->queue));
536
537
  if (fd)
538
  {
539
    fds[*fds_count] = fd;
540
    (*fds_count)++;
541
  }
542
543
#if 0
544
545
  if (vcm->drdynvc_channel)
546
  {
547
    fd = GetEventWaitObject(vcm->drdynvc_channel->receiveEvent);
548
549
    if (fd)
550
    {
551
      fds[*fds_count] = fd;
552
      (*fds_count)++;
553
    }
554
  }
555
556
#endif
557
}
558
#endif
559
560
BOOL WTSVirtualChannelManagerOpen(HANDLE hServer)
561
0
{
562
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
563
564
0
  if (!vcm)
565
0
    return FALSE;
566
567
0
  if (vcm->drdynvc_state == DRDYNVC_STATE_NONE)
568
0
  {
569
0
    rdpPeerChannel* channel = nullptr;
570
571
    /* Initialize drdynvc channel once and only once. */
572
0
    vcm->drdynvc_state = DRDYNVC_STATE_INITIALIZED;
573
0
    channel = (rdpPeerChannel*)WTSVirtualChannelOpen((HANDLE)vcm, WTS_CURRENT_SESSION,
574
0
                                                     DRDYNVC_SVC_CHANNEL_NAME);
575
576
0
    if (channel)
577
0
    {
578
0
      BYTE capaBuffer[12] = WINPR_C_ARRAY_INIT;
579
0
      wStream staticS = WINPR_C_ARRAY_INIT;
580
0
      wStream* s = Stream_StaticInit(&staticS, capaBuffer, sizeof(capaBuffer));
581
582
0
      vcm->drdynvc_channel = channel;
583
0
      vcm->dvc_spoken_version = 1;
584
0
      Stream_Write_UINT8(s, 0x50);    /* Cmd=5 sp=0 cbId=0 */
585
0
      Stream_Write_UINT8(s, 0x00);    /* Pad */
586
0
      Stream_Write_UINT16(s, 0x0001); /* Version */
587
588
      /* TODO: shall implement version 2 and 3 */
589
590
0
      const size_t pos = Stream_GetPosition(s);
591
0
      WINPR_ASSERT(pos <= UINT32_MAX);
592
0
      ULONG written = 0;
593
0
      if (!WTSVirtualChannelWrite(channel, (PCHAR)capaBuffer, (UINT32)pos, &written))
594
0
        return FALSE;
595
0
    }
596
0
  }
597
598
0
  return TRUE;
599
0
}
600
601
BOOL WTSVirtualChannelManagerCheckFileDescriptorEx(HANDLE hServer, BOOL autoOpen)
602
0
{
603
0
  wMessage message = WINPR_C_ARRAY_INIT;
604
0
  BOOL status = TRUE;
605
0
  WTSVirtualChannelManager* vcm = nullptr;
606
607
0
  if (!hServer || hServer == INVALID_HANDLE_VALUE)
608
0
    return FALSE;
609
610
0
  vcm = (WTSVirtualChannelManager*)hServer;
611
612
0
  if (autoOpen)
613
0
  {
614
0
    if (!WTSVirtualChannelManagerOpen(hServer))
615
0
      return FALSE;
616
0
  }
617
618
0
  while (MessageQueue_Peek(vcm->queue, &message, TRUE))
619
0
  {
620
0
    BYTE* buffer = nullptr;
621
0
    UINT32 length = 0;
622
0
    UINT16 channelId = 0;
623
0
    channelId = (UINT16)(UINT_PTR)message.context;
624
0
    buffer = (BYTE*)message.wParam;
625
0
    length = (UINT32)(UINT_PTR)message.lParam;
626
627
0
    WINPR_ASSERT(vcm->client);
628
0
    WINPR_ASSERT(vcm->client->SendChannelData);
629
0
    if (!vcm->client->SendChannelData(vcm->client, channelId, buffer, length))
630
0
    {
631
0
      status = FALSE;
632
0
    }
633
634
0
    free(buffer);
635
636
0
    if (!status)
637
0
      break;
638
0
  }
639
640
0
  return status;
641
0
}
642
643
BOOL WTSVirtualChannelManagerCheckFileDescriptor(HANDLE hServer)
644
0
{
645
0
  return WTSVirtualChannelManagerCheckFileDescriptorEx(hServer, TRUE);
646
0
}
647
648
HANDLE WTSVirtualChannelManagerGetEventHandle(HANDLE hServer)
649
0
{
650
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
651
0
  WINPR_ASSERT(vcm);
652
0
  return MessageQueue_Event(vcm->queue);
653
0
}
654
655
static rdpMcsChannel* wts_get_joined_channel_by_name(rdpMcs* mcs, const char* channel_name)
656
0
{
657
0
  if (!mcs || !channel_name || !strnlen(channel_name, CHANNEL_NAME_LEN + 1))
658
0
    return nullptr;
659
660
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
661
0
  {
662
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
663
0
    if (mchannel->joined)
664
0
    {
665
0
      if (_strnicmp(mchannel->Name, channel_name, CHANNEL_NAME_LEN + 1) == 0)
666
0
        return mchannel;
667
0
    }
668
0
  }
669
670
0
  return nullptr;
671
0
}
672
673
static rdpMcsChannel* wts_get_joined_channel_by_id(rdpMcs* mcs, const UINT16 channel_id)
674
0
{
675
0
  if (!mcs || !channel_id)
676
0
    return nullptr;
677
678
0
  WINPR_ASSERT(mcs->channels);
679
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
680
0
  {
681
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
682
0
    if (mchannel->joined)
683
0
    {
684
0
      if (mchannel->ChannelId == channel_id)
685
0
        return &mcs->channels[index];
686
0
    }
687
0
  }
688
689
0
  return nullptr;
690
0
}
691
692
BOOL WTSIsChannelJoinedByName(freerdp_peer* client, const char* channel_name)
693
0
{
694
0
  if (!client || !client->context || !client->context->rdp)
695
0
    return FALSE;
696
697
0
  return (wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name) != nullptr);
698
0
}
699
700
BOOL WTSIsChannelJoinedById(freerdp_peer* client, UINT16 channel_id)
701
0
{
702
0
  if (!client || !client->context || !client->context->rdp)
703
0
    return FALSE;
704
705
0
  return (wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id) != nullptr);
706
0
}
707
708
BOOL WTSVirtualChannelManagerIsChannelJoined(HANDLE hServer, const char* name)
709
0
{
710
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
711
712
0
  if (!vcm || !vcm->rdp)
713
0
    return FALSE;
714
715
0
  return (wts_get_joined_channel_by_name(vcm->rdp->mcs, name) != nullptr);
716
0
}
717
718
BYTE WTSVirtualChannelManagerGetDrdynvcState(HANDLE hServer)
719
0
{
720
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
721
0
  WINPR_ASSERT(vcm);
722
0
  return vcm->drdynvc_state;
723
0
}
724
725
void WTSVirtualChannelManagerSetDVCCreationCallback(HANDLE hServer, psDVCCreationStatusCallback cb,
726
                                                    void* userdata)
727
0
{
728
0
  WTSVirtualChannelManager* vcm = hServer;
729
730
0
  WINPR_ASSERT(vcm);
731
732
0
  vcm->dvc_creation_status = cb;
733
0
  vcm->dvc_creation_status_userdata = userdata;
734
0
}
735
736
UINT16 WTSChannelGetId(freerdp_peer* client, const char* channel_name)
737
0
{
738
0
  rdpMcsChannel* channel = nullptr;
739
740
0
  WINPR_ASSERT(channel_name);
741
0
  if (!client || !client->context || !client->context->rdp)
742
0
    return 0;
743
744
0
  channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
745
746
0
  if (!channel)
747
0
    return 0;
748
749
0
  return channel->ChannelId;
750
0
}
751
752
UINT32 WTSChannelGetIdByHandle(HANDLE hChannelHandle)
753
0
{
754
0
  rdpPeerChannel* channel = hChannelHandle;
755
756
0
  WINPR_ASSERT(channel);
757
758
0
  return channel->channelId;
759
0
}
760
761
BOOL WTSChannelSetHandleByName(freerdp_peer* client, const char* channel_name, void* handle)
762
0
{
763
0
  rdpMcsChannel* channel = nullptr;
764
765
0
  WINPR_ASSERT(channel_name);
766
0
  if (!client || !client->context || !client->context->rdp)
767
0
    return FALSE;
768
769
0
  channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
770
771
0
  if (!channel)
772
0
    return FALSE;
773
774
0
  channel->handle = handle;
775
0
  return TRUE;
776
0
}
777
778
BOOL WTSChannelSetHandleById(freerdp_peer* client, UINT16 channel_id, void* handle)
779
0
{
780
0
  rdpMcsChannel* channel = nullptr;
781
782
0
  if (!client || !client->context || !client->context->rdp)
783
0
    return FALSE;
784
785
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
786
787
0
  if (!channel)
788
0
    return FALSE;
789
790
0
  channel->handle = handle;
791
0
  return TRUE;
792
0
}
793
794
void* WTSChannelGetHandleByName(freerdp_peer* client, const char* channel_name)
795
0
{
796
0
  rdpMcsChannel* channel = nullptr;
797
798
0
  WINPR_ASSERT(channel_name);
799
0
  if (!client || !client->context || !client->context->rdp)
800
0
    return nullptr;
801
802
0
  channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
803
804
0
  if (!channel)
805
0
    return nullptr;
806
807
0
  return channel->handle;
808
0
}
809
810
void* WTSChannelGetHandleById(freerdp_peer* client, UINT16 channel_id)
811
0
{
812
0
  rdpMcsChannel* channel = nullptr;
813
814
0
  if (!client || !client->context || !client->context->rdp)
815
0
    return nullptr;
816
817
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
818
819
0
  if (!channel)
820
0
    return nullptr;
821
822
0
  return channel->handle;
823
0
}
824
825
const char* WTSChannelGetName(freerdp_peer* client, UINT16 channel_id)
826
0
{
827
0
  rdpMcsChannel* channel = nullptr;
828
829
0
  if (!client || !client->context || !client->context->rdp)
830
0
    return nullptr;
831
832
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
833
834
0
  if (!channel)
835
0
    return nullptr;
836
837
0
  return (const char*)channel->Name;
838
0
}
839
840
char** WTSGetAcceptedChannelNames(freerdp_peer* client, size_t* count)
841
0
{
842
0
  rdpMcs* mcs = nullptr;
843
0
  char** names = nullptr;
844
845
0
  if (!client || !client->context || !count)
846
0
    return nullptr;
847
848
0
  WINPR_ASSERT(client->context->rdp);
849
0
  mcs = client->context->rdp->mcs;
850
0
  WINPR_ASSERT(mcs);
851
0
  *count = mcs->channelCount;
852
853
0
  names = (char**)calloc(mcs->channelCount, sizeof(char*));
854
0
  if (!names)
855
0
    return nullptr;
856
857
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
858
0
  {
859
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
860
0
    names[index] = mchannel->Name;
861
0
  }
862
863
0
  return names;
864
0
}
865
866
INT64 WTSChannelGetOptions(freerdp_peer* client, UINT16 channel_id)
867
0
{
868
0
  rdpMcsChannel* channel = nullptr;
869
870
0
  if (!client || !client->context || !client->context->rdp)
871
0
    return -1;
872
873
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
874
875
0
  if (!channel)
876
0
    return -1;
877
878
0
  return (INT64)channel->options;
879
0
}
880
881
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionW(WINPR_ATTR_UNUSED LPWSTR pTargetServerName,
882
                                                  WINPR_ATTR_UNUSED ULONG TargetLogonId,
883
                                                  WINPR_ATTR_UNUSED BYTE HotkeyVk,
884
                                                  WINPR_ATTR_UNUSED USHORT HotkeyModifiers)
885
0
{
886
0
  WLog_ERR("TODO", "TODO: implement");
887
0
  return FALSE;
888
0
}
889
890
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionA(WINPR_ATTR_UNUSED LPSTR pTargetServerName,
891
                                                  WINPR_ATTR_UNUSED ULONG TargetLogonId,
892
                                                  WINPR_ATTR_UNUSED BYTE HotkeyVk,
893
                                                  WINPR_ATTR_UNUSED USHORT HotkeyModifiers)
894
0
{
895
0
  WLog_ERR("TODO", "TODO: implement");
896
0
  return FALSE;
897
0
}
898
899
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExW(WINPR_ATTR_UNUSED LPWSTR pTargetServerName,
900
                                                    WINPR_ATTR_UNUSED ULONG TargetLogonId,
901
                                                    WINPR_ATTR_UNUSED BYTE HotkeyVk,
902
                                                    WINPR_ATTR_UNUSED USHORT HotkeyModifiers,
903
                                                    WINPR_ATTR_UNUSED DWORD flags)
904
0
{
905
0
  WLog_ERR("TODO", "TODO: implement");
906
0
  return FALSE;
907
0
}
908
909
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExA(WINPR_ATTR_UNUSED LPSTR pTargetServerName,
910
                                                    WINPR_ATTR_UNUSED ULONG TargetLogonId,
911
                                                    WINPR_ATTR_UNUSED BYTE HotkeyVk,
912
                                                    WINPR_ATTR_UNUSED USHORT HotkeyModifiers,
913
                                                    WINPR_ATTR_UNUSED DWORD flags)
914
0
{
915
0
  WLog_ERR("TODO", "TODO: implement");
916
0
  return FALSE;
917
0
}
918
919
BOOL WINAPI FreeRDP_WTSStopRemoteControlSession(WINPR_ATTR_UNUSED ULONG LogonId)
920
0
{
921
0
  WLog_ERR("TODO", "TODO: implement");
922
0
  return FALSE;
923
0
}
924
925
BOOL WINAPI FreeRDP_WTSConnectSessionW(WINPR_ATTR_UNUSED ULONG LogonId,
926
                                       WINPR_ATTR_UNUSED ULONG TargetLogonId,
927
                                       WINPR_ATTR_UNUSED PWSTR pPassword,
928
                                       WINPR_ATTR_UNUSED BOOL bWait)
929
0
{
930
0
  WLog_ERR("TODO", "TODO: implement");
931
0
  return FALSE;
932
0
}
933
934
BOOL WINAPI FreeRDP_WTSConnectSessionA(WINPR_ATTR_UNUSED ULONG LogonId,
935
                                       WINPR_ATTR_UNUSED ULONG TargetLogonId,
936
                                       WINPR_ATTR_UNUSED PSTR pPassword,
937
                                       WINPR_ATTR_UNUSED BOOL bWait)
938
0
{
939
0
  WLog_ERR("TODO", "TODO: implement");
940
0
  return FALSE;
941
0
}
942
943
BOOL WINAPI FreeRDP_WTSEnumerateServersW(WINPR_ATTR_UNUSED LPWSTR pDomainName,
944
                                         WINPR_ATTR_UNUSED DWORD Reserved,
945
                                         WINPR_ATTR_UNUSED DWORD Version,
946
                                         WINPR_ATTR_UNUSED PWTS_SERVER_INFOW* ppServerInfo,
947
                                         WINPR_ATTR_UNUSED DWORD* pCount)
948
0
{
949
0
  WLog_ERR("TODO", "TODO: implement");
950
0
  return FALSE;
951
0
}
952
953
BOOL WINAPI FreeRDP_WTSEnumerateServersA(WINPR_ATTR_UNUSED LPSTR pDomainName,
954
                                         WINPR_ATTR_UNUSED DWORD Reserved,
955
                                         WINPR_ATTR_UNUSED DWORD Version,
956
                                         WINPR_ATTR_UNUSED PWTS_SERVER_INFOA* ppServerInfo,
957
                                         WINPR_ATTR_UNUSED DWORD* pCount)
958
0
{
959
0
  WLog_ERR("TODO", "TODO: implement");
960
0
  return FALSE;
961
0
}
962
963
HANDLE WINAPI FreeRDP_WTSOpenServerW(WINPR_ATTR_UNUSED LPWSTR pServerName)
964
0
{
965
0
  WLog_ERR("TODO", "TODO: implement");
966
0
  return INVALID_HANDLE_VALUE;
967
0
}
968
969
static void wts_virtual_channel_manager_free_message(void* obj)
970
0
{
971
0
  wMessage* msg = (wMessage*)obj;
972
973
0
  if (msg)
974
0
  {
975
0
    BYTE* buffer = (BYTE*)msg->wParam;
976
977
0
    if (buffer)
978
0
      free(buffer);
979
0
  }
980
0
}
981
982
static void channel_free(rdpPeerChannel* channel)
983
0
{
984
0
  server_channel_common_free(channel);
985
0
}
986
987
static void array_channel_free(void* ptr)
988
0
{
989
0
  rdpPeerChannel* channel = ptr;
990
0
  channel_free(channel);
991
0
}
992
993
static BOOL dynChannelMatch(const void* v1, const void* v2)
994
0
{
995
0
  const UINT32* p1 = (const UINT32*)v1;
996
0
  const UINT32* p2 = (const UINT32*)v2;
997
0
  return *p1 == *p2;
998
0
}
999
1000
static UINT32 channelId_Hash(const void* key)
1001
0
{
1002
0
  const UINT32* v = (const UINT32*)key;
1003
0
  return *v;
1004
0
}
1005
1006
static void clearHandles(void)
1007
0
{
1008
0
  HashTable_Free(g_ServerHandles);
1009
0
  g_ServerHandles = nullptr;
1010
0
}
1011
1012
static BOOL CALLBACK initializeHandles(WINPR_ATTR_UNUSED PINIT_ONCE once,
1013
                                       WINPR_ATTR_UNUSED PVOID param,
1014
                                       WINPR_ATTR_UNUSED PVOID* context)
1015
0
{
1016
0
  WINPR_ASSERT(g_ServerHandles == nullptr);
1017
0
  g_ServerHandles = HashTable_New(TRUE);
1018
0
  (void)winpr_atexit(clearHandles);
1019
0
  return g_ServerHandles != nullptr;
1020
0
}
1021
1022
static void wtsCloseVCM(WTSVirtualChannelManager* vcm, bool closeDrdynvc)
1023
0
{
1024
0
  WINPR_ASSERT(vcm);
1025
1026
0
  HashTable_Lock(g_ServerHandles);
1027
1028
/* clang analyzer does not like the check for INVALID_HANDLE_VALUE and considers the path not taken,
1029
 * leading to false positives on memory leaks. */
1030
#ifdef __clang_analyzer__
1031
  const BOOL valid = vcm != nullptr;
1032
#else
1033
0
  const BOOL valid = (vcm != nullptr) && (vcm != INVALID_HANDLE_VALUE);
1034
0
#endif
1035
0
  if (valid)
1036
0
  {
1037
0
    HashTable_Remove(g_ServerHandles, (void*)(UINT_PTR)vcm->SessionId);
1038
1039
0
    HashTable_Free(vcm->dynamicVirtualChannels);
1040
1041
0
    if (vcm->drdynvc_channel)
1042
0
    {
1043
0
      if (closeDrdynvc)
1044
0
        (void)WTSVirtualChannelClose(vcm->drdynvc_channel);
1045
0
      vcm->drdynvc_channel = nullptr;
1046
0
    }
1047
1048
0
    MessageQueue_Free(vcm->queue);
1049
0
    free(vcm);
1050
0
  }
1051
0
  HashTable_Unlock(g_ServerHandles);
1052
0
}
1053
1054
HANDLE WINAPI FreeRDP_WTSOpenServerA(LPSTR pServerName)
1055
0
{
1056
0
  wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1057
1058
0
  rdpContext* context = (rdpContext*)pServerName;
1059
1060
0
  if (!context)
1061
0
    return INVALID_HANDLE_VALUE;
1062
1063
0
  freerdp_peer* client = context->peer;
1064
1065
0
  if (!client)
1066
0
  {
1067
0
    SetLastError(ERROR_INVALID_DATA);
1068
0
    return INVALID_HANDLE_VALUE;
1069
0
  }
1070
1071
0
  if (!InitOnceExecuteOnce(&g_HandleInitializer, initializeHandles, nullptr, nullptr))
1072
0
    return INVALID_HANDLE_VALUE;
1073
1074
0
  WTSVirtualChannelManager* vcm =
1075
0
      (WTSVirtualChannelManager*)calloc(1, sizeof(WTSVirtualChannelManager));
1076
1077
0
  if (!vcm)
1078
0
    goto fail;
1079
1080
0
  vcm->client = client;
1081
0
  vcm->rdp = context->rdp;
1082
1083
0
  queueCallbacks.fnObjectFree = wts_virtual_channel_manager_free_message;
1084
0
  vcm->queue = MessageQueue_New(&queueCallbacks);
1085
1086
0
  if (!vcm->queue)
1087
0
    goto fail;
1088
1089
0
  vcm->dvc_channel_id_seq = 0;
1090
0
  vcm->dynamicVirtualChannels = HashTable_New(TRUE);
1091
1092
0
  if (!vcm->dynamicVirtualChannels)
1093
0
    goto fail;
1094
1095
0
  if (!HashTable_SetHashFunction(vcm->dynamicVirtualChannels, channelId_Hash))
1096
0
    goto fail;
1097
1098
0
  {
1099
0
    wObject* obj = HashTable_ValueObject(vcm->dynamicVirtualChannels);
1100
0
    WINPR_ASSERT(obj);
1101
0
    obj->fnObjectFree = array_channel_free;
1102
1103
0
    obj = HashTable_KeyObject(vcm->dynamicVirtualChannels);
1104
0
    obj->fnObjectEquals = dynChannelMatch;
1105
0
  }
1106
0
  client->ReceiveChannelData = WTSReceiveChannelData;
1107
0
  {
1108
0
    HashTable_Lock(g_ServerHandles);
1109
0
    vcm->SessionId = g_SessionId++;
1110
0
    const BOOL rc =
1111
0
        HashTable_Insert(g_ServerHandles, (void*)(UINT_PTR)vcm->SessionId, (void*)vcm);
1112
0
    HashTable_Unlock(g_ServerHandles);
1113
0
    if (!rc)
1114
0
      goto fail;
1115
0
  }
1116
1117
0
  HANDLE hServer = (HANDLE)vcm;
1118
0
  return hServer;
1119
1120
0
fail:
1121
0
  wtsCloseVCM(vcm, false);
1122
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1123
0
  return INVALID_HANDLE_VALUE;
1124
0
}
1125
1126
HANDLE WINAPI FreeRDP_WTSOpenServerExW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1127
0
{
1128
0
  WLog_ERR("TODO", "TODO: implement");
1129
0
  return INVALID_HANDLE_VALUE;
1130
0
}
1131
1132
HANDLE WINAPI FreeRDP_WTSOpenServerExA(LPSTR pServerName)
1133
0
{
1134
0
  return FreeRDP_WTSOpenServerA(pServerName);
1135
0
}
1136
1137
VOID WINAPI FreeRDP_WTSCloseServer(HANDLE hServer)
1138
0
{
1139
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
1140
0
  wtsCloseVCM(vcm, true);
1141
0
}
1142
1143
BOOL WINAPI FreeRDP_WTSEnumerateSessionsW(WINPR_ATTR_UNUSED HANDLE hServer,
1144
                                          WINPR_ATTR_UNUSED DWORD Reserved,
1145
                                          WINPR_ATTR_UNUSED DWORD Version,
1146
                                          WINPR_ATTR_UNUSED PWTS_SESSION_INFOW* ppSessionInfo,
1147
                                          WINPR_ATTR_UNUSED DWORD* pCount)
1148
0
{
1149
0
  WLog_ERR("TODO", "TODO: implement");
1150
0
  return FALSE;
1151
0
}
1152
1153
BOOL WINAPI FreeRDP_WTSEnumerateSessionsA(WINPR_ATTR_UNUSED HANDLE hServer,
1154
                                          WINPR_ATTR_UNUSED DWORD Reserved,
1155
                                          WINPR_ATTR_UNUSED DWORD Version,
1156
                                          WINPR_ATTR_UNUSED PWTS_SESSION_INFOA* ppSessionInfo,
1157
                                          WINPR_ATTR_UNUSED DWORD* pCount)
1158
0
{
1159
0
  WLog_ERR("TODO", "TODO: implement");
1160
0
  return FALSE;
1161
0
}
1162
1163
BOOL WINAPI FreeRDP_WTSEnumerateSessionsExW(WINPR_ATTR_UNUSED HANDLE hServer,
1164
                                            WINPR_ATTR_UNUSED DWORD* pLevel,
1165
                                            WINPR_ATTR_UNUSED DWORD Filter,
1166
                                            WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1W* ppSessionInfo,
1167
                                            WINPR_ATTR_UNUSED DWORD* pCount)
1168
0
{
1169
0
  WLog_ERR("TODO", "TODO: implement");
1170
0
  return FALSE;
1171
0
}
1172
1173
BOOL WINAPI FreeRDP_WTSEnumerateSessionsExA(WINPR_ATTR_UNUSED HANDLE hServer,
1174
                                            WINPR_ATTR_UNUSED DWORD* pLevel,
1175
                                            WINPR_ATTR_UNUSED DWORD Filter,
1176
                                            WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1A* ppSessionInfo,
1177
                                            WINPR_ATTR_UNUSED DWORD* pCount)
1178
0
{
1179
0
  WLog_ERR("TODO", "TODO: implement");
1180
0
  return FALSE;
1181
0
}
1182
1183
BOOL WINAPI FreeRDP_WTSEnumerateProcessesW(WINPR_ATTR_UNUSED HANDLE hServer,
1184
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1185
                                           WINPR_ATTR_UNUSED DWORD Version,
1186
                                           WINPR_ATTR_UNUSED PWTS_PROCESS_INFOW* ppProcessInfo,
1187
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1188
0
{
1189
0
  WLog_ERR("TODO", "TODO: implement");
1190
0
  return FALSE;
1191
0
}
1192
1193
BOOL WINAPI FreeRDP_WTSEnumerateProcessesA(WINPR_ATTR_UNUSED HANDLE hServer,
1194
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1195
                                           WINPR_ATTR_UNUSED DWORD Version,
1196
                                           WINPR_ATTR_UNUSED PWTS_PROCESS_INFOA* ppProcessInfo,
1197
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1198
0
{
1199
0
  WLog_ERR("TODO", "TODO: implement");
1200
0
  return FALSE;
1201
0
}
1202
1203
BOOL WINAPI FreeRDP_WTSTerminateProcess(WINPR_ATTR_UNUSED HANDLE hServer,
1204
                                        WINPR_ATTR_UNUSED DWORD ProcessId,
1205
                                        WINPR_ATTR_UNUSED DWORD ExitCode)
1206
0
{
1207
0
  WLog_ERR("TODO", "TODO: implement");
1208
0
  return FALSE;
1209
0
}
1210
1211
BOOL WINAPI FreeRDP_WTSQuerySessionInformationW(WINPR_ATTR_UNUSED HANDLE hServer,
1212
                                                WINPR_ATTR_UNUSED DWORD SessionId,
1213
                                                WINPR_ATTR_UNUSED WTS_INFO_CLASS WTSInfoClass,
1214
                                                WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1215
                                                WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1216
0
{
1217
0
  WLog_ERR("TODO", "TODO: implement");
1218
0
  return FALSE;
1219
0
}
1220
1221
BOOL WINAPI FreeRDP_WTSQuerySessionInformationA(HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1222
                                                WTS_INFO_CLASS WTSInfoClass, LPSTR* ppBuffer,
1223
                                                DWORD* pBytesReturned)
1224
0
{
1225
0
  DWORD BytesReturned = 0;
1226
0
  WTSVirtualChannelManager* vcm = nullptr;
1227
0
  vcm = (WTSVirtualChannelManager*)hServer;
1228
1229
0
  if (!vcm)
1230
0
    return FALSE;
1231
1232
0
  if (WTSInfoClass == WTSSessionId)
1233
0
  {
1234
0
    ULONG* pBuffer = nullptr;
1235
0
    BytesReturned = sizeof(ULONG);
1236
0
    pBuffer = (ULONG*)malloc(sizeof(BytesReturned));
1237
1238
0
    if (!pBuffer)
1239
0
    {
1240
0
      SetLastError(g_err_oom);
1241
0
      return FALSE;
1242
0
    }
1243
1244
0
    *pBuffer = vcm->SessionId;
1245
0
    *ppBuffer = (LPSTR)pBuffer;
1246
0
    *pBytesReturned = BytesReturned;
1247
0
    return TRUE;
1248
0
  }
1249
1250
0
  return FALSE;
1251
0
}
1252
1253
BOOL WINAPI FreeRDP_WTSQueryUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1254
                                        WINPR_ATTR_UNUSED LPWSTR pUserName,
1255
                                        WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1256
                                        WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1257
                                        WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1258
0
{
1259
0
  WLog_ERR("TODO", "TODO: implement");
1260
0
  return FALSE;
1261
0
}
1262
1263
BOOL WINAPI FreeRDP_WTSQueryUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1264
                                        WINPR_ATTR_UNUSED LPSTR pUserName,
1265
                                        WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1266
                                        WINPR_ATTR_UNUSED LPSTR* ppBuffer,
1267
                                        WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1268
0
{
1269
0
  WLog_ERR("TODO", "TODO: implement");
1270
0
  return FALSE;
1271
0
}
1272
1273
BOOL WINAPI FreeRDP_WTSSetUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1274
                                      WINPR_ATTR_UNUSED LPWSTR pUserName,
1275
                                      WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1276
                                      WINPR_ATTR_UNUSED LPWSTR pBuffer,
1277
                                      WINPR_ATTR_UNUSED DWORD DataLength)
1278
0
{
1279
0
  WLog_ERR("TODO", "TODO: implement");
1280
0
  return FALSE;
1281
0
}
1282
1283
BOOL WINAPI FreeRDP_WTSSetUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1284
                                      WINPR_ATTR_UNUSED LPSTR pUserName,
1285
                                      WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1286
                                      WINPR_ATTR_UNUSED LPSTR pBuffer,
1287
                                      WINPR_ATTR_UNUSED DWORD DataLength)
1288
0
{
1289
0
  WLog_ERR("TODO", "TODO: implement");
1290
0
  return FALSE;
1291
0
}
1292
1293
BOOL WINAPI
1294
FreeRDP_WTSSendMessageW(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1295
                        WINPR_ATTR_UNUSED LPWSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1296
                        WINPR_ATTR_UNUSED LPWSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1297
                        WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1298
                        WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1299
0
{
1300
0
  WLog_ERR("TODO", "TODO: implement");
1301
0
  return FALSE;
1302
0
}
1303
1304
BOOL WINAPI
1305
FreeRDP_WTSSendMessageA(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1306
                        WINPR_ATTR_UNUSED LPSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1307
                        WINPR_ATTR_UNUSED LPSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1308
                        WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1309
                        WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1310
0
{
1311
0
  WLog_ERR("TODO", "TODO: implement");
1312
0
  return FALSE;
1313
0
}
1314
1315
BOOL WINAPI FreeRDP_WTSDisconnectSession(WINPR_ATTR_UNUSED HANDLE hServer,
1316
                                         WINPR_ATTR_UNUSED DWORD SessionId,
1317
                                         WINPR_ATTR_UNUSED BOOL bWait)
1318
0
{
1319
0
  WLog_ERR("TODO", "TODO: implement");
1320
0
  return FALSE;
1321
0
}
1322
1323
BOOL WINAPI FreeRDP_WTSLogoffSession(WINPR_ATTR_UNUSED HANDLE hServer,
1324
                                     WINPR_ATTR_UNUSED DWORD SessionId,
1325
                                     WINPR_ATTR_UNUSED BOOL bWait)
1326
0
{
1327
0
  WLog_ERR("TODO", "TODO: implement");
1328
0
  return FALSE;
1329
0
}
1330
1331
BOOL WINAPI FreeRDP_WTSShutdownSystem(WINPR_ATTR_UNUSED HANDLE hServer,
1332
                                      WINPR_ATTR_UNUSED DWORD ShutdownFlag)
1333
0
{
1334
0
  WLog_ERR("TODO", "TODO: implement");
1335
0
  return FALSE;
1336
0
}
1337
1338
BOOL WINAPI FreeRDP_WTSWaitSystemEvent(WINPR_ATTR_UNUSED HANDLE hServer,
1339
                                       WINPR_ATTR_UNUSED DWORD EventMask,
1340
                                       WINPR_ATTR_UNUSED DWORD* pEventFlags)
1341
0
{
1342
0
  WLog_ERR("TODO", "TODO: implement");
1343
0
  return FALSE;
1344
0
}
1345
1346
static void peer_channel_queue_free_message(void* obj)
1347
0
{
1348
0
  wMessage* msg = (wMessage*)obj;
1349
0
  if (!msg)
1350
0
    return;
1351
1352
0
  free(msg->context);
1353
0
  msg->context = nullptr;
1354
0
}
1355
1356
static rdpPeerChannel* channel_new(WTSVirtualChannelManager* vcm, freerdp_peer* client,
1357
                                   UINT32 ChannelId, UINT16 index, UINT16 type, size_t chunkSize,
1358
                                   const char* name)
1359
0
{
1360
0
  wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1361
0
  queueCallbacks.fnObjectFree = peer_channel_queue_free_message;
1362
1363
0
  rdpPeerChannel* channel =
1364
0
      server_channel_common_new(client, index, ChannelId, chunkSize, &queueCallbacks, name);
1365
1366
0
  WINPR_ASSERT(vcm);
1367
0
  WINPR_ASSERT(client);
1368
1369
0
  if (!channel)
1370
0
    goto fail;
1371
1372
0
  channel->vcm = vcm;
1373
0
  channel->channelType = type;
1374
0
  channel->creationStatus =
1375
0
      (type == RDP_PEER_CHANNEL_TYPE_SVC) ? ERROR_SUCCESS : ERROR_OPERATION_IN_PROGRESS;
1376
1377
0
  return channel;
1378
0
fail:
1379
0
  channel_free(channel);
1380
0
  return nullptr;
1381
0
}
1382
1383
HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1384
                                            LPSTR pVirtualName)
1385
0
{
1386
0
  size_t length = 0;
1387
0
  rdpMcs* mcs = nullptr;
1388
0
  rdpMcsChannel* joined_channel = nullptr;
1389
0
  freerdp_peer* client = nullptr;
1390
0
  rdpPeerChannel* channel = nullptr;
1391
0
  WTSVirtualChannelManager* vcm = nullptr;
1392
0
  HANDLE hChannelHandle = nullptr;
1393
0
  rdpContext* context = nullptr;
1394
0
  vcm = (WTSVirtualChannelManager*)hServer;
1395
1396
0
  if (!vcm)
1397
0
  {
1398
0
    SetLastError(ERROR_INVALID_DATA);
1399
0
    return nullptr;
1400
0
  }
1401
1402
0
  client = vcm->client;
1403
0
  WINPR_ASSERT(client);
1404
1405
0
  context = client->context;
1406
0
  WINPR_ASSERT(context);
1407
0
  WINPR_ASSERT(context->rdp);
1408
0
  WINPR_ASSERT(context->settings);
1409
1410
0
  mcs = context->rdp->mcs;
1411
0
  WINPR_ASSERT(mcs);
1412
1413
0
  length = strnlen(pVirtualName, CHANNEL_NAME_LEN + 1);
1414
1415
0
  if (length > CHANNEL_NAME_LEN)
1416
0
  {
1417
0
    SetLastError(ERROR_NOT_FOUND);
1418
0
    return nullptr;
1419
0
  }
1420
1421
0
  UINT32 index = 0;
1422
0
  for (; index < mcs->channelCount; index++)
1423
0
  {
1424
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
1425
0
    if (mchannel->joined && (strncmp(mchannel->Name, pVirtualName, length) == 0))
1426
0
    {
1427
0
      joined_channel = mchannel;
1428
0
      break;
1429
0
    }
1430
0
  }
1431
1432
0
  if (!joined_channel)
1433
0
  {
1434
0
    SetLastError(ERROR_NOT_FOUND);
1435
0
    return nullptr;
1436
0
  }
1437
1438
0
  channel = (rdpPeerChannel*)joined_channel->handle;
1439
1440
0
  if (!channel)
1441
0
  {
1442
0
    const UINT32 VCChunkSize =
1443
0
        freerdp_settings_get_uint32(context->settings, FreeRDP_VCChunkSize);
1444
1445
0
    WINPR_ASSERT(index <= UINT16_MAX);
1446
0
    channel = channel_new(vcm, client, joined_channel->ChannelId, (UINT16)index,
1447
0
                          RDP_PEER_CHANNEL_TYPE_SVC, VCChunkSize, pVirtualName);
1448
1449
0
    if (!channel)
1450
0
      goto fail;
1451
1452
0
    joined_channel->handle = channel;
1453
0
  }
1454
1455
0
  hChannelHandle = (HANDLE)channel;
1456
0
  return hChannelHandle;
1457
0
fail:
1458
0
  channel_free(channel);
1459
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1460
0
  return nullptr;
1461
0
}
1462
1463
HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenEx(DWORD SessionId, LPSTR pVirtualName, DWORD flags)
1464
0
{
1465
0
  wStream* s = nullptr;
1466
0
  rdpPeerChannel* channel = nullptr;
1467
0
  BOOL joined = FALSE;
1468
0
  ULONG written = 0;
1469
1470
0
  if (SessionId == WTS_CURRENT_SESSION)
1471
0
    return nullptr;
1472
1473
0
  HashTable_Lock(g_ServerHandles);
1474
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)HashTable_GetItemValue(
1475
0
      g_ServerHandles, (void*)(UINT_PTR)SessionId);
1476
1477
0
  if (!vcm)
1478
0
    goto end;
1479
1480
0
  if (!(flags & WTS_CHANNEL_OPTION_DYNAMIC))
1481
0
  {
1482
0
    HashTable_Unlock(g_ServerHandles);
1483
0
    return FreeRDP_WTSVirtualChannelOpen((HANDLE)vcm, SessionId, pVirtualName);
1484
0
  }
1485
1486
0
  freerdp_peer* client = vcm->client;
1487
0
  WINPR_ASSERT(client);
1488
0
  WINPR_ASSERT(client->context);
1489
0
  WINPR_ASSERT(client->context->rdp);
1490
1491
0
  rdpMcs* mcs = client->context->rdp->mcs;
1492
0
  WINPR_ASSERT(mcs);
1493
1494
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
1495
0
  {
1496
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
1497
0
    if (mchannel->joined &&
1498
0
        (strncmp(mchannel->Name, DRDYNVC_SVC_CHANNEL_NAME, CHANNEL_NAME_LEN + 1) == 0))
1499
0
    {
1500
0
      joined = TRUE;
1501
0
      break;
1502
0
    }
1503
0
  }
1504
1505
0
  if (!joined)
1506
0
  {
1507
0
    SetLastError(ERROR_NOT_FOUND);
1508
0
    goto end;
1509
0
  }
1510
1511
0
  if (!vcm->drdynvc_channel || (vcm->drdynvc_state != DRDYNVC_STATE_READY))
1512
0
  {
1513
0
    SetLastError(ERROR_NOT_READY);
1514
0
    goto end;
1515
0
  }
1516
1517
0
  WINPR_ASSERT(client);
1518
0
  WINPR_ASSERT(client->context);
1519
0
  WINPR_ASSERT(client->context->settings);
1520
1521
0
  const UINT32 VCChunkSize =
1522
0
      freerdp_settings_get_uint32(client->context->settings, FreeRDP_VCChunkSize);
1523
0
  channel = channel_new(vcm, client, 0, 0, RDP_PEER_CHANNEL_TYPE_DVC, VCChunkSize, pVirtualName);
1524
1525
0
  if (!channel)
1526
0
  {
1527
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1528
0
    goto end;
1529
0
  }
1530
1531
0
  const LONG hdl = InterlockedIncrement(&vcm->dvc_channel_id_seq);
1532
0
  channel->channelId = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1533
1534
0
  if (!HashTable_Insert(vcm->dynamicVirtualChannels, &channel->channelId, channel))
1535
0
  {
1536
0
    channel_free(channel);
1537
0
    channel = nullptr;
1538
0
    goto fail;
1539
0
  }
1540
0
  s = Stream_New(nullptr, 64);
1541
1542
0
  if (!s)
1543
0
    goto fail;
1544
1545
0
  if (!wts_write_drdynvc_create_request(s, channel->channelId, pVirtualName))
1546
0
    goto fail;
1547
1548
0
  {
1549
0
    const size_t pos = Stream_GetPosition(s);
1550
0
    WINPR_ASSERT(pos <= UINT32_MAX);
1551
0
    if (!WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s, char), (UINT32)pos,
1552
0
                                &written))
1553
0
      goto fail;
1554
0
  }
1555
1556
0
end:
1557
0
  Stream_Free(s, TRUE);
1558
0
  HashTable_Unlock(g_ServerHandles);
1559
0
  return channel;
1560
1561
0
fail:
1562
0
  Stream_Free(s, TRUE);
1563
0
  if (channel)
1564
0
    HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1565
0
  HashTable_Unlock(g_ServerHandles);
1566
1567
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1568
0
  return nullptr;
1569
0
}
1570
1571
BOOL WINAPI FreeRDP_WTSVirtualChannelClose(HANDLE hChannelHandle)
1572
0
{
1573
0
  wStream* s = nullptr;
1574
0
  rdpMcs* mcs = nullptr;
1575
1576
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1577
0
  BOOL ret = TRUE;
1578
1579
0
  if (channel)
1580
0
  {
1581
0
    WTSVirtualChannelManager* vcm = channel->vcm;
1582
1583
0
    WINPR_ASSERT(vcm);
1584
0
    WINPR_ASSERT(vcm->client);
1585
0
    WINPR_ASSERT(vcm->client->context);
1586
0
    WINPR_ASSERT(vcm->client->context->rdp);
1587
0
    mcs = vcm->client->context->rdp->mcs;
1588
1589
0
    if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1590
0
    {
1591
0
      if (channel->index < mcs->channelCount)
1592
0
      {
1593
0
        rdpMcsChannel* cur = &mcs->channels[channel->index];
1594
0
        rdpPeerChannel* peerChannel = (rdpPeerChannel*)cur->handle;
1595
0
        channel_free(peerChannel);
1596
0
        cur->handle = nullptr;
1597
0
      }
1598
0
    }
1599
0
    else
1600
0
    {
1601
0
      if (channel->dvc_open_state == DVC_OPEN_STATE_SUCCEEDED)
1602
0
      {
1603
0
        ULONG written = 0;
1604
0
        s = Stream_New(nullptr, 8);
1605
1606
0
        if (!s)
1607
0
        {
1608
0
          WLog_ERR(TAG, "Stream_New failed!");
1609
0
          ret = FALSE;
1610
0
        }
1611
0
        else
1612
0
        {
1613
0
          wts_write_drdynvc_header(s, CLOSE_REQUEST_PDU, channel->channelId);
1614
1615
0
          const size_t pos = Stream_GetPosition(s);
1616
0
          WINPR_ASSERT(pos <= UINT32_MAX);
1617
0
          ret = WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s, char),
1618
0
                                       (UINT32)pos, &written);
1619
0
          Stream_Free(s, TRUE);
1620
0
        }
1621
0
      }
1622
0
      HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1623
0
    }
1624
0
  }
1625
1626
0
  return ret;
1627
0
}
1628
1629
BOOL WINAPI FreeRDP_WTSVirtualChannelRead(HANDLE hChannelHandle, WINPR_ATTR_UNUSED ULONG TimeOut,
1630
                                          PCHAR Buffer, ULONG BufferSize, PULONG pBytesRead)
1631
0
{
1632
0
  BYTE* buffer = nullptr;
1633
0
  wMessage message = WINPR_C_ARRAY_INIT;
1634
0
  wtsChannelMessage* messageCtx = nullptr;
1635
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1636
1637
0
  WINPR_ASSERT(channel);
1638
1639
0
  if (!MessageQueue_Peek(channel->queue, &message, FALSE))
1640
0
  {
1641
0
    SetLastError(ERROR_NO_DATA);
1642
0
    *pBytesRead = 0;
1643
0
    return FALSE;
1644
0
  }
1645
1646
0
  messageCtx = message.context;
1647
1648
0
  if (messageCtx == nullptr)
1649
0
    return FALSE;
1650
1651
0
  buffer = (BYTE*)(messageCtx + 1);
1652
0
  *pBytesRead = messageCtx->length - messageCtx->offset;
1653
1654
0
  if (Buffer == nullptr || BufferSize == 0)
1655
0
  {
1656
0
    return TRUE;
1657
0
  }
1658
1659
0
  if (*pBytesRead > BufferSize)
1660
0
    *pBytesRead = BufferSize;
1661
1662
0
  CopyMemory(Buffer, buffer + messageCtx->offset, *pBytesRead);
1663
0
  messageCtx->offset += *pBytesRead;
1664
1665
0
  if (messageCtx->offset >= messageCtx->length)
1666
0
  {
1667
0
    const int rc = MessageQueue_Peek(channel->queue, &message, TRUE);
1668
0
    peer_channel_queue_free_message(&message);
1669
0
    if (rc < 0)
1670
0
      return FALSE;
1671
0
  }
1672
1673
0
  return TRUE;
1674
0
}
1675
1676
BOOL WINAPI FreeRDP_WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer, ULONG uLength,
1677
                                           PULONG pBytesWritten)
1678
0
{
1679
0
  wStream* s = nullptr;
1680
0
  int cbLen = 0;
1681
0
  int cbChId = 0;
1682
0
  int first = 0;
1683
0
  BYTE* buffer = nullptr;
1684
0
  size_t totalWritten = 0;
1685
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1686
0
  BOOL ret = FALSE;
1687
1688
0
  if (!channel)
1689
0
    return FALSE;
1690
1691
0
  EnterCriticalSection(&channel->writeLock);
1692
0
  WINPR_ASSERT(channel->vcm);
1693
0
  if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1694
0
  {
1695
0
    buffer = (BYTE*)malloc(uLength);
1696
1697
0
    if (!buffer)
1698
0
    {
1699
0
      SetLastError(g_err_oom);
1700
0
      goto fail;
1701
0
    }
1702
1703
0
    CopyMemory(buffer, Buffer, uLength);
1704
0
    totalWritten = uLength;
1705
0
    if (!wts_queue_send_item(channel, buffer, uLength))
1706
0
      goto fail;
1707
0
  }
1708
0
  else if (!channel->vcm->drdynvc_channel || (channel->vcm->drdynvc_state != DRDYNVC_STATE_READY))
1709
0
  {
1710
0
    DEBUG_DVC("drdynvc not ready");
1711
0
    goto fail;
1712
0
  }
1713
0
  else
1714
0
  {
1715
0
    first = TRUE;
1716
1717
0
    size_t Length = uLength;
1718
0
    while (Length > 0)
1719
0
    {
1720
0
      s = Stream_New(nullptr, DVC_MAX_DATA_PDU_SIZE);
1721
1722
0
      if (!s)
1723
0
      {
1724
0
        WLog_ERR(TAG, "Stream_New failed!");
1725
0
        SetLastError(g_err_oom);
1726
0
        goto fail;
1727
0
      }
1728
1729
0
      buffer = Stream_Buffer(s);
1730
0
      Stream_Seek_UINT8(s);
1731
0
      cbChId = wts_write_variable_uint(s, channel->channelId);
1732
1733
0
      if (first && (Length > Stream_GetRemainingLength(s)))
1734
0
      {
1735
0
        cbLen = wts_write_variable_uint(s, WINPR_ASSERTING_INT_CAST(uint32_t, Length));
1736
0
        buffer[0] = ((DATA_FIRST_PDU << 4) | (cbLen << 2) | cbChId) & 0xFF;
1737
0
      }
1738
0
      else
1739
0
      {
1740
0
        buffer[0] = ((DATA_PDU << 4) | cbChId) & 0xFF;
1741
0
      }
1742
1743
0
      first = FALSE;
1744
0
      size_t written = Stream_GetRemainingLength(s);
1745
1746
0
      if (written > Length)
1747
0
        written = Length;
1748
1749
0
      Stream_Write(s, Buffer, written);
1750
0
      const size_t length = Stream_GetPosition(s);
1751
0
      Stream_Free(s, FALSE);
1752
0
      if (length > UINT32_MAX)
1753
0
        goto fail;
1754
0
      Length -= written;
1755
0
      Buffer += written;
1756
0
      totalWritten += written;
1757
0
      if (!wts_queue_send_item(channel->vcm->drdynvc_channel, buffer, (UINT32)length))
1758
0
        goto fail;
1759
0
    }
1760
0
  }
1761
1762
0
  if (pBytesWritten)
1763
0
    *pBytesWritten = WINPR_ASSERTING_INT_CAST(uint32_t, totalWritten);
1764
1765
0
  ret = TRUE;
1766
0
fail:
1767
0
  LeaveCriticalSection(&channel->writeLock);
1768
0
  return ret;
1769
0
}
1770
1771
BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeInput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1772
0
{
1773
0
  WLog_ERR("TODO", "TODO: implement");
1774
0
  return TRUE;
1775
0
}
1776
1777
BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeOutput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1778
0
{
1779
0
  WLog_ERR("TODO", "TODO: implement");
1780
0
  return TRUE;
1781
0
}
1782
1783
BOOL WINAPI FreeRDP_WTSVirtualChannelQuery(HANDLE hChannelHandle, WTS_VIRTUAL_CLASS WtsVirtualClass,
1784
                                           PVOID* ppBuffer, DWORD* pBytesReturned)
1785
0
{
1786
0
  void* pfd = nullptr;
1787
0
  BOOL bval = 0;
1788
0
  void* fds[10] = WINPR_C_ARRAY_INIT;
1789
0
  HANDLE hEvent = nullptr;
1790
0
  int fds_count = 0;
1791
0
  BOOL status = FALSE;
1792
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1793
1794
0
  WINPR_ASSERT(channel);
1795
1796
0
  switch ((UINT32)WtsVirtualClass)
1797
0
  {
1798
0
    case WTSVirtualFileHandle:
1799
0
      hEvent = MessageQueue_Event(channel->queue);
1800
0
      pfd = GetEventWaitObject(hEvent);
1801
1802
0
      if (pfd)
1803
0
      {
1804
0
        fds[fds_count] = pfd;
1805
0
        (fds_count)++;
1806
0
      }
1807
1808
0
      *ppBuffer = malloc(sizeof(void*));
1809
1810
0
      if (!*ppBuffer)
1811
0
      {
1812
0
        SetLastError(g_err_oom);
1813
0
      }
1814
0
      else
1815
0
      {
1816
0
        CopyMemory(*ppBuffer, (void*)&fds[0], sizeof(void*));
1817
0
        *pBytesReturned = sizeof(void*);
1818
0
        status = TRUE;
1819
0
      }
1820
1821
0
      break;
1822
1823
0
    case WTSVirtualEventHandle:
1824
0
      hEvent = MessageQueue_Event(channel->queue);
1825
1826
0
      *ppBuffer = malloc(sizeof(HANDLE));
1827
1828
0
      if (!*ppBuffer)
1829
0
      {
1830
0
        SetLastError(g_err_oom);
1831
0
      }
1832
0
      else
1833
0
      {
1834
0
        CopyMemory(*ppBuffer, (void*)&hEvent, sizeof(HANDLE));
1835
0
        *pBytesReturned = sizeof(void*);
1836
0
        status = TRUE;
1837
0
      }
1838
1839
0
      break;
1840
1841
0
    case WTSVirtualChannelReady:
1842
0
      if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1843
0
      {
1844
0
        bval = TRUE;
1845
0
        status = TRUE;
1846
0
      }
1847
0
      else
1848
0
      {
1849
0
        switch (channel->dvc_open_state)
1850
0
        {
1851
0
          case DVC_OPEN_STATE_NONE:
1852
0
            bval = FALSE;
1853
0
            status = TRUE;
1854
0
            break;
1855
1856
0
          case DVC_OPEN_STATE_SUCCEEDED:
1857
0
            bval = TRUE;
1858
0
            status = TRUE;
1859
0
            break;
1860
1861
0
          default:
1862
0
            *ppBuffer = nullptr;
1863
0
            *pBytesReturned = 0;
1864
0
            return FALSE;
1865
0
        }
1866
0
      }
1867
1868
0
      *ppBuffer = malloc(sizeof(BOOL));
1869
1870
0
      if (!*ppBuffer)
1871
0
      {
1872
0
        SetLastError(g_err_oom);
1873
0
        status = FALSE;
1874
0
      }
1875
0
      else
1876
0
      {
1877
0
        CopyMemory(*ppBuffer, &bval, sizeof(BOOL));
1878
0
        *pBytesReturned = sizeof(BOOL);
1879
0
      }
1880
1881
0
      break;
1882
0
    case WTSVirtualChannelOpenStatus:
1883
0
    {
1884
0
      INT32 value = channel->creationStatus;
1885
0
      status = TRUE;
1886
1887
0
      *ppBuffer = malloc(sizeof(value));
1888
0
      if (!*ppBuffer)
1889
0
      {
1890
0
        SetLastError(g_err_oom);
1891
0
        status = FALSE;
1892
0
      }
1893
0
      else
1894
0
      {
1895
0
        CopyMemory(*ppBuffer, &value, sizeof(value));
1896
0
        *pBytesReturned = sizeof(value);
1897
0
      }
1898
0
      break;
1899
0
    }
1900
0
    default:
1901
0
      break;
1902
0
  }
1903
1904
0
  return status;
1905
0
}
1906
1907
VOID WINAPI FreeRDP_WTSFreeMemory(PVOID pMemory)
1908
0
{
1909
0
  free(pMemory);
1910
0
}
1911
1912
BOOL WINAPI FreeRDP_WTSFreeMemoryExW(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
1913
                                     WINPR_ATTR_UNUSED PVOID pMemory,
1914
                                     WINPR_ATTR_UNUSED ULONG NumberOfEntries)
1915
0
{
1916
0
  WLog_ERR("TODO", "TODO: implement");
1917
0
  return FALSE;
1918
0
}
1919
1920
BOOL WINAPI FreeRDP_WTSFreeMemoryExA(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
1921
                                     WINPR_ATTR_UNUSED PVOID pMemory,
1922
                                     WINPR_ATTR_UNUSED ULONG NumberOfEntries)
1923
0
{
1924
0
  WLog_ERR("TODO", "TODO: implement");
1925
0
  return FALSE;
1926
0
}
1927
1928
BOOL WINAPI FreeRDP_WTSRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd,
1929
                                                   WINPR_ATTR_UNUSED DWORD dwFlags)
1930
0
{
1931
0
  WLog_ERR("TODO", "TODO: implement");
1932
0
  return FALSE;
1933
0
}
1934
1935
BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd)
1936
0
{
1937
0
  WLog_ERR("TODO", "TODO: implement");
1938
0
  return FALSE;
1939
0
}
1940
1941
BOOL WINAPI FreeRDP_WTSRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
1942
                                                     WINPR_ATTR_UNUSED HWND hWnd,
1943
                                                     WINPR_ATTR_UNUSED DWORD dwFlags)
1944
0
{
1945
0
  WLog_ERR("TODO", "TODO: implement");
1946
0
  return FALSE;
1947
0
}
1948
1949
BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
1950
                                                       WINPR_ATTR_UNUSED HWND hWnd)
1951
0
{
1952
0
  WLog_ERR("TODO", "TODO: implement");
1953
0
  return FALSE;
1954
0
}
1955
1956
BOOL WINAPI FreeRDP_WTSQueryUserToken(WINPR_ATTR_UNUSED ULONG SessionId,
1957
                                      WINPR_ATTR_UNUSED PHANDLE phToken)
1958
0
{
1959
0
  WLog_ERR("TODO", "TODO: implement");
1960
0
  return FALSE;
1961
0
}
1962
1963
BOOL WINAPI FreeRDP_WTSEnumerateProcessesExW(WINPR_ATTR_UNUSED HANDLE hServer,
1964
                                             WINPR_ATTR_UNUSED DWORD* pLevel,
1965
                                             WINPR_ATTR_UNUSED DWORD SessionId,
1966
                                             WINPR_ATTR_UNUSED LPWSTR* ppProcessInfo,
1967
                                             WINPR_ATTR_UNUSED DWORD* pCount)
1968
0
{
1969
0
  WLog_ERR("TODO", "TODO: implement");
1970
0
  return FALSE;
1971
0
}
1972
1973
BOOL WINAPI FreeRDP_WTSEnumerateProcessesExA(WINPR_ATTR_UNUSED HANDLE hServer,
1974
                                             WINPR_ATTR_UNUSED DWORD* pLevel,
1975
                                             WINPR_ATTR_UNUSED DWORD SessionId,
1976
                                             WINPR_ATTR_UNUSED LPSTR* ppProcessInfo,
1977
                                             WINPR_ATTR_UNUSED DWORD* pCount)
1978
0
{
1979
0
  WLog_ERR("TODO", "TODO: implement");
1980
0
  return FALSE;
1981
0
}
1982
1983
BOOL WINAPI FreeRDP_WTSEnumerateListenersW(WINPR_ATTR_UNUSED HANDLE hServer,
1984
                                           WINPR_ATTR_UNUSED PVOID pReserved,
1985
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1986
                                           WINPR_ATTR_UNUSED PWTSLISTENERNAMEW pListeners,
1987
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1988
0
{
1989
0
  WLog_ERR("TODO", "TODO: implement");
1990
0
  return FALSE;
1991
0
}
1992
1993
BOOL WINAPI FreeRDP_WTSEnumerateListenersA(WINPR_ATTR_UNUSED HANDLE hServer,
1994
                                           WINPR_ATTR_UNUSED PVOID pReserved,
1995
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1996
                                           WINPR_ATTR_UNUSED PWTSLISTENERNAMEA pListeners,
1997
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1998
0
{
1999
0
  WLog_ERR("TODO", "TODO: implement");
2000
0
  return FALSE;
2001
0
}
2002
2003
BOOL WINAPI FreeRDP_WTSQueryListenerConfigW(WINPR_ATTR_UNUSED HANDLE hServer,
2004
                                            WINPR_ATTR_UNUSED PVOID pReserved,
2005
                                            WINPR_ATTR_UNUSED DWORD Reserved,
2006
                                            WINPR_ATTR_UNUSED LPWSTR pListenerName,
2007
                                            WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer)
2008
0
{
2009
0
  WLog_ERR("TODO", "TODO: implement");
2010
0
  return FALSE;
2011
0
}
2012
2013
BOOL WINAPI FreeRDP_WTSQueryListenerConfigA(WINPR_ATTR_UNUSED HANDLE hServer,
2014
                                            WINPR_ATTR_UNUSED PVOID pReserved,
2015
                                            WINPR_ATTR_UNUSED DWORD Reserved,
2016
                                            WINPR_ATTR_UNUSED LPSTR pListenerName,
2017
                                            WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer)
2018
0
{
2019
0
  WLog_ERR("TODO", "TODO: implement");
2020
0
  return FALSE;
2021
0
}
2022
2023
BOOL WINAPI FreeRDP_WTSCreateListenerW(WINPR_ATTR_UNUSED HANDLE hServer,
2024
                                       WINPR_ATTR_UNUSED PVOID pReserved,
2025
                                       WINPR_ATTR_UNUSED DWORD Reserved,
2026
                                       WINPR_ATTR_UNUSED LPWSTR pListenerName,
2027
                                       WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer,
2028
                                       WINPR_ATTR_UNUSED DWORD flag)
2029
0
{
2030
0
  WLog_ERR("TODO", "TODO: implement");
2031
0
  return FALSE;
2032
0
}
2033
2034
BOOL WINAPI FreeRDP_WTSCreateListenerA(WINPR_ATTR_UNUSED HANDLE hServer,
2035
                                       WINPR_ATTR_UNUSED PVOID pReserved,
2036
                                       WINPR_ATTR_UNUSED DWORD Reserved,
2037
                                       WINPR_ATTR_UNUSED LPSTR pListenerName,
2038
                                       WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer,
2039
                                       WINPR_ATTR_UNUSED DWORD flag)
2040
0
{
2041
0
  WLog_ERR("TODO", "TODO: implement");
2042
0
  return FALSE;
2043
0
}
2044
2045
BOOL WINAPI FreeRDP_WTSSetListenerSecurityW(
2046
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2047
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2048
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2049
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2050
0
{
2051
0
  WLog_ERR("TODO", "TODO: implement");
2052
0
  return FALSE;
2053
0
}
2054
2055
BOOL WINAPI FreeRDP_WTSSetListenerSecurityA(
2056
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2057
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2058
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2059
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2060
0
{
2061
0
  WLog_ERR("TODO", "TODO: implement");
2062
0
  return FALSE;
2063
0
}
2064
2065
BOOL WINAPI FreeRDP_WTSGetListenerSecurityW(
2066
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2067
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2068
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2069
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2070
    WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2071
0
{
2072
0
  WLog_ERR("TODO", "TODO: implement");
2073
0
  return FALSE;
2074
0
}
2075
2076
BOOL WINAPI FreeRDP_WTSGetListenerSecurityA(
2077
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2078
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2079
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2080
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2081
    WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2082
0
{
2083
0
  WLog_ERR("TODO", "TODO: implement");
2084
0
  return FALSE;
2085
0
}
2086
2087
BOOL CDECL FreeRDP_WTSEnableChildSessions(WINPR_ATTR_UNUSED BOOL bEnable)
2088
0
{
2089
0
  WLog_ERR("TODO", "TODO: implement");
2090
0
  return FALSE;
2091
0
}
2092
2093
BOOL CDECL FreeRDP_WTSIsChildSessionsEnabled(WINPR_ATTR_UNUSED PBOOL pbEnabled)
2094
0
{
2095
0
  WLog_ERR("TODO", "TODO: implement");
2096
0
  return FALSE;
2097
0
}
2098
2099
BOOL CDECL FreeRDP_WTSGetChildSessionId(WINPR_ATTR_UNUSED PULONG pSessionId)
2100
0
{
2101
0
  WLog_ERR("TODO", "TODO: implement");
2102
0
  return FALSE;
2103
0
}
2104
2105
DWORD WINAPI FreeRDP_WTSGetActiveConsoleSessionId(void)
2106
0
{
2107
0
  WLog_ERR("TODO", "TODO: implement");
2108
0
  return 0xFFFFFFFF;
2109
0
}
2110
BOOL WINAPI FreeRDP_WTSLogoffUser(WINPR_ATTR_UNUSED HANDLE hServer)
2111
0
{
2112
0
  WLog_ERR("TODO", "TODO: implement");
2113
0
  return FALSE;
2114
0
}
2115
2116
BOOL WINAPI FreeRDP_WTSLogonUser(WINPR_ATTR_UNUSED HANDLE hServer,
2117
                                 WINPR_ATTR_UNUSED LPCSTR username,
2118
                                 WINPR_ATTR_UNUSED LPCSTR password, WINPR_ATTR_UNUSED LPCSTR domain)
2119
0
{
2120
0
  WLog_ERR("TODO", "TODO: implement");
2121
0
  return FALSE;
2122
0
}
2123
2124
void server_channel_common_free(rdpPeerChannel* channel)
2125
0
{
2126
0
  if (!channel)
2127
0
    return;
2128
0
  MessageQueue_Free(channel->queue);
2129
0
  Stream_Free(channel->receiveData, TRUE);
2130
0
  DeleteCriticalSection(&channel->writeLock);
2131
0
  free(channel);
2132
0
}
2133
2134
rdpPeerChannel* server_channel_common_new(freerdp_peer* client, UINT16 index, UINT32 channelId,
2135
                                          size_t chunkSize, const wObject* callback,
2136
                                          const char* name)
2137
0
{
2138
0
  rdpPeerChannel* channel = (rdpPeerChannel*)calloc(1, sizeof(rdpPeerChannel));
2139
0
  if (!channel)
2140
0
    return nullptr;
2141
2142
0
  InitializeCriticalSection(&channel->writeLock);
2143
2144
0
  channel->receiveData = Stream_New(nullptr, chunkSize);
2145
0
  if (!channel->receiveData)
2146
0
    goto fail;
2147
2148
0
  channel->queue = MessageQueue_New(callback);
2149
0
  if (!channel->queue)
2150
0
    goto fail;
2151
2152
0
  channel->index = index;
2153
0
  channel->client = client;
2154
0
  channel->channelId = channelId;
2155
0
  strncpy(channel->channelName, name, ARRAYSIZE(channel->channelName) - 1);
2156
0
  return channel;
2157
0
fail:
2158
0
  WINPR_PRAGMA_DIAG_PUSH
2159
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2160
0
  server_channel_common_free(channel);
2161
0
  WINPR_PRAGMA_DIAG_POP
2162
0
  return nullptr;
2163
0
}