-
Notifications
You must be signed in to change notification settings - Fork 405
/
service.c
227 lines (199 loc) · 5.75 KB
/
service.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
*
* Copyright (C) 2004 Mathias Sundman <[email protected]>
*
* 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
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <windows.h>
#include <stdio.h>
#include "service.h"
#include "options.h"
#include "main.h"
#include "misc.h"
#include "openvpn-gui-res.h"
#include "localization.h"
#define OPENVPN_SERVICE_NAME_OVPN3 L"ovpnagent"
#define OPENVPN_SERVICE_NAME_OVPN2 L"OpenVPNServiceInteractive"
extern options_t o;
bool
CheckIServiceStatus(BOOL warn)
{
SC_HANDLE schSCManager = NULL;
SC_HANDLE schService = NULL;
SERVICE_STATUS ssStatus;
BOOL ret = false;
/* Open a handle to the SC Manager database. */
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (NULL == schSCManager)
{
return(false);
}
schService = OpenService(schSCManager, o.ovpn_engine == OPENVPN_ENGINE_OVPN3 ?
OPENVPN_SERVICE_NAME_OVPN3 : OPENVPN_SERVICE_NAME_OVPN2, SERVICE_QUERY_STATUS);
if (schService == NULL
&& GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
{
/* warn that iservice is not installed */
if (warn)
{
ShowLocalizedMsg(IDS_ERR_INSTALL_ISERVICE);
}
goto out;
}
if (!QueryServiceStatus(schService, &ssStatus))
{
goto out;
}
if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{
/* warn that iservice is not started */
if (warn)
{
if (IsUserAdmin())
{
ShowLocalizedMsg(IDS_ERR_NOTSTARTED_ISERVICE_ADM);
}
else
{
ShowLocalizedMsg(IDS_ERR_NOTSTARTED_ISERVICE);
}
}
goto out;
}
ret = true;
out:
if (schService)
{
CloseServiceHandle(schService);
}
if (schSCManager)
{
CloseServiceHandle(schSCManager);
}
return ret;
}
VOID
CheckServiceStatus()
{
SC_HANDLE schSCManager = NULL;
SC_HANDLE schService = NULL;
SERVICE_STATUS ssStatus;
/* Open a handle to the SC Manager database. */
schSCManager = OpenSCManager(
NULL, /* local machine */
NULL, /* ServicesActive database */
SC_MANAGER_CONNECT); /* Connect rights */
if (NULL == schSCManager)
{
o.service_state = service_noaccess;
goto out;
}
schService = OpenService(
schSCManager, /* SCM database */
_T("OpenVPNService"), /* service name */
SERVICE_QUERY_STATUS);
if (schService == NULL)
{
o.service_state = service_noaccess;
goto out;
}
if (!QueryServiceStatus(
schService, /* handle to service */
&ssStatus) ) /* address of status information structure */
{
/* query failed */
o.service_state = service_noaccess;
MsgToEventLog(EVENTLOG_ERROR_TYPE, LoadLocalizedString(IDS_ERR_QUERY_SERVICE));
goto out;
}
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
o.service_state = service_connected;
goto out;
}
else
{
o.service_state = service_disconnected;
goto out;
}
out:
if (schService)
{
CloseServiceHandle(schService);
}
if (schSCManager)
{
CloseServiceHandle(schSCManager);
}
}
/* 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;
}
/*
* Returns the processId of the Interactive Service or zero on error
* which includes service not running.
*/
ULONG
GetServicePid(void)
{
SC_HANDLE schManager;
SC_HANDLE schService;
ULONG pid = 0;
schManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (schManager)
{
schService = OpenService(schManager, o.ovpn_engine == OPENVPN_ENGINE_OVPN3 ?
OPENVPN_SERVICE_NAME_OVPN3 : OPENVPN_SERVICE_NAME_OVPN2, SERVICE_QUERY_STATUS);
if (schService)
{
SERVICE_STATUS_PROCESS ssp = {0};
DWORD nbytes = 0;
if (QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (BYTE *)&ssp, sizeof(ssp), &nbytes)
&& ssp.dwCurrentState == SERVICE_RUNNING)
{
pid = ssp.dwProcessId;
}
CloseServiceHandle(schService);
}
CloseServiceHandle(schManager);
}
if (pid == 0)
{
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"%hs:%d Failed to get service process id: (error = 0x%08x)",
__func__, __LINE__, GetLastError());
}
return pid;
}