Coverage Report

Created: 2026-04-12 07:03

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[] = { 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
  UINT32 ids[1];
527
528
  DIR* pDir = opendir(szdir);
529
530
  if (pDir == nullptr)
531
  {
532
    printf("Cannot open directory\n");
533
    return ERROR_OPEN_FAILED;
534
  }
535
536
  while ((pDirent = readdir(pDir)) != nullptr)
537
  {
538
    if (pDirent->d_name[0] != '.')
539
    {
540
      (void)sprintf_s(fullpath, ARRAYSIZE(fullpath), "%s/%s", szdir, pDirent->d_name);
541
      if (stat(fullpath, &buf) != 0)
542
        continue;
543
544
      if (S_ISDIR(buf.st_mode))
545
      {
546
        dev_array[size].path = _strdup(fullpath);
547
548
        if (!dev_array[size].path)
549
        {
550
          closedir(pDir);
551
          error = CHANNEL_RC_NO_MEMORY;
552
          goto cleanup;
553
        }
554
555
        dev_array[size++].to_add = TRUE;
556
      }
557
    }
558
  }
559
560
  closedir(pDir);
561
  /* delete removed devices */
562
  count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
563
564
  for (size_t j = 0; j < count; j++)
565
  {
566
    char* path = nullptr;
567
    BOOL dev_found = FALSE;
568
    device_ext =
569
        (DEVICE_DRIVE_EXT*)ListDictionary_GetItemValue(rdpdr->devman->devices, (void*)keys[j]);
570
571
    if (!device_ext || !device_ext->automount)
572
      continue;
573
574
    if (device_ext->device.type != RDPDR_DTYP_FILESYSTEM)
575
      continue;
576
577
    if (device_ext->path == nullptr)
578
      continue;
579
580
    path = ConvertWCharToUtf8Alloc(device_ext->path, nullptr);
581
    if (!path)
582
      continue;
583
584
    /* not pluggable device */
585
    if (strstr(path, "/Volumes/") == nullptr)
586
    {
587
      free(path);
588
      continue;
589
    }
590
591
    for (size_t i = 0; i < size; i++)
592
    {
593
      if (strstr(path, dev_array[i].path) != nullptr)
594
      {
595
        dev_found = TRUE;
596
        dev_array[i].to_add = FALSE;
597
        break;
598
      }
599
    }
600
601
    free(path);
602
603
    if (!dev_found)
604
    {
605
      const uint32_t ids[] = { keys[j] };
606
      WINPR_ASSERT(rdpdr->context.RdpdrUnregisterDevice);
607
      error = rdpdr->context.RdpdrUnregisterDevice(&rdpdr->context, ARRAYSIZE(ids), ids);
608
      if (error)
609
      {
610
        WLog_Print(rdpdr->log, WLOG_ERROR,
611
                   "rdpdr_send_device_list_remove_request failed with error %" PRIu32 "!",
612
                   error);
613
        goto cleanup;
614
      }
615
    }
616
  }
617
618
  /* add new devices */
619
  for (size_t i = 0; i < size; i++)
620
  {
621
    const hotplug_dev* dev = &dev_array[i];
622
    if (dev->to_add)
623
    {
624
      const char* path = dev->path;
625
      const char* name = strrchr(path, '/') + 1;
626
      error = rdpdr_load_drive(rdpdr, name, path, TRUE);
627
      if (error)
628
        goto cleanup;
629
    }
630
  }
631
632
cleanup:
633
  free(keys);
634
635
  for (size_t i = 0; i < size; i++)
636
    free(dev_array[i].path);
637
638
  return error;
639
}
640
641
static void drive_hotplug_fsevent_callback(ConstFSEventStreamRef streamRef,
642
                                           void* clientCallBackInfo, size_t numEvents,
643
                                           void* eventPaths,
644
                                           const FSEventStreamEventFlags eventFlags[],
645
                                           const FSEventStreamEventId eventIds[])
646
{
647
  rdpdrPlugin* rdpdr;
648
  UINT error;
649
  char** paths = (char**)eventPaths;
650
  rdpdr = (rdpdrPlugin*)clientCallBackInfo;
651
652
  for (size_t i = 0; i < numEvents; i++)
653
  {
654
    if (strcmp(paths[i], "/Volumes/") == 0)
655
    {
656
      UINT error = ERROR_CALL_NOT_IMPLEMENTED;
657
      if (rdpdr->context.RdpdrHotplugDevice)
658
        error = rdpdr->context.RdpdrHotplugDevice(&rdpdr->context,
659
                                                  RDPDR_HOTPLUG_CHECK_FOR_CHANGES);
660
      switch (error)
661
      {
662
        case ERROR_DISK_CHANGE:
663
        case CHANNEL_RC_OK:
664
          break;
665
        case ERROR_CALL_NOT_IMPLEMENTED:
666
          break;
667
        default:
668
          WLog_Print(rdpdr->log, WLOG_ERROR,
669
                     "handle_hotplug failed with error %" PRIu32 "!", error);
670
          break;
671
      }
672
    }
673
  }
674
}
675
676
static void first_hotplug(rdpdrPlugin* rdpdr)
677
{
678
  WINPR_ASSERT(rdpdr);
679
  UINT error = ERROR_CALL_NOT_IMPLEMENTED;
680
  if (rdpdr->context.RdpdrHotplugDevice)
681
    error = rdpdr->context.RdpdrHotplugDevice(&rdpdr->context, RDPDR_HOTPLUG_FIRST_CHECK);
682
683
  switch (error)
684
  {
685
    case ERROR_DISK_CHANGE:
686
    case CHANNEL_RC_OK:
687
    case ERROR_CALL_NOT_IMPLEMENTED:
688
      break;
689
    default:
690
      WLog_Print(rdpdr->log, WLOG_ERROR, "handle_hotplug failed with error %" PRIu32 "!",
691
                 error);
692
      break;
693
  }
694
}
695
696
static DWORD WINAPI drive_hotplug_thread_func(LPVOID arg)
697
{
698
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)arg;
699
  WINPR_ASSERT(rdpdr);
700
  WINPR_ASSERT(rdpdr->stopEvent);
701
702
  CFStringRef path = CFSTR("/Volumes/");
703
  CFArrayRef pathsToWatch = CFArrayCreate(kCFAllocatorMalloc, (const void**)&path, 1, nullptr);
704
  FSEventStreamContext ctx = {
705
    .copyDescription = nullptr, .info = arg, .release = nullptr, .retain = nullptr, .version = 0
706
  };
707
  FSEventStreamRef fsev =
708
      FSEventStreamCreate(kCFAllocatorMalloc, drive_hotplug_fsevent_callback, &ctx, pathsToWatch,
709
                          kFSEventStreamEventIdSinceNow, 1, kFSEventStreamCreateFlagNone);
710
711
  dispatch_queue_t queue = dispatch_queue_create(TAG, nullptr);
712
  FSEventStreamSetDispatchQueue(fsev, queue);
713
  FSEventStreamStart(fsev);
714
  WLog_Print(rdpdr->log, WLOG_DEBUG, "Started hotplug watcher");
715
  HANDLE handles[] = { rdpdr->stopEvent, freerdp_abort_event(rdpdr->rdpcontext) };
716
  const DWORD status = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
717
  WLog_Print(rdpdr->log, WLOG_DEBUG, "Stopped hotplug watcher");
718
  FSEventStreamStop(fsev);
719
  FSEventStreamRelease(fsev);
720
  dispatch_release(queue);
721
722
  UINT error = CHANNEL_RC_OK;
723
  if (status > WAIT_OBJECT_0 + ARRAYSIZE(handles))
724
    error = ERROR_INTERNAL_ERROR;
725
  ExitThread(error);
726
  return error;
727
}
728
729
#else
730
731
static const char* automountLocations[] = { "/run/user/%lu/gvfs", "/run/media/%s", "/media/%s",
732
                                          "/media", "/mnt" };
733
734
static BOOL isAutomountLocation(const char* path)
735
0
{
736
0
  const size_t nrLocations = sizeof(automountLocations) / sizeof(automountLocations[0]);
737
0
  char buffer[MAX_PATH] = WINPR_C_ARRAY_INIT;
738
0
  uid_t uid = getuid();
739
0
  char uname[MAX_PATH] = WINPR_C_ARRAY_INIT;
740
0
  ULONG size = sizeof(uname) - 1;
741
742
0
  if (!GetUserNameExA(NameSamCompatible, uname, &size))
743
0
    return FALSE;
744
745
0
  if (!path)
746
0
    return FALSE;
747
748
0
  for (size_t x = 0; x < nrLocations; x++)
749
0
  {
750
0
    const char* location = automountLocations[x];
751
0
    size_t length = 0;
752
753
0
    WINPR_PRAGMA_DIAG_PUSH
754
0
    WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
755
0
    if (strstr(location, "%lu"))
756
0
      (void)snprintf(buffer, sizeof(buffer), location, (unsigned long)uid);
757
0
    else if (strstr(location, "%s"))
758
0
      (void)snprintf(buffer, sizeof(buffer), location, uname);
759
0
    else
760
0
      (void)snprintf(buffer, sizeof(buffer), "%s", location);
761
0
    WINPR_PRAGMA_DIAG_POP
762
763
0
    length = strnlen(buffer, sizeof(buffer));
764
765
0
    if (strncmp(buffer, path, length) == 0)
766
0
    {
767
0
      const char* rest = &path[length];
768
769
      /* Only consider mount locations with max depth of 1 below the
770
       * base path or the base path itself. */
771
0
      if (*rest == '\0')
772
0
        return TRUE;
773
0
      else if (*rest == '/')
774
0
      {
775
0
        const char* token = strstr(&rest[1], "/");
776
777
0
        if (!token || (token[1] == '\0'))
778
0
          return TRUE;
779
0
      }
780
0
    }
781
0
  }
782
783
0
  return FALSE;
784
0
}
785
786
0
#define MAX_USB_DEVICES 100
787
788
typedef struct
789
{
790
  char* path;
791
  BOOL to_add;
792
} hotplug_dev;
793
794
static void handle_mountpoint(hotplug_dev* dev_array, size_t* size, const char* mountpoint)
795
0
{
796
0
  if (!mountpoint)
797
0
    return;
798
  /* copy hotpluged device mount point to the dev_array */
799
0
  if (isAutomountLocation(mountpoint) && (*size < MAX_USB_DEVICES))
800
0
  {
801
0
    dev_array[*size].path = _strdup(mountpoint);
802
0
    dev_array[*size].to_add = TRUE;
803
0
    (*size)++;
804
0
  }
805
0
}
806
807
#ifdef __sun
808
#include <sys/mnttab.h>
809
static UINT handle_platform_mounts_sun(wLog* log, hotplug_dev* dev_array, size_t* size)
810
{
811
  FILE* f;
812
  struct mnttab ent;
813
  f = winpr_fopen("/etc/mnttab", "r");
814
  if (f == nullptr)
815
  {
816
    WLog_Print(log, WLOG_ERROR, "fopen failed!");
817
    return ERROR_OPEN_FAILED;
818
  }
819
  while (getmntent(f, &ent) == 0)
820
  {
821
    handle_mountpoint(dev_array, size, ent.mnt_mountp);
822
  }
823
  fclose(f);
824
  return ERROR_SUCCESS;
825
}
826
#endif
827
828
#if defined(__FreeBSD__) || defined(__OpenBSD__)
829
#include <sys/mount.h>
830
static UINT handle_platform_mounts_bsd(wLog* log, hotplug_dev* dev_array, size_t* size)
831
{
832
  int mntsize;
833
  struct statfs* mntbuf = nullptr;
834
835
  mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
836
  if (!mntsize)
837
  {
838
    /* TODO: handle 'errno' */
839
    WLog_Print(log, WLOG_ERROR, "getmntinfo failed!");
840
    return ERROR_OPEN_FAILED;
841
  }
842
  for (size_t idx = 0; idx < (size_t)mntsize; idx++)
843
  {
844
    handle_mountpoint(dev_array, size, mntbuf[idx].f_mntonname);
845
  }
846
  free(mntbuf);
847
  return ERROR_SUCCESS;
848
}
849
#endif
850
851
#if defined(__LINUX__) || defined(__linux__)
852
#include <mntent.h>
853
static struct mntent* getmntent_x(FILE* f, struct mntent* buffer, char* pathbuffer,
854
                                  size_t pathbuffersize)
