Coverage Report

Created: 2026-03-04 06:17

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