Coverage Report

Created: 2026-04-12 07:03

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 void wtsCloseVCM(WTSVirtualChannelManager* vcm, bool closeDrdynvc)
1020
0
{
1021
0
  WINPR_ASSERT(vcm);
1022
1023
0
  HashTable_Lock(g_ServerHandles);
1024
1025
/* clang analyzer does not like the check for INVALID_HANDLE_VALUE and considers the path not taken,
1026
 * leading to false positives on memory leaks. */
1027
#ifdef __clang_analyzer__
1028
  const BOOL valid = vcm != nullptr;
1029
#else
1030
0
  const BOOL valid = (vcm != nullptr) && (vcm != INVALID_HANDLE_VALUE);
1031
0
#endif
1032
0
  if (valid)
1033
0
  {
1034
0
    HashTable_Remove(g_ServerHandles, (void*)(UINT_PTR)vcm->SessionId);
1035
1036
0
    HashTable_Free(vcm->dynamicVirtualChannels);
1037
1038
0
    if (vcm->drdynvc_channel)
1039
0
    {
1040
0
      if (closeDrdynvc)
1041
0
        (void)WTSVirtualChannelClose(vcm->drdynvc_channel);
1042
0
      vcm->drdynvc_channel = nullptr;
1043
0
    }
1044
1045
0
    MessageQueue_Free(vcm->queue);
1046
0
    free(vcm);
1047
0
  }
1048
0
  HashTable_Unlock(g_ServerHandles);
1049
0
}
1050
1051
HANDLE WINAPI FreeRDP_WTSOpenServerA(LPSTR pServerName)
1052
0
{
1053
0
  wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1054
1055
0
  rdpContext* context = (rdpContext*)pServerName;
1056
1057
0
  if (!context)
1058
0
    return INVALID_HANDLE_VALUE;
1059
1060
0
  freerdp_peer* client = context->peer;
1061
1062
0
  if (!client)
1063
0
  {
1064
0
    SetLastError(ERROR_INVALID_DATA);
1065
0
    return INVALID_HANDLE_VALUE;
1066
0
  }
1067
1068
0
  if (!InitOnceExecuteOnce(&g_HandleInitializer, initializeHandles, nullptr, nullptr))
1069
0
    return INVALID_HANDLE_VALUE;
1070
1071
0
  WTSVirtualChannelManager* vcm =
1072
0
      (WTSVirtualChannelManager*)calloc(1, sizeof(WTSVirtualChannelManager));
1073
1074
0
  if (!vcm)
1075
0
    goto fail;
1076
1077
0
  vcm->client = client;
1078
0
  vcm->rdp = context->rdp;
1079
1080
0
  queueCallbacks.fnObjectFree = wts_virtual_channel_manager_free_message;
1081
0
  vcm->queue = MessageQueue_New(&queueCallbacks);
1082
1083
0
  if (!vcm->queue)
1084
0
    goto fail;
1085
1086
0
  vcm->dvc_channel_id_seq = 0;
1087
0
  vcm->dynamicVirtualChannels = HashTable_New(TRUE);
1088
1089
0
  if (!vcm->dynamicVirtualChannels)
1090
0
    goto fail;
1091
1092
0
  if (!HashTable_SetHashFunction(vcm->dynamicVirtualChannels, channelId_Hash))
1093
0
    goto fail;
1094
1095
0
  {
1096
0
    wObject* obj = HashTable_ValueObject(vcm->dynamicVirtualChannels);
1097
0
    WINPR_ASSERT(obj);
1098
0
    obj->fnObjectFree = array_channel_free;
1099
1100
0
    obj = HashTable_KeyObject(vcm->dynamicVirtualChannels);
1101
0
    obj->fnObjectEquals = dynChannelMatch;
1102
0
  }
1103
0
  client->ReceiveChannelData = WTSReceiveChannelData;
1104
0
  {
1105
0
    HashTable_Lock(g_ServerHandles);
1106
0
    vcm->SessionId = g_SessionId++;
1107
0
    const BOOL rc =
1108
0
        HashTable_Insert(g_ServerHandles, (void*)(UINT_PTR)vcm->SessionId, (void*)vcm);
1109
0
    HashTable_Unlock(g_ServerHandles);
1110
0
    if (!rc)
1111
0
      goto fail;
1112
0
  }
1113
1114
0
  HANDLE hServer = (HANDLE)vcm;
1115
0
  return hServer;
1116
1117
0
fail:
1118
0
  wtsCloseVCM(vcm, false);
1119
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1120
0
  return INVALID_HANDLE_VALUE;
1121
0
}
1122
1123
HANDLE WINAPI FreeRDP_WTSOpenServerExW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1124
0
{
1125
0
  WLog_ERR("TODO", "TODO: implement");
1126
0
  return INVALID_HANDLE_VALUE;
1127
0
}
1128
1129
HANDLE WINAPI FreeRDP_WTSOpenServerExA(LPSTR pServerName)
1130
0
{
1131
0
  return FreeRDP_WTSOpenServerA(pServerName);
1132
0
}
1133
1134
VOID WINAPI FreeRDP_WTSCloseServer(HANDLE hServer)
1135
0
{
1136
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
1137
0
  wtsCloseVCM(vcm, true);
1138
0
}
1139
1140
BOOL WINAPI FreeRDP_WTSEnumerateSessionsW(WINPR_ATTR_UNUSED HANDLE hServer,
1141
                                          WINPR_ATTR_UNUSED DWORD Reserved,
1142
                                          WINPR_ATTR_UNUSED DWORD Version,
1143
                                          WINPR_ATTR_UNUSED PWTS_SESSION_INFOW* ppSessionInfo,
1144
                                          WINPR_ATTR_UNUSED DWORD* pCount)