855
0
{
856
0
#if defined(FREERDP_HAVE_GETMNTENT_R)
857
0
  WINPR_ASSERT(pathbuffersize <= INT32_MAX);
858
0
  return getmntent_r(f, buffer, pathbuffer, (int)pathbuffersize);
859
#else
860
  (void)buffer;
861
  (void)pathbuffer;
862
  (void)pathbuffersize;
863
  return getmntent(f);
864
#endif
865
0
}
866
867
static UINT handle_platform_mounts_linux(wLog* log, hotplug_dev* dev_array, size_t* size)
868
0
{
869
0
  FILE* f = nullptr;
870
0
  struct mntent mnt = WINPR_C_ARRAY_INIT;
871
0
  char pathbuffer[PATH_MAX] = WINPR_C_ARRAY_INIT;
872
0
  struct mntent* ent = nullptr;
873
0
  f = winpr_fopen("/proc/mounts", "r");
874
0
  if (f == nullptr)
875
0
  {
876
0
    WLog_Print(log, WLOG_ERROR, "fopen failed!");
877
0
    return ERROR_OPEN_FAILED;
878
0
  }
879
0
  while ((ent = getmntent_x(f, &mnt, pathbuffer, sizeof(pathbuffer))) != nullptr)
880
0
  {
881
0
    handle_mountpoint(dev_array, size, ent->mnt_dir);
882
0
  }
883
0
  (void)fclose(f);
884
0
  return ERROR_SUCCESS;
885
0
}
886
#endif
887
888
static UINT handle_platform_mounts(wLog* log, hotplug_dev* dev_array, size_t* size)
889
0
{
890
#ifdef __sun
891
  return handle_platform_mounts_sun(log, dev_array, size);
892
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
893
  return handle_platform_mounts_bsd(log, dev_array, size);
894
#elif defined(__LINUX__) || defined(__linux__)
895
  return handle_platform_mounts_linux(log, dev_array, size);
896
0
#endif
897
0
  return ERROR_CALL_NOT_IMPLEMENTED;
898
0
}
899
900
static BOOL device_not_plugged(ULONG_PTR key, void* element, void* data)
901
0
{
902
0
  const WCHAR* path = (const WCHAR*)data;
903
0
  DEVICE_DRIVE_EXT* device_ext = (DEVICE_DRIVE_EXT*)element;
904
905
0
  WINPR_UNUSED(key);
906
0
  WINPR_ASSERT(path);
907
908
0
  if (!device_ext || (device_ext->device.type != RDPDR_DTYP_FILESYSTEM) || !device_ext->path)
909
0
    return TRUE;
910
0
  if (_wcscmp(device_ext->path, path) != 0)
911
0
    return TRUE;
912
0
  return FALSE;
913
0
}
914
915
static BOOL device_already_plugged(rdpdrPlugin* rdpdr, const hotplug_dev* device)
916
0
{
917
0
  BOOL rc = FALSE;
918
0
  WCHAR* path = nullptr;
919
920
0
  if (!rdpdr || !device)
921
0
    return TRUE;
922
0
  if (!device->to_add)
923
0
    return TRUE;
924
925
0
  WINPR_ASSERT(rdpdr->devman);
926
0
  WINPR_ASSERT(device->path);
927
928
0
  path = ConvertUtf8ToWCharAlloc(device->path, nullptr);
929
0
  if (!path)
930
0
    return TRUE;
931
932
0
  rc = device_foreach(rdpdr, TRUE, device_not_plugged, path);
933
0
  free(path);
934
0
  return !rc;
935
0
}
936
937
struct hotplug_delete_arg
938
{
939
  hotplug_dev* dev_array;
940
  size_t dev_array_size;
941
  rdpdrPlugin* rdpdr;
942
};
943
944
static BOOL hotplug_delete_foreach(ULONG_PTR key, void* element, void* data)
945
0
{
946
0
  char* path = nullptr;
947
0
  BOOL dev_found = FALSE;
948
0
  struct hotplug_delete_arg* arg = (struct hotplug_delete_arg*)data;
949
0
  DEVICE_DRIVE_EXT* device_ext = (DEVICE_DRIVE_EXT*)element;
950
951
0
  WINPR_ASSERT(arg);
952
0
  WINPR_ASSERT(arg->rdpdr);
953
0
  WINPR_ASSERT(arg->dev_array || (arg->dev_array_size == 0));
954
0
  WINPR_ASSERT(key <= UINT32_MAX);
955
956
0
  if (!device_ext || (device_ext->device.type != RDPDR_DTYP_FILESYSTEM) || !device_ext->path ||
957
0
      !device_ext->automount)
958
0
    return TRUE;
959
960
0
  WINPR_ASSERT(device_ext->path);
961
0
  path = ConvertWCharToUtf8Alloc(device_ext->path, nullptr);
962
0
  if (!path)
963
0
    return FALSE;
964
965
  /* not pluggable device */
966
0
  if (isAutomountLocation(path))
967
0
  {
968
0
    for (size_t i = 0; i < arg->dev_array_size; i++)
969
0
    {
970
0
      hotplug_dev* cur = &arg->dev_array[i];
971
0
      if (cur->path && strstr(path, cur->path) != nullptr)
972
0
      {
973
0
        dev_found = TRUE;
974
0
        cur->to_add = FALSE;
975
0
        break;
976
0
      }
977
0
    }
978
0
  }
979
980
0
  free(path);
981
982
0
  if (!dev_found)
983
0
  {
984
0
    const UINT32 ids[1] = { (UINT32)key };
985
0
    WINPR_ASSERT(arg->rdpdr->context.RdpdrUnregisterDevice);
986
0
    const UINT error =
987
0
        arg->rdpdr->context.RdpdrUnregisterDevice(&arg->rdpdr->context, ARRAYSIZE(ids), ids);
988
989
0
    if (error)
990
0
    {
991
0
      WLog_Print(arg->rdpdr->log, WLOG_ERROR,
992
0
                 "rdpdr_send_device_list_remove_request failed with error %" PRIu32 "!",
993
0
                 error);
994
0
      return FALSE;
995
0
    }
996
0
  }
997
998
0
  return TRUE;
999
0
}
1000
1001
static UINT handle_hotplug(RdpdrClientContext* context,
1002
                           WINPR_ATTR_UNUSED RdpdrHotplugEventType type)
1003
0
{
1004
0
  WINPR_ASSERT(context);
1005
0
  rdpdrPlugin* rdpdr = context->handle;
1006
1007
0
  hotplug_dev dev_array[MAX_USB_DEVICES] = WINPR_C_ARRAY_INIT;
1008
0
  size_t size = 0;
1009
0
  UINT error = ERROR_SUCCESS;
1010
0
  struct hotplug_delete_arg arg = { dev_array, ARRAYSIZE(dev_array), rdpdr };
1011
1012
0
  WINPR_ASSERT(rdpdr);
1013
0
  WINPR_ASSERT(rdpdr->devman);
1014
1015
0
  error = handle_platform_mounts(rdpdr->log, dev_array, &size);
1016
1017
  /* delete removed devices */
1018
0
  /* Ignore result */ device_foreach(rdpdr, FALSE, hotplug_delete_foreach, &arg);
1019
1020
  /* add new devices */
1021
0
  for (size_t i = 0; i < size; i++)
1022
0
  {
1023
0
    hotplug_dev* cur = &dev_array[i];
1024
0
    if (!device_already_plugged(rdpdr, cur))
1025
0
    {
1026
0
      const char* path = cur->path;
1027
0
      const char* name = strrchr(path, '/') + 1;
1028
1029
0
      rdpdr_load_drive(rdpdr, name, path, TRUE);
1030
0
      error = ERROR_DISK_CHANGE;
1031
0
    }
1032
0
  }
1033
1034
0
  for (size_t i = 0; i < size; i++)
1035
0
    free(dev_array[i].path);
1036
1037
0
  return error;
1038
0
}
1039
1040
static void first_hotplug(rdpdrPlugin* rdpdr)
1041
0
{
1042
0
  UINT error = ERROR_CALL_NOT_IMPLEMENTED;
1043
1044
0
  WINPR_ASSERT(rdpdr);
1045
0
  if (rdpdr->context.RdpdrHotplugDevice)
1046
0
    error = rdpdr->context.RdpdrHotplugDevice(&rdpdr->context, RDPDR_HOTPLUG_FIRST_CHECK);
1047
1048
0
  switch (error)
1049
0
  {
1050
0
    case ERROR_DISK_CHANGE:
1051
0
    case CHANNEL_RC_OK:
1052
0
    case ERROR_OPEN_FAILED:
1053
0
    case ERROR_CALL_NOT_IMPLEMENTED:
1054
0
      break;
1055
0
    default:
1056
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "handle_hotplug failed with error %" PRIu32 "!",
1057
0
                 error);
1058
0
      break;
1059
0
  }
1060
0
}
1061
1062
static DWORD WINAPI drive_hotplug_thread_func(LPVOID arg)
1063
0
{
1064
0
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)arg;
1065
1066
0
  WINPR_ASSERT(rdpdr);
