blob: 6be3f9ab888a65072ffe2cbaa164485e8d0d5071 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* 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>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000024#include <rfb_win32/Service.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000025#include <lmcons.h>
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010026#include <wtsapi32.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000027
28using namespace rfb;
29using namespace win32;
30
31static LogWriter vlog("CurrentUser");
32
33
34const TCHAR* shellIconClass = _T("Shell_TrayWnd");
35
36BOOL 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
56BOOL 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
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010070CurrentUserToken::CurrentUserToken() {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000071 if (isServiceProcess()) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000072 // Try to get the user token using the Terminal Services APIs
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010073 WTSQueryUserToken(-1, &h);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000074 } 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);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000080 h = INVALID_HANDLE_VALUE;
81 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000082 }
83}
84
85
86ImpersonateCurrentUser::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
99ImpersonateCurrentUser::~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
109UserName::UserName() : TCharArray(UNLEN+1) {
110 DWORD len = UNLEN+1;
111 if (!GetUserName(buf, &len))
112 throw rdr::SystemException("GetUserName failed", GetLastError());
113}
114
115
116UserSID::UserSID() {
117 CurrentUserToken token;
118 if (!token.canImpersonate())
119 return;
120 setSID(Sid::FromToken(token.h));
121}