1145
0
{
1146
0
  WLog_ERR("TODO", "TODO: implement");
1147
0
  return FALSE;
1148
0
}
1149
1150
BOOL WINAPI FreeRDP_WTSEnumerateSessionsA(WINPR_ATTR_UNUSED HANDLE hServer,
1151
                                          WINPR_ATTR_UNUSED DWORD Reserved,
1152
                                          WINPR_ATTR_UNUSED DWORD Version,
1153
                                          WINPR_ATTR_UNUSED PWTS_SESSION_INFOA* ppSessionInfo,
1154
                                          WINPR_ATTR_UNUSED DWORD* pCount)
1155
0
{
1156
0
  WLog_ERR("TODO", "TODO: implement");
1157
0
  return FALSE;
1158
0
}
1159
1160
BOOL WINAPI FreeRDP_WTSEnumerateSessionsExW(WINPR_ATTR_UNUSED HANDLE hServer,
1161
                                            WINPR_ATTR_UNUSED DWORD* pLevel,
1162
                                            WINPR_ATTR_UNUSED DWORD Filter,
1163
                                            WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1W* ppSessionInfo,
1164
                                            WINPR_ATTR_UNUSED DWORD* pCount)
1165
0
{
1166
0
  WLog_ERR("TODO", "TODO: implement");
1167
0
  return FALSE;
1168
0
}
1169
1170
BOOL WINAPI FreeRDP_WTSEnumerateSessionsExA(WINPR_ATTR_UNUSED HANDLE hServer,
1171
                                            WINPR_ATTR_UNUSED DWORD* pLevel,
1172
                                            WINPR_ATTR_UNUSED DWORD Filter,
1173
                                            WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1A* ppSessionInfo,
1174
                                            WINPR_ATTR_UNUSED DWORD* pCount)
