Coverage Report

Created: 2026-08-31 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/rdpdr/client/rdpdr_main.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Device Redirection Virtual Channel
4
 *
5
 * Copyright 2010-2011 Vic Lee
6
 * Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
 * Copyright 2015-2016 Thincast Technologies GmbH
8
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
9
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
10
 * Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 *     http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24
25
#include <freerdp/config.h>
26
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <stdint.h>
31
32
#include <winpr/crt.h>
33
#include <winpr/sysinfo.h>
34
#include <winpr/assert.h>
35
#include <winpr/stream.h>
36
37
#include <winpr/print.h>
38
#include <winpr/sspicli.h>
39
40
#include <freerdp/types.h>
41
#include <freerdp/freerdp.h>
42
#include <freerdp/constants.h>
43
#include <freerdp/channels/log.h>
44
#include <freerdp/channels/rdpdr.h>
45
#include <freerdp/utils/rdpdr_utils.h>
46
47
#ifdef _WIN32
48
#include <windows.h>
49
#include <dbt.h>
50
#else
51
#include <sys/types.h>
52
#include <sys/stat.h>
53
#include <fcntl.h>
54
#endif
55
56
#ifdef __MACOSX__
57
#include <CoreFoundation/CoreFoundation.h>
58
#include <stdio.h>
59
#include <dirent.h>
60
#include <sys/types.h>
61
#include <sys/stat.h>
62
#include <unistd.h>
63
#endif
64
65
#include "rdpdr_capabilities.h"
66
67
#include "devman.h"
68
#include "irp.h"
69
70
#include "rdpdr_main.h"
71
72
0
#define TAG CHANNELS_TAG("rdpdr.client")
73
74
/* IMPORTANT: Keep in sync with DRIVE_DEVICE */
75
typedef struct
76
{
77
  DEVICE device;
78
  WCHAR* path;
79
  BOOL automount;
80
} DEVICE_DRIVE_EXT;
81
82
static const char* rdpdr_state_str(enum RDPDR_CHANNEL_STATE state)
83
0
{
84
0
  switch (state)
85
0
  {
86
0
    case RDPDR_CHANNEL_STATE_INITIAL:
87
0
      return "RDPDR_CHANNEL_STATE_INITIAL";
88
0
    case RDPDR_CHANNEL_STATE_ANNOUNCE:
89
0
      return "RDPDR_CHANNEL_STATE_ANNOUNCE";
90
0
    case RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY:
91
0
      return "RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY";
92
0
    case RDPDR_CHANNEL_STATE_NAME_REQUEST:
93
0
      return "RDPDR_CHANNEL_STATE_NAME_REQUEST";
94
0
    case RDPDR_CHANNEL_STATE_SERVER_CAPS:
95
0
      return "RDPDR_CHANNEL_STATE_SERVER_CAPS";
96
0
    case RDPDR_CHANNEL_STATE_CLIENT_CAPS:
97
0
      return "RDPDR_CHANNEL_STATE_CLIENT_CAPS";
98
0
    case RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM:
99
0
      return "RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM";
100
0
    case RDPDR_CHANNEL_STATE_READY:
101
0
      return "RDPDR_CHANNEL_STATE_READY";
102
0
    case RDPDR_CHANNEL_STATE_USER_LOGGEDON:
103
0
      return "RDPDR_CHANNEL_STATE_USER_LOGGEDON";
104
0
    default:
105
0
      return "RDPDR_CHANNEL_STATE_UNKNOWN";
106
0
  }
107
0
}
108
109
static const char* support_str(BOOL val)
110
0
{
111
0
  if (val)
112
0
    return "supported";
113
0
  return "not found";
114
0
}
115
116
static const char* rdpdr_caps_pdu_str(UINT32 flag)
117
0
{
118
0
  switch (flag)
119
0
  {
120
0
    case RDPDR_DEVICE_REMOVE_PDUS:
121
0
      return "RDPDR_USER_LOGGEDON_PDU";
122
0
    case RDPDR_CLIENT_DISPLAY_NAME_PDU:
123
0
      return "RDPDR_CLIENT_DISPLAY_NAME_PDU";
124
0
    case RDPDR_USER_LOGGEDON_PDU:
125
0
      return "RDPDR_USER_LOGGEDON_PDU";
126
0
    default:
127
0
      return "RDPDR_UNKNONW";
128
0
  }
129
0
}
130
131
static BOOL rdpdr_check_extended_pdu_flag(rdpdrPlugin* rdpdr, UINT32 flag)
132
0
{
133
0
  WINPR_ASSERT(rdpdr);
134
135
0
  const BOOL client = (rdpdr->clientExtendedPDU & flag) != 0;
136
0
  const BOOL server = (rdpdr->serverExtendedPDU & flag) != 0;
137
138
0
  if (!client || !server)
139
0
  {
140
0
    WLog_Print(rdpdr->log, WLOG_WARN, "Checking ExtendedPDU::%s, client %s, server %s",
141
0
               rdpdr_caps_pdu_str(flag), support_str(client), support_str(server));
142
0
    return FALSE;
143
0
  }
144
0
  return TRUE;
145
0
}
146
147
BOOL rdpdr_state_advance(rdpdrPlugin* rdpdr, enum RDPDR_CHANNEL_STATE next)
148
0
{
149
0
  WINPR_ASSERT(rdpdr);
150
151
0
  if (next != rdpdr->state)
152
0
    WLog_Print(rdpdr->log, WLOG_DEBUG, "[RDPDR] transition from %s to %s",
153
0
               rdpdr_state_str(rdpdr->state), rdpdr_state_str(next));
154
0
  rdpdr->state = next;
155
0
  return TRUE;
156
0
}
157
158
static BOOL device_foreach(rdpdrPlugin* rdpdr, BOOL abortOnFail,
159
                           BOOL (*fkt)(ULONG_PTR key, void* element, void* data), void* data)
160
0
{
161
0
  BOOL rc = TRUE;
162
0
  ULONG_PTR* keys = nullptr;
163
164
0
  ListDictionary_Lock(rdpdr->devman->devices);
165
0
  const size_t count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
166
0
  for (size_t x = 0; x < count; x++)
167
0
  {
168
0
    void* element = ListDictionary_GetItemValue(rdpdr->devman->devices, (void*)keys[x]);
169
0
    if (!fkt(keys[x], element, data))
170
0
    {
171
0
      rc = FALSE;
172
0
      if (abortOnFail)
173
0
        break;
174
0
    }
175
0
  }
176
0
  free(keys);
177
0
  ListDictionary_Unlock(rdpdr->devman->devices);
178
0
  return rc;
179
0
}
180
181
/**
182
 * Function description
183
 *
184
 * @return 0 on success, otherwise a Win32 error code
185
 */
186
static UINT rdpdr_try_send_device_list_announce_request(rdpdrPlugin* rdpdr);
187
188
static BOOL rdpdr_load_drive(rdpdrPlugin* rdpdr, const char* name, const char* path, BOOL automount)
189
0
{
190
0
  UINT rc = ERROR_INTERNAL_ERROR;
191
0
  union
192
0
  {
193
0
    RDPDR_DRIVE* drive;
194
0
    RDPDR_DEVICE* device;
195
0
  } drive;
196
0
  const char* args[] = { name, path, automount ? nullptr : name };
197
198
0
  drive.device = freerdp_device_new(RDPDR_DTYP_FILESYSTEM, ARRAYSIZE(args), args);
199
0
  if (!drive.device)
200
0
    goto fail;
201
202
0
  WINPR_ASSERT(rdpdr->context.RdpdrRegisterDevice);
203
0
  rc = rdpdr->context.RdpdrRegisterDevice(&rdpdr->context, drive.device, &drive.device->Id);
204
0
  if (rc != CHANNEL_RC_OK)
205
0
    goto fail;
206
207
0
fail:
208
0
  freerdp_device_free(drive.device);
209
0
  return rc == CHANNEL_RC_OK;
210
0
}
211
212
/**
213
 * Function description
214
 *
215
 * @return 0 on success, otherwise a Win32 error code
216
 */
217
static UINT rdpdr_send_device_list_remove_request(rdpdrPlugin* rdpdr, UINT32 count,
218
                                                  const UINT32 ids[])
219
0
{
220
0
  wStream* s = nullptr;
221
222
0
  WINPR_ASSERT(rdpdr);
223
0
  WINPR_ASSERT(ids || (count == 0));
224
225
0
  if (count == 0)
226
0
    return CHANNEL_RC_OK;
227
228
0
  if (!rdpdr_check_extended_pdu_flag(rdpdr, RDPDR_DEVICE_REMOVE_PDUS))
229
0
    return CHANNEL_RC_OK;
230
231
0
  s = StreamPool_Take(rdpdr->pool, count * sizeof(UINT32) + 8);
232
233
0
  if (!s)
234
0
  {
235
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
236
0
    return CHANNEL_RC_NO_MEMORY;
237
0
  }
238
239
0
  Stream_Write_UINT16(s, RDPDR_CTYP_CORE);
240
0
  Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_REMOVE);
241
0
  Stream_Write_UINT32(s, count);
242
243
0
  for (UINT32 i = 0; i < count; i++)
244
0
    Stream_Write_UINT32(s, ids[i]);
245
246
0
  Stream_SealLength(s);
247
0
  return rdpdr_send(rdpdr, s);
248
0
}
249
250
#if defined(_UWP) || defined(__IOS__)
251
252
static UINT handle_hotplug(WINPR_ATTR_UNUSED RdpdrClientContext* context,
253
                           WINPR_ATTR_UNUSED RdpdrHotplugEventType type)
254
{
255
  return ERROR_CALL_NOT_IMPLEMENTED;
256
}
257
258
static void first_hotplug(WINPR_ATTR_UNUSED rdpdrPlugin* rdpdr)
259
{
260
}
261
262
static DWORD WINAPI drive_hotplug_thread_func(WINPR_ATTR_UNUSED LPVOID arg)
263
{
264
  return CHANNEL_RC_OK;
265
}
266
267
static UINT drive_hotplug_thread_terminate(WINPR_ATTR_UNUSED rdpdrPlugin* rdpdr)
268
{
269
  return CHANNEL_RC_OK;
270
}
271
272
#elif defined(_WIN32)
273
274
static UINT handle_hotplug(WINPR_ATTR_UNUSED RdpdrClientContext* context,
275
                           WINPR_ATTR_UNUSED RdpdrHotplugEventType type)
