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 | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
| 14 | #include <chromeos/patchpanel/client.h> |
| 15 | #include <dbus/object_proxy.h> |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 16 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 17 | #include "system-proxy/kerberos_client.h" |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 18 | #include "system_proxy/proto_bindings/system_proxy_service.pb.h" |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 19 | #include "system-proxy/sandboxed_worker.h" |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 20 | |
| 21 | namespace system_proxy { |
| 22 | namespace { |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 23 | |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 24 | constexpr int kProxyPort = 3128; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 25 | constexpr char kNoCredentialsSpecifiedError[] = |
| 26 | "No authentication credentials specified"; |
| 27 | constexpr char kOnlySystemTrafficSupportedError[] = |
| 28 | "Only system services traffic is currenly supported"; |
| 29 | constexpr char kFailedToStartWorkerError[] = "Failed to start worker process"; |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 30 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 31 | // Serializes |proto| to a vector of bytes. |
| 32 | std::vector<uint8_t> SerializeProto( |
| 33 | const google::protobuf::MessageLite& proto) { |
| 34 | std::vector<uint8_t> proto_blob(proto.ByteSizeLong()); |
Andreea Costinas | c991e23 | 2020-06-08 20:30:58 +0200 | [diff] [blame] | 35 | bool result = proto.SerializeToArray(proto_blob.data(), proto_blob.size()); |
| 36 | DCHECK(result); |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 37 | return proto_blob; |
| 38 | } |
| 39 | |
| 40 | // Parses a proto from an array of bytes |proto_blob|. Returns |
| 41 | // ERROR_PARSE_REQUEST_FAILED on error. |
| 42 | std::string DeserializeProto(const base::Location& from_here, |
| 43 | google::protobuf::MessageLite* proto, |
| 44 | const std::vector<uint8_t>& proto_blob) { |
| 45 | if (!proto->ParseFromArray(proto_blob.data(), proto_blob.size())) { |
| 46 | const std::string error_message = "Failed to parse proto message."; |
| 47 | LOG(ERROR) << from_here.ToString() << error_message; |
| 48 | return error_message; |
| 49 | } |
| 50 | return ""; |
| 51 | } |
| 52 | } // namespace |
| 53 | |
| 54 | SystemProxyAdaptor::SystemProxyAdaptor( |
| 55 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object) |
| 56 | : org::chromium::SystemProxyAdaptor(this), |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 57 | dbus_object_(std::move(dbus_object)), |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 58 | weak_ptr_factory_(this) { |
| 59 | kerberos_client_ = std::make_unique<KerberosClient>(dbus_object_->GetBus()); |
| 60 | } |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 61 | |
| 62 | SystemProxyAdaptor::~SystemProxyAdaptor() = default; |
| 63 | |
| 64 | void SystemProxyAdaptor::RegisterAsync( |
| 65 | const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& |
| 66 | completion_callback) { |
| 67 | RegisterWithDBusObject(dbus_object_.get()); |
| 68 | dbus_object_->RegisterAsync(completion_callback); |
| 69 | } |
| 70 | |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 71 | std::vector<uint8_t> SystemProxyAdaptor::SetAuthenticationDetails( |
| 72 | const std::vector<uint8_t>& request_blob) { |
| 73 | LOG(INFO) << "Received set authentication details request."; |
| 74 | |
| 75 | SetAuthenticationDetailsRequest request; |
| 76 | const std::string error_message = |
| 77 | DeserializeProto(FROM_HERE, &request, request_blob); |
| 78 | |
| 79 | SetAuthenticationDetailsResponse response; |
| 80 | if (!error_message.empty()) { |
| 81 | response.set_error_message(error_message); |
| 82 | return SerializeProto(response); |
| 83 | } |
| 84 | |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 85 | if (request.traffic_type() != TrafficOrigin::SYSTEM) { |
| 86 | response.set_error_message(kOnlySystemTrafficSupportedError); |
| 87 | return SerializeProto(response); |
| 88 | } |
| 89 | |
| 90 | if (!CreateWorkerIfNeeded(/* user_traffic */ false)) { |
| 91 | response.set_error_message(kFailedToStartWorkerError); |
| 92 | return SerializeProto(response); |
| 93 | } |
| 94 | |
| 95 | if (request.has_credentials()) { |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 96 | if (!((request.credentials().has_username() && |
| 97 | request.credentials().has_password()) || |
| 98 | request.has_protection_space())) { |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 99 | response.set_error_message(kNoCredentialsSpecifiedError); |
| 100 | return SerializeProto(response); |
| 101 | } |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 102 | worker::Credentials credentials; |
| 103 | if (request.has_protection_space()) { |
| 104 | worker::ProtectionSpace protection_space; |
| 105 | protection_space.set_origin(request.protection_space().origin()); |
| 106 | protection_space.set_scheme(request.protection_space().scheme()); |
| 107 | protection_space.set_realm(request.protection_space().realm()); |
| 108 | *credentials.mutable_protection_space() = protection_space; |
| 109 | } |
| 110 | if (request.credentials().has_username()) { |
| 111 | credentials.set_username(request.credentials().username()); |
| 112 | credentials.set_password(request.credentials().password()); |
| 113 | } |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 114 | brillo::MessageLoop::current()->PostTask( |
| 115 | FROM_HERE, base::Bind(&SystemProxyAdaptor::SetCredentialsTask, |
| 116 | weak_ptr_factory_.GetWeakPtr(), |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 117 | system_services_worker_.get(), credentials)); |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 120 | if (request.has_kerberos_enabled()) { |
| 121 | std::string principal_name = request.has_active_principal_name() |
| 122 | ? request.active_principal_name() |
| 123 | : std::string(); |
| 124 | |
| 125 | brillo::MessageLoop::current()->PostTask( |
| 126 | FROM_HERE, base::Bind(&SystemProxyAdaptor::SetKerberosEnabledTask, |
| 127 | weak_ptr_factory_.GetWeakPtr(), |
| 128 | system_services_worker_.get(), |
| 129 | request.kerberos_enabled(), principal_name)); |
| 130 | } |
| 131 | |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 132 | return SerializeProto(response); |
| 133 | } |
| 134 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 135 | std::vector<uint8_t> SystemProxyAdaptor::SetSystemTrafficCredentials( |
| 136 | const std::vector<uint8_t>& request_blob) { |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 137 | SetSystemTrafficCredentialsResponse response; |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 138 | response.set_error_message("Deprecated. Please use SetAuthenticationDetails"); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 139 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 140 | return SerializeProto(response); |
| 141 | } |
| 142 | |
| 143 | std::vector<uint8_t> SystemProxyAdaptor::ShutDown() { |
| 144 | LOG(INFO) << "Received shutdown request."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 145 | |
| 146 | std::string error_message; |
| 147 | if (system_services_worker_ && system_services_worker_->IsRunning()) { |
| 148 | if (!system_services_worker_->Stop()) |
| 149 | error_message = |
| 150 | "Failure to terminate worker process for system services traffic."; |
| 151 | } |
| 152 | |
| 153 | if (arc_worker_ && arc_worker_->IsRunning()) { |
| 154 | if (!arc_worker_->Stop()) |
| 155 | error_message += "Failure to terminate worker process for arc traffic."; |
| 156 | } |
| 157 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 158 | ShutDownResponse response; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 159 | 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 Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 166 | return SerializeProto(response); |
| 167 | } |
| 168 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 169 | void SystemProxyAdaptor::GetChromeProxyServersAsync( |
| 170 | const std::string& target_url, |
| 171 | const brillo::http::GetChromeProxyServersCallback& callback) { |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 172 | brillo::http::GetChromeProxyServersAsync(dbus_object_->GetBus(), target_url, |
| 173 | move(callback)); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 176 | std::unique_ptr<SandboxedWorker> SystemProxyAdaptor::CreateWorker() { |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 177 | return std::make_unique<SandboxedWorker>(weak_ptr_factory_.GetWeakPtr()); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 180 | bool SystemProxyAdaptor::CreateWorkerIfNeeded(bool user_traffic) { |
| 181 | if (user_traffic) { |
| 182 | // Not supported at the moment. |
| 183 | return false; |
| 184 | } |
| 185 | if (system_services_worker_) { |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | system_services_worker_ = CreateWorker(); |
| 190 | if (!StartWorker(system_services_worker_.get(), |
| 191 | /* user_traffic= */ false)) { |
| 192 | system_services_worker_.reset(); |
| 193 | return false; |
| 194 | } |
| 195 | // patchpanel_proxy is owned by |dbus_object_->bus_|. |
| 196 | dbus::ObjectProxy* patchpanel_proxy = dbus_object_->GetBus()->GetObjectProxy( |
| 197 | patchpanel::kPatchPanelServiceName, |
| 198 | dbus::ObjectPath(patchpanel::kPatchPanelServicePath)); |
| 199 | patchpanel_proxy->WaitForServiceToBeAvailable( |
| 200 | base::Bind(&SystemProxyAdaptor::OnPatchpanelServiceAvailable, |
| 201 | weak_ptr_factory_.GetWeakPtr())); |
| 202 | return true; |
| 203 | } |
| 204 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 205 | void SystemProxyAdaptor::SetCredentialsTask( |
| 206 | SandboxedWorker* worker, const worker::Credentials& credentials) { |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 207 | DCHECK(worker); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 208 | worker->SetCredentials(credentials); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 211 | void SystemProxyAdaptor::SetKerberosEnabledTask( |
| 212 | SandboxedWorker* worker, |
| 213 | bool kerberos_enabled, |
| 214 | const std::string& principal_name) { |
| 215 | DCHECK(worker); |
| 216 | |
| 217 | worker->SetKerberosEnabled(kerberos_enabled, |
| 218 | kerberos_client_->krb5_conf_path(), |
| 219 | kerberos_client_->krb5_ccache_path()); |
| 220 | kerberos_client_->SetKerberosEnabled(kerberos_enabled); |
| 221 | if (kerberos_enabled) { |
| 222 | kerberos_client_->SetPrincipalName(principal_name); |
| 223 | } |
| 224 | } |
| 225 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 226 | void SystemProxyAdaptor::ShutDownTask() { |
| 227 | brillo::MessageLoop::current()->BreakLoop(); |
| 228 | } |
| 229 | |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 230 | bool SystemProxyAdaptor::StartWorker(SandboxedWorker* worker, |
| 231 | bool user_traffic) { |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 232 | DCHECK(worker); |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 233 | return worker->Start(); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 236 | // Called when the patchpanel D-Bus service becomes available. |
| 237 | void SystemProxyAdaptor::OnPatchpanelServiceAvailable(bool is_available) { |
| 238 | if (!is_available) { |
| 239 | LOG(ERROR) << "Patchpanel service not available"; |
| 240 | return; |
| 241 | } |
| 242 | if (system_services_worker_) { |
| 243 | DCHECK(system_services_worker_->IsRunning()); |
| 244 | ConnectNamespace(system_services_worker_.get(), /* user_traffic= */ false); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | bool SystemProxyAdaptor::ConnectNamespace(SandboxedWorker* worker, |
| 249 | bool user_traffic) { |
| 250 | std::unique_ptr<patchpanel::Client> patchpanel_client = |
| 251 | patchpanel::Client::New(); |
| 252 | if (!patchpanel_client) { |
| 253 | LOG(ERROR) << "Failed to open networking service client"; |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse> result = |
| 258 | patchpanel_client->ConnectNamespace( |
| 259 | worker->pid(), "" /* outbound_ifname */, user_traffic); |
| 260 | |
| 261 | if (!result.first.is_valid()) { |
| 262 | LOG(ERROR) << "Failed to setup network namespace"; |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | worker->SetNetNamespaceLifelineFd(std::move(result.first)); |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 267 | if (!worker->SetListeningAddress(result.second.host_ipv4_address(), |
| 268 | kProxyPort)) { |
| 269 | return false; |
| 270 | } |
| 271 | OnNamespaceConnected(worker, user_traffic); |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | void SystemProxyAdaptor::OnNamespaceConnected(SandboxedWorker* worker, |
| 276 | bool user_traffic) { |
| 277 | WorkerActiveSignalDetails details; |
| 278 | details.set_traffic_origin(user_traffic ? TrafficOrigin::USER |
| 279 | : TrafficOrigin::SYSTEM); |
| 280 | details.set_local_proxy_url(worker->local_proxy_host_and_port()); |
| 281 | SendWorkerActiveSignal(SerializeProto(details)); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 284 | void SystemProxyAdaptor::RequestAuthenticationCredentials( |
| 285 | const worker::ProtectionSpace& protection_space) { |
| 286 | AuthenticationRequiredDetails details; |
| 287 | ProtectionSpace proxy_protection_space; |
| 288 | proxy_protection_space.set_origin(protection_space.origin()); |
| 289 | proxy_protection_space.set_realm(protection_space.realm()); |
| 290 | proxy_protection_space.set_scheme(protection_space.scheme()); |
| 291 | *details.mutable_proxy_protection_space() = proxy_protection_space; |
| 292 | SendAuthenticationRequiredSignal(SerializeProto(details)); |
| 293 | } |
| 294 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 295 | } // namespace system_proxy |