1067
0
  WINPR_ASSERT(rdpdr->stopEvent);
1068
1069
0
  while (WaitForSingleObject(rdpdr->stopEvent, 1000) == WAIT_TIMEOUT)
1070
0
  {
1071
0
    UINT error = ERROR_CALL_NOT_IMPLEMENTED;
1072
0
    if (rdpdr->context.RdpdrHotplugDevice)
1073
0
      error =
1074
0
          rdpdr->context.RdpdrHotplugDevice(&rdpdr->context, RDPDR_HOTPLUG_CHECK_FOR_CHANGES);
1075
0
    switch (error)
1076
0
    {
1077
0
      case ERROR_DISK_CHANGE:
1078
0
        break;
1079
0
      case CHANNEL_RC_OK:
1080
0
      case ERROR_OPEN_FAILED:
1081
0
      case ERROR_CALL_NOT_IMPLEMENTED:
1082
0
        break;
1083
0
      default:
1084
0
        WLog_Print(rdpdr->log, WLOG_ERROR, "handle_hotplug failed with error %" PRIu32 "!",
1085
0
                   error);
1086
0
        goto out;
1087
0
    }
1088
0
  }
1089
1090
0
out:
1091
0
{
1092
0
  const UINT error = GetLastError();
1093
0
  if (error && rdpdr->rdpcontext)
1094
0
    setChannelError(rdpdr->rdpcontext, error, "reported an error");
1095
1096
0
  ExitThread(error);
1097
0
  return error;
1098
0
}
1099
0
}
1100
1101
#endif
1102
1103
#if !defined(_WIN32) && !defined(__IOS__)
1104
/**
1105
 * Function description
1106
 *
1107
 * @return 0 on success, otherwise a Win32 error code
1108
 */
1109
static UINT drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
1110
0
{
1111
0
  UINT error = 0;
1112
1113
0
  WINPR_ASSERT(rdpdr);
1114
1115
0
  if (rdpdr->hotplugThread)
1116
0
  {
1117
0
#if !defined(_WIN32)
1118
0
    if (rdpdr->stopEvent)
1119
0
      (void)SetEvent(rdpdr->stopEvent);
1120
0
#endif
1121
1122
0
    if (WaitForSingleObject(rdpdr->hotplugThread, INFINITE) == WAIT_FAILED)
1123
0
    {
1124
0
      error = GetLastError();
1125
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "!",
1126
0
                 error);
1127
0
      return error;
1128
0
    }
1129
1130
0
    (void)CloseHandle(rdpdr->hotplugThread);
1131
0
    rdpdr->hotplugThread = nullptr;
1132
0
  }
1133
1134
0
  return CHANNEL_RC_OK;
1135
0
}
1136
1137
#endif
1138
1139
static UINT rdpdr_add_devices(rdpdrPlugin* rdpdr)
1140
0
{
1141
0
  WINPR_ASSERT(rdpdr);
1142
0
  WINPR_ASSERT(rdpdr->rdpcontext);
1143
1144
0
  rdpSettings* settings = rdpdr->rdpcontext->settings;
1145
0
  WINPR_ASSERT(settings);
1146
1147
0
  for (UINT32 index = 0; index < freerdp_settings_get_uint32(settings, FreeRDP_DeviceCount);
1148
0
       index++)
1149
0
  {
1150
0
    RDPDR_DEVICE* device =
1151
0
        freerdp_settings_get_pointer_array_writable(settings, FreeRDP_DeviceArray, index);
1152
0
    WINPR_ASSERT(device);
1153
1154
0
    if (device->Type == RDPDR_DTYP_FILESYSTEM)
1155
0
    {
1156
0
      const char DynamicDrives[] = "DynamicDrives";
1157
0
      const RDPDR_DRIVE* drive = (const RDPDR_DRIVE*)device;
1158
0
      if (!drive->Path)
1159
0
        continue;
1160
1161
0
      const char wildcard[] = "*";
1162
0
      BOOL hotplugAll = strncmp(drive->Path, wildcard, sizeof(wildcard)) == 0;
1163
0
      BOOL hotplugLater = strncmp(drive->Path, DynamicDrives, sizeof(DynamicDrives)) == 0;
1164
1165
0
      if (hotplugAll || hotplugLater)
1166
0
      {
1167
0
        if (!rdpdr->async)
1168
0
        {
1169
0
          WLog_Print(rdpdr->log, WLOG_WARN,
1170
0
                     "Drive hotplug is not supported in synchronous mode!");
1171
0
          continue;
1172
0
        }
1173
1174
0
        if (hotplugAll)
1175
0
          first_hotplug(rdpdr);
1176
1177
        /* There might be multiple hotplug related device entries.
1178
         * Ensure the thread is only started once
1179
         */
1180
0
        if (!rdpdr->hotplugThread)
1181
0
        {
1182
0
          rdpdr->hotplugThread =
1183
0
              CreateThread(nullptr, 0, drive_hotplug_thread_func, rdpdr, 0, nullptr);
1184
0
          if (!rdpdr->hotplugThread)
1185
0
          {
1186
0
            WLog_Print(rdpdr->log, WLOG_ERROR, "CreateThread failed!");
1187
0
            return ERROR_INTERNAL_ERROR;
1188
0
          }
1189
0
        }
1190
1191
0
        continue;
1192
0
      }
1193
0
    }
1194
1195
0
    const UINT error = devman_load_device_service(rdpdr->devman, device, rdpdr->rdpcontext);
1196
0
    if (error)
1197
0
    {
1198
0
      WLog_Print(rdpdr->log, WLOG_ERROR,
1199
0
                 "devman_load_device_service failed with error %" PRIu32 "!", error);
1200
0
      return error;
1201
0
    }
1202
0
  }
1203
0
  return CHANNEL_RC_OK;
1204
0
}
1205
1206
/**
1207
 * Function description
1208
 *
1209
 * @return 0 on success, otherwise a Win32 error code
1210
 */
1211
static UINT rdpdr_process_connect(rdpdrPlugin* rdpdr)
1212
0
{
1213
0
  WINPR_ASSERT(rdpdr);
1214
1215
0
  rdpdr->devman = devman_new(rdpdr);
1216
1217
0
  if (!rdpdr->devman)
1218
0
  {
1219
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "devman_new failed!");
1220
0
    return CHANNEL_RC_NO_MEMORY;
1221
0
  }
1222
1223
0
  WINPR_ASSERT(rdpdr->rdpcontext);
1224
1225
0
  rdpSettings* settings = rdpdr->rdpcontext->settings;
1226
0
  WINPR_ASSERT(settings);
1227
1228
0
  rdpdr->ignoreInvalidDevices = freerdp_settings_get_bool(settings, FreeRDP_IgnoreInvalidDevices);
1229
1230
0
  const char* name = freerdp_settings_get_string(settings, FreeRDP_ClientHostname);
1231
0
  if (!name)
1232
0
    name = freerdp_settings_get_string(settings, FreeRDP_ComputerName);
1233
0
  if (!name)
1234
0
  {
1235
0
    DWORD size = ARRAYSIZE(rdpdr->computerName);
1236
0
    if (!GetComputerNameExA(ComputerNameNetBIOS, rdpdr->computerName, &size))
1237
0
      return ERROR_INTERNAL_ERROR;
1238
0
  }
1239
0
  else
1240
0
    strncpy(rdpdr->computerName, name, strnlen(name, sizeof(rdpdr->computerName)));
1241
1242
0
  return rdpdr_add_devices(rdpdr);
1243
0
}
1244
1245
static UINT rdpdr_process_server_announce_request(rdpdrPlugin* rdpdr, wStream* s)
1246
0
{
1247
0
  WINPR_ASSERT(rdpdr);
1248
0
  WINPR_ASSERT(s);
1249
1250
0
  if (!Stream_CheckAndLogRequiredLengthWLog(rdpdr->log, s, 8))
1251
0
    return ERROR_INVALID_DATA;
1252
1253
0
  Stream_Read_UINT16(s, rdpdr->serverVersionMajor);
1254
0
  Stream_Read_UINT16(s, rdpdr->serverVersionMinor);
1255
0
  Stream_Read_UINT32(s, rdpdr->clientID);
1256
0
  rdpdr->sequenceId++;
1257
1258
0
  rdpdr->clientVersionMajor = MIN(RDPDR_VERSION_MAJOR, rdpdr->serverVersionMajor);
1259
0
  rdpdr->clientVersionMinor = MIN(RDPDR_VERSION_MINOR_RDP10X, rdpdr->serverVersionMinor);
1260
0
  WLog_Print(rdpdr->log, WLOG_DEBUG,
1261
0
             "[rdpdr] server announces version %" PRIu32 ".%" PRIu32 ", client uses %" PRIu32
1262
0
             ".%" PRIu32,
1263
0
             rdpdr->serverVersionMajor, rdpdr->serverVersionMinor, rdpdr->clientVersionMajor,
1264
0
             rdpdr->clientVersionMinor);
1265
0
  return CHANNEL_RC_OK;
1266
0
}
1267
1268
/**
1269
 * Function description
1270
 *
1271
 * @return 0 on success, otherwise a Win32 error code
1272
 */
1273
static UINT rdpdr_send_client_announce_reply(rdpdrPlugin* rdpdr)
1274
0
{
1275
0
  WINPR_ASSERT(rdpdr);
1276
0
  WINPR_ASSERT(rdpdr->state == RDPDR_CHANNEL_STATE_ANNOUNCE);
1277
0
  if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY))
1278
0
    return ERROR_INVALID_STATE;
1279
1280
0
  wStream* s = StreamPool_Take(rdpdr->pool, 12);
1281
1282
0
  if (!s)
1283
0
  {
1284
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
1285
0
    return CHANNEL_RC_NO_MEMORY;
1286
0
  }
1287
1288
0
  Stream_Write_UINT16(s, RDPDR_CTYP_CORE);             /* Component (2 bytes) */
1289
0
  Stream_Write_UINT16(s, PAKID_CORE_CLIENTID_CONFIRM); /* PacketId (2 bytes) */
1290
0
  Stream_Write_UINT16(s, rdpdr->clientVersionMajor);
1291
0
  Stream_Write_UINT16(s, rdpdr->clientVersionMinor);
1292
0
  Stream_Write_UINT32(s, rdpdr->clientID);
1293
0
  return rdpdr_send(rdpdr, s);
1294
0
}
1295
1296
/**
1297
 * Function description
1298
 *
1299
 * @return 0 on success, otherwise a Win32 error code
1300
 */
