Coverage Report

Created: 2026-05-30 06:46

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
  WINPR_ASSERT(s);
422
423
0
  BYTE* bm = Stream_PointerAs(s, BYTE);
424
0
  Stream_Seek_UINT8(s);
425
0
  const int cbChId = wts_write_variable_uint(s, ChannelId);
426
0
  *bm = (((Cmd & 0x0F) << 4) | cbChId) & 0xFF;
427
0
}
428
429
static BOOL wts_write_drdynvc_create_request(wStream* s, UINT32 ChannelId, const char* ChannelName)
430
0
{
431
0
  size_t len = 0;
432
433
0
  WINPR_ASSERT(s);
434
0
  WINPR_ASSERT(ChannelName);
435
436
0
  wts_write_drdynvc_header(s, CREATE_REQUEST_PDU, ChannelId);
437
0
  len = strlen(ChannelName) + 1;
438
439
0
  if (!Stream_EnsureRemainingCapacity(s, len))
440
0
    return FALSE;
441
442
0
  Stream_Write(s, ChannelName, len);
443
0
  return TRUE;
444
0
}
445
446
static BOOL WTSProcessChannelData(rdpPeerChannel* channel, UINT16 channelId, const BYTE* data,
447
                                  size_t s, UINT32 flags, size_t t)
448
0
{
449
0
  BOOL ret = TRUE;
450
0
  const size_t size = s;
451
0
  const size_t totalSize = t;
452
453
0
  WINPR_ASSERT(channel);
454
0
  WINPR_ASSERT(channel->vcm);
455
0
  WINPR_UNUSED(channelId);
456
457
0
  if (flags & CHANNEL_FLAG_FIRST)
458
0
  {
459
0
    Stream_ResetPosition(channel->receiveData);
460
0
  }
461
462
0
  if (!Stream_EnsureRemainingCapacity(channel->receiveData, size))
463
0
    return FALSE;
464
465
0
  Stream_Write(channel->receiveData, data, size);
466
467
0
  if (flags & CHANNEL_FLAG_LAST)
468
0
  {
469
0
    if (Stream_GetPosition(channel->receiveData) != totalSize)
470
0
    {
471
0
      WLog_ERR(TAG, "read error");
472
0
    }
473
474
0
    if (channel == channel->vcm->drdynvc_channel)
475
0
    {
476
0
      ret = wts_read_drdynvc_pdu(channel);
477
0
    }
478
0
    else
479
0
    {
480
0
      const size_t pos = Stream_GetPosition(channel->receiveData);
481
0
      if (pos > UINT32_MAX)
482
0
        ret = FALSE;
483
0
      else
484
0
        ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData),
485
0
                                     (UINT32)pos);
486
0
    }
487
488
0
    Stream_ResetPosition(channel->receiveData);
489
0
  }
490
491
0
  return ret;
492
0
}
493
494
static BOOL WTSReceiveChannelData(freerdp_peer* client, UINT16 channelId, const BYTE* data,
495
                                  size_t size, UINT32 flags, size_t totalSize)
496
0
{
497
0
  rdpMcs* mcs = nullptr;
498
499
0
  WINPR_ASSERT(client);
500
0
  WINPR_ASSERT(client->context);
501
0
  WINPR_ASSERT(client->context->rdp);
502
503
0
  mcs = client->context->rdp->mcs;
504
0
  WINPR_ASSERT(mcs);
505
506
0
  for (UINT32 i = 0; i < mcs->channelCount; i++)
507
0
  {
508
0
    rdpMcsChannel* cur = &mcs->channels[i];
509
0
    if (cur->ChannelId == channelId)
510
0
    {
511
0
      rdpPeerChannel* channel = (rdpPeerChannel*)cur->handle;
512
513
0
      if (channel)
514
0
        return WTSProcessChannelData(channel, channelId, data, size, flags, totalSize);
515
0
    }
516
0
  }
517
518
0
  WLog_WARN(TAG, "unknown channelId %" PRIu16 " ignored", channelId);
519
520
0
  return TRUE;
521
0
}
522
523
#if defined(WITH_FREERDP_DEPRECATED)
524
void WTSVirtualChannelManagerGetFileDescriptor(HANDLE hServer, void** fds, int* fds_count)
525
{
526
  void* fd = nullptr;
527
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
528
  WINPR_ASSERT(vcm);
529
  WINPR_ASSERT(fds);
530
  WINPR_ASSERT(fds_count);
531
532
  fd = GetEventWaitObject(MessageQueue_Event(vcm->queue));
533
534
  if (fd)
535
  {
536
    fds[*fds_count] = fd;
537
    (*fds_count)++;
538
  }
539
540
#if 0
541
542
  if (vcm->drdynvc_channel)
543
  {
544
    fd = GetEventWaitObject(vcm->drdynvc_channel->receiveEvent);
545
546
    if (fd)
547
    {
548
      fds[*fds_count] = fd;
549
      (*fds_count)++;
550
    }
551
  }
552
553
#endif
554
}
555
#endif
556
557
BOOL WTSVirtualChannelManagerOpen(HANDLE hServer)
558
0
{
559
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
560
561
0
  if (!vcm)
562
0
    return FALSE;
563
564
0
  if (vcm->drdynvc_state == DRDYNVC_STATE_NONE)
565
0
  {
566
0
    rdpPeerChannel* channel = nullptr;
567
568
    /* Initialize drdynvc channel once and only once. */
569
0
    vcm->drdynvc_state = DRDYNVC_STATE_INITIALIZED;
570
0
    channel = (rdpPeerChannel*)WTSVirtualChannelOpen((HANDLE)vcm, WTS_CURRENT_SESSION,
571
0
                                                     DRDYNVC_SVC_CHANNEL_NAME);
572
573
0
    if (channel)
574
0
    {
575
0
      BYTE capaBuffer[12] = WINPR_C_ARRAY_INIT;
576
0
      wStream staticS = WINPR_C_ARRAY_INIT;
577
0
      wStream* s = Stream_StaticInit(&staticS, capaBuffer, sizeof(capaBuffer));
578
579
0
      vcm->drdynvc_channel = channel;
580
0
      vcm->dvc_spoken_version = 1;
581
0
      Stream_Write_UINT8(s, 0x50);    /* Cmd=5 sp=0 cbId=0 */
582
0
      Stream_Write_UINT8(s, 0x00);    /* Pad */
583
0
      Stream_Write_UINT16(s, 0x0001); /* Version */
584
585
      /* TODO: shall implement version 2 and 3 */
586
587
0
      const size_t pos = Stream_GetPosition(s);
588
0
      WINPR_ASSERT(pos <= UINT32_MAX);
589
0
      ULONG written = 0;
590
0
      if (!WTSVirtualChannelWrite(channel, (PCHAR)capaBuffer, (UINT32)pos, &written))
591
0
        return FALSE;
592
0
    }
593
0
  }
594
595
0
  return TRUE;
596
0
}
597
598
BOOL WTSVirtualChannelManagerCheckFileDescriptorEx(HANDLE hServer, BOOL autoOpen)
599
0
{
600
0
  wMessage message = WINPR_C_ARRAY_INIT;
601
0
  BOOL status = TRUE;
602
0
  WTSVirtualChannelManager* vcm = nullptr;
603
604
0
  if (!hServer || hServer == INVALID_HANDLE_VALUE)
605
0
    return FALSE;
606
607
0
  vcm = (WTSVirtualChannelManager*)hServer;
608
609
0
  if (autoOpen)
610
0
  {
611
0
    if (!WTSVirtualChannelManagerOpen(hServer))
612
0
      return FALSE;
613
0
  }
614
615
0
  while (MessageQueue_Peek(vcm->queue, &message, TRUE))
616
0
  {
617
0
    BYTE* buffer = nullptr;
618
0
    UINT32 length = 0;
619
0
    UINT16 channelId = 0;
620
0
    channelId = (UINT16)(UINT_PTR)message.context;
621
0
    buffer = (BYTE*)message.wParam;
622
0
    length = (UINT32)(UINT_PTR)message.lParam;
623
624
0
    WINPR_ASSERT(vcm->client);
625
0
    WINPR_ASSERT(vcm->client->SendChannelData);
626
0
    if (!vcm->client->SendChannelData(vcm->client, channelId, buffer, length))
627
0
    {
628
0
      status = FALSE;
629
0
    }
630
631
0
    free(buffer);
632
633
0
    if (!status)
634
0
      break;
635
0
  }
636
637
0
  return status;
638
0
}
639
640
BOOL WTSVirtualChannelManagerCheckFileDescriptor(HANDLE hServer)
641
0
{
642
0
  return WTSVirtualChannelManagerCheckFileDescriptorEx(hServer, TRUE);
643
0
}
644
645
HANDLE WTSVirtualChannelManagerGetEventHandle(HANDLE hServer)
646
0
{
647
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
648
0
  WINPR_ASSERT(vcm);
649
0
  return MessageQueue_Event(vcm->queue);
650
0
}
651
652
static rdpMcsChannel* wts_get_joined_channel_by_name(rdpMcs* mcs, const char* channel_name)
653
0
{
654
0
  if (!mcs || !channel_name || !strnlen(channel_name, CHANNEL_NAME_LEN + 1))
655
0
    return nullptr;
656
657
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
658
0
  {
659
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
660
0
    if (mchannel->joined)
661
0
    {
662
0
      if (_strnicmp(mchannel->Name, channel_name, CHANNEL_NAME_LEN + 1) == 0)
663
0
        return mchannel;
664
0
    }
665
0
  }
666
667
0
  return nullptr;
668
0
}
669
670
static rdpMcsChannel* wts_get_joined_channel_by_id(rdpMcs* mcs, const UINT16 channel_id)
671
0
{
672
0
  if (!mcs || !channel_id)
673
0
    return nullptr;
674
675
0
  WINPR_ASSERT(mcs->channels);
676
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
677
0
  {
678
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
679
0
    if (mchannel->joined)
680
0
    {
681
0
      if (mchannel->ChannelId == channel_id)
682
0
        return &mcs->channels[index];
683
0
    }
684
0
  }
685
686
0
  return nullptr;
687
0
}
688
689
BOOL WTSIsChannelJoinedByName(freerdp_peer* client, const char* channel_name)
690
0
{
691
0
  if (!client || !client->context || !client->context->rdp)
692
0
    return FALSE;
693
694
0
  return (wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name) != nullptr);
