From 4437ce7a8cd7e0508d26fede84fcaf5d040a8c05 Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Thu, 4 Feb 2016 23:28:09 -0500 Subject: [PATCH] Change default log file location to a OpenVPN/log in user's profile directory Change the default log file location to OpenVPN/log in user's profile directory to support running without admin privileges using the interactive service. The registry keys are moved to HKCU from HKLM to allow for user-specific settings as well as to avoid the need for running the GUI as admin at the first instance. Signed-off-by: Selva Nair --- openvpn-gui-res.h | 1 + registry.c | 18 ++++++++++++------ res/openvpn-gui-res-en.rc | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/openvpn-gui-res.h b/openvpn-gui-res.h index 86057bb..8377ce3 100644 --- a/openvpn-gui-res.h +++ b/openvpn-gui-res.h @@ -239,5 +239,6 @@ #define IDS_ERR_OPEN_WRITE_REG 1810 #define IDS_ERR_READ_SET_KEY 1811 #define IDS_ERR_WRITE_REGVALUE 1812 +#define IDS_ERR_GET_PROFILE_DIR 1813 #endif diff --git a/registry.c b/registry.c index 70d8149..923c7dd 100644 --- a/registry.c +++ b/registry.c @@ -42,6 +42,7 @@ GetRegistryKeys() TCHAR windows_dir[MAX_PATH]; TCHAR temp_path[MAX_PATH]; TCHAR openvpn_path[MAX_PATH]; + TCHAR profile_dir[MAX_PATH]; HKEY regkey; if (!GetWindowsDirectory(windows_dir, _countof(windows_dir))) { @@ -50,6 +51,11 @@ GetRegistryKeys() return(false); } + if (SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, profile_dir) != S_OK) { + ShowLocalizedMsg(IDS_ERR_GET_PROFILE_DIR); + return(false); + } + /* Get path to OpenVPN installation. */ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\OpenVPN"), 0, KEY_READ, ®key) != ERROR_SUCCESS) @@ -78,7 +84,7 @@ GetRegistryKeys() if (!GetRegKey(_T("exe_path"), o.exe_path, temp_path, _countof(o.exe_path))) return(false); - _sntprintf_0(temp_path, _T("%slog"), openvpn_path); + _sntprintf_0(temp_path, _T("%s\\OpenVPN\\log"), profile_dir); if (!GetRegKey(_T("log_dir"), o.log_dir, temp_path, _countof(o.log_dir))) return(false); @@ -178,7 +184,7 @@ int GetRegKey(const TCHAR name[], TCHAR *data, const TCHAR default_data[], DWORD return(true); } - status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, + status = RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\OpenVPN-GUI"), 0, KEY_READ, @@ -186,7 +192,7 @@ int GetRegKey(const TCHAR name[], TCHAR *data, const TCHAR default_data[], DWORD if (status != ERROR_SUCCESS) { - if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, + if (RegCreateKeyEx(HKEY_CURRENT_USER, _T("Software\\OpenVPN-GUI"), 0, _T(""), @@ -197,7 +203,7 @@ int GetRegKey(const TCHAR name[], TCHAR *data, const TCHAR default_data[], DWORD &dwDispos) != ERROR_SUCCESS) { /* error creating registry key */ - ShowLocalizedMsg(IDS_ERR_CREATE_REG_KEY); + ShowLocalizedMsg(IDS_ERR_CREATE_REG_HKCU_KEY, _T("OpenVPN-GUI")); return(false); } } @@ -208,7 +214,7 @@ int GetRegKey(const TCHAR name[], TCHAR *data, const TCHAR default_data[], DWORD if (status != ERROR_SUCCESS || type != REG_SZ) { /* key did not exist - set default value */ - status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, + status = RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\OpenVPN-GUI"), 0, KEY_READ | KEY_WRITE, @@ -216,7 +222,7 @@ int GetRegKey(const TCHAR name[], TCHAR *data, const TCHAR default_data[], DWORD if (status != ERROR_SUCCESS) { /* can't open registry for writing */ - ShowLocalizedMsg(IDS_ERR_OPEN_WRITE_REG); + ShowLocalizedMsg(IDS_ERR_WRITE_REGVALUE, _T("OpenVPN-GUI"), name); return(false); } if(!SetRegistryValue(openvpn_key_write, name, default_data)) diff --git a/res/openvpn-gui-res-en.rc b/res/openvpn-gui-res-en.rc index 490efcb..431f4bd 100644 --- a/res/openvpn-gui-res-en.rc +++ b/res/openvpn-gui-res-en.rc @@ -325,6 +325,7 @@ BEGIN /* registry */ IDS_ERR_GET_WINDOWS_DIR "Error getting Windows Directory." + IDS_ERR_GET_PROFILE_DIR "Error getting User Profile Directory." IDS_ERR_GET_PROGRAM_DIR "Error getting ""Program"" folder name." IDS_ERR_OPEN_REGISTRY "Error opening registry for reading (HKLM\\SOFTWARE\\OpenVPN).\n " \ "OpenVPN is probably not installed"