1301
static UINT rdpdr_send_client_name_request(rdpdrPlugin* rdpdr)
1302
0
{
1303
0
  wStream* s = nullptr;
1304
0
  WCHAR* computerNameW = nullptr;
1305
0
  size_t computerNameLenW = 0;
1306
1307
0
  WINPR_ASSERT(rdpdr);
1308
0
  WINPR_ASSERT(rdpdr->state == RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY);
1309
0
  if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_NAME_REQUEST))
1310
0
    return ERROR_INVALID_STATE;
1311
1312
0
  const size_t len = strnlen(rdpdr->computerName, sizeof(rdpdr->computerName));
1313
0
  if (len == 0)
1314
0
    return ERROR_INTERNAL_ERROR;
1315
1316
0
  WINPR_ASSERT(rdpdr->computerName);
1317
0
  computerNameW = ConvertUtf8NToWCharAlloc(rdpdr->computerName, len, &computerNameLenW);
1318
0
  computerNameLenW *= sizeof(WCHAR);
1319
1320
0
  if (computerNameLenW > 0)
1321
0
    computerNameLenW += sizeof(WCHAR); // also write '\0'
1322
1323
0
  s = StreamPool_Take(rdpdr->pool, 16U + computerNameLenW);
1324
1325
0
  if (!s)
1326
0
  {
1327
0
    free(computerNameW);
1328
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
1329
0
    return CHANNEL_RC_NO_MEMORY;
1330
0
  }
1331
1332
0
  Stream_Write_UINT16(s, RDPDR_CTYP_CORE);        /* Component (2 bytes) */
1333
0
  Stream_Write_UINT16(s, PAKID_CORE_CLIENT_NAME); /* PacketId (2 bytes) */
1334
0
  Stream_Write_UINT32(s, 1);                      /* unicodeFlag, 0 for ASCII and 1 for Unicode */
1335
0
  Stream_Write_UINT32(s, 0);                      /* codePage, must be set to zero */
1336
0
  Stream_Write_UINT32(s,
1337
0
                      (UINT32)computerNameLenW); /* computerNameLen, including null terminator */
1338
0
  Stream_Write(s, computerNameW, computerNameLenW);
1339
0
  free(computerNameW);
1340
0
  return rdpdr_send(rdpdr, s);
1341
0
}
1342
1343
static UINT rdpdr_process_server_clientid_confirm(rdpdrPlugin* rdpdr, wStream* s)
1344
0
{
1345
0
  UINT16 versionMajor = 0;
1346
0
  UINT16 versionMinor = 0;
1347
0
  UINT32 clientID = 0;
1348
1349
0
  WINPR_ASSERT(rdpdr);
1350
0
  WINPR_ASSERT(s);
1351
1352
0
  if (!Stream_CheckAndLogRequiredLengthWLog(rdpdr->log, s, 8))
1353
0
    return ERROR_INVALID_DATA;
1354
1355
0
  Stream_Read_UINT16(s, versionMajor);
1356
0
  Stream_Read_UINT16(s, versionMinor);
1357
0
  Stream_Read_UINT32(s, clientID);
1358
1359
0
  if (versionMajor != rdpdr->clientVersionMajor || versionMinor != rdpdr->clientVersionMinor)
1360
0
  {
1361
0
    WLog_Print(rdpdr->log, WLOG_WARN,
1362
0
               "[rdpdr] server announced version %" PRIu32 ".%" PRIu32 ", client uses %" PRIu32
1363
0
               ".%" PRIu32 " but clientid confirm requests version %" PRIu32 ".%" PRIu32,
1364
0
               rdpdr->serverVersionMajor, rdpdr->serverVersionMinor, rdpdr->clientVersionMajor,
1365
0
               rdpdr->clientVersionMinor, versionMajor, versionMinor);
1366
0
    rdpdr->clientVersionMajor = versionMajor;
1367
0
    rdpdr->clientVersionMinor = versionMinor;
1368
0
  }
1369
1370
0
  if (clientID != rdpdr->clientID)
1371
0
    rdpdr->clientID = clientID;
1372
1373
0
  return CHANNEL_RC_OK;
1374
0
}
1375
1376
struct device_announce_arg
1377
{
1378
  rdpdrPlugin* rdpdr;
1379
  wStream* s;
1380
  BOOL userLoggedOn;
1381
  UINT32 count;
1382
};
1383
1384
static BOOL device_announce(ULONG_PTR key, void* element, void* data)
1385
0
{
1386
0
  struct device_announce_arg* arg = data;
1387
0
  rdpdrPlugin* rdpdr = nullptr;
1388
0
  DEVICE* device = (DEVICE*)element;
1389
1390
0
  WINPR_UNUSED(key);
1391
1392
0
  WINPR_ASSERT(arg);
1393
0
  WINPR_ASSERT(device);
1394
0
  WINPR_ASSERT(arg->rdpdr);
1395
0
  WINPR_ASSERT(arg->s);
1396
1397
0
  rdpdr = arg->rdpdr;
1398
1399
  /**
1400
   * 1. versionMinor 0x0005 doesn't send PAKID_CORE_USER_LOGGEDON
1401
   *    so all devices should be sent regardless of user_loggedon
1402
   * 2. smartcard devices should be always sent
1403
   * 3. other devices are sent only after user_loggedon
1404
   */
1405
1406
0
  if ((rdpdr->clientVersionMinor == RDPDR_VERSION_MINOR_RDP51) ||
1407
0
      (device->type == RDPDR_DTYP_SMARTCARD) || arg->userLoggedOn)
1408
0
  {
1409
0
    size_t data_len = (device->data == nullptr ? 0 : Stream_GetPosition(device->data));
1410
1411
0
    if (!Stream_EnsureRemainingCapacity(arg->s, 20 + data_len))
1412
0
    {
1413
0
      Stream_Release(arg->s);
1414
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
1415
0
      return FALSE;
1416
0
    }
1417
1418
0
    Stream_Write_UINT32(arg->s, device->type); /* deviceType */
1419
0
    Stream_Write_UINT32(arg->s, device->id);   /* deviceID */
1420
0
    strncpy(Stream_Pointer(arg->s), device->name, 8);
1421
1422
0
    for (size_t i = 0; i < 8; i++)
1423
0
    {
1424
0
      BYTE c = 0;
1425
0
      Stream_Peek_UINT8(arg->s, c);
1426
1427
0
      if (c > 0x7F)
1428
0
        Stream_Write_UINT8(arg->s, '_');
1429
0
      else
1430
0
        Stream_Seek_UINT8(arg->s);
1431
0
    }
1432
1433
0
    WINPR_ASSERT(data_len <= UINT32_MAX);
1434
0
    Stream_Write_UINT32(arg->s, (UINT32)data_len);
1435
1436
0
    if (data_len > 0)
1437
0
      Stream_Write(arg->s, Stream_Buffer(device->data), data_len);
1438
1439
0
    arg->count++;
1440
0
    WLog_Print(rdpdr->log, WLOG_INFO,
1441
0
               "registered [%9s] device #%" PRIu32 ": %5s (type=%2" PRIu32 " id=%2" PRIu32 ")",
1442
0
               rdpdr_device_type_string(device->type), arg->count, device->name, device->type,
1443
0
               device->id);
1444
0
  }
1445
0
  return TRUE;
1446
0
}
1447
1448
static UINT rdpdr_send_device_list_announce_request(rdpdrPlugin* rdpdr, BOOL userLoggedOn)
1449
0
{
1450
0
  size_t pos = 0;
1451
0
  wStream* s = nullptr;
1452
0
  size_t count_pos = 0;
1453
0
  struct device_announce_arg arg = WINPR_C_ARRAY_INIT;
1454
1455
0
  WINPR_ASSERT(rdpdr);
1456
0
  WINPR_ASSERT(rdpdr->devman);
1457
1458
0
  if (userLoggedOn)
1459
0
  {
1460
0
    rdpdr->userLoggedOn = TRUE;
1461
0
  }
1462
1463
0
  s = StreamPool_Take(rdpdr->pool, 256);
1464
1465
0
  if (!s)
1466
0
  {
1467
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
1468
0
    return CHANNEL_RC_NO_MEMORY;
1469
0
  }
1470
1471
0
  Stream_Write_UINT16(s, RDPDR_CTYP_CORE);                /* Component (2 bytes) */
1472
0
  Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_ANNOUNCE); /* PacketId (2 bytes) */
1473
0
  count_pos = Stream_GetPosition(s);
1474
0
  Stream_Seek_UINT32(s); /* deviceCount */
1475
1476
0
  arg.rdpdr = rdpdr;
1477
0
  arg.userLoggedOn = userLoggedOn;
1478
0
  arg.s = s;
1479
0
  if (!device_foreach(rdpdr, TRUE, device_announce, &arg))
1480
0
    return ERROR_INVALID_DATA;
1481
1482
0
  if (arg.count == 0)
1483
0
  {
1484
0
    Stream_Release(s);
1485
0
    return CHANNEL_RC_OK;
1486
0
  }
1487
0
  pos = Stream_GetPosition(s);
1488
0
  if (!Stream_SetPosition(s, count_pos))
1489
0
  {
1490
0
    Stream_Release(s);
1491
0
    return ERROR_INVALID_DATA;
1492
0
  }
1493
0
  Stream_Write_UINT32(s, arg.count);
1494
0
  if (!Stream_SetPosition(s, pos))
1495
0
  {
1496
0
    Stream_Release(s);
1497
0
    return ERROR_INVALID_DATA;
1498
0
  }
1499
0
  Stream_SealLength(s);
1500
0
  return rdpdr_send(rdpdr, s);
1501
0
}
1502
1503
UINT rdpdr_try_send_device_list_announce_request(rdpdrPlugin* rdpdr)
1504
0
{
1505
0
  WINPR_ASSERT(rdpdr);
1506
0
  if (rdpdr->state != RDPDR_CHANNEL_STATE_READY)
1507
0
  {
1508
0
    WLog_Print(rdpdr->log, WLOG_DEBUG,
1509
0
               "hotplug event received, but channel [RDPDR] is not ready (state %s), ignoring.",
1510
0
               rdpdr_state_str(rdpdr->state));
1511
0
    return CHANNEL_RC_OK;
1512
0
  }
1513
0
  return rdpdr_send_device_list_announce_request(rdpdr, rdpdr->userLoggedOn);
1514
0
}
1515
1516
static UINT dummy_irp_response(rdpdrPlugin* rdpdr, wStream* s)
1517
0
{
1518
0
  WINPR_ASSERT(rdpdr);
1519
0
  WINPR_ASSERT(s);
1520
1521
0
  wStream* output = StreamPool_Take(rdpdr->pool, 256); // RDPDR_DEVICE_IO_RESPONSE_LENGTH
1522
0
  if (!output)
1523
0
  {
1524
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
1525
0
    return CHANNEL_RC_NO_MEMORY;
1526
0
  }
1527
1528
0
  if (!Stream_SetPosition(s, 4)) /* see "rdpdr_process_receive" */
1529
0
  {
1530
0
    Stream_Release(output);
1531
0
    return ERROR_INVALID_DATA;
1532
0
  }
1533
1534
0
  const uint32_t DeviceId = Stream_Get_UINT32(s);     /* DeviceId (4 bytes) */
1535
0
  const uint32_t FileId = Stream_Get_UINT32(s);       /* FileId (4 bytes) */
1536
0
  const uint32_t CompletionId = Stream_Get_UINT32(s); /* CompletionId (4 bytes) */
1537
1538
0
  WLog_Print(rdpdr->log, WLOG_WARN,
1539
0
             "Dummy response {DeviceId=%" PRIu32 ", FileId=%" PRIu32 ", CompletionId=%" PRIu32
1540
0
             "}",
1541
0
             DeviceId, FileId, CompletionId);
1542
0
  if (!rdpdr_write_iocompletion_header(output, DeviceId, CompletionId, STATUS_UNSUCCESSFUL))
1543
0
    return CHANNEL_RC_NO_MEMORY;
1544
1545
0
  return rdpdr_send(rdpdr, output);
1546
0
}
1547
1548
/**
1549
 * Function description
1550
 *
1551
 * @return 0 on success, otherwise a Win32 error code
1552
 */
