2011-08-04 19:22:58 +04:00
|
|
|
/**
|
2012-10-09 05:00:07 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Device Redirection Virtual Channel
|
2011-08-04 19:22:58 +04:00
|
|
|
*
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
2012-10-09 05:00:07 +04:00
|
|
|
* Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2016-01-28 13:12:20 +03:00
|
|
|
* Copyright 2015-2016 Thincast Technologies GmbH
|
2015-06-03 14:38:47 +03:00
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2016-01-28 13:12:20 +03:00
|
|
|
* Copyright 2016 Armin Novak <armin.novak@thincast.com>
|
2016-06-29 00:39:14 +03:00
|
|
|
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
|
2011-08-04 19:22:58 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-10-08 06:53:24 +04:00
|
|
|
#include <winpr/crt.h>
|
2022-03-15 17:19:41 +03:00
|
|
|
#include <winpr/sysinfo.h>
|
2021-06-18 11:55:18 +03:00
|
|
|
#include <winpr/assert.h>
|
2013-10-17 23:42:51 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-10-08 06:53:24 +04:00
|
|
|
|
2021-12-14 14:59:16 +03:00
|
|
|
#include <winpr/print.h>
|
2022-01-25 14:27:17 +03:00
|
|
|
#include <winpr/sspicli.h>
|
2021-12-14 14:59:16 +03:00
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
#include <freerdp/types.h>
|
2023-03-14 12:39:18 +03:00
|
|
|
#include <freerdp/freerdp.h>
|
2012-10-07 18:21:54 +04:00
|
|
|
#include <freerdp/constants.h>
|
2014-08-11 11:12:01 +04:00
|
|
|
#include <freerdp/channels/log.h>
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2021-12-14 14:59:16 +03:00
|
|
|
#include <freerdp/utils/rdpdr_utils.h>
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <dbt.h>
|
2014-02-20 13:42:02 +04:00
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2014-02-14 11:09:55 +04:00
|
|
|
#endif
|
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
#ifdef __MACOSX__
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-05 11:43:48 +04:00
|
|
|
#include "rdpdr_capabilities.h"
|
2012-10-09 05:00:07 +04:00
|
|
|
|
2011-08-05 21:54:40 +04:00
|
|
|
#include "devman.h"
|
|
|
|
#include "irp.h"
|
2012-10-09 04:26:11 +04:00
|
|
|
|
2011-08-05 11:43:48 +04:00
|
|
|
#include "rdpdr_main.h"
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2018-07-30 13:31:11 +03:00
|
|
|
/* IMPORTANT: Keep in sync with DRIVE_DEVICE */
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2014-02-14 12:35:44 +04:00
|
|
|
{
|
|
|
|
DEVICE device;
|
2017-04-13 17:20:01 +03:00
|
|
|
WCHAR* path;
|
2018-07-30 13:31:11 +03:00
|
|
|
BOOL automount;
|
2022-02-14 16:59:22 +03:00
|
|
|
} DEVICE_DRIVE_EXT;
|
2014-02-14 12:35:44 +04:00
|
|
|
|
2023-06-02 12:44:38 +03:00
|
|
|
static const char* rdpdr_state_str(enum RDPDR_CHANNEL_STATE state)
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case RDPDR_CHANNEL_STATE_INITIAL:
|
|
|
|
return "RDPDR_CHANNEL_STATE_INITIAL";
|
|
|
|
case RDPDR_CHANNEL_STATE_ANNOUNCE:
|
|
|
|
return "RDPDR_CHANNEL_STATE_ANNOUNCE";
|
|
|
|
case RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY:
|
|
|
|
return "RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY";
|
|
|
|
case RDPDR_CHANNEL_STATE_NAME_REQUEST:
|
|
|
|
return "RDPDR_CHANNEL_STATE_NAME_REQUEST";
|
|
|
|
case RDPDR_CHANNEL_STATE_SERVER_CAPS:
|
|
|
|
return "RDPDR_CHANNEL_STATE_SERVER_CAPS";
|
|
|
|
case RDPDR_CHANNEL_STATE_CLIENT_CAPS:
|
|
|
|
return "RDPDR_CHANNEL_STATE_CLIENT_CAPS";
|
|
|
|
case RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM:
|
|
|
|
return "RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM";
|
|
|
|
case RDPDR_CHANNEL_STATE_READY:
|
|
|
|
return "RDPDR_CHANNEL_STATE_READY";
|
|
|
|
case RDPDR_CHANNEL_STATE_USER_LOGGEDON:
|
|
|
|
return "RDPDR_CHANNEL_STATE_USER_LOGGEDON";
|
|
|
|
default:
|
|
|
|
return "RDPDR_CHANNEL_STATE_UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 10:29:45 +03:00
|
|
|
static const char* rdpdr_device_type_string(UINT32 type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case RDPDR_DTYP_SERIAL:
|
|
|
|
return "serial";
|
|
|
|
case RDPDR_DTYP_PRINT:
|
|
|
|
return "printer";
|
|
|
|
case RDPDR_DTYP_FILESYSTEM:
|
|
|
|
return "drive";
|
|
|
|
case RDPDR_DTYP_SMARTCARD:
|
|
|
|
return "smartcard";
|
|
|
|
case RDPDR_DTYP_PARALLEL:
|
|
|
|
return "parallel";
|
|
|
|
default:
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 11:37:03 +03:00
|
|
|
|
2023-06-02 12:44:38 +03:00
|
|
|
BOOL rdpdr_state_advance(rdpdrPlugin* rdpdr, enum RDPDR_CHANNEL_STATE next)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
|
|
|
|
if (next != rdpdr->state)
|
|
|
|
WLog_Print(rdpdr->log, WLOG_DEBUG, "[RDPDR] transition from %s to %s",
|
|
|
|
rdpdr_state_str(rdpdr->state), rdpdr_state_str(next));
|
|
|
|
rdpdr->state = next;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
static BOOL device_foreach(rdpdrPlugin* rdpdr, BOOL abortOnFail,
|
|
|
|
BOOL (*fkt)(ULONG_PTR key, void* element, void* data), void* data)
|
|
|
|
{
|
|
|
|
BOOL rc = TRUE;
|
|
|
|
int count, x;
|
|
|
|
ULONG_PTR* keys = NULL;
|
|
|
|
|
|
|
|
ListDictionary_Lock(rdpdr->devman->devices);
|
|
|
|
count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
|
|
|
|
for (x = 0; x < count; x++)
|
|
|
|
{
|
|
|
|
void* element = ListDictionary_GetItemValue(rdpdr->devman->devices, (void*)keys[x]);
|
|
|
|
if (!fkt(keys[x], element, data))
|
|
|
|
{
|
|
|
|
rc = FALSE;
|
|
|
|
if (abortOnFail)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(keys);
|
|
|
|
ListDictionary_Unlock(rdpdr->devman->devices);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2023-06-02 12:44:38 +03:00
|
|
|
static UINT rdpdr_try_send_device_list_announce_request(rdpdrPlugin* rdpdr);
|
2014-02-14 11:09:55 +04:00
|
|
|
|
2021-10-21 21:33:52 +03:00
|
|
|
static BOOL rdpdr_load_drive(rdpdrPlugin* rdpdr, const char* name, const char* path, BOOL automount)
|
2021-10-07 11:43:16 +03:00
|
|
|
{
|
|
|
|
UINT rc = ERROR_INTERNAL_ERROR;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
RDPDR_DRIVE* drive;
|
|
|
|
RDPDR_DEVICE* device;
|
|
|
|
} drive;
|
|
|
|
const char* args[] = { name, path, automount ? NULL : name };
|
|
|
|
|
|
|
|
drive.device = freerdp_device_new(RDPDR_DTYP_FILESYSTEM, ARRAYSIZE(args), args);
|
|
|
|
if (!drive.device)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rc = devman_load_device_service(rdpdr->devman, drive.device, rdpdr->rdpcontext);
|
|
|
|
if (rc != CHANNEL_RC_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
freerdp_device_free(drive.device);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpdr_send_device_list_remove_request(rdpdrPlugin* rdpdr, UINT32 count, UINT32 ids[])
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2014-05-10 17:50:36 +04:00
|
|
|
UINT32 i;
|
2014-02-14 11:09:55 +04:00
|
|
|
wStream* s;
|
2021-07-28 09:02:59 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
WINPR_ASSERT(ids || (count == 0));
|
|
|
|
|
2022-12-22 12:16:11 +03:00
|
|
|
if (count == 0)
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
|
2022-06-27 13:05:35 +03:00
|
|
|
s = StreamPool_Take(rdpdr->pool, count * sizeof(UINT32) + 8);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2014-02-14 11:09:55 +04:00
|
|
|
|
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE);
|
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_REMOVE);
|
|
|
|
Stream_Write_UINT32(s, count);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
Stream_Write_UINT32(s, ids[i]);
|
|
|
|
|
|
|
|
Stream_SealLength(s);
|
2015-06-03 14:38:47 +03:00
|
|
|
return rdpdr_send(rdpdr, s);
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
|
|
|
|
2019-11-18 16:28:39 +03:00
|
|
|
#if defined(_UWP) || defined(__IOS__)
|
2016-03-29 23:03:15 +03:00
|
|
|
|
2021-07-28 09:43:01 +03:00
|
|
|
static void first_hotplug(rdpdrPlugin* rdpdr)
|
2016-03-29 23:03:15 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI drive_hotplug_thread_func(LPVOID arg)
|
2016-03-29 23:03:15 +03:00
|
|
|
{
|
2018-03-07 14:03:10 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2016-03-29 23:03:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static UINT drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
}
|
|
|
|
|
2021-06-18 11:55:18 +03:00
|
|
|
#elif defined(_WIN32)
|
2014-02-14 11:09:55 +04:00
|
|
|
|
2021-07-28 09:43:01 +03:00
|
|
|
static BOOL check_path(const char* path)
|
2016-02-26 21:32:54 +03:00
|
|
|
{
|
|
|
|
UINT type = GetDriveTypeA(path);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
if (!(type == DRIVE_FIXED || type == DRIVE_REMOVABLE || type == DRIVE_CDROM ||
|
|
|
|
type == DRIVE_REMOTE))
|
2016-02-26 21:32:54 +03:00
|
|
|
return FALSE;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2016-02-26 21:32:54 +03:00
|
|
|
return GetVolumeInformationA(path, NULL, 0, NULL, NULL, NULL, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2021-07-28 09:43:01 +03:00
|
|
|
static void first_hotplug(rdpdrPlugin* rdpdr)
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
2020-01-07 13:33:14 +03:00
|
|
|
size_t i;
|
2015-09-07 13:16:03 +03:00
|
|
|
DWORD unitmask = GetLogicalDrives();
|
|
|
|
|
|
|
|
for (i = 0; i < 26; i++)
|
|
|
|
{
|
|
|
|
if (unitmask & 0x01)
|
|
|
|
{
|
2020-01-07 13:33:14 +03:00
|
|
|
char drive_path[] = { 'c', ':', '\\', '\0' };
|
|
|
|
char drive_name[] = { 'c', '\0' };
|
|
|
|
drive_path[0] = 'A' + (char)i;
|
|
|
|
drive_name[0] = 'A' + (char)i;
|
2015-09-07 13:16:03 +03:00
|
|
|
|
2016-02-26 21:32:54 +03:00
|
|
|
if (check_path(drive_path))
|
|
|
|
{
|
2021-10-07 11:43:16 +03:00
|
|
|
rdpdr_load_drive(rdpdr, drive_name, drive_path, TRUE);
|
2016-02-26 21:32:54 +03:00
|
|
|
}
|
2015-09-07 13:16:03 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
unitmask = unitmask >> 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 09:43:01 +03:00
|
|
|
static LRESULT CALLBACK hotplug_proc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
rdpdrPlugin* rdpdr;
|
2014-02-14 11:09:55 +04:00
|
|
|
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2016-07-21 17:51:42 +03:00
|
|
|
rdpdr = (rdpdrPlugin*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
|
2014-02-14 11:09:55 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
switch (Msg)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
|
|
|
case WM_DEVICECHANGE:
|
|
|
|
switch (wParam)
|
|
|
|
{
|
|
|
|
case DBT_DEVICEARRIVAL:
|
2019-11-06 17:24:51 +03:00
|
|
|
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
|
|
|
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
|
|
|
|
DWORD unitmask = lpdbv->dbcv_unitmask;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 26; i++)
|
|
|
|
{
|
|
|
|
if (unitmask & 0x01)
|
2019-11-06 17:24:51 +03:00
|
|
|
{
|
2020-01-07 13:33:14 +03:00
|
|
|
char drive_path[] = { 'c', ':', '/', '\0' };
|
|
|
|
char drive_name[] = { 'c', '\0' };
|
|
|
|
drive_path[0] = 'A' + (char)i;
|
|
|
|
drive_name[0] = 'A' + (char)i;
|
2014-02-14 11:09:55 +04:00
|
|
|
|
2016-02-26 21:32:54 +03:00
|
|
|
if (check_path(drive_path))
|
|
|
|
{
|
2021-10-07 11:43:16 +03:00
|
|
|
rdpdr_load_drive(rdpdr, drive_name, drive_path, TRUE);
|
2023-06-02 12:44:38 +03:00
|
|
|
rdpdr_try_send_device_list_announce_request(rdpdr);
|
2016-02-26 21:32:54 +03:00
|
|
|
}
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
unitmask = unitmask >> 1;
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DBT_DEVICEREMOVECOMPLETE:
|
2019-11-06 17:24:51 +03:00
|
|
|
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
|
|
|
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
|
|
|
|
DWORD unitmask = lpdbv->dbcv_unitmask;
|
|
|
|
int i, j, count;
|
|
|
|
char drive_name_upper, drive_name_lower;
|
2018-08-17 13:56:45 +03:00
|
|
|
ULONG_PTR* keys = NULL;
|
2016-07-21 17:51:42 +03:00
|
|
|
DEVICE_DRIVE_EXT* device_ext;
|
2014-02-14 11:09:55 +04:00
|
|
|
UINT32 ids[1];
|
|
|
|
|
|
|
|
for (i = 0; i < 26; i++)
|
|
|
|
{
|
|
|
|
if (unitmask & 0x01)
|
|
|
|
{
|
|
|
|
drive_name_upper = 'A' + i;
|
|
|
|
drive_name_lower = 'a' + i;
|
|
|
|
count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
|
|
|
|
|
|
|
|
for (j = 0; j < count; j++)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
device_ext = (DEVICE_DRIVE_EXT*)ListDictionary_GetItemValue(
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpdr->devman->devices, (void*)keys[j]);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2021-03-09 14:23:37 +03:00
|
|
|
if (device_ext->device.type != RDPDR_DTYP_FILESYSTEM)
|
|
|
|
continue;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (device_ext->path[0] == drive_name_upper ||
|
|
|
|
device_ext->path[0] == drive_name_lower)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2018-07-30 13:37:19 +03:00
|
|
|
if (device_ext->automount)
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2018-07-30 13:37:19 +03:00
|
|
|
devman_unregister_device(rdpdr->devman, (void*)keys[j]);
|
|
|
|
ids[0] = keys[j];
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((error = rdpdr_send_device_list_remove_request(
|
|
|
|
rdpdr, 1, ids)))
|
2018-07-30 13:37:19 +03:00
|
|
|
{
|
|
|
|
// dont end on error, just report ?
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(
|
|
|
|
rdpdr->log, WLOG_ERROR,
|
2019-11-06 17:24:51 +03:00
|
|
|
"rdpdr_send_device_list_remove_request failed "
|
|
|
|
"with error %" PRIu32 "!",
|
|
|
|
error);
|
2018-07-30 13:37:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
|
|
|
}
|
2018-08-17 13:56:45 +03:00
|
|
|
|
|
|
|
free(keys);
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
unitmask = unitmask >> 1;
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return DefWindowProc(hWnd, Msg, wParam, lParam);
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
return DefWindowProc(hWnd, Msg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI drive_hotplug_thread_func(LPVOID arg)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
rdpdrPlugin* rdpdr;
|
2014-02-14 11:09:55 +04:00
|
|
|
WNDCLASSEX wnd_cls;
|
|
|
|
HWND hwnd;
|
|
|
|
MSG msg;
|
|
|
|
BOOL bRet;
|
|
|
|
DEV_BROADCAST_HANDLE NotificationFilter;
|
|
|
|
HDEVNOTIFY hDevNotify;
|
2016-07-21 17:51:42 +03:00
|
|
|
rdpdr = (rdpdrPlugin*)arg;
|
2014-02-14 11:09:55 +04:00
|
|
|
/* init windows class */
|
2019-11-06 17:24:51 +03:00
|
|
|
wnd_cls.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
wnd_cls.style = CS_HREDRAW | CS_VREDRAW;
|
|
|
|
wnd_cls.lpfnWndProc = hotplug_proc;
|
|
|
|
wnd_cls.cbClsExtra = 0;
|
|
|
|
wnd_cls.cbWndExtra = 0;
|
|
|
|
wnd_cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
|
|
|
wnd_cls.hCursor = NULL;
|
2014-02-14 11:09:55 +04:00
|
|
|
wnd_cls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
|
2019-11-06 17:24:51 +03:00
|
|
|
wnd_cls.lpszMenuName = NULL;
|
2014-02-14 11:09:55 +04:00
|
|
|
wnd_cls.lpszClassName = L"DRIVE_HOTPLUG";
|
2019-11-06 17:24:51 +03:00
|
|
|
wnd_cls.hInstance = NULL;
|
|
|
|
wnd_cls.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
|
2014-02-14 11:09:55 +04:00
|
|
|
RegisterClassEx(&wnd_cls);
|
|
|
|
/* create window */
|
2019-11-06 17:24:51 +03:00
|
|
|
hwnd = CreateWindowEx(0, L"DRIVE_HOTPLUG", NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
|
2014-02-14 11:09:55 +04:00
|
|
|
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)rdpdr);
|
|
|
|
rdpdr->hotplug_wnd = hwnd;
|
|
|
|
/* register device interface to hwnd */
|
|
|
|
NotificationFilter.dbch_size = sizeof(DEV_BROADCAST_HANDLE);
|
|
|
|
NotificationFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;
|
2019-11-06 17:24:51 +03:00
|
|
|
hDevNotify = RegisterDeviceNotification(hwnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
|
2014-02-14 11:09:55 +04:00
|
|
|
|
|
|
|
/* message loop */
|
|
|
|
while ((bRet = GetMessage(&msg, 0, 0, 0)) != 0)
|
|
|
|
{
|
|
|
|
if (bRet == -1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnregisterDeviceNotification(hDevNotify);
|
2018-03-07 14:03:10 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-30 17:50:10 +03:00
|
|
|
if (rdpdr->hotplug_wnd && !PostMessage(rdpdr->hotplug_wnd, WM_QUIT, 0, 0))
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "PostMessage failed with error %" PRIu32 "", error);
|
2015-07-30 17:50:10 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-30 17:50:10 +03:00
|
|
|
return error;
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
|
|
|
|
2019-11-18 16:28:39 +03:00
|
|
|
#elif defined(__MACOSX__)
|
2015-09-07 13:16:03 +03:00
|
|
|
|
|
|
|
#define MAX_USB_DEVICES 100
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
char* path;
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL to_add;
|
2015-09-07 13:16:03 +03:00
|
|
|
} hotplug_dev;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT handle_hotplug(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
struct dirent* pDirent;
|
|
|
|
DIR* pDir;
|
2015-09-07 13:16:03 +03:00
|
|
|
char fullpath[PATH_MAX];
|
|
|
|
char* szdir = (char*)"/Volumes";
|
|
|
|
struct stat buf;
|
|
|
|
hotplug_dev dev_array[MAX_USB_DEVICES];
|
|
|
|
int count;
|
2016-07-21 17:51:42 +03:00
|
|
|
DEVICE_DRIVE_EXT* device_ext;
|
2018-08-17 13:56:45 +03:00
|
|
|
ULONG_PTR* keys = NULL;
|
2015-09-07 13:16:03 +03:00
|
|
|
int i, j;
|
|
|
|
int size = 0;
|
|
|
|
UINT error;
|
|
|
|
UINT32 ids[1];
|
2016-07-21 17:51:42 +03:00
|
|
|
pDir = opendir(szdir);
|
2015-09-07 13:16:03 +03:00
|
|
|
|
|
|
|
if (pDir == NULL)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
printf("Cannot open directory\n");
|
2015-09-07 13:16:03 +03:00
|
|
|
return ERROR_OPEN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((pDirent = readdir(pDir)) != NULL)
|
|
|
|
{
|
|
|
|
if (pDirent->d_name[0] != '.')
|
|
|
|
{
|
2018-08-24 11:39:48 +03:00
|
|
|
sprintf_s(fullpath, ARRAYSIZE(fullpath), "%s/%s", szdir, pDirent->d_name);
|
2019-09-12 15:35:01 +03:00
|
|
|
if (stat(fullpath, &buf) != 0)
|
|
|
|
continue;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
|
|
|
if (S_ISDIR(buf.st_mode))
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
dev_array[size].path = _strdup(fullpath);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
if (!dev_array[size].path)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
closedir(pDir);
|
2015-09-07 13:16:03 +03:00
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
dev_array[size++].to_add = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
closedir(pDir);
|
2015-09-07 13:16:03 +03:00
|
|
|
/* delete removed devices */
|
|
|
|
count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
|
|
|
|
|
|
|
|
for (j = 0; j < count; j++)
|
|
|
|
{
|
2018-01-25 17:43:53 +03:00
|
|
|
char* path = NULL;
|
2015-09-07 13:16:03 +03:00
|
|
|
BOOL dev_found = FALSE;
|
2019-11-06 17:24:51 +03:00
|
|
|
device_ext =
|
|
|
|
(DEVICE_DRIVE_EXT*)ListDictionary_GetItemValue(rdpdr->devman->devices, (void*)keys[j]);
|
2015-09-07 13:16:03 +03:00
|
|
|
|
2018-07-30 13:37:19 +03:00
|
|
|
if (!device_ext || !device_ext->automount)
|
2015-09-07 13:16:03 +03:00
|
|
|
continue;
|
|
|
|
|
2019-09-12 11:51:55 +03:00
|
|
|
if (device_ext->device.type != RDPDR_DTYP_FILESYSTEM)
|
|
|
|
continue;
|
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
if (device_ext->path == NULL)
|
|
|
|
continue;
|
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
path = ConvertWCharToUtf8Alloc(device_ext->path, NULL);
|
|
|
|
if (!path)
|
2017-07-24 09:53:21 +03:00
|
|
|
continue;
|
2017-07-20 13:35:43 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
/* not plugable device */
|
2017-07-20 13:35:43 +03:00
|
|
|
if (strstr(path, "/Volumes/") == NULL)
|
|
|
|
{
|
|
|
|
free(path);
|
2015-09-07 13:16:03 +03:00
|
|
|
continue;
|
2017-07-20 13:35:43 +03:00
|
|
|
}
|
2015-09-07 13:16:03 +03:00
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2017-07-20 13:35:43 +03:00
|
|
|
if (strstr(path, dev_array[i].path) != NULL)
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
dev_found = TRUE;
|
|
|
|
dev_array[i].to_add = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 17:43:53 +03:00
|
|
|
|
2017-07-20 13:35:43 +03:00
|
|
|
free(path);
|
2015-09-07 13:16:03 +03:00
|
|
|
|
|
|
|
if (!dev_found)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
devman_unregister_device(rdpdr->devman, (void*)keys[j]);
|
2015-09-07 13:16:03 +03:00
|
|
|
ids[0] = keys[j];
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
if ((error = rdpdr_send_device_list_remove_request(rdpdr, 1, ids)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_send_device_list_remove_request failed with error %" PRIu32 "!",
|
|
|
|
error);
|
2015-09-07 13:16:03 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add new devices */
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2021-10-07 11:43:16 +03:00
|
|
|
const hotplug_dev* dev = &dev_array[i];
|
|
|
|
if (dev->to_add)
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
2021-10-07 11:43:16 +03:00
|
|
|
const char* path = dev->path;
|
|
|
|
const char* name = strrchr(path, '/') + 1;
|
|
|
|
error = rdpdr_load_drive(rdpdr, name, path, TRUE);
|
|
|
|
if (error)
|
2015-09-07 13:16:03 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2018-08-17 13:56:45 +03:00
|
|
|
free(keys);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
for (i = 0; i < size; i++)
|
2016-07-21 17:51:42 +03:00
|
|
|
free(dev_array[i].path);
|
2015-09-07 13:16:03 +03:00
|
|
|
|
|
|
|
return error;
|
2016-07-21 17:51:42 +03:00
|
|
|
}
|
2015-09-07 13:16:03 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
static void drive_hotplug_fsevent_callback(ConstFSEventStreamRef streamRef,
|
2019-11-06 17:24:51 +03:00
|
|
|
void* clientCallBackInfo, size_t numEvents,
|
|
|
|
void* eventPaths,
|
|
|
|
const FSEventStreamEventFlags eventFlags[],
|
|
|
|
const FSEventStreamEventId eventIds[])
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
rdpdrPlugin* rdpdr;
|
2019-02-21 19:28:49 +03:00
|
|
|
size_t i;
|
2015-09-07 13:16:03 +03:00
|
|
|
UINT error;
|
2016-07-21 17:51:42 +03:00
|
|
|
char** paths = (char**)eventPaths;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpdr = (rdpdrPlugin*)clientCallBackInfo;
|
2015-09-07 13:16:03 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
for (i = 0; i < numEvents; i++)
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
if (strcmp(paths[i], "/Volumes/") == 0)
|
|
|
|
{
|
|
|
|
if ((error = handle_hotplug(rdpdr)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "handle_hotplug failed with error %" PRIu32 "!",
|
|
|
|
error);
|
2015-09-07 13:16:03 +03:00
|
|
|
}
|
|
|
|
else
|
2023-06-02 12:44:38 +03:00
|
|
|
rdpdr_try_send_device_list_announce_request(rdpdr);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 09:43:01 +03:00
|
|
|
static void first_hotplug(rdpdrPlugin* rdpdr)
|
2016-02-26 21:31:14 +03:00
|
|
|
{
|
|
|
|
UINT error;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2016-02-26 21:31:14 +03:00
|
|
|
if ((error = handle_hotplug(rdpdr)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "handle_hotplug failed with error %" PRIu32 "!", error);
|
2016-02-26 21:31:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI drive_hotplug_thread_func(LPVOID arg)
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
rdpdrPlugin* rdpdr;
|
|
|
|
FSEventStreamRef fsev;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpdr = (rdpdrPlugin*)arg;
|
2015-09-07 13:16:03 +03:00
|
|
|
CFStringRef path = CFSTR("/Volumes/");
|
2019-11-06 17:24:51 +03:00
|
|
|
CFArrayRef pathsToWatch = CFArrayCreate(kCFAllocatorMalloc, (const void**)&path, 1, NULL);
|
2021-07-28 09:43:01 +03:00
|
|
|
FSEventStreamContext ctx = { 0 };
|
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
ctx.info = arg;
|
2021-07-28 09:43:01 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(!rdpdr->stopEvent);
|
|
|
|
rdpdr->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!rdpdr->stopEvent)
|
|
|
|
goto out;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
fsev =
|
|
|
|
FSEventStreamCreate(kCFAllocatorMalloc, drive_hotplug_fsevent_callback, &ctx, pathsToWatch,
|
|
|
|
kFSEventStreamEventIdSinceNow, 1, kFSEventStreamCreateFlagNone);
|
2021-07-28 09:43:01 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
rdpdr->runLoop = CFRunLoopGetCurrent();
|
|
|
|
FSEventStreamScheduleWithRunLoop(fsev, rdpdr->runLoop, kCFRunLoopDefaultMode);
|
|
|
|
FSEventStreamStart(fsev);
|
|
|
|
CFRunLoopRun();
|
|
|
|
FSEventStreamStop(fsev);
|
|
|
|
FSEventStreamRelease(fsev);
|
2021-07-28 09:43:01 +03:00
|
|
|
out:
|
|
|
|
if (rdpdr->stopEvent)
|
|
|
|
{
|
|
|
|
CloseHandle(rdpdr->stopEvent);
|
|
|
|
rdpdr->stopEvent = NULL;
|
|
|
|
}
|
2015-09-07 13:16:03 +03:00
|
|
|
ExitThread(CHANNEL_RC_OK);
|
2018-03-07 14:03:10 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2015-09-07 13:16:03 +03:00
|
|
|
}
|
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
#else
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static const char* automountLocations[] = { "/run/user/%lu/gvfs", "/run/media/%s", "/media/%s",
|
|
|
|
"/media", "/mnt" };
|
2017-04-18 10:13:35 +03:00
|
|
|
|
|
|
|
static BOOL isAutomountLocation(const char* path)
|
|
|
|
{
|
|
|
|
const size_t nrLocations = sizeof(automountLocations) / sizeof(automountLocations[0]);
|
|
|
|
size_t x;
|
2022-01-25 14:27:17 +03:00
|
|
|
char buffer[MAX_PATH] = { 0 };
|
2017-04-18 10:13:35 +03:00
|
|
|
uid_t uid = getuid();
|
2020-06-02 12:33:48 +03:00
|
|
|
char uname[MAX_PATH] = { 0 };
|
2022-01-25 14:27:17 +03:00
|
|
|
ULONG size = sizeof(uname) - 1;
|
2020-05-25 15:01:23 +03:00
|
|
|
|
2022-01-25 14:27:17 +03:00
|
|
|
if (!GetUserNameExA(NameSamCompatible, uname, &size))
|
2020-05-25 15:01:23 +03:00
|
|
|
return FALSE;
|
2017-04-18 10:13:35 +03:00
|
|
|
|
|
|
|
if (!path)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (x = 0; x < nrLocations; x++)
|
|
|
|
{
|
|
|
|
const char* location = automountLocations[x];
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
if (strstr(location, "%lu"))
|
|
|
|
snprintf(buffer, sizeof(buffer), location, (unsigned long)uid);
|
|
|
|
else if (strstr(location, "%s"))
|
|
|
|
snprintf(buffer, sizeof(buffer), location, uname);
|
|
|
|
else
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s", location);
|
|
|
|
|
|
|
|
length = strnlen(buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
if (strncmp(buffer, path, length) == 0)
|
|
|
|
{
|
|
|
|
const char* rest = &path[length];
|
|
|
|
|
|
|
|
/* Only consider mount locations with max depth of 1 below the
|
|
|
|
* base path or the base path itself. */
|
|
|
|
if (*rest == '\0')
|
|
|
|
return TRUE;
|
|
|
|
else if (*rest == '/')
|
|
|
|
{
|
|
|
|
const char* token = strstr(&rest[1], "/");
|
|
|
|
|
|
|
|
if (!token || (token[1] == '\0'))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:36:24 +03:00
|
|
|
#define MAX_USB_DEVICES 100
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2014-02-20 13:42:02 +04:00
|
|
|
{
|
2020-07-27 12:36:24 +03:00
|
|
|
char* path;
|
|
|
|
BOOL to_add;
|
|
|
|
} hotplug_dev;
|
|
|
|
|
|
|
|
static void handle_mountpoint(hotplug_dev* dev_array, size_t* size, const char* mountpoint)
|
|
|
|
{
|
|
|
|
if (!mountpoint)
|
|
|
|
return;
|
|
|
|
/* copy hotpluged device mount point to the dev_array */
|
|
|
|
if (isAutomountLocation(mountpoint) && (*size < MAX_USB_DEVICES))
|
|
|
|
{
|
|
|
|
dev_array[*size].path = _strdup(mountpoint);
|
2022-01-25 17:28:27 +03:00
|
|
|
dev_array[*size].to_add = TRUE;
|
2020-07-27 12:36:24 +03:00
|
|
|
(*size)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:22:34 +03:00
|
|
|
#ifdef __sun
|
2020-07-27 12:36:24 +03:00
|
|
|
#include <sys/mnttab.h>
|
2022-12-22 02:32:17 +03:00
|
|
|
static UINT handle_platform_mounts_sun(wLog* log, hotplug_dev* dev_array, size_t* size)
|
2020-07-27 12:36:24 +03:00
|
|
|
{
|
|
|
|
FILE* f;
|
2020-07-23 14:22:34 +03:00
|
|
|
struct mnttab ent;
|
2021-09-08 16:37:45 +03:00
|
|
|
f = winpr_fopen("/etc/mnttab", "r");
|
2020-07-27 12:36:24 +03:00
|
|
|
if (f == NULL)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(log, WLOG_ERROR, "fopen failed!");
|
2020-07-27 12:36:24 +03:00
|
|
|
return ERROR_OPEN_FAILED;
|
|
|
|
}
|
|
|
|
while (getmntent(f, &ent) == 0)
|
|
|
|
{
|
|
|
|
handle_mountpoint(dev_array, size, ent.mnt_mountp);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2020-07-23 14:22:34 +03:00
|
|
|
#endif
|
2019-08-23 11:08:59 +03:00
|
|
|
|
2020-07-27 12:36:24 +03:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
|
|
|
#include <sys/mount.h>
|
2022-12-22 02:32:17 +03:00
|
|
|
static UINT handle_platform_mounts_bsd(wLog* log, hotplug_dev* dev_array, size_t* size)
|
2020-07-27 12:36:24 +03:00
|
|
|
{
|
|
|
|
int mntsize;
|
|
|
|
size_t idx;
|
|
|
|
struct statfs* mntbuf = NULL;
|
|
|
|
|
|
|
|
mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
|
|
|
|
if (!mntsize)
|
|
|
|
{
|
|
|
|
/* TODO: handle 'errno' */
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(log, WLOG_ERROR, "getmntinfo failed!");
|
2020-07-27 12:36:24 +03:00
|
|
|
return ERROR_OPEN_FAILED;
|
|
|
|
}
|
|
|
|
for (idx = 0; idx < (size_t)mntsize; idx++)
|
|
|
|
{
|
|
|
|
handle_mountpoint(dev_array, size, mntbuf[idx].f_mntonname);
|
|
|
|
}
|
|
|
|
free(mntbuf);
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2020-07-23 14:22:34 +03:00
|
|
|
#endif
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2020-07-27 12:36:24 +03:00
|
|
|
#if defined(__LINUX__) || defined(__linux__)
|
|
|
|
#include <mntent.h>
|
2022-12-22 02:32:17 +03:00
|
|
|
static UINT handle_platform_mounts_linux(wLog* log, hotplug_dev* dev_array, size_t* size)
|
2020-07-27 12:36:24 +03:00
|
|
|
{
|
|
|
|
FILE* f;
|
|
|
|
struct mntent* ent;
|
2021-09-08 16:37:45 +03:00
|
|
|
f = winpr_fopen("/proc/mounts", "r");
|
2014-02-20 13:42:02 +04:00
|
|
|
if (f == NULL)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(log, WLOG_ERROR, "fopen failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return ERROR_OPEN_FAILED;
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
2019-11-18 16:28:39 +03:00
|
|
|
while ((ent = getmntent(f)) != NULL)
|
2014-02-20 13:42:02 +04:00
|
|
|
{
|
2020-07-27 12:36:24 +03:00
|
|
|
handle_mountpoint(dev_array, size, ent->mnt_dir);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-12-22 02:32:17 +03:00
|
|
|
static UINT handle_platform_mounts(wLog* log, hotplug_dev* dev_array, size_t* size)
|
2020-07-27 12:36:24 +03:00
|
|
|
{
|
2020-07-23 14:22:34 +03:00
|
|
|
#ifdef __sun
|
2022-12-22 02:32:17 +03:00
|
|
|
return handle_platform_mounts_sun(log, dev_array, size);
|
2020-07-27 12:36:24 +03:00
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
|
2022-12-22 02:32:17 +03:00
|
|
|
return handle_platform_mounts_bsd(log, dev_array, size);
|
2020-07-27 12:36:24 +03:00
|
|
|
#elif defined(__LINUX__) || defined(__linux__)
|
2022-12-22 02:32:17 +03:00
|
|
|
return handle_platform_mounts_linux(log, dev_array, size);
|
2020-07-23 14:22:34 +03:00
|
|
|
#endif
|
2020-07-27 12:36:24 +03:00
|
|
|
return ERROR_CALL_NOT_IMPLEMENTED;
|
|
|
|
}
|
2020-07-23 14:22:34 +03:00
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
static BOOL device_not_plugged(ULONG_PTR key, void* element, void* data)
|
|
|
|
{
|
|
|
|
const WCHAR* path = (const WCHAR*)data;
|
|
|
|
DEVICE_DRIVE_EXT* device_ext = (DEVICE_DRIVE_EXT*)element;
|
|
|
|
|
|
|
|
WINPR_UNUSED(key);
|
|
|
|
WINPR_ASSERT(path);
|
|
|
|
|
|
|
|
if (!device_ext || (device_ext->device.type != RDPDR_DTYP_FILESYSTEM) || !device_ext->path)
|
|
|
|
return TRUE;
|
|
|
|
if (_wcscmp(device_ext->path, path) != 0)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-01-15 12:41:12 +03:00
|
|
|
static BOOL device_already_plugged(rdpdrPlugin* rdpdr, const hotplug_dev* device)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
2021-01-27 11:25:26 +03:00
|
|
|
WCHAR* path = NULL;
|
2021-01-15 12:41:12 +03:00
|
|
|
|
|
|
|
if (!rdpdr || !device)
|
|
|
|
return TRUE;
|
|
|
|
if (!device->to_add)
|
|
|
|
return TRUE;
|
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr->devman);
|
2022-10-28 09:09:27 +03:00
|
|
|
WINPR_ASSERT(device->path);
|
2021-07-28 09:02:59 +03:00
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
path = ConvertUtf8ToWCharAlloc(device->path, NULL);
|
|
|
|
if (!path)
|
2021-01-27 11:25:26 +03:00
|
|
|
return TRUE;
|
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
rc = device_foreach(rdpdr, TRUE, device_not_plugged, path);
|
2021-01-27 11:25:26 +03:00
|
|
|
free(path);
|
2021-07-28 11:37:03 +03:00
|
|
|
return !rc;
|
2021-01-15 12:41:12 +03:00
|
|
|
}
|
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
struct hotplug_delete_arg
|
2020-07-27 12:36:24 +03:00
|
|
|
{
|
2021-07-28 11:37:03 +03:00
|
|
|
hotplug_dev* dev_array;
|
|
|
|
size_t dev_array_size;
|
|
|
|
rdpdrPlugin* rdpdr;
|
|
|
|
};
|
2020-07-27 12:36:24 +03:00
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
static BOOL hotplug_delete_foreach(ULONG_PTR key, void* element, void* data)
|
|
|
|
{
|
|
|
|
char* path = NULL;
|
|
|
|
BOOL dev_found = FALSE;
|
|
|
|
struct hotplug_delete_arg* arg = (struct hotplug_delete_arg*)data;
|
|
|
|
DEVICE_DRIVE_EXT* device_ext = (DEVICE_DRIVE_EXT*)element;
|
|
|
|
|
|
|
|
WINPR_ASSERT(arg);
|
|
|
|
WINPR_ASSERT(arg->rdpdr);
|
|
|
|
WINPR_ASSERT(arg->dev_array || (arg->dev_array_size == 0));
|
|
|
|
|
|
|
|
if (!device_ext || (device_ext->device.type != RDPDR_DTYP_FILESYSTEM) || !device_ext->path ||
|
|
|
|
!device_ext->automount)
|
|
|
|
return TRUE;
|
2021-07-28 09:02:59 +03:00
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
WINPR_ASSERT(device_ext->path);
|
|
|
|
path = ConvertWCharToUtf8Alloc(device_ext->path, NULL);
|
|
|
|
if (!path)
|
2021-07-28 11:37:03 +03:00
|
|
|
return FALSE;
|
2014-02-20 13:42:02 +04:00
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
/* not plugable device */
|
|
|
|
if (isAutomountLocation(path))
|
2014-02-20 13:42:02 +04:00
|
|
|
{
|
2021-07-28 11:37:03 +03:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < arg->dev_array_size; i++)
|
2014-02-20 13:42:02 +04:00
|
|
|
{
|
2021-07-28 11:37:03 +03:00
|
|
|
hotplug_dev* cur = &arg->dev_array[i];
|
|
|
|
if (strstr(path, cur->path) != NULL)
|
2014-02-20 13:42:02 +04:00
|
|
|
{
|
2021-07-28 11:37:03 +03:00
|
|
|
dev_found = TRUE;
|
|
|
|
cur->to_add = FALSE;
|
|
|
|
break;
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
}
|
2021-07-28 11:37:03 +03:00
|
|
|
}
|
2014-02-20 13:42:02 +04:00
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
free(path);
|
2017-04-13 17:20:01 +03:00
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
if (!dev_found)
|
|
|
|
{
|
|
|
|
UINT error;
|
|
|
|
UINT32 ids[1] = { key };
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
WINPR_ASSERT(arg->rdpdr->devman);
|
|
|
|
devman_unregister_device(arg->rdpdr->devman, (void*)key);
|
|
|
|
WINPR_ASSERT(key <= UINT32_MAX);
|
|
|
|
|
|
|
|
error = rdpdr_send_device_list_remove_request(arg->rdpdr, 1, ids);
|
|
|
|
if (error)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(arg->rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_send_device_list_remove_request failed with error %" PRIu32 "!",
|
|
|
|
error);
|
2021-07-28 11:37:03 +03:00
|
|
|
return FALSE;
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT handle_hotplug(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
|
|
|
hotplug_dev dev_array[MAX_USB_DEVICES] = { 0 };
|
|
|
|
size_t i;
|
|
|
|
size_t size = 0;
|
|
|
|
UINT error = ERROR_SUCCESS;
|
|
|
|
struct hotplug_delete_arg arg = { dev_array, ARRAYSIZE(dev_array), rdpdr };
|
|
|
|
|
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
WINPR_ASSERT(rdpdr->devman);
|
|
|
|
|
2022-12-22 02:32:17 +03:00
|
|
|
error = handle_platform_mounts(rdpdr->log, dev_array, &size);
|
2021-07-28 11:37:03 +03:00
|
|
|
|
|
|
|
/* delete removed devices */
|
|
|
|
/* Ignore result */ device_foreach(rdpdr, FALSE, hotplug_delete_foreach, &arg);
|
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
/* add new devices */
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2021-07-28 09:43:01 +03:00
|
|
|
hotplug_dev* cur = &dev_array[i];
|
|
|
|
if (!device_already_plugged(rdpdr, cur))
|
2014-02-20 13:42:02 +04:00
|
|
|
{
|
2021-10-07 11:43:16 +03:00
|
|
|
const char* path = cur->path;
|
|
|
|
const char* name = strrchr(path, '/') + 1;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2021-10-07 11:43:16 +03:00
|
|
|
rdpdr_load_drive(rdpdr, name, path, TRUE);
|
2020-10-22 15:42:27 +03:00
|
|
|
error = ERROR_DISK_CHANGE;
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-28 12:17:20 +03:00
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
2016-07-21 17:51:42 +03:00
|
|
|
free(dev_array[i].path);
|
2015-08-28 12:17:20 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
return error;
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
|
2016-08-09 13:04:06 +03:00
|
|
|
static void first_hotplug(rdpdrPlugin* rdpdr)
|
2016-02-26 21:31:14 +03:00
|
|
|
{
|
|
|
|
UINT error;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
2016-02-26 21:31:14 +03:00
|
|
|
if ((error = handle_hotplug(rdpdr)))
|
|
|
|
{
|
2021-07-27 16:03:35 +03:00
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case ERROR_DISK_CHANGE:
|
|
|
|
case CHANNEL_RC_OK:
|
|
|
|
case ERROR_OPEN_FAILED:
|
|
|
|
case ERROR_CALL_NOT_IMPLEMENTED:
|
|
|
|
break;
|
|
|
|
default:
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "handle_hotplug failed with error %" PRIu32 "!",
|
|
|
|
error);
|
2021-07-27 16:03:35 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-02-26 21:31:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI drive_hotplug_thread_func(LPVOID arg)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2014-02-20 13:42:02 +04:00
|
|
|
rdpdrPlugin* rdpdr;
|
2016-03-02 04:10:22 +03:00
|
|
|
UINT error = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpdr = (rdpdrPlugin*)arg;
|
2014-03-26 00:37:47 +04:00
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
2021-07-28 09:43:01 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(!rdpdr->stopEvent);
|
|
|
|
rdpdr->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!rdpdr->stopEvent)
|
|
|
|
goto out;
|
|
|
|
|
2022-04-28 10:56:11 +03:00
|
|
|
while (WaitForSingleObject(rdpdr->stopEvent, 1000) == WAIT_TIMEOUT)
|
2014-02-20 13:42:02 +04:00
|
|
|
{
|
2020-10-22 15:42:27 +03:00
|
|
|
error = handle_hotplug(rdpdr);
|
|
|
|
switch (error)
|
2014-02-20 13:42:02 +04:00
|
|
|
{
|
2020-10-22 15:42:27 +03:00
|
|
|
case ERROR_DISK_CHANGE:
|
2023-06-02 12:44:38 +03:00
|
|
|
rdpdr_try_send_device_list_announce_request(rdpdr);
|
2020-10-22 15:42:27 +03:00
|
|
|
break;
|
|
|
|
case CHANNEL_RC_OK:
|
|
|
|
case ERROR_OPEN_FAILED:
|
|
|
|
case ERROR_CALL_NOT_IMPLEMENTED:
|
|
|
|
break;
|
|
|
|
default:
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "handle_hotplug failed with error %" PRIu32 "!",
|
|
|
|
error);
|
2016-01-27 21:26:52 +03:00
|
|
|
goto out;
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
out:
|
2020-10-22 15:42:27 +03:00
|
|
|
error = GetLastError();
|
2016-01-27 21:26:52 +03:00
|
|
|
if (error && rdpdr->rdpcontext)
|
2023-01-23 14:03:18 +03:00
|
|
|
setChannelError(rdpdr->rdpcontext, error, "reported an error");
|
2021-07-28 09:43:01 +03:00
|
|
|
|
|
|
|
if (rdpdr->stopEvent)
|
|
|
|
{
|
|
|
|
CloseHandle(rdpdr->stopEvent);
|
|
|
|
rdpdr->stopEvent = NULL;
|
|
|
|
}
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
ExitThread(error);
|
|
|
|
return error;
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
|
|
|
|
2018-12-12 11:20:42 +03:00
|
|
|
#endif
|
|
|
|
|
2019-11-18 16:28:39 +03:00
|
|
|
#if !defined(_WIN32) && !defined(__IOS__)
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2016-01-27 21:26:52 +03:00
|
|
|
UINT error;
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
|
2014-03-26 00:37:47 +04:00
|
|
|
if (rdpdr->hotplugThread)
|
|
|
|
{
|
2021-07-28 09:43:01 +03:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
if (rdpdr->stopEvent)
|
|
|
|
SetEvent(rdpdr->stopEvent);
|
|
|
|
#endif
|
2018-12-12 11:20:42 +03:00
|
|
|
#ifdef __MACOSX__
|
|
|
|
CFRunLoopStop(rdpdr->runLoop);
|
|
|
|
#endif
|
2014-03-26 00:37:47 +04:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
if (WaitForSingleObject(rdpdr->hotplugThread, INFINITE) == WAIT_FAILED)
|
2016-01-27 21:26:52 +03:00
|
|
|
{
|
|
|
|
error = GetLastError();
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "!",
|
|
|
|
error);
|
2016-01-27 21:26:52 +03:00
|
|
|
return error;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 11:20:42 +03:00
|
|
|
CloseHandle(rdpdr->hotplugThread);
|
2014-03-26 00:37:47 +04:00
|
|
|
rdpdr->hotplugThread = NULL;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2016-01-27 21:26:52 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_process_connect(rdpdrPlugin* rdpdr)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
2014-02-10 10:06:11 +04:00
|
|
|
UINT32 index;
|
2012-11-09 04:01:52 +04:00
|
|
|
rdpSettings* settings;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2021-07-28 09:02:59 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
|
2013-10-17 23:42:51 +04:00
|
|
|
rdpdr->devman = devman_new(rdpdr);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!rdpdr->devman)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "devman_new failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:04:30 +03:00
|
|
|
WINPR_ASSERT(rdpdr->rdpcontext);
|
|
|
|
settings = rdpdr->rdpcontext->settings;
|
|
|
|
WINPR_ASSERT(settings);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2023-04-21 20:25:14 +03:00
|
|
|
rdpdr->ignoreInvalidDevices = settings->IgnoreInvalidDevices;
|
|
|
|
|
2014-12-30 15:34:11 +03:00
|
|
|
if (settings->ClientHostname)
|
2019-11-06 17:24:51 +03:00
|
|
|
strncpy(rdpdr->computerName, settings->ClientHostname, sizeof(rdpdr->computerName) - 1);
|
2014-12-30 15:34:11 +03:00
|
|
|
else
|
2019-11-06 17:24:51 +03:00
|
|
|
strncpy(rdpdr->computerName, settings->ComputerName, sizeof(rdpdr->computerName) - 1);
|
2012-10-07 18:21:54 +04:00
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
for (index = 0; index < settings->DeviceCount; index++)
|
|
|
|
{
|
2019-10-24 15:34:26 +03:00
|
|
|
const RDPDR_DEVICE* device = settings->DeviceArray[index];
|
2014-03-26 00:37:47 +04:00
|
|
|
|
2018-01-25 17:43:53 +03:00
|
|
|
if (device->Type == RDPDR_DTYP_FILESYSTEM)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2019-08-23 11:08:59 +03:00
|
|
|
const char DynamicDrives[] = "DynamicDrives";
|
2019-10-24 15:34:26 +03:00
|
|
|
const RDPDR_DRIVE* drive = (const RDPDR_DRIVE*)device;
|
2019-08-23 11:08:59 +03:00
|
|
|
BOOL hotplugAll = strncmp(drive->Path, "*", 2) == 0;
|
|
|
|
BOOL hotplugLater = strncmp(drive->Path, DynamicDrives, sizeof(DynamicDrives)) == 0;
|
2021-07-28 09:43:01 +03:00
|
|
|
|
2019-08-23 11:08:59 +03:00
|
|
|
if (drive->Path && (hotplugAll || hotplugLater))
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2019-08-23 11:08:59 +03:00
|
|
|
if (hotplugAll)
|
|
|
|
first_hotplug(rdpdr);
|
2018-12-12 11:20:42 +03:00
|
|
|
|
2021-07-28 09:43:01 +03:00
|
|
|
/* There might be multiple hotplug related device entries.
|
|
|
|
* Ensure the thread is only started once
|
|
|
|
*/
|
|
|
|
if (!rdpdr->hotplugThread)
|
2018-12-12 11:20:42 +03:00
|
|
|
{
|
2021-07-28 09:43:01 +03:00
|
|
|
rdpdr->hotplugThread =
|
|
|
|
CreateThread(NULL, 0, drive_hotplug_thread_func, rdpdr, 0, NULL);
|
|
|
|
if (!rdpdr->hotplugThread)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "CreateThread failed!");
|
2021-07-28 09:43:01 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2018-01-25 17:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
2014-03-26 00:37:47 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((error = devman_load_device_service(rdpdr->devman, device, rdpdr->rdpcontext)))
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"devman_load_device_service failed with error %" PRIu32 "!", error);
|
2015-06-03 14:38:47 +03:00
|
|
|
return error;
|
|
|
|
}
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
return error;
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpdr_process_server_announce_request(rdpdrPlugin* rdpdr, wStream* s)
|
2011-08-05 06:24:46 +04:00
|
|
|
{
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
2022-12-22 02:32:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLengthWLog(rdpdr->log, s, 8))
|
2016-02-03 13:04:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2023-01-09 12:41:46 +03:00
|
|
|
Stream_Read_UINT16(s, rdpdr->serverVersionMajor);
|
|
|
|
Stream_Read_UINT16(s, rdpdr->serverVersionMinor);
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Read_UINT32(s, rdpdr->clientID);
|
2014-05-07 02:19:54 +04:00
|
|
|
rdpdr->sequenceId++;
|
2023-01-09 12:41:46 +03:00
|
|
|
|
|
|
|
rdpdr->clientVersionMajor = MIN(RDPDR_VERSION_MAJOR, rdpdr->serverVersionMajor);
|
|
|
|
rdpdr->clientVersionMinor = MIN(RDPDR_VERSION_MINOR_RDP10X, rdpdr->serverVersionMinor);
|
|
|
|
WLog_Print(rdpdr->log, WLOG_DEBUG,
|
|
|
|
"[rdpdr] server announces version %" PRIu32 ".%" PRIu32 ", client uses %" PRIu32
|
|
|
|
".%" PRIu32,
|
|
|
|
rdpdr->serverVersionMajor, rdpdr->serverVersionMinor, rdpdr->clientVersionMajor,
|
|
|
|
rdpdr->clientVersionMinor);
|
2016-01-24 17:21:06 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_send_client_announce_reply(rdpdrPlugin* rdpdr)
|
2011-08-05 06:24:46 +04:00
|
|
|
{
|
2013-10-17 22:19:33 +04:00
|
|
|
wStream* s;
|
2021-07-28 09:02:59 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(rdpdr);
|
2023-06-02 12:44:38 +03:00
|
|
|
WINPR_ASSERT(rdpdr->state == RDPDR_CHANNEL_STATE_ANNOUNCE);
|
|
|
|
rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY);
|
2021-07-28 09:02:59 +03:00
|
|
|
|
2022-06-27 13:05:35 +03:00
|
|
|
s = StreamPool_Take(rdpdr->pool, 12);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
|
2016-01-27 21:26:52 +03:00
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE); /* Component (2 bytes) */
|
2014-12-28 22:56:13 +03:00
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_CLIENTID_CONFIRM); /* PacketId (2 bytes) */
|
2023-01-09 12:41:46 +03:00
|
|
|
Stream_Write_UINT16(s, rdpdr->clientVersionMajor);
|
|
|
|
Stream_Write_UINT16(s, rdpdr->clientVersionMinor);
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT32(s, (UINT32)rdpdr->clientID);
|
2015-06-03 14:38:47 +03:00
|
|
|
return rdpdr_send(rdpdr, s);
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_send_client_name_request(rdpdrPlugin* rdpdr)
|
2011-08-05 06:24:46 +04:00
|
|
|
{
|
2013-10-17 22:19:33 +04:00
|
|
|
wStream* s;
|
2013-01-11 00:30:32 +04:00
|
|
|
WCHAR* computerNameW = NULL;
|
2022-10-28 09:09:27 +03:00
|
|
|
size_t computerNameLenW;
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
2023-06-02 12:44:38 +03:00
|
|
|
WINPR_ASSERT(rdpdr->state == RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY);
|
|
|
|
rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_NAME_REQUEST);
|
2021-07-28 09:02:59 +03:00
|
|
|
|
2011-08-05 06:24:46 +04:00
|
|
|
if (!rdpdr->computerName[0])
|
2022-03-15 17:19:41 +03:00
|
|
|
{
|
|
|
|
DWORD size = sizeof(rdpdr->computerName) - 1;
|
|
|
|
GetComputerNameA(rdpdr->computerName, &size);
|
|
|
|
}
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
WINPR_ASSERT(rdpdr->computerName);
|
|
|
|
computerNameW = ConvertUtf8ToWCharAlloc(rdpdr->computerName, &computerNameLenW);
|
|
|
|
computerNameLenW *= sizeof(WCHAR);
|
2021-06-18 11:55:18 +03:00
|
|
|
WINPR_ASSERT(computerNameLenW >= 0);
|
2022-06-27 13:05:35 +03:00
|
|
|
|
2022-12-08 12:36:16 +03:00
|
|
|
if (computerNameLenW > 0)
|
|
|
|
computerNameLenW += sizeof(WCHAR); // also write '\0'
|
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
s = StreamPool_Take(rdpdr->pool, 16U + computerNameLenW);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
2018-08-17 13:56:45 +03:00
|
|
|
free(computerNameW);
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE); /* Component (2 bytes) */
|
2014-12-28 22:56:13 +03:00
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_CLIENT_NAME); /* PacketId (2 bytes) */
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT32(s, 1); /* unicodeFlag, 0 for ASCII and 1 for Unicode */
|
|
|
|
Stream_Write_UINT32(s, 0); /* codePage, must be set to zero */
|
2021-07-06 17:44:45 +03:00
|
|
|
Stream_Write_UINT32(s,
|
|
|
|
(UINT32)computerNameLenW); /* computerNameLen, including null terminator */
|
2021-06-18 11:55:18 +03:00
|
|
|
Stream_Write(s, computerNameW, (size_t)computerNameLenW);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(computerNameW);
|
2015-06-03 14:38:47 +03:00
|
|
|
return rdpdr_send(rdpdr, s);
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpdr_process_server_clientid_confirm(rdpdrPlugin* rdpdr, wStream* s)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 versionMajor;
|
|
|
|
UINT16 versionMinor;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 clientID;
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
2022-12-22 02:32:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLengthWLog(rdpdr->log, s, 8))
|
2016-02-03 13:04:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Read_UINT16(s, versionMajor);
|
|
|
|
Stream_Read_UINT16(s, versionMinor);
|
|
|
|
Stream_Read_UINT32(s, clientID);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2023-01-09 12:41:46 +03:00
|
|
|
if (versionMajor != rdpdr->clientVersionMajor || versionMinor != rdpdr->clientVersionMinor)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2023-01-09 12:41:46 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_WARN,
|
|
|
|
"[rdpdr] server announced version %" PRIu32 ".%" PRIu32 ", client uses %" PRIu32
|
|
|
|
".%" PRIu32 " but clientid confirm requests version %" PRIu32 ".%" PRIu32,
|
|
|
|
rdpdr->serverVersionMajor, rdpdr->serverVersionMinor, rdpdr->clientVersionMajor,
|
|
|
|
rdpdr->clientVersionMinor, versionMajor, versionMinor);
|
|
|
|
rdpdr->clientVersionMajor = versionMajor;
|
|
|
|
rdpdr->clientVersionMinor = versionMinor;
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clientID != rdpdr->clientID)
|
|
|
|
rdpdr->clientID = clientID;
|
2016-01-24 17:21:06 +03:00
|
|
|
|
|
|
|
return CHANNEL_RC_OK;
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
struct device_announce_arg
|
|
|
|
{
|
|
|
|
rdpdrPlugin* rdpdr;
|
|
|
|
wStream* s;
|
|
|
|
BOOL userLoggedOn;
|
|
|
|
UINT32 count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BOOL device_announce(ULONG_PTR key, void* element, void* data)
|
|
|
|
{
|
|
|
|
struct device_announce_arg* arg = data;
|
|
|
|
rdpdrPlugin* rdpdr;
|
|
|
|
DEVICE* device = (DEVICE*)element;
|
|
|
|
|
|
|
|
WINPR_UNUSED(key);
|
|
|
|
|
|
|
|
WINPR_ASSERT(arg);
|
|
|
|
WINPR_ASSERT(device);
|
|
|
|
WINPR_ASSERT(arg->rdpdr);
|
|
|
|
WINPR_ASSERT(arg->s);
|
|
|
|
|
|
|
|
rdpdr = arg->rdpdr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. versionMinor 0x0005 doesn't send PAKID_CORE_USER_LOGGEDON
|
|
|
|
* so all devices should be sent regardless of user_loggedon
|
|
|
|
* 2. smartcard devices should be always sent
|
|
|
|
* 3. other devices are sent only after user_loggedon
|
|
|
|
*/
|
|
|
|
|
2023-01-09 12:41:46 +03:00
|
|
|
if ((rdpdr->clientVersionMinor == RDPDR_VERSION_MINOR_RDP51) ||
|
|
|
|
(device->type == RDPDR_DTYP_SMARTCARD) || arg->userLoggedOn)
|
2021-07-28 11:37:03 +03:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t data_len = (device->data == NULL ? 0 : Stream_GetPosition(device->data));
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(arg->s, 20 + data_len))
|
|
|
|
{
|
2022-06-24 17:30:17 +03:00
|
|
|
Stream_Release(arg->s);
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
|
2021-07-28 11:37:03 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Write_UINT32(arg->s, device->type); /* deviceType */
|
|
|
|
Stream_Write_UINT32(arg->s, device->id); /* deviceID */
|
2023-06-07 16:38:21 +03:00
|
|
|
strncpy(Stream_Pointer(arg->s), device->name, 8);
|
2021-07-28 11:37:03 +03:00
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
BYTE c;
|
|
|
|
Stream_Peek_UINT8(arg->s, c);
|
|
|
|
|
|
|
|
if (c > 0x7F)
|
|
|
|
Stream_Write_UINT8(arg->s, '_');
|
|
|
|
else
|
|
|
|
Stream_Seek_UINT8(arg->s);
|
|
|
|
}
|
|
|
|
|
|
|
|
WINPR_ASSERT(data_len <= UINT32_MAX);
|
|
|
|
Stream_Write_UINT32(arg->s, (UINT32)data_len);
|
|
|
|
|
|
|
|
if (data_len > 0)
|
|
|
|
Stream_Write(arg->s, Stream_Buffer(device->data), data_len);
|
|
|
|
|
|
|
|
arg->count++;
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_INFO,
|
|
|
|
"registered [%09s] device #%" PRIu32 ": %s (type=%" PRIu32 " id=%" PRIu32 ")",
|
|
|
|
rdpdr_device_type_string(device->type), arg->count, device->name, device->type,
|
|
|
|
device->id);
|
2021-07-28 11:37:03 +03:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpdr_send_device_list_announce_request(rdpdrPlugin* rdpdr, BOOL userLoggedOn)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2017-12-11 12:25:21 +03:00
|
|
|
size_t pos;
|
2013-10-17 22:19:33 +04:00
|
|
|
wStream* s;
|
2017-12-11 12:25:21 +03:00
|
|
|
size_t count_pos;
|
2021-07-28 11:37:03 +03:00
|
|
|
struct device_announce_arg arg = { 0 };
|
2021-07-28 09:02:59 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
WINPR_ASSERT(rdpdr->devman);
|
|
|
|
|
2023-06-02 12:44:38 +03:00
|
|
|
if (userLoggedOn)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(rdpdr->state >= RDPDR_CHANNEL_STATE_READY);
|
|
|
|
rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_USER_LOGGEDON);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(rdpdr->state >= RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM);
|
|
|
|
rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_READY);
|
|
|
|
}
|
|
|
|
|
2022-06-24 17:30:17 +03:00
|
|
|
s = StreamPool_Take(rdpdr->pool, 256);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE); /* Component (2 bytes) */
|
2014-12-28 22:56:13 +03:00
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_ANNOUNCE); /* PacketId (2 bytes) */
|
2017-12-11 12:25:21 +03:00
|
|
|
count_pos = Stream_GetPosition(s);
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Seek_UINT32(s); /* deviceCount */
|
2013-10-18 00:30:36 +04:00
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
arg.rdpdr = rdpdr;
|
|
|
|
arg.userLoggedOn = userLoggedOn;
|
|
|
|
arg.s = s;
|
|
|
|
if (!device_foreach(rdpdr, TRUE, device_announce, &arg))
|
|
|
|
return ERROR_INVALID_DATA;
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2022-12-22 12:16:11 +03:00
|
|
|
if (arg.count == 0)
|
|
|
|
{
|
|
|
|
Stream_Release(s);
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
}
|
2017-12-11 12:25:21 +03:00
|
|
|
pos = Stream_GetPosition(s);
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_SetPosition(s, count_pos);
|
2021-07-28 11:37:03 +03:00
|
|
|
Stream_Write_UINT32(s, arg.count);
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_SetPosition(s, pos);
|
|
|
|
Stream_SealLength(s);
|
2015-06-03 14:38:47 +03:00
|
|
|
return rdpdr_send(rdpdr, s);
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
2023-06-02 12:44:38 +03:00
|
|
|
UINT rdpdr_try_send_device_list_announce_request(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
if (rdpdr->state != RDPDR_CHANNEL_STATE_USER_LOGGEDON)
|
|
|
|
{
|
|
|
|
WLog_Print(rdpdr->log, WLOG_DEBUG,
|
|
|
|
"hotplug event received, but channel [RDPDR] is not ready (state %s), ignoring.",
|
|
|
|
rdpdr_state_str(rdpdr->state));
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
}
|
|
|
|
return rdpdr_send_device_list_announce_request(rdpdr, TRUE);
|
|
|
|
}
|
|
|
|
|
2019-12-06 17:14:43 +03:00
|
|
|
static UINT dummy_irp_response(rdpdrPlugin* rdpdr, wStream* s)
|
|
|
|
{
|
2021-07-28 09:02:59 +03:00
|
|
|
wStream* output;
|
2019-12-06 17:14:43 +03:00
|
|
|
UINT32 DeviceId;
|
|
|
|
UINT32 FileId;
|
|
|
|
UINT32 CompletionId;
|
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
2022-06-27 13:05:35 +03:00
|
|
|
output = StreamPool_Take(rdpdr->pool, 256); // RDPDR_DEVICE_IO_RESPONSE_LENGTH
|
2019-12-06 17:14:43 +03:00
|
|
|
if (!output)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
|
2019-12-06 17:14:43 +03:00
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_SetPosition(s, 4); /* see "rdpdr_process_receive" */
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, DeviceId); /* DeviceId (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, FileId); /* FileId (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, CompletionId); /* CompletionId (4 bytes) */
|
|
|
|
|
2021-12-14 14:59:16 +03:00
|
|
|
if (!rdpdr_write_iocompletion_header(output, DeviceId, CompletionId,
|
|
|
|
(UINT32)STATUS_UNSUCCESSFUL))
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
2019-12-06 17:14:43 +03:00
|
|
|
|
|
|
|
return rdpdr_send(rdpdr, output);
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_process_irp(rdpdrPlugin* rdpdr, wStream* s)
|
2011-08-05 21:54:40 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2021-07-28 09:02:59 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
2022-12-22 02:32:17 +03:00
|
|
|
irp = irp_new(rdpdr->devman, rdpdr->pool, s, rdpdr->log, &error);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2013-10-17 23:42:51 +04:00
|
|
|
if (!irp)
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "irp_new failed with %" PRIu32 "!", error);
|
2019-12-06 17:14:43 +03:00
|
|
|
|
2023-04-21 20:25:14 +03:00
|
|
|
if (error == CHANNEL_RC_OK || (error == ERROR_DEV_NOT_EXIST && rdpdr->ignoreInvalidDevices))
|
2019-12-06 17:14:43 +03:00
|
|
|
{
|
|
|
|
return dummy_irp_response(rdpdr, s);
|
|
|
|
}
|
|
|
|
|
2016-01-28 14:05:14 +03:00
|
|
|
return error;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2011-08-05 21:54:40 +04:00
|
|
|
|
2022-11-08 15:55:27 +03:00
|
|
|
if (irp->device->IRPRequest)
|
|
|
|
IFCALLRET(irp->device->IRPRequest, error, irp->device, irp);
|
|
|
|
else
|
|
|
|
irp->Discard(irp);
|
2011-08-05 21:54:40 +04:00
|
|
|
|
2022-11-08 15:55:27 +03:00
|
|
|
if (error != CHANNEL_RC_OK)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "device->IRPRequest failed with error %" PRIu32 "",
|
|
|
|
error);
|
2022-11-08 15:55:27 +03:00
|
|
|
irp->Discard(irp);
|
|
|
|
}
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-01-27 21:26:52 +03:00
|
|
|
return error;
|
2011-08-05 21:54:40 +04:00
|
|
|
}
|
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
static UINT rdpdr_process_component(rdpdrPlugin* rdpdr, UINT16 component, UINT16 packetId,
|
|
|
|
wStream* s)
|
|
|
|
{
|
|
|
|
UINT32 type;
|
|
|
|
DEVICE* device;
|
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
switch (component)
|
|
|
|
{
|
|
|
|
case RDPDR_CTYP_PRN:
|
|
|
|
type = RDPDR_DTYP_PRINT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
device = devman_get_device_by_type(rdpdr->devman, type);
|
|
|
|
|
|
|
|
if (!device)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
return IFCALLRESULT(ERROR_INVALID_PARAMETER, device->CustomComponentRequest, device, component,
|
|
|
|
packetId, s);
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2021-07-28 11:37:03 +03:00
|
|
|
static BOOL device_init(ULONG_PTR key, void* element, void* data)
|
2014-04-06 00:57:31 +04:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
wLog* log = data;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2021-07-28 11:37:03 +03:00
|
|
|
DEVICE* device = element;
|
2021-07-28 09:02:59 +03:00
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
WINPR_UNUSED(key);
|
|
|
|
WINPR_UNUSED(data);
|
2021-07-28 09:02:59 +03:00
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
IFCALLRET(device->Init, error, device);
|
2014-04-06 00:57:31 +04:00
|
|
|
|
2021-07-28 11:37:03 +03:00
|
|
|
if (error != CHANNEL_RC_OK)
|
2014-04-06 00:57:31 +04:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(log, WLOG_ERROR, "Device init failed with %s", WTSErrorToString(error));
|
2021-07-28 11:37:03 +03:00
|
|
|
return FALSE;
|
2014-04-06 00:57:31 +04:00
|
|
|
}
|
2021-07-28 11:37:03 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT rdpdr_process_init(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
WINPR_ASSERT(rdpdr->devman);
|
2014-04-06 01:15:17 +04:00
|
|
|
|
2022-12-22 02:32:17 +03:00
|
|
|
if (!device_foreach(rdpdr, TRUE, device_init, rdpdr->log))
|
2021-07-28 11:37:03 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-04-06 00:57:31 +04:00
|
|
|
}
|
|
|
|
|
2023-06-02 12:44:38 +03:00
|
|
|
static BOOL rdpdr_state_check(rdpdrPlugin* rdpdr, UINT16 packetid,
|
|
|
|
enum RDPDR_CHANNEL_STATE expected, enum RDPDR_CHANNEL_STATE next)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
|
|
|
|
const char* strstate = rdpdr_state_str(rdpdr->state);
|
|
|
|
if (rdpdr->state != expected)
|
|
|
|
{
|
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"channel [RDPDR] received %s, expected state %s but have state %s, aborting.",
|
|
|
|
rdpdr_packetid_string(packetid), rdpdr_state_str(expected), strstate);
|
|
|
|
|
|
|
|
rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_INITIAL);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return rdpdr_state_advance(rdpdr, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL rdpdr_check_channel_state(rdpdrPlugin* rdpdr, UINT16 packetid)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
|
|
|
|
switch (packetid)
|
|
|
|
{
|
|
|
|
case PAKID_CORE_SERVER_ANNOUNCE:
|
|
|
|
/* windows servers sometimes send this message.
|
|
|
|
* it seems related to session login (e.g. first initialization for RDP/TLS style login,
|
|
|
|
* then reinitialize the channel after login successful
|
|
|
|
*/
|
|
|
|
rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_INITIAL);
|
|
|
|
return rdpdr_state_check(rdpdr, packetid, RDPDR_CHANNEL_STATE_INITIAL,
|
|
|
|
RDPDR_CHANNEL_STATE_ANNOUNCE);
|
|
|
|
case PAKID_CORE_SERVER_CAPABILITY:
|
|
|
|
return rdpdr_state_check(rdpdr, packetid, RDPDR_CHANNEL_STATE_NAME_REQUEST,
|
|
|
|
RDPDR_CHANNEL_STATE_SERVER_CAPS);
|
|
|
|
case PAKID_CORE_CLIENTID_CONFIRM:
|
|
|
|
return rdpdr_state_check(rdpdr, packetid, RDPDR_CHANNEL_STATE_CLIENT_CAPS,
|
|
|
|
RDPDR_CHANNEL_STATE_CLIENTID_CONFIRM);
|
|
|
|
case PAKID_CORE_USER_LOGGEDON:
|
|
|
|
return rdpdr_state_check(rdpdr, packetid, RDPDR_CHANNEL_STATE_READY,
|
|
|
|
RDPDR_CHANNEL_STATE_USER_LOGGEDON);
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
enum RDPDR_CHANNEL_STATE state = RDPDR_CHANNEL_STATE_READY;
|
|
|
|
if (rdpdr->state == RDPDR_CHANNEL_STATE_USER_LOGGEDON)
|
|
|
|
state = RDPDR_CHANNEL_STATE_USER_LOGGEDON;
|
|
|
|
return rdpdr_state_check(rdpdr, packetid, state, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_process_receive(rdpdrPlugin* rdpdr, wStream* s)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 component;
|
2014-12-28 22:56:13 +03:00
|
|
|
UINT16 packetId;
|
|
|
|
UINT32 deviceId;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 status;
|
2018-12-12 12:06:41 +03:00
|
|
|
UINT error = ERROR_INVALID_DATA;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!rdpdr || !s)
|
2016-01-27 21:26:52 +03:00
|
|
|
return CHANNEL_RC_NULL_DATA;
|
|
|
|
|
2022-12-22 14:04:13 +03:00
|
|
|
rdpdr_dump_received_packet(rdpdr->log, WLOG_TRACE, s, "[rdpdr-channel] receive");
|
2018-12-12 12:06:41 +03:00
|
|
|
if (Stream_GetRemainingLength(s) >= 4)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
2018-12-12 12:06:41 +03:00
|
|
|
Stream_Read_UINT16(s, component); /* Component (2 bytes) */
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Read_UINT16(s, packetId); /* PacketId (2 bytes) */
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
if (component == RDPDR_CTYP_CORE)
|
|
|
|
{
|
2023-06-02 12:44:38 +03:00
|
|
|
if (!rdpdr_check_channel_state(rdpdr, packetId))
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
switch (packetId)
|
|
|
|
{
|
|
|
|
case PAKID_CORE_SERVER_ANNOUNCE:
|
|
|
|
if ((error = rdpdr_process_server_announce_request(rdpdr, s)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if ((error = rdpdr_send_client_announce_reply(rdpdr)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_send_client_announce_reply failed with error %" PRIu32 "",
|
|
|
|
error);
|
2018-12-12 12:06:41 +03:00
|
|
|
}
|
|
|
|
else if ((error = rdpdr_send_client_name_request(rdpdr)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_send_client_name_request failed with error %" PRIu32 "",
|
|
|
|
error);
|
2018-12-12 12:06:41 +03:00
|
|
|
}
|
|
|
|
else if ((error = rdpdr_process_init(rdpdr)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_process_init failed with error %" PRIu32 "", error);
|
2018-12-12 12:06:41 +03:00
|
|
|
}
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
break;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
case PAKID_CORE_SERVER_CAPABILITY:
|
|
|
|
if ((error = rdpdr_process_capability_request(rdpdr, s)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if ((error = rdpdr_send_capability_response(rdpdr)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_send_capability_response failed with error %" PRIu32 "",
|
|
|
|
error);
|
2018-12-12 12:06:41 +03:00
|
|
|
}
|
2016-01-27 21:26:52 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
break;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
case PAKID_CORE_CLIENTID_CONFIRM:
|
|
|
|
if ((error = rdpdr_process_server_clientid_confirm(rdpdr, s)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if ((error = rdpdr_send_device_list_announce_request(rdpdr, FALSE)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(
|
|
|
|
rdpdr->log, WLOG_ERROR,
|
2019-11-06 17:24:51 +03:00
|
|
|
"rdpdr_send_device_list_announce_request failed with error %" PRIu32 "",
|
|
|
|
error);
|
2018-12-12 12:06:41 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
break;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
case PAKID_CORE_USER_LOGGEDON:
|
|
|
|
if ((error = rdpdr_send_device_list_announce_request(rdpdr, TRUE)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(
|
|
|
|
rdpdr->log, WLOG_ERROR,
|
2019-11-06 17:24:51 +03:00
|
|
|
"rdpdr_send_device_list_announce_request failed with error %" PRIu32 "",
|
|
|
|
error);
|
2018-12-12 12:06:41 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
break;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
case PAKID_CORE_DEVICE_REPLY:
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
/* connect to a specific resource */
|
|
|
|
if (Stream_GetRemainingLength(s) >= 8)
|
|
|
|
{
|
|
|
|
Stream_Read_UINT32(s, deviceId);
|
|
|
|
Stream_Read_UINT32(s, status);
|
2021-11-30 11:10:18 +03:00
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
devman_unregister_device(rdpdr->devman, (void*)((size_t)deviceId));
|
2018-12-12 12:06:41 +03:00
|
|
|
error = CHANNEL_RC_OK;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
break;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
case PAKID_CORE_DEVICE_IOREQUEST:
|
|
|
|
if ((error = rdpdr_process_irp(rdpdr, s)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_process_irp failed with error %" PRIu32 "", error);
|
2018-12-12 12:06:41 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s = NULL;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
break;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
default:
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"RDPDR_CTYP_CORE unknown PacketId: 0x%04" PRIX16 "", packetId);
|
2018-12-12 12:06:41 +03:00
|
|
|
error = ERROR_INVALID_DATA;
|
|
|
|
break;
|
|
|
|
}
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
2018-12-12 12:06:41 +03:00
|
|
|
else
|
2015-07-28 15:05:57 +03:00
|
|
|
{
|
2018-12-12 12:06:41 +03:00
|
|
|
error = rdpdr_process_component(rdpdr, component, packetId, s);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
if (error != CHANNEL_RC_OK)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"Unknown message: Component: %s [0x%04" PRIX16
|
|
|
|
"] PacketId: %s [0x%04" PRIX16 "]",
|
|
|
|
rdpdr_component_string(component), component,
|
|
|
|
rdpdr_packetid_string(packetId), packetId);
|
2018-12-12 12:06:41 +03:00
|
|
|
}
|
2015-07-28 15:05:57 +03:00
|
|
|
}
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
return error;
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT rdpdr_send(rdpdrPlugin* rdpdr, wStream* s)
|
2013-10-17 22:19:33 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT status;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpdrPlugin* plugin = (rdpdrPlugin*)rdpdr;
|
2013-10-17 22:19:33 +04:00
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!rdpdr || !s)
|
2019-10-28 16:28:11 +03:00
|
|
|
{
|
2022-06-24 17:30:17 +03:00
|
|
|
Stream_Release(s);
|
2016-01-27 21:26:52 +03:00
|
|
|
return CHANNEL_RC_NULL_DATA;
|
2019-10-28 16:28:11 +03:00
|
|
|
}
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
if (!plugin)
|
2019-10-28 16:28:11 +03:00
|
|
|
{
|
2022-06-24 17:30:17 +03:00
|
|
|
Stream_Release(s);
|
2013-10-17 22:19:33 +04:00
|
|
|
status = CHANNEL_RC_BAD_INIT_HANDLE;
|
2019-10-28 16:28:11 +03:00
|
|
|
}
|
2013-10-17 22:19:33 +04:00
|
|
|
else
|
2014-02-10 10:06:11 +04:00
|
|
|
{
|
2021-12-14 14:59:16 +03:00
|
|
|
const size_t pos = Stream_GetPosition(s);
|
2022-12-22 14:04:13 +03:00
|
|
|
rdpdr_dump_send_packet(rdpdr->log, WLOG_TRACE, s, "[rdpdr-channel] send");
|
2019-11-06 17:24:51 +03:00
|
|
|
status = plugin->channelEntryPoints.pVirtualChannelWriteEx(
|
2021-12-14 14:59:16 +03:00
|
|
|
plugin->InitHandle, plugin->OpenHandle, Stream_Buffer(s), pos, s);
|
2014-02-10 10:06:11 +04:00
|
|
|
}
|
2013-10-17 22:19:33 +04:00
|
|
|
|
|
|
|
if (status != CHANNEL_RC_OK)
|
|
|
|
{
|
2022-06-24 17:30:17 +03:00
|
|
|
Stream_Release(s);
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "pVirtualChannelWriteEx failed with %s [%08" PRIX32 "]",
|
|
|
|
WTSErrorToString(status), status);
|
2013-10-17 22:19:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpdr_virtual_channel_event_data_received(rdpdrPlugin* rdpdr, void* pData,
|
|
|
|
UINT32 dataLength, UINT32 totalLength,
|
|
|
|
UINT32 dataFlags)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
|
|
|
wStream* data_in;
|
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
WINPR_ASSERT(pData || (dataLength == 0));
|
|
|
|
|
2013-10-17 23:20:04 +04:00
|
|
|
if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* According to MS-RDPBCGR 2.2.6.1, "All virtual channel traffic MUST be suspended.
|
|
|
|
* This flag is only valid in server-to-client virtual channel traffic. It MUST be
|
|
|
|
* ignored in client-to-server data." Thus it would be best practice to cease data
|
|
|
|
* transmission. However, simply returning here avoids a crash.
|
|
|
|
*/
|
2015-07-30 16:49:21 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dataFlags & CHANNEL_FLAG_FIRST)
|
|
|
|
{
|
2015-01-20 13:55:22 +03:00
|
|
|
if (rdpdr->data_in != NULL)
|
2022-06-24 17:30:17 +03:00
|
|
|
Stream_Release(rdpdr->data_in);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2022-06-24 17:30:17 +03:00
|
|
|
rdpdr->data_in = StreamPool_Take(rdpdr->pool, totalLength);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!rdpdr->data_in)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
data_in = rdpdr->data_in;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2020-02-21 13:30:23 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(data_in, dataLength))
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
|
2016-02-03 13:04:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2013-10-17 23:20:04 +04:00
|
|
|
Stream_Write(data_in, pData, dataLength);
|
|
|
|
|
|
|
|
if (dataFlags & CHANNEL_FLAG_LAST)
|
|
|
|
{
|
2022-06-24 17:30:17 +03:00
|
|
|
const size_t pos = Stream_GetPosition(data_in);
|
|
|
|
const size_t cap = Stream_Capacity(data_in);
|
|
|
|
if (cap < pos)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_virtual_channel_event_data_received: read error");
|
2015-06-03 14:38:47 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream_SealLength(data_in);
|
|
|
|
Stream_SetPosition(data_in, 0);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!MessageQueue_Post(rdpdr->queue, NULL, 0, (void*)data_in, NULL))
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "MessageQueue_Post failed!");
|
2015-07-15 10:50:35 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2022-06-22 14:01:28 +03:00
|
|
|
rdpdr->data_in = NULL;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2016-11-23 15:17:56 +03:00
|
|
|
static VOID VCAPITYPE rdpdr_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
|
2019-11-06 17:24:51 +03:00
|
|
|
UINT event, LPVOID pData,
|
|
|
|
UINT32 dataLength, UINT32 totalLength,
|
|
|
|
UINT32 dataFlags)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpdrPlugin* rdpdr = (rdpdrPlugin*)lpUserParam;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
2013-10-17 23:20:04 +04:00
|
|
|
switch (event)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
case CHANNEL_EVENT_DATA_RECEIVED:
|
2020-01-29 10:52:25 +03:00
|
|
|
if (!rdpdr || !pData || (rdpdr->OpenHandle != openHandle))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "error no match");
|
2020-01-29 10:52:25 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((error = rdpdr_virtual_channel_event_data_received(rdpdr, pData, dataLength,
|
|
|
|
totalLength, dataFlags)))
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_virtual_channel_event_data_received failed with error %" PRIu32
|
|
|
|
"!",
|
|
|
|
error);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
|
|
|
break;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2019-10-28 17:13:42 +03:00
|
|
|
case CHANNEL_EVENT_WRITE_CANCELLED:
|
2016-07-21 17:51:42 +03:00
|
|
|
case CHANNEL_EVENT_WRITE_COMPLETE:
|
2019-10-28 16:57:39 +03:00
|
|
|
{
|
2019-10-28 18:43:47 +03:00
|
|
|
wStream* s = (wStream*)pData;
|
2022-06-24 17:30:17 +03:00
|
|
|
Stream_Release(s);
|
2019-10-28 16:57:39 +03:00
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
break;
|
2015-01-20 13:55:22 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
case CHANNEL_EVENT_USER:
|
|
|
|
break;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2020-01-29 10:52:25 +03:00
|
|
|
if (error && rdpdr && rdpdr->rdpcontext)
|
2016-07-21 17:51:42 +03:00
|
|
|
setChannelError(rdpdr->rdpcontext, error,
|
2016-11-22 14:33:41 +03:00
|
|
|
"rdpdr_virtual_channel_open_event_ex reported an error");
|
2015-07-15 10:50:35 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
return;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI rdpdr_virtual_channel_client_thread(LPVOID arg)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpdrPlugin* rdpdr = (rdpdrPlugin*)arg;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!rdpdr)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
ExitThread((DWORD)CHANNEL_RC_NULL_DATA);
|
2018-03-07 14:03:10 +03:00
|
|
|
return CHANNEL_RC_NULL_DATA;
|
2016-01-24 17:21:06 +03:00
|
|
|
}
|
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if ((error = rdpdr_process_connect(rdpdr)))
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "rdpdr_process_connect failed with error %" PRIu32 "!",
|
|
|
|
error);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (rdpdr->rdpcontext)
|
2016-07-21 17:51:42 +03:00
|
|
|
setChannelError(rdpdr->rdpcontext, error,
|
2016-09-26 13:12:37 +03:00
|
|
|
"rdpdr_virtual_channel_client_thread reported an error");
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
ExitThread(error);
|
|
|
|
return error;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2022-06-22 14:01:28 +03:00
|
|
|
wMessage message = { 0 };
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
if (!MessageQueue_Wait(rdpdr->queue))
|
2013-10-17 23:20:04 +04:00
|
|
|
break;
|
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
if (MessageQueue_Peek(rdpdr->queue, &message, TRUE))
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
|
|
|
if (message.id == WMQ_QUIT)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (message.id == 0)
|
|
|
|
{
|
2022-06-22 14:01:28 +03:00
|
|
|
wStream* data = (wStream*)message.wParam;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2022-06-22 14:01:28 +03:00
|
|
|
error = rdpdr_process_receive(rdpdr, data);
|
|
|
|
|
2022-06-24 17:30:17 +03:00
|
|
|
Stream_Release(data);
|
2022-06-22 14:01:28 +03:00
|
|
|
if (error)
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_process_receive failed with error %" PRIu32 "!", error);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (rdpdr->rdpcontext)
|
2016-07-21 17:51:42 +03:00
|
|
|
setChannelError(rdpdr->rdpcontext, error,
|
2016-09-26 13:12:37 +03:00
|
|
|
"rdpdr_virtual_channel_client_thread reported an error");
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2021-07-28 09:43:01 +03:00
|
|
|
goto fail;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 09:43:01 +03:00
|
|
|
fail:
|
|
|
|
if ((error = drive_hotplug_thread_terminate(rdpdr)))
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"drive_hotplug_thread_terminate failed with error %" PRIu32 "!", error);
|
2021-07-28 09:43:01 +03:00
|
|
|
|
|
|
|
ExitThread(error);
|
|
|
|
return error;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2020-02-21 13:30:23 +03:00
|
|
|
static void queue_free(void* obj)
|
|
|
|
{
|
2021-10-18 13:30:24 +03:00
|
|
|
wStream* s;
|
|
|
|
wMessage* msg = (wMessage*)obj;
|
|
|
|
|
|
|
|
if (!msg || (msg->id != 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
s = (wStream*)msg->wParam;
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(s);
|
2022-06-24 17:30:17 +03:00
|
|
|
Stream_Release(s);
|
2020-02-21 13:30:23 +03:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpdr_virtual_channel_event_connected(rdpdrPlugin* rdpdr, LPVOID pData,
|
|
|
|
UINT32 dataLength)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2020-05-12 18:16:14 +03:00
|
|
|
wObject* obj;
|
2021-06-18 11:55:18 +03:00
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
2021-06-18 11:55:18 +03:00
|
|
|
WINPR_UNUSED(pData);
|
|
|
|
WINPR_UNUSED(dataLength);
|
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
rdpdr->queue = MessageQueue_New(NULL);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!rdpdr->queue)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "MessageQueue_New failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
2015-05-18 12:28:00 +03:00
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2020-05-12 18:16:14 +03:00
|
|
|
obj = MessageQueue_Object(rdpdr->queue);
|
|
|
|
obj->fnObjectFree = queue_free;
|
2020-02-21 13:30:23 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!(rdpdr->thread =
|
|
|
|
CreateThread(NULL, 0, rdpdr_virtual_channel_client_thread, (void*)rdpdr, 0, NULL)))
|
2015-05-18 12:28:00 +03:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "CreateThread failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
2015-05-18 12:28:00 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2021-09-06 16:50:50 +03:00
|
|
|
return rdpdr->channelEntryPoints.pVirtualChannelOpenEx(rdpdr->InitHandle, &rdpdr->OpenHandle,
|
|
|
|
rdpdr->channelDef.name,
|
|
|
|
rdpdr_virtual_channel_open_event_ex);
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_virtual_channel_event_disconnected(rdpdrPlugin* rdpdr)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
|
|
|
|
2018-04-09 12:10:08 +03:00
|
|
|
if (rdpdr->OpenHandle == 0)
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
|
2021-09-06 17:46:01 +03:00
|
|
|
if (rdpdr->queue && rdpdr->thread)
|
2016-01-27 21:26:52 +03:00
|
|
|
{
|
2021-09-06 17:46:01 +03:00
|
|
|
if (MessageQueue_PostQuit(rdpdr->queue, 0) &&
|
|
|
|
(WaitForSingleObject(rdpdr->thread, INFINITE) == WAIT_FAILED))
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "!",
|
|
|
|
error);
|
2021-09-06 17:46:01 +03:00
|
|
|
return error;
|
|
|
|
}
|
2016-01-27 21:26:52 +03:00
|
|
|
}
|
2014-12-27 23:20:29 +03:00
|
|
|
|
2021-09-17 09:59:57 +03:00
|
|
|
if (rdpdr->thread)
|
|
|
|
CloseHandle(rdpdr->thread);
|
2022-06-22 14:01:28 +03:00
|
|
|
MessageQueue_Free(rdpdr->queue);
|
2015-01-20 13:55:22 +03:00
|
|
|
rdpdr->queue = NULL;
|
|
|
|
rdpdr->thread = NULL;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2021-09-06 17:46:01 +03:00
|
|
|
WINPR_ASSERT(rdpdr->channelEntryPoints.pVirtualChannelCloseEx);
|
2016-11-22 14:33:41 +03:00
|
|
|
error = rdpdr->channelEntryPoints.pVirtualChannelCloseEx(rdpdr->InitHandle, rdpdr->OpenHandle);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (CHANNEL_RC_OK != error)
|
2015-01-20 13:55:22 +03:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "pVirtualChannelCloseEx failed with %s [%08" PRIX32 "]",
|
|
|
|
WTSErrorToString(error), error);
|
2015-01-20 13:55:22 +03:00
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2016-08-09 13:04:06 +03:00
|
|
|
rdpdr->OpenHandle = 0;
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (rdpdr->data_in)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2022-06-24 17:30:17 +03:00
|
|
|
Stream_Release(rdpdr->data_in);
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->data_in = NULL;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (rdpdr->devman)
|
|
|
|
{
|
|
|
|
devman_free(rdpdr->devman);
|
|
|
|
rdpdr->devman = NULL;
|
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
return error;
|
2015-01-20 13:55:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpdr_virtual_channel_event_terminated(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(rdpdr);
|
2016-11-22 14:33:41 +03:00
|
|
|
rdpdr->InitHandle = 0;
|
2022-06-24 17:30:17 +03:00
|
|
|
StreamPool_Free(rdpdr->pool);
|
2014-12-03 22:17:27 +03:00
|
|
|
free(rdpdr);
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2016-11-22 14:33:41 +03:00
|
|
|
static VOID VCAPITYPE rdpdr_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
|
2019-11-06 17:24:51 +03:00
|
|
|
UINT event, LPVOID pData, UINT dataLength)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpdrPlugin* rdpdr = (rdpdrPlugin*)lpUserParam;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2016-08-09 13:04:06 +03:00
|
|
|
if (!rdpdr || (rdpdr->InitHandle != pInitHandle))
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "error no match");
|
2013-10-17 23:20:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-28 09:02:59 +03:00
|
|
|
WINPR_ASSERT(pData || (dataLength == 0));
|
|
|
|
|
2013-10-17 23:20:04 +04:00
|
|
|
switch (event)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
case CHANNEL_EVENT_INITIALIZED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHANNEL_EVENT_CONNECTED:
|
|
|
|
if ((error = rdpdr_virtual_channel_event_connected(rdpdr, pData, dataLength)))
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_virtual_channel_event_connected failed with error %" PRIu32 "!",
|
|
|
|
error);
|
2016-01-27 21:26:52 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
break;
|
2016-01-27 21:26:52 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
case CHANNEL_EVENT_DISCONNECTED:
|
|
|
|
if ((error = rdpdr_virtual_channel_event_disconnected(rdpdr)))
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR,
|
|
|
|
"rdpdr_virtual_channel_event_disconnected failed with error %" PRIu32
|
|
|
|
"!",
|
|
|
|
error);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHANNEL_EVENT_TERMINATED:
|
|
|
|
rdpdr_virtual_channel_event_terminated(rdpdr);
|
2021-09-17 09:59:57 +03:00
|
|
|
rdpdr = NULL;
|
2016-07-21 17:51:42 +03:00
|
|
|
break;
|
|
|
|
|
2016-12-19 19:07:01 +03:00
|
|
|
case CHANNEL_EVENT_ATTACHED:
|
|
|
|
case CHANNEL_EVENT_DETACHED:
|
2016-07-21 17:51:42 +03:00
|
|
|
default:
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "unknown event %" PRIu32 "!", event);
|
2016-07-21 17:51:42 +03:00
|
|
|
break;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
2015-07-15 10:50:35 +03:00
|
|
|
|
2021-09-17 09:59:57 +03:00
|
|
|
if (error && rdpdr && rdpdr->rdpcontext)
|
2016-07-21 17:51:42 +03:00
|
|
|
setChannelError(rdpdr->rdpcontext, error,
|
2016-11-22 14:33:41 +03:00
|
|
|
"rdpdr_virtual_channel_init_event_ex reported an error");
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 06:48:17 +04:00
|
|
|
/* rdpdr is always built-in */
|
2022-12-22 02:32:17 +03:00
|
|
|
#define TAG CHANNELS_TAG("rdpdr.client")
|
2019-11-06 17:24:51 +03:00
|
|
|
#define VirtualChannelEntryEx rdpdr_VirtualChannelEntryEx
|
2012-10-08 06:53:24 +04:00
|
|
|
|
2016-11-22 14:33:41 +03:00
|
|
|
BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS pEntryPoints, PVOID pInitHandle)
|
2012-10-08 06:53:24 +04:00
|
|
|
{
|
2015-01-20 13:55:22 +03:00
|
|
|
UINT rc;
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdrPlugin* rdpdr;
|
2016-11-22 14:33:41 +03:00
|
|
|
CHANNEL_ENTRY_POINTS_FREERDP_EX* pEntryPointsEx;
|
2021-07-28 09:02:59 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(pEntryPoints);
|
|
|
|
WINPR_ASSERT(pInitHandle);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpdr = (rdpdrPlugin*)calloc(1, sizeof(rdpdrPlugin));
|
2014-02-17 02:27:36 +04:00
|
|
|
|
|
|
|
if (!rdpdr)
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "calloc failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return FALSE;
|
2022-06-24 17:30:17 +03:00
|
|
|
}
|
2022-12-22 02:32:17 +03:00
|
|
|
rdpdr->log = WLog_Get(TAG);
|
2022-06-24 17:30:17 +03:00
|
|
|
|
|
|
|
rdpdr->pool = StreamPool_New(TRUE, 1024);
|
|
|
|
if (!rdpdr->pool)
|
|
|
|
{
|
|
|
|
free(rdpdr);
|
|
|
|
return FALSE;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2012-10-08 06:53:24 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->channelDef.options =
|
2019-11-06 17:24:51 +03:00
|
|
|
CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP | CHANNEL_OPTION_COMPRESS_RDP;
|
2021-09-22 15:27:21 +03:00
|
|
|
sprintf_s(rdpdr->channelDef.name, ARRAYSIZE(rdpdr->channelDef.name), RDPDR_SVC_CHANNEL_NAME);
|
2014-05-07 02:19:54 +04:00
|
|
|
rdpdr->sequenceId = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
pEntryPointsEx = (CHANNEL_ENTRY_POINTS_FREERDP_EX*)pEntryPoints;
|
2015-07-15 10:50:35 +03:00
|
|
|
|
2016-11-22 14:33:41 +03:00
|
|
|
if ((pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX)) &&
|
2016-07-21 17:51:42 +03:00
|
|
|
(pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
rdpdr->rdpcontext = pEntryPointsEx->context;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
CopyMemory(&(rdpdr->channelEntryPoints), pEntryPoints, sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX));
|
2016-11-22 14:33:41 +03:00
|
|
|
rdpdr->InitHandle = pInitHandle;
|
2019-11-06 17:24:51 +03:00
|
|
|
rc = rdpdr->channelEntryPoints.pVirtualChannelInitEx(
|
|
|
|
rdpdr, NULL, pInitHandle, &rdpdr->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
|
|
|
|
rdpdr_virtual_channel_init_event_ex);
|
2015-06-03 14:38:47 +03:00
|
|
|
|
2015-01-20 13:55:22 +03:00
|
|
|
if (CHANNEL_RC_OK != rc)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_ERROR, "pVirtualChannelInitEx failed with %s [%08" PRIX32 "]",
|
|
|
|
WTSErrorToString(rc), rc);
|
2015-01-20 13:55:22 +03:00
|
|
|
free(rdpdr);
|
2015-06-03 14:38:47 +03:00
|
|
|
return FALSE;
|
2015-01-20 13:55:22 +03:00
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
return TRUE;
|
2012-10-08 06:53:24 +04:00
|
|
|
}
|