/
w32time.c
333 lines (278 loc) · 8.49 KB
/
w32time.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
/*
* PROJECT: ReactOS W32Time Service
* LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
* PURPOSE: Create W32Time Service that reqularly syncs clock to Internet Time
* COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
* Copyright 2018 Doug Lyons
*/
#include "w32time.h"
#include <debug.h>
#include <strsafe.h>
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
static WCHAR ServiceName[] = L"W32Time";
int InitService(VOID);
BOOL
SystemSetTime(LPSYSTEMTIME lpSystemTime)
{
HANDLE hToken;
DWORD PrevSize;
TOKEN_PRIVILEGES priv, previouspriv;
BOOL Ret = FALSE;
/*
* Enable the SeSystemtimePrivilege privilege
*/
if (OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&hToken))
{
priv.PrivilegeCount = 1;
priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (LookupPrivilegeValueW(NULL,
SE_SYSTEMTIME_NAME,
&priv.Privileges[0].Luid))
{
if (AdjustTokenPrivileges(hToken,
FALSE,
&priv,
sizeof(previouspriv),
&previouspriv,
&PrevSize) &&
GetLastError() == ERROR_SUCCESS)
{
/*
* We successfully enabled it, we're permitted to change the time.
*/
Ret = SetSystemTime(lpSystemTime);
/*
* For the sake of security, restore the previous status again
*/
if (previouspriv.PrivilegeCount > 0)
{
AdjustTokenPrivileges(hToken,
FALSE,
&previouspriv,
0,
NULL,
0);
}
}
}
CloseHandle(hToken);
}
return Ret;
}
/*
* NTP servers state the number of seconds passed since
* 1st Jan, 1900. The time returned from the server
* needs adding to that date to get the current Gregorian time
*/
static DWORD
UpdateSystemTime(ULONG ulTime)
{
FILETIME ftNew;
LARGE_INTEGER li;
SYSTEMTIME stNew;
/* Time at 1st Jan 1900 */
stNew.wYear = 1900;
stNew.wMonth = 1;
stNew.wDay = 1;
stNew.wHour = 0;
stNew.wMinute = 0;
stNew.wSecond = 0;
stNew.wMilliseconds = 0;
/* Convert to a file time */
if (!SystemTimeToFileTime(&stNew, &ftNew))
{
return GetLastError();
}
/* Add on the time passed since 1st Jan 1900 */
li = *(LARGE_INTEGER *)&ftNew;
li.QuadPart += (LONGLONG)10000000 * ulTime;
ftNew = * (FILETIME *)&li;
/* Convert back to a system time */
if (!FileTimeToSystemTime(&ftNew, &stNew))
{
return GetLastError();
}
if (!SystemSetTime(&stNew))
{
return GetLastError();
}
return ERROR_SUCCESS;
}
static DWORD
GetIntervalSetting(VOID)
{
HKEY hKey;
DWORD dwData;
DWORD dwSize = sizeof(dwData);
LONG lRet;
dwData = 0;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\Services\\W32Time\\TimeProviders\\NtpClient",
0,
KEY_QUERY_VALUE,
&hKey) == ERROR_SUCCESS)
{
/* This key holds the update interval in seconds
* It is useful for testing to set it to a value of 10 (Decimal)
* This will cause the clock to try and update every 10 seconds
* So you can change the time and expect it to be set back correctly in 10-20 seconds
*/
lRet = RegQueryValueExW(hKey,
L"SpecialPollInterval",
NULL,
NULL,
(LPBYTE)&dwData,
&dwSize);
RegCloseKey(hKey);
}
if (lRet != ERROR_SUCCESS)
return 0;
else
return dwData;
}
DWORD
SetTime(VOID)
{
ULONG ulTime;
LONG lRet;
HKEY hKey;
WCHAR szData[MAX_VALUE_NAME] = L"";
DWORD cbName = sizeof(szData);
DPRINT("Entered SetTime.\n");
lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
0,
KEY_QUERY_VALUE,
&hKey);
if (lRet != ERROR_SUCCESS)
{
return lRet;
}
lRet = RegQueryValueExW(hKey, NULL, NULL, NULL, (LPBYTE)szData, &cbName);
if (lRet == ERROR_SUCCESS)
{
cbName = sizeof(szData);
lRet = RegQueryValueExW(hKey, szData, NULL, NULL, (LPBYTE)szData, &cbName);
}
RegCloseKey(hKey);
DPRINT("Time Server is '%S'.\n", szData);
ulTime = GetServerTime(szData);
if (ulTime != 0)
{
return UpdateSystemTime(ulTime);
}
else
return ERROR_GEN_FAILURE;
}
/* Control handler function */
VOID WINAPI
ControlHandler(DWORD request)
{
switch (request)
{
case SERVICE_CONTROL_STOP:
DPRINT("W32Time Service stopped.\n");
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hStatus, &ServiceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
DPRINT("W32Time Service stopped.\n");
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hStatus, &ServiceStatus);
return;
default:
break;
}
/* Report current status */
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
VOID
WINAPI
ServiceMain(DWORD argc, LPWSTR *argv)
{
int result;
DWORD dwPollInterval;
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
hStatus = RegisterServiceCtrlHandlerW(ServiceName,
ControlHandler);
if (!hStatus)
{
/* Registering Control Handler failed */
return;
}
/* We report the running status to SCM. */
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(hStatus, &ServiceStatus);
/* The worker loop of a service */
while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
{
dwPollInterval = GetIntervalSetting();
result = SetTime();
if (result)
DPRINT("W32Time Service failed to set clock.\n");
else
DPRINT("W32Time Service successfully set clock.\n");
if (result)
{
/* In general we do not want to stop this service for a single
* Internet read failure but there may be other reasons for which
* we really might want to stop it.
* Therefore this code is left here to make it easy to stop this
* service when the correct conditions can be determined, but it
* is left commented out.
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
*/
}
Sleep(dwPollInterval * 1000);
}
return;
}
BOOL WINAPI
DllMain(HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hinstDLL);
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
DWORD WINAPI
W32TimeSyncNow(LPCWSTR cmdline,
UINT blocking,
UINT flags)
{
DWORD result;
result = SetTime();
if (result)
{
DPRINT("W32TimeSyncNow failed and clock not set.\n");
}
else
{
DPRINT("W32TimeSyncNow succeeded and clock set.\n");
}
return result;
}