695
0
}
696
697
BOOL WTSIsChannelJoinedById(freerdp_peer* client, UINT16 channel_id)
698
0
{
699
0
  if (!client || !client->context || !client->context->rdp)
700
0
    return FALSE;
701
702
0
  return (wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id) != nullptr);
703
0
}
704
705
BOOL WTSVirtualChannelManagerIsChannelJoined(HANDLE hServer, const char* name)
706
0
{
707
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
708
709
0
  if (!vcm || !vcm->rdp)
710
0
    return FALSE;
711
712
0
  return (wts_get_joined_channel_by_name(vcm->rdp->mcs, name) != nullptr);
713
0
}
714
715
BYTE WTSVirtualChannelManagerGetDrdynvcState(HANDLE hServer)
716
0
{
717
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
718
0
  WINPR_ASSERT(vcm);
719
0
  return vcm->drdynvc_state;
720
0
}
721
722
void WTSVirtualChannelManagerSetDVCCreationCallback(HANDLE hServer, psDVCCreationStatusCallback cb,
723
                                                    void* userdata)
724
0
{
725
0
  WTSVirtualChannelManager* vcm = hServer;
726
727
0
  WINPR_ASSERT(vcm);
728
729
0
  vcm->dvc_creation_status = cb;
730
0
  vcm->dvc_creation_status_userdata = userdata;
731
0
}
732
733
UINT16 WTSChannelGetId(freerdp_peer* client, const char* channel_name)
734
0
{
735
0
  rdpMcsChannel* channel = nullptr;
736
737
0
  WINPR_ASSERT(channel_name);
738
0
  if (!client || !client->context || !client->context->rdp)
739
0
    return 0;
740
741
0
  channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
742
743
0
  if (!channel)
744
0
    return 0;
745
746
0
  return channel->ChannelId;
747
0
}
748
749
UINT32 WTSChannelGetIdByHandle(HANDLE hChannelHandle)
750
0
{
751
0
  rdpPeerChannel* channel = hChannelHandle;
752
753
0
  WINPR_ASSERT(channel);
754
755
0
  return channel->channelId;
756
0
}
757
758
BOOL WTSChannelSetHandleByName(freerdp_peer* client, const char* channel_name, void* handle)
759
0
{
760
0
  rdpMcsChannel* channel = nullptr;
761
762
0
  WINPR_ASSERT(channel_name);
763
0
  if (!client || !client->context || !client->context->rdp)
764
0
    return FALSE;
765
766
0
  channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
767
768
0
  if (!channel)
769
0
    return FALSE;
770
771
0
  channel->handle = handle;
772
0
  return TRUE;
773
0
}
774
775
BOOL WTSChannelSetHandleById(freerdp_peer* client, UINT16 channel_id, void* handle)
776
0
{
777
0
  rdpMcsChannel* channel = nullptr;
778
779
0
  if (!client || !client->context || !client->context->rdp)
780
0
    return FALSE;
781
782
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
783
784
0
  if (!channel)
785
0
    return FALSE;
786
787
0
  channel->handle = handle;
788
0
  return TRUE;
789
0
}
790
791
void* WTSChannelGetHandleByName(freerdp_peer* client, const char* channel_name)
792
0
{
793
0
  rdpMcsChannel* channel = nullptr;
794
795
0
  WINPR_ASSERT(channel_name);
796
0
  if (!client || !client->context || !client->context->rdp)
797
0
    return nullptr;
798
799
0
  channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
800
801
0
  if (!channel)
802
0
    return nullptr;
803
804
0
  return channel->handle;
805
0
}
806
807
void* WTSChannelGetHandleById(freerdp_peer* client, UINT16 channel_id)
808
0
{
809
0
  rdpMcsChannel* channel = nullptr;
810
811
0
  if (!client || !client->context || !client->context->rdp)
812
0
    return nullptr;
813
814
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
815
816
0
  if (!channel)
817
0
    return nullptr;
818
819
0
  return channel->handle;
820
0
}
821
822
const char* WTSChannelGetName(freerdp_peer* client, UINT16 channel_id)
823
0
{
824
0
  rdpMcsChannel* channel = nullptr;
825
826
0
  if (!client || !client->context || !client->context->rdp)
827
0
    return nullptr;
828
829
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
830
831
0
  if (!channel)
832
0
    return nullptr;
833
834
0
  return (const char*)channel->Name;
835
0
}
836
837
char** WTSGetAcceptedChannelNames(freerdp_peer* client, size_t* count)
838
0
{
839
0
  rdpMcs* mcs = nullptr;
840
0
  char** names = nullptr;
841
842
0
  if (!client || !client->context || !count)
843
0
    return nullptr;
844
845
0
  WINPR_ASSERT(client->context->rdp);
846
0
  mcs = client->context->rdp->mcs;
847
0
  WINPR_ASSERT(mcs);
848
0
  *count = mcs->channelCount;
849
850
0
  names = (char**)calloc(mcs->channelCount, sizeof(char*));
851
0
  if (!names)
852
0
    return nullptr;
853
854
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
855
0
  {
856
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
857
0
    names[index] = mchannel->Name;
858
0
  }
859
860
0
  return names;
861
0
}
862
863
INT64 WTSChannelGetOptions(freerdp_peer* client, UINT16 channel_id)
864
0
{
865
0
  rdpMcsChannel* channel = nullptr;
866
867
0
  if (!client || !client->context || !client->context->rdp)
868
0
    return -1;
869
870
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
871
872
0
  if (!channel)
873
0
    return -1;
874
875
0
  return (INT64)channel->options;
876
0
}
877
878
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionW(WINPR_ATTR_UNUSED LPWSTR pTargetServerName,
879
                                                  WINPR_ATTR_UNUSED ULONG TargetLogonId,
880
                                                  WINPR_ATTR_UNUSED BYTE HotkeyVk,
881
                                                  WINPR_ATTR_UNUSED USHORT HotkeyModifiers)
882
0
{
883
0
  WLog_ERR("TODO", "TODO: implement");
884
0
  return FALSE;
885
0
}
886
887
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionA(WINPR_ATTR_UNUSED LPSTR pTargetServerName,
888
                                                  WINPR_ATTR_UNUSED ULONG TargetLogonId,
889
                                                  WINPR_ATTR_UNUSED BYTE HotkeyVk,
890
                                                  WINPR_ATTR_UNUSED USHORT HotkeyModifiers)
891
0
{
892
0
  WLog_ERR("TODO", "TODO: implement");
893
0
  return FALSE;
894
0
}
895
896
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExW(WINPR_ATTR_UNUSED LPWSTR pTargetServerName,
897
                                                    WINPR_ATTR_UNUSED ULONG TargetLogonId,
898
                                                    WINPR_ATTR_UNUSED BYTE HotkeyVk,
899
                                                    WINPR_ATTR_UNUSED USHORT HotkeyModifiers,
900
                                                    WINPR_ATTR_UNUSED DWORD flags)
901
0
{
902
0
  WLog_ERR("TODO", "TODO: implement");
903
0
  return FALSE;
904
0
}
905
906
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExA(WINPR_ATTR_UNUSED LPSTR pTargetServerName,
907
                                                    WINPR_ATTR_UNUSED ULONG TargetLogonId,
908
                                                    WINPR_ATTR_UNUSED BYTE HotkeyVk,
909
                                                    WINPR_ATTR_UNUSED USHORT HotkeyModifiers,
910
                                                    WINPR_ATTR_UNUSED DWORD flags)
