Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 1 | // 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 Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 12 | #include <brillo/message_loops/message_loop.h> |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 13 | |
| 14 | #include "system_proxy/proto_bindings/system_proxy_service.pb.h" |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 15 | #include "system-proxy/sandboxed_worker.h" |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 16 | |
| 17 | namespace system_proxy { |
| 18 | namespace { |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 19 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 20 | // Serializes |proto| to a vector of bytes. |
| 21 | std::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. |
| 30 | std::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 | |
| 42 | SystemProxyAdaptor::SystemProxyAdaptor( |
| 43 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object) |
| 44 | : org::chromium::SystemProxyAdaptor(this), |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 45 | dbus_object_(std::move(dbus_object)), |
| 46 | weak_ptr_factory_(this) {} |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 47 | |
| 48 | SystemProxyAdaptor::~SystemProxyAdaptor() = default; |
| 49 | |
| 50 | void 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 | |
| 57 | std::vector<uint8_t> SystemProxyAdaptor::SetSystemTrafficCredentials( |
| 58 | const std::vector<uint8_t>& request_blob) { |
| 59 | LOG(INFO) << "Received set credentials request."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 60 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 61 | SetSystemTrafficCredentialsRequest request; |
| 62 | const std::string error_message = |
| 63 | DeserializeProto(FROM_HERE, &request, request_blob); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 64 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 65 | SetSystemTrafficCredentialsResponse response; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 66 | if (!error_message.empty()) { |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 67 | response.set_error_message(error_message); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 68 | 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 Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 89 | return SerializeProto(response); |
| 90 | } |
| 91 | |
| 92 | std::vector<uint8_t> SystemProxyAdaptor::ShutDown() { |
| 93 | LOG(INFO) << "Received shutdown request."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 94 | |
| 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 Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 107 | ShutDownResponse response; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 108 | 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 Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 115 | return SerializeProto(response); |
| 116 | } |
| 117 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 118 | std::unique_ptr<SandboxedWorker> SystemProxyAdaptor::CreateWorker() { |
| 119 | return std::make_unique<SandboxedWorker>(); |
| 120 | } |
| 121 | |
| 122 | void 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 | |
| 130 | void SystemProxyAdaptor::ShutDownTask() { |
| 131 | brillo::MessageLoop::current()->BreakLoop(); |
| 132 | } |
| 133 | |
| 134 | void 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 | |
| 144 | void 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 | |
| 149 | void 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 Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 155 | } // namespace system_proxy |