1175
0
{
1176
0
  WLog_ERR("TODO", "TODO: implement");
1177
0
  return FALSE;
1178
0
}
1179
1180
BOOL WINAPI FreeRDP_WTSEnumerateProcessesW(WINPR_ATTR_UNUSED HANDLE hServer,
1181
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1182
                                           WINPR_ATTR_UNUSED DWORD Version,
1183
                                           WINPR_ATTR_UNUSED PWTS_PROCESS_INFOW* ppProcessInfo,
1184
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1185
0
{
1186
0
  WLog_ERR("TODO", "TODO: implement");
1187
0
  return FALSE;
1188
0
}
1189
1190
BOOL WINAPI FreeRDP_WTSEnumerateProcessesA(WINPR_ATTR_UNUSED HANDLE hServer,
1191
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1192
                                           WINPR_ATTR_UNUSED DWORD Version,
1193
                                           WINPR_ATTR_UNUSED PWTS_PROCESS_INFOA* ppProcessInfo,
1194
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1195
0
{
1196
0
  WLog_ERR("TODO", "TODO: implement");
1197
0
  return FALSE;
1198
0
}
1199
1200
BOOL WINAPI FreeRDP_WTSTerminateProcess(WINPR_ATTR_UNUSED HANDLE hServer,
1201
                                        WINPR_ATTR_UNUSED DWORD ProcessId,
1202
                                        WINPR_ATTR_UNUSED DWORD ExitCode)
1203
0
{
1204
0
  WLog_ERR("TODO", "TODO: implement");
1205
0
  return FALSE;
1206
0
}
1207
1208
BOOL WINAPI FreeRDP_WTSQuerySessionInformationW(WINPR_ATTR_UNUSED HANDLE hServer,
1209
                                                WINPR_ATTR_UNUSED DWORD SessionId,
1210
                                                WINPR_ATTR_UNUSED WTS_INFO_CLASS WTSInfoClass,
1211
                                                WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1212
                                                WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1213
0
{
1214
0
  WLog_ERR("TODO", "TODO: implement");
1215
0
  return FALSE;
1216
0
}
1217
1218
BOOL WINAPI FreeRDP_WTSQuerySessionInformationA(HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1219
                                                WTS_INFO_CLASS WTSInfoClass, LPSTR* ppBuffer,
1220
                                                DWORD* pBytesReturned)
1221
0
{
1222
0
  DWORD BytesReturned = 0;
1223
0
  WTSVirtualChannelManager* vcm = nullptr;
1224
0
  vcm = (WTSVirtualChannelManager*)hServer;
1225
1226
0
  if (!vcm)
1227
0
    return FALSE;
1228
1229
0
  if (WTSInfoClass == WTSSessionId)
1230
0
  {
1231
0
    ULONG* pBuffer = nullptr;
1232
0
    BytesReturned = sizeof(ULONG);
1233
0
    pBuffer = (ULONG*)malloc(sizeof(BytesReturned));
1234
1235
0
    if (!pBuffer)
1236
0
    {
1237
0
      SetLastError(g_err_oom);
1238
0
      return FALSE;
1239
0
    }
1240
1241
0
    *pBuffer = vcm->SessionId;
1242
0
    *ppBuffer = (LPSTR)pBuffer;
1243
0
    *pBytesReturned = BytesReturned;
1244
0
    return TRUE;
1245
0
  }
1246
1247
0
  return FALSE;
1248
0
}
1249
1250
BOOL WINAPI FreeRDP_WTSQueryUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1251
                                        WINPR_ATTR_UNUSED LPWSTR pUserName,
1252
                                        WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1253
                                        WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1254
                                        WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1255
0
{
1256
0
  WLog_ERR("TODO", "TODO: implement");
1257
0
  return FALSE;
1258
0
}
1259
1260
BOOL WINAPI FreeRDP_WTSQueryUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1261
                                        WINPR_ATTR_UNUSED LPSTR pUserName,
1262
                                        WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1263
                                        WINPR_ATTR_UNUSED LPSTR* ppBuffer,
1264
                                        WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1265
0
{
1266
0
  WLog_ERR("TODO", "TODO: implement");
1267
0
  return FALSE;
1268
0
}
1269
1270
BOOL WINAPI FreeRDP_WTSSetUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1271
                                      WINPR_ATTR_UNUSED LPWSTR pUserName,
1272
                                      WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1273
                                      WINPR_ATTR_UNUSED LPWSTR pBuffer,
1274
                                      WINPR_ATTR_UNUSED DWORD DataLength)
1275
0
{
1276
0
  WLog_ERR("TODO", "TODO: implement");
1277
0
  return FALSE;
1278
0
}
1279
1280
BOOL WINAPI FreeRDP_WTSSetUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1281
                                      WINPR_ATTR_UNUSED LPSTR pUserName,
1282
                                      WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1283
                                      WINPR_ATTR_UNUSED LPSTR pBuffer,
1284
                                      WINPR_ATTR_UNUSED DWORD DataLength)
1285
0
{
1286
0
  WLog_ERR("TODO", "TODO: implement");
1287
0
  return FALSE;
1288
0
}
1289
1290
BOOL WINAPI
1291
FreeRDP_WTSSendMessageW(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1292
                        WINPR_ATTR_UNUSED LPWSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1293
                        WINPR_ATTR_UNUSED LPWSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1294
                        WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1295
                        WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1296
0
{
1297
0
  WLog_ERR("TODO", "TODO: implement");
1298
0
  return FALSE;
1299
0
}
1300
1301
BOOL WINAPI
1302
FreeRDP_WTSSendMessageA(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1303
                        WINPR_ATTR_UNUSED LPSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1304
                        WINPR_ATTR_UNUSED LPSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1305
                        WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1306
                        WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1307
0
{
1308
0
  WLog_ERR("TODO", "TODO: implement");
1309
0
  return FALSE;
1310
0
}
1311
1312
BOOL WINAPI FreeRDP_WTSDisconnectSession(WINPR_ATTR_UNUSED HANDLE hServer,
1313
                                         WINPR_ATTR_UNUSED DWORD SessionId,
1314
                                         WINPR_ATTR_UNUSED BOOL bWait)
1315
0
{
1316
0
  WLog_ERR("TODO", "TODO: implement");
1317
0
  return FALSE;
1318
0
}
1319
1320
BOOL WINAPI FreeRDP_WTSLogoffSession(WINPR_ATTR_UNUSED HANDLE hServer,
1321
                                     WINPR_ATTR_UNUSED DWORD SessionId,
1322
                                     WINPR_ATTR_UNUSED BOOL bWait)
1323
0
{
1324
0
  WLog_ERR("TODO", "TODO: implement");
1325
0
  return FALSE;
1326
0
}
1327
1328
BOOL WINAPI FreeRDP_WTSShutdownSystem(WINPR_ATTR_UNUSED HANDLE hServer,
1329
                                      WINPR_ATTR_UNUSED DWORD ShutdownFlag)
1330
0
{
1331
0
  WLog_ERR("TODO", "TODO: implement");
1332
0
  return FALSE;
1333
0
}
1334
1335
BOOL WINAPI FreeRDP_WTSWaitSystemEvent(WINPR_ATTR_UNUSED HANDLE hServer,
1336
                                       WINPR_ATTR_UNUSED DWORD EventMask,
1337
                                       WINPR_ATTR_UNUSED DWORD* pEventFlags)
1338
0
{
1339
0
  WLog_ERR("TODO", "TODO: implement");
1340
0
  return FALSE;
1341
0
}
1342
1343
static void peer_channel_queue_free_message(void* obj)
1344
0
{
1345
0
  wMessage* msg = (wMessage*)obj;
1346
0
  if (!msg)
1347
0
    return;
1348
1349
0
  free(msg->context);
1350
0
  msg->context = nullptr;
1351
0
}
1352
1353
static rdpPeerChannel* channel_new(WTSVirtualChannelManager* vcm, freerdp_peer* client,
1354
                                   UINT32 ChannelId, UINT16 index, UINT16 type, size_t chunkSize,
1355
                                   const char* name)
1356
0
{
1357
0
  wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1358
0
  queueCallbacks.fnObjectFree = peer_channel_queue_free_message;
1359
1360
0
  rdpPeerChannel* channel =
1361
0
      server_channel_common_new(client, index, ChannelId, chunkSize, &queueCallbacks, name);
1362
1363
0
  WINPR_ASSERT(vcm);
1364
0
  WINPR_ASSERT(client);
1365
1366
0
  if (!channel)
1367
0
    goto fail;
1368
1369
0
  channel->vcm = vcm;
1370
0
  channel->channelType = type;
1371
0
  channel->creationStatus =
1372
0
      (type == RDP_PEER_CHANNEL_TYPE_SVC) ? ERROR_SUCCESS : ERROR_OPERATION_IN_PROGRESS;
1373
1374
0
  return channel;
1375
0
fail:
1376
0
  channel_free(channel);
1377
0
  return nullptr;
1378
0
}
1379
1380
HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1381
                                            LPSTR pVirtualName)
1382
0
{
1383
0
  size_t length = 0;
1384
0
  rdpMcs* mcs = nullptr;
1385
0
  rdpMcsChannel* joined_channel = nullptr;
1386
0
  freerdp_peer* client = nullptr;
1387
0
  rdpPeerChannel* channel = nullptr;
1388
0
  WTSVirtualChannelManager* vcm = nullptr;
1389
0
  HANDLE hChannelHandle = nullptr;
1390
0
  rdpContext* context = nullptr;
1391
0
  vcm = (WTSVirtualChannelManager*)hServer;
1392
1393
0
  if (!vcm)
1394
0
  {
1395
0
    SetLastError(ERROR_INVALID_DATA);
1396
0
    return nullptr;
1397
0
  }
1398
1399
0
  client = vcm->client;
1400
0
  WINPR_ASSERT(client);
1401
1402
0
  context = client->context;
1403
0
  WINPR_ASSERT(context);
1404
0
  WINPR_ASSERT(context->rdp);
1405
0
  WINPR_ASSERT(context->settings);
1406
1407
0
  mcs = context->rdp->mcs;
1408
0
  WINPR_ASSERT(mcs);
1409
1410
0
  length = strnlen(pVirtualName, CHANNEL_NAME_LEN + 1);
1411
1412
0
  if (length > CHANNEL_NAME_LEN)
1413
0
  {
1414
0
    SetLastError(ERROR_NOT_FOUND);
1415
0
    return nullptr;
1416
0
  }
1417
1418
0
  UINT32 index = 0;
1419
0
  for (; index < mcs->channelCount; index++)
1420
0
  {
1421
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
1422
0
    if (mchannel->joined && (strncmp(mchannel->Name, pVirtualName, length) == 0))
1423
0
    {
1424
0
      joined_channel = mchannel;
1425
0
      break;
1426
0
    }
1427
0
  }
1428
1429
0
  if (!joined_channel)
1430
0
  {
1431
0
    SetLastError(ERROR_NOT_FOUND);
1432
0
    return nullptr;
1433
0
  }
1434
1435
0
  channel = (rdpPeerChannel*)joined_channel->handle;
1436
1437
0
  if (!channel)
1438
0
  {
1439
0
    const UINT32 VCChunkSize =
1440
0
        freerdp_settings_get_uint32(context->settings, FreeRDP_VCChunkSize);
1441
1442
0
    WINPR_ASSERT(index <= UINT16_MAX);
1443
0
    channel = channel_new(vcm, client, joined_channel->ChannelId, (UINT16)index,
1444
0
                          RDP_PEER_CHANNEL_TYPE_SVC, VCChunkSize, pVirtualName);
1445
1446
0
    if (!channel)
1447
0
      goto fail;
1448
1449
0
    joined_channel->handle = channel;
1450
0
  }
1451
1452
0
  hChannelHandle = (HANDLE)channel;
1453
0
  return hChannelHandle;
1454
0
fail:
1455
0
  channel_free(channel);
1456
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1457
0
  return nullptr;
1458
0
}
1459
1460
HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenEx(DWORD SessionId, LPSTR pVirtualName, DWORD flags)
1461
0
{
1462
0
  wStream* s = nullptr;
1463
0
  rdpPeerChannel* channel = nullptr;
1464
0
  BOOL joined = FALSE;
1465
0
  ULONG written = 0;
1466
1467
0
  if (SessionId == WTS_CURRENT_SESSION)
1468
0
    return nullptr;
1469
1470
0
  HashTable_Lock(g_ServerHandles);
1471
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)HashTable_GetItemValue(
1472
0
      g_ServerHandles, (void*)(UINT_PTR)SessionId);
1473
1474
0
  if (!vcm)
1475
0
    goto end;
1476
1477
0
  if (!(flags & WTS_CHANNEL_OPTION_DYNAMIC))
1478
0
  {
1479
0
    HashTable_Unlock(g_ServerHandles);
1480
0
    return FreeRDP_WTSVirtualChannelOpen((HANDLE)vcm, SessionId, pVirtualName);
1481
0
  }
1482
1483
0
  freerdp_peer* client = vcm->client;
1484
0
  WINPR_ASSERT(client);
1485
0
  WINPR_ASSERT(client->context);
1486
0
  WINPR_ASSERT(client->context->rdp);
1487
1488
0
  rdpMcs* mcs = client->context->rdp->mcs;
1489
0
  WINPR_ASSERT(mcs);
1490
1491
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
1492
0
  {
1493
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
1494
0
    if (mchannel->joined &&
1495
0
        (strncmp(mchannel->Name, DRDYNVC_SVC_CHANNEL_NAME, CHANNEL_NAME_LEN + 1) == 0))
1496
0
    {
1497
0
      joined = TRUE;
1498
0
      break;
1499
0
    }
1500
0
  }
1501
1502
0
  if (!joined)
1503
0
  {
1504
0
    SetLastError(ERROR_NOT_FOUND);
1505
0
    goto end;
1506
0
  }
1507
1508
0
  if (!vcm->drdynvc_channel || (vcm->drdynvc_state != DRDYNVC_STATE_READY))
1509
0
  {
1510
0
    SetLastError(ERROR_NOT_READY);
1511
0
    goto end;
1512
0
  }
1513
1514
0
  WINPR_ASSERT(client);
1515
0
  WINPR_ASSERT(client->context);
1516
0
  WINPR_ASSERT(client->context->settings);
1517
1518
0
  const UINT32 VCChunkSize =
1519
0
      freerdp_settings_get_uint32(client->context->settings, FreeRDP_VCChunkSize);
1520
0
  channel = channel_new(vcm, client, 0, 0, RDP_PEER_CHANNEL_TYPE_DVC, VCChunkSize, pVirtualName);
1521
1522
0
  if (!channel)
1523
0
  {
1524
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1525
0
    goto end;
1526
0
  }
1527
1528
0
  const LONG hdl = InterlockedIncrement(&vcm->dvc_channel_id_seq);
1529
0
  channel->channelId = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1530
1531
0
  if (!HashTable_Insert(vcm->dynamicVirtualChannels, &channel->channelId, channel))
1532
0
  {
1533
0
    channel_free(channel);
1534
0
    channel = nullptr;
1535
0
    goto fail;
1536
0
  }
1537
0
  s = Stream_New(nullptr, 64);
1538
1539
0
  if (!s)
1540
0
    goto fail;
1541
1542
0
  if (!wts_write_drdynvc_create_request(s, channel->channelId, pVirtualName))
1543
0
    goto fail;
1544
1545
0
  {
1546
0
    const size_t pos = Stream_GetPosition(s);
1547
0
    WINPR_ASSERT(pos <= UINT32_MAX);
1548
0
    if (!WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s, char), (UINT32)pos,
1549
0
                                &written))
1550
0
      goto fail;
1551
0
  }