911
0
{
912
0
  WLog_ERR("TODO", "TODO: implement");
913
0
  return FALSE;
914
0
}
915
916
BOOL WINAPI FreeRDP_WTSStopRemoteControlSession(WINPR_ATTR_UNUSED ULONG LogonId)
917
0
{
918
0
  WLog_ERR("TODO", "TODO: implement");
919
0
  return FALSE;
920
0
}
921
922
BOOL WINAPI FreeRDP_WTSConnectSessionW(WINPR_ATTR_UNUSED ULONG LogonId,
923
                                       WINPR_ATTR_UNUSED ULONG TargetLogonId,
924
                                       WINPR_ATTR_UNUSED PWSTR pPassword,
925
                                       WINPR_ATTR_UNUSED BOOL bWait)
926
0
{
927
0
  WLog_ERR("TODO", "TODO: implement");
928
0
  return FALSE;
929
0
}
930
931
BOOL WINAPI FreeRDP_WTSConnectSessionA(WINPR_ATTR_UNUSED ULONG LogonId,
932
                                       WINPR_ATTR_UNUSED ULONG TargetLogonId,
933
                                       WINPR_ATTR_UNUSED PSTR pPassword,
934
                                       WINPR_ATTR_UNUSED BOOL bWait)
935
0
{
936
0
  WLog_ERR("TODO", "TODO: implement");
937
0
  return FALSE;
938
0
}
939
940
BOOL WINAPI FreeRDP_WTSEnumerateServersW(WINPR_ATTR_UNUSED LPWSTR pDomainName,
941
                                         WINPR_ATTR_UNUSED DWORD Reserved,
942
                                         WINPR_ATTR_UNUSED DWORD Version,
943
                                         WINPR_ATTR_UNUSED PWTS_SERVER_INFOW* ppServerInfo,
944
                                         WINPR_ATTR_UNUSED DWORD* pCount)
945
0
{
946
0
  WLog_ERR("TODO", "TODO: implement");
947
0
  return FALSE;
948
0
}
949
950
BOOL WINAPI FreeRDP_WTSEnumerateServersA(WINPR_ATTR_UNUSED LPSTR pDomainName,
951
                                         WINPR_ATTR_UNUSED DWORD Reserved,
952
                                         WINPR_ATTR_UNUSED DWORD Version,
953
                                         WINPR_ATTR_UNUSED PWTS_SERVER_INFOA* ppServerInfo,
954
                                         WINPR_ATTR_UNUSED DWORD* pCount)
955
0
{
956
0
  WLog_ERR("TODO", "TODO: implement");
957
0
  return FALSE;
958
0
}
959
960
HANDLE WINAPI FreeRDP_WTSOpenServerW(WINPR_ATTR_UNUSED LPWSTR pServerName)
961
0
{
962
0
  WLog_ERR("TODO", "TODO: implement");
963
0
  return INVALID_HANDLE_VALUE;
964
0
}
965
966
static void wts_virtual_channel_manager_free_message(void* obj)
967
0
{
968
0
  wMessage* msg = (wMessage*)obj;
969
970
0
  if (msg)
971
0
  {
972
0
    BYTE* buffer = (BYTE*)msg->wParam;
973
974
0
    if (buffer)
975
0
      free(buffer);
976
0
  }
977
0
}
978
979
static void channel_free(rdpPeerChannel* channel)
980
0
{
981
0
  server_channel_common_free(channel);
982
0
}
983
984
static void array_channel_free(void* ptr)
985
0
{
986
0
  rdpPeerChannel* channel = ptr;
987
0
  channel_free(channel);
988
0
}
989
990
static BOOL dynChannelMatch(const void* v1, const void* v2)
991
0
{
992
0
  const UINT32* p1 = (const UINT32*)v1;
993
0
  const UINT32* p2 = (const UINT32*)v2;
994
0
  return *p1 == *p2;
995
0
}
996
997
static UINT32 channelId_Hash(const void* key)
998
0
{
999
0
  const UINT32* v = (const UINT32*)key;
1000
0
  return *v;
1001
0
}
1002
1003
static void clearHandles(void)
1004
0
{
1005
0
  HashTable_Free(g_ServerHandles);
1006
0
  g_ServerHandles = nullptr;
1007
0
}
1008
1009
static BOOL CALLBACK initializeHandles(WINPR_ATTR_UNUSED PINIT_ONCE once,
1010
                                       WINPR_ATTR_UNUSED PVOID param,
1011
                                       WINPR_ATTR_UNUSED PVOID* context)
1012
0
{
1013
0
  WINPR_ASSERT(g_ServerHandles == nullptr);
1014
0
  g_ServerHandles = HashTable_New(TRUE);
1015
0
  (void)winpr_atexit(clearHandles);
1016
0
  return g_ServerHandles != nullptr;
1017
0
}
1018
1019
static bool setup(void)
1020
0
{
1021
0
  return InitOnceExecuteOnce(&g_HandleInitializer, initializeHandles, nullptr, nullptr);
1022
0
}
1023
1024
static void wtsCloseVCM(WTSVirtualChannelManager* vcm, bool closeDrdynvc)
1025
0
{
1026
0
  WINPR_ASSERT(vcm);
1027
1028
0
  HashTable_Lock(g_ServerHandles);
1029
1030
/* clang analyzer does not like the check for INVALID_HANDLE_VALUE and considers the path not taken,
1031
 * leading to false positives on memory leaks. */
1032
#ifdef __clang_analyzer__
1033
  const BOOL valid = vcm != nullptr;
1034
#else
1035
0
  const BOOL valid = (vcm != nullptr) && (vcm != INVALID_HANDLE_VALUE);
1036
0
#endif
1037
0
  if (valid)
1038
0
  {
1039
0
    HashTable_Remove(g_ServerHandles, (void*)(UINT_PTR)vcm->SessionId);
1040
1041
0
    HashTable_Free(vcm->dynamicVirtualChannels);
1042
1043
0
    if (vcm->drdynvc_channel)
1044
0
    {
1045
0
      if (closeDrdynvc)
1046
0
        (void)WTSVirtualChannelClose(vcm->drdynvc_channel);
1047
0
      vcm->drdynvc_channel = nullptr;
1048
0
    }
1049
1050
0
    MessageQueue_Free(vcm->queue);
1051
0
    free(vcm);
1052
0
  }
1053
0
  HashTable_Unlock(g_ServerHandles);
1054
0
}
1055
1056
HANDLE WINAPI FreeRDP_WTSOpenServerA(LPSTR pServerName)
1057
0
{
1058
0
  wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1059
1060
0
  rdpContext* context = (rdpContext*)pServerName;
1061
1062
0
  if (!setup() || !context)
1063
0
    return INVALID_HANDLE_VALUE;
1064
1065
0
  freerdp_peer* client = context->peer;
1066
1067
0
  if (!client)
1068
0
  {
1069
0
    SetLastError(ERROR_INVALID_DATA);
1070
0
    return INVALID_HANDLE_VALUE;
1071
0
  }
1072
1073
0
  WTSVirtualChannelManager* vcm = calloc(1, sizeof(WTSVirtualChannelManager));
1074
1075
0
  if (!vcm)
1076
0
    goto fail;
1077
1078
0
  vcm->client = client;
1079
0
  vcm->rdp = context->rdp;
1080
1081
0
  queueCallbacks.fnObjectFree = wts_virtual_channel_manager_free_message;
1082
0
  vcm->queue = MessageQueue_New(&queueCallbacks);
1083
1084
0
  if (!vcm->queue)
1085
0
    goto fail;
1086
1087
0
  vcm->dvc_channel_id_seq = 0;
1088
0
  vcm->dynamicVirtualChannels = HashTable_New(TRUE);
1089
1090
0
  if (!vcm->dynamicVirtualChannels)
1091
0
    goto fail;
1092
1093
0
  if (!HashTable_SetHashFunction(vcm->dynamicVirtualChannels, channelId_Hash))
1094
0
    goto fail;
1095
1096
0
  {
1097
0
    wObject* obj = HashTable_ValueObject(vcm->dynamicVirtualChannels);
1098
0
    WINPR_ASSERT(obj);
1099
0
    obj->fnObjectFree = array_channel_free;
1100
1101
0
    obj = HashTable_KeyObject(vcm->dynamicVirtualChannels);
1102
0
    obj->fnObjectEquals = dynChannelMatch;
1103
0
  }
1104
0
  client->ReceiveChannelData = WTSReceiveChannelData;
1105
0
  {
1106
0
    HashTable_Lock(g_ServerHandles);
1107
0
    vcm->SessionId = g_SessionId++;
1108
0
    const BOOL rc =
1109
0
        HashTable_Insert(g_ServerHandles, (void*)(UINT_PTR)vcm->SessionId, (void*)vcm);
1110
0
    HashTable_Unlock(g_ServerHandles);
1111
0
    if (!rc)
1112
0
      goto fail;
1113
0
  }
1114
1115
0
  HANDLE hServer = (HANDLE)vcm;
1116
0
  return hServer;
1117
1118
0
fail:
1119
0
  wtsCloseVCM(vcm, false);
1120
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1121
0
  return INVALID_HANDLE_VALUE;
1122
0
}
1123
1124
HANDLE WINAPI FreeRDP_WTSOpenServerExW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1125
0
{
1126
0
  WLog_ERR("TODO", "TODO: implement");
1127
0
  return INVALID_HANDLE_VALUE;
1128
0
}
1129
1130
HANDLE WINAPI FreeRDP_WTSOpenServerExA(LPSTR pServerName)
1131
0
{
1132
0
  return FreeRDP_WTSOpenServerA(pServerName);
1133
0
}
1134
1135
VOID WINAPI FreeRDP_WTSCloseServer(HANDLE hServer)
1136
0
{
1137
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
1138
0
  wtsCloseVCM(vcm, true);
1139
0
}
1140
1141
BOOL WINAPI FreeRDP_WTSEnumerateSessionsW(WINPR_ATTR_UNUSED HANDLE hServer,
1142
                                          WINPR_ATTR_UNUSED DWORD Reserved,
1143
                                          WINPR_ATTR_UNUSED DWORD Version,
1144
                                          WINPR_ATTR_UNUSED PWTS_SESSION_INFOW* ppSessionInfo,
1145
                                          WINPR_ATTR_UNUSED DWORD* pCount)
