[BOOTDATA][LSASRV] Add the security service(s)
[reactos.git] / dll / win32 / lsasrv / service.c
1 /*
2 * PROJECT: Local Security Authority Server DLL
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/lsasrv/service.c
5 * PURPOSE: Security service
6 * COPYRIGHT: Copyright 2016, 2019 Eric Kohl <eric.kohl@reactos.org>
7 */
8
9 /* INCLUDES ****************************************************************/
10
11 #include "lsasrv.h"
12 #include <winsvc.h>
13
14 VOID WINAPI I_ScIsSecurityProcess(VOID);
15
16 static VOID WINAPI SamSsServiceMain(DWORD dwArgc, PWSTR *pszArgv);
17
18 SERVICE_TABLE_ENTRYW ServiceTable[] =
19 {
20 {L"SAMSS", SamSsServiceMain},
21 {NULL, NULL}
22 };
23
24
25 /* FUNCTIONS ***************************************************************/
26
27 static
28 VOID
29 WINAPI
30 SamSsControlHandler(
31 _In_ DWORD fdwControl)
32 {
33 TRACE("SamSsControlHandler(%lu)\n", fdwControl);
34 }
35
36
37 static
38 VOID
39 WINAPI
40 SamSsServiceMain(
41 _In_ DWORD dwArgc,
42 _In_ PWSTR *pszArgv)
43 {
44 SERVICE_STATUS_HANDLE hStatus;
45 SERVICE_STATUS ServiceStatus;
46
47 TRACE("SamSsServiceMain(%lu %p)\n", dwArgc, pszArgv);
48
49 hStatus = RegisterServiceCtrlHandlerW(L"SAMSS",
50 SamSsControlHandler);
51 if (hStatus == NULL)
52 return;
53
54 ServiceStatus.dwServiceType = SERVICE_WIN32_SHARE_PROCESS;
55 ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
56 ServiceStatus.dwControlsAccepted = 0;
57 ServiceStatus.dwWin32ExitCode = ERROR_SUCCESS;
58 ServiceStatus.dwServiceSpecificExitCode = ERROR_SUCCESS;
59 ServiceStatus.dwCheckPoint = 1;
60 ServiceStatus.dwWaitHint = 0x7530;
61
62 SetServiceStatus(hStatus, &ServiceStatus);
63
64 ServiceStatus.dwCurrentState = SERVICE_RUNNING;
65 ServiceStatus.dwCheckPoint = 0;
66 ServiceStatus.dwWaitHint = 0;
67
68 SetServiceStatus(hStatus, &ServiceStatus);
69 }
70
71
72 static
73 DWORD
74 WINAPI
75 DispatcherThread(
76 _In_ PVOID pParameter)
77 {
78 HANDLE hEvent;
79 DWORD dwError;
80
81 TRACE("DispatcherThread(%p)\n", pParameter);
82
83 /* Create or open the SECURITY_SERVICES_STARTED event */
84 hEvent = CreateEventW(NULL,
85 TRUE,
86 FALSE,
87 L"SECURITY_SERVICES_STARTED");
88 if (hEvent == NULL)
89 {
90 dwError = GetLastError();
91 if (dwError != ERROR_ALREADY_EXISTS)
92 return dwError;
93
94 hEvent = OpenEventW(SYNCHRONIZE,
95 FALSE,
96 L"SECURITY_SERVICES_STARTED");
97 if (hEvent == NULL)
98 return GetLastError();
99 }
100
101 /* Wait for the SECURITY_SERVICES_STARTED event to be signaled */
102 TRACE("Waiting for the SECURITY_SERVICES_STARTED event!\n");
103 dwError = WaitForSingleObject(hEvent, INFINITE);
104 TRACE("WaitForSingleObject returned %lu\n", dwError);
105
106 /* Close the event handle */
107 CloseHandle(hEvent);
108
109 /* Fail, if the event was not signaled */
110 if (dwError != WAIT_OBJECT_0)
111 {
112 ERR("Wait failed!\n");
113 return dwError;
114 }
115
116 /* This is the security process */
117 I_ScIsSecurityProcess();
118
119 /* Start the services */
120 TRACE("Start the security services!\n");
121 if (!StartServiceCtrlDispatcherW(ServiceTable))
122 return GetLastError();
123
124 TRACE("Done!\n");
125
126 return ERROR_SUCCESS;
127 }
128
129
130 NTSTATUS
131 WINAPI
132 ServiceInit(VOID)
133 {
134 HANDLE hThread;
135 DWORD dwThreadId;
136
137 TRACE("ServiceInit()\n");
138
139 hThread = CreateThread(NULL,
140 0,
141 DispatcherThread,
142 NULL,
143 0,
144 &dwThreadId);
145 if (hThread == NULL)
146 return (NTSTATUS)GetLastError();
147
148 return STATUS_SUCCESS;
149 }
150
151 /* EOF */