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
|
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
#include <windows.h>
|
2010-03-11 21:58:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2008-12-18 11:08:35 +00:00
|
|
|
#include "service.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "main.h"
|
2020-09-14 18:47:36 +00:00
|
|
|
#include "misc.h"
|
2008-12-18 11:08:35 +00:00
|
|
|
#include "openvpn-gui-res.h"
|
2009-01-18 20:59:52 +00:00
|
|
|
#include "localization.h"
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2019-12-26 14:14:23 +00:00
|
|
|
#define OPENVPN_SERVICE_NAME_OVPN3 L"ovpnagent"
|
|
|
|
#define OPENVPN_SERVICE_NAME_OVPN2 L"OpenVPNServiceInteractive"
|
|
|
|
|
2010-04-09 04:01:30 +00:00
|
|
|
extern options_t o;
|
2008-12-18 11:08:35 +00:00
|
|
|
|
2016-02-16 20:49:13 +00:00
|
|
|
bool
|
2016-02-26 17:21:23 +00:00
|
|
|
CheckIServiceStatus(BOOL warn)
|
2016-02-16 20:49:13 +00:00
|
|
|
{
|
2017-05-11 23:11:55 +00:00
|
|
|
SC_HANDLE schSCManager = NULL;
|
|
|
|
SC_HANDLE schService = NULL;
|
2016-02-16 20:49:13 +00:00
|
|
|
SERVICE_STATUS ssStatus;
|
2017-05-11 23:11:55 +00:00
|
|
|
BOOL ret = false;
|
2016-02-16 20:49:13 +00:00
|
|
|
|
|
|
|
// Open a handle to the SC Manager database.
|
|
|
|
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
|
|
|
|
|
|
|
|
if (NULL == schSCManager)
|
|
|
|
return(false);
|
|
|
|
|
2019-12-26 14:14:23 +00:00
|
|
|
schService = OpenService(schSCManager, o.ovpn_engine == OPENVPN_ENGINE_OVPN3 ?
|
|
|
|
OPENVPN_SERVICE_NAME_OVPN3 : OPENVPN_SERVICE_NAME_OVPN2, SERVICE_QUERY_STATUS);
|
|
|
|
|
2016-02-16 20:49:13 +00:00
|
|
|
if (schService == NULL &&
|
|
|
|
GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
|
|
|
{
|
|
|
|
/* warn that iservice is not installed */
|
2016-02-26 17:21:23 +00:00
|
|
|
if (warn)
|
|
|
|
ShowLocalizedMsg(IDS_ERR_INSTALL_ISERVICE);
|
2017-05-11 23:11:55 +00:00
|
|
|
goto out;
|
2016-02-16 20:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!QueryServiceStatus(schService, &ssStatus))
|
2017-05-11 23:11:55 +00:00
|
|
|
goto out;
|
2016-02-16 20:49:13 +00:00
|
|
|
|
|
|
|
if (ssStatus.dwCurrentState != SERVICE_RUNNING)
|
|
|
|
{
|
|
|
|
/* warn that iservice is not started */
|
2016-02-26 17:21:23 +00:00
|
|
|
if (warn)
|
2020-09-14 18:47:36 +00:00
|
|
|
{
|
|
|
|
if (IsUserAdmin())
|
|
|
|
ShowLocalizedMsg(IDS_ERR_NOTSTARTED_ISERVICE_ADM);
|
|
|
|
else
|
|
|
|
ShowLocalizedMsg(IDS_ERR_NOTSTARTED_ISERVICE);
|
|
|
|
}
|
2017-05-11 23:11:55 +00:00
|
|
|
goto out;
|
2016-02-16 20:49:13 +00:00
|
|
|
}
|
2017-05-11 23:11:55 +00:00
|
|
|
ret = true;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (schService)
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
if (schSCManager)
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
return ret;
|
2016-02-16 20:49:13 +00:00
|
|
|
}
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
int CheckServiceStatus()
|
|
|
|
{
|
|
|
|
|
2017-05-11 23:11:55 +00:00
|
|
|
SC_HANDLE schSCManager = NULL;
|
|
|
|
SC_HANDLE schService = NULL;
|
2008-12-18 11:08:35 +00:00
|
|
|
SERVICE_STATUS ssStatus;
|
2017-05-11 23:11:55 +00:00
|
|
|
BOOL ret = false;
|
2008-12-18 11:08:35 +00:00
|
|
|
|
|
|
|
// Open a handle to the SC Manager database.
|
|
|
|
schSCManager = OpenSCManager(
|
|
|
|
NULL, // local machine
|
|
|
|
NULL, // ServicesActive database
|
|
|
|
SC_MANAGER_CONNECT); // Connect rights
|
|
|
|
|
|
|
|
if (NULL == schSCManager) {
|
2010-04-09 04:01:30 +00:00
|
|
|
o.service_state = service_noaccess;
|
2017-05-11 23:11:55 +00:00
|
|
|
goto out;
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
schService = OpenService(
|
|
|
|
schSCManager, // SCM database
|
2010-03-21 09:07:14 +00:00
|
|
|
_T("OpenVPNService"), // service name
|
2008-12-18 11:08:35 +00:00
|
|
|
SERVICE_QUERY_STATUS);
|
|
|
|
|
|
|
|
if (schService == NULL) {
|
2010-04-09 04:01:30 +00:00
|
|
|
o.service_state = service_noaccess;
|
2017-05-11 23:11:55 +00:00
|
|
|
goto out;
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!QueryServiceStatus(
|
|
|
|
schService, // handle to service
|
|
|
|
&ssStatus) ) // address of status information structure
|
|
|
|
{
|
|
|
|
/* query failed */
|
2022-07-03 20:57:40 +00:00
|
|
|
o.service_state = service_noaccess;
|
|
|
|
MsgToEventLog(EVENTLOG_ERROR_TYPE, LoadLocalizedString(IDS_ERR_QUERY_SERVICE));
|
2017-05-11 23:11:55 +00:00
|
|
|
goto out;
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
|
|
|
|
{
|
2010-04-09 04:01:30 +00:00
|
|
|
o.service_state = service_connected;
|
2017-05-11 23:11:55 +00:00
|
|
|
ret = true;
|
|
|
|
goto out;
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-09 04:01:30 +00:00
|
|
|
o.service_state = service_disconnected;
|
2017-05-11 23:11:55 +00:00
|
|
|
goto out;
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
2017-05-11 23:11:55 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
if (schService)
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
if (schSCManager)
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
return ret;
|
2008-12-18 11:08:35 +00:00
|
|
|
}
|
2022-10-16 20:30:11 +00:00
|
|
|
|
|
|
|
/* Attempt to start OpenVPN Automatc Service */
|
|
|
|
void StartAutomaticService(void)
|
|
|
|
{
|
|
|
|
SC_HANDLE schSCManager = NULL;
|
|
|
|
SC_HANDLE schService = NULL;
|
|
|
|
|
|
|
|
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
|
|
|
|
|
|
|
|
if (schSCManager) {
|
|
|
|
schService = OpenService(schSCManager, L"OpenVPNService", SERVICE_START);
|
|
|
|
|
|
|
|
if (schService) {
|
|
|
|
StartService(schService, 0, NULL);
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|