FreeRDP/libfreerdp/core/peer.c

652 lines
15 KiB
C
Raw Normal View History

2011-08-18 12:06:32 +04:00
/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
2011-08-18 12:06:32 +04:00
* RDP Server Peer
*
* Copyright 2011 Vic Lee
*
* 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 "info.h"
#include "certificate.h"
#include <freerdp/log.h>
2012-03-18 21:13:34 +04:00
#include <freerdp/utils/tcp.h>
2011-08-18 12:06:32 +04:00
#include "peer.h"
#define TAG FREERDP_TAG("core.peer")
#ifdef WITH_DEBUG_RDP
extern const char* DATA_PDU_TYPE_STRINGS[80];
#endif
static HANDLE freerdp_peer_virtual_channel_open(freerdp_peer* client, const char* name, UINT32 flags)
{
int length;
UINT32 index;
BOOL joined = FALSE;
rdpMcsChannel* mcsChannel = NULL;
rdpPeerChannel* peerChannel = NULL;
rdpMcs* mcs = client->context->rdp->mcs;
if (flags & WTS_CHANNEL_OPTION_DYNAMIC)
return NULL; /* not yet supported */
length = strlen(name);
if (length > 8)
return NULL; /* SVC maximum name length is 8 */
for (index = 0; index < mcs->channelCount; index++)
{
mcsChannel = &(mcs->channels[index]);
if (!mcsChannel->joined)
continue;
if (strncmp(name, mcsChannel->Name, length) == 0)
{
joined = TRUE;
break;
}
}
if (!joined)
return NULL; /* channel is not joined */
peerChannel = (rdpPeerChannel*) mcsChannel->handle;
if (peerChannel)
{
/* channel is already open */
return (HANDLE) peerChannel;
}
peerChannel = (rdpPeerChannel*) calloc(1, sizeof(rdpPeerChannel));
if (peerChannel)
{
peerChannel->index = index;
peerChannel->client = client;
peerChannel->channelFlags = flags;
peerChannel->channelId = mcsChannel->ChannelId;
peerChannel->mcsChannel = mcsChannel;
mcsChannel->handle = (void*) peerChannel;
}
return (HANDLE) peerChannel;
}
static BOOL freerdp_peer_virtual_channel_close(freerdp_peer* client, HANDLE hChannel)
{
rdpMcsChannel* mcsChannel = NULL;
rdpPeerChannel* peerChannel = NULL;
if (!hChannel)
return FALSE;
peerChannel = (rdpPeerChannel*) hChannel;
mcsChannel = peerChannel->mcsChannel;
mcsChannel->handle = NULL;
free(peerChannel);
return TRUE;
}
int freerdp_peer_virtual_channel_read(freerdp_peer* client, HANDLE hChannel, BYTE* buffer, UINT32 length)
{
return 1;
}
static int freerdp_peer_virtual_channel_write(freerdp_peer* client, HANDLE hChannel, BYTE* buffer, UINT32 length)
{
wStream* s;
UINT32 flags;
UINT32 chunkSize;
UINT32 maxChunkSize;
UINT32 totalLength;
rdpRdp* rdp = client->context->rdp;
rdpPeerChannel* peerChannel = (rdpPeerChannel*) hChannel;
rdpMcsChannel* mcsChannel = peerChannel->mcsChannel;
if (!hChannel)
return -1;
if (peerChannel->channelFlags & WTS_CHANNEL_OPTION_DYNAMIC)
return -1; /* not yet supported */
maxChunkSize = rdp->settings->VirtualChannelChunkSize;
totalLength = length;
flags = CHANNEL_FLAG_FIRST;
while (length > 0)
{
s = rdp_send_stream_init(rdp);
if (length > maxChunkSize)
{
chunkSize = rdp->settings->VirtualChannelChunkSize;
}
else
{
chunkSize = length;
flags |= CHANNEL_FLAG_LAST;
}
if (mcsChannel->options & CHANNEL_OPTION_SHOW_PROTOCOL)
flags |= CHANNEL_FLAG_SHOW_PROTOCOL;
Stream_Write_UINT32(s, totalLength);
Stream_Write_UINT32(s, flags);
Stream_EnsureRemainingCapacity(s, chunkSize);
Stream_Write(s, buffer, chunkSize);
rdp_send(rdp, s, peerChannel->channelId);
buffer += chunkSize;
length -= chunkSize;
flags = 0;
}
return 1;
}
static BOOL freerdp_peer_initialize(freerdp_peer* client)
2011-08-18 12:06:32 +04:00
{
rdpRdp* rdp = client->context->rdp;
rdpSettings* settings = rdp->settings;
2012-03-18 21:13:34 +04:00
settings->ServerMode = TRUE;
settings->FrameAcknowledge = 0;
settings->LocalConnection = client->local;
rdp->state = CONNECTION_STATE_INITIAL;
if (settings->RdpKeyFile)
2012-03-18 21:13:34 +04:00
{
settings->RdpServerRsaKey = key_new(settings->RdpKeyFile);
if (!settings->RdpServerRsaKey)
{
WLog_ERR(TAG, "inavlid RDP key file %s", settings->RdpKeyFile);
return FALSE;
}
if (settings->RdpServerRsaKey->ModulusLength > 256)
{
WLog_ERR(TAG, "Key sizes > 2048 are currently not supported for RDP security.");
WLog_ERR(TAG, "Set a different key file than %s", settings->RdpKeyFile);
exit(1);
}
}
return TRUE;
2011-08-18 12:06:32 +04:00
}
static BOOL freerdp_peer_get_fds(freerdp_peer* client, void** rfds, int* rcount)
2011-08-18 12:06:32 +04:00
{
rfds[*rcount] = (void*)(long)(client->context->rdp->transport->TcpIn->sockfd);
2011-08-18 12:06:32 +04:00
(*rcount)++;
return TRUE;
2011-08-18 12:06:32 +04:00
}
static HANDLE freerdp_peer_get_event_handle(freerdp_peer* client)
{
return client->context->rdp->transport->TcpIn->event;
}
static BOOL freerdp_peer_check_fds(freerdp_peer* peer)
2011-08-18 12:06:32 +04:00
{
int status;
2012-03-18 21:13:34 +04:00
rdpRdp* rdp;
rdp = peer->context->rdp;
status = rdp_check_fds(rdp);
2012-03-18 21:13:34 +04:00
if (status < 0)
return FALSE;
return TRUE;
2011-08-18 12:06:32 +04:00
}
static BOOL peer_recv_data_pdu(freerdp_peer* client, wStream* s)
2011-08-21 18:52:37 +04:00
{
BYTE type;
UINT16 length;
2012-10-09 11:26:39 +04:00
UINT32 share_id;
BYTE compressed_type;
UINT16 compressed_len;
2011-08-21 18:52:37 +04:00
if (!rdp_read_share_data_header(s, &length, &type, &share_id, &compressed_type, &compressed_len))
return FALSE;
2011-08-21 18:52:37 +04:00
#ifdef WITH_DEBUG_RDP
WLog_DBG(TAG, "recv %s Data PDU (0x%02X), length: %d",
type < ARRAYSIZE(DATA_PDU_TYPE_STRINGS) ? DATA_PDU_TYPE_STRINGS[type] : "???", type, length);
#endif
2011-08-21 18:52:37 +04:00
switch (type)
{
case DATA_PDU_TYPE_SYNCHRONIZE:
if (!rdp_recv_client_synchronize_pdu(client->context->rdp, s))
return FALSE;
2011-08-21 18:52:37 +04:00
break;
case DATA_PDU_TYPE_CONTROL:
if (!rdp_server_accept_client_control_pdu(client->context->rdp, s))
return FALSE;
break;
case DATA_PDU_TYPE_INPUT:
if (!input_recv(client->context->rdp->input, s))
return FALSE;
break;
case DATA_PDU_TYPE_BITMAP_CACHE_PERSISTENT_LIST:
/* TODO: notify server bitmap cache data */
break;
case DATA_PDU_TYPE_FONT_LIST:
2012-03-18 21:13:34 +04:00
if (!rdp_server_accept_client_font_list_pdu(client->context->rdp, s))
return FALSE;
2012-03-18 21:13:34 +04:00
2011-08-21 18:52:37 +04:00
break;
case DATA_PDU_TYPE_SHUTDOWN_REQUEST:
mcs_send_disconnect_provider_ultimatum(client->context->rdp->mcs);
return FALSE;
case DATA_PDU_TYPE_FRAME_ACKNOWLEDGE:
if (Stream_GetRemainingLength(s) < 4)
return FALSE;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(s, client->ack_frame_id);
2013-06-02 00:57:34 +04:00
IFCALL(client->update->SurfaceFrameAcknowledge, client->update->context, client->ack_frame_id);
break;
case DATA_PDU_TYPE_REFRESH_RECT:
if (!update_read_refresh_rect(client->update, s))
return FALSE;
break;
case DATA_PDU_TYPE_SUPPRESS_OUTPUT:
if (!update_read_suppress_output(client->update, s))
return FALSE;
break;
2011-08-21 18:52:37 +04:00
default:
WLog_ERR(TAG, "Data PDU type %d", type);
2011-08-21 18:52:37 +04:00
break;
}
return TRUE;
2011-08-21 18:52:37 +04:00
}
static int peer_recv_tpkt_pdu(freerdp_peer* client, wStream* s)
2011-08-21 18:52:37 +04:00
{
2012-03-18 21:13:34 +04:00
rdpRdp* rdp;
UINT16 length;
UINT16 pduType;
UINT16 pduLength;
UINT16 pduSource;
UINT16 channelId;
UINT16 securityFlags;
2011-08-21 18:52:37 +04:00
2012-01-25 19:30:54 +04:00
rdp = client->context->rdp;
if (!rdp_read_header(rdp, s, &length, &channelId))
2011-08-21 18:52:37 +04:00
{
WLog_ERR(TAG, "Incorrect RDP header.");
return -1;
2011-08-21 18:52:37 +04:00
}
if (rdp->disconnect)
return 0;
if (rdp->settings->DisableEncryption)
2012-01-25 19:30:54 +04:00
{
if (!rdp_read_security_header(s, &securityFlags))
return -1;
2012-03-18 21:13:34 +04:00
2012-01-25 19:30:54 +04:00
if (securityFlags & SEC_ENCRYPT)
{
if (!rdp_decrypt(rdp, s, length - 4, securityFlags))
{
WLog_ERR(TAG, "rdp_decrypt failed");
return -1;
2012-01-25 19:30:54 +04:00
}
}
}
2011-08-21 18:52:37 +04:00
if (channelId != MCS_GLOBAL_CHANNEL_ID)
{
if (!freerdp_channel_peer_process(client, s, channelId))
return -1;
2011-08-21 18:52:37 +04:00
}
else
{
if (!rdp_read_share_control_header(s, &pduLength, &pduType, &pduSource))
return -1;
2011-08-21 18:52:37 +04:00
client->settings->PduSource = pduSource;
2011-08-21 18:52:37 +04:00
switch (pduType)
{
case PDU_TYPE_DATA:
if (!peer_recv_data_pdu(client, s))
return -1;
2011-08-21 18:52:37 +04:00
break;
case PDU_TYPE_CONFIRM_ACTIVE:
if (!rdp_server_accept_confirm_active(rdp, s))
return -1;
break;
2011-08-21 18:52:37 +04:00
default:
WLog_ERR(TAG, "Client sent pduType %d", pduType);
return -1;
2011-08-21 18:52:37 +04:00
}
}
return 0;
2011-08-21 18:52:37 +04:00
}
static int peer_recv_fastpath_pdu(freerdp_peer* client, wStream* s)
2011-08-21 18:52:37 +04:00
{
2011-09-27 09:30:58 +04:00
rdpRdp* rdp;
UINT16 length;
2011-09-27 09:30:58 +04:00
rdpFastPath* fastpath;
rdp = client->context->rdp;
2011-09-27 09:30:58 +04:00
fastpath = rdp->fastpath;
fastpath_read_header_rdp(fastpath, s, &length);
2011-08-23 11:51:51 +04:00
if ((length == 0) || (length > Stream_GetRemainingLength(s)))
2011-08-23 11:51:51 +04:00
{
WLog_ERR(TAG, "incorrect FastPath PDU header length %d", length);
return -1;
2011-08-23 11:51:51 +04:00
}
2011-09-27 09:30:58 +04:00
if (fastpath->encryptionFlags & FASTPATH_OUTPUT_ENCRYPTED)
2011-09-15 01:14:50 +04:00
{
if (!rdp_decrypt(rdp, s, length, (fastpath->encryptionFlags & FASTPATH_OUTPUT_SECURE_CHECKSUM) ? SEC_SECURE_CHECKSUM : 0))
return -1;
2011-09-15 01:14:50 +04:00
}
2011-09-27 09:30:58 +04:00
return fastpath_recv_inputs(fastpath, s);
2011-08-21 18:52:37 +04:00
}
static int peer_recv_pdu(freerdp_peer* client, wStream* s)
2011-08-21 18:52:37 +04:00
{
if (tpkt_verify_header(s))
return peer_recv_tpkt_pdu(client, s);
2011-08-21 18:52:37 +04:00
else
return peer_recv_fastpath_pdu(client, s);
2011-08-21 18:52:37 +04:00
}
static int peer_recv_callback(rdpTransport* transport, wStream* s, void* extra)
{
freerdp_peer* client = (freerdp_peer*) extra;
rdpRdp* rdp = client->context->rdp;
switch (rdp->state)
{
case CONNECTION_STATE_INITIAL:
if (!rdp_server_accept_nego(rdp, s))
return -1;
if (rdp->nego->selected_protocol & PROTOCOL_NLA)
{
sspi_CopyAuthIdentity(&client->identity, &(rdp->nego->transport->credssp->identity));
IFCALLRET(client->Logon, client->authenticated, client, &client->identity, TRUE);
credssp_free(rdp->nego->transport->credssp);
rdp->nego->transport->credssp = NULL;
}
else
{
IFCALLRET(client->Logon, client->authenticated, client, &client->identity, FALSE);
}
break;
case CONNECTION_STATE_NEGO:
if (!rdp_server_accept_mcs_connect_initial(rdp, s))
return -1;
break;
case CONNECTION_STATE_MCS_CONNECT:
if (!rdp_server_accept_mcs_erect_domain_request(rdp, s))
return -1;
break;
case CONNECTION_STATE_MCS_ERECT_DOMAIN:
if (!rdp_server_accept_mcs_attach_user_request(rdp, s))
return -1;
break;
case CONNECTION_STATE_MCS_ATTACH_USER:
if (!rdp_server_accept_mcs_channel_join_request(rdp, s))
return -1;
2011-08-20 14:22:14 +04:00
break;
case CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT:
if (rdp->settings->DisableEncryption)
{
if (!rdp_server_establish_keys(rdp, s))
return -1;
}
rdp_server_transition_to_state(rdp, CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE);
if (Stream_GetRemainingLength(s) > 0)
return peer_recv_callback(transport, s, extra);
break;
2012-03-18 21:13:34 +04:00
case CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE:
if (!rdp_recv_client_info(rdp, s))
return -1;
rdp_server_transition_to_state(rdp, CONNECTION_STATE_LICENSING);
return peer_recv_callback(transport, NULL, extra);
2011-08-21 11:52:44 +04:00
break;
case CONNECTION_STATE_LICENSING:
if (!license_send_valid_client_error_packet(rdp->license))
return FALSE;
rdp_server_transition_to_state(rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE);
return peer_recv_callback(transport, NULL, extra);
break;
case CONNECTION_STATE_CAPABILITIES_EXCHANGE:
if (!rdp->AwaitCapabilities)
{
IFCALL(client->Capabilities, client);
if (!rdp_send_demand_active(rdp))
return -1;
rdp->AwaitCapabilities = TRUE;
if (s)
{
if (peer_recv_pdu(client, s) < 0)
return -1;
}
}
else
{
/**
* During reactivation sequence the client might sent some input or channel data
* before receiving the Deactivate All PDU. We need to process them as usual.
*/
if (peer_recv_pdu(client, s) < 0)
return -1;
}
break;
case CONNECTION_STATE_FINALIZATION:
if (peer_recv_pdu(client, s) < 0)
return -1;
2011-08-21 18:52:37 +04:00
break;
case CONNECTION_STATE_ACTIVE:
if (peer_recv_pdu(client, s) < 0)
return -1;
break;
default:
WLog_ERR(TAG, "Invalid state %d", rdp->state);
return -1;
}
return 0;
}
static BOOL freerdp_peer_close(freerdp_peer* client)
{
/**
* [MS-RDPBCGR] 1.3.1.4.2 User-Initiated Disconnection Sequence on Server
* The server first sends the client a Deactivate All PDU followed by an
* optional MCS Disconnect Provider Ultimatum PDU.
*/
if (!rdp_send_deactivate_all(client->context->rdp))
return FALSE;
return mcs_send_disconnect_provider_ultimatum(client->context->rdp->mcs);
}
2011-08-18 12:06:32 +04:00
static void freerdp_peer_disconnect(freerdp_peer* client)
{
transport_disconnect(client->context->rdp->transport);
}
static int freerdp_peer_send_channel_data(freerdp_peer* client, UINT16 channelId, BYTE* data, int size)
{
return rdp_send_channel_data(client->context->rdp, channelId, data, size);
}
static BOOL freerdp_peer_is_write_blocked(freerdp_peer* peer)
{
return tranport_is_write_blocked(peer->context->rdp->transport);
}
static int freerdp_peer_drain_output_buffer(freerdp_peer* peer)
{
rdpTransport* transport = peer->context->rdp->transport;
return tranport_drain_output_buffer(transport);
}
void freerdp_peer_context_new(freerdp_peer* client)
{
rdpRdp* rdp;
client->context = (rdpContext*) calloc(1, client->ContextSize);
client->context->ServerMode = TRUE;
client->context->metrics = metrics_new(client->context);
rdp = rdp_new(client->context);
client->input = rdp->input;
client->update = rdp->update;
client->settings = rdp->settings;
client->context->rdp = rdp;
client->context->peer = client;
client->context->input = client->input;
client->context->update = client->update;
client->context->settings = client->settings;
client->update->context = client->context;
client->input->context = client->context;
update_register_server_callbacks(client->update);
transport_attach(rdp->transport, client->sockfd);
2011-08-23 12:14:32 +04:00
2012-12-22 00:49:02 +04:00
rdp->transport->ReceiveCallback = peer_recv_callback;
rdp->transport->ReceiveExtra = client;
transport_set_blocking_mode(rdp->transport, FALSE);
client->IsWriteBlocked = freerdp_peer_is_write_blocked;
client->DrainOutputBuffer = freerdp_peer_drain_output_buffer;
IFCALL(client->ContextNew, client, client->context);
}
void freerdp_peer_context_free(freerdp_peer* client)
{
IFCALL(client->ContextFree, client, client->context);
2014-06-03 16:54:56 +04:00
metrics_free(client->context->metrics);
2011-08-18 12:06:32 +04:00
}
freerdp_peer* freerdp_peer_new(int sockfd)
{
freerdp_peer* client;
client = (freerdp_peer*) calloc(1, sizeof(freerdp_peer));
2011-08-18 12:06:32 +04:00
freerdp_tcp_set_no_delay(sockfd, TRUE);
2012-03-18 21:13:34 +04:00
if (client)
2011-10-30 09:43:04 +04:00
{
client->sockfd = sockfd;
2013-06-14 05:34:46 +04:00
client->ContextSize = sizeof(rdpContext);
2011-10-30 09:43:04 +04:00
client->Initialize = freerdp_peer_initialize;
client->GetFileDescriptor = freerdp_peer_get_fds;
client->GetEventHandle = freerdp_peer_get_event_handle;
2011-10-30 09:43:04 +04:00
client->CheckFileDescriptor = freerdp_peer_check_fds;
client->Close = freerdp_peer_close;
2011-10-30 09:43:04 +04:00
client->Disconnect = freerdp_peer_disconnect;
client->SendChannelData = freerdp_peer_send_channel_data;
client->IsWriteBlocked = freerdp_peer_is_write_blocked;
client->DrainOutputBuffer = freerdp_peer_drain_output_buffer;
client->VirtualChannelOpen = freerdp_peer_virtual_channel_open;
client->VirtualChannelClose = freerdp_peer_virtual_channel_close;
client->VirtualChannelWrite = freerdp_peer_virtual_channel_write;
2011-10-30 09:43:04 +04:00
}
2011-08-18 12:06:32 +04:00
return client;
}
void freerdp_peer_free(freerdp_peer* client)
{
if (!client)
return;
rdp_free(client->context->rdp);
free(client->context);
free(client);
2011-08-18 12:06:32 +04:00
}