Coverage Report

Created: 2026-10-28 06:54

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