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>
pull/446/head
Lev Stipakov 2021-06-29 09:54:36 +03:00 committed by Selva Nair
parent 9ded7996ab
commit 78ee9b981d
3 changed files with 57 additions and 48 deletions

51
main.c
View File

@ -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);
}
}

52
misc.c
View File

@ -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);
}

2
misc.h
View File

@ -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