1146
0
{
1147
0
  WLog_ERR("TODO", "TODO: implement");
1148
0
  return FALSE;
1149
0
}
1150
1151
BOOL WINAPI FreeRDP_WTSEnumerateSessionsA(WINPR_ATTR_UNUSED HANDLE hServer,
1152
                                          WINPR_ATTR_UNUSED DWORD Reserved,
1153
                                          WINPR_ATTR_UNUSED DWORD Version,
1154
                                          WINPR_ATTR_UNUSED PWTS_SESSION_INFOA* ppSessionInfo,
1155
                                          WINPR_ATTR_UNUSED DWORD* pCount)
1156
0
{
1157
0
  WLog_ERR("TODO", "TODO: implement");
1158
0
  return FALSE;
1159
0
}
1160
1161
BOOL WINAPI FreeRDP_WTSEnumerateSessionsExW(WINPR_ATTR_UNUSED HANDLE hServer,
1162
                                            WINPR_ATTR_UNUSED DWORD* pLevel,
1163
                                            WINPR_ATTR_UNUSED DWORD Filter,
1164
                                            WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1W* ppSessionInfo,
1165
                                            WINPR_ATTR_UNUSED DWORD* pCount)
1166
0
{
1167
0
  WLog_ERR("TODO", "TODO: implement");
1168
0
  return FALSE;
1169
0
}
1170
1171
BOOL WINAPI FreeRDP_WTSEnumerateSessionsExA(WINPR_ATTR_UNUSED HANDLE hServer,
1172
                                            WINPR_ATTR_UNUSED DWORD* pLevel,
1173
                                            WINPR_ATTR_UNUSED DWORD Filter,
1174
                                            WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1A* ppSessionInfo,
1175
                                            WINPR_ATTR_UNUSED DWORD* pCount)