276
{
277
  return CHANNEL_RC_OK;
278
}
279
280
static BOOL check_path(const char* path)
281
{
282
  UINT type = GetDriveTypeA(path);
283
284
  if (!(type == DRIVE_FIXED || type == DRIVE_REMOVABLE || type == DRIVE_CDROM ||
285
        type == DRIVE_REMOTE))
286
    return FALSE;
287
288
  return GetVolumeInformationA(path, nullptr, 0, nullptr, nullptr, nullptr, nullptr, 0);
289
}
290
291
static void first_hotplug(rdpdrPlugin* rdpdr)
292
{
293
  DWORD unitmask = GetLogicalDrives();
294
295
  for (size_t i = 0; i < 26; i++)
296
  {
297
    if (unitmask & 0x01)
298
    {
299
      char drive_path[] = { 'c', ':', '\\', '\0' };
300
      char drive_name[] = { 'c', '\0' };
301
      drive_path[0] = 'A' + (char)i;
302
      drive_name[0] = 'A' + (char)i;
303
304
      if (check_path(drive_path))
305
      {
306
        rdpdr_load_drive(rdpdr, drive_name, drive_path, TRUE);
307
      }
308
    }
309
310
    unitmask = unitmask >> 1;
311
  }
312
}
313
314
static LRESULT CALLBACK hotplug_proc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
315
{
316
  rdpdrPlugin* rdpdr;
317
  PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
318
  UINT error;
319
  rdpdr = (rdpdrPlugin*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
320
321
  switch (Msg)
322
  {
323
    case WM_DEVICECHANGE:
324
      switch (wParam)
325
      {
326
        case DBT_DEVICEARRIVAL:
327
          if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
328
          {
329
            PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
330
            DWORD unitmask = lpdbv->dbcv_unitmask;
331
332
            for (int i = 0; i < 26; i++)
333
            {
334
              if (unitmask & 0x01)
335
              {
336
                char drive_path[] = { 'c', ':', '/', '\0' };
337
                char drive_name[] = { 'c', '\0' };
338
                drive_path[0] = 'A' + (char)i;
339
                drive_name[0] = 'A' + (char)i;
340
341
                if (check_path(drive_path))
342
                {
343
                  rdpdr_load_drive(rdpdr, drive_name, drive_path, TRUE);
344
                }
345
              }
346
347
              unitmask = unitmask >> 1;
348
            }
349
          }
350
351
          break;
352
353
        case DBT_DEVICEREMOVECOMPLETE:
354
          if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
355
          {
356
            PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
357
            DWORD unitmask = lpdbv->dbcv_unitmask;
358
            char drive_name_upper, drive_name_lower;
359
            ULONG_PTR* keys = nullptr;
360
            DEVICE_DRIVE_EXT* device_ext;
361
362
            for (int i = 0; i < 26; i++)
363
            {
364
              if (unitmask & 0x01)
365
              {
366
                drive_name_upper = 'A' + i;
367
                drive_name_lower = 'a' + i;
368
                const size_t count =
369
                    ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
370
371
                for (size_t j = 0; j < count; j++)
372
                {
373
                  device_ext = (DEVICE_DRIVE_EXT*)ListDictionary_GetItemValue(
374
                      rdpdr->devman->devices, (void*)keys[j]);
375
376
                  if (device_ext->device.type != RDPDR_DTYP_FILESYSTEM)
377
                    continue;
378
379
                  if (device_ext->path[0] == drive_name_upper ||
380
                      device_ext->path[0] == drive_name_lower)
381
                  {
382
                    if (device_ext->automount)
383
                    {
384
                      const uint32_t ids[] = { (uint32_t)keys[j] };
385
                      WINPR_ASSERT(rdpdr->context.RdpdrUnregisterDevice);
386
                      error = rdpdr->context.RdpdrUnregisterDevice(
387
                          &rdpdr->context, ARRAYSIZE(ids), ids);
388
                      if (error)
389
                      {
390
                        // don't end on error, just report ?
391
                        WLog_Print(
392
                            rdpdr->log, WLOG_ERROR,
393
                            "rdpdr_send_device_list_remove_request failed "
394
                            "with error %" PRIu32 "!",
395
                            error);
396
                      }
397
398
                      break;
399
                    }
400
                  }
401
                }
402
403
                free(keys);
404
              }
405
406
              unitmask = unitmask >> 1;
407
            }
408
          }
409
410
          break;
411
412
        default:
413
          break;
414
      }
415
416
      break;
417
418
    default:
419
      return DefWindowProc(hWnd, Msg, wParam, lParam);
420
  }
421
422
  return DefWindowProc(hWnd, Msg, wParam, lParam);
423
}
424
425
static DWORD WINAPI drive_hotplug_thread_func(LPVOID arg)
426
{
427
  rdpdrPlugin* rdpdr;
428
  WNDCLASSEX wnd_cls;
429
  HWND hwnd;
430
  MSG msg;
431
  BOOL bRet;
432
  DEV_BROADCAST_HANDLE NotificationFilter;
433
  HDEVNOTIFY hDevNotify;
434
  rdpdr = (rdpdrPlugin*)arg;
435
  /* init windows class */
436
  wnd_cls.cbSize = sizeof(WNDCLASSEX);
437
  wnd_cls.style = CS_HREDRAW | CS_VREDRAW;
438
  wnd_cls.lpfnWndProc = hotplug_proc;
439
  wnd_cls.cbClsExtra = 0;
440
  wnd_cls.cbWndExtra = 0;
441
  wnd_cls.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
442
  wnd_cls.hCursor = nullptr;
443
  wnd_cls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
444
  wnd_cls.lpszMenuName = nullptr;
445
  wnd_cls.lpszClassName = L"DRIVE_HOTPLUG";
446
  wnd_cls.hInstance = nullptr;
447
  wnd_cls.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
448
  RegisterClassEx(&wnd_cls);
449
  /* create window */
450
  hwnd = CreateWindowEx(0, L"DRIVE_HOTPLUG", nullptr, 0, 0, 0, 0, 0, nullptr, nullptr, nullptr,
451
                        nullptr);
452
  SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)rdpdr);
453
  rdpdr->hotplug_wnd = hwnd;
454
  /* register device interface to hwnd */
455
  NotificationFilter.dbch_size = sizeof(DEV_BROADCAST_HANDLE);
456
  NotificationFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;
457
  hDevNotify = RegisterDeviceNotification(hwnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
458
459
  /* message loop */
460
  while ((bRet = GetMessage(&msg, 0, 0, 0)) != 0)
461
  {
462
    if (bRet == -1)
463
    {
464
      break;
465
    }
466
    else
467
    {
468
      TranslateMessage(&msg);
469
      DispatchMessage(&msg);
470
    }
471
  }
472
473
  UnregisterDeviceNotification(hDevNotify);
474
  return CHANNEL_RC_OK;
475
}
476
477
/**
478
 * Function description
479
 *
480
 * @return 0 on success, otherwise a Win32 error code
481
 */
482
static UINT drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
483
{
484
  UINT error = CHANNEL_RC_OK;
485
486
  if (rdpdr->hotplug_wnd && !PostMessage(rdpdr->hotplug_wnd, WM_QUIT, 0, 0))
487
  {
488
    error = GetLastError();
489
    WLog_Print(rdpdr->log, WLOG_ERROR, "PostMessage failed with error %" PRIu32 "", error);
490
  }
491
492
  return error;
493
}
494
495
#elif defined(__MACOSX__)
496
497
#define MAX_USB_DEVICES 100
498
499
typedef struct
500
{
501
  char* path;
502
  BOOL to_add;
503
} hotplug_dev;
504
505
/**
506
 * Function description
507
 *
508
 * @return 0 on success, otherwise a Win32 error code
509
 */
510
static UINT handle_hotplug(WINPR_ATTR_UNUSED RdpdrClientContext* context,
511
                           WINPR_ATTR_UNUSED RdpdrHotplugEventType type)
512
{
513
  WINPR_ASSERT(context);
514
  rdpdrPlugin* rdpdr = context->handle;
515
516
  struct dirent* pDirent = nullptr;
517
  char fullpath[PATH_MAX] = WINPR_C_ARRAY_INIT;
518
  char* szdir = (char*)"/Volumes";
519
  struct stat buf = WINPR_C_ARRAY_INIT;
520
  hotplug_dev dev_array[MAX_USB_DEVICES] = WINPR_C_ARRAY_INIT;
521
  int count = 0;
522
  DEVICE_DRIVE_EXT* device_ext = nullptr;
523
  ULONG_PTR* keys = nullptr;
524
  int size = 0;
525
  UINT error = ERROR_INTERNAL_ERROR;
526
527
  DIR* pDir = opendir(szdir);
528
529
  if (pDir == nullptr)
530
  {
531
    printf("Cannot open directory\n");
532
    return ERROR_OPEN_FAILED;
533
  }
534
535
  while ((pDirent = readdir(pDir)) != nullptr)
536
  {
537
    if (pDirent->d_name[0] != '.')
538
    {
539
      (void)sprintf_s(fullpath, ARRAYSIZE(fullpath), "%s/%s", szdir, pDirent->d_name);
540
      if (stat(fullpath, &buf) != 0)
541
        continue;
542
543
      if (S_ISDIR(buf.st_mode))
544
      {
545
        dev_array[size].path = _strdup(fullpath);
546
547
        if (!dev_array[size].path)
548
        {
549
          closedir(pDir);
550
          error = CHANNEL_RC_NO_MEMORY;
551
          goto cleanup;
552
        }
553
554
        dev_array[size++].to_add = TRUE;
555
      }
556
    }
557
  }
558
559
  closedir(pDir);
560
  /* delete removed devices */
561
  count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
562
563
  for (size_t j = 0; j < count; j++)
564
  {
565
    char* path = nullptr;
566
    BOOL dev_found = FALSE;
567
    device_ext =
568
        (DEVICE_DRIVE_EXT*)ListDictionary_GetItemValue(rdpdr->devman->devices, (void*)keys[j]);
569
570
    if (!device_ext || !device_ext->automount)
571
      continue;
572
573
    if (device_ext->device.type != RDPDR_DTYP_FILESYSTEM)
574
      continue;
575
576
    if (device_ext->path == nullptr)
577
      continue;
578
579
    path = ConvertWCharToUtf8Alloc(device_ext->path, nullptr);
580
    if (!path)
581
      continue;
582
583
    /* not pluggable device */
584
    if (strstr(path, "/Volumes/") == nullptr)
585
    {
586
      free(path);
587
      continue;
588
    }
589
590
    for (size_t i = 0; i < size; i++)
591
    {
592
      if (strstr(path, dev_array[i].path) != nullptr)
593
      {
594
        dev_found = TRUE;
595
        dev_array[i].to_add = FALSE;
596
        break;
597
      }
598
    }
599
600
    free(path);
601
602
    if (!dev_found)
603
    {
604
      const uint32_t ids[] = { (uint32_t)keys[j] };
605
      WINPR_ASSERT(rdpdr->context.RdpdrUnregisterDevice);
606
      error = rdpdr->context.RdpdrUnregisterDevice(&rdpdr->context, ARRAYSIZE(ids), ids);
607
      if (error)
608
      {
609
        WLog_Print(rdpdr->log, WLOG_ERROR,
610
                   "rdpdr_send_device_list_remove_request failed with error %" PRIu32 "!",
611
                   error);
612
        goto cleanup;
613
      }
614
    }
615
  }
616
617
  /* add new devices */
618
  for (size_t i = 0; i < size; i++)
619
  {
620
    const hotplug_dev* dev = &dev_array[i];
621
    if (dev->to_add)
622
    {
623
      const char* path = dev->path;
624
      const char* name = strrchr(path, '/') + 1;
625
      error = rdpdr_load_drive(rdpdr, name, path, TRUE);
626
      if (error)
627
        goto cleanup;
628
    }
629
  }
630
631
cleanup:
632
  free(keys);
633
634
  for (size_t i = 0; i < size; i++)
635
    free(dev_array[i].path);
636
637
  return error;
638
}
639
640
static void drive_hotplug_fsevent_callback(ConstFSEventStreamRef streamRef,
641
                                           void* clientCallBackInfo, size_t numEvents,
642
                                           void* eventPaths,
643
                                           const FSEventStreamEventFlags eventFlags[],
644
                                           const FSEventStreamEventId eventIds[])
645
{
646
  rdpdrPlugin* rdpdr;
647
  UINT error;
648
  char** paths = (char**)eventPaths;
649
  rdpdr = (rdpdrPlugin*)clientCallBackInfo;
650
651
  for (size_t i = 0; i < numEvents; i++)
652
  {
653
    if (strcmp(paths[i], "/Volumes/") == 0)
654
    {
655
      UINT error = ERROR_CALL_NOT_IMPLEMENTED;
656
      if (rdpdr->context.RdpdrHotplugDevice)
657
        error = rdpdr->context.RdpdrHotplugDevice(&rdpdr->context,
658
                                                  RDPDR_HOTPLUG_CHECK_FOR_CHANGES);
659
      switch (error)
660
      {
661
        case ERROR_DISK_CHANGE:
662
        case CHANNEL_RC_OK:
663
          break;
664
        case ERROR_CALL_NOT_IMPLEMENTED:
665
          break;
666
        default:
667
          WLog_Print(rdpdr->log, WLOG_ERROR,
668
                     "handle_hotplug failed with error %" PRIu32 "!", error);
669
          break;
670
      }
671
    }
672
  }
673
}
674
675
static void first_hotplug(rdpdrPlugin* rdpdr)
676
{
677
  WINPR_ASSERT(rdpdr);
678
  UINT error = ERROR_CALL_NOT_IMPLEMENTED;
679
  if (rdpdr->context.RdpdrHotplugDevice)
680
    error = rdpdr->context.RdpdrHotplugDevice(&rdpdr->context, RDPDR_HOTPLUG_FIRST_CHECK);
681
682
  switch (error)
683
  {
684
    case ERROR_DISK_CHANGE:
685
    case CHANNEL_RC_OK:
686
    case ERROR_CALL_NOT_IMPLEMENTED:
687
      break;
688
    default:
689
      WLog_Print(rdpdr->log, WLOG_ERROR, "handle_hotplug failed with error %" PRIu32 "!",
690
                 error);
691
      break;
692
  }
693
}
694
695
static DWORD WINAPI drive_hotplug_thread_func(LPVOID arg)
696
{
697
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)arg;
698
  WINPR_ASSERT(rdpdr);
699
  WINPR_ASSERT(rdpdr->stopEvent);
700
701
  CFStringRef path = CFSTR("/Volumes/");
702
  CFArrayRef pathsToWatch = CFArrayCreate(kCFAllocatorMalloc, (const void**)&path, 1, nullptr);
703
  FSEventStreamContext ctx = {
704
    .copyDescription = nullptr, .info = arg, .release = nullptr, .retain = nullptr, .version = 0
705
  };
706
  FSEventStreamRef fsev =
707
      FSEventStreamCreate(kCFAllocatorMalloc, drive_hotplug_fsevent_callback, &ctx, pathsToWatch,
708
                          kFSEventStreamEventIdSinceNow, 1, kFSEventStreamCreateFlagNone);
709
710
  dispatch_queue_t queue = dispatch_queue_create(TAG, nullptr);
711
  FSEventStreamSetDispatchQueue(fsev, queue);
712
  FSEventStreamStart(fsev);
713
  WLog_Print(rdpdr->log, WLOG_DEBUG, "Started hotplug watcher");
714
  HANDLE handles[] = { rdpdr->stopEvent, freerdp_abort_event(rdpdr->rdpcontext) };
715
  const DWORD status = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
716
  WLog_Print(rdpdr->log, WLOG_DEBUG, "Stopped hotplug watcher");
717
  FSEventStreamStop(fsev);
718
  FSEventStreamRelease(fsev);
719
  dispatch_release(queue);
720
721
  UINT error = CHANNEL_RC_OK;
