You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CurrentUser.cxx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright (C) 2002-2004 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 <windows.h>
  20. #include <lmcons.h>
  21. #include <rfb_win32/CurrentUser.h>
  22. #include <rfb_win32/Service.h>
  23. #include <rfb_win32/OSVersion.h>
  24. using namespace rfb;
  25. using namespace win32;
  26. CurrentUserToken::CurrentUserToken() : isValid_(false) {
  27. // - If the OS doesn't support security, ignore it
  28. if (!isServiceProcess()) {
  29. // - Running in User-Mode - just get our current token
  30. if (!OpenProcessToken(GetCurrentProcess(), GENERIC_ALL, &h)) {
  31. DWORD err = GetLastError();
  32. if (err != ERROR_CALL_NOT_IMPLEMENTED)
  33. throw rdr::SystemException("OpenProcessToken failed", GetLastError());
  34. }
  35. isValid_ = true;
  36. } else {
  37. // - Under XP/2003 and above, we can just ask the operating system
  38. typedef BOOL (WINAPI *WTSQueryUserToken_proto)(ULONG, PHANDLE);
  39. DynamicFn<WTSQueryUserToken_proto> _WTSQueryUserToken(_T("wtsapi32.dll"), "WTSQueryUserToken");
  40. if (_WTSQueryUserToken.isValid()) {
  41. (*_WTSQueryUserToken)(-1, &h);
  42. isValid_ = true;
  43. } else {
  44. // - Under NT/2K we have to resort to a nasty hack...
  45. HWND tray = FindWindow(_T("Shell_TrayWnd"), 0);
  46. if (!tray)
  47. return;
  48. DWORD processId = 0;
  49. GetWindowThreadProcessId(tray, &processId);
  50. if (!processId)
  51. return;
  52. Handle process = OpenProcess(MAXIMUM_ALLOWED, FALSE, processId);
  53. if (!process.h)
  54. return;
  55. OpenProcessToken(process, MAXIMUM_ALLOWED, &h);
  56. isValid_ = true;
  57. }
  58. }
  59. }
  60. ImpersonateCurrentUser::ImpersonateCurrentUser() {
  61. RegCloseKey(HKEY_CURRENT_USER);
  62. if (!isServiceProcess())
  63. return;
  64. if (!token.isValid())
  65. throw rdr::Exception("CurrentUserToken is not valid");
  66. if (!ImpersonateLoggedOnUser(token)) {
  67. DWORD err = GetLastError();
  68. if (err != ERROR_CALL_NOT_IMPLEMENTED)
  69. throw rdr::SystemException("Failed to impersonate user", GetLastError());
  70. }
  71. }
  72. ImpersonateCurrentUser::~ImpersonateCurrentUser() {
  73. if (!RevertToSelf()) {
  74. DWORD err = GetLastError();
  75. if (err != ERROR_CALL_NOT_IMPLEMENTED)
  76. exit(err);
  77. }
  78. }
  79. UserName::UserName() : TCharArray(UNLEN+1) {
  80. DWORD len = UNLEN+1;
  81. if (!GetUserName(buf, &len))
  82. throw rdr::SystemException("GetUserName failed", GetLastError());
  83. }