-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_ntservice.cpp
358 lines (255 loc) · 8.62 KB
/
util_ntservice.cpp
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/***
*
* BNBT Beta 8.0 - A C++ BitTorrent Tracker
* Copyright (C) 2003 Trevor Hogan
*
* CBTT variations (C) 2003-2005 Harold Feit
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
***/
/***********************************************************************
* NT Service code written by ConfusedFish and modified by Trevor Hogan *
***********************************************************************/
#include "bnbt.h"
#include "server.h"
#include "util_ntservice.h"
#include "util.h"
#include "config.h"
BOOL UTIL_NTServiceTest( )
{
CFG_Open( CFG_FILE );
//#undef BNBT_SERVICE_NAME
#define BNBT_SERVICE_NAME const_cast<LPSTR> (CFG_GetString( "cbtt_service_name", "CBTT Service" ).c_str())
CFG_Close( CFG_FILE );
// test if the service is installed or not
BOOL bResult = FALSE;
// open the Service Control Manager
SC_HANDLE hSCM = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( hSCM )
{
// try to open the service
SC_HANDLE hService = OpenService( hSCM, BNBT_SERVICE_NAME, SERVICE_QUERY_CONFIG );
if( hService )
{
bResult = TRUE;
CloseServiceHandle( hService );
}
CloseServiceHandle( hSCM );
}
return bResult;
}
BOOL UTIL_NTServiceInstall( )
{
CFG_Open( CFG_FILE );
//#undef BNBT_SERVICE_NAME
#define BNBT_SERVICE_NAME const_cast<LPSTR> (CFG_GetString( "cbtt_service_name", "CBTT Service" ).c_str())
CFG_Close( CFG_FILE );
// open the Service Control Manager
SC_HANDLE hSCM = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( !hSCM )
return FALSE;
// get the executable file path
char szFilePath[_MAX_PATH];
GetModuleFileName( NULL, szFilePath, sizeof( szFilePath ) );
// add the run as service parameter
strcat( szFilePath, " -s" );
// create the service
SC_HANDLE hService = CreateService( hSCM,
BNBT_SERVICE_NAME,
BNBT_SERVICE_NAME,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
szFilePath,
NULL,
NULL,
NULL,
NULL,
NULL );
if( !hService )
{
CloseServiceHandle( hSCM );
return FALSE;
}
// make registry entries to support logging messages to the EventLog
char szKey[256];
HKEY hKey = NULL;
strcpy( szKey, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\" );
strcat( szKey, BNBT_SERVICE_NAME );
if( RegCreateKey( HKEY_LOCAL_MACHINE, szKey, &hKey ) != ERROR_SUCCESS )
{
CloseServiceHandle( hService );
CloseServiceHandle( hSCM );
return FALSE;
}
RegSetValueEx( hKey, "EventMessageFile", 0, REG_EXPAND_SZ, (CONST BYTE *)szFilePath, strlen( szFilePath ) + 1 );
// set the supported types flags
DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
RegSetValueEx( hKey, "TypesSupported", 0, REG_DWORD, (CONST BYTE *)&dwData, sizeof( DWORD ) );
RegCloseKey( hKey );
UTIL_NTLogEvent( EVENTLOG_INFORMATION_TYPE, EVMSG_INSTALLED, BNBT_SERVICE_NAME );
// tidy up
CloseServiceHandle( hService );
CloseServiceHandle( hSCM );
return TRUE;
}
BOOL UTIL_NTServiceUninstall( )
{
CFG_Open( CFG_FILE );
//#undef BNBT_SERVICE_NAME
#define BNBT_SERVICE_NAME const_cast<LPSTR> (CFG_GetString( "cbtt_service_name", "CBTT Service" ).c_str())
CFG_Close( CFG_FILE );
// open the Service Control Manager
SC_HANDLE hSCM = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( !hSCM )
return FALSE;
BOOL bResult = FALSE;
SC_HANDLE hService = OpenService( hSCM, BNBT_SERVICE_NAME, DELETE );
if( hService )
{
if( DeleteService( hService ) )
{
UTIL_NTLogEvent( EVENTLOG_INFORMATION_TYPE, EVMSG_REMOVED, BNBT_SERVICE_NAME );
bResult = TRUE;
}
else
UTIL_NTLogEvent( EVENTLOG_ERROR_TYPE, EVMSG_NOTREMOVED, BNBT_SERVICE_NAME );
CloseServiceHandle( hService );
}
CloseServiceHandle( hSCM );
return bResult;
}
BOOL UTIL_NTServiceStart( )
{
CFG_Open( CFG_FILE );
//#undef BNBT_SERVICE_NAME
#define BNBT_SERVICE_NAME const_cast<LPSTR> (CFG_GetString( "cbtt_service_name", "CBTT Service" ).c_str())
CFG_Close( CFG_FILE );
BOOL bResult = FALSE;
// open the Service Control Manager
SC_HANDLE hSCM = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( hSCM )
{
// try to open the service
SC_HANDLE hService = OpenService( hSCM, BNBT_SERVICE_NAME, SERVICE_START );
if( hService )
{
StartService( hService, 0, NULL );
bResult = TRUE;
CloseServiceHandle( hService );
}
// tidy up
CloseServiceHandle( hSCM );
}
return bResult;
}
BOOL UTIL_NTServiceStop( )
{
CFG_Open( CFG_FILE );
//#undef BNBT_SERVICE_NAME
#define BNBT_SERVICE_NAME const_cast<LPSTR> (CFG_GetString( "cbtt_service_name", "CBTT Service" ).c_str())
CFG_Close( CFG_FILE );
BOOL bResult = FALSE;
// open the Service Control Manager
SC_HANDLE hSCM = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( hSCM )
{
// try to open the service
SC_HANDLE hService = OpenService( hSCM, BNBT_SERVICE_NAME, SERVICE_STOP );
if( hService )
{
SERVICE_STATUS ssStatus;
if( ControlService( hService, SERVICE_CONTROL_STOP, &ssStatus ) )
bResult = TRUE;
CloseServiceHandle( hService );
}
CloseServiceHandle( hSCM );
}
return bResult;
}
void UTIL_NTLogEvent( WORD wType, DWORD dwID, const char *pszS1, const char *pszS2, const char *pszS3 )
{
CFG_Open( CFG_FILE );
//#undef BNBT_SERVICE_NAME
#define BNBT_SERVICE_NAME const_cast<LPSTR> (CFG_GetString( "cbtt_service_name", "CBTT Service" ).c_str())
CFG_Close( CFG_FILE );
// make an entry into the application event log
const char *ps[3];
ps[0] = pszS1;
ps[1] = pszS2;
ps[2] = pszS3;
int iStr = 0;
for( int i = 0; i < 3; i++ )
{
if( ps[i] != NULL )
iStr++;
}
// check if the event source has been registered, and if not then register it now
HANDLE hNTEventSource = RegisterEventSource( NULL, BNBT_SERVICE_NAME );
if( hNTEventSource )
{
ReportEvent( hNTEventSource, wType, 0, dwID, NULL, iStr, 0, ps, NULL );
DeregisterEventSource( hNTEventSource );
}
}
void WINAPI NTServiceHandler( DWORD dwOpcode )
{
CFG_Open( CFG_FILE );
//#undef BNBT_SERVICE_NAME
#define BNBT_SERVICE_NAME const_cast<LPSTR> (CFG_GetString( "cbtt_service_name", "CBTT Service" ).c_str())
CFG_Close( CFG_FILE );
// handle commands from the Service Control Manager
if( dwOpcode == SERVICE_CONTROL_STOP )
{
gssStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus( ghServiceStatus, &gssStatus );
gpServer->Kill( );
}
SetServiceStatus( ghServiceStatus, &gssStatus );
}
void WINAPI NTServiceMain( DWORD dwArgc, LPTSTR *lpszArgv )
{
gssStatus.dwCheckPoint = 0;
gssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
gssStatus.dwCurrentState = SERVICE_START_PENDING;
gssStatus.dwServiceSpecificExitCode = 0;
gssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
gssStatus.dwWaitHint = 0;
gssStatus.dwWin32ExitCode = 0;
// register the control request handler
if( !( ghServiceStatus = RegisterServiceCtrlHandler( BNBT_SERVICE_NAME, NTServiceHandler ) ) )
return;
SetServiceStatus( ghServiceStatus, &gssStatus );
gssStatus.dwWin32ExitCode = 0;
gssStatus.dwCheckPoint = 0;
gssStatus.dwWaitHint = 0;
UTIL_NTLogEvent( EVENTLOG_INFORMATION_TYPE, EVMSG_STARTED );
// tell the Service Control Manager we started
gssStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus( ghServiceStatus, &gssStatus );
gssStatus.dwWin32ExitCode = 0;
gssStatus.dwCheckPoint = 0;
gssStatus.dwWaitHint = 0;
// run the tracker
bnbtmain( );
// tell the Service Control Manager we stopped
UTIL_NTLogEvent( EVENTLOG_INFORMATION_TYPE, EVMSG_STOPPED );
gssStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus( ghServiceStatus, &gssStatus );
}
SERVICE_STATUS_HANDLE ghServiceStatus;
SERVICE_STATUS gssStatus;