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> |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 11 | #include <base/strings/stringprintf.h> |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 12 | #include <base/time/time.h> |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 13 | #include <brillo/dbus/dbus_object.h> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 14 | #include <brillo/message_loops/message_loop.h> |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 15 | #include <chromeos/dbus/service_constants.h> |
Jason Jeremy Iman | adffbcb | 2020-08-31 13:21:36 +0900 | [diff] [blame] | 16 | #include <chromeos/patchpanel/dbus/client.h> |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 17 | #include <dbus/object_proxy.h> |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 18 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 19 | #include "system-proxy/kerberos_client.h" |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 20 | #include "system-proxy/sandboxed_worker.h" |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 21 | |
| 22 | namespace system_proxy { |
| 23 | namespace { |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 24 | |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 25 | constexpr int kProxyPort = 3128; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 26 | constexpr char kFailedToStartWorkerError[] = "Failed to start worker process"; |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 27 | // Time delay for calling patchpanel::ConnectNamespace(). Patchpanel needs to |
| 28 | // enter the network namespace of the worker process to configure it and fails |
| 29 | // if it's soon after the process starts. See https://crbug.com/1095170 for |
| 30 | // details. |
| 31 | constexpr base::TimeDelta kConnectNamespaceDelay = |
| 32 | base::TimeDelta::FromSeconds(1); |
| 33 | constexpr int kNetworkNamespaceReconnectAttempts = 3; |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 34 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 35 | // Serializes |proto| to a vector of bytes. |
| 36 | std::vector<uint8_t> SerializeProto( |
| 37 | const google::protobuf::MessageLite& proto) { |
| 38 | std::vector<uint8_t> proto_blob(proto.ByteSizeLong()); |
Andreea Costinas | c991e23 | 2020-06-08 20:30:58 +0200 | [diff] [blame] | 39 | bool result = proto.SerializeToArray(proto_blob.data(), proto_blob.size()); |
| 40 | DCHECK(result); |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 41 | return proto_blob; |
| 42 | } |
| 43 | |
| 44 | // Parses a proto from an array of bytes |proto_blob|. Returns |
| 45 | // ERROR_PARSE_REQUEST_FAILED on error. |
| 46 | std::string DeserializeProto(const base::Location& from_here, |
| 47 | google::protobuf::MessageLite* proto, |
| 48 | const std::vector<uint8_t>& proto_blob) { |
| 49 | if (!proto->ParseFromArray(proto_blob.data(), proto_blob.size())) { |
| 50 | const std::string error_message = "Failed to parse proto message."; |
| 51 | LOG(ERROR) << from_here.ToString() << error_message; |
| 52 | return error_message; |
| 53 | } |
| 54 | return ""; |
| 55 | } |
| 56 | } // namespace |
| 57 | |
| 58 | SystemProxyAdaptor::SystemProxyAdaptor( |
| 59 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object) |
| 60 | : org::chromium::SystemProxyAdaptor(this), |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 61 | netns_reconnect_attempts_available_(kNetworkNamespaceReconnectAttempts), |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 62 | dbus_object_(std::move(dbus_object)), |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 63 | weak_ptr_factory_(this) { |
| 64 | kerberos_client_ = std::make_unique<KerberosClient>(dbus_object_->GetBus()); |
| 65 | } |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 66 | |
| 67 | SystemProxyAdaptor::~SystemProxyAdaptor() = default; |
| 68 | |
| 69 | void SystemProxyAdaptor::RegisterAsync( |
| 70 | const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& |
| 71 | completion_callback) { |
| 72 | RegisterWithDBusObject(dbus_object_.get()); |
| 73 | dbus_object_->RegisterAsync(completion_callback); |
| 74 | } |
| 75 | |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 76 | std::vector<uint8_t> SystemProxyAdaptor::SetAuthenticationDetails( |
| 77 | const std::vector<uint8_t>& request_blob) { |
| 78 | LOG(INFO) << "Received set authentication details request."; |
| 79 | |
| 80 | SetAuthenticationDetailsRequest request; |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 81 | std::string error_message = |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 82 | DeserializeProto(FROM_HERE, &request, request_blob); |
| 83 | |
| 84 | SetAuthenticationDetailsResponse response; |
| 85 | if (!error_message.empty()) { |
| 86 | response.set_error_message(error_message); |
| 87 | return SerializeProto(response); |
| 88 | } |
| 89 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 90 | if (IncludesSystemTraffic(request.traffic_type())) { |
| 91 | SetAuthenticationDetails(request, /*user_traffic=*/false, &error_message); |
| 92 | } |
| 93 | if (IncludesUserTraffic(request.traffic_type())) { |
| 94 | SetAuthenticationDetails(request, /*user_traffic=*/true, &error_message); |
| 95 | } |
| 96 | if (!error_message.empty()) { |
| 97 | response.set_error_message(error_message); |
| 98 | } |
| 99 | return SerializeProto(response); |
| 100 | } |
| 101 | |
| 102 | void SystemProxyAdaptor::SetAuthenticationDetails( |
| 103 | SetAuthenticationDetailsRequest auth_details, |
| 104 | bool user_traffic, |
| 105 | std::string* error_message) { |
| 106 | SandboxedWorker* worker = CreateWorkerIfNeeded(user_traffic); |
| 107 | if (!worker) { |
| 108 | error_message->append(kFailedToStartWorkerError); |
| 109 | return; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 112 | if (auth_details.has_credentials() || auth_details.has_protection_space()) { |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 113 | worker::Credentials credentials; |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 114 | if (auth_details.has_protection_space()) { |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 115 | worker::ProtectionSpace protection_space; |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 116 | protection_space.set_origin(auth_details.protection_space().origin()); |
| 117 | protection_space.set_scheme(auth_details.protection_space().scheme()); |
| 118 | protection_space.set_realm(auth_details.protection_space().realm()); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 119 | *credentials.mutable_protection_space() = protection_space; |
| 120 | } |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 121 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 122 | if (auth_details.has_credentials()) { |
Andreea Costinas | cc4d54e | 2020-10-19 15:46:25 +0200 | [diff] [blame] | 123 | system_proxy::Credentials dbus_cred = auth_details.credentials(); |
| 124 | if (dbus_cred.has_username() && dbus_cred.has_password()) { |
| 125 | credentials.set_username(dbus_cred.username()); |
| 126 | credentials.set_password(dbus_cred.password()); |
| 127 | credentials.mutable_policy_credentials_auth_schemes()->Swap( |
| 128 | dbus_cred.mutable_policy_credentials_auth_schemes()); |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | brillo::MessageLoop::current()->PostTask( |
| 133 | FROM_HERE, |
| 134 | base::Bind(&SystemProxyAdaptor::SetCredentialsTask, |
| 135 | weak_ptr_factory_.GetWeakPtr(), worker, credentials)); |
| 136 | } |
| 137 | if (auth_details.has_kerberos_enabled()) { |
| 138 | std::string principal_name = auth_details.has_active_principal_name() |
| 139 | ? auth_details.active_principal_name() |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 140 | : std::string(); |
| 141 | |
| 142 | brillo::MessageLoop::current()->PostTask( |
| 143 | FROM_HERE, base::Bind(&SystemProxyAdaptor::SetKerberosEnabledTask, |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 144 | weak_ptr_factory_.GetWeakPtr(), worker, |
| 145 | auth_details.kerberos_enabled(), principal_name)); |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 146 | } |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Andreea Costinas | fc3dc7d | 2020-07-20 18:54:38 +0200 | [diff] [blame] | 149 | // TODO(acostinas, crbug.com/1109144): Deprecated in favor of |ShutDownProcess|. |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 150 | std::vector<uint8_t> SystemProxyAdaptor::ShutDown() { |
| 151 | LOG(INFO) << "Received shutdown request."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 152 | |
| 153 | std::string error_message; |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 154 | if (!ResetWorker(/* user_traffic=*/false)) { |
| 155 | error_message = |
| 156 | "Failure to terminate worker process for system services traffic."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 159 | if (!ResetWorker(/* user_traffic=*/true)) { |
| 160 | error_message += "Failure to terminate worker process for arc traffic."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 163 | ShutDownResponse response; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 164 | if (!error_message.empty()) |
| 165 | response.set_error_message(error_message); |
| 166 | |
| 167 | brillo::MessageLoop::current()->PostTask( |
| 168 | FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask, |
| 169 | weak_ptr_factory_.GetWeakPtr())); |
| 170 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 171 | return SerializeProto(response); |
| 172 | } |
| 173 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 174 | std::vector<uint8_t> SystemProxyAdaptor::ClearUserCredentials( |
| 175 | const std::vector<uint8_t>& request_blob) { |
| 176 | LOG(INFO) << "Received request to clear user credentials."; |
| 177 | std::string error_message; |
| 178 | ClearUserCredentials(/*user_traffic=*/false, &error_message); |
| 179 | ClearUserCredentials(/*user_traffic=*/true, &error_message); |
| 180 | |
| 181 | ClearUserCredentialsResponse response; |
| 182 | if (!error_message.empty()) |
| 183 | response.set_error_message(error_message); |
| 184 | return SerializeProto(response); |
| 185 | } |
| 186 | |
| 187 | void SystemProxyAdaptor::ClearUserCredentials(bool user_traffic, |
| 188 | std::string* error_message) { |
| 189 | SandboxedWorker* worker = GetWorker(user_traffic); |
| 190 | if (!worker) { |
| 191 | return; |
| 192 | } |
| 193 | if (!worker->ClearUserCredentials()) { |
| 194 | error_message->append( |
| 195 | base::StringPrintf("Failure to clear user credentials for worker with " |
| 196 | "pid %s. Restarting worker.", |
| 197 | std::to_string(worker->pid()).c_str())); |
| 198 | ResetWorker(user_traffic); |
| 199 | CreateWorkerIfNeeded(user_traffic); |
| 200 | } |
| 201 | } |
| 202 | |
Andreea Costinas | fc3dc7d | 2020-07-20 18:54:38 +0200 | [diff] [blame] | 203 | std::vector<uint8_t> SystemProxyAdaptor::ShutDownProcess( |
| 204 | const std::vector<uint8_t>& request_blob) { |
| 205 | LOG(INFO) << "Received shutdown request."; |
| 206 | ShutDownRequest request; |
| 207 | std::string error_message = |
| 208 | DeserializeProto(FROM_HERE, &request, request_blob); |
| 209 | |
| 210 | if (IncludesSystemTraffic(request.traffic_type()) && |
| 211 | !ResetWorker(/* user_traffic=*/false)) { |
| 212 | error_message = |
| 213 | "Failure to terminate worker process for system services traffic."; |
| 214 | } |
| 215 | |
| 216 | if (IncludesUserTraffic(request.traffic_type()) && |
| 217 | !ResetWorker(/* user_traffic=*/true)) { |
| 218 | error_message += "Failure to terminate worker process for arc traffic."; |
| 219 | } |
| 220 | |
| 221 | ShutDownResponse response; |
| 222 | if (!error_message.empty()) |
| 223 | response.set_error_message(error_message); |
| 224 | |
| 225 | if (request.traffic_type() == TrafficOrigin::ALL) { |
| 226 | brillo::MessageLoop::current()->PostTask( |
| 227 | FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask, |
| 228 | weak_ptr_factory_.GetWeakPtr())); |
| 229 | } |
| 230 | return SerializeProto(response); |
| 231 | } |
| 232 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 233 | void SystemProxyAdaptor::GetChromeProxyServersAsync( |
| 234 | const std::string& target_url, |
| 235 | const brillo::http::GetChromeProxyServersCallback& callback) { |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 236 | brillo::http::GetChromeProxyServersAsync(dbus_object_->GetBus(), target_url, |
| 237 | move(callback)); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 240 | bool SystemProxyAdaptor::IsLocalProxy(const std::string& proxy) { |
| 241 | if (system_services_worker_ && |
| 242 | proxy.find(system_services_worker_->local_proxy_host_and_port()) != |
| 243 | std::string::npos) { |
| 244 | return true; |
| 245 | } |
| 246 | return arc_worker_ && proxy.find(arc_worker_->local_proxy_host_and_port()) != |
| 247 | std::string::npos; |
| 248 | } |
| 249 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 250 | std::unique_ptr<SandboxedWorker> SystemProxyAdaptor::CreateWorker() { |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 251 | return std::make_unique<SandboxedWorker>(weak_ptr_factory_.GetWeakPtr()); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 254 | SandboxedWorker* SystemProxyAdaptor::CreateWorkerIfNeeded(bool user_traffic) { |
| 255 | SandboxedWorker* worker = GetWorker(user_traffic); |
| 256 | if (worker) { |
| 257 | // A worker for traffic indicated by |user_traffic| already exists. |
| 258 | return worker; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 259 | } |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 260 | SetWorker(user_traffic, CreateWorker()); |
| 261 | worker = GetWorker(user_traffic); |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 262 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 263 | if (!worker->Start()) { |
| 264 | ResetWorker(user_traffic); |
| 265 | return nullptr; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 266 | } |
| 267 | // patchpanel_proxy is owned by |dbus_object_->bus_|. |
| 268 | dbus::ObjectProxy* patchpanel_proxy = dbus_object_->GetBus()->GetObjectProxy( |
| 269 | patchpanel::kPatchPanelServiceName, |
| 270 | dbus::ObjectPath(patchpanel::kPatchPanelServicePath)); |
| 271 | patchpanel_proxy->WaitForServiceToBeAvailable( |
| 272 | base::Bind(&SystemProxyAdaptor::OnPatchpanelServiceAvailable, |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 273 | weak_ptr_factory_.GetWeakPtr(), user_traffic)); |
| 274 | return worker; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 277 | void SystemProxyAdaptor::SetCredentialsTask( |
| 278 | SandboxedWorker* worker, const worker::Credentials& credentials) { |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 279 | DCHECK(worker); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 280 | worker->SetCredentials(credentials); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 283 | void SystemProxyAdaptor::SetKerberosEnabledTask( |
| 284 | SandboxedWorker* worker, |
| 285 | bool kerberos_enabled, |
| 286 | const std::string& principal_name) { |
| 287 | DCHECK(worker); |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 288 | worker->SetKerberosEnabled(kerberos_enabled, |
| 289 | kerberos_client_->krb5_conf_path(), |
| 290 | kerberos_client_->krb5_ccache_path()); |
| 291 | kerberos_client_->SetKerberosEnabled(kerberos_enabled); |
| 292 | if (kerberos_enabled) { |
| 293 | kerberos_client_->SetPrincipalName(principal_name); |
| 294 | } |
| 295 | } |
| 296 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 297 | void SystemProxyAdaptor::ShutDownTask() { |
| 298 | brillo::MessageLoop::current()->BreakLoop(); |
| 299 | } |
| 300 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 301 | void SystemProxyAdaptor::SetWorker(bool user_traffic, |
| 302 | std::unique_ptr<SandboxedWorker> worker) { |
| 303 | if (user_traffic) { |
| 304 | arc_worker_ = std::move(worker); |
| 305 | } else { |
| 306 | system_services_worker_ = std::move(worker); |
| 307 | } |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 310 | bool SystemProxyAdaptor::ResetWorker(bool user_traffic) { |
| 311 | SandboxedWorker* worker = |
| 312 | user_traffic ? arc_worker_.get() : system_services_worker_.get(); |
| 313 | if (!worker) { |
| 314 | return true; |
| 315 | } |
| 316 | if (!worker->Stop()) { |
| 317 | return false; |
| 318 | } |
| 319 | if (user_traffic) { |
| 320 | arc_worker_.reset(); |
| 321 | } else { |
| 322 | system_services_worker_.reset(); |
| 323 | } |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | SandboxedWorker* SystemProxyAdaptor::GetWorker(bool user_traffic) { |
| 328 | return user_traffic ? arc_worker_.get() : system_services_worker_.get(); |
| 329 | } |
| 330 | |
Andreea Costinas | fc3dc7d | 2020-07-20 18:54:38 +0200 | [diff] [blame] | 331 | bool SystemProxyAdaptor::IncludesSystemTraffic(TrafficOrigin traffic_origin) { |
| 332 | return traffic_origin != TrafficOrigin::USER; |
| 333 | } |
| 334 | |
| 335 | bool SystemProxyAdaptor::IncludesUserTraffic(TrafficOrigin traffic_origin) { |
| 336 | return traffic_origin != TrafficOrigin::SYSTEM; |
| 337 | } |
| 338 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 339 | void SystemProxyAdaptor::OnPatchpanelServiceAvailable(bool user_traffic, |
| 340 | bool is_available) { |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 341 | if (!is_available) { |
| 342 | LOG(ERROR) << "Patchpanel service not available"; |
| 343 | return; |
| 344 | } |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 345 | ConnectNamespace(user_traffic); |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 348 | void SystemProxyAdaptor::ConnectNamespace(bool user_traffic) { |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 349 | DCHECK_GT(netns_reconnect_attempts_available_, 0); |
| 350 | --netns_reconnect_attempts_available_; |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 351 | SandboxedWorker* worker = GetWorker(user_traffic); |
| 352 | DCHECK(worker); |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 353 | // TODO(b/160736881, acostinas): Remove the delay after patchpanel |
| 354 | // implements "ip netns" to create the veth pair across network namespaces. |
| 355 | brillo::MessageLoop::current()->PostDelayedTask( |
| 356 | FROM_HERE, |
| 357 | base::Bind(&SystemProxyAdaptor::ConnectNamespaceTask, |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 358 | weak_ptr_factory_.GetWeakPtr(), worker, user_traffic), |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 359 | kConnectNamespaceDelay); |
| 360 | } |
| 361 | |
| 362 | void SystemProxyAdaptor::ConnectNamespaceTask(SandboxedWorker* worker, |
| 363 | bool user_traffic) { |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 364 | std::unique_ptr<patchpanel::Client> patchpanel_client = |
| 365 | patchpanel::Client::New(); |
| 366 | if (!patchpanel_client) { |
| 367 | LOG(ERROR) << "Failed to open networking service client"; |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 368 | return; |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse> result = |
| 372 | patchpanel_client->ConnectNamespace( |
| 373 | worker->pid(), "" /* outbound_ifname */, user_traffic); |
| 374 | |
| 375 | if (!result.first.is_valid()) { |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 376 | LOG(ERROR) << "Failed to setup network namespace on attempt " |
| 377 | << kNetworkNamespaceReconnectAttempts - |
| 378 | netns_reconnect_attempts_available_; |
| 379 | if (netns_reconnect_attempts_available_ > 0) { |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 380 | ConnectNamespace(user_traffic); |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 381 | } |
| 382 | return; |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | worker->SetNetNamespaceLifelineFd(std::move(result.first)); |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 386 | if (!worker->SetListeningAddress(result.second.host_ipv4_address(), |
| 387 | kProxyPort)) { |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 388 | return; |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 389 | } |
| 390 | OnNamespaceConnected(worker, user_traffic); |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void SystemProxyAdaptor::OnNamespaceConnected(SandboxedWorker* worker, |
| 394 | bool user_traffic) { |
| 395 | WorkerActiveSignalDetails details; |
| 396 | details.set_traffic_origin(user_traffic ? TrafficOrigin::USER |
| 397 | : TrafficOrigin::SYSTEM); |
| 398 | details.set_local_proxy_url(worker->local_proxy_host_and_port()); |
| 399 | SendWorkerActiveSignal(SerializeProto(details)); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 402 | void SystemProxyAdaptor::RequestAuthenticationCredentials( |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame] | 403 | const worker::ProtectionSpace& protection_space, |
| 404 | bool bad_cached_credentials) { |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 405 | AuthenticationRequiredDetails details; |
| 406 | ProtectionSpace proxy_protection_space; |
| 407 | proxy_protection_space.set_origin(protection_space.origin()); |
| 408 | proxy_protection_space.set_realm(protection_space.realm()); |
| 409 | proxy_protection_space.set_scheme(protection_space.scheme()); |
| 410 | *details.mutable_proxy_protection_space() = proxy_protection_space; |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame] | 411 | details.set_bad_cached_credentials(bad_cached_credentials); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 412 | SendAuthenticationRequiredSignal(SerializeProto(details)); |
| 413 | } |
| 414 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 415 | } // namespace system_proxy |