1 /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19 // -=- Currentuser.cxx
20
21 #include <stdlib.h>
22 #include <rfb/LogWriter.h>
23 #include <rfb_win32/CurrentUser.h>
24 #include <rfb_win32/Service.h>
25 #include <lmcons.h>
26 #include <wtsapi32.h>
27
28 using namespace rfb;
29 using namespace win32;
30
31 static LogWriter vlog("CurrentUser");
32
33
34 const TCHAR* shellIconClass = _T("Shell_TrayWnd");
35
enumWindows(HWND hwnd,LPARAM lParam)36 BOOL CALLBACK enumWindows(HWND hwnd, LPARAM lParam) {
37 TCHAR className[16];
38 if (GetClassName(hwnd, className, sizeof(className)) &&
39 (_tcscmp(className, shellIconClass) == 0)) {
40 vlog.debug("located tray icon window (%s)", (const char*)CStr(className));
41 DWORD processId = 0;
42 GetWindowThreadProcessId(hwnd, &processId);
43 if (!processId)
44 return TRUE;
45 Handle process = OpenProcess(MAXIMUM_ALLOWED, FALSE, processId);
46 if (!process.h)
47 return TRUE;
48 if (!OpenProcessToken(process, MAXIMUM_ALLOWED, (HANDLE*)lParam))
49 return TRUE;
50 vlog.debug("obtained user token");
51 return FALSE;
52 }
53 return TRUE;
54 }
55
enumDesktops(LPTSTR lpszDesktop,LPARAM lParam)56 BOOL CALLBACK enumDesktops(LPTSTR lpszDesktop, LPARAM lParam) {
57 HDESK desktop = OpenDesktop(lpszDesktop, 0, FALSE, DESKTOP_ENUMERATE);
58 vlog.debug("opening \"%s\"", lpszDesktop);
59 if (!desktop) {
60 vlog.info("desktop \"%s\" inaccessible", (const char*)CStr(lpszDesktop));
61 return TRUE;
62 }
63 BOOL result = EnumDesktopWindows(desktop, enumWindows, lParam);
64 if (!CloseDesktop(desktop))
65 vlog.info("unable to close desktop: %ld", GetLastError());
66 return result;
67 }
68
69
CurrentUserToken()70 CurrentUserToken::CurrentUserToken() {
71 if (isServiceProcess()) {
72 // Try to get the user token using the Terminal Services APIs
73 WTSQueryUserToken(-1, &h);
74 } else {
75 // Try to open the security token for the User-Mode process
76 if (!OpenProcessToken(GetCurrentProcess(), GENERIC_ALL, &h)) {
77 DWORD err = GetLastError();
78 if (err != ERROR_CALL_NOT_IMPLEMENTED)
79 throw rdr::SystemException("OpenProcessToken failed", err);
80 h = INVALID_HANDLE_VALUE;
81 }
82 }
83 }
84
85
ImpersonateCurrentUser()86 ImpersonateCurrentUser::ImpersonateCurrentUser() {
87 RegCloseKey(HKEY_CURRENT_USER);
88 if (!isServiceProcess())
89 return;
90 if (!token.canImpersonate())
91 throw rdr::Exception("Cannot impersonate unsafe or null token");
92 if (!ImpersonateLoggedOnUser(token)) {
93 DWORD err = GetLastError();
94 if (err != ERROR_CALL_NOT_IMPLEMENTED)
95 throw rdr::SystemException("Failed to impersonate user", GetLastError());
96 }
97 }
98
~ImpersonateCurrentUser()99 ImpersonateCurrentUser::~ImpersonateCurrentUser() {
100 if (!RevertToSelf()) {
101 DWORD err = GetLastError();
102 if (err != ERROR_CALL_NOT_IMPLEMENTED)
103 exit(err);
104 }
105 RegCloseKey(HKEY_CURRENT_USER);
106 }
107
108
UserName()109 UserName::UserName() : TCharArray(UNLEN+1) {
110 DWORD len = UNLEN+1;
111 if (!GetUserName(buf, &len))
112 throw rdr::SystemException("GetUserName failed", GetLastError());
113 }
114
115
UserSID()116 UserSID::UserSID() {
117 CurrentUserToken token;
118 if (!token.canImpersonate())
119 return;
120 setSID(Sid::FromToken(token.h));
121 }
122