Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +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 | |
| 5 | #include "system-proxy/server_proxy.h" |
| 6 | |
| 7 | #include <iostream> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/bind.h> |
| 13 | #include <base/bind_helpers.h> |
| 14 | #include <base/callback_helpers.h> |
| 15 | #include <base/posix/eintr_wrapper.h> |
| 16 | #include <base/files/file_util.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 17 | #include <base/strings/string_util.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 18 | #include <base/threading/thread.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 19 | #include <base/threading/thread_task_runner_handle.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 20 | #include <brillo/data_encoding.h> |
| 21 | #include <brillo/http/http_transport.h> |
Garrick Evans | cd8c297 | 2020-04-14 14:35:52 +0900 | [diff] [blame] | 22 | #include <chromeos/patchpanel/socket.h> |
| 23 | #include <chromeos/patchpanel/socket_forwarder.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 24 | |
| 25 | #include "bindings/worker_common.pb.h" |
| 26 | #include "system-proxy/protobuf_util.h" |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 27 | #include "system-proxy/proxy_connect_job.h" |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 28 | |
| 29 | namespace system_proxy { |
| 30 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 31 | namespace { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 32 | |
| 33 | constexpr int kMaxConn = 100; |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 34 | // Name of the environment variable that points to the location of the kerberos |
| 35 | // credentials (ticket) cache. |
| 36 | constexpr char kKrb5CCEnvKey[] = "KRB5CCNAME"; |
| 37 | // Name of the environment variable that points to the kerberos configuration |
| 38 | // file which contains information regarding the locations of KDCs and admin |
| 39 | // servers for the Kerberos realms of interest, defaults for the current realm |
| 40 | // and for Kerberos applications, and mappings of hostnames onto Kerberos |
| 41 | // realms. |
| 42 | constexpr char kKrb5ConfEnvKey[] = "KRB5_CONFIG"; |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 43 | constexpr char kCredentialsColonSeparator[] = ":"; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 44 | |
| 45 | // Returns the URL encoded value of |text|. It also verifies if the string was |
| 46 | // already encoded and, if true it returns it unmodified. |
| 47 | std::string UrlEncode(const std::string& text) { |
| 48 | if (text == brillo::data_encoding::UrlDecode(text.c_str())) |
| 49 | return brillo::data_encoding::UrlEncode(text.c_str(), false); |
| 50 | return text; |
| 51 | } |
| 52 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 53 | } // namespace |
| 54 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 55 | ServerProxy::ServerProxy(base::OnceClosure quit_closure) |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 56 | : system_credentials_(kCredentialsColonSeparator), |
| 57 | quit_closure_(std::move(quit_closure)), |
| 58 | weak_ptr_factory_(this) {} |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 59 | ServerProxy::~ServerProxy() = default; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 60 | |
| 61 | void ServerProxy::Init() { |
| 62 | // Start listening for input. |
| 63 | stdin_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 64 | GetStdinPipe(), base::Bind(&ServerProxy::HandleStdinReadable, |
| 65 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 66 | |
| 67 | // Handle termination signals. |
| 68 | signal_handler_.Init(); |
| 69 | for (int signal : {SIGINT, SIGTERM, SIGHUP, SIGQUIT}) { |
| 70 | signal_handler_.RegisterHandler( |
| 71 | signal, base::BindRepeating(&ServerProxy::HandleSignal, |
| 72 | base::Unretained(this))); |
| 73 | } |
| 74 | } |
| 75 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 76 | void ServerProxy::ResolveProxy(const std::string& target_url, |
| 77 | OnProxyResolvedCallback callback) { |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 78 | auto it = pending_proxy_resolution_requests_.find(target_url); |
| 79 | if (it != pending_proxy_resolution_requests_.end()) { |
| 80 | it->second.push_back(std::move(callback)); |
| 81 | return; |
| 82 | } |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 83 | worker::ProxyResolutionRequest proxy_request; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 84 | proxy_request.set_target_url(target_url); |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 85 | worker::WorkerRequest request; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 86 | *request.mutable_proxy_resolution_request() = proxy_request; |
| 87 | if (!WriteProtobuf(GetStdoutPipe(), request)) { |
| 88 | LOG(ERROR) << "Failed to send proxy resolution request for url: " |
| 89 | << target_url; |
| 90 | std::move(callback).Run({brillo::http::kDirectProxy}); |
| 91 | return; |
| 92 | } |
| 93 | pending_proxy_resolution_requests_[target_url].push_back(std::move(callback)); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 94 | } |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 95 | |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 96 | void ServerProxy::AuthenticationRequired(const std::string& proxy_url, |
| 97 | const std::string& scheme, |
| 98 | const std::string& realm, |
| 99 | OnAuthAcquiredCallback callback) { |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 100 | worker::ProtectionSpace protection_space; |
| 101 | protection_space.set_origin(proxy_url); |
| 102 | protection_space.set_realm(realm); |
| 103 | protection_space.set_scheme(scheme); |
| 104 | |
| 105 | std::string auth_key = protection_space.SerializeAsString(); |
| 106 | // Check the local cache. |
| 107 | auto it = auth_cache_.find(auth_key); |
| 108 | if (it != auth_cache_.end()) { |
| 109 | std::move(callback).Run(it->second); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | // Request the credentials from the main process. |
| 114 | worker::AuthRequiredRequest auth_request; |
| 115 | *auth_request.mutable_protection_space() = protection_space; |
| 116 | |
| 117 | worker::WorkerRequest request; |
| 118 | *request.mutable_auth_required_request() = auth_request; |
| 119 | |
| 120 | if (!WriteProtobuf(GetStdoutPipe(), request)) { |
| 121 | LOG(ERROR) << "Failed to send authentication required request"; |
| 122 | std::move(callback).Run(/* credentials= */ std::string()); |
| 123 | return; |
| 124 | } |
| 125 | pending_auth_required_requests_[auth_key].push_back(std::move(callback)); |
| 126 | } |
| 127 | |
| 128 | void ServerProxy::AuthCredentialsProvided( |
| 129 | const std::string& auth_credentials_key, const std::string& credentials) { |
| 130 | auto it = pending_auth_required_requests_.find(auth_credentials_key); |
| 131 | if (it == pending_auth_required_requests_.end()) { |
| 132 | LOG(WARNING) << "No pending requests found for credentials"; |
| 133 | return; |
| 134 | } |
| 135 | for (auto& auth_acquired_callback : it->second) { |
| 136 | std::move(auth_acquired_callback).Run(credentials); |
| 137 | } |
| 138 | pending_auth_required_requests_.erase(auth_credentials_key); |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 141 | void ServerProxy::HandleStdinReadable() { |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 142 | worker::WorkerConfigs config; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 143 | if (!ReadProtobuf(GetStdinPipe(), &config)) { |
| 144 | LOG(ERROR) << "Error decoding protobuf configurations." << std::endl; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 145 | return; |
| 146 | } |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 147 | |
| 148 | if (config.has_credentials()) { |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 149 | std::string credentials; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 150 | const std::string username = UrlEncode(config.credentials().username()); |
| 151 | const std::string password = UrlEncode(config.credentials().password()); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 152 | credentials = base::JoinString({username.c_str(), password.c_str()}, |
| 153 | kCredentialsColonSeparator); |
| 154 | if (config.credentials().has_protection_space()) { |
| 155 | std::string auth_key = |
| 156 | config.credentials().protection_space().SerializeAsString(); |
| 157 | if (!username.empty() && !password.empty()) { |
| 158 | auth_cache_[auth_key] = credentials; |
| 159 | AuthCredentialsProvided(auth_key, credentials); |
| 160 | } else { |
| 161 | AuthCredentialsProvided(auth_key, std::string()); |
| 162 | } |
| 163 | } else { |
| 164 | system_credentials_ = credentials; |
| 165 | } |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if (config.has_listening_address()) { |
| 169 | if (listening_addr_ != 0) { |
| 170 | LOG(ERROR) |
| 171 | << "Failure to set configurations: listening port was already set." |
| 172 | << std::endl; |
| 173 | return; |
| 174 | } |
| 175 | listening_addr_ = config.listening_address().addr(); |
| 176 | listening_port_ = config.listening_address().port(); |
| 177 | CreateListeningSocket(); |
| 178 | } |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 179 | |
| 180 | if (config.has_proxy_resolution_reply()) { |
| 181 | std::list<std::string> proxies; |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 182 | const worker::ProxyResolutionReply& reply = config.proxy_resolution_reply(); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 183 | for (auto const& proxy : reply.proxy_servers()) |
| 184 | proxies.push_back(proxy); |
| 185 | |
| 186 | OnProxyResolved(reply.target_url(), proxies); |
| 187 | } |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 188 | |
| 189 | if (config.has_kerberos_config()) { |
| 190 | if (config.kerberos_config().enabled()) { |
| 191 | // Set the environment variables that allow libcurl to use the existing |
| 192 | // kerberos ticket for proxy authentication. The files to which the env |
| 193 | // variables point to are maintained by the parent process. |
| 194 | setenv(kKrb5ConfEnvKey, config.kerberos_config().krb5conf_path().c_str(), |
| 195 | /* overwrite = */ 1); |
| 196 | setenv(kKrb5CCEnvKey, config.kerberos_config().krb5cc_path().c_str(), |
| 197 | /* overwrite = */ 1); |
| 198 | } else { |
| 199 | unsetenv(kKrb5ConfEnvKey); |
| 200 | unsetenv(kKrb5CCEnvKey); |
| 201 | } |
| 202 | } |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | bool ServerProxy::HandleSignal(const struct signalfd_siginfo& siginfo) { |
| 206 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 207 | std::move(quit_closure_)); |
| 208 | return true; |
| 209 | } |
| 210 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 211 | int ServerProxy::GetStdinPipe() { |
| 212 | return STDIN_FILENO; |
| 213 | } |
| 214 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 215 | int ServerProxy::GetStdoutPipe() { |
| 216 | return STDOUT_FILENO; |
| 217 | } |
| 218 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 219 | void ServerProxy::CreateListeningSocket() { |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 220 | listening_fd_ = std::make_unique<patchpanel::Socket>( |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 221 | AF_INET, SOCK_STREAM | SOCK_NONBLOCK); |
| 222 | |
| 223 | struct sockaddr_in addr = {0}; |
| 224 | addr.sin_family = AF_INET; |
| 225 | addr.sin_port = htons(listening_port_); |
| 226 | addr.sin_addr.s_addr = listening_addr_; |
| 227 | if (!listening_fd_->Bind((const struct sockaddr*)&addr, sizeof(addr))) { |
| 228 | LOG(ERROR) << "Cannot bind source socket" << std::endl; |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | if (!listening_fd_->Listen(kMaxConn)) { |
| 233 | LOG(ERROR) << "Cannot listen on source socket." << std::endl; |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | fd_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 238 | listening_fd_->fd(), base::BindRepeating(&ServerProxy::OnConnectionAccept, |
| 239 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 242 | void ServerProxy::OnConnectionAccept() { |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 243 | struct sockaddr_storage client_src = {}; |
| 244 | socklen_t sockaddr_len = sizeof(client_src); |
| 245 | if (auto client_conn = |
| 246 | listening_fd_->Accept((struct sockaddr*)&client_src, &sockaddr_len)) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 247 | auto connect_job = std::make_unique<ProxyConnectJob>( |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 248 | std::move(client_conn), system_credentials_, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 249 | base::BindOnce(&ServerProxy::ResolveProxy, base::Unretained(this)), |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 250 | base::BindOnce(&ServerProxy::AuthenticationRequired, |
| 251 | base::Unretained(this)), |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 252 | base::BindOnce(&ServerProxy::OnConnectionSetupFinished, |
| 253 | base::Unretained(this))); |
| 254 | if (connect_job->Start()) |
| 255 | pending_connect_jobs_[connect_job.get()] = std::move(connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 256 | } |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 257 | // Cleanup any defunct forwarders. |
| 258 | // TODO(acostinas, chromium:1064536) Monitor the client and server sockets |
| 259 | // and remove the corresponding SocketForwarder when a socket closes. |
| 260 | for (auto it = forwarders_.begin(); it != forwarders_.end(); ++it) { |
| 261 | if (!(*it)->IsRunning() && (*it)->HasBeenStarted()) |
| 262 | it = forwarders_.erase(it); |
| 263 | } |
| 264 | } |
| 265 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 266 | void ServerProxy::OnProxyResolved(const std::string& target_url, |
| 267 | const std::list<std::string>& proxy_servers) { |
| 268 | auto callbacks = std::move(pending_proxy_resolution_requests_[target_url]); |
| 269 | pending_proxy_resolution_requests_.erase(target_url); |
| 270 | |
| 271 | for (auto& callback : callbacks) |
| 272 | std::move(callback).Run(proxy_servers); |
| 273 | } |
| 274 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 275 | void ServerProxy::OnConnectionSetupFinished( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 276 | std::unique_ptr<patchpanel::SocketForwarder> fwd, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 277 | ProxyConnectJob* connect_job) { |
| 278 | if (fwd) { |
| 279 | // The connection was set up successfully. |
| 280 | forwarders_.emplace_back(std::move(fwd)); |
| 281 | } |
| 282 | pending_connect_jobs_.erase(connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 285 | } // namespace system_proxy |