From 8e4183f9a91de98331eadd60625749638b55bae5 Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Mon, 23 Aug 2021 20:14:10 -0400 Subject: [PATCH] Add '--command import' command line option Import a config file from command line as `openvpn-gui.exe --command import ` The command is send to a running instance if any. Otherwise the GUI extecutable is started and the import processed. `openvpn-gui --import ` is interpreted as the same command. Signed-off-by: Selva Nair --- README.rst | 3 +++ main.c | 17 ++++++++++++++--- misc.c | 15 ++++++++++++--- openvpn-gui-res.h | 1 + options.c | 15 +++++++++++++++ res/openvpn-gui-res-en.rc | 2 ++ 6 files changed, 47 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 9b27516..75d8013 100644 --- a/README.rst +++ b/README.rst @@ -175,6 +175,9 @@ exit rescan Rescan the config folders for changes +import ``path`` + Import the config file pointed to by ``path``. + If no running instance of the GUI is found, these commands do nothing except for *--command connect config-name* which gets interpreted as *--connect config-name* diff --git a/main.c b/main.c index a3f8f8f..0ba2dbc 100644 --- a/main.c +++ b/main.c @@ -253,6 +253,10 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance, { PrintDebug(L"Instance 1: Called with --command connect xxx. Treating it as --connect xxx"); } + else if (o.action == WM_OVPN_IMPORT) + { + ; /* pass -- import is handled after Window initialization */ + } else if (o.action) { MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Called with --command when no previous instance available"); @@ -274,6 +278,7 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance, CheckIServiceStatus(TRUE); BuildFileList(); + if (!VerifyAutoConnections()) { exit(1); } @@ -464,11 +469,10 @@ HandleCopyDataMessage(const COPYDATASTRUCT *copy_data) { CloseApplication(o.hWnd); } - /* Not yet implemented - else if(copy_data->dwData == WM_OVPN_IMPORT) + else if(copy_data->dwData == WM_OVPN_IMPORT && str) { + ImportConfigFile(str); } - */ else if (copy_data->dwData == WM_OVPN_NOTIFY) { ShowTrayBalloon(L"", copy_data->lpData); @@ -523,6 +527,13 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM ShowTrayIcon(); if (o.service_only) CheckServiceStatus(); // Check if service is running or not + + /* if '--import' was specified, do it now */ + if (o.action == WM_OVPN_IMPORT && o.action_arg) + { + ImportConfigFile(o.action_arg); + } + if (!AutoStartConnections()) { SendMessage(hwnd, WM_CLOSE, 0, 0); break; diff --git a/misc.c b/misc.c index 45a6abe..9b38e96 100644 --- a/misc.c +++ b/misc.c @@ -37,6 +37,7 @@ #include "main.h" #include "misc.h" #include "main.h" +#include "openvpn_config.h" #include "openvpn-gui-res.h" /* @@ -641,19 +642,25 @@ ImportConfigFile(const TCHAR* source) _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) + if (wcsnicmp(source, o.global_config_dir, wcslen(o.global_config_dir)) == 0 + || wcsnicmp(source, o.config_dir, wcslen(o.config_dir)) == 0) { ShowLocalizedMsg(IDS_ERR_IMPORT_SOURCE, source); return; } + /* Ensure the source exists and is readable */ + if (!CheckFileAccess(source, GENERIC_READ)) + { + ShowLocalizedMsg(IDS_ERR_IMPORT_ACCESS, source); + return; + } WCHAR destination[MAX_PATH+1]; bool no_overwrite = TRUE; /* 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) + if (c && wcsnicmp(c->config_dir, o.config_dir, wcslen(o.config_dir)) == 0) { /* Ask the user whether to replace the profile or not. */ if (ShowLocalizedMsgEx(MB_YESNO, NULL, _T(PACKAGE_NAME), IDS_NFO_IMPORT_OVERWRITE, fileName) == IDNO) @@ -686,4 +693,6 @@ ImportConfigFile(const TCHAR* source) } ShowLocalizedMsg(IDS_NFO_IMPORT_SUCCESS); + /* rescan file list after import */ + BuildFileList(); } diff --git a/openvpn-gui-res.h b/openvpn-gui-res.h index e146f16..16b870c 100644 --- a/openvpn-gui-res.h +++ b/openvpn-gui-res.h @@ -337,6 +337,7 @@ #define IDS_NFO_IMPORT_SUCCESS 1903 #define IDS_NFO_IMPORT_OVERWRITE 1904 #define IDS_ERR_IMPORT_SOURCE 1905 +#define IDS_ERR_IMPORT_ACCESS 1906 /* Save password related messages */ #define IDS_NFO_DELETE_PASS 2001 diff --git a/options.c b/options.c index ab8012b..63205fc 100644 --- a/options.c +++ b/options.c @@ -120,6 +120,15 @@ add_option(options_t *options, int i, TCHAR **p) options->action_arg = p[1]; } } + else if (streq(p[0], L"import") && p[1]) + { + ++i; + /* This is interpreted directly or as a command depending + * on we are the first instance or not. So, set as an action. + */ + options->action = WM_OVPN_IMPORT; + options->action_arg = p[1]; + } else if (streq(p[0], _T("exe_path")) && p[1]) { ++i; @@ -261,6 +270,12 @@ add_option(options_t *options, int i, TCHAR **p) options->action = WM_OVPN_SHOWSTATUS; options->action_arg = p[2]; } + else if (streq(p[1], L"import") && p[2]) + { + ++i; + options->action = WM_OVPN_IMPORT; + options->action_arg = p[2]; + } else if (streq(p[1], _T("silent_connection"))) { ++i; diff --git a/res/openvpn-gui-res-en.rc b/res/openvpn-gui-res-en.rc index 693981f..2aea42b 100644 --- a/res/openvpn-gui-res-en.rc +++ b/res/openvpn-gui-res-en.rc @@ -404,6 +404,7 @@ Supported commands:\n\ exit \t\t: terminate the running GUI instance (may ask for confirmation)\n\ status cnn \t: show the status window of config ""cnn"" if connected\n\ silent_connection [0|1]\t: set the silent_connection flag on (1) or off (0)\n\ + import path \t\t: Import the config file pointed to by path\n\ \t\t\tExample: openvpn-gui.exe --command disconnect myconfig\n\ \n\ Options to override registry settings:\n\ @@ -520,6 +521,7 @@ once as Administrator to update the registry." IDS_NFO_IMPORT_SUCCESS "File imported successfully." IDS_NFO_IMPORT_OVERWRITE "A config named ""%s"" already exists. Do you want to replace it?" IDS_ERR_IMPORT_SOURCE "Cannot import file <%s> as it is already in the global or local config directory" + IDS_ERR_IMPORT_ACCESS "Cannot import <%ls> as it is missing or not readable" /* save/delete password */ IDS_NFO_DELETE_PASS "Press OK to delete saved passwords for config ""%s"""