722
  if (status > WAIT_OBJECT_0 + ARRAYSIZE(handles))
723
    error = ERROR_INTERNAL_ERROR;
724
  ExitThread(error);
725
  return error;
726
}
727
728
#else
729
730
static const char* automountLocations[] = { "/run/user/%lu/gvfs", "/run/media/%s", "/media/%s",
731
                                          "/media", "/mnt" };
732
733
static BOOL isAutomountLocation(const char* path)
734
0
{
735
0
  const size_t nrLocations = sizeof(automountLocations) / sizeof(automountLocations[0]);
736
0
  char buffer[MAX_PATH] = WINPR_C_ARRAY_INIT;
737
0
  uid_t uid = getuid();
738
0
  char uname[MAX_PATH] = WINPR_C_ARRAY_INIT;
739
0
  ULONG size = sizeof(uname) - 1;
740
741
0
  if (!GetUserNameExA(NameSamCompatible, uname, &size))
742
0
    return FALSE;
743
744
0
  if (!path)
745
0
    return FALSE;
746
747
0
  for (size_t x = 0; x < nrLocations; x++)
748
0
  {
749
0
    const char* location = automountLocations[x];
750
0
    size_t length = 0;
751
752
0
    WINPR_PRAGMA_DIAG_PUSH
753
0
    WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
754
0
    if (strstr(location, "%lu"))
755
0
      (void)snprintf(buffer, sizeof(buffer), location, (unsigned long)uid);
756
0
    else if (strstr(location, "%s"))
757
0
      (void)snprintf(buffer, sizeof(buffer), location, uname);
758
0
    else
759
0
      (void)snprintf(buffer, sizeof(buffer), "%s", location);
760
0
    WINPR_PRAGMA_DIAG_POP
761
762
0
    length = strnlen(buffer, sizeof(buffer));
763
764
0
    if (strncmp(buffer, path, length) == 0)
765
0
    {
766
0
      const char* rest = &path[length];
767
768
      /* Only consider mount locations with max depth of 1 below the
769
       * base path or the base path itself. */
770
0
      if (*rest == '\0')
771
0
        return TRUE;
772
0
      else if (*rest == '/')
773
0
      {
774
0
        const char* token = strstr(&rest[1], "/");
775
776
0
        if (!token || (token[1] == '\0'))
777
0
          return TRUE;
778
0
      }
779
0
    }
780
0
  }
781
782
0
  return FALSE;
783
0
}
784
785
0
#define MAX_USB_DEVICES 100
786
787
typedef struct
788
{
789
  char* path;
790
  BOOL to_add;
791
} hotplug_dev;
792
793
static void handle_mountpoint(hotplug_dev* dev_array, size_t* size, const char* mountpoint)
794
0
{
795
0
  if (!mountpoint)
796
0
    return;
797
  /* copy hotpluged device mount point to the dev_array */
798
0
  if (isAutomountLocation(mountpoint) && (*size < MAX_USB_DEVICES))
799
0
  {
800
0
    dev_array[*size].path = _strdup(mountpoint);
801
0
    dev_array[*size].to_add = TRUE;
802
0
    (*size)++;
803
0
  }
804
0
}
805
806
#ifdef __sun
807
#include <sys/mnttab.h>
808
static UINT handle_platform_mounts_sun(wLog* log, hotplug_dev* dev_array, size_t* size)
809
{
810
  FILE* f;
811
  struct mnttab ent;
812
  f = winpr_fopen("/etc/mnttab", "r");
813
  if (f == nullptr)
814
  {
815
    WLog_Print(log, WLOG_ERROR, "fopen failed!");
816
    return ERROR_OPEN_FAILED;
817
  }
818
  while (getmntent(f, &ent) == 0)
819
  {
820
    handle_mountpoint(dev_array, size, ent.mnt_mountp);
821
  }
822
  fclose(f);
823
  return ERROR_SUCCESS;
824
}
825
#endif
826
827
#if defined(__FreeBSD__) || defined(__OpenBSD__)
828
#include <sys/mount.h>
829
static UINT handle_platform_mounts_bsd(wLog* log, hotplug_dev* dev_array, size_t* size)
830
{
831
  int mntsize;
832
  struct statfs* mntbuf = nullptr;
833
834
  mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
835
  if (!mntsize)
836
  {
837
    /* TODO: handle 'errno' */
838
    WLog_Print(log, WLOG_ERROR, "getmntinfo failed!");
839
    return ERROR_OPEN_FAILED;
840
  }
841
  for (size_t idx = 0; idx < (size_t)mntsize; idx++)
842
  {
843
    handle_mountpoint(dev_array, size, mntbuf[idx].f_mntonname);
844
  }
845
  return ERROR_SUCCESS;
846
}
847
#endif
848
849
#if defined(__LINUX__) || defined(__linux__)
850
#include <mntent.h>
851
static struct mntent* getmntent_x(FILE* f, struct mntent* buffer, char* pathbuffer,
852
                                  size_t pathbuffersize)
853
0
{
854
0
#if defined(FREERDP_HAVE_GETMNTENT_R)
855
0
  WINPR_ASSERT(pathbuffersize <= INT32_MAX);
856
0
  return getmntent_r(f, buffer, pathbuffer, (int)pathbuffersize);
857
#else
858
  (void)buffer;
859
  (void)pathbuffer;
860
  (void)pathbuffersize;
861
  return getmntent(f);
862
#endif
863
0
}
864
865
static UINT handle_platform_mounts_linux(wLog* log, hotplug_dev* dev_array, size_t* size)
866
0
{
867
0
  FILE* f = nullptr;
868
0
  struct mntent mnt = WINPR_C_ARRAY_INIT;
869
0
  char pathbuffer[PATH_MAX] = WINPR_C_ARRAY_INIT;
870
0
  struct mntent* ent = nullptr;
871
0
  f = winpr_fopen("/proc/mounts", "r");
872
0
  if (f == nullptr)
873
0
  {
874
0
    WLog_Print(log, WLOG_ERROR, "fopen failed!");
875
0
    return ERROR_OPEN_FAILED;
876
0
  }
877
0
  while ((ent = getmntent_x(f, &mnt, pathbuffer, sizeof(pathbuffer))) != nullptr)
878
0
  {
879
0
    handle_mountpoint(dev_array, size, ent->mnt_dir);
880
0
  }
881
0
  (void)fclose(f);
882
0
  return ERROR_SUCCESS;
883
0
}
884
#endif
885
886
static UINT handle_platform_mounts(wLog* log, hotplug_dev* dev_array, size_t* size)
887
0
{
888
#ifdef __sun
889
  return handle_platform_mounts_sun(log, dev_array, size);
890
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
891
  return handle_platform_mounts_bsd(log, dev_array, size);
892
#elif defined(__LINUX__) || defined(__linux__)
893
  return handle_platform_mounts_linux(log, dev_array, size);
894
0
#endif
895
0
  return ERROR_CALL_NOT_IMPLEMENTED;
896
0
}
897
898
static BOOL device_not_plugged(ULONG_PTR key, void* element, void* data)
899
0
{
900
0
  const WCHAR* path = (const WCHAR*)data;
901
0
  DEVICE_DRIVE_EXT* device_ext = (DEVICE_DRIVE_EXT*)element;
902
903
0
  WINPR_UNUSED(key);
904
0
  WINPR_ASSERT(path);
905
906
0
  if (!device_ext || (device_ext->device.type != RDPDR_DTYP_FILESYSTEM) || !device_ext->path)
907
0
    return TRUE;
908
0
  if (_wcscmp(device_ext->path, path) != 0)
909
0
    return TRUE;
910
0
  return FALSE;
911
0
}
912
913
static BOOL device_already_plugged(rdpdrPlugin* rdpdr, const hotplug_dev* device)
914
0
{
915
0
  BOOL rc = FALSE;
916
0
  WCHAR* path = nullptr;
917
918
0
  if (!rdpdr || !device)
919
0
    return TRUE;
920
0
  if (!device->to_add)
921
0
    return TRUE;
922
923
0
  WINPR_ASSERT(rdpdr->devman);
924
0
  WINPR_ASSERT(device->path);
925
926
0
  path = ConvertUtf8ToWCharAlloc(device->path, nullptr);
927
0
  if (!path)
928
0
    return TRUE;
929
930
0
  rc = device_foreach(rdpdr, TRUE, device_not_plugged, path);
931
0
  free(path);
932
0
  return !rc;
933
0
}
934
935
struct hotplug_delete_arg
936
{
937
  hotplug_dev* dev_array;
938
  size_t dev_array_size;
939
  rdpdrPlugin* rdpdr;
940
};
941
942
static BOOL hotplug_delete_foreach(ULONG_PTR key, void* element, void* data)
943
0
{
944
0
  char* path = nullptr;
945
0
  BOOL dev_found = FALSE;
946
0
  struct hotplug_delete_arg* arg = (struct hotplug_delete_arg*)data;
947
0
  DEVICE_DRIVE_EXT* device_ext = (DEVICE_DRIVE_EXT*)element;
948
949
0
  WINPR_ASSERT(arg);
950
0
  WINPR_ASSERT(arg->rdpdr);
951
0
  WINPR_ASSERT(arg->dev_array || (arg->dev_array_size == 0));
952
0
  WINPR_ASSERT(key <= UINT32_MAX);
953
954
0
  if (!device_ext || (device_ext->device.type != RDPDR_DTYP_FILESYSTEM) || !device_ext->path ||
955
0
      !device_ext->automount)
956
0
    return TRUE;
957
958
0
  WINPR_ASSERT(device_ext->path);
959
0
  path = ConvertWCharToUtf8Alloc(device_ext->path, nullptr);
960
0
  if (!path)
961
0
    return FALSE;
962
963
  /* not pluggable device */
964
0
  if (isAutomountLocation(path))
965
0
  {
966
0
    for (size_t i = 0; i < arg->dev_array_size; i++)
967
0
    {
968
0
      hotplug_dev* cur = &arg->dev_array[i];
969
0
      if (cur->path && strstr(path, cur->path) != nullptr)
970
0
      {
971
0
        dev_found = TRUE;
972
0
        cur->to_add = FALSE;
973
0
        break;
974
0
      }
975
0
    }
976
0
  }
977
978
0
  free(path);
979
980
0
  if (!dev_found)
981
0
  {
982
0
    const UINT32 ids[1] = { (UINT32)key };
983
0
    WINPR_ASSERT(arg->rdpdr->context.RdpdrUnregisterDevice);
984
0
    const UINT error =
985
0
        arg->rdpdr->context.RdpdrUnregisterDevice(&arg->rdpdr->context, ARRAYSIZE(ids), ids);
986
987
0
    if (error)
988
0
    {
989
0
      WLog_Print(arg->rdpdr->log, WLOG_ERROR,
990
0
                 "rdpdr_send_device_list_remove_request failed with error %" PRIu32 "!",
991
0
                 error);
992
0
      return FALSE;
993
0
    }
994
0
  }
995
996
0
  return TRUE;
997
0
}
998
999
static UINT handle_hotplug(RdpdrClientContext* context,
1000
                           WINPR_ATTR_UNUSED RdpdrHotplugEventType type)
1001
0
{
1002
0
  WINPR_ASSERT(context);
1003
0
  rdpdrPlugin* rdpdr = context->handle;
1004
1005
0
  hotplug_dev dev_array[MAX_USB_DEVICES] = WINPR_C_ARRAY_INIT;
1006
0
  size_t size = 0;
1007
0
  UINT error = ERROR_SUCCESS;
1008
0
  struct hotplug_delete_arg arg = { dev_array, ARRAYSIZE(dev_array), rdpdr };
1009
1010
0
  WINPR_ASSERT(rdpdr);
1011
0
  WINPR_ASSERT(rdpdr->devman);
1012
1013
0
  error = handle_platform_mounts(rdpdr->log, dev_array, &size);
1014
1015
  /* delete removed devices */
1016
0
  /* Ignore result */ device_foreach(rdpdr, FALSE, hotplug_delete_foreach, &arg);
1017
1018
  /* add new devices */
1019
0
  for (size_t i = 0; i < size; i++)
1020
0
  {
1021
0
    hotplug_dev* cur = &dev_array[i];
1022
0
    if (!device_already_plugged(rdpdr, cur))
1023
0
    {
1024
0
      const char* path = cur->path;
1025
0
      const char* name = strrchr(path, '/') + 1;
1026
1027
0
      rdpdr_load_drive(rdpdr, name, path, TRUE);
1028
0
      error = ERROR_DISK_CHANGE;
1029
0
    }
1030
0
  }
1031
1032
0
  for (size_t i = 0; i < size; i++)
1033
0
    free(dev_array[i].path);
1034
1035
0
  return error;
1036
0
}
1037
1038
static void first_hotplug(rdpdrPlugin* rdpdr)
1039
0
{
1040
0
  UINT error = ERROR_CALL_NOT_IMPLEMENTED;
1041
1042
0
  WINPR_ASSERT(rdpdr);
1043
0
  if (rdpdr->context.RdpdrHotplugDevice)
1044
0
    error = rdpdr->context.RdpdrHotplugDevice(&rdpdr->context, RDPDR_HOTPLUG_FIRST_CHECK);
1045
1046
0
  switch (error)
1047
0
  {
1048
0
    case ERROR_DISK_CHANGE:
1049
0
    case CHANNEL_RC_OK:
1050
0
    case ERROR_OPEN_FAILED:
1051
0
    case ERROR_CALL_NOT_IMPLEMENTED:
1052
0
      break;
1053
0
    default:
1054
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "handle_hotplug failed with error %" PRIu32 "!",
1055
0
                 error);
1056
0
      break;
1057
0
  }
