26#include <winpr/path.h>
27#include <winpr/collections.h>
28#include <winpr/cmdline.h>
31#include <freerdp/server/proxy/proxy_config.h>
33#include <freerdp/server/proxy/proxy_log.h>
35#include <freerdp/crypto/crypto.h>
36#include <freerdp/channels/cliprdr.h>
37#include <freerdp/channels/rdpsnd.h>
38#include <freerdp/channels/audin.h>
39#include <freerdp/channels/rdpdr.h>
40#include <freerdp/channels/disp.h>
41#include <freerdp/channels/rail.h>
42#include <freerdp/channels/rdpei.h>
43#include <freerdp/channels/tsmf.h>
44#include <freerdp/channels/video.h>
45#include <freerdp/channels/rdpecam.h>
49#define TAG PROXY_TAG("config")
51#define CONFIG_PRINT_SECTION(section) WLog_INFO(TAG, "\t%s:", section)
52#define CONFIG_PRINT_SECTION_KEY(section, key) WLog_INFO(TAG, "\t%s/%s:", section, key)
53#define CONFIG_PRINT_STR(config, key) WLog_INFO(TAG, "\t\t%s: %s", #key, (config)->key)
54#define CONFIG_PRINT_STR_CONTENT(config, key) \
55 WLog_INFO(TAG, "\t\t%s: %s", #key, (config)->key ? "set" : nullptr)
56#define CONFIG_PRINT_BOOL(config, key) WLog_INFO(TAG, "\t\t%s: %s", #key, boolstr((config)->key))
57#define CONFIG_PRINT_UINT16(config, key) WLog_INFO(TAG, "\t\t%s: %" PRIu16 "", #key, (config)->key)
58#define CONFIG_PRINT_UINT32(config, key) WLog_INFO(TAG, "\t\t%s: %" PRIu32 "", #key, (config)->key)
60static const char* bool_str_true =
"true";
61static const char* bool_str_false =
"false";
64static const char* boolstr(BOOL rc)
66 return rc ? bool_str_true : bool_str_false;
69static const char* section_server =
"Server";
70static const char* key_host =
"Host";
71static const char* key_port =
"Port";
73static const char* section_target =
"Target";
74static const char* key_target_fixed =
"FixedTarget";
75static const char* key_target_user =
"User";
76static const char* key_target_pwd =
"Password";
77static const char* key_target_domain =
"Domain";
78static const char* key_target_tls_seclevel =
"TlsSecLevel";
80static const char* section_plugins =
"Plugins";
81static const char* key_plugins_modules =
"Modules";
82static const char* key_plugins_required =
"Required";
84static const char* section_channels =
"Channels";
85static const char* key_channels_gfx =
"GFX";
86static const char* key_channels_disp =
"DisplayControl";
87static const char* key_channels_clip =
"Clipboard";
88static const char* key_channels_mic =
"AudioInput";
89static const char* key_channels_sound =
"AudioOutput";
90static const char* key_channels_rdpdr =
"DeviceRedirection";
91static const char* key_channels_video =
"VideoRedirection";
92static const char* key_channels_camera =
"CameraRedirection";
93static const char* key_channels_rails =
"RemoteApp";
94static const char* key_channels_blacklist =
"PassthroughIsBlacklist";
95static const char* key_channels_pass =
"Passthrough";
96static const char* key_channels_intercept =
"Intercept";
98static const char* section_input =
"Input";
99static const char* key_input_kbd =
"Keyboard";
100static const char* key_input_mouse =
"Mouse";
101static const char* key_input_multitouch =
"Multitouch";
103static const char* section_security =
"Security";
104static const char* key_security_server_nla =
"ServerNlaSecurity";
105static const char* key_security_server_tls =
"ServerTlsSecurity";
106static const char* key_security_server_rdp =
"ServerRdpSecurity";
107static const char* key_security_client_nla =
"ClientNlaSecurity";
108static const char* key_security_client_tls =
"ClientTlsSecurity";
109static const char* key_security_client_rdp =
"ClientRdpSecurity";
110static const char* key_security_client_fallback =
"ClientAllowFallbackToTls";
112static const char* section_certificates =
"Certificates";
113static const char* key_private_key_file =
"PrivateKeyFile";
114static const char* key_private_key_content =
"PrivateKeyContent";
115static const char* key_cert_file =
"CertificateFile";
116static const char* key_cert_content =
"CertificateContent";
118WINPR_ATTR_MALLOC(CommandLineParserFree, 1)
120static
char** pf_config_parse_comma_separated_list(const
char* list,
size_t* count)
125 if (strlen(list) == 0)
131 return CommandLineParseCommaSeparatedValues(list, count);
135static BOOL pf_config_get_uint16(wIniFile* ini,
const char* section,
const char* key,
136 UINT16* result, BOOL required)
139 const char* strval =
nullptr;
141 WINPR_ASSERT(result);
143 strval = IniFile_GetKeyValueString(ini, section, key);
144 if (!strval && required)
146 WLog_ERR(TAG,
"key '%s.%s' does not exist.", section, key);
149 val = IniFile_GetKeyValueInt(ini, section, key);
150 if ((val <= 0) || (val > UINT16_MAX))
152 WLog_ERR(TAG,
"invalid value %d for key '%s.%s'.", val, section, key);
156 *result = (UINT16)val;
161static BOOL pf_config_get_uint32(wIniFile* ini,
const char* section,
const char* key,
162 UINT32* result, BOOL required)
164 WINPR_ASSERT(result);
166 const char* strval = IniFile_GetKeyValueString(ini, section, key);
170 WLog_ERR(TAG,
"key '%s.%s' does not exist.", section, key);
174 const int val = IniFile_GetKeyValueInt(ini, section, key);
177 WLog_ERR(TAG,
"invalid value %d for key '%s.%s'.", val, section, key);
181 *result = (UINT32)val;
186static BOOL pf_config_get_bool(wIniFile* ini,
const char* section,
const char* key, BOOL fallback)
189 const char* str_value =
nullptr;
191 str_value = IniFile_GetKeyValueString(ini, section, key);
194 WLog_WARN(TAG,
"key '%s.%s' not found, value defaults to %s.", section, key,
195 fallback ? bool_str_true : bool_str_false);
199 if (_stricmp(str_value, bool_str_true) == 0)
201 if (_stricmp(str_value, bool_str_false) == 0)
204 num_value = IniFile_GetKeyValueInt(ini, section, key);
206 return (num_value != 0);
210static const char* pf_config_get_str(wIniFile* ini,
const char* section,
const char* key,
213 const char* value =
nullptr;
215 value = IniFile_GetKeyValueString(ini, section, key);
220 WLog_ERR(TAG,
"key '%s.%s' not found.", section, key);
228static BOOL pf_config_load_server(wIniFile* ini, proxyConfig* config)
230 WINPR_ASSERT(config);
231 const char* host = pf_config_get_str(ini, section_server, key_host, FALSE);
237 config->Host = _strdup(host);
242 if (!pf_config_get_uint16(ini, section_server, key_port, &config->Port, TRUE))
249static BOOL pf_config_load_target(wIniFile* ini, proxyConfig* config)
251 const char* target_value =
nullptr;
253 WINPR_ASSERT(config);
254 config->FixedTarget = pf_config_get_bool(ini, section_target, key_target_fixed, FALSE);
256 if (!pf_config_get_uint16(ini, section_target, key_port, &config->TargetPort,
257 config->FixedTarget))
260 if (!pf_config_get_uint32(ini, section_target, key_target_tls_seclevel,
261 &config->TargetTlsSecLevel, FALSE))
264 if (config->FixedTarget)
266 target_value = pf_config_get_str(ini, section_target, key_host, TRUE);
270 free(config->TargetHost);
271 config->TargetHost = _strdup(target_value);
272 if (!config->TargetHost)
276 target_value = pf_config_get_str(ini, section_target, key_target_user, FALSE);
279 free(config->TargetUser);
280 config->TargetUser = _strdup(target_value);
281 if (!config->TargetUser)
285 target_value = pf_config_get_str(ini, section_target, key_target_pwd, FALSE);
288 free(config->TargetPassword);
289 config->TargetPassword = _strdup(target_value);
290 if (!config->TargetPassword)
294 target_value = pf_config_get_str(ini, section_target, key_target_domain, FALSE);
297 free(config->TargetDomain);
298 config->TargetDomain = _strdup(target_value);
299 if (!config->TargetDomain)
307static BOOL pf_config_load_channels(wIniFile* ini, proxyConfig* config)
309 WINPR_ASSERT(config);
310 config->GFX = pf_config_get_bool(ini, section_channels, key_channels_gfx, TRUE);
311 config->DisplayControl = pf_config_get_bool(ini, section_channels, key_channels_disp, TRUE);
312 config->Clipboard = pf_config_get_bool(ini, section_channels, key_channels_clip, FALSE);
313 config->AudioOutput = pf_config_get_bool(ini, section_channels, key_channels_mic, TRUE);
314 config->AudioInput = pf_config_get_bool(ini, section_channels, key_channels_sound, TRUE);
315 config->DeviceRedirection = pf_config_get_bool(ini, section_channels, key_channels_rdpdr, TRUE);
316 config->VideoRedirection = pf_config_get_bool(ini, section_channels, key_channels_video, TRUE);
317 config->CameraRedirection =
318 pf_config_get_bool(ini, section_channels, key_channels_camera, TRUE);
319 config->RemoteApp = pf_config_get_bool(ini, section_channels, key_channels_rails, FALSE);
320 config->PassthroughIsBlacklist =
321 pf_config_get_bool(ini, section_channels, key_channels_blacklist, FALSE);
322 config->Passthrough = pf_config_parse_comma_separated_list(
323 pf_config_get_str(ini, section_channels, key_channels_pass, FALSE),
324 &config->PassthroughCount);
325 config->Intercept = pf_config_parse_comma_separated_list(
326 pf_config_get_str(ini, section_channels, key_channels_intercept, FALSE),
327 &config->InterceptCount);
333static BOOL pf_config_load_input(wIniFile* ini, proxyConfig* config)
335 WINPR_ASSERT(config);
336 config->Keyboard = pf_config_get_bool(ini, section_input, key_input_kbd, TRUE);
337 config->Mouse = pf_config_get_bool(ini, section_input, key_input_mouse, TRUE);
338 config->Multitouch = pf_config_get_bool(ini, section_input, key_input_multitouch, TRUE);
343static BOOL pf_config_load_security(wIniFile* ini, proxyConfig* config)
345 WINPR_ASSERT(config);
346 config->ServerTlsSecurity =
347 pf_config_get_bool(ini, section_security, key_security_server_tls, TRUE);
348 config->ServerNlaSecurity =
349 pf_config_get_bool(ini, section_security, key_security_server_nla, FALSE);
350 config->ServerRdpSecurity =
351 pf_config_get_bool(ini, section_security, key_security_server_rdp, TRUE);
353 config->ClientTlsSecurity =
354 pf_config_get_bool(ini, section_security, key_security_client_tls, TRUE);
355 config->ClientNlaSecurity =
356 pf_config_get_bool(ini, section_security, key_security_client_nla, TRUE);
357 config->ClientRdpSecurity =
358 pf_config_get_bool(ini, section_security, key_security_client_rdp, TRUE);
359 config->ClientAllowFallbackToTls =
360 pf_config_get_bool(ini, section_security, key_security_client_fallback, TRUE);
365static BOOL pf_config_load_modules(wIniFile* ini, proxyConfig* config)
367 const char* modules_to_load =
nullptr;
368 const char* required_modules =
nullptr;
370 modules_to_load = pf_config_get_str(ini, section_plugins, key_plugins_modules, FALSE);
371 required_modules = pf_config_get_str(ini, section_plugins, key_plugins_required, FALSE);
373 WINPR_ASSERT(config);
374 config->Modules = pf_config_parse_comma_separated_list(modules_to_load, &config->ModulesCount);
376 config->RequiredPlugins =
377 pf_config_parse_comma_separated_list(required_modules, &config->RequiredPluginsCount);
382static char* pf_config_decode_base64(
const char* data,
const char* name,
size_t* pLength)
384 const char* headers[] = {
"-----BEGIN PUBLIC KEY-----",
"-----BEGIN RSA PUBLIC KEY-----",
385 "-----BEGIN CERTIFICATE-----",
"-----BEGIN PRIVATE KEY-----",
386 "-----BEGIN RSA PRIVATE KEY-----" };
388 size_t decoded_length = 0;
389 char* decoded =
nullptr;
392 WLog_ERR(TAG,
"Invalid base64 data [nullptr] for %s", name);
397 WINPR_ASSERT(pLength);
399 const size_t length = strlen(data);
401 if (strncmp(data,
"-----", 5) == 0)
403 BOOL expected = FALSE;
404 for (
size_t x = 0; x < ARRAYSIZE(headers); x++)
406 const char* header = headers[x];
408 if (strncmp(data, header, strlen(header)) == 0)
417 char hdr[128] = WINPR_C_ARRAY_INIT;
418 const char* end = strchr(&data[5],
'-');
424 const size_t s = MIN(ARRAYSIZE(hdr) - 1ULL, (
size_t)(end - data));
425 memcpy(hdr, data, s);
428 WLog_WARN(TAG,
"PEM has unexpected header '%s'. Known supported headers are:", hdr);
429 for (
size_t x = 0; x < ARRAYSIZE(headers); x++)
431 const char* header = headers[x];
432 WLog_WARN(TAG,
"%s", header);
436 *pLength = length + 1;
437 return _strdup(data);
440 crypto_base64_decode(data, length, (BYTE**)&decoded, &decoded_length);
441 if (!decoded || decoded_length == 0)
443 WLog_ERR(TAG,
"Failed to decode base64 data of length %" PRIuz
" for %s", length, name);
448 *pLength = strnlen(decoded, decoded_length) + 1;
453static BOOL pf_config_load_certificates(wIniFile* ini, proxyConfig* config)
455 const char* tmp1 =
nullptr;
456 const char* tmp2 =
nullptr;
459 WINPR_ASSERT(config);
461 tmp1 = pf_config_get_str(ini, section_certificates, key_cert_file, FALSE);
464 if (!winpr_PathFileExists(tmp1))
466 WLog_ERR(TAG,
"%s/%s file %s does not exist", section_certificates, key_cert_file,
470 config->CertificateFile = _strdup(tmp1);
471 config->CertificatePEM =
472 crypto_read_pem(config->CertificateFile, &config->CertificatePEMLength);
473 if (!config->CertificatePEM)
475 config->CertificatePEMLength += 1;
477 tmp2 = pf_config_get_str(ini, section_certificates, key_cert_content, FALSE);
480 if (strlen(tmp2) < 1)
482 WLog_ERR(TAG,
"%s/%s has invalid empty value", section_certificates, key_cert_content);
485 config->CertificateContent = _strdup(tmp2);
486 config->CertificatePEM = pf_config_decode_base64(
487 config->CertificateContent,
"CertificateContent", &config->CertificatePEMLength);
488 if (!config->CertificatePEM)
494 "%s/%s and %s/%s are "
495 "mutually exclusive options",
496 section_certificates, key_cert_file, section_certificates, key_cert_content);
499 else if (!tmp1 && !tmp2)
502 "%s/%s or %s/%s are "
504 section_certificates, key_cert_file, section_certificates, key_cert_content);
508 tmp1 = pf_config_get_str(ini, section_certificates, key_private_key_file, FALSE);
511 if (!winpr_PathFileExists(tmp1))
513 WLog_ERR(TAG,
"%s/%s file %s does not exist", section_certificates,
514 key_private_key_file, tmp1);
517 config->PrivateKeyFile = _strdup(tmp1);
518 config->PrivateKeyPEM =
519 crypto_read_pem(config->PrivateKeyFile, &config->PrivateKeyPEMLength);
520 if (!config->PrivateKeyPEM)
522 config->PrivateKeyPEMLength += 1;
524 tmp2 = pf_config_get_str(ini, section_certificates, key_private_key_content, FALSE);
527 if (strlen(tmp2) < 1)
529 WLog_ERR(TAG,
"%s/%s has invalid empty value", section_certificates,
530 key_private_key_content);
533 config->PrivateKeyContent = _strdup(tmp2);
534 config->PrivateKeyPEM = pf_config_decode_base64(
535 config->PrivateKeyContent,
"PrivateKeyContent", &config->PrivateKeyPEMLength);
536 if (!config->PrivateKeyPEM)
543 "%s/%s and %s/%s are "
544 "mutually exclusive options",
545 section_certificates, key_private_key_file, section_certificates,
546 key_private_key_content);
549 else if (!tmp1 && !tmp2)
552 "%s/%s or %s/%s are "
553 "are required settings",
554 section_certificates, key_private_key_file, section_certificates,
555 key_private_key_content);
564 proxyConfig* config =
nullptr;
568 config = calloc(1,
sizeof(proxyConfig));
572 config->TargetTlsSecLevel = 1;
575 if (!pf_config_load_server(ini, config))
578 if (!pf_config_load_target(ini, config))
581 if (!pf_config_load_channels(ini, config))
584 if (!pf_config_load_input(ini, config))
587 if (!pf_config_load_security(ini, config))
590 if (!pf_config_load_modules(ini, config))
593 if (!pf_config_load_certificates(ini, config))
595 config->ini = IniFile_Clone(ini);
601 WINPR_PRAGMA_DIAG_PUSH
602 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
604 WINPR_PRAGMA_DIAG_POP
612 wIniFile* ini = IniFile_New();
617 if (IniFile_SetKeyValueString(ini, section_server, key_host,
"0.0.0.0") < 0)
619 if (IniFile_SetKeyValueInt(ini, section_server, key_port, 3389) < 0)
623 if (IniFile_SetKeyValueString(ini, section_target, key_host,
"somehost.example.com") < 0)
625 if (IniFile_SetKeyValueInt(ini, section_target, key_port, 3389) < 0)
627 if (IniFile_SetKeyValueString(ini, section_target, key_target_fixed, bool_str_true) < 0)
629 if (IniFile_SetKeyValueInt(ini, section_target, key_target_tls_seclevel, 1) < 0)
633 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_gfx, bool_str_true) < 0)
635 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_disp, bool_str_true) < 0)
637 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_clip, bool_str_true) < 0)
639 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_mic, bool_str_true) < 0)
641 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_sound, bool_str_true) < 0)
643 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_rdpdr, bool_str_true) < 0)
645 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_video, bool_str_true) < 0)
647 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_camera, bool_str_true) < 0)
649 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_rails, bool_str_false) < 0)
652 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_blacklist, bool_str_true) < 0)
654 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_pass,
"") < 0)
656 if (IniFile_SetKeyValueString(ini, section_channels, key_channels_intercept,
"") < 0)
660 if (IniFile_SetKeyValueString(ini, section_input, key_input_kbd, bool_str_true) < 0)
662 if (IniFile_SetKeyValueString(ini, section_input, key_input_mouse, bool_str_true) < 0)
664 if (IniFile_SetKeyValueString(ini, section_input, key_input_multitouch, bool_str_true) < 0)
668 if (IniFile_SetKeyValueString(ini, section_security, key_security_server_tls, bool_str_true) <
671 if (IniFile_SetKeyValueString(ini, section_security, key_security_server_nla, bool_str_false) <
674 if (IniFile_SetKeyValueString(ini, section_security, key_security_server_rdp, bool_str_true) <
678 if (IniFile_SetKeyValueString(ini, section_security, key_security_client_tls, bool_str_true) <
681 if (IniFile_SetKeyValueString(ini, section_security, key_security_client_nla, bool_str_true) <
684 if (IniFile_SetKeyValueString(ini, section_security, key_security_client_rdp, bool_str_true) <
687 if (IniFile_SetKeyValueString(ini, section_security, key_security_client_fallback,
692 if (IniFile_SetKeyValueString(ini, section_plugins, key_plugins_modules,
693 "module1,module2,...") < 0)
695 if (IniFile_SetKeyValueString(ini, section_plugins, key_plugins_required,
696 "module1,module2,...") < 0)
700 if (IniFile_SetKeyValueString(ini, section_certificates, key_cert_file,
701 "<absolute path to some certificate file> OR") < 0)
703 if (IniFile_SetKeyValueString(ini, section_certificates, key_cert_content,
704 "<Contents of some certificate file in PEM format>") < 0)
707 if (IniFile_SetKeyValueString(ini, section_certificates, key_private_key_file,
708 "<absolute path to some private key file> OR") < 0)
710 if (IniFile_SetKeyValueString(ini, section_certificates, key_private_key_content,
711 "<Contents of some private key file in PEM format>") < 0)
715 if (IniFile_WriteFile(ini, file) < 0)
727 proxyConfig* config =
nullptr;
728 wIniFile* ini =
nullptr;
734 WLog_ERR(TAG,
"IniFile_New() failed!");
738 if (IniFile_ReadBuffer(ini, buffer) < 0)
740 WLog_ERR(TAG,
"failed to parse ini: '%s'", buffer);
752 proxyConfig* config =
nullptr;
753 wIniFile* ini = IniFile_New();
757 WLog_ERR(TAG,
"IniFile_New() failed!");
761 if (IniFile_ReadFile(ini, path) < 0)
763 WLog_ERR(TAG,
"failed to parse ini file: '%s'", path);
773static void pf_server_config_print_list(
char** list,
size_t count)
776 for (
size_t i = 0; i < count; i++)
777 WLog_INFO(TAG,
"\t\t- %s", list[i]);
782 WINPR_ASSERT(config);
783 WLog_INFO(TAG,
"Proxy configuration:");
785 CONFIG_PRINT_SECTION(section_server);
786 CONFIG_PRINT_STR(config, Host);
787 CONFIG_PRINT_UINT16(config, Port);
789 if (config->FixedTarget)
791 CONFIG_PRINT_SECTION(section_target);
792 CONFIG_PRINT_STR(config, TargetHost);
793 CONFIG_PRINT_UINT16(config, TargetPort);
794 CONFIG_PRINT_UINT32(config, TargetTlsSecLevel);
796 if (config->TargetUser)
797 CONFIG_PRINT_STR(config, TargetUser);
798 if (config->TargetDomain)
799 CONFIG_PRINT_STR(config, TargetDomain);
802 CONFIG_PRINT_SECTION(section_input);
803 CONFIG_PRINT_BOOL(config, Keyboard);
804 CONFIG_PRINT_BOOL(config, Mouse);
805 CONFIG_PRINT_BOOL(config, Multitouch);
807 CONFIG_PRINT_SECTION(section_security);
808 CONFIG_PRINT_BOOL(config, ServerNlaSecurity);
809 CONFIG_PRINT_BOOL(config, ServerTlsSecurity);
810 CONFIG_PRINT_BOOL(config, ServerRdpSecurity);
811 CONFIG_PRINT_BOOL(config, ClientNlaSecurity);
812 CONFIG_PRINT_BOOL(config, ClientTlsSecurity);
813 CONFIG_PRINT_BOOL(config, ClientRdpSecurity);
814 CONFIG_PRINT_BOOL(config, ClientAllowFallbackToTls);
816 CONFIG_PRINT_SECTION(section_channels);
817 CONFIG_PRINT_BOOL(config, GFX);
818 CONFIG_PRINT_BOOL(config, DisplayControl);
819 CONFIG_PRINT_BOOL(config, Clipboard);
820 CONFIG_PRINT_BOOL(config, AudioOutput);
821 CONFIG_PRINT_BOOL(config, AudioInput);
822 CONFIG_PRINT_BOOL(config, DeviceRedirection);
823 CONFIG_PRINT_BOOL(config, VideoRedirection);
824 CONFIG_PRINT_BOOL(config, CameraRedirection);
825 CONFIG_PRINT_BOOL(config, RemoteApp);
826 CONFIG_PRINT_BOOL(config, PassthroughIsBlacklist);
828 if (config->PassthroughCount)
830 WLog_INFO(TAG,
"\tStatic Channels Proxy:");
831 pf_server_config_print_list(config->Passthrough, config->PassthroughCount);
834 if (config->InterceptCount)
836 WLog_INFO(TAG,
"\tStatic Channels Proxy-Intercept:");
837 pf_server_config_print_list(config->Intercept, config->InterceptCount);
841 CONFIG_PRINT_SECTION_KEY(section_plugins, key_plugins_modules);
842 for (
size_t x = 0; x < config->ModulesCount; x++)
843 CONFIG_PRINT_STR(config, Modules[x]);
846 CONFIG_PRINT_SECTION_KEY(section_plugins, key_plugins_required);
847 for (
size_t x = 0; x < config->RequiredPluginsCount; x++)
848 CONFIG_PRINT_STR(config, RequiredPlugins[x]);
850 CONFIG_PRINT_SECTION(section_certificates);
851 CONFIG_PRINT_STR(config, CertificateFile);
852 CONFIG_PRINT_STR_CONTENT(config, CertificateContent);
853 CONFIG_PRINT_STR(config, PrivateKeyFile);
854 CONFIG_PRINT_STR_CONTENT(config, PrivateKeyContent);
857static void zfree(
char* str)
861 const size_t len = strlen(str);
866static void znfree(
char* str,
size_t len)
876 if (config ==
nullptr)
880 free(config->TargetHost);
881 free(config->TargetUser);
882 free(config->TargetDomain);
883 free(config->TargetPassword);
885 CommandLineParserFree(config->Passthrough);
886 CommandLineParserFree(config->Intercept);
887 CommandLineParserFree(config->Modules);
888 CommandLineParserFree(config->RequiredPlugins);
890 free(config->CertificateFile);
891 zfree(config->CertificateContent);
892 znfree(config->CertificatePEM, config->CertificatePEMLength);
893 free(config->PrivateKeyFile);
894 zfree(config->PrivateKeyContent);
895 znfree(config->PrivateKeyPEM, config->PrivateKeyPEMLength);
896 IniFile_Free(config->ini);
902 WINPR_ASSERT(config);
903 return config->RequiredPluginsCount;
908 WINPR_ASSERT(config);
909 if (index >= config->RequiredPluginsCount)
912 return config->RequiredPlugins[index];
917 WINPR_ASSERT(config);
918 return config->ModulesCount;
929 WINPR_ASSERT(config);
931 cnv.ppc = config->Modules;
936static BOOL pf_config_copy_string(
char** dst,
const char* src)
945static BOOL pf_config_copy_string_n(
char** dst,
const char* src,
size_t size)
949 if (src && (size > 0))
951 WINPR_ASSERT(strnlen(src, size) == size - 1);
952 *dst = calloc(size,
sizeof(
char));
955 memcpy(*dst, src, size);
962static BOOL pf_config_copy_string_list(
char*** dst,
size_t* size,
char** src,
size_t srcSize)
966 WINPR_ASSERT(src || (srcSize == 0));
970 if (srcSize > INT32_MAX)
975 char* csv = CommandLineToCommaSeparatedValues((INT32)srcSize, src);
976 *dst = CommandLineParseCommaSeparatedValues(csv, size);
985 proxyConfig* tmp = calloc(1,
sizeof(proxyConfig));
988 WINPR_ASSERT(config);
995 if (!pf_config_copy_string(&tmp->Host, config->Host))
997 if (!pf_config_copy_string(&tmp->TargetHost, config->TargetHost))
999 if (!pf_config_copy_string(&tmp->TargetUser, config->TargetUser))
1001 if (!pf_config_copy_string(&tmp->TargetDomain, config->TargetDomain))
1003 if (!pf_config_copy_string(&tmp->TargetPassword, config->TargetPassword))
1005 if (!pf_config_copy_string_list(&tmp->Passthrough, &tmp->PassthroughCount, config->Passthrough,
1006 config->PassthroughCount))
1008 if (!pf_config_copy_string_list(&tmp->Intercept, &tmp->InterceptCount, config->Intercept,
1009 config->InterceptCount))
1011 if (!pf_config_copy_string_list(&tmp->Modules, &tmp->ModulesCount, config->Modules,
1012 config->ModulesCount))
1014 if (!pf_config_copy_string_list(&tmp->RequiredPlugins, &tmp->RequiredPluginsCount,
1015 config->RequiredPlugins, config->RequiredPluginsCount))
1017 if (!pf_config_copy_string(&tmp->CertificateFile, config->CertificateFile))
1019 if (!pf_config_copy_string(&tmp->CertificateContent, config->CertificateContent))
1021 if (!pf_config_copy_string_n(&tmp->CertificatePEM, config->CertificatePEM,
1022 config->CertificatePEMLength))
1024 if (!pf_config_copy_string(&tmp->PrivateKeyFile, config->PrivateKeyFile))
1026 if (!pf_config_copy_string(&tmp->PrivateKeyContent, config->PrivateKeyContent))
1028 if (!pf_config_copy_string_n(&tmp->PrivateKeyPEM, config->PrivateKeyPEM,
1029 config->PrivateKeyPEMLength))
1032 tmp->ini = IniFile_Clone(config->ini);
1040 WINPR_PRAGMA_DIAG_PUSH
1041 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1043 WINPR_PRAGMA_DIAG_POP
1047struct config_plugin_data
1049 proxyPluginsManager* mgr;
1050 const proxyConfig* config;
1053static const char config_plugin_name[] =
"config";
1054static const char config_plugin_desc[] =
1055 "A plugin filtering according to proxy configuration file rules";
1058static BOOL config_plugin_unload(proxyPlugin* plugin)
1060 WINPR_ASSERT(plugin);
1065 free(plugin->custom);
1066 plugin->custom =
nullptr;
1073static BOOL config_plugin_keyboard_event(proxyPlugin* plugin, WINPR_ATTR_UNUSED proxyData* pdata,
1077 const struct config_plugin_data* custom =
nullptr;
1078 const proxyConfig* cfg =
nullptr;
1081 WINPR_ASSERT(plugin);
1082 WINPR_ASSERT(pdata);
1083 WINPR_ASSERT(event_data);
1085 WINPR_UNUSED(event_data);
1087 custom = plugin->custom;
1088 WINPR_ASSERT(custom);
1090 cfg = custom->config;
1094 WLog_DBG(TAG,
"%s", boolstr(rc));
1099static BOOL config_plugin_unicode_event(proxyPlugin* plugin, WINPR_ATTR_UNUSED proxyData* pdata,
1103 const struct config_plugin_data* custom =
nullptr;
1104 const proxyConfig* cfg =
nullptr;
1107 WINPR_ASSERT(plugin);
1108 WINPR_ASSERT(pdata);
1109 WINPR_ASSERT(event_data);
1111 WINPR_UNUSED(event_data);
1113 custom = plugin->custom;
1114 WINPR_ASSERT(custom);
1116 cfg = custom->config;
1120 WLog_DBG(TAG,
"%s", boolstr(rc));
1125static BOOL config_plugin_mouse_event(proxyPlugin* plugin, WINPR_ATTR_UNUSED proxyData* pdata,
1129 const struct config_plugin_data* custom =
nullptr;
1130 const proxyConfig* cfg =
nullptr;
1133 WINPR_ASSERT(plugin);
1134 WINPR_ASSERT(pdata);
1135 WINPR_ASSERT(event_data);
1137 WINPR_UNUSED(event_data);
1139 custom = plugin->custom;
1140 WINPR_ASSERT(custom);
1142 cfg = custom->config;
1150static BOOL config_plugin_mouse_ex_event(proxyPlugin* plugin, WINPR_ATTR_UNUSED proxyData* pdata,
1154 const struct config_plugin_data* custom =
nullptr;
1155 const proxyConfig* cfg =
nullptr;
1158 WINPR_ASSERT(plugin);
1159 WINPR_ASSERT(pdata);
1160 WINPR_ASSERT(event_data);
1162 WINPR_UNUSED(event_data);
1164 custom = plugin->custom;
1165 WINPR_ASSERT(custom);
1167 cfg = custom->config;
1175static BOOL config_plugin_client_channel_data(WINPR_ATTR_UNUSED proxyPlugin* plugin,
1176 WINPR_ATTR_UNUSED proxyData* pdata,
void* param)
1180 WINPR_ASSERT(plugin);
1181 WINPR_ASSERT(pdata);
1182 WINPR_ASSERT(channel);
1184 WLog_DBG(TAG,
"%s [0x%04" PRIx16
"] got %" PRIuz, channel->channel_name, channel->channel_id,
1190static BOOL config_plugin_server_channel_data(WINPR_ATTR_UNUSED proxyPlugin* plugin,
1191 WINPR_ATTR_UNUSED proxyData* pdata,
void* param)
1195 WINPR_ASSERT(plugin);
1196 WINPR_ASSERT(pdata);
1197 WINPR_ASSERT(channel);
1199 WLog_DBG(TAG,
"%s [0x%04" PRIx16
"] got %" PRIuz, channel->channel_name, channel->channel_id,
1205static BOOL config_plugin_dynamic_channel_create(proxyPlugin* plugin,
1206 WINPR_ATTR_UNUSED proxyData* pdata,
void* param)
1211 WINPR_ASSERT(plugin);
1212 WINPR_ASSERT(pdata);
1213 WINPR_ASSERT(channel);
1215 const struct config_plugin_data* custom = plugin->custom;
1216 WINPR_ASSERT(custom);
1218 const proxyConfig* cfg = custom->config;
1221 pf_utils_channel_mode rc = pf_utils_get_channel_mode(cfg, channel->channel_name);
1225 case PF_UTILS_CHANNEL_INTERCEPT:
1226 case PF_UTILS_CHANNEL_PASSTHROUGH:
1229 case PF_UTILS_CHANNEL_BLOCK:
1237 if (strncmp(RDPGFX_DVC_CHANNEL_NAME, channel->channel_name,
1238 sizeof(RDPGFX_DVC_CHANNEL_NAME)) == 0)
1240 else if (strncmp(RDPSND_DVC_CHANNEL_NAME, channel->channel_name,
1241 sizeof(RDPSND_DVC_CHANNEL_NAME)) == 0)
1242 accept = cfg->AudioOutput;
1243 else if (strncmp(RDPSND_LOSSY_DVC_CHANNEL_NAME, channel->channel_name,
1244 sizeof(RDPSND_LOSSY_DVC_CHANNEL_NAME)) == 0)
1245 accept = cfg->AudioOutput;
1246 else if (strncmp(AUDIN_DVC_CHANNEL_NAME, channel->channel_name,
1247 sizeof(AUDIN_DVC_CHANNEL_NAME)) == 0)
1248 accept = cfg->AudioInput;
1249 else if (strncmp(RDPEI_DVC_CHANNEL_NAME, channel->channel_name,
1250 sizeof(RDPEI_DVC_CHANNEL_NAME)) == 0)
1251 accept = cfg->Multitouch;
1252 else if (strncmp(TSMF_DVC_CHANNEL_NAME, channel->channel_name,
1253 sizeof(TSMF_DVC_CHANNEL_NAME)) == 0)
1254 accept = cfg->VideoRedirection;
1255 else if (strncmp(VIDEO_CONTROL_DVC_CHANNEL_NAME, channel->channel_name,
1256 sizeof(VIDEO_CONTROL_DVC_CHANNEL_NAME)) == 0)
1257 accept = cfg->VideoRedirection;
1258 else if (strncmp(VIDEO_DATA_DVC_CHANNEL_NAME, channel->channel_name,
1259 sizeof(VIDEO_DATA_DVC_CHANNEL_NAME)) == 0)
1260 accept = cfg->VideoRedirection;
1261 else if (strncmp(RDPECAM_DVC_CHANNEL_NAME, channel->channel_name,
1262 sizeof(RDPECAM_DVC_CHANNEL_NAME)) == 0)
1263 accept = cfg->CameraRedirection;
1266 WLog_DBG(TAG,
"%s [0x%04" PRIx16
"]: %s", channel->channel_name, channel->channel_id,
1272static BOOL config_plugin_channel_create(proxyPlugin* plugin, WINPR_ATTR_UNUSED proxyData* pdata,
1278 WINPR_ASSERT(plugin);
1279 WINPR_ASSERT(pdata);
1280 WINPR_ASSERT(channel);
1282 const struct config_plugin_data* custom = plugin->custom;
1283 WINPR_ASSERT(custom);
1285 const proxyConfig* cfg = custom->config;
1288 pf_utils_channel_mode rc = pf_utils_get_channel_mode(cfg, channel->channel_name);
1291 case PF_UTILS_CHANNEL_INTERCEPT:
1292 case PF_UTILS_CHANNEL_PASSTHROUGH:
1295 case PF_UTILS_CHANNEL_BLOCK:
1302 if (strncmp(CLIPRDR_SVC_CHANNEL_NAME, channel->channel_name,
1303 sizeof(CLIPRDR_SVC_CHANNEL_NAME)) == 0)
1304 accept = cfg->Clipboard;
1305 else if (strncmp(RDPSND_CHANNEL_NAME, channel->channel_name,
sizeof(RDPSND_CHANNEL_NAME)) ==
1307 accept = cfg->AudioOutput;
1308 else if (strncmp(RDPDR_SVC_CHANNEL_NAME, channel->channel_name,
1309 sizeof(RDPDR_SVC_CHANNEL_NAME)) == 0)
1310 accept = cfg->DeviceRedirection;
1311 else if (strncmp(DISP_DVC_CHANNEL_NAME, channel->channel_name,
1312 sizeof(DISP_DVC_CHANNEL_NAME)) == 0)
1313 accept = cfg->DisplayControl;
1314 else if (strncmp(RAIL_SVC_CHANNEL_NAME, channel->channel_name,
1315 sizeof(RAIL_SVC_CHANNEL_NAME)) == 0)
1316 accept = cfg->RemoteApp;
1319 WLog_DBG(TAG,
"%s [static]: %s", channel->channel_name, boolstr(accept));
1325 struct config_plugin_data* custom =
nullptr;
1326 proxyPlugin plugin = WINPR_C_ARRAY_INIT;
1328 plugin.name = config_plugin_name;
1329 plugin.description = config_plugin_desc;
1330 plugin.PluginUnload = config_plugin_unload;
1332 plugin.KeyboardEvent = config_plugin_keyboard_event;
1333 plugin.UnicodeEvent = config_plugin_unicode_event;
1334 plugin.MouseEvent = config_plugin_mouse_event;
1335 plugin.MouseExEvent = config_plugin_mouse_ex_event;
1336 plugin.ClientChannelData = config_plugin_client_channel_data;
1337 plugin.ServerChannelData = config_plugin_server_channel_data;
1338 plugin.ChannelCreate = config_plugin_channel_create;
1339 plugin.DynamicChannelCreate = config_plugin_dynamic_channel_create;
1340 plugin.userdata = userdata;
1342 custom = calloc(1,
sizeof(
struct config_plugin_data));
1346 custom->mgr = plugins_manager;
1347 custom->config = userdata;
1349 plugin.custom = custom;
1350 plugin.userdata = userdata;
1352 return plugins_manager->RegisterPlugin(plugins_manager, &plugin);
1355const char*
pf_config_get(
const proxyConfig* config,
const char* section,
const char* key)
1357 WINPR_ASSERT(config);
1358 WINPR_ASSERT(config->ini);
1359 WINPR_ASSERT(section);
1362 return IniFile_GetKeyValueString(config->ini, section, key);
void pf_server_config_free(proxyConfig *config)
pf_server_config_free Releases all resources associated with proxyConfig
proxyConfig * pf_server_config_load_file(const char *path)
pf_server_config_load_file Create a proxyConfig from a INI file found at path.
void pf_server_config_print(const proxyConfig *config)
pf_server_config_print Print the configuration to stdout
const char ** pf_config_modules(const proxyConfig *config)
pf_config_modules
const char * pf_config_required_plugin(const proxyConfig *config, size_t index)
pf_config_required_plugin
proxyConfig * pf_server_config_load_buffer(const char *buffer)
pf_server_config_load_buffer Create a proxyConfig from a memory string buffer in INI file format
size_t pf_config_modules_count(const proxyConfig *config)
pf_config_modules_count
BOOL pf_config_clone(proxyConfig **dst, const proxyConfig *config)
pf_config_clone Create a copy of the configuration
proxyConfig * server_config_load_ini(wIniFile *ini)
server_config_load_ini Create a proxyConfig from a already loaded INI file.
size_t pf_config_required_plugins_count(const proxyConfig *config)
pf_config_required_plugins_count
BOOL pf_server_config_dump(const char *file)
pf_server_config_dump Dumps a default INI configuration file
BOOL pf_config_plugin(proxyPluginsManager *plugins_manager, void *userdata)
pf_config_plugin Register a proxy plugin handling event filtering defined in the configuration.
const char * pf_config_get(const proxyConfig *config, const char *section, const char *key)
pf_config_get get a value for a section/key