| 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 <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/location.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 13 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 17 #include "base/timer/timer.h" | 20 #include "base/timer/timer.h" |
| 18 #include "base/win/win_util.h" | 21 #include "base/win/win_util.h" |
| 19 #include "ipc/ipc_channel.h" | 22 #include "ipc/ipc_channel.h" |
| 20 #include "ipc/ipc_listener.h" | 23 #include "ipc/ipc_listener.h" |
| 21 #include "ipc/ipc_message.h" | 24 #include "ipc/ipc_message.h" |
| 22 #include "ipc/ipc_message_macros.h" | 25 #include "ipc/ipc_message_macros.h" |
| 23 #include "remoting/base/logging.h" | 26 #include "remoting/base/logging.h" |
| 24 #include "remoting/host/chromoting_messages.h" | 27 #include "remoting/host/chromoting_messages.h" |
| 25 #include "remoting/host/ipc_util.h" | 28 #include "remoting/host/ipc_util.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 typedef std::map<int, std::unique_ptr<RemoteSecurityKeyIpcServer>> | 65 typedef std::map<int, std::unique_ptr<RemoteSecurityKeyIpcServer>> |
| 63 ActiveChannels; | 66 ActiveChannels; |
| 64 | 67 |
| 65 // GnubbyAuthHandler interface. | 68 // GnubbyAuthHandler interface. |
| 66 void CreateGnubbyConnection() override; | 69 void CreateGnubbyConnection() override; |
| 67 bool IsValidConnectionId(int gnubby_connection_id) const override; | 70 bool IsValidConnectionId(int gnubby_connection_id) const override; |
| 68 void SendClientResponse(int gnubby_connection_id, | 71 void SendClientResponse(int gnubby_connection_id, |
| 69 const std::string& response) override; | 72 const std::string& response) override; |
| 70 void SendErrorAndCloseConnection(int gnubby_connection_id) override; | 73 void SendErrorAndCloseConnection(int gnubby_connection_id) override; |
| 71 void SetSendMessageCallback(const SendMessageCallback& callback) override; | 74 void SetSendMessageCallback(const SendMessageCallback& callback) override; |
| 75 void SetSessionIdCallback(const SessionIdCallback& callback) override; |
| 72 size_t GetActiveConnectionCountForTest() const override; | 76 size_t GetActiveConnectionCountForTest() const override; |
| 73 void SetRequestTimeoutForTest(base::TimeDelta timeout) override; | 77 void SetRequestTimeoutForTest(base::TimeDelta timeout) override; |
| 74 | 78 |
| 75 // IPC::Listener implementation. | 79 // IPC::Listener implementation. |
| 76 bool OnMessageReceived(const IPC::Message& message) override; | 80 bool OnMessageReceived(const IPC::Message& message) override; |
| 77 void OnChannelConnected(int32_t peer_pid) override; | 81 void OnChannelConnected(int32_t peer_pid) override; |
| 78 void OnChannelError() override; | 82 void OnChannelError() override; |
| 79 | 83 |
| 80 // Creates the IPC server channel and waits for a connection using | 84 // Creates the IPC server channel and waits for a connection using |
| 81 // |ipc_server_channel_name_|. | 85 // |ipc_server_channel_name_|. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 93 | 97 |
| 94 // Creates a unique name based on the well-known IPC channel name. | 98 // Creates a unique name based on the well-known IPC channel name. |
| 95 std::string GenerateUniqueChannelName(); | 99 std::string GenerateUniqueChannelName(); |
| 96 | 100 |
| 97 // Represents the last id assigned to a new security key request IPC channel. | 101 // Represents the last id assigned to a new security key request IPC channel. |
| 98 int last_connection_id_ = 0; | 102 int last_connection_id_ = 0; |
| 99 | 103 |
| 100 // Sends a gnubby extension messages to the remote client when called. | 104 // Sends a gnubby extension messages to the remote client when called. |
| 101 SendMessageCallback send_message_callback_; | 105 SendMessageCallback send_message_callback_; |
| 102 | 106 |
| 107 // Used to retrieve the id of the remoted session. |
| 108 SessionIdCallback session_id_callback_; |
| 109 |
| 103 // Tracks the IPC channel created for each security key forwarding session. | 110 // Tracks the IPC channel created for each security key forwarding session. |
| 104 ActiveChannels active_channels_; | 111 ActiveChannels active_channels_; |
| 105 | 112 |
| 106 // The amount of time to wait for a client to process the connection details | 113 // 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. | 114 // message and disconnect from the IPC server channel before disconnecting it. |
| 108 base::TimeDelta disconnect_timeout_; | 115 base::TimeDelta disconnect_timeout_; |
| 109 | 116 |
| 110 // Used to recreate the IPC server channel if a client forgets to disconnect. | 117 // Used to recreate the IPC server channel if a client forgets to disconnect. |
| 111 base::OneShotTimer timer_; | 118 base::OneShotTimer timer_; |
| 112 | 119 |
| 113 // IPC Clients connect to this channel first to receive their own IPC | 120 // IPC Clients connect to this channel first to receive their own IPC |
| 114 // channel to start a security key forwarding session on. | 121 // channel to start a security key forwarding session on. |
| 115 std::unique_ptr<IPC::Channel> ipc_server_channel_; | 122 std::unique_ptr<IPC::Channel> ipc_server_channel_; |
| 116 | 123 |
| 117 // Ensures GnubbyAuthHandlerWin methods are called on the same thread. | 124 // Ensures GnubbyAuthHandlerWin methods are called on the same thread. |
| 118 base::ThreadChecker thread_checker_; | 125 base::ThreadChecker thread_checker_; |
| 119 | 126 |
| 127 base::WeakPtrFactory<GnubbyAuthHandlerWin> weak_factory_; |
| 128 |
| 120 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerWin); | 129 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerWin); |
| 121 }; | 130 }; |
| 122 | 131 |
| 123 std::unique_ptr<GnubbyAuthHandler> GnubbyAuthHandler::Create( | 132 std::unique_ptr<GnubbyAuthHandler> GnubbyAuthHandler::Create( |
| 124 const SendMessageCallback& callback) { | 133 const SendMessageCallback& send_message_callback, |
| 134 const SessionIdCallback& session_id_callback) { |
| 125 std::unique_ptr<GnubbyAuthHandler> auth_handler(new GnubbyAuthHandlerWin()); | 135 std::unique_ptr<GnubbyAuthHandler> auth_handler(new GnubbyAuthHandlerWin()); |
| 126 auth_handler->SetSendMessageCallback(callback); | 136 auth_handler->SetSendMessageCallback(send_message_callback); |
| 137 auth_handler->SetSessionIdCallback(session_id_callback); |
| 127 return auth_handler; | 138 return auth_handler; |
| 128 } | 139 } |
| 129 | 140 |
| 130 GnubbyAuthHandlerWin::GnubbyAuthHandlerWin() | 141 GnubbyAuthHandlerWin::GnubbyAuthHandlerWin() |
| 131 : disconnect_timeout_( | 142 : disconnect_timeout_( |
| 132 base::TimeDelta::FromSeconds(kInitialRequestTimeoutSeconds)) {} | 143 base::TimeDelta::FromSeconds(kInitialRequestTimeoutSeconds)), |
| 144 weak_factory_(this) {} |
| 133 | 145 |
| 134 GnubbyAuthHandlerWin::~GnubbyAuthHandlerWin() {} | 146 GnubbyAuthHandlerWin::~GnubbyAuthHandlerWin() {} |
| 135 | 147 |
| 136 void GnubbyAuthHandlerWin::CreateGnubbyConnection() { | 148 void GnubbyAuthHandlerWin::CreateGnubbyConnection() { |
| 137 DCHECK(thread_checker_.CalledOnValidThread()); | 149 DCHECK(thread_checker_.CalledOnValidThread()); |
| 138 StartIpcServerChannel(); | 150 StartIpcServerChannel(); |
| 139 } | 151 } |
| 140 | 152 |
| 141 bool GnubbyAuthHandlerWin::IsValidConnectionId(int connection_id) const { | 153 bool GnubbyAuthHandlerWin::IsValidConnectionId(int connection_id) const { |
| 142 DCHECK(thread_checker_.CalledOnValidThread()); | 154 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 166 SendClientResponse(connection_id, kRemoteSecurityKeyConnectionError); | 178 SendClientResponse(connection_id, kRemoteSecurityKeyConnectionError); |
| 167 CloseSecurityKeyRequestIpcChannel(connection_id); | 179 CloseSecurityKeyRequestIpcChannel(connection_id); |
| 168 } | 180 } |
| 169 | 181 |
| 170 void GnubbyAuthHandlerWin::SetSendMessageCallback( | 182 void GnubbyAuthHandlerWin::SetSendMessageCallback( |
| 171 const SendMessageCallback& callback) { | 183 const SendMessageCallback& callback) { |
| 172 DCHECK(thread_checker_.CalledOnValidThread()); | 184 DCHECK(thread_checker_.CalledOnValidThread()); |
| 173 send_message_callback_ = callback; | 185 send_message_callback_ = callback; |
| 174 } | 186 } |
| 175 | 187 |
| 188 void GnubbyAuthHandlerWin::SetSessionIdCallback( |
| 189 const SessionIdCallback& callback) { |
| 190 DCHECK(thread_checker_.CalledOnValidThread()); |
| 191 session_id_callback_ = callback; |
| 192 } |
| 193 |
| 176 size_t GnubbyAuthHandlerWin::GetActiveConnectionCountForTest() const { | 194 size_t GnubbyAuthHandlerWin::GetActiveConnectionCountForTest() const { |
| 177 return active_channels_.size(); | 195 return active_channels_.size(); |
| 178 } | 196 } |
| 179 | 197 |
| 180 void GnubbyAuthHandlerWin::SetRequestTimeoutForTest(base::TimeDelta timeout) { | 198 void GnubbyAuthHandlerWin::SetRequestTimeoutForTest(base::TimeDelta timeout) { |
| 181 disconnect_timeout_ = timeout; | 199 disconnect_timeout_ = timeout; |
| 182 } | 200 } |
| 183 | 201 |
| 184 void GnubbyAuthHandlerWin::StartIpcServerChannel() { | 202 void GnubbyAuthHandlerWin::StartIpcServerChannel() { |
| 185 DCHECK(thread_checker_.CalledOnValidThread()); | 203 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 return false; | 250 return false; |
| 233 } | 251 } |
| 234 | 252 |
| 235 void GnubbyAuthHandlerWin::OnChannelConnected(int32_t peer_pid) { | 253 void GnubbyAuthHandlerWin::OnChannelConnected(int32_t peer_pid) { |
| 236 DCHECK(thread_checker_.CalledOnValidThread()); | 254 DCHECK(thread_checker_.CalledOnValidThread()); |
| 237 | 255 |
| 238 timer_.Start(FROM_HERE, disconnect_timeout_, | 256 timer_.Start(FROM_HERE, disconnect_timeout_, |
| 239 base::Bind(&GnubbyAuthHandlerWin::OnChannelError, | 257 base::Bind(&GnubbyAuthHandlerWin::OnChannelError, |
| 240 base::Unretained(this))); | 258 base::Unretained(this))); |
| 241 | 259 |
| 242 // TODO(joedow): Use |peer_pid| to determine the originating session | 260 // Verify the IPC connection attempt originated from the session we are |
| 243 // using ProcessIdToSessionId() and verify it is the one we created. | 261 // currently remoting. We don't want to service requests from arbitrary |
| 244 // Tracked via crbug.com/591746 | 262 // Windows sessions. |
| 263 DWORD peer_session_id; |
| 264 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) { |
| 265 PLOG(ERROR) << "ProcessIdToSessionId() failed"; |
| 266 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 267 FROM_HERE, base::Bind(&GnubbyAuthHandlerWin::OnChannelError, |
| 268 weak_factory_.GetWeakPtr())); |
| 269 return; |
| 270 } |
| 271 if (peer_session_id != session_id_callback_.Run()) { |
| 272 LOG(INFO) << "Ignoring connection attempt from outside remoted session."; |
| 273 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 274 FROM_HERE, base::Bind(&GnubbyAuthHandlerWin::OnChannelError, |
| 275 weak_factory_.GetWeakPtr())); |
| 276 return; |
| 277 } |
| 245 | 278 |
| 246 int new_connection_id = ++last_connection_id_; | 279 int new_connection_id = ++last_connection_id_; |
| 247 std::unique_ptr<RemoteSecurityKeyIpcServer> ipc_server( | 280 std::unique_ptr<RemoteSecurityKeyIpcServer> ipc_server( |
| 248 RemoteSecurityKeyIpcServer::Create( | 281 RemoteSecurityKeyIpcServer::Create( |
| 249 new_connection_id, disconnect_timeout_, send_message_callback_, | 282 new_connection_id, peer_session_id, disconnect_timeout_, |
| 283 send_message_callback_, |
| 250 base::Bind(&GnubbyAuthHandlerWin::CloseSecurityKeyRequestIpcChannel, | 284 base::Bind(&GnubbyAuthHandlerWin::CloseSecurityKeyRequestIpcChannel, |
| 251 base::Unretained(this), new_connection_id))); | 285 base::Unretained(this), new_connection_id))); |
| 252 | 286 |
| 253 std::string unique_channel_name = GenerateUniqueChannelName(); | 287 std::string unique_channel_name = GenerateUniqueChannelName(); |
| 254 if (ipc_server->CreateChannel( | 288 if (ipc_server->CreateChannel( |
| 255 unique_channel_name, | 289 unique_channel_name, |
| 256 base::TimeDelta::FromSeconds(kGnubbyRequestTimeoutSeconds))) { | 290 base::TimeDelta::FromSeconds(kGnubbyRequestTimeoutSeconds))) { |
| 257 active_channels_[new_connection_id] = std::move(ipc_server); | 291 active_channels_[new_connection_id] = std::move(ipc_server); |
| 258 ipc_server_channel_->Send( | 292 ipc_server_channel_->Send( |
| 259 new ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails( | 293 new ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails( |
| 260 unique_channel_name)); | 294 unique_channel_name)); |
| 261 } | 295 } |
| 262 } | 296 } |
| 263 | 297 |
| 264 void GnubbyAuthHandlerWin::OnChannelError() { | 298 void GnubbyAuthHandlerWin::OnChannelError() { |
| 265 DCHECK(thread_checker_.CalledOnValidThread()); | 299 DCHECK(thread_checker_.CalledOnValidThread()); |
| 266 | 300 |
| 267 // Could be an error, most likely the client disconnected though. Either way | 301 // Could be an error, most likely the client disconnected though. Either way |
| 268 // we should restart the server to prepare for the next connection. | 302 // we should restart the server to prepare for the next connection. |
| 269 RecreateIpcServerChannel(); | 303 RecreateIpcServerChannel(); |
| 270 } | 304 } |
| 271 | 305 |
| 272 } // namespace remoting | 306 } // namespace remoting |
| OLD | NEW |