2008-12-18 11:08:35 +00:00
|
|
|
/*
|
|
|
|
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Mathias Sundman <mathias@nilings.se>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program (see the file COPYING included with this
|
|
|
|
* distribution); if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2012-03-28 10:34:47 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2010-08-13 15:42:23 +00:00
|
|
|
|
2016-02-26 17:21:23 +00:00
|
|
|
#if !defined (UNICODE)
|
|
|
|
#error UNICODE and _UNICODE must be defined. This version only supports unicode builds.
|
|
|
|
#endif
|
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
#include <windows.h>
|
2020-09-14 18:47:36 +00:00
|
|
|
#include <versionhelpers.h>
|
2008-12-18 11:08:35 +00:00
|
|
|
#include <shlwapi.h>
|
2013-04-25 15:53:16 +00:00
|
|
|
#include <wtsapi32.h>
|
2012-03-28 10:34:47 +00:00
|
|
|
#include <prsht.h>
|
2016-02-17 00:20:53 +00:00
|
|
|
#include <commdlg.h>
|
2010-03-11 21:58:45 +00:00
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
#include "tray.h"
|
|
|
|
#include "openvpn.h"
|
|
|
|
#include "openvpn_config.h"
|
|
|
|
#include "viewlog.h"
|
|
|
|
#include "service.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "passphrase.h"
|
|
|
|
#include "proxy.h"
|
|
|
|
#include "registry.h"
|
|
|
|
#include "openvpn-gui-res.h"
|
2009-01-18 20:59:52 +00:00
|
|
|
#include "localization.h"
|
2010-08-13 15:42:23 +00:00
|
|
|
#include "manage.h"
|
2012-07-26 07:27:59 +00:00
|
|
|
#include "misc.h"
|
2016-03-21 12:43:41 +00:00
|
|
|
#include "save_pass.h"
|
Parse and display messages received by echo msg commands
Process four new echo commands to construct messages to be
displayed to the user:
echo msg message-text
echo msg-n message-text
echo msg-window message-title
echo msg-notify message-title
Note: All rules of push and echo processing apply and determine
what is received as echo commands by the GUI. In addition,
'url-encoded' characters (% followed by two hex digits) are
decoded and displayed.
The message is constructed in the GUI by concatenating the text
specified in one or more "echo msg text" or "echo msg-n text"
commands. In case of "echo msg text" text is appended with a new
line. An empty text in this case will
just add a new line.
The message ends and gets displayed when one of the following
are receieved:
echo msg-window title
echo msg-notify title
where "title" becomes the title of the message window. In case of
msg-window, a modeless window shows the message, in the latter case
a notification balloon is shown.
Example: when pushed from the server:
push "echo msg I say let the world go to hell%2C"
push "echo msg I must have my cup of tea."
push "echo msg-window Notes from the underground"
will display a modeless window with title
"Notes from the underground" and a two line body
--
I say let the world go to hell,
I must have my cup of tea.
--
Note that the message itself is not quoted in the above examples
and so it relies on the server's option-parser combining
individual words into a space separated string. Number of words
on a line is limited by the maximum number of parameters allowed
in openvpn commands (16). This limitation may be avoided by quoting
the text that follows so that the option parser sees it as one
parameter.
The comma character is not allowed in pushed strings, so
it has to be sent encoded as %2C as shown above.
Such encoding of arbitrary bytes is suppored. For example,
newlines may be embedded as %0A, though discouraged. Instead
use multiple "echo msg" commands to separate lines by new line.
An example with embedded spaces and multiple lines concatenated
without a new line in between (note use of single quotes):
push "echo msg-n I swear to you gentlemen%2C that to be"
push "echo msg-n ' overly conscious is a sickness%2C ' "
push "echo msg-n a real%2C thorough sickness."
push "echo msg-notify Quote of the Day"
will show up as a notification that displays for an
OS-dependent interval as:
--
Quote of the Day
I swear to you gentlemen, that to be overly conscious
is a sickness, a real, thorough sickness.
--
where the location of the line break is automatically determined
by the notification API and is OS version-dependent.
Commands like "echo msg ..." in the config file are also
processed the same way. It gets displayed when the GUI connects
to the management interface and receives all pending echo.
Pushed message(s) get displayed when the client daemon
processes push-reply and passes on echo directives to the
GUI.
TODO: The actual window that displays the messages is
implemented in the next commit.
Signed-off-by: Selva Nair <selva.nair@gmail.com>
2017-11-26 22:38:11 +00:00
|
|
|
#include "echo.h"
|
2021-06-28 13:05:23 +00:00
|
|
|
#include "as.h"
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
#ifndef DISABLE_CHANGE_PASSWORD
|
2022-02-03 02:15:45 +00:00
|
|
|
#include <openssl/crypto.h>
|
2008-12-18 11:08:35 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-27 18:10:49 +00:00
|
|
|
#define OVPN_EXITCODE_ERROR 1
|
|
|
|
#define OVPN_EXITCODE_TIMEOUT 2
|
|
|
|
#define OVPN_EXITCODE_NOTREADY 3
|
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
/* Declare Windows procedure */
|
|
|
|
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
|
2009-01-26 21:07:04 +00:00
|
|
|
static void ShowSettingsDialog();
|
2008-12-18 11:08:35 +00:00
|
|
|
void CloseApplication(HWND hwnd);
|
2021-06-29 06:54:36 +00:00
|
|
|
void ImportConfigFileFromDisk();
|
2021-06-28 13:05:23 +00:00
|
|
|
void ImportConfigFromAS();
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
/* Class name and window title */
|
2010-03-21 09:07:14 +00:00
|
|
|
TCHAR szClassName[ ] = _T("OpenVPN-GUI");
|
|
|
|
TCHAR szTitleText[ ] = _T("OpenVPN");
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
/* Options structure */
|
2010-04-09 04:01:30 +00:00
|
|
|
options_t o;
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2017-01-18 07:46:47 +00:00
|
|
|
/* Workaround for ASLR on Windows */
|
|
|
|
__declspec(dllexport) char aslr_workaround;
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
static int
|
|
|
|
VerifyAutoConnections()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2018-04-20 02:19:10 +00:00
|
|
|
for (i = 0; i < o.num_auto_connect; i++)
|
2010-08-13 15:42:23 +00:00
|
|
|
{
|
2017-06-11 20:29:46 +00:00
|
|
|
if (GetConnByName(o.auto_connect[i]) == NULL)
|
2010-08-13 15:42:23 +00:00
|
|
|
{
|
|
|
|
/* autostart config not found */
|
|
|
|
ShowLocalizedMsg(IDS_ERR_AUTOSTART_CONF, o.auto_connect[i]);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-02-27 18:10:49 +00:00
|
|
|
/*
|
|
|
|
* Send a copydata message corresponding to any --command action option specified
|
|
|
|
* to the running instance and return success or error.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
NotifyRunningInstance()
|
|
|
|
{
|
|
|
|
/* Check if a previous instance has a window initialized
|
|
|
|
* Even if we are not the first instance this may return null
|
|
|
|
* if the previous instance has not fully started up
|
|
|
|
*/
|
|
|
|
HANDLE hwnd_master = FindWindow (szClassName, NULL);
|
|
|
|
int exit_code = 0;
|
|
|
|
if (hwnd_master)
|
|
|
|
{
|
|
|
|
/* GUI up and running -- send a message if any action is pecified,
|
|
|
|
else show the balloon */
|
|
|
|
COPYDATASTRUCT config_data = {0};
|
|
|
|
int timeout = 30*1000; /* 30 seconds */
|
|
|
|
if (!o.action)
|
|
|
|
{
|
|
|
|
o.action = WM_OVPN_NOTIFY;
|
|
|
|
o.action_arg = LoadLocalizedString(IDS_NFO_CLICK_HERE_TO_START);
|
|
|
|
}
|
|
|
|
config_data.dwData = o.action;
|
|
|
|
if (o.action_arg)
|
|
|
|
{
|
|
|
|
config_data.cbData = (wcslen(o.action_arg)+1)*sizeof(o.action_arg[0]);
|
|
|
|
config_data.lpData = (void *) o.action_arg;
|
|
|
|
}
|
2021-10-24 01:57:11 +00:00
|
|
|
PrintDebug(L"Instance 2: called with action %d : %ls", o.action, o.action_arg);
|
2016-02-27 18:10:49 +00:00
|
|
|
if (!SendMessageTimeout (hwnd_master, WM_COPYDATA, 0,
|
|
|
|
(LPARAM) &config_data, 0, timeout, NULL))
|
2018-04-01 16:45:07 +00:00
|
|
|
{
|
|
|
|
DWORD error = GetLastError();
|
|
|
|
if (error == ERROR_TIMEOUT)
|
|
|
|
{
|
|
|
|
exit_code = OVPN_EXITCODE_TIMEOUT;
|
|
|
|
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Sending command to running instance timed out.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
exit_code = OVPN_EXITCODE_ERROR;
|
|
|
|
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Sending command to running instance failed (error = %lu).",
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
}
|
2016-02-27 18:10:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-01 16:45:07 +00:00
|
|
|
/* An instance is already running but its main window not yet initialized */
|
2016-02-27 18:10:49 +00:00
|
|
|
exit_code = OVPN_EXITCODE_NOTREADY;
|
2018-04-01 16:45:07 +00:00
|
|
|
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Previous instance not yet ready to accept commands. "
|
|
|
|
"Try again later.");
|
2016-02-27 18:10:49 +00:00
|
|
|
}
|
2018-04-01 16:45:07 +00:00
|
|
|
|
2016-02-27 18:10:49 +00:00
|
|
|
return exit_code;
|
|
|
|
}
|
2010-08-13 15:42:23 +00:00
|
|
|
|
2012-03-28 10:34:47 +00:00
|
|
|
int WINAPI _tWinMain (HINSTANCE hThisInstance,
|
2008-12-18 11:08:35 +00:00
|
|
|
UNUSED HINSTANCE hPrevInstance,
|
2012-03-28 10:34:47 +00:00
|
|
|
UNUSED LPTSTR lpszArgument,
|
2008-12-18 11:08:35 +00:00
|
|
|
UNUSED int nCmdShow)
|
|
|
|
{
|
|
|
|
MSG messages; /* Here messages to the application are saved */
|
|
|
|
WNDCLASSEX wincl; /* Data structure for the windowclass */
|
|
|
|
DWORD shell32_version;
|
2016-02-27 18:10:49 +00:00
|
|
|
BOOL first_instance = TRUE;
|
|
|
|
/* a session local semaphore to detect second instance */
|
|
|
|
HANDLE session_semaphore = InitSemaphore(L"Local\\"PACKAGE_NAME);
|
|
|
|
|
2020-12-29 21:36:28 +00:00
|
|
|
srand(time(NULL));
|
2016-02-27 18:10:49 +00:00
|
|
|
/* try to lock the semaphore, else we are not the first instance */
|
|
|
|
if (session_semaphore &&
|
2018-04-01 16:37:21 +00:00
|
|
|
WaitForSingleObject(session_semaphore, 200) != WAIT_OBJECT_0)
|
2016-02-27 18:10:49 +00:00
|
|
|
{
|
|
|
|
first_instance = FALSE;
|
|
|
|
}
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
/* Initialize handlers for manangement interface notifications */
|
|
|
|
mgmt_rtmsg_handler handler[] = {
|
2021-05-13 08:27:47 +00:00
|
|
|
{ ready_, OnReady },
|
|
|
|
{ hold_, OnHold },
|
|
|
|
{ log_, OnLogLine },
|
|
|
|
{ state_, OnStateChange },
|
|
|
|
{ password_, OnPassword },
|
|
|
|
{ proxy_, OnProxy },
|
|
|
|
{ stop_, OnStop },
|
|
|
|
{ needok_, OnNeedOk },
|
|
|
|
{ needstr_, OnNeedStr },
|
|
|
|
{ echo_, OnEcho },
|
|
|
|
{ bytecount_,OnByteCount },
|
2021-05-31 20:13:10 +00:00
|
|
|
{ infomsg_, OnInfoMsg },
|
2018-04-18 02:39:12 +00:00
|
|
|
{ timeout_, OnTimeout },
|
2010-08-13 15:42:23 +00:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
InitManagement(handler);
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
/* initialize options to default state */
|
2010-04-09 04:01:30 +00:00
|
|
|
InitOptions(&o);
|
2018-04-01 16:37:21 +00:00
|
|
|
if (first_instance)
|
|
|
|
o.session_semaphore = session_semaphore;
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* Open debug file for output */
|
2016-02-26 17:21:23 +00:00
|
|
|
if (!(o.debug_fp = _wfopen(DEBUG_FILE, L"a+,ccs=UTF-8")))
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
|
|
|
/* can't open debug file */
|
2010-03-21 09:07:14 +00:00
|
|
|
ShowLocalizedMsg(IDS_ERR_OPEN_DEBUG_FILE, DEBUG_FILE);
|
2008-12-18 11:08:35 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2021-10-24 01:57:11 +00:00
|
|
|
PrintDebug(_T("Starting OpenVPN GUI v%hs"), PACKAGE_VERSION);
|
2008-12-18 11:08:35 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
o.hInstance = hThisInstance;
|
|
|
|
|
2010-03-21 09:07:14 +00:00
|
|
|
if(!GetModuleHandle(_T("RICHED20.DLL")))
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2010-03-21 09:07:14 +00:00
|
|
|
LoadLibrary(_T("RICHED20.DLL"));
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* can't load riched20.dll */
|
2010-03-21 09:07:14 +00:00
|
|
|
ShowLocalizedMsg(IDS_ERR_LOAD_RICHED20);
|
2008-12-18 11:08:35 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check version of shell32.dll */
|
2010-03-21 09:07:14 +00:00
|
|
|
shell32_version=GetDllVersion(_T("shell32.dll"));
|
2008-12-18 11:08:35 +00:00
|
|
|
if (shell32_version < PACKVERSION(5,0))
|
|
|
|
{
|
|
|
|
/* shell32.dll version to low */
|
2010-03-21 09:07:14 +00:00
|
|
|
ShowLocalizedMsg(IDS_ERR_SHELL_DLL_VERSION, shell32_version);
|
2008-12-18 11:08:35 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2012-02-29 20:22:17 +00:00
|
|
|
PrintDebug(_T("Shell32.dll version: 0x%lx"), shell32_version);
|
2008-12-18 11:08:35 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-27 18:10:49 +00:00
|
|
|
if (first_instance)
|
|
|
|
UpdateRegistry(); /* Checks version change and update keys/values */
|
2016-07-09 19:14:36 +00:00
|
|
|
|
|
|
|
GetRegistryKeys();
|
|
|
|
/* Parse command-line options */
|
|
|
|
ProcessCommandLine(&o, GetCommandLine());
|
|
|
|
|
2016-02-17 21:02:40 +00:00
|
|
|
EnsureDirExists(o.config_dir);
|
|
|
|
|
2016-02-27 18:10:49 +00:00
|
|
|
if (!first_instance)
|
|
|
|
{
|
|
|
|
int res = NotifyRunningInstance();
|
|
|
|
exit(res);
|
|
|
|
}
|
|
|
|
else if (o.action == WM_OVPN_START)
|
|
|
|
{
|
|
|
|
PrintDebug(L"Instance 1: Called with --command connect xxx. Treating it as --connect xxx");
|
|
|
|
}
|
2021-08-24 00:14:10 +00:00
|
|
|
else if (o.action == WM_OVPN_IMPORT)
|
|
|
|
{
|
|
|
|
; /* pass -- import is handled after Window initialization */
|
|
|
|
}
|
2016-02-27 18:10:49 +00:00
|
|
|
else if (o.action)
|
|
|
|
{
|
2018-04-01 16:45:07 +00:00
|
|
|
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Called with --command when no previous instance available");
|
2016-02-27 18:10:49 +00:00
|
|
|
exit(OVPN_EXITCODE_ERROR);
|
|
|
|
}
|
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
if (!CheckVersion()) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-08-13 15:42:23 +00:00
|
|
|
|
2012-07-26 07:27:59 +00:00
|
|
|
if (!EnsureDirExists(o.log_dir))
|
|
|
|
{
|
|
|
|
ShowLocalizedMsg(IDS_ERR_CREATE_PATH, _T("log_dir"), o.log_dir);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:47:36 +00:00
|
|
|
BOOL use_iservice = (o.iservice_admin && IsWindows7OrGreater()) || !IsUserAdmin();
|
|
|
|
if (use_iservice && strtod(o.ovpn_version, NULL) > 2.3 && !o.silent_connection)
|
2016-02-26 17:21:23 +00:00
|
|
|
CheckIServiceStatus(TRUE);
|
2016-02-16 20:49:13 +00:00
|
|
|
|
2022-07-03 20:57:40 +00:00
|
|
|
CheckServiceStatus(); /* Check if automatic service is running or not */
|
2010-03-21 20:28:35 +00:00
|
|
|
BuildFileList();
|
2021-08-24 00:14:10 +00:00
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
if (!VerifyAutoConnections()) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-02-27 18:10:49 +00:00
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
GetProxyRegistrySettings();
|
|
|
|
|
|
|
|
#ifndef DISABLE_CHANGE_PASSWORD
|
|
|
|
/* Initialize OpenSSL */
|
2022-02-03 02:15:45 +00:00
|
|
|
set_openssl_env_vars();
|
|
|
|
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
|
|
|
|
#endif /* DISABLE_CHANGE_PASSWORD */
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
/* The Window structure */
|
|
|
|
wincl.hInstance = hThisInstance;
|
|
|
|
wincl.lpszClassName = szClassName;
|
|
|
|
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
|
|
|
|
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
|
|
|
|
wincl.cbSize = sizeof (WNDCLASSEX);
|
|
|
|
|
|
|
|
/* Use default icon and mouse-pointer */
|
2009-02-04 15:54:37 +00:00
|
|
|
wincl.hIcon = LoadLocalizedIcon(ID_ICO_APP);
|
|
|
|
wincl.hIconSm = LoadLocalizedIcon(ID_ICO_APP);
|
2008-12-18 11:08:35 +00:00
|
|
|
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
|
|
|
|
wincl.lpszMenuName = NULL; /* No menu */
|
|
|
|
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
|
|
|
|
wincl.cbWndExtra = 0; /* structure or the window instance */
|
|
|
|
/* Use Windows's default color as the background of the window */
|
|
|
|
wincl.hbrBackground = (HBRUSH) COLOR_3DSHADOW; //COLOR_BACKGROUND;
|
|
|
|
|
|
|
|
/* Register the window class, and if it fails quit the program */
|
|
|
|
if (!RegisterClassEx (&wincl))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* The class is registered, let's create the program*/
|
2012-03-12 17:58:38 +00:00
|
|
|
CreateWindowEx (
|
2008-12-18 11:08:35 +00:00
|
|
|
0, /* Extended possibilites for variation */
|
|
|
|
szClassName, /* Classname */
|
|
|
|
szTitleText, /* Title Text */
|
|
|
|
WS_OVERLAPPEDWINDOW, /* default window */
|
|
|
|
(int)CW_USEDEFAULT, /* Windows decides the position */
|
|
|
|
(int)CW_USEDEFAULT, /* where the window ends up on the screen */
|
|
|
|
230, /* The programs width */
|
|
|
|
200, /* and height in pixels */
|
|
|
|
HWND_DESKTOP, /* The window is a child-window to desktop */
|
|
|
|
NULL, /* No menu */
|
|
|
|
hThisInstance, /* Program Instance handler */
|
|
|
|
NULL /* No Window Creation data */
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/* Run the message loop. It will run until GetMessage() returns 0 */
|
|
|
|
while (GetMessage (&messages, NULL, 0, 0))
|
|
|
|
{
|
|
|
|
TranslateMessage(&messages);
|
|
|
|
DispatchMessage(&messages);
|
|
|
|
}
|
|
|
|
|
2018-04-01 16:37:21 +00:00
|
|
|
CloseSemaphore(o.session_semaphore);
|
|
|
|
o.session_semaphore = NULL; /* though we're going to die.. */
|
2018-04-01 16:45:07 +00:00
|
|
|
if (o.event_log)
|
|
|
|
DeregisterEventSource(o.event_log);
|
2018-04-01 16:37:21 +00:00
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
/* The program return-value is 0 - The value that PostQuitMessage() gave */
|
|
|
|
return messages.wParam;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
static void
|
|
|
|
StopAllOpenVPN()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-07-03 20:30:31 +00:00
|
|
|
/* Stop all connections started by us -- we leave persistent ones
|
|
|
|
* at their current state. Use the disconnect menu to put them into
|
|
|
|
* hold state before exit, if desired.
|
|
|
|
*/
|
2010-08-13 15:42:23 +00:00
|
|
|
for (i = 0; i < o.num_configs; i++)
|
|
|
|
{
|
2022-07-08 00:49:00 +00:00
|
|
|
if (o.conn[i].state != disconnected && o.conn[i].state != detached)
|
2022-07-03 20:30:31 +00:00
|
|
|
{
|
|
|
|
if (o.conn[i].flags & FLAG_DAEMON_PERSISTENT)
|
|
|
|
{
|
|
|
|
DetachOpenVPN(&o.conn[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StopOpenVPN(&o.conn[i]);
|
|
|
|
}
|
|
|
|
}
|
2010-08-13 15:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for all connections to terminate (Max 5 sec) */
|
|
|
|
for (i = 0; i < 20; i++, Sleep(250))
|
|
|
|
{
|
2022-07-08 00:49:00 +00:00
|
|
|
if (CountConnState(disconnected) + CountConnState(detached) == o.num_configs)
|
2010-08-13 15:42:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
AutoStartConnections()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < o.num_configs; i++)
|
|
|
|
{
|
2022-07-21 00:58:04 +00:00
|
|
|
if (o.conn[i].auto_connect && !(o.conn[i].flags & FLAG_DAEMON_PERSISTENT))
|
2010-08-13 15:42:23 +00:00
|
|
|
StartOpenVPN(&o.conn[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-25 15:53:16 +00:00
|
|
|
static void
|
|
|
|
ResumeConnections()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < o.num_configs; i++) {
|
|
|
|
/* Restart suspend connections */
|
|
|
|
if (o.conn[i].state == suspended)
|
|
|
|
StartOpenVPN(&o.conn[i]);
|
|
|
|
|
|
|
|
/* If some connection never reached SUSPENDED state */
|
|
|
|
if (o.conn[i].state == suspending)
|
|
|
|
StopOpenVPN(&o.conn[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 18:10:49 +00:00
|
|
|
static int
|
|
|
|
HandleCopyDataMessage(const COPYDATASTRUCT *copy_data)
|
|
|
|
{
|
|
|
|
WCHAR *str = NULL;
|
|
|
|
connection_t *c = NULL;
|
2021-10-24 01:57:11 +00:00
|
|
|
PrintDebug (L"WM_COPYDATA message received. (dwData: %lu, cbData: %lu, lpData: %ls)",
|
2016-02-27 18:10:49 +00:00
|
|
|
copy_data->dwData, copy_data->cbData, copy_data->lpData);
|
|
|
|
if (copy_data->cbData >= sizeof(WCHAR) && copy_data->lpData)
|
|
|
|
{
|
|
|
|
str = (WCHAR*) copy_data->lpData;
|
|
|
|
str[copy_data->cbData/sizeof(WCHAR)-1] = L'\0'; /* in case not nul-terminated */
|
|
|
|
c = GetConnByName(str);
|
|
|
|
}
|
|
|
|
if(copy_data->dwData == WM_OVPN_START && c)
|
|
|
|
{
|
|
|
|
if (!o.silent_connection)
|
|
|
|
ForceForegroundWindow(o.hWnd);
|
|
|
|
StartOpenVPN(c);
|
|
|
|
}
|
|
|
|
else if(copy_data->dwData == WM_OVPN_STOP && c)
|
|
|
|
StopOpenVPN(c);
|
|
|
|
else if(copy_data->dwData == WM_OVPN_RESTART && c)
|
|
|
|
{
|
|
|
|
if (!o.silent_connection)
|
|
|
|
ForceForegroundWindow(o.hWnd);
|
|
|
|
RestartOpenVPN(c);
|
|
|
|
}
|
2019-03-13 09:37:58 +00:00
|
|
|
else if(copy_data->dwData == WM_OVPN_SHOWSTATUS && c && c->hwndStatus)
|
2016-02-27 18:10:49 +00:00
|
|
|
{
|
|
|
|
ForceForegroundWindow(o.hWnd);
|
|
|
|
ShowWindow(c->hwndStatus, SW_SHOW);
|
|
|
|
}
|
|
|
|
else if(copy_data->dwData == WM_OVPN_STOPALL)
|
|
|
|
StopAllOpenVPN();
|
|
|
|
else if(copy_data->dwData == WM_OVPN_SILENT && str)
|
|
|
|
{
|
|
|
|
if (_wtoi(str) == 0)
|
|
|
|
o.silent_connection = 0;
|
|
|
|
else
|
|
|
|
o.silent_connection = 1;
|
|
|
|
}
|
|
|
|
else if(copy_data->dwData == WM_OVPN_EXIT)
|
|
|
|
{
|
|
|
|
CloseApplication(o.hWnd);
|
|
|
|
}
|
2021-08-24 00:14:10 +00:00
|
|
|
else if(copy_data->dwData == WM_OVPN_IMPORT && str)
|
2016-02-27 18:10:49 +00:00
|
|
|
{
|
2021-11-17 04:11:26 +00:00
|
|
|
ImportConfigFile(str, true); /* prompt user */
|
2016-02-27 18:10:49 +00:00
|
|
|
}
|
|
|
|
else if (copy_data->dwData == WM_OVPN_NOTIFY)
|
|
|
|
{
|
|
|
|
ShowTrayBalloon(L"", copy_data->lpData);
|
|
|
|
}
|
2019-11-16 15:40:50 +00:00
|
|
|
else if (copy_data->dwData == WM_OVPN_RESCAN)
|
|
|
|
{
|
|
|
|
OnNotifyTray(WM_OVPN_RESCAN);
|
|
|
|
}
|
2016-02-27 18:10:49 +00:00
|
|
|
else
|
|
|
|
{
|
2018-04-01 16:45:07 +00:00
|
|
|
MsgToEventLog(EVENTLOG_ERROR_TYPE,
|
2021-10-24 01:57:11 +00:00
|
|
|
L"Unknown WM_COPYDATA message ignored. (dwData: %lu, cbData: %lu, lpData: %ls)",
|
2018-04-01 16:45:07 +00:00
|
|
|
copy_data->dwData, copy_data->cbData, copy_data->lpData);
|
|
|
|
return FALSE;
|
2016-02-27 18:10:49 +00:00
|
|
|
}
|
|
|
|
return TRUE; /* indicate we handled the message */
|
|
|
|
}
|
|
|
|
|
2022-07-05 02:57:40 +00:00
|
|
|
/* If automatic service is running, check whether we are
|
|
|
|
* attached to the management i/f of persistent daemons
|
|
|
|
* and re-attach if necessary. The timer is reset to
|
|
|
|
* call this routine again after a delay. Periodic check
|
|
|
|
* is required as we get detached if the daemon gets restarted
|
|
|
|
* by the service or we do a disconnect.
|
|
|
|
*/
|
|
|
|
static void CALLBACK
|
|
|
|
ManagePersistent(HWND hwnd, UINT UNUSED msg, UINT_PTR id, DWORD UNUSED now)
|
|
|
|
{
|
|
|
|
CheckServiceStatus(false);
|
|
|
|
if (o.service_state == service_connected)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < o.num_configs; i++)
|
|
|
|
{
|
|
|
|
if (o.conn[i].flags & FLAG_DAEMON_PERSISTENT
|
|
|
|
&& o.conn[i].auto_connect
|
2022-07-08 00:49:00 +00:00
|
|
|
&& (o.conn[i].state == disconnected || o.conn[i].state == detached))
|
2022-07-05 02:57:40 +00:00
|
|
|
{
|
|
|
|
/* disable auto-connect to avoid repeated re-connect
|
|
|
|
* after unrecoverable errors. Re-enabled on successful
|
|
|
|
* connect.
|
|
|
|
*/
|
|
|
|
o.conn[i].auto_connect = false;
|
2022-07-21 00:58:04 +00:00
|
|
|
o.conn[i].state = detached; /* this is required to retain management-hold on re-attach */
|
2022-07-05 02:57:40 +00:00
|
|
|
StartOpenVPN(&o.conn[i]); /* attach to the management i/f */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* schedule to call again after 10 sec */
|
|
|
|
SetTimer(hwnd, id, 10000, ManagePersistent);
|
|
|
|
}
|
|
|
|
|
2022-07-10 01:04:19 +00:00
|
|
|
/* Detach from the mgmt i/f of all atatched persistent
|
|
|
|
* connections. Called on session lock so that another
|
|
|
|
* user can attach to these.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
HandleSessionLock(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < o.num_configs; i++)
|
|
|
|
{
|
|
|
|
if (o.conn[i].flags & FLAG_DAEMON_PERSISTENT
|
|
|
|
&& (o.conn[i].state != disconnected && o.conn[i].state != detached))
|
|
|
|
{
|
|
|
|
o.conn[i].auto_connect = false;
|
|
|
|
DetachOpenVPN(&o.conn[i]);
|
|
|
|
o.conn[i].flags |= FLAG_WAIT_UNLOCK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Undo any actions done at session lock/disconnect.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
HandleSessionUnlock(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < o.num_configs; i++)
|
|
|
|
{
|
|
|
|
if (o.conn[i].flags & FLAG_WAIT_UNLOCK)
|
|
|
|
{
|
|
|
|
o.conn[i].auto_connect = true; /* so that ManagePersistent will trigger attach */
|
|
|
|
o.conn[i].flags &= ~ FLAG_WAIT_UNLOCK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
/* This function is called by the Windows function DispatchMessage() */
|
|
|
|
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
static UINT s_uTaskbarRestart;
|
2021-02-27 19:40:15 +00:00
|
|
|
int conn_id = 0;
|
|
|
|
MENUINFO minfo = {.cbSize = sizeof(MENUINFO)};
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
switch (message) {
|
2021-05-13 08:25:39 +00:00
|
|
|
case WM_CREATE:
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
/* Save Window Handle */
|
|
|
|
o.hWnd = hwnd;
|
2022-10-12 20:58:59 +00:00
|
|
|
dpi_initialize(&o);
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
|
|
|
|
|
2013-04-25 15:53:16 +00:00
|
|
|
WTSRegisterSessionNotification(hwnd, NOTIFY_FOR_THIS_SESSION);
|
|
|
|
|
2010-04-23 10:57:00 +00:00
|
|
|
/* Load application icon */
|
|
|
|
HICON hIcon = LoadLocalizedIcon(ID_ICO_APP);
|
|
|
|
if (hIcon) {
|
|
|
|
SendMessage(hwnd, WM_SETICON, (WPARAM) (ICON_SMALL), (LPARAM) (hIcon));
|
|
|
|
SendMessage(hwnd, WM_SETICON, (WPARAM) (ICON_BIG), (LPARAM) (hIcon));
|
|
|
|
}
|
|
|
|
|
2016-02-27 18:10:49 +00:00
|
|
|
/* Enable next line to accept WM_COPYDATA messages from lower level processes */
|
|
|
|
#if 0
|
|
|
|
ChangeWindowMessageFilterEx(hwnd, WM_COPYDATA, MSGFLT_ALLOW, NULL);
|
|
|
|
#endif
|
|
|
|
|
Parse and display messages received by echo msg commands
Process four new echo commands to construct messages to be
displayed to the user:
echo msg message-text
echo msg-n message-text
echo msg-window message-title
echo msg-notify message-title
Note: All rules of push and echo processing apply and determine
what is received as echo commands by the GUI. In addition,
'url-encoded' characters (% followed by two hex digits) are
decoded and displayed.
The message is constructed in the GUI by concatenating the text
specified in one or more "echo msg text" or "echo msg-n text"
commands. In case of "echo msg text" text is appended with a new
line. An empty text in this case will
just add a new line.
The message ends and gets displayed when one of the following
are receieved:
echo msg-window title
echo msg-notify title
where "title" becomes the title of the message window. In case of
msg-window, a modeless window shows the message, in the latter case
a notification balloon is shown.
Example: when pushed from the server:
push "echo msg I say let the world go to hell%2C"
push "echo msg I must have my cup of tea."
push "echo msg-window Notes from the underground"
will display a modeless window with title
"Notes from the underground" and a two line body
--
I say let the world go to hell,
I must have my cup of tea.
--
Note that the message itself is not quoted in the above examples
and so it relies on the server's option-parser combining
individual words into a space separated string. Number of words
on a line is limited by the maximum number of parameters allowed
in openvpn commands (16). This limitation may be avoided by quoting
the text that follows so that the option parser sees it as one
parameter.
The comma character is not allowed in pushed strings, so
it has to be sent encoded as %2C as shown above.
Such encoding of arbitrary bytes is suppored. For example,
newlines may be embedded as %0A, though discouraged. Instead
use multiple "echo msg" commands to separate lines by new line.
An example with embedded spaces and multiple lines concatenated
without a new line in between (note use of single quotes):
push "echo msg-n I swear to you gentlemen%2C that to be"
push "echo msg-n ' overly conscious is a sickness%2C ' "
push "echo msg-n a real%2C thorough sickness."
push "echo msg-notify Quote of the Day"
will show up as a notification that displays for an
OS-dependent interval as:
--
Quote of the Day
I swear to you gentlemen, that to be overly conscious
is a sickness, a real, thorough sickness.
--
where the location of the line break is automatically determined
by the notification API and is OS version-dependent.
Commands like "echo msg ..." in the config file are also
processed the same way. It gets displayed when the GUI connects
to the management interface and receives all pending echo.
Pushed message(s) get displayed when the client daemon
processes push-reply and passes on echo directives to the
GUI.
TODO: The actual window that displays the messages is
implemented in the next commit.
Signed-off-by: Selva Nair <selva.nair@gmail.com>
2017-11-26 22:38:11 +00:00
|
|
|
echo_msg_init();
|
|
|
|
|
2021-05-13 08:25:39 +00:00
|
|
|
CreatePopupMenus(); /* Create popup menus */
|
2008-12-18 11:08:35 +00:00
|
|
|
ShowTrayIcon();
|
2021-08-24 00:14:10 +00:00
|
|
|
|
|
|
|
/* if '--import' was specified, do it now */
|
|
|
|
if (o.action == WM_OVPN_IMPORT && o.action_arg)
|
|
|
|
{
|
2021-11-17 04:11:26 +00:00
|
|
|
ImportConfigFile(o.action_arg, true); /* prompt user */
|
2021-08-24 00:14:10 +00:00
|
|
|
}
|
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
if (!AutoStartConnections()) {
|
|
|
|
SendMessage(hwnd, WM_CLOSE, 0, 0);
|
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:57:40 +00:00
|
|
|
/* A timer to periodically tend to persistent connections */
|
2022-10-16 20:30:11 +00:00
|
|
|
SetTimer(hwnd, 1, 100, ManagePersistent);
|
2022-07-05 02:57:40 +00:00
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
break;
|
2021-05-13 08:25:39 +00:00
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
case WM_NOTIFYICONTRAY:
|
|
|
|
OnNotifyTray(lParam); // Manages message from tray
|
2010-04-23 10:57:00 +00:00
|
|
|
break;
|
2010-08-13 15:42:23 +00:00
|
|
|
|
2016-02-27 18:10:49 +00:00
|
|
|
case WM_COPYDATA: // custom messages with data from other processes
|
|
|
|
HandleCopyDataMessage((COPYDATASTRUCT*) lParam);
|
|
|
|
return TRUE; /* lets the sender free copy_data */
|
|
|
|
|
2021-02-27 19:40:15 +00:00
|
|
|
case WM_MENUCOMMAND:
|
|
|
|
/* Get the menu item id and save it in wParam for use below */
|
|
|
|
wParam = GetMenuItemID((HMENU) lParam, wParam);
|
|
|
|
|
|
|
|
/* we first check global menu items which do not require a connnection index */
|
2021-06-28 13:05:23 +00:00
|
|
|
if (LOWORD(wParam) == IDM_IMPORT_FILE) {
|
2021-06-29 06:54:36 +00:00
|
|
|
ImportConfigFileFromDisk();
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
2021-06-28 13:05:23 +00:00
|
|
|
else if (LOWORD(wParam) == IDM_IMPORT_AS) {
|
|
|
|
ImportConfigFromAS();
|
|
|
|
}
|
2021-08-29 02:22:40 +00:00
|
|
|
else if (LOWORD(wParam) == IDM_IMPORT_URL) {
|
|
|
|
ImportConfigFromURL();
|
|
|
|
}
|
2021-02-27 19:40:15 +00:00
|
|
|
else if (LOWORD(wParam) == IDM_SETTINGS) {
|
|
|
|
ShowSettingsDialog();
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
2021-02-27 19:40:15 +00:00
|
|
|
else if (LOWORD(wParam) == IDM_CLOSE) {
|
|
|
|
CloseApplication(hwnd);
|
2017-01-23 03:01:29 +00:00
|
|
|
}
|
2021-02-27 19:40:15 +00:00
|
|
|
/* rest of the handlers require a connection id */
|
|
|
|
else {
|
|
|
|
minfo.fMask = MIM_MENUDATA;
|
|
|
|
GetMenuInfo((HMENU) lParam, &minfo);
|
|
|
|
conn_id = (INT) minfo.dwMenuData;
|
|
|
|
if (conn_id < 0 || conn_id >= o.num_configs) break; /* ignore invalid connection id */
|
2016-03-21 12:43:41 +00:00
|
|
|
}
|
2021-02-27 19:40:15 +00:00
|
|
|
|
|
|
|
/* reach here only if the command did not match any global items and a valid connection id is available */
|
|
|
|
|
|
|
|
if (LOWORD(wParam) == IDM_CONNECTMENU) {
|
|
|
|
StartOpenVPN(&o.conn[conn_id]);
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
2021-02-27 19:40:15 +00:00
|
|
|
else if (LOWORD(wParam) == IDM_DISCONNECTMENU) {
|
|
|
|
StopOpenVPN(&o.conn[conn_id]);
|
2016-02-17 00:20:53 +00:00
|
|
|
}
|
2021-02-27 19:40:15 +00:00
|
|
|
else if (LOWORD(wParam) == IDM_RECONNECTMENU) {
|
|
|
|
RestartOpenVPN(&o.conn[conn_id]);
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
2021-02-27 19:40:15 +00:00
|
|
|
else if (LOWORD(wParam) == IDM_STATUSMENU) {
|
|
|
|
ShowWindow(o.conn[conn_id].hwndStatus, SW_SHOW);
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
2021-02-27 19:40:15 +00:00
|
|
|
else if (LOWORD(wParam) == IDM_VIEWLOGMENU) {
|
|
|
|
ViewLog(conn_id);
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
2021-02-27 19:40:15 +00:00
|
|
|
else if (LOWORD(wParam) == IDM_EDITMENU) {
|
|
|
|
EditConfig(conn_id);
|
|
|
|
}
|
|
|
|
else if (LOWORD(wParam) == IDM_CLEARPASSMENU) {
|
|
|
|
ResetSavePasswords(&o.conn[conn_id]);
|
|
|
|
}
|
|
|
|
#ifndef DISABLE_CHANGE_PASSWORD
|
|
|
|
else if (LOWORD(wParam) == IDM_PASSPHRASEMENU) {
|
|
|
|
ShowChangePassphraseDialog(&o.conn[conn_id]);
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-18 11:08:35 +00:00
|
|
|
break;
|
2020-12-31 18:41:56 +00:00
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
case WM_CLOSE:
|
|
|
|
CloseApplication(hwnd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_DESTROY:
|
2013-04-25 15:53:16 +00:00
|
|
|
WTSUnRegisterSessionNotification(hwnd);
|
2021-05-13 08:25:39 +00:00
|
|
|
StopAllOpenVPN();
|
2008-12-18 11:08:35 +00:00
|
|
|
OnDestroyTray(); /* Remove Tray Icon and destroy menus */
|
|
|
|
PostQuitMessage (0); /* Send a WM_QUIT to the message queue */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_QUERYENDSESSION:
|
|
|
|
return(TRUE);
|
|
|
|
|
|
|
|
case WM_ENDSESSION:
|
|
|
|
StopAllOpenVPN();
|
|
|
|
OnDestroyTray();
|
|
|
|
break;
|
|
|
|
|
2013-04-25 15:53:16 +00:00
|
|
|
case WM_WTSSESSION_CHANGE:
|
|
|
|
switch (wParam) {
|
|
|
|
case WTS_SESSION_LOCK:
|
2022-07-10 01:04:19 +00:00
|
|
|
PrintDebug(L"Session lock triggered");
|
2013-04-25 15:53:16 +00:00
|
|
|
o.session_locked = TRUE;
|
2022-07-10 01:04:19 +00:00
|
|
|
/* Detach persistent connections so that other users can connect to it */
|
|
|
|
HandleSessionLock();
|
2022-10-16 20:30:11 +00:00
|
|
|
KillTimer(hwnd, 1); /* This ensure ManagePersistent is not called when session is locked */
|
2013-04-25 15:53:16 +00:00
|
|
|
break;
|
2022-07-10 01:04:19 +00:00
|
|
|
|
2013-04-25 15:53:16 +00:00
|
|
|
case WTS_SESSION_UNLOCK:
|
2022-07-10 01:04:19 +00:00
|
|
|
PrintDebug(L"Session unlock triggered");
|
2013-04-25 15:53:16 +00:00
|
|
|
o.session_locked = FALSE;
|
2022-07-10 01:04:19 +00:00
|
|
|
HandleSessionUnlock();
|
2022-10-16 20:30:11 +00:00
|
|
|
SetTimer(hwnd, 1, 100, ManagePersistent);
|
2013-04-25 15:53:16 +00:00
|
|
|
if (CountConnState(suspended) != 0)
|
|
|
|
ResumeConnections();
|
|
|
|
break;
|
2022-07-10 01:04:19 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
PrintDebug(L"Session change with wParam = %lu", wParam);
|
|
|
|
break;
|
2013-04-25 15:53:16 +00:00
|
|
|
}
|
2013-08-05 08:47:32 +00:00
|
|
|
break;
|
2013-04-25 15:53:16 +00:00
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
default: /* for messages that we don't deal with */
|
|
|
|
if (message == s_uTaskbarRestart)
|
|
|
|
{
|
|
|
|
/* Explorer has restarted, re-register the tray icon. */
|
|
|
|
ShowTrayIcon();
|
|
|
|
CheckAndSetTrayIcon();
|
|
|
|
break;
|
2021-05-13 08:25:39 +00:00
|
|
|
}
|
2008-12-18 11:08:35 +00:00
|
|
|
return DefWindowProc (hwnd, message, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-30 16:20:23 +00:00
|
|
|
static INT_PTR CALLBACK
|
2012-03-12 17:58:38 +00:00
|
|
|
AboutDialogFunc(UNUSED HWND hDlg, UINT msg, UNUSED WPARAM wParam, LPARAM lParam)
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2011-11-30 16:42:20 +00:00
|
|
|
LPPSHNOTIFY psn;
|
2021-05-19 02:27:31 +00:00
|
|
|
wchar_t tmp1[256], tmp2[256];
|
|
|
|
switch (msg) {
|
|
|
|
case WM_INITDIALOG:
|
|
|
|
if (GetDlgItemText(hDlg, ID_TXT_VERSION, tmp1, _countof(tmp1))) {
|
|
|
|
_sntprintf_0(tmp2, tmp1, TEXT(PACKAGE_VERSION_RESOURCE_STR));
|
|
|
|
SetDlgItemText(hDlg, ID_TXT_VERSION, tmp2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WM_NOTIFY:
|
|
|
|
psn = (LPPSHNOTIFY) lParam;
|
|
|
|
if (psn->hdr.code == (UINT) PSN_APPLY)
|
2011-11-30 16:42:20 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2008-12-18 11:08:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-01-26 21:07:04 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
ShowSettingsDialog()
|
|
|
|
{
|
2016-07-09 19:14:36 +00:00
|
|
|
PROPSHEETPAGE psp[4];
|
2010-04-23 10:57:00 +00:00
|
|
|
int page_number = 0;
|
|
|
|
|
2016-07-09 19:14:36 +00:00
|
|
|
/* General tab */
|
|
|
|
psp[page_number].dwSize = sizeof(PROPSHEETPAGE);
|
|
|
|
psp[page_number].dwFlags = PSP_DLGINDIRECT;
|
|
|
|
psp[page_number].hInstance = o.hInstance;
|
|
|
|
psp[page_number].pResource = LocalizedDialogResource(ID_DLG_GENERAL);
|
|
|
|
psp[page_number].pfnDlgProc = GeneralSettingsDlgProc;
|
|
|
|
psp[page_number].lParam = 0;
|
|
|
|
psp[page_number].pfnCallback = NULL;
|
|
|
|
++page_number;
|
|
|
|
|
2010-04-23 10:57:00 +00:00
|
|
|
/* Proxy tab */
|
2022-07-03 20:57:40 +00:00
|
|
|
psp[page_number].dwSize = sizeof(PROPSHEETPAGE);
|
|
|
|
psp[page_number].dwFlags = PSP_DLGINDIRECT;
|
|
|
|
psp[page_number].hInstance = o.hInstance;
|
|
|
|
psp[page_number].pResource = LocalizedDialogResource(ID_DLG_PROXY);
|
|
|
|
psp[page_number].pfnDlgProc = ProxySettingsDialogFunc;
|
|
|
|
psp[page_number].lParam = 0;
|
|
|
|
psp[page_number].pfnCallback = NULL;
|
|
|
|
++page_number;
|
2010-04-23 10:57:00 +00:00
|
|
|
|
2016-07-09 19:14:36 +00:00
|
|
|
/* Advanced tab */
|
2010-04-23 10:57:00 +00:00
|
|
|
psp[page_number].dwSize = sizeof(PROPSHEETPAGE);
|
|
|
|
psp[page_number].dwFlags = PSP_DLGINDIRECT;
|
|
|
|
psp[page_number].hInstance = o.hInstance;
|
2016-07-09 19:14:36 +00:00
|
|
|
psp[page_number].pResource = LocalizedDialogResource(ID_DLG_ADVANCED);
|
|
|
|
psp[page_number].pfnDlgProc = AdvancedSettingsDlgProc;
|
2010-04-23 10:57:00 +00:00
|
|
|
psp[page_number].lParam = 0;
|
|
|
|
psp[page_number].pfnCallback = NULL;
|
|
|
|
++page_number;
|
2009-01-26 21:07:04 +00:00
|
|
|
|
2011-11-30 16:20:23 +00:00
|
|
|
/* About tab */
|
|
|
|
psp[page_number].dwSize = sizeof(PROPSHEETPAGE);
|
|
|
|
psp[page_number].dwFlags = PSP_DLGINDIRECT;
|
|
|
|
psp[page_number].hInstance = o.hInstance;
|
|
|
|
psp[page_number].pResource = LocalizedDialogResource(ID_DLG_ABOUT);
|
|
|
|
psp[page_number].pfnDlgProc = AboutDialogFunc;
|
|
|
|
psp[page_number].lParam = 0;
|
|
|
|
psp[page_number].pfnCallback = NULL;
|
|
|
|
++page_number;
|
|
|
|
|
2009-01-26 21:07:04 +00:00
|
|
|
PROPSHEETHEADER psh;
|
|
|
|
psh.dwSize = sizeof(PROPSHEETHEADER);
|
|
|
|
psh.dwFlags = PSH_USEHICON | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW | PSH_NOCONTEXTHELP;
|
|
|
|
psh.hwndParent = o.hWnd;
|
|
|
|
psh.hInstance = o.hInstance;
|
2009-02-04 15:54:37 +00:00
|
|
|
psh.hIcon = LoadLocalizedIcon(ID_ICO_APP);
|
2009-01-26 21:07:04 +00:00
|
|
|
psh.pszCaption = LoadLocalizedString(IDS_SETTINGS_CAPTION);
|
2010-04-23 10:57:00 +00:00
|
|
|
psh.nPages = page_number;
|
2009-01-26 21:07:04 +00:00
|
|
|
psh.nStartPage = 0;
|
|
|
|
psh.ppsp = (LPCPROPSHEETPAGE) &psp;
|
|
|
|
psh.pfnCallback = NULL;
|
|
|
|
|
|
|
|
PropertySheet(&psh);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-20 15:44:48 +00:00
|
|
|
void
|
|
|
|
CloseApplication(HWND hwnd)
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2012-07-20 15:44:48 +00:00
|
|
|
int i;
|
|
|
|
|
2022-07-03 20:30:31 +00:00
|
|
|
/* Show a message if any non-persistent connections are active */
|
2012-07-20 15:44:48 +00:00
|
|
|
for (i = 0; i < o.num_configs; i++)
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2022-07-03 20:30:31 +00:00
|
|
|
if (o.conn[i].state == disconnected
|
|
|
|
|| o.conn[i].flags & FLAG_DAEMON_PERSISTENT)
|
|
|
|
{
|
2012-07-20 15:44:48 +00:00
|
|
|
continue;
|
2022-07-03 20:30:31 +00:00
|
|
|
}
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2012-07-20 15:44:48 +00:00
|
|
|
/* Ask for confirmation if still connected */
|
2021-06-30 09:59:42 +00:00
|
|
|
if (ShowLocalizedMsgEx(MB_YESNO, NULL, _T("Exit OpenVPN"), IDS_NFO_ACTIVE_CONN_EXIT) == IDNO)
|
2022-07-03 20:30:31 +00:00
|
|
|
{
|
2012-07-20 15:44:48 +00:00
|
|
|
return;
|
2022-07-03 20:30:31 +00:00
|
|
|
}
|
|
|
|
break; /* show the above message box only once */
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
2012-07-20 15:44:48 +00:00
|
|
|
|
|
|
|
DestroyWindow(hwnd);
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 00:20:53 +00:00
|
|
|
void
|
2021-06-29 06:54:36 +00:00
|
|
|
ImportConfigFileFromDisk()
|
2016-02-17 00:20:53 +00:00
|
|
|
{
|
2016-07-09 19:14:36 +00:00
|
|
|
TCHAR filter[2*_countof(o.ext_string)+5];
|
2016-02-17 00:20:53 +00:00
|
|
|
|
2021-10-24 01:57:11 +00:00
|
|
|
_sntprintf_0(filter, _T("*.%ls%lc*.%ls%lc"), o.ext_string, _T('\0'), o.ext_string, _T('\0'));
|
2021-05-13 08:25:39 +00:00
|
|
|
|
2016-02-17 00:20:53 +00:00
|
|
|
OPENFILENAME fn;
|
|
|
|
TCHAR source[MAX_PATH] = _T("");
|
|
|
|
|
|
|
|
fn.lStructSize = sizeof(OPENFILENAME);
|
|
|
|
fn.hwndOwner = NULL;
|
|
|
|
fn.lpstrFilter = filter;
|
|
|
|
fn.lpstrCustomFilter = NULL;
|
|
|
|
fn.nFilterIndex = 1;
|
|
|
|
fn.lpstrFile = source;
|
|
|
|
fn.nMaxFile = MAX_PATH;
|
|
|
|
fn.lpstrFileTitle = NULL;
|
|
|
|
fn.lpstrInitialDir = NULL;
|
|
|
|
fn.lpstrTitle = NULL;
|
|
|
|
fn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
|
|
|
|
fn.lpstrDefExt = NULL;
|
|
|
|
|
2021-05-13 08:25:39 +00:00
|
|
|
if (GetOpenFileName(&fn))
|
2016-02-17 00:20:53 +00:00
|
|
|
{
|
2021-11-17 04:11:26 +00:00
|
|
|
ImportConfigFile(source, false); /* do not prompt user */
|
2016-02-17 00:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
#ifdef DEBUG
|
2012-02-29 20:22:17 +00:00
|
|
|
void PrintDebugMsg(TCHAR *msg)
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
|
|
|
time_t log_time;
|
|
|
|
struct tm *time_struct;
|
2012-02-29 20:22:17 +00:00
|
|
|
TCHAR date[30];
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
log_time = time(NULL);
|
|
|
|
time_struct = localtime(&log_time);
|
2012-03-29 15:40:34 +00:00
|
|
|
_sntprintf(date, _countof(date), _T("%d-%.2d-%.2d %.2d:%.2d:%.2d"),
|
2008-12-18 11:08:35 +00:00
|
|
|
time_struct->tm_year + 1900,
|
|
|
|
time_struct->tm_mon + 1,
|
|
|
|
time_struct->tm_mday,
|
|
|
|
time_struct->tm_hour,
|
|
|
|
time_struct->tm_min,
|
|
|
|
time_struct->tm_sec);
|
|
|
|
|
2021-10-24 01:57:11 +00:00
|
|
|
_ftprintf(o.debug_fp, _T("%ls %ls\n"), date, msg);
|
2008-12-18 11:08:35 +00:00
|
|
|
fflush(o.debug_fp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PACKVERSION(major,minor) MAKELONG(minor,major)
|
|
|
|
DWORD GetDllVersion(LPCTSTR lpszDllName)
|
|
|
|
{
|
|
|
|
HINSTANCE hinstDll;
|
|
|
|
DWORD dwVersion = 0;
|
|
|
|
|
2021-05-13 08:25:39 +00:00
|
|
|
/* For security purposes, LoadLibrary should be provided with a
|
2008-12-18 11:08:35 +00:00
|
|
|
fully-qualified path to the DLL. The lpszDllName variable should be
|
|
|
|
tested to ensure that it is a fully qualified path before it is used. */
|
|
|
|
hinstDll = LoadLibrary(lpszDllName);
|
2021-05-13 08:25:39 +00:00
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
if(hinstDll)
|
|
|
|
{
|
|
|
|
DLLGETVERSIONPROC pDllGetVersion;
|
2021-05-13 08:25:39 +00:00
|
|
|
pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(hinstDll,
|
2008-12-18 11:08:35 +00:00
|
|
|
"DllGetVersion");
|
|
|
|
|
|
|
|
/* Because some DLLs might not implement this function, you
|
2021-05-13 08:25:39 +00:00
|
|
|
must test for it explicitly. Depending on the particular
|
2008-12-18 11:08:35 +00:00
|
|
|
DLL, the lack of a DllGetVersion function can be a useful
|
|
|
|
indicator of the version. */
|
|
|
|
|
|
|
|
if(pDllGetVersion)
|
|
|
|
{
|
|
|
|
DLLVERSIONINFO dvi;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
ZeroMemory(&dvi, sizeof(dvi));
|
|
|
|
dvi.cbSize = sizeof(dvi);
|
|
|
|
|
|
|
|
hr = (*pDllGetVersion)(&dvi);
|
|
|
|
|
|
|
|
if(SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
dwVersion = PACKVERSION(dvi.dwMajorVersion, dvi.dwMinorVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeLibrary(hinstDll);
|
|
|
|
}
|
|
|
|
return dwVersion;
|
|
|
|
}
|
2018-04-01 16:45:07 +00:00
|
|
|
|
2018-04-20 01:43:17 +00:00
|
|
|
void
|
|
|
|
ErrorExit(int exit_code, const wchar_t *msg)
|
|
|
|
{
|
|
|
|
if (msg)
|
|
|
|
MessageBoxExW(NULL, msg, TEXT(PACKAGE_NAME),
|
|
|
|
MB_OK | MB_SETFOREGROUND|MB_ICONERROR, GetGUILanguage());
|
|
|
|
if (o.hWnd)
|
|
|
|
{
|
|
|
|
StopAllOpenVPN();
|
|
|
|
PostQuitMessage(exit_code);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
exit(exit_code);
|
|
|
|
}
|
|
|
|
}
|