Blame channels/remdesk/server/remdesk_main.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Remote Assistance Virtual Channel
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 * Copyright 2015 Thincast Technologies GmbH
Packit 1fb8d4
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/print.h>
Packit 1fb8d4
#include <winpr/stream.h>
Packit 1fb8d4
Packit 1fb8d4
#include "remdesk_main.h"
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT remdesk_virtual_channel_write(RemdeskServerContext* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	ULONG BytesWritten = 0;
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &BytesWritten);
Packit 1fb8d4
	return (status) ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT remdesk_read_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
	UINT32 ChannelNameLen;
Packit 1fb8d4
	char* pChannelName = NULL;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 8)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_GetRemainingLength failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT32(s, ChannelNameLen);     /* ChannelNameLen (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, header->DataLength); /* DataLen (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if (ChannelNameLen > 64)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "ChannelNameLen > 64!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((ChannelNameLen % 2) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "(ChannelNameLen %% 2) != 0!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < ChannelNameLen)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_GetRemainingLength failed!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	ZeroMemory(header->ChannelName, sizeof(header->ChannelName));
Packit Service 5a9772
	pChannelName = (char*)header->ChannelName;
Packit Service 5a9772
	status = ConvertFromUnicode(CP_UTF8, 0, (WCHAR*)Stream_Pointer(s), ChannelNameLen / 2,
Packit Service 5a9772
	                            &pChannelName, 32, NULL, NULL);
Packit 1fb8d4
	Stream_Seek(s, ChannelNameLen);
Packit 1fb8d4
Packit 1fb8d4
	if (status <= 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "ConvertFromUnicode failed!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT remdesk_write_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	int index;
Packit 1fb8d4
	UINT32 ChannelNameLen;
Packit 1fb8d4
	WCHAR ChannelNameW[32];
Packit 1fb8d4
	ZeroMemory(ChannelNameW, sizeof(ChannelNameW));
Packit 1fb8d4
Packit 1fb8d4
	for (index = 0; index < 32; index++)
Packit 1fb8d4
	{
Packit Service 5a9772
		ChannelNameW[index] = (WCHAR)header->ChannelName[index];
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	ChannelNameLen = (strnlen(header->ChannelName, sizeof(header->ChannelName)) + 1) * 2;
Packit Service 5a9772
	Stream_Write_UINT32(s, ChannelNameLen);        /* ChannelNameLen (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, header->DataLength);    /* DataLen (4 bytes) */
Packit 1fb8d4
	Stream_Write(s, ChannelNameW, ChannelNameLen); /* ChannelName (variable) */
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT remdesk_write_ctl_header(wStream* s, REMDESK_CTL_HEADER* ctlHeader)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
Packit Service 5a9772
	if ((error = remdesk_write_channel_header(s, (REMDESK_CHANNEL_HEADER*)ctlHeader)))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_write_channel_header failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		return error;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(s, ctlHeader->msgType); /* msgType (4 bytes) */
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT remdesk_prepare_ctl_header(REMDESK_CTL_HEADER* ctlHeader, UINT32 msgType,
Packit Service 5a9772
                                       UINT32 msgSize)