1552
1553
0
end:
1554
0
  Stream_Free(s, TRUE);
1555
0
  HashTable_Unlock(g_ServerHandles);
1556
0
  return channel;
1557
1558
0
fail:
1559
0
  Stream_Free(s, TRUE);
1560
0
  if (channel)
1561
0
    HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1562
0
  HashTable_Unlock(g_ServerHandles);
1563
1564
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1565
0
  return nullptr;
1566
0
}
1567
1568
BOOL WINAPI FreeRDP_WTSVirtualChannelClose(HANDLE hChannelHandle)
1569
0
{
1570
0
  wStream* s = nullptr;
1571
0
  rdpMcs* mcs = nullptr;
1572
1573
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1574
0
  BOOL ret = TRUE;
1575
1576
0
  if (channel)
1577
0
  {
1578
0
    WTSVirtualChannelManager* vcm = channel->vcm;
1579
1580
0
    WINPR_ASSERT(vcm);
1581
0
    WINPR_ASSERT(vcm->client);
1582
0
    WINPR_ASSERT(vcm->client->context);
1583
0
    WINPR_ASSERT(vcm->client->context->rdp);
1584
0
    mcs = vcm->client->context->rdp->mcs;
1585
1586
0
    if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1587
0
    {
1588
0
      if (channel->index < mcs->channelCount)
1589
0
      {
1590
0
        rdpMcsChannel* cur = &mcs->channels[channel->index];
1591
0
        rdpPeerChannel* peerChannel = (rdpPeerChannel*)cur->handle;
1592
0
        channel_free(peerChannel);
1593
0
        cur->handle = nullptr;
1594
0
      }
1595
0
    }
1596
0
    else
1597
0
    {
1598
0
      if (channel->dvc_open_state == DVC_OPEN_STATE_SUCCEEDED)
1599
0
      {
1600
0
        ULONG written = 0;
1601
0
        s = Stream_New(nullptr, 8);
1602
1603
0
        if (!s)
1604
0
        {
1605
0
          WLog_ERR(TAG, "Stream_New failed!");
1606
0
          ret = FALSE;
1607
0
        }
1608
0
        else
1609
0
        {
1610
0
          wts_write_drdynvc_header(s, CLOSE_REQUEST_PDU, channel->channelId);
1611
1612
0
          const size_t pos = Stream_GetPosition(s);
1613
0
          WINPR_ASSERT(pos <= UINT32_MAX);
1614
0
          ret = WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s, char),
1615
0
                                       (UINT32)pos, &written);
1616
0
          Stream_Free(s, TRUE);
1617
0
        }
1618
0
      }
1619
0
      HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1620
0
    }
