forked from OpenVPN/openvpn-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.c
336 lines (281 loc) · 8.78 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* 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 "tray.h"
#include "service.h"
#include "openvpn.h"
#include "options.h"
#include "scripts.h"
#include "main.h"
#include "openvpn-gui-res.h"
#include "localization.h"
extern options_t o;
int MyStartService()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
int i;
/* Set Service Status = Connecting */
o.service_state = service_connecting;
SetServiceMenuStatus();
CheckAndSetTrayIcon();
// Open a handle to the SC Manager database.
schSCManager = OpenSCManager(
NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_CONNECT); // Connect rights
if (NULL == schSCManager) {
/* open SC manager failed */
ShowLocalizedMsg(IDS_ERR_OPEN_SCMGR);
goto failed;
}
schService = OpenService(
schSCManager, // SCM database
_T("OpenVPNService"), // service name
SERVICE_START | SERVICE_QUERY_STATUS);
if (schService == NULL) {
/* can't open VPN service */
ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE);
goto failed;
}
/* Run Pre-connect script */
for (i=0; i<o.num_configs; i++)
RunPreconnectScript(&o.conn[i]);
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
/* can't start */
ShowLocalizedMsg(IDS_ERR_START_SERVICE);
goto failed;
}
else
{
//printf("Service start pending.\n");
}
// Check the status until the service is no longer start pending.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information structure
{
/* query failed */
ShowLocalizedMsg(IDS_ERR_QUERY_SERVICE);
goto failed;
}
// Save the tick count and initial checkpoint.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one tenth the wait hint, but no less than 1 second and no
// more than 5 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 5000 )
dwWaitTime = 5000;
Sleep( dwWaitTime );
// Check the status again.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of structure
break;
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
// No progress made within the wait hint
break;
}
}
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{
/* service hasn't started */
ShowLocalizedMsg(IDS_ERR_SERVICE_START_FAILED);
goto failed;
}
/* Run Connect script */
for (i=0; i<o.num_configs; i++)
RunConnectScript(&o.conn[i], true);
/* Set Service Status = Connected */
o.service_state = service_connected;
SetServiceMenuStatus();
CheckAndSetTrayIcon();
/* Show "OpenVPN Service Started" Tray Balloon msg */
ShowTrayBalloon(LoadLocalizedString(IDS_NFO_SERVICE_STARTED), _T(""));
return(true);
failed:
/* Set Service Status = Disconnecting */
o.service_state = service_disconnected;
SetServiceMenuStatus();
CheckAndSetTrayIcon();
return(false);
}
int MyStopService()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ssStatus;
int i;
// Open a handle to the SC Manager database.
schSCManager = OpenSCManager(
NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_CONNECT); // Connect rights
if (NULL == schSCManager) {
/* can't open SCManager */
ShowLocalizedMsg(IDS_ERR_OPEN_SCMGR, (int) GetLastError());
return(false);
}
schService = OpenService(
schSCManager, // SCM database
_T("OpenVPNService"), // service name
SERVICE_STOP);
if (schService == NULL) {
/* can't open vpn service */
ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE);
return(false);
}
/* Run DisConnect script */
for (i=0; i<o.num_configs; i++)
RunDisconnectScript(&o.conn[i], true);
if (!ControlService(
schService, // handle to service
SERVICE_CONTROL_STOP, // control value to send
&ssStatus) ) // address of status info
{
/* stop failed */
ShowLocalizedMsg(IDS_ERR_STOP_SERVICE);
return(false);
}
o.service_state = service_disconnected;
SetServiceMenuStatus();
CheckAndSetTrayIcon();
return(true);
}
int MyReStartService()
{
MyStopService();
Sleep(1000);
if (MyStartService()) {
return(true);
}
return(false);
}
bool
CheckIServiceStatus(BOOL warn)
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ssStatus;
// Open a handle to the SC Manager database.
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (NULL == schSCManager)
return(false);
schService = OpenService(schSCManager, _T("OpenVPNServiceInteractive"),
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);
return(false);
}
if (!QueryServiceStatus(schService, &ssStatus))
return(false);
if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{
/* warn that iservice is not started */
if (warn)
ShowLocalizedMsg(IDS_ERR_NOTSTARTED_ISERVICE);
return(false);
}
return true;
}
int CheckServiceStatus()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
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;
SetServiceMenuStatus();
return(false);
}
schService = OpenService(
schSCManager, // SCM database
_T("OpenVPNService"), // service name
SERVICE_QUERY_STATUS);
if (schService == NULL) {
/* open vpn service failed */
ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE);
o.service_state = service_noaccess;
SetServiceMenuStatus();
return(false);
}
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information structure
{
/* query failed */
ShowLocalizedMsg(IDS_ERR_QUERY_SERVICE);
return(false);
}
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
o.service_state = service_connected;
SetServiceMenuStatus();
SetTrayIcon(connected);
return(true);
}
else
{
o.service_state = service_disconnected;
SetServiceMenuStatus();
SetTrayIcon(disconnected);
return(false);
}
}