blob: d0478dd2c48322163070bd1e90e3bf2a2f0d58ac [file] [log] [blame]
Andreea Costinas942284d2020-01-28 16:28:40 +01001// Copyright 2020 The Chromium OS 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#include "system-proxy/system_proxy_adaptor.h"
5
6#include <string>
7#include <utility>
8#include <vector>
9
10#include <base/location.h>
Andreea Costinase9c73592020-07-17 15:27:54 +020011#include <base/strings/stringprintf.h>
Andreea Costinas91f75352020-07-08 14:47:47 +020012#include <base/time/time.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010013#include <brillo/dbus/dbus_object.h>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010014#include <brillo/message_loops/message_loop.h>
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020015#include <chromeos/dbus/service_constants.h>
16#include <chromeos/patchpanel/client.h>
17#include <dbus/object_proxy.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010018
Andreea Costinas922fbaf2020-05-28 11:55:22 +020019#include "system-proxy/kerberos_client.h"
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010020#include "system-proxy/sandboxed_worker.h"
Andreea Costinas942284d2020-01-28 16:28:40 +010021
22namespace system_proxy {
23namespace {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010024
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020025constexpr int kProxyPort = 3128;
Andreea Costinas77b180e2020-05-12 15:17:32 +020026constexpr char kNoCredentialsSpecifiedError[] =
27 "No authentication credentials specified";
28constexpr char kOnlySystemTrafficSupportedError[] =
29 "Only system services traffic is currenly supported";
30constexpr char kFailedToStartWorkerError[] = "Failed to start worker process";
Andreea Costinas91f75352020-07-08 14:47:47 +020031// Time delay for calling patchpanel::ConnectNamespace(). Patchpanel needs to
32// enter the network namespace of the worker process to configure it and fails
33// if it's soon after the process starts. See https://crbug.com/1095170 for
34// details.
35constexpr base::TimeDelta kConnectNamespaceDelay =
36 base::TimeDelta::FromSeconds(1);
37constexpr int kNetworkNamespaceReconnectAttempts = 3;
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020038
Andreea Costinas942284d2020-01-28 16:28:40 +010039// Serializes |proto| to a vector of bytes.
40std::vector<uint8_t> SerializeProto(
41 const google::protobuf::MessageLite& proto) {
42 std::vector<uint8_t> proto_blob(proto.ByteSizeLong());
Andreea Costinasc991e232020-06-08 20:30:58 +020043 bool result = proto.SerializeToArray(proto_blob.data(), proto_blob.size());
44 DCHECK(result);
Andreea Costinas942284d2020-01-28 16:28:40 +010045 return proto_blob;
46}
47
48// Parses a proto from an array of bytes |proto_blob|. Returns
49// ERROR_PARSE_REQUEST_FAILED on error.
50std::string DeserializeProto(const base::Location& from_here,
51 google::protobuf::MessageLite* proto,
52 const std::vector<uint8_t>& proto_blob) {
53 if (!proto->ParseFromArray(proto_blob.data(), proto_blob.size())) {
54 const std::string error_message = "Failed to parse proto message.";
55 LOG(ERROR) << from_here.ToString() << error_message;
56 return error_message;
57 }
58 return "";
59}
60} // namespace
61
62SystemProxyAdaptor::SystemProxyAdaptor(
63 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object)
64 : org::chromium::SystemProxyAdaptor(this),
Andreea Costinas91f75352020-07-08 14:47:47 +020065 netns_reconnect_attempts_available_(kNetworkNamespaceReconnectAttempts),
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010066 dbus_object_(std::move(dbus_object)),
Andreea Costinas922fbaf2020-05-28 11:55:22 +020067 weak_ptr_factory_(this) {
68 kerberos_client_ = std::make_unique<KerberosClient>(dbus_object_->GetBus());
69}
Andreea Costinas942284d2020-01-28 16:28:40 +010070
71SystemProxyAdaptor::~SystemProxyAdaptor() = default;
72
73void SystemProxyAdaptor::RegisterAsync(
74 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction&
75 completion_callback) {
76 RegisterWithDBusObject(dbus_object_.get());
77 dbus_object_->RegisterAsync(completion_callback);
78}
79
Andreea Costinas77b180e2020-05-12 15:17:32 +020080std::vector<uint8_t> SystemProxyAdaptor::SetAuthenticationDetails(
81 const std::vector<uint8_t>& request_blob) {
82 LOG(INFO) << "Received set authentication details request.";
83
84 SetAuthenticationDetailsRequest request;
85 const std::string error_message =
86 DeserializeProto(FROM_HERE, &request, request_blob);
87
88 SetAuthenticationDetailsResponse response;
89 if (!error_message.empty()) {
90 response.set_error_message(error_message);
91 return SerializeProto(response);
92 }
93
Andreea Costinas77b180e2020-05-12 15:17:32 +020094 if (request.traffic_type() != TrafficOrigin::SYSTEM) {
95 response.set_error_message(kOnlySystemTrafficSupportedError);
96 return SerializeProto(response);
97 }
98
99 if (!CreateWorkerIfNeeded(/* user_traffic */ false)) {
100 response.set_error_message(kFailedToStartWorkerError);
101 return SerializeProto(response);
102 }
103
104 if (request.has_credentials()) {
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200105 if (!((request.credentials().has_username() &&
106 request.credentials().has_password()) ||
107 request.has_protection_space())) {
Andreea Costinas77b180e2020-05-12 15:17:32 +0200108 response.set_error_message(kNoCredentialsSpecifiedError);
109 return SerializeProto(response);
110 }
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200111 worker::Credentials credentials;
112 if (request.has_protection_space()) {
113 worker::ProtectionSpace protection_space;
114 protection_space.set_origin(request.protection_space().origin());
115 protection_space.set_scheme(request.protection_space().scheme());
116 protection_space.set_realm(request.protection_space().realm());
117 *credentials.mutable_protection_space() = protection_space;
118 }
119 if (request.credentials().has_username()) {
120 credentials.set_username(request.credentials().username());
121 credentials.set_password(request.credentials().password());
122 }
Andreea Costinas77b180e2020-05-12 15:17:32 +0200123 brillo::MessageLoop::current()->PostTask(
124 FROM_HERE, base::Bind(&SystemProxyAdaptor::SetCredentialsTask,
125 weak_ptr_factory_.GetWeakPtr(),
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200126 system_services_worker_.get(), credentials));
Andreea Costinas77b180e2020-05-12 15:17:32 +0200127 }
128
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200129 if (request.has_kerberos_enabled()) {
130 std::string principal_name = request.has_active_principal_name()
131 ? request.active_principal_name()
132 : std::string();
133
134 brillo::MessageLoop::current()->PostTask(
135 FROM_HERE, base::Bind(&SystemProxyAdaptor::SetKerberosEnabledTask,
136 weak_ptr_factory_.GetWeakPtr(),
137 system_services_worker_.get(),
138 request.kerberos_enabled(), principal_name));
139 }
140
Andreea Costinas77b180e2020-05-12 15:17:32 +0200141 return SerializeProto(response);
142}
143
Andreea Costinasfc3dc7d2020-07-20 18:54:38 +0200144// TODO(acostinas, crbug.com/1109144): Deprecated in favor of |ShutDownProcess|.
Andreea Costinas942284d2020-01-28 16:28:40 +0100145std::vector<uint8_t> SystemProxyAdaptor::ShutDown() {
146 LOG(INFO) << "Received shutdown request.";
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100147
148 std::string error_message;
Andreea Costinase9c73592020-07-17 15:27:54 +0200149 if (!ResetWorker(/* user_traffic=*/false)) {
150 error_message =
151 "Failure to terminate worker process for system services traffic.";
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100152 }
153
Andreea Costinase9c73592020-07-17 15:27:54 +0200154 if (!ResetWorker(/* user_traffic=*/true)) {
155 error_message += "Failure to terminate worker process for arc traffic.";
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100156 }
157
Andreea Costinas942284d2020-01-28 16:28:40 +0100158 ShutDownResponse response;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100159 if (!error_message.empty())
160 response.set_error_message(error_message);
161
162 brillo::MessageLoop::current()->PostTask(
163 FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask,
164 weak_ptr_factory_.GetWeakPtr()));
165
Andreea Costinas942284d2020-01-28 16:28:40 +0100166 return SerializeProto(response);
167}
168
Andreea Costinase9c73592020-07-17 15:27:54 +0200169std::vector<uint8_t> SystemProxyAdaptor::ClearUserCredentials(
170 const std::vector<uint8_t>& request_blob) {
171 LOG(INFO) << "Received request to clear user credentials.";
172 std::string error_message;
173 ClearUserCredentials(/*user_traffic=*/false, &error_message);
174 ClearUserCredentials(/*user_traffic=*/true, &error_message);
175
176 ClearUserCredentialsResponse response;
177 if (!error_message.empty())
178 response.set_error_message(error_message);
179 return SerializeProto(response);
180}
181
182void SystemProxyAdaptor::ClearUserCredentials(bool user_traffic,
183 std::string* error_message) {
184 SandboxedWorker* worker = GetWorker(user_traffic);
185 if (!worker) {
186 return;
187 }
188 if (!worker->ClearUserCredentials()) {
189 error_message->append(
190 base::StringPrintf("Failure to clear user credentials for worker with "
191 "pid %s. Restarting worker.",
192 std::to_string(worker->pid()).c_str()));
193 ResetWorker(user_traffic);
194 CreateWorkerIfNeeded(user_traffic);
195 }
196}
197
Andreea Costinasfc3dc7d2020-07-20 18:54:38 +0200198std::vector<uint8_t> SystemProxyAdaptor::ShutDownProcess(
199 const std::vector<uint8_t>& request_blob) {
200 LOG(INFO) << "Received shutdown request.";
201 ShutDownRequest request;
202 std::string error_message =
203 DeserializeProto(FROM_HERE, &request, request_blob);
204
205 if (IncludesSystemTraffic(request.traffic_type()) &&
206 !ResetWorker(/* user_traffic=*/false)) {
207 error_message =
208 "Failure to terminate worker process for system services traffic.";
209 }
210
211 if (IncludesUserTraffic(request.traffic_type()) &&
212 !ResetWorker(/* user_traffic=*/true)) {
213 error_message += "Failure to terminate worker process for arc traffic.";
214 }
215
216 ShutDownResponse response;
217 if (!error_message.empty())
218 response.set_error_message(error_message);
219
220 if (request.traffic_type() == TrafficOrigin::ALL) {
221 brillo::MessageLoop::current()->PostTask(
222 FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask,
223 weak_ptr_factory_.GetWeakPtr()));
224 }
225 return SerializeProto(response);
226}
227
Andreea Costinas5862b102020-03-19 14:45:36 +0100228void SystemProxyAdaptor::GetChromeProxyServersAsync(
229 const std::string& target_url,
230 const brillo::http::GetChromeProxyServersCallback& callback) {
Andreea Costinasc9defae2020-04-22 10:28:35 +0200231 brillo::http::GetChromeProxyServersAsync(dbus_object_->GetBus(), target_url,
232 move(callback));
Andreea Costinas5862b102020-03-19 14:45:36 +0100233}
234
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100235std::unique_ptr<SandboxedWorker> SystemProxyAdaptor::CreateWorker() {
Andreea Costinas5862b102020-03-19 14:45:36 +0100236 return std::make_unique<SandboxedWorker>(weak_ptr_factory_.GetWeakPtr());
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100237}
238
Andreea Costinas77b180e2020-05-12 15:17:32 +0200239bool SystemProxyAdaptor::CreateWorkerIfNeeded(bool user_traffic) {
240 if (user_traffic) {
241 // Not supported at the moment.
242 return false;
243 }
244 if (system_services_worker_) {
245 return true;
246 }
247
248 system_services_worker_ = CreateWorker();
249 if (!StartWorker(system_services_worker_.get(),
250 /* user_traffic= */ false)) {
251 system_services_worker_.reset();
252 return false;
253 }
254 // patchpanel_proxy is owned by |dbus_object_->bus_|.
255 dbus::ObjectProxy* patchpanel_proxy = dbus_object_->GetBus()->GetObjectProxy(
256 patchpanel::kPatchPanelServiceName,
257 dbus::ObjectPath(patchpanel::kPatchPanelServicePath));
258 patchpanel_proxy->WaitForServiceToBeAvailable(
259 base::Bind(&SystemProxyAdaptor::OnPatchpanelServiceAvailable,
260 weak_ptr_factory_.GetWeakPtr()));
261 return true;
262}
263
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200264void SystemProxyAdaptor::SetCredentialsTask(
265 SandboxedWorker* worker, const worker::Credentials& credentials) {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100266 DCHECK(worker);
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200267 worker->SetCredentials(credentials);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100268}
269
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200270void SystemProxyAdaptor::SetKerberosEnabledTask(
271 SandboxedWorker* worker,
272 bool kerberos_enabled,
273 const std::string& principal_name) {
274 DCHECK(worker);
275
276 worker->SetKerberosEnabled(kerberos_enabled,
277 kerberos_client_->krb5_conf_path(),
278 kerberos_client_->krb5_ccache_path());
279 kerberos_client_->SetKerberosEnabled(kerberos_enabled);
280 if (kerberos_enabled) {
281 kerberos_client_->SetPrincipalName(principal_name);
282 }
283}
284
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100285void SystemProxyAdaptor::ShutDownTask() {
286 brillo::MessageLoop::current()->BreakLoop();
287}
288
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200289bool SystemProxyAdaptor::StartWorker(SandboxedWorker* worker,
290 bool user_traffic) {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100291 DCHECK(worker);
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200292 return worker->Start();
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100293}
294
Andreea Costinase9c73592020-07-17 15:27:54 +0200295bool SystemProxyAdaptor::ResetWorker(bool user_traffic) {
296 SandboxedWorker* worker =
297 user_traffic ? arc_worker_.get() : system_services_worker_.get();
298 if (!worker) {
299 return true;
300 }
301 if (!worker->Stop()) {
302 return false;
303 }
304 if (user_traffic) {
305 arc_worker_.reset();
306 } else {
307 system_services_worker_.reset();
308 }
309 return true;
310}
311
312SandboxedWorker* SystemProxyAdaptor::GetWorker(bool user_traffic) {
313 return user_traffic ? arc_worker_.get() : system_services_worker_.get();
314}
315
Andreea Costinasfc3dc7d2020-07-20 18:54:38 +0200316bool SystemProxyAdaptor::IncludesSystemTraffic(TrafficOrigin traffic_origin) {
317 return traffic_origin != TrafficOrigin::USER;
318}
319
320bool SystemProxyAdaptor::IncludesUserTraffic(TrafficOrigin traffic_origin) {
321 return traffic_origin != TrafficOrigin::SYSTEM;
322}
323
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200324// Called when the patchpanel D-Bus service becomes available.
325void SystemProxyAdaptor::OnPatchpanelServiceAvailable(bool is_available) {
326 if (!is_available) {
327 LOG(ERROR) << "Patchpanel service not available";
328 return;
329 }
330 if (system_services_worker_) {
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200331 ConnectNamespace(system_services_worker_.get(), /* user_traffic= */ false);
332 }
333}
334
Andreea Costinas91f75352020-07-08 14:47:47 +0200335void SystemProxyAdaptor::ConnectNamespace(SandboxedWorker* worker,
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200336 bool user_traffic) {
Andreea Costinase9c73592020-07-17 15:27:54 +0200337 DCHECK(worker->IsRunning());
Andreea Costinas91f75352020-07-08 14:47:47 +0200338 DCHECK_GT(netns_reconnect_attempts_available_, 0);
339 --netns_reconnect_attempts_available_;
340 // TODO(b/160736881, acostinas): Remove the delay after patchpanel
341 // implements "ip netns" to create the veth pair across network namespaces.
342 brillo::MessageLoop::current()->PostDelayedTask(
343 FROM_HERE,
344 base::Bind(&SystemProxyAdaptor::ConnectNamespaceTask,
Andreea Costinase9c73592020-07-17 15:27:54 +0200345 weak_ptr_factory_.GetWeakPtr(), worker, user_traffic),
Andreea Costinas91f75352020-07-08 14:47:47 +0200346 kConnectNamespaceDelay);
347}
348
349void SystemProxyAdaptor::ConnectNamespaceTask(SandboxedWorker* worker,
350 bool user_traffic) {
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200351 std::unique_ptr<patchpanel::Client> patchpanel_client =
352 patchpanel::Client::New();
353 if (!patchpanel_client) {
354 LOG(ERROR) << "Failed to open networking service client";
Andreea Costinas91f75352020-07-08 14:47:47 +0200355 return;
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200356 }
357
358 std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse> result =
359 patchpanel_client->ConnectNamespace(
360 worker->pid(), "" /* outbound_ifname */, user_traffic);
361
362 if (!result.first.is_valid()) {
Andreea Costinas91f75352020-07-08 14:47:47 +0200363 LOG(ERROR) << "Failed to setup network namespace on attempt "
364 << kNetworkNamespaceReconnectAttempts -
365 netns_reconnect_attempts_available_;
366 if (netns_reconnect_attempts_available_ > 0) {
367 ConnectNamespace(worker, user_traffic);
368 }
369 return;
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200370 }
371
372 worker->SetNetNamespaceLifelineFd(std::move(result.first));
Andreea Costinasa89309d2020-05-08 15:51:12 +0200373 if (!worker->SetListeningAddress(result.second.host_ipv4_address(),
374 kProxyPort)) {
Andreea Costinas91f75352020-07-08 14:47:47 +0200375 return;
Andreea Costinasa89309d2020-05-08 15:51:12 +0200376 }
377 OnNamespaceConnected(worker, user_traffic);
Andreea Costinasa89309d2020-05-08 15:51:12 +0200378}
379
380void SystemProxyAdaptor::OnNamespaceConnected(SandboxedWorker* worker,
381 bool user_traffic) {
382 WorkerActiveSignalDetails details;
383 details.set_traffic_origin(user_traffic ? TrafficOrigin::USER
384 : TrafficOrigin::SYSTEM);
385 details.set_local_proxy_url(worker->local_proxy_host_and_port());
386 SendWorkerActiveSignal(SerializeProto(details));
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100387}
388
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200389void SystemProxyAdaptor::RequestAuthenticationCredentials(
390 const worker::ProtectionSpace& protection_space) {
391 AuthenticationRequiredDetails details;
392 ProtectionSpace proxy_protection_space;
393 proxy_protection_space.set_origin(protection_space.origin());
394 proxy_protection_space.set_realm(protection_space.realm());
395 proxy_protection_space.set_scheme(protection_space.scheme());
396 *details.mutable_proxy_protection_space() = proxy_protection_space;
397 SendAuthenticationRequiredSignal(SerializeProto(details));
398}
399
Andreea Costinas942284d2020-01-28 16:28:40 +0100400} // namespace system_proxy