From 77538dd0d04f33b6f486e35f72854604b49298c1 Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Wed, 17 Feb 2016 16:02:40 -0500 Subject: [PATCH] Support user and global config directories - Set default config directory (config_dir) to %UserProfile%\OpenVPN\config (saved and read back from HKCU\Software\OpenVPN-GUI\config_dir) - Add a global_config_dir variable read from HKLM\Software\OpenVPN\config_dir or set to OpenVPN-install-path\config - Scan both directories and their sub directories for connection profiles. In case of name conflicts config_dir gets priority over global_config_dir - Eliminate multiple warnings of duplicate configs Fixed on review (Thanks to leobasilio@gmail.com) - Fix wrongly used o.config_dir in 2 locations - Unrelated: Added missing CheckIServiceStatus to service.h Signed-off-by: Selva Nair --- main.c | 2 ++ openvpn_config.c | 52 ++++++++++++++++++++++++++------------- options.h | 1 + registry.c | 9 ++++++- res/openvpn-gui-res-en.rc | 2 +- service.h | 1 + 6 files changed, 48 insertions(+), 19 deletions(-) diff --git a/main.c b/main.c index 770fd12..3177cde 100644 --- a/main.c +++ b/main.c @@ -170,6 +170,8 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance, if (!GetRegistryKeys()) { exit(1); } + EnsureDirExists(o.config_dir); + if (!CheckVersion()) { exit(1); } diff --git a/openvpn_config.c b/openvpn_config.c index 1adb579..31a1ebe 100644 --- a/openvpn_config.c +++ b/openvpn_config.c @@ -92,7 +92,7 @@ ConfigAlreadyExists(TCHAR *newconfig) static void -AddConfigFileToList(int config, TCHAR *filename, TCHAR *config_dir) +AddConfigFileToList(int config, const TCHAR *filename, const TCHAR *config_dir) { connection_t *c = &o.conn[config]; int i; @@ -120,22 +120,18 @@ AddConfigFileToList(int config, TCHAR *filename, TCHAR *config_dir) } -void -BuildFileList() +static void +BuildFileList0(const TCHAR *config_dir, bool warn_duplicates) { WIN32_FIND_DATA find_obj; HANDLE find_handle; TCHAR find_string[MAX_PATH]; TCHAR subdir_table[MAX_CONFIG_SUBDIRS][MAX_PATH]; TCHAR fullpath[MAX_PATH]; - static int warn_no_configs = 1; int subdirs = 0; int i; - /* Reset config counter */ - o.num_configs = 0; - - _sntprintf_0(find_string, _T("%s\\*"), o.config_dir); + _sntprintf_0(find_string, _T("%s\\*"), config_dir); find_handle = FindFirstFile(find_string, &find_obj); if (find_handle == INVALID_HANDLE_VALUE) return; @@ -152,9 +148,17 @@ BuildFileList() match_t match_type = match(&find_obj, o.ext_string); if (match_type == match_file) { - _sntprintf_0(fullpath, _T("%s\\%s"), o.config_dir, find_obj.cFileName); + _sntprintf_0(fullpath, _T("%s\\%s"), config_dir, find_obj.cFileName); + + if (ConfigAlreadyExists(find_obj.cFileName)) + { + if (warn_duplicates) + ShowLocalizedMsg(IDS_ERR_CONFIG_EXIST, find_obj.cFileName); + continue; + } + if (CheckReadAccess (fullpath)) - AddConfigFileToList(o.num_configs++, find_obj.cFileName, o.config_dir); + AddConfigFileToList(o.num_configs++, find_obj.cFileName, config_dir); } else if (match_type == match_dir) { @@ -163,7 +167,7 @@ BuildFileList() && subdirs < MAX_CONFIG_SUBDIRS) { /* Add dir to dir_table */ - _sntprintf_0(subdir_table[subdirs], _T("%s\\%s"), o.config_dir, find_obj.cFileName); + _sntprintf_0(subdir_table[subdirs], _T("%s\\%s"), config_dir, find_obj.cFileName); subdirs++; } } @@ -195,7 +199,8 @@ BuildFileList() if (ConfigAlreadyExists(find_obj.cFileName)) { - ShowLocalizedMsg(IDS_ERR_CONFIG_EXIST, find_obj.cFileName); + if (warn_duplicates) + ShowLocalizedMsg(IDS_ERR_CONFIG_EXIST, find_obj.cFileName); continue; } @@ -204,9 +209,22 @@ BuildFileList() FindClose(find_handle); } - if (o.num_configs == 0 && warn_no_configs) - { - ShowLocalizedMsg(IDS_NFO_NO_CONFIGS, o.config_dir); - warn_no_configs = 0; - } +} + +void +BuildFileList() +{ + static bool issue_warnings = true; + + o.num_configs = 0; + + BuildFileList0 (o.config_dir, issue_warnings); + + if (_tcscmp (o.global_config_dir, o.config_dir)) + BuildFileList0 (o.global_config_dir, issue_warnings); + + if (o.num_configs == 0 && issue_warnings) + ShowLocalizedMsg(IDS_NFO_NO_CONFIGS); + + issue_warnings = false; } diff --git a/options.h b/options.h index d581c49..f8dda93 100644 --- a/options.h +++ b/options.h @@ -125,6 +125,7 @@ typedef struct { /* Registry values */ TCHAR exe_path[MAX_PATH]; TCHAR config_dir[MAX_PATH]; + TCHAR global_config_dir[MAX_PATH]; TCHAR ext_string[16]; TCHAR log_dir[MAX_PATH]; TCHAR priority_string[64]; diff --git a/registry.c b/registry.c index f220566..e4dac2d 100644 --- a/registry.c +++ b/registry.c @@ -73,8 +73,15 @@ GetRegistryKeys() if (openvpn_path[_tcslen(openvpn_path) - 1] != _T('\\')) _tcscat(openvpn_path, _T("\\")); + /* an admin-defined global config dir defined in HKLM\OpenVPN\config_dir */ + if (!GetRegistryValue(regkey, _T("config_dir"), o.global_config_dir, _countof(o.global_config_dir))) + { + /* use default = openvpnpath\config */ + _sntprintf_0(o.global_config_dir, _T("%sconfig"), openvpn_path); + } - _sntprintf_0(temp_path, _T("%sconfig"), openvpn_path); + /* config_dir in user's profile by default */ + _sntprintf_0(temp_path, _T("%s\\OpenVPN\\config"), profile_dir); if (!GetRegKey(_T("config_dir"), o.config_dir, temp_path, _countof(o.config_dir))) return(false); diff --git a/res/openvpn-gui-res-en.rc b/res/openvpn-gui-res-en.rc index 999086c..58f2df2 100644 --- a/res/openvpn-gui-res-en.rc +++ b/res/openvpn-gui-res-en.rc @@ -201,7 +201,7 @@ BEGIN /* OpenVPN */ IDS_ERR_MANY_CONFIGS "OpenVPN GUI does not support more than %d configs. Please contact the author if you have the need for more." - IDS_NFO_NO_CONFIGS "No readable connection profiles (config files) found at %s" + IDS_NFO_NO_CONFIGS "No readable connection profiles (config files) found" IDS_ERR_ONE_CONN_OLD_VER "You can only have one connection running at the same time when using an older version on OpenVPN than 2.0-beta6." IDS_ERR_STOP_SERV_OLD_VER "You cannot use OpenVPN GUI to start a connection while the OpenVPN Service is running (with OpenVPN 1.5/1.6). Stop OpenVPN Service first if you want to use OpenVPN GUI." IDS_ERR_CREATE_EVENT "CreateEvent failed on exit event: %s" diff --git a/service.h b/service.h index 5010d6a..dba8c83 100644 --- a/service.h +++ b/service.h @@ -23,3 +23,4 @@ int MyStartService(); int MyStopService(); int MyReStartService(); int CheckServiceStatus(); +BOOL CheckIServiceStatus();