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/remote_security_key_ipc_server_impl.h" | 5 #include "remoting/host/security_key/remote_security_key_ipc_server_impl.h" |
6 | 6 |
7 #include <cstdint> | |
7 #include <memory> | 8 #include <memory> |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/location.h" | |
12 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "base/threading/thread_task_runner_handle.h" | |
13 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
14 #include "ipc/ipc_channel.h" | 17 #include "ipc/ipc_channel.h" |
15 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
16 #include "ipc/ipc_message_macros.h" | 19 #include "ipc/ipc_message_macros.h" |
17 #include "remoting/base/logging.h" | 20 #include "remoting/base/logging.h" |
18 #include "remoting/host/chromoting_messages.h" | 21 #include "remoting/host/chromoting_messages.h" |
19 | 22 |
20 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
21 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
22 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
23 #include "base/win/win_util.h" | 26 #include "base/win/win_util.h" |
24 #include "remoting/host/ipc_util.h" | 27 #include "remoting/host/ipc_util.h" |
25 #endif // defined(OS_WIN) | 28 #endif // defined(OS_WIN) |
26 | 29 |
27 namespace { | 30 namespace { |
28 | 31 |
29 // Returns the command code (the first byte of the data) if it exists, or -1 if | 32 // Returns the command code (the first byte of the data) if it exists, or -1 if |
30 // the data is empty. | 33 // the data is empty. |
31 unsigned int GetCommandCode(const std::string& data) { | 34 unsigned int GetCommandCode(const std::string& data) { |
32 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); | 35 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); |
33 } | 36 } |
34 | 37 |
35 } // namespace | 38 } // namespace |
36 | 39 |
37 namespace remoting { | 40 namespace remoting { |
38 | 41 |
39 RemoteSecurityKeyIpcServerImpl::RemoteSecurityKeyIpcServerImpl( | 42 RemoteSecurityKeyIpcServerImpl::RemoteSecurityKeyIpcServerImpl( |
40 int connection_id, | 43 int connection_id, |
44 uint32_t peer_session_id, | |
41 base::TimeDelta initial_connect_timeout, | 45 base::TimeDelta initial_connect_timeout, |
42 const GnubbyAuthHandler::SendMessageCallback& message_callback, | 46 const GnubbyAuthHandler::SendMessageCallback& message_callback, |
43 const base::Closure& done_callback) | 47 const base::Closure& done_callback) |
44 : connection_id_(connection_id), | 48 : connection_id_(connection_id), |
49 peer_session_id_(peer_session_id), | |
45 initial_connect_timeout_(initial_connect_timeout), | 50 initial_connect_timeout_(initial_connect_timeout), |
46 done_callback_(done_callback), | 51 done_callback_(done_callback), |
47 message_callback_(message_callback) { | 52 message_callback_(message_callback), |
53 weak_factory_(this) { | |
48 DCHECK_GT(connection_id_, 0); | 54 DCHECK_GT(connection_id_, 0); |
49 DCHECK(!done_callback_.is_null()); | 55 DCHECK(!done_callback_.is_null()); |
50 DCHECK(!message_callback_.is_null()); | 56 DCHECK(!message_callback_.is_null()); |
51 } | 57 } |
52 | 58 |
53 RemoteSecurityKeyIpcServerImpl::~RemoteSecurityKeyIpcServerImpl() {} | 59 RemoteSecurityKeyIpcServerImpl::~RemoteSecurityKeyIpcServerImpl() {} |
54 | 60 |
55 bool RemoteSecurityKeyIpcServerImpl::CreateChannel( | 61 bool RemoteSecurityKeyIpcServerImpl::CreateChannel( |
56 const std::string& channel_name, | 62 const std::string& channel_name, |
57 base::TimeDelta request_timeout) { | 63 base::TimeDelta request_timeout) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 OnSecurityKeyRequest) | 127 OnSecurityKeyRequest) |
122 IPC_MESSAGE_UNHANDLED(handled = false) | 128 IPC_MESSAGE_UNHANDLED(handled = false) |
123 IPC_END_MESSAGE_MAP() | 129 IPC_END_MESSAGE_MAP() |
124 | 130 |
125 CHECK(handled) << "Received unexpected IPC type: " << message.type(); | 131 CHECK(handled) << "Received unexpected IPC type: " << message.type(); |
126 return handled; | 132 return handled; |
127 } | 133 } |
128 | 134 |
129 void RemoteSecurityKeyIpcServerImpl::OnChannelConnected(int32_t peer_pid) { | 135 void RemoteSecurityKeyIpcServerImpl::OnChannelConnected(int32_t peer_pid) { |
130 DCHECK(thread_checker_.CalledOnValidThread()); | 136 DCHECK(thread_checker_.CalledOnValidThread()); |
137 | |
138 #if defined(OS_WIN) | |
139 DWORD peer_session_id; | |
140 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) { | |
141 PLOG(ERROR) << "ProcessIdToSessionId() failed"; | |
142 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
143 FROM_HERE, base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | |
144 weak_factory_.GetWeakPtr())); | |
145 return; | |
146 } | |
147 if (peer_session_id != peer_session_id_) { | |
148 LOG(ERROR) << "Ignoring connection attempt from outside remoted session."; | |
149 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
Sergey Ulanov
2016/06/29 22:36:46
This duplicates the PostTask() call above. Structu
Sergey Ulanov
2016/06/29 22:36:46
Should we also close the channel here? Otherwise t
joedow
2016/06/30 00:10:08
I think that is a good idea, I'll add a member boo
joedow
2016/06/30 00:10:08
Done.
| |
150 FROM_HERE, base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | |
151 weak_factory_.GetWeakPtr())); | |
152 return; | |
153 } | |
154 #else // !defined(OS_WIN) | |
155 CHECK_EQ(peer_session_id_, UINT32_MAX); | |
156 #endif // !defined(OS_WIN) | |
157 | |
131 // Reset the timer to give the client a chance to send the request. | 158 // Reset the timer to give the client a chance to send the request. |
132 timer_.Start(FROM_HERE, initial_connect_timeout_, | 159 timer_.Start(FROM_HERE, initial_connect_timeout_, |
133 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | 160 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, |
134 base::Unretained(this))); | 161 base::Unretained(this))); |
135 } | 162 } |
136 | 163 |
137 void RemoteSecurityKeyIpcServerImpl::OnChannelError() { | 164 void RemoteSecurityKeyIpcServerImpl::OnChannelError() { |
138 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
166 if (ipc_channel_) { | |
167 ipc_channel_->Close(); | |
168 } | |
139 | 169 |
140 if (!done_callback_.is_null()) { | 170 if (!done_callback_.is_null()) { |
141 // Note: This callback may result in this object being torn down. | 171 // Note: This callback may result in this object being torn down. |
142 base::ResetAndReturn(&done_callback_).Run(); | 172 base::ResetAndReturn(&done_callback_).Run(); |
143 } | 173 } |
144 } | 174 } |
145 | 175 |
146 void RemoteSecurityKeyIpcServerImpl::OnSecurityKeyRequest( | 176 void RemoteSecurityKeyIpcServerImpl::OnSecurityKeyRequest( |
147 const std::string& request_data) { | 177 const std::string& request_data) { |
148 DCHECK(thread_checker_.CalledOnValidThread()); | 178 DCHECK(thread_checker_.CalledOnValidThread()); |
149 | 179 |
150 // Reset the timer to give the client a chance to send the response. | 180 // Reset the timer to give the client a chance to send the response. |
151 timer_.Start(FROM_HERE, security_key_request_timeout_, | 181 timer_.Start(FROM_HERE, security_key_request_timeout_, |
152 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | 182 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, |
153 base::Unretained(this))); | 183 base::Unretained(this))); |
154 | 184 |
155 HOST_LOG << "Received gnubby request: " << GetCommandCode(request_data); | 185 HOST_LOG << "Received gnubby request: " << GetCommandCode(request_data); |
156 message_callback_.Run(connection_id_, request_data); | 186 message_callback_.Run(connection_id_, request_data); |
157 } | 187 } |
158 | 188 |
159 } // namespace remoting | 189 } // namespace remoting |
OLD | NEW |