2019-05-17 15:32:54 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* FreeRDP Proxy Server
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <winpr/crt.h>
|
2019-05-12 20:48:51 +03:00
|
|
|
#include <winpr/collections.h>
|
2020-01-21 15:56:49 +03:00
|
|
|
#include <winpr/cmdline.h>
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
#include "pf_log.h"
|
|
|
|
#include "pf_server.h"
|
|
|
|
#include "pf_config.h"
|
2019-08-19 16:05:59 +03:00
|
|
|
#include "pf_modules.h"
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
#define TAG PROXY_TAG("config")
|
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
#define CONFIG_PRINT_SECTION(section) WLog_INFO(TAG, "\t%s:", section)
|
|
|
|
#define CONFIG_PRINT_STR(config, key) WLog_INFO(TAG, "\t\t%s: %s", #key, config->key)
|
2019-11-06 17:24:51 +03:00
|
|
|
#define CONFIG_PRINT_BOOL(config, key) \
|
|
|
|
WLog_INFO(TAG, "\t\t%s: %s", #key, config->key ? "TRUE" : "FALSE")
|
|
|
|
#define CONFIG_PRINT_UINT16(config, key) WLog_INFO(TAG, "\t\t%s: %" PRIu16 "", #key, config->key)
|
|
|
|
#define CONFIG_PRINT_UINT32(config, key) WLog_INFO(TAG, "\t\t%s: %" PRIu32 "", #key, config->key)
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-10-24 10:51:52 +03:00
|
|
|
BOOL pf_config_get_uint16(wIniFile* ini, const char* section, const char* key, UINT16* result)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-08-03 16:46:59 +03:00
|
|
|
int val;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
val = IniFile_GetKeyValueInt(ini, section, key);
|
|
|
|
if ((val < 0) || (val > UINT16_MAX))
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-10-24 10:51:52 +03:00
|
|
|
WLog_ERR(TAG, "[%s]: invalid value %d for key '%s.%s'.", __FUNCTION__, val, section, key);
|
2019-08-03 16:46:59 +03:00
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
*result = (UINT16)val;
|
2019-08-03 16:46:59 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-10-24 10:51:52 +03:00
|
|
|
BOOL pf_config_get_uint32(wIniFile* ini, const char* section, const char* key, UINT32* result)
|
2019-08-12 15:11:09 +03:00
|
|
|
{
|
|
|
|
int val;
|
|
|
|
|
|
|
|
val = IniFile_GetKeyValueInt(ini, section, key);
|
2019-10-16 15:54:05 +03:00
|
|
|
if ((val < 0) || (val > INT32_MAX))
|
2019-08-12 15:11:09 +03:00
|
|
|
{
|
2019-10-24 10:51:52 +03:00
|
|
|
WLog_ERR(TAG, "[%s]: invalid value %d for key '%s.%s'.", __FUNCTION__, val, section, key);
|
2019-08-12 15:11:09 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
*result = (UINT32)val;
|
2019-08-12 15:11:09 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-10-24 10:51:52 +03:00
|
|
|
BOOL pf_config_get_bool(wIniFile* ini, const char* section, const char* key)
|
|
|
|
{
|
|
|
|
int num_value;
|
|
|
|
const char* str_value;
|
|
|
|
|
|
|
|
str_value = IniFile_GetKeyValueString(ini, section, key);
|
|
|
|
if (!str_value)
|
|
|
|
{
|
2019-12-29 11:55:40 +03:00
|
|
|
WLog_WARN(TAG, "[%s]: key '%s.%s' not found, value defaults to false.", __FUNCTION__,
|
|
|
|
section, key);
|
2019-10-24 10:51:52 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(str_value, "TRUE") == 0 || strcmp(str_value, "true") == 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
num_value = IniFile_GetKeyValueInt(ini, section, key);
|
|
|
|
|
|
|
|
if (num_value == 1)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* pf_config_get_str(wIniFile* ini, const char* section, const char* key)
|
|
|
|
{
|
|
|
|
const char* value;
|
|
|
|
|
|
|
|
value = IniFile_GetKeyValueString(ini, section, key);
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
{
|
2019-12-31 12:16:12 +03:00
|
|
|
WLog_ERR(TAG, "[%s]: key '%s.%s' not found.", __FUNCTION__, section, key);
|
2019-10-24 10:51:52 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
static BOOL pf_config_load_server(wIniFile* ini, proxyConfig* config)
|
|
|
|
{
|
2019-10-24 10:51:52 +03:00
|
|
|
const char* host;
|
2019-10-22 16:06:05 +03:00
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
if (!pf_config_get_uint16(ini, "Server", "Port", &config->Port))
|
|
|
|
return FALSE;
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2019-10-24 10:51:52 +03:00
|
|
|
host = pf_config_get_str(ini, "Server", "Host");
|
|
|
|
|
|
|
|
if (!host)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
config->Host = _strdup(host);
|
|
|
|
|
|
|
|
if (!config->Host)
|
|
|
|
return FALSE;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
static BOOL pf_config_load_target(wIniFile* ini, proxyConfig* config)
|
|
|
|
{
|
2019-10-24 10:51:52 +03:00
|
|
|
const char* target_host;
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2019-10-24 10:51:52 +03:00
|
|
|
if (!pf_config_get_uint16(ini, "Target", "Port", &config->TargetPort))
|
|
|
|
return FALSE;
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2019-10-24 10:51:52 +03:00
|
|
|
target_host = pf_config_get_str(ini, "Target", "Host");
|
|
|
|
|
|
|
|
if (!target_host)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
config->TargetHost = _strdup(target_host);
|
|
|
|
if (!config->TargetHost)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
config->UseLoadBalanceInfo = pf_config_get_bool(ini, "Target", "UseLoadBalanceInfo");
|
|
|
|
return TRUE;
|
2019-08-03 16:46:59 +03:00
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
static BOOL pf_config_load_channels(wIniFile* ini, proxyConfig* config)
|
|
|
|
{
|
2019-10-24 10:51:52 +03:00
|
|
|
config->GFX = pf_config_get_bool(ini, "Channels", "GFX");
|
|
|
|
config->DisplayControl = pf_config_get_bool(ini, "Channels", "DisplayControl");
|
|
|
|
config->Clipboard = pf_config_get_bool(ini, "Channels", "Clipboard");
|
|
|
|
config->AudioOutput = pf_config_get_bool(ini, "Channels", "AudioOutput");
|
2019-08-06 16:46:18 +03:00
|
|
|
config->RemoteApp = pf_config_get_bool(ini, "Channels", "RemoteApp");
|
2019-08-03 16:46:59 +03:00
|
|
|
return TRUE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
static BOOL pf_config_load_input(wIniFile* ini, proxyConfig* config)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-10-24 10:51:52 +03:00
|
|
|
config->Keyboard = pf_config_get_bool(ini, "Input", "Keyboard");
|
|
|
|
config->Mouse = pf_config_get_bool(ini, "Input", "Mouse");
|
2019-08-03 16:46:59 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
static BOOL pf_config_load_security(wIniFile* ini, proxyConfig* config)
|
|
|
|
{
|
2019-10-24 10:51:52 +03:00
|
|
|
config->ServerTlsSecurity = pf_config_get_bool(ini, "Security", "ServerTlsSecurity");
|
|
|
|
config->ServerRdpSecurity = pf_config_get_bool(ini, "Security", "ServerRdpSecurity");
|
2019-09-18 16:08:25 +03:00
|
|
|
|
2019-10-24 10:51:52 +03:00
|
|
|
config->ClientTlsSecurity = pf_config_get_bool(ini, "Security", "ClientTlsSecurity");
|
|
|
|
config->ClientNlaSecurity = pf_config_get_bool(ini, "Security", "ClientNlaSecurity");
|
|
|
|
config->ClientRdpSecurity = pf_config_get_bool(ini, "Security", "ClientRdpSecurity");
|
2019-12-29 11:55:40 +03:00
|
|
|
config->ClientAllowFallbackToTls =
|
|
|
|
pf_config_get_bool(ini, "Security", "ClientAllowFallbackToTls");
|
2019-08-03 16:46:59 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-08-12 15:11:09 +03:00
|
|
|
static BOOL pf_config_load_clipboard(wIniFile* ini, proxyConfig* config)
|
|
|
|
{
|
2019-10-24 10:51:52 +03:00
|
|
|
config->TextOnly = pf_config_get_bool(ini, "Clipboard", "TextOnly");
|
2019-08-12 15:11:09 +03:00
|
|
|
|
|
|
|
if (!pf_config_get_uint32(ini, "Clipboard", "MaxTextLength", &config->MaxTextLength))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:05:59 +03:00
|
|
|
static BOOL pf_config_load_modules(wIniFile* ini, proxyConfig* config)
|
2019-08-03 16:46:59 +03:00
|
|
|
{
|
2020-01-21 15:56:49 +03:00
|
|
|
const char* modules_to_load;
|
2019-12-31 12:16:12 +03:00
|
|
|
const char* required_modules;
|
2019-08-03 16:46:59 +03:00
|
|
|
|
2020-01-21 15:56:49 +03:00
|
|
|
modules_to_load = IniFile_GetKeyValueString(ini, "Plugins", "Modules");
|
2019-12-31 12:16:12 +03:00
|
|
|
required_modules = IniFile_GetKeyValueString(ini, "Plugins", "Required");
|
2019-08-03 16:46:59 +03:00
|
|
|
|
2020-01-21 15:56:49 +03:00
|
|
|
config->Modules = CommandLineParseCommaSeparatedValues(modules_to_load, &config->ModulesCount);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2020-01-21 15:56:49 +03:00
|
|
|
config->RequiredPlugins =
|
|
|
|
CommandLineParseCommaSeparatedValues(required_modules, &config->RequiredPluginsCount);
|
2019-05-17 15:32:54 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-10-22 16:06:05 +03:00
|
|
|
static BOOL pf_config_load_captures(wIniFile* ini, proxyConfig* config)
|
|
|
|
{
|
|
|
|
const char* captures_dir;
|
|
|
|
|
2019-10-24 10:51:52 +03:00
|
|
|
config->SessionCapture = pf_config_get_bool(ini, "SessionCapture", "Enabled");
|
2019-10-27 14:38:31 +03:00
|
|
|
if (!config->SessionCapture)
|
|
|
|
return TRUE;
|
|
|
|
|
2019-10-24 10:51:52 +03:00
|
|
|
captures_dir = pf_config_get_str(ini, "SessionCapture", "CapturesDirectory");
|
2019-10-22 16:06:05 +03:00
|
|
|
|
|
|
|
if (!captures_dir)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
config->CapturesDirectory = strdup(captures_dir);
|
|
|
|
if (!config->CapturesDirectory)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!PathFileExistsA(config->CapturesDirectory))
|
|
|
|
{
|
|
|
|
if (!CreateDirectoryA(config->CapturesDirectory, NULL))
|
|
|
|
{
|
2019-10-24 10:51:52 +03:00
|
|
|
free(config->CapturesDirectory);
|
|
|
|
config->CapturesDirectory = NULL;
|
2019-10-22 16:06:05 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
proxyConfig* pf_server_config_load(const char* path)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2020-01-19 12:51:34 +03:00
|
|
|
proxyConfig* config = NULL;
|
2019-05-17 15:32:54 +03:00
|
|
|
wIniFile* ini = IniFile_New();
|
|
|
|
|
|
|
|
if (!ini)
|
2019-08-03 16:46:59 +03:00
|
|
|
{
|
2020-01-21 15:56:49 +03:00
|
|
|
WLog_ERR(TAG, "[%s]: IniFile_New() failed!", __FUNCTION__);
|
2019-08-03 16:46:59 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
if (IniFile_ReadFile(ini, path) < 0)
|
2019-08-03 16:46:59 +03:00
|
|
|
{
|
2020-01-21 15:56:49 +03:00
|
|
|
WLog_ERR(TAG, "[%s] failed to parse ini file: '%s'", __FUNCTION__, path);
|
2019-05-17 15:32:54 +03:00
|
|
|
goto out;
|
2019-08-03 16:46:59 +03:00
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
config = calloc(1, sizeof(proxyConfig));
|
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
if (!pf_config_load_server(ini, config))
|
2019-05-17 15:32:54 +03:00
|
|
|
goto out;
|
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
if (!pf_config_load_target(ini, config))
|
2019-05-17 15:32:54 +03:00
|
|
|
goto out;
|
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
if (!pf_config_load_channels(ini, config))
|
|
|
|
goto out;
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
if (!pf_config_load_input(ini, config))
|
2019-05-12 20:48:51 +03:00
|
|
|
goto out;
|
|
|
|
|
2019-08-03 16:46:59 +03:00
|
|
|
if (!pf_config_load_security(ini, config))
|
|
|
|
goto out;
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2019-08-19 16:05:59 +03:00
|
|
|
if (!pf_config_load_modules(ini, config))
|
2019-08-03 16:46:59 +03:00
|
|
|
goto out;
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2019-08-12 15:11:09 +03:00
|
|
|
if (!pf_config_load_clipboard(ini, config))
|
|
|
|
goto out;
|
|
|
|
|
2019-10-22 16:06:05 +03:00
|
|
|
if (!pf_config_load_captures(ini, config))
|
|
|
|
goto out;
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
IniFile_Free(ini);
|
|
|
|
return config;
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
out:
|
|
|
|
IniFile_Free(ini);
|
2020-01-19 12:51:34 +03:00
|
|
|
pf_server_config_free(config);
|
|
|
|
return NULL;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-08-04 10:08:43 +03:00
|
|
|
void pf_server_config_print(proxyConfig* config)
|
|
|
|
{
|
|
|
|
WLog_INFO(TAG, "Proxy configuration:");
|
|
|
|
|
|
|
|
CONFIG_PRINT_SECTION("Server");
|
|
|
|
CONFIG_PRINT_STR(config, Host);
|
|
|
|
CONFIG_PRINT_UINT16(config, Port);
|
2019-10-22 16:06:05 +03:00
|
|
|
CONFIG_PRINT_BOOL(config, SessionCapture);
|
2019-08-04 10:08:43 +03:00
|
|
|
|
|
|
|
if (!config->UseLoadBalanceInfo)
|
|
|
|
{
|
|
|
|
CONFIG_PRINT_SECTION("Target");
|
|
|
|
CONFIG_PRINT_STR(config, TargetHost);
|
|
|
|
CONFIG_PRINT_UINT16(config, TargetPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
CONFIG_PRINT_SECTION("Input");
|
|
|
|
CONFIG_PRINT_BOOL(config, Keyboard);
|
|
|
|
CONFIG_PRINT_BOOL(config, Mouse);
|
|
|
|
|
2019-09-18 16:08:25 +03:00
|
|
|
CONFIG_PRINT_SECTION("Server Security");
|
|
|
|
CONFIG_PRINT_BOOL(config, ServerTlsSecurity);
|
|
|
|
CONFIG_PRINT_BOOL(config, ServerRdpSecurity);
|
|
|
|
|
|
|
|
CONFIG_PRINT_SECTION("Client Security");
|
|
|
|
CONFIG_PRINT_BOOL(config, ClientNlaSecurity);
|
|
|
|
CONFIG_PRINT_BOOL(config, ClientTlsSecurity);
|
|
|
|
CONFIG_PRINT_BOOL(config, ClientRdpSecurity);
|
2019-12-29 11:55:40 +03:00
|
|
|
CONFIG_PRINT_BOOL(config, ClientAllowFallbackToTls);
|
2019-08-04 10:08:43 +03:00
|
|
|
|
|
|
|
CONFIG_PRINT_SECTION("Channels");
|
|
|
|
CONFIG_PRINT_BOOL(config, GFX);
|
|
|
|
CONFIG_PRINT_BOOL(config, DisplayControl);
|
2019-08-04 17:41:43 +03:00
|
|
|
CONFIG_PRINT_BOOL(config, Clipboard);
|
2019-08-13 13:53:36 +03:00
|
|
|
CONFIG_PRINT_BOOL(config, AudioOutput);
|
2019-08-06 16:46:18 +03:00
|
|
|
CONFIG_PRINT_BOOL(config, RemoteApp);
|
2019-08-04 17:41:43 +03:00
|
|
|
|
|
|
|
CONFIG_PRINT_SECTION("Clipboard");
|
|
|
|
CONFIG_PRINT_BOOL(config, TextOnly);
|
|
|
|
if (config->MaxTextLength > 0)
|
|
|
|
CONFIG_PRINT_UINT32(config, MaxTextLength);
|
2019-10-22 16:06:05 +03:00
|
|
|
|
2019-10-24 10:51:52 +03:00
|
|
|
CONFIG_PRINT_SECTION("SessionCapture");
|
|
|
|
CONFIG_PRINT_BOOL(config, SessionCapture);
|
|
|
|
CONFIG_PRINT_STR(config, CapturesDirectory);
|
2019-08-04 10:08:43 +03:00
|
|
|
}
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
void pf_server_config_free(proxyConfig* config)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-12-31 16:52:33 +03:00
|
|
|
if (config == NULL)
|
|
|
|
return;
|
|
|
|
|
2019-10-22 16:06:05 +03:00
|
|
|
free(config->CapturesDirectory);
|
2020-01-21 15:56:49 +03:00
|
|
|
free(config->RequiredPlugins);
|
|
|
|
free(config->Modules);
|
2019-05-17 15:32:54 +03:00
|
|
|
free(config->TargetHost);
|
|
|
|
free(config->Host);
|
2020-01-19 12:51:34 +03:00
|
|
|
free(config);
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|