blob: 3cef8402ca1c565e25feb2a13518c533998ade94 [file] [log] [blame]
Andreea Costinas41e06442020-03-09 09:41:51 +01001// 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
5#include "system-proxy/server_proxy.h"
6
7#include <iostream>
8#include <string>
9#include <utility>
10#include <vector>
11
Andreea Costinascc4d54e2020-10-19 15:46:25 +020012#include <curl/curl.h>
13
Andreea Costinas41e06442020-03-09 09:41:51 +010014#include <base/bind.h>
15#include <base/bind_helpers.h>
16#include <base/callback_helpers.h>
17#include <base/posix/eintr_wrapper.h>
18#include <base/files/file_util.h>
Andreea Costinascc4d54e2020-10-19 15:46:25 +020019#include <base/strings/string_split.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010020#include <base/strings/string_util.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010021#include <base/threading/thread.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010022#include <base/threading/thread_task_runner_handle.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010023#include <brillo/data_encoding.h>
24#include <brillo/http/http_transport.h>
Garrick Evanscd8c2972020-04-14 14:35:52 +090025#include <chromeos/patchpanel/socket.h>
26#include <chromeos/patchpanel/socket_forwarder.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010027
28#include "bindings/worker_common.pb.h"
Andreea Costinascc4d54e2020-10-19 15:46:25 +020029#include "system-proxy/http_util.h"
Andreea Costinas41e06442020-03-09 09:41:51 +010030#include "system-proxy/protobuf_util.h"
Andreea Costinase45d54b2020-03-10 09:21:14 +010031#include "system-proxy/proxy_connect_job.h"
Andreea Costinas41e06442020-03-09 09:41:51 +010032
33namespace system_proxy {
34
Andreea Costinas44cefa22020-03-09 09:07:39 +010035namespace {
Andreea Costinase45d54b2020-03-10 09:21:14 +010036
37constexpr int kMaxConn = 100;
Andreea Costinas922fbaf2020-05-28 11:55:22 +020038// Name of the environment variable that points to the location of the kerberos
39// credentials (ticket) cache.
40constexpr char kKrb5CCEnvKey[] = "KRB5CCNAME";
41// Name of the environment variable that points to the kerberos configuration
42// file which contains information regarding the locations of KDCs and admin
43// servers for the Kerberos realms of interest, defaults for the current realm
44// and for Kerberos applications, and mappings of hostnames onto Kerberos
45// realms.
46constexpr char kKrb5ConfEnvKey[] = "KRB5_CONFIG";
Andreea Costinasbb2aa022020-06-13 00:03:23 +020047constexpr char kCredentialsColonSeparator[] = ":";
Andreea Costinase45d54b2020-03-10 09:21:14 +010048
Andreea Costinas20660a12020-07-07 09:32:42 +020049// Returns the URL encoded value of |text|.
Andreea Costinase45d54b2020-03-10 09:21:14 +010050std::string UrlEncode(const std::string& text) {
Andreea Costinas20660a12020-07-07 09:32:42 +020051 return brillo::data_encoding::UrlEncode(text.c_str(),
52 /* encodeSpaceAsPlus= */ false);
Andreea Costinase45d54b2020-03-10 09:21:14 +010053}
54
Andreea Costinascc4d54e2020-10-19 15:46:25 +020055// Converts the list of proxy authentication schemes in string format to the
56// curl bit-mask format.
57int64_t GetAuthSchemes(
58 const google::protobuf::RepeatedPtrField<std::string>& auth_schemes) {
59 if (auth_schemes.empty())
60 return CURLAUTH_ANY;
61 // Convert auth schemes to curl format.
62 int64_t curl_scheme = CURLAUTH_NEGOTIATE;
63 // Auth scheme is case insensitive, see
64 // https://tools.ietf.org/html/rfc7235#section-2.1
65 for (auto const& scheme : auth_schemes) {
66 const std::string lower_scheme = base::ToLowerASCII(scheme);
67 if (lower_scheme == "basic")
68 curl_scheme |= CURLAUTH_BASIC;
69 if (lower_scheme == "digest")
70 curl_scheme |= CURLAUTH_DIGEST;
71 if (lower_scheme == "ntlm")
72 curl_scheme |= CURLAUTH_NTLM;
73 }
74 return curl_scheme;
75}
Andreea Costinas44cefa22020-03-09 09:07:39 +010076} // namespace
77
Andreea Costinas41e06442020-03-09 09:41:51 +010078ServerProxy::ServerProxy(base::OnceClosure quit_closure)
Andreea Costinasbb2aa022020-06-13 00:03:23 +020079 : system_credentials_(kCredentialsColonSeparator),
80 quit_closure_(std::move(quit_closure)),
81 weak_ptr_factory_(this) {}
Andreea Costinase45d54b2020-03-10 09:21:14 +010082ServerProxy::~ServerProxy() = default;
Andreea Costinas41e06442020-03-09 09:41:51 +010083
84void ServerProxy::Init() {
85 // Start listening for input.
86 stdin_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Andreea Costinase45d54b2020-03-10 09:21:14 +010087 GetStdinPipe(), base::Bind(&ServerProxy::HandleStdinReadable,
88 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinas41e06442020-03-09 09:41:51 +010089
90 // Handle termination signals.
91 signal_handler_.Init();
92 for (int signal : {SIGINT, SIGTERM, SIGHUP, SIGQUIT}) {
93 signal_handler_.RegisterHandler(
94 signal, base::BindRepeating(&ServerProxy::HandleSignal,
95 base::Unretained(this)));
96 }
97}
98
Andreea Costinase45d54b2020-03-10 09:21:14 +010099void ServerProxy::ResolveProxy(const std::string& target_url,
100 OnProxyResolvedCallback callback) {
Andreea Costinas5862b102020-03-19 14:45:36 +0100101 auto it = pending_proxy_resolution_requests_.find(target_url);
102 if (it != pending_proxy_resolution_requests_.end()) {
103 it->second.push_back(std::move(callback));
104 return;
105 }
Andreea Costinasaae97382020-05-05 13:31:58 +0200106 worker::ProxyResolutionRequest proxy_request;
Andreea Costinas5862b102020-03-19 14:45:36 +0100107 proxy_request.set_target_url(target_url);
Andreea Costinasaae97382020-05-05 13:31:58 +0200108 worker::WorkerRequest request;
Andreea Costinas5862b102020-03-19 14:45:36 +0100109 *request.mutable_proxy_resolution_request() = proxy_request;
110 if (!WriteProtobuf(GetStdoutPipe(), request)) {
111 LOG(ERROR) << "Failed to send proxy resolution request for url: "
112 << target_url;
113 std::move(callback).Run({brillo::http::kDirectProxy});
114 return;
115 }
116 pending_proxy_resolution_requests_[target_url].push_back(std::move(callback));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100117}
Andreea Costinas41e06442020-03-09 09:41:51 +0100118
Andreea Costinased9e6122020-08-12 12:06:19 +0200119void ServerProxy::AuthenticationRequired(
120 const std::string& proxy_url,
121 const std::string& scheme,
122 const std::string& realm,
123 const std::string& bad_cached_credentials,
124 OnAuthAcquiredCallback callback) {
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200125 worker::ProtectionSpace protection_space;
126 protection_space.set_origin(proxy_url);
127 protection_space.set_realm(realm);
128 protection_space.set_scheme(scheme);
129
130 std::string auth_key = protection_space.SerializeAsString();
131 // Check the local cache.
132 auto it = auth_cache_.find(auth_key);
133 if (it != auth_cache_.end()) {
Andreea Costinased9e6122020-08-12 12:06:19 +0200134 // Don't use the cached credentials if they are flagged by the connection as
135 // "bad".
136 if (it->second != bad_cached_credentials) {
137 std::move(callback).Run(it->second);
138 return;
139 }
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200140 }
141
142 // Request the credentials from the main process.
143 worker::AuthRequiredRequest auth_request;
144 *auth_request.mutable_protection_space() = protection_space;
Andreea Costinased9e6122020-08-12 12:06:19 +0200145 auth_request.set_bad_cached_credentials(bad_cached_credentials !=
146 kCredentialsColonSeparator);
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200147
148 worker::WorkerRequest request;
149 *request.mutable_auth_required_request() = auth_request;
150
151 if (!WriteProtobuf(GetStdoutPipe(), request)) {
152 LOG(ERROR) << "Failed to send authentication required request";
153 std::move(callback).Run(/* credentials= */ std::string());
154 return;
155 }
156 pending_auth_required_requests_[auth_key].push_back(std::move(callback));
157}
158
159void ServerProxy::AuthCredentialsProvided(
160 const std::string& auth_credentials_key, const std::string& credentials) {
161 auto it = pending_auth_required_requests_.find(auth_credentials_key);
162 if (it == pending_auth_required_requests_.end()) {
163 LOG(WARNING) << "No pending requests found for credentials";
164 return;
165 }
166 for (auto& auth_acquired_callback : it->second) {
167 std::move(auth_acquired_callback).Run(credentials);
168 }
169 pending_auth_required_requests_.erase(auth_credentials_key);
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200170}
171
Andreea Costinas41e06442020-03-09 09:41:51 +0100172void ServerProxy::HandleStdinReadable() {
Andreea Costinasaae97382020-05-05 13:31:58 +0200173 worker::WorkerConfigs config;
Andreea Costinas44cefa22020-03-09 09:07:39 +0100174 if (!ReadProtobuf(GetStdinPipe(), &config)) {
175 LOG(ERROR) << "Error decoding protobuf configurations." << std::endl;
Andreea Costinas41e06442020-03-09 09:41:51 +0100176 return;
177 }
Andreea Costinas44cefa22020-03-09 09:07:39 +0100178
179 if (config.has_credentials()) {
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200180 std::string credentials;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100181 const std::string username = UrlEncode(config.credentials().username());
182 const std::string password = UrlEncode(config.credentials().password());
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200183 credentials = base::JoinString({username.c_str(), password.c_str()},
184 kCredentialsColonSeparator);
185 if (config.credentials().has_protection_space()) {
186 std::string auth_key =
187 config.credentials().protection_space().SerializeAsString();
188 if (!username.empty() && !password.empty()) {
189 auth_cache_[auth_key] = credentials;
190 AuthCredentialsProvided(auth_key, credentials);
191 } else {
192 AuthCredentialsProvided(auth_key, std::string());
193 }
194 } else {
Andreea Costinascc4d54e2020-10-19 15:46:25 +0200195 system_credentials_auth_schemes_ = GetAuthSchemes(
196 config.credentials().policy_credentials_auth_schemes());
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200197 system_credentials_ = credentials;
198 }
Andreea Costinas44cefa22020-03-09 09:07:39 +0100199 }
200
201 if (config.has_listening_address()) {
202 if (listening_addr_ != 0) {
203 LOG(ERROR)
204 << "Failure to set configurations: listening port was already set."
205 << std::endl;
206 return;
207 }
208 listening_addr_ = config.listening_address().addr();
209 listening_port_ = config.listening_address().port();
210 CreateListeningSocket();
211 }
Andreea Costinas5862b102020-03-19 14:45:36 +0100212
213 if (config.has_proxy_resolution_reply()) {
214 std::list<std::string> proxies;
Andreea Costinasaae97382020-05-05 13:31:58 +0200215 const worker::ProxyResolutionReply& reply = config.proxy_resolution_reply();
Andreea Costinas5862b102020-03-19 14:45:36 +0100216 for (auto const& proxy : reply.proxy_servers())
217 proxies.push_back(proxy);
218
219 OnProxyResolved(reply.target_url(), proxies);
220 }
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200221
222 if (config.has_kerberos_config()) {
223 if (config.kerberos_config().enabled()) {
224 // Set the environment variables that allow libcurl to use the existing
225 // kerberos ticket for proxy authentication. The files to which the env
226 // variables point to are maintained by the parent process.
227 setenv(kKrb5ConfEnvKey, config.kerberos_config().krb5conf_path().c_str(),
228 /* overwrite = */ 1);
229 setenv(kKrb5CCEnvKey, config.kerberos_config().krb5cc_path().c_str(),
230 /* overwrite = */ 1);
231 } else {
232 unsetenv(kKrb5ConfEnvKey);
233 unsetenv(kKrb5CCEnvKey);
234 }
235 }
Andreea Costinase9c73592020-07-17 15:27:54 +0200236
237 if (config.has_clear_user_credentials()) {
238 auth_cache_.clear();
239 }
Andreea Costinas41e06442020-03-09 09:41:51 +0100240}
241
242bool ServerProxy::HandleSignal(const struct signalfd_siginfo& siginfo) {
243 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
244 std::move(quit_closure_));
245 return true;
246}
247
Andreea Costinas44cefa22020-03-09 09:07:39 +0100248int ServerProxy::GetStdinPipe() {
249 return STDIN_FILENO;
250}
251
Andreea Costinas5862b102020-03-19 14:45:36 +0100252int ServerProxy::GetStdoutPipe() {
253 return STDOUT_FILENO;
254}
255
Andreea Costinas44cefa22020-03-09 09:07:39 +0100256void ServerProxy::CreateListeningSocket() {
Garrick Evans3388a032020-03-24 11:25:55 +0900257 listening_fd_ = std::make_unique<patchpanel::Socket>(
Andreea Costinas44cefa22020-03-09 09:07:39 +0100258 AF_INET, SOCK_STREAM | SOCK_NONBLOCK);
259
260 struct sockaddr_in addr = {0};
261 addr.sin_family = AF_INET;
262 addr.sin_port = htons(listening_port_);
263 addr.sin_addr.s_addr = listening_addr_;
264 if (!listening_fd_->Bind((const struct sockaddr*)&addr, sizeof(addr))) {
265 LOG(ERROR) << "Cannot bind source socket" << std::endl;
266 return;
267 }
268
269 if (!listening_fd_->Listen(kMaxConn)) {
270 LOG(ERROR) << "Cannot listen on source socket." << std::endl;
271 return;
272 }
273
274 fd_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Andreea Costinase45d54b2020-03-10 09:21:14 +0100275 listening_fd_->fd(), base::BindRepeating(&ServerProxy::OnConnectionAccept,
276 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinas44cefa22020-03-09 09:07:39 +0100277}
278
Andreea Costinase45d54b2020-03-10 09:21:14 +0100279void ServerProxy::OnConnectionAccept() {
Andreea Costinas44cefa22020-03-09 09:07:39 +0100280 struct sockaddr_storage client_src = {};
281 socklen_t sockaddr_len = sizeof(client_src);
282 if (auto client_conn =
283 listening_fd_->Accept((struct sockaddr*)&client_src, &sockaddr_len)) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100284 auto connect_job = std::make_unique<ProxyConnectJob>(
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200285 std::move(client_conn), system_credentials_,
Andreea Costinascc4d54e2020-10-19 15:46:25 +0200286 system_credentials_auth_schemes_,
Andreea Costinase45d54b2020-03-10 09:21:14 +0100287 base::BindOnce(&ServerProxy::ResolveProxy, base::Unretained(this)),
Andreea Costinased9e6122020-08-12 12:06:19 +0200288 base::BindRepeating(&ServerProxy::AuthenticationRequired,
289 base::Unretained(this)),
Andreea Costinase45d54b2020-03-10 09:21:14 +0100290 base::BindOnce(&ServerProxy::OnConnectionSetupFinished,
291 base::Unretained(this)));
292 if (connect_job->Start())
293 pending_connect_jobs_[connect_job.get()] = std::move(connect_job);
Andreea Costinas44cefa22020-03-09 09:07:39 +0100294 }
Andreea Costinase45d54b2020-03-10 09:21:14 +0100295 // Cleanup any defunct forwarders.
296 // TODO(acostinas, chromium:1064536) Monitor the client and server sockets
297 // and remove the corresponding SocketForwarder when a socket closes.
298 for (auto it = forwarders_.begin(); it != forwarders_.end(); ++it) {
299 if (!(*it)->IsRunning() && (*it)->HasBeenStarted())
300 it = forwarders_.erase(it);
301 }
302}
303
Andreea Costinas5862b102020-03-19 14:45:36 +0100304void ServerProxy::OnProxyResolved(const std::string& target_url,
305 const std::list<std::string>& proxy_servers) {
306 auto callbacks = std::move(pending_proxy_resolution_requests_[target_url]);
307 pending_proxy_resolution_requests_.erase(target_url);
308
309 for (auto& callback : callbacks)
310 std::move(callback).Run(proxy_servers);
311}
312
Andreea Costinase45d54b2020-03-10 09:21:14 +0100313void ServerProxy::OnConnectionSetupFinished(
Garrick Evans3388a032020-03-24 11:25:55 +0900314 std::unique_ptr<patchpanel::SocketForwarder> fwd,
Andreea Costinase45d54b2020-03-10 09:21:14 +0100315 ProxyConnectJob* connect_job) {
316 if (fwd) {
317 // The connection was set up successfully.
318 forwarders_.emplace_back(std::move(fwd));
319 }
320 pending_connect_jobs_.erase(connect_job);
Andreea Costinas44cefa22020-03-09 09:07:39 +0100321}
322
Andreea Costinas41e06442020-03-09 09:41:51 +0100323} // namespace system_proxy