1553
static UINT rdpdr_process_irp(rdpdrPlugin* rdpdr, wStream* s)
1554
0
{
1555
0
  UINT error = CHANNEL_RC_OK;
1556
1557
0
  WINPR_ASSERT(rdpdr);
1558
0
  WINPR_ASSERT(s);
1559
1560
0
  IRP* irp = irp_new(rdpdr->devman, rdpdr->pool, s, rdpdr->log, &error);
1561
1562
0
  if (!irp)
1563
0
  {
1564
0
    if ((error == CHANNEL_RC_OK) ||
1565
0
        (error == ERROR_DEV_NOT_EXIST && rdpdr->ignoreInvalidDevices))
1566
0
    {
1567
0
      return dummy_irp_response(rdpdr, s);
1568
0
    }
1569
1570
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "irp_new failed with %" PRIu32 "!", error);
1571
0
    return error;
1572
0
  }
1573
1574
0
  if (irp->device->IRPRequest)
1575
0
    error = irp->device->IRPRequest(irp->device, irp);
1576
0
  else
1577
0
    error = irp->Discard(irp);
1578
1579
0
  if (error != CHANNEL_RC_OK)
1580
0
  {
1581
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "device->IRPRequest failed with error %" PRIu32 "",
1582
0
               error);
1583
0
  }
1584
1585
0
  return error;
1586
0
}
1587
1588
static UINT rdpdr_process_component(rdpdrPlugin* rdpdr, UINT16 component, UINT16 packetId,
1589
                                    wStream* s)
1590
0
{
1591
0
  UINT32 type = 0;
1592
0
  DEVICE* device = nullptr;
1593
1594
0
  WINPR_ASSERT(rdpdr);
1595
0
  WINPR_ASSERT(s);
1596
1597
0
  switch (component)
1598
0
  {
1599
0
    case RDPDR_CTYP_PRN:
1600
0
      type = RDPDR_DTYP_PRINT;
1601
0
      break;
1602
1603
0
    default:
1604
0
      return ERROR_INVALID_DATA;
1605
0
  }
1606
1607
0
  device = devman_get_device_by_type(rdpdr->devman, type);
1608
1609
0
  if (!device)
1610
0
    return ERROR_DEV_NOT_EXIST;
1611
1612
0
  return IFCALLRESULT(ERROR_INVALID_PARAMETER, device->CustomComponentRequest, device, component,
1613
0
                      packetId, s);
1614
0
}
1615
1616
/**
1617
 * Function description
1618
 *
1619
 * @return 0 on success, otherwise a Win32 error code
1620
 */
1621
static BOOL device_init(ULONG_PTR key, void* element, void* data)
1622
0
{
1623
0
  wLog* log = data;
1624
0
  UINT error = CHANNEL_RC_OK;
1625
0
  DEVICE* device = element;
1626
1627
0
  WINPR_UNUSED(key);
1628
0
  WINPR_UNUSED(data);
1629
1630
0
  IFCALLRET(device->Init, error, device);
1631
1632
0
  if (error != CHANNEL_RC_OK)
1633
0
  {
1634
0
    WLog_Print(log, WLOG_ERROR, "Device init failed with %s", WTSErrorToString(error));
1635
0
    return FALSE;
1636
0
  }
1637
0
  return TRUE;
1638
0
}
1639
1640
static UINT rdpdr_process_init(rdpdrPlugin* rdpdr)
1641
0
{
1642
0
  WINPR_ASSERT(rdpdr);
1643
0
  WINPR_ASSERT(rdpdr->devman);
1644
1645
0
  rdpdr->userLoggedOn = FALSE; /* reset possible received state */
1646
0
  if (!device_foreach(rdpdr, TRUE, device_init, rdpdr->log))
1647
0
    return ERROR_INTERNAL_ERROR;
1648
0
  return CHANNEL_RC_OK;
1649
0
}
1650
1651
static BOOL state_match(enum RDPDR_CHANNEL_STATE state, size_t count, va_list ap)
1652
0
{
1653
0
  for (size_t x = 0; x < count; x++)
1654
0
  {
1655
0
    enum RDPDR_CHANNEL_STATE cur = va_arg(ap, enum RDPDR_CHANNEL_STATE);
1656
0
    if (state == cur)
1657
0
      return TRUE;
1658
0
  }
1659
0
  return FALSE;
1660
0
}
1661
1662
static const char* state_str(size_t count, va_list ap, char* buffer, size_t size)
1663
0
{
1664
0
  for (size_t x = 0; x < count; x++)
1665
0
  {
1666
0
    enum RDPDR_CHANNEL_STATE cur = va_arg(ap, enum RDPDR_CHANNEL_STATE);
1667
0
    const char* curstr = rdpdr_state_str(cur);
1668
0
    winpr_str_append(curstr, buffer, size, "|");
1669
0
  }
1670
0
  return buffer;
1671
0
}
1672
1673
static BOOL rdpdr_state_check(rdpdrPlugin* rdpdr, UINT16 packetid, enum RDPDR_CHANNEL_STATE next,
1674
                              size_t count, ...)
1675
0
{
1676
0
  va_list ap = WINPR_C_ARRAY_INIT;
1677
0
  WINPR_ASSERT(rdpdr);
1678
1679
0
  va_start(ap, count);
1680
0
  BOOL rc = state_match(rdpdr->state, count, ap);
1681
0
  va_end(ap);
1682
1683
0
  if (!rc)
1684
0
  {
1685
0
    const char* strstate = rdpdr_state_str(rdpdr->state);
1686
0
    char buffer[256] = WINPR_C_ARRAY_INIT;
1687
1688
0
    va_start(ap, count);
1689
0
    state_str(count, ap, buffer, sizeof(buffer));
1690
0
    va_end(ap);
1691
1692
0
    WLog_Print(rdpdr->log, WLOG_ERROR,
1693
0
               "channel [RDPDR] received %s, expected states [%s] but have state %s, aborting.",
1694
0
               rdpdr_packetid_string(packetid), buffer, strstate);
1695
1696
0
    if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_INITIAL))
1697
0
      return FALSE;
1698
0
    return FALSE;
1699
0
  }
1700
0
  return rdpdr_state_advance(rdpdr, next);
1701
0
}
1702
1703
static BOOL rdpdr_check_channel_state(rdpdrPlugin* rdpdr, UINT16 packetid)
1704
0
{
1705
0
  WINPR_ASSERT(rdpdr);
1706
1707
0
  switch (packetid)
1708
0
  {
1709
0
    case PAKID_CORE_SERVER_ANNOUNCE:
1710
      /* windows servers sometimes send this message.
1711
       * it seems related to session login (e.g. first initialization for RDP/TLS style login,
1712
       * then reinitialize the channel after login successful
1713
       */
1714
0
      if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_INITIAL))
1715
0
        return FALSE;
1716
0
      return rdpdr_state_check(rdpdr, packetid, RDPDR_CHANNEL_STATE_ANNOUNCE, 1,
1717
0
                               RDPDR_CHANNEL_STATE_INITIAL);
1718
0
    case PAKID_CORE_SERVER_CAPABILITY:
1719
0
      return rdpdr_state_check(
1720
0
          rdpdr, packetid, RDPDR_CHANNEL_STATE_SERVER_CAPS, 6,
1721
0
          RDPDR_CHANNEL_STATE_NAME_REQUEST, RDPDR_CHANNEL_STATE_SERVER_CAPS,
1722
0
          RDPDR_CHANNEL_STATE_READY, RDPDR_CHANNEL_STATE_CLIENT_CAPS,
1723
0
          RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM, RDPDR_CHANNEL_STATE_USER_LOGGEDON);
1724
0
    case PAKID_CORE_CLIENTID_CONFIRM:
1725
0
      return rdpdr_state_check(rdpdr, packetid, RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM, 5,
1726
0
                               RDPDR_CHANNEL_STATE_NAME_REQUEST,
1727
0
                               RDPDR_CHANNEL_STATE_SERVER_CAPS,
1728
0
                               RDPDR_CHANNEL_STATE_CLIENT_CAPS, RDPDR_CHANNEL_STATE_READY,
1729
0
                               RDPDR_CHANNEL_STATE_USER_LOGGEDON);
1730
0
    case PAKID_CORE_USER_LOGGEDON:
1731
0
      if (!rdpdr_check_extended_pdu_flag(rdpdr, RDPDR_USER_LOGGEDON_PDU))
1732
0
        return FALSE;
1733
1734
0
      return rdpdr_state_check(
1735
0
          rdpdr, packetid, RDPDR_CHANNEL_STATE_USER_LOGGEDON, 4,
1736
0
          RDPDR_CHANNEL_STATE_NAME_REQUEST, RDPDR_CHANNEL_STATE_CLIENT_CAPS,
1737
0
          RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM, RDPDR_CHANNEL_STATE_READY);
1738
0
    default:
1739
0
    {
1740
0
      enum RDPDR_CHANNEL_STATE state = RDPDR_CHANNEL_STATE_READY;
1741
0
      return rdpdr_state_check(rdpdr, packetid, state, 1, state);
1742
0
    }
1743
0
  }
1744
0
}
1745
1746
static BOOL tryAdvance(rdpdrPlugin* rdpdr)
1747
0
{
1748
0
  if (rdpdr->haveClientId && rdpdr->haveServerCaps)
1749
0
  {
1750
0
    const UINT error = rdpdr_send_device_list_announce_request(rdpdr, FALSE);
1751
0
    if (error)
1752
0
    {
1753
0
      WLog_Print(rdpdr->log, WLOG_ERROR,
1754
0
                 "rdpdr_send_device_list_announce_request failed with error %" PRIu32 "",
1755
0
                 error);
1756
0
      return FALSE;
1757
0
    }
1758
0
    if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_READY))
