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 <stdint.h> | |
8 #include <unistd.h> | 7 #include <unistd.h> |
9 | 8 |
| 9 #include <cstdint> |
| 10 #include <map> |
10 #include <memory> | 11 #include <memory> |
| 12 #include <string> |
| 13 #include <utility> |
11 | 14 |
12 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/callback.h" |
| 17 #include "base/files/file_path.h" |
13 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
14 #include "base/lazy_instance.h" | 19 #include "base/lazy_instance.h" |
| 20 #include "base/location.h" |
15 #include "base/logging.h" | 21 #include "base/logging.h" |
16 #include "base/stl_util.h" | 22 #include "base/memory/ptr_util.h" |
| 23 #include "base/memory/ref_counted.h" |
| 24 #include "base/memory/weak_ptr.h" |
| 25 #include "base/single_thread_task_runner.h" |
17 #include "base/threading/thread_checker.h" | 26 #include "base/threading/thread_checker.h" |
18 #include "base/threading/thread_restrictions.h" | |
19 #include "base/values.h" | |
20 #include "net/base/completion_callback.h" | 27 #include "net/base/completion_callback.h" |
21 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
22 #include "net/socket/stream_socket.h" | 29 #include "net/socket/stream_socket.h" |
23 #include "net/socket/unix_domain_server_socket_posix.h" | 30 #include "net/socket/unix_domain_server_socket_posix.h" |
24 #include "remoting/base/logging.h" | 31 #include "remoting/base/logging.h" |
25 #include "remoting/host/security_key/security_key_socket.h" | 32 #include "remoting/host/security_key/security_key_socket.h" |
26 | 33 |
27 namespace { | 34 namespace { |
28 | 35 |
29 const int64_t kDefaultRequestTimeoutSeconds = 60; | 36 const int64_t kDefaultRequestTimeoutSeconds = 60; |
(...skipping 16 matching lines...) Expand all Loading... |
46 unsigned int GetCommandCode(const std::string& data) { | 53 unsigned int GetCommandCode(const std::string& data) { |
47 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); | 54 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); |
48 } | 55 } |
49 | 56 |
50 } // namespace | 57 } // namespace |
51 | 58 |
52 namespace remoting { | 59 namespace remoting { |
53 | 60 |
54 class SecurityKeyAuthHandlerLinux : public SecurityKeyAuthHandler { | 61 class SecurityKeyAuthHandlerLinux : public SecurityKeyAuthHandler { |
55 public: | 62 public: |
56 SecurityKeyAuthHandlerLinux(); | 63 explicit SecurityKeyAuthHandlerLinux( |
| 64 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); |
57 ~SecurityKeyAuthHandlerLinux() override; | 65 ~SecurityKeyAuthHandlerLinux() override; |
58 | 66 |
59 private: | 67 private: |
60 typedef std::map<int, SecurityKeySocket*> ActiveSockets; | 68 typedef std::map<int, std::unique_ptr<SecurityKeySocket>> ActiveSockets; |
61 | 69 |
62 // SecurityKeyAuthHandler interface. | 70 // SecurityKeyAuthHandler interface. |
63 void CreateSecurityKeyConnection() override; | 71 void CreateSecurityKeyConnection() override; |
64 bool IsValidConnectionId(int security_key_connection_id) const override; | 72 bool IsValidConnectionId(int security_key_connection_id) const override; |
65 void SendClientResponse(int security_key_connection_id, | 73 void SendClientResponse(int security_key_connection_id, |
66 const std::string& response) override; | 74 const std::string& response) override; |
67 void SendErrorAndCloseConnection(int security_key_connection_id) override; | 75 void SendErrorAndCloseConnection(int security_key_connection_id) override; |
68 void SetSendMessageCallback(const SendMessageCallback& callback) override; | 76 void SetSendMessageCallback(const SendMessageCallback& callback) override; |
69 size_t GetActiveConnectionCountForTest() const override; | 77 size_t GetActiveConnectionCountForTest() const override; |
70 void SetRequestTimeoutForTest(base::TimeDelta timeout) override; | 78 void SetRequestTimeoutForTest(base::TimeDelta timeout) override; |
71 | 79 |
| 80 // Sets up the socket used for accepting new connections. |
| 81 void CreateSocket(); |
| 82 |
72 // Starts listening for connection. | 83 // Starts listening for connection. |
73 void DoAccept(); | 84 void DoAccept(); |
74 | 85 |
75 // Called when a connection is accepted. | 86 // Called when a connection is accepted. |
76 void OnAccepted(int result); | 87 void OnAccepted(int result); |
77 | 88 |
78 // Called when a SecurityKeySocket has done reading. | 89 // Called when a SecurityKeySocket has done reading. |
79 void OnReadComplete(int security_key_connection_id); | 90 void OnReadComplete(int security_key_connection_id); |
80 | 91 |
81 // Gets an active socket iterator for |security_key_connection_id|. | 92 // Gets an active socket iterator for |security_key_connection_id|. |
(...skipping 12 matching lines...) Expand all Loading... |
94 // Socket used to listen for authorization requests. | 105 // Socket used to listen for authorization requests. |
95 std::unique_ptr<net::UnixDomainServerSocket> auth_socket_; | 106 std::unique_ptr<net::UnixDomainServerSocket> auth_socket_; |
96 | 107 |
97 // A temporary holder for an accepted connection. | 108 // A temporary holder for an accepted connection. |
98 std::unique_ptr<net::StreamSocket> accept_socket_; | 109 std::unique_ptr<net::StreamSocket> accept_socket_; |
99 | 110 |
100 // Used to pass security key extension messages to the client. | 111 // Used to pass security key extension messages to the client. |
101 SendMessageCallback send_message_callback_; | 112 SendMessageCallback send_message_callback_; |
102 | 113 |
103 // The last assigned security key connection id. | 114 // The last assigned security key connection id. |
104 int last_connection_id_; | 115 int last_connection_id_ = 0; |
105 | 116 |
106 // Sockets by connection id used to process gnubbyd requests. | 117 // Sockets by connection id used to process gnubbyd requests. |
107 ActiveSockets active_sockets_; | 118 ActiveSockets active_sockets_; |
108 | 119 |
| 120 // Used to perform blocking File IO. |
| 121 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
| 122 |
109 // Timeout used for a request. | 123 // Timeout used for a request. |
110 base::TimeDelta request_timeout_; | 124 base::TimeDelta request_timeout_; |
111 | 125 |
| 126 base::WeakPtrFactory<SecurityKeyAuthHandlerLinux> weak_factory_; |
| 127 |
112 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerLinux); | 128 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerLinux); |
113 }; | 129 }; |
114 | 130 |
115 std::unique_ptr<SecurityKeyAuthHandler> SecurityKeyAuthHandler::Create( | 131 std::unique_ptr<SecurityKeyAuthHandler> SecurityKeyAuthHandler::Create( |
116 ClientSessionDetails* client_session_details, | 132 ClientSessionDetails* client_session_details, |
117 const SendMessageCallback& send_message_callback) { | 133 const SendMessageCallback& send_message_callback, |
| 134 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { |
118 std::unique_ptr<SecurityKeyAuthHandler> auth_handler( | 135 std::unique_ptr<SecurityKeyAuthHandler> auth_handler( |
119 new SecurityKeyAuthHandlerLinux()); | 136 new SecurityKeyAuthHandlerLinux(file_task_runner)); |
120 auth_handler->SetSendMessageCallback(send_message_callback); | 137 auth_handler->SetSendMessageCallback(send_message_callback); |
121 return auth_handler; | 138 return auth_handler; |
122 } | 139 } |
123 | 140 |
124 void SecurityKeyAuthHandler::SetSecurityKeySocketName( | 141 void SecurityKeyAuthHandler::SetSecurityKeySocketName( |
125 const base::FilePath& security_key_socket_name) { | 142 const base::FilePath& security_key_socket_name) { |
126 g_security_key_socket_name.Get() = security_key_socket_name; | 143 g_security_key_socket_name.Get() = security_key_socket_name; |
127 } | 144 } |
128 | 145 |
129 SecurityKeyAuthHandlerLinux::SecurityKeyAuthHandlerLinux() | 146 SecurityKeyAuthHandlerLinux::SecurityKeyAuthHandlerLinux( |
130 : last_connection_id_(0), | 147 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
| 148 : file_task_runner_(file_task_runner), |
131 request_timeout_( | 149 request_timeout_( |
132 base::TimeDelta::FromSeconds(kDefaultRequestTimeoutSeconds)) {} | 150 base::TimeDelta::FromSeconds(kDefaultRequestTimeoutSeconds)), |
| 151 weak_factory_(this) {} |
133 | 152 |
134 SecurityKeyAuthHandlerLinux::~SecurityKeyAuthHandlerLinux() { | 153 SecurityKeyAuthHandlerLinux::~SecurityKeyAuthHandlerLinux() {} |
135 STLDeleteValues(&active_sockets_); | |
136 } | |
137 | 154 |
138 void SecurityKeyAuthHandlerLinux::CreateSecurityKeyConnection() { | 155 void SecurityKeyAuthHandlerLinux::CreateSecurityKeyConnection() { |
139 DCHECK(thread_checker_.CalledOnValidThread()); | 156 DCHECK(thread_checker_.CalledOnValidThread()); |
140 DCHECK(!g_security_key_socket_name.Get().empty()); | 157 DCHECK(!g_security_key_socket_name.Get().empty()); |
141 | 158 |
142 { | 159 // We need to run the DeleteFile method on |file_task_runner_| as it is a |
143 // DeleteFile() is a blocking operation, but so is creation of the unix | 160 // blocking function call which cannot be run on the main thread. Once |
144 // socket below. Consider moving this class to a different thread if this | 161 // that task has completed, the main thread will be called back and we will |
145 // causes any problems. See crbug.com/509807. | 162 // resume setting up our security key auth socket there. |
146 // TODO(joedow): Since this code now runs as a host extension, we should | 163 file_task_runner_->PostTaskAndReply( |
147 // perform our IO on a separate thread: crbug.com/591739 | 164 FROM_HERE, base::Bind(base::IgnoreResult(&base::DeleteFile), |
148 base::ThreadRestrictions::ScopedAllowIO allow_io; | 165 g_security_key_socket_name.Get(), false), |
| 166 base::Bind(&SecurityKeyAuthHandlerLinux::CreateSocket, |
| 167 weak_factory_.GetWeakPtr())); |
| 168 } |
149 | 169 |
150 // If the file already exists, a socket in use error is returned. | 170 void SecurityKeyAuthHandlerLinux::CreateSocket() { |
151 base::DeleteFile(g_security_key_socket_name.Get(), false); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
152 } | |
153 | |
154 HOST_LOG << "Listening for security key requests on " | 172 HOST_LOG << "Listening for security key requests on " |
155 << g_security_key_socket_name.Get().value(); | 173 << g_security_key_socket_name.Get().value(); |
156 | 174 |
157 auth_socket_.reset( | 175 auth_socket_.reset( |
158 new net::UnixDomainServerSocket(base::Bind(MatchUid), false)); | 176 new net::UnixDomainServerSocket(base::Bind(MatchUid), false)); |
159 int rv = auth_socket_->BindAndListen(g_security_key_socket_name.Get().value(), | 177 int rv = auth_socket_->BindAndListen(g_security_key_socket_name.Get().value(), |
160 /*backlog=*/1); | 178 /*backlog=*/1); |
161 if (rv != net::OK) { | 179 if (rv != net::OK) { |
162 LOG(ERROR) << "Failed to open socket for auth requests: '" << rv << "'"; | 180 LOG(ERROR) << "Failed to open socket for auth requests: '" << rv << "'"; |
163 return; | 181 return; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 size_t SecurityKeyAuthHandlerLinux::GetActiveConnectionCountForTest() const { | 223 size_t SecurityKeyAuthHandlerLinux::GetActiveConnectionCountForTest() const { |
206 return active_sockets_.size(); | 224 return active_sockets_.size(); |
207 } | 225 } |
208 | 226 |
209 void SecurityKeyAuthHandlerLinux::SetRequestTimeoutForTest( | 227 void SecurityKeyAuthHandlerLinux::SetRequestTimeoutForTest( |
210 base::TimeDelta timeout) { | 228 base::TimeDelta timeout) { |
211 request_timeout_ = timeout; | 229 request_timeout_ = timeout; |
212 } | 230 } |
213 | 231 |
214 void SecurityKeyAuthHandlerLinux::DoAccept() { | 232 void SecurityKeyAuthHandlerLinux::DoAccept() { |
| 233 DCHECK(thread_checker_.CalledOnValidThread()); |
215 int result = auth_socket_->Accept( | 234 int result = auth_socket_->Accept( |
216 &accept_socket_, base::Bind(&SecurityKeyAuthHandlerLinux::OnAccepted, | 235 &accept_socket_, base::Bind(&SecurityKeyAuthHandlerLinux::OnAccepted, |
217 base::Unretained(this))); | 236 base::Unretained(this))); |
218 if (result != net::ERR_IO_PENDING) | 237 if (result != net::ERR_IO_PENDING) |
219 OnAccepted(result); | 238 OnAccepted(result); |
220 } | 239 } |
221 | 240 |
222 void SecurityKeyAuthHandlerLinux::OnAccepted(int result) { | 241 void SecurityKeyAuthHandlerLinux::OnAccepted(int result) { |
223 DCHECK(thread_checker_.CalledOnValidThread()); | 242 DCHECK(thread_checker_.CalledOnValidThread()); |
224 DCHECK_NE(net::ERR_IO_PENDING, result); | 243 DCHECK_NE(net::ERR_IO_PENDING, result); |
225 | 244 |
226 if (result < 0) { | 245 if (result < 0) { |
227 LOG(ERROR) << "Error in accepting a new connection"; | 246 LOG(ERROR) << "Error in accepting a new connection"; |
228 return; | 247 return; |
229 } | 248 } |
230 | 249 |
231 int security_key_connection_id = ++last_connection_id_; | 250 int security_key_connection_id = ++last_connection_id_; |
232 SecurityKeySocket* socket = new SecurityKeySocket( | 251 SecurityKeySocket* socket = new SecurityKeySocket( |
233 std::move(accept_socket_), request_timeout_, | 252 std::move(accept_socket_), request_timeout_, |
234 base::Bind(&SecurityKeyAuthHandlerLinux::RequestTimedOut, | 253 base::Bind(&SecurityKeyAuthHandlerLinux::RequestTimedOut, |
235 base::Unretained(this), security_key_connection_id)); | 254 base::Unretained(this), security_key_connection_id)); |
236 active_sockets_[security_key_connection_id] = socket; | 255 active_sockets_[security_key_connection_id] = base::WrapUnique(socket); |
237 socket->StartReadingRequest( | 256 socket->StartReadingRequest( |
238 base::Bind(&SecurityKeyAuthHandlerLinux::OnReadComplete, | 257 base::Bind(&SecurityKeyAuthHandlerLinux::OnReadComplete, |
239 base::Unretained(this), security_key_connection_id)); | 258 base::Unretained(this), security_key_connection_id)); |
240 | 259 |
241 // Continue accepting new connections. | 260 // Continue accepting new connections. |
242 DoAccept(); | 261 DoAccept(); |
243 } | 262 } |
244 | 263 |
245 void SecurityKeyAuthHandlerLinux::OnReadComplete( | 264 void SecurityKeyAuthHandlerLinux::OnReadComplete( |
246 int security_key_connection_id) { | 265 int security_key_connection_id) { |
(...skipping 18 matching lines...) Expand all Loading... |
265 | 284 |
266 SecurityKeyAuthHandlerLinux::ActiveSockets::const_iterator | 285 SecurityKeyAuthHandlerLinux::ActiveSockets::const_iterator |
267 SecurityKeyAuthHandlerLinux::GetSocketForConnectionId( | 286 SecurityKeyAuthHandlerLinux::GetSocketForConnectionId( |
268 int security_key_connection_id) const { | 287 int security_key_connection_id) const { |
269 return active_sockets_.find(security_key_connection_id); | 288 return active_sockets_.find(security_key_connection_id); |
270 } | 289 } |
271 | 290 |
272 void SecurityKeyAuthHandlerLinux::SendErrorAndCloseActiveSocket( | 291 void SecurityKeyAuthHandlerLinux::SendErrorAndCloseActiveSocket( |
273 const ActiveSockets::const_iterator& iter) { | 292 const ActiveSockets::const_iterator& iter) { |
274 iter->second->SendSshError(); | 293 iter->second->SendSshError(); |
275 delete iter->second; | |
276 active_sockets_.erase(iter); | 294 active_sockets_.erase(iter); |
277 } | 295 } |
278 | 296 |
279 void SecurityKeyAuthHandlerLinux::RequestTimedOut( | 297 void SecurityKeyAuthHandlerLinux::RequestTimedOut( |
280 int security_key_connection_id) { | 298 int security_key_connection_id) { |
281 HOST_LOG << "SecurityKey request timed out"; | 299 HOST_LOG << "SecurityKey request timed out"; |
282 ActiveSockets::const_iterator iter = | 300 ActiveSockets::const_iterator iter = |
283 active_sockets_.find(security_key_connection_id); | 301 active_sockets_.find(security_key_connection_id); |
284 if (iter != active_sockets_.end()) | 302 if (iter != active_sockets_.end()) |
285 SendErrorAndCloseActiveSocket(iter); | 303 SendErrorAndCloseActiveSocket(iter); |
286 } | 304 } |
287 | 305 |
288 } // namespace remoting | 306 } // namespace remoting |
OLD | NEW |