1621
0
  }
1622
1623
0
  return ret;
1624
0
}
1625
1626
BOOL WINAPI FreeRDP_WTSVirtualChannelRead(HANDLE hChannelHandle, WINPR_ATTR_UNUSED ULONG TimeOut,
1627
                                          PCHAR Buffer, ULONG BufferSize, PULONG pBytesRead)
1628
0
{
1629
0
  BYTE* buffer = nullptr;
1630
0
  wMessage message = WINPR_C_ARRAY_INIT;
1631
0
  wtsChannelMessage* messageCtx = nullptr;
1632
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1633
1634
0
  WINPR_ASSERT(channel);
1635
1636
0
  if (!MessageQueue_Peek(channel->queue, &message, FALSE))
1637
0
  {
1638
0
    SetLastError(ERROR_NO_DATA);
1639
0
    *pBytesRead = 0;
1640
0
    return FALSE;
1641
0
  }
1642
1643
0
  messageCtx = message.context;
1644
1645
0
  if (messageCtx == nullptr)
1646
0
    return FALSE;
1647
1648
0
  buffer = (BYTE*)(messageCtx + 1);
1649
0
  *pBytesRead = messageCtx->length - messageCtx->offset;
1650
1651
0
  if (Buffer == nullptr || BufferSize == 0)
1652
0
  {
1653
0
    return TRUE;
1654
0
  }
1655
1656
0
  if (*pBytesRead > BufferSize)
1657
0
    *pBytesRead = BufferSize;
1658
1659
0
  CopyMemory(Buffer, buffer + messageCtx->offset, *pBytesRead);
1660
0
  messageCtx->offset += *pBytesRead;
1661
1662
0
  if (messageCtx->offset >= messageCtx->length)
1663
0
  {
1664
0
    const int rc = MessageQueue_Peek(channel->queue, &message, TRUE);
1665
0
    peer_channel_queue_free_message(&message);
1666
0
    if (rc < 0)
1667
0
      return FALSE;
1668
0
  }
1669
1670
0
  return TRUE;
1671
0
}
1672
1673
BOOL WINAPI FreeRDP_WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer, ULONG uLength,
1674
                                           PULONG pBytesWritten)