1759
0
      return FALSE;
1760
0
  }
1761
0
  return TRUE;
1762
0
}
1763
1764
/**
1765
 * Function description
1766
 *
1767
 * @return 0 on success, otherwise a Win32 error code
1768
 */
1769
static UINT rdpdr_process_receive(rdpdrPlugin* rdpdr, wStream* s)
1770
0
{
1771
0
  UINT16 component = 0;
1772
0
  UINT16 packetId = 0;
1773
0
  UINT32 deviceId = 0;
1774
0
  UINT32 status = 0;
1775
0
  UINT error = ERROR_INVALID_DATA;
1776
1777
0
  if (!rdpdr || !s)
1778
0
    return CHANNEL_RC_NULL_DATA;
1779
1780
0
  rdpdr_dump_received_packet(rdpdr->log, WLOG_TRACE, s, "[rdpdr-channel] receive");
1781
0
  if (Stream_GetRemainingLength(s) >= 4)
1782
0
  {
1783
0
    Stream_Read_UINT16(s, component); /* Component (2 bytes) */
1784
0
    Stream_Read_UINT16(s, packetId);  /* PacketId (2 bytes) */
1785
1786
0
    if (component == RDPDR_CTYP_CORE)
1787
0
    {
1788
0
      if (!rdpdr_check_channel_state(rdpdr, packetId))
1789
0
        return CHANNEL_RC_OK;
1790
1791
0
      switch (packetId)
1792
0
      {
1793
0
        case PAKID_CORE_SERVER_ANNOUNCE:
1794
0
          rdpdr->haveClientId = FALSE;
1795
0
          rdpdr->haveServerCaps = FALSE;
1796
0
          if ((error = rdpdr_process_server_announce_request(rdpdr, s)))
1797
0
          {
1798
0
          }
1799
0
          else if ((error = rdpdr_send_client_announce_reply(rdpdr)))
1800
0
          {
1801
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1802
0
                       "rdpdr_send_client_announce_reply failed with error %" PRIu32 "",
1803
0
                       error);
1804
0
          }
1805
0
          else if ((error = rdpdr_send_client_name_request(rdpdr)))
1806
0
          {
1807
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1808
0
                       "rdpdr_send_client_name_request failed with error %" PRIu32 "",
1809
0
                       error);
1810
0
          }
1811
0
          else if ((error = rdpdr_process_init(rdpdr)))
1812
0
          {
1813
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1814
0
                       "rdpdr_process_init failed with error %" PRIu32 "", error);
1815
0
          }
1816
1817
0
          break;
1818
1819
0
        case PAKID_CORE_SERVER_CAPABILITY:
1820
0
          if ((error = rdpdr_process_capability_request(rdpdr, s)))
1821
0
          {
1822
0
          }
1823
0
          else if ((error = rdpdr_send_capability_response(rdpdr)))
1824
0
          {
1825
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1826
0
                       "rdpdr_send_capability_response failed with error %" PRIu32 "",
1827
0
                       error);
1828
0
          }
1829
0
          else
1830
0
          {
1831
0
            rdpdr->haveServerCaps = TRUE;
1832
0
            if (!tryAdvance(rdpdr))
1833
0
              error = ERROR_INTERNAL_ERROR;
1834
0
          }
1835
1836
0
          break;
1837
1838
0
        case PAKID_CORE_CLIENTID_CONFIRM:
1839
0
          if ((error = rdpdr_process_server_clientid_confirm(rdpdr, s)))
1840
0
          {
1841
0
          }
1842
0
          else
1843
0
          {
1844
0
            rdpdr->haveClientId = TRUE;
1845
0
            if (!tryAdvance(rdpdr))
1846
0
              error = ERROR_INTERNAL_ERROR;
1847
0
          }
1848
0
          break;
1849
1850
0
        case PAKID_CORE_USER_LOGGEDON:
1851
0
          if (!rdpdr->haveServerCaps)
1852
0
          {
1853
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1854
0
                       "Wrong state %s for %s. [serverCaps=%d, clientId=%d]",
1855
0
                       rdpdr_state_str(rdpdr->state), rdpdr_packetid_string(packetId),
1856
0
                       rdpdr->haveServerCaps, rdpdr->haveClientId);
1857
0
            error = ERROR_INTERNAL_ERROR;
1858
0
          }
1859
0
          else if ((error = rdpdr_send_device_list_announce_request(rdpdr, TRUE)))
1860
0
          {
1861
0
            WLog_Print(
1862
0
                rdpdr->log, WLOG_ERROR,
1863
0
                "rdpdr_send_device_list_announce_request failed with error %" PRIu32 "",
1864
0
                error);
1865
0
          }
1866
0
          else if (!tryAdvance(rdpdr))
1867
0
          {
1868
0
            error = ERROR_INTERNAL_ERROR;
1869
0
          }
1870
1871
0
          break;
1872
1873
0
        case PAKID_CORE_DEVICE_REPLY:
1874
1875
          /* connect to a specific resource */
1876
0
          if (Stream_GetRemainingLength(s) >= 8)
1877
0
          {
1878
0
            Stream_Read_UINT32(s, deviceId);
1879
0
            Stream_Read_UINT32(s, status);
1880
1881
0
            if (status != 0)
1882
0
              devman_unregister_device(rdpdr->devman, (void*)((size_t)deviceId));
1883
0
            error = CHANNEL_RC_OK;
1884
0
          }
1885
1886
0
          break;
1887
1888
0
        case PAKID_CORE_DEVICE_IOREQUEST:
1889
0
          if ((error = rdpdr_process_irp(rdpdr, s)))
1890
0
          {
1891
0
            WLog_Print(rdpdr->log, WLOG_ERROR,
1892
0
                       "rdpdr_process_irp failed with error %" PRIu32 "", error);
1893
0
            return error;
1894
0
          }
1895
0
          else
1896
0
            s = nullptr;
1897
1898
0
          break;
1899
1900
0
        default:
1901
0
          WLog_Print(rdpdr->log, WLOG_ERROR,
1902
0
                     "RDPDR_CTYP_CORE unknown PacketId: 0x%04" PRIX16 "", packetId);
1903
0
          error = ERROR_INVALID_DATA;
1904
0
          break;
1905
0
      }
1906
0
    }
1907
0
    else
1908
0
    {
1909
0
      error = rdpdr_process_component(rdpdr, component, packetId, s);
1910
1911
0
      if (error != CHANNEL_RC_OK)
1912
0
      {
1913
0
        DWORD level = WLOG_ERROR;
1914
0
        if (rdpdr->ignoreInvalidDevices)
1915
0
        {
1916
0
          if (error == ERROR_DEV_NOT_EXIST)
1917
0
          {
1918
0
            level = WLOG_WARN;
1919
0
            error = CHANNEL_RC_OK;
1920
0
          }
1921
0
        }
1922
0
        WLog_Print(rdpdr->log, level,
1923
0
                   "Unknown message: Component: %s [0x%04" PRIX16
1924
0
                   "] PacketId: %s [0x%04" PRIX16 "]",
1925
0
                   rdpdr_component_string(component), component,
1926
0
                   rdpdr_packetid_string(packetId), packetId);
1927
0
      }
1928
0
    }
1929
0
  }
1930
1931
0
  return error;
1932
0
}
1933
1934
/**
1935
 * Function description
1936
 *
1937
 * @return 0 on success, otherwise a Win32 error code
1938
 */
1939
UINT rdpdr_send(rdpdrPlugin* rdpdr, wStream* s)
1940
0
{
1941
0
  rdpdrPlugin* plugin = rdpdr;
1942
1943
0
  if (!s)
1944
0
  {
1945
0
    Stream_Release(s);
1946
0
    return CHANNEL_RC_NULL_DATA;
1947
0
  }
1948
1949
0
  if (!plugin)
1950
0
  {
1951
0
    Stream_Release(s);
1952
0
    return CHANNEL_RC_BAD_INIT_HANDLE;
1953
0
  }
1954
1955
0
  const size_t pos = Stream_GetPosition(s);
1956
0
  UINT status = ERROR_INTERNAL_ERROR;
1957
0
  if (pos <= UINT32_MAX)
1958
0
  {
1959
0
    rdpdr_dump_send_packet(rdpdr->log, WLOG_TRACE, s, "[rdpdr-channel] send");
1960
0
    status = plugin->channelEntryPoints.pVirtualChannelWriteEx(
1961
0
        plugin->InitHandle, plugin->OpenHandle, Stream_Buffer(s), (UINT32)pos, s);
1962
0
  }
1963
1964
0
  if (status != CHANNEL_RC_OK)
1965
0
  {
1966
0
    Stream_Release(s);
1967
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "pVirtualChannelWriteEx failed with %s [%08" PRIX32 "]",
1968
0
               WTSErrorToString(status), status);
1969
0
  }
1970
1971
0
  return status;
1972
0
}
1973
1974
/**
1975
 * Function description
1976
 *
1977
 * @return 0 on success, otherwise a Win32 error code
1978
 */
1979
static UINT rdpdr_virtual_channel_event_data_received(rdpdrPlugin* rdpdr, void* pData,
1980
                                                      UINT32 dataLength, UINT32 totalLength,
1981
                                                      UINT32 dataFlags)