1176
0
{
1177
0
  WLog_ERR("TODO", "TODO: implement");
1178
0
  return FALSE;
1179
0
}
1180
1181
BOOL WINAPI FreeRDP_WTSEnumerateProcessesW(WINPR_ATTR_UNUSED HANDLE hServer,
1182
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1183
                                           WINPR_ATTR_UNUSED DWORD Version,
1184
                                           WINPR_ATTR_UNUSED PWTS_PROCESS_INFOW* ppProcessInfo,
1185
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1186
0
{
1187
0
  WLog_ERR("TODO", "TODO: implement");
1188
0
  return FALSE;
1189
0
}
1190
1191
BOOL WINAPI FreeRDP_WTSEnumerateProcessesA(WINPR_ATTR_UNUSED HANDLE hServer,
1192
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1193
                                           WINPR_ATTR_UNUSED DWORD Version,
1194
                                           WINPR_ATTR_UNUSED PWTS_PROCESS_INFOA* ppProcessInfo,
1195
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1196
0
{
1197
0
  WLog_ERR("TODO", "TODO: implement");
1198
0
  return FALSE;
1199
0
}
1200
1201
BOOL WINAPI FreeRDP_WTSTerminateProcess(WINPR_ATTR_UNUSED HANDLE hServer,
1202
                                        WINPR_ATTR_UNUSED DWORD ProcessId,
1203
                                        WINPR_ATTR_UNUSED DWORD ExitCode)
1204
0
{
1205
0
  WLog_ERR("TODO", "TODO: implement");
1206
0
  return FALSE;
1207
0
}
1208
1209
BOOL WINAPI FreeRDP_WTSQuerySessionInformationW(WINPR_ATTR_UNUSED HANDLE hServer,
1210
                                                WINPR_ATTR_UNUSED DWORD SessionId,
1211
                                                WINPR_ATTR_UNUSED WTS_INFO_CLASS WTSInfoClass,
1212
                                                WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1213
                                                WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1214
0
{
1215
0
  WLog_ERR("TODO", "TODO: implement");
1216
0
  return FALSE;
1217
0
}
1218
1219
BOOL WINAPI FreeRDP_WTSQuerySessionInformationA(HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1220
                                                WTS_INFO_CLASS WTSInfoClass, LPSTR* ppBuffer,
1221
                                                DWORD* pBytesReturned)
1222
0
{
1223
0
  DWORD BytesReturned = 0;
1224
0
  WTSVirtualChannelManager* vcm = nullptr;
1225
0
  vcm = (WTSVirtualChannelManager*)hServer;
1226
1227
0
  if (!vcm)
1228
0
    return FALSE;
1229
1230
0
  if (WTSInfoClass == WTSSessionId)
1231
0
  {
1232
0
    ULONG* pBuffer = nullptr;
1233
0
    BytesReturned = sizeof(ULONG);
1234
0
    pBuffer = (ULONG*)malloc(sizeof(BytesReturned));
1235
1236
0
    if (!pBuffer)
1237
0
    {
1238
0
      SetLastError(g_err_oom);
1239
0
      return FALSE;
1240
0
    }
1241
1242
0
    *pBuffer = vcm->SessionId;
1243
0
    *ppBuffer = (LPSTR)pBuffer;
1244
0
    *pBytesReturned = BytesReturned;
1245
0
    return TRUE;
1246
0
  }
1247
1248
0
  return FALSE;
1249
0
}
1250
1251
BOOL WINAPI FreeRDP_WTSQueryUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1252
                                        WINPR_ATTR_UNUSED LPWSTR pUserName,
1253
                                        WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1254
                                        WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1255
                                        WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1256
0
{
1257
0
  WLog_ERR("TODO", "TODO: implement");
1258
0
  return FALSE;
1259
0
}
1260
1261
BOOL WINAPI FreeRDP_WTSQueryUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1262
                                        WINPR_ATTR_UNUSED LPSTR pUserName,
1263
                                        WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1264
                                        WINPR_ATTR_UNUSED LPSTR* ppBuffer,
1265
                                        WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1266
0
{
1267
0
  WLog_ERR("TODO", "TODO: implement");
1268
0
  return FALSE;
1269
0
}
1270
1271
BOOL WINAPI FreeRDP_WTSSetUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1272
                                      WINPR_ATTR_UNUSED LPWSTR pUserName,
1273
                                      WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1274
                                      WINPR_ATTR_UNUSED LPWSTR pBuffer,
1275
                                      WINPR_ATTR_UNUSED DWORD DataLength)
1276
0
{
1277
0
  WLog_ERR("TODO", "TODO: implement");
1278
0
  return FALSE;
1279
0
}
1280
1281
BOOL WINAPI FreeRDP_WTSSetUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1282
                                      WINPR_ATTR_UNUSED LPSTR pUserName,
1283
                                      WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1284
                                      WINPR_ATTR_UNUSED LPSTR pBuffer,
1285
                                      WINPR_ATTR_UNUSED DWORD DataLength)
1286
0
{
1287
0
  WLog_ERR("TODO", "TODO: implement");
1288
0
  return FALSE;
1289
0
}
1290
1291
BOOL WINAPI
1292
FreeRDP_WTSSendMessageW(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1293
                        WINPR_ATTR_UNUSED LPWSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1294
                        WINPR_ATTR_UNUSED LPWSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1295
                        WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1296
                        WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1297
0
{
1298
0
  WLog_ERR("TODO", "TODO: implement");
1299
0
  return FALSE;
1300
0
}
1301
1302
BOOL WINAPI
1303
FreeRDP_WTSSendMessageA(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1304
                        WINPR_ATTR_UNUSED LPSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1305
                        WINPR_ATTR_UNUSED LPSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1306
                        WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1307
                        WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1308
0
{
1309
0
  WLog_ERR("TODO", "TODO: implement");
1310
0
  return FALSE;
1311
0
}
1312
1313
BOOL WINAPI FreeRDP_WTSDisconnectSession(WINPR_ATTR_UNUSED HANDLE hServer,
1314
                                         WINPR_ATTR_UNUSED DWORD SessionId,
1315
                                         WINPR_ATTR_UNUSED BOOL bWait)
1316
0
{
1317
0
  WLog_ERR("TODO", "TODO: implement");
1318
0
  return FALSE;
1319
0
}
1320
1321
BOOL WINAPI FreeRDP_WTSLogoffSession(WINPR_ATTR_UNUSED HANDLE hServer,
1322
                                     WINPR_ATTR_UNUSED DWORD SessionId,
1323
                                     WINPR_ATTR_UNUSED BOOL bWait)
1324
0
{
1325
0
  WLog_ERR("TODO", "TODO: implement");
1326
0
  return FALSE;
1327
0
}
1328
1329
BOOL WINAPI FreeRDP_WTSShutdownSystem(WINPR_ATTR_UNUSED HANDLE hServer,
1330
                                      WINPR_ATTR_UNUSED DWORD ShutdownFlag)
1331
0
{
1332
0
  WLog_ERR("TODO", "TODO: implement");
1333
0
  return FALSE;
1334
0
}
1335
1336
BOOL WINAPI FreeRDP_WTSWaitSystemEvent(WINPR_ATTR_UNUSED HANDLE hServer,
1337
                                       WINPR_ATTR_UNUSED DWORD EventMask,
1338
                                       WINPR_ATTR_UNUSED DWORD* pEventFlags)
1339
0
{
1340
0
  WLog_ERR("TODO", "TODO: implement");
1341
0
  return FALSE;
1342
0
}
1343
1344
static void peer_channel_queue_free_message(void* obj)
1345
0
{
1346
0
  wMessage* msg = (wMessage*)obj;
1347
0
  if (!msg)
1348
0
    return;
1349
1350
0
  free(msg->context);
1351
0
  msg->context = nullptr;
1352
0
}
1353
1354
static rdpPeerChannel* channel_new(WTSVirtualChannelManager* vcm, freerdp_peer* client,
1355
                                   UINT32 ChannelId, UINT16 index, UINT16 type, size_t chunkSize,
1356
                                   const char* name)
1357
0
{
1358
0
  wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1359
0
  queueCallbacks.fnObjectFree = peer_channel_queue_free_message;
1360
1361
0
  rdpPeerChannel* channel =
1362
0
      server_channel_common_new(client, index, ChannelId, chunkSize, &queueCallbacks, name);
1363
1364
0
  WINPR_ASSERT(vcm);
1365
0
  WINPR_ASSERT(client);
1366
1367
0
  if (!channel)
1368
0
    goto fail;
1369
1370
0
  channel->vcm = vcm;
1371
0
  channel->channelType = type;
1372
0
  channel->creationStatus =
1373
0
      (type == RDP_PEER_CHANNEL_TYPE_SVC) ? ERROR_SUCCESS : ERROR_OPERATION_IN_PROGRESS;
1374
1375
0
  return channel;
1376
0
fail:
1377
0
  channel_free(channel);
1378
0
  return nullptr;
1379
0
}
1380
1381
HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1382
                                            LPSTR pVirtualName)
1383
0
{
1384
0
  size_t length = 0;
1385
0
  rdpMcs* mcs = nullptr;
1386
0
  rdpMcsChannel* joined_channel = nullptr;
1387
0
  freerdp_peer* client = nullptr;
1388
0
  rdpPeerChannel* channel = nullptr;
1389
0
  WTSVirtualChannelManager* vcm = nullptr;
1390
0
  HANDLE hChannelHandle = nullptr;
1391
0
  rdpContext* context = nullptr;
1392
0
  vcm = (WTSVirtualChannelManager*)hServer;
1393
1394
0
  if (!vcm)
1395
0
  {
1396
0
    SetLastError(ERROR_INVALID_DATA);
1397
0
    return nullptr;
1398
0
  }
1399
1400
0
  client = vcm->client;
1401
0
  WINPR_ASSERT(client);
1402
1403
0
  context = client->context;
1404
0
  WINPR_ASSERT(context);
1405
0
  WINPR_ASSERT(context->rdp);
1406
0
  WINPR_ASSERT(context->settings);
1407
1408
0
  mcs = context->rdp->mcs;
1409
0
  WINPR_ASSERT(mcs);
1410
1411
0
  length = strnlen(pVirtualName, CHANNEL_NAME_LEN + 1);
1412
1413
0
  if (length > CHANNEL_NAME_LEN)
1414
0
  {
1415
0
    SetLastError(ERROR_NOT_FOUND);
1416
0
    return nullptr;
1417
0
  }
1418
1419
0
  UINT32 index = 0;
1420
0
  for (; index < mcs->channelCount; index++)
1421
0
  {
1422
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
1423
0
    if (mchannel->joined && (strncmp(mchannel->Name, pVirtualName, length) == 0))
1424
0
    {
1425
0
      joined_channel = mchannel;
1426
0
      break;
1427
0
    }
1428
0
  }
1429
1430
0
  if (!joined_channel)
1431
0
  {
1432
0
    SetLastError(ERROR_NOT_FOUND);
1433
0
    return nullptr;
1434
0
  }
1435
1436
0
  channel = (rdpPeerChannel*)joined_channel->handle;
1437
1438
0
  if (!channel)
1439
0
  {
1440
0
    const UINT32 VCChunkSize =
1441
0
        freerdp_settings_get_uint32(context->settings, FreeRDP_VCChunkSize);
1442
1443
0
    WINPR_ASSERT(index <= UINT16_MAX);
1444
0
    channel = channel_new(vcm, client, joined_channel->ChannelId, (UINT16)index,
1445
0
                          RDP_PEER_CHANNEL_TYPE_SVC, VCChunkSize, pVirtualName);
1446
1447
0
    if (!channel)
1448
0
      goto fail;
1449
1450
0
    joined_channel->handle = channel;
1451
0
  }
1452
1453
0
  hChannelHandle = (HANDLE)channel;
1454
0
  return hChannelHandle;
1455
0
fail:
1456
0
  channel_free(channel);
1457
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1458
0
  return nullptr;
1459
0
}
1460
1461
HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenEx(DWORD SessionId, LPSTR pVirtualName, DWORD flags)
1462
0
{
1463
0
  wStream* s = nullptr;
1464
0
  rdpPeerChannel* channel = nullptr;
1465
0
  BOOL joined = FALSE;
1466
0
  ULONG written = 0;
1467
1468
0
  if (!setup())
1469
0
    return nullptr;
1470
1471
0
  if (SessionId == WTS_CURRENT_SESSION)
1472
0
    return nullptr;
1473
1474
0
  HashTable_Lock(g_ServerHandles);
1475
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)HashTable_GetItemValue(
1476
0
      g_ServerHandles, (void*)(UINT_PTR)SessionId);
1477
1478
0
  if (!vcm)
1479
0
    goto end;
1480
1481
0
  if (!(flags & WTS_CHANNEL_OPTION_DYNAMIC))
1482
0
  {
1483
0
    HashTable_Unlock(g_ServerHandles);
1484
0
    return FreeRDP_WTSVirtualChannelOpen((HANDLE)vcm, SessionId, pVirtualName);
1485
0
  }
1486
1487
0
  freerdp_peer* client = vcm->client;
1488
0
  WINPR_ASSERT(client);
1489
0
  WINPR_ASSERT(client->context);
1490
0
  WINPR_ASSERT(client->context->rdp);
1491
1492
0
  rdpMcs* mcs = client->context->rdp->mcs;
1493
0
  WINPR_ASSERT(mcs);
1494
1495
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
1496
0
  {
1497
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
1498
0
    if (mchannel->joined &&
1499
0
        (strncmp(mchannel->Name, DRDYNVC_SVC_CHANNEL_NAME, CHANNEL_NAME_LEN + 1) == 0))
1500
0
    {
1501
0
      joined = TRUE;
1502
0
      break;
1503
0
    }
1504
0
  }
1505
1506
0
  if (!joined)
1507
0
  {
1508
0
    SetLastError(ERROR_NOT_FOUND);
1509
0
    goto end;
1510
0
  }
1511
1512
0
  if (!vcm->drdynvc_channel || (vcm->drdynvc_state != DRDYNVC_STATE_READY))
1513
0
  {
1514
0
    SetLastError(ERROR_NOT_READY);
1515
0
    goto end;
1516
0
  }
1517
1518
0
  WINPR_ASSERT(client);
1519
0
  WINPR_ASSERT(client->context);
1520
0
  WINPR_ASSERT(client->context->settings);
1521
1522
0
  const UINT32 VCChunkSize =
1523
0
      freerdp_settings_get_uint32(client->context->settings, FreeRDP_VCChunkSize);
1524
0
  channel = channel_new(vcm, client, 0, 0, RDP_PEER_CHANNEL_TYPE_DVC, VCChunkSize, pVirtualName);
1525
1526
0
  if (!channel)
1527
0
  {
1528
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1529
0
    goto end;
1530
0
  }
1531
1532
0
  const LONG hdl = InterlockedIncrement(&vcm->dvc_channel_id_seq);
1533
0
  channel->channelId = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1534
1535
0
  if (!HashTable_Insert(vcm->dynamicVirtualChannels, &channel->channelId, channel))
1536
0
  {
1537
0
    channel_free(channel);
1538
0
    channel = nullptr;
1539
0
    goto fail;
1540
0
  }
1541
0
  s = Stream_New(nullptr, 64);
1542
1543
0
  if (!s)
1544
0
    goto fail;
1545
1546
0
  if (!wts_write_drdynvc_create_request(s, channel->channelId, pVirtualName))
1547
0
    goto fail;
1548
1549
0
  {
1550
0
    const size_t pos = Stream_GetPosition(s);
1551
0
    WINPR_ASSERT(pos <= UINT32_MAX);
1552
0
    if (!WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s, char), (UINT32)pos,
1553
0
                                &written))
1554
0
      goto fail;
1555
0
  }
