Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/host/desktop_session_win.h" |
| 6 | |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 7 | #include <objbase.h> |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 8 | #include <sddl.h> |
Robert Liao | 1a37835 | 2017-10-18 01:31:17 | [diff] [blame] | 9 | #include <wrl/client.h> |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 10 | |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 11 | #include <limits> |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 12 | #include <memory> |
David Bienvenu | bb3fd1a | 2023-01-10 16:42:17 | [diff] [blame] | 13 | #include <string> |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 14 | #include <utility> |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 15 | |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 16 | #include "base/base_switches.h" |
| 17 | #include "base/command_line.h" |
Lei Zhang | 71d3a02 | 2021-05-27 17:08:42 | [diff] [blame] | 18 | #include "base/cxx17_backports.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 19 | #include "base/files/file_path.h" |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 20 | #include "base/guid.h" |
Ken Rockot | 33f8dfb | 2020-08-31 20:17:40 | [diff] [blame] | 21 | #include "base/logging.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 22 | #include "base/memory/ref_counted.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 23 | #include "base/memory/weak_ptr.h" |
[email protected] | eaf9253 | 2013-06-11 07:39:19 | [diff] [blame] | 24 | #include "base/strings/stringprintf.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 25 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 26 | #include "base/threading/thread_checker.h" |
[email protected] | 5d7eb86 | 2013-06-28 15:21:24 | [diff] [blame] | 27 | #include "base/timer/timer.h" |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 28 | #include "base/win/registry.h" |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 29 | #include "base/win/scoped_bstr.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 30 | #include "base/win/scoped_handle.h" |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 31 | #include "remoting/base/auto_thread_task_runner.h" |
Joe Downing | b0fb542 | 2021-12-07 21:26:20 | [diff] [blame] | 32 | #include "remoting/host/base/screen_resolution.h" |
| 33 | #include "remoting/host/base/switches.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 34 | #include "remoting/host/daemon_process.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 35 | #include "remoting/host/desktop_session.h" |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 36 | #include "remoting/host/host_main.h" |
| 37 | #include "remoting/host/ipc_constants.h" |
[email protected] | 0dffd55 | 2012-12-07 01:08:09 | [diff] [blame] | 38 | #include "remoting/host/sas_injector.h" |
nicholss | e3320ae | 2016-09-16 20:12:59 | [diff] [blame] | 39 | // MIDL-generated declarations and definitions. |
| 40 | #include "remoting/host/win/chromoting_lib.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 41 | #include "remoting/host/win/host_service.h" |
Yuwei Huang | aa06bab4 | 2022-02-25 21:24:40 | [diff] [blame] | 42 | #include "remoting/host/win/trust_util.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 43 | #include "remoting/host/win/worker_process_launcher.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 44 | #include "remoting/host/win/wts_session_process_delegate.h" |
[email protected] | 3fd005e2 | 2013-02-28 20:11:29 | [diff] [blame] | 45 | #include "remoting/host/win/wts_terminal_monitor.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 46 | #include "remoting/host/win/wts_terminal_observer.h" |
| 47 | #include "remoting/host/worker_process_ipc_delegate.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 48 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 49 | using base::win::ScopedHandle; |
| 50 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 51 | namespace remoting { |
| 52 | |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 53 | namespace { |
| 54 | |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 55 | // The security descriptor of the daemon IPC endpoint. It gives full access |
[email protected] | 6d65223d | 2013-03-05 20:32:46 | [diff] [blame] | 56 | // to SYSTEM and denies access by anyone else. |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 57 | const wchar_t kDaemonIpcSecurityDescriptor[] = |
| 58 | SDDL_OWNER L":" SDDL_LOCAL_SYSTEM |
| 59 | SDDL_GROUP L":" SDDL_LOCAL_SYSTEM |
| 60 | SDDL_DACL L":(" |
| 61 | SDDL_ACCESS_ALLOWED L";;" SDDL_GENERIC_ALL L";;;" SDDL_LOCAL_SYSTEM |
| 62 | L")"; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 63 | |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 64 | // The command line parameters that should be copied from the service's command |
[email protected] | f0ce8fbb | 2014-06-16 18:36:17 | [diff] [blame] | 65 | // line to the desktop process. |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 66 | const char* kCopiedSwitchNames[] = {switches::kV, switches::kVModule}; |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 67 | |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 68 | // The default screen dimensions for an RDP session. |
[email protected] | 792c4f4 | 2013-04-06 16:05:21 | [diff] [blame] | 69 | const int kDefaultRdpScreenWidth = 1280; |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 70 | const int kDefaultRdpScreenHeight = 768; |
| 71 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 72 | // The minimum effective screen dimensions supported by Windows are 800x600. |
| 73 | const int kMinRdpScreenWidth = 800; |
| 74 | const int kMinRdpScreenHeight = 600; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 75 | |
David Bienvenu | bb3fd1a | 2023-01-10 16:42:17 | [diff] [blame] | 76 | // Windows supports dimensions up to 8192x8192. |
| 77 | const int kMaxRdpScreenWidth = 8192; |
| 78 | const int kMaxRdpScreenHeight = 8192; |
Joe Downing | 4cc20bee | 2021-07-02 18:14:57 | [diff] [blame] | 79 | |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 80 | // Default dots per inch used by RDP is 96 DPI. |
| 81 | const int kDefaultRdpDpi = 96; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 82 | |
| 83 | // The session attach notification should arrive within 30 seconds. |
| 84 | const int kSessionAttachTimeoutSeconds = 30; |
| 85 | |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 86 | // The default port number used for establishing an RDP session. |
| 87 | const int kDefaultRdpPort = 3389; |
| 88 | |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 89 | // Used for validating the required RDP registry values. |
| 90 | const int kRdpConnectionsDisabled = 1; |
| 91 | const int kNetworkLevelAuthEnabled = 1; |
| 92 | const int kSecurityLayerTlsRequired = 2; |
| 93 | |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 94 | // The values used to establish RDP connections are stored in the registry. |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 95 | const wchar_t kRdpSettingsKeyName[] = |
| 96 | L"SYSTEM\\CurrentControlSet\\Control\\Terminal Server"; |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 97 | const wchar_t kRdpTcpSettingsKeyName[] = |
| 98 | L"SYSTEM\\CurrentControlSet\\" |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 99 | L"Control\\Terminal Server\\WinStations\\RDP-Tcp"; |
| 100 | const wchar_t kRdpPortValueName[] = L"PortNumber"; |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 101 | const wchar_t kDenyTsConnectionsValueName[] = L"fDenyTSConnections"; |
| 102 | const wchar_t kNetworkLevelAuthValueName[] = L"UserAuthentication"; |
| 103 | const wchar_t kSecurityLayerValueName[] = L"SecurityLayer"; |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 104 | |
joedow | b7a9ca3 | 2016-12-23 00:15:25 | [diff] [blame] | 105 | webrtc::DesktopSize GetBoundedRdpDesktopSize(int width, int height) { |
| 106 | return webrtc::DesktopSize( |
David Bienvenu | bb3fd1a | 2023-01-10 16:42:17 | [diff] [blame] | 107 | base::clamp(width, kMinRdpScreenWidth, kMaxRdpScreenWidth), |
| 108 | base::clamp(height, kMinRdpScreenHeight, kMaxRdpScreenHeight)); |
joedow | b7a9ca3 | 2016-12-23 00:15:25 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 111 | // DesktopSession implementation which attaches to the host's physical console. |
| 112 | // Receives IPC messages from the desktop process, running in the console |
| 113 | // session, via |WorkerProcessIpcDelegate|, and monitors console session |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 114 | // attach/detach events via |WtsConsoleObserver|. |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 115 | class ConsoleSession : public DesktopSessionWin { |
| 116 | public: |
| 117 | // Same as DesktopSessionWin(). |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 118 | ConsoleSession(scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 119 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 120 | DaemonProcess* daemon_process, |
| 121 | int id, |
| 122 | WtsTerminalMonitor* monitor); |
Peter Boström | e9178e4 | 2021-09-22 18:11:49 | [diff] [blame] | 123 | |
| 124 | ConsoleSession(const ConsoleSession&) = delete; |
| 125 | ConsoleSession& operator=(const ConsoleSession&) = delete; |
| 126 | |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 127 | ~ConsoleSession() override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 128 | |
| 129 | protected: |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 130 | // DesktopSession overrides. |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 131 | void SetScreenResolution(const ScreenResolution& resolution) override; |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 132 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 133 | // DesktopSessionWin overrides. |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 134 | void InjectSas() override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 135 | |
| 136 | private: |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 137 | std::unique_ptr<SasInjector> sas_injector_; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | // DesktopSession implementation which attaches to virtual RDP console. |
| 141 | // Receives IPC messages from the desktop process, running in the console |
| 142 | // session, via |WorkerProcessIpcDelegate|, and monitors console session |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 143 | // attach/detach events via |WtsConsoleObserver|. |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 144 | class RdpSession : public DesktopSessionWin { |
| 145 | public: |
| 146 | // Same as DesktopSessionWin(). |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 147 | RdpSession(scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 148 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 149 | DaemonProcess* daemon_process, |
| 150 | int id, |
| 151 | WtsTerminalMonitor* monitor); |
Peter Boström | e9178e4 | 2021-09-22 18:11:49 | [diff] [blame] | 152 | |
| 153 | RdpSession(const RdpSession&) = delete; |
| 154 | RdpSession& operator=(const RdpSession&) = delete; |
| 155 | |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 156 | ~RdpSession() override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 157 | |
| 158 | // Performs the part of initialization that can fail. |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 159 | bool Initialize(const ScreenResolution& resolution); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 160 | |
| 161 | // Mirrors IRdpDesktopSessionEventHandler. |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 162 | void OnRdpConnected(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 163 | void OnRdpClosed(); |
| 164 | |
| 165 | protected: |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 166 | // DesktopSession overrides. |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 167 | void SetScreenResolution(const ScreenResolution& resolution) override; |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 168 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 169 | // DesktopSessionWin overrides. |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 170 | void InjectSas() override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 171 | |
| 172 | private: |
| 173 | // An implementation of IRdpDesktopSessionEventHandler interface that forwards |
| 174 | // notifications to the owning desktop session. |
| 175 | class EventHandler : public IRdpDesktopSessionEventHandler { |
| 176 | public: |
| 177 | explicit EventHandler(base::WeakPtr<RdpSession> desktop_session); |
Peter Boström | e9178e4 | 2021-09-22 18:11:49 | [diff] [blame] | 178 | |
| 179 | EventHandler(const EventHandler&) = delete; |
| 180 | EventHandler& operator=(const EventHandler&) = delete; |
| 181 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 182 | virtual ~EventHandler(); |
| 183 | |
| 184 | // IUnknown interface. |
mostynb | 11d989c | 2014-10-08 16:58:09 | [diff] [blame] | 185 | STDMETHOD_(ULONG, AddRef)() override; |
| 186 | STDMETHOD_(ULONG, Release)() override; |
| 187 | STDMETHOD(QueryInterface)(REFIID riid, void** ppv) override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 188 | |
| 189 | // IRdpDesktopSessionEventHandler interface. |
mostynb | 11d989c | 2014-10-08 16:58:09 | [diff] [blame] | 190 | STDMETHOD(OnRdpConnected)() override; |
| 191 | STDMETHOD(OnRdpClosed)() override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 192 | |
| 193 | private: |
| 194 | ULONG ref_count_; |
| 195 | |
| 196 | // Points to the desktop session object receiving OnRdpXxx() notifications. |
| 197 | base::WeakPtr<RdpSession> desktop_session_; |
| 198 | |
| 199 | // This class must be used on a single thread. |
| 200 | base::ThreadChecker thread_checker_; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 201 | }; |
| 202 | |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 203 | // Examines the system settings required to establish an RDP session. |
| 204 | // This method returns false if the values are retrieved and any of them would |
| 205 | // prevent us from creating an RDP connection. |
| 206 | bool VerifyRdpSettings(); |
| 207 | |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 208 | // Retrieves a DWORD value from the registry. Returns true on success. |
| 209 | bool RetrieveDwordRegistryValue(const wchar_t* key_name, |
| 210 | const wchar_t* value_name, |
| 211 | DWORD* value); |
| 212 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 213 | // Used to create an RDP desktop session. |
Robert Liao | 1a37835 | 2017-10-18 01:31:17 | [diff] [blame] | 214 | Microsoft::WRL::ComPtr<IRdpDesktopSession> rdp_desktop_session_; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 215 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 216 | // Used to match |rdp_desktop_session_| with the session it is attached to. |
| 217 | std::string terminal_id_; |
| 218 | |
Jeremy Roman | 2b7c950 | 2019-08-21 22:34:08 | [diff] [blame] | 219 | base::WeakPtrFactory<RdpSession> weak_factory_{this}; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | ConsoleSession::ConsoleSession( |
| 223 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 224 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 225 | DaemonProcess* daemon_process, |
| 226 | int id, |
| 227 | WtsTerminalMonitor* monitor) |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 228 | : DesktopSessionWin(caller_task_runner, |
| 229 | io_task_runner, |
| 230 | daemon_process, |
| 231 | id, |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 232 | monitor) { |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 233 | StartMonitoring(WtsTerminalMonitor::kConsole); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 234 | } |
| 235 | |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 236 | ConsoleSession::~ConsoleSession() {} |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 237 | |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 238 | void ConsoleSession::SetScreenResolution(const ScreenResolution& resolution) { |
| 239 | // Do nothing. The screen resolution of the console session is controlled by |
| 240 | // the DesktopSessionAgent instance running in that session. |
| 241 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 242 | } |
| 243 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 244 | void ConsoleSession::InjectSas() { |
[email protected] | dd8116608 | 2013-03-18 20:33:33 | [diff] [blame] | 245 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 246 | |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 247 | if (!sas_injector_) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 248 | sas_injector_ = SasInjector::Create(); |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 249 | } |
| 250 | if (!sas_injector_->InjectSas()) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 251 | LOG(ERROR) << "Failed to inject Secure Attention Sequence."; |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 252 | } |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 253 | } |
| 254 | |
Jeremy Roman | 2b7c950 | 2019-08-21 22:34:08 | [diff] [blame] | 255 | RdpSession::RdpSession(scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 256 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 257 | DaemonProcess* daemon_process, |
| 258 | int id, |
| 259 | WtsTerminalMonitor* monitor) |
| 260 | : DesktopSessionWin(caller_task_runner, |
| 261 | io_task_runner, |
| 262 | daemon_process, |
| 263 | id, |
| 264 | monitor) {} |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 265 | |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 266 | RdpSession::~RdpSession() {} |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 267 | |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 268 | bool RdpSession::Initialize(const ScreenResolution& resolution) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 269 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 270 | |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 271 | if (!VerifyRdpSettings()) { |
| 272 | LOG(ERROR) << "Could not create an RDP session due to invalid settings."; |
| 273 | return false; |
| 274 | } |
| 275 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 276 | // Create the RDP wrapper object. |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 277 | HRESULT result = |
| 278 | ::CoCreateInstance(__uuidof(RdpDesktopSession), nullptr, CLSCTX_ALL, |
| 279 | IID_PPV_ARGS(&rdp_desktop_session_)); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 280 | if (FAILED(result)) { |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 281 | LOG(ERROR) << "Failed to create RdpSession object, 0x" << std::hex << result |
| 282 | << std::dec << "."; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 286 | ScreenResolution local_resolution = resolution; |
| 287 | |
| 288 | // If the screen resolution is not specified, use the default screen |
| 289 | // resolution. |
| 290 | if (local_resolution.IsEmpty()) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 291 | local_resolution = ScreenResolution( |
| 292 | webrtc::DesktopSize(kDefaultRdpScreenWidth, kDefaultRdpScreenHeight), |
| 293 | webrtc::DesktopVector(kDefaultRdpDpi, kDefaultRdpDpi)); |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 294 | } |
| 295 | |
Joe Downing | 90e213f | 2023-02-13 19:13:01 | [diff] [blame] | 296 | // Get the screen dimensions using the default DPI for the RDP client window. |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 297 | webrtc::DesktopSize host_size = local_resolution.ScaleDimensionsToDpi( |
| 298 | webrtc::DesktopVector(kDefaultRdpDpi, kDefaultRdpDpi)); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 299 | |
| 300 | // Make sure that the host resolution is within the limits supported by RDP. |
joedow | b7a9ca3 | 2016-12-23 00:15:25 | [diff] [blame] | 301 | host_size = GetBoundedRdpDesktopSize(host_size.width(), host_size.height()); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 302 | |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 303 | // Read the port number used by RDP. |
| 304 | DWORD server_port = kDefaultRdpPort; |
| 305 | if (RetrieveDwordRegistryValue(kRdpTcpSettingsKeyName, kRdpPortValueName, |
| 306 | &server_port) && |
| 307 | server_port > 65535) { |
| 308 | LOG(ERROR) << "Invalid RDP port specified: " << server_port; |
| 309 | return false; |
| 310 | } |
| 311 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 312 | // Create an RDP session. |
Robert Liao | 1a37835 | 2017-10-18 01:31:17 | [diff] [blame] | 313 | Microsoft::WRL::ComPtr<IRdpDesktopSessionEventHandler> event_handler( |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 314 | new EventHandler(weak_factory_.GetWeakPtr())); |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 315 | terminal_id_ = base::GenerateGUID(); |
Jan Wilken Dörrie | 06451e6 | 2021-02-22 18:07:29 | [diff] [blame] | 316 | base::win::ScopedBstr terminal_id(base::UTF8ToWide(terminal_id_)); |
Robert Liao | 7dfd293b | 2020-02-06 21:11:48 | [diff] [blame] | 317 | result = rdp_desktop_session_->Connect( |
| 318 | host_size.width(), host_size.height(), kDefaultRdpDpi, kDefaultRdpDpi, |
| 319 | terminal_id.Get(), server_port, event_handler.Get()); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 320 | if (FAILED(result)) { |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 321 | LOG(ERROR) << "RdpSession::Create() failed, 0x" << std::hex << result |
| 322 | << std::dec << "."; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 323 | return false; |
| 324 | } |
| 325 | |
| 326 | return true; |
| 327 | } |
| 328 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 329 | void RdpSession::OnRdpConnected() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 330 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 331 | |
| 332 | StopMonitoring(); |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 333 | StartMonitoring(terminal_id_); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void RdpSession::OnRdpClosed() { |
| 337 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 338 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 339 | TerminateSession(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 340 | } |
| 341 | |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 342 | void RdpSession::SetScreenResolution(const ScreenResolution& resolution) { |
| 343 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
joedow | b7a9ca3 | 2016-12-23 00:15:25 | [diff] [blame] | 344 | DCHECK(!resolution.IsEmpty()); |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 345 | |
Joe Downing | 90e213f | 2023-02-13 19:13:01 | [diff] [blame] | 346 | webrtc::DesktopSize bounded_size = GetBoundedRdpDesktopSize( |
| 347 | resolution.dimensions().width(), resolution.dimensions().height()); |
joedow | b7a9ca3 | 2016-12-23 00:15:25 | [diff] [blame] | 348 | |
Joe Downing | 90e213f | 2023-02-13 19:13:01 | [diff] [blame] | 349 | rdp_desktop_session_->ChangeResolution( |
| 350 | bounded_size.width(), bounded_size.height(), resolution.dpi().x(), |
| 351 | resolution.dpi().y()); |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 352 | } |
| 353 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 354 | void RdpSession::InjectSas() { |
| 355 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 356 | |
[email protected] | 97328ee | 2013-06-04 06:46:59 | [diff] [blame] | 357 | rdp_desktop_session_->InjectSas(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 358 | } |
| 359 | |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 360 | bool RdpSession::VerifyRdpSettings() { |
| 361 | // Verify RDP connections are enabled. |
| 362 | DWORD deny_ts_connections_flag = 0; |
| 363 | if (RetrieveDwordRegistryValue(kRdpSettingsKeyName, |
| 364 | kDenyTsConnectionsValueName, |
| 365 | &deny_ts_connections_flag) && |
| 366 | deny_ts_connections_flag == kRdpConnectionsDisabled) { |
| 367 | LOG(ERROR) << "RDP Connections must be enabled."; |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | // Verify Network Level Authentication is disabled. |
| 372 | DWORD network_level_auth_flag = 0; |
| 373 | if (RetrieveDwordRegistryValue(kRdpTcpSettingsKeyName, |
| 374 | kNetworkLevelAuthValueName, |
| 375 | &network_level_auth_flag) && |
| 376 | network_level_auth_flag == kNetworkLevelAuthEnabled) { |
| 377 | LOG(ERROR) << "Network Level Authentication for RDP must be disabled."; |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | // Verify Security Layer is not set to TLS. It can be either of the other two |
| 382 | // values, but forcing TLS will prevent us from establishing a connection. |
| 383 | DWORD security_layer_flag = 0; |
| 384 | if (RetrieveDwordRegistryValue(kRdpTcpSettingsKeyName, |
| 385 | kSecurityLayerValueName, |
| 386 | &security_layer_flag) && |
| 387 | security_layer_flag == kSecurityLayerTlsRequired) { |
| 388 | LOG(ERROR) << "RDP SecurityLayer must not be set to TLS."; |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | return true; |
| 393 | } |
| 394 | |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 395 | bool RdpSession::RetrieveDwordRegistryValue(const wchar_t* key_name, |
| 396 | const wchar_t* value_name, |
| 397 | DWORD* value) { |
| 398 | DCHECK(key_name); |
| 399 | DCHECK(value_name); |
| 400 | DCHECK(value); |
| 401 | |
| 402 | base::win::RegKey key(HKEY_LOCAL_MACHINE, key_name, KEY_READ); |
| 403 | if (!key.Valid()) { |
| 404 | LOG(WARNING) << "Failed to open key: " << key_name; |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | if (key.ReadValueDW(value_name, value) != ERROR_SUCCESS) { |
| 409 | LOG(WARNING) << "Failed to read registry value: " << value_name; |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | return true; |
| 414 | } |
| 415 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 416 | RdpSession::EventHandler::EventHandler( |
| 417 | base::WeakPtr<RdpSession> desktop_session) |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 418 | : ref_count_(0), desktop_session_(desktop_session) {} |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 419 | |
| 420 | RdpSession::EventHandler::~EventHandler() { |
| 421 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 422 | |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 423 | if (desktop_session_) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 424 | desktop_session_->OnRdpClosed(); |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 425 | } |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 426 | } |
| 427 | |
Nico Weber | 793099e | 2022-02-03 21:16:34 | [diff] [blame] | 428 | ULONG STDMETHODCALLTYPE RdpSession::EventHandler::AddRef() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 429 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 430 | |
| 431 | return ++ref_count_; |
| 432 | } |
| 433 | |
Nico Weber | 793099e | 2022-02-03 21:16:34 | [diff] [blame] | 434 | ULONG STDMETHODCALLTYPE RdpSession::EventHandler::Release() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 435 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 436 | |
| 437 | if (--ref_count_ == 0) { |
| 438 | delete this; |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | return ref_count_; |
| 443 | } |
| 444 | |
Nico Weber | 793099e | 2022-02-03 21:16:34 | [diff] [blame] | 445 | STDMETHODIMP |
Nico Weber | 829c6df | 2021-11-26 19:13:39 | [diff] [blame] | 446 | RdpSession::EventHandler::QueryInterface(REFIID riid, void** ppv) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 447 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 448 | |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 449 | if (riid == IID_IUnknown || riid == IID_IRdpDesktopSessionEventHandler) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 450 | *ppv = static_cast<IRdpDesktopSessionEventHandler*>(this); |
| 451 | AddRef(); |
| 452 | return S_OK; |
| 453 | } |
| 454 | |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 455 | *ppv = nullptr; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 456 | return E_NOINTERFACE; |
| 457 | } |
| 458 | |
Nico Weber | 793099e | 2022-02-03 21:16:34 | [diff] [blame] | 459 | STDMETHODIMP RdpSession::EventHandler::OnRdpConnected() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 460 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 461 | |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 462 | if (desktop_session_) { |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 463 | desktop_session_->OnRdpConnected(); |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 464 | } |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 465 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 466 | return S_OK; |
| 467 | } |
| 468 | |
Nico Weber | 793099e | 2022-02-03 21:16:34 | [diff] [blame] | 469 | STDMETHODIMP RdpSession::EventHandler::OnRdpClosed() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 470 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 471 | |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 472 | if (!desktop_session_) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 473 | return S_OK; |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 474 | } |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 475 | |
| 476 | base::WeakPtr<RdpSession> desktop_session = desktop_session_; |
| 477 | desktop_session_.reset(); |
| 478 | desktop_session->OnRdpClosed(); |
| 479 | return S_OK; |
| 480 | } |
| 481 | |
David Bienvenu | bb3fd1a | 2023-01-10 16:42:17 | [diff] [blame] | 482 | } // namespace |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 483 | |
| 484 | // static |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 485 | std::unique_ptr<DesktopSession> DesktopSessionWin::CreateForConsole( |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 486 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 487 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 488 | DaemonProcess* daemon_process, |
| 489 | int id, |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 490 | const ScreenResolution& resolution) { |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 491 | return std::make_unique<ConsoleSession>(caller_task_runner, io_task_runner, |
ricea | 68860bd | 2016-08-22 02:48:56 | [diff] [blame] | 492 | daemon_process, id, |
| 493 | HostService::GetInstance()); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // static |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 497 | std::unique_ptr<DesktopSession> DesktopSessionWin::CreateForVirtualTerminal( |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 498 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 499 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 500 | DaemonProcess* daemon_process, |
| 501 | int id, |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 502 | const ScreenResolution& resolution) { |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 503 | std::unique_ptr<RdpSession> session( |
| 504 | new RdpSession(caller_task_runner, io_task_runner, daemon_process, id, |
| 505 | HostService::GetInstance())); |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 506 | if (!session->Initialize(resolution)) { |
sergeyu | afce978 | 2014-09-29 19:38:30 | [diff] [blame] | 507 | return nullptr; |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 508 | } |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 509 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 510 | return std::move(session); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | DesktopSessionWin::DesktopSessionWin( |
| 514 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 515 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 516 | DaemonProcess* daemon_process, |
| 517 | int id, |
[email protected] | 3fd005e2 | 2013-02-28 20:11:29 | [diff] [blame] | 518 | WtsTerminalMonitor* monitor) |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 519 | : DesktopSession(daemon_process, id), |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 520 | caller_task_runner_(caller_task_runner), |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 521 | io_task_runner_(io_task_runner), |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 522 | monitor_(monitor), |
| 523 | monitoring_notifications_(false) { |
| 524 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 525 | |
| 526 | ReportElapsedTime("created"); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | DesktopSessionWin::~DesktopSessionWin() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 530 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 531 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 532 | StopMonitoring(); |
| 533 | } |
| 534 | |
| 535 | void DesktopSessionWin::OnSessionAttachTimeout() { |
| 536 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 537 | |
| 538 | LOG(ERROR) << "Session attach notification didn't arrived within " |
| 539 | << kSessionAttachTimeoutSeconds << " seconds."; |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 540 | TerminateSession(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 541 | } |
| 542 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 543 | void DesktopSessionWin::StartMonitoring(const std::string& terminal_id) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 544 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 545 | DCHECK(!monitoring_notifications_); |
| 546 | DCHECK(!session_attach_timer_.IsRunning()); |
| 547 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 548 | ReportElapsedTime("started monitoring"); |
| 549 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 550 | session_attach_timer_.Start(FROM_HERE, |
| 551 | base::Seconds(kSessionAttachTimeoutSeconds), this, |
| 552 | &DesktopSessionWin::OnSessionAttachTimeout); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 553 | |
| 554 | monitoring_notifications_ = true; |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 555 | monitor_->AddWtsTerminalObserver(terminal_id, this); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | void DesktopSessionWin::StopMonitoring() { |
| 559 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 560 | |
| 561 | if (monitoring_notifications_) { |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 562 | ReportElapsedTime("stopped monitoring"); |
| 563 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 564 | monitoring_notifications_ = false; |
| 565 | monitor_->RemoveWtsTerminalObserver(this); |
| 566 | } |
| 567 | |
| 568 | session_attach_timer_.Stop(); |
| 569 | OnSessionDetached(); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 570 | } |
| 571 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 572 | void DesktopSessionWin::TerminateSession() { |
| 573 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 574 | |
| 575 | StopMonitoring(); |
| 576 | |
| 577 | // This call will delete |this| so it should be at the very end of the method. |
| 578 | daemon_process()->CloseDesktopSession(id()); |
| 579 | } |
| 580 | |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 581 | void DesktopSessionWin::OnChannelConnected(int32_t peer_pid) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 582 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 583 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 584 | ReportElapsedTime("channel connected"); |
| 585 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 586 | VLOG(1) << "IPC: daemon <- desktop (" << peer_pid << ")"; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 587 | } |
| 588 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 589 | void DesktopSessionWin::OnPermanentError(int exit_code) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 590 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 591 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 592 | TerminateSession(); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 593 | } |
| 594 | |
zijiehe | dda3acb | 2017-06-29 23:19:16 | [diff] [blame] | 595 | void DesktopSessionWin::OnWorkerProcessStopped() {} |
| 596 | |
Joe Downing | d4d6f34 | 2021-09-23 22:12:09 | [diff] [blame] | 597 | void DesktopSessionWin::OnAssociatedInterfaceRequest( |
| 598 | const std::string& interface_name, |
| 599 | mojo::ScopedInterfaceEndpointHandle handle) { |
| 600 | if (interface_name == mojom::DesktopSessionRequestHandler::Name_) { |
| 601 | if (desktop_session_request_handler_.is_bound()) { |
| 602 | LOG(ERROR) << "Receiver already bound for associated interface: " |
| 603 | << mojom::DesktopSessionRequestHandler::Name_; |
| 604 | CrashDesktopProcess(FROM_HERE); |
| 605 | } |
| 606 | |
| 607 | mojo::PendingAssociatedReceiver<mojom::DesktopSessionRequestHandler> |
| 608 | pending_receiver(std::move(handle)); |
| 609 | desktop_session_request_handler_.Bind(std::move(pending_receiver)); |
Joe Downing | 8248020 | 2021-11-02 22:41:12 | [diff] [blame] | 610 | |
Joe Downing | cae37b5 | 2021-11-09 18:54:42 | [diff] [blame] | 611 | // Reset the receiver on disconnect so |desktop_session_request_handler_| |
| 612 | // can be re-bound if |launcher_| spawns a new desktop process. |
| 613 | desktop_session_request_handler_.reset_on_disconnect(); |
Joe Downing | d4d6f34 | 2021-09-23 22:12:09 | [diff] [blame] | 614 | } else { |
| 615 | LOG(ERROR) << "Unknown associated interface requested: " << interface_name |
| 616 | << ", crashing the desktop process"; |
| 617 | CrashDesktopProcess(FROM_HERE); |
| 618 | } |
| 619 | } |
| 620 | |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 621 | void DesktopSessionWin::OnSessionAttached(uint32_t session_id) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 622 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 623 | DCHECK(!launcher_); |
| 624 | DCHECK(monitoring_notifications_); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 625 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 626 | ReportElapsedTime("attached"); |
| 627 | |
David Bienvenu | bb3fd1a | 2023-01-10 16:42:17 | [diff] [blame] | 628 | // Get the name of the executable to run. `kDesktopBinaryName` specifies |
| 629 | // uiAccess="true" in its manifest. Prefer kDesktopBinaryName but fall back |
| 630 | // to kHostBinaryName if there is a problem loading it. |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 631 | base::FilePath desktop_binary; |
David Bienvenu | bb3fd1a | 2023-01-10 16:42:17 | [diff] [blame] | 632 | bool result = GetInstalledBinaryPath(kDesktopBinaryName, &desktop_binary); |
| 633 | |
Joe Downing | 9aa63b5e7 | 2021-08-24 21:19:25 | [diff] [blame] | 634 | if (!result || !IsBinaryTrusted(desktop_binary)) { |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 635 | result = GetInstalledBinaryPath(kHostBinaryName, &desktop_binary); |
| 636 | } |
| 637 | |
| 638 | if (!result) { |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 639 | TerminateSession(); |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 640 | return; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 641 | } |
| 642 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 643 | session_attach_timer_.Stop(); |
| 644 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 645 | std::unique_ptr<base::CommandLine> target( |
| 646 | new base::CommandLine(desktop_binary)); |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 647 | target->AppendSwitchASCII(kProcessTypeSwitchName, kProcessTypeDesktop); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 648 | // Copy the command line switches enabling verbose logging. |
avi | 429bbdd | 2014-12-23 00:27:27 | [diff] [blame] | 649 | target->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(), |
Daniel Cheng | 9327fd31 | 2022-02-26 09:26:52 | [diff] [blame] | 650 | kCopiedSwitchNames, std::size(kCopiedSwitchNames)); |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 651 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 652 | // Create a delegate capable of launching a process in a different session. |
David Bienvenu | bb3fd1a | 2023-01-10 16:42:17 | [diff] [blame] | 653 | // Launch elevated to enable injection of Alt+Tab and Ctrl+Alt+Del. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 654 | std::unique_ptr<WtsSessionProcessDelegate> delegate( |
| 655 | new WtsSessionProcessDelegate( |
David Bienvenu | bb3fd1a | 2023-01-10 16:42:17 | [diff] [blame] | 656 | io_task_runner_, std::move(target), /*launch_elevated=*/true, |
joedow | f84fffae | 2016-12-20 03:35:51 | [diff] [blame] | 657 | base::WideToUTF8(kDaemonIpcSecurityDescriptor))); |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 658 | if (!delegate->Initialize(session_id)) { |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 659 | TerminateSession(); |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 660 | return; |
| 661 | } |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 662 | |
| 663 | // Create a launcher for the desktop process, using the per-session delegate. |
Peter Boström | 560859d | 2021-05-01 01:31:25 | [diff] [blame] | 664 | launcher_ = |
| 665 | std::make_unique<WorkerProcessLauncher>(std::move(delegate), this); |
sammc | f9cd299a | 2016-11-02 22:13:26 | [diff] [blame] | 666 | session_id_ = session_id; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | void DesktopSessionWin::OnSessionDetached() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 670 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 671 | |
| 672 | launcher_.reset(); |
Joe Downing | d4d6f34 | 2021-09-23 22:12:09 | [diff] [blame] | 673 | desktop_session_request_handler_.reset(); |
sammc | f9cd299a | 2016-11-02 22:13:26 | [diff] [blame] | 674 | session_id_ = UINT32_MAX; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 675 | |
| 676 | if (monitoring_notifications_) { |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 677 | ReportElapsedTime("detached"); |
| 678 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 679 | session_attach_timer_.Start( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 680 | FROM_HERE, base::Seconds(kSessionAttachTimeoutSeconds), this, |
| 681 | &DesktopSessionWin::OnSessionAttachTimeout); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 682 | } |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 683 | } |
| 684 | |
Joe Downing | d4d6f34 | 2021-09-23 22:12:09 | [diff] [blame] | 685 | void DesktopSessionWin::ConnectDesktopChannel( |
| 686 | mojo::ScopedMessagePipeHandle desktop_pipe) { |
| 687 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 688 | |
| 689 | if (!daemon_process()->OnDesktopSessionAgentAttached( |
Joe Downing | 185945a | 2022-02-15 18:57:19 | [diff] [blame] | 690 | id(), session_id_, std::move(desktop_pipe))) { |
[email protected] | e9057a6f | 2013-03-08 22:37:55 | [diff] [blame] | 691 | CrashDesktopProcess(FROM_HERE); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
Joe Downing | d4d6f34 | 2021-09-23 22:12:09 | [diff] [blame] | 695 | void DesktopSessionWin::InjectSecureAttentionSequence() { |
| 696 | InjectSas(); |
| 697 | } |
| 698 | |
| 699 | void DesktopSessionWin::CrashNetworkProcess() { |
| 700 | daemon_process()->CrashNetworkProcess(FROM_HERE); |
| 701 | } |
| 702 | |
Brett Wilson | 9c36199 | 2017-09-12 06:05:21 | [diff] [blame] | 703 | void DesktopSessionWin::CrashDesktopProcess(const base::Location& location) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 704 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 705 | |
[email protected] | e9057a6f | 2013-03-08 22:37:55 | [diff] [blame] | 706 | launcher_->Crash(location); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 707 | } |
| 708 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 709 | void DesktopSessionWin::ReportElapsedTime(const std::string& event) { |
| 710 | base::Time now = base::Time::Now(); |
| 711 | |
| 712 | std::string passed; |
| 713 | if (!last_timestamp_.is_null()) { |
| 714 | passed = base::StringPrintf(", %.2fs passed", |
| 715 | (now - last_timestamp_).InSecondsF()); |
| 716 | } |
| 717 | |
| 718 | base::Time::Exploded exploded; |
| 719 | now.LocalExplode(&exploded); |
| 720 | VLOG(1) << base::StringPrintf("session(%d): %s at %02d:%02d:%02d.%03d%s", |
Joe Downing | fd8a422 | 2023-01-11 23:39:40 | [diff] [blame] | 721 | id(), event.c_str(), exploded.hour, |
| 722 | exploded.minute, exploded.second, |
| 723 | exploded.millisecond, passed.c_str()); |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 724 | |
| 725 | last_timestamp_ = now; |
| 726 | } |
| 727 | |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 728 | } // namespace remoting |