1 | /* $Id: VBoxService-win.cpp 25959 2010-01-21 14:02:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Guest Additions Service Skeleton, Windows Specific Parts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <iprt/assert.h>
|
---|
27 | #include <iprt/err.h>
|
---|
28 | #include <VBox/VBoxGuestLib.h>
|
---|
29 | #include "VBoxServiceInternal.h"
|
---|
30 |
|
---|
31 | #include <Windows.h>
|
---|
32 | #include <process.h>
|
---|
33 | #include <aclapi.h>
|
---|
34 |
|
---|
35 | DWORD g_rcWinService = 0;
|
---|
36 | SERVICE_STATUS_HANDLE g_hWinServiceStatus = NULL;
|
---|
37 |
|
---|
38 | void WINAPI VBoxServiceWinMain (DWORD argc, LPTSTR *argv);
|
---|
39 |
|
---|
40 | static SERVICE_TABLE_ENTRY const g_aServiceTable[]=
|
---|
41 | {
|
---|
42 | {VBOXSERVICE_NAME, VBoxServiceWinMain},
|
---|
43 | {NULL,NULL}
|
---|
44 | };
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @todo Format code style.
|
---|
49 | * @todo Add full unicode support.
|
---|
50 | * @todo Add event log capabilities / check return values.
|
---|
51 | */
|
---|
52 | DWORD VBoxServiceWinAddAceToObjectsSecurityDescriptor(LPTSTR pszObjName,
|
---|
53 | SE_OBJECT_TYPE ObjectType,
|
---|
54 | LPTSTR pszTrustee,
|
---|
55 | TRUSTEE_FORM TrusteeForm,
|
---|
56 | DWORD dwAccessRights,
|
---|
57 | ACCESS_MODE AccessMode,
|
---|
58 | DWORD dwInheritance)
|
---|
59 | {
|
---|
60 | DWORD dwRes = 0;
|
---|
61 | PACL pOldDACL = NULL, pNewDACL = NULL;
|
---|
62 | PSECURITY_DESCRIPTOR pSD = NULL;
|
---|
63 | EXPLICIT_ACCESS ea;
|
---|
64 |
|
---|
65 | if (NULL == pszObjName)
|
---|
66 | return ERROR_INVALID_PARAMETER;
|
---|
67 |
|
---|
68 | /* Get a pointer to the existing DACL. */
|
---|
69 | dwRes = GetNamedSecurityInfo(pszObjName, ObjectType,
|
---|
70 | DACL_SECURITY_INFORMATION,
|
---|
71 | NULL, NULL, &pOldDACL, NULL, &pSD);
|
---|
72 | if (ERROR_SUCCESS != dwRes)
|
---|
73 | {
|
---|
74 | if (dwRes == ERROR_FILE_NOT_FOUND)
|
---|
75 | VBoxServiceError("AddAceToObjectsSecurityDescriptor: Object not found/installed: %s\n", pszObjName);
|
---|
76 | else
|
---|
77 | VBoxServiceError("AddAceToObjectsSecurityDescriptor: GetNamedSecurityInfo: Error %u\n", dwRes);
|
---|
78 | goto Cleanup;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /* Initialize an EXPLICIT_ACCESS structure for the new ACE. */
|
---|
82 | ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
|
---|
83 | ea.grfAccessPermissions = dwAccessRights;
|
---|
84 | ea.grfAccessMode = AccessMode;
|
---|
85 | ea.grfInheritance= dwInheritance;
|
---|
86 | ea.Trustee.TrusteeForm = TrusteeForm;
|
---|
87 | ea.Trustee.ptstrName = pszTrustee;
|
---|
88 |
|
---|
89 | /* Create a new ACL that merges the new ACE into the existing DACL. */
|
---|
90 | dwRes = SetEntriesInAcl(1, &ea, pOldDACL, &pNewDACL);
|
---|
91 | if (ERROR_SUCCESS != dwRes)
|
---|
92 | {
|
---|
93 | VBoxServiceError("AddAceToObjectsSecurityDescriptor: SetEntriesInAcl: Error %u\n", dwRes);
|
---|
94 | goto Cleanup;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* Attach the new ACL as the object's DACL. */
|
---|
98 | dwRes = SetNamedSecurityInfo(pszObjName, ObjectType,
|
---|
99 | DACL_SECURITY_INFORMATION,
|
---|
100 | NULL, NULL, pNewDACL, NULL);
|
---|
101 | if (ERROR_SUCCESS != dwRes)
|
---|
102 | {
|
---|
103 | VBoxServiceError("AddAceToObjectsSecurityDescriptor: SetNamedSecurityInfo: Error %u\n", dwRes);
|
---|
104 | goto Cleanup;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** @todo get rid of that spaghetti jump ... */
|
---|
108 | Cleanup:
|
---|
109 |
|
---|
110 | if(pSD != NULL)
|
---|
111 | LocalFree((HLOCAL) pSD);
|
---|
112 | if(pNewDACL != NULL)
|
---|
113 | LocalFree((HLOCAL) pNewDACL);
|
---|
114 |
|
---|
115 | return dwRes;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | BOOL VBoxServiceWinSetStatus(DWORD dwStatus, DWORD dwCheckPoint)
|
---|
120 | {
|
---|
121 | if (NULL == g_hWinServiceStatus) /* Program could be in testing mode, so no service environment available. */
|
---|
122 | return FALSE;
|
---|
123 |
|
---|
124 | VBoxServiceVerbose(2, "Setting service status to: %ld\n", dwStatus);
|
---|
125 | g_rcWinService = dwStatus;
|
---|
126 |
|
---|
127 | SERVICE_STATUS ss;
|
---|
128 | ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
---|
129 | ss.dwCurrentState = g_rcWinService;
|
---|
130 | ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
---|
131 | ss.dwWin32ExitCode = NO_ERROR;
|
---|
132 | ss.dwServiceSpecificExitCode = 0; /* Not used */
|
---|
133 | ss.dwCheckPoint = dwCheckPoint;
|
---|
134 | ss.dwWaitHint = 3000;
|
---|
135 |
|
---|
136 | return SetServiceStatus(g_hWinServiceStatus, &ss);
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | int VBoxServiceWinSetDesc(SC_HANDLE hService)
|
---|
141 | {
|
---|
142 | /* On W2K+ there's ChangeServiceConfig2() which lets us set some fields
|
---|
143 | like a longer service description. */
|
---|
144 | #ifndef TARGET_NT4
|
---|
145 | SERVICE_DESCRIPTION desc;
|
---|
146 | /** @todo On Vista+ SERVICE_DESCRIPTION also supports localized strings! */
|
---|
147 | desc. lpDescription = VBOXSERVICE_DESCRIPTION;
|
---|
148 | if (FALSE == ChangeServiceConfig2(hService,
|
---|
149 | SERVICE_CONFIG_DESCRIPTION, /* Service info level */
|
---|
150 | &desc))
|
---|
151 | {
|
---|
152 | VBoxServiceError("Cannot set the service description! Error: %ld\n", GetLastError());
|
---|
153 | return 1;
|
---|
154 | }
|
---|
155 | #endif
|
---|
156 | return VINF_SUCCESS;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | int VBoxServiceWinInstall(void)
|
---|
161 | {
|
---|
162 | SC_HANDLE hService, hSCManager;
|
---|
163 | TCHAR imagePath[MAX_PATH] = { 0 };
|
---|
164 |
|
---|
165 | GetModuleFileName(NULL,imagePath,MAX_PATH);
|
---|
166 | VBoxServiceVerbose(1, "Installing service ...\n");
|
---|
167 |
|
---|
168 | hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
|
---|
169 |
|
---|
170 | if (NULL == hSCManager)
|
---|
171 | {
|
---|
172 | VBoxServiceError("Could not open SCM! Error: %ld\n", GetLastError());
|
---|
173 | return 1;
|
---|
174 | }
|
---|
175 |
|
---|
176 | hService = ::CreateService (hSCManager,
|
---|
177 | VBOXSERVICE_NAME, VBOXSERVICE_FRIENDLY_NAME,
|
---|
178 | SERVICE_ALL_ACCESS,
|
---|
179 | SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
|
---|
180 | SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
|
---|
181 | imagePath, NULL, NULL, NULL, NULL, NULL);
|
---|
182 | int rc = VINF_SUCCESS;
|
---|
183 | if (NULL == hService)
|
---|
184 | {
|
---|
185 | DWORD dwErr = GetLastError();
|
---|
186 | switch (dwErr)
|
---|
187 | {
|
---|
188 |
|
---|
189 | case ERROR_SERVICE_EXISTS:
|
---|
190 |
|
---|
191 | VBoxServiceVerbose(1, "Service already exists, just updating the service config.\n");
|
---|
192 | hService = OpenService (hSCManager,
|
---|
193 | VBOXSERVICE_NAME,
|
---|
194 | SERVICE_ALL_ACCESS);
|
---|
195 | if (NULL == hService)
|
---|
196 | {
|
---|
197 | VBoxServiceError("Could not open service! Error: %ld\n", GetLastError());
|
---|
198 | rc = 1;
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | if (ChangeServiceConfig (hService,
|
---|
203 | SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
|
---|
204 | SERVICE_DEMAND_START,
|
---|
205 | SERVICE_ERROR_NORMAL,
|
---|
206 | imagePath,
|
---|
207 | NULL,
|
---|
208 | NULL,
|
---|
209 | NULL,
|
---|
210 | NULL,
|
---|
211 | NULL,
|
---|
212 | VBOXSERVICE_FRIENDLY_NAME))
|
---|
213 | {
|
---|
214 | VBoxServiceVerbose(1, "The service config has been successfully updated.\n");
|
---|
215 | }
|
---|
216 | else
|
---|
217 | {
|
---|
218 | VBoxServiceError("Could not change service config! Error: %ld\n", GetLastError());
|
---|
219 | rc = 1;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | break;
|
---|
223 |
|
---|
224 | default:
|
---|
225 |
|
---|
226 | VBoxServiceError("Could not create service! Error: %ld\n", dwErr);
|
---|
227 | rc = 1;
|
---|
228 | break;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | else
|
---|
232 | {
|
---|
233 | VBoxServiceVerbose(0, "Service successfully installed!\n");
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (RT_SUCCESS(rc))
|
---|
237 | rc = VBoxServiceWinSetDesc(hService);
|
---|
238 |
|
---|
239 | CloseServiceHandle(hService);
|
---|
240 | CloseServiceHandle(hSCManager);
|
---|
241 | return rc;
|
---|
242 | }
|
---|
243 |
|
---|
244 | int VBoxServiceWinUninstall(void)
|
---|
245 | {
|
---|
246 | SC_HANDLE hService, hSCManager;
|
---|
247 | hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
|
---|
248 |
|
---|
249 | VBoxServiceVerbose(1, "Uninstalling service ...\n");
|
---|
250 |
|
---|
251 | if (NULL == hSCManager) {
|
---|
252 | VBoxServiceError("Could not open SCM! Error: %d\n", GetLastError());
|
---|
253 | return 1;
|
---|
254 | }
|
---|
255 |
|
---|
256 | hService = OpenService (hSCManager, VBOXSERVICE_NAME, SERVICE_ALL_ACCESS );
|
---|
257 | if (NULL == hService) {
|
---|
258 | VBoxServiceError("Could not open service! Error: %d\n", GetLastError());
|
---|
259 | CloseServiceHandle (hSCManager);
|
---|
260 | return 1;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (FALSE == DeleteService (hService))
|
---|
264 | {
|
---|
265 | VBoxServiceError("Could not remove service! Error: %d\n", GetLastError());
|
---|
266 | CloseServiceHandle (hService);
|
---|
267 | CloseServiceHandle (hSCManager);
|
---|
268 | return 1;
|
---|
269 | }
|
---|
270 | else
|
---|
271 | {
|
---|
272 | HKEY hKey = NULL;
|
---|
273 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\System", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) {
|
---|
274 | RegDeleteKey(hKey, VBOXSERVICE_NAME);
|
---|
275 | RegCloseKey(hKey);
|
---|
276 | }
|
---|
277 |
|
---|
278 | VBoxServiceVerbose(0, "Service successfully uninstalled!\n");
|
---|
279 | }
|
---|
280 |
|
---|
281 | CloseServiceHandle(hService);
|
---|
282 | CloseServiceHandle(hSCManager);
|
---|
283 |
|
---|
284 | return 0;
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | int VBoxServiceWinStart(void)
|
---|
289 | {
|
---|
290 | int rc = VINF_SUCCESS;
|
---|
291 |
|
---|
292 | #ifndef TARGET_NT4
|
---|
293 | /* Create a well-known SID for the "Builtin Users" group. */
|
---|
294 | PSID pBuiltinUsersSID = NULL;
|
---|
295 | SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_LOCAL_SID_AUTHORITY;
|
---|
296 |
|
---|
297 | if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
|
---|
298 | SECURITY_LOCAL_RID,
|
---|
299 | 0, 0, 0, 0, 0, 0, 0,
|
---|
300 | &pBuiltinUsersSID))
|
---|
301 | {
|
---|
302 | VBoxServiceError("AllocateAndInitializeSid: Error %u\n", GetLastError());
|
---|
303 | }
|
---|
304 | else
|
---|
305 | {
|
---|
306 | DWORD dwRes = VBoxServiceWinAddAceToObjectsSecurityDescriptor (TEXT("\\\\.\\VBoxMiniRdrDN"),
|
---|
307 | SE_FILE_OBJECT,
|
---|
308 | (LPTSTR)pBuiltinUsersSID,
|
---|
309 | TRUSTEE_IS_SID,
|
---|
310 | FILE_GENERIC_READ | FILE_GENERIC_WRITE,
|
---|
311 | SET_ACCESS,
|
---|
312 | NO_INHERITANCE);
|
---|
313 | if (dwRes != ERROR_SUCCESS)
|
---|
314 | {
|
---|
315 | if (dwRes == ERROR_FILE_NOT_FOUND)
|
---|
316 | {
|
---|
317 | /* If we don't find our "VBoxMiniRdrDN" (for Shared Folders) object above,
|
---|
318 | don't report an error; it just might be not installed. Otherwise this
|
---|
319 | would cause the SCM to hang on starting up the service. */
|
---|
320 | rc = VINF_SUCCESS;
|
---|
321 | }
|
---|
322 | else rc = RTErrConvertFromWin32(dwRes);
|
---|
323 | }
|
---|
324 | }
|
---|
325 | #endif
|
---|
326 |
|
---|
327 | if (RT_SUCCESS(rc))
|
---|
328 | {
|
---|
329 | /* Notify SCM *before* we're starting the services, because the last services
|
---|
330 | always starts in main thread (which causes the SCM to wait because of the non-responding
|
---|
331 | service). */
|
---|
332 | VBoxServiceWinSetStatus (SERVICE_RUNNING, 0);
|
---|
333 |
|
---|
334 | /*
|
---|
335 | * Check that at least one service is enabled.
|
---|
336 | */
|
---|
337 | unsigned iMain = VBoxServiceGetStartedServices();
|
---|
338 | rc = VBoxServiceStartServices(iMain); /* Start all the services. */
|
---|
339 |
|
---|
340 | if (RT_FAILURE(rc))
|
---|
341 | VBoxServiceWinSetStatus (SERVICE_STOPPED, 0);
|
---|
342 | }
|
---|
343 |
|
---|
344 | if (RT_FAILURE(rc))
|
---|
345 | VBoxServiceError("Service failed to start with rc=%Rrc!\n", rc);
|
---|
346 |
|
---|
347 | return rc;
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | #ifdef TARGET_NT4
|
---|
352 | VOID WINAPI VBoxServiceWinCtrlHandler(DWORD dwControl)
|
---|
353 | #else
|
---|
354 | DWORD WINAPI VBoxServiceWinCtrlHandler(DWORD dwControl,
|
---|
355 | DWORD dwEventType,
|
---|
356 | LPVOID lpEventData,
|
---|
357 | LPVOID lpContext)
|
---|
358 | #endif
|
---|
359 | {
|
---|
360 | DWORD rc = NO_ERROR;
|
---|
361 |
|
---|
362 | VBoxServiceVerbose(2, "Control handler: Control=%ld\n", dwControl);
|
---|
363 | #ifndef TARGET_NT4
|
---|
364 | VBoxServiceVerbose(2, "Control handler: EventType=%ld\n", dwEventType);
|
---|
365 | #endif
|
---|
366 |
|
---|
367 | switch (dwControl)
|
---|
368 | {
|
---|
369 |
|
---|
370 | case SERVICE_CONTROL_INTERROGATE:
|
---|
371 | VBoxServiceWinSetStatus(g_rcWinService, 0);
|
---|
372 | break;
|
---|
373 |
|
---|
374 | case SERVICE_CONTROL_STOP:
|
---|
375 | case SERVICE_CONTROL_SHUTDOWN:
|
---|
376 | {
|
---|
377 | VBoxServiceWinSetStatus(SERVICE_STOP_PENDING, 0);
|
---|
378 |
|
---|
379 | rc = VBoxServiceStopServices();
|
---|
380 |
|
---|
381 | VBoxServiceWinSetStatus(SERVICE_STOPPED, 0);
|
---|
382 | }
|
---|
383 | break;
|
---|
384 |
|
---|
385 | case SERVICE_CONTROL_SESSIONCHANGE: /* Only Win XP and up. */
|
---|
386 |
|
---|
387 | #ifndef TARGET_NT4
|
---|
388 | switch (dwEventType)
|
---|
389 | {
|
---|
390 | /*case WTS_SESSION_LOGON:
|
---|
391 | VBoxServiceVerbose(2, "A user has logged on to the session.\n");
|
---|
392 | break;
|
---|
393 |
|
---|
394 | case WTS_SESSION_LOGOFF:
|
---|
395 | VBoxServiceVerbose(2, "A user has logged off from the session.\n");
|
---|
396 | break;*/
|
---|
397 | default:
|
---|
398 | break;
|
---|
399 | }
|
---|
400 | #endif /* TARGET_NT4 */
|
---|
401 | break;
|
---|
402 |
|
---|
403 | default:
|
---|
404 |
|
---|
405 | VBoxServiceVerbose(1, "Service control function not implemented: %ld\n", dwControl);
|
---|
406 | rc = ERROR_CALL_NOT_IMPLEMENTED;
|
---|
407 | break;
|
---|
408 | }
|
---|
409 |
|
---|
410 | #ifndef TARGET_NT4
|
---|
411 | return rc;
|
---|
412 | #endif
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | void WINAPI VBoxServiceWinMain(DWORD argc, LPTSTR *argv)
|
---|
417 | {
|
---|
418 | int rc = VINF_SUCCESS;
|
---|
419 |
|
---|
420 | VBoxServiceVerbose(2, "Registering service control handler ...\n");
|
---|
421 | #ifdef TARGET_NT4
|
---|
422 | g_hWinServiceStatus = RegisterServiceCtrlHandler (VBOXSERVICE_NAME, VBoxServiceWinCtrlHandler);
|
---|
423 | #else
|
---|
424 | g_hWinServiceStatus = RegisterServiceCtrlHandlerEx (VBOXSERVICE_NAME, VBoxServiceWinCtrlHandler, NULL);
|
---|
425 | #endif
|
---|
426 |
|
---|
427 | if (NULL == g_hWinServiceStatus)
|
---|
428 | {
|
---|
429 | DWORD dwErr = GetLastError();
|
---|
430 |
|
---|
431 | switch (dwErr)
|
---|
432 | {
|
---|
433 | case ERROR_INVALID_NAME:
|
---|
434 | VBoxServiceError("Invalid service name!\n");
|
---|
435 | break;
|
---|
436 | case ERROR_SERVICE_DOES_NOT_EXIST:
|
---|
437 | VBoxServiceError("Service does not exist!\n");
|
---|
438 | break;
|
---|
439 | default:
|
---|
440 | VBoxServiceError("Could not register service control handle! Error: %ld\n", dwErr);
|
---|
441 | break;
|
---|
442 | }
|
---|
443 | }
|
---|
444 | else
|
---|
445 | {
|
---|
446 | VBoxServiceVerbose(2, "Service control handler registered.\n");
|
---|
447 |
|
---|
448 | rc = VBoxServiceWinStart();
|
---|
449 | }
|
---|
450 | }
|
---|