1556
1557
0
end:
1558
0
  Stream_Free(s, TRUE);
1559
0
  HashTable_Unlock(g_ServerHandles);
1560
0
  return channel;
1561
1562
0
fail:
1563
0
  Stream_Free(s, TRUE);
1564
0
  if (channel)
1565
0
    HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1566
0
  HashTable_Unlock(g_ServerHandles);
1567
1568
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1569
0
  return nullptr;
1570
0
}
1571
1572
BOOL WINAPI FreeRDP_WTSVirtualChannelClose(HANDLE hChannelHandle)
1573
0
{
1574
0
  wStream* s = nullptr;
1575
0
  rdpMcs* mcs = nullptr;
1576
1577
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1578
0
  BOOL ret = TRUE;
1579
1580
0
  if (channel)
1581
0
  {
1582
0
    WTSVirtualChannelManager* vcm = channel->vcm;
1583
1584
0
    WINPR_ASSERT(vcm);
1585
0
    WINPR_ASSERT(vcm->client);
1586
0
    WINPR_ASSERT(vcm->client->context);
1587
0
    WINPR_ASSERT(vcm->client->context->rdp);
1588
0
    mcs = vcm->client->context->rdp->mcs;
1589
1590
0
    if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1591
0
    {
1592
0
      if (channel->index < mcs->channelCount)
1593
0
      {
1594
0
        rdpMcsChannel* cur = &mcs->channels[channel->index];
1595
0
        rdpPeerChannel* peerChannel = (rdpPeerChannel*)cur->handle;
1596
0
        channel_free(peerChannel);
1597
0
        cur->handle = nullptr;
1598
0
      }
1599
0
    }
1600
0
    else
1601
0
    {
1602
0
      if (channel->dvc_open_state == DVC_OPEN_STATE_SUCCEEDED)
1603
0
      {
1604
0
        ULONG written = 0;
1605
0
        s = Stream_New(nullptr, 8);
1606
1607
0
        if (!s)
1608
0
        {
1609
0
          WLog_ERR(TAG, "Stream_New failed!");
1610
0
          ret = FALSE;
1611
0
        }
1612
0
        else
1613
0
        {
1614
0
          wts_write_drdynvc_header(s, CLOSE_REQUEST_PDU, channel->channelId);
1615
1616
0
          const size_t pos = Stream_GetPosition(s);
1617
0
          WINPR_ASSERT(pos <= UINT32_MAX);
1618
0
          ret = WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s, char),
1619
0
                                       (UINT32)pos, &written);
1620
0
          Stream_Free(s, TRUE);
1621
0
        }
1622
0
      }
1623
0
      HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1624
0
    }
1625
0
  }
1626
1627
0
  return ret;
1628
0
}
1629
1630
BOOL WINAPI FreeRDP_WTSVirtualChannelRead(HANDLE hChannelHandle, WINPR_ATTR_UNUSED ULONG TimeOut,
1631
                                          PCHAR Buffer, ULONG BufferSize, PULONG pBytesRead)
1632
0
{
1633
0
  BYTE* buffer = nullptr;
1634
0
  wMessage message = WINPR_C_ARRAY_INIT;
1635
0
  wtsChannelMessage* messageCtx = nullptr;
1636
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1637
1638
0
  WINPR_ASSERT(channel);
1639
1640
0
  if (!MessageQueue_Peek(channel->queue, &message, FALSE))
1641
0
  {
1642
0
    SetLastError(ERROR_NO_DATA);
1643
0
    *pBytesRead = 0;
1644
0
    return FALSE;
1645
0
  }
1646
1647
0
  messageCtx = message.context;
1648
1649
0
  if (messageCtx == nullptr)
1650
0
    return FALSE;
1651
1652
0
  buffer = (BYTE*)(messageCtx + 1);
1653
0
  *pBytesRead = messageCtx->length - messageCtx->offset;
1654
1655
0
  if (Buffer == nullptr || BufferSize == 0)
1656
0
  {
1657
0
    return TRUE;
1658
0
  }
1659
1660
0
  if (*pBytesRead > BufferSize)
1661
0
    *pBytesRead = BufferSize;
1662
1663
0
  CopyMemory(Buffer, buffer + messageCtx->offset, *pBytesRead);
1664
0
  messageCtx->offset += *pBytesRead;
1665
1666
0
  if (messageCtx->offset >= messageCtx->length)
1667
0
  {
1668
0
    const int rc = MessageQueue_Peek(channel->queue, &message, TRUE);
1669
0
    peer_channel_queue_free_message(&message);
1670
0
    if (rc < 0)
1671
0
      return FALSE;
1672
0
  }
1673
1674
0
  return TRUE;
1675
0
}
1676
1677
BOOL WINAPI FreeRDP_WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer, ULONG uLength,
1678
                                           PULONG pBytesWritten)
1679
0
{
1680
0
  wStream* s = nullptr;
1681
0
  int cbLen = 0;
1682
0
  int cbChId = 0;
1683
0
  int first = 0;
1684
0
  BYTE* buffer = nullptr;
1685
0
  size_t totalWritten = 0;
1686
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1687
0
  BOOL ret = FALSE;
1688
1689
0
  if (!channel)
1690
0
    return FALSE;
1691
1692
0
  EnterCriticalSection(&channel->writeLock);
1693
0
  WINPR_ASSERT(channel->vcm);
1694
0
  if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1695
0
  {
1696
0
    buffer = (BYTE*)malloc(uLength);
1697
1698
0
    if (!buffer)
1699
0
    {
1700
0
      SetLastError(g_err_oom);
1701
0
      goto fail;
1702
0
    }
1703
1704
0
    CopyMemory(buffer, Buffer, uLength);
1705
0
    totalWritten = uLength;
1706
0
    if (!wts_queue_send_item(channel, buffer, uLength))
1707
0
      goto fail;
1708
0
  }
1709
0
  else if (!channel->vcm->drdynvc_channel || (channel->vcm->drdynvc_state != DRDYNVC_STATE_READY))
1710
0
  {
1711
0
    DEBUG_DVC("drdynvc not ready");
1712
0
    goto fail;
1713
0
  }
1714
0
  else
1715
0
  {
1716
0
    first = TRUE;
1717
1718
0
    size_t Length = uLength;
1719
0
    while (Length > 0)
1720
0
    {
1721
0
      s = Stream_New(nullptr, DVC_MAX_DATA_PDU_SIZE);
1722
1723
0
      if (!s)
1724
0
      {
1725
0
        WLog_ERR(TAG, "Stream_New failed!");
1726
0
        SetLastError(g_err_oom);
1727
0
        goto fail;
1728
0
      }
1729
1730
0
      buffer = Stream_Buffer(s);
1731
0
      Stream_Seek_UINT8(s);
1732
0
      cbChId = wts_write_variable_uint(s, channel->channelId);
1733
1734
0
      if (first && (Length > Stream_GetRemainingLength(s)))
1735
0
      {
1736
0
        cbLen = wts_write_variable_uint(s, WINPR_ASSERTING_INT_CAST(uint32_t, Length));
1737
0
        buffer[0] = ((DATA_FIRST_PDU << 4) | (cbLen << 2) | cbChId) & 0xFF;
1738
0
      }
1739
0
      else
1740
0
      {
1741
0
        buffer[0] = ((DATA_PDU << 4) | cbChId) & 0xFF;
1742
0
      }
1743
1744
0
      first = FALSE;
1745
0
      size_t written = Stream_GetRemainingLength(s);
1746
1747
0
      if (written > Length)
1748
0
        written = Length;
1749
1750
0
      Stream_Write(s, Buffer, written);
1751
0
      const size_t length = Stream_GetPosition(s);
1752
0
      Stream_Free(s, FALSE);
1753
0
      if (length > UINT32_MAX)
1754
0
        goto fail;
1755
0
      Length -= written;
1756
0
      Buffer += written;
1757
0
      totalWritten += written;
1758
0
      if (!wts_queue_send_item(channel->vcm->drdynvc_channel, buffer, (UINT32)length))
1759
0
        goto fail;
1760
0
    }
1761
0
  }
1762
1763
0
  if (pBytesWritten)
1764
0
    *pBytesWritten = WINPR_ASSERTING_INT_CAST(uint32_t, totalWritten);
1765
1766
0
  ret = TRUE;
1767
0
fail:
1768
0
  LeaveCriticalSection(&channel->writeLock);
1769
0
  return ret;
1770
0
}
1771
1772
BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeInput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1773
0
{
1774
0
  WLog_ERR("TODO", "TODO: implement");
1775
0
  return TRUE;
1776
0
}
1777
1778
BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeOutput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1779
0
{
1780
0
  WLog_ERR("TODO", "TODO: implement");
1781
0
  return TRUE;
1782
0
}
1783
1784
BOOL WINAPI FreeRDP_WTSVirtualChannelQuery(HANDLE hChannelHandle, WTS_VIRTUAL_CLASS WtsVirtualClass,
1785
                                           PVOID* ppBuffer, DWORD* pBytesReturned)
