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/security_key_ipc_server_impl.h" |
6 | 6 |
7 #include <cstdint> | 7 #include <cstdint> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
(...skipping 16 matching lines...) Expand all Loading... |
32 // 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 |
33 // the data is empty. | 33 // the data is empty. |
34 unsigned int GetCommandCode(const std::string& data) { | 34 unsigned int GetCommandCode(const std::string& data) { |
35 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); | 35 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); |
36 } | 36 } |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 namespace remoting { | 40 namespace remoting { |
41 | 41 |
42 RemoteSecurityKeyIpcServerImpl::RemoteSecurityKeyIpcServerImpl( | 42 SecurityKeyIpcServerImpl::SecurityKeyIpcServerImpl( |
43 int connection_id, | 43 int connection_id, |
44 uint32_t peer_session_id, | 44 uint32_t peer_session_id, |
45 base::TimeDelta initial_connect_timeout, | 45 base::TimeDelta initial_connect_timeout, |
46 const GnubbyAuthHandler::SendMessageCallback& message_callback, | 46 const SecurityKeyAuthHandler::SendMessageCallback& message_callback, |
47 const base::Closure& done_callback) | 47 const base::Closure& done_callback) |
48 : connection_id_(connection_id), | 48 : connection_id_(connection_id), |
49 peer_session_id_(peer_session_id), | 49 peer_session_id_(peer_session_id), |
50 initial_connect_timeout_(initial_connect_timeout), | 50 initial_connect_timeout_(initial_connect_timeout), |
51 done_callback_(done_callback), | 51 done_callback_(done_callback), |
52 message_callback_(message_callback), | 52 message_callback_(message_callback), |
53 weak_factory_(this) { | 53 weak_factory_(this) { |
54 DCHECK_GT(connection_id_, 0); | 54 DCHECK_GT(connection_id_, 0); |
55 DCHECK(!done_callback_.is_null()); | 55 DCHECK(!done_callback_.is_null()); |
56 DCHECK(!message_callback_.is_null()); | 56 DCHECK(!message_callback_.is_null()); |
57 } | 57 } |
58 | 58 |
59 RemoteSecurityKeyIpcServerImpl::~RemoteSecurityKeyIpcServerImpl() {} | 59 SecurityKeyIpcServerImpl::~SecurityKeyIpcServerImpl() {} |
60 | 60 |
61 bool RemoteSecurityKeyIpcServerImpl::CreateChannel( | 61 bool SecurityKeyIpcServerImpl::CreateChannel(const std::string& channel_name, |
62 const std::string& channel_name, | 62 base::TimeDelta request_timeout) { |
63 base::TimeDelta request_timeout) { | |
64 DCHECK(thread_checker_.CalledOnValidThread()); | 63 DCHECK(thread_checker_.CalledOnValidThread()); |
65 DCHECK(!ipc_channel_); | 64 DCHECK(!ipc_channel_); |
66 security_key_request_timeout_ = request_timeout; | 65 security_key_request_timeout_ = request_timeout; |
67 | 66 |
68 #if defined(OS_WIN) | 67 #if defined(OS_WIN) |
69 // Create a named pipe owned by the current user (the LocalService account | 68 // Create a named pipe owned by the current user (the LocalService account |
70 // (SID: S-1-5-19) when running in the network process) which is available to | 69 // (SID: S-1-5-19) when running in the network process) which is available to |
71 // all authenticated users. | 70 // all authenticated users. |
72 // presubmit: allow wstring | 71 // presubmit: allow wstring |
73 std::wstring user_sid; | 72 std::wstring user_sid; |
(...skipping 18 matching lines...) Expand all Loading... |
92 | 91 |
93 if (!ipc_channel_->Connect()) { | 92 if (!ipc_channel_->Connect()) { |
94 ipc_channel_.reset(); | 93 ipc_channel_.reset(); |
95 return false; | 94 return false; |
96 } | 95 } |
97 // It is safe to use base::Unretained here as |timer_| will be stopped and | 96 // It is safe to use base::Unretained here as |timer_| will be stopped and |
98 // this task will be removed when this instance is being destroyed. All | 97 // this task will be removed when this instance is being destroyed. All |
99 // methods must execute on the same thread (due to |thread_Checker_| so | 98 // methods must execute on the same thread (due to |thread_Checker_| so |
100 // the posted task and D'Tor can not execute concurrently. | 99 // the posted task and D'Tor can not execute concurrently. |
101 timer_.Start(FROM_HERE, initial_connect_timeout_, | 100 timer_.Start(FROM_HERE, initial_connect_timeout_, |
102 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | 101 base::Bind(&SecurityKeyIpcServerImpl::OnChannelError, |
103 base::Unretained(this))); | 102 base::Unretained(this))); |
104 return true; | 103 return true; |
105 } | 104 } |
106 | 105 |
107 bool RemoteSecurityKeyIpcServerImpl::SendResponse(const std::string& response) { | 106 bool SecurityKeyIpcServerImpl::SendResponse(const std::string& response) { |
108 DCHECK(thread_checker_.CalledOnValidThread()); | 107 DCHECK(thread_checker_.CalledOnValidThread()); |
109 | 108 |
110 // Since we have received a response, we update the timer and wait | 109 // Since we have received a response, we update the timer and wait |
111 // for a subsequent request. | 110 // for a subsequent request. |
112 timer_.Start(FROM_HERE, security_key_request_timeout_, | 111 timer_.Start(FROM_HERE, security_key_request_timeout_, |
113 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | 112 base::Bind(&SecurityKeyIpcServerImpl::OnChannelError, |
114 base::Unretained(this))); | 113 base::Unretained(this))); |
115 | 114 |
116 return ipc_channel_->Send( | 115 return ipc_channel_->Send( |
117 new ChromotingNetworkToRemoteSecurityKeyMsg_Response(response)); | 116 new ChromotingNetworkToRemoteSecurityKeyMsg_Response(response)); |
118 } | 117 } |
119 | 118 |
120 bool RemoteSecurityKeyIpcServerImpl::OnMessageReceived( | 119 bool SecurityKeyIpcServerImpl::OnMessageReceived(const IPC::Message& message) { |
121 const IPC::Message& message) { | |
122 DCHECK(thread_checker_.CalledOnValidThread()); | 120 DCHECK(thread_checker_.CalledOnValidThread()); |
123 | 121 |
124 if (connection_close_pending_) { | 122 if (connection_close_pending_) { |
125 LOG(WARNING) << "IPC Message ignored because channel is being closed."; | 123 LOG(WARNING) << "IPC Message ignored because channel is being closed."; |
126 return false; | 124 return false; |
127 } | 125 } |
128 | 126 |
129 bool handled = true; | 127 bool handled = true; |
130 IPC_BEGIN_MESSAGE_MAP(RemoteSecurityKeyIpcServerImpl, message) | 128 IPC_BEGIN_MESSAGE_MAP(SecurityKeyIpcServerImpl, message) |
131 IPC_MESSAGE_HANDLER(ChromotingRemoteSecurityKeyToNetworkMsg_Request, | 129 IPC_MESSAGE_HANDLER(ChromotingRemoteSecurityKeyToNetworkMsg_Request, |
132 OnSecurityKeyRequest) | 130 OnSecurityKeyRequest) |
133 IPC_MESSAGE_UNHANDLED(handled = false) | 131 IPC_MESSAGE_UNHANDLED(handled = false) |
134 IPC_END_MESSAGE_MAP() | 132 IPC_END_MESSAGE_MAP() |
135 | 133 |
136 CHECK(handled) << "Received unexpected IPC type: " << message.type(); | 134 CHECK(handled) << "Received unexpected IPC type: " << message.type(); |
137 return handled; | 135 return handled; |
138 } | 136 } |
139 | 137 |
140 void RemoteSecurityKeyIpcServerImpl::OnChannelConnected(int32_t peer_pid) { | 138 void SecurityKeyIpcServerImpl::OnChannelConnected(int32_t peer_pid) { |
141 DCHECK(thread_checker_.CalledOnValidThread()); | 139 DCHECK(thread_checker_.CalledOnValidThread()); |
142 | 140 |
143 #if defined(OS_WIN) | 141 #if defined(OS_WIN) |
144 DWORD peer_session_id; | 142 DWORD peer_session_id; |
145 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) { | 143 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) { |
146 PLOG(ERROR) << "ProcessIdToSessionId() failed"; | 144 PLOG(ERROR) << "ProcessIdToSessionId() failed"; |
147 connection_close_pending_ = true; | 145 connection_close_pending_ = true; |
148 } else if (peer_session_id != peer_session_id_) { | 146 } else if (peer_session_id != peer_session_id_) { |
149 LOG(ERROR) << "Ignoring connection attempt from outside remoted session."; | 147 LOG(ERROR) << "Ignoring connection attempt from outside remoted session."; |
150 connection_close_pending_ = true; | 148 connection_close_pending_ = true; |
151 } | 149 } |
152 if (connection_close_pending_) { | 150 if (connection_close_pending_) { |
153 base::ThreadTaskRunnerHandle::Get()->PostTask( | 151 base::ThreadTaskRunnerHandle::Get()->PostTask( |
154 FROM_HERE, base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | 152 FROM_HERE, base::Bind(&SecurityKeyIpcServerImpl::OnChannelError, |
155 weak_factory_.GetWeakPtr())); | 153 weak_factory_.GetWeakPtr())); |
156 return; | 154 return; |
157 } | 155 } |
158 #else // !defined(OS_WIN) | 156 #else // !defined(OS_WIN) |
159 CHECK_EQ(peer_session_id_, UINT32_MAX); | 157 CHECK_EQ(peer_session_id_, UINT32_MAX); |
160 #endif // !defined(OS_WIN) | 158 #endif // !defined(OS_WIN) |
161 | 159 |
162 // Reset the timer to give the client a chance to send the request. | 160 // Reset the timer to give the client a chance to send the request. |
163 timer_.Start(FROM_HERE, initial_connect_timeout_, | 161 timer_.Start(FROM_HERE, initial_connect_timeout_, |
164 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | 162 base::Bind(&SecurityKeyIpcServerImpl::OnChannelError, |
165 base::Unretained(this))); | 163 base::Unretained(this))); |
166 } | 164 } |
167 | 165 |
168 void RemoteSecurityKeyIpcServerImpl::OnChannelError() { | 166 void SecurityKeyIpcServerImpl::OnChannelError() { |
169 DCHECK(thread_checker_.CalledOnValidThread()); | 167 DCHECK(thread_checker_.CalledOnValidThread()); |
170 if (ipc_channel_) { | 168 if (ipc_channel_) { |
171 ipc_channel_->Close(); | 169 ipc_channel_->Close(); |
172 connection_close_pending_ = false; | 170 connection_close_pending_ = false; |
173 } | 171 } |
174 | 172 |
175 if (!done_callback_.is_null()) { | 173 if (!done_callback_.is_null()) { |
176 // Note: This callback may result in this object being torn down. | 174 // Note: This callback may result in this object being torn down. |
177 base::ResetAndReturn(&done_callback_).Run(); | 175 base::ResetAndReturn(&done_callback_).Run(); |
178 } | 176 } |
179 } | 177 } |
180 | 178 |
181 void RemoteSecurityKeyIpcServerImpl::OnSecurityKeyRequest( | 179 void SecurityKeyIpcServerImpl::OnSecurityKeyRequest( |
182 const std::string& request_data) { | 180 const std::string& request_data) { |
183 DCHECK(thread_checker_.CalledOnValidThread()); | 181 DCHECK(thread_checker_.CalledOnValidThread()); |
184 | 182 |
185 // Reset the timer to give the client a chance to send the response. | 183 // Reset the timer to give the client a chance to send the response. |
186 timer_.Start(FROM_HERE, security_key_request_timeout_, | 184 timer_.Start(FROM_HERE, security_key_request_timeout_, |
187 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | 185 base::Bind(&SecurityKeyIpcServerImpl::OnChannelError, |
188 base::Unretained(this))); | 186 base::Unretained(this))); |
189 | 187 |
190 HOST_LOG << "Received gnubby request: " << GetCommandCode(request_data); | 188 HOST_LOG << "Received security key request: " << GetCommandCode(request_data); |
191 message_callback_.Run(connection_id_, request_data); | 189 message_callback_.Run(connection_id_, request_data); |
192 } | 190 } |
193 | 191 |
194 } // namespace remoting | 192 } // namespace remoting |
OLD | NEW |