2013-08-19 02:09:12 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Device Redirection Virtual Channel Extension
|
|
|
|
*
|
|
|
|
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/print.h>
|
|
|
|
#include <winpr/stream.h>
|
|
|
|
|
2014-08-11 11:12:01 +04:00
|
|
|
#include <freerdp/channels/log.h>
|
2013-08-19 02:09:12 +04:00
|
|
|
#include "rdpdr_main.h"
|
|
|
|
|
2014-08-18 19:23:03 +04:00
|
|
|
#define TAG "rdpdr.server"
|
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
static UINT32 g_ClientId = 0;
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_send_announce_request(RdpdrServerContext* context)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2013-08-19 02:09:12 +04:00
|
|
|
BOOL status;
|
|
|
|
RDPDR_HEADER header;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "RdpdrServerSendAnnounceRequest");
|
2013-08-19 02:09:12 +04:00
|
|
|
header.Component = RDPDR_CTYP_CORE;
|
|
|
|
header.PacketId = PAKID_CORE_SERVER_ANNOUNCE;
|
|
|
|
s = Stream_New(NULL, RDPDR_HEADER_LENGTH + 8);
|
|
|
|
Stream_Write_UINT16(s, header.Component); /* Component (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, header.PacketId); /* PacketId (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, context->priv->VersionMajor); /* VersionMajor (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, context->priv->VersionMinor); /* VersionMinor (2 bytes) */
|
|
|
|
Stream_Write_UINT32(s, context->priv->ClientId); /* ClientId (4 bytes) */
|
|
|
|
Stream_SealLength(s);
|
2014-05-28 19:04:24 +04:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written);
|
2013-08-19 02:09:12 +04:00
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_receive_announce_response(RdpdrServerContext* context, wStream* s, RDPDR_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
UINT32 ClientId;
|
|
|
|
UINT16 VersionMajor;
|
|
|
|
UINT16 VersionMinor;
|
|
|
|
Stream_Read_UINT16(s, VersionMajor); /* VersionMajor (2 bytes) */
|
|
|
|
Stream_Read_UINT16(s, VersionMinor); /* VersionMinor (2 bytes) */
|
|
|
|
Stream_Read_UINT32(s, ClientId); /* ClientId (4 bytes) */
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "Client Announce Response: VersionMajor: 0x%04X VersionMinor: 0x%04X ClientId: 0x%04X",
|
2014-08-18 19:23:03 +04:00
|
|
|
VersionMajor, VersionMinor, ClientId);
|
2013-08-19 04:27:50 +04:00
|
|
|
context->priv->ClientId = ClientId;
|
2013-08-19 02:09:12 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_receive_client_name_request(RdpdrServerContext* context, wStream* s, RDPDR_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
UINT32 UnicodeFlag;
|
|
|
|
UINT32 ComputerNameLen;
|
|
|
|
Stream_Read_UINT32(s, UnicodeFlag); /* UnicodeFlag (4 bytes) */
|
|
|
|
Stream_Seek_UINT32(s); /* CodePage (4 bytes), MUST be set to zero */
|
|
|
|
Stream_Read_UINT32(s, ComputerNameLen); /* ComputerNameLen (4 bytes) */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Caution: ComputerNameLen is given *bytes*,
|
|
|
|
* not in characters, including the NULL terminator!
|
|
|
|
*/
|
|
|
|
|
2013-08-19 20:21:40 +04:00
|
|
|
if (context->priv->ClientComputerName)
|
|
|
|
{
|
|
|
|
free(context->priv->ClientComputerName);
|
|
|
|
context->priv->ClientComputerName = NULL;
|
|
|
|
}
|
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
if (UnicodeFlag)
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
ConvertFromUnicode(CP_UTF8, 0, (WCHAR*) Stream_Pointer(s),
|
2014-08-18 19:23:03 +04:00
|
|
|
-1, &(context->priv->ClientComputerName), 0, NULL, NULL);
|
2013-08-19 02:09:12 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
context->priv->ClientComputerName = _strdup((char*) Stream_Pointer(s));
|
2013-08-19 02:09:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Seek(s, ComputerNameLen);
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "ClientComputerName: %s", context->priv->ClientComputerName);
|
2013-08-19 02:09:12 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_read_capability_set_header(wStream* s, RDPDR_CAPABILITY_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
Stream_Read_UINT16(s, header->CapabilityType); /* CapabilityType (2 bytes) */
|
|
|
|
Stream_Read_UINT16(s, header->CapabilityLength); /* CapabilityLength (2 bytes) */
|
|
|
|
Stream_Read_UINT32(s, header->Version); /* Version (4 bytes) */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_write_capability_set_header(wStream* s, RDPDR_CAPABILITY_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
Stream_Write_UINT16(s, header->CapabilityType); /* CapabilityType (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, header->CapabilityLength); /* CapabilityLength (2 bytes) */
|
|
|
|
Stream_Write_UINT32(s, header->Version); /* Version (4 bytes) */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_read_general_capability_set(RdpdrServerContext* context, wStream* s, RDPDR_CAPABILITY_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
UINT32 ioCode1;
|
|
|
|
UINT32 extraFlags1;
|
|
|
|
UINT32 extendedPdu;
|
|
|
|
UINT16 VersionMajor;
|
|
|
|
UINT16 VersionMinor;
|
|
|
|
UINT32 SpecialTypeDeviceCap;
|
|
|
|
Stream_Seek_UINT32(s); /* osType (4 bytes), ignored on receipt */
|
|
|
|
Stream_Seek_UINT32(s); /* osVersion (4 bytes), unused and must be set to zero */
|
|
|
|
Stream_Read_UINT16(s, VersionMajor); /* protocolMajorVersion (2 bytes) */
|
|
|
|
Stream_Read_UINT16(s, VersionMinor); /* protocolMinorVersion (2 bytes) */
|
|
|
|
Stream_Read_UINT32(s, ioCode1); /* ioCode1 (4 bytes) */
|
|
|
|
Stream_Seek_UINT32(s); /* ioCode2 (4 bytes), must be set to zero, reserved for future use */
|
|
|
|
Stream_Read_UINT32(s, extendedPdu); /* extendedPdu (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, extraFlags1); /* extraFlags1 (4 bytes) */
|
|
|
|
Stream_Seek_UINT32(s); /* extraFlags2 (4 bytes), must be set to zero, reserved for future use */
|
|
|
|
Stream_Read_UINT32(s, SpecialTypeDeviceCap); /* SpecialTypeDeviceCap (4 bytes) */
|
2013-08-19 04:27:50 +04:00
|
|
|
context->priv->UserLoggedOnPdu = (extendedPdu & RDPDR_USER_LOGGEDON_PDU) ? TRUE : FALSE;
|
2013-08-19 02:09:12 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_write_general_capability_set(RdpdrServerContext* context, wStream* s)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
UINT32 ioCode1;
|
|
|
|
UINT32 extendedPdu;
|
|
|
|
UINT32 extraFlags1;
|
|
|
|
UINT32 SpecialTypeDeviceCap;
|
|
|
|
RDPDR_CAPABILITY_HEADER header;
|
|
|
|
header.CapabilityLength = RDPDR_CAPABILITY_HEADER_LENGTH + 36;
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, header.CapabilityLength))
|
2015-03-26 19:09:47 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
header.CapabilityType = CAP_GENERAL_TYPE;
|
2013-08-19 02:09:12 +04:00
|
|
|
header.Version = GENERAL_CAPABILITY_VERSION_02;
|
|
|
|
ioCode1 = 0;
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_CREATE; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_CLEANUP; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_CLOSE; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_READ; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_WRITE; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_FLUSH_BUFFERS; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_SHUTDOWN; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_DEVICE_CONTROL; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_QUERY_VOLUME_INFORMATION; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_SET_VOLUME_INFORMATION; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_QUERY_INFORMATION; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_SET_INFORMATION; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_DIRECTORY_CONTROL; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_LOCK_CONTROL; /* always set */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_QUERY_SECURITY; /* optional */
|
|
|
|
ioCode1 |= RDPDR_IRP_MJ_SET_SECURITY; /* optional */
|
|
|
|
extendedPdu = 0;
|
|
|
|
extendedPdu |= RDPDR_CLIENT_DISPLAY_NAME_PDU; /* always set */
|
|
|
|
extendedPdu |= RDPDR_DEVICE_REMOVE_PDUS; /* optional */
|
2013-08-19 04:27:50 +04:00
|
|
|
|
|
|
|
if (context->priv->UserLoggedOnPdu)
|
|
|
|
extendedPdu |= RDPDR_USER_LOGGEDON_PDU; /* optional */
|
2013-08-19 02:09:12 +04:00
|
|
|
|
|
|
|
extraFlags1 = 0;
|
|
|
|
extraFlags1 |= ENABLE_ASYNCIO; /* optional */
|
|
|
|
SpecialTypeDeviceCap = 0;
|
|
|
|
rdpdr_server_write_capability_set_header(s, &header);
|
|
|
|
Stream_Write_UINT32(s, 0); /* osType (4 bytes), ignored on receipt */
|
|
|
|
Stream_Write_UINT32(s, 0); /* osVersion (4 bytes), unused and must be set to zero */
|
|
|
|
Stream_Write_UINT16(s, context->priv->VersionMajor); /* protocolMajorVersion (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, context->priv->VersionMinor); /* protocolMinorVersion (2 bytes) */
|
|
|
|
Stream_Write_UINT32(s, ioCode1); /* ioCode1 (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, 0); /* ioCode2 (4 bytes), must be set to zero, reserved for future use */
|
|
|
|
Stream_Write_UINT32(s, extendedPdu); /* extendedPdu (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, extraFlags1); /* extraFlags1 (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, 0); /* extraFlags2 (4 bytes), must be set to zero, reserved for future use */
|
|
|
|
Stream_Write_UINT32(s, SpecialTypeDeviceCap); /* SpecialTypeDeviceCap (4 bytes) */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_read_printer_capability_set(RdpdrServerContext* context, wStream* s, RDPDR_CAPABILITY_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_write_printer_capability_set(RdpdrServerContext* context, wStream* s)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
RDPDR_CAPABILITY_HEADER header;
|
|
|
|
header.CapabilityType = CAP_PRINTER_TYPE;
|
|
|
|
header.CapabilityLength = RDPDR_CAPABILITY_HEADER_LENGTH;
|
|
|
|
header.Version = PRINT_CAPABILITY_VERSION_01;
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, header.CapabilityLength))
|
2015-03-26 19:09:47 +03:00
|
|
|
return -1;
|
2013-08-19 02:09:12 +04:00
|
|
|
rdpdr_server_write_capability_set_header(s, &header);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_read_port_capability_set(RdpdrServerContext* context, wStream* s, RDPDR_CAPABILITY_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_write_port_capability_set(RdpdrServerContext* context, wStream* s)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
RDPDR_CAPABILITY_HEADER header;
|
|
|
|
header.CapabilityType = CAP_PORT_TYPE;
|
|
|
|
header.CapabilityLength = RDPDR_CAPABILITY_HEADER_LENGTH;
|
|
|
|
header.Version = PORT_CAPABILITY_VERSION_01;
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, header.CapabilityLength))
|
2015-03-26 19:09:47 +03:00
|
|
|
return -1;
|
2013-08-19 02:09:12 +04:00
|
|
|
rdpdr_server_write_capability_set_header(s, &header);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_read_drive_capability_set(RdpdrServerContext* context, wStream* s, RDPDR_CAPABILITY_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_write_drive_capability_set(RdpdrServerContext* context, wStream* s)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
RDPDR_CAPABILITY_HEADER header;
|
|
|
|
header.CapabilityType = CAP_DRIVE_TYPE;
|
|
|
|
header.CapabilityLength = RDPDR_CAPABILITY_HEADER_LENGTH;
|
|
|
|
header.Version = DRIVE_CAPABILITY_VERSION_02;
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, header.CapabilityLength))
|
2015-03-26 19:09:47 +03:00
|
|
|
return -1;
|
2013-08-19 02:09:12 +04:00
|
|
|
rdpdr_server_write_capability_set_header(s, &header);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_read_smartcard_capability_set(RdpdrServerContext* context, wStream* s, RDPDR_CAPABILITY_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_write_smartcard_capability_set(RdpdrServerContext* context, wStream* s)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
RDPDR_CAPABILITY_HEADER header;
|
|
|
|
header.CapabilityType = CAP_SMARTCARD_TYPE;
|
|
|
|
header.CapabilityLength = RDPDR_CAPABILITY_HEADER_LENGTH;
|
|
|
|
header.Version = SMARTCARD_CAPABILITY_VERSION_01;
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, header.CapabilityLength))
|
2015-03-26 19:09:47 +03:00
|
|
|
return -1;
|
2013-08-19 02:09:12 +04:00
|
|
|
rdpdr_server_write_capability_set_header(s, &header);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_send_core_capability_request(RdpdrServerContext* context)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2015-03-30 19:29:32 +03:00
|
|
|
BOOL status = FALSE;
|
2013-08-19 02:09:12 +04:00
|
|
|
RDPDR_HEADER header;
|
|
|
|
UINT16 numCapabilities;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "RdpdrServerSendCoreCapabilityRequest");
|
2013-08-19 02:09:12 +04:00
|
|
|
header.Component = RDPDR_CTYP_CORE;
|
|
|
|
header.PacketId = PAKID_CORE_SERVER_CAPABILITY;
|
|
|
|
numCapabilities = 5;
|
|
|
|
s = Stream_New(NULL, RDPDR_HEADER_LENGTH + 512);
|
2015-03-26 19:09:47 +03:00
|
|
|
if (!s)
|
|
|
|
return -1;
|
2013-08-19 02:09:12 +04:00
|
|
|
Stream_Write_UINT16(s, header.Component); /* Component (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, header.PacketId); /* PacketId (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, numCapabilities); /* numCapabilities (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, 0); /* Padding (2 bytes) */
|
2015-03-30 18:15:45 +03:00
|
|
|
if (rdpdr_server_write_general_capability_set(context, s) != 0)
|
2015-03-26 19:09:47 +03:00
|
|
|
goto out_error;
|
2015-03-30 18:15:45 +03:00
|
|
|
if (rdpdr_server_write_printer_capability_set(context, s) != 0)
|
2015-03-26 19:09:47 +03:00
|
|
|
goto out_error;
|
2015-03-30 18:15:45 +03:00
|
|
|
if (rdpdr_server_write_port_capability_set(context, s) != 0)
|
2015-03-26 19:09:47 +03:00
|
|
|
goto out_error;
|
2015-03-30 18:15:45 +03:00
|
|
|
if (rdpdr_server_write_drive_capability_set(context, s) != 0)
|
2015-03-26 19:09:47 +03:00
|
|
|
goto out_error;
|
2015-03-30 18:15:45 +03:00
|
|
|
if (rdpdr_server_write_smartcard_capability_set(context, s) != 0)
|
2015-03-26 19:09:47 +03:00
|
|
|
goto out_error;
|
2013-08-19 02:09:12 +04:00
|
|
|
Stream_SealLength(s);
|
2014-05-28 19:04:24 +04:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written);
|
2015-03-26 19:09:47 +03:00
|
|
|
out_error:
|
|
|
|
Stream_Free(s, TRUE);
|
2015-03-30 19:29:32 +03:00
|
|
|
return status ? 0 : -1;
|
2013-08-19 02:09:12 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_receive_core_capability_response(RdpdrServerContext* context, wStream* s, RDPDR_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
UINT16 numCapabilities;
|
|
|
|
RDPDR_CAPABILITY_HEADER capabilityHeader;
|
|
|
|
Stream_Read_UINT16(s, numCapabilities); /* numCapabilities (2 bytes) */
|
|
|
|
Stream_Seek_UINT16(s); /* Padding (2 bytes) */
|
|
|
|
|
|
|
|
for (i = 0; i < numCapabilities; i++)
|
|
|
|
{
|
|
|
|
rdpdr_server_read_capability_set_header(s, &capabilityHeader);
|
|
|
|
|
|
|
|
switch (capabilityHeader.CapabilityType)
|
|
|
|
{
|
|
|
|
case CAP_GENERAL_TYPE:
|
|
|
|
rdpdr_server_read_general_capability_set(context, s, &capabilityHeader);
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case CAP_PRINTER_TYPE:
|
|
|
|
rdpdr_server_read_printer_capability_set(context, s, &capabilityHeader);
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case CAP_PORT_TYPE:
|
|
|
|
rdpdr_server_read_port_capability_set(context, s, &capabilityHeader);
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case CAP_DRIVE_TYPE:
|
|
|
|
rdpdr_server_read_drive_capability_set(context, s, &capabilityHeader);
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case CAP_SMARTCARD_TYPE:
|
|
|
|
rdpdr_server_read_smartcard_capability_set(context, s, &capabilityHeader);
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
default:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "Unknown capabilityType %d", capabilityHeader.CapabilityType);
|
2013-08-19 02:09:12 +04:00
|
|
|
Stream_Seek(s, capabilityHeader.CapabilityLength - RDPDR_CAPABILITY_HEADER_LENGTH);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_send_client_id_confirm(RdpdrServerContext* context)
|
2013-08-19 04:27:50 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2013-08-19 04:27:50 +04:00
|
|
|
BOOL status;
|
|
|
|
RDPDR_HEADER header;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "RdpdrServerSendClientIdConfirm");
|
2013-08-19 04:27:50 +04:00
|
|
|
header.Component = RDPDR_CTYP_CORE;
|
|
|
|
header.PacketId = PAKID_CORE_CLIENTID_CONFIRM;
|
|
|
|
s = Stream_New(NULL, RDPDR_HEADER_LENGTH + 8);
|
|
|
|
Stream_Write_UINT16(s, header.Component); /* Component (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, header.PacketId); /* PacketId (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, context->priv->VersionMajor); /* VersionMajor (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, context->priv->VersionMinor); /* VersionMinor (2 bytes) */
|
|
|
|
Stream_Write_UINT32(s, context->priv->ClientId); /* ClientId (4 bytes) */
|
|
|
|
Stream_SealLength(s);
|
2014-05-28 19:04:24 +04:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written);
|
2013-08-19 04:27:50 +04:00
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_receive_device_list_announce_request(RdpdrServerContext* context, wStream* s, RDPDR_HEADER* header)
|
2013-08-19 04:27:50 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
UINT32 DeviceCount;
|
|
|
|
UINT32 DeviceType;
|
|
|
|
UINT32 DeviceId;
|
|
|
|
char PreferredDosName[9];
|
|
|
|
UINT32 DeviceDataLength;
|
|
|
|
PreferredDosName[8] = 0;
|
|
|
|
Stream_Read_UINT32(s, DeviceCount); /* DeviceCount (4 bytes) */
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "DeviceCount: %d", DeviceCount);
|
2013-08-19 04:27:50 +04:00
|
|
|
|
|
|
|
for (i = 0; i < DeviceCount; i++)
|
|
|
|
{
|
|
|
|
Stream_Read_UINT32(s, DeviceType); /* DeviceType (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, DeviceId); /* DeviceId (4 bytes) */
|
|
|
|
Stream_Read(s, PreferredDosName, 8); /* PreferredDosName (8 bytes) */
|
|
|
|
Stream_Read_UINT32(s, DeviceDataLength); /* DeviceDataLength (4 bytes) */
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "Device %d Name: %s Id: 0x%04X DataLength: %d",
|
2014-08-18 19:23:03 +04:00
|
|
|
i, PreferredDosName, DeviceId, DeviceDataLength);
|
2013-08-19 04:27:50 +04:00
|
|
|
|
|
|
|
switch (DeviceId)
|
|
|
|
{
|
|
|
|
case RDPDR_DTYP_FILESYSTEM:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 04:27:50 +04:00
|
|
|
case RDPDR_DTYP_PRINT:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 04:27:50 +04:00
|
|
|
case RDPDR_DTYP_SERIAL:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 04:27:50 +04:00
|
|
|
case RDPDR_DTYP_PARALLEL:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 04:27:50 +04:00
|
|
|
case RDPDR_DTYP_SMARTCARD:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 04:27:50 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Seek(s, DeviceDataLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_send_user_logged_on(RdpdrServerContext* context)
|
2013-08-19 04:27:50 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2013-08-19 04:27:50 +04:00
|
|
|
BOOL status;
|
|
|
|
RDPDR_HEADER header;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "%s");
|
2013-08-19 04:27:50 +04:00
|
|
|
header.Component = RDPDR_CTYP_CORE;
|
|
|
|
header.PacketId = PAKID_CORE_USER_LOGGEDON;
|
|
|
|
s = Stream_New(NULL, RDPDR_HEADER_LENGTH);
|
|
|
|
Stream_Write_UINT16(s, header.Component); /* Component (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, header.PacketId); /* PacketId (2 bytes) */
|
|
|
|
Stream_SealLength(s);
|
2014-05-28 19:04:24 +04:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written);
|
2013-08-19 04:27:50 +04:00
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_receive_pdu(RdpdrServerContext* context, wStream* s, RDPDR_HEADER* header)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "RdpdrServerReceivePdu: Component: 0x%04X PacketId: 0x%04X",
|
2014-08-18 19:23:03 +04:00
|
|
|
header->Component, header->PacketId);
|
|
|
|
winpr_HexDump(TAG, WLOG_DEBUG, Stream_Buffer(s), Stream_Length(s));
|
2013-08-19 02:09:12 +04:00
|
|
|
|
|
|
|
if (header->Component == RDPDR_CTYP_CORE)
|
|
|
|
{
|
|
|
|
switch (header->PacketId)
|
|
|
|
{
|
|
|
|
case PAKID_CORE_CLIENTID_CONFIRM:
|
|
|
|
rdpdr_server_receive_announce_response(context, s, header);
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case PAKID_CORE_CLIENT_NAME:
|
|
|
|
rdpdr_server_receive_client_name_request(context, s, header);
|
|
|
|
rdpdr_server_send_core_capability_request(context);
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case PAKID_CORE_CLIENT_CAPABILITY:
|
|
|
|
rdpdr_server_receive_core_capability_response(context, s, header);
|
2013-08-19 04:27:50 +04:00
|
|
|
rdpdr_server_send_client_id_confirm(context);
|
|
|
|
|
|
|
|
if (context->priv->UserLoggedOnPdu)
|
|
|
|
rdpdr_server_send_user_logged_on(context);
|
2013-08-19 02:09:12 +04:00
|
|
|
|
2014-08-18 19:23:03 +04:00
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case PAKID_CORE_DEVICELIST_ANNOUNCE:
|
2013-08-19 04:27:50 +04:00
|
|
|
rdpdr_server_receive_device_list_announce_request(context, s, header);
|
2013-08-19 02:09:12 +04:00
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case PAKID_CORE_DEVICE_REPLY:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case PAKID_CORE_DEVICE_IOREQUEST:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case PAKID_CORE_DEVICE_IOCOMPLETION:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case PAKID_CORE_DEVICELIST_REMOVE:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (header->Component == RDPDR_CTYP_PRN)
|
|
|
|
{
|
|
|
|
switch (header->PacketId)
|
|
|
|
{
|
|
|
|
case PAKID_PRN_CACHE_DATA:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
case PAKID_PRN_USING_XPS:
|
|
|
|
break;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2013-08-19 02:09:12 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_WARN(TAG, "Unknown RDPDR_HEADER.Component: 0x%04X", header->Component);
|
2013-08-19 02:09:12 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void* rdpdr_server_thread(void* arg)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2013-08-19 02:09:12 +04:00
|
|
|
DWORD status;
|
|
|
|
DWORD nCount;
|
2014-08-18 21:34:47 +04:00
|
|
|
void* buffer;
|
2013-08-19 02:09:12 +04:00
|
|
|
int position;
|
|
|
|
HANDLE events[8];
|
|
|
|
RDPDR_HEADER header;
|
|
|
|
HANDLE ChannelEvent;
|
2013-08-21 02:06:19 +04:00
|
|
|
DWORD BytesReturned;
|
2014-08-18 21:34:47 +04:00
|
|
|
RdpdrServerContext* context;
|
|
|
|
context = (RdpdrServerContext*) arg;
|
2013-08-19 02:09:12 +04:00
|
|
|
buffer = NULL;
|
|
|
|
BytesReturned = 0;
|
|
|
|
ChannelEvent = NULL;
|
|
|
|
s = Stream_New(NULL, 4096);
|
|
|
|
|
|
|
|
if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer, &BytesReturned) == TRUE)
|
|
|
|
{
|
|
|
|
if (BytesReturned == sizeof(HANDLE))
|
|
|
|
CopyMemory(&ChannelEvent, buffer, sizeof(HANDLE));
|
|
|
|
|
|
|
|
WTSFreeMemory(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
nCount = 0;
|
|
|
|
events[nCount++] = ChannelEvent;
|
|
|
|
events[nCount++] = context->priv->StopEvent;
|
2013-08-19 04:27:50 +04:00
|
|
|
rdpdr_server_send_announce_request(context);
|
2013-08-19 02:09:12 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2013-08-19 20:21:40 +04:00
|
|
|
BytesReturned = 0;
|
2013-08-19 02:09:12 +04:00
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
|
|
|
|
|
|
|
if (WaitForSingleObject(context->priv->StopEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-14 16:00:38 +04:00
|
|
|
WTSVirtualChannelRead(context->priv->ChannelHandle, 0, NULL, 0, &BytesReturned);
|
2014-08-18 19:23:03 +04:00
|
|
|
|
2014-07-14 16:00:38 +04:00
|
|
|
if (BytesReturned < 1)
|
|
|
|
continue;
|
2014-08-18 19:23:03 +04:00
|
|
|
|
2014-07-14 16:00:38 +04:00
|
|
|
Stream_EnsureRemainingCapacity(s, BytesReturned);
|
2014-08-18 19:23:03 +04:00
|
|
|
|
2014-07-14 16:00:38 +04:00
|
|
|
if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0,
|
2014-08-18 19:23:03 +04:00
|
|
|
(PCHAR) Stream_Buffer(s), Stream_Capacity(s), &BytesReturned))
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
2014-07-14 16:00:38 +04:00
|
|
|
break;
|
2013-08-19 02:09:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Stream_GetPosition(s) >= RDPDR_HEADER_LENGTH)
|
|
|
|
{
|
|
|
|
position = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
Stream_Read_UINT16(s, header.Component); /* Component (2 bytes) */
|
|
|
|
Stream_Read_UINT16(s, header.PacketId); /* PacketId (2 bytes) */
|
|
|
|
Stream_SetPosition(s, position);
|
|
|
|
Stream_SealLength(s);
|
|
|
|
Stream_SetPosition(s, RDPDR_HEADER_LENGTH);
|
|
|
|
rdpdr_server_receive_pdu(context, s, &header);
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_start(RdpdrServerContext* context)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
2014-02-27 22:30:04 +04:00
|
|
|
context->priv->ChannelHandle = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, "rdpdr");
|
2013-08-19 02:09:12 +04:00
|
|
|
|
|
|
|
if (!context->priv->ChannelHandle)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
context->priv->Thread = CreateThread(NULL, 0,
|
2014-08-18 21:34:47 +04:00
|
|
|
(LPTHREAD_START_ROUTINE) rdpdr_server_thread, (void*) context, 0, NULL);
|
2013-08-19 02:09:12 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int rdpdr_server_stop(RdpdrServerContext* context)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
SetEvent(context->priv->StopEvent);
|
|
|
|
WaitForSingleObject(context->priv->Thread, INFINITE);
|
|
|
|
CloseHandle(context->priv->Thread);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
RdpdrServerContext* rdpdr_server_context_new(HANDLE vcm)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
RdpdrServerContext* context;
|
|
|
|
context = (RdpdrServerContext*) malloc(sizeof(RdpdrServerContext));
|
2013-08-19 02:09:12 +04:00
|
|
|
|
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
ZeroMemory(context, sizeof(RdpdrServerContext));
|
|
|
|
context->vcm = vcm;
|
|
|
|
context->Start = rdpdr_server_start;
|
|
|
|
context->Stop = rdpdr_server_stop;
|
2014-08-18 21:34:47 +04:00
|
|
|
context->priv = (RdpdrServerPrivate*) malloc(sizeof(RdpdrServerPrivate));
|
2013-08-19 02:09:12 +04:00
|
|
|
|
|
|
|
if (context->priv)
|
|
|
|
{
|
|
|
|
ZeroMemory(context->priv, sizeof(RdpdrServerPrivate));
|
|
|
|
context->priv->VersionMajor = RDPDR_VERSION_MAJOR;
|
|
|
|
context->priv->VersionMinor = RDPDR_VERSION_MINOR_RDP6X;
|
|
|
|
context->priv->ClientId = g_ClientId++;
|
2013-08-19 04:27:50 +04:00
|
|
|
context->priv->UserLoggedOnPdu = TRUE;
|
2013-08-19 02:09:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void rdpdr_server_context_free(RdpdrServerContext* context)
|
2013-08-19 02:09:12 +04:00
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
if (context->priv)
|
|
|
|
{
|
|
|
|
free(context->priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(context);
|
|
|
|
}
|
|
|
|
}
|