1786
0
{
1787
0
  void* pfd = nullptr;
1788
0
  BOOL bval = 0;
1789
0
  void* fds[10] = WINPR_C_ARRAY_INIT;
1790
0
  HANDLE hEvent = nullptr;
1791
0
  int fds_count = 0;
1792
0
  BOOL status = FALSE;
1793
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1794
1795
0
  WINPR_ASSERT(channel);
1796
1797
0
  switch ((UINT32)WtsVirtualClass)
1798
0
  {
1799
0
    case WTSVirtualFileHandle:
1800
0
      hEvent = MessageQueue_Event(channel->queue);
1801
0
      pfd = GetEventWaitObject(hEvent);
1802
1803
0
      if (pfd)
1804
0
      {
1805
0
        fds[fds_count] = pfd;
1806
0
        (fds_count)++;
1807
0
      }
1808
1809
0
      *ppBuffer = malloc(sizeof(void*));
1810
1811
0
      if (!*ppBuffer)
1812
0
      {
1813
0
        SetLastError(g_err_oom);
1814
0
      }
1815
0
      else
1816
0
      {
1817
0
        CopyMemory(*ppBuffer, (void*)&fds[0], sizeof(void*));
1818
0
        *pBytesReturned = sizeof(void*);
1819
0
        status = TRUE;
1820
0
      }
1821
1822
0
      break;
1823
1824
0
    case WTSVirtualEventHandle:
1825
0
      hEvent = MessageQueue_Event(channel->queue);
1826
1827
0
      *ppBuffer = malloc(sizeof(HANDLE));
1828
1829
0
      if (!*ppBuffer)
1830
0
      {
1831
0
        SetLastError(g_err_oom);
1832
0
      }
1833
0
      else
1834
0
      {
1835
0
        CopyMemory(*ppBuffer, (void*)&hEvent, sizeof(HANDLE));
1836
0
        *pBytesReturned = sizeof(void*);
1837
0
        status = TRUE;
1838
0
      }
1839
1840
0
      break;
1841
1842
0
    case WTSVirtualChannelReady:
1843
0
      if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1844
0
      {
1845
0
        bval = TRUE;
1846
0
        status = TRUE;
1847
0
      }
1848
0
      else
1849
0
      {
1850
0
        switch (channel->dvc_open_state)
1851
0
        {
1852
0
          case DVC_OPEN_STATE_NONE:
1853
0
            bval = FALSE;
1854
0
            status = TRUE;
1855
0
            break;
1856
1857
0
          case DVC_OPEN_STATE_SUCCEEDED:
1858
0
            bval = TRUE;
1859
0
            status = TRUE;
1860
0
            break;
1861
1862
0
          default:
1863
0
            *ppBuffer = nullptr;
1864
0
            *pBytesReturned = 0;
1865
0
            return FALSE;
1866
0
        }
1867
0
      }
1868
1869
0
      *ppBuffer = malloc(sizeof(BOOL));
1870
1871
0
      if (!*ppBuffer)
1872
0
      {
1873
0
        SetLastError(g_err_oom);
1874
0
        status = FALSE;
1875
0
      }
1876
0
      else
1877
0
      {
1878
0
        CopyMemory(*ppBuffer, &bval, sizeof(BOOL));
1879
0
        *pBytesReturned = sizeof(BOOL);
1880
0
      }
1881
1882
0
      break;
1883
0
    case WTSVirtualChannelOpenStatus:
1884
0
    {
1885
0
      INT32 value = channel->creationStatus;
1886
0
      status = TRUE;
1887
1888
0
      *ppBuffer = malloc(sizeof(value));
1889
0
      if (!*ppBuffer)
1890
0
      {
1891
0
        SetLastError(g_err_oom);
1892
0
        status = FALSE;
1893
0
      }
1894
0
      else
1895
0
      {
1896
0
        CopyMemory(*ppBuffer, &value, sizeof(value));
1897
0
        *pBytesReturned = sizeof(value);
1898
0
      }
1899
0
      break;
1900
0
    }
1901
0
    default:
1902
0
      break;
1903
0
  }
1904
1905
0
  return status;
1906
0
}
1907
1908
VOID WINAPI FreeRDP_WTSFreeMemory(PVOID pMemory)
1909
0
{
1910
0
  free(pMemory);
1911
0
}
1912
1913
BOOL WINAPI FreeRDP_WTSFreeMemoryExW(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
1914
                                     WINPR_ATTR_UNUSED PVOID pMemory,
1915
                                     WINPR_ATTR_UNUSED ULONG NumberOfEntries)
1916
0
{
1917
0
  WLog_ERR("TODO", "TODO: implement");
1918
0
  return FALSE;
1919
0
}
1920
1921
BOOL WINAPI FreeRDP_WTSFreeMemoryExA(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
1922
                                     WINPR_ATTR_UNUSED PVOID pMemory,
1923
                                     WINPR_ATTR_UNUSED ULONG NumberOfEntries)
1924
0
{
1925
0
  WLog_ERR("TODO", "TODO: implement");
1926
0
  return FALSE;
1927
0
}
1928
1929
BOOL WINAPI FreeRDP_WTSRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd,
1930
                                                   WINPR_ATTR_UNUSED DWORD dwFlags)
1931
0
{
1932
0
  WLog_ERR("TODO", "TODO: implement");
1933
0
  return FALSE;
1934
0
}
1935
1936
BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd)
1937
0
{
1938
0
  WLog_ERR("TODO", "TODO: implement");
1939
0
  return FALSE;
1940
0
}
1941
1942
BOOL WINAPI FreeRDP_WTSRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
1943
                                                     WINPR_ATTR_UNUSED HWND hWnd,
1944
                                                     WINPR_ATTR_UNUSED DWORD dwFlags)
1945
0
{
1946
0
  WLog_ERR("TODO", "TODO: implement");
1947
0
  return FALSE;
1948
0
}
1949
1950
BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
1951
                                                       WINPR_ATTR_UNUSED HWND hWnd)
1952
0
{
1953
0
  WLog_ERR("TODO", "TODO: implement");
1954
0
  return FALSE;
1955
0
}
1956
1957
BOOL WINAPI FreeRDP_WTSQueryUserToken(WINPR_ATTR_UNUSED ULONG SessionId,
1958
                                      WINPR_ATTR_UNUSED PHANDLE phToken)
1959
0
{
1960
0
  WLog_ERR("TODO", "TODO: implement");
1961
0
  return FALSE;
1962
0
}
1963
1964
BOOL WINAPI FreeRDP_WTSEnumerateProcessesExW(WINPR_ATTR_UNUSED HANDLE hServer,
1965
                                             WINPR_ATTR_UNUSED DWORD* pLevel,
1966
                                             WINPR_ATTR_UNUSED DWORD SessionId,
1967
                                             WINPR_ATTR_UNUSED LPWSTR* ppProcessInfo,
1968
                                             WINPR_ATTR_UNUSED DWORD* pCount)
