2014-06-24 00:18:03 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Multiparty Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2015-06-08 15:44:10 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2014-06-24 00:18:03 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/print.h>
|
|
|
|
#include <winpr/stream.h>
|
|
|
|
|
2014-09-12 18:19:32 +04:00
|
|
|
#include <freerdp/channels/log.h>
|
|
|
|
|
2014-06-24 00:18:03 +04:00
|
|
|
#include "encomsp_main.h"
|
|
|
|
|
2014-09-12 18:19:32 +04:00
|
|
|
#define TAG CHANNELS_TAG("encomsp.server")
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT encomsp_read_header(wStream* s, ENCOMSP_ORDER_HEADER* header)
|
2014-07-16 00:34:15 +04:00
|
|
|
{
|
|
|
|
if (Stream_GetRemainingLength(s) < ENCOMSP_ORDER_HEADER_SIZE)
|
2015-06-08 15:44:10 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2014-07-16 00:34:15 +04:00
|
|
|
|
|
|
|
Stream_Read_UINT16(s, header->Type); /* Type (2 bytes) */
|
|
|
|
Stream_Read_UINT16(s, header->Length); /* Length (2 bytes) */
|
2015-06-08 15:44:10 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-07-16 00:34:15 +04:00
|
|
|
}
|
|
|
|
|
2014-08-19 01:25:48 +04:00
|
|
|
#if 0
|
|
|
|
|
2014-07-16 00:34:15 +04:00
|
|
|
static int encomsp_write_header(wStream* s, ENCOMSP_ORDER_HEADER* header)
|
|
|
|
{
|
|
|
|
Stream_Write_UINT16(s, header->Type); /* Type (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, header->Length); /* Length (2 bytes) */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int encomsp_read_unicode_string(wStream* s, ENCOMSP_UNICODE_STRING* str)
|
|
|
|
{
|
|
|
|
ZeroMemory(str, sizeof(ENCOMSP_UNICODE_STRING));
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
Stream_Read_UINT16(s, str->cchString); /* cchString (2 bytes) */
|
|
|
|
|
|
|
|
if (str->cchString > 1024)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < (str->cchString * 2))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
Stream_Read(s, &(str->wString), (str->cchString * 2)); /* String (variable) */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-19 01:25:48 +04:00
|
|
|
#endif
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2016-09-26 13:12:37 +03:00
|
|
|
static UINT encomsp_recv_change_participant_control_level_pdu(
|
|
|
|
EncomspServerContext* context, wStream* s, ENCOMSP_ORDER_HEADER* header)
|
2014-07-16 00:34:15 +04:00
|
|
|
{
|
|
|
|
int beg, end;
|
|
|
|
ENCOMSP_CHANGE_PARTICIPANT_CONTROL_LEVEL_PDU pdu;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2014-07-16 00:34:15 +04:00
|
|
|
beg = ((int) Stream_GetPosition(s)) - ENCOMSP_ORDER_HEADER_SIZE;
|
|
|
|
CopyMemory(&pdu, header, sizeof(ENCOMSP_ORDER_HEADER));
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 6)
|
2015-06-08 15:44:10 +03:00
|
|
|
{
|
2016-12-01 13:15:11 +03:00
|
|
|
WLog_ERR(TAG, "Not enough data!");
|
2015-06-08 15:44:10 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
2014-07-16 00:34:15 +04:00
|
|
|
|
|
|
|
Stream_Read_UINT16(s, pdu.Flags); /* Flags (2 bytes) */
|
|
|
|
Stream_Read_UINT32(s, pdu.ParticipantId); /* ParticipantId (4 bytes) */
|
|
|
|
end = (int) Stream_GetPosition(s);
|
|
|
|
|
|
|
|
if ((beg + header->Length) < end)
|
2015-06-08 15:44:10 +03:00
|
|
|
{
|
2016-12-01 13:15:11 +03:00
|
|
|
WLog_ERR(TAG, "Not enough data!");
|
2015-06-08 15:44:10 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
2014-07-16 00:34:15 +04:00
|
|
|
|
|
|
|
if ((beg + header->Length) > end)
|
|
|
|
{
|
|
|
|
if (Stream_GetRemainingLength(s) < ((beg + header->Length) - end))
|
2015-06-08 15:44:10 +03:00
|
|
|
{
|
2016-12-01 13:15:11 +03:00
|
|
|
WLog_ERR(TAG, "Not enough data!");
|
2015-06-08 15:44:10 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
2014-07-16 00:34:15 +04:00
|
|
|
|
|
|
|
Stream_SetPosition(s, (beg + header->Length));
|
|
|
|
}
|
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
IFCALLRET(context->ChangeParticipantControlLevel, error, context, &pdu);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
if (error)
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "context->ChangeParticipantControlLevel failed with error %"PRIu32"",
|
2016-09-26 13:12:37 +03:00
|
|
|
error);
|
2014-07-16 00:34:15 +04:00
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
return error;
|
2014-07-16 00:34:15 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2016-09-26 13:12:37 +03:00
|
|
|
static UINT encomsp_server_receive_pdu(EncomspServerContext* context,
|
|
|
|
wStream* s)
|
2014-06-24 00:18:03 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2014-07-15 02:01:29 +04:00
|
|
|
ENCOMSP_ORDER_HEADER header;
|
|
|
|
|
|
|
|
while (Stream_GetRemainingLength(s) > 0)
|
|
|
|
{
|
2015-06-08 15:44:10 +03:00
|
|
|
if ((error = encomsp_read_header(s, &header)))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "encomsp_read_header failed with error %"PRIu32"!", error);
|
2015-06-08 15:44:10 +03:00
|
|
|
return error;
|
|
|
|
}
|
2014-07-15 02:01:29 +04:00
|
|
|
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_INFO(TAG, "EncomspReceive: Type: %"PRIu16" Length: %"PRIu16"", header.Type,
|
2016-09-26 13:12:37 +03:00
|
|
|
header.Length);
|
2014-07-15 02:01:29 +04:00
|
|
|
|
|
|
|
switch (header.Type)
|
|
|
|
{
|
|
|
|
case ODTYPE_PARTICIPANT_CTRL_CHANGED:
|
2016-09-26 13:12:37 +03:00
|
|
|
if ((error = encomsp_recv_change_participant_control_level_pdu(context, s,
|
|
|
|
&header)))
|
2015-06-08 15:44:10 +03:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
WLog_ERR(TAG,
|
2016-12-14 00:47:08 +03:00
|
|
|
"encomsp_recv_change_participant_control_level_pdu failed with error %"PRIu32"!",
|
2016-09-26 13:12:37 +03:00
|
|
|
error);
|
2015-06-08 15:44:10 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2014-07-15 02:01:29 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "header.Type unknown %"PRIu16"!", header.Type);
|
2015-06-08 15:44:10 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2014-07-15 02:01:29 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
return error;
|
2014-06-24 00:18:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* encomsp_server_thread(void* arg)
|
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
DWORD nCount;
|
|
|
|
void* buffer;
|
|
|
|
HANDLE events[8];
|
|
|
|
HANDLE ChannelEvent;
|
|
|
|
DWORD BytesReturned;
|
2014-07-16 00:34:15 +04:00
|
|
|
ENCOMSP_ORDER_HEADER* header;
|
2014-06-24 00:18:03 +04:00
|
|
|
EncomspServerContext* context;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2016-09-26 13:12:37 +03:00
|
|
|
DWORD status;
|
2014-06-24 00:18:03 +04:00
|
|
|
context = (EncomspServerContext*) arg;
|
2016-11-15 19:41:01 +03:00
|
|
|
|
2014-06-24 00:18:03 +04:00
|
|
|
buffer = NULL;
|
|
|
|
BytesReturned = 0;
|
|
|
|
ChannelEvent = NULL;
|
|
|
|
s = Stream_New(NULL, 4096);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
2015-07-15 10:50:35 +03:00
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto out;
|
2015-06-08 15:44:10 +03:00
|
|
|
}
|
2014-06-24 00:18:03 +04:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle,
|
|
|
|
&buffer, &BytesReturned) == TRUE)
|
2014-06-24 00:18:03 +04:00
|
|
|
{
|
|
|
|
if (BytesReturned == sizeof(HANDLE))
|
|
|
|
CopyMemory(&ChannelEvent, buffer, sizeof(HANDLE));
|
|
|
|
|
|
|
|
WTSFreeMemory(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
nCount = 0;
|
|
|
|
events[nCount++] = ChannelEvent;
|
|
|
|
events[nCount++] = context->priv->StopEvent;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
2014-06-24 00:18:03 +04:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "WaitForMultipleObjects failed with error %"PRIu32"", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
status = WaitForSingleObject(context->priv->StopEvent, 0);
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %"PRIu32"", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-07-30 16:49:21 +03:00
|
|
|
|
|
|
|
if (status == WAIT_OBJECT_0)
|
2014-06-24 00:18:03 +04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-14 16:00:38 +04:00
|
|
|
WTSVirtualChannelRead(context->priv->ChannelHandle, 0, NULL, 0, &BytesReturned);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-14 16:00:38 +04:00
|
|
|
if (BytesReturned < 1)
|
|
|
|
continue;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-14 16:00:38 +04:00
|
|
|
if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0,
|
2016-09-26 13:12:37 +03:00
|
|
|
(PCHAR) Stream_Buffer(s), Stream_Capacity(s), &BytesReturned))
|
2014-06-24 00:18:03 +04:00
|
|
|
{
|
2015-06-08 15:44:10 +03:00
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
2014-07-14 16:00:38 +04:00
|
|
|
break;
|
2014-06-24 00:18:03 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 00:34:15 +04:00
|
|
|
if (Stream_GetPosition(s) >= ENCOMSP_ORDER_HEADER_SIZE)
|
|
|
|
{
|
|
|
|
header = (ENCOMSP_ORDER_HEADER*) Stream_Buffer(s);
|
|
|
|
|
|
|
|
if (header->Length >= Stream_GetPosition(s))
|
|
|
|
{
|
|
|
|
Stream_SealLength(s);
|
|
|
|
Stream_SetPosition(s, 0);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
if ((error = encomsp_server_receive_pdu(context, s)))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "encomsp_server_receive_pdu failed with error %"PRIu32"!", error);
|
2015-06-08 15:44:10 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-16 00:34:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
}
|
|
|
|
}
|
2014-06-24 00:18:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Free(s, TRUE);
|
2015-07-15 10:50:35 +03:00
|
|
|
out:
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (error && context->rdpcontext)
|
2016-09-26 13:12:37 +03:00
|
|
|
setChannelError(context->rdpcontext, error,
|
|
|
|
"encomsp_server_thread reported an error");
|
2015-07-15 10:50:35 +03:00
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
ExitThread((DWORD)error);
|
2014-06-24 00:18:03 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT encomsp_server_start(EncomspServerContext* context)
|
2014-06-24 00:18:03 +04:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
context->priv->ChannelHandle = WTSVirtualChannelOpen(context->vcm,
|
|
|
|
WTS_CURRENT_SESSION, "encomsp");
|
2014-06-24 00:18:03 +04:00
|
|
|
|
|
|
|
if (!context->priv->ChannelHandle)
|
2015-06-08 15:44:10 +03:00
|
|
|
return CHANNEL_RC_BAD_CHANNEL;
|
2014-06-24 00:18:03 +04:00
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
if (!(context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2014-06-24 00:18:03 +04:00
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
if (!(context->priv->Thread = CreateThread(NULL, 0,
|
2016-09-26 13:12:37 +03:00
|
|
|
(LPTHREAD_START_ROUTINE) encomsp_server_thread, (void*) context, 0, NULL)))
|
2015-06-08 15:44:10 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
|
|
|
CloseHandle(context->priv->StopEvent);
|
|
|
|
context->priv->StopEvent = NULL;
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2014-06-24 00:18:03 +04:00
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-06-24 00:18:03 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT encomsp_server_stop(EncomspServerContext* context)
|
2014-06-24 00:18:03 +04:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2014-06-24 00:18:03 +04:00
|
|
|
SetEvent(context->priv->StopEvent);
|
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
|
2016-09-26 13:12:37 +03:00
|
|
|
{
|
|
|
|
error = GetLastError();
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %"PRIu32"", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
return error;
|
|
|
|
}
|
2014-06-24 00:18:03 +04:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
CloseHandle(context->priv->Thread);
|
2017-05-11 23:21:47 +03:00
|
|
|
CloseHandle(context->priv->StopEvent);
|
2015-07-30 16:49:21 +03:00
|
|
|
return error;
|
2014-06-24 00:18:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
EncomspServerContext* encomsp_server_context_new(HANDLE vcm)
|
|
|
|
{
|
|
|
|
EncomspServerContext* context;
|
|
|
|
context = (EncomspServerContext*) calloc(1, sizeof(EncomspServerContext));
|
|
|
|
|
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
context->vcm = vcm;
|
|
|
|
context->Start = encomsp_server_start;
|
|
|
|
context->Stop = encomsp_server_stop;
|
|
|
|
context->priv = (EncomspServerPrivate*) calloc(1, sizeof(EncomspServerPrivate));
|
|
|
|
|
2015-06-08 15:44:10 +03:00
|
|
|
if (!context->priv)
|
2014-06-24 00:18:03 +04:00
|
|
|
{
|
2015-06-08 15:44:10 +03:00
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
free(context);
|
|
|
|
return NULL;
|
2014-06-24 00:18:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void encomsp_server_context_free(EncomspServerContext* context)
|
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
{
|
2017-05-11 23:21:47 +03:00
|
|
|
if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
|
|
|
|
WTSVirtualChannelClose(context->priv->ChannelHandle);
|
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(context->priv);
|
2014-06-24 00:18:03 +04:00
|
|
|
free(context);
|
|
|
|
}
|
|
|
|
}
|