1058
0
}
1059
1060
static DWORD WINAPI drive_hotplug_thread_func(LPVOID arg)
1061
0
{
1062
0
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)arg;
1063
1064
0
  WINPR_ASSERT(rdpdr);
1065
0
  WINPR_ASSERT(rdpdr->stopEvent);
1066
1067
0
  while (WaitForSingleObject(rdpdr->stopEvent, 1000) == WAIT_TIMEOUT)
1068
0
  {
1069
0
    UINT error = ERROR_CALL_NOT_IMPLEMENTED;
1070
0
    if (rdpdr->context.RdpdrHotplugDevice)
1071
0
      error =
1072
0
          rdpdr->context.RdpdrHotplugDevice(&rdpdr->context, RDPDR_HOTPLUG_CHECK_FOR_CHANGES);
1073
0
    switch (error)
1074
0
    {
1075
0
      case ERROR_DISK_CHANGE:
1076
0
        break;
1077
0
      case CHANNEL_RC_OK:
1078
0
      case ERROR_OPEN_FAILED:
1079
0
      case ERROR_CALL_NOT_IMPLEMENTED:
1080
0
        break;
1081
0
      default:
1082
0
        WLog_Print(rdpdr->log, WLOG_ERROR, "handle_hotplug failed with error %" PRIu32 "!",
1083
0
                   error);
1084
0
        goto out;
1085
0
    }
1086
0
  }
1087
1088
0
out:
1089
0
{
1090
0
  const UINT error = GetLastError();
1091
0
  if (error && rdpdr->rdpcontext)
1092
0
    setChannelError(rdpdr->rdpcontext, error, "reported an error");
1093
1094
0
  ExitThread(error);
1095
0
  return error;
1096
0
}
1097
0
}
1098
1099
#endif
1100
1101
#if !defined(_WIN32) && !defined(__IOS__)
1102
/**
1103
 * Function description
1104
 *
1105
 * @return 0 on success, otherwise a Win32 error code
1106
 */
1107
static UINT drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
1108
0
{
1109
0
  UINT error = 0;
1110
1111
0
  WINPR_ASSERT(rdpdr);
1112
1113
0
  if (rdpdr->hotplugThread)
1114
0
  {
1115
0
#if !defined(_WIN32)
1116
0
    if (rdpdr->stopEvent)
1117
0
      (void)SetEvent(rdpdr->stopEvent);
1118
0
#endif
1119
1120
0
    if (WaitForSingleObject(rdpdr->hotplugThread, INFINITE) == WAIT_FAILED)
1121
0
    {
1122
0
      error = GetLastError();
1123
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "!",
1124
0
                 error);
1125
0
      return error;
1126
0
    }
1127
1128
0
    (void)CloseHandle(rdpdr->hotplugThread);
1129
0
    rdpdr->hotplugThread = nullptr;
1130
0
  }
1131
1132
0
  return CHANNEL_RC_OK;
1133
0
}
1134
1135
#endif
1136
1137
static UINT rdpdr_add_devices(rdpdrPlugin* rdpdr)
1138
0
{
1139
0
  WINPR_ASSERT(rdpdr);
1140
0
  WINPR_ASSERT(rdpdr->rdpcontext);
1141
1142
0
  rdpSettings* settings = rdpdr->rdpcontext->settings;
1143
0
  WINPR_ASSERT(settings);
1144
1145
0
  for (UINT32 index = 0; index < freerdp_settings_get_uint32(settings, FreeRDP_DeviceCount);
1146
0
       index++)
1147
0
  {
1148
0
    RDPDR_DEVICE* device =
1149
0
        freerdp_settings_get_pointer_array_writable(settings, FreeRDP_DeviceArray, index);
1150
0
    WINPR_ASSERT(device);
1151
1152
0
    if (device->Type == RDPDR_DTYP_FILESYSTEM)
1153
0
    {
1154
0
      const char DynamicDrives[] = "DynamicDrives";
1155
0
      const RDPDR_DRIVE* drive = (const RDPDR_DRIVE*)device;
1156
0
      if (!drive->Path)
1157
0
        continue;
1158
1159
0
      const char wildcard[] = "*";
1160
0
      BOOL hotplugAll = strncmp(drive->Path, wildcard, sizeof(wildcard)) == 0;
1161
0
      BOOL hotplugLater = strncmp(drive->Path, DynamicDrives, sizeof(DynamicDrives)) == 0;
1162
1163
0
      if (hotplugAll || hotplugLater)
1164
0
      {
1165
0
        if (!rdpdr->async)
1166
0
        {
1167
0
          WLog_Print(rdpdr->log, WLOG_WARN,
1168
0
                     "Drive hotplug is not supported in synchronous mode!");
1169
0
          continue;
1170
0
        }
1171
1172
0
        if (hotplugAll)
1173
0
          first_hotplug(rdpdr);
1174
1175
        /* There might be multiple hotplug related device entries.
1176
         * Ensure the thread is only started once
1177
         */
1178
0
        if (!rdpdr->hotplugThread)
1179
0
        {
1180
0
          rdpdr->hotplugThread =
1181
0
              CreateThread(nullptr, 0, drive_hotplug_thread_func, rdpdr, 0, nullptr);
1182
0
          if (!rdpdr->hotplugThread)
1183
0
          {
1184
0
            WLog_Print(rdpdr->log, WLOG_ERROR, "CreateThread failed!");
1185
0
            return ERROR_INTERNAL_ERROR;
1186
0
          }
1187
0
        }
1188
1189
0
        continue;
1190
0
      }
1191
0
    }
1192
1193
0
    const UINT error = devman_load_device_service(rdpdr->devman, device, rdpdr->rdpcontext);
1194
0
    if (error)
1195
0
    {
1196
0
      WLog_Print(rdpdr->log, WLOG_ERROR,
1197
0
                 "devman_load_device_service failed with error %" PRIu32 "!", error);
1198
0
      return error;
1199
0
    }
1200
0
  }
1201
0
  return CHANNEL_RC_OK;
1202
0
}
1203
1204
/**
1205
 * Function description
1206
 *
1207
 * @return 0 on success, otherwise a Win32 error code
1208
 */
1209
static UINT rdpdr_process_connect(rdpdrPlugin* rdpdr)
1210
0
{
1211
0
  WINPR_ASSERT(rdpdr);
1212
1213
0
  rdpdr->devman = devman_new(rdpdr);
1214
1215
0
  if (!rdpdr->devman)
1216
0
  {
1217
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "devman_new failed!");
1218
0
    return CHANNEL_RC_NO_MEMORY;
1219
0
  }
1220
1221
0
  WINPR_ASSERT(rdpdr->rdpcontext);
1222
1223
0
  rdpSettings* settings = rdpdr->rdpcontext->settings;
1224
0
  WINPR_ASSERT(settings);
1225
1226
0
  rdpdr->ignoreInvalidDevices = freerdp_settings_get_bool(settings, FreeRDP_IgnoreInvalidDevices);
1227
1228
0
  const char* name = freerdp_settings_get_string(settings, FreeRDP_ClientHostname);
1229
0
  if (!name)
1230
0
    name = freerdp_settings_get_string(settings, FreeRDP_ComputerName);
1231
0
  if (!name)
1232
0
  {
1233
0
    DWORD size = ARRAYSIZE(rdpdr->computerName);
1234
0
    if (!GetComputerNameExA(ComputerNameNetBIOS, rdpdr->computerName, &size))
1235
0
      return ERROR_INTERNAL_ERROR;
1236
0
  }
1237
0
  else
1238
0
    strncpy(rdpdr->computerName, name, strnlen(name, sizeof(rdpdr->computerName)));
1239
1240
0
  return rdpdr_add_devices(rdpdr);
1241
0
}
1242
1243
static UINT rdpdr_process_server_announce_request(rdpdrPlugin* rdpdr, wStream* s)
1244
0
{
1245
0
  WINPR_ASSERT(rdpdr);
1246
0
  WINPR_ASSERT(s);
1247
1248
0
  if (!Stream_CheckAndLogRequiredLengthWLog(rdpdr->log, s, 8))
1249
0
    return ERROR_INVALID_DATA;
1250
1251
0
  Stream_Read_UINT16(s, rdpdr->serverVersionMajor);
1252
0
  Stream_Read_UINT16(s, rdpdr->serverVersionMinor);
1253
0
  Stream_Read_UINT32(s, rdpdr->clientID);
1254
0
  rdpdr->sequenceId++;
1255
1256
0
  rdpdr->clientVersionMajor = MIN(RDPDR_VERSION_MAJOR, rdpdr->serverVersionMajor);
1257
0
  rdpdr->clientVersionMinor = MIN(RDPDR_VERSION_MINOR_RDP10X, rdpdr->serverVersionMinor);
1258
0
  WLog_Print(rdpdr->log, WLOG_DEBUG,
1259
0
             "[rdpdr] server announces version %" PRIu32 ".%" PRIu32 ", client uses %" PRIu32
1260
0
             ".%" PRIu32,
1261
0
             rdpdr->serverVersionMajor, rdpdr->serverVersionMinor, rdpdr->clientVersionMajor,
1262
0
             rdpdr->clientVersionMinor);
1263
0
  return CHANNEL_RC_OK;
1264
0
}
1265
1266
/**
1267
 * Function description
1268
 *
1269
 * @return 0 on success, otherwise a Win32 error code
1270
 */
1271
static UINT rdpdr_send_client_announce_reply(rdpdrPlugin* rdpdr)
1272
0
{
1273
0
  WINPR_ASSERT(rdpdr);
1274
0
  WINPR_ASSERT(rdpdr->state == RDPDR_CHANNEL_STATE_ANNOUNCE);
1275
0
  if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY))
1276
0
    return ERROR_INVALID_STATE;
1277
1278
0
  wStream* s = StreamPool_Take(rdpdr->pool, 12);
1279
1280
0
  if (!s)
1281
0
  {
1282
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
1283
0
    return CHANNEL_RC_NO_MEMORY;
1284
0
  }
1285
1286
0
  Stream_Write_UINT16(s, RDPDR_CTYP_CORE);             /* Component (2 bytes) */
1287
0
  Stream_Write_UINT16(s, PAKID_CORE_CLIENTID_CONFIRM); /* PacketId (2 bytes) */
1288
0
  Stream_Write_UINT16(s, rdpdr->clientVersionMajor);
1289
0
  Stream_Write_UINT16(s, rdpdr->clientVersionMinor);
1290
0
  Stream_Write_UINT32(s, rdpdr->clientID);
1291
0
  return rdpdr_send(rdpdr, s);
1292
0
}
1293
1294
/**
1295
 * Function description
1296
 *
1297
 * @return 0 on success, otherwise a Win32 error code
1298
 */
1299
static UINT rdpdr_send_client_name_request(rdpdrPlugin* rdpdr)
1300
0
{
1301
0
  wStream* s = nullptr;
1302
0
  WCHAR* computerNameW = nullptr;
1303
0
  size_t computerNameLenW = 0;
1304
1305
0
  WINPR_ASSERT(rdpdr);
1306
0
  WINPR_ASSERT(rdpdr->state == RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY);
1307
0
  if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_NAME_REQUEST))
1308
0
    return ERROR_INVALID_STATE;
1309
1310
0
  const size_t len = strnlen(rdpdr->computerName, sizeof(rdpdr->computerName));
1311
0
  if (len == 0)
1312
0
    return ERROR_INTERNAL_ERROR;
1313
1314
0
  WINPR_ASSERT(rdpdr->computerName);
1315
0
  computerNameW = ConvertUtf8NToWCharAlloc(rdpdr->computerName, len, &computerNameLenW);
1316
0
  computerNameLenW *= sizeof(WCHAR);
1317
1318
0
  if (computerNameLenW > 0)
1319
0
    computerNameLenW += sizeof(WCHAR); // also write '\0'
1320
1321
0
  s = StreamPool_Take(rdpdr->pool, 16U + computerNameLenW);
1322
1323
0
  if (!s)
1324
0
  {
1325
0
    free(computerNameW);
1326
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
1327
0
    return CHANNEL_RC_NO_MEMORY;
1328
0
  }
1329
1330
0
  Stream_Write_UINT16(s, RDPDR_CTYP_CORE);        /* Component (2 bytes) */
1331
0
  Stream_Write_UINT16(s, PAKID_CORE_CLIENT_NAME); /* PacketId (2 bytes) */
1332
0
  Stream_Write_UINT32(s, 1);                      /* unicodeFlag, 0 for ASCII and 1 for Unicode */
1333
0
  Stream_Write_UINT32(s, 0);                      /* codePage, must be set to zero */
1334
0
  Stream_Write_UINT32(s,
1335
0
                      (UINT32)computerNameLenW); /* computerNameLen, including null terminator */
