VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceVMInfo-win.cpp@ 49764

Last change on this file since 49764 was 48678, checked in by vboxsync, 11 years ago

Forward ported r89250 + r89298 (VBoxService/VMInfo: Fixed memory leak caused in WTSQuerySessionInformation on Windows OSes without appropriate hotfix, logging typo).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.1 KB
Line 
1/* $Id: VBoxServiceVMInfo-win.cpp 48678 2013-09-25 11:11:22Z vboxsync $ */
2/** @file
3 * VBoxService - Virtual Machine Information for the Host, Windows specifics.
4 */
5
6/*
7 * Copyright (C) 2009-2013 Oracle Corporation
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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0502
23# undef _WIN32_WINNT
24# define _WIN32_WINNT 0x0502 /* CachedRemoteInteractive in recent SDKs. */
25#endif
26#include <Windows.h>
27#include <wtsapi32.h> /* For WTS* calls. */
28#include <psapi.h> /* EnumProcesses. */
29#include <Ntsecapi.h> /* Needed for process security information. */
30
31#include <iprt/assert.h>
32#include <iprt/ldr.h>
33#include <iprt/localipc.h>
34#include <iprt/mem.h>
35#include <iprt/thread.h>
36#include <iprt/string.h>
37#include <iprt/semaphore.h>
38#include <iprt/system.h>
39#include <iprt/time.h>
40#include <VBox/VBoxGuestLib.h>
41#include "VBoxServiceInternal.h"
42#include "VBoxServiceUtils.h"
43#include "VBoxServiceVMInfo.h"
44#include "../../WINNT/VBoxTray/VBoxTrayMsg.h" /* For IPC. */
45
46static uint32_t s_uDebugGuestPropClientID = 0;
47static uint32_t s_uDebugIter = 0;
48/** Whether to skip the logged-in user detection over RDP or not.
49 * See notes in this section why we might want to skip this. */
50static bool s_fSkipRDPDetection = false;
51
52/*******************************************************************************
53* Structures and Typedefs *
54*******************************************************************************/
55/** Structure for storing the looked up user information. */
56typedef struct VBOXSERVICEVMINFOUSER
57{
58 WCHAR wszUser[_MAX_PATH];
59 WCHAR wszAuthenticationPackage[_MAX_PATH];
60 WCHAR wszLogonDomain[_MAX_PATH];
61 /** Number of assigned user processes. */
62 ULONG ulNumProcs;
63 /** Last (highest) session ID. This
64 * is needed for distinguishing old session
65 * process counts from new (current) session
66 * ones. */
67 ULONG ulLastSession;
68} VBOXSERVICEVMINFOUSER, *PVBOXSERVICEVMINFOUSER;
69
70/** Structure for the file information lookup. */
71typedef struct VBOXSERVICEVMINFOFILE
72{
73 char *pszFilePath;
74 char *pszFileName;
75} VBOXSERVICEVMINFOFILE, *PVBOXSERVICEVMINFOFILE;
76
77/** Structure for process information lookup. */
78typedef struct VBOXSERVICEVMINFOPROC
79{
80 /** The PID. */
81 DWORD id;
82 /** The SID. */
83 PSID pSid;
84 /** The LUID. */
85 LUID luid;
86 /** Interactive process. */
87 bool fInteractive;
88} VBOXSERVICEVMINFOPROC, *PVBOXSERVICEVMINFOPROC;
89
90
91/*******************************************************************************
92* Prototypes
93*******************************************************************************/
94uint32_t VBoxServiceVMInfoWinSessionHasProcesses(PLUID pSession, PVBOXSERVICEVMINFOPROC const paProcs, DWORD cProcs);
95bool VBoxServiceVMInfoWinIsLoggedIn(PVBOXSERVICEVMINFOUSER a_pUserInfo, PLUID a_pSession);
96int VBoxServiceVMInfoWinProcessesEnumerate(PVBOXSERVICEVMINFOPROC *ppProc, DWORD *pdwCount);
97void VBoxServiceVMInfoWinProcessesFree(DWORD cProcs, PVBOXSERVICEVMINFOPROC paProcs);
98int vboxServiceVMInfoWinWriteLastInput(PVBOXSERVICEVEPROPCACHE pCache, const char *pszUser, const char *pszDomain);
99
100typedef BOOL WINAPI FNQUERYFULLPROCESSIMAGENAME(HANDLE, DWORD, LPTSTR, PDWORD);
101typedef FNQUERYFULLPROCESSIMAGENAME *PFNQUERYFULLPROCESSIMAGENAME;
102
103
104#ifndef TARGET_NT4
105
106static bool vboxServiceVMInfoSession0Separation(void)
107{
108 /** @todo Only do this once. Later. */
109 OSVERSIONINFOEX OSInfoEx;
110 RT_ZERO(OSInfoEx);
111 OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
112 if ( !GetVersionEx((LPOSVERSIONINFO) &OSInfoEx)
113 || OSInfoEx.dwPlatformId != VER_PLATFORM_WIN32_NT)
114 {
115 /* Platform other than NT (e.g. Win9x) not supported. */
116 return false;
117 }
118
119 if ( OSInfoEx.dwMajorVersion >= 6
120 && OSInfoEx.dwMinorVersion >= 0)
121 {
122 return true;
123 }
124
125 return false;
126}
127
128/**
129 * Retrieves the module name of a given process.
130 *
131 * @return IPRT status code.
132 * @param pProc
133 * @param pszBuf
134 * @param cbBuf
135 */
136static int VBoxServiceVMInfoWinProcessesGetModuleName(PVBOXSERVICEVMINFOPROC const pProc,
137 TCHAR *pszName, size_t cbName)
138{
139 AssertPtrReturn(pProc, VERR_INVALID_POINTER);
140 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
141 AssertReturn(cbName, VERR_INVALID_PARAMETER);
142
143 /** @todo Only do this once. Later. */
144 OSVERSIONINFOEX OSInfoEx;
145 RT_ZERO(OSInfoEx);
146 OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
147 if ( !GetVersionEx((LPOSVERSIONINFO) &OSInfoEx)
148 || OSInfoEx.dwPlatformId != VER_PLATFORM_WIN32_NT)
149 {
150 /* Platform other than NT (e.g. Win9x) not supported. */
151 return VERR_NOT_SUPPORTED;
152 }
153
154 int rc = VINF_SUCCESS;
155
156 DWORD dwFlags = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
157 if (OSInfoEx.dwMajorVersion >= 6 /* Vista or later */)
158 dwFlags = 0x1000; /* = PROCESS_QUERY_LIMITED_INFORMATION; less privileges needed. */
159
160 HANDLE h = OpenProcess(dwFlags, FALSE, pProc->id);
161 if (h == NULL)
162 {
163 DWORD dwErr = GetLastError();
164 if (g_cVerbosity)
165 VBoxServiceError("Unable to open process with PID=%ld, error=%ld\n",
166 pProc->id, dwErr);
167 rc = RTErrConvertFromWin32(dwErr);
168 }
169 else
170 {
171 /* Since GetModuleFileNameEx has trouble with cross-bitness stuff (32-bit apps cannot query 64-bit
172 apps and vice verse) we have to use a different code path for Vista and up. */
173
174 /* Note: For 2000 + NT4 we might just use GetModuleFileName() instead. */
175 if (OSInfoEx.dwMajorVersion >= 6 /* Vista or later */)
176 {
177 /* Loading the module and getting the symbol for each and every process is expensive
178 * -- since this function (at the moment) only is used for debugging purposes it's okay. */
179 PFNQUERYFULLPROCESSIMAGENAME pfnQueryFullProcessImageName;
180 pfnQueryFullProcessImageName = (PFNQUERYFULLPROCESSIMAGENAME)
181 RTLdrGetSystemSymbol("kernel32.dll", "QueryFullProcessImageNameA");
182 /** @todo r=bird: WTF don't we query the UNICODE name? */
183 if (pfnQueryFullProcessImageName)
184 {
185 /** @todo r=bird: Completely bogus use of TCHAR.
186 * !!ALL USE OF TCHAR IS HEREWITH BANNED IN ALL VBOX SOURCES!!
187 * We use WCHAR when talking to windows, everything else is WRONG. (We don't
188 * want Chinese MBCS being treated as UTF-8.) */
189 DWORD dwLen = cbName / sizeof(TCHAR);
190 if (!pfnQueryFullProcessImageName(h, 0 /*PROCESS_NAME_NATIVE*/, pszName, &dwLen))
191 rc = VERR_ACCESS_DENIED;
192 }
193 }
194 else
195 {
196 if (!GetModuleFileNameEx(h, NULL /* Get main executable */, pszName, cbName / sizeof(TCHAR)))
197 rc = VERR_ACCESS_DENIED;
198 }
199
200 CloseHandle(h);
201 }
202
203 return rc;
204}
205
206
207/**
208 * Fills in more data for a process.
209 *
210 * @returns VBox status code.
211 * @param pProc The process structure to fill data into.
212 * @param tkClass The kind of token information to get.
213 */
214static int VBoxServiceVMInfoWinProcessesGetTokenInfo(PVBOXSERVICEVMINFOPROC pProc,
215 TOKEN_INFORMATION_CLASS tkClass)
216{
217 AssertPtrReturn(pProc, VERR_INVALID_POINTER);
218
219 DWORD dwErr = ERROR_SUCCESS;
220 HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pProc->id);
221 if (h == NULL)
222 {
223 dwErr = GetLastError();
224 if (g_cVerbosity > 4)
225 VBoxServiceError("Unable to open process with PID=%ld, error=%ld\n",
226 pProc->id, dwErr);
227 return RTErrConvertFromWin32(dwErr);
228 }
229
230 int rc = VINF_SUCCESS;
231 HANDLE hToken;
232 if (OpenProcessToken(h, TOKEN_QUERY, &hToken))
233 {
234 void *pvTokenInfo = NULL;
235 DWORD dwTokenInfoSize;
236 switch (tkClass)
237 {
238 case TokenStatistics:
239 dwTokenInfoSize = sizeof(TOKEN_STATISTICS);
240 pvTokenInfo = HeapAlloc(GetProcessHeap(),
241 HEAP_ZERO_MEMORY, dwTokenInfoSize);
242 AssertPtr(pvTokenInfo);
243 break;
244
245 case TokenGroups:
246 dwTokenInfoSize = 0;
247 /* Allocation will follow in a second step. */
248 break;
249
250 case TokenUser:
251 dwTokenInfoSize = 0;
252 /* Allocation will follow in a second step. */
253 break;
254
255 default:
256 VBoxServiceError("Token class not implemented: %ld", tkClass);
257 rc = VERR_NOT_IMPLEMENTED;
258 break;
259 }
260
261 if (RT_SUCCESS(rc))
262 {
263 DWORD dwRetLength;
264 if (!GetTokenInformation(hToken, tkClass, pvTokenInfo, dwTokenInfoSize, &dwRetLength))
265 {
266 dwErr = GetLastError();
267 if (dwErr == ERROR_INSUFFICIENT_BUFFER)
268 {
269 dwErr = ERROR_SUCCESS;
270
271 switch (tkClass)
272 {
273 case TokenGroups:
274 pvTokenInfo = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
275 HEAP_ZERO_MEMORY, dwRetLength);
276 if (!pvTokenInfo)
277 dwErr = GetLastError();
278 dwTokenInfoSize = dwRetLength;
279 break;
280
281 case TokenUser:
282 pvTokenInfo = (PTOKEN_USER)HeapAlloc(GetProcessHeap(),
283 HEAP_ZERO_MEMORY, dwRetLength);
284 if (!pvTokenInfo)
285 dwErr = GetLastError();
286 dwTokenInfoSize = dwRetLength;
287 break;
288
289 default:
290 AssertMsgFailed(("Re-allocating of token information for token class not implemented\n"));
291 break;
292 }
293
294 if (dwErr == ERROR_SUCCESS)
295 {
296 if (!GetTokenInformation(hToken, tkClass, pvTokenInfo, dwTokenInfoSize, &dwRetLength))
297 dwErr = GetLastError();
298 }
299 }
300 }
301
302 if (dwErr == ERROR_SUCCESS)
303 {
304 rc = VINF_SUCCESS;
305
306 switch (tkClass)
307 {
308 case TokenStatistics:
309 {
310 PTOKEN_STATISTICS pStats = (PTOKEN_STATISTICS)pvTokenInfo;
311 AssertPtr(pStats);
312 memcpy(&pProc->luid, &pStats->AuthenticationId, sizeof(LUID));
313 /** @todo Add more information of TOKEN_STATISTICS as needed. */
314 break;
315 }
316
317 case TokenGroups:
318 {
319 pProc->fInteractive = false;
320
321 SID_IDENTIFIER_AUTHORITY sidAuthNT = SECURITY_NT_AUTHORITY;
322 PSID pSidInteractive = NULL; /* S-1-5-4 */
323 if (!AllocateAndInitializeSid(&sidAuthNT, 1, 4, 0, 0, 0, 0, 0, 0, 0, &pSidInteractive))
324 dwErr = GetLastError();
325
326 PSID pSidLocal = NULL; /* S-1-2-0 */
327 if (dwErr == ERROR_SUCCESS)
328 {
329 SID_IDENTIFIER_AUTHORITY sidAuthLocal = SECURITY_LOCAL_SID_AUTHORITY;
330 if (!AllocateAndInitializeSid(&sidAuthLocal, 1, 0, 0, 0, 0, 0, 0, 0, 0, &pSidLocal))
331 dwErr = GetLastError();
332 }
333
334 if (dwErr == ERROR_SUCCESS)
335 {
336 PTOKEN_GROUPS pGroups = (PTOKEN_GROUPS)pvTokenInfo;
337 AssertPtr(pGroups);
338 for (DWORD i = 0; i < pGroups->GroupCount; i++)
339 {
340 if ( EqualSid(pGroups->Groups[i].Sid, pSidInteractive)
341 || EqualSid(pGroups->Groups[i].Sid, pSidLocal)
342 || pGroups->Groups[i].Attributes & SE_GROUP_LOGON_ID)
343 {
344 pProc->fInteractive = true;
345 break;
346 }
347 }
348 }
349
350 if (pSidInteractive)
351 FreeSid(pSidInteractive);
352 if (pSidLocal)
353 FreeSid(pSidLocal);
354 break;
355 }
356
357 case TokenUser:
358 {
359 PTOKEN_USER pUser = (PTOKEN_USER)pvTokenInfo;
360 AssertPtr(pUser);
361
362 DWORD dwLength = GetLengthSid(pUser->User.Sid);
363 Assert(dwLength);
364 if (dwLength)
365 {
366 pProc->pSid = (PSID)RTMemAlloc(dwLength);
367 AssertPtr(pProc->pSid);
368 if (CopySid(dwLength, pProc->pSid, pUser->User.Sid))
369 {
370 if (!IsValidSid(pProc->pSid))
371 dwErr = ERROR_INVALID_NAME;
372 }
373 else
374 dwErr = GetLastError();
375 }
376 else
377 dwErr = ERROR_NO_DATA;
378
379 if (dwErr != ERROR_SUCCESS)
380 VBoxServiceError("Error retrieving SID of process PID=%ld: %ld\n",
381 pProc->id, dwErr);
382 break;
383 }
384
385 default:
386 AssertMsgFailed(("Unhandled token information class\n"));
387 break;
388 }
389 }
390
391 if (pvTokenInfo)
392 HeapFree(GetProcessHeap(), 0 /* Flags */, pvTokenInfo);
393 }
394 CloseHandle(hToken);
395 }
396 else
397 dwErr = GetLastError();
398
399 if (dwErr != ERROR_SUCCESS)
400 {
401 if (g_cVerbosity)
402 VBoxServiceError("Unable to query token information for PID=%ld, error=%ld\n",
403 pProc->id, dwErr);
404 rc = RTErrConvertFromWin32(dwErr);
405 }
406
407 CloseHandle(h);
408 return rc;
409}
410
411
412/**
413 * Enumerate all the processes in the system and get the logon user IDs for
414 * them.
415 *
416 * @returns VBox status code.
417 * @param ppaProcs Where to return the process snapshot. This must be
418 * freed by calling VBoxServiceVMInfoWinProcessesFree.
419 *
420 * @param pcProcs Where to store the returned process count.
421 */
422int VBoxServiceVMInfoWinProcessesEnumerate(PVBOXSERVICEVMINFOPROC *ppaProcs, PDWORD pcProcs)
423{
424 AssertPtr(ppaProcs);
425 AssertPtr(pcProcs);
426
427 /*
428 * Call EnumProcesses with an increasingly larger buffer until it all fits
429 * or we think something is screwed up.
430 */
431 DWORD cProcesses = 64;
432 PDWORD paPID = NULL;
433 int rc = VINF_SUCCESS;
434 do
435 {
436 /* Allocate / grow the buffer first. */
437 cProcesses *= 2;
438 void *pvNew = RTMemRealloc(paPID, cProcesses * sizeof(DWORD));
439 if (!pvNew)
440 {
441 rc = VERR_NO_MEMORY;
442 break;
443 }
444 paPID = (PDWORD)pvNew;
445
446 /* Query the processes. Not the cbRet == buffer size means there could be more work to be done. */
447 DWORD cbRet;
448 if (!EnumProcesses(paPID, cProcesses * sizeof(DWORD), &cbRet))
449 {
450 rc = RTErrConvertFromWin32(GetLastError());
451 break;
452 }
453 if (cbRet < cProcesses * sizeof(DWORD))
454 {
455 cProcesses = cbRet / sizeof(DWORD);
456 break;
457 }
458 } while (cProcesses <= _32K); /* Should be enough; see: http://blogs.technet.com/markrussinovich/archive/2009/07/08/3261309.aspx */
459 if (RT_SUCCESS(rc))
460 {
461 /*
462 * Allocate out process structures and fill data into them.
463 * We currently only try lookup their LUID's.
464 */
465 PVBOXSERVICEVMINFOPROC paProcs;
466 paProcs = (PVBOXSERVICEVMINFOPROC)RTMemAllocZ(cProcesses * sizeof(VBOXSERVICEVMINFOPROC));
467 if (paProcs)
468 {
469 for (DWORD i = 0; i < cProcesses; i++)
470 {
471 paProcs[i].id = paPID[i];
472 paProcs[i].pSid = NULL;
473
474 int rc2 = VBoxServiceVMInfoWinProcessesGetTokenInfo(&paProcs[i], TokenUser);
475 if (RT_FAILURE(rc2) && g_cVerbosity)
476 VBoxServiceError("Get token class \"user\" for process %ld failed, rc=%Rrc\n",
477 paProcs[i].id, rc2);
478
479 rc2 = VBoxServiceVMInfoWinProcessesGetTokenInfo(&paProcs[i], TokenGroups);
480 if (RT_FAILURE(rc2) && g_cVerbosity)
481 VBoxServiceError("Get token class \"groups\" for process %ld failed, rc=%Rrc\n",
482 paProcs[i].id, rc2);
483
484 rc2 = VBoxServiceVMInfoWinProcessesGetTokenInfo(&paProcs[i], TokenStatistics);
485 if (RT_FAILURE(rc2) && g_cVerbosity)
486 VBoxServiceError("Get token class \"statistics\" for process %ld failed, rc=%Rrc\n",
487 paProcs[i].id, rc2);
488 }
489
490 /* Save number of processes */
491 if (RT_SUCCESS(rc))
492 {
493 *pcProcs = cProcesses;
494 *ppaProcs = paProcs;
495 }
496 else
497 RTMemFree(paProcs);
498 }
499 else
500 rc = VERR_NO_MEMORY;
501 }
502
503 RTMemFree(paPID);
504 return rc;
505}
506
507/**
508 * Frees the process structures returned by
509 * VBoxServiceVMInfoWinProcessesEnumerate() before.
510 *
511 * @param paProcs What
512 */
513void VBoxServiceVMInfoWinProcessesFree(DWORD cProcs, PVBOXSERVICEVMINFOPROC paProcs)
514{
515 for (DWORD i = 0; i < cProcs; i++)
516 {
517 if (paProcs[i].pSid)
518 RTMemFree(paProcs[i].pSid);
519 }
520 RTMemFree(paProcs);
521}
522
523/**
524 * Determines whether the specified session has processes on the system.
525 *
526 * @returns Number of processes found for a specified session.
527 * @param pSession The current user's SID.
528 * @param paProcs The process snapshot.
529 * @param cProcs The number of processes in the snaphot.
530 * @param puSession Looked up session number. Optional.
531 */
532uint32_t VBoxServiceVMInfoWinSessionHasProcesses(PLUID pSession,
533 PVBOXSERVICEVMINFOPROC const paProcs, DWORD cProcs,
534 PULONG puTerminalSession)
535{
536 if (!pSession)
537 {
538 VBoxServiceVerbose(1, "Session became invalid while enumerating!\n");
539 return 0;
540 }
541
542 PSECURITY_LOGON_SESSION_DATA pSessionData = NULL;
543 NTSTATUS rcNt = LsaGetLogonSessionData(pSession, &pSessionData);
544 if (rcNt != STATUS_SUCCESS)
545 {
546 VBoxServiceError("Could not get logon session data! rcNt=%#x\n", rcNt);
547 return 0;
548 }
549
550 if (!IsValidSid(pSessionData->Sid))
551 {
552 VBoxServiceError("User SID=%p is not valid\n", pSessionData->Sid);
553 if (pSessionData)
554 LsaFreeReturnBuffer(pSessionData);
555 return 0;
556 }
557
558 int rc = VINF_SUCCESS;
559
560 /*
561 * Even if a user seems to be logged in, it could be a stale/orphaned logon
562 * session. So check if we have some processes bound to it by comparing the
563 * session <-> process LUIDs.
564 */
565 uint32_t cNumProcs = 0;
566 for (DWORD i = 0; i < cProcs; i++)
567 {
568 if (g_cVerbosity)
569 {
570 TCHAR szModule[_1K];
571 rc = VBoxServiceVMInfoWinProcessesGetModuleName(&paProcs[i], szModule, sizeof(szModule));
572 if (RT_SUCCESS(rc))
573 VBoxServiceVerbose(4, "PID=%ld: %s\n",
574 paProcs[i].id, szModule);
575 }
576
577 PSID pProcSID = paProcs[i].pSid;
578 if ( RT_SUCCESS(rc)
579 && pProcSID
580 && IsValidSid(pProcSID))
581 {
582 if ( EqualSid(pSessionData->Sid, paProcs[i].pSid)
583 && paProcs[i].fInteractive)
584 {
585 cNumProcs++;
586 if (!g_cVerbosity) /* We want a bit more info on higher verbosity. */
587 break;
588 }
589 }
590 }
591
592 if (puTerminalSession)
593 *puTerminalSession = pSessionData->Session;
594
595 LsaFreeReturnBuffer(pSessionData);
596
597 return cNumProcs;
598}
599
600
601/**
602 * Save and noisy string copy.
603 *
604 * @param pwszDst Destination buffer.
605 * @param cbDst Size in bytes - not WCHAR count!
606 * @param pSrc Source string.
607 * @param pszWhat What this is. For the log.
608 */
609static void VBoxServiceVMInfoWinSafeCopy(PWCHAR pwszDst, size_t cbDst, LSA_UNICODE_STRING const *pSrc, const char *pszWhat)
610{
611 Assert(RT_ALIGN(cbDst, sizeof(WCHAR)) == cbDst);
612
613 size_t cbCopy = pSrc->Length;
614 if (cbCopy + sizeof(WCHAR) > cbDst)
615 {
616 VBoxServiceVerbose(0, "%s is too long - %u bytes, buffer %u bytes! It will be truncated.\n",
617 pszWhat, cbCopy, cbDst);
618 cbCopy = cbDst - sizeof(WCHAR);
619 }
620 if (cbCopy)
621 memcpy(pwszDst, pSrc->Buffer, cbCopy);
622 pwszDst[cbCopy / sizeof(WCHAR)] = '\0';
623}
624
625
626/**
627 * Detects whether a user is logged on.
628 *
629 * @returns true if logged in, false if not (or error).
630 * @param pUserInfo Where to return the user information.
631 * @param pSession The session to check.
632 */
633bool VBoxServiceVMInfoWinIsLoggedIn(PVBOXSERVICEVMINFOUSER pUserInfo, PLUID pSession)
634{
635 AssertPtrReturn(pUserInfo, false);
636 if (!pSession)
637 return false;
638
639 PSECURITY_LOGON_SESSION_DATA pSessionData = NULL;
640 NTSTATUS rcNt = LsaGetLogonSessionData(pSession, &pSessionData);
641 if (rcNt != STATUS_SUCCESS)
642 {
643 ULONG ulError = LsaNtStatusToWinError(rcNt);
644 switch (ulError)
645 {
646 case ERROR_NOT_ENOUGH_MEMORY:
647 /* If we don't have enough memory it's hard to judge whether the specified user
648 * is logged in or not, so just assume he/she's not. */
649 VBoxServiceVerbose(3, "Not enough memory to retrieve logon session data!\n");
650 break;
651
652 case ERROR_NO_SUCH_LOGON_SESSION:
653 /* Skip session data which is not valid anymore because it may have been
654 * already terminated. */
655 break;
656
657 default:
658 VBoxServiceError("LsaGetLogonSessionData failed with error %u\n", ulError);
659 break;
660 }
661 if (pSessionData)
662 LsaFreeReturnBuffer(pSessionData);
663 return false;
664 }
665 if (!pSessionData)
666 {
667 VBoxServiceError("Invalid logon session data!\n");
668 return false;
669 }
670
671 VBoxServiceVerbose(3, "Session data: Name=%ls, SessionID=%RU32, LogonID=%ld,%ld, LogonType=%ld\n",
672 pSessionData->UserName.Buffer,
673 pSessionData->Session,
674 pSessionData->LogonId.HighPart, pSessionData->LogonId.LowPart,
675 pSessionData->LogonType);
676
677 if (vboxServiceVMInfoSession0Separation())
678 {
679 /* Starting at Windows Vista user sessions begin with session 1, so
680 * ignore (stale) session 0 users. */
681 if ( pSessionData->Session == 0
682 /* Also check the logon time. */
683 || pSessionData->LogonTime.QuadPart == 0)
684 {
685 LsaFreeReturnBuffer(pSessionData);
686 return false;
687 }
688 }
689
690 /*
691 * Only handle users which can login interactively or logged in
692 * remotely over native RDP.
693 */
694 bool fFoundUser = false;
695 DWORD dwErr = NO_ERROR;
696 if ( IsValidSid(pSessionData->Sid)
697 && ( (SECURITY_LOGON_TYPE)pSessionData->LogonType == Interactive
698 || (SECURITY_LOGON_TYPE)pSessionData->LogonType == RemoteInteractive
699 /* Note: We also need CachedInteractive in case Windows cached the credentials
700 * or just wants to reuse them! */
701 || (SECURITY_LOGON_TYPE)pSessionData->LogonType == CachedInteractive))
702 {
703 VBoxServiceVerbose(3, "Session LogonType=%ld is supported -- looking up SID + type ...\n",
704 pSessionData->LogonType);
705
706 /*
707 * Copy out relevant data.
708 */
709 VBoxServiceVMInfoWinSafeCopy(pUserInfo->wszUser, sizeof(pUserInfo->wszUser),
710 &pSessionData->UserName, "User name");
711 VBoxServiceVMInfoWinSafeCopy(pUserInfo->wszAuthenticationPackage, sizeof(pUserInfo->wszAuthenticationPackage),
712 &pSessionData->AuthenticationPackage, "Authentication pkg name");
713 VBoxServiceVMInfoWinSafeCopy(pUserInfo->wszLogonDomain, sizeof(pUserInfo->wszLogonDomain),
714 &pSessionData->LogonDomain, "Logon domain name");
715
716 TCHAR szOwnerName[_MAX_PATH] = { 0 };
717 DWORD dwOwnerNameSize = sizeof(szOwnerName);
718 TCHAR szDomainName[_MAX_PATH] = { 0 };
719 DWORD dwDomainNameSize = sizeof(szDomainName);
720 SID_NAME_USE enmOwnerType = SidTypeInvalid;
721 if (!LookupAccountSid(NULL,
722 pSessionData->Sid,
723 szOwnerName,
724 &dwOwnerNameSize,
725 szDomainName,
726 &dwDomainNameSize,
727 &enmOwnerType))
728 {
729 DWORD dwErr = GetLastError();
730 /*
731 * If a network time-out prevents the function from finding the name or
732 * if a SID that does not have a corresponding account name (such as a
733 * logon SID that identifies a logon session), we get ERROR_NONE_MAPPED
734 * here that we just skip.
735 */
736 if (dwErr != ERROR_NONE_MAPPED)
737 VBoxServiceError("Failed looking up account info for user=%ls, error=$ld!\n",
738 pUserInfo->wszUser, dwErr);
739 }
740 else
741 {
742 if (enmOwnerType == SidTypeUser) /* Only recognize users; we don't care about the rest! */
743 {
744 VBoxServiceVerbose(3, "Account User=%ls, Session=%ld, LogonID=%ld,%ld, AuthPkg=%ls, Domain=%ls\n",
745 pUserInfo->wszUser, pSessionData->Session, pSessionData->LogonId.HighPart,
746 pSessionData->LogonId.LowPart, pUserInfo->wszAuthenticationPackage,
747 pUserInfo->wszLogonDomain);
748
749 /**
750 * Note: On certain Windows OSes WTSQuerySessionInformation leaks memory when used
751 * under a heavy stress situation. There are hotfixes available from Microsoft.
752 *
753 * See: http://support.microsoft.com/kb/970910
754 */
755 if (!s_fSkipRDPDetection)
756 {
757 /** @todo Only do this once. Later. */
758 OSVERSIONINFOEX OSInfoEx;
759 RT_ZERO(OSInfoEx);
760 OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
761
762 /* Skip RDP detection on non-NT systems. */
763 if ( !GetVersionEx((LPOSVERSIONINFO) &OSInfoEx)
764 || OSInfoEx.dwPlatformId != VER_PLATFORM_WIN32_NT)
765 {
766 s_fSkipRDPDetection = true;
767 }
768 /* Skip RDP detection on Windows 2000.
769 * For Windows 2000 however we don't have any hotfixes, so just skip the
770 * RDP detection in any case. */
771 if ( OSInfoEx.dwMajorVersion == 5
772 && OSInfoEx.dwMinorVersion == 0)
773 {
774 s_fSkipRDPDetection = true;
775 }
776
777 if (s_fSkipRDPDetection)
778 VBoxServiceVerbose(0, "Detection of logged-in users via RDP is disabled\n");
779 }
780
781 if (!s_fSkipRDPDetection)
782 {
783 /* Detect RDP sessions as well. */
784 LPTSTR pBuffer = NULL;
785 DWORD cbRet = 0;
786 int iState = -1;
787 if (WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
788 pSessionData->Session,
789 WTSConnectState,
790 &pBuffer,
791 &cbRet))
792 {
793 if (cbRet)
794 iState = *pBuffer;
795 VBoxServiceVerbose(3, "Account User=%ls, WTSConnectState=%d (%ld)\n",
796 pUserInfo->wszUser, iState, cbRet);
797 if ( iState == WTSActive /* User logged on to WinStation. */
798 || iState == WTSShadow /* Shadowing another WinStation. */
799 || iState == WTSDisconnected) /* WinStation logged on without client. */
800 {
801 /** @todo On Vista and W2K, always "old" user name are still
802 * there. Filter out the old one! */
803 VBoxServiceVerbose(3, "Account User=%ls using TCS/RDP, state=%d \n",
804 pUserInfo->wszUser, iState);
805 fFoundUser = true;
806 }
807 if (pBuffer)
808 WTSFreeMemory(pBuffer);
809 }
810 else
811 {
812 DWORD dwLastErr = GetLastError();
813 switch (dwLastErr)
814 {
815 /*
816 * Terminal services don't run (for example in W2K,
817 * nothing to worry about ...). ... or is on the Vista
818 * fast user switching page!
819 */
820 case ERROR_CTX_WINSTATION_NOT_FOUND:
821 VBoxServiceVerbose(3, "No WinStation found for user=%ls\n",
822 pUserInfo->wszUser);
823 break;
824
825 default:
826 VBoxServiceVerbose(3, "Cannot query WTS connection state for user=%ls, error=%ld\n",
827 pUserInfo->wszUser, dwLastErr);
828 break;
829 }
830
831 fFoundUser = true;
832 }
833 }
834 }
835 else
836 VBoxServiceVerbose(3, "SID owner type=%d not handled, skipping\n",
837 enmOwnerType);
838 }
839
840 VBoxServiceVerbose(3, "Account User=%ls %s logged in\n",
841 pUserInfo->wszUser, fFoundUser ? "is" : "is not");
842 }
843
844 if (fFoundUser)
845 pUserInfo->ulLastSession = pSessionData->Session;
846
847 LsaFreeReturnBuffer(pSessionData);
848 return fFoundUser;
849}
850
851
852static int vboxServiceVMInfoWinWriteLastInput(PVBOXSERVICEVEPROPCACHE pCache,
853 const char *pszUser, const char *pszDomain)
854{
855 AssertPtrReturn(pCache, VERR_INVALID_POINTER);
856 AssertPtrReturn(pszUser, VERR_INVALID_POINTER);
857 /* pszDomain is optional. */
858
859 int rc = VINF_SUCCESS;
860
861 char szPipeName[255];
862 if (RTStrPrintf(szPipeName, sizeof(szPipeName), "%s%s",
863 VBOXTRAY_IPC_PIPE_PREFIX, pszUser))
864 {
865 bool fReportToHost = false;
866 VBoxGuestUserState userState = VBoxGuestUserState_Unknown;
867
868 RTLOCALIPCSESSION hSession;
869 rc = RTLocalIpcSessionConnect(&hSession, szPipeName, 0 /* Flags */);
870 if (RT_SUCCESS(rc))
871 {
872 VBOXTRAYIPCHEADER ipcHdr = { VBOXTRAY_IPC_HDR_MAGIC, 0 /* Header version */,
873 VBOXTRAYIPCMSGTYPE_USERLASTINPUT, 0 /* No msg */ };
874
875 rc = RTLocalIpcSessionWrite(hSession, &ipcHdr, sizeof(ipcHdr));
876
877 VBOXTRAYIPCRES_USERLASTINPUT ipcRes;
878 if (RT_SUCCESS(rc))
879 rc = RTLocalIpcSessionRead(hSession, &ipcRes, sizeof(ipcRes),
880 NULL /* Exact read */);
881 if ( RT_SUCCESS(rc)
882 /* If uLastInput is set to UINT32_MAX VBoxTray was not able to retrieve the
883 * user's last input time. This might happen when running on Windows NT4 or older. */
884 && ipcRes.uLastInput != UINT32_MAX)
885 {
886 userState = (ipcRes.uLastInput * 1000) < g_uVMInfoUserIdleThresholdMS
887 ? VBoxGuestUserState_InUse
888 : VBoxGuestUserState_Idle;
889
890 rc = vboxServiceUserUpdateF(pCache, pszUser, pszDomain, "UsageState",
891 userState == VBoxGuestUserState_InUse
892 ? "InUse" : "Idle");
893
894 /*
895 * Note: vboxServiceUserUpdateF can return VINF_NO_CHANGE in case there wasn't anything
896 * to update. So only report the user's status to host when we really got something
897 * new.
898 */
899 fReportToHost = rc == VINF_SUCCESS;
900 VBoxServiceVerbose(4, "User \"%s\" (domain \"%s\") is idle for %RU32, fReportToHost=%RTbool\n",
901 pszUser, pszDomain ? pszDomain : "<None>", ipcRes.uLastInput, fReportToHost);
902
903#if 0 /* Do we want to write the idle time as well? */
904 /* Also write the user's current idle time, if there is any. */
905 if (userState == VBoxGuestUserState_Idle)
906 rc = vboxServiceUserUpdateF(pCache, pszUser, pszDomain, "IdleTimeMs",
907 "%RU32", ipcRes.uLastInputMs);
908 else
909 rc = vboxServiceUserUpdateF(pCache, pszUser, pszDomain, "IdleTimeMs",
910 NULL /* Delete property */);
911
912 if (RT_SUCCESS(rc))
913#endif
914 }
915#ifdef DEBUG
916 else if (ipcRes.uLastInput == UINT32_MAX)
917 VBoxServiceVerbose(4, "Last input for user \"%s\" is not supported, skipping\n",
918 pszUser, rc);
919
920 VBoxServiceVerbose(4, "Getting last input for user \"%s\" ended with rc=%Rrc\n",
921 pszUser, rc);
922#endif
923 int rc2 = RTLocalIpcSessionClose(hSession);
924 if (RT_SUCCESS(rc))
925 rc = rc2;
926 }
927 else
928 {
929 switch (rc)
930 {
931 case VERR_FILE_NOT_FOUND:
932 {
933 /* No VBoxTray (or too old version which does not support IPC) running
934 for the given user. Not much we can do then. */
935 VBoxServiceVerbose(4, "VBoxTray for user \"%s\" not running (anymore), no last input available\n",
936 pszUser);
937
938 /* Overwrite rc from above. */
939 rc = vboxServiceUserUpdateF(pCache, pszUser, pszDomain,
940 "UsageState", "Idle");
941
942 fReportToHost = rc == VINF_SUCCESS;
943 if (fReportToHost)
944 userState = VBoxGuestUserState_Idle;
945 break;
946 }
947
948 default:
949 VBoxServiceError("Error querying last input for user \"%s\", rc=%Rrc\n",
950 pszUser, rc);
951 break;
952 }
953 }
954
955 if (fReportToHost)
956 {
957 Assert(userState != VBoxGuestUserState_Unknown);
958 int rc2 = VbglR3GuestUserReportState(pszUser, pszDomain, userState,
959 NULL /* No details */, 0);
960 if (RT_FAILURE(rc2))
961 VBoxServiceError("Error reporting usage state %ld for user \"%s\" to host, rc=%Rrc\n",
962 userState, pszUser, rc2);
963
964 if (RT_SUCCESS(rc))
965 rc = rc2;
966 }
967 }
968
969 return rc;
970}
971
972
973/**
974 * Retrieves the currently logged in users and stores their names along with the
975 * user count.
976 *
977 * @returns VBox status code.
978 * @param pCachce Property cache to use for storing some of the lookup
979 * data in between calls.
980 * @param ppszUserList Where to store the user list (separated by commas).
981 * Must be freed with RTStrFree().
982 * @param pcUsersInList Where to store the number of users in the list.
983 */
984int VBoxServiceVMInfoWinWriteUsers(PVBOXSERVICEVEPROPCACHE pCache,
985 char **ppszUserList, uint32_t *pcUsersInList)
986{
987 AssertPtrReturn(pCache, VERR_INVALID_POINTER);
988 AssertPtrReturn(ppszUserList, VERR_INVALID_POINTER);
989 AssertPtrReturn(pcUsersInList, VERR_INVALID_POINTER);
990
991 int rc2 = VbglR3GuestPropConnect(&s_uDebugGuestPropClientID);
992 AssertRC(rc2);
993
994 /* This function can report stale or orphaned interactive logon sessions
995 of already logged off users (especially in Windows 2000). */
996 PLUID paSessions = NULL;
997 ULONG cSessions = 0;
998 NTSTATUS rcNt = LsaEnumerateLogonSessions(&cSessions, &paSessions);
999 if (rcNt != STATUS_SUCCESS)
1000 {
1001 ULONG ulError = LsaNtStatusToWinError(rcNt);
1002 switch (ulError)
1003 {
1004 case ERROR_NOT_ENOUGH_MEMORY:
1005 VBoxServiceError("Not enough memory to enumerate logon sessions!\n");
1006 break;
1007
1008 case ERROR_SHUTDOWN_IN_PROGRESS:
1009 /* If we're about to shutdown when we were in the middle of enumerating the logon
1010 * sessions, skip the error to not confuse the user with an unnecessary log message. */
1011 VBoxServiceVerbose(3, "Shutdown in progress ...\n");
1012 ulError = ERROR_SUCCESS;
1013 break;
1014
1015 default:
1016 VBoxServiceError("LsaEnumerate failed with error %RU32\n", ulError);
1017 break;
1018 }
1019
1020 if (paSessions)
1021 LsaFreeReturnBuffer(paSessions);
1022
1023 return RTErrConvertFromWin32(ulError);
1024 }
1025 VBoxServiceVerbose(3, "Found %ld sessions\n", cSessions);
1026
1027 PVBOXSERVICEVMINFOPROC paProcs;
1028 DWORD cProcs;
1029 int rc = VBoxServiceVMInfoWinProcessesEnumerate(&paProcs, &cProcs);
1030 if (RT_FAILURE(rc))
1031 {
1032 if (rc == VERR_NO_MEMORY)
1033 VBoxServiceError("Not enough memory to enumerate processes\n");
1034 else
1035 VBoxServiceError("Failed to enumerate processes, rc=%Rrc\n", rc);
1036 }
1037 else
1038 {
1039 PVBOXSERVICEVMINFOUSER pUserInfo;
1040 pUserInfo = (PVBOXSERVICEVMINFOUSER)RTMemAllocZ(cSessions * sizeof(VBOXSERVICEVMINFOUSER) + 1);
1041 if (!pUserInfo)
1042 VBoxServiceError("Not enough memory to store enumerated users!\n");
1043 else
1044 {
1045 ULONG cUniqueUsers = 0;
1046
1047 /**
1048 * Note: The cSessions loop variable does *not* correlate with
1049 * the Windows session ID!
1050 */
1051 for (ULONG i = 0; i < cSessions; i++)
1052 {
1053 VBoxServiceVerbose(3, "Handling session %RU32 (of %RU32)\n", i + 1, cSessions);
1054
1055 VBOXSERVICEVMINFOUSER userSession;
1056 if (VBoxServiceVMInfoWinIsLoggedIn(&userSession, &paSessions[i]))
1057 {
1058 VBoxServiceVerbose(4, "Handling user=%ls, domain=%ls, package=%ls, session=%RU32\n",
1059 userSession.wszUser, userSession.wszLogonDomain, userSession.wszAuthenticationPackage,
1060 userSession.ulLastSession);
1061
1062 /* Retrieve assigned processes of current session. */
1063 uint32_t cCurSessionProcs = VBoxServiceVMInfoWinSessionHasProcesses(&paSessions[i], paProcs, cProcs,
1064 NULL /* Terminal session ID */);
1065 /* Don't return here when current session does not have assigned processes
1066 * anymore -- in that case we have to search through the unique users list below
1067 * and see if got a stale user/session entry. */
1068
1069 if (g_cVerbosity > 3)
1070 {
1071 char szDebugSessionPath[255]; RTStrPrintf(szDebugSessionPath, sizeof(szDebugSessionPath), "/VirtualBox/GuestInfo/Debug/LSA/Session/%RU32",
1072 userSession.ulLastSession);
1073 VBoxServiceWritePropF(s_uDebugGuestPropClientID, szDebugSessionPath,
1074 "#%RU32: cSessionProcs=%RU32 (of %RU32 procs total)", s_uDebugIter, cCurSessionProcs, cProcs);
1075 }
1076
1077 bool fFoundUser = false;
1078 for (ULONG a = 0; a < cUniqueUsers; a++)
1079 {
1080 PVBOXSERVICEVMINFOUSER pCurUser = &pUserInfo[a];
1081 AssertPtr(pCurUser);
1082
1083 if ( !wcscmp(userSession.wszUser, pCurUser->wszUser)
1084 && !wcscmp(userSession.wszLogonDomain, pCurUser->wszLogonDomain)
1085 && !wcscmp(userSession.wszAuthenticationPackage, pCurUser->wszAuthenticationPackage))
1086 {
1087 /*
1088 * Only respect the highest session for the current user.
1089 */
1090 if (userSession.ulLastSession > pCurUser->ulLastSession)
1091 {
1092 VBoxServiceVerbose(4, "Updating user=%ls to %u processes (last used session: %RU32)\n",
1093 pCurUser->wszUser, cCurSessionProcs, userSession.ulLastSession);
1094
1095 if (!cCurSessionProcs)
1096 VBoxServiceVerbose(3, "Stale session for user=%ls detected! Processes: %RU32 -> %RU32, Session: %RU32 -> %RU32\n",
1097 pCurUser->wszUser,
1098 pCurUser->ulNumProcs, cCurSessionProcs,
1099 pCurUser->ulLastSession, userSession.ulLastSession);
1100
1101 pCurUser->ulNumProcs = cCurSessionProcs;
1102 pCurUser->ulLastSession = userSession.ulLastSession;
1103 }
1104 /* There can be multiple session objects using the same session ID for the
1105 * current user -- so when we got the same session again just add the found
1106 * processes to it. */
1107 else if (pCurUser->ulLastSession == userSession.ulLastSession)
1108 {
1109 VBoxServiceVerbose(4, "Updating processes for user=%ls (old procs=%RU32, new procs=%RU32, session=%RU32)\n",
1110 pCurUser->wszUser, pCurUser->ulNumProcs, cCurSessionProcs, pCurUser->ulLastSession);
1111
1112 pCurUser->ulNumProcs = cCurSessionProcs;
1113 }
1114
1115 fFoundUser = true;
1116 break;
1117 }
1118 }
1119
1120 if (!fFoundUser)
1121 {
1122 VBoxServiceVerbose(4, "Adding new user=%ls (session=%RU32) with %RU32 processes\n",
1123 userSession.wszUser, userSession.ulLastSession, cCurSessionProcs);
1124
1125 memcpy(&pUserInfo[cUniqueUsers], &userSession, sizeof(VBOXSERVICEVMINFOUSER));
1126 pUserInfo[cUniqueUsers].ulNumProcs = cCurSessionProcs;
1127 cUniqueUsers++;
1128 Assert(cUniqueUsers <= cSessions);
1129 }
1130 }
1131 }
1132
1133 if (g_cVerbosity > 3)
1134 VBoxServiceWritePropF(s_uDebugGuestPropClientID, "/VirtualBox/GuestInfo/Debug/LSA",
1135 "#%RU32: cSessions=%RU32, cProcs=%RU32, cUniqueUsers=%RU32",
1136 s_uDebugIter, cSessions, cProcs, cUniqueUsers);
1137
1138 VBoxServiceVerbose(3, "Found %u unique logged-in user(s)\n",
1139 cUniqueUsers);
1140
1141 *pcUsersInList = 0;
1142 for (ULONG i = 0; i < cUniqueUsers; i++)
1143 {
1144 if (g_cVerbosity > 3)
1145 {
1146 char szDebugUserPath[255]; RTStrPrintf(szDebugUserPath, sizeof(szDebugUserPath), "/VirtualBox/GuestInfo/Debug/LSA/User/%RU32", i);
1147 VBoxServiceWritePropF(s_uDebugGuestPropClientID, szDebugUserPath,
1148 "#%RU32: szName=%ls, sessionID=%RU32, cProcs=%RU32",
1149 s_uDebugIter, pUserInfo[i].wszUser, pUserInfo[i].ulLastSession, pUserInfo[i].ulNumProcs);
1150 }
1151
1152 bool fAddUser = false;
1153 if (pUserInfo[i].ulNumProcs)
1154 fAddUser = true;
1155
1156 if (fAddUser)
1157 {
1158 VBoxServiceVerbose(3, "User \"%ls\" has %RU32 interactive processes (session=%RU32)\n",
1159 pUserInfo[i].wszUser, pUserInfo[i].ulNumProcs, pUserInfo[i].ulLastSession);
1160
1161 if (*pcUsersInList > 0)
1162 {
1163 rc = RTStrAAppend(ppszUserList, ",");
1164 AssertRCBreakStmt(rc, RTStrFree(*ppszUserList));
1165 }
1166
1167 *pcUsersInList += 1;
1168
1169 char *pszUser = NULL;
1170 char *pszDomain = NULL;
1171 rc = RTUtf16ToUtf8(pUserInfo[i].wszUser, &pszUser);
1172 if ( RT_SUCCESS(rc)
1173 && pUserInfo[i].wszLogonDomain)
1174 rc = RTUtf16ToUtf8(pUserInfo[i].wszLogonDomain, &pszDomain);
1175 if (RT_SUCCESS(rc))
1176 {
1177 /* Append user to users list. */
1178 rc = RTStrAAppend(ppszUserList, pszUser);
1179
1180 /* Do idle detection. */
1181 if (RT_SUCCESS(rc))
1182 rc = vboxServiceVMInfoWinWriteLastInput(pCache, pszUser, pszDomain);
1183 }
1184 else
1185 rc = RTStrAAppend(ppszUserList, "<string-conversion-error>");
1186
1187 RTStrFree(pszUser);
1188 RTStrFree(pszDomain);
1189
1190 AssertRCBreakStmt(rc, RTStrFree(*ppszUserList));
1191 }
1192 }
1193
1194 RTMemFree(pUserInfo);
1195 }
1196 VBoxServiceVMInfoWinProcessesFree(cProcs, paProcs);
1197 }
1198 if (paSessions)
1199 LsaFreeReturnBuffer(paSessions);
1200
1201 s_uDebugIter++;
1202 VbglR3GuestPropDisconnect(s_uDebugGuestPropClientID);
1203
1204 return rc;
1205}
1206
1207#endif /* TARGET_NT4 */
1208
1209int VBoxServiceWinGetComponentVersions(uint32_t uClientID)
1210{
1211 int rc;
1212 char szSysDir[_MAX_PATH] = {0};
1213 char szWinDir[_MAX_PATH] = {0};
1214 char szDriversDir[_MAX_PATH + 32] = {0};
1215
1216 /* ASSUME: szSysDir and szWinDir and derivatives are always ASCII compatible. */
1217 GetSystemDirectory(szSysDir, _MAX_PATH);
1218 GetWindowsDirectory(szWinDir, _MAX_PATH);
1219 RTStrPrintf(szDriversDir, sizeof(szDriversDir), "%s\\drivers", szSysDir);
1220#ifdef RT_ARCH_AMD64
1221 char szSysWowDir[_MAX_PATH + 32] = {0};
1222 RTStrPrintf(szSysWowDir, sizeof(szSysWowDir), "%s\\SysWow64", szWinDir);
1223#endif
1224
1225 /* The file information table. */
1226#ifndef TARGET_NT4
1227 const VBOXSERVICEVMINFOFILE aVBoxFiles[] =
1228 {
1229 { szSysDir, "VBoxControl.exe" },
1230 { szSysDir, "VBoxHook.dll" },
1231 { szSysDir, "VBoxDisp.dll" },
1232 { szSysDir, "VBoxMRXNP.dll" },
1233 { szSysDir, "VBoxService.exe" },
1234 { szSysDir, "VBoxTray.exe" },
1235 { szSysDir, "VBoxGINA.dll" },
1236 { szSysDir, "VBoxCredProv.dll" },
1237# ifdef VBOX_WITH_MMR
1238 { szSysDir, "VBoxMMR.exe" },
1239# endif /* VBOX_WITH_MMR */
1240
1241 /* On 64-bit we don't yet have the OpenGL DLLs in native format.
1242 So just enumerate the 32-bit files in the SYSWOW directory. */
1243# ifdef RT_ARCH_AMD64
1244# ifdef VBOX_WITH_MMR
1245 { szSysWowDir, "VBoxMMRHook.dll" },
1246# endif /* VBOX_WITH_MMR */
1247 { szSysWowDir, "VBoxOGLarrayspu.dll" },
1248 { szSysWowDir, "VBoxOGLcrutil.dll" },
1249 { szSysWowDir, "VBoxOGLerrorspu.dll" },
1250 { szSysWowDir, "VBoxOGLpackspu.dll" },
1251 { szSysWowDir, "VBoxOGLpassthroughspu.dll" },
1252 { szSysWowDir, "VBoxOGLfeedbackspu.dll" },
1253 { szSysWowDir, "VBoxOGL.dll" },
1254# else /* !RT_ARCH_AMD64 */
1255# ifdef VBOX_WITH_MMR
1256 { szSysDir, "VBoxMMRHook.dll" },
1257# endif /* VBOX_WITH_MMR */
1258 { szSysDir, "VBoxOGLarrayspu.dll" },
1259 { szSysDir, "VBoxOGLcrutil.dll" },
1260 { szSysDir, "VBoxOGLerrorspu.dll" },
1261 { szSysDir, "VBoxOGLpackspu.dll" },
1262 { szSysDir, "VBoxOGLpassthroughspu.dll" },
1263 { szSysDir, "VBoxOGLfeedbackspu.dll" },
1264 { szSysDir, "VBoxOGL.dll" },
1265# endif /* !RT_ARCH_AMD64 */
1266
1267 { szDriversDir, "VBoxGuest.sys" },
1268 { szDriversDir, "VBoxMouse.sys" },
1269 { szDriversDir, "VBoxSF.sys" },
1270 { szDriversDir, "VBoxVideo.sys" },
1271 };
1272
1273#else /* TARGET_NT4 */
1274 const VBOXSERVICEVMINFOFILE aVBoxFiles[] =
1275 {
1276 { szSysDir, "VBoxControl.exe" },
1277 { szSysDir, "VBoxHook.dll" },
1278 { szSysDir, "VBoxDisp.dll" },
1279 { szSysDir, "VBoxServiceNT.exe" },
1280 { szSysDir, "VBoxTray.exe" },
1281
1282 { szDriversDir, "VBoxGuestNT.sys" },
1283 { szDriversDir, "VBoxMouseNT.sys" },
1284 { szDriversDir, "VBoxVideo.sys" },
1285 };
1286#endif /* TARGET_NT4 */
1287
1288 for (unsigned i = 0; i < RT_ELEMENTS(aVBoxFiles); i++)
1289 {
1290 char szVer[128];
1291 VBoxServiceGetFileVersionString(aVBoxFiles[i].pszFilePath, aVBoxFiles[i].pszFileName, szVer, sizeof(szVer));
1292 char szPropPath[256];
1293 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestAdd/Components/%s", aVBoxFiles[i].pszFileName);
1294 rc = VBoxServiceWritePropF(uClientID, szPropPath, "%s", szVer);
1295 }
1296
1297 return VINF_SUCCESS;
1298}
1299
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette