Coverage Report

Created: 2026-07-30 07:11

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