1675
0
{
1676
0
  wStream* s = nullptr;
1677
0
  int cbLen = 0;
1678
0
  int cbChId = 0;
1679
0
  int first = 0;
1680
0
  BYTE* buffer = nullptr;
1681
0
  size_t totalWritten = 0;
1682
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1683
0
  BOOL ret = FALSE;
1684
1685
0
  if (!channel)
1686
0
    return FALSE;
1687
1688
0
  EnterCriticalSection(&channel->writeLock);
1689
0
  WINPR_ASSERT(channel->vcm);
1690
0
  if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1691
0
  {
1692
0
    buffer = (BYTE*)malloc(uLength);
1693
1694
0
    if (!buffer)
1695
0
    {
1696
0
      SetLastError(g_err_oom);
1697
0
      goto fail;
1698
0
    }
1699
1700
0
    CopyMemory(buffer, Buffer, uLength);
1701
0
    totalWritten = uLength;
1702
0
    if (!wts_queue_send_item(channel, buffer, uLength))
1703
0
      goto fail;
1704
0
  }
1705
0
  else if (!channel->vcm->drdynvc_channel || (channel->vcm->drdynvc_state != DRDYNVC_STATE_READY))
1706
0
  {
1707
0
    DEBUG_DVC("drdynvc not ready");
1708
0
    goto fail;
1709
0
  }
1710
0
  else
1711
0
  {
1712
0
    first = TRUE;
1713
1714
0
    size_t Length = uLength;
1715
0
    while (Length > 0)
1716
0
    {
1717
0
      s = Stream_New(nullptr, DVC_MAX_DATA_PDU_SIZE);
1718
1719
0
      if (!s)
1720
0
      {
1721
0
        WLog_ERR(TAG, "Stream_New failed!");
1722
0
        SetLastError(g_err_oom);
1723
0
        goto fail;
1724
0
      }
1725
1726
0
      buffer = Stream_Buffer(s);
1727
0
      Stream_Seek_UINT8(s);
1728
0
      cbChId = wts_write_variable_uint(s, channel->channelId);
1729
1730
0
      if (first && (Length > Stream_GetRemainingLength(s)))
1731
0
      {
1732
0
        cbLen = wts_write_variable_uint(s, WINPR_ASSERTING_INT_CAST(uint32_t, Length));
1733
0
        buffer[0] = ((DATA_FIRST_PDU << 4) | (cbLen << 2) | cbChId) & 0xFF;
1734
0
      }
1735
0
      else
1736
0
      {
1737
0
        buffer[0] = ((DATA_PDU << 4) | cbChId) & 0xFF;
1738
0
      }
1739
1740
0
      first = FALSE;
1741
0
      size_t written = Stream_GetRemainingLength(s);
1742
1743
0
      if (written > Length)
1744
0
        written = Length;
1745
1746
0
      Stream_Write(s, Buffer, written);
1747
0
      const size_t length = Stream_GetPosition(s);
1748
0
      Stream_Free(s, FALSE);
1749
0
      if (length > UINT32_MAX)
1750
0
        goto fail;
1751
0
      Length -= written;
1752
0
      Buffer += written;
1753
0
      totalWritten += written;
1754
0
      if (!wts_queue_send_item(channel->vcm->drdynvc_channel, buffer, (UINT32)length))
1755
0
        goto fail;
1756
0
    }
1757
0
  }
1758
1759
0
  if (pBytesWritten)
1760
0
    *pBytesWritten = WINPR_ASSERTING_INT_CAST(uint32_t, totalWritten);
1761
1762
0
  ret = TRUE;
1763
0
fail:
1764
0
  LeaveCriticalSection(&channel->writeLock);
1765
0
  return ret;
1766
0
}
1767
1768
BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeInput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1769
0
{
1770
0
  WLog_ERR("TODO", "TODO: implement");
1771
0
  return TRUE;
1772
0
}
1773
1774
BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeOutput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1775
0
{
1776
0
  WLog_ERR("TODO", "TODO: implement");
1777
0
  return TRUE;
1778
0
}
1779
1780
BOOL WINAPI FreeRDP_WTSVirtualChannelQuery(HANDLE hChannelHandle, WTS_VIRTUAL_CLASS WtsVirtualClass,
1781
                                           PVOID* ppBuffer, DWORD* pBytesReturned)
1782
0
{
1783
0
  void* pfd = nullptr;
1784
0
  BOOL bval = 0;
1785
0
  void* fds[10] = WINPR_C_ARRAY_INIT;
1786
0
  HANDLE hEvent = nullptr;
1787
0
  int fds_count = 0;
1788
0
  BOOL status = FALSE;
1789
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1790
1791
0
  WINPR_ASSERT(channel);
1792
1793
0
  switch ((UINT32)WtsVirtualClass)
1794
0
  {
1795
0
    case WTSVirtualFileHandle:
1796
0
      hEvent = MessageQueue_Event(channel->queue);
1797
0
      pfd = GetEventWaitObject(hEvent);
1798
1799
0
      if (pfd)
1800
0
      {
1801
0
        fds[fds_count] = pfd;
1802
0
        (fds_count)++;
1803
0
      }
1804
1805
0
      *ppBuffer = malloc(sizeof(void*));
1806
1807
0
      if (!*ppBuffer)
1808
0
      {
1809
0
        SetLastError(g_err_oom);
1810
0
      }
1811
0
      else
1812
0
      {
1813
0
        CopyMemory(*ppBuffer, (void*)&fds[0], sizeof(void*));
1814
0
        *pBytesReturned = sizeof(void*);
1815
0
        status = TRUE;
1816
0
      }
1817
1818
0
      break;
1819
1820
0
    case WTSVirtualEventHandle:
1821
0
      hEvent = MessageQueue_Event(channel->queue);
1822
1823
0
      *ppBuffer = malloc(sizeof(HANDLE));
1824
1825
0
      if (!*ppBuffer)
1826
0
      {
1827
0
        SetLastError(g_err_oom);
1828
0
      }
1829
0
      else
1830
0
      {
1831
0
        CopyMemory(*ppBuffer, (void*)&hEvent, sizeof(HANDLE));
1832
0
        *pBytesReturned = sizeof(void*);
1833
0
        status = TRUE;
1834
0
      }
1835
1836
0
      break;
1837
1838
0
    case WTSVirtualChannelReady:
1839
0
      if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1840
0
      {
1841
0
        bval = TRUE;
1842
0
        status = TRUE;
1843
0
      }
1844
0
      else
1845
0
      {
1846
0
        switch (channel->dvc_open_state)
1847
0
        {
1848
0
          case DVC_OPEN_STATE_NONE:
1849
0
            bval = FALSE;
1850
0
            status = TRUE;
1851
0
            break;
1852
1853
0
          case DVC_OPEN_STATE_SUCCEEDED:
1854
0
            bval = TRUE;
1855
0
            status = TRUE;
1856
0
            break;
1857
1858
0
          default:
1859
0
            *ppBuffer = nullptr;
1860
0
            *pBytesReturned = 0;
1861
0
            return FALSE;
1862
0
        }
1863
0
      }
1864
1865
0
      *ppBuffer = malloc(sizeof(BOOL));
1866
1867
0
      if (!*ppBuffer)
1868
0
      {
1869
0
        SetLastError(g_err_oom);
1870
0
        status = FALSE;
1871
0
      }
1872
0
      else
1873
0
      {
1874
0
        CopyMemory(*ppBuffer, &bval, sizeof(BOOL));
1875
0
        *pBytesReturned = sizeof(BOOL);
1876
0
      }
1877
1878
0
      break;
1879
0
    case WTSVirtualChannelOpenStatus:
1880
0
    {
1881
0
      INT32 value = channel->creationStatus;
1882
0
      status = TRUE;
1883
1884
0
      *ppBuffer = malloc(sizeof(value));
1885
0
      if (!*ppBuffer)
1886
0
      {
1887
0
        SetLastError(g_err_oom);
1888
0
        status = FALSE;
1889
0
      }
1890
0
      else
1891
0
      {
1892
0
        CopyMemory(*ppBuffer, &value, sizeof(value));
1893
0
        *pBytesReturned = sizeof(value);
1894
0
      }
1895
0
      break;
1896
0
    }
1897
0
    default:
1898
0
      break;
1899
0
  }
1900
1901
0
  return status;
1902
0
}
1903
1904
VOID WINAPI FreeRDP_WTSFreeMemory(PVOID pMemory)
1905
0
{
1906
0
  free(pMemory);
1907
0
}
1908
1909
BOOL WINAPI FreeRDP_WTSFreeMemoryExW(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
1910
                                     WINPR_ATTR_UNUSED PVOID pMemory,
1911
                                     WINPR_ATTR_UNUSED ULONG NumberOfEntries)