1969
0
{
1970
0
  WLog_ERR("TODO", "TODO: implement");
1971
0
  return FALSE;
1972
0
}
1973
1974
BOOL WINAPI FreeRDP_WTSEnumerateProcessesExA(WINPR_ATTR_UNUSED HANDLE hServer,
1975
                                             WINPR_ATTR_UNUSED DWORD* pLevel,
1976
                                             WINPR_ATTR_UNUSED DWORD SessionId,
1977
                                             WINPR_ATTR_UNUSED LPSTR* ppProcessInfo,
1978
                                             WINPR_ATTR_UNUSED DWORD* pCount)
1979
0
{
1980
0
  WLog_ERR("TODO", "TODO: implement");
1981
0
  return FALSE;
1982
0
}
1983
1984
BOOL WINAPI FreeRDP_WTSEnumerateListenersW(WINPR_ATTR_UNUSED HANDLE hServer,
1985
                                           WINPR_ATTR_UNUSED PVOID pReserved,
1986
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1987
                                           WINPR_ATTR_UNUSED PWTSLISTENERNAMEW pListeners,
1988
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1989
0
{
1990
0
  WLog_ERR("TODO", "TODO: implement");
1991
0
  return FALSE;
1992
0
}
1993
1994
BOOL WINAPI FreeRDP_WTSEnumerateListenersA(WINPR_ATTR_UNUSED HANDLE hServer,
1995
                                           WINPR_ATTR_UNUSED PVOID pReserved,
1996
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1997
                                           WINPR_ATTR_UNUSED PWTSLISTENERNAMEA pListeners,
1998
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1999
0
{
2000
0
  WLog_ERR("TODO", "TODO: implement");
2001
0
  return FALSE;
2002
0
}
2003
2004
BOOL WINAPI FreeRDP_WTSQueryListenerConfigW(WINPR_ATTR_UNUSED HANDLE hServer,
2005
                                            WINPR_ATTR_UNUSED PVOID pReserved,
2006
                                            WINPR_ATTR_UNUSED DWORD Reserved,
2007
                                            WINPR_ATTR_UNUSED LPWSTR pListenerName,
2008
                                            WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer)
2009
0
{
2010
0
  WLog_ERR("TODO", "TODO: implement");
2011
0
  return FALSE;
2012
0
}
2013
2014
BOOL WINAPI FreeRDP_WTSQueryListenerConfigA(WINPR_ATTR_UNUSED HANDLE hServer,
2015
                                            WINPR_ATTR_UNUSED PVOID pReserved,
2016
                                            WINPR_ATTR_UNUSED DWORD Reserved,
2017
                                            WINPR_ATTR_UNUSED LPSTR pListenerName,
2018
                                            WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer)
2019
0
{
2020
0
  WLog_ERR("TODO", "TODO: implement");
2021
0
  return FALSE;
2022
0
}
2023
2024
BOOL WINAPI FreeRDP_WTSCreateListenerW(WINPR_ATTR_UNUSED HANDLE hServer,
2025
                                       WINPR_ATTR_UNUSED PVOID pReserved,
2026
                                       WINPR_ATTR_UNUSED DWORD Reserved,
2027
                                       WINPR_ATTR_UNUSED LPWSTR pListenerName,
2028
                                       WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer,
2029
                                       WINPR_ATTR_UNUSED DWORD flag)
2030
0
{
2031
0
  WLog_ERR("TODO", "TODO: implement");
2032
0
  return FALSE;
2033
0
}
2034
2035
BOOL WINAPI FreeRDP_WTSCreateListenerA(WINPR_ATTR_UNUSED HANDLE hServer,
2036
                                       WINPR_ATTR_UNUSED PVOID pReserved,
2037
                                       WINPR_ATTR_UNUSED DWORD Reserved,
2038
                                       WINPR_ATTR_UNUSED LPSTR pListenerName,
2039
                                       WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer,
2040
                                       WINPR_ATTR_UNUSED DWORD flag)
2041
0
{
2042
0
  WLog_ERR("TODO", "TODO: implement");
2043
0
  return FALSE;
2044
0
}
2045
2046
BOOL WINAPI FreeRDP_WTSSetListenerSecurityW(
2047
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2048
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2049
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2050
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2051
0
{
2052
0
  WLog_ERR("TODO", "TODO: implement");
2053
0
  return FALSE;
2054
0
}
2055
2056
BOOL WINAPI FreeRDP_WTSSetListenerSecurityA(
2057
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2058
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2059
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2060
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2061
0
{
2062
0
  WLog_ERR("TODO", "TODO: implement");
2063
0
  return FALSE;
2064
0
}
2065
2066
BOOL WINAPI FreeRDP_WTSGetListenerSecurityW(
2067
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2068
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2069
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2070
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2071
    WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2072
0
{
2073
0
  WLog_ERR("TODO", "TODO: implement");
2074
0
  return FALSE;
2075
0
}
2076
2077
BOOL WINAPI FreeRDP_WTSGetListenerSecurityA(
2078
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2079
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2080
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2081
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2082
    WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2083
0
{
2084
0
  WLog_ERR("TODO", "TODO: implement");
2085
0
  return FALSE;
2086
0
}
2087
2088
BOOL CDECL FreeRDP_WTSEnableChildSessions(WINPR_ATTR_UNUSED BOOL bEnable)
2089
0
{
2090
0
  WLog_ERR("TODO", "TODO: implement");
2091
0
  return FALSE;
2092
0
}
2093
2094
BOOL CDECL FreeRDP_WTSIsChildSessionsEnabled(WINPR_ATTR_UNUSED PBOOL pbEnabled)
2095
0
{
2096
0
  WLog_ERR("TODO", "TODO: implement");
2097
0
  return FALSE;
2098
0
}
2099
2100
BOOL CDECL FreeRDP_WTSGetChildSessionId(WINPR_ATTR_UNUSED PULONG pSessionId)
2101
0
{
2102
0
  WLog_ERR("TODO", "TODO: implement");
2103
0
  return FALSE;
2104
0
}
2105
2106
DWORD WINAPI FreeRDP_WTSGetActiveConsoleSessionId(void)
2107
0
{
2108
0
  WLog_ERR("TODO", "TODO: implement");
2109
0
  return 0xFFFFFFFF;
2110
0
}
2111
BOOL WINAPI FreeRDP_WTSLogoffUser(WINPR_ATTR_UNUSED HANDLE hServer)
2112
0
{
2113
0
  WLog_ERR("TODO", "TODO: implement");
2114
0
  return FALSE;
2115
0
}
2116
2117
BOOL WINAPI FreeRDP_WTSLogonUser(WINPR_ATTR_UNUSED HANDLE hServer,
2118
                                 WINPR_ATTR_UNUSED LPCSTR username,
2119
                                 WINPR_ATTR_UNUSED LPCSTR password, WINPR_ATTR_UNUSED LPCSTR domain)
2120
0
{
2121
0
  WLog_ERR("TODO", "TODO: implement");
2122
0
  return FALSE;
2123
0
}
2124
2125
void server_channel_common_free(rdpPeerChannel* channel)
2126
0
{
2127
0
  if (!channel)
2128
0
    return;
2129
0
  MessageQueue_Free(channel->queue);
2130
0
  Stream_Free(channel->receiveData, TRUE);
2131
0
  DeleteCriticalSection(&channel->writeLock);
2132
0
  free(channel);
2133
0
}
2134
2135
rdpPeerChannel* server_channel_common_new(freerdp_peer* client, UINT16 index, UINT32 channelId,
2136
                                          size_t chunkSize, const wObject* callback,
2137
                                          const char* name)
2138
0
{
2139
0
  rdpPeerChannel* channel = (rdpPeerChannel*)calloc(1, sizeof(rdpPeerChannel));
2140
0
  if (!channel)
2141
0
    return nullptr;
2142
2143
0
  InitializeCriticalSection(&channel->writeLock);
2144
2145
0
  channel->receiveData = Stream_New(nullptr, chunkSize);
2146
0
  if (!channel->receiveData)
2147
0
    goto fail;
2148
2149
0
  channel->queue = MessageQueue_New(callback);
2150
0
  if (!channel->queue)
2151
0
    goto fail;
2152
2153
0
  channel->index = index;
2154
0
  channel->client = client;
2155
0
  channel->channelId = channelId;
2156
0
  strncpy(channel->channelName, name, ARRAYSIZE(channel->channelName) - 1);
2157
0
  return channel;
2158
0
fail:
2159
0
  WINPR_PRAGMA_DIAG_PUSH
2160
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2161
0
  server_channel_common_free(channel);
2162
0
  WINPR_PRAGMA_DIAG_POP
2163
0
  return nullptr;
2164
0
}