blob: e24cf6f948311179fa4e181103b854d7bf7a0ad3 [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>
11#include <brillo/dbus/dbus_object.h>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010012#include <brillo/message_loops/message_loop.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010013
14#include "system_proxy/proto_bindings/system_proxy_service.pb.h"
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010015#include "system-proxy/sandboxed_worker.h"
Andreea Costinas942284d2020-01-28 16:28:40 +010016
17namespace system_proxy {
18namespace {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010019
Andreea Costinas942284d2020-01-28 16:28:40 +010020// Serializes |proto| to a vector of bytes.
21std::vector<uint8_t> SerializeProto(
22 const google::protobuf::MessageLite& proto) {
23 std::vector<uint8_t> proto_blob(proto.ByteSizeLong());
24 DCHECK(proto.SerializeToArray(proto_blob.data(), proto_blob.size()));
25 return proto_blob;
26}
27
28// Parses a proto from an array of bytes |proto_blob|. Returns
29// ERROR_PARSE_REQUEST_FAILED on error.
30std::string DeserializeProto(const base::Location& from_here,
31 google::protobuf::MessageLite* proto,
32 const std::vector<uint8_t>& proto_blob) {
33 if (!proto->ParseFromArray(proto_blob.data(), proto_blob.size())) {
34 const std::string error_message = "Failed to parse proto message.";
35 LOG(ERROR) << from_here.ToString() << error_message;
36 return error_message;
37 }
38 return "";
39}
40} // namespace
41
42SystemProxyAdaptor::SystemProxyAdaptor(
43 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object)
44 : org::chromium::SystemProxyAdaptor(this),
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010045 dbus_object_(std::move(dbus_object)),
46 weak_ptr_factory_(this) {}
Andreea Costinas942284d2020-01-28 16:28:40 +010047
48SystemProxyAdaptor::~SystemProxyAdaptor() = default;
49
50void SystemProxyAdaptor::RegisterAsync(
51 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction&
52 completion_callback) {
53 RegisterWithDBusObject(dbus_object_.get());
54 dbus_object_->RegisterAsync(completion_callback);
55}
56
57std::vector<uint8_t> SystemProxyAdaptor::SetSystemTrafficCredentials(
58 const std::vector<uint8_t>& request_blob) {
59 LOG(INFO) << "Received set credentials request.";
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010060
Andreea Costinas942284d2020-01-28 16:28:40 +010061 SetSystemTrafficCredentialsRequest request;
62 const std::string error_message =
63 DeserializeProto(FROM_HERE, &request, request_blob);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010064
Andreea Costinas942284d2020-01-28 16:28:40 +010065 SetSystemTrafficCredentialsResponse response;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010066 if (!error_message.empty()) {
Andreea Costinas942284d2020-01-28 16:28:40 +010067 response.set_error_message(error_message);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010068 return SerializeProto(response);
69 }
70
71 if (!request.has_system_services_username() ||
72 !request.has_system_services_password()) {
73 response.set_error_message("No credentials specified");
74 return SerializeProto(response);
75 }
76
77 if (!system_services_worker_) {
78 system_services_worker_ = CreateWorker();
79 StartWorker(system_services_worker_.get());
80 }
81
82 brillo::MessageLoop::current()->PostTask(
83 FROM_HERE,
84 base::Bind(&SystemProxyAdaptor::SetCredentialsTask,
85 weak_ptr_factory_.GetWeakPtr(), system_services_worker_.get(),
86 request.system_services_username(),
87 request.system_services_password()));
88
Andreea Costinas942284d2020-01-28 16:28:40 +010089 return SerializeProto(response);
90}
91
92std::vector<uint8_t> SystemProxyAdaptor::ShutDown() {
93 LOG(INFO) << "Received shutdown request.";
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010094
95 std::string error_message;
96 if (system_services_worker_ && system_services_worker_->IsRunning()) {
97 if (!system_services_worker_->Stop())
98 error_message =
99 "Failure to terminate worker process for system services traffic.";
100 }
101
102 if (arc_worker_ && arc_worker_->IsRunning()) {
103 if (!arc_worker_->Stop())
104 error_message += "Failure to terminate worker process for arc traffic.";
105 }
106
Andreea Costinas942284d2020-01-28 16:28:40 +0100107 ShutDownResponse response;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100108 if (!error_message.empty())
109 response.set_error_message(error_message);
110
111 brillo::MessageLoop::current()->PostTask(
112 FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask,
113 weak_ptr_factory_.GetWeakPtr()));
114
Andreea Costinas942284d2020-01-28 16:28:40 +0100115 return SerializeProto(response);
116}
117
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100118std::unique_ptr<SandboxedWorker> SystemProxyAdaptor::CreateWorker() {
119 return std::make_unique<SandboxedWorker>();
120}
121
122void SystemProxyAdaptor::SetCredentialsTask(SandboxedWorker* worker,
123 const std::string& username,
124 const std::string& password) {
125 DCHECK(worker);
126 // TODO(acostinas,chromium/1042626) Forward the credentials to the worker
127 // process.
128}
129
130void SystemProxyAdaptor::ShutDownTask() {
131 brillo::MessageLoop::current()->BreakLoop();
132}
133
134void SystemProxyAdaptor::StartWorker(SandboxedWorker* worker) {
135 DCHECK(worker);
136 worker->Start();
137 if (!worker->IsRunning()) {
138 LOG(ERROR) << "Failed to start worker process";
139 return;
140 }
141 ConnectNamespace(worker);
142}
143
144void SystemProxyAdaptor::ConnectNamespace(SandboxedWorker* worker) {
145 // TODO(acostinas,b/147712924) Call the datapath service to setup routing and
146 // create a veth pair for the network namespace.
147}
148
149void SystemProxyAdaptor::OnConnectNamespace(
150 SandboxedWorker* worker, const patchpanel::IPv4Subnet& ipv4_subnet) {
151 // TODO(acostinas,chromium/1042626) Forward the ipv4 subnet to the worker
152 // process.
153}
154
Andreea Costinas942284d2020-01-28 16:28:40 +0100155} // namespace system_proxy