Packit 1fb8d4
{
Packit 1fb8d4
	ctlHeader->msgType = msgType;
Packit 1fb8d4
	sprintf_s(ctlHeader->ChannelName, ARRAYSIZE(ctlHeader->ChannelName), REMDESK_CHANNEL_CTL_NAME);
Packit 1fb8d4
	ctlHeader->DataLength = 4 + msgSize;
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT remdesk_send_ctl_result_pdu(RemdeskServerContext* context, UINT32 result)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	REMDESK_CTL_RESULT_PDU pdu;
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
	pdu.result = result;
Packit 1fb8d4
Packit Service 5a9772
	if ((error = remdesk_prepare_ctl_header(&(pdu.ctlHeader), REMDESK_CTL_RESULT, 4)))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_prepare_ctl_header failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		return error;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	s = Stream_New(NULL, REMDESK_CHANNEL_CTL_SIZE + pdu.ctlHeader.DataLength);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((error = remdesk_write_ctl_header(s, &(pdu.ctlHeader))))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_write_ctl_header failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		goto out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(s, pdu.result); /* result (4 bytes) */
Packit 1fb8d4
	Stream_SealLength(s);
Packit 1fb8d4
Packit 1fb8d4
	if ((error = remdesk_virtual_channel_write(context, s)))
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_virtual_channel_write failed with error %" PRIu32 "!", error);
Packit 1fb8d4
Packit 1fb8d4
out:
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT remdesk_send_ctl_version_info_pdu(RemdeskServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	REMDESK_CTL_VERSION_INFO_PDU pdu;
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
Packit Service 5a9772
	if ((error = remdesk_prepare_ctl_header(&(pdu.ctlHeader), REMDESK_CTL_VERSIONINFO, 8)))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_prepare_ctl_header failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		return error;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pdu.versionMajor = 1;
Packit 1fb8d4
	pdu.versionMinor = 2;
Packit 1fb8d4
	s = Stream_New(NULL, REMDESK_CHANNEL_CTL_SIZE + pdu.ctlHeader.DataLength);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((error = remdesk_write_ctl_header(s, &(pdu.ctlHeader))))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_write_ctl_header failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		goto out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(s, pdu.versionMajor); /* versionMajor (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, pdu.versionMinor); /* versionMinor (4 bytes) */
Packit 1fb8d4
	Stream_SealLength(s);
Packit 1fb8d4
Packit 1fb8d4
	if ((error = remdesk_virtual_channel_write(context, s)))
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_virtual_channel_write failed with error %" PRIu32 "!", error);
Packit 1fb8d4
Packit 1fb8d4
out:
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT remdesk_recv_ctl_version_info_pdu(RemdeskServerContext* context, wStream* s,
Packit Service 5a9772
                                              REMDESK_CHANNEL_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 versionMajor;
Packit 1fb8d4
	UINT32 versionMinor;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 8)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_GetRemainingLength failed!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, versionMajor); /* versionMajor (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, versionMinor); /* versionMinor (4 bytes) */
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT remdesk_recv_ctl_remote_control_desktop_pdu(RemdeskServerContext* context, wStream* s,
Packit Service 5a9772
                                                        REMDESK_CHANNEL_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
	int cchStringW;
Packit 1fb8d4
	WCHAR* pStringW;
Packit 1fb8d4
	UINT32 msgLength;
Packit 1fb8d4
	int cbRaConnectionStringW = 0;
Packit 1fb8d4
	WCHAR* raConnectionStringW = NULL;
Packit 1fb8d4
	REMDESK_CTL_REMOTE_CONTROL_DESKTOP_PDU pdu;
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
	msgLength = header->DataLength - 4;
Packit Service 5a9772
	pStringW = (WCHAR*)Stream_Pointer(s);
Packit 1fb8d4
	raConnectionStringW = pStringW;
Packit 1fb8d4
	cchStringW = 0;
Packit 1fb8d4
Packit 1fb8d4
	while ((msgLength > 0) && pStringW[cchStringW])
Packit 1fb8d4
	{
Packit 1fb8d4
		msgLength -= 2;
Packit 1fb8d4
		cchStringW++;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (pStringW[cchStringW] || !cchStringW)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	cchStringW++;
Packit 1fb8d4
	cbRaConnectionStringW = cchStringW * 2;
Packit 1fb8d4
	pdu.raConnectionString = NULL;
Packit Service 5a9772
	status = ConvertFromUnicode(CP_UTF8, 0, raConnectionStringW, cbRaConnectionStringW / 2,
Packit Service 5a9772
	                            &pdu.raConnectionString, 0, NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (status <= 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "ConvertFromUnicode failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	WLog_INFO(TAG, "RaConnectionString: %s", pdu.raConnectionString);
Packit 1fb8d4
	free(pdu.raConnectionString);
Packit 1fb8d4
Packit 1fb8d4
	if ((error = remdesk_send_ctl_result_pdu(context, 0)))
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_send_ctl_result_pdu failed with error %" PRIu32 "!", error);
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT remdesk_recv_ctl_authenticate_pdu(RemdeskServerContext* context, wStream* s,
Packit Service 5a9772
                                              REMDESK_CHANNEL_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
	int cchStringW;
Packit 1fb8d4
	WCHAR* pStringW;
Packit 1fb8d4
	UINT32 msgLength;
Packit 1fb8d4
	int cbExpertBlobW = 0;
Packit 1fb8d4
	WCHAR* expertBlobW = NULL;
Packit 1fb8d4
	int cbRaConnectionStringW = 0;
Packit 1fb8d4
	WCHAR* raConnectionStringW = NULL;
Packit 1fb8d4
	REMDESK_CTL_AUTHENTICATE_PDU pdu;
Packit 1fb8d4
	msgLength = header->DataLength - 4;
Packit Service 5a9772
	pStringW = (WCHAR*)Stream_Pointer(s);
Packit 1fb8d4
	raConnectionStringW = pStringW;
Packit 1fb8d4
	cchStringW = 0;
Packit 1fb8d4
Packit 1fb8d4
	while ((msgLength > 0) && pStringW[cchStringW])
Packit 1fb8d4
	{
Packit 1fb8d4
		msgLength -= 2;
Packit 1fb8d4
		cchStringW++;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (pStringW[cchStringW] || !cchStringW)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	cchStringW++;
Packit 1fb8d4
	cbRaConnectionStringW = cchStringW * 2;
Packit 1fb8d4
	pStringW += cchStringW;
Packit 1fb8d4
	expertBlobW = pStringW;
Packit 1fb8d4
	cchStringW = 0;
Packit 1fb8d4
Packit 1fb8d4
	while ((msgLength > 0) && pStringW[cchStringW])
Packit 1fb8d4
	{
Packit 1fb8d4
		msgLength -= 2;
Packit 1fb8d4
		cchStringW++;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (pStringW[cchStringW] || !cchStringW)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	cchStringW++;
Packit 1fb8d4
	cbExpertBlobW = cchStringW * 2;
Packit 1fb8d4
	pdu.raConnectionString = NULL;
Packit Service 5a9772
	status = ConvertFromUnicode(CP_UTF8, 0, raConnectionStringW, cbRaConnectionStringW / 2,
Packit Service 5a9772
	                            &pdu.raConnectionString, 0, NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (status <= 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "ConvertFromUnicode failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pdu.expertBlob = NULL;
Packit Service 5a9772
	status = ConvertFromUnicode(CP_UTF8, 0, expertBlobW, cbExpertBlobW / 2, &pdu.expertBlob, 0,
Packit Service 5a9772
	                            NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (status <= 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "ConvertFromUnicode failed!");
Packit 1fb8d4
		free(pdu.raConnectionString);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	WLog_INFO(TAG, "RaConnectionString: %s ExpertBlob: %s", pdu.raConnectionString, pdu.expertBlob);
Packit 1fb8d4
	free(pdu.raConnectionString);
Packit 1fb8d4
	free(pdu.expertBlob);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT remdesk_recv_ctl_verify_password_pdu(RemdeskServerContext* context, wStream* s,
Packit Service 5a9772
                                                 REMDESK_CHANNEL_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
	int cbExpertBlobW = 0;
Packit 1fb8d4
	WCHAR* expertBlobW = NULL;
Packit 1fb8d4
	REMDESK_CTL_VERIFY_PASSWORD_PDU pdu;
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 8)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_GetRemainingLength failed!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pdu.expertBlob = NULL;
Packit Service 5a9772
	expertBlobW = (WCHAR*)Stream_Pointer(s);
Packit 1fb8d4
	cbExpertBlobW = header->DataLength - 4;
Packit Service 5a9772
	status = ConvertFromUnicode(CP_UTF8, 0, expertBlobW, cbExpertBlobW / 2, &pdu.expertBlob, 0,
Packit Service 5a9772
	                            NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (status <= 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "ConvertFromUnicode failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	WLog_INFO(TAG, "ExpertBlob: %s", pdu.expertBlob);
Packit 1fb8d4
Packit 1fb8d4
	if ((error = remdesk_send_ctl_result_pdu(context, 0)))
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_send_ctl_result_pdu failed with error %" PRIu32 "!", error);
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT remdesk_recv_ctl_pdu(RemdeskServerContext* context, wStream* s,
Packit 1fb8d4
                                 REMDESK_CHANNEL_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	UINT32 msgType = 0;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_GetRemainingLength failed!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, msgType); /* msgType (4 bytes) */
Packit Service 5a9772
	WLog_INFO(TAG, "msgType: %" PRIu32 "", msgType);
Packit 1fb8d4
Packit 1fb8d4
	switch (msgType)
Packit 1fb8d4
	{
Packit 1fb8d4
		case REMDESK_CTL_REMOTE_CONTROL_DESKTOP:
Packit 1fb8d4
			if ((error = remdesk_recv_ctl_remote_control_desktop_pdu(context, s, header)))
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG,
Packit Service 5a9772
				         "remdesk_recv_ctl_remote_control_desktop_pdu failed with error %" PRIu32
Packit Service 5a9772
				         "!",
Packit Service 5a9772
				         error);
Packit 1fb8d4
				return error;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case REMDESK_CTL_AUTHENTICATE:
Packit 1fb8d4
			if ((error = remdesk_recv_ctl_authenticate_pdu(context, s, header)))
Packit 1fb8d4
			{
Packit Service 5a9772
				WLog_ERR(TAG, "remdesk_recv_ctl_authenticate_pdu failed with error %" PRIu32 "!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
				return error;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case REMDESK_CTL_DISCONNECT:
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case REMDESK_CTL_VERSIONINFO:
Packit 1fb8d4
			if ((error = remdesk_recv_ctl_version_info_pdu(context, s, header)))
Packit 1fb8d4
			{
Packit Service 5a9772
				WLog_ERR(TAG, "remdesk_recv_ctl_version_info_pdu failed with error %" PRIu32 "!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
				return error;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case REMDESK_CTL_ISCONNECTED:
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case REMDESK_CTL_VERIFY_PASSWORD:
Packit 1fb8d4
			if ((error = remdesk_recv_ctl_verify_password_pdu(context, s, header)))
Packit 1fb8d4
			{
Packit Service 5a9772
				WLog_ERR(TAG, "remdesk_recv_ctl_verify_password_pdu failed with error %" PRIu32 "!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
				return error;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case REMDESK_CTL_EXPERT_ON_VISTA:
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case REMDESK_CTL_RANOVICE_NAME:
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case REMDESK_CTL_RAEXPERT_NAME:
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case REMDESK_CTL_TOKEN:
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit Service 5a9772
			WLog_ERR(TAG, "remdesk_recv_control_pdu: unknown msgType: %" PRIu32 "", msgType);
Packit 1fb8d4
			error = ERROR_INVALID_DATA;
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT remdesk_server_receive_pdu(RemdeskServerContext* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	REMDESK_CHANNEL_HEADER header;
Packit 1fb8d4
#if 0
Packit 1fb8d4
	WLog_INFO(TAG, "RemdeskReceive: %"PRIuz"", Stream_GetRemainingLength(s));
Packit 1fb8d4
	winpr_HexDump(Stream_Pointer(s), Stream_GetRemainingLength(s));
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
	if ((error = remdesk_read_channel_header(s, &header)))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_read_channel_header failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		return error;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(header.ChannelName, "RC_CTL") == 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		if ((error = remdesk_recv_ctl_pdu(context, s, &header)))
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "remdesk_recv_ctl_pdu failed with error %" PRIu32 "!", error);
Packit 1fb8d4
			return error;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (strcmp(header.ChannelName, "70") == 0)
Packit 1fb8d4
	{
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (strcmp(header.ChannelName, "71") == 0)
Packit 1fb8d4
	{
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (strcmp(header.ChannelName, ".") == 0)
Packit 1fb8d4
	{
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (strcmp(header.ChannelName, "1000.") == 0)
Packit 1fb8d4
	{
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (strcmp(header.ChannelName, "RA_FX") == 0)
Packit 1fb8d4
	{
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static DWORD WINAPI remdesk_server_thread(LPVOID arg)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	DWORD status;
Packit 1fb8d4
	DWORD nCount;
Packit 1fb8d4
	void* buffer;
Packit 1fb8d4
	UINT32* pHeader;
Packit 1fb8d4
	UINT32 PduLength;
Packit 1fb8d4
	HANDLE events[8];
Packit 1fb8d4
	HANDLE ChannelEvent;
Packit 1fb8d4
	DWORD BytesReturned;
Packit 1fb8d4
	RemdeskServerContext* context;
Packit 1fb8d4
	UINT error;
Packit Service 5a9772
	context = (RemdeskServerContext*)arg;
Packit 1fb8d4
	buffer = NULL;
Packit 1fb8d4
	BytesReturned = 0;
Packit 1fb8d4
	ChannelEvent = NULL;
Packit 1fb8d4
	s = Stream_New(NULL, 4096);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		error = CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		goto out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
Packit Service 5a9772
	                           &BytesReturned) == TRUE)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (BytesReturned == sizeof(HANDLE))
Packit 1fb8d4
			CopyMemory(&ChannelEvent, buffer, sizeof(HANDLE));
Packit 1fb8d4
Packit 1fb8d4
		WTSFreeMemory(buffer);
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "WTSVirtualChannelQuery failed!");
Packit 1fb8d4
		error = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
		goto out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	nCount = 0;
Packit 1fb8d4
	events[nCount++] = ChannelEvent;
Packit 1fb8d4
	events[nCount++] = context->priv->StopEvent;
Packit 1fb8d4
Packit 1fb8d4
	if ((error = remdesk_send_ctl_version_info_pdu(context)))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "remdesk_send_ctl_version_info_pdu failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		goto out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	while (1)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
Packit 1fb8d4
Packit 1fb8d4
		if (status == WAIT_FAILED)
Packit 1fb8d4
		{
Packit 1fb8d4
			error = GetLastError();
Packit Service 5a9772
			WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "", error);
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		status = WaitForSingleObject(context->priv->StopEvent, 0);
Packit 1fb8d4
Packit 1fb8d4
		if (status == WAIT_FAILED)
Packit 1fb8d4
		{
Packit 1fb8d4
			error = GetLastError();
Packit Service 5a9772
			WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (status == WAIT_OBJECT_0)
Packit 1fb8d4
		{
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		if (WTSVirtualChannelRead(context->priv->ChannelHandle, 0, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
		                          Stream_Capacity(s), &BytesReturned))
Packit 1fb8d4
		{
Packit 1fb8d4
			if (BytesReturned)
Packit 1fb8d4
				Stream_Seek(s, BytesReturned);
Packit 1fb8d4
		}
Packit 1fb8d4
		else
Packit 1fb8d4
		{
Packit 1fb8d4
			if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
Packit 1fb8d4
				error = CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
				break;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (Stream_GetPosition(s) >= 8)
Packit 1fb8d4
		{
Packit Service 5a9772
			pHeader = (UINT32*)Stream_Buffer(s);
Packit 1fb8d4
			PduLength = pHeader[0] + pHeader[1] + 8;
Packit 1fb8d4
Packit 1fb8d4
			if (PduLength >= Stream_GetPosition(s))
Packit 1fb8d4
			{
Packit 1fb8d4
				Stream_SealLength(s);
Packit 1fb8d4
				Stream_SetPosition(s, 0);
Packit 1fb8d4
Packit 1fb8d4
				if ((error = remdesk_server_receive_pdu(context, s)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG, "remdesk_server_receive_pdu failed with error %" PRIu32 "!",
Packit Service 5a9772
					         error);
Packit 1fb8d4
					break;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				Stream_SetPosition(s, 0);
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
out:
Packit 1fb8d4
Packit 1fb8d4
	if (error && context->rdpcontext)
Packit Service 5a9772
		setChannelError(context->rdpcontext, error, "remdesk_server_thread reported an error");
Packit 1fb8d4
Packit 1fb8d4
	ExitThread(error);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT remdesk_server_start(RemdeskServerContext* context)
Packit 1fb8d4
{
Packit Service 5a9772
	context->priv->ChannelHandle =
Packit Service 5a9772
	    WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, "remdesk");
Packit 1fb8d4
Packit 1fb8d4
	if (!context->priv->ChannelHandle)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "WTSVirtualChannelOpen failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!(context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "CreateEvent failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (!(context->priv->Thread =
Packit Service 5a9772
	          CreateThread(NULL, 0, remdesk_server_thread, (void*)context, 0, NULL)))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "CreateThread failed!");
Packit 1fb8d4
		CloseHandle(context->priv->StopEvent);
Packit 1fb8d4
		context->priv->StopEvent = NULL;
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT remdesk_server_stop(RemdeskServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
	SetEvent(context->priv->StopEvent);
Packit 1fb8d4
Packit 1fb8d4
	if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
Packit 1fb8d4
	{
Packit 1fb8d4
		error = GetLastError();
Packit Service 5a9772
		WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		return error;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	CloseHandle(context->priv->Thread);
Packit 1fb8d4
	CloseHandle(context->priv->StopEvent);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
RemdeskServerContext* remdesk_server_context_new(HANDLE vcm)
Packit 1fb8d4
{
Packit 1fb8d4
	RemdeskServerContext* context;
Packit Service 5a9772
	context = (RemdeskServerContext*)calloc(1, sizeof(RemdeskServerContext));
Packit 1fb8d4
Packit 1fb8d4
	if (context)
Packit 1fb8d4
	{
Packit 1fb8d4
		context->vcm = vcm;
Packit 1fb8d4
		context->Start = remdesk_server_start;
Packit 1fb8d4
		context->Stop = remdesk_server_stop;
Packit Service 5a9772
		context->priv = (RemdeskServerPrivate*)calloc(1, sizeof(RemdeskServerPrivate));
Packit 1fb8d4
Packit 1fb8d4
		if (!context->priv)
Packit 1fb8d4
		{
Packit 1fb8d4
			free(context);
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		context->priv->Version = 1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return context;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void remdesk_server_context_free(RemdeskServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	if (context)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
Packit 1fb8d4
			WTSVirtualChannelClose(context->priv->ChannelHandle);
Packit 1fb8d4
Packit 1fb8d4
		free(context->priv);
Packit 1fb8d4
		free(context);
Packit 1fb8d4
	}
Packit 1fb8d4
}