blob: 41051308f1da8ccdfa5f97f44701634b2e1bef35 [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 Costinas91f75352020-07-08 14:47:47 +020011#include <base/time/time.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010012#include <brillo/dbus/dbus_object.h>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010013#include <brillo/message_loops/message_loop.h>
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020014#include <chromeos/dbus/service_constants.h>
15#include <chromeos/patchpanel/client.h>
16#include <dbus/object_proxy.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010017
Andreea Costinas922fbaf2020-05-28 11:55:22 +020018#include "system-proxy/kerberos_client.h"
Andreea Costinas942284d2020-01-28 16:28:40 +010019#include "system_proxy/proto_bindings/system_proxy_service.pb.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 Costinas942284d2020-01-28 16:28:40 +0100144std::vector<uint8_t> SystemProxyAdaptor::ShutDown() {
145 LOG(INFO) << "Received shutdown request.";
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100146
147 std::string error_message;
148 if (system_services_worker_ && system_services_worker_->IsRunning()) {
149 if (!system_services_worker_->Stop())
150 error_message =
151 "Failure to terminate worker process for system services traffic.";
152 }
153
154 if (arc_worker_ && arc_worker_->IsRunning()) {
155 if (!arc_worker_->Stop())
156 error_message += "Failure to terminate worker process for arc traffic.";
157 }
158
Andreea Costinas942284d2020-01-28 16:28:40 +0100159 ShutDownResponse response;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100160 if (!error_message.empty())
161 response.set_error_message(error_message);
162
163 brillo::MessageLoop::current()->PostTask(
164 FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask,
165 weak_ptr_factory_.GetWeakPtr()));
166
Andreea Costinas942284d2020-01-28 16:28:40 +0100167 return SerializeProto(response);
168}
169
Andreea Costinas5862b102020-03-19 14:45:36 +0100170void SystemProxyAdaptor::GetChromeProxyServersAsync(
171 const std::string& target_url,
172 const brillo::http::GetChromeProxyServersCallback& callback) {
Andreea Costinasc9defae2020-04-22 10:28:35 +0200173 brillo::http::GetChromeProxyServersAsync(dbus_object_->GetBus(), target_url,
174 move(callback));
Andreea Costinas5862b102020-03-19 14:45:36 +0100175}
176
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100177std::unique_ptr<SandboxedWorker> SystemProxyAdaptor::CreateWorker() {
Andreea Costinas5862b102020-03-19 14:45:36 +0100178 return std::make_unique<SandboxedWorker>(weak_ptr_factory_.GetWeakPtr());
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100179}
180
Andreea Costinas77b180e2020-05-12 15:17:32 +0200181bool SystemProxyAdaptor::CreateWorkerIfNeeded(bool user_traffic) {
182 if (user_traffic) {
183 // Not supported at the moment.
184 return false;
185 }
186 if (system_services_worker_) {
187 return true;
188 }
189
190 system_services_worker_ = CreateWorker();
191 if (!StartWorker(system_services_worker_.get(),
192 /* user_traffic= */ false)) {
193 system_services_worker_.reset();
194 return false;
195 }
196 // patchpanel_proxy is owned by |dbus_object_->bus_|.
197 dbus::ObjectProxy* patchpanel_proxy = dbus_object_->GetBus()->GetObjectProxy(
198 patchpanel::kPatchPanelServiceName,
199 dbus::ObjectPath(patchpanel::kPatchPanelServicePath));
200 patchpanel_proxy->WaitForServiceToBeAvailable(
201 base::Bind(&SystemProxyAdaptor::OnPatchpanelServiceAvailable,
202 weak_ptr_factory_.GetWeakPtr()));
203 return true;
204}
205
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200206void SystemProxyAdaptor::SetCredentialsTask(
207 SandboxedWorker* worker, const worker::Credentials& credentials) {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100208 DCHECK(worker);
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200209 worker->SetCredentials(credentials);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100210}
211
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200212void SystemProxyAdaptor::SetKerberosEnabledTask(
213 SandboxedWorker* worker,
214 bool kerberos_enabled,
215 const std::string& principal_name) {
216 DCHECK(worker);
217
218 worker->SetKerberosEnabled(kerberos_enabled,
219 kerberos_client_->krb5_conf_path(),
220 kerberos_client_->krb5_ccache_path());
221 kerberos_client_->SetKerberosEnabled(kerberos_enabled);
222 if (kerberos_enabled) {
223 kerberos_client_->SetPrincipalName(principal_name);
224 }
225}
226
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100227void SystemProxyAdaptor::ShutDownTask() {
228 brillo::MessageLoop::current()->BreakLoop();
229}
230
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200231bool SystemProxyAdaptor::StartWorker(SandboxedWorker* worker,
232 bool user_traffic) {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100233 DCHECK(worker);
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200234 return worker->Start();
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100235}
236
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200237// Called when the patchpanel D-Bus service becomes available.
238void SystemProxyAdaptor::OnPatchpanelServiceAvailable(bool is_available) {
239 if (!is_available) {
240 LOG(ERROR) << "Patchpanel service not available";
241 return;
242 }
243 if (system_services_worker_) {
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200244 ConnectNamespace(system_services_worker_.get(), /* user_traffic= */ false);
245 }
246}
247
Andreea Costinas91f75352020-07-08 14:47:47 +0200248void SystemProxyAdaptor::ConnectNamespace(SandboxedWorker* worker,
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200249 bool user_traffic) {
Andreea Costinas91f75352020-07-08 14:47:47 +0200250 DCHECK(system_services_worker_->IsRunning());
251 DCHECK_GT(netns_reconnect_attempts_available_, 0);
252 --netns_reconnect_attempts_available_;
253 // TODO(b/160736881, acostinas): Remove the delay after patchpanel
254 // implements "ip netns" to create the veth pair across network namespaces.
255 brillo::MessageLoop::current()->PostDelayedTask(
256 FROM_HERE,
257 base::Bind(&SystemProxyAdaptor::ConnectNamespaceTask,
258 weak_ptr_factory_.GetWeakPtr(), worker,
259 /* user_traffic= */ false),
260 kConnectNamespaceDelay);
261}
262
263void SystemProxyAdaptor::ConnectNamespaceTask(SandboxedWorker* worker,
264 bool user_traffic) {
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200265 std::unique_ptr<patchpanel::Client> patchpanel_client =
266 patchpanel::Client::New();
267 if (!patchpanel_client) {
268 LOG(ERROR) << "Failed to open networking service client";
Andreea Costinas91f75352020-07-08 14:47:47 +0200269 return;
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200270 }
271
272 std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse> result =
273 patchpanel_client->ConnectNamespace(
274 worker->pid(), "" /* outbound_ifname */, user_traffic);
275
276 if (!result.first.is_valid()) {
Andreea Costinas91f75352020-07-08 14:47:47 +0200277 LOG(ERROR) << "Failed to setup network namespace on attempt "
278 << kNetworkNamespaceReconnectAttempts -
279 netns_reconnect_attempts_available_;
280 if (netns_reconnect_attempts_available_ > 0) {
281 ConnectNamespace(worker, user_traffic);
282 }
283 return;
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200284 }
285
286 worker->SetNetNamespaceLifelineFd(std::move(result.first));
Andreea Costinasa89309d2020-05-08 15:51:12 +0200287 if (!worker->SetListeningAddress(result.second.host_ipv4_address(),
288 kProxyPort)) {
Andreea Costinas91f75352020-07-08 14:47:47 +0200289 return;
Andreea Costinasa89309d2020-05-08 15:51:12 +0200290 }
291 OnNamespaceConnected(worker, user_traffic);
Andreea Costinasa89309d2020-05-08 15:51:12 +0200292}
293
294void SystemProxyAdaptor::OnNamespaceConnected(SandboxedWorker* worker,
295 bool user_traffic) {
296 WorkerActiveSignalDetails details;
297 details.set_traffic_origin(user_traffic ? TrafficOrigin::USER
298 : TrafficOrigin::SYSTEM);
299 details.set_local_proxy_url(worker->local_proxy_host_and_port());
300 SendWorkerActiveSignal(SerializeProto(details));
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100301}
302
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200303void SystemProxyAdaptor::RequestAuthenticationCredentials(
304 const worker::ProtectionSpace& protection_space) {
305 AuthenticationRequiredDetails details;
306 ProtectionSpace proxy_protection_space;
307 proxy_protection_space.set_origin(protection_space.origin());
308 proxy_protection_space.set_realm(protection_space.realm());
309 proxy_protection_space.set_scheme(protection_space.scheme());
310 *details.mutable_proxy_protection_space() = proxy_protection_space;
311 SendAuthenticationRequiredSignal(SerializeProto(details));
312}
313
Andreea Costinas942284d2020-01-28 16:28:40 +0100314} // namespace system_proxy