1336
0
  Stream_Write(s, computerNameW, computerNameLenW);
1337
0
  free(computerNameW);
1338
0
  return rdpdr_send(rdpdr, s);
1339
0
}
1340
1341
static UINT rdpdr_process_server_clientid_confirm(rdpdrPlugin* rdpdr, wStream* s)
1342
0
{
1343
0
  UINT16 versionMajor = 0;
1344
0
  UINT16 versionMinor = 0;
1345
0
  UINT32 clientID = 0;
1346
1347
0
  WINPR_ASSERT(rdpdr);
1348
0
  WINPR_ASSERT(s);
1349
1350
0
  if (!Stream_CheckAndLogRequiredLengthWLog(rdpdr->log, s, 8))
1351
0
    return ERROR_INVALID_DATA;
1352
1353
0
  Stream_Read_UINT16(s, versionMajor);
1354
0
  Stream_Read_UINT16(s, versionMinor);
1355
0
  Stream_Read_UINT32(s, clientID);
1356
1357
0
  if (versionMajor != rdpdr->clientVersionMajor || versionMinor != rdpdr->clientVersionMinor)
1358
0
  {
1359
0
    WLog_Print(rdpdr->log, WLOG_WARN,
1360
0
               "[rdpdr] server announced version %" PRIu32 ".%" PRIu32 ", client uses %" PRIu32
1361
0
               ".%" PRIu32 " but clientid confirm requests version %" PRIu32 ".%" PRIu32,
1362
0
               rdpdr->serverVersionMajor, rdpdr->serverVersionMinor, rdpdr->clientVersionMajor,
1363
0
               rdpdr->clientVersionMinor, versionMajor, versionMinor);
1364
0
    rdpdr->clientVersionMajor = versionMajor;
1365
0
    rdpdr->clientVersionMinor = versionMinor;
1366
0
  }
1367
1368
0
  if (clientID != rdpdr->clientID)
1369
0
    rdpdr->clientID = clientID;
1370
1371
0
  return CHANNEL_RC_OK;
1372
0
}
1373
1374
struct device_announce_arg
1375
{
1376
  rdpdrPlugin* rdpdr;
1377
  wStream* s;
1378
  BOOL userLoggedOn;
1379
  UINT32 count;
1380
};
1381
1382
static BOOL device_announce(ULONG_PTR key, void* element, void* data)
1383
0
{
1384
0
  struct device_announce_arg* arg = data;
1385
0
  rdpdrPlugin* rdpdr = nullptr;
1386
0
  DEVICE* device = (DEVICE*)element;
1387
1388
0
  WINPR_UNUSED(key);
1389
1390
0
  WINPR_ASSERT(arg);
1391
0
  WINPR_ASSERT(device);
1392
0
  WINPR_ASSERT(arg->rdpdr);
1393
0
  WINPR_ASSERT(arg->s);
1394
1395
0
  rdpdr = arg->rdpdr;
1396
1397
  /**
1398
   * 1. versionMinor 0x0005 doesn't send PAKID_CORE_USER_LOGGEDON
1399
   *    so all devices should be sent regardless of user_loggedon
1400
   * 2. smartcard devices should be always sent
1401
   * 3. other devices are sent only after user_loggedon
1402
   */
1403
1404
0
  if ((rdpdr->clientVersionMinor == RDPDR_VERSION_MINOR_RDP51) ||
1405
0
      (device->type == RDPDR_DTYP_SMARTCARD) || arg->userLoggedOn)
1406
0
  {
1407
0
    size_t data_len = (device->data == nullptr ? 0 : Stream_GetPosition(device->data));
1408
1409
0
    if (!Stream_EnsureRemainingCapacity(arg->s, 20 + data_len))
1410
0
    {
1411
0
      Stream_Release(arg->s);
1412
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
1413
0
      return FALSE;
1414
0
    }
1415
1416
0
    Stream_Write_UINT32(arg->s, device->type); /* deviceType */
1417
0
    Stream_Write_UINT32(arg->s, device->id);   /* deviceID */
1418
0
    strncpy(Stream_Pointer(arg->s), device->name, 8);
1419
1420
0
    for (size_t i = 0; i < 8; i++)
1421
0
    {
1422
0
      BYTE c = 0;
1423
0
      Stream_Peek_UINT8(arg->s, c);
1424
1425
0
      if (c > 0x7F)
1426
0
        Stream_Write_UINT8(arg->s, '_');
1427
0
      else
1428
0
        Stream_Seek_UINT8(arg->s);
1429
0
    }
1430
1431
0
    WINPR_ASSERT(data_len <= UINT32_MAX);
1432
0
    Stream_Write_UINT32(arg->s, (UINT32)data_len);
1433
1434
0
    if (data_len > 0)
1435
0
      Stream_Write(arg->s, Stream_Buffer(device->data), data_len);
1436
1437
0
    arg->count++;
1438
0
    WLog_Print(rdpdr->log, WLOG_INFO,
1439
0
               "registered [%9s] device #%" PRIu32 ": %5s (type=%2" PRIu32 " id=%2" PRIu32 ")",
1440
0
               rdpdr_device_type_string(device->type), arg->count, device->name, device->type,
1441
0
               device->id);
1442
0
  }
1443
0
  return TRUE;
1444
0
}
1445
1446
static UINT rdpdr_send_device_list_announce_request(rdpdrPlugin* rdpdr, BOOL userLoggedOn)
1447
0
{
1448
0
  size_t pos = 0;
1449
0
  wStream* s = nullptr;
1450
0
  size_t count_pos = 0;
1451
0
  struct device_announce_arg arg = WINPR_C_ARRAY_INIT;
1452
1453
0
  WINPR_ASSERT(rdpdr);
1454
0
  WINPR_ASSERT(rdpdr->devman);
1455
1456
0
  if (userLoggedOn)
1457
0
  {
1458
0
    rdpdr->userLoggedOn = TRUE;
1459
0
  }
1460
1461
0
  s = StreamPool_Take(rdpdr->pool, 256);
1462
1463
0
  if (!s)
1464
0
  {
1465
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
1466
0
    return CHANNEL_RC_NO_MEMORY;
1467
0
  }
1468
1469
0
  Stream_Write_UINT16(s, RDPDR_CTYP_CORE);                /* Component (2 bytes) */
1470
0
  Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_ANNOUNCE); /* PacketId (2 bytes) */
1471
0
  count_pos = Stream_GetPosition(s);
1472
0
  Stream_Seek_UINT32(s); /* deviceCount */
1473
1474
0
  arg.rdpdr = rdpdr;
1475
0
  arg.userLoggedOn = userLoggedOn || rdpdr->userLoggedOn;
1476
0
  arg.s = s;
1477
0
  if (!device_foreach(rdpdr, TRUE, device_announce, &arg))
1478
0
    return ERROR_INVALID_DATA;
1479
1480
0
  if (arg.count == 0)
1481
0
  {
1482
0
    Stream_Release(s);
1483
0
    return CHANNEL_RC_OK;
1484
0
  }
1485
0
  pos = Stream_GetPosition(s);
1486
0
  if (!Stream_SetPosition(s, count_pos))
1487
0
  {
1488
0
    Stream_Release(s);
1489
0
    return ERROR_INVALID_DATA;
1490
0
  }
1491
0
  Stream_Write_UINT32(s, arg.count);
1492
0
  if (!Stream_SetPosition(s, pos))
1493
0
  {
1494
0
    Stream_Release(s);
1495
0
    return ERROR_INVALID_DATA;
1496
0
  }
1497
0
  Stream_SealLength(s);
1498
0
  return rdpdr_send(rdpdr, s);
1499
0
}
1500
1501
UINT rdpdr_try_send_device_list_announce_request(rdpdrPlugin* rdpdr)
1502
0
{
1503
0
  WINPR_ASSERT(rdpdr);
1504
0
  if (rdpdr->state != RDPDR_CHANNEL_STATE_READY)
1505
0
  {
1506
0
    WLog_Print(rdpdr->log, WLOG_DEBUG,
1507
0
               "hotplug event received, but channel [RDPDR] is not ready (state %s), ignoring.",
1508
0
               rdpdr_state_str(rdpdr->state));
1509
0
    return CHANNEL_RC_OK;
1510
0
  }
1511
0
  return rdpdr_send_device_list_announce_request(rdpdr, rdpdr->userLoggedOn);
1512
0
}
1513
1514
static UINT dummy_irp_response(rdpdrPlugin* rdpdr, wStream* s)
1515
0
{
1516
0
  WINPR_ASSERT(rdpdr);
1517
0
  WINPR_ASSERT(s);
1518
1519
0
  wStream* output = StreamPool_Take(rdpdr->pool, 256); // RDPDR_DEVICE_IO_RESPONSE_LENGTH
1520
0
  if (!output)
1521
0
  {
1522
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
1523
0
    return CHANNEL_RC_NO_MEMORY;
1524
0
  }
1525
1526
0
  if (!Stream_SetPosition(s, 4)) /* see "rdpdr_process_receive" */
1527
0
  {
1528
0
    Stream_Release(output);
1529
0
    return ERROR_INVALID_DATA;
1530
0
  }
1531
1532
0
  const uint32_t DeviceId = Stream_Get_UINT32(s);     /* DeviceId (4 bytes) */
1533
0
  const uint32_t FileId = Stream_Get_UINT32(s);       /* FileId (4 bytes) */
1534
0
  const uint32_t CompletionId = Stream_Get_UINT32(s); /* CompletionId (4 bytes) */
1535
1536
0
  WLog_Print(rdpdr->log, WLOG_WARN,
1537
0
             "Dummy response {DeviceId=%" PRIu32 ", FileId=%" PRIu32 ", CompletionId=%" PRIu32
1538
0
             "}",
1539
0
             DeviceId, FileId, CompletionId);
1540
0
  if (!rdpdr_write_iocompletion_header(output, DeviceId, CompletionId, STATUS_UNSUCCESSFUL))
1541
0
    return CHANNEL_RC_NO_MEMORY;
1542
1543
0
  return rdpdr_send(rdpdr, output);
1544
0
}
1545
1546
/**
1547
 * Function description
1548
 *
1549
 * @return 0 on success, otherwise a Win32 error code
1550
 */
1551
static UINT rdpdr_process_irp(rdpdrPlugin* rdpdr, wStream* s)
1552
0
{
1553
0
  UINT error = CHANNEL_RC_OK;
1554
1555
0
  WINPR_ASSERT(rdpdr);
1556
0
  WINPR_ASSERT(s);
1557
1558
0
  IRP* irp = irp_new(rdpdr->devman, rdpdr->pool, s, rdpdr->log, &error);
1559
1560
0
  if (!irp)
1561
0
  {
1562
0
    if ((error == CHANNEL_RC_OK) ||
1563
0
        (error == ERROR_DEV_NOT_EXIST && rdpdr->ignoreInvalidDevices))
1564
0
    {
1565
0
      return dummy_irp_response(rdpdr, s);
1566
0
    }
1567
1568
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "irp_new failed with %" PRIu32 "!", error);
1569
0
    return error;
1570
0
  }
1571
1572
0
  if (irp->device->IRPRequest)
1573
0
    error = irp->device->IRPRequest(irp->device, irp);
1574
0
  else
1575
0
    error = irp->Discard(irp);
1576
1577
0
  if (error != CHANNEL_RC_OK)
1578
0
  {
1579
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "device->IRPRequest failed with error %" PRIu32 "",
1580
0
               error);
1581
0
  }
1582
1583
0
  return error;
1584
0
}
1585
1586
static UINT rdpdr_process_component(rdpdrPlugin* rdpdr, UINT16 component, UINT16 packetId,
1587
                                    wStream* s)
1588
0
{
1589
0
  UINT32 type = 0;
1590
0
  DEVICE* device = nullptr;
1591
1592
0
  WINPR_ASSERT(rdpdr);
1593
0
  WINPR_ASSERT(s);
1594
1595
0
  switch (component)
1596
0
  {
1597
0
    case RDPDR_CTYP_PRN:
1598
0
      type = RDPDR_DTYP_PRINT;
1599
0
      break;
1600
1601
0
    default:
1602
0
      return ERROR_INVALID_DATA;
1603
0
  }
1604
1605
0
  device = devman_get_device_by_type(rdpdr->devman, type);
1606
1607
0
  if (!device)
1608
0
    return ERROR_DEV_NOT_EXIST;
1609
1610
0
  return IFCALLRESULT(ERROR_INVALID_PARAMETER, device->CustomComponentRequest, device, component,
1611
0
                      packetId, s);
1612
0
}
1613
1614
/**
1615
 * Function description
1616
 *
1617
 * @return 0 on success, otherwise a Win32 error code
1618
 */
