ensure_user_win.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/host/file_transfer/ensure_user.h"
  5. #include <Windows.h>
  6. #include <WtsApi32.h>
  7. #include "base/logging.h"
  8. #include "base/win/scoped_handle.h"
  9. namespace remoting {
  10. protocol::FileTransferResult<absl::monostate> EnsureUserContext() {
  11. // Impersonate the currently logged-in user, or fail if there is none.
  12. HANDLE user_token = nullptr;
  13. if (!WTSQueryUserToken(WTS_CURRENT_SESSION, &user_token)) {
  14. PLOG(ERROR) << "Failed to get current user token";
  15. return protocol::MakeFileTransferError(
  16. FROM_HERE,
  17. GetLastError() == ERROR_NO_TOKEN
  18. ? protocol::FileTransfer_Error_Type_NOT_LOGGED_IN
  19. : protocol::FileTransfer_Error_Type_UNEXPECTED_ERROR,
  20. GetLastError());
  21. }
  22. base::win::ScopedHandle scoped_user_token(user_token);
  23. if (!ImpersonateLoggedOnUser(scoped_user_token.Get())) {
  24. PLOG(ERROR) << "Failed to impersonate user";
  25. return protocol::MakeFileTransferError(
  26. FROM_HERE, protocol::FileTransfer_Error_Type_UNEXPECTED_ERROR,
  27. GetLastError());
  28. }
  29. return kSuccessTag;
  30. }
  31. } // namespace remoting