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 | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 20 | constexpr int kProxyPort = 3128; |
| 21 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 22 | // Serializes |proto| to a vector of bytes. |
| 23 | std::vector<uint8_t> SerializeProto( |
| 24 | const google::protobuf::MessageLite& proto) { |
| 25 | std::vector<uint8_t> proto_blob(proto.ByteSizeLong()); |
| 26 | DCHECK(proto.SerializeToArray(proto_blob.data(), proto_blob.size())); |
| 27 | return proto_blob; |
| 28 | } |
| 29 | |
| 30 | // Parses a proto from an array of bytes |proto_blob|. Returns |
| 31 | // ERROR_PARSE_REQUEST_FAILED on error. |
| 32 | std::string DeserializeProto(const base::Location& from_here, |
| 33 | google::protobuf::MessageLite* proto, |
| 34 | const std::vector<uint8_t>& proto_blob) { |
| 35 | if (!proto->ParseFromArray(proto_blob.data(), proto_blob.size())) { |
| 36 | const std::string error_message = "Failed to parse proto message."; |
| 37 | LOG(ERROR) << from_here.ToString() << error_message; |
| 38 | return error_message; |
| 39 | } |
| 40 | return ""; |
| 41 | } |
| 42 | } // namespace |
| 43 | |
| 44 | SystemProxyAdaptor::SystemProxyAdaptor( |
| 45 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object) |
| 46 | : org::chromium::SystemProxyAdaptor(this), |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 47 | dbus_object_(std::move(dbus_object)), |
| 48 | weak_ptr_factory_(this) {} |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 49 | |
| 50 | SystemProxyAdaptor::~SystemProxyAdaptor() = default; |
| 51 | |
| 52 | void SystemProxyAdaptor::RegisterAsync( |
| 53 | const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& |
| 54 | completion_callback) { |
| 55 | RegisterWithDBusObject(dbus_object_.get()); |
| 56 | dbus_object_->RegisterAsync(completion_callback); |
| 57 | } |
| 58 | |
| 59 | std::vector<uint8_t> SystemProxyAdaptor::SetSystemTrafficCredentials( |
| 60 | const std::vector<uint8_t>& request_blob) { |
| 61 | LOG(INFO) << "Received set credentials request."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 62 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 63 | SetSystemTrafficCredentialsRequest request; |
| 64 | const std::string error_message = |
| 65 | DeserializeProto(FROM_HERE, &request, request_blob); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 66 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 67 | SetSystemTrafficCredentialsResponse response; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 68 | if (!error_message.empty()) { |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 69 | response.set_error_message(error_message); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 70 | return SerializeProto(response); |
| 71 | } |
| 72 | |
| 73 | if (!request.has_system_services_username() || |
| 74 | !request.has_system_services_password()) { |
| 75 | response.set_error_message("No credentials specified"); |
| 76 | return SerializeProto(response); |
| 77 | } |
| 78 | |
| 79 | if (!system_services_worker_) { |
| 80 | system_services_worker_ = CreateWorker(); |
| 81 | StartWorker(system_services_worker_.get()); |
| 82 | } |
| 83 | |
| 84 | brillo::MessageLoop::current()->PostTask( |
| 85 | FROM_HERE, |
| 86 | base::Bind(&SystemProxyAdaptor::SetCredentialsTask, |
| 87 | weak_ptr_factory_.GetWeakPtr(), system_services_worker_.get(), |
| 88 | request.system_services_username(), |
| 89 | request.system_services_password())); |
| 90 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 91 | return SerializeProto(response); |
| 92 | } |
| 93 | |
| 94 | std::vector<uint8_t> SystemProxyAdaptor::ShutDown() { |
| 95 | LOG(INFO) << "Received shutdown request."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 96 | |
| 97 | std::string error_message; |
| 98 | if (system_services_worker_ && system_services_worker_->IsRunning()) { |
| 99 | if (!system_services_worker_->Stop()) |
| 100 | error_message = |
| 101 | "Failure to terminate worker process for system services traffic."; |
| 102 | } |
| 103 | |
| 104 | if (arc_worker_ && arc_worker_->IsRunning()) { |
| 105 | if (!arc_worker_->Stop()) |
| 106 | error_message += "Failure to terminate worker process for arc traffic."; |
| 107 | } |
| 108 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 109 | ShutDownResponse response; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 110 | if (!error_message.empty()) |
| 111 | response.set_error_message(error_message); |
| 112 | |
| 113 | brillo::MessageLoop::current()->PostTask( |
| 114 | FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask, |
| 115 | weak_ptr_factory_.GetWeakPtr())); |
| 116 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 117 | return SerializeProto(response); |
| 118 | } |
| 119 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 120 | std::unique_ptr<SandboxedWorker> SystemProxyAdaptor::CreateWorker() { |
| 121 | return std::make_unique<SandboxedWorker>(); |
| 122 | } |
| 123 | |
| 124 | void SystemProxyAdaptor::SetCredentialsTask(SandboxedWorker* worker, |
| 125 | const std::string& username, |
| 126 | const std::string& password) { |
| 127 | DCHECK(worker); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 128 | worker->SetUsernameAndPassword(username, password); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void SystemProxyAdaptor::ShutDownTask() { |
| 132 | brillo::MessageLoop::current()->BreakLoop(); |
| 133 | } |
| 134 | |
| 135 | void SystemProxyAdaptor::StartWorker(SandboxedWorker* worker) { |
| 136 | DCHECK(worker); |
| 137 | worker->Start(); |
| 138 | if (!worker->IsRunning()) { |
| 139 | LOG(ERROR) << "Failed to start worker process"; |
| 140 | return; |
| 141 | } |
| 142 | ConnectNamespace(worker); |
| 143 | } |
| 144 | |
| 145 | void SystemProxyAdaptor::ConnectNamespace(SandboxedWorker* worker) { |
| 146 | // TODO(acostinas,b/147712924) Call the datapath service to setup routing and |
| 147 | // create a veth pair for the network namespace. |
| 148 | } |
| 149 | |
| 150 | void SystemProxyAdaptor::OnConnectNamespace( |
| 151 | SandboxedWorker* worker, const patchpanel::IPv4Subnet& ipv4_subnet) { |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame^] | 152 | worker->SetListeningAddress(ipv4_subnet.base_addr(), kProxyPort); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 155 | } // namespace system_proxy |