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()) { |
| 123 | if (auth_details.credentials().has_username() && |
| 124 | auth_details.credentials().has_password()) { |
| 125 | credentials.set_username(auth_details.credentials().username()); |
| 126 | credentials.set_password(auth_details.credentials().password()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | brillo::MessageLoop::current()->PostTask( |
| 131 | FROM_HERE, |
| 132 | base::Bind(&SystemProxyAdaptor::SetCredentialsTask, |
| 133 | weak_ptr_factory_.GetWeakPtr(), worker, credentials)); |
| 134 | } |
| 135 | if (auth_details.has_kerberos_enabled()) { |
| 136 | std::string principal_name = auth_details.has_active_principal_name() |
| 137 | ? auth_details.active_principal_name() |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 138 | : std::string(); |
| 139 | |
| 140 | brillo::MessageLoop::current()->PostTask( |
| 141 | FROM_HERE, base::Bind(&SystemProxyAdaptor::SetKerberosEnabledTask, |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 142 | weak_ptr_factory_.GetWeakPtr(), worker, |
| 143 | auth_details.kerberos_enabled(), principal_name)); |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 144 | } |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Andreea Costinas | fc3dc7d | 2020-07-20 18:54:38 +0200 | [diff] [blame] | 147 | // TODO(acostinas, crbug.com/1109144): Deprecated in favor of |ShutDownProcess|. |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 148 | std::vector<uint8_t> SystemProxyAdaptor::ShutDown() { |
| 149 | LOG(INFO) << "Received shutdown request."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 150 | |
| 151 | std::string error_message; |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 152 | if (!ResetWorker(/* user_traffic=*/false)) { |
| 153 | error_message = |
| 154 | "Failure to terminate worker process for system services traffic."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 157 | if (!ResetWorker(/* user_traffic=*/true)) { |
| 158 | error_message += "Failure to terminate worker process for arc traffic."; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 161 | ShutDownResponse response; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 162 | if (!error_message.empty()) |
| 163 | response.set_error_message(error_message); |
| 164 | |
| 165 | brillo::MessageLoop::current()->PostTask( |
| 166 | FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask, |
| 167 | weak_ptr_factory_.GetWeakPtr())); |
| 168 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 169 | return SerializeProto(response); |
| 170 | } |
| 171 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 172 | std::vector<uint8_t> SystemProxyAdaptor::ClearUserCredentials( |
| 173 | const std::vector<uint8_t>& request_blob) { |
| 174 | LOG(INFO) << "Received request to clear user credentials."; |
| 175 | std::string error_message; |
| 176 | ClearUserCredentials(/*user_traffic=*/false, &error_message); |
| 177 | ClearUserCredentials(/*user_traffic=*/true, &error_message); |
| 178 | |
| 179 | ClearUserCredentialsResponse response; |
| 180 | if (!error_message.empty()) |
| 181 | response.set_error_message(error_message); |
| 182 | return SerializeProto(response); |
| 183 | } |
| 184 | |
| 185 | void SystemProxyAdaptor::ClearUserCredentials(bool user_traffic, |
| 186 | std::string* error_message) { |
| 187 | SandboxedWorker* worker = GetWorker(user_traffic); |
| 188 | if (!worker) { |
| 189 | return; |
| 190 | } |
| 191 | if (!worker->ClearUserCredentials()) { |
| 192 | error_message->append( |
| 193 | base::StringPrintf("Failure to clear user credentials for worker with " |
| 194 | "pid %s. Restarting worker.", |
| 195 | std::to_string(worker->pid()).c_str())); |
| 196 | ResetWorker(user_traffic); |
| 197 | CreateWorkerIfNeeded(user_traffic); |
| 198 | } |
| 199 | } |
| 200 | |
Andreea Costinas | fc3dc7d | 2020-07-20 18:54:38 +0200 | [diff] [blame] | 201 | std::vector<uint8_t> SystemProxyAdaptor::ShutDownProcess( |
| 202 | const std::vector<uint8_t>& request_blob) { |
| 203 | LOG(INFO) << "Received shutdown request."; |
| 204 | ShutDownRequest request; |
| 205 | std::string error_message = |
| 206 | DeserializeProto(FROM_HERE, &request, request_blob); |
| 207 | |
| 208 | if (IncludesSystemTraffic(request.traffic_type()) && |
| 209 | !ResetWorker(/* user_traffic=*/false)) { |
| 210 | error_message = |
| 211 | "Failure to terminate worker process for system services traffic."; |
| 212 | } |
| 213 | |
| 214 | if (IncludesUserTraffic(request.traffic_type()) && |
| 215 | !ResetWorker(/* user_traffic=*/true)) { |
| 216 | error_message += "Failure to terminate worker process for arc traffic."; |
| 217 | } |
| 218 | |
| 219 | ShutDownResponse response; |
| 220 | if (!error_message.empty()) |
| 221 | response.set_error_message(error_message); |
| 222 | |
| 223 | if (request.traffic_type() == TrafficOrigin::ALL) { |
| 224 | brillo::MessageLoop::current()->PostTask( |
| 225 | FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask, |
| 226 | weak_ptr_factory_.GetWeakPtr())); |
| 227 | } |
| 228 | return SerializeProto(response); |
| 229 | } |
| 230 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 231 | void SystemProxyAdaptor::GetChromeProxyServersAsync( |
| 232 | const std::string& target_url, |
| 233 | const brillo::http::GetChromeProxyServersCallback& callback) { |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 234 | brillo::http::GetChromeProxyServersAsync(dbus_object_->GetBus(), target_url, |
| 235 | move(callback)); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 238 | bool SystemProxyAdaptor::IsLocalProxy(const std::string& proxy) { |
| 239 | if (system_services_worker_ && |
| 240 | proxy.find(system_services_worker_->local_proxy_host_and_port()) != |
| 241 | std::string::npos) { |
| 242 | return true; |
| 243 | } |
| 244 | return arc_worker_ && proxy.find(arc_worker_->local_proxy_host_and_port()) != |
| 245 | std::string::npos; |
| 246 | } |
| 247 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 248 | std::unique_ptr<SandboxedWorker> SystemProxyAdaptor::CreateWorker() { |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 249 | return std::make_unique<SandboxedWorker>(weak_ptr_factory_.GetWeakPtr()); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 252 | SandboxedWorker* SystemProxyAdaptor::CreateWorkerIfNeeded(bool user_traffic) { |
| 253 | SandboxedWorker* worker = GetWorker(user_traffic); |
| 254 | if (worker) { |
| 255 | // A worker for traffic indicated by |user_traffic| already exists. |
| 256 | return worker; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 257 | } |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 258 | SetWorker(user_traffic, CreateWorker()); |
| 259 | worker = GetWorker(user_traffic); |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 260 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 261 | if (!worker->Start()) { |
| 262 | ResetWorker(user_traffic); |
| 263 | return nullptr; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 264 | } |
| 265 | // patchpanel_proxy is owned by |dbus_object_->bus_|. |
| 266 | dbus::ObjectProxy* patchpanel_proxy = dbus_object_->GetBus()->GetObjectProxy( |
| 267 | patchpanel::kPatchPanelServiceName, |
| 268 | dbus::ObjectPath(patchpanel::kPatchPanelServicePath)); |
| 269 | patchpanel_proxy->WaitForServiceToBeAvailable( |
| 270 | base::Bind(&SystemProxyAdaptor::OnPatchpanelServiceAvailable, |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 271 | weak_ptr_factory_.GetWeakPtr(), user_traffic)); |
| 272 | return worker; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 275 | void SystemProxyAdaptor::SetCredentialsTask( |
| 276 | SandboxedWorker* worker, const worker::Credentials& credentials) { |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 277 | DCHECK(worker); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 278 | worker->SetCredentials(credentials); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 281 | void SystemProxyAdaptor::SetKerberosEnabledTask( |
| 282 | SandboxedWorker* worker, |
| 283 | bool kerberos_enabled, |
| 284 | const std::string& principal_name) { |
| 285 | DCHECK(worker); |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 286 | worker->SetKerberosEnabled(kerberos_enabled, |
| 287 | kerberos_client_->krb5_conf_path(), |
| 288 | kerberos_client_->krb5_ccache_path()); |
| 289 | kerberos_client_->SetKerberosEnabled(kerberos_enabled); |
| 290 | if (kerberos_enabled) { |
| 291 | kerberos_client_->SetPrincipalName(principal_name); |
| 292 | } |
| 293 | } |
| 294 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 295 | void SystemProxyAdaptor::ShutDownTask() { |
| 296 | brillo::MessageLoop::current()->BreakLoop(); |
| 297 | } |
| 298 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 299 | void SystemProxyAdaptor::SetWorker(bool user_traffic, |
| 300 | std::unique_ptr<SandboxedWorker> worker) { |
| 301 | if (user_traffic) { |
| 302 | arc_worker_ = std::move(worker); |
| 303 | } else { |
| 304 | system_services_worker_ = std::move(worker); |
| 305 | } |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 308 | bool SystemProxyAdaptor::ResetWorker(bool user_traffic) { |
| 309 | SandboxedWorker* worker = |
| 310 | user_traffic ? arc_worker_.get() : system_services_worker_.get(); |
| 311 | if (!worker) { |
| 312 | return true; |
| 313 | } |
| 314 | if (!worker->Stop()) { |
| 315 | return false; |
| 316 | } |
| 317 | if (user_traffic) { |
| 318 | arc_worker_.reset(); |
| 319 | } else { |
| 320 | system_services_worker_.reset(); |
| 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | SandboxedWorker* SystemProxyAdaptor::GetWorker(bool user_traffic) { |
| 326 | return user_traffic ? arc_worker_.get() : system_services_worker_.get(); |
| 327 | } |
| 328 | |
Andreea Costinas | fc3dc7d | 2020-07-20 18:54:38 +0200 | [diff] [blame] | 329 | bool SystemProxyAdaptor::IncludesSystemTraffic(TrafficOrigin traffic_origin) { |
| 330 | return traffic_origin != TrafficOrigin::USER; |
| 331 | } |
| 332 | |
| 333 | bool SystemProxyAdaptor::IncludesUserTraffic(TrafficOrigin traffic_origin) { |
| 334 | return traffic_origin != TrafficOrigin::SYSTEM; |
| 335 | } |
| 336 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 337 | void SystemProxyAdaptor::OnPatchpanelServiceAvailable(bool user_traffic, |
| 338 | bool is_available) { |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 339 | if (!is_available) { |
| 340 | LOG(ERROR) << "Patchpanel service not available"; |
| 341 | return; |
| 342 | } |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 343 | ConnectNamespace(user_traffic); |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 346 | void SystemProxyAdaptor::ConnectNamespace(bool user_traffic) { |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 347 | DCHECK_GT(netns_reconnect_attempts_available_, 0); |
| 348 | --netns_reconnect_attempts_available_; |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 349 | SandboxedWorker* worker = GetWorker(user_traffic); |
| 350 | DCHECK(worker); |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 351 | // TODO(b/160736881, acostinas): Remove the delay after patchpanel |
| 352 | // implements "ip netns" to create the veth pair across network namespaces. |
| 353 | brillo::MessageLoop::current()->PostDelayedTask( |
| 354 | FROM_HERE, |
| 355 | base::Bind(&SystemProxyAdaptor::ConnectNamespaceTask, |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 356 | weak_ptr_factory_.GetWeakPtr(), worker, user_traffic), |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 357 | kConnectNamespaceDelay); |
| 358 | } |
| 359 | |
| 360 | void SystemProxyAdaptor::ConnectNamespaceTask(SandboxedWorker* worker, |
| 361 | bool user_traffic) { |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 362 | std::unique_ptr<patchpanel::Client> patchpanel_client = |
| 363 | patchpanel::Client::New(); |
| 364 | if (!patchpanel_client) { |
| 365 | LOG(ERROR) << "Failed to open networking service client"; |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 366 | return; |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse> result = |
| 370 | patchpanel_client->ConnectNamespace( |
| 371 | worker->pid(), "" /* outbound_ifname */, user_traffic); |
| 372 | |
| 373 | if (!result.first.is_valid()) { |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 374 | LOG(ERROR) << "Failed to setup network namespace on attempt " |
| 375 | << kNetworkNamespaceReconnectAttempts - |
| 376 | netns_reconnect_attempts_available_; |
| 377 | if (netns_reconnect_attempts_available_ > 0) { |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 378 | ConnectNamespace(user_traffic); |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 379 | } |
| 380 | return; |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | worker->SetNetNamespaceLifelineFd(std::move(result.first)); |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 384 | if (!worker->SetListeningAddress(result.second.host_ipv4_address(), |
| 385 | kProxyPort)) { |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 386 | return; |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 387 | } |
| 388 | OnNamespaceConnected(worker, user_traffic); |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void SystemProxyAdaptor::OnNamespaceConnected(SandboxedWorker* worker, |
| 392 | bool user_traffic) { |
| 393 | WorkerActiveSignalDetails details; |
| 394 | details.set_traffic_origin(user_traffic ? TrafficOrigin::USER |
| 395 | : TrafficOrigin::SYSTEM); |
| 396 | details.set_local_proxy_url(worker->local_proxy_host_and_port()); |
| 397 | SendWorkerActiveSignal(SerializeProto(details)); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 400 | void SystemProxyAdaptor::RequestAuthenticationCredentials( |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame] | 401 | const worker::ProtectionSpace& protection_space, |
| 402 | bool bad_cached_credentials) { |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 403 | AuthenticationRequiredDetails details; |
| 404 | ProtectionSpace proxy_protection_space; |
| 405 | proxy_protection_space.set_origin(protection_space.origin()); |
| 406 | proxy_protection_space.set_realm(protection_space.realm()); |
| 407 | proxy_protection_space.set_scheme(protection_space.scheme()); |
| 408 | *details.mutable_proxy_protection_space() = proxy_protection_space; |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame] | 409 | details.set_bad_cached_credentials(bad_cached_credentials); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 410 | SendAuthenticationRequiredSignal(SerializeProto(details)); |
| 411 | } |
| 412 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 413 | } // namespace system_proxy |