1619
static BOOL device_init(ULONG_PTR key, void* element, void* data)
1620
0
{
1621
0
  wLog* log = data;
1622
0
  UINT error = CHANNEL_RC_OK;
1623
0
  DEVICE* device = element;
1624
1625
0
  WINPR_UNUSED(key);
1626
0
  WINPR_UNUSED(data);
1627
1628
0
  IFCALLRET(device->Init, error, device);
1629
1630
0
  if (error != CHANNEL_RC_OK)
1631
0
  {
1632
0
    WLog_Print(log, WLOG_ERROR, "Device init failed with %s", WTSErrorToString(error));
1633
0
    return FALSE;
1634
0
  }
1635
0
  return TRUE;
1636
0
}
1637
1638
static UINT rdpdr_process_init(rdpdrPlugin* rdpdr)
1639
0
{
1640
0
  WINPR_ASSERT(rdpdr);
1641
0
  WINPR_ASSERT(rdpdr->devman);
1642
1643
0
  rdpdr->userLoggedOn = FALSE; /* reset possible received state */
1644
1645
  /* windows servers tend to trail off if pending IRP are completed after a
1646
   * PAKID_CORE_SERVER_ANNOUNCE message was received.
1647
   * So, set rdpdr->clearing and discard all response messages triggered by
1648
   * cancelling the pending requests.
1649
   */
1650
0
  rdpdr->clearing = TRUE;
1651
0
  BOOL rc = device_foreach(rdpdr, TRUE, device_init, rdpdr->log);
1652
0
  rdpdr->clearing = FALSE;
1653
0
  if (!rc)
1654
0
    return ERROR_INTERNAL_ERROR;
1655
0
  return CHANNEL_RC_OK;
1656
0
}
1657
1658
static BOOL state_match(enum RDPDR_CHANNEL_STATE state, size_t count, va_list ap)
1659
0
{
1660
0
  for (size_t x = 0; x < count; x++)
1661
0
  {
1662
0
    enum RDPDR_CHANNEL_STATE cur = va_arg(ap, enum RDPDR_CHANNEL_STATE);
1663
0
    if (state == cur)
1664
0
      return TRUE;
1665
0
  }
1666
0
  return FALSE;
1667
0
}
1668
1669
static const char* state_str(size_t count, va_list ap, char* buffer, size_t size)
1670
0
{
1671
0
  for (size_t x = 0; x < count; x++)
1672
0
  {
1673
0
    enum RDPDR_CHANNEL_STATE cur = va_arg(ap, enum RDPDR_CHANNEL_STATE);
1674
0
    const char* curstr = rdpdr_state_str(cur);
1675
0
    winpr_str_append(curstr, buffer, size, "|");
1676
0
  }
1677
0
  return buffer;
1678
0
}
1679
1680
static BOOL rdpdr_state_check(rdpdrPlugin* rdpdr, UINT16 packetid, enum RDPDR_CHANNEL_STATE next,
1681
                              size_t count, ...)
1682
0
{
1683
0
  va_list ap = WINPR_C_ARRAY_INIT;
1684
0
  WINPR_ASSERT(rdpdr);
1685
1686
0
  va_start(ap, count);
1687
0
  BOOL rc = state_match(rdpdr->state, count, ap);
1688
0
  va_end(ap);
1689
1690
0
  if (!rc)
1691
0
  {
1692
0
    const char* strstate = rdpdr_state_str(rdpdr->state);
1693
0
    char buffer[256] = WINPR_C_ARRAY_INIT;
1694
1695
0
    va_start(ap, count);
1696
0
    state_str(count, ap, buffer, sizeof(buffer));
1697
0
    va_end(ap);
1698
1699
0
    WLog_Print(rdpdr->log, WLOG_ERROR,
1700
0
               "channel [RDPDR] received %s, expected states [%s] but have state %s, aborting.",
1701
0
               rdpdr_packetid_string(packetid), buffer, strstate);
1702
1703
0
    if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_INITIAL))
1704
0
      return FALSE;
1705
0
    return FALSE;
1706
0
  }
1707
0
  return rdpdr_state_advance(rdpdr, next);
1708
0
}
1709
1710
static BOOL rdpdr_check_channel_state(rdpdrPlugin* rdpdr, UINT16 packetid)
1711
0
{
1712
0
  WINPR_ASSERT(rdpdr);
1713
1714
0
  switch (packetid)
1715
0
  {
1716
0
    case PAKID_CORE_SERVER_ANNOUNCE:
1717
      /* windows servers sometimes send this message.
1718
       * it seems related to session login (e.g. first initialization for RDP/TLS style login,
1719
       * then reinitialize the channel after login successful
1720
       */
1721
0
      if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_INITIAL))
1722
0
        return FALSE;
1723
0
      return rdpdr_state_check(rdpdr, packetid, RDPDR_CHANNEL_STATE_ANNOUNCE, 1,
1724
0
                               RDPDR_CHANNEL_STATE_INITIAL);
1725
0
    case PAKID_CORE_SERVER_CAPABILITY:
1726
0
      return rdpdr_state_check(
1727
0
          rdpdr, packetid, RDPDR_CHANNEL_STATE_SERVER_CAPS, 6,
1728
0
          RDPDR_CHANNEL_STATE_NAME_REQUEST, RDPDR_CHANNEL_STATE_SERVER_CAPS,
1729
0
          RDPDR_CHANNEL_STATE_READY, RDPDR_CHANNEL_STATE_CLIENT_CAPS,
1730
0
          RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM, RDPDR_CHANNEL_STATE_USER_LOGGEDON);
1731
0
    case PAKID_CORE_CLIENTID_CONFIRM:
1732
0
      return rdpdr_state_check(rdpdr, packetid, RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM, 5,
1733
0
                               RDPDR_CHANNEL_STATE_NAME_REQUEST,
1734
0
                               RDPDR_CHANNEL_STATE_SERVER_CAPS,
1735
0
                               RDPDR_CHANNEL_STATE_CLIENT_CAPS, RDPDR_CHANNEL_STATE_READY,
1736
0
                               RDPDR_CHANNEL_STATE_USER_LOGGEDON);
1737
0
    case PAKID_CORE_USER_LOGGEDON:
1738
0
      if (!rdpdr_check_extended_pdu_flag(rdpdr, RDPDR_USER_LOGGEDON_PDU))
1739
0
        return FALSE;
1740
1741
0
      return rdpdr_state_check(
1742
0
          rdpdr, packetid, RDPDR_CHANNEL_STATE_USER_LOGGEDON, 4,
1743
0
          RDPDR_CHANNEL_STATE_NAME_REQUEST, RDPDR_CHANNEL_STATE_CLIENT_CAPS,
1744
0
          RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM, RDPDR_CHANNEL_STATE_READY);
1745
0
    default:
1746
0
    {
1747
0
      enum RDPDR_CHANNEL_STATE state = RDPDR_CHANNEL_STATE_READY;
1748
0
      return rdpdr_state_check(rdpdr, packetid, state, 1, state);
1749
0
    }
1750
0
  }
1751
0
}
1752
1753
static BOOL tryAdvance(rdpdrPlugin* rdpdr, BOOL announce)
1754
0
{
1755
0
  if (rdpdr->haveClientId && rdpdr->haveServerCaps)
1756
0
  {
1757
0
    if (announce)
1758
0
    {
1759
0
      const UINT error = rdpdr_send_device_list_announce_request(rdpdr, FALSE);
1760
0
      if (error)
1761
0
      {
1762
0
        WLog_Print(rdpdr->log, WLOG_ERROR,
1763
0
                   "rdpdr_send_device_list_announce_request failed with error %" PRIu32 "",
1764
0
                   error);
1765
0
        return FALSE;
1766
0
      }
1767
0
    }
1768
0
    if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_READY))
1769
0
      return FALSE;
1770
0
  }
1771
0
  return TRUE;
1772
0
}
1773
1774
/**
1775
 * Function description
1776
 *
1777
 * @return 0 on success, otherwise a Win32 error code
1778
 */
1779
static UINT rdpdr_process_receive(rdpdrPlugin* rdpdr, wStream* s)
1780
0
{
1781
0
  UINT16 component = 0;
1782
0
  UINT16 packetId = 0;
1783
0
  UINT32 deviceId = 0;
1784
0
  UINT32 status = 0;
1785
0
  UINT error = ERROR_INVALID_DATA;
1786
1787
0
  if (!rdpdr || !s)
1788
0
    return CHANNEL_RC_NULL_DATA;
1789
1790
0
  rdpdr_dump_received_packet(rdpdr->log, WLOG_TRACE, s, "[rdpdr-channel] receive");
1791
0
  if (Stream_GetRemainingLength(s) >= 4)
1792
0
  {
1793
0
    Stream_Read_UINT16(s, component); /* Component (2 bytes) */
1794
0
    Stream_Read_UINT16(s, packetId);  /* PacketId (2 bytes) */
1795
1796
0
    if (component == RDPDR_CTYP_CORE)
1797
0
    {
1798
0
      if (!rdpdr_check_channel_state(rdpdr, packetId))
1799
0
        return CHANNEL_RC_OK;
1800
1801
0
      switch (packetId)
1802
0
      {
1803
0
        case PAKID_CORE_SERVER_ANNOUNCE:
1804
0
          rdpdr->haveClientId = FALSE;
1805
0
          rdpdr->haveServerCaps = FALSE;
1806
0
          if ((error = rdpdr_process_server_announce_request(rdpdr, s)))
1807
0
          {
1808
0
          }
1809
0
          else if ((error = rdpdr_send_client_announce_reply(rdpdr)))
1810
0
          {
1811
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1812
0
                       "rdpdr_send_client_announce_reply failed with error %" PRIu32 "",
1813
0
                       error);
1814
0
          }
1815
0
          else if ((error = rdpdr_send_client_name_request(rdpdr)))
1816
0
          {
1817
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1818
0
                       "rdpdr_send_client_name_request failed with error %" PRIu32 "",
1819
0
                       error);
1820
0
          }
1821
0
          else if ((error = rdpdr_process_init(rdpdr)))
1822
0
          {
1823
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1824
0
                       "rdpdr_process_init failed with error %" PRIu32 "", error);
1825
0
          }
1826
1827
0
          break;
1828
1829
0
        case PAKID_CORE_SERVER_CAPABILITY:
1830
0
          if ((error = rdpdr_process_capability_request(rdpdr, s)))
1831
0
          {
1832
0
          }
1833
0
          else if ((error = rdpdr_send_capability_response(rdpdr)))
1834
0
          {
1835
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1836
0
                       "rdpdr_send_capability_response failed with error %" PRIu32 "",
1837
0
                       error);
1838
0
          }
1839
0
          else
1840
0
          {
1841
0
            rdpdr->haveServerCaps = TRUE;
1842
0
            if (!tryAdvance(rdpdr, TRUE))
1843
0
              error = ERROR_INTERNAL_ERROR;
1844
0
          }
1845
1846
0
          break;
1847
1848
0
        case PAKID_CORE_CLIENTID_CONFIRM:
1849
0
          if ((error = rdpdr_process_server_clientid_confirm(rdpdr, s)))
1850
0
          {
1851
0
          }
1852
0
          else
1853
0
          {
1854
0
            rdpdr->haveClientId = TRUE;
1855
0
            if (!tryAdvance(rdpdr, TRUE))
1856
0
              error = ERROR_INTERNAL_ERROR;
1857
0
          }
1858
0
          break;
1859
1860
0
        case PAKID_CORE_USER_LOGGEDON:
1861
0
          if (!rdpdr->haveServerCaps)
1862
0
          {
1863
            /* Windows re-announces the channel after logon and may send
1864
             * USER_LOGGEDON before the new SERVER_CAPABILITY arrives.
1865
             * Not fatal: skip the device announce here, tryAdvance()
1866
             * sends it once the capability exchange completes. */
1867
0
            WLog_Print(rdpdr->log, WLOG_WARN,
1868
0
                       "%s in state %s, ignoring. [serverCaps=%d, clientId=%d]",
1869
0
                       rdpdr_packetid_string(packetId), rdpdr_state_str(rdpdr->state),
1870
0
                       rdpdr->haveServerCaps, rdpdr->haveClientId);
1871
0
            error = CHANNEL_RC_OK;
1872
0
          }
1873
0
          else if ((error = rdpdr_send_device_list_announce_request(rdpdr, TRUE)))
1874
0
          {
1875
0
            WLog_Print(
1876
0
                rdpdr->log, WLOG_ERROR,
1877
0
                "rdpdr_send_device_list_announce_request failed with error %" PRIu32 "",
1878
0
                error);
1879
0
          }
1880
0
          else if (!tryAdvance(rdpdr, FALSE))
1881
0
          {
1882
0
            error = ERROR_INTERNAL_ERROR;
1883
0
          }
1884
1885
0
          break;
1886
1887
0
        case PAKID_CORE_DEVICE_REPLY:
1888
1889
          /* connect to a specific resource */
1890
0
          if (Stream_GetRemainingLength(s) >= 8)
1891
0
          {
1892
0
            Stream_Read_UINT32(s, deviceId);
1893
0
            Stream_Read_UINT32(s, status);
1894
1895
0
            if (status != 0)
1896
0
              devman_unregister_device(rdpdr->devman, (void*)((size_t)deviceId));
1897
0
            error = CHANNEL_RC_OK;
1898
0
          }
1899
1900
0
          break;
1901
1902
0
        case PAKID_CORE_DEVICE_IOREQUEST:
1903
0
          if ((error = rdpdr_process_irp(rdpdr, s)))
1904
0
          {
1905
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1906
0
                       "rdpdr_process_irp failed with error %" PRIu32 "", error);
1907
0
            return error;
