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>
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-08-05 06:24:46 +04:00
|
|
|
#include "config.h"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
|
|
|
|
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>
|
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
#include <freerdp/types.h>
|
2012-10-07 18:21:54 +04:00
|
|
|
#include <freerdp/constants.h>
|
2011-08-04 19:22:58 +04:00
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
#include <freerdp/utils/stream.h>
|
2011-08-05 11:43:48 +04:00
|
|
|
#include <freerdp/utils/unicode.h>
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2011-08-04 19:22:58 +04:00
|
|
|
#include <freerdp/utils/svc_plugin.h>
|
|
|
|
|
2011-08-05 06:24:46 +04:00
|
|
|
#ifdef HAVE_UNISTD_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
|
|
|
|
|
|
|
static void rdpdr_process_connect(rdpSvcPlugin* plugin)
|
|
|
|
{
|
2012-11-09 04:01:52 +04:00
|
|
|
int index;
|
|
|
|
RDPDR_DEVICE* device;
|
|
|
|
rdpSettings* settings;
|
2012-02-10 05:04:27 +04:00
|
|
|
rdpdrPlugin* rdpdr = (rdpdrPlugin*) plugin;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
|
|
|
rdpdr->devman = devman_new(plugin);
|
2012-11-09 04:01:52 +04:00
|
|
|
settings = (rdpSettings*) plugin->channel_entry_points.pExtendedData;
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2012-11-09 04:01:52 +04: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++)
|
|
|
|
{
|
|
|
|
device = settings->DeviceArray[index];
|
|
|
|
devman_load_device_service(rdpdr->devman, device);
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-05 06:24:46 +04:00
|
|
|
static void rdpdr_process_server_announce_request(rdpdrPlugin* rdpdr, STREAM* data_in)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_read_UINT16(data_in, rdpdr->versionMajor);
|
|
|
|
stream_read_UINT16(data_in, rdpdr->versionMinor);
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_read_UINT32(data_in, rdpdr->clientID);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
|
|
|
DEBUG_SVC("version %d.%d clientID %d", rdpdr->versionMajor, rdpdr->versionMinor, rdpdr->clientID);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpdr_send_client_announce_reply(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
|
|
|
STREAM* data_out;
|
|
|
|
|
|
|
|
data_out = stream_new(12);
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_write_UINT16(data_out, RDPDR_CTYP_CORE);
|
|
|
|
stream_write_UINT16(data_out, PAKID_CORE_CLIENTID_CONFIRM);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_write_UINT16(data_out, rdpdr->versionMajor);
|
|
|
|
stream_write_UINT16(data_out, rdpdr->versionMinor);
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_write_UINT32(data_out, (UINT32) rdpdr->clientID);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2012-02-10 05:04:27 +04:00
|
|
|
svc_plugin_send((rdpSvcPlugin*) rdpdr, data_out);
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpdr_send_client_name_request(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
|
|
|
STREAM* data_out;
|
2012-09-24 03:49:13 +04:00
|
|
|
WCHAR* computerNameW;
|
2012-02-10 05:04:27 +04:00
|
|
|
size_t computerNameLenW;
|
2011-08-05 06:24:46 +04:00
|
|
|
|
|
|
|
if (!rdpdr->computerName[0])
|
|
|
|
gethostname(rdpdr->computerName, sizeof(rdpdr->computerName) - 1);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2012-09-24 03:49:13 +04:00
|
|
|
computerNameLenW = freerdp_AsciiToUnicodeAlloc(rdpdr->computerName, &computerNameW, 0) * 2;
|
|
|
|
|
2011-08-05 06:24:46 +04:00
|
|
|
data_out = stream_new(16 + computerNameLenW + 2);
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_write_UINT16(data_out, RDPDR_CTYP_CORE);
|
|
|
|
stream_write_UINT16(data_out, PAKID_CORE_CLIENT_NAME);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_write_UINT32(data_out, 1); /* unicodeFlag, 0 for ASCII and 1 for Unicode */
|
|
|
|
stream_write_UINT32(data_out, 0); /* codePage, must be set to zero */
|
|
|
|
stream_write_UINT32(data_out, computerNameLenW + 2); /* computerNameLen, including null terminator */
|
2012-09-24 03:49:13 +04:00
|
|
|
stream_write(data_out, computerNameW, computerNameLenW);
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_write_UINT16(data_out, 0); /* null terminator */
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(computerNameW);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2012-02-10 05:04:27 +04:00
|
|
|
svc_plugin_send((rdpSvcPlugin*) rdpdr, data_out);
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
2011-08-05 18:44:06 +04:00
|
|
|
static void rdpdr_process_server_clientid_confirm(rdpdrPlugin* rdpdr, STREAM* data_in)
|
|
|
|
{
|
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
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_read_UINT16(data_in, versionMajor);
|
|
|
|
stream_read_UINT16(data_in, versionMinor);
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_read_UINT32(data_in, clientID);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
if (versionMajor != rdpdr->versionMajor || versionMinor != rdpdr->versionMinor)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("unmatched version %d.%d", versionMajor, versionMinor);
|
|
|
|
rdpdr->versionMajor = versionMajor;
|
|
|
|
rdpdr->versionMinor = versionMinor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientID != rdpdr->clientID)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("unmatched clientID %d", clientID);
|
|
|
|
rdpdr->clientID = clientID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static void rdpdr_send_device_list_announce_request(rdpdrPlugin* rdpdr, BOOL user_loggedon)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2012-02-10 05:04:27 +04:00
|
|
|
int i;
|
|
|
|
int pos;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE c;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 count;
|
2011-08-05 18:44:06 +04:00
|
|
|
int data_len;
|
|
|
|
int count_pos;
|
2012-02-10 05:04:27 +04:00
|
|
|
STREAM* data_out;
|
|
|
|
DEVICE* device;
|
|
|
|
LIST_ITEM* item;
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
data_out = stream_new(256);
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_write_UINT16(data_out, RDPDR_CTYP_CORE);
|
|
|
|
stream_write_UINT16(data_out, PAKID_CORE_DEVICELIST_ANNOUNCE);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
count_pos = stream_get_pos(data_out);
|
|
|
|
count = 0;
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_seek_UINT32(data_out); /* deviceCount */
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
for (item = rdpdr->devman->devices->head; item; item = item->next)
|
|
|
|
{
|
2012-02-10 05:04:27 +04:00
|
|
|
device = (DEVICE*) item->data;
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
if (rdpdr->versionMinor == 0x0005 ||
|
2012-10-03 05:52:27 +04:00
|
|
|
device->type == RDPDR_DTYP_SMARTCARD || user_loggedon)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2011-08-05 20:11:41 +04:00
|
|
|
data_len = (device->data == NULL ? 0 : stream_get_length(device->data));
|
2011-08-05 18:44:06 +04:00
|
|
|
stream_check_size(data_out, 20 + data_len);
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_write_UINT32(data_out, device->type); /* deviceType */
|
|
|
|
stream_write_UINT32(data_out, device->id); /* deviceID */
|
2012-02-10 05:04:27 +04:00
|
|
|
strncpy((char*) stream_get_tail(data_out), device->name, 8);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_peek_BYTE(data_out, c);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2011-08-05 18:44:06 +04:00
|
|
|
if (c > 0x7F)
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_write_BYTE(data_out, '_');
|
2011-08-05 18:44:06 +04:00
|
|
|
else
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_seek_BYTE(data_out);
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_write_UINT32(data_out, data_len);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2011-08-05 18:44:06 +04:00
|
|
|
if (data_len > 0)
|
|
|
|
stream_write(data_out, stream_get_data(device->data), data_len);
|
|
|
|
|
|
|
|
count++;
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2011-08-05 18:44:06 +04:00
|
|
|
printf("registered device #%d: %s (type=%d id=%d)\n",
|
|
|
|
count, device->name, device->type, device->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = stream_get_pos(data_out);
|
|
|
|
stream_set_pos(data_out, count_pos);
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_write_UINT32(data_out, count);
|
2011-08-05 18:44:06 +04:00
|
|
|
stream_set_pos(data_out, pos);
|
|
|
|
stream_seal(data_out);
|
|
|
|
|
2012-02-10 05:04:27 +04:00
|
|
|
svc_plugin_send((rdpSvcPlugin*) rdpdr, data_out);
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL rdpdr_process_irp(rdpdrPlugin* rdpdr, STREAM* data_in)
|
2011-08-05 21:54:40 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
|
|
|
|
|
|
|
irp = irp_new(rdpdr->devman, data_in);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2011-08-05 21:54:40 +04:00
|
|
|
if (irp == NULL)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-05 21:54:40 +04:00
|
|
|
|
|
|
|
IFCALL(irp->device->IRPRequest, irp->device, irp);
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-05 21:54:40 +04:00
|
|
|
}
|
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
static void rdpdr_process_receive(rdpSvcPlugin* plugin, STREAM* data_in)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 component;
|
|
|
|
UINT16 packetID;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 deviceID;
|
|
|
|
UINT32 status;
|
2012-09-23 21:54:14 +04:00
|
|
|
rdpdrPlugin* rdpdr = (rdpdrPlugin*) plugin;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_read_UINT16(data_in, component);
|
|
|
|
stream_read_UINT16(data_in, packetID);
|
2011-08-04 19:22:58 +04:00
|
|
|
|
|
|
|
if (component == RDPDR_CTYP_CORE)
|
|
|
|
{
|
|
|
|
switch (packetID)
|
|
|
|
{
|
|
|
|
case PAKID_CORE_SERVER_ANNOUNCE:
|
|
|
|
DEBUG_SVC("RDPDR_CTYP_CORE / PAKID_CORE_SERVER_ANNOUNCE");
|
2011-08-05 06:24:46 +04:00
|
|
|
rdpdr_process_server_announce_request(rdpdr, data_in);
|
|
|
|
rdpdr_send_client_announce_reply(rdpdr);
|
|
|
|
rdpdr_send_client_name_request(rdpdr);
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_SERVER_CAPABILITY:
|
|
|
|
DEBUG_SVC("RDPDR_CTYP_CORE / PAKID_CORE_SERVER_CAPABILITY");
|
2011-08-05 11:43:48 +04:00
|
|
|
rdpdr_process_capability_request(rdpdr, data_in);
|
|
|
|
rdpdr_send_capability_response(rdpdr);
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_CLIENTID_CONFIRM:
|
|
|
|
DEBUG_SVC("RDPDR_CTYP_CORE / PAKID_CORE_CLIENTID_CONFIRM");
|
2011-08-05 18:44:06 +04:00
|
|
|
rdpdr_process_server_clientid_confirm(rdpdr, data_in);
|
2012-10-09 10:31:28 +04:00
|
|
|
rdpdr_send_device_list_announce_request(rdpdr, FALSE);
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_USER_LOGGEDON:
|
|
|
|
DEBUG_SVC("RDPDR_CTYP_CORE / PAKID_CORE_USER_LOGGEDON");
|
2012-10-09 10:31:28 +04:00
|
|
|
rdpdr_send_device_list_announce_request(rdpdr, TRUE);
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_DEVICE_REPLY:
|
|
|
|
/* connect to a specific resource */
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_read_UINT32(data_in, deviceID);
|
|
|
|
stream_read_UINT32(data_in, status);
|
2011-08-05 20:11:41 +04:00
|
|
|
DEBUG_SVC("RDPDR_CTYP_CORE / PAKID_CORE_DEVICE_REPLY (deviceID=%d status=0x%08X)", deviceID, status);
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_DEVICE_IOREQUEST:
|
|
|
|
DEBUG_SVC("RDPDR_CTYP_CORE / PAKID_CORE_DEVICE_IOREQUEST");
|
2011-08-05 21:54:40 +04:00
|
|
|
if (rdpdr_process_irp(rdpdr, data_in))
|
|
|
|
data_in = NULL;
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_WARN("RDPDR_CTYP_CORE / unknown packetID: 0x%02X", packetID);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (component == RDPDR_CTYP_PRN)
|
|
|
|
{
|
|
|
|
DEBUG_SVC("RDPDR_CTYP_PRN");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-22 02:22:01 +04:00
|
|
|
DEBUG_WARN("RDPDR component: 0x%02X packetID: 0x%02X", component, packetID);
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
stream_free(data_in);
|
|
|
|
}
|
|
|
|
|
2011-08-18 01:28:26 +04:00
|
|
|
static void rdpdr_process_event(rdpSvcPlugin* plugin, RDP_EVENT* event)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
|
|
|
freerdp_event_free(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpdr_process_terminate(rdpSvcPlugin* plugin)
|
|
|
|
{
|
2012-02-10 05:04:27 +04:00
|
|
|
rdpdrPlugin* rdpdr = (rdpdrPlugin*) plugin;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
|
|
|
devman_free(rdpdr->devman);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(plugin);
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 06:48:17 +04:00
|
|
|
/* rdpdr is always built-in */
|
2012-10-08 06:53:24 +04:00
|
|
|
#define VirtualChannelEntry rdpdr_VirtualChannelEntry
|
|
|
|
|
2012-11-01 07:04:31 +04:00
|
|
|
int VirtualChannelEntry(PCHANNEL_ENTRY_POINTS pEntryPoints)
|
2012-10-08 06:53:24 +04:00
|
|
|
{
|
|
|
|
rdpdrPlugin* _p;
|
|
|
|
|
|
|
|
_p = (rdpdrPlugin*) malloc(sizeof(rdpdrPlugin));
|
|
|
|
ZeroMemory(_p, sizeof(rdpdrPlugin));
|
|
|
|
|
|
|
|
_p->plugin.channel_def.options =
|
|
|
|
CHANNEL_OPTION_INITIALIZED |
|
|
|
|
CHANNEL_OPTION_ENCRYPT_RDP |
|
|
|
|
CHANNEL_OPTION_COMPRESS_RDP;
|
|
|
|
|
|
|
|
strcpy(_p->plugin.channel_def.name, "rdpdr");
|
|
|
|
|
|
|
|
_p->plugin.connect_callback = rdpdr_process_connect;
|
|
|
|
_p->plugin.receive_callback = rdpdr_process_receive;
|
|
|
|
_p->plugin.event_callback = rdpdr_process_event;
|
|
|
|
_p->plugin.terminate_callback = rdpdr_process_terminate;
|
|
|
|
|
|
|
|
svc_plugin_init((rdpSvcPlugin*) _p, pEntryPoints);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|