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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <winpr/crt.h>
|
2019-05-12 20:48:51 +03:00
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
#include "pf_log.h"
|
|
|
|
#include "pf_server.h"
|
|
|
|
#include "pf_config.h"
|
|
|
|
|
|
|
|
#define TAG PROXY_TAG("config")
|
|
|
|
|
|
|
|
#define CHANNELS_SEPERATOR ","
|
|
|
|
|
2019-05-12 20:48:51 +03:00
|
|
|
wArrayList* parse_string_array_from_str(const char* str)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-05-12 20:48:51 +03:00
|
|
|
wArrayList* list = ArrayList_New(FALSE);
|
2019-05-27 12:06:59 +03:00
|
|
|
char* s;
|
|
|
|
char* temp;
|
|
|
|
char* token;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-05-12 20:48:51 +03:00
|
|
|
if (list == NULL)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-05-12 20:48:51 +03:00
|
|
|
WLog_ERR(TAG, "parse_string_array_from_str(): ArrayList_New failed!");
|
|
|
|
return NULL;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-27 12:06:59 +03:00
|
|
|
|
|
|
|
temp = s = _strdup(str);
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "parse_string_array_from_str(): strdup failed!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-12 20:48:51 +03:00
|
|
|
|
|
|
|
if (s == NULL)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-05-12 20:48:51 +03:00
|
|
|
WLog_ERR(TAG, "parse_string_array_from_str(): strdup failed!");
|
|
|
|
goto error;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
2019-05-12 20:48:51 +03:00
|
|
|
|
|
|
|
while ((token = StrSep(&temp, CHANNELS_SEPERATOR)) != NULL)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-05-12 20:48:51 +03:00
|
|
|
char* current_token = _strdup(token);
|
|
|
|
|
|
|
|
if (current_token == NULL)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "parse_string_array_from_str(): strdup failed!");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ArrayList_Add(list, current_token) < 0)
|
|
|
|
{
|
|
|
|
free(current_token);
|
|
|
|
goto error;
|
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
free(s);
|
2019-05-12 20:48:51 +03:00
|
|
|
return list;
|
|
|
|
error:
|
|
|
|
free(s);
|
|
|
|
ArrayList_Free(list);
|
|
|
|
return NULL;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL pf_server_is_config_valid(proxyConfig* config)
|
|
|
|
{
|
|
|
|
if (config->Host == NULL)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Configuration value for `Server.Host` is not valid");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->Port <= 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Configuration value for `Server.Port` is not valid");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config->UseLoadBalanceInfo)
|
|
|
|
{
|
|
|
|
if (config->TargetHost == NULL)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Configuration value for `Target.Host` is not valid");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->TargetPort <= 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Configuration value for `Target.Port` is not valid");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD pf_server_load_config(const char* path, proxyConfig* config)
|
|
|
|
{
|
|
|
|
const char* input;
|
2019-05-12 20:48:51 +03:00
|
|
|
char** filters_names;
|
2019-05-17 15:32:54 +03:00
|
|
|
int rc;
|
2019-05-12 20:48:51 +03:00
|
|
|
int filters_count = 0;
|
2019-05-27 12:06:59 +03:00
|
|
|
UINT32 index;
|
2019-05-17 15:32:54 +03:00
|
|
|
DWORD result = CONFIG_PARSE_ERROR;
|
|
|
|
wIniFile* ini = IniFile_New();
|
|
|
|
|
|
|
|
if (!ini)
|
|
|
|
return CONFIG_PARSE_ERROR;
|
|
|
|
|
|
|
|
if (IniFile_ReadFile(ini, path) < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* server */
|
|
|
|
config->Host = _strdup(IniFile_GetKeyValueString(ini, "Server", "Host"));
|
|
|
|
config->LocalOnly = IniFile_GetKeyValueInt(ini, "Server", "LocalOnly");
|
|
|
|
rc = IniFile_GetKeyValueInt(ini, "Server", "Port");
|
|
|
|
|
|
|
|
if ((rc < 0) || (rc > UINT16_MAX))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
config->Port = (UINT16)rc;
|
|
|
|
/* target */
|
|
|
|
config->UseLoadBalanceInfo = IniFile_GetKeyValueInt(ini, "Target", "UseLoadBalanceInfo");
|
|
|
|
config->TargetHost = _strdup(IniFile_GetKeyValueString(ini, "Target", "Host"));
|
|
|
|
rc = IniFile_GetKeyValueInt(ini, "Target", "Port");
|
|
|
|
|
|
|
|
if ((rc < 0) || (rc > UINT16_MAX))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
config->TargetPort = (UINT16)rc;
|
|
|
|
/* graphics */
|
|
|
|
config->GFX = IniFile_GetKeyValueInt(ini, "Graphics", "GFX");
|
|
|
|
config->BitmapUpdate = IniFile_GetKeyValueInt(ini, "Graphics", "BitmapUpdate");
|
|
|
|
/* input */
|
|
|
|
config->Keyboard = IniFile_GetKeyValueInt(ini, "Input", "Keyboard");
|
|
|
|
config->Mouse = IniFile_GetKeyValueInt(ini, "Input", "Mouse");
|
|
|
|
/* security */
|
|
|
|
config->TlsSecurity = IniFile_GetKeyValueInt(ini, "Security", "TlsSecurity");
|
|
|
|
config->NlaSecurity = IniFile_GetKeyValueInt(ini, "Security", "NlaSecurity");
|
|
|
|
config->RdpSecurity = IniFile_GetKeyValueInt(ini, "Security", "RdpSecurity");
|
|
|
|
/* channels filtering */
|
|
|
|
config->WhitelistMode = IniFile_GetKeyValueInt(ini, "Channels", "WhitelistMode");
|
|
|
|
input = IniFile_GetKeyValueString(ini, "Channels", "AllowedChannels");
|
2019-05-12 20:48:51 +03:00
|
|
|
/* filters api */
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
if (input)
|
|
|
|
{
|
2019-05-12 20:48:51 +03:00
|
|
|
config->AllowedChannels = parse_string_array_from_str(input);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
if (config->AllowedChannels == NULL)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
input = IniFile_GetKeyValueString(ini, "Channels", "DeniedChannels");
|
|
|
|
|
|
|
|
if (input)
|
|
|
|
{
|
2019-05-12 20:48:51 +03:00
|
|
|
config->BlockedChannels = parse_string_array_from_str(input);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
if (config->BlockedChannels == NULL)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = CONFIG_PARSE_SUCCESS;
|
2019-05-12 20:48:51 +03:00
|
|
|
|
|
|
|
if (!pf_filters_init(&config->Filters))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
filters_names = IniFile_GetSectionKeyNames(ini, "Filters", &filters_count);
|
|
|
|
|
2019-05-27 12:06:59 +03:00
|
|
|
for (index = 0; index < filters_count; index++)
|
2019-05-12 20:48:51 +03:00
|
|
|
{
|
2019-05-27 12:06:59 +03:00
|
|
|
char* filter_name = filters_names[index];
|
2019-05-12 20:48:51 +03:00
|
|
|
const char* path = IniFile_GetKeyValueString(ini, "Filters", filter_name);
|
|
|
|
|
|
|
|
if (!pf_filters_register_new(config->Filters, path, filter_name))
|
|
|
|
{
|
|
|
|
WLog_DBG(TAG, "pf_server_load_config(): failed to register %s (%s)", filter_name, path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WLog_DBG(TAG, "pf_server_load_config(): registered filter %s (%s) successfully", filter_name, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
out:
|
|
|
|
IniFile_Free(ini);
|
|
|
|
|
|
|
|
if (!pf_server_is_config_valid(config))
|
|
|
|
return CONFIG_INVALID;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pf_server_config_free(proxyConfig* config)
|
|
|
|
{
|
2019-05-12 20:48:51 +03:00
|
|
|
pf_filters_unregister_all(config->Filters);
|
|
|
|
ArrayList_Free(config->AllowedChannels);
|
|
|
|
ArrayList_Free(config->BlockedChannels);
|
2019-05-17 15:32:54 +03:00
|
|
|
free(config->TargetHost);
|
|
|
|
free(config->Host);
|
|
|
|
free(config);
|
|
|
|
}
|