1982
0
{
1983
0
  wStream* data_in = nullptr;
1984
1985
0
  WINPR_ASSERT(rdpdr);
1986
0
  WINPR_ASSERT(pData || (dataLength == 0));
1987
1988
0
  if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
1989
0
  {
1990
    /*
1991
     * According to MS-RDPBCGR 2.2.6.1, "All virtual channel traffic MUST be suspended.
1992
     * This flag is only valid in server-to-client virtual channel traffic. It MUST be
1993
     * ignored in client-to-server data." Thus it would be best practice to cease data
1994
     * transmission. However, simply returning here avoids a crash.
1995
     */
1996
0
    return CHANNEL_RC_OK;
1997
0
  }
1998
1999
0
  if (dataFlags & CHANNEL_FLAG_FIRST)
2000
0
  {
2001
0
    if (rdpdr->data_in != nullptr)
2002
0
      Stream_Release(rdpdr->data_in);
2003
2004
0
    rdpdr->data_in = StreamPool_Take(rdpdr->pool, totalLength);
2005
2006
0
    if (!rdpdr->data_in)
2007
0
    {
2008
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
2009
0
      return CHANNEL_RC_NO_MEMORY;
2010
0
    }
2011
0
  }
2012
2013
0
  data_in = rdpdr->data_in;
2014
2015
0
  if (!Stream_EnsureRemainingCapacity(data_in, dataLength))
2016
0
  {
2017
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
2018
0
    return ERROR_INVALID_DATA;
2019
0
  }
2020
2021
0
  Stream_Write(data_in, pData, dataLength);
2022
2023
0
  if (dataFlags & CHANNEL_FLAG_LAST)
2024
0
  {
2025
0
    const size_t pos = Stream_GetPosition(data_in);
2026
0
    const size_t cap = Stream_Capacity(data_in);
2027
0
    if (cap < pos)
2028
0
    {
2029
0
      WLog_Print(rdpdr->log, WLOG_ERROR,
2030
0
                 "rdpdr_virtual_channel_event_data_received: read error");
2031
0
      return ERROR_INTERNAL_ERROR;
2032
0
    }
2033
2034
0
    Stream_SealLength(data_in);
2035
0
    Stream_ResetPosition(data_in);
2036
2037
0
    if (rdpdr->async)
2038
0
    {
2039
0
      if (!MessageQueue_Post(rdpdr->queue, nullptr, 0, (void*)data_in, nullptr))
2040
0
      {
2041
0
        WLog_Print(rdpdr->log, WLOG_ERROR, "MessageQueue_Post failed!");
2042
0
        return ERROR_INTERNAL_ERROR;
2043
0
      }
2044
0
      rdpdr->data_in = nullptr;
2045
0
    }
2046
0
    else
2047
0
    {
2048
0
      UINT error = rdpdr_process_receive(rdpdr, data_in);
2049
0
      Stream_Release(data_in);
2050
0
      rdpdr->data_in = nullptr;
2051
0
      if (error)
2052
0
        return error;
2053
0
    }
2054
0
  }
2055
2056
0
  return CHANNEL_RC_OK;
2057
0
}
2058
2059
static VOID VCAPITYPE rdpdr_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
2060
                                                          UINT event, LPVOID pData,
2061
                                                          UINT32 dataLength, UINT32 totalLength,
2062
                                                          UINT32 dataFlags)
2063
0
{
2064
0
  UINT error = CHANNEL_RC_OK;
2065
0
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)lpUserParam;
2066
2067
0
  WINPR_ASSERT(rdpdr);
2068
0
  switch (event)
2069
0
  {
2070
0
    case CHANNEL_EVENT_DATA_RECEIVED:
2071
0
      if (!rdpdr || !pData || (rdpdr->OpenHandle != openHandle))
2072
0
      {
2073
0
        WLog_Print(rdpdr->log, WLOG_ERROR, "error no match");
2074
0
        return;
2075
0
      }
2076
0
      if ((error = rdpdr_virtual_channel_event_data_received(rdpdr, pData, dataLength,
2077
0
                                                             totalLength, dataFlags)))
2078
0
        WLog_Print(rdpdr->log, WLOG_ERROR,
2079
0
                   "rdpdr_virtual_channel_event_data_received failed with error %" PRIu32
2080
0
                   "!",
2081
0
                   error);
2082
2083
0
      break;
2084
2085
0
    case CHANNEL_EVENT_WRITE_CANCELLED:
2086
0
    case CHANNEL_EVENT_WRITE_COMPLETE:
2087
0
    {
2088
0
      wStream* s = (wStream*)pData;
2089
0
      Stream_Release(s);
2090
0
    }
2091
0
    break;
2092
2093
0
    case CHANNEL_EVENT_USER:
2094
0
      break;
2095
0
    default:
2096
0
      break;
2097
0
  }
2098
2099
0
  if (error && rdpdr && rdpdr->rdpcontext)
2100
0
    setChannelError(rdpdr->rdpcontext, error,
2101
0
                    "rdpdr_virtual_channel_open_event_ex reported an error");
2102
0
}
2103
2104
static DWORD WINAPI rdpdr_virtual_channel_client_thread(LPVOID arg)
2105
0
{
2106
0
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)arg;
2107
0
  UINT error = 0;
2108
2109
0
  if (!rdpdr)
2110
0
  {
2111
0
    ExitThread((DWORD)CHANNEL_RC_NULL_DATA);
2112
0
    return CHANNEL_RC_NULL_DATA;
2113
0
  }
2114
2115
0
  if ((error = rdpdr_process_connect(rdpdr)))
2116
0
  {
2117
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "rdpdr_process_connect failed with error %" PRIu32 "!",
2118
0
               error);
2119
2120
0
    if (rdpdr->rdpcontext)
2121
0
      setChannelError(rdpdr->rdpcontext, error,
2122
0
                      "rdpdr_virtual_channel_client_thread reported an error");
2123
2124
0
    ExitThread(error);
2125
0
    return error;
2126
0
  }
2127
2128
0
  while (1)
2129
0
  {
2130
0
    wMessage message = WINPR_C_ARRAY_INIT;
2131
0
    WINPR_ASSERT(rdpdr);
2132
2133
0
    if (!MessageQueue_Wait(rdpdr->queue))
2134
0
      break;
2135
2136
0
    if (MessageQueue_Peek(rdpdr->queue, &message, TRUE))
2137
0
    {
2138
0
      if (message.id == WMQ_QUIT)
2139
0
        break;
2140
2141
0
      if (message.id == 0)
2142
0
      {
2143
0
        wStream* data = (wStream*)message.wParam;
2144
2145
0
        error = rdpdr_process_receive(rdpdr, data);
2146
2147
0
        Stream_Release(data);
2148
0
        if (error)
2149
0
        {
2150
0
          WLog_Print(rdpdr->log, WLOG_ERROR,
2151
0
                     "rdpdr_process_receive failed with error %" PRIu32 "!", error);
2152
2153
0
          if (rdpdr->rdpcontext)
2154
0
            setChannelError(rdpdr->rdpcontext, error,
2155
0
                            "rdpdr_virtual_channel_client_thread reported an error");
2156
2157
0
          goto fail;
2158
0
        }
2159
0
      }
2160
0
    }
2161
0
  }
2162
2163
0
fail:
2164
0
  if ((error = drive_hotplug_thread_terminate(rdpdr)))
2165
0
    WLog_Print(rdpdr->log, WLOG_ERROR,
2166
0
               "drive_hotplug_thread_terminate failed with error %" PRIu32 "!", error);
2167
2168
0
  ExitThread(error);
2169
0
  return error;
2170
0
}
2171
2172
static void queue_free(void* obj)
2173
0
{
2174
0
  wStream* s = nullptr;
2175
0
  wMessage* msg = (wMessage*)obj;
2176
2177
0
  if (!msg || (msg->id != 0))
2178
0
    return;
2179
2180
0
  s = (wStream*)msg->wParam;
2181
0
  WINPR_ASSERT(s);
2182
0
  Stream_Release(s);
2183
0
}
2184
2185
/**
2186
 * Function description
2187
 *
2188
 * @return 0 on success, otherwise a Win32 error code
2189
 */
2190
static UINT rdpdr_virtual_channel_event_connected(rdpdrPlugin* rdpdr, LPVOID pData,
2191
                                                  UINT32 dataLength)
2192
0
{
2193
0
  wObject* obj = nullptr;
2194
2195
0
  WINPR_ASSERT(rdpdr);
2196
0
  WINPR_UNUSED(pData);
2197
0
  WINPR_UNUSED(dataLength);
2198
2199
0
  if (rdpdr->async)
2200
0
  {
2201
0
    rdpdr->queue = MessageQueue_New(nullptr);
2202
2203
0
    if (!rdpdr->queue)
2204
0
    {
2205
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "MessageQueue_New failed!");
2206
0
      return CHANNEL_RC_NO_MEMORY;
2207
0
    }
2208
2209
0
    obj = MessageQueue_Object(rdpdr->queue);
2210
0
    obj->fnObjectFree = queue_free;
2211
2212
0
    if (!(rdpdr->thread = CreateThread(nullptr, 0, rdpdr_virtual_channel_client_thread,
2213
0
                                       (void*)rdpdr, 0, nullptr)))
2214
0
    {
2215
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "CreateThread failed!");
2216
0
      return ERROR_INTERNAL_ERROR;
2217
0
    }
2218
0
  }
2219
0
  else
2220
0
  {
2221
0
    UINT error = rdpdr_process_connect(rdpdr);
2222
0
    if (error)
2223
0
    {
2224
0
      WLog_Print(rdpdr->log, WLOG_ERROR,
2225
0
                 "rdpdr_process_connect failed with error %" PRIu32 "!", error);
2226
0
      return error;
2227
0
    }
2228
0
  }
2229
2230
0
  return rdpdr->channelEntryPoints.pVirtualChannelOpenEx(rdpdr->InitHandle, &rdpdr->OpenHandle,
2231
0
                                                         rdpdr->channelDef.name,
2232
0
                                                         rdpdr_virtual_channel_open_event_ex);
2233
0
}
2234
2235
/**
2236
 * Function description
2237
 *
2238
 * @return 0 on success, otherwise a Win32 error code
2239
 */
2240
static UINT rdpdr_virtual_channel_event_disconnected(rdpdrPlugin* rdpdr)
2241
0
{
2242
0
  UINT error = 0;
2243
2244
0
  WINPR_ASSERT(rdpdr);
2245
2246
0
  if (rdpdr->OpenHandle == 0)
2247
0
    return CHANNEL_RC_OK;
2248
2249
0
  if (rdpdr->queue && rdpdr->thread)
2250
0
  {
2251
0
    if (MessageQueue_PostQuit(rdpdr->queue, 0) &&
2252
0
        (WaitForSingleObject(rdpdr->thread, INFINITE) == WAIT_FAILED))
2253
0
    {
2254
0
      error = GetLastError();
2255
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "!",
2256
0
                 error);
2257
0
      return error;
2258
0
    }
2259
0
  }
2260
2261
0
  if (rdpdr->thread)
2262
0
    (void)CloseHandle(rdpdr->thread);
2263
0
  MessageQueue_Free(rdpdr->queue);
2264
0
  rdpdr->queue = nullptr;
2265
0
  rdpdr->thread = nullptr;
2266
2267
0
  WINPR_ASSERT(rdpdr->channelEntryPoints.pVirtualChannelCloseEx);
2268
0
  error = rdpdr->channelEntryPoints.pVirtualChannelCloseEx(rdpdr->InitHandle, rdpdr->OpenHandle);
2269
2270
0
  if (CHANNEL_RC_OK != error)
2271
0
  {
2272
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "pVirtualChannelCloseEx failed with %s [%08" PRIX32 "]",
2273
0
               WTSErrorToString(error), error);
2274
0
  }
2275
2276
0
  rdpdr->OpenHandle = 0;
2277
2278
0
  if (rdpdr->data_in)
