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 25 matching lines...) Expand all Loading... | |
| 36 const int64_t kDefaultRequestTimeoutSeconds = 60; | 36 const int64_t kDefaultRequestTimeoutSeconds = 60; |
| 37 | 37 |
| 38 // The name of the socket to listen for security key requests on. | 38 // The name of the socket to listen for security key requests on. |
| 39 base::LazyInstance<base::FilePath>::Leaky g_security_key_socket_name = | 39 base::LazyInstance<base::FilePath>::Leaky g_security_key_socket_name = |
| 40 LAZY_INSTANCE_INITIALIZER; | 40 LAZY_INSTANCE_INITIALIZER; |
| 41 | 41 |
| 42 // Socket authentication function that only allows connections from callers with | 42 // Socket authentication function that only allows connections from callers with |
| 43 // the current uid. | 43 // the current uid. |
| 44 bool MatchUid(const net::UnixDomainServerSocket::Credentials& credentials) { | 44 bool MatchUid(const net::UnixDomainServerSocket::Credentials& credentials) { |
| 45 bool allowed = credentials.user_id == getuid(); | 45 bool allowed = credentials.user_id == getuid(); |
| 46 if (!allowed) | 46 if (!allowed) { |
| 47 HOST_LOG << "Refused socket connection from uid " << credentials.user_id; | 47 HOST_LOG << "Refused socket connection from uid " << credentials.user_id; |
| 48 } | |
| 48 return allowed; | 49 return allowed; |
| 49 } | 50 } |
| 50 | 51 |
| 51 // Returns the command code (the first byte of the data) if it exists, or -1 if | 52 // Returns the command code (the first byte of the data) if it exists, or -1 if |
| 52 // the data is empty. | 53 // the data is empty. |
| 53 unsigned int GetCommandCode(const std::string& data) { | 54 unsigned int GetCommandCode(const std::string& data) { |
| 54 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); | 55 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); |
| 55 } | 56 } |
| 56 | 57 |
| 57 } // namespace | 58 } // namespace |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 | 195 |
| 195 bool SecurityKeyAuthHandlerPosix::IsValidConnectionId(int connection_id) const { | 196 bool SecurityKeyAuthHandlerPosix::IsValidConnectionId(int connection_id) const { |
| 196 return GetSocketForConnectionId(connection_id) != active_sockets_.end(); | 197 return GetSocketForConnectionId(connection_id) != active_sockets_.end(); |
| 197 } | 198 } |
| 198 | 199 |
| 199 void SecurityKeyAuthHandlerPosix::SendClientResponse( | 200 void SecurityKeyAuthHandlerPosix::SendClientResponse( |
| 200 int connection_id, | 201 int connection_id, |
| 201 const std::string& response) { | 202 const std::string& response) { |
| 202 ActiveSockets::const_iterator iter = GetSocketForConnectionId(connection_id); | 203 ActiveSockets::const_iterator iter = GetSocketForConnectionId(connection_id); |
| 203 if (iter != active_sockets_.end()) { | 204 if (iter != active_sockets_.end()) { |
| 205 HOST_LOG << "Sending client response to socket: " << connection_id; | |
|
Sergey Ulanov
2016/12/19 23:19:18
Do we really need to log for every Gnubby connecti
joedow
2016/12/20 00:28:00
There shouldn't be too much debug spew but I'll up
| |
| 204 iter->second->SendResponse(response); | 206 iter->second->SendResponse(response); |
| 207 iter->second->StartReadingRequest( | |
| 208 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete, | |
| 209 base::Unretained(this), connection_id)); | |
| 205 } else { | 210 } else { |
| 206 LOG(WARNING) << "Unknown gnubby-auth connection id: " << connection_id; | 211 LOG(WARNING) << "Unknown gnubby-auth connection id: " << connection_id; |
| 207 } | 212 } |
| 208 } | 213 } |
| 209 | 214 |
| 210 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseConnection(int id) { | 215 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseConnection(int id) { |
| 211 ActiveSockets::const_iterator iter = GetSocketForConnectionId(id); | 216 ActiveSockets::const_iterator iter = GetSocketForConnectionId(id); |
| 212 if (iter != active_sockets_.end()) { | 217 if (iter != active_sockets_.end()) { |
| 213 HOST_LOG << "Sending security key error"; | 218 HOST_LOG << "Sending error and closing socket: " << id; |
| 214 SendErrorAndCloseActiveSocket(iter); | 219 SendErrorAndCloseActiveSocket(iter); |
| 215 } else { | 220 } else { |
| 216 LOG(WARNING) << "Unknown gnubby-auth connection id: " << id; | 221 LOG(WARNING) << "Unknown gnubby-auth connection id: " << id; |
| 217 } | 222 } |
| 218 } | 223 } |
| 219 | 224 |
| 220 void SecurityKeyAuthHandlerPosix::SetSendMessageCallback( | 225 void SecurityKeyAuthHandlerPosix::SetSendMessageCallback( |
| 221 const SendMessageCallback& callback) { | 226 const SendMessageCallback& callback) { |
| 222 send_message_callback_ = callback; | 227 send_message_callback_ = callback; |
| 223 } | 228 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 239 if (result != net::ERR_IO_PENDING) { | 244 if (result != net::ERR_IO_PENDING) { |
| 240 OnAccepted(result); | 245 OnAccepted(result); |
| 241 } | 246 } |
| 242 } | 247 } |
| 243 | 248 |
| 244 void SecurityKeyAuthHandlerPosix::OnAccepted(int result) { | 249 void SecurityKeyAuthHandlerPosix::OnAccepted(int result) { |
| 245 DCHECK(thread_checker_.CalledOnValidThread()); | 250 DCHECK(thread_checker_.CalledOnValidThread()); |
| 246 DCHECK_NE(net::ERR_IO_PENDING, result); | 251 DCHECK_NE(net::ERR_IO_PENDING, result); |
| 247 | 252 |
| 248 if (result < 0) { | 253 if (result < 0) { |
| 249 LOG(ERROR) << "Error in accepting a new connection"; | 254 LOG(ERROR) << "Error accepting new socket connection: " << result; |
| 250 return; | 255 return; |
| 251 } | 256 } |
| 252 | 257 |
| 253 int security_key_connection_id = ++last_connection_id_; | 258 int security_key_connection_id = ++last_connection_id_; |
| 259 HOST_LOG << "Creating new socket: " << security_key_connection_id; | |
| 254 SecurityKeySocket* socket = new SecurityKeySocket( | 260 SecurityKeySocket* socket = new SecurityKeySocket( |
| 255 std::move(accept_socket_), request_timeout_, | 261 std::move(accept_socket_), request_timeout_, |
| 256 base::Bind(&SecurityKeyAuthHandlerPosix::RequestTimedOut, | 262 base::Bind(&SecurityKeyAuthHandlerPosix::RequestTimedOut, |
| 257 base::Unretained(this), security_key_connection_id)); | 263 base::Unretained(this), security_key_connection_id)); |
| 258 active_sockets_[security_key_connection_id] = base::WrapUnique(socket); | 264 active_sockets_[security_key_connection_id] = base::WrapUnique(socket); |
| 259 socket->StartReadingRequest( | 265 socket->StartReadingRequest( |
| 260 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete, | 266 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete, |
| 261 base::Unretained(this), security_key_connection_id)); | 267 base::Unretained(this), security_key_connection_id)); |
| 262 | 268 |
| 263 // Continue accepting new connections. | 269 // Continue accepting new connections. |
| 264 DoAccept(); | 270 DoAccept(); |
| 265 } | 271 } |
| 266 | 272 |
| 267 void SecurityKeyAuthHandlerPosix::OnReadComplete(int connection_id) { | 273 void SecurityKeyAuthHandlerPosix::OnReadComplete(int connection_id) { |
| 268 DCHECK(thread_checker_.CalledOnValidThread()); | 274 DCHECK(thread_checker_.CalledOnValidThread()); |
| 269 | 275 |
| 270 ActiveSockets::const_iterator iter = active_sockets_.find(connection_id); | 276 ActiveSockets::const_iterator iter = active_sockets_.find(connection_id); |
| 271 DCHECK(iter != active_sockets_.end()); | 277 DCHECK(iter != active_sockets_.end()); |
| 272 std::string request_data; | 278 std::string request_data; |
| 273 if (!iter->second->GetAndClearRequestData(&request_data)) { | 279 if (!iter->second->GetAndClearRequestData(&request_data)) { |
| 274 SendErrorAndCloseActiveSocket(iter); | 280 HOST_LOG << "Closing socket: " << connection_id; |
|
Sergey Ulanov
2016/12/19 23:19:18
Log only in case of an error?
joedow
2016/12/20 00:28:00
Updating to DLOG
| |
| 281 if (iter->second->socket_read_error()) { | |
| 282 iter->second->SendSshError(); | |
| 283 } | |
| 284 active_sockets_.erase(iter); | |
| 275 return; | 285 return; |
| 276 } | 286 } |
| 277 | 287 |
| 278 HOST_LOG << "Received security key request: " << GetCommandCode(request_data); | 288 HOST_LOG << "Received request from socket: " << connection_id |
| 289 << ", code: " << GetCommandCode(request_data); | |
| 279 send_message_callback_.Run(connection_id, request_data); | 290 send_message_callback_.Run(connection_id, request_data); |
| 280 | |
| 281 iter->second->StartReadingRequest( | |
| 282 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete, | |
| 283 base::Unretained(this), connection_id)); | |
| 284 } | 291 } |
| 285 | 292 |
| 286 SecurityKeyAuthHandlerPosix::ActiveSockets::const_iterator | 293 SecurityKeyAuthHandlerPosix::ActiveSockets::const_iterator |
| 287 SecurityKeyAuthHandlerPosix::GetSocketForConnectionId(int connection_id) const { | 294 SecurityKeyAuthHandlerPosix::GetSocketForConnectionId(int connection_id) const { |
| 288 return active_sockets_.find(connection_id); | 295 return active_sockets_.find(connection_id); |
| 289 } | 296 } |
| 290 | 297 |
| 291 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseActiveSocket( | 298 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseActiveSocket( |
| 292 const ActiveSockets::const_iterator& iter) { | 299 const ActiveSockets::const_iterator& iter) { |
| 293 iter->second->SendSshError(); | 300 iter->second->SendSshError(); |
| 294 active_sockets_.erase(iter); | 301 active_sockets_.erase(iter); |
| 295 } | 302 } |
| 296 | 303 |
| 297 void SecurityKeyAuthHandlerPosix::RequestTimedOut(int connection_id) { | 304 void SecurityKeyAuthHandlerPosix::RequestTimedOut(int connection_id) { |
| 298 HOST_LOG << "SecurityKey request timed out: " << connection_id; | 305 HOST_LOG << "SecurityKey request timed out for socket: " << connection_id; |
| 299 ActiveSockets::const_iterator iter = active_sockets_.find(connection_id); | 306 ActiveSockets::const_iterator iter = active_sockets_.find(connection_id); |
| 300 if (iter != active_sockets_.end()) { | 307 if (iter != active_sockets_.end()) { |
| 301 SendErrorAndCloseActiveSocket(iter); | 308 SendErrorAndCloseActiveSocket(iter); |
| 302 } | 309 } |
| 303 } | 310 } |
| 304 | 311 |
| 305 } // namespace remoting | 312 } // namespace remoting |
| OLD | NEW |