From 0e7cd6a10b1802e394727e013a1cd5933c49650c Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Mon, 23 Aug 2021 20:10:33 -0400 Subject: [PATCH] During import check whether profile with same name exists Currently we construct the destination path and check whether it exists. This could miss a connection profile with same name in another directory. If a config with same name is found we set it as the destination, and ask the user for permission to overwrite. However, if the duplicate is in the global_config_dir, the behaviour is not changed -- that is, the config is imported with no further prompts. Also fix the use of same buffer as destination and source in swprintf(). It seems to work, but is not 'legal'. Signed-off-by: Selva Nair --- misc.c | 57 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/misc.c b/misc.c index b63df5e..45a6abe 100644 --- a/misc.c +++ b/misc.c @@ -648,33 +648,42 @@ ImportConfigFile(const TCHAR* source) return; } - TCHAR destination[MAX_PATH]; - _sntprintf_0(destination, _T("%s\\%s"), o.config_dir, fileName); + WCHAR destination[MAX_PATH+1]; + bool no_overwrite = TRUE; - if (EnsureDirExists(destination)) + /* profile name must be unique: check whether a config by same name exists */ + connection_t *c = GetConnByName(fileName); + if (c && wcsncmp(c->config_dir, o.config_dir, wcslen(o.config_dir)) == 0) { - _sntprintf_0(destination, _T("%s\\%s.%s"), destination, fileName, o.ext_string); - - bool no_overwrite = TRUE; - while (!CopyFile(source, destination, no_overwrite)) + /* Ask the user whether to replace the profile or not. */ + if (ShowLocalizedMsgEx(MB_YESNO, NULL, _T(PACKAGE_NAME), IDS_NFO_IMPORT_OVERWRITE, fileName) == IDNO) { - 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, NULL, _T(PACKAGE_NAME), IDS_NFO_IMPORT_OVERWRITE, fileName) == IDNO) - return; - - /* try again with overwrite allowed */ - no_overwrite = FALSE; + return; } - ShowLocalizedMsg(IDS_NFO_IMPORT_SUCCESS); - return; + no_overwrite = FALSE; + swprintf(destination, MAX_PATH, L"%ls\\%ls", c->config_dir, c->config_file); } - ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, destination); + else + { + WCHAR dest_dir[MAX_PATH+1]; + swprintf(dest_dir, MAX_PATH, L"%ls\\%ls", o.config_dir, fileName); + dest_dir[MAX_PATH] = L'\0'; + if (!EnsureDirExists(dest_dir)) + { + ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, dest_dir); + return; + } + swprintf(destination, MAX_PATH, L"%ls\\%ls.%ls", dest_dir, fileName, o.ext_string); + } + destination[MAX_PATH] = L'\0'; + + if (!CopyFile(source, destination, no_overwrite)) + { + MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Copy file <%ls> to <%ls> failed (error = %lu)", + source, destination, GetLastError()); + ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, destination); + return; + } + + ShowLocalizedMsg(IDS_NFO_IMPORT_SUCCESS); }