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 | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 43 | |
| 44 | // Returns the URL encoded value of |text|. It also verifies if the string was |
| 45 | // already encoded and, if true it returns it unmodified. |
| 46 | std::string UrlEncode(const std::string& text) { |
| 47 | if (text == brillo::data_encoding::UrlDecode(text.c_str())) |
| 48 | return brillo::data_encoding::UrlEncode(text.c_str(), false); |
| 49 | return text; |
| 50 | } |
| 51 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 52 | } // namespace |
| 53 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 54 | ServerProxy::ServerProxy(base::OnceClosure quit_closure) |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 55 | : quit_closure_(std::move(quit_closure)), weak_ptr_factory_(this) {} |
| 56 | ServerProxy::~ServerProxy() = default; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 57 | |
| 58 | void ServerProxy::Init() { |
| 59 | // Start listening for input. |
| 60 | stdin_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 61 | GetStdinPipe(), base::Bind(&ServerProxy::HandleStdinReadable, |
| 62 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 63 | |
| 64 | // Handle termination signals. |
| 65 | signal_handler_.Init(); |
| 66 | for (int signal : {SIGINT, SIGTERM, SIGHUP, SIGQUIT}) { |
| 67 | signal_handler_.RegisterHandler( |
| 68 | signal, base::BindRepeating(&ServerProxy::HandleSignal, |
| 69 | base::Unretained(this))); |
| 70 | } |
| 71 | } |
| 72 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 73 | void ServerProxy::ResolveProxy(const std::string& target_url, |
| 74 | OnProxyResolvedCallback callback) { |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 75 | auto it = pending_proxy_resolution_requests_.find(target_url); |
| 76 | if (it != pending_proxy_resolution_requests_.end()) { |
| 77 | it->second.push_back(std::move(callback)); |
| 78 | return; |
| 79 | } |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 80 | worker::ProxyResolutionRequest proxy_request; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 81 | proxy_request.set_target_url(target_url); |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 82 | worker::WorkerRequest request; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 83 | *request.mutable_proxy_resolution_request() = proxy_request; |
| 84 | if (!WriteProtobuf(GetStdoutPipe(), request)) { |
| 85 | LOG(ERROR) << "Failed to send proxy resolution request for url: " |
| 86 | << target_url; |
| 87 | std::move(callback).Run({brillo::http::kDirectProxy}); |
| 88 | return; |
| 89 | } |
| 90 | pending_proxy_resolution_requests_[target_url].push_back(std::move(callback)); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 91 | } |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 92 | |
| 93 | void ServerProxy::HandleStdinReadable() { |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 94 | worker::WorkerConfigs config; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 95 | if (!ReadProtobuf(GetStdinPipe(), &config)) { |
| 96 | LOG(ERROR) << "Error decoding protobuf configurations." << std::endl; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 97 | return; |
| 98 | } |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 99 | |
| 100 | if (config.has_credentials()) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 101 | const std::string username = UrlEncode(config.credentials().username()); |
| 102 | const std::string password = UrlEncode(config.credentials().password()); |
| 103 | credentials_ = base::JoinString({username.c_str(), password.c_str()}, ":"); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (config.has_listening_address()) { |
| 107 | if (listening_addr_ != 0) { |
| 108 | LOG(ERROR) |
| 109 | << "Failure to set configurations: listening port was already set." |
| 110 | << std::endl; |
| 111 | return; |
| 112 | } |
| 113 | listening_addr_ = config.listening_address().addr(); |
| 114 | listening_port_ = config.listening_address().port(); |
| 115 | CreateListeningSocket(); |
| 116 | } |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 117 | |
| 118 | if (config.has_proxy_resolution_reply()) { |
| 119 | std::list<std::string> proxies; |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 120 | const worker::ProxyResolutionReply& reply = config.proxy_resolution_reply(); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 121 | for (auto const& proxy : reply.proxy_servers()) |
| 122 | proxies.push_back(proxy); |
| 123 | |
| 124 | OnProxyResolved(reply.target_url(), proxies); |
| 125 | } |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 126 | |
| 127 | if (config.has_kerberos_config()) { |
| 128 | if (config.kerberos_config().enabled()) { |
| 129 | // Set the environment variables that allow libcurl to use the existing |
| 130 | // kerberos ticket for proxy authentication. The files to which the env |
| 131 | // variables point to are maintained by the parent process. |
| 132 | setenv(kKrb5ConfEnvKey, config.kerberos_config().krb5conf_path().c_str(), |
| 133 | /* overwrite = */ 1); |
| 134 | setenv(kKrb5CCEnvKey, config.kerberos_config().krb5cc_path().c_str(), |
| 135 | /* overwrite = */ 1); |
| 136 | } else { |
| 137 | unsetenv(kKrb5ConfEnvKey); |
| 138 | unsetenv(kKrb5CCEnvKey); |
| 139 | } |
| 140 | } |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bool ServerProxy::HandleSignal(const struct signalfd_siginfo& siginfo) { |
| 144 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 145 | std::move(quit_closure_)); |
| 146 | return true; |
| 147 | } |
| 148 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 149 | int ServerProxy::GetStdinPipe() { |
| 150 | return STDIN_FILENO; |
| 151 | } |
| 152 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 153 | int ServerProxy::GetStdoutPipe() { |
| 154 | return STDOUT_FILENO; |
| 155 | } |
| 156 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 157 | void ServerProxy::CreateListeningSocket() { |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 158 | listening_fd_ = std::make_unique<patchpanel::Socket>( |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 159 | AF_INET, SOCK_STREAM | SOCK_NONBLOCK); |
| 160 | |
| 161 | struct sockaddr_in addr = {0}; |
| 162 | addr.sin_family = AF_INET; |
| 163 | addr.sin_port = htons(listening_port_); |
| 164 | addr.sin_addr.s_addr = listening_addr_; |
| 165 | if (!listening_fd_->Bind((const struct sockaddr*)&addr, sizeof(addr))) { |
| 166 | LOG(ERROR) << "Cannot bind source socket" << std::endl; |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | if (!listening_fd_->Listen(kMaxConn)) { |
| 171 | LOG(ERROR) << "Cannot listen on source socket." << std::endl; |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | fd_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 176 | listening_fd_->fd(), base::BindRepeating(&ServerProxy::OnConnectionAccept, |
| 177 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 180 | void ServerProxy::OnConnectionAccept() { |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 181 | struct sockaddr_storage client_src = {}; |
| 182 | socklen_t sockaddr_len = sizeof(client_src); |
| 183 | if (auto client_conn = |
| 184 | listening_fd_->Accept((struct sockaddr*)&client_src, &sockaddr_len)) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 185 | auto connect_job = std::make_unique<ProxyConnectJob>( |
| 186 | std::move(client_conn), credentials_, |
| 187 | base::BindOnce(&ServerProxy::ResolveProxy, base::Unretained(this)), |
| 188 | base::BindOnce(&ServerProxy::OnConnectionSetupFinished, |
| 189 | base::Unretained(this))); |
| 190 | if (connect_job->Start()) |
| 191 | pending_connect_jobs_[connect_job.get()] = std::move(connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 192 | } |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 193 | // Cleanup any defunct forwarders. |
| 194 | // TODO(acostinas, chromium:1064536) Monitor the client and server sockets |
| 195 | // and remove the corresponding SocketForwarder when a socket closes. |
| 196 | for (auto it = forwarders_.begin(); it != forwarders_.end(); ++it) { |
| 197 | if (!(*it)->IsRunning() && (*it)->HasBeenStarted()) |
| 198 | it = forwarders_.erase(it); |
| 199 | } |
| 200 | } |
| 201 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 202 | void ServerProxy::OnProxyResolved(const std::string& target_url, |
| 203 | const std::list<std::string>& proxy_servers) { |
| 204 | auto callbacks = std::move(pending_proxy_resolution_requests_[target_url]); |
| 205 | pending_proxy_resolution_requests_.erase(target_url); |
| 206 | |
| 207 | for (auto& callback : callbacks) |
| 208 | std::move(callback).Run(proxy_servers); |
| 209 | } |
| 210 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 211 | void ServerProxy::OnConnectionSetupFinished( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 212 | std::unique_ptr<patchpanel::SocketForwarder> fwd, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 213 | ProxyConnectJob* connect_job) { |
| 214 | if (fwd) { |
| 215 | // The connection was set up successfully. |
| 216 | forwarders_.emplace_back(std::move(fwd)); |
| 217 | } |
| 218 | pending_connect_jobs_.erase(connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 221 | } // namespace system_proxy |