1912
0
{
1913
0
  WLog_ERR("TODO", "TODO: implement");
1914
0
  return FALSE;
1915
0
}
1916
1917
BOOL WINAPI FreeRDP_WTSFreeMemoryExA(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
1918
                                     WINPR_ATTR_UNUSED PVOID pMemory,
1919
                                     WINPR_ATTR_UNUSED ULONG NumberOfEntries)
1920
0
{
1921
0
  WLog_ERR("TODO", "TODO: implement");
1922
0
  return FALSE;
1923
0
}
1924
1925
BOOL WINAPI FreeRDP_WTSRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd,
1926
                                                   WINPR_ATTR_UNUSED DWORD dwFlags)
1927
0
{
1928
0
  WLog_ERR("TODO", "TODO: implement");
1929
0
  return FALSE;
1930
0
}
1931
1932
BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd)
1933
0
{
1934
0
  WLog_ERR("TODO", "TODO: implement");
1935
0
  return FALSE;
1936
0
}
1937
1938
BOOL WINAPI FreeRDP_WTSRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
1939
                                                     WINPR_ATTR_UNUSED HWND hWnd,
1940
                                                     WINPR_ATTR_UNUSED DWORD dwFlags)
1941
0
{
1942
0
  WLog_ERR("TODO", "TODO: implement");
1943
0
  return FALSE;
1944
0
}
1945
1946
BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
1947
                                                       WINPR_ATTR_UNUSED HWND hWnd)
1948
0
{
1949
0
  WLog_ERR("TODO", "TODO: implement");
1950
0
  return FALSE;
1951
0
}
1952
1953
BOOL WINAPI FreeRDP_WTSQueryUserToken(WINPR_ATTR_UNUSED ULONG SessionId,
1954
                                      WINPR_ATTR_UNUSED PHANDLE phToken)
1955
0
{
1956
0
  WLog_ERR("TODO", "TODO: implement");
1957
0
  return FALSE;
1958
0
}
1959
1960
BOOL WINAPI FreeRDP_WTSEnumerateProcessesExW(WINPR_ATTR_UNUSED HANDLE hServer,
1961
                                             WINPR_ATTR_UNUSED DWORD* pLevel,
1962
                                             WINPR_ATTR_UNUSED DWORD SessionId,
1963
                                             WINPR_ATTR_UNUSED LPWSTR* ppProcessInfo,
1964
                                             WINPR_ATTR_UNUSED DWORD* pCount)
1965
0
{
1966
0
  WLog_ERR("TODO", "TODO: implement");
1967
0
  return FALSE;
1968
0
}
1969
1970
BOOL WINAPI FreeRDP_WTSEnumerateProcessesExA(WINPR_ATTR_UNUSED HANDLE hServer,
1971
                                             WINPR_ATTR_UNUSED DWORD* pLevel,
1972
                                             WINPR_ATTR_UNUSED DWORD SessionId,
1973
                                             WINPR_ATTR_UNUSED LPSTR* ppProcessInfo,
1974
                                             WINPR_ATTR_UNUSED DWORD* pCount)
