2009-02-04 16:03:43 +00:00
|
|
|
/*
|
2008-12-18 11:08:35 +00:00
|
|
|
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Mathias Sundman <mathias@nilings.se>
|
2011-03-18 11:09:57 +00:00
|
|
|
* 2011 Heiko Hund <heikoh@users.sf.net>
|
2008-12-18 11:08:35 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <process.h>
|
2010-03-21 09:07:14 +00:00
|
|
|
#include <tchar.h>
|
2010-08-13 15:42:23 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
2010-03-11 21:58:45 +00:00
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include "openvpn-gui-res.h"
|
|
|
|
#include "options.h"
|
2017-02-27 02:00:24 +00:00
|
|
|
#include "misc.h"
|
2009-01-18 20:59:52 +00:00
|
|
|
#include "localization.h"
|
2017-02-27 02:00:24 +00:00
|
|
|
#include "env_set.h"
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2010-04-09 04:01:30 +00:00
|
|
|
extern options_t o;
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
RunPreconnectScript(connection_t *c)
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2010-08-13 15:42:23 +00:00
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
TCHAR cmdline[256];
|
|
|
|
DWORD exit_code;
|
|
|
|
struct _stat st;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Cut off extention from config filename and add "_pre.bat" */
|
2011-03-18 11:09:57 +00:00
|
|
|
int len = _tcslen(c->config_file) - _tcslen(o.ext_string) - 1;
|
2021-10-24 01:57:11 +00:00
|
|
|
_sntprintf_0(cmdline, _T("%ls\\%.*ls_pre.bat"), c->config_dir, len, c->config_file);
|
2010-08-13 15:42:23 +00:00
|
|
|
|
|
|
|
/* Return if no script exists */
|
|
|
|
if (_tstat(cmdline, &st) == -1)
|
|
|
|
return;
|
|
|
|
|
2020-11-01 20:39:21 +00:00
|
|
|
// Create the filename of the logfile
|
|
|
|
TCHAR script_log_filename[MAX_PATH];
|
2021-10-24 01:57:11 +00:00
|
|
|
_sntprintf_0(script_log_filename, _T("%ls\\%ls_pre.log"), o.log_dir, c->config_name);
|
2020-11-01 20:39:21 +00:00
|
|
|
|
|
|
|
// Create the log file
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
CLEAR(sa);
|
|
|
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
|
|
|
|
HANDLE logfile_handle = CreateFile(script_log_filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
&sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
CLEAR(si);
|
|
|
|
CLEAR(pi);
|
|
|
|
|
|
|
|
/* fill in STARTUPINFO struct */
|
|
|
|
GetStartupInfo(&si);
|
|
|
|
si.cb = sizeof(si);
|
2020-11-01 20:39:21 +00:00
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
2010-08-13 15:42:23 +00:00
|
|
|
si.wShowWindow = SW_SHOWDEFAULT;
|
|
|
|
si.hStdInput = NULL;
|
2020-11-01 20:39:21 +00:00
|
|
|
si.hStdOutput = logfile_handle;
|
|
|
|
si.hStdError = logfile_handle;
|
2010-08-13 15:42:23 +00:00
|
|
|
|
2017-02-27 02:00:24 +00:00
|
|
|
/* Preconnect script is run too early for config env to be available
|
|
|
|
* so we use the default process env here.
|
|
|
|
*/
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE,
|
2016-07-09 19:14:36 +00:00
|
|
|
(o.show_script_window ? CREATE_NEW_CONSOLE : CREATE_NO_WINDOW),
|
2010-08-13 15:42:23 +00:00
|
|
|
NULL, c->config_dir, &si, &pi))
|
|
|
|
return;
|
|
|
|
|
2016-07-09 19:14:36 +00:00
|
|
|
for (i = 0; i <= (int) o.preconnectscript_timeout; i++)
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2010-08-13 15:42:23 +00:00
|
|
|
if (!GetExitCodeProcess(pi.hProcess, &exit_code))
|
|
|
|
goto out;
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
if (exit_code != STILL_ACTIVE)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
Sleep(1000);
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
2010-08-13 15:42:23 +00:00
|
|
|
out:
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
CloseHandle(pi.hProcess);
|
2020-11-01 20:39:21 +00:00
|
|
|
if (logfile_handle != NULL)
|
|
|
|
CloseHandle(logfile_handle);
|
2010-08-13 15:42:23 +00:00
|
|
|
}
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
void
|
|
|
|
RunConnectScript(connection_t *c, int run_as_service)
|
|
|
|
{
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
TCHAR cmdline[256];
|
|
|
|
DWORD exit_code;
|
|
|
|
struct _stat st;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Cut off extention from config filename and add "_up.bat" */
|
2011-03-18 11:09:57 +00:00
|
|
|
int len = _tcslen(c->config_file) - _tcslen(o.ext_string) - 1;
|
2021-10-24 01:57:11 +00:00
|
|
|
_sntprintf_0(cmdline, _T("%ls\\%.*ls_up.bat"), c->config_dir, len, c->config_file);
|
2010-08-13 15:42:23 +00:00
|
|
|
|
|
|
|
/* Return if no script exists */
|
|
|
|
if (_tstat(cmdline, &st) == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!run_as_service)
|
|
|
|
SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONN_SCRIPT));
|
|
|
|
|
2020-10-30 15:58:37 +00:00
|
|
|
// Create the filename of the logfile
|
|
|
|
TCHAR script_log_filename[MAX_PATH];
|
2021-10-24 01:57:11 +00:00
|
|
|
_sntprintf_0(script_log_filename, _T("%ls\\%ls_up.log"), o.log_dir, c->config_name);
|
2020-10-30 15:58:37 +00:00
|
|
|
|
|
|
|
// Create the log file
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
CLEAR(sa);
|
|
|
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
|
|
|
|
HANDLE logfile_handle = CreateFile(script_log_filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
&sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
CLEAR(si);
|
|
|
|
CLEAR(pi);
|
|
|
|
|
|
|
|
/* fill in STARTUPINFO struct */
|
|
|
|
GetStartupInfo(&si);
|
|
|
|
si.cb = sizeof(si);
|
2020-10-30 15:58:37 +00:00
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
2010-08-13 15:42:23 +00:00
|
|
|
si.wShowWindow = SW_SHOWDEFAULT;
|
|
|
|
si.hStdInput = NULL;
|
2020-10-30 15:58:37 +00:00
|
|
|
si.hStdOutput = logfile_handle;
|
|
|
|
si.hStdError = logfile_handle;
|
2010-08-13 15:42:23 +00:00
|
|
|
|
2017-02-27 02:00:24 +00:00
|
|
|
/* make an env array with confg specific env appended to the process's env */
|
|
|
|
WCHAR *env = c->es ? merge_env_block(c->es) : NULL;
|
|
|
|
DWORD flags = CREATE_UNICODE_ENVIRONMENT;
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE,
|
2017-02-27 02:00:24 +00:00
|
|
|
(o.show_script_window ? flags|CREATE_NEW_CONSOLE : flags|CREATE_NO_WINDOW),
|
|
|
|
env, c->config_dir, &si, &pi))
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2017-02-27 02:00:24 +00:00
|
|
|
PrintDebug(L"CreateProcess: error = %lu", GetLastError());
|
2010-08-13 15:42:23 +00:00
|
|
|
ShowLocalizedMsg(IDS_ERR_RUN_CONN_SCRIPT, cmdline);
|
2017-02-27 02:00:24 +00:00
|
|
|
free(env);
|
2010-08-13 15:42:23 +00:00
|
|
|
return;
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
if (o.connectscript_timeout == 0)
|
|
|
|
goto out;
|
|
|
|
|
2016-07-09 19:14:36 +00:00
|
|
|
for (i = 0; i <= (int) o.connectscript_timeout; i++)
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2010-08-13 15:42:23 +00:00
|
|
|
if (!GetExitCodeProcess(pi.hProcess, &exit_code))
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2010-08-13 15:42:23 +00:00
|
|
|
ShowLocalizedMsg(IDS_ERR_GET_EXIT_CODE, cmdline);
|
|
|
|
goto out;
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
if (exit_code != STILL_ACTIVE)
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2010-08-13 15:42:23 +00:00
|
|
|
if (exit_code != 0)
|
|
|
|
ShowLocalizedMsg(IDS_ERR_CONN_SCRIPT_FAILED, exit_code);
|
|
|
|
goto out;
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
Sleep(1000);
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
ShowLocalizedMsg(IDS_ERR_RUN_CONN_SCRIPT_TIMEOUT, o.connectscript_timeout);
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
out:
|
2017-02-27 02:00:24 +00:00
|
|
|
free(env);
|
2010-08-13 15:42:23 +00:00
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
CloseHandle(pi.hProcess);
|
2020-10-30 15:58:37 +00:00
|
|
|
if (logfile_handle != NULL)
|
|
|
|
CloseHandle(logfile_handle);
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
RunDisconnectScript(connection_t *c, int run_as_service)
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2010-08-13 15:42:23 +00:00
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
TCHAR cmdline[256];
|
|
|
|
DWORD exit_code;
|
|
|
|
struct _stat st;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Cut off extention from config filename and add "_down.bat" */
|
2011-03-18 11:09:57 +00:00
|
|
|
int len = _tcslen(c->config_file) - _tcslen(o.ext_string) - 1;
|
2021-10-24 01:57:11 +00:00
|
|
|
_sntprintf_0(cmdline, _T("%ls\\%.*ls_down.bat"), c->config_dir, len, c->config_file);
|
2010-08-13 15:42:23 +00:00
|
|
|
|
|
|
|
/* Return if no script exists */
|
|
|
|
if (_tstat(cmdline, &st) == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!run_as_service)
|
|
|
|
SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_DISCONN_SCRIPT));
|
|
|
|
|
2020-11-01 20:39:48 +00:00
|
|
|
// Create the filename of the logfile
|
|
|
|
TCHAR script_log_filename[MAX_PATH];
|
2021-10-24 01:57:11 +00:00
|
|
|
_sntprintf_0(script_log_filename, _T("%ls\\%ls_down.log"), o.log_dir, c->config_name);
|
2020-11-01 20:39:48 +00:00
|
|
|
|
|
|
|
// Create the log file
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
CLEAR(sa);
|
|
|
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
|
|
|
|
HANDLE logfile_handle = CreateFile(script_log_filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
&sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
CLEAR(si);
|
|
|
|
CLEAR(pi);
|
|
|
|
|
|
|
|
/* fill in STARTUPINFO struct */
|
|
|
|
GetStartupInfo(&si);
|
|
|
|
si.cb = sizeof(si);
|
2020-11-01 20:39:48 +00:00
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
2010-08-13 15:42:23 +00:00
|
|
|
si.wShowWindow = SW_SHOWDEFAULT;
|
|
|
|
si.hStdInput = NULL;
|
2020-11-01 20:39:48 +00:00
|
|
|
si.hStdOutput = logfile_handle;
|
|
|
|
si.hStdError = logfile_handle;
|
2010-08-13 15:42:23 +00:00
|
|
|
|
2017-02-27 02:00:24 +00:00
|
|
|
/* make an env array with confg specific env appended to the process's env */
|
|
|
|
WCHAR *env = c->es ? merge_env_block(c->es) : NULL;
|
|
|
|
DWORD flags = CREATE_UNICODE_ENVIRONMENT;
|
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE,
|
2017-02-27 02:00:24 +00:00
|
|
|
(o.show_script_window ? flags|CREATE_NEW_CONSOLE : flags|CREATE_NO_WINDOW),
|
2010-08-13 15:42:23 +00:00
|
|
|
NULL, c->config_dir, &si, &pi))
|
2017-02-27 02:00:24 +00:00
|
|
|
{
|
|
|
|
free(env);
|
2010-08-13 15:42:23 +00:00
|
|
|
return;
|
2017-02-27 02:00:24 +00:00
|
|
|
}
|
2010-08-13 15:42:23 +00:00
|
|
|
|
2016-07-09 19:14:36 +00:00
|
|
|
for (i = 0; i <= (int) o.disconnectscript_timeout; i++)
|
2008-12-18 11:08:35 +00:00
|
|
|
{
|
2010-08-13 15:42:23 +00:00
|
|
|
if (!GetExitCodeProcess(pi.hProcess, &exit_code))
|
|
|
|
goto out;
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
if (exit_code != STILL_ACTIVE)
|
|
|
|
goto out;
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2010-08-13 15:42:23 +00:00
|
|
|
Sleep(1000);
|
|
|
|
}
|
|
|
|
out:
|
2017-02-27 02:00:24 +00:00
|
|
|
free(env);
|
2010-08-13 15:42:23 +00:00
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
CloseHandle(pi.hProcess);
|
2020-11-01 20:39:48 +00:00
|
|
|
if (logfile_handle != NULL)
|
|
|
|
CloseHandle(logfile_handle);
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|