Chromium Code Reviews| 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/security_key_auth_handler.h" | 5 #include "remoting/host/security_key/security_key_auth_handler.h" |
| 6 | 6 |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 | 8 |
| 9 #include <cstdint> | 9 #include <cstdint> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 HOST_LOG << "Refused socket connection from uid " << credentials.user_id; | 47 HOST_LOG << "Refused socket connection from uid " << credentials.user_id; |
| 48 return allowed; | 48 return allowed; |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Returns the command code (the first byte of the data) if it exists, or -1 if | 51 // Returns the command code (the first byte of the data) if it exists, or -1 if |
| 52 // the data is empty. | 52 // the data is empty. |
| 53 unsigned int GetCommandCode(const std::string& data) { | 53 unsigned int GetCommandCode(const std::string& data) { |
| 54 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); | 54 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void DeleteSocket(std::unique_ptr<remoting::SecurityKeySocket> socket) {} | |
|
Sergey Ulanov
2016/12/07 20:41:36
base::DeletePointer() can be used instead of this
joedow
2016/12/13 20:30:44
Acknowledged.
| |
| 58 | |
| 57 } // namespace | 59 } // namespace |
| 58 | 60 |
| 59 namespace remoting { | 61 namespace remoting { |
| 60 | 62 |
| 61 class SecurityKeyAuthHandlerPosix : public SecurityKeyAuthHandler { | 63 class SecurityKeyAuthHandlerPosix : public SecurityKeyAuthHandler { |
| 62 public: | 64 public: |
| 63 explicit SecurityKeyAuthHandlerPosix( | 65 explicit SecurityKeyAuthHandlerPosix( |
| 64 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | 66 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); |
| 65 ~SecurityKeyAuthHandlerPosix() override; | 67 ~SecurityKeyAuthHandlerPosix() override; |
| 66 | 68 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 87 void OnAccepted(int result); | 89 void OnAccepted(int result); |
| 88 | 90 |
| 89 // Called when a SecurityKeySocket has done reading. | 91 // Called when a SecurityKeySocket has done reading. |
| 90 void OnReadComplete(int security_key_connection_id); | 92 void OnReadComplete(int security_key_connection_id); |
| 91 | 93 |
| 92 // Gets an active socket iterator for |security_key_connection_id|. | 94 // Gets an active socket iterator for |security_key_connection_id|. |
| 93 ActiveSockets::const_iterator GetSocketForConnectionId( | 95 ActiveSockets::const_iterator GetSocketForConnectionId( |
| 94 int security_key_connection_id) const; | 96 int security_key_connection_id) const; |
| 95 | 97 |
| 96 // Send an error and closes an active socket. | 98 // Send an error and closes an active socket. |
| 97 void SendErrorAndCloseActiveSocket(const ActiveSockets::const_iterator& iter); | 99 void SendErrorAndCloseActiveSocket(const ActiveSockets::iterator& iter); |
| 98 | 100 |
| 99 // A request timed out. | 101 // A request timed out. |
| 100 void RequestTimedOut(int security_key_connection_id); | 102 void RequestTimedOut(int security_key_connection_id); |
| 101 | 103 |
| 102 // Ensures SecurityKeyAuthHandlerPosix methods are called on the same thread. | 104 // Ensures SecurityKeyAuthHandlerPosix methods are called on the same thread. |
| 103 base::ThreadChecker thread_checker_; | 105 base::ThreadChecker thread_checker_; |
| 104 | 106 |
| 105 // Socket used to listen for authorization requests. | 107 // Socket used to listen for authorization requests. |
| 106 std::unique_ptr<net::UnixDomainServerSocket> auth_socket_; | 108 std::unique_ptr<net::UnixDomainServerSocket> auth_socket_; |
| 107 | 109 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 return GetSocketForConnectionId(security_key_connection_id) != | 199 return GetSocketForConnectionId(security_key_connection_id) != |
| 198 active_sockets_.end(); | 200 active_sockets_.end(); |
| 199 } | 201 } |
| 200 | 202 |
| 201 void SecurityKeyAuthHandlerPosix::SendClientResponse( | 203 void SecurityKeyAuthHandlerPosix::SendClientResponse( |
| 202 int security_key_connection_id, | 204 int security_key_connection_id, |
| 203 const std::string& response) { | 205 const std::string& response) { |
| 204 ActiveSockets::const_iterator iter = | 206 ActiveSockets::const_iterator iter = |
| 205 GetSocketForConnectionId(security_key_connection_id); | 207 GetSocketForConnectionId(security_key_connection_id); |
| 206 if (iter != active_sockets_.end()) { | 208 if (iter != active_sockets_.end()) { |
| 207 iter->second->SendResponse(response); | 209 iter->second->SendResponse(response, base::Closure()); |
| 208 } else { | 210 } else { |
| 209 LOG(WARNING) << "Unknown gnubby-auth data connection: '" | 211 LOG(WARNING) << "Unknown gnubby-auth data connection: '" |
| 210 << security_key_connection_id << "'"; | 212 << security_key_connection_id << "'"; |
| 211 } | 213 } |
| 212 } | 214 } |
| 213 | 215 |
| 214 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseConnection( | 216 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseConnection(int id) { |
| 215 int security_key_connection_id) { | 217 ActiveSockets::iterator iter = active_sockets_.find(id); |
| 216 ActiveSockets::const_iterator iter = | |
| 217 GetSocketForConnectionId(security_key_connection_id); | |
| 218 if (iter != active_sockets_.end()) { | 218 if (iter != active_sockets_.end()) { |
| 219 HOST_LOG << "Sending security key error"; | 219 HOST_LOG << "Sending security key error"; |
| 220 SendErrorAndCloseActiveSocket(iter); | 220 SendErrorAndCloseActiveSocket(iter); |
| 221 } else { | 221 } else { |
| 222 LOG(WARNING) << "Unknown gnubby-auth data connection: '" | 222 LOG(WARNING) << "Unknown gnubby-auth data connection: '" << id << "'"; |
| 223 << security_key_connection_id << "'"; | |
| 224 } | 223 } |
| 225 } | 224 } |
| 226 | 225 |
| 227 void SecurityKeyAuthHandlerPosix::SetSendMessageCallback( | 226 void SecurityKeyAuthHandlerPosix::SetSendMessageCallback( |
| 228 const SendMessageCallback& callback) { | 227 const SendMessageCallback& callback) { |
| 229 send_message_callback_ = callback; | 228 send_message_callback_ = callback; |
| 230 } | 229 } |
| 231 | 230 |
| 232 size_t SecurityKeyAuthHandlerPosix::GetActiveConnectionCountForTest() const { | 231 size_t SecurityKeyAuthHandlerPosix::GetActiveConnectionCountForTest() const { |
| 233 return active_sockets_.size(); | 232 return active_sockets_.size(); |
| 234 } | 233 } |
| 235 | 234 |
| 236 void SecurityKeyAuthHandlerPosix::SetRequestTimeoutForTest( | 235 void SecurityKeyAuthHandlerPosix::SetRequestTimeoutForTest( |
| 237 base::TimeDelta timeout) { | 236 base::TimeDelta timeout) { |
| 238 request_timeout_ = timeout; | 237 request_timeout_ = timeout; |
| 239 } | 238 } |
| 240 | 239 |
| 241 void SecurityKeyAuthHandlerPosix::DoAccept() { | 240 void SecurityKeyAuthHandlerPosix::DoAccept() { |
| 242 DCHECK(thread_checker_.CalledOnValidThread()); | 241 DCHECK(thread_checker_.CalledOnValidThread()); |
| 243 int result = auth_socket_->Accept( | 242 int result = auth_socket_->Accept( |
| 244 &accept_socket_, base::Bind(&SecurityKeyAuthHandlerPosix::OnAccepted, | 243 &accept_socket_, base::Bind(&SecurityKeyAuthHandlerPosix::OnAccepted, |
| 245 base::Unretained(this))); | 244 base::Unretained(this))); |
| 246 if (result != net::ERR_IO_PENDING) | 245 if (result != net::ERR_IO_PENDING) { |
| 247 OnAccepted(result); | 246 OnAccepted(result); |
| 247 } | |
| 248 } | 248 } |
| 249 | 249 |
| 250 void SecurityKeyAuthHandlerPosix::OnAccepted(int result) { | 250 void SecurityKeyAuthHandlerPosix::OnAccepted(int result) { |
| 251 DCHECK(thread_checker_.CalledOnValidThread()); | 251 DCHECK(thread_checker_.CalledOnValidThread()); |
| 252 DCHECK_NE(net::ERR_IO_PENDING, result); | 252 DCHECK_NE(net::ERR_IO_PENDING, result); |
| 253 | 253 |
| 254 if (result < 0) { | 254 if (result < 0) { |
| 255 LOG(ERROR) << "Error in accepting a new connection"; | 255 LOG(ERROR) << "Error in accepting a new connection"; |
| 256 return; | 256 return; |
| 257 } | 257 } |
| 258 | 258 |
| 259 int security_key_connection_id = ++last_connection_id_; | 259 int security_key_connection_id = ++last_connection_id_; |
| 260 SecurityKeySocket* socket = new SecurityKeySocket( | 260 SecurityKeySocket* socket = new SecurityKeySocket( |
| 261 std::move(accept_socket_), request_timeout_, | 261 std::move(accept_socket_), request_timeout_, |
| 262 base::Bind(&SecurityKeyAuthHandlerPosix::RequestTimedOut, | 262 base::Bind(&SecurityKeyAuthHandlerPosix::RequestTimedOut, |
| 263 base::Unretained(this), security_key_connection_id)); | 263 base::Unretained(this), security_key_connection_id)); |
| 264 active_sockets_[security_key_connection_id] = base::WrapUnique(socket); | 264 active_sockets_[security_key_connection_id] = base::WrapUnique(socket); |
| 265 socket->StartReadingRequest( | 265 socket->StartReadingRequest( |
| 266 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete, | 266 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete, |
| 267 base::Unretained(this), security_key_connection_id)); | 267 base::Unretained(this), security_key_connection_id)); |
| 268 | 268 |
| 269 // Continue accepting new connections. | 269 // Continue accepting new connections. |
| 270 DoAccept(); | 270 DoAccept(); |
| 271 } | 271 } |
| 272 | 272 |
| 273 void SecurityKeyAuthHandlerPosix::OnReadComplete( | 273 void SecurityKeyAuthHandlerPosix::OnReadComplete(int connection_id) { |
| 274 int security_key_connection_id) { | |
| 275 DCHECK(thread_checker_.CalledOnValidThread()); | 274 DCHECK(thread_checker_.CalledOnValidThread()); |
| 276 | 275 |
| 277 ActiveSockets::const_iterator iter = | 276 ActiveSockets::iterator iter = active_sockets_.find(connection_id); |
| 278 active_sockets_.find(security_key_connection_id); | |
| 279 DCHECK(iter != active_sockets_.end()); | 277 DCHECK(iter != active_sockets_.end()); |
| 280 std::string request_data; | 278 std::string request_data; |
| 281 if (!iter->second->GetAndClearRequestData(&request_data)) { | 279 if (!iter->second->GetAndClearRequestData(&request_data)) { |
| 282 SendErrorAndCloseActiveSocket(iter); | 280 SendErrorAndCloseActiveSocket(iter); |
| 283 return; | 281 return; |
| 284 } | 282 } |
| 285 | 283 |
| 286 HOST_LOG << "Received security key request: " << GetCommandCode(request_data); | 284 HOST_LOG << "Received security key request: " << GetCommandCode(request_data); |
| 287 send_message_callback_.Run(security_key_connection_id, request_data); | 285 send_message_callback_.Run(connection_id, request_data); |
| 288 | 286 |
| 289 iter->second->StartReadingRequest( | 287 iter->second->StartReadingRequest( |
| 290 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete, | 288 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete, |
| 291 base::Unretained(this), security_key_connection_id)); | 289 base::Unretained(this), connection_id)); |
| 292 } | 290 } |
| 293 | 291 |
| 294 SecurityKeyAuthHandlerPosix::ActiveSockets::const_iterator | 292 SecurityKeyAuthHandlerPosix::ActiveSockets::const_iterator |
| 295 SecurityKeyAuthHandlerPosix::GetSocketForConnectionId( | 293 SecurityKeyAuthHandlerPosix::GetSocketForConnectionId( |
| 296 int security_key_connection_id) const { | 294 int security_key_connection_id) const { |
| 297 return active_sockets_.find(security_key_connection_id); | 295 return active_sockets_.find(security_key_connection_id); |
| 298 } | 296 } |
| 299 | 297 |
| 300 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseActiveSocket( | 298 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseActiveSocket( |
| 301 const ActiveSockets::const_iterator& iter) { | 299 const ActiveSockets::iterator& iter) { |
| 302 iter->second->SendSshError(); | 300 iter->second->SendSshError( |
| 301 base::Bind(&DeleteSocket, base::Passed(&iter->second))); | |
|
Sergey Ulanov
2016/12/07 20:41:36
I don't think there is anything that guarantees th
joedow
2016/12/13 20:30:44
That's true. I had a follow-up CL which used this
| |
| 303 active_sockets_.erase(iter); | 302 active_sockets_.erase(iter); |
| 304 } | 303 } |
| 305 | 304 |
| 306 void SecurityKeyAuthHandlerPosix::RequestTimedOut( | 305 void SecurityKeyAuthHandlerPosix::RequestTimedOut(int connection_id) { |
| 307 int security_key_connection_id) { | |
| 308 HOST_LOG << "SecurityKey request timed out"; | 306 HOST_LOG << "SecurityKey request timed out"; |
| 309 ActiveSockets::const_iterator iter = | 307 ActiveSockets::iterator iter = active_sockets_.find(connection_id); |
| 310 active_sockets_.find(security_key_connection_id); | 308 if (iter != active_sockets_.end()) { |
| 311 if (iter != active_sockets_.end()) | |
| 312 SendErrorAndCloseActiveSocket(iter); | 309 SendErrorAndCloseActiveSocket(iter); |
| 310 } | |
| 313 } | 311 } |
| 314 | 312 |
| 315 } // namespace remoting | 313 } // namespace remoting |
| OLD | NEW |