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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
/*++
Copyright (c) 1991 Microsoft Corporation
Module Name:
services.c
Abstract:
This is the service dispatcher for the security process. It contains
the service dispatcher initialization routine and the routines to
load the DLL for the individual serices and execute them.
Author:
Rajen Shah (rajens) 11-Apr-1991
[Environment:]
User Mode - Win32
Revision History:
11-Apr-1991 RajenS
created
27-Sep-1991 JohnRo
More work toward UNICODE.
24-Jan-1991 CliffV
Converted to be service dispatcher for the security process.
--*/
//
// INCLUDES
//
#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <windows.h>
#include <lmcons.h>
#include <lmerr.h> // NERR_ and ERROR_ equates.
#include <lmsname.h>
#include <winsvc.h>
#include <crypt.h>
#include <ntsam.h>
#include <logonmsv.h>
#if DBG
#define IF_DEBUG() if (TRUE)
#else
#define IF_DEBUG() if (FALSE)
#endif
#define IN_RANGE(SomeValue, SomeMin, SomeMax) \
( ((SomeValue) >= (SomeMin)) && ((SomeValue) <= (SomeMax)) )
//
// SET_SERVICE_EXITCODE() sets the SomeApiStatus to NetCodeVariable
// if it is within the NERR_BASE and NERR_MAX range. Otherwise,
// Win32CodeVariable is set.
//
#define SET_SERVICE_EXITCODE(SomeApiStatus, Win32CodeVariable, NetCodeVariable) \
{ \
if ((SomeApiStatus) == NERR_Success) { \
(Win32CodeVariable) = NO_ERROR; \
(NetCodeVariable) = NERR_Success; \
} else if (! IN_RANGE((SomeApiStatus), MIN_LANMAN_MESSAGE_ID, \
MAX_LANMAN_MESSAGE_ID)) { \
(Win32CodeVariable) = (DWORD) (SomeApiStatus); \
(NetCodeVariable) = (DWORD) (SomeApiStatus); \
} else { \
(Win32CodeVariable) = ERROR_SERVICE_SPECIFIC_ERROR; \
(NetCodeVariable) = (DWORD) (SomeApiStatus); \
} \
}
typedef DWORD (*PNETLOGON_MAIN) ( \
IN DWORD dwNumServicesArgs, \
IN LPTSTR *lpServiceArgVectors \
);
VOID
DummyControlHandler(
IN DWORD opcode
)
/*++
Routine Description:
Process and respond to a control signal from the service controller.
Arguments:
opcode - Supplies a value which specifies the action for the Netlogon
service to perform.
Return Value:
None.
NOTE : this is a dummy handler, used to uninstall the netlogon service
when we unable to load netlogon dll.
--*/
{
IF_DEBUG() {
DbgPrint( "[Security Process] in control handler\n");
}
return;
}
VOID
SrvLoadNetlogon (
IN DWORD dwNumServicesArgs,
IN LPTSTR *lpServiceArgVectors
)
/*++
Routine Description:
This routine is the 'main' routine for the netlogon service. It loads
Netlogon.dll (which contains the remainder of the service) and
calls the main entry point there.
Arguments:
dwNumServicesArgs - Number of arguments in lpServiceArgVectors.
lpServiceArgVectors - Argument strings.
Return Value:
return nothing.
Note:
--*/
{
NET_API_STATUS NetStatus;
HANDLE NetlogonDllHandle = NULL;
PNETLOGON_MAIN NetlogonMain;
DWORD ReturnStatus;
SERVICE_STATUS_HANDLE ServiceHandle;
SERVICE_STATUS ServiceStatus;
//
// Load netlogon.dll
//
NetlogonDllHandle = LoadLibraryA( "Netlogon" );
if ( NetlogonDllHandle == NULL ) {
NetStatus = GetLastError();
IF_DEBUG() {
DbgPrint( "[Security process] "
"load library netlogon.dll failed %ld\n", NetStatus );
}
goto Cleanup;
}
//
// Find the main entry point for the netlogon service.
//
NetlogonMain = (PNETLOGON_MAIN)
GetProcAddress( NetlogonDllHandle, "NlNetlogonMain");
if ( NetlogonMain == NULL ) {
NetStatus = GetLastError();
IF_DEBUG() {
DbgPrint( "[Security process] GetProcAddress failed %ld\n",
NetStatus );
}
goto Cleanup;
}
//
// Call the Netlogon service.
//
ReturnStatus = (*NetlogonMain)(
dwNumServicesArgs,
lpServiceArgVectors
);
//
// Unload the library and return.
//
(VOID) FreeLibrary( NetlogonDllHandle );
return;
Cleanup:
if ( NetlogonDllHandle != NULL ) {
(VOID) FreeLibrary( NetlogonDllHandle );
}
//
// register netlogon to service controller
//
ServiceHandle =
RegisterServiceCtrlHandler( SERVICE_NETLOGON, DummyControlHandler);
if (ServiceHandle != (SERVICE_STATUS_HANDLE) NULL) {
//
// inform service controller that the service can't start.
//
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
SERVICE_ACCEPT_PAUSE_CONTINUE;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
SET_SERVICE_EXITCODE(
NetStatus,
ServiceStatus.dwWin32ExitCode,
ServiceStatus.dwServiceSpecificExitCode
);
if( !SetServiceStatus( ServiceHandle, &ServiceStatus ) ) {
IF_DEBUG() {
DbgPrint( "[Security process] SetServiceStatus failed %ld\n",
GetLastError() );
}
}
}
else {
IF_DEBUG() {
DbgPrint( "[Security process] "
"RegisterServiceCtrlHandler failed %ld\n",
GetLastError() );
}
}
return;
}
//
// Dispatch table for all services. Passed to NetServiceStartCtrlDispatcher.
//
// Add new service entries here.
//
SERVICE_TABLE_ENTRY SecurityServiceDispatchTable[] = {
{ SERVICE_NETLOGON, SrvLoadNetlogon },
{ NULL, NULL }
};
DWORD
ServiceDispatcherThread (
LPVOID Parameter
)
/*++
Routine Description:
This routine synchronizes with the service controller. It waits
for the service controller to set the SECURITY_SERVICES_STARTED
event then starts up the main
thread that is going to handle the control requests from the service
controller.
It basically sets up the ControlDispatcher and, on return, exits from
this main thread. The call to NetServiceStartCtrlDispatcher does
not return until all services have terminated, and this process can
go away.
It will be up to the ControlDispatcher thread to start/stop/pause/continue
any services. If a service is to be started, it will create a thread
and then call the main routine of that service.
Arguments:
EventHandle - Event handle to wait on before continuing.
Return Value:
Exit status of thread.
Note:
--*/
{
DWORD WaitStatus;
HANDLE EventHandle;
BOOL StartStatus;
//
// Create an event for us to wait on.
//
EventHandle = CreateEventW( NULL, // No special security
TRUE, // Must be manually reset
FALSE, // The event is initially not signalled
SECURITY_SERVICES_STARTED );
if ( EventHandle == NULL ) {
WaitStatus = GetLastError();
//
// If the event already exists,
// the service controller already created it. Just open it.
//
if ( WaitStatus == ERROR_ALREADY_EXISTS ) {
EventHandle = OpenEventW( EVENT_ALL_ACCESS,
FALSE,
SECURITY_SERVICES_STARTED );
if ( EventHandle == NULL ) {
WaitStatus = GetLastError();
IF_DEBUG() {
DbgPrint("[Security process] OpenEvent failed %ld\n",
WaitStatus );
}
return WaitStatus;
}
} else {
IF_DEBUG() {
DbgPrint("[Security process] CreateEvent failed %ld\n",
WaitStatus);
}
return WaitStatus;
}
}
//
// Wait for the service controller to come up.
//
WaitStatus = WaitForSingleObject( (HANDLE) EventHandle, (DWORD) -1 );
(VOID) CloseHandle( EventHandle );
if ( WaitStatus != 0 ) {
IF_DEBUG() {
DbgPrint("[Security process] WaitForSingleObject failed %ld\n",
WaitStatus );
}
return WaitStatus;
}
//
// Call NetServiceStartCtrlDispatcher to set up the control interface.
// The API won't return until all services have been terminated. At that
// point, we just exit.
//
StartStatus =
StartServiceCtrlDispatcher ( SecurityServiceDispatchTable );
IF_DEBUG() {
DbgPrint("[Security process] return from StartCtrlDispatcher %ld \n",
StartStatus );
}
return StartStatus;
UNREFERENCED_PARAMETER(Parameter);
}
NTSTATUS
ServiceInit (
VOID
)
/*++
Routine Description:
This is a main routine for the service dispatcher of the security process.
It starts up a thread responsible for coordinating with the
service controller.
Arguments:
NONE.
Return Value:
Status of the thread creation operation.
Note:
--*/
{
DWORD ThreadId;
HANDLE ThreadHandle;
//
// The control dispatcher runs in a thread of its own.
//
ThreadHandle = CreateThread(
NULL, // No special thread attributes
0, // No special stack size
&ServiceDispatcherThread,
NULL, // No special parameter
0, // No special creation flags
&ThreadId);
if ( ThreadHandle == NULL ) {
return (NTSTATUS) GetLastError();
}
return STATUS_SUCCESS;
}
|