Coverage Report

Created: 2026-03-04 06:17

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