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