From 78ee9b981dcd06c36d501908c2b4609fdf3fb7db Mon Sep 17 00:00:00 2001 From: Lev Stipakov <lev@openvpn.net> Date: Tue, 29 Jun 2021 09:54:36 +0300 Subject: [PATCH] URL profile import: refactor ImportConfigFile Factor out importing part (everything except file open dialog) into separate function, which can be used when importing profile from URL. Signed-off-by: Lev Stipakov <lev@openvpn.net> --- main.c | 51 ++++----------------------------------------------- misc.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- misc.h | 2 ++ 3 files changed, 57 insertions(+), 48 deletions(-) diff --git a/main.c b/main.c index 75320f5..ad31e8d 100644 --- a/main.c +++ b/main.c @@ -65,7 +65,7 @@ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); static void ShowSettingsDialog(); void CloseApplication(HWND hwnd); -void ImportConfigFile(); +void ImportConfigFileFromDisk(); void ImportConfigFromAS(); /* Class name and window title */ @@ -543,7 +543,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM /* we first check global menu items which do not require a connnection index */ if (LOWORD(wParam) == IDM_IMPORT_FILE) { - ImportConfigFile(); + ImportConfigFileFromDisk(); } else if (LOWORD(wParam) == IDM_IMPORT_AS) { ImportConfigFromAS(); @@ -756,9 +756,8 @@ CloseApplication(HWND hwnd) } void -ImportConfigFile() +ImportConfigFileFromDisk() { - TCHAR filter[2*_countof(o.ext_string)+5]; _sntprintf_0(filter, _T("*.%s%c*.%s%c"), o.ext_string, _T('\0'), o.ext_string, _T('\0')); @@ -781,49 +780,7 @@ ImportConfigFile() if (GetOpenFileName(&fn)) { - - TCHAR destination[MAX_PATH]; - PTCHAR fileName = source + fn.nFileOffset; - - /* check if the source points to the config_dir */ - if (wcsncmp(source, o.global_config_dir, wcslen(o.global_config_dir)) == 0 - || wcsncmp(source, o.config_dir, wcslen(o.config_dir)) == 0) - { - ShowLocalizedMsg(IDS_ERR_IMPORT_SOURCE, source); - return; - } - - _sntprintf_0(destination, _T("%s\\%s"), o.config_dir, fileName); - - destination[_tcslen(destination) - _tcslen(o.ext_string) - 1] = _T('\0'); - - if (EnsureDirExists(destination)) - { - - _sntprintf_0(destination, _T("%s\\%s"), destination, fileName); - - bool no_overwrite = TRUE; - while(!CopyFile(source, destination, no_overwrite)) - { - if (GetLastError() != ERROR_FILE_EXISTS || !no_overwrite) - { - MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Copy file <%s> to <%s> failed (error = %lu)", - source, destination, GetLastError()); - ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, destination); - return; - } - - /* A file with same name exists. Ask the user whether to replace or not. */ - if (ShowLocalizedMsgEx(MB_YESNO, _T(PACKAGE_NAME), IDS_NFO_IMPORT_OVERWRITE, fileName) == IDNO) - return; - - /* try again with overwrite allowed */ - no_overwrite = FALSE; - } - ShowLocalizedMsg(IDS_NFO_IMPORT_SUCCESS); - return; - } - ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, destination); + ImportConfigFile(source); } } diff --git a/misc.c b/misc.c index 2c5c258..65a09a7 100644 --- a/misc.c +++ b/misc.c @@ -31,11 +31,13 @@ #include <malloc.h> #include <shellapi.h> +#include "localization.h" #include "options.h" #include "manage.h" #include "main.h" #include "misc.h" #include "main.h" +#include "openvpn-gui-res.h" /* * Helper function to do base64 conversion through CryptoAPI @@ -401,7 +403,7 @@ DpiSetScale(options_t* options, UINT dpix) * Check user has admin rights * Taken from https://msdn.microsoft.com/en-us/library/windows/desktop/aa376389(v=vs.85).aspx * Returns true if the calling process token has the local Administrators group enabled - * in its SID. Assumes the caller is not impersonating and has access to open its own + * in its SID. Assumes the caller is not impersonating and has access to open its own * process token. */ BOOL IsUserAdmin(VOID) @@ -632,3 +634,51 @@ open_url(const wchar_t *url) } return true; } + +extern options_t o; + +void +ImportConfigFile(const TCHAR* source) +{ + TCHAR fileName[MAX_PATH] = _T(""); + TCHAR ext[MAX_PATH] = _T(""); + _wsplitpath(source, NULL, NULL, fileName, ext); + + /* check if the source points to the config_dir */ + if (wcsncmp(source, o.global_config_dir, wcslen(o.global_config_dir)) == 0 + || wcsncmp(source, o.config_dir, wcslen(o.config_dir)) == 0) + { + ShowLocalizedMsg(IDS_ERR_IMPORT_SOURCE, source); + return; + } + + TCHAR destination[MAX_PATH]; + _sntprintf_0(destination, _T("%s\\%s"), o.config_dir, fileName); + + if (EnsureDirExists(destination)) + { + _sntprintf_0(destination, _T("%s\\%s.%s"), destination, fileName, o.ext_string); + + bool no_overwrite = TRUE; + while (!CopyFile(source, destination, no_overwrite)) + { + if (GetLastError() != ERROR_FILE_EXISTS || !no_overwrite) + { + MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Copy file <%s> to <%s> failed (error = %lu)", + source, destination, GetLastError()); + ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, destination); + return; + } + + /* A file with same name exists. Ask the user whether to replace or not. */ + if (ShowLocalizedMsgEx(MB_YESNO, _T(PACKAGE_NAME), IDS_NFO_IMPORT_OVERWRITE, fileName) == IDNO) + return; + + /* try again with overwrite allowed */ + no_overwrite = FALSE; + } + ShowLocalizedMsg(IDS_NFO_IMPORT_SUCCESS); + return; + } + ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, destination); +} diff --git a/misc.h b/misc.h index 74a3e30..46cd6df 100644 --- a/misc.h +++ b/misc.h @@ -71,4 +71,6 @@ DWORD md_final(md_ctx *ctx, BYTE *md); /* Open specified http/https URL using ShellExecute. */ BOOL open_url(const wchar_t *url); +void ImportConfigFile(const TCHAR* path); + #endif