2019-05-17 15:32:54 +03:00
|
|
|
/**
|
2019-11-06 17:24:51 +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.
|
|
|
|
*/
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-12-26 11:25:13 +03:00
|
|
|
#include <winpr/crypto.h>
|
|
|
|
#include <winpr/print.h>
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
#include "pf_client.h"
|
|
|
|
#include "pf_context.h"
|
|
|
|
|
2020-02-02 16:15:28 +03:00
|
|
|
static wHashTable* create_channel_ids_map()
|
|
|
|
{
|
|
|
|
wHashTable* table = HashTable_New(TRUE);
|
|
|
|
if (!table)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
table->hash = HashTable_StringHash;
|
|
|
|
table->keyCompare = HashTable_StringCompare;
|
|
|
|
table->keyClone = HashTable_StringClone;
|
|
|
|
table->keyFree = HashTable_StringFree;
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
/* Proxy context initialization callback */
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL client_to_proxy_context_new(freerdp_peer* client, pServerContext* context)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2020-02-02 16:15:28 +03:00
|
|
|
proxyServer* server = (proxyServer*)client->ContextExtra;
|
|
|
|
proxyConfig* config = server->config;
|
|
|
|
|
2019-09-08 18:10:50 +03:00
|
|
|
context->dynvcReady = NULL;
|
2019-08-19 16:05:59 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
context->vcm = WTSOpenServerA((LPSTR)client->context);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
if (!context->vcm || context->vcm == INVALID_HANDLE_VALUE)
|
2019-09-08 18:10:50 +03:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(context->dynvcReady = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
goto error;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2020-02-02 16:15:28 +03:00
|
|
|
context->vc_handles = (HANDLE*)calloc(config->PassthroughCount, sizeof(HANDLE));
|
|
|
|
if (!context->vc_handles)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
context->vc_ids = create_channel_ids_map();
|
|
|
|
if (!context->vc_ids)
|
|
|
|
goto error;
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
return TRUE;
|
2019-09-08 18:10:50 +03:00
|
|
|
|
|
|
|
error:
|
|
|
|
WTSCloseServer((HANDLE)context->vcm);
|
2019-05-17 15:32:54 +03:00
|
|
|
context->vcm = NULL;
|
2019-09-08 18:10:50 +03:00
|
|
|
|
|
|
|
if (context->dynvcReady)
|
|
|
|
{
|
|
|
|
CloseHandle(context->dynvcReady);
|
|
|
|
context->dynvcReady = NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-02 16:15:28 +03:00
|
|
|
free(context->vc_handles);
|
|
|
|
context->vc_handles = NULL;
|
|
|
|
HashTable_Free(context->vc_ids);
|
|
|
|
context->vc_ids = NULL;
|
2019-05-17 15:32:54 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Proxy context free callback */
|
2019-11-06 17:24:51 +03:00
|
|
|
static void client_to_proxy_context_free(freerdp_peer* client, pServerContext* context)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2020-01-19 12:51:34 +03:00
|
|
|
proxyServer* server;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
if (!client || !context)
|
2019-05-28 16:10:04 +03:00
|
|
|
return;
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
server = (proxyServer*)client->ContextExtra;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
WTSCloseServer((HANDLE)context->vcm);
|
2019-05-28 16:10:04 +03:00
|
|
|
|
|
|
|
if (context->dynvcReady)
|
|
|
|
{
|
|
|
|
CloseHandle(context->dynvcReady);
|
|
|
|
context->dynvcReady = NULL;
|
|
|
|
}
|
2020-02-02 16:15:28 +03:00
|
|
|
|
|
|
|
HashTable_Free(context->vc_ids);
|
|
|
|
free(context->vc_handles);
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-09-08 18:10:50 +03:00
|
|
|
BOOL pf_context_init_server_context(freerdp_peer* client)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
client->ContextSize = sizeof(pServerContext);
|
2019-11-06 17:24:51 +03:00
|
|
|
client->ContextNew = (psPeerContextNew)client_to_proxy_context_new;
|
|
|
|
client->ContextFree = (psPeerContextFree)client_to_proxy_context_free;
|
2019-09-08 18:10:50 +03:00
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
return freerdp_peer_context_new(client);
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:18:45 +03:00
|
|
|
BOOL pf_context_copy_settings(rdpSettings* dst, const rdpSettings* src)
|
2019-07-16 13:28:14 +03:00
|
|
|
{
|
2020-05-19 14:42:26 +03:00
|
|
|
BOOL rc = FALSE;
|
|
|
|
rdpSettings* before_copy;
|
|
|
|
|
|
|
|
if (!dst || !src)
|
|
|
|
return FALSE;
|
2019-08-06 16:46:18 +03:00
|
|
|
|
2020-05-19 14:42:26 +03:00
|
|
|
before_copy = freerdp_settings_clone(dst);
|
2019-07-16 13:28:14 +03:00
|
|
|
if (!before_copy)
|
2019-08-13 15:06:45 +03:00
|
|
|
return FALSE;
|
2019-07-16 13:28:14 +03:00
|
|
|
|
2020-05-19 14:42:26 +03:00
|
|
|
#define REVERT_STR_VALUE(name) \
|
|
|
|
free(dst->name); \
|
|
|
|
dst->name = NULL; \
|
|
|
|
if (before_copy->name && !(dst->name = _strdup(before_copy->name))) \
|
|
|
|
goto out_fail
|
|
|
|
|
2019-07-16 13:28:14 +03:00
|
|
|
if (!freerdp_settings_copy(dst, src))
|
|
|
|
{
|
|
|
|
freerdp_settings_free(before_copy);
|
2019-08-13 15:06:45 +03:00
|
|
|
return FALSE;
|
2019-07-16 13:28:14 +03:00
|
|
|
}
|
|
|
|
|
2020-05-19 14:42:26 +03:00
|
|
|
/* keep original ServerMode value */
|
2019-09-08 15:18:45 +03:00
|
|
|
dst->ServerMode = before_copy->ServerMode;
|
2019-08-13 15:06:45 +03:00
|
|
|
|
|
|
|
/* revert some values that must not be changed */
|
2020-05-19 14:42:26 +03:00
|
|
|
REVERT_STR_VALUE(ConfigPath);
|
|
|
|
REVERT_STR_VALUE(PrivateKeyContent);
|
|
|
|
REVERT_STR_VALUE(RdpKeyContent);
|
|
|
|
REVERT_STR_VALUE(RdpKeyFile);
|
|
|
|
REVERT_STR_VALUE(PrivateKeyFile);
|
|
|
|
REVERT_STR_VALUE(CertificateFile);
|
|
|
|
REVERT_STR_VALUE(CertificateName);
|
|
|
|
REVERT_STR_VALUE(CertificateContent);
|
2019-07-16 13:28:14 +03:00
|
|
|
|
2019-09-08 15:18:45 +03:00
|
|
|
if (!dst->ServerMode)
|
2019-07-16 13:28:14 +03:00
|
|
|
{
|
2020-05-19 14:42:26 +03:00
|
|
|
/* adjust instance pointer */
|
2019-07-16 13:28:14 +03:00
|
|
|
dst->instance = before_copy->instance;
|
2020-01-02 16:19:08 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* RdpServerRsaKey must be set to NULL if `dst` is client's context
|
|
|
|
* it must be freed before setting it to NULL to avoid a memory leak!
|
|
|
|
*/
|
|
|
|
|
|
|
|
free(dst->RdpServerRsaKey->Modulus);
|
|
|
|
free(dst->RdpServerRsaKey->PrivateExponent);
|
|
|
|
free(dst->RdpServerRsaKey);
|
2019-07-16 13:28:14 +03:00
|
|
|
dst->RdpServerRsaKey = NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-19 14:42:26 +03:00
|
|
|
rc = TRUE;
|
|
|
|
|
|
|
|
out_fail:
|
2019-07-16 13:28:14 +03:00
|
|
|
freerdp_settings_free(before_copy);
|
2020-05-19 14:42:26 +03:00
|
|
|
return rc;
|
2019-07-16 13:28:14 +03:00
|
|
|
}
|
|
|
|
|
2019-09-25 16:09:36 +03:00
|
|
|
pClientContext* pf_context_create_client_context(rdpSettings* clientSettings)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
RDP_CLIENT_ENTRY_POINTS clientEntryPoints;
|
2019-09-25 16:09:36 +03:00
|
|
|
pClientContext* pc;
|
2019-05-17 15:32:54 +03:00
|
|
|
rdpContext* context;
|
|
|
|
RdpClientEntry(&clientEntryPoints);
|
|
|
|
context = freerdp_client_context_new(&clientEntryPoints);
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pc = (pClientContext*)context;
|
2019-09-25 16:09:36 +03:00
|
|
|
|
2019-09-08 15:18:45 +03:00
|
|
|
if (!pf_context_copy_settings(context->settings, clientSettings))
|
2019-05-20 14:09:12 +03:00
|
|
|
goto error;
|
|
|
|
|
2020-02-02 16:15:28 +03:00
|
|
|
pc->vc_ids = create_channel_ids_map();
|
|
|
|
if (!pc->vc_ids)
|
|
|
|
goto error;
|
|
|
|
|
2019-09-25 16:09:36 +03:00
|
|
|
return pc;
|
2019-05-20 14:09:12 +03:00
|
|
|
error:
|
|
|
|
freerdp_client_context_free(context);
|
|
|
|
return NULL;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2019-08-19 16:05:59 +03:00
|
|
|
proxyData* proxy_data_new(void)
|
2019-05-12 20:48:51 +03:00
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
BYTE temp[16];
|
2020-06-10 10:16:59 +03:00
|
|
|
char* hex;
|
|
|
|
proxyData* pdata;
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2020-06-10 10:16:59 +03:00
|
|
|
pdata = calloc(1, sizeof(proxyData));
|
2020-05-19 14:42:26 +03:00
|
|
|
if (!pdata)
|
2019-05-12 20:48:51 +03:00
|
|
|
return NULL;
|
|
|
|
|
2019-07-16 16:13:12 +03:00
|
|
|
if (!(pdata->abort_event = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
2020-05-19 14:42:26 +03:00
|
|
|
goto error;
|
2019-05-26 15:33:28 +03:00
|
|
|
|
2019-11-12 11:42:28 +03:00
|
|
|
if (!(pdata->gfx_server_ready = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
2020-05-19 14:42:26 +03:00
|
|
|
goto error;
|
2019-11-12 11:42:28 +03:00
|
|
|
|
2019-12-26 11:25:13 +03:00
|
|
|
winpr_RAND((BYTE*)&temp, 16);
|
2020-06-10 10:16:59 +03:00
|
|
|
hex = winpr_BinToHexString(temp, 16, FALSE);
|
|
|
|
if (!hex)
|
2020-05-19 14:42:26 +03:00
|
|
|
goto error;
|
2019-12-26 11:25:13 +03:00
|
|
|
|
2020-06-10 10:16:59 +03:00
|
|
|
CopyMemory(pdata->session_id, hex, PROXY_SESSION_ID_LENGTH);
|
|
|
|
pdata->session_id[PROXY_SESSION_ID_LENGTH] = '\0';
|
|
|
|
free(hex);
|
|
|
|
|
2019-12-31 12:16:12 +03:00
|
|
|
if (!(pdata->modules_info = HashTable_New(FALSE)))
|
2020-05-19 14:42:26 +03:00
|
|
|
goto error;
|
2019-12-31 12:16:12 +03:00
|
|
|
|
|
|
|
/* modules_info maps between plugin name to custom data */
|
|
|
|
pdata->modules_info->hash = HashTable_StringHash;
|
|
|
|
pdata->modules_info->keyCompare = HashTable_StringCompare;
|
|
|
|
pdata->modules_info->keyClone = HashTable_StringClone;
|
|
|
|
pdata->modules_info->keyFree = HashTable_StringFree;
|
2020-05-19 14:42:26 +03:00
|
|
|
|
2019-05-12 20:48:51 +03:00
|
|
|
return pdata;
|
2020-05-19 14:42:26 +03:00
|
|
|
error:
|
|
|
|
proxy_data_free(pdata);
|
|
|
|
return NULL;
|
2019-05-12 20:48:51 +03:00
|
|
|
}
|
|
|
|
|
2019-12-26 11:25:13 +03:00
|
|
|
/* updates circular pointers between proxyData and pClientContext instances */
|
|
|
|
void proxy_data_set_client_context(proxyData* pdata, pClientContext* context)
|
|
|
|
{
|
|
|
|
pdata->pc = context;
|
|
|
|
context->pdata = pdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* updates circular pointers between proxyData and pServerContext instances */
|
|
|
|
void proxy_data_set_server_context(proxyData* pdata, pServerContext* context)
|
|
|
|
{
|
|
|
|
pdata->ps = context;
|
|
|
|
context->pdata = pdata;
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:33:28 +03:00
|
|
|
void proxy_data_free(proxyData* pdata)
|
2019-05-12 20:48:51 +03:00
|
|
|
{
|
2019-07-16 16:13:12 +03:00
|
|
|
if (pdata->abort_event)
|
2019-05-28 16:10:04 +03:00
|
|
|
{
|
2019-07-16 16:13:12 +03:00
|
|
|
CloseHandle(pdata->abort_event);
|
|
|
|
pdata->abort_event = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pdata->client_thread)
|
|
|
|
{
|
|
|
|
CloseHandle(pdata->client_thread);
|
|
|
|
pdata->client_thread = NULL;
|
2019-05-28 16:10:04 +03:00
|
|
|
}
|
|
|
|
|
2019-11-12 11:42:28 +03:00
|
|
|
if (pdata->gfx_server_ready)
|
|
|
|
{
|
|
|
|
CloseHandle(pdata->gfx_server_ready);
|
|
|
|
pdata->gfx_server_ready = NULL;
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:16:12 +03:00
|
|
|
if (pdata->modules_info)
|
|
|
|
HashTable_Free(pdata->modules_info);
|
|
|
|
|
2019-05-12 20:48:51 +03:00
|
|
|
free(pdata);
|
|
|
|
}
|
2019-07-16 16:13:12 +03:00
|
|
|
|
|
|
|
void proxy_data_abort_connect(proxyData* pdata)
|
|
|
|
{
|
|
|
|
SetEvent(pdata->abort_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL proxy_data_shall_disconnect(proxyData* pdata)
|
|
|
|
{
|
|
|
|
return WaitForSingleObject(pdata->abort_event, 0) == WAIT_OBJECT_0;
|
|
|
|
}
|