2014-02-12 00:42:28 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Virtual Channels
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
2015-02-16 14:16:54 +03:00
|
|
|
* Copyright 2015 Copyright 2015 Thincast Technologies GmbH
|
2014-02-12 00:42:28 +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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/stream.h>
|
2014-02-17 05:12:45 +04:00
|
|
|
#include <winpr/wtsapi.h>
|
2014-02-12 00:42:28 +04:00
|
|
|
|
|
|
|
#include <freerdp/freerdp.h>
|
|
|
|
#include <freerdp/constants.h>
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#include <freerdp/log.h>
|
2014-02-12 00:42:28 +04:00
|
|
|
#include <freerdp/svc.h>
|
|
|
|
#include <freerdp/peer.h>
|
|
|
|
#include <freerdp/addin.h>
|
2014-09-12 16:36:29 +04:00
|
|
|
|
2014-02-12 00:42:28 +04:00
|
|
|
#include <freerdp/client/channels.h>
|
|
|
|
#include <freerdp/client/drdynvc.h>
|
|
|
|
#include <freerdp/channels/channels.h>
|
|
|
|
|
|
|
|
#include "rdp.h"
|
2014-02-16 00:26:34 +04:00
|
|
|
#include "client.h"
|
2014-02-17 05:41:19 +04:00
|
|
|
#include "server.h"
|
2014-02-12 00:42:28 +04:00
|
|
|
#include "channels.h"
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("core.channels")
|
|
|
|
|
2020-03-04 16:42:42 +03:00
|
|
|
BOOL freerdp_channel_send(rdpRdp* rdp, UINT16 channelId, const BYTE* data, size_t size)
|
2014-02-12 00:42:28 +04:00
|
|
|
{
|
|
|
|
DWORD i;
|
2020-03-04 16:42:42 +03:00
|
|
|
size_t left;
|
2014-02-12 00:42:28 +04:00
|
|
|
wStream* s;
|
|
|
|
UINT32 flags;
|
2020-02-20 10:22:19 +03:00
|
|
|
size_t chunkSize;
|
2014-02-16 01:32:38 +04:00
|
|
|
rdpMcs* mcs = rdp->mcs;
|
|
|
|
rdpMcsChannel* channel = NULL;
|
2014-02-12 00:42:28 +04:00
|
|
|
|
2014-02-16 01:32:38 +04:00
|
|
|
for (i = 0; i < mcs->channelCount; i++)
|
2014-02-12 00:42:28 +04:00
|
|
|
{
|
2014-02-16 01:32:38 +04:00
|
|
|
if (mcs->channels[i].ChannelId == channelId)
|
2014-02-12 00:42:28 +04:00
|
|
|
{
|
2014-02-16 01:32:38 +04:00
|
|
|
channel = &mcs->channels[i];
|
2014-02-12 00:42:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!channel)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "freerdp_channel_send: unknown channelId %" PRIu16 "", channelId);
|
2014-02-12 00:42:28 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = CHANNEL_FLAG_FIRST;
|
|
|
|
left = size;
|
|
|
|
|
|
|
|
while (left > 0)
|
|
|
|
{
|
|
|
|
s = rdp_send_stream_init(rdp);
|
2016-07-14 17:08:06 +03:00
|
|
|
|
2015-03-26 19:09:47 +03:00
|
|
|
if (!s)
|
|
|
|
return FALSE;
|
2014-02-12 00:42:28 +04:00
|
|
|
|
2020-03-04 16:42:42 +03:00
|
|
|
if (left > rdp->settings->VirtualChannelChunkSize)
|
2014-02-12 00:42:28 +04:00
|
|
|
{
|
|
|
|
chunkSize = rdp->settings->VirtualChannelChunkSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chunkSize = left;
|
|
|
|
flags |= CHANNEL_FLAG_LAST;
|
|
|
|
}
|
|
|
|
|
2019-07-08 13:14:49 +03:00
|
|
|
if (!rdp->settings->ServerMode && (channel->options & CHANNEL_OPTION_SHOW_PROTOCOL))
|
2014-02-12 00:42:28 +04:00
|
|
|
{
|
|
|
|
flags |= CHANNEL_FLAG_SHOW_PROTOCOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Write_UINT32(s, size);
|
|
|
|
Stream_Write_UINT32(s, flags);
|
2016-07-14 17:08:06 +03:00
|
|
|
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureCapacity(s, chunkSize))
|
2015-03-26 19:09:47 +03:00
|
|
|
{
|
|
|
|
Stream_Release(s);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-07-14 17:08:06 +03:00
|
|
|
|
2014-02-12 00:42:28 +04:00
|
|
|
Stream_Write(s, data, chunkSize);
|
|
|
|
|
2018-01-31 14:44:45 +03:00
|
|
|
/* WLog_DBG(TAG, "%s: sending data (flags=0x%x size=%d)", __FUNCTION__, flags, size); */
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!rdp_send(rdp, s, channelId))
|
2015-03-26 19:09:47 +03:00
|
|
|
return FALSE;
|
2014-02-12 00:42:28 +04:00
|
|
|
|
|
|
|
data += chunkSize;
|
|
|
|
left -= chunkSize;
|
|
|
|
flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-03-04 16:26:41 +03:00
|
|
|
BOOL freerdp_channel_process(freerdp* instance, wStream* s, UINT16 channelId, size_t packetLength)
|
2014-02-12 00:42:28 +04:00
|
|
|
{
|
2020-03-09 12:58:02 +03:00
|
|
|
BOOL rc = FALSE;
|
2014-02-12 00:42:28 +04:00
|
|
|
UINT32 length;
|
|
|
|
UINT32 flags;
|
2020-02-20 10:22:19 +03:00
|
|
|
size_t chunkLength;
|
2014-02-12 00:42:28 +04:00
|
|
|
|
2020-03-04 16:26:41 +03:00
|
|
|
if (packetLength < 8)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Header length %" PRIdz " bytes promised, none available", packetLength);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
packetLength -= 8;
|
|
|
|
|
2014-02-12 00:42:28 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 8)
|
|
|
|
return FALSE;
|
|
|
|
|
2020-03-04 16:59:27 +03:00
|
|
|
/* [MS-RDPBCGR] 3.1.5.2.2 Processing of Virtual Channel PDU
|
|
|
|
* chunked data. Length is the total size of the combined data,
|
|
|
|
* chunkLength is the actual data received.
|
|
|
|
* check chunkLength against packetLength, which is the TPKT header size.
|
|
|
|
*/
|
2014-02-12 00:42:28 +04:00
|
|
|
Stream_Read_UINT32(s, length);
|
|
|
|
Stream_Read_UINT32(s, flags);
|
|
|
|
chunkLength = Stream_GetRemainingLength(s);
|
2020-03-04 16:26:41 +03:00
|
|
|
if (packetLength != chunkLength)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Header length %" PRIdz " != actual length %" PRIdz, packetLength,
|
|
|
|
chunkLength);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (length < chunkLength)
|
|
|
|
{
|
2020-03-04 16:59:27 +03:00
|
|
|
WLog_ERR(TAG, "Expected %" PRIu32 " bytes, but have %" PRIdz, length, chunkLength);
|
2020-03-04 16:26:41 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
IFCALLRET(instance->ReceiveChannelData, rc, instance, channelId, Stream_Pointer(s), chunkLength,
|
|
|
|
flags, length);
|
2020-03-09 12:58:02 +03:00
|
|
|
if (!rc)
|
2020-03-04 16:26:41 +03:00
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "ReceiveChannelData returned %d", rc);
|
2020-02-20 10:22:19 +03:00
|
|
|
return FALSE;
|
2020-03-04 16:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Stream_SafeSeek(s, chunkLength);
|
2014-02-12 00:42:28 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL freerdp_channel_peer_process(freerdp_peer* client, wStream* s, UINT16 channelId)
|
2014-02-12 00:42:28 +04:00
|
|
|
{
|
|
|
|
UINT32 length;
|
|
|
|
UINT32 flags;
|
2020-02-21 11:17:00 +03:00
|
|
|
size_t chunkLength;
|
2014-02-12 00:42:28 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 8)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, length);
|
|
|
|
Stream_Read_UINT32(s, flags);
|
|
|
|
chunkLength = Stream_GetRemainingLength(s);
|
|
|
|
|
2014-10-11 01:19:38 +04:00
|
|
|
if (client->VirtualChannelRead)
|
|
|
|
{
|
2020-02-21 11:17:00 +03:00
|
|
|
int rc;
|
2014-10-11 01:19:38 +04:00
|
|
|
UINT32 index;
|
|
|
|
BOOL found = FALSE;
|
|
|
|
HANDLE hChannel = 0;
|
|
|
|
rdpContext* context = client->context;
|
|
|
|
rdpMcs* mcs = context->rdp->mcs;
|
|
|
|
rdpMcsChannel* mcsChannel = NULL;
|
|
|
|
|
|
|
|
for (index = 0; index < mcs->channelCount; index++)
|
|
|
|
{
|
|
|
|
mcsChannel = &(mcs->channels[index]);
|
|
|
|
|
|
|
|
if (mcsChannel->ChannelId == channelId)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
hChannel = (HANDLE)mcsChannel->handle;
|
2014-10-11 01:19:38 +04:00
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return FALSE;
|
|
|
|
|
2020-02-21 11:17:00 +03:00
|
|
|
rc = client->VirtualChannelRead(client, hChannel, Stream_Pointer(s), chunkLength);
|
|
|
|
if (rc < 0)
|
|
|
|
return FALSE;
|
2014-10-11 01:19:38 +04:00
|
|
|
}
|
|
|
|
else if (client->ReceiveChannelData)
|
|
|
|
{
|
2020-03-09 12:58:02 +03:00
|
|
|
BOOL rc = client->ReceiveChannelData(client, channelId, Stream_Pointer(s), chunkLength,
|
|
|
|
flags, length);
|
|
|
|
if (!rc)
|
2020-02-21 11:17:00 +03:00
|
|
|
return FALSE;
|
2014-10-11 01:19:38 +04:00
|
|
|
}
|
2020-02-21 11:17:00 +03:00
|
|
|
return Stream_SafeSeek(s, chunkLength);
|
2014-02-12 00:42:28 +04:00
|
|
|
}
|
2014-02-17 05:12:45 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static const WtsApiFunctionTable FreeRDP_WtsApiFunctionTable = {
|
2014-02-17 05:12:45 +04:00
|
|
|
0, /* dwVersion */
|
|
|
|
0, /* dwFlags */
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
FreeRDP_WTSStopRemoteControlSession, /* StopRemoteControlSession */
|
|
|
|
FreeRDP_WTSStartRemoteControlSessionW, /* StartRemoteControlSessionW */
|
|
|
|
FreeRDP_WTSStartRemoteControlSessionA, /* StartRemoteControlSessionA */
|
|
|
|
FreeRDP_WTSConnectSessionW, /* ConnectSessionW */
|
|
|
|
FreeRDP_WTSConnectSessionA, /* ConnectSessionA */
|
|
|
|
FreeRDP_WTSEnumerateServersW, /* EnumerateServersW */
|
|
|
|
FreeRDP_WTSEnumerateServersA, /* EnumerateServersA */
|
|
|
|
FreeRDP_WTSOpenServerW, /* OpenServerW */
|
|
|
|
FreeRDP_WTSOpenServerA, /* OpenServerA */
|
|
|
|
FreeRDP_WTSOpenServerExW, /* OpenServerExW */
|
|
|
|
FreeRDP_WTSOpenServerExA, /* OpenServerExA */
|
|
|
|
FreeRDP_WTSCloseServer, /* CloseServer */
|
|
|
|
FreeRDP_WTSEnumerateSessionsW, /* EnumerateSessionsW */
|
|
|
|
FreeRDP_WTSEnumerateSessionsA, /* EnumerateSessionsA */
|
|
|
|
FreeRDP_WTSEnumerateSessionsExW, /* EnumerateSessionsExW */
|
|
|
|
FreeRDP_WTSEnumerateSessionsExA, /* EnumerateSessionsExA */
|
|
|
|
FreeRDP_WTSEnumerateProcessesW, /* EnumerateProcessesW */
|
|
|
|
FreeRDP_WTSEnumerateProcessesA, /* EnumerateProcessesA */
|
|
|
|
FreeRDP_WTSTerminateProcess, /* TerminateProcess */
|
|
|
|
FreeRDP_WTSQuerySessionInformationW, /* QuerySessionInformationW */
|
|
|
|
FreeRDP_WTSQuerySessionInformationA, /* QuerySessionInformationA */
|
|
|
|
FreeRDP_WTSQueryUserConfigW, /* QueryUserConfigW */
|
|
|
|
FreeRDP_WTSQueryUserConfigA, /* QueryUserConfigA */
|
|
|
|
FreeRDP_WTSSetUserConfigW, /* SetUserConfigW */
|
|
|
|
FreeRDP_WTSSetUserConfigA, /* SetUserConfigA */
|
|
|
|
FreeRDP_WTSSendMessageW, /* SendMessageW */
|
|
|
|
FreeRDP_WTSSendMessageA, /* SendMessageA */
|
|
|
|
FreeRDP_WTSDisconnectSession, /* DisconnectSession */
|
|
|
|
FreeRDP_WTSLogoffSession, /* LogoffSession */
|
|
|
|
FreeRDP_WTSShutdownSystem, /* ShutdownSystem */
|
|
|
|
FreeRDP_WTSWaitSystemEvent, /* WaitSystemEvent */
|
|
|
|
FreeRDP_WTSVirtualChannelOpen, /* VirtualChannelOpen */
|
|
|
|
FreeRDP_WTSVirtualChannelOpenEx, /* VirtualChannelOpenEx */
|
|
|
|
FreeRDP_WTSVirtualChannelClose, /* VirtualChannelClose */
|
|
|
|
FreeRDP_WTSVirtualChannelRead, /* VirtualChannelRead */
|
|
|
|
FreeRDP_WTSVirtualChannelWrite, /* VirtualChannelWrite */
|
|
|
|
FreeRDP_WTSVirtualChannelPurgeInput, /* VirtualChannelPurgeInput */
|
|
|
|
FreeRDP_WTSVirtualChannelPurgeOutput, /* VirtualChannelPurgeOutput */
|
|
|
|
FreeRDP_WTSVirtualChannelQuery, /* VirtualChannelQuery */
|
|
|
|
FreeRDP_WTSFreeMemory, /* FreeMemory */
|
|
|
|
FreeRDP_WTSRegisterSessionNotification, /* RegisterSessionNotification */
|
|
|
|
FreeRDP_WTSUnRegisterSessionNotification, /* UnRegisterSessionNotification */
|
|
|
|
FreeRDP_WTSRegisterSessionNotificationEx, /* RegisterSessionNotificationEx */
|
2014-02-17 06:19:25 +04:00
|
|
|
FreeRDP_WTSUnRegisterSessionNotificationEx, /* UnRegisterSessionNotificationEx */
|
2019-11-06 17:24:51 +03:00
|
|
|
FreeRDP_WTSQueryUserToken, /* QueryUserToken */
|
|
|
|
FreeRDP_WTSFreeMemoryExW, /* FreeMemoryExW */
|
|
|
|
FreeRDP_WTSFreeMemoryExA, /* FreeMemoryExA */
|
|
|
|
FreeRDP_WTSEnumerateProcessesExW, /* EnumerateProcessesExW */
|
|
|
|
FreeRDP_WTSEnumerateProcessesExA, /* EnumerateProcessesExA */
|
|
|
|
FreeRDP_WTSEnumerateListenersW, /* EnumerateListenersW */
|
|
|
|
FreeRDP_WTSEnumerateListenersA, /* EnumerateListenersA */
|
|
|
|
FreeRDP_WTSQueryListenerConfigW, /* QueryListenerConfigW */
|
|
|
|
FreeRDP_WTSQueryListenerConfigA, /* QueryListenerConfigA */
|
|
|
|
FreeRDP_WTSCreateListenerW, /* CreateListenerW */
|
|
|
|
FreeRDP_WTSCreateListenerA, /* CreateListenerA */
|
|
|
|
FreeRDP_WTSSetListenerSecurityW, /* SetListenerSecurityW */
|
|
|
|
FreeRDP_WTSSetListenerSecurityA, /* SetListenerSecurityA */
|
|
|
|
FreeRDP_WTSGetListenerSecurityW, /* GetListenerSecurityW */
|
|
|
|
FreeRDP_WTSGetListenerSecurityA, /* GetListenerSecurityA */
|
|
|
|
FreeRDP_WTSEnableChildSessions, /* EnableChildSessions */
|
|
|
|
FreeRDP_WTSIsChildSessionsEnabled, /* IsChildSessionsEnabled */
|
|
|
|
FreeRDP_WTSGetChildSessionId, /* GetChildSessionId */
|
|
|
|
FreeRDP_WTSGetActiveConsoleSessionId, /* GetActiveConsoleSessionId */
|
2015-02-12 12:31:00 +03:00
|
|
|
FreeRDP_WTSLogonUser,
|
2015-02-16 14:16:54 +03:00
|
|
|
FreeRDP_WTSLogoffUser,
|
|
|
|
FreeRDP_WTSStartRemoteControlSessionExW,
|
|
|
|
FreeRDP_WTSStartRemoteControlSessionExA
|
2014-02-17 05:12:45 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
PWtsApiFunctionTable FreeRDP_InitWtsApi(void)
|
|
|
|
{
|
2016-10-04 17:04:20 +03:00
|
|
|
return (PWtsApiFunctionTable)&FreeRDP_WtsApiFunctionTable;
|
2014-02-17 05:12:45 +04:00
|
|
|
}
|