Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CurrentUser.cxx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // -=- Currentuser.cxx
  19. #include <stdlib.h>
  20. #include <rfb/LogWriter.h>
  21. #include <rfb_win32/CurrentUser.h>
  22. #include <rfb_win32/Service.h>
  23. #include <lmcons.h>
  24. #include <wtsapi32.h>
  25. using namespace rfb;
  26. using namespace win32;
  27. static LogWriter vlog("CurrentUser");
  28. const TCHAR* shellIconClass = _T("Shell_TrayWnd");
  29. BOOL CALLBACK enumWindows(HWND hwnd, LPARAM lParam) {
  30. TCHAR className[16];
  31. if (GetClassName(hwnd, className, sizeof(className)) &&
  32. (_tcscmp(className, shellIconClass) == 0)) {
  33. vlog.debug("located tray icon window (%s)", (const char*)CStr(className));
  34. DWORD processId = 0;
  35. GetWindowThreadProcessId(hwnd, &processId);
  36. if (!processId)
  37. return TRUE;
  38. Handle process = OpenProcess(MAXIMUM_ALLOWED, FALSE, processId);
  39. if (!process.h)
  40. return TRUE;
  41. if (!OpenProcessToken(process, MAXIMUM_ALLOWED, (HANDLE*)lParam))
  42. return TRUE;
  43. vlog.debug("obtained user token");
  44. return FALSE;
  45. }
  46. return TRUE;
  47. }
  48. BOOL CALLBACK enumDesktops(LPTSTR lpszDesktop, LPARAM lParam) {
  49. HDESK desktop = OpenDesktop(lpszDesktop, 0, FALSE, DESKTOP_ENUMERATE);
  50. vlog.debug("opening \"%s\"", lpszDesktop);
  51. if (!desktop) {
  52. vlog.info("desktop \"%s\" inaccessible", (const char*)CStr(lpszDesktop));
  53. return TRUE;
  54. }
  55. BOOL result = EnumDesktopWindows(desktop, enumWindows, lParam);
  56. if (!CloseDesktop(desktop))
  57. vlog.info("unable to close desktop: %ld", GetLastError());
  58. return result;
  59. }
  60. CurrentUserToken::CurrentUserToken() {
  61. if (isServiceProcess()) {
  62. // Try to get the user token using the Terminal Services APIs
  63. WTSQueryUserToken(-1, &h);
  64. } else {
  65. // Try to open the security token for the User-Mode process
  66. if (!OpenProcessToken(GetCurrentProcess(), GENERIC_ALL, &h)) {
  67. DWORD err = GetLastError();
  68. if (err != ERROR_CALL_NOT_IMPLEMENTED)
  69. throw rdr::SystemException("OpenProcessToken failed", err);
  70. h = INVALID_HANDLE_VALUE;
  71. }
  72. }
  73. }
  74. ImpersonateCurrentUser::ImpersonateCurrentUser() {
  75. RegCloseKey(HKEY_CURRENT_USER);
  76. if (!isServiceProcess())
  77. return;
  78. if (!token.canImpersonate())
  79. throw rdr::Exception("Cannot impersonate unsafe or null token");
  80. if (!ImpersonateLoggedOnUser(token)) {
  81. DWORD err = GetLastError();
  82. if (err != ERROR_CALL_NOT_IMPLEMENTED)
  83. throw rdr::SystemException("Failed to impersonate user", GetLastError());
  84. }
  85. }
  86. ImpersonateCurrentUser::~ImpersonateCurrentUser() {
  87. if (!RevertToSelf()) {
  88. DWORD err = GetLastError();
  89. if (err != ERROR_CALL_NOT_IMPLEMENTED)
  90. exit(err);
  91. }
  92. RegCloseKey(HKEY_CURRENT_USER);
  93. }
  94. UserName::UserName() : TCharArray(UNLEN+1) {
  95. DWORD len = UNLEN+1;
  96. if (!GetUserName(buf, &len))
  97. throw rdr::SystemException("GetUserName failed", GetLastError());
  98. }
  99. UserSID::UserSID() {
  100. CurrentUserToken token;
  101. if (!token.canImpersonate())
  102. return;
  103. setSID(Sid::FromToken(token.h));
  104. }