diff --git a/CMakeLists.txt b/CMakeLists.txt index 0867fae..8678e96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE Cryptui.lib Wininet.lib Iphlpapi.lib + Pathcch.lib Ntdll.lib) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) @@ -154,6 +155,7 @@ target_link_libraries(${PROJECT_NAME_PLAP} PRIVATE Cryptui.lib Rpcrt4.lib Iphlpapi.lib + Pathcch.lib Ntdll.lib) add_dependencies(${PROJECT_NAME_PLAP} msg_header_gen) diff --git a/Makefile.am b/Makefile.am index 96c686c..8bd71a4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -129,6 +129,7 @@ openvpn_gui_LDADD = \ -lsecur32 \ -lwininet \ -liphlpapi \ + -lpathcch \ -lntdll \ $(JSON_LIBS) diff --git a/access.c b/access.c index de3e84e..0cf53d8 100644 --- a/access.c +++ b/access.c @@ -28,7 +28,8 @@ #endif #include -#include +#include +#include #include #include #include diff --git a/plap/Makefile.am b/plap/Makefile.am index 570437c..94e7c61 100644 --- a/plap/Makefile.am +++ b/plap/Makefile.am @@ -119,6 +119,7 @@ libopenvpn_plap_la_LIBADD = \ -lgdi32 \ -lrpcrt4 \ -liphlpapi \ + -lpathcch \ -lntdll libopenvpn_plap_la_LDFLAGS = -no-undefined -avoid-version -static-libgcc diff --git a/registry.c b/registry.c index 20c683a..94c627c 100644 --- a/registry.c +++ b/registry.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "main.h" #include "options.h" @@ -79,6 +80,14 @@ RegValueExists(HKEY regkey, const WCHAR *name) return (RegQueryValueEx(regkey, name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS); } +/* Add trailing slash to a path if not present */ +static BOOL +AddTrailingBackslash(WCHAR *dir, size_t nchar) +{ + HRESULT res = PathCchAddBackslash(dir, nchar); + return (res == S_OK || res == S_FALSE) ? TRUE : FALSE; +} + static int GetGlobalRegistryKeys() { @@ -106,7 +115,8 @@ GetGlobalRegistryKeys() ShowLocalizedMsg(IDS_ERR_OPEN_REGISTRY); } if (!regkey || !GetRegistryValue(regkey, _T(""), o.install_path, _countof(o.install_path)) - || _tcslen(o.install_path) == 0) + || _tcslen(o.install_path) == 0 + || !AddTrailingBackslash(o.install_path, _countof(o.install_path))) { /* error reading registry value */ if (regkey) @@ -116,33 +126,32 @@ GetGlobalRegistryKeys() /* Use a sane default value */ _sntprintf_0(o.install_path, _T("%ls"), _T("C:\\Program Files\\OpenVPN\\")); } - if (o.install_path[_tcslen(o.install_path) - 1] != _T('\\')) - { - _tcscat(o.install_path, _T("\\")); - } /* an admin-defined global config dir defined in HKLM\OpenVPN\config_dir */ if (!regkey || !GetRegistryValue( - regkey, _T("config_dir"), o.global_config_dir, _countof(o.global_config_dir))) + regkey, _T("config_dir"), o.global_config_dir, _countof(o.global_config_dir)) + || !AddTrailingBackslash(o.global_config_dir, _countof(o.global_config_dir))) { /* use default = openvpnpath\config */ - _sntprintf_0(o.global_config_dir, _T("%lsconfig"), o.install_path); + _sntprintf_0(o.global_config_dir, _T("%lsconfig\\"), o.install_path); } if (!regkey || !GetRegistryValue( - regkey, _T("autostart_config_dir"), o.config_auto_dir, _countof(o.config_auto_dir))) + regkey, _T("autostart_config_dir"), o.config_auto_dir, _countof(o.config_auto_dir)) + || !AddTrailingBackslash(o.config_auto_dir, _countof(o.config_auto_dir))) { /* use default = openvpnpath\config-auto */ - _sntprintf_0(o.config_auto_dir, L"%lsconfig-auto", o.install_path); + _sntprintf_0(o.config_auto_dir, L"%lsconfig-auto\\", o.install_path); } if (!regkey - || !GetRegistryValue(regkey, _T("log_dir"), o.global_log_dir, _countof(o.global_log_dir))) + || !GetRegistryValue(regkey, _T("log_dir"), o.global_log_dir, _countof(o.global_log_dir)) + || !AddTrailingBackslash(o.global_log_dir, _countof(o.global_log_dir))) { /* use default = openvpnpath\log */ - _sntprintf_0(o.global_log_dir, L"%lslog", o.install_path); + _sntprintf_0(o.global_log_dir, L"%lslog\\", o.install_path); } if (!regkey