2019-05-17 15:32:54 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* FreeRDP Proxy Server
|
|
|
|
*
|
|
|
|
* Copyright 2019 Mati Shabtay <matishabtay@gmail.com>
|
|
|
|
* Copyright 2019 Kobi Mizrachi <kmizrachi18@gmail.com>
|
|
|
|
* Copyright 2019 Idan Freiberg <speidy@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 <freerdp/freerdp.h>
|
|
|
|
#include <freerdp/gdi/gdi.h>
|
|
|
|
#include <freerdp/client/cmdline.h>
|
|
|
|
|
|
|
|
#include "pf_channels.h"
|
|
|
|
#include "pf_gdi.h"
|
|
|
|
#include "pf_graphics.h"
|
|
|
|
#include "pf_client.h"
|
|
|
|
#include "pf_context.h"
|
2019-05-29 10:00:52 +03:00
|
|
|
#include "pf_update.h"
|
2019-05-17 15:32:54 +03:00
|
|
|
#include "pf_log.h"
|
2019-08-19 16:06:42 +03:00
|
|
|
#include "pf_modules.h"
|
2019-10-22 16:06:05 +03:00
|
|
|
#include "pf_capture.h"
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
#define TAG PROXY_TAG("client")
|
|
|
|
|
2020-02-02 16:15:28 +03:00
|
|
|
static pReceiveChannelData client_receive_channel_data_original = NULL;
|
|
|
|
|
2019-09-08 15:18:45 +03:00
|
|
|
static BOOL proxy_server_reactivate(rdpContext* ps, const rdpContext* pc)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-09-08 15:18:45 +03:00
|
|
|
if (!pf_context_copy_settings(ps->settings, pc->settings))
|
|
|
|
return FALSE;
|
2019-07-16 13:28:14 +03:00
|
|
|
|
2019-09-08 18:10:50 +03:00
|
|
|
/*
|
|
|
|
* DesktopResize causes internal function rdp_server_reactivate to be called,
|
2019-05-17 15:32:54 +03:00
|
|
|
* which causes the reactivation.
|
|
|
|
*/
|
2019-09-08 15:18:45 +03:00
|
|
|
if (!ps->update->DesktopResize(ps))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-12-26 11:25:13 +03:00
|
|
|
static void pf_client_on_error_info(void* ctx, ErrorInfoEventArgs* e)
|
2019-05-19 14:35:06 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
pClientContext* pc = (pClientContext*)ctx;
|
2019-09-08 18:10:50 +03:00
|
|
|
pServerContext* ps = pc->pdata->ps;
|
2019-05-19 14:35:06 +03:00
|
|
|
|
2019-09-08 18:10:50 +03:00
|
|
|
if (e->code == ERRINFO_NONE)
|
|
|
|
return;
|
|
|
|
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_WARN(TAG, pc, "received ErrorInfo PDU. code=0x%08" PRIu32 ", message: %s", e->code,
|
|
|
|
freerdp_get_error_info_string(e->code));
|
2019-09-08 18:10:50 +03:00
|
|
|
|
|
|
|
/* forward error back to client */
|
|
|
|
freerdp_set_error_info(ps->context.rdp, e->code);
|
|
|
|
freerdp_send_error_info(ps->context.rdp);
|
2019-05-19 14:35:06 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 13:53:36 +03:00
|
|
|
static BOOL pf_client_load_rdpsnd(pClientContext* pc, proxyConfig* config)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpContext* context = (rdpContext*)pc;
|
2019-08-13 13:53:36 +03:00
|
|
|
pServerContext* ps = pc->pdata->ps;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if AudioOutput is enabled in proxy and client connected with rdpsnd, use proxy as rdpsnd
|
|
|
|
* backend. Otherwise, use sys:fake.
|
|
|
|
*/
|
|
|
|
if (!freerdp_static_channel_collection_find(context->settings, "rdpsnd"))
|
|
|
|
{
|
|
|
|
char* params[2];
|
|
|
|
params[0] = "rdpsnd";
|
|
|
|
|
|
|
|
if (config->AudioOutput && WTSVirtualChannelManagerIsChannelJoined(ps->vcm, "rdpsnd"))
|
|
|
|
params[1] = "sys:proxy";
|
|
|
|
else
|
|
|
|
params[1] = "sys:fake";
|
2019-11-06 17:24:51 +03:00
|
|
|
|
|
|
|
if (!freerdp_client_add_static_channel(context->settings, 2, (char**)params))
|
2019-08-13 13:53:36 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
/**
|
|
|
|
* Called before a connection is established.
|
|
|
|
*
|
|
|
|
* TODO: Take client to proxy settings and use channel whitelist to filter out
|
|
|
|
* unwanted channels.
|
|
|
|
*/
|
|
|
|
static BOOL pf_client_pre_connect(freerdp* instance)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
pClientContext* pc = (pClientContext*)instance->context;
|
2019-08-06 11:49:04 +03:00
|
|
|
pServerContext* ps = pc->pdata->ps;
|
2019-08-13 13:53:36 +03:00
|
|
|
proxyConfig* config = ps->pdata->config;
|
2019-05-17 15:32:54 +03:00
|
|
|
rdpSettings* settings = instance->settings;
|
2019-08-06 11:48:26 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* as the client's settings are copied from the server's, GlyphSupportLevel might not be
|
|
|
|
* GLYPH_SUPPORT_NONE. the proxy currently do not support GDI & GLYPH_SUPPORT_CACHE, so
|
|
|
|
* GlyphCacheSupport must be explicitly set to GLYPH_SUPPORT_NONE.
|
2019-09-08 18:10:50 +03:00
|
|
|
*
|
|
|
|
* Also, OrderSupport need to be zeroed, because it is currently not supported.
|
2019-08-06 11:48:26 +03:00
|
|
|
*/
|
|
|
|
settings->GlyphSupportLevel = GLYPH_SUPPORT_NONE;
|
2019-09-08 18:10:50 +03:00
|
|
|
ZeroMemory(instance->settings->OrderSupport, 32);
|
2019-08-13 15:06:45 +03:00
|
|
|
|
2019-09-08 18:10:50 +03:00
|
|
|
settings->SupportDynamicChannels = TRUE;
|
2019-06-13 16:55:56 +03:00
|
|
|
|
2019-09-08 18:10:50 +03:00
|
|
|
/* Multimon */
|
|
|
|
settings->UseMultimon = TRUE;
|
|
|
|
|
|
|
|
/* Sound */
|
|
|
|
settings->AudioPlayback = FALSE;
|
|
|
|
settings->DeviceRedirection = TRUE;
|
|
|
|
|
|
|
|
/* Display control */
|
|
|
|
settings->SupportDisplayControl = config->DisplayControl;
|
|
|
|
settings->DynamicResolutionUpdate = config->DisplayControl;
|
2019-06-04 17:33:48 +03:00
|
|
|
|
2019-09-25 16:09:15 +03:00
|
|
|
settings->AutoReconnectionEnabled = TRUE;
|
2019-05-17 15:32:54 +03:00
|
|
|
/**
|
|
|
|
* Register the channel listeners.
|
|
|
|
* They are required to set up / tear down channels if they are loaded.
|
|
|
|
*/
|
2019-12-26 11:25:13 +03:00
|
|
|
PubSub_SubscribeChannelConnected(instance->context->pubSub,
|
|
|
|
pf_channels_on_client_channel_connect);
|
2019-05-17 15:32:54 +03:00
|
|
|
PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
|
2019-12-26 11:25:13 +03:00
|
|
|
pf_channels_on_client_channel_disconnect);
|
|
|
|
PubSub_SubscribeErrorInfo(instance->context->pubSub, pf_client_on_error_info);
|
2019-08-06 11:49:04 +03:00
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
/**
|
|
|
|
* Load all required plugins / channels / libraries specified by current
|
|
|
|
* settings.
|
|
|
|
*/
|
2020-01-28 16:36:21 +03:00
|
|
|
LOG_INFO(TAG, pc, "Loading addins");
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2020-02-02 16:15:28 +03:00
|
|
|
{
|
|
|
|
/* add passthrough channels to channel def array */
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (settings->ChannelCount + config->PassthroughCount >= settings->ChannelDefArraySize)
|
|
|
|
{
|
|
|
|
LOG_ERR(TAG, pc, "too many channels");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < config->PassthroughCount; i++)
|
|
|
|
{
|
|
|
|
const char* channel_name = config->Passthrough[i];
|
|
|
|
CHANNEL_DEF channel = { 0 };
|
|
|
|
|
|
|
|
/* only connect connect this channel if already joined in peer connection */
|
|
|
|
if (!WTSVirtualChannelManagerIsChannelJoined(ps->vcm, channel_name))
|
|
|
|
{
|
|
|
|
LOG_INFO(TAG, ps, "client did not connected with channel %s, skipping passthrough",
|
|
|
|
channel_name);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.options = CHANNEL_OPTION_INITIALIZED; /* TODO: Export to config. */
|
|
|
|
strncpy(channel.name, channel_name, CHANNEL_NAME_LEN);
|
|
|
|
|
|
|
|
settings->ChannelDefArray[settings->ChannelCount++] = channel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:53:36 +03:00
|
|
|
if (!pf_client_load_rdpsnd(pc, config))
|
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, pc, "Failed to load rdpsnd client");
|
2019-08-13 13:53:36 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!freerdp_client_load_addins(instance->context->channels, instance->settings))
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2020-01-28 16:36:21 +03:00
|
|
|
LOG_ERR(TAG, pc, "Failed to load addins");
|
2019-05-17 15:32:54 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-03-09 12:58:02 +03:00
|
|
|
static BOOL pf_client_receive_channel_data_hook(freerdp* instance, UINT16 channelId,
|
|
|
|
const BYTE* data, size_t size, UINT32 flags,
|
|
|
|
size_t totalSize)
|
2020-02-02 16:15:28 +03:00
|
|
|
{
|
|
|
|
pClientContext* pc = (pClientContext*)instance->context;
|
|
|
|
pServerContext* ps = pc->pdata->ps;
|
|
|
|
proxyData* pdata = ps->pdata;
|
|
|
|
proxyConfig* config = pdata->config;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
const char* channel_name = freerdp_channels_get_name_by_id(instance, channelId);
|
|
|
|
|
|
|
|
for (i = 0; i < config->PassthroughCount; i++)
|
|
|
|
{
|
|
|
|
if (strncmp(channel_name, config->Passthrough[i], CHANNEL_NAME_LEN) == 0)
|
|
|
|
{
|
|
|
|
proxyChannelDataEventInfo ev;
|
|
|
|
UINT64 server_channel_id;
|
|
|
|
|
|
|
|
ev.channel_id = channelId;
|
|
|
|
ev.channel_name = channel_name;
|
|
|
|
ev.data = (const BYTE*)data;
|
|
|
|
ev.data_len = size;
|
|
|
|
|
|
|
|
if (!pf_modules_run_filter(FILTER_TYPE_CLIENT_PASSTHROUGH_CHANNEL_DATA, pdata, &ev))
|
2020-03-09 12:58:02 +03:00
|
|
|
return FALSE;
|
2020-02-02 16:15:28 +03:00
|
|
|
|
|
|
|
server_channel_id = (UINT64)HashTable_GetItemValue(ps->vc_ids, (void*)channel_name);
|
2020-03-09 12:58:02 +03:00
|
|
|
return ps->context.peer->SendChannelData(ps->context.peer, (UINT16)server_channel_id,
|
|
|
|
data, size);
|
2020-02-02 16:15:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return client_receive_channel_data_original(instance, channelId, data, size, flags, totalSize);
|
|
|
|
}
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
/**
|
|
|
|
* Called after a RDP connection was successfully established.
|
2019-06-13 16:55:56 +03:00
|
|
|
* Settings might have changed during negotiation of client / server feature
|
2019-05-17 15:32:54 +03:00
|
|
|
* support.
|
|
|
|
*
|
|
|
|
* Set up local framebuffers and painting callbacks.
|
|
|
|
* If required, register pointer callbacks to change the local mouse cursor
|
|
|
|
* when hovering over the RDP window
|
|
|
|
*/
|
|
|
|
static BOOL pf_client_post_connect(freerdp* instance)
|
|
|
|
{
|
|
|
|
rdpContext* context;
|
|
|
|
rdpSettings* settings;
|
|
|
|
rdpUpdate* update;
|
|
|
|
rdpContext* ps;
|
2019-10-22 16:06:05 +03:00
|
|
|
pClientContext* pc;
|
|
|
|
proxyConfig* config;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
context = instance->context;
|
|
|
|
settings = instance->settings;
|
|
|
|
update = instance->update;
|
2019-11-06 17:24:51 +03:00
|
|
|
pc = (pClientContext*)context;
|
|
|
|
ps = (rdpContext*)pc->pdata->ps;
|
2019-10-22 16:06:05 +03:00
|
|
|
config = pc->pdata->config;
|
|
|
|
|
|
|
|
if (config->SessionCapture)
|
|
|
|
{
|
|
|
|
if (!pf_capture_create_session_directory(pc))
|
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, pc, "pf_capture_create_session_directory failed!");
|
2019-10-22 16:06:05 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, pc, "frames dir created: %s", pc->frames_dir);
|
2019-10-22 16:06:05 +03:00
|
|
|
}
|
2019-06-11 18:51:21 +03:00
|
|
|
|
2019-10-22 16:01:00 +03:00
|
|
|
if (!gdi_init(instance, PIXEL_FORMAT_BGRA32))
|
2019-06-11 18:51:21 +03:00
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
if (!pf_register_pointer(context->graphics))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!settings->SoftwareGdi)
|
|
|
|
{
|
|
|
|
if (!pf_register_graphics(context->graphics))
|
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, pc, "failed to register graphics");
|
2019-05-17 15:32:54 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
pf_gdi_register_update_callbacks(update);
|
2019-05-29 10:00:52 +03:00
|
|
|
brush_cache_register_callbacks(update);
|
|
|
|
glyph_cache_register_callbacks(update);
|
|
|
|
bitmap_cache_register_callbacks(update);
|
|
|
|
offscreen_cache_register_callbacks(update);
|
|
|
|
palette_cache_register_callbacks(update);
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2019-05-29 10:00:52 +03:00
|
|
|
pf_client_register_update_callbacks(update);
|
2019-09-08 18:10:50 +03:00
|
|
|
|
2020-02-02 16:15:28 +03:00
|
|
|
/* virtual channels receive data hook */
|
|
|
|
client_receive_channel_data_original = instance->ReceiveChannelData;
|
|
|
|
instance->ReceiveChannelData = pf_client_receive_channel_data_hook;
|
|
|
|
|
|
|
|
/* populate channel name -> channel ids map */
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < config->PassthroughCount; i++)
|
|
|
|
{
|
|
|
|
char* channel_name = config->Passthrough[i];
|
|
|
|
UINT64 channel_id = (UINT64)freerdp_channels_get_id_by_name(instance, channel_name);
|
|
|
|
HashTable_Add(pc->vc_ids, (void*)channel_name, (void*)channel_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 18:10:50 +03:00
|
|
|
/*
|
2020-02-02 16:15:28 +03:00
|
|
|
* after the connection fully established and settings were negotiated with target server,
|
|
|
|
* send a reactivation sequence to the client with the negotiated settings. This way,
|
|
|
|
* settings are synchorinized between proxy's peer and and remote target.
|
2019-09-08 18:10:50 +03:00
|
|
|
*/
|
2019-09-08 15:18:45 +03:00
|
|
|
return proxy_server_reactivate(ps, context);
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function is called whether a session ends by failure or success.
|
|
|
|
* Clean up everything allocated by pre_connect and post_connect.
|
|
|
|
*/
|
|
|
|
static void pf_client_post_disconnect(freerdp* instance)
|
|
|
|
{
|
|
|
|
pClientContext* context;
|
|
|
|
proxyData* pdata;
|
|
|
|
|
|
|
|
if (!instance)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!instance->context)
|
|
|
|
return;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
context = (pClientContext*)instance->context;
|
2019-05-17 15:32:54 +03:00
|
|
|
pdata = context->pdata;
|
2019-05-19 14:35:06 +03:00
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
|
2019-12-26 11:25:13 +03:00
|
|
|
pf_channels_on_client_channel_connect);
|
2019-05-17 15:32:54 +03:00
|
|
|
PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
|
2019-12-26 11:25:13 +03:00
|
|
|
pf_channels_on_client_channel_disconnect);
|
|
|
|
PubSub_UnsubscribeErrorInfo(instance->context->pubSub, pf_client_on_error_info);
|
2019-05-17 15:32:54 +03:00
|
|
|
gdi_free(instance);
|
|
|
|
|
2019-08-01 17:01:11 +03:00
|
|
|
/* Only close the connection if NLA fallback process is done */
|
2019-09-10 10:11:00 +03:00
|
|
|
if (!context->allow_next_conn_failure)
|
2019-07-16 16:13:12 +03:00
|
|
|
proxy_data_abort_connect(pdata);
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 16:08:25 +03:00
|
|
|
/*
|
|
|
|
* pf_client_should_retry_without_nla:
|
|
|
|
*
|
|
|
|
* returns TRUE if in case of connection failure, the client should try again without NLA.
|
|
|
|
* Otherwise, returns FALSE.
|
|
|
|
*/
|
|
|
|
static BOOL pf_client_should_retry_without_nla(pClientContext* pc)
|
|
|
|
{
|
|
|
|
rdpSettings* settings = pc->context.settings;
|
|
|
|
proxyConfig* config = pc->pdata->config;
|
|
|
|
|
2019-12-29 11:55:40 +03:00
|
|
|
if (!config->ClientAllowFallbackToTls || !settings->NlaSecurity)
|
2019-09-18 16:08:25 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return config->ClientTlsSecurity || config->ClientRdpSecurity;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pf_client_set_security_settings(pClientContext* pc)
|
|
|
|
{
|
|
|
|
rdpSettings* settings = pc->context.settings;
|
|
|
|
proxyConfig* config = pc->pdata->config;
|
|
|
|
|
|
|
|
settings->RdpSecurity = config->ClientRdpSecurity;
|
|
|
|
settings->TlsSecurity = config->ClientTlsSecurity;
|
|
|
|
settings->NlaSecurity = FALSE;
|
|
|
|
|
|
|
|
if (!config->ClientNlaSecurity)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!settings->Username || !settings->Password)
|
|
|
|
return;
|
|
|
|
|
|
|
|
settings->NlaSecurity = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL pf_client_connect_without_nla(pClientContext* pc)
|
|
|
|
{
|
|
|
|
freerdp* instance = pc->context.instance;
|
|
|
|
rdpSettings* settings = pc->context.settings;
|
|
|
|
|
|
|
|
/* disable NLA */
|
|
|
|
settings->NlaSecurity = FALSE;
|
|
|
|
|
|
|
|
/* do not allow next connection failure */
|
|
|
|
pc->allow_next_conn_failure = FALSE;
|
|
|
|
return freerdp_connect(instance);
|
|
|
|
}
|
|
|
|
|
2019-09-08 18:10:50 +03:00
|
|
|
static BOOL pf_client_connect(freerdp* instance)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
pClientContext* pc = (pClientContext*)instance->context;
|
2020-01-28 16:36:21 +03:00
|
|
|
rdpSettings* settings = instance->settings;
|
2019-09-18 16:08:25 +03:00
|
|
|
BOOL rc = FALSE;
|
2019-12-29 11:55:40 +03:00
|
|
|
BOOL retry = FALSE;
|
2019-09-08 18:10:50 +03:00
|
|
|
|
2020-01-28 16:36:21 +03:00
|
|
|
LOG_INFO(TAG, pc, "connecting using client info: Username: %s, Domain: %s", settings->Username,
|
|
|
|
settings->Domain);
|
|
|
|
|
2019-09-18 16:08:25 +03:00
|
|
|
pf_client_set_security_settings(pc);
|
|
|
|
if (pf_client_should_retry_without_nla(pc))
|
2019-12-29 11:55:40 +03:00
|
|
|
retry = pc->allow_next_conn_failure = TRUE;
|
2019-09-08 18:10:50 +03:00
|
|
|
|
|
|
|
if (!freerdp_connect(instance))
|
|
|
|
{
|
2019-12-29 11:55:40 +03:00
|
|
|
if (!retry)
|
|
|
|
goto out;
|
|
|
|
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, pc, "failed to connect with NLA. retrying to connect without NLA");
|
2019-12-31 12:16:12 +03:00
|
|
|
pf_modules_run_hook(HOOK_TYPE_CLIENT_LOGIN_FAILURE, pc->pdata);
|
2019-09-08 18:10:50 +03:00
|
|
|
|
2019-10-31 12:26:13 +03:00
|
|
|
if (!pf_client_connect_without_nla(pc))
|
2019-09-08 18:10:50 +03:00
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, pc, "pf_client_connect_without_nla failed!");
|
2019-09-18 16:08:25 +03:00
|
|
|
goto out;
|
2019-09-08 18:10:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 16:08:25 +03:00
|
|
|
rc = TRUE;
|
|
|
|
out:
|
2019-09-10 10:11:00 +03:00
|
|
|
pc->allow_next_conn_failure = FALSE;
|
2019-09-18 16:08:25 +03:00
|
|
|
return rc;
|
2019-09-08 18:10:50 +03:00
|
|
|
}
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
/**
|
|
|
|
* RDP main loop.
|
|
|
|
* Connects RDP, loops while running and handles event and dispatch, cleans up
|
|
|
|
* after the connection ends.
|
|
|
|
*/
|
|
|
|
static DWORD WINAPI pf_client_thread_proc(LPVOID arg)
|
|
|
|
{
|
|
|
|
freerdp* instance = (freerdp*)arg;
|
2019-07-15 10:55:57 +03:00
|
|
|
pClientContext* pc = (pClientContext*)instance->context;
|
2019-08-01 17:01:11 +03:00
|
|
|
proxyData* pdata = pc->pdata;
|
2019-05-17 15:32:54 +03:00
|
|
|
DWORD nCount;
|
|
|
|
DWORD status;
|
2019-08-01 17:01:11 +03:00
|
|
|
HANDLE handles[65];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* during redirection, freerdp's abort event might be overriden (reset) by the library, after
|
|
|
|
* the server set it in order to shutdown the connection. it means that the server might signal
|
|
|
|
* the client to abort, but the library code will override the signal and the client will
|
|
|
|
* continue its work instead of exiting. That's why the client must wait on `pdata->abort_event`
|
|
|
|
* too, which will never be modified by the library.
|
|
|
|
*/
|
|
|
|
handles[64] = pdata->abort_event;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-12-31 12:16:12 +03:00
|
|
|
if (!pf_modules_run_hook(HOOK_TYPE_CLIENT_PRE_CONNECT, pdata))
|
2019-10-22 10:38:25 +03:00
|
|
|
{
|
|
|
|
proxy_data_abort_connect(pdata);
|
2019-09-08 18:10:50 +03:00
|
|
|
return FALSE;
|
2019-10-22 10:38:25 +03:00
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-09-08 18:10:50 +03:00
|
|
|
if (!pf_client_connect(instance))
|
2019-09-18 16:08:25 +03:00
|
|
|
{
|
|
|
|
proxy_data_abort_connect(pdata);
|
2019-09-08 18:10:50 +03:00
|
|
|
return FALSE;
|
2019-09-18 16:08:25 +03:00
|
|
|
}
|
2019-07-15 10:55:57 +03:00
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
while (!freerdp_shall_disconnect(instance))
|
|
|
|
{
|
|
|
|
nCount = freerdp_get_event_handles(instance->context, &handles[0], 64);
|
|
|
|
|
|
|
|
if (nCount == 0)
|
|
|
|
{
|
2019-12-31 12:16:12 +03:00
|
|
|
LOG_ERR(TAG, pc, "freerdp_get_event_handles failed!");
|
2019-05-17 15:32:54 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-01 17:01:11 +03:00
|
|
|
status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "%s: WaitForMultipleObjects failed with %" PRIu32 "", __FUNCTION__,
|
2019-05-17 15:32:54 +03:00
|
|
|
status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freerdp_shall_disconnect(instance))
|
|
|
|
break;
|
|
|
|
|
2019-08-01 17:01:11 +03:00
|
|
|
if (proxy_data_shall_disconnect(pdata))
|
|
|
|
break;
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
if (!freerdp_check_event_handles(instance->context))
|
|
|
|
{
|
|
|
|
if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_SUCCESS)
|
|
|
|
WLog_ERR(TAG, "Failed to check FreeRDP event handles");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freerdp_disconnect(instance);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
|
|
|
|
{
|
|
|
|
const char* str_data = freerdp_get_logon_error_info_data(data);
|
|
|
|
const char* str_type = freerdp_get_logon_error_info_type(type);
|
|
|
|
|
|
|
|
if (!instance || !instance->context)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
WLog_INFO(TAG, "Logon Error Info %s [%s]", str_data, str_type);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback set in the rdp_freerdp structure, and used to make a certificate validation
|
|
|
|
* when the connection requires it.
|
|
|
|
* This function will actually be called by tls_verify_certificate().
|
|
|
|
* @see rdp_client_connect() and tls_connect()
|
|
|
|
* @param instance pointer to the rdp_freerdp structure that contains the connection settings
|
|
|
|
* @param host The host currently connecting to
|
|
|
|
* @param port The port currently connecting to
|
|
|
|
* @param common_name The common name of the certificate, should match host or an alias of it
|
|
|
|
* @param subject The subject of the certificate
|
|
|
|
* @param issuer The certificate issuer name
|
|
|
|
* @param fingerprint The fingerprint of the certificate
|
|
|
|
* @param flags See VERIFY_CERT_FLAG_* for possible values.
|
|
|
|
*
|
|
|
|
* @return 1 if the certificate is trusted, 2 if temporary trusted, 0 otherwise.
|
|
|
|
*/
|
|
|
|
static DWORD pf_client_verify_certificate_ex(freerdp* instance, const char* host, UINT16 port,
|
2019-11-06 17:24:51 +03:00
|
|
|
const char* common_name, const char* subject,
|
|
|
|
const char* issuer, const char* fingerprint,
|
|
|
|
DWORD flags)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
/* TODO: Add trust level to proxy configurable settings */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback set in the rdp_freerdp structure, and used to make a certificate validation
|
|
|
|
* when a stored certificate does not match the remote counterpart.
|
|
|
|
* This function will actually be called by tls_verify_certificate().
|
|
|
|
* @see rdp_client_connect() and tls_connect()
|
|
|
|
* @param instance pointer to the rdp_freerdp structure that contains the connection settings
|
|
|
|
* @param host The host currently connecting to
|
|
|
|
* @param port The port currently connecting to
|
|
|
|
* @param common_name The common name of the certificate, should match host or an alias of it
|
|
|
|
* @param subject The subject of the certificate
|
|
|
|
* @param issuer The certificate issuer name
|
|
|
|
* @param fingerprint The fingerprint of the certificate
|
|
|
|
* @param old_subject The subject of the previous certificate
|
|
|
|
* @param old_issuer The previous certificate issuer name
|
|
|
|
* @param old_fingerprint The fingerprint of the previous certificate
|
|
|
|
* @param flags See VERIFY_CERT_FLAG_* for possible values.
|
|
|
|
*
|
|
|
|
* @return 1 if the certificate is trusted, 2 if temporary trusted, 0 otherwise.
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static DWORD pf_client_verify_changed_certificate_ex(
|
|
|
|
freerdp* instance, const char* host, UINT16 port, const char* common_name, const char* subject,
|
|
|
|
const char* issuer, const char* fingerprint, const char* old_subject, const char* old_issuer,
|
|
|
|
const char* old_fingerprint, DWORD flags)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
/* TODO: Add trust level to proxy configurable settings */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:44:25 +03:00
|
|
|
static void pf_client_context_free(freerdp* instance, rdpContext* context)
|
|
|
|
{
|
|
|
|
pClientContext* pc = (pClientContext*)context;
|
|
|
|
|
|
|
|
if (!pc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
free(pc->frames_dir);
|
|
|
|
pc->frames_dir = NULL;
|
|
|
|
|
|
|
|
HashTable_Free(pc->vc_ids);
|
|
|
|
}
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
static BOOL pf_client_client_new(freerdp* instance, rdpContext* context)
|
|
|
|
{
|
|
|
|
if (!instance || !context)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
instance->PreConnect = pf_client_pre_connect;
|
|
|
|
instance->PostConnect = pf_client_post_connect;
|
|
|
|
instance->PostDisconnect = pf_client_post_disconnect;
|
|
|
|
instance->VerifyCertificateEx = pf_client_verify_certificate_ex;
|
|
|
|
instance->VerifyChangedCertificateEx = pf_client_verify_changed_certificate_ex;
|
|
|
|
instance->LogonErrorInfo = pf_logon_error_info;
|
2020-02-03 11:44:25 +03:00
|
|
|
instance->ContextFree = pf_client_context_free;
|
2019-10-22 16:06:05 +03:00
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-05-30 11:22:50 +03:00
|
|
|
static int pf_client_client_stop(rdpContext* context)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
pClientContext* pc = (pClientContext*)context;
|
2019-07-16 16:13:12 +03:00
|
|
|
proxyData* pdata = pc->pdata;
|
2019-08-01 17:01:11 +03:00
|
|
|
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_DBG(TAG, pc, "aborting client connection");
|
2019-08-01 17:01:11 +03:00
|
|
|
proxy_data_abort_connect(pdata);
|
2019-05-30 11:22:50 +03:00
|
|
|
freerdp_abort_connect(context->instance);
|
|
|
|
|
2019-07-16 16:13:12 +03:00
|
|
|
if (pdata->client_thread)
|
2019-05-30 11:22:50 +03:00
|
|
|
{
|
2019-08-01 17:01:11 +03:00
|
|
|
/*
|
|
|
|
* Wait for client thread to finish. No need to call CloseHandle() here, as
|
2019-07-16 16:13:12 +03:00
|
|
|
* it is the responsibility of `proxy_data_free`.
|
|
|
|
*/
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_DBG(TAG, pc, "waiting for client thread to finish");
|
2019-07-16 16:13:12 +03:00
|
|
|
WaitForSingleObject(pdata->client_thread, INFINITE);
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_DBG(TAG, pc, "thread finished");
|
2019-05-30 11:22:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
|
|
|
|
{
|
|
|
|
ZeroMemory(pEntryPoints, sizeof(RDP_CLIENT_ENTRY_POINTS));
|
|
|
|
pEntryPoints->Version = RDP_CLIENT_INTERFACE_VERSION;
|
|
|
|
pEntryPoints->Size = sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
|
|
|
|
pEntryPoints->ContextSize = sizeof(pClientContext);
|
|
|
|
/* Client init and finish */
|
|
|
|
pEntryPoints->ClientNew = pf_client_client_new;
|
2019-05-30 11:22:50 +03:00
|
|
|
pEntryPoints->ClientStop = pf_client_client_stop;
|
2019-05-17 15:32:54 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts running a client connection towards target server.
|
|
|
|
*/
|
|
|
|
DWORD WINAPI pf_client_start(LPVOID arg)
|
|
|
|
{
|
|
|
|
rdpContext* context = (rdpContext*)arg;
|
|
|
|
|
|
|
|
if (freerdp_client_start(context) != 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return pf_client_thread_proc(context->instance);
|
|
|
|
}
|