1975
0
{
1976
0
  WLog_ERR("TODO", "TODO: implement");
1977
0
  return FALSE;
1978
0
}
1979
1980
BOOL WINAPI FreeRDP_WTSEnumerateListenersW(WINPR_ATTR_UNUSED HANDLE hServer,
1981
                                           WINPR_ATTR_UNUSED PVOID pReserved,
1982
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1983
                                           WINPR_ATTR_UNUSED PWTSLISTENERNAMEW pListeners,
1984
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1985
0
{
1986
0
  WLog_ERR("TODO", "TODO: implement");
1987
0
  return FALSE;
1988
0
}
1989
1990
BOOL WINAPI FreeRDP_WTSEnumerateListenersA(WINPR_ATTR_UNUSED HANDLE hServer,
1991
                                           WINPR_ATTR_UNUSED PVOID pReserved,
1992
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1993
                                           WINPR_ATTR_UNUSED PWTSLISTENERNAMEA pListeners,
1994
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1995
0
{
1996
0
  WLog_ERR("TODO", "TODO: implement");
1997
0
  return FALSE;
1998
0
}
1999
2000
BOOL WINAPI FreeRDP_WTSQueryListenerConfigW(WINPR_ATTR_UNUSED HANDLE hServer,
2001
                                            WINPR_ATTR_UNUSED PVOID pReserved,
2002
                                            WINPR_ATTR_UNUSED DWORD Reserved,
2003
                                            WINPR_ATTR_UNUSED LPWSTR pListenerName,
2004
                                            WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer)
2005
0
{
2006
0
  WLog_ERR("TODO", "TODO: implement");
2007
0
  return FALSE;
2008
0
}
2009
2010
BOOL WINAPI FreeRDP_WTSQueryListenerConfigA(WINPR_ATTR_UNUSED HANDLE hServer,
2011
                                            WINPR_ATTR_UNUSED PVOID pReserved,
2012
                                            WINPR_ATTR_UNUSED DWORD Reserved,
2013
                                            WINPR_ATTR_UNUSED LPSTR pListenerName,
2014
                                            WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer)
2015
0
{
2016
0
  WLog_ERR("TODO", "TODO: implement");
2017
0
  return FALSE;
2018
0
}
2019
2020
BOOL WINAPI FreeRDP_WTSCreateListenerW(WINPR_ATTR_UNUSED HANDLE hServer,
2021
                                       WINPR_ATTR_UNUSED PVOID pReserved,
2022
                                       WINPR_ATTR_UNUSED DWORD Reserved,
2023
                                       WINPR_ATTR_UNUSED LPWSTR pListenerName,
2024
                                       WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer,
2025
                                       WINPR_ATTR_UNUSED DWORD flag)
2026
0
{
2027
0
  WLog_ERR("TODO", "TODO: implement");
2028
0
  return FALSE;
2029
0
}
2030
2031
BOOL WINAPI FreeRDP_WTSCreateListenerA(WINPR_ATTR_UNUSED HANDLE hServer,
2032
                                       WINPR_ATTR_UNUSED PVOID pReserved,
2033
                                       WINPR_ATTR_UNUSED DWORD Reserved,
2034
                                       WINPR_ATTR_UNUSED LPSTR pListenerName,
2035
                                       WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer,
2036
                                       WINPR_ATTR_UNUSED DWORD flag)
2037
0
{
2038
0
  WLog_ERR("TODO", "TODO: implement");
2039
0
  return FALSE;
2040
0
}
2041
2042
BOOL WINAPI FreeRDP_WTSSetListenerSecurityW(
2043
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2044
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2045
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2046
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2047
0
{
2048
0
  WLog_ERR("TODO", "TODO: implement");
2049
0
  return FALSE;
2050
0
}
2051
2052
BOOL WINAPI FreeRDP_WTSSetListenerSecurityA(
2053
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2054
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2055
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2056
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2057
0
{
2058
0
  WLog_ERR("TODO", "TODO: implement");
2059
0
  return FALSE;
2060
0
}
2061
2062
BOOL WINAPI FreeRDP_WTSGetListenerSecurityW(
2063
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2064
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2065
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2066
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2067
    WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2068
0
{
2069
0
  WLog_ERR("TODO", "TODO: implement");
2070
0
  return FALSE;
2071
0
}
2072
2073
BOOL WINAPI FreeRDP_WTSGetListenerSecurityA(
2074
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2075
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2076
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2077
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2078
    WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2079
0
{
2080
0
  WLog_ERR("TODO", "TODO: implement");
2081
0
  return FALSE;
2082
0
}
2083
2084
BOOL CDECL FreeRDP_WTSEnableChildSessions(WINPR_ATTR_UNUSED BOOL bEnable)
2085
0
{
2086
0
  WLog_ERR("TODO", "TODO: implement");
2087
0
  return FALSE;
2088
0
}
2089
2090
BOOL CDECL FreeRDP_WTSIsChildSessionsEnabled(WINPR_ATTR_UNUSED PBOOL pbEnabled)
2091
0
{
2092
0
  WLog_ERR("TODO", "TODO: implement");
2093
0
  return FALSE;
2094
0
}
2095
2096
BOOL CDECL FreeRDP_WTSGetChildSessionId(WINPR_ATTR_UNUSED PULONG pSessionId)
2097
0
{
2098
0
  WLog_ERR("TODO", "TODO: implement");
2099
0
  return FALSE;
2100
0
}
2101
2102
DWORD WINAPI FreeRDP_WTSGetActiveConsoleSessionId(void)
2103
0
{
2104
0
  WLog_ERR("TODO", "TODO: implement");
2105
0
  return 0xFFFFFFFF;
2106
0
}
2107
BOOL WINAPI FreeRDP_WTSLogoffUser(WINPR_ATTR_UNUSED HANDLE hServer)
2108
0
{
2109
0
  WLog_ERR("TODO", "TODO: implement");
2110
0
  return FALSE;
2111
0
}
2112
2113
BOOL WINAPI FreeRDP_WTSLogonUser(WINPR_ATTR_UNUSED HANDLE hServer,
2114
                                 WINPR_ATTR_UNUSED LPCSTR username,
2115
                                 WINPR_ATTR_UNUSED LPCSTR password, WINPR_ATTR_UNUSED LPCSTR domain)
2116
0
{
2117
0
  WLog_ERR("TODO", "TODO: implement");
2118
0
  return FALSE;
2119
0
}
2120
2121
void server_channel_common_free(rdpPeerChannel* channel)
2122
0
{
2123
0
  if (!channel)
2124
0
    return;
2125
0
  MessageQueue_Free(channel->queue);
2126
0
  Stream_Free(channel->receiveData, TRUE);
2127
0
  DeleteCriticalSection(&channel->writeLock);
2128
0
  free(channel);
2129
0
}
2130
2131
rdpPeerChannel* server_channel_common_new(freerdp_peer* client, UINT16 index, UINT32 channelId,
2132
                                          size_t chunkSize, const wObject* callback,
2133
                                          const char* name)
2134
0
{
2135
0
  rdpPeerChannel* channel = (rdpPeerChannel*)calloc(1, sizeof(rdpPeerChannel));
2136
0
  if (!channel)
2137
0
    return nullptr;
2138
2139
0
  InitializeCriticalSection(&channel->writeLock);
2140
2141
0
  channel->receiveData = Stream_New(nullptr, chunkSize);
2142
0
  if (!channel->receiveData)
2143
0
    goto fail;
2144
2145
0
  channel->queue = MessageQueue_New(callback);
2146
0
  if (!channel->queue)
2147
0
    goto fail;
2148
2149
0
  channel->index = index;
2150
0
  channel->client = client;
2151
0
  channel->channelId = channelId;
2152
0
  strncpy(channel->channelName, name, ARRAYSIZE(channel->channelName) - 1);
2153
0
  return channel;
2154
0
fail:
2155
0
  WINPR_PRAGMA_DIAG_PUSH
2156
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2157
0
  server_channel_common_free(channel);
2158
0
  WINPR_PRAGMA_DIAG_POP
2159
0
  return nullptr;
2160
0
}