2279
0
  {
2280
0
    Stream_Release(rdpdr->data_in);
2281
0
    rdpdr->data_in = nullptr;
2282
0
  }
2283
2284
0
  if (rdpdr->devman)
2285
0
  {
2286
0
    devman_free(rdpdr->devman);
2287
0
    rdpdr->devman = nullptr;
2288
0
  }
2289
2290
0
  return error;
2291
0
}
2292
2293
static void rdpdr_virtual_channel_event_terminated(rdpdrPlugin* rdpdr)
2294
0
{
2295
0
  WINPR_ASSERT(rdpdr);
2296
0
#if !defined(_WIN32)
2297
0
  if (rdpdr->stopEvent)
2298
0
  {
2299
0
    (void)CloseHandle(rdpdr->stopEvent);
2300
0
    rdpdr->stopEvent = nullptr;
2301
0
  }
2302
0
#endif
2303
0
  rdpdr->InitHandle = nullptr;
2304
0
  StreamPool_Free(rdpdr->pool);
2305
0
  free(rdpdr);
2306
0
}
2307
2308
static UINT rdpdr_register_device(RdpdrClientContext* context, const RDPDR_DEVICE* device,
2309
                                  uint32_t* pid)
2310
0
{
2311
0
  WINPR_ASSERT(context);
2312
0
  WINPR_ASSERT(device);
2313
0
  WINPR_ASSERT(pid);
2314
2315
0
  rdpdrPlugin* rdpdr = context->handle;
2316
0
  WINPR_ASSERT(rdpdr);
2317
2318
0
  RDPDR_DEVICE* copy = freerdp_device_clone(device);
2319
0
  if (!copy)
2320
0
    return ERROR_INVALID_DATA;
2321
0
  UINT rc = devman_load_device_service(rdpdr->devman, copy, rdpdr->rdpcontext);
2322
0
  *pid = copy->Id;
2323
0
  freerdp_device_free(copy);
2324
0
  if (rc == CHANNEL_RC_OK)
2325
0
    rc = rdpdr_try_send_device_list_announce_request(rdpdr);
2326
0
  return rc;
2327
0
}
2328
2329
static UINT rdpdr_unregister_device(RdpdrClientContext* context, size_t count, const uint32_t ids[])
2330
0
{
2331
0
  WINPR_ASSERT(context);
2332
2333
0
  rdpdrPlugin* rdpdr = context->handle;
2334
0
  WINPR_ASSERT(rdpdr);
2335
2336
0
  for (size_t x = 0; x < count; x++)
2337
0
  {
2338
0
    const uintptr_t id = ids[x];
2339
0
    devman_unregister_device(rdpdr->devman, (void*)id);
2340
0
  }
2341
0
  return rdpdr_send_device_list_remove_request(rdpdr, WINPR_ASSERTING_INT_CAST(uint32_t, count),
2342
0
                                               ids);
2343
0
}
2344
2345
static UINT rdpdr_virtual_channel_event_initialized(rdpdrPlugin* rdpdr,
2346
                                                    WINPR_ATTR_UNUSED LPVOID pData,
2347
                                                    WINPR_ATTR_UNUSED UINT32 dataLength)
2348
0
{
2349
0
  WINPR_ASSERT(rdpdr);
2350
0
#if !defined(_WIN32)
2351
0
  WINPR_ASSERT(!rdpdr->stopEvent);
2352
0
  rdpdr->stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
2353
0
  WINPR_ASSERT(rdpdr->stopEvent);
2354
0
#endif
2355
2356
0
  rdpdr->context.handle = rdpdr;
2357
0
  rdpdr->context.RdpdrHotplugDevice = handle_hotplug;
2358
0
  rdpdr->context.RdpdrRegisterDevice = rdpdr_register_device;
2359
0
  rdpdr->context.RdpdrUnregisterDevice = rdpdr_unregister_device;
2360
0
  return CHANNEL_RC_OK;
2361
0
}
2362
2363
static VOID VCAPITYPE rdpdr_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
2364
                                                          UINT event, LPVOID pData, UINT dataLength)
2365
0
{
2366
0
  UINT error = CHANNEL_RC_OK;
2367
0
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)lpUserParam;
2368
2369
0
  if (!rdpdr || (rdpdr->InitHandle != pInitHandle))
2370
0
  {
2371
0
    WLog_ERR(TAG, "error no match");
2372
0
    return;
2373
0
  }
2374
2375
0
  WINPR_ASSERT(pData || (dataLength == 0));
2376
2377
0
  switch (event)
2378
0
  {
2379
0
    case CHANNEL_EVENT_INITIALIZED:
2380
0
      error = rdpdr_virtual_channel_event_initialized(rdpdr, pData, dataLength);
2381
0
      break;
2382
2383
0
    case CHANNEL_EVENT_CONNECTED:
2384
0
      if ((error = rdpdr_virtual_channel_event_connected(rdpdr, pData, dataLength)))
2385
0
        WLog_Print(rdpdr->log, WLOG_ERROR,
2386
0
                   "rdpdr_virtual_channel_event_connected failed with error %" PRIu32 "!",
2387
0
                   error);
2388
2389
0
      break;
2390
2391
0
    case CHANNEL_EVENT_DISCONNECTED:
2392
0
      if ((error = rdpdr_virtual_channel_event_disconnected(rdpdr)))
2393
0
        WLog_Print(rdpdr->log, WLOG_ERROR,
2394
0
                   "rdpdr_virtual_channel_event_disconnected failed with error %" PRIu32
2395
0
                   "!",
2396
0
                   error);
2397
2398
0
      break;
2399
2400
0
    case CHANNEL_EVENT_TERMINATED:
2401
0
      rdpdr_virtual_channel_event_terminated(rdpdr);
2402
0
      rdpdr = nullptr;
2403
0
      break;
2404
2405
0
    case CHANNEL_EVENT_ATTACHED:
2406
0
    case CHANNEL_EVENT_DETACHED:
2407
0
    default:
2408
0
      WLog_Print(rdpdr->log, WLOG_ERROR, "unknown event %" PRIu32 "!", event);
2409
0
      break;
2410
0
  }
2411
2412
0
  if (error && rdpdr && rdpdr->rdpcontext)
2413
0
    setChannelError(rdpdr->rdpcontext, error,
2414
0
                    "rdpdr_virtual_channel_init_event_ex reported an error");
2415
0
}
2416
2417
/* rdpdr is always built-in */
2418
#define VirtualChannelEntryEx rdpdr_VirtualChannelEntryEx
2419
2420
FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS_EX pEntryPoints,
2421
                                                         PVOID pInitHandle))
2422
0
{
2423
0
  WINPR_ASSERT(pEntryPoints);
2424
0
  WINPR_ASSERT(pInitHandle);
2425
2426
0
  rdpdrPlugin* rdpdr = (rdpdrPlugin*)calloc(1, sizeof(rdpdrPlugin));
2427
2428
0
  if (!rdpdr)
2429
0
  {
2430
0
    WLog_ERR(TAG, "calloc failed!");
2431
0
    return FALSE;
2432
0
  }
2433
0
  rdpdr->log = WLog_Get(TAG);
2434
2435
0
  rdpdr->clientExtendedPDU =
2436
0
      RDPDR_DEVICE_REMOVE_PDUS | RDPDR_CLIENT_DISPLAY_NAME_PDU | RDPDR_USER_LOGGEDON_PDU;
2437
0
  rdpdr->clientIOCode1 =
2438
0
      RDPDR_IRP_MJ_CREATE | RDPDR_IRP_MJ_CLEANUP | RDPDR_IRP_MJ_CLOSE | RDPDR_IRP_MJ_READ |
2439
0
      RDPDR_IRP_MJ_WRITE | RDPDR_IRP_MJ_FLUSH_BUFFERS | RDPDR_IRP_MJ_SHUTDOWN |
2440
0
      RDPDR_IRP_MJ_DEVICE_CONTROL | RDPDR_IRP_MJ_QUERY_VOLUME_INFORMATION |
2441
0
      RDPDR_IRP_MJ_SET_VOLUME_INFORMATION | RDPDR_IRP_MJ_QUERY_INFORMATION |
2442
0
      RDPDR_IRP_MJ_SET_INFORMATION | RDPDR_IRP_MJ_DIRECTORY_CONTROL | RDPDR_IRP_MJ_LOCK_CONTROL |
2443
0
      RDPDR_IRP_MJ_QUERY_SECURITY | RDPDR_IRP_MJ_SET_SECURITY;
2444
2445
0
  rdpdr->clientExtraFlags1 = ENABLE_ASYNCIO;
2446
2447
0
  rdpdr->pool = StreamPool_New(TRUE, 1024);
2448
0
  if (!rdpdr->pool)
2449
0
  {
2450
0
    free(rdpdr);
2451
0
    return FALSE;
2452
0
  }
2453
2454
0
  rdpdr->channelDef.options =
2455
0
      CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP | CHANNEL_OPTION_COMPRESS_RDP;
2456
0
  (void)sprintf_s(rdpdr->channelDef.name, ARRAYSIZE(rdpdr->channelDef.name),
2457
0
                  RDPDR_SVC_CHANNEL_NAME);
2458
0
  rdpdr->sequenceId = 0;
2459
0
  CHANNEL_ENTRY_POINTS_FREERDP_EX* pEntryPointsEx =
2460
0
      (CHANNEL_ENTRY_POINTS_FREERDP_EX*)pEntryPoints;
2461
2462
0
  if ((pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX)) &&
2463
0
      (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
2464
0
  {
2465
0
    rdpdr->rdpcontext = pEntryPointsEx->context;
2466
0
    if (!freerdp_settings_get_bool(rdpdr->rdpcontext->settings,
2467
0
                                   FreeRDP_SynchronousStaticChannels))
2468
0
      rdpdr->async = TRUE;
2469
0
  }
2470
2471
0
  CopyMemory(&(rdpdr->channelEntryPoints), pEntryPoints, sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX));
2472
0
  rdpdr->InitHandle = pInitHandle;
2473
0
  const UINT rc = rdpdr->channelEntryPoints.pVirtualChannelInitEx(
2474
0
      rdpdr, &rdpdr->context, pInitHandle, &rdpdr->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
2475
0
      rdpdr_virtual_channel_init_event_ex);
2476
2477
0
  if (CHANNEL_RC_OK != rc)
2478
0
  {
2479
0
    WLog_Print(rdpdr->log, WLOG_ERROR, "pVirtualChannelInitEx failed with %s [%08" PRIX32 "]",
2480
0
               WTSErrorToString(rc), rc);
2481
0
    free(rdpdr);
2482
0
    return FALSE;
2483
0
  }
2484
2485
0
  return TRUE;
2486
0
}