OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/security_key/gnubby_auth_handler.h" | 5 #include "remoting/host/security_key/gnubby_auth_handler.h" |
6 | 6 |
7 #include <cstdint> | |
7 #include <map> | 8 #include <map> |
8 #include <memory> | 9 #include <memory> |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/location.h" | |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/weak_ptr.h" | |
13 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
15 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
19 #include "base/threading/thread_task_runner_handle.h" | |
16 #include "base/time/time.h" | 20 #include "base/time/time.h" |
17 #include "base/timer/timer.h" | 21 #include "base/timer/timer.h" |
18 #include "base/win/win_util.h" | 22 #include "base/win/win_util.h" |
19 #include "ipc/ipc_channel.h" | 23 #include "ipc/ipc_channel.h" |
20 #include "ipc/ipc_listener.h" | 24 #include "ipc/ipc_listener.h" |
21 #include "ipc/ipc_message.h" | 25 #include "ipc/ipc_message.h" |
22 #include "ipc/ipc_message_macros.h" | 26 #include "ipc/ipc_message_macros.h" |
23 #include "remoting/base/logging.h" | 27 #include "remoting/base/logging.h" |
24 #include "remoting/host/chromoting_messages.h" | 28 #include "remoting/host/chromoting_messages.h" |
29 #include "remoting/host/client_session_details.h" | |
25 #include "remoting/host/ipc_util.h" | 30 #include "remoting/host/ipc_util.h" |
26 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" | 31 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" |
27 #include "remoting/host/security_key/remote_security_key_ipc_server.h" | 32 #include "remoting/host/security_key/remote_security_key_ipc_server.h" |
28 | 33 |
29 namespace { | 34 namespace { |
30 | 35 |
31 // The timeout used to disconnect a client from the IPC Server channel if it | 36 // The timeout used to disconnect a client from the IPC Server channel if it |
32 // forgets to do so. This ensures the server channel is not blocked forever. | 37 // forgets to do so. This ensures the server channel is not blocked forever. |
33 const int kInitialRequestTimeoutSeconds = 5; | 38 const int kInitialRequestTimeoutSeconds = 5; |
34 | 39 |
(...skipping 13 matching lines...) Expand all Loading... | |
48 // assigned a unique IPC channel name and connection id. The channel name is | 53 // assigned a unique IPC channel name and connection id. The channel name is |
49 // sent to the client which should disconnect the IPC server channel and | 54 // sent to the client which should disconnect the IPC server channel and |
50 // connect to the security key forwarding session IPC channel to send/receive | 55 // connect to the security key forwarding session IPC channel to send/receive |
51 // security key messages. The IPC server channel will then be reset so it can | 56 // security key messages. The IPC server channel will then be reset so it can |
52 // can service the next client/request. This system allows multiple security | 57 // can service the next client/request. This system allows multiple security |
53 // key forwarding sessions to occur concurrently. | 58 // key forwarding sessions to occur concurrently. |
54 // TODO(joedow): Update GnubbyAuthHandler impls to run on a separate IO thread | 59 // TODO(joedow): Update GnubbyAuthHandler impls to run on a separate IO thread |
55 // instead of the thread it was created on: crbug.com/591739 | 60 // instead of the thread it was created on: crbug.com/591739 |
56 class GnubbyAuthHandlerWin : public GnubbyAuthHandler, public IPC::Listener { | 61 class GnubbyAuthHandlerWin : public GnubbyAuthHandler, public IPC::Listener { |
57 public: | 62 public: |
58 GnubbyAuthHandlerWin(); | 63 explicit GnubbyAuthHandlerWin(ClientSessionDetails* client_session_details); |
59 ~GnubbyAuthHandlerWin() override; | 64 ~GnubbyAuthHandlerWin() override; |
60 | 65 |
61 private: | 66 private: |
62 typedef std::map<int, std::unique_ptr<RemoteSecurityKeyIpcServer>> | 67 typedef std::map<int, std::unique_ptr<RemoteSecurityKeyIpcServer>> |
63 ActiveChannels; | 68 ActiveChannels; |
64 | 69 |
65 // GnubbyAuthHandler interface. | 70 // GnubbyAuthHandler interface. |
66 void CreateGnubbyConnection() override; | 71 void CreateGnubbyConnection() override; |
67 bool IsValidConnectionId(int gnubby_connection_id) const override; | 72 bool IsValidConnectionId(int gnubby_connection_id) const override; |
68 void SendClientResponse(int gnubby_connection_id, | 73 void SendClientResponse(int gnubby_connection_id, |
69 const std::string& response) override; | 74 const std::string& response) override; |
70 void SendErrorAndCloseConnection(int gnubby_connection_id) override; | 75 void SendErrorAndCloseConnection(int gnubby_connection_id) override; |
71 void SetSendMessageCallback(const SendMessageCallback& callback) override; | 76 void SetSendMessageCallback(const SendMessageCallback& callback) override; |
72 size_t GetActiveConnectionCountForTest() const override; | 77 size_t GetActiveConnectionCountForTest() const override; |
73 void SetRequestTimeoutForTest(base::TimeDelta timeout) override; | 78 void SetRequestTimeoutForTest(base::TimeDelta timeout) override; |
74 | 79 |
75 // IPC::Listener implementation. | 80 // IPC::Listener implementation. |
76 bool OnMessageReceived(const IPC::Message& message) override; | 81 bool OnMessageReceived(const IPC::Message& message) override; |
77 void OnChannelConnected(int32_t peer_pid) override; | 82 void OnChannelConnected(int32_t peer_pid) override; |
78 void OnChannelError() override; | 83 void OnChannelError() override; |
79 | 84 |
85 uint32_t GetDesktopSessionId() const; | |
86 | |
80 // Creates the IPC server channel and waits for a connection using | 87 // Creates the IPC server channel and waits for a connection using |
81 // |ipc_server_channel_name_|. | 88 // |ipc_server_channel_name_|. |
82 void StartIpcServerChannel(); | 89 void StartIpcServerChannel(); |
83 | 90 |
84 // Restarts the IPC server channel to prepare for another connection. | 91 // Restarts the IPC server channel to prepare for another connection. |
85 void RecreateIpcServerChannel(); | 92 void RecreateIpcServerChannel(); |
86 | 93 |
87 // Closes the IPC channel created for a security key forwarding session. | 94 // Closes the IPC channel created for a security key forwarding session. |
88 void CloseSecurityKeyRequestIpcChannel(int connection_id); | 95 void CloseSecurityKeyRequestIpcChannel(int connection_id); |
89 | 96 |
90 // Returns the IPC Channel instance created for |connection_id|. | 97 // Returns the IPC Channel instance created for |connection_id|. |
91 ActiveChannels::const_iterator GetChannelForConnectionId( | 98 ActiveChannels::const_iterator GetChannelForConnectionId( |
92 int connection_id) const; | 99 int connection_id) const; |
93 | 100 |
94 // Creates a unique name based on the well-known IPC channel name. | 101 // Creates a unique name based on the well-known IPC channel name. |
95 std::string GenerateUniqueChannelName(); | 102 std::string GenerateUniqueChannelName(); |
96 | 103 |
97 // Represents the last id assigned to a new security key request IPC channel. | 104 // Represents the last id assigned to a new security key request IPC channel. |
98 int last_connection_id_ = 0; | 105 int last_connection_id_ = 0; |
99 | 106 |
100 // Sends a gnubby extension messages to the remote client when called. | 107 // Sends a gnubby extension messages to the remote client when called. |
101 SendMessageCallback send_message_callback_; | 108 SendMessageCallback send_message_callback_; |
102 | 109 |
110 // Interface which provides details about the client session. | |
111 ClientSessionDetails* client_session_details_ = nullptr; | |
112 | |
103 // Tracks the IPC channel created for each security key forwarding session. | 113 // Tracks the IPC channel created for each security key forwarding session. |
104 ActiveChannels active_channels_; | 114 ActiveChannels active_channels_; |
105 | 115 |
106 // The amount of time to wait for a client to process the connection details | 116 // The amount of time to wait for a client to process the connection details |
107 // message and disconnect from the IPC server channel before disconnecting it. | 117 // message and disconnect from the IPC server channel before disconnecting it. |
108 base::TimeDelta disconnect_timeout_; | 118 base::TimeDelta disconnect_timeout_; |
109 | 119 |
110 // Used to recreate the IPC server channel if a client forgets to disconnect. | 120 // Used to recreate the IPC server channel if a client forgets to disconnect. |
111 base::OneShotTimer timer_; | 121 base::OneShotTimer timer_; |
112 | 122 |
113 // IPC Clients connect to this channel first to receive their own IPC | 123 // IPC Clients connect to this channel first to receive their own IPC |
114 // channel to start a security key forwarding session on. | 124 // channel to start a security key forwarding session on. |
115 std::unique_ptr<IPC::Channel> ipc_server_channel_; | 125 std::unique_ptr<IPC::Channel> ipc_server_channel_; |
116 | 126 |
117 // Ensures GnubbyAuthHandlerWin methods are called on the same thread. | 127 // Ensures GnubbyAuthHandlerWin methods are called on the same thread. |
118 base::ThreadChecker thread_checker_; | 128 base::ThreadChecker thread_checker_; |
119 | 129 |
130 base::WeakPtrFactory<GnubbyAuthHandlerWin> weak_factory_; | |
131 | |
120 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerWin); | 132 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerWin); |
121 }; | 133 }; |
122 | 134 |
123 std::unique_ptr<GnubbyAuthHandler> GnubbyAuthHandler::Create( | 135 std::unique_ptr<GnubbyAuthHandler> GnubbyAuthHandler::Create( |
124 const SendMessageCallback& callback) { | 136 ClientSessionDetails* client_session_details, |
125 std::unique_ptr<GnubbyAuthHandler> auth_handler(new GnubbyAuthHandlerWin()); | 137 const SendMessageCallback& send_message_callback) { |
126 auth_handler->SetSendMessageCallback(callback); | 138 std::unique_ptr<GnubbyAuthHandler> auth_handler( |
139 new GnubbyAuthHandlerWin(client_session_details)); | |
140 auth_handler->SetSendMessageCallback(send_message_callback); | |
127 return auth_handler; | 141 return auth_handler; |
128 } | 142 } |
129 | 143 |
130 GnubbyAuthHandlerWin::GnubbyAuthHandlerWin() | 144 GnubbyAuthHandlerWin::GnubbyAuthHandlerWin( |
131 : disconnect_timeout_( | 145 ClientSessionDetails* client_session_details) |
132 base::TimeDelta::FromSeconds(kInitialRequestTimeoutSeconds)) {} | 146 : client_session_details_(client_session_details), |
147 disconnect_timeout_( | |
148 base::TimeDelta::FromSeconds(kInitialRequestTimeoutSeconds)), | |
149 weak_factory_(this) { | |
150 DCHECK(client_session_details_); | |
151 } | |
133 | 152 |
134 GnubbyAuthHandlerWin::~GnubbyAuthHandlerWin() {} | 153 GnubbyAuthHandlerWin::~GnubbyAuthHandlerWin() {} |
135 | 154 |
136 void GnubbyAuthHandlerWin::CreateGnubbyConnection() { | 155 void GnubbyAuthHandlerWin::CreateGnubbyConnection() { |
137 DCHECK(thread_checker_.CalledOnValidThread()); | 156 DCHECK(thread_checker_.CalledOnValidThread()); |
138 StartIpcServerChannel(); | 157 StartIpcServerChannel(); |
139 } | 158 } |
140 | 159 |
141 bool GnubbyAuthHandlerWin::IsValidConnectionId(int connection_id) const { | 160 bool GnubbyAuthHandlerWin::IsValidConnectionId(int connection_id) const { |
142 DCHECK(thread_checker_.CalledOnValidThread()); | 161 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 return false; | 251 return false; |
233 } | 252 } |
234 | 253 |
235 void GnubbyAuthHandlerWin::OnChannelConnected(int32_t peer_pid) { | 254 void GnubbyAuthHandlerWin::OnChannelConnected(int32_t peer_pid) { |
236 DCHECK(thread_checker_.CalledOnValidThread()); | 255 DCHECK(thread_checker_.CalledOnValidThread()); |
237 | 256 |
238 timer_.Start(FROM_HERE, disconnect_timeout_, | 257 timer_.Start(FROM_HERE, disconnect_timeout_, |
239 base::Bind(&GnubbyAuthHandlerWin::OnChannelError, | 258 base::Bind(&GnubbyAuthHandlerWin::OnChannelError, |
240 base::Unretained(this))); | 259 base::Unretained(this))); |
241 | 260 |
242 // TODO(joedow): Use |peer_pid| to determine the originating session | 261 // Verify the IPC connection attempt originated from the session we are |
243 // using ProcessIdToSessionId() and verify it is the one we created. | 262 // currently remoting. We don't want to service requests from arbitrary |
244 // Tracked via crbug.com/591746 | 263 // Windows sessions. |
264 DWORD peer_session_id; | |
265 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) { | |
266 PLOG(ERROR) << "ProcessIdToSessionId() failed"; | |
267 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
268 FROM_HERE, base::Bind(&GnubbyAuthHandlerWin::OnChannelError, | |
269 weak_factory_.GetWeakPtr())); | |
270 return; | |
271 } | |
272 if (peer_session_id != GetDesktopSessionId()) { | |
273 LOG(INFO) << "Ignoring connection attempt from outside remoted session."; | |
274 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
275 FROM_HERE, base::Bind(&GnubbyAuthHandlerWin::OnChannelError, | |
276 weak_factory_.GetWeakPtr())); | |
277 return; | |
278 } | |
245 | 279 |
246 int new_connection_id = ++last_connection_id_; | 280 int new_connection_id = ++last_connection_id_; |
247 std::unique_ptr<RemoteSecurityKeyIpcServer> ipc_server( | 281 std::unique_ptr<RemoteSecurityKeyIpcServer> ipc_server( |
248 RemoteSecurityKeyIpcServer::Create( | 282 RemoteSecurityKeyIpcServer::Create( |
249 new_connection_id, disconnect_timeout_, send_message_callback_, | 283 new_connection_id, peer_session_id, disconnect_timeout_, |
284 send_message_callback_, | |
250 base::Bind(&GnubbyAuthHandlerWin::CloseSecurityKeyRequestIpcChannel, | 285 base::Bind(&GnubbyAuthHandlerWin::CloseSecurityKeyRequestIpcChannel, |
251 base::Unretained(this), new_connection_id))); | 286 base::Unretained(this), new_connection_id))); |
252 | 287 |
253 std::string unique_channel_name = GenerateUniqueChannelName(); | 288 std::string unique_channel_name = GenerateUniqueChannelName(); |
254 if (ipc_server->CreateChannel( | 289 if (ipc_server->CreateChannel( |
255 unique_channel_name, | 290 unique_channel_name, |
256 base::TimeDelta::FromSeconds(kGnubbyRequestTimeoutSeconds))) { | 291 base::TimeDelta::FromSeconds(kGnubbyRequestTimeoutSeconds))) { |
257 active_channels_[new_connection_id] = std::move(ipc_server); | 292 active_channels_[new_connection_id] = std::move(ipc_server); |
258 ipc_server_channel_->Send( | 293 ipc_server_channel_->Send( |
259 new ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails( | 294 new ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails( |
260 unique_channel_name)); | 295 unique_channel_name)); |
261 } | 296 } |
262 } | 297 } |
263 | 298 |
264 void GnubbyAuthHandlerWin::OnChannelError() { | 299 void GnubbyAuthHandlerWin::OnChannelError() { |
265 DCHECK(thread_checker_.CalledOnValidThread()); | 300 DCHECK(thread_checker_.CalledOnValidThread()); |
266 | 301 |
267 // Could be an error, most likely the client disconnected though. Either way | 302 // Could be an error, most likely the client disconnected though. Either way |
268 // we should restart the server to prepare for the next connection. | 303 // we should restart the server to prepare for the next connection. |
269 RecreateIpcServerChannel(); | 304 RecreateIpcServerChannel(); |
270 } | 305 } |
271 | 306 |
307 uint32_t GnubbyAuthHandlerWin::GetDesktopSessionId() const { | |
Sergey Ulanov
2016/06/29 22:36:46
Do you really need this method? It's only one line
joedow
2016/06/30 00:10:08
Done.
| |
308 return client_session_details_->desktop_session_id(); | |
309 } | |
310 | |
272 } // namespace remoting | 311 } // namespace remoting |
OLD | NEW |