1908
0
          }
1909
0
          else
1910
0
            s = nullptr;
1911
1912
0
          break;
1913
1914
0
        default:
1915
0
          WLog_Print(rdpdr->log, WLOG_ERROR,
1916
0
                     "RDPDR_CTYP_CORE unknown PacketId: 0x%04" PRIX16 "", packetId);
1917
0
          error = ERROR_INVALID_DATA;
1918
0
          break;
1919
0
      }
1920
0
    }
1921
0
    else
1922
0
    {
1923
0
      error = rdpdr_process_component(rdpdr, component, packetId, s);
1924
1925
0
      if (error != CHANNEL_RC_OK)
1926
0
      {
1927
0
        DWORD level = WLOG_ERROR;
1928
0
        if (rdpdr->ignoreInvalidDevices)
1929
0
        {
1930
0
          if (error == ERROR_DEV_NOT_EXIST)
1931
0
          {
1932
0
            level = WLOG_WARN;
1933
0
            error = CHANNEL_RC_OK;
1934
0
          }
1935
0
        }
1936
0
        WLog_Print(rdpdr->log, level,
1937
0
                   "Unknown message: Component: %s [0x%04" PRIX16
1938
0
                   "] PacketId: %s [0x%04" PRIX16 "]",
1939
0
                   rdpdr_component_string(component), component,
1940
0
                   rdpdr_packetid_string(packetId), packetId);
1941
0
      }
1942
0
    }
1943
0
  }
1944
1945
0
  return error;
1946
0
}
1947
1948
/**
1949
 * Function description
1950
 *
1951
 * @return 0 on success, otherwise a Win32 error code
1952
 */
1953
UINT rdpdr_send(rdpdrPlugin* rdpdr, wStream* s)
1954
0
{
1955
0
  rdpdrPlugin* plugin = rdpdr;
1956
1957
0
  if (rdpdr->clearing)
1958
0
  {
1959
0
    WLog_ERR(TAG, "trying to send message while reinitializing channel, aborting");
1960
0
    return ERROR_INTERNAL_ERROR;
1961
0
  }
1962
0
  if (!s)
1963
0
  {
1964
0
    Stream_Release(s);
1965
0
    return CHANNEL_RC_NULL_DATA;
1966
0
  }
1967
1968
0
  if (!plugin)
1969
0
  {
1970
0
    Stream_Release(s);
1971
0
    return CHANNEL_RC_BAD_INIT_HANDLE;
1972
0
  }
1973
1974
0
  const size_t pos = Stream_GetPosition(s);
1975
0
  UINT status = ERROR_INTERNAL_ERROR;
1976
0
  if (pos <= UINT32_MAX)
1977
0
  {
1978
0
    rdpdr_dump_send_packet(rdpdr->log, WLOG_TRACE, s, "[rdpdr-channel] send");
1979
0
    status = plugin->channelEntryPoints.pVirtualChannelWriteEx(
1980
0
        plugin->InitHandle, plugin->OpenHandle, Stream_Buffer(s), (UINT32)pos, s);
1981
0
  }
1982
1983
0
  if (status != CHANNEL_RC_OK)
1984
0
  {
1985
0
    Stream_Release(s);
1986
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "pVirtualChannelWriteEx failed with %s [%08" PRIX32 "]",
1987
0
               WTSErrorToString(status), status);
1988
0
  }
1989
1990
0
  return status;
1991
0
}
1992
1993
/**
1994
 * Function description
1995
 *
1996
 * @return 0 on success, otherwise a Win32 error code
1997
 */
1998
static UINT rdpdr_virtual_channel_event_data_received(rdpdrPlugin* rdpdr, void* pData,
1999
                                                      UINT32 dataLength, UINT32 totalLength,
2000
                                                      UINT32 dataFlags)
2001
0
{
2002
0
  WINPR_ASSERT(rdpdr);
2003
0
  WINPR_ASSERT(pData || (dataLength == 0));
2004
2005
0
  if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
2006
0
  {
2007
    /*
2008
     * According to MS-RDPBCGR 2.2.6.1, "All virtual channel traffic MUST be suspended.
2009
     * This flag is only valid in server-to-client virtual channel traffic. It MUST be
2010
     * ignored in client-to-server data." Thus it would be best practice to cease data
2011
     * transmission. However, simply returning here avoids a crash.
2012
     */
2013
0
    return CHANNEL_RC_OK;
2014
0
  }
2015
2016
0
  if (dataFlags & CHANNEL_FLAG_FIRST)
2017
0
  {
2018
0
    if (rdpdr->data_in != nullptr)
2019
0
      Stream_Release(rdpdr->data_in);
2020
2021
0
    rdpdr->data_in = StreamPool_Take(rdpdr->pool, totalLength);
2022
2023
0
    if (!rdpdr->data_in)
2024
0
    {
2025
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
2026
0
      return CHANNEL_RC_NO_MEMORY;
2027
0
    }
2028
0
  }
2029
2030
0
  if (!rdpdr->data_in)
2031
0
  {
2032
0
    WLog_Print(rdpdr->log, WLOG_ERROR,
2033
0
               "Invalid state, no CHANNEL_FLAG_FIRST received, aborting.");
2034
0
    return ERROR_INVALID_DATA;
2035
0
  }
2036
2037
0
  wStream* data_in = rdpdr->data_in;
2038
0
  if (!Stream_EnsureRemainingCapacity(data_in, dataLength))
2039
0
  {
2040
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
2041
0
    return ERROR_INVALID_DATA;
2042
0
  }
2043
2044
0
  Stream_Write(data_in, pData, dataLength);
2045
2046
0
  if (dataFlags & CHANNEL_FLAG_LAST)
2047
0
  {
2048
0
    const size_t pos = Stream_GetPosition(data_in);
2049
0
    const size_t cap = Stream_Capacity(data_in);
2050
0
    if (cap < pos)
2051
0
    {
2052
0
      WLog_Print(rdpdr->log, WLOG_ERROR,
2053
0
                 "rdpdr_virtual_channel_event_data_received: read error");
2054
0
      return ERROR_INTERNAL_ERROR;
2055
0
    }
2056
2057
0
    Stream_SealLength(data_in);
2058
0
    Stream_ResetPosition(data_in);
2059
2060
0
    if (rdpdr->async)
2061
0
    {
2062
0
      if (!MessageQueue_Post(rdpdr->queue, nullptr, 0, (void*)data_in, nullptr))
2063
0
      {
2064
0
        WLog_Print(rdpdr->log, WLOG_ERROR, "MessageQueue_Post failed!");
2065
0
        return ERROR_INTERNAL_ERROR;
2066
0
      }
2067
0
      rdpdr->data_in = nullptr;
2068
0
    }
2069
0
    else
2070
0
    {
2071
0
      UINT error = rdpdr_process_receive(rdpdr, data_in);
2072
0
      Stream_Release(data_in);
2073
0
      rdpdr->data_in = nullptr;
2074
0
      if (error)
2075
0
        return error;
2076
0
    }
2077
0
  }
2078
2079
0
  return CHANNEL_RC_OK;
2080
0
}
2081
2082
static VOID VCAPITYPE rdpdr_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
2083
                                                          UINT event, LPVOID pData,
2084
                                                          UINT32 dataLength, UINT32 totalLength,
2085
                                                          UINT32 dataFlags)
2086
0
{
2087
0
  UINT error = CHANNEL_RC_OK;
2088
0
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)lpUserParam;
2089
2090
0
  WINPR_ASSERT(rdpdr);
2091
0
  switch (event)
2092
0
  {
2093
0
    case CHANNEL_EVENT_DATA_RECEIVED:
2094
0
      if (!rdpdr || !pData || (rdpdr->OpenHandle != openHandle))
2095
0
      {
2096
0
        WLog_Print(rdpdr->log, WLOG_ERROR, "error no match");
2097
0
        return;
2098
0
      }
2099
0
      if ((error = rdpdr_virtual_channel_event_data_received(rdpdr, pData, dataLength,
2100
0
                                                             totalLength, dataFlags)))
2101
0
        WLog_Print(rdpdr->log, WLOG_ERROR,
2102
0
                   "rdpdr_virtual_channel_event_data_received failed with error %" PRIu32
2103
0
                   "!",
2104
0
                   error);
2105
2106
0
      break;
2107
2108
0
    case CHANNEL_EVENT_WRITE_CANCELLED:
2109
0
    case CHANNEL_EVENT_WRITE_COMPLETE:
2110
0
    {
2111
0
      wStream* s = (wStream*)pData;
2112
0
      Stream_Release(s);
2113
0
    }
2114
0
    break;
2115
2116
0
    case CHANNEL_EVENT_USER:
2117
0
      break;
2118
0
    default:
2119
0
      break;
2120
0
  }
2121
2122
0
  if (error && rdpdr && rdpdr->rdpcontext)
2123
0
    setChannelError(rdpdr->rdpcontext, error,
2124
0
                    "rdpdr_virtual_channel_open_event_ex reported an error");
2125
0
}
2126
2127
static DWORD WINAPI rdpdr_virtual_channel_client_thread(LPVOID arg)
2128
0
{
2129
0
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)arg;
2130
0
  UINT error = 0;
2131
2132
0
  if (!rdpdr)
2133
0
  {
2134
0
    ExitThread((DWORD)CHANNEL_RC_NULL_DATA);
2135
0
    return CHANNEL_RC_NULL_DATA;
2136
0
  }
2137
2138
0
  if ((error = rdpdr_process_connect(rdpdr)))
2139
0
  {
2140
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "rdpdr_process_connect failed with error %" PRIu32 "!",
2141
0
               error);
2142
2143
0
    if (rdpdr->rdpcontext)
2144
0
      setChannelError(rdpdr->rdpcontext, error,
2145
0
                      "rdpdr_virtual_channel_client_thread reported an error");
2146
2147
0
    ExitThread(error);
2148
0
    return error;
2149
0
  }
2150
2151
0
  while (1)
2152
0
  {
2153
0
    wMessage message = WINPR_C_ARRAY_INIT;
2154
0
    WINPR_ASSERT(rdpdr);
2155
2156
0
    if (!MessageQueue_Wait(rdpdr->queue))
2157
0
      break;
2158
2159
0
    if (MessageQueue_Peek(rdpdr->queue, &message, TRUE))
2160
0
    {
2161
0
      if (message.id == WMQ_QUIT)
2162
0
        break;
2163
2164
0
      if (message.id == 0)
2165
0
      {
2166
0
        wStream* data = (wStream*)message.wParam;
2167
2168
0
        error = rdpdr_process_receive(rdpdr, data);
2169
2170
0
        Stream_Release(data);
2171
0
        if (error)
2172
0
        {
2173
0
          WLog_Print(rdpdr->log, WLOG_ERROR,
2174
0
                     "rdpdr_process_receive failed with error %" PRIu32 "!", error);
2175
2176
0
          if (rdpdr->rdpcontext)
2177
0
            setChannelError(rdpdr->rdpcontext, error,
2178
0
                            "rdpdr_virtual_channel_client_thread reported an error");
2179
2180
0
          goto fail;
2181
0
        }
2182
0
      }
2183
0
    }
2184
0
  }
2185
2186
0
fail:
2187
0
  if ((error = drive_hotplug_thread_terminate(rdpdr)))
2188
0
    WLog_Print(rdpdr->log, WLOG_ERROR,
2189
0
               "drive_hotplug_thread_terminate failed with error %" PRIu32 "!", error);
2190
2191
0
  ExitThread(error);
2192
0
  return error;
2193
0
}
2194
2195
static void queue_free(void* obj)
2196
0
{
2197
0
  wStream* s = nullptr;
2198
0
  wMessage* msg = (wMessage*)obj;
2199
2200
0
  if (!msg || (msg->id != 0))
2201
0
    return;
2202
2203
0
  s = (wStream*)msg->wParam;
2204
0
  WINPR_ASSERT(s);
2205
0
  Stream_Release(s);
2206
0
}
2207
2208
/**
2209
 * Function description
2210
 *
2211
 * @return 0 on success, otherwise a Win32 error code
2212
 */
2213
static UINT rdpdr_virtual_channel_event_connected(rdpdrPlugin* rdpdr, LPVOID pData,
2214
                                                  UINT32 dataLength)
2215
0
{
2216
0
  wObject* obj = nullptr;
2217
2218
0
  WINPR_ASSERT(rdpdr);
2219
0
  WINPR_UNUSED(pData);
2220
0
  WINPR_UNUSED(dataLength);
2221
2222
0
  if (rdpdr->async)
2223
0
  {
2224
0
    rdpdr->queue = MessageQueue_New(nullptr);
2225
2226
0
    if (!rdpdr->queue)
2227
0
    {
2228
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "MessageQueue_New failed!");
2229
0
      return CHANNEL_RC_NO_MEMORY;
2230
0
    }
2231
2232
0
    obj = MessageQueue_Object(rdpdr->queue);
2233
0
    obj->fnObjectFree = queue_free;
2234
2235
0
    if (!(rdpdr->thread = CreateThread(nullptr, 0, rdpdr_virtual_channel_client_thread,
2236
0
                                       (void*)rdpdr, 0, nullptr)))
2237
0
    {
2238
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "CreateThread failed!");
2239
0
      return ERROR_INTERNAL_ERROR;
2240
0
    }
2241
0
  }
2242
0
  else
2243
0
  {
2244
0
    UINT error = rdpdr_process_connect(rdpdr);
2245
0
    if (error)
2246
0
    {
2247
0
      WLog_Print(rdpdr->log, WLOG_ERROR,
2248
0
                 "rdpdr_process_connect failed with error %" PRIu32 "!", error);
2249
0
      return error;
2250
0
    }
2251
0
  }
2252
2253
0
  return rdpdr->channelEntryPoints.pVirtualChannelOpenEx(rdpdr->InitHandle, &rdpdr->OpenHandle,
2254
0
                                                         rdpdr->channelDef.name,
2255
0
                                                         rdpdr_virtual_channel_open_event_ex);
2256
0
}
2257
2258
/**
2259
 * Function description
2260
 *
2261
 * @return 0 on success, otherwise a Win32 error code
2262
 */
2263
static UINT rdpdr_virtual_channel_event_disconnected(rdpdrPlugin* rdpdr)
2264
0
{
2265
0
  UINT error = 0;
2266
2267
0
  WINPR_ASSERT(rdpdr);
2268
2269
0
  if (rdpdr->OpenHandle == 0)
2270
0
    return CHANNEL_RC_OK;
2271
2272
0
  if (rdpdr->queue && rdpdr->thread)
2273
0
  {
2274
0
    if (MessageQueue_PostQuit(rdpdr->queue, 0) &&
2275
0
        (WaitForSingleObject(rdpdr->thread, INFINITE) == WAIT_FAILED))
2276
0
    {
2277
0
      error = GetLastError();
2278
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "!",
2279
0
                 error);
2280
0
      return error;
2281
0
    }
2282
0
  }
2283
2284
0
  if (rdpdr->thread)
2285
0
    (void)CloseHandle(rdpdr->thread);
2286
0
  MessageQueue_Free(rdpdr->queue);
2287
0
  rdpdr->queue = nullptr;
2288
0
  rdpdr->thread = nullptr;
2289
2290
0
  WINPR_ASSERT(rdpdr->channelEntryPoints.pVirtualChannelCloseEx);
2291
0
  error = rdpdr->channelEntryPoints.pVirtualChannelCloseEx(rdpdr->InitHandle, rdpdr->OpenHandle);
2292
2293
0
  if (CHANNEL_RC_OK != error)
2294
0
  {
2295
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "pVirtualChannelCloseEx failed with %s [%08" PRIX32 "]",
2296
0
               WTSErrorToString(error), error);
2297
0
  }
2298
2299
0
  rdpdr->OpenHandle = 0;
2300
2301
0
  if (rdpdr->data_in)
2302
0
  {
2303
0
    Stream_Release(rdpdr->data_in);
2304
0
    rdpdr->data_in = nullptr;
2305
0
  }
2306
2307
0
  if (rdpdr->devman)
2308
0
  {
2309
0
    devman_free(rdpdr->devman);
2310
0
    rdpdr->devman = nullptr;
2311
0
  }
2312
2313
0
  return error;
2314
0
}
2315
2316
static void rdpdr_virtual_channel_event_terminated(rdpdrPlugin* rdpdr)
2317
0
{
2318
0
  WINPR_ASSERT(rdpdr);
2319
0
#if !defined(_WIN32)
2320
0
  if (rdpdr->stopEvent)
2321
0
  {
2322
0
    (void)CloseHandle(rdpdr->stopEvent);
2323
0
    rdpdr->stopEvent = nullptr;
2324
0
  }
2325
0
#endif
2326
0
  rdpdr->InitHandle = nullptr;
2327
0
  StreamPool_Free(rdpdr->pool);
2328
0
  free(rdpdr);
2329
0
}
2330
2331
static UINT rdpdr_register_device(RdpdrClientContext* context, const RDPDR_DEVICE* device,
2332
                                  uint32_t* pid)
2333
0
{
2334
0
  WINPR_ASSERT(context);
2335
0
  WINPR_ASSERT(device);
2336
0
  WINPR_ASSERT(pid);
2337
2338
0
  rdpdrPlugin* rdpdr = context->handle;
2339
0
  WINPR_ASSERT(rdpdr);
2340
2341
0
  RDPDR_DEVICE* copy = freerdp_device_clone(device);
2342
0
  if (!copy)
2343
0
    return ERROR_INVALID_DATA;
2344
0
  UINT rc = devman_load_device_service(rdpdr->devman, copy, rdpdr->rdpcontext);
2345
0
  *pid = copy->Id;
2346
0
  freerdp_device_free(copy);
2347
0
  if (rc == CHANNEL_RC_OK)
2348
0
    rc = rdpdr_try_send_device_list_announce_request(rdpdr);
2349
0
  return rc;
2350
0
}
2351
2352
static UINT rdpdr_unregister_device(RdpdrClientContext* context, size_t count, const uint32_t ids[])
2353
0
{
2354
0
  WINPR_ASSERT(context);
2355
2356
0
  rdpdrPlugin* rdpdr = context->handle;
2357
0
  WINPR_ASSERT(rdpdr);
2358
2359
0
  for (size_t x = 0; x < count; x++)
2360
0
  {
2361
0
    const uintptr_t id = ids[x];
2362
0
    devman_unregister_device(rdpdr->devman, (void*)id);
2363
0
  }
2364
0
  return rdpdr_send_device_list_remove_request(rdpdr, WINPR_ASSERTING_INT_CAST(uint32_t, count),
2365
0
                                               ids);
2366
0
}
2367
2368
static UINT rdpdr_virtual_channel_event_initialized(rdpdrPlugin* rdpdr,
2369
                                                    WINPR_ATTR_UNUSED LPVOID pData,
2370
                                                    WINPR_ATTR_UNUSED UINT32 dataLength)
2371
0
{
2372
0
  WINPR_ASSERT(rdpdr);
2373
0
#if !defined(_WIN32)
2374
0
  WINPR_ASSERT(!rdpdr->stopEvent);
2375
0
  rdpdr->stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
2376
0
  if (!rdpdr->stopEvent)
2377
0
    return ERROR_INTERNAL_ERROR;
2378
0
#endif
2379
2380
0
  rdpdr->context.handle = rdpdr;
2381
0
  rdpdr->context.RdpdrHotplugDevice = handle_hotplug;
2382
0
  rdpdr->context.RdpdrRegisterDevice = rdpdr_register_device;
2383
0
  rdpdr->context.RdpdrUnregisterDevice = rdpdr_unregister_device;
2384
0
  return CHANNEL_RC_OK;
2385
0
}
2386
2387
static VOID VCAPITYPE rdpdr_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
2388
                                                          UINT event, LPVOID pData, UINT dataLength)
2389
0
{
2390
0
  UINT error = CHANNEL_RC_OK;
2391
0
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)lpUserParam;
2392
2393
0
  if (!rdpdr || (rdpdr->InitHandle != pInitHandle))
2394
0
  {
2395
0
    WLog_ERR(TAG, "error no match");
2396
0
    return;
2397
0
  }
2398
2399
0
  WINPR_ASSERT(pData || (dataLength == 0));
2400
2401
0
  switch (event)
2402
0
  {
2403
0
    case CHANNEL_EVENT_INITIALIZED:
2404
0
      error = rdpdr_virtual_channel_event_initialized(rdpdr, pData, dataLength);
2405
0
      break;
2406
2407
0
    case CHANNEL_EVENT_CONNECTED:
2408
0
      if ((error = rdpdr_virtual_channel_event_connected(rdpdr, pData, dataLength)))
2409
0
        WLog_Print(rdpdr->log, WLOG_ERROR,
2410
0
                   "rdpdr_virtual_channel_event_connected failed with error %" PRIu32 "!",
2411
0
                   error);
2412
2413
0
      break;
2414
2415
0
    case CHANNEL_EVENT_DISCONNECTED:
2416
0
      if ((error = rdpdr_virtual_channel_event_disconnected(rdpdr)))
2417
0
        WLog_Print(rdpdr->log, WLOG_ERROR,
2418
0
                   "rdpdr_virtual_channel_event_disconnected failed with error %" PRIu32
2419
0
                   "!",
2420
0
                   error);
2421
2422
0
      break;
2423
2424
0
    case CHANNEL_EVENT_TERMINATED:
2425
0
      rdpdr_virtual_channel_event_terminated(rdpdr);
2426
0
      rdpdr = nullptr;
2427
0
      break;
2428
2429
0
    case CHANNEL_EVENT_ATTACHED:
2430
0
    case CHANNEL_EVENT_DETACHED:
2431
0
    default:
2432
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "unknown event %" PRIu32 "!", event);
2433
0
      break;
2434
0
  }
2435
2436
0
  if (error && rdpdr && rdpdr->rdpcontext)
2437
0
    setChannelError(rdpdr->rdpcontext, error,
2438
0
                    "rdpdr_virtual_channel_init_event_ex reported an error");
2439
0
}
2440
2441
/* rdpdr is always built-in */
2442
#define VirtualChannelEntryEx rdpdr_VirtualChannelEntryEx
2443
2444
FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS_EX pEntryPoints,
2445
                                                         PVOID pInitHandle))
2446
0
{
2447
0
  WINPR_ASSERT(pEntryPoints);
2448
0
  WINPR_ASSERT(pInitHandle);
2449
2450
0
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)calloc(1, sizeof(rdpdrPlugin));
2451
2452
0
  if (!rdpdr)
2453
0
  {
2454
0
    WLog_ERR(TAG, "calloc failed!");
2455
0
    return FALSE;
2456
0
  }
2457
0
  rdpdr->log = WLog_Get(TAG);
2458
2459
0
  rdpdr->clientExtendedPDU =
2460
0
      RDPDR_DEVICE_REMOVE_PDUS | RDPDR_CLIENT_DISPLAY_NAME_PDU | RDPDR_USER_LOGGEDON_PDU;
2461
0
  rdpdr->clientIOCode1 =
2462
0
      RDPDR_IRP_MJ_CREATE | RDPDR_IRP_MJ_CLEANUP | RDPDR_IRP_MJ_CLOSE | RDPDR_IRP_MJ_READ |
2463
0
      RDPDR_IRP_MJ_WRITE | RDPDR_IRP_MJ_FLUSH_BUFFERS | RDPDR_IRP_MJ_SHUTDOWN |
2464
0
      RDPDR_IRP_MJ_DEVICE_CONTROL | RDPDR_IRP_MJ_QUERY_VOLUME_INFORMATION |
2465
0
      RDPDR_IRP_MJ_SET_VOLUME_INFORMATION | RDPDR_IRP_MJ_QUERY_INFORMATION |
2466
0
      RDPDR_IRP_MJ_SET_INFORMATION | RDPDR_IRP_MJ_DIRECTORY_CONTROL | RDPDR_IRP_MJ_LOCK_CONTROL |
2467
0
      RDPDR_IRP_MJ_QUERY_SECURITY | RDPDR_IRP_MJ_SET_SECURITY;
2468
2469
0
  rdpdr->clientExtraFlags1 = ENABLE_ASYNCIO;
2470
2471
0
  rdpdr->pool = StreamPool_New(TRUE, 1024);
2472
0
  if (!rdpdr->pool)
2473
0
  {
2474
0
    free(rdpdr);
2475
0
    return FALSE;
2476
0
  }
2477
2478
0
  rdpdr->channelDef.options =
2479
0
      CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP | CHANNEL_OPTION_COMPRESS_RDP;
2480
0
  (void)sprintf_s(rdpdr->channelDef.name, ARRAYSIZE(rdpdr->channelDef.name),
2481
0
                  RDPDR_SVC_CHANNEL_NAME);
2482
0
  rdpdr->sequenceId = 0;
2483
0
  CHANNEL_ENTRY_POINTS_FREERDP_EX* pEntryPointsEx =
2484
0
      (CHANNEL_ENTRY_POINTS_FREERDP_EX*)pEntryPoints;
2485
2486
0
  if ((pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX)) &&
2487
0
      (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
2488
0
  {
2489
0
    rdpdr->rdpcontext = pEntryPointsEx->context;
2490
0
    if (!freerdp_settings_get_bool(rdpdr->rdpcontext->settings,
2491
0
                                   FreeRDP_SynchronousStaticChannels))
2492
0
      rdpdr->async = TRUE;
2493
0
  }
2494
2495
0
  CopyMemory(&(rdpdr->channelEntryPoints), pEntryPoints, sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX));
2496
0
  rdpdr->InitHandle = pInitHandle;
2497
0
  const UINT rc = rdpdr->channelEntryPoints.pVirtualChannelInitEx(
2498
0
      rdpdr, &rdpdr->context, pInitHandle, &rdpdr->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
2499
0
      rdpdr_virtual_channel_init_event_ex);
2500
2501
0
  if (CHANNEL_RC_OK != rc)
2502
0
  {
2503
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "pVirtualChannelInitEx failed with %s [%08" PRIX32 "]",
2504
0
               WTSErrorToString(rc), rc);
2505
0
    free(rdpdr);
2506
0
    return FALSE;
2507
0
  }
2508
2509
0
  return TRUE;
2510
0
}