OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/host/security_key/remote_security_key_ipc_server_impl.h" | |
6 | |
7 #include <cstdint> | |
8 #include <memory> | |
9 #include <string> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/callback_helpers.h" | |
13 #include "base/location.h" | |
14 #include "base/threading/thread_checker.h" | |
15 #include "base/threading/thread_task_runner_handle.h" | |
16 #include "base/timer/timer.h" | |
17 #include "ipc/ipc_channel.h" | |
18 #include "ipc/ipc_message.h" | |
19 #include "ipc/ipc_message_macros.h" | |
20 #include "remoting/base/logging.h" | |
21 #include "remoting/host/chromoting_messages.h" | |
22 | |
23 #if defined(OS_WIN) | |
24 #include "base/strings/stringprintf.h" | |
25 #include "base/strings/utf_string_conversions.h" | |
26 #include "base/win/win_util.h" | |
27 #include "remoting/host/ipc_util.h" | |
28 #endif // defined(OS_WIN) | |
29 | |
30 namespace { | |
31 | |
32 // Returns the command code (the first byte of the data) if it exists, or -1 if | |
33 // the data is empty. | |
34 unsigned int GetCommandCode(const std::string& data) { | |
35 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 namespace remoting { | |
41 | |
42 RemoteSecurityKeyIpcServerImpl::RemoteSecurityKeyIpcServerImpl( | |
43 int connection_id, | |
44 uint32_t peer_session_id, | |
45 base::TimeDelta initial_connect_timeout, | |
46 const GnubbyAuthHandler::SendMessageCallback& message_callback, | |
47 const base::Closure& done_callback) | |
48 : connection_id_(connection_id), | |
49 peer_session_id_(peer_session_id), | |
50 initial_connect_timeout_(initial_connect_timeout), | |
51 done_callback_(done_callback), | |
52 message_callback_(message_callback), | |
53 weak_factory_(this) { | |
54 DCHECK_GT(connection_id_, 0); | |
55 DCHECK(!done_callback_.is_null()); | |
56 DCHECK(!message_callback_.is_null()); | |
57 } | |
58 | |
59 RemoteSecurityKeyIpcServerImpl::~RemoteSecurityKeyIpcServerImpl() {} | |
60 | |
61 bool RemoteSecurityKeyIpcServerImpl::CreateChannel( | |
62 const std::string& channel_name, | |
63 base::TimeDelta request_timeout) { | |
64 DCHECK(thread_checker_.CalledOnValidThread()); | |
65 DCHECK(!ipc_channel_); | |
66 security_key_request_timeout_ = request_timeout; | |
67 | |
68 #if defined(OS_WIN) | |
69 // 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 | |
71 // all authenticated users. | |
72 // presubmit: allow wstring | |
73 std::wstring user_sid; | |
74 if (!base::win::GetUserSidString(&user_sid)) { | |
75 return false; | |
76 } | |
77 std::string user_sid_utf8 = base::WideToUTF8(user_sid); | |
78 std::string security_descriptor = base::StringPrintf( | |
79 "O:%sG:%sD:(A;;GA;;;AU)", user_sid_utf8.c_str(), user_sid_utf8.c_str()); | |
80 | |
81 base::win::ScopedHandle pipe; | |
82 if (!CreateIpcChannel(channel_name, security_descriptor, &pipe)) { | |
83 return false; | |
84 } | |
85 | |
86 ipc_channel_ = | |
87 IPC::Channel::CreateNamedServer(IPC::ChannelHandle(pipe.Get()), this); | |
88 #else // defined(OS_WIN) | |
89 ipc_channel_ = | |
90 IPC::Channel::CreateNamedServer(IPC::ChannelHandle(channel_name), this); | |
91 #endif // !defined(OS_WIN) | |
92 | |
93 if (!ipc_channel_->Connect()) { | |
94 ipc_channel_.reset(); | |
95 return false; | |
96 } | |
97 // 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 | |
99 // methods must execute on the same thread (due to |thread_Checker_| so | |
100 // the posted task and D'Tor can not execute concurrently. | |
101 timer_.Start(FROM_HERE, initial_connect_timeout_, | |
102 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | |
103 base::Unretained(this))); | |
104 return true; | |
105 } | |
106 | |
107 bool RemoteSecurityKeyIpcServerImpl::SendResponse(const std::string& response) { | |
108 DCHECK(thread_checker_.CalledOnValidThread()); | |
109 | |
110 // Since we have received a response, we update the timer and wait | |
111 // for a subsequent request. | |
112 timer_.Start(FROM_HERE, security_key_request_timeout_, | |
113 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | |
114 base::Unretained(this))); | |
115 | |
116 return ipc_channel_->Send( | |
117 new ChromotingNetworkToRemoteSecurityKeyMsg_Response(response)); | |
118 } | |
119 | |
120 bool RemoteSecurityKeyIpcServerImpl::OnMessageReceived( | |
121 const IPC::Message& message) { | |
122 DCHECK(thread_checker_.CalledOnValidThread()); | |
123 | |
124 if (connection_close_pending_) { | |
125 LOG(WARNING) << "IPC Message ignored because channel is being closed."; | |
126 return false; | |
127 } | |
128 | |
129 bool handled = true; | |
130 IPC_BEGIN_MESSAGE_MAP(RemoteSecurityKeyIpcServerImpl, message) | |
131 IPC_MESSAGE_HANDLER(ChromotingRemoteSecurityKeyToNetworkMsg_Request, | |
132 OnSecurityKeyRequest) | |
133 IPC_MESSAGE_UNHANDLED(handled = false) | |
134 IPC_END_MESSAGE_MAP() | |
135 | |
136 CHECK(handled) << "Received unexpected IPC type: " << message.type(); | |
137 return handled; | |
138 } | |
139 | |
140 void RemoteSecurityKeyIpcServerImpl::OnChannelConnected(int32_t peer_pid) { | |
141 DCHECK(thread_checker_.CalledOnValidThread()); | |
142 | |
143 #if defined(OS_WIN) | |
144 DWORD peer_session_id; | |
145 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) { | |
146 PLOG(ERROR) << "ProcessIdToSessionId() failed"; | |
147 connection_close_pending_ = true; | |
148 } else if (peer_session_id != peer_session_id_) { | |
149 LOG(ERROR) << "Ignoring connection attempt from outside remoted session."; | |
150 connection_close_pending_ = true; | |
151 } | |
152 if (connection_close_pending_) { | |
153 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
154 FROM_HERE, base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | |
155 weak_factory_.GetWeakPtr())); | |
156 return; | |
157 } | |
158 #else // !defined(OS_WIN) | |
159 CHECK_EQ(peer_session_id_, UINT32_MAX); | |
160 #endif // !defined(OS_WIN) | |
161 | |
162 // Reset the timer to give the client a chance to send the request. | |
163 timer_.Start(FROM_HERE, initial_connect_timeout_, | |
164 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | |
165 base::Unretained(this))); | |
166 } | |
167 | |
168 void RemoteSecurityKeyIpcServerImpl::OnChannelError() { | |
169 DCHECK(thread_checker_.CalledOnValidThread()); | |
170 if (ipc_channel_) { | |
171 ipc_channel_->Close(); | |
172 connection_close_pending_ = false; | |
173 } | |
174 | |
175 if (!done_callback_.is_null()) { | |
176 // Note: This callback may result in this object being torn down. | |
177 base::ResetAndReturn(&done_callback_).Run(); | |
178 } | |
179 } | |
180 | |
181 void RemoteSecurityKeyIpcServerImpl::OnSecurityKeyRequest( | |
182 const std::string& request_data) { | |
183 DCHECK(thread_checker_.CalledOnValidThread()); | |
184 | |
185 // Reset the timer to give the client a chance to send the response. | |
186 timer_.Start(FROM_HERE, security_key_request_timeout_, | |
187 base::Bind(&RemoteSecurityKeyIpcServerImpl::OnChannelError, | |
188 base::Unretained(this))); | |
189 | |
190 HOST_LOG << "Received gnubby request: " << GetCommandCode(request_data); | |
191 message_callback_.Run(connection_id_, request_data); | |
192 } | |
193 | |
